xref: /freebsd/crypto/openssh/compat.c (revision b1f9167f94059fd55c630891d359bcff987bd7eb)
1 /* $OpenBSD: compat.c,v 1.82 2013/12/30 23:52:27 djm Exp $ */
2 /* $FreeBSD$ */
3 /*
4  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 __RCSID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 
36 #include "xmalloc.h"
37 #include "buffer.h"
38 #include "packet.h"
39 #include "compat.h"
40 #include "log.h"
41 #include "match.h"
42 
43 int compat13 = 0;
44 int compat20 = 0;
45 int datafellows = 0;
46 
47 void
48 enable_compat20(void)
49 {
50 	if (compat20)
51 		return;
52 	debug("Enabling compatibility mode for protocol 2.0");
53 	compat20 = 1;
54 }
55 void
56 enable_compat13(void)
57 {
58 	debug("Enabling compatibility mode for protocol 1.3");
59 	compat13 = 1;
60 }
61 /* datafellows bug compatibility */
62 void
63 compat_datafellows(const char *version)
64 {
65 	int i;
66 	static struct {
67 		char	*pat;
68 		int	bugs;
69 	} check[] = {
70 		{ "OpenSSH-2.0*,"
71 		  "OpenSSH-2.1*,"
72 		  "OpenSSH_2.1*,"
73 		  "OpenSSH_2.2*",	SSH_OLD_SESSIONID|SSH_BUG_BANNER|
74 					SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
75 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
76 		{ "OpenSSH_2.3.0*",	SSH_BUG_BANNER|SSH_BUG_BIGENDIANAES|
77 					SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
78 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
79 		{ "OpenSSH_2.3.*",	SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
80 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
81 					SSH_OLD_FORWARD_ADDR},
82 		{ "OpenSSH_2.5.0p1*,"
83 		  "OpenSSH_2.5.1p1*",
84 					SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX|
85 					SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
86 					SSH_OLD_FORWARD_ADDR},
87 		{ "OpenSSH_2.5.0*,"
88 		  "OpenSSH_2.5.1*,"
89 		  "OpenSSH_2.5.2*",	SSH_OLD_DHGEX|SSH_BUG_NOREKEY|
90 					SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
91 		{ "OpenSSH_2.5.3*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
92 					SSH_OLD_FORWARD_ADDR},
93 		{ "OpenSSH_2.*,"
94 		  "OpenSSH_3.0*,"
95 		  "OpenSSH_3.1*",	SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR},
96 		{ "OpenSSH_3.*",	SSH_OLD_FORWARD_ADDR },
97 		{ "Sun_SSH_1.0*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF},
98 		{ "OpenSSH_4*",		0 },
99 		{ "OpenSSH_5*",		SSH_NEW_OPENSSH|SSH_BUG_DYNAMIC_RPORT},
100 		{ "OpenSSH_6.6.1*",	SSH_NEW_OPENSSH},
101 		{ "OpenSSH_6.5*,"
102 		  "OpenSSH_6.6*",	SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD},
103 		{ "OpenSSH*",		SSH_NEW_OPENSSH },
104 		{ "*MindTerm*",		0 },
105 		{ "2.1.0*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
106 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
107 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
108 					SSH_BUG_FIRSTKEX },
109 		{ "2.1 *",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
110 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
111 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
112 					SSH_BUG_FIRSTKEX },
113 		{ "2.0.13*,"
114 		  "2.0.14*,"
115 		  "2.0.15*,"
116 		  "2.0.16*,"
117 		  "2.0.17*,"
118 		  "2.0.18*,"
119 		  "2.0.19*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
120 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
121 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
122 					SSH_BUG_PKOK|SSH_BUG_RSASIGMD5|
123 					SSH_BUG_HBSERVICE|SSH_BUG_OPENFAILURE|
124 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
125 		{ "2.0.11*,"
126 		  "2.0.12*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
127 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
128 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
129 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
130 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
131 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
132 		{ "2.0.*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
133 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
134 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
135 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
136 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
137 					SSH_BUG_DERIVEKEY|SSH_BUG_DUMMYCHAN|
138 					SSH_BUG_FIRSTKEX },
139 		{ "2.2.0*,"
140 		  "2.3.0*",		SSH_BUG_HMAC|SSH_BUG_DEBUG|
141 					SSH_BUG_RSASIGMD5|SSH_BUG_FIRSTKEX },
142 		{ "2.3.*",		SSH_BUG_DEBUG|SSH_BUG_RSASIGMD5|
143 					SSH_BUG_FIRSTKEX },
144 		{ "2.4",		SSH_OLD_SESSIONID },	/* Van Dyke */
145 		{ "2.*",		SSH_BUG_DEBUG|SSH_BUG_FIRSTKEX|
146 					SSH_BUG_RFWD_ADDR },
147 		{ "3.0.*",		SSH_BUG_DEBUG },
148 		{ "3.0 SecureCRT*",	SSH_OLD_SESSIONID },
149 		{ "1.7 SecureFX*",	SSH_OLD_SESSIONID },
150 		{ "1.2.18*,"
151 		  "1.2.19*,"
152 		  "1.2.20*,"
153 		  "1.2.21*,"
154 		  "1.2.22*",		SSH_BUG_IGNOREMSG },
155 		{ "1.3.2*",		/* F-Secure */
156 					SSH_BUG_IGNOREMSG },
157 		{ "*SSH Compatible Server*",			/* Netscreen */
158 					SSH_BUG_PASSWORDPAD },
159 		{ "*OSU_0*,"
160 		  "OSU_1.0*,"
161 		  "OSU_1.1*,"
162 		  "OSU_1.2*,"
163 		  "OSU_1.3*,"
164 		  "OSU_1.4*,"
165 		  "OSU_1.5alpha1*,"
166 		  "OSU_1.5alpha2*,"
167 		  "OSU_1.5alpha3*",	SSH_BUG_PASSWORDPAD },
168 		{ "*SSH_Version_Mapper*",
169 					SSH_BUG_SCANNER },
170 		{ "Probe-*",
171 					SSH_BUG_PROBE },
172 		{ NULL,			0 }
173 	};
174 
175 	/* process table, return first match */
176 	for (i = 0; check[i].pat; i++) {
177 		if (match_pattern_list(version, check[i].pat,
178 		    strlen(check[i].pat), 0) == 1) {
179 			datafellows = check[i].bugs;
180 			debug("match: %s pat %s compat 0x%08x",
181 			    version, check[i].pat, datafellows);
182 			/*
183 			 * Check to see if the remote side is OpenSSH and not
184 			 * HPN.  It is utterly strange to check it from the
185 			 * version string and expose the option that way.
186 			 */
187 			if (strstr(version,"OpenSSH") != NULL &&
188 			    strstr(version,"hpn") == NULL) {
189 				datafellows |= SSH_BUG_LARGEWINDOW;
190 				debug("Remote is not HPN-aware");
191 			}
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 			logit("ignoring bad proto spec: '%s'.", p);
220 			break;
221 		}
222 	}
223 	free(s);
224 	return ret;
225 }
226 
227 /*
228  * Filters a proposal string, excluding any algorithm matching the 'filter'
229  * pattern list.
230  */
231 static char *
232 filter_proposal(char *proposal, const char *filter)
233 {
234 	Buffer b;
235 	char *orig_prop, *fix_prop;
236 	char *cp, *tmp;
237 
238 	buffer_init(&b);
239 	tmp = orig_prop = xstrdup(proposal);
240 	while ((cp = strsep(&tmp, ",")) != NULL) {
241 		if (match_pattern_list(cp, filter, strlen(cp), 0) != 1) {
242 			if (buffer_len(&b) > 0)
243 				buffer_append(&b, ",", 1);
244 			buffer_append(&b, cp, strlen(cp));
245 		} else
246 			debug2("Compat: skipping algorithm \"%s\"", cp);
247 	}
248 	buffer_append(&b, "\0", 1);
249 	fix_prop = xstrdup(buffer_ptr(&b));
250 	buffer_free(&b);
251 	free(orig_prop);
252 
253 	return fix_prop;
254 }
255 
256 char *
257 compat_cipher_proposal(char *cipher_prop)
258 {
259 	if (!(datafellows & SSH_BUG_BIGENDIANAES))
260 		return cipher_prop;
261 	debug2("%s: original cipher proposal: %s", __func__, cipher_prop);
262 	cipher_prop = filter_proposal(cipher_prop, "aes*");
263 	debug2("%s: compat cipher proposal: %s", __func__, cipher_prop);
264 	if (*cipher_prop == '\0')
265 		fatal("No supported ciphers found");
266 	return cipher_prop;
267 }
268 
269 char *
270 compat_pkalg_proposal(char *pkalg_prop)
271 {
272 	if (!(datafellows & SSH_BUG_RSASIGMD5))
273 		return pkalg_prop;
274 	debug2("%s: original public key proposal: %s", __func__, pkalg_prop);
275 	pkalg_prop = filter_proposal(pkalg_prop, "ssh-rsa");
276 	debug2("%s: compat public key proposal: %s", __func__, pkalg_prop);
277 	if (*pkalg_prop == '\0')
278 		fatal("No supported PK algorithms found");
279 	return pkalg_prop;
280 }
281 
282 char *
283 compat_kex_proposal(char *kex_prop)
284 {
285 	if (!(datafellows & SSH_BUG_CURVE25519PAD))
286 		return kex_prop;
287 	debug2("%s: original KEX proposal: %s", __func__, kex_prop);
288 	kex_prop = filter_proposal(kex_prop, "curve25519-sha256@libssh.org");
289 	debug2("%s: compat KEX proposal: %s", __func__, kex_prop);
290 	if (*kex_prop == '\0')
291 		fatal("No supported key exchange algorithms found");
292 	return kex_prop;
293 }
294 
295