Home | History | Annotate | Download | only in openssh
      1 /* $OpenBSD: compat.c,v 1.87 2015/01/19 20:20:20 markus 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 
     41 int compat13 = 0;
     42 int compat20 = 0;
     43 int datafellows = 0;
     44 
     45 void
     46 enable_compat20(void)
     47 {
     48 	if (compat20)
     49 		return;
     50 	debug("Enabling compatibility mode for protocol 2.0");
     51 	compat20 = 1;
     52 }
     53 void
     54 enable_compat13(void)
     55 {
     56 	debug("Enabling compatibility mode for protocol 1.3");
     57 	compat13 = 1;
     58 }
     59 /* datafellows bug compatibility */
     60 u_int
     61 compat_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_6.6.1*",	SSH_NEW_OPENSSH},
     99 		{ "OpenSSH_6.5*,"
    100 		  "OpenSSH_6.6*",	SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD},
    101 		{ "OpenSSH*",		SSH_NEW_OPENSSH },
    102 		{ "*MindTerm*",		0 },
    103 		{ "2.1.0*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
    104 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
    105 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
    106 					SSH_BUG_FIRSTKEX },
    107 		{ "2.1 *",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
    108 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
    109 					SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE|
    110 					SSH_BUG_FIRSTKEX },
    111 		{ "2.0.13*,"
    112 		  "2.0.14*,"
    113 		  "2.0.15*,"
    114 		  "2.0.16*,"
    115 		  "2.0.17*,"
    116 		  "2.0.18*,"
    117 		  "2.0.19*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
    118 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
    119 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
    120 					SSH_BUG_PKOK|SSH_BUG_RSASIGMD5|
    121 					SSH_BUG_HBSERVICE|SSH_BUG_OPENFAILURE|
    122 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
    123 		{ "2.0.11*,"
    124 		  "2.0.12*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
    125 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
    126 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
    127 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
    128 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
    129 					SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX },
    130 		{ "2.0.*",		SSH_BUG_SIGBLOB|SSH_BUG_HMAC|
    131 					SSH_OLD_SESSIONID|SSH_BUG_DEBUG|
    132 					SSH_BUG_PKSERVICE|SSH_BUG_X11FWD|
    133 					SSH_BUG_PKAUTH|SSH_BUG_PKOK|
    134 					SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE|
    135 					SSH_BUG_DERIVEKEY|SSH_BUG_DUMMYCHAN|
    136 					SSH_BUG_FIRSTKEX },
    137 		{ "2.2.0*,"
    138 		  "2.3.0*",		SSH_BUG_HMAC|SSH_BUG_DEBUG|
    139 					SSH_BUG_RSASIGMD5|SSH_BUG_FIRSTKEX },
    140 		{ "2.3.*",		SSH_BUG_DEBUG|SSH_BUG_RSASIGMD5|
    141 					SSH_BUG_FIRSTKEX },
    142 		{ "2.4",		SSH_OLD_SESSIONID },	/* Van Dyke */
    143 		{ "2.*",		SSH_BUG_DEBUG|SSH_BUG_FIRSTKEX|
    144 					SSH_BUG_RFWD_ADDR },
    145 		{ "3.0.*",		SSH_BUG_DEBUG },
    146 		{ "3.0 SecureCRT*",	SSH_OLD_SESSIONID },
    147 		{ "1.7 SecureFX*",	SSH_OLD_SESSIONID },
    148 		{ "1.2.18*,"
    149 		  "1.2.19*,"
    150 		  "1.2.20*,"
    151 		  "1.2.21*,"
    152 		  "1.2.22*",		SSH_BUG_IGNOREMSG },
    153 		{ "1.3.2*",		/* F-Secure */
    154 					SSH_BUG_IGNOREMSG },
    155 		{ "*SSH Compatible Server*",			/* Netscreen */
    156 					SSH_BUG_PASSWORDPAD },
    157 		{ "*OSU_0*,"
    158 		  "OSU_1.0*,"
    159 		  "OSU_1.1*,"
    160 		  "OSU_1.2*,"
    161 		  "OSU_1.3*,"
    162 		  "OSU_1.4*,"
    163 		  "OSU_1.5alpha1*,"
    164 		  "OSU_1.5alpha2*,"
    165 		  "OSU_1.5alpha3*",	SSH_BUG_PASSWORDPAD },
    166 		{ "*SSH_Version_Mapper*",
    167 					SSH_BUG_SCANNER },
    168 		{ "Probe-*",
    169 					SSH_BUG_PROBE },
    170 		{ NULL,			0 }
    171 	};
    172 
    173 	/* process table, return first match */
    174 	for (i = 0; check[i].pat; i++) {
    175 		if (match_pattern_list(version, check[i].pat,
    176 		    strlen(check[i].pat), 0) == 1) {
    177 			debug("match: %s pat %s compat 0x%08x",
    178 			    version, check[i].pat, check[i].bugs);
    179 			datafellows = check[i].bugs;	/* XXX for now */
    180 			return check[i].bugs;
    181 		}
    182 	}
    183 	debug("no match: %s", version);
    184 	return 0;
    185 }
    186 
    187 #define	SEP	","
    188 int
    189 proto_spec(const char *spec)
    190 {
    191 	char *s, *p, *q;
    192 	int ret = SSH_PROTO_UNKNOWN;
    193 
    194 	if (spec == NULL)
    195 		return ret;
    196 	q = s = strdup(spec);
    197 	if (s == NULL)
    198 		return ret;
    199 	for ((p = strsep(&q, SEP)); p && *p != '\0'; (p = strsep(&q, SEP))) {
    200 		switch (atoi(p)) {
    201 		case 1:
    202 			if (ret == SSH_PROTO_UNKNOWN)
    203 				ret |= SSH_PROTO_1_PREFERRED;
    204 			ret |= SSH_PROTO_1;
    205 			break;
    206 		case 2:
    207 			ret |= SSH_PROTO_2;
    208 			break;
    209 		default:
    210 			logit("ignoring bad proto spec: '%s'.", p);
    211 			break;
    212 		}
    213 	}
    214 	free(s);
    215 	return ret;
    216 }
    217 
    218 /*
    219  * Filters a proposal string, excluding any algorithm matching the 'filter'
    220  * pattern list.
    221  */
    222 static char *
    223 filter_proposal(char *proposal, const char *filter)
    224 {
    225 	Buffer b;
    226 	char *orig_prop, *fix_prop;
    227 	char *cp, *tmp;
    228 
    229 	buffer_init(&b);
    230 	tmp = orig_prop = xstrdup(proposal);
    231 	while ((cp = strsep(&tmp, ",")) != NULL) {
    232 		if (match_pattern_list(cp, filter, strlen(cp), 0) != 1) {
    233 			if (buffer_len(&b) > 0)
    234 				buffer_append(&b, ",", 1);
    235 			buffer_append(&b, cp, strlen(cp));
    236 		} else
    237 			debug2("Compat: skipping algorithm \"%s\"", cp);
    238 	}
    239 	buffer_append(&b, "\0", 1);
    240 	fix_prop = xstrdup((char *)buffer_ptr(&b));
    241 	buffer_free(&b);
    242 	free(orig_prop);
    243 
    244 	return fix_prop;
    245 }
    246 
    247 char *
    248 compat_cipher_proposal(char *cipher_prop)
    249 {
    250 	if (!(datafellows & SSH_BUG_BIGENDIANAES))
    251 		return cipher_prop;
    252 	debug2("%s: original cipher proposal: %s", __func__, cipher_prop);
    253 	cipher_prop = filter_proposal(cipher_prop, "aes*");
    254 	debug2("%s: compat cipher proposal: %s", __func__, cipher_prop);
    255 	if (*cipher_prop == '\0')
    256 		fatal("No supported ciphers found");
    257 	return cipher_prop;
    258 }
    259 
    260 char *
    261 compat_pkalg_proposal(char *pkalg_prop)
    262 {
    263 	if (!(datafellows & SSH_BUG_RSASIGMD5))
    264 		return pkalg_prop;
    265 	debug2("%s: original public key proposal: %s", __func__, pkalg_prop);
    266 	pkalg_prop = filter_proposal(pkalg_prop, "ssh-rsa");
    267 	debug2("%s: compat public key proposal: %s", __func__, pkalg_prop);
    268 	if (*pkalg_prop == '\0')
    269 		fatal("No supported PK algorithms found");
    270 	return pkalg_prop;
    271 }
    272 
    273 char *
    274 compat_kex_proposal(char *kex_prop)
    275 {
    276 	if (!(datafellows & SSH_BUG_CURVE25519PAD))
    277 		return kex_prop;
    278 	debug2("%s: original KEX proposal: %s", __func__, kex_prop);
    279 	kex_prop = filter_proposal(kex_prop, "curve25519-sha256 (at) libssh.org");
    280 	debug2("%s: compat KEX proposal: %s", __func__, kex_prop);
    281 	if (*kex_prop == '\0')
    282 		fatal("No supported key exchange algorithms found");
    283 	return kex_prop;
    284 }
    285 
    286