1 /*        $NetBSD: compat.c,v 1.26 2023/07/26 17:58:15 christos Exp $ */
2 /* $OpenBSD: compat.c,v 1.126 2023/03/06 12:14:48 dtucker Exp $ */
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("$NetBSD: compat.c,v 1.26 2023/07/26 17:58:15 christos Exp $");
29 #include <sys/types.h>
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 
35 #include "xmalloc.h"
36 #include "packet.h"
37 #include "compat.h"
38 #include "log.h"
39 #include "match.h"
40 
41 /* determine bug flags from SSH protocol banner */
42 void
compat_banner(struct ssh * ssh,const char * version)43 compat_banner(struct ssh *ssh, const char *version)
44 {
45           int i;
46           static struct {
47                     const char          *pat;
48                     int       bugs;
49           } check[] = {
50                     { "OpenSSH_2.*,"
51                       "OpenSSH_3.0*,"
52                       "OpenSSH_3.1*",   SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR|
53                                                   SSH_BUG_SIGTYPE},
54                     { "OpenSSH_3.*",    SSH_OLD_FORWARD_ADDR|SSH_BUG_SIGTYPE },
55                     { "Sun_SSH_1.0*",   SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
56                                                   SSH_BUG_SIGTYPE},
57                     { "OpenSSH_2*,"
58                       "OpenSSH_3*,"
59                       "OpenSSH_4*",               SSH_BUG_SIGTYPE },
60                     { "OpenSSH_5*",               SSH_NEW_OPENSSH|SSH_BUG_DYNAMIC_RPORT|
61                                                   SSH_BUG_SIGTYPE},
62                     { "OpenSSH_6.6.1*", SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
63                     { "OpenSSH_6.5*,"
64                       "OpenSSH_6.6*",   SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD|
65                                                   SSH_BUG_SIGTYPE},
66                     { "OpenSSH_7.4*",   SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE|
67                                                   SSH_BUG_SIGTYPE74},
68                     { "OpenSSH_7.0*,"
69                       "OpenSSH_7.1*,"
70                       "OpenSSH_7.2*,"
71                       "OpenSSH_7.3*,"
72                       "OpenSSH_7.5*,"
73                       "OpenSSH_7.6*,"
74                       "OpenSSH_7.7*",   SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
75                     { "OpenSSH*",                 SSH_NEW_OPENSSH },
76                     { "*MindTerm*",               0 },
77                     { "3.0.*",                    SSH_BUG_DEBUG },
78                     { "3.0 SecureCRT*", SSH_OLD_SESSIONID },
79                     { "1.7 SecureFX*",  SSH_OLD_SESSIONID },
80                     { "Cisco-1.*",                SSH_BUG_DHGEX_LARGE|
81                                                   SSH_BUG_HOSTKEYS },
82                     { "*SSH_Version_Mapper*",
83                                                   SSH_BUG_SCANNER },
84                     { "PuTTY_Local:*,"  /* dev versions < Sep 2014 */
85                       "PuTTY-Release-0.5*," /* 0.50-0.57, DH-GEX in >=0.52 */
86                       "PuTTY_Release_0.5*,"       /* 0.58-0.59 */
87                       "PuTTY_Release_0.60*,"
88                       "PuTTY_Release_0.61*,"
89                       "PuTTY_Release_0.62*,"
90                       "PuTTY_Release_0.63*,"
91                       "PuTTY_Release_0.64*",
92                                                   SSH_OLD_DHGEX },
93                     { "FuTTY*",                   SSH_OLD_DHGEX }, /* Putty Fork */
94                     { "Probe-*",
95                                                   SSH_BUG_PROBE },
96                     { "TeraTerm SSH*,"
97                       "TTSSH/1.5.*,"
98                       "TTSSH/2.1*,"
99                       "TTSSH/2.2*,"
100                       "TTSSH/2.3*,"
101                       "TTSSH/2.4*,"
102                       "TTSSH/2.5*,"
103                       "TTSSH/2.6*,"
104                       "TTSSH/2.70*,"
105                       "TTSSH/2.71*,"
106                       "TTSSH/2.72*",    SSH_BUG_HOSTKEYS },
107                     { "WinSCP_release_4*,"
108                       "WinSCP_release_5.0*,"
109                       "WinSCP_release_5.1,"
110                       "WinSCP_release_5.1.*,"
111                       "WinSCP_release_5.5,"
112                       "WinSCP_release_5.5.*,"
113                       "WinSCP_release_5.6,"
114                       "WinSCP_release_5.6.*,"
115                       "WinSCP_release_5.7,"
116                       "WinSCP_release_5.7.1,"
117                       "WinSCP_release_5.7.2,"
118                       "WinSCP_release_5.7.3,"
119                       "WinSCP_release_5.7.4",
120                                                   SSH_OLD_DHGEX },
121                     { "ConfD-*",
122                                                   SSH_BUG_UTF8TTYMODE },
123                     { "Twisted_*",                0 },
124                     { "Twisted*",                 SSH_BUG_DEBUG },
125                     { NULL,                       0 }
126           };
127 
128           /* process table, return first match */
129           ssh->compat = 0;
130           for (i = 0; check[i].pat; i++) {
131                     if (match_pattern_list(version, check[i].pat, 0) == 1) {
132                               debug_f("match: %s pat %s compat 0x%08x",
133                                   version, check[i].pat, check[i].bugs);
134                               ssh->compat = check[i].bugs;
135                               return;
136                     }
137           }
138           debug_f("no match: %s", version);
139 }
140 
141 /* Always returns pointer to allocated memory, caller must free. */
142 char *
compat_kex_proposal(struct ssh * ssh,const char * p)143 compat_kex_proposal(struct ssh *ssh, const char *p)
144 {
145           char *cp = NULL, *cp2 = NULL;
146 
147           if ((ssh->compat & (SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX)) == 0)
148                     return xstrdup(p);
149           debug2_f("original KEX proposal: %s", p);
150           if ((ssh->compat & SSH_BUG_CURVE25519PAD) != 0)
151                     if ((cp = match_filter_denylist(p,
152                         "curve25519-sha256@libssh.org")) == NULL)
153                               fatal("match_filter_denylist failed");
154           if ((ssh->compat & SSH_OLD_DHGEX) != 0) {
155                     if ((cp2 = match_filter_denylist(cp ? cp : p,
156                         "diffie-hellman-group-exchange-sha256,"
157                         "diffie-hellman-group-exchange-sha1")) == NULL)
158                               fatal("match_filter_denylist failed");
159                     free(cp);
160                     cp = cp2;
161           }
162           if (cp == NULL || *cp == '\0')
163                     fatal("No supported key exchange algorithms found");
164           debug2_f("compat KEX proposal: %s", cp);
165           return cp;
166 }
167 
168