xref: /titanic_44/usr/src/cmd/ssh/libssh/common/compat.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 2004 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},
72 		{ "OpenSSH_2.3.0*",	SSH_BUG_BANNER|SSH_BUG_BIGENDIANAES|
73 					SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
74 					SSH_BUG_EXTEOF},
75 		{ "OpenSSH_2.3.*",	SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
76 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF},
77 		{ "OpenSSH_2.5.0p1*,"
78 		  "OpenSSH_2.5.1p1*",
79 					SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
80 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
81 					SSH_OLD_GSSAPI},
82 		{ "OpenSSH_2.5.0*,"
83 		  "OpenSSH_2.5.1*,"
84 		  "OpenSSH_2.5.2*",	SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
85 					SSH_BUG_EXTEOF},
86 		{ "OpenSSH_2.5.3*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF},
87 		{ "OpenSSH_2.9p*",      SSH_BUG_EXTEOF|SSH_OLD_GSSAPI|
88 					SSH_BUG_GSSKEX_HOSTKEY},
89 		{ "OpenSSH_2.*,"
90 		  "OpenSSH_3.0*,"
91 		  "OpenSSH_3.1*",       SSH_BUG_EXTEOF|SSH_OLD_GSSAPI|
92 					SSH_BUG_GSSAPI_BER|
93 					SSH_BUG_GSSKEX_HOSTKEY},
94 		{ "OpenSSH_3.2*,"
95 		  "OpenSSH_3.3*,"
96 		  "OpenSSH_3.4*,"
97 		  "OpenSSH_3.5*",       SSH_BUG_GSSAPI_BER|SSH_OLD_GSSAPI|
98 					SSH_BUG_GSSKEX_HOSTKEY},
99 		{ "OpenSSH_3.6*,"
100 		  "OpenSSH_3.7*,"
101 		  "OpenSSH_3.8*",       SSH_BUG_GSSKEX_HOSTKEY},
102 		{ "OpenSSH*",		0 },
103 		{ "Sun_SSH_1.0.*",	SSH_BUG_NOREKEY|
104 					SSH_BUG_DFLT_CLNT_EXIT_0|
105 					SSH_BUG_LOCALES_NOT_LANGTAGS},
106 		{ "Sun_SSH_1.0*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
107 					SSH_BUG_DFLT_CLNT_EXIT_0|
108 					SSH_BUG_LOCALES_NOT_LANGTAGS},
109 		{ "*MindTerm*",		0 },
110 		{ "2.1.0*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
111 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
112 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
113 					SSH_BUG_FIRSTKEX },
114 		{ "2.1 *",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
115 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
116 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
117 					SSH_BUG_FIRSTKEX },
118 		{ "2.0.13*,"
119 		  "2.0.14*,"
120 		  "2.0.15*,"
121 		  "2.0.16*,"
122 		  "2.0.17*,"
123 		  "2.0.18*,"
124 		  "2.0.19*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
125 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
126 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
127 					SSH_BUG_PKOK|SSH_BUG_RSASIGMD5|
128 					SSH_BUG_HBSERVICE|SSH_BUG_OPENFAILURE|
129 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
130 		{ "2.0.11*,"
131 		  "2.0.12*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
132 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
133 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
134 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
135 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
136 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
137 		{ "2.0.*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
138 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
139 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
140 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
141 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
142 					SSH_BUG_DERIVEKEY|SSH_BUG_DUMMYCHAN|
143 					SSH_BUG_FIRSTKEX },
144 		{ "2.2.0*,"
145 		  "2.3.0*",		SSH_BUG_HMAC|SSH_BUG_DEBUG|
146 					SSH_BUG_RSASIGMD5|SSH_BUG_FIRSTKEX },
147 		{ "2.3.*",		SSH_BUG_DEBUG|SSH_BUG_RSASIGMD5|
148 					SSH_BUG_FIRSTKEX },
149 		{ "2.4",		SSH_OLD_SESSIONID },	/* Van Dyke */
150 		{ "2.*",		SSH_BUG_DEBUG|SSH_BUG_FIRSTKEX },
151 		{ "3.0.*",		SSH_BUG_DEBUG },
152 		{ "3.0 SecureCRT*",	SSH_OLD_SESSIONID },
153 		{ "1.7 SecureFX*",	SSH_OLD_SESSIONID },
154 		{ "1.2.18*,"
155 		  "1.2.19*,"
156 		  "1.2.20*,"
157 		  "1.2.21*,"
158 		  "1.2.22*",		SSH_BUG_IGNOREMSG|SSH_BUG_K5USER },
159 		{ "1.3.2*",		/* F-Secure */
160 					SSH_BUG_IGNOREMSG|SSH_BUG_K5USER },
161 		{ "1.2.1*,"
162 		  "1.2.2*,"
163 		  "1.2.3*",		SSH_BUG_K5USER },
164 		{ "*SSH Compatible Server*",			/* Netscreen */
165 					SSH_BUG_PASSWORDPAD },
166 		{ "*OSU_0*,"
167 		  "OSU_1.0*,"
168 		  "OSU_1.1*,"
169 		  "OSU_1.2*,"
170 		  "OSU_1.3*,"
171 		  "OSU_1.4*,"
172 		  "OSU_1.5alpha1*,"
173 		  "OSU_1.5alpha2*,"
174 		  "OSU_1.5alpha3*",	SSH_BUG_PASSWORDPAD },
175 		{ "*SSH_Version_Mapper*",
176 					SSH_BUG_SCANNER },
177 		{ "Probe-*",
178 					SSH_BUG_PROBE },
179 		{ NULL,			0 }
180 	};
181 
182 	/* process table, return first match */
183 	for (i = 0; check[i].pat; i++) {
184 		if (match_pattern_list(version, check[i].pat,
185 		    strlen(check[i].pat), 0) == 1) {
186 			debug("match: %s pat %s", version, check[i].pat);
187 			datafellows = check[i].bugs;
188 			return;
189 		}
190 	}
191 	debug("no match: %s", version);
192 }
193 
194 #define	SEP	","
195 int
196 proto_spec(const char *spec)
197 {
198 	char *s, *p, *q;
199 	int ret = SSH_PROTO_UNKNOWN;
200 
201 	if (spec == NULL)
202 		return ret;
203 	q = s = xstrdup(spec);
204 	for ((p = strsep(&q, SEP)); p && *p != '\0'; (p = strsep(&q, SEP))) {
205 		switch (atoi(p)) {
206 		case 1:
207 			if (ret == SSH_PROTO_UNKNOWN)
208 				ret |= SSH_PROTO_1_PREFERRED;
209 			ret |= SSH_PROTO_1;
210 			break;
211 		case 2:
212 			ret |= SSH_PROTO_2;
213 			break;
214 		default:
215 			log("ignoring bad proto spec: '%s'.", p);
216 			break;
217 		}
218 	}
219 	xfree(s);
220 	return ret;
221 }
222 
223 char *
224 compat_cipher_proposal(char *cipher_prop)
225 {
226 	Buffer b;
227 	char *orig_prop, *fix_ciphers;
228 	char *cp, *tmp;
229 
230 	if (!(datafellows & SSH_BUG_BIGENDIANAES))
231 		return(cipher_prop);
232 
233 	buffer_init(&b);
234 	tmp = orig_prop = xstrdup(cipher_prop);
235 	while ((cp = strsep(&tmp, ",")) != NULL) {
236 		if (strncmp(cp, "aes", 3) != 0) {
237 			if (buffer_len(&b) > 0)
238 				buffer_append(&b, ",", 1);
239 			buffer_append(&b, cp, strlen(cp));
240 		}
241 	}
242 	buffer_append(&b, "\0", 1);
243 	fix_ciphers = xstrdup(buffer_ptr(&b));
244 	buffer_free(&b);
245 	xfree(orig_prop);
246 	debug2("Original cipher proposal: %s", cipher_prop);
247 	debug2("Compat cipher proposal: %s", fix_ciphers);
248 	if (!*fix_ciphers)
249 		fatal("No available ciphers found.");
250 
251 	return(fix_ciphers);
252 }
253