annotate svr-algo.c @ 740:3062da90dab8 kexguess

Add kexguess2 behaviour
author Matt Johnston <matt@ucc.asn.au>
date Fri, 29 Mar 2013 23:29:48 +0800
parents 870c63519757
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
74
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
1 /*
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
2 * Dropbear - a SSH2 server
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
3 * SSH client implementation
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
4 *
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
5 * Copyright (c) 2002,2003 Matt Johnston
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
6 * Copyright (c) 2004 by Mihnea Stoenescu
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
7 * All rights reserved.
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
8 *
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
10 * of this software and associated documentation files (the "Software"), to deal
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
11 * in the Software without restriction, including without limitation the rights
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
13 * copies of the Software, and to permit persons to whom the Software is
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
14 * furnished to do so, subject to the following conditions:
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
15 *
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
16 * The above copyright notice and this permission notice shall be included in
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
17 * all copies or substantial portions of the Software.
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
18 *
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
25 * SOFTWARE. */
e3adf4cf5465 License boilerplate etc, add Mihnea as an author to some of the files
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
26
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include "algo.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include "dbutil.h"
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 /* match the first algorithm in the comma-separated list in buf which is
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 * also in localalgos[], or return NULL on failure.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 * (*goodguess) is set to 1 if the preferred client/server algos match,
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 * 0 otherwise. This is used for checking if the kexalgo/hostkeyalgos are
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * guessed correctly */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 algo_type * svr_buf_match_algo(buffer* buf, algo_type localalgos[],
740
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
36 enum kexguess2_used *kexguess2, int *goodguess)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 unsigned char * algolist = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 unsigned char * remotealgos[MAX_PROPOSED_ALGO];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 unsigned int len;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 unsigned int count, i, j;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 algo_type * ret = NULL;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
740
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
45 if (goodguess) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
46 *goodguess = 0;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
47 }
26
0969767bca0d snapshot of stuff
Matt Johnston <matt@ucc.asn.au>
parents: 4
diff changeset
48
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 /* get the comma-separated list from the buffer ie "algo1,algo2,algo3" */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 algolist = buf_getstring(buf, &len);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 /* Debug this */
165
0cfba3034be5 Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
Matt Johnston <matt@ucc.asn.au>
parents: 74
diff changeset
52 TRACE(("buf_match_algo: %s", algolist))
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if (len > MAX_PROPOSED_ALGO*(MAX_NAME_LEN+1)) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 goto out; /* just a sanity check, no other use */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 /* remotealgos will contain a list of the strings parsed out */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 /* We will have at least one string (even if it's just "") */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 remotealgos[0] = algolist;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 count = 1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 /* Iterate through, replacing ','s with NULs, to split it into
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 * words. */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 for (i = 0; i < len; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if (algolist[i] == '\0') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 /* someone is trying something strange */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 goto out;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if (algolist[i] == ',') {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 algolist[i] = '\0';
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 remotealgos[count] = &algolist[i+1];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 count++;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 }
611
870c63519757 Change comparison to be more paranoid (and perhaps avoid Klocwork false
Matt Johnston <matt@ucc.asn.au>
parents: 165
diff changeset
73 if (count >= MAX_PROPOSED_ALGO) {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 break;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
740
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
78 if (kexguess2 && *kexguess2 == KEXGUESS2_LOOK) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
79 for (i = 0; i < count; i++)
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
80 {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
81 if (strcmp(remotealgos[i], KEXGUESS2_ALGO_NAME) == 0) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
82 *kexguess2 = KEXGUESS2_YES;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
83 break;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
84 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
85 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
86 if (*kexguess2 == KEXGUESS2_LOOK) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
87 *kexguess2 = KEXGUESS2_NO;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
88 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
89 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
90
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /* iterate and find the first match */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 for (i = 0; i < count; i++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 len = strlen(remotealgos[i]);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 for (j = 0; localalgos[j].name != NULL; j++) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 if (localalgos[j].usable) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 if (len == strlen(localalgos[j].name) &&
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 strncmp(localalgos[j].name, remotealgos[i], len) == 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 /* set if it was a good guess */
740
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
101 if (goodguess && kexguess2) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
102 if (*kexguess2 == KEXGUESS2_YES) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
103 if (i == 0) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
104 *goodguess = 1;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
105 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
106
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
107 } else {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
108 if (i == 0 && j == 0) {
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
109 *goodguess = 1;
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
110 }
3062da90dab8 Add kexguess2 behaviour
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
111 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 /* set the algo to return */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 ret = &localalgos[j];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 goto out;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 out:
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 m_free(algolist);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 return ret;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 }