xref: /titanic_41/usr/src/cmd/ssh/libssh/common/compat.c (revision 936b7af69172dce89b577831f79c0e18d15e854b)
1 /*
2  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 /*
25  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include "includes.h"
30 RCSID("$OpenBSD: compat.c,v 1.65 2002/09/27 10:42:09 mickey Exp $");
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include "buffer.h"
35 #include "packet.h"
36 #include "xmalloc.h"
37 #include "compat.h"
38 #include "log.h"
39 #include "match.h"
40 
41 int compat13 = 0;
42 int compat20 = 0;
43 int datafellows = 0;
44 
45 void
46 enable_compat20(void)
47 {
48 	debug("Enabling compatibility mode for protocol 2.0");
49 	compat20 = 1;
50 }
51 void
52 enable_compat13(void)
53 {
54 	debug("Enabling compatibility mode for protocol 1.3");
55 	compat13 = 1;
56 }
57 /* datafellows bug compatibility */
58 void
59 compat_datafellows(const char *version)
60 {
61 	int i;
62 	static struct {
63 		char	*pat;
64 		int	bugs;
65 	} check[] = {
66 		{ "OpenSSH-2.0*,"
67 		  "OpenSSH-2.1*,"
68 		  "OpenSSH_2.1*,"
69 		  "OpenSSH_2.2*",	SSH_OLD_SESSIONID|SSH_BUG_BANNER|
70 					SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
71 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
72 		{ "OpenSSH_2.3.0*",	SSH_BUG_BANNER|SSH_BUG_BIGENDIANAES|
73 					SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
74 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
75 		{ "OpenSSH_2.3.*",	SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
76 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
77 					SSH_OLD_FORWARD_ADDR},
78 		{ "OpenSSH_2.5.0p1*,"
79 		  "OpenSSH_2.5.1p1*",
80 					SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
81 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
82 					SSH_OLD_GSSAPI|SSH_OLD_FORWARD_ADDR},
83 		{ "OpenSSH_2.5.0*,"
84 		  "OpenSSH_2.5.1*,"
85 		  "OpenSSH_2.5.2*",	SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
86 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
87 		{ "OpenSSH_2.5.3*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
88 					SSH_OLD_FORWARD_ADDR},
89 		{ "OpenSSH_2.9p*",      SSH_BUG_EXTEOF|SSH_OLD_GSSAPI|
90 					SSH_BUG_GSSKEX_HOSTKEY|SSH_OLD_FORWARD_ADDR},
91 		{ "OpenSSH_2.*,"
92 		  "OpenSSH_3.0*,"
93 		  "OpenSSH_3.1*",       SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR|
94 		  			SSH_OLD_GSSAPI|SSH_BUG_GSSAPI_BER|
95 					SSH_BUG_GSSKEX_HOSTKEY},
96 		{ "OpenSSH_3.2*,"
97 		  "OpenSSH_3.3*,"
98 		  "OpenSSH_3.4*,"
99 		  "OpenSSH_3.5*",       SSH_BUG_GSSAPI_BER|SSH_OLD_GSSAPI|
100 					SSH_BUG_GSSKEX_HOSTKEY|SSH_OLD_FORWARD_ADDR},
101 		{ "OpenSSH_3.6*,"
102 		  "OpenSSH_3.7*,"
103 		  "OpenSSH_3.8*",	SSH_BUG_GSSKEX_HOSTKEY|SSH_OLD_FORWARD_ADDR},
104 		{ "OpenSSH_3.*",	SSH_OLD_FORWARD_ADDR},
105 		{ "OpenSSH*",		0 },
106 		{ "Sun_SSH_1.0.*",	SSH_BUG_NOREKEY|
107 					SSH_BUG_LOCALES_NOT_LANGTAGS|SSH_OLD_FORWARD_ADDR},
108 		{ "Sun_SSH_1.0*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
109 					SSH_BUG_LOCALES_NOT_LANGTAGS|SSH_OLD_FORWARD_ADDR},
110 		{ "Sun_SSH_1.1*",	SSH_OLD_FORWARD_ADDR},
111 		{ "Sun_SSH_1.2*",	0 },
112 		{ "*MindTerm*",		0 },
113 		{ "2.1.0*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
114 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
115 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
116 					SSH_BUG_FIRSTKEX },
117 		{ "2.1 *",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
118 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
119 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
120 					SSH_BUG_FIRSTKEX },
121 		{ "2.0.13*,"
122 		  "2.0.14*,"
123 		  "2.0.15*,"
124 		  "2.0.16*,"
125 		  "2.0.17*,"
126 		  "2.0.18*,"
127 		  "2.0.19*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
128 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
129 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
130 					SSH_BUG_PKOK|SSH_BUG_RSASIGMD5|
131 					SSH_BUG_HBSERVICE|SSH_BUG_OPENFAILURE|
132 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
133 		{ "2.0.11*,"
134 		  "2.0.12*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
135 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
136 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
137 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
138 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
139 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
140 		{ "2.0.*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
141 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
142 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
143 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
144 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
145 					SSH_BUG_DERIVEKEY|SSH_BUG_DUMMYCHAN|
146 					SSH_BUG_FIRSTKEX },
147 		{ "2.2.0*,"
148 		  "2.3.0*",		SSH_BUG_HMAC|SSH_BUG_DEBUG|
149 					SSH_BUG_RSASIGMD5|SSH_BUG_FIRSTKEX },
150 		{ "2.3.*",		SSH_BUG_DEBUG|SSH_BUG_RSASIGMD5|
151 					SSH_BUG_FIRSTKEX },
152 		{ "2.4",		SSH_OLD_SESSIONID },	/* Van Dyke */
153 		{ "2.*",		SSH_BUG_DEBUG|SSH_BUG_FIRSTKEX|
154 					SSH_BUG_RFWD_ADDR},
155 		{ "3.0.*",		SSH_BUG_DEBUG },
156 		{ "3.0 SecureCRT*",	SSH_OLD_SESSIONID },
157 		{ "1.7 SecureFX*",	SSH_OLD_SESSIONID },
158 		{ "1.2.18*,"
159 		  "1.2.19*,"
160 		  "1.2.20*,"
161 		  "1.2.21*,"
162 		  "1.2.22*",		SSH_BUG_IGNOREMSG|SSH_BUG_K5USER },
163 		{ "1.3.2*",		/* F-Secure */
164 					SSH_BUG_IGNOREMSG|SSH_BUG_K5USER },
165 		{ "1.2.1*,"
166 		  "1.2.2*,"
167 		  "1.2.3*",		SSH_BUG_K5USER },
168 		{ "*SSH Compatible Server*",			/* Netscreen */
169 					SSH_BUG_PASSWORDPAD },
170 		{ "*OSU_0*,"
171 		  "OSU_1.0*,"
172 		  "OSU_1.1*,"
173 		  "OSU_1.2*,"
174 		  "OSU_1.3*,"
175 		  "OSU_1.4*,"
176 		  "OSU_1.5alpha1*,"
177 		  "OSU_1.5alpha2*,"
178 		  "OSU_1.5alpha3*",	SSH_BUG_PASSWORDPAD },
179 		{ "*SSH_Version_Mapper*",
180 					SSH_BUG_SCANNER },
181 		{ "Probe-*",
182 					SSH_BUG_PROBE },
183 		{ NULL,			0 }
184 	};
185 
186 	/* process table, return first match */
187 	for (i = 0; check[i].pat; i++) {
188 		if (match_pattern_list(version, check[i].pat,
189 		    strlen(check[i].pat), 0) == 1) {
190 			debug("match: %s pat %s", version, check[i].pat);
191 			datafellows = check[i].bugs;
192 			return;
193 		}
194 	}
195 	debug("no match: %s", version);
196 }
197 
198 #define	SEP	","
199 int
200 proto_spec(const char *spec)
201 {
202 	char *s, *p, *q;
203 	int ret = SSH_PROTO_UNKNOWN;
204 
205 	if (spec == NULL)
206 		return ret;
207 	q = s = xstrdup(spec);
208 	for ((p = strsep(&q, SEP)); p && *p != '\0'; (p = strsep(&q, SEP))) {
209 		switch (atoi(p)) {
210 		case 1:
211 			if (ret == SSH_PROTO_UNKNOWN)
212 				ret |= SSH_PROTO_1_PREFERRED;
213 			ret |= SSH_PROTO_1;
214 			break;
215 		case 2:
216 			ret |= SSH_PROTO_2;
217 			break;
218 		default:
219 			log("ignoring bad proto spec: '%s'.", p);
220 			break;
221 		}
222 	}
223 	xfree(s);
224 	return ret;
225 }
226 
227 char *
228 compat_cipher_proposal(char *cipher_prop)
229 {
230 	Buffer b;
231 	char *orig_prop, *fix_ciphers;
232 	char *cp, *tmp;
233 
234 	if (!(datafellows & SSH_BUG_BIGENDIANAES))
235 		return(cipher_prop);
236 
237 	buffer_init(&b);
238 	tmp = orig_prop = xstrdup(cipher_prop);
239 	while ((cp = strsep(&tmp, ",")) != NULL) {
240 		if (strncmp(cp, "aes", 3) != 0) {
241 			if (buffer_len(&b) > 0)
242 				buffer_append(&b, ",", 1);
243 			buffer_append(&b, cp, strlen(cp));
244 		}
245 	}
246 	buffer_append(&b, "\0", 1);
247 	fix_ciphers = xstrdup(buffer_ptr(&b));
248 	buffer_free(&b);
249 	xfree(orig_prop);
250 	debug2("Original cipher proposal: %s", cipher_prop);
251 	debug2("Compat cipher proposal: %s", fix_ciphers);
252 	if (!*fix_ciphers)
253 		fatal("No available ciphers found.");
254 
255 	return(fix_ciphers);
256 }
257