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