xref: /netbsd-src/crypto/external/bsd/openssh/dist/compat.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
1 /*	$NetBSD: compat.c,v 1.20 2019/01/27 02:08:33 pgoyette Exp $	*/
2 /* $OpenBSD: compat.c,v 1.113 2018/08/13 02:41:05 djm Exp $ */
3 
4 /*
5  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "includes.h"
29 __RCSID("$NetBSD: compat.c,v 1.20 2019/01/27 02:08:33 pgoyette Exp $");
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 "packet.h"
38 #include "compat.h"
39 #include "log.h"
40 #include "match.h"
41 #include "kex.h"
42 
43 int datafellows = 0;
44 
45 /* datafellows bug compatibility */
46 u_int
47 compat_datafellows(const char *version)
48 {
49 	int i;
50 	static struct {
51 		const char	*pat;
52 		int	bugs;
53 	} check[] = {
54 		{ "OpenSSH_2.*,"
55 		  "OpenSSH_3.0*,"
56 		  "OpenSSH_3.1*",	SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR|
57 					SSH_BUG_SIGTYPE},
58 		{ "OpenSSH_3.*",	SSH_OLD_FORWARD_ADDR|SSH_BUG_SIGTYPE },
59 		{ "Sun_SSH_1.0*",	SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
60 					SSH_BUG_SIGTYPE},
61 		{ "OpenSSH_2*,"
62 		  "OpenSSH_3*,"
63 		  "OpenSSH_4*",		SSH_BUG_SIGTYPE },
64 		{ "OpenSSH_5*",		SSH_NEW_OPENSSH|SSH_BUG_DYNAMIC_RPORT|
65 					SSH_BUG_SIGTYPE},
66 		{ "OpenSSH_6.6.1*",	SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
67 		{ "OpenSSH_6.5*,"
68 		  "OpenSSH_6.6*",	SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD|
69 					SSH_BUG_SIGTYPE},
70 		{ "OpenSSH_7.0*,"
71 		  "OpenSSH_7.1*,"
72 		  "OpenSSH_7.2*,"
73 		  "OpenSSH_7.3*,"
74 		  "OpenSSH_7.4*,"
75 		  "OpenSSH_7.5*,"
76 		  "OpenSSH_7.6*,"
77 		  "OpenSSH_7.7*",	SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
78 		{ "OpenSSH*",		SSH_NEW_OPENSSH },
79 		{ "*MindTerm*",		0 },
80 		{ "3.0.*",		SSH_BUG_DEBUG },
81 		{ "3.0 SecureCRT*",	SSH_OLD_SESSIONID },
82 		{ "1.7 SecureFX*",	SSH_OLD_SESSIONID },
83 		{ "1.2.18*,"
84 		  "1.2.19*,"
85 		  "1.2.20*,"
86 		  "1.2.21*,"
87 		  "1.2.22*",		SSH_BUG_IGNOREMSG },
88 		{ "1.3.2*",		/* F-Secure */
89 					SSH_BUG_IGNOREMSG },
90 		{ "Cisco-1.*",		SSH_BUG_DHGEX_LARGE|
91 					SSH_BUG_HOSTKEYS },
92 		{ "*SSH Compatible Server*",			/* Netscreen */
93 					SSH_BUG_PASSWORDPAD },
94 		{ "*OSU_0*,"
95 		  "OSU_1.0*,"
96 		  "OSU_1.1*,"
97 		  "OSU_1.2*,"
98 		  "OSU_1.3*,"
99 		  "OSU_1.4*,"
100 		  "OSU_1.5alpha1*,"
101 		  "OSU_1.5alpha2*,"
102 		  "OSU_1.5alpha3*",	SSH_BUG_PASSWORDPAD },
103 		{ "*SSH_Version_Mapper*",
104 					SSH_BUG_SCANNER },
105 		{ "PuTTY_Local:*,"	/* dev versions < Sep 2014 */
106 		  "PuTTY-Release-0.5*," /* 0.50-0.57, DH-GEX in >=0.52 */
107 		  "PuTTY_Release_0.5*,"	/* 0.58-0.59 */
108 		  "PuTTY_Release_0.60*,"
109 		  "PuTTY_Release_0.61*,"
110 		  "PuTTY_Release_0.62*,"
111 		  "PuTTY_Release_0.63*,"
112 		  "PuTTY_Release_0.64*",
113 					SSH_OLD_DHGEX },
114 		{ "FuTTY*",		SSH_OLD_DHGEX }, /* Putty Fork */
115 		{ "Probe-*",
116 					SSH_BUG_PROBE },
117 		{ "TeraTerm SSH*,"
118 		  "TTSSH/1.5.*,"
119 		  "TTSSH/2.1*,"
120 		  "TTSSH/2.2*,"
121 		  "TTSSH/2.3*,"
122 		  "TTSSH/2.4*,"
123 		  "TTSSH/2.5*,"
124 		  "TTSSH/2.6*,"
125 		  "TTSSH/2.70*,"
126 		  "TTSSH/2.71*,"
127 		  "TTSSH/2.72*",	SSH_BUG_HOSTKEYS },
128 		{ "WinSCP_release_4*,"
129 		  "WinSCP_release_5.0*,"
130 		  "WinSCP_release_5.1,"
131 		  "WinSCP_release_5.1.*,"
132 		  "WinSCP_release_5.5,"
133 		  "WinSCP_release_5.5.*,"
134 		  "WinSCP_release_5.6,"
135 		  "WinSCP_release_5.6.*,"
136 		  "WinSCP_release_5.7,"
137 		  "WinSCP_release_5.7.1,"
138 		  "WinSCP_release_5.7.2,"
139 		  "WinSCP_release_5.7.3,"
140 		  "WinSCP_release_5.7.4",
141 					SSH_OLD_DHGEX },
142 		{ "ConfD-*",
143 					SSH_BUG_UTF8TTYMODE },
144 		{ "Twisted_*",		0 },
145 		{ "Twisted*",		SSH_BUG_DEBUG },
146 		{ NULL,			0 }
147 	};
148 
149 	/* process table, return first match */
150 	for (i = 0; check[i].pat; i++) {
151 		if (match_pattern_list(version, check[i].pat, 0) == 1) {
152 			debug("match: %s pat %s compat 0x%08x",
153 			    version, check[i].pat, check[i].bugs);
154 			datafellows = check[i].bugs;	/* XXX for now */
155 			/* Check to see if the remote side is OpenSSH and not HPN */
156 			if(strstr(version,"OpenSSH") != NULL)
157 			{
158 				if (strstr(version,"hpn") == NULL)
159 				{
160 					datafellows |= SSH_BUG_LARGEWINDOW;
161 					debug("Remote is NON-HPN aware");
162 				}
163 			}
164 			return check[i].bugs;
165 		}
166 	}
167 	debug("no match: %s", version);
168 	return 0;
169 }
170 
171 #define	SEP	","
172 int
173 proto_spec(const char *spec)
174 {
175 	char *s, *p, *q;
176 	int ret = SSH_PROTO_UNKNOWN;
177 
178 	if (spec == NULL)
179 		return ret;
180 	q = s = strdup(spec);
181 	if (s == NULL)
182 		return ret;
183 	for ((p = strsep(&q, SEP)); p && *p != '\0'; (p = strsep(&q, SEP))) {
184 		switch (atoi(p)) {
185 		case 2:
186 			ret |= SSH_PROTO_2;
187 			break;
188 		default:
189 			logit("ignoring bad proto spec: '%s'.", p);
190 			break;
191 		}
192 	}
193 	free(s);
194 	return ret;
195 }
196 
197 const char *
198 compat_cipher_proposal(const char *cipher_prop)
199 {
200 	if (!(datafellows & SSH_BUG_BIGENDIANAES))
201 		return cipher_prop;
202 	debug2("%s: original cipher proposal: %s", __func__, cipher_prop);
203 	if ((cipher_prop = match_filter_blacklist(cipher_prop, "aes*")) == NULL)
204 		fatal("match_filter_blacklist failed");
205 	debug2("%s: compat cipher proposal: %s", __func__, cipher_prop);
206 	if (*cipher_prop == '\0')
207 		fatal("No supported ciphers found");
208 	return cipher_prop;
209 }
210 
211 char *
212 compat_pkalg_proposal(char *pkalg_prop)
213 {
214 	if (!(datafellows & SSH_BUG_RSASIGMD5))
215 		return pkalg_prop;
216 	debug2("%s: original public key proposal: %s", __func__, pkalg_prop);
217 	if ((pkalg_prop = match_filter_blacklist(pkalg_prop, "ssh-rsa")) == NULL)
218 		fatal("match_filter_blacklist failed");
219 	debug2("%s: compat public key proposal: %s", __func__, pkalg_prop);
220 	if (*pkalg_prop == '\0')
221 		fatal("No supported PK algorithms found");
222 	return pkalg_prop;
223 }
224 
225 const char *
226 compat_kex_proposal(const char *p)
227 {
228 	if ((datafellows & (SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX)) == 0)
229 		return p;
230 	debug2("%s: original KEX proposal: %s", __func__, p);
231 	if ((datafellows & SSH_BUG_CURVE25519PAD) != 0)
232 		if ((p = match_filter_blacklist(p,
233 		    "curve25519-sha256@libssh.org")) == NULL)
234 			fatal("match_filter_blacklist failed");
235 	if ((datafellows & SSH_OLD_DHGEX) != 0) {
236 		if ((p = match_filter_blacklist(p,
237 		    "diffie-hellman-group-exchange-sha256,"
238 		    "diffie-hellman-group-exchange-sha1")) == NULL)
239 			fatal("match_filter_blacklist failed");
240 	}
241 	debug2("%s: compat KEX proposal: %s", __func__, p);
242 	if (*p == '\0')
243 		fatal("No supported key exchange algorithms found");
244 	return p;
245 }
246 
247