1 /* $OpenBSD: compat.c,v 1.104 2017/07/25 09:22:25 dtucker 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 #include "kex.h" 41 42 int datafellows = 0; 43 44 /* datafellows bug compatibility */ 45 u_int 46 compat_datafellows(const char *version) 47 { 48 int i; 49 static struct { 50 char *pat; 51 int bugs; 52 } check[] = { 53 { "OpenSSH-2.0*," 54 "OpenSSH-2.1*," 55 "OpenSSH_2.1*," 56 "OpenSSH_2.2*", SSH_OLD_SESSIONID|SSH_BUG_BANNER| 57 SSH_OLD_DHGEX|SSH_BUG_NOREKEY| 58 SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR}, 59 { "OpenSSH_2.3.0*", SSH_BUG_BANNER|SSH_BUG_BIGENDIANAES| 60 SSH_OLD_DHGEX|SSH_BUG_NOREKEY| 61 SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR}, 62 { "OpenSSH_2.3.*", SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX| 63 SSH_BUG_NOREKEY|SSH_BUG_EXTEOF| 64 SSH_OLD_FORWARD_ADDR}, 65 { "OpenSSH_2.5.0p1*," 66 "OpenSSH_2.5.1p1*", 67 SSH_BUG_BIGENDIANAES|SSH_OLD_DHGEX| 68 SSH_BUG_NOREKEY|SSH_BUG_EXTEOF| 69 SSH_OLD_FORWARD_ADDR}, 70 { "OpenSSH_2.5.0*," 71 "OpenSSH_2.5.1*," 72 "OpenSSH_2.5.2*", SSH_OLD_DHGEX|SSH_BUG_NOREKEY| 73 SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR}, 74 { "OpenSSH_2.5.3*", SSH_BUG_NOREKEY|SSH_BUG_EXTEOF| 75 SSH_OLD_FORWARD_ADDR}, 76 { "OpenSSH_2.*," 77 "OpenSSH_3.0*," 78 "OpenSSH_3.1*", SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR}, 79 { "OpenSSH_3.*", SSH_OLD_FORWARD_ADDR }, 80 { "Sun_SSH_1.0*", SSH_BUG_NOREKEY|SSH_BUG_EXTEOF}, 81 { "OpenSSH_4*", 0 }, 82 { "OpenSSH_5*", SSH_NEW_OPENSSH|SSH_BUG_DYNAMIC_RPORT}, 83 { "OpenSSH_6.6.1*", SSH_NEW_OPENSSH}, 84 { "OpenSSH_6.5*," 85 "OpenSSH_6.6*", SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD}, 86 { "OpenSSH*", SSH_NEW_OPENSSH }, 87 { "*MindTerm*", 0 }, 88 { "2.1.0*", SSH_BUG_SIGBLOB|SSH_BUG_HMAC| 89 SSH_OLD_SESSIONID|SSH_BUG_DEBUG| 90 SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE| 91 SSH_BUG_FIRSTKEX }, 92 { "2.1 *", SSH_BUG_SIGBLOB|SSH_BUG_HMAC| 93 SSH_OLD_SESSIONID|SSH_BUG_DEBUG| 94 SSH_BUG_RSASIGMD5|SSH_BUG_HBSERVICE| 95 SSH_BUG_FIRSTKEX }, 96 { "2.0.13*," 97 "2.0.14*," 98 "2.0.15*," 99 "2.0.16*," 100 "2.0.17*," 101 "2.0.18*," 102 "2.0.19*", SSH_BUG_SIGBLOB|SSH_BUG_HMAC| 103 SSH_OLD_SESSIONID|SSH_BUG_DEBUG| 104 SSH_BUG_PKSERVICE|SSH_BUG_X11FWD| 105 SSH_BUG_PKOK|SSH_BUG_RSASIGMD5| 106 SSH_BUG_HBSERVICE|SSH_BUG_OPENFAILURE| 107 SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX }, 108 { "2.0.11*," 109 "2.0.12*", SSH_BUG_SIGBLOB|SSH_BUG_HMAC| 110 SSH_OLD_SESSIONID|SSH_BUG_DEBUG| 111 SSH_BUG_PKSERVICE|SSH_BUG_X11FWD| 112 SSH_BUG_PKAUTH|SSH_BUG_PKOK| 113 SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE| 114 SSH_BUG_DUMMYCHAN|SSH_BUG_FIRSTKEX }, 115 { "2.0.*", SSH_BUG_SIGBLOB|SSH_BUG_HMAC| 116 SSH_OLD_SESSIONID|SSH_BUG_DEBUG| 117 SSH_BUG_PKSERVICE|SSH_BUG_X11FWD| 118 SSH_BUG_PKAUTH|SSH_BUG_PKOK| 119 SSH_BUG_RSASIGMD5|SSH_BUG_OPENFAILURE| 120 SSH_BUG_DERIVEKEY|SSH_BUG_DUMMYCHAN| 121 SSH_BUG_FIRSTKEX }, 122 { "2.2.0*," 123 "2.3.0*", SSH_BUG_HMAC|SSH_BUG_DEBUG| 124 SSH_BUG_RSASIGMD5|SSH_BUG_FIRSTKEX }, 125 { "2.3.*", SSH_BUG_DEBUG|SSH_BUG_RSASIGMD5| 126 SSH_BUG_FIRSTKEX }, 127 { "2.4", SSH_OLD_SESSIONID }, /* Van Dyke */ 128 { "2.*", SSH_BUG_DEBUG|SSH_BUG_FIRSTKEX| 129 SSH_BUG_RFWD_ADDR }, 130 { "3.0.*", SSH_BUG_DEBUG }, 131 { "3.0 SecureCRT*", SSH_OLD_SESSIONID }, 132 { "1.7 SecureFX*", SSH_OLD_SESSIONID }, 133 { "1.2.18*," 134 "1.2.19*," 135 "1.2.20*," 136 "1.2.21*," 137 "1.2.22*", SSH_BUG_IGNOREMSG }, 138 { "1.3.2*", /* F-Secure */ 139 SSH_BUG_IGNOREMSG }, 140 { "Cisco-1.*", SSH_BUG_DHGEX_LARGE| 141 SSH_BUG_HOSTKEYS }, 142 { "*SSH Compatible Server*", /* Netscreen */ 143 SSH_BUG_PASSWORDPAD }, 144 { "*OSU_0*," 145 "OSU_1.0*," 146 "OSU_1.1*," 147 "OSU_1.2*," 148 "OSU_1.3*," 149 "OSU_1.4*," 150 "OSU_1.5alpha1*," 151 "OSU_1.5alpha2*," 152 "OSU_1.5alpha3*", SSH_BUG_PASSWORDPAD }, 153 { "*SSH_Version_Mapper*", 154 SSH_BUG_SCANNER }, 155 { "PuTTY_Local:*," /* dev versions < Sep 2014 */ 156 "PuTTY-Release-0.5*," /* 0.50-0.57, DH-GEX in >=0.52 */ 157 "PuTTY_Release_0.5*," /* 0.58-0.59 */ 158 "PuTTY_Release_0.60*," 159 "PuTTY_Release_0.61*," 160 "PuTTY_Release_0.62*," 161 "PuTTY_Release_0.63*," 162 "PuTTY_Release_0.64*", 163 SSH_OLD_DHGEX }, 164 { "FuTTY*", SSH_OLD_DHGEX }, /* Putty Fork */ 165 { "Probe-*", 166 SSH_BUG_PROBE }, 167 { "TeraTerm SSH*," 168 "TTSSH/1.5.*," 169 "TTSSH/2.1*," 170 "TTSSH/2.2*," 171 "TTSSH/2.3*," 172 "TTSSH/2.4*," 173 "TTSSH/2.5*," 174 "TTSSH/2.6*," 175 "TTSSH/2.70*," 176 "TTSSH/2.71*," 177 "TTSSH/2.72*", SSH_BUG_HOSTKEYS }, 178 { "WinSCP_release_4*," 179 "WinSCP_release_5.0*," 180 "WinSCP_release_5.1," 181 "WinSCP_release_5.1.*," 182 "WinSCP_release_5.5," 183 "WinSCP_release_5.5.*," 184 "WinSCP_release_5.6," 185 "WinSCP_release_5.6.*," 186 "WinSCP_release_5.7," 187 "WinSCP_release_5.7.1," 188 "WinSCP_release_5.7.2," 189 "WinSCP_release_5.7.3," 190 "WinSCP_release_5.7.4", 191 SSH_OLD_DHGEX }, 192 { NULL, 0 } 193 }; 194 195 /* process table, return first match */ 196 for (i = 0; check[i].pat; i++) { 197 if (match_pattern_list(version, check[i].pat, 0) == 1) { 198 debug("match: %s pat %s compat 0x%08x", 199 version, check[i].pat, check[i].bugs); 200 datafellows = check[i].bugs; /* XXX for now */ 201 return check[i].bugs; 202 } 203 } 204 debug("no match: %s", version); 205 return 0; 206 } 207 208 #define SEP "," 209 int 210 proto_spec(const char *spec) 211 { 212 char *s, *p, *q; 213 int ret = SSH_PROTO_UNKNOWN; 214 215 if (spec == NULL) 216 return ret; 217 q = s = strdup(spec); 218 if (s == NULL) 219 return ret; 220 for ((p = strsep(&q, SEP)); p && *p != '\0'; (p = strsep(&q, SEP))) { 221 switch (atoi(p)) { 222 case 2: 223 ret |= SSH_PROTO_2; 224 break; 225 default: 226 logit("ignoring bad proto spec: '%s'.", p); 227 break; 228 } 229 } 230 free(s); 231 return ret; 232 } 233 234 char * 235 compat_cipher_proposal(char *cipher_prop) 236 { 237 if (!(datafellows & SSH_BUG_BIGENDIANAES)) 238 return cipher_prop; 239 debug2("%s: original cipher proposal: %s", __func__, cipher_prop); 240 if ((cipher_prop = match_filter_list(cipher_prop, "aes*")) == NULL) 241 fatal("match_filter_list failed"); 242 debug2("%s: compat cipher proposal: %s", __func__, cipher_prop); 243 if (*cipher_prop == '\0') 244 fatal("No supported ciphers found"); 245 return cipher_prop; 246 } 247 248 char * 249 compat_pkalg_proposal(char *pkalg_prop) 250 { 251 if (!(datafellows & SSH_BUG_RSASIGMD5)) 252 return pkalg_prop; 253 debug2("%s: original public key proposal: %s", __func__, pkalg_prop); 254 if ((pkalg_prop = match_filter_list(pkalg_prop, "ssh-rsa")) == NULL) 255 fatal("match_filter_list failed"); 256 debug2("%s: compat public key proposal: %s", __func__, pkalg_prop); 257 if (*pkalg_prop == '\0') 258 fatal("No supported PK algorithms found"); 259 return pkalg_prop; 260 } 261 262 char * 263 compat_kex_proposal(char *p) 264 { 265 if ((datafellows & (SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX)) == 0) 266 return p; 267 debug2("%s: original KEX proposal: %s", __func__, p); 268 if ((datafellows & SSH_BUG_CURVE25519PAD) != 0) 269 if ((p = match_filter_list(p, 270 "curve25519-sha256@libssh.org")) == NULL) 271 fatal("match_filter_list failed"); 272 if ((datafellows & SSH_OLD_DHGEX) != 0) { 273 if ((p = match_filter_list(p, 274 "diffie-hellman-group-exchange-sha256," 275 "diffie-hellman-group-exchange-sha1")) == NULL) 276 fatal("match_filter_list failed"); 277 } 278 debug2("%s: compat KEX proposal: %s", __func__, p); 279 if (*p == '\0') 280 fatal("No supported key exchange algorithms found"); 281 return p; 282 } 283 284