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