• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Dropbear - a SSH2 server
3  * SSH client implementation
4  *
5  * Copyright (c) 2002,2003 Matt Johnston
6  * Copyright (c) 2004 by Mihnea Stoenescu
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE. */
26 
27 #include "algo.h"
28 #include "dbutil.h"
29 
30 
31 /*
32  * The chosen [encryption | MAC | compression] algorithm to each
33  * direction MUST be the first algorithm  on the client's list
34  * that is also on the server's list.
35  */
cli_buf_match_algo(buffer * buf,algo_type localalgos[],int * goodguess)36 algo_type * cli_buf_match_algo(buffer* buf, algo_type localalgos[],
37 		int *goodguess) {
38 
39 	unsigned char * algolist = NULL;
40 	unsigned char * remotealgos[MAX_PROPOSED_ALGO];
41 	unsigned int len;
42 	unsigned int count, i, j;
43 	algo_type * ret = NULL;
44 
45 	*goodguess = 0;
46 
47 	/* get the comma-separated list from the buffer ie "algo1,algo2,algo3" */
48 	algolist = buf_getstring(buf, &len);
49 	TRACE(("cli_buf_match_algo: %s", algolist))
50 	if (len > MAX_PROPOSED_ALGO*(MAX_NAME_LEN+1)) {
51 		goto out; /* just a sanity check, no other use */
52 	}
53 
54 	/* remotealgos will contain a list of the strings parsed out */
55 	/* We will have at least one string (even if it's just "") */
56 	remotealgos[0] = algolist;
57 	count = 1;
58 	/* Iterate through, replacing ','s with NULs, to split it into
59 	 * words. */
60 	for (i = 0; i < len; i++) {
61 		if (algolist[i] == '\0') {
62 			/* someone is trying something strange */
63 			goto out;
64 		}
65 		if (algolist[i] == ',') {
66 			algolist[i] = '\0';
67 			remotealgos[count] = &algolist[i+1];
68 			count++;
69 		}
70 		if (count == MAX_PROPOSED_ALGO) {
71 			break;
72 		}
73 	}
74 
75 	/* iterate and find the first match */
76 
77 	for (j = 0; localalgos[j].name != NULL; j++) {
78 		if (localalgos[j].usable) {
79 		len = strlen(localalgos[j].name);
80 			for (i = 0; i < count; i++) {
81 				if (len == strlen(remotealgos[i])
82 						&& strncmp(localalgos[j].name,
83 							remotealgos[i], len) == 0) {
84 					if (i == 0 && j == 0) {
85 						/* was a good guess */
86 						*goodguess = 1;
87 					}
88 					ret = &localalgos[j];
89 					goto out;
90 				}
91 			}
92 		}
93 	}
94 
95 out:
96 	m_free(algolist);
97 	return ret;
98 }
99 
100