Coverage Report

Created: 2026-10-19 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/compat.c
Line
Count
Source
1
/* $OpenBSD: compat.c,v 1.128 2026/03/02 02:40:15 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 <stdint.h>
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
43
compat_banner(struct ssh *ssh, const char *version)
44
25.8k
{
45
25.8k
  int i;
46
25.8k
  static struct {
47
25.8k
    char  *pat;
48
25.8k
    uint32_t bugs;
49
25.8k
  } check[] = {
50
25.8k
    { "OpenSSH_2.*,"
51
25.8k
      "OpenSSH_3.0*,"
52
25.8k
      "OpenSSH_3.1*", SSH_BUG_EXTEOF|SSH_OLD_FORWARD_ADDR|
53
25.8k
          SSH_BUG_SIGTYPE},
54
25.8k
    { "OpenSSH_3.*",  SSH_OLD_FORWARD_ADDR|SSH_BUG_SIGTYPE },
55
25.8k
    { "Sun_SSH_1.0*", SSH_BUG_NOREKEY|SSH_BUG_EXTEOF|
56
25.8k
          SSH_BUG_SIGTYPE},
57
25.8k
    { "OpenSSH_2*,"
58
25.8k
      "OpenSSH_3*,"
59
25.8k
      "OpenSSH_4*",   SSH_BUG_SIGTYPE },
60
25.8k
    { "OpenSSH_5*",   SSH_NEW_OPENSSH|SSH_BUG_DYNAMIC_RPORT|
61
25.8k
          SSH_BUG_SIGTYPE},
62
25.8k
    { "OpenSSH_6.6.1*", SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
63
25.8k
    { "OpenSSH_6.5*,"
64
25.8k
      "OpenSSH_6.6*", SSH_NEW_OPENSSH|SSH_BUG_CURVE25519PAD|
65
25.8k
          SSH_BUG_SIGTYPE},
66
25.8k
    { "OpenSSH_7.4*", SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE|
67
25.8k
          SSH_BUG_SIGTYPE74},
68
25.8k
    { "OpenSSH_7.0*,"
69
25.8k
      "OpenSSH_7.1*,"
70
25.8k
      "OpenSSH_7.2*,"
71
25.8k
      "OpenSSH_7.3*,"
72
25.8k
      "OpenSSH_7.5*,"
73
25.8k
      "OpenSSH_7.6*,"
74
25.8k
      "OpenSSH_7.7*", SSH_NEW_OPENSSH|SSH_BUG_SIGTYPE},
75
25.8k
    { "OpenSSH*",   SSH_NEW_OPENSSH },
76
25.8k
    { "*MindTerm*",   0 },
77
25.8k
    { "3.0.*",    SSH_BUG_DEBUG },
78
25.8k
    { "3.0 SecureCRT*", SSH_OLD_SESSIONID },
79
25.8k
    { "1.7 SecureFX*",  SSH_OLD_SESSIONID },
80
25.8k
    { "Cisco-1.*",    SSH_BUG_DHGEX_LARGE|
81
25.8k
          SSH_BUG_HOSTKEYS },
82
25.8k
    { "*SSH_Version_Mapper*",
83
25.8k
          SSH_BUG_SCANNER },
84
25.8k
    { "PuTTY_Local:*,"  /* dev versions < Sep 2014 */
85
25.8k
      "PuTTY-Release-0.5*," /* 0.50-0.57, DH-GEX in >=0.52 */
86
25.8k
      "PuTTY_Release_0.5*," /* 0.58-0.59 */
87
25.8k
      "PuTTY_Release_0.60*,"
88
25.8k
      "PuTTY_Release_0.61*,"
89
25.8k
      "PuTTY_Release_0.62*,"
90
25.8k
      "PuTTY_Release_0.63*,"
91
25.8k
      "PuTTY_Release_0.64*",
92
25.8k
          SSH_OLD_DHGEX },
93
25.8k
    { "FuTTY*",   SSH_OLD_DHGEX }, /* Putty Fork */
94
25.8k
    { "Probe-*",
95
25.8k
          SSH_BUG_PROBE },
96
25.8k
    { "TeraTerm SSH*,"
97
25.8k
      "TTSSH/1.5.*,"
98
25.8k
      "TTSSH/2.1*,"
99
25.8k
      "TTSSH/2.2*,"
100
25.8k
      "TTSSH/2.3*,"
101
25.8k
      "TTSSH/2.4*,"
102
25.8k
      "TTSSH/2.5*,"
103
25.8k
      "TTSSH/2.6*,"
104
25.8k
      "TTSSH/2.70*,"
105
25.8k
      "TTSSH/2.71*,"
106
25.8k
      "TTSSH/2.72*",  SSH_BUG_HOSTKEYS },
107
25.8k
    { "WinSCP_release_4*,"
108
25.8k
      "WinSCP_release_5.0*,"
109
25.8k
      "WinSCP_release_5.1,"
110
25.8k
      "WinSCP_release_5.1.*,"
111
25.8k
      "WinSCP_release_5.5,"
112
25.8k
      "WinSCP_release_5.5.*,"
113
25.8k
      "WinSCP_release_5.6,"
114
25.8k
      "WinSCP_release_5.6.*,"
115
25.8k
      "WinSCP_release_5.7,"
116
25.8k
      "WinSCP_release_5.7.1,"
117
25.8k
      "WinSCP_release_5.7.2,"
118
25.8k
      "WinSCP_release_5.7.3,"
119
25.8k
      "WinSCP_release_5.7.4",
120
25.8k
          SSH_OLD_DHGEX },
121
25.8k
    { "ConfD-*",
122
25.8k
          SSH_BUG_UTF8TTYMODE },
123
25.8k
    { "Twisted_*",    0 },
124
25.8k
    { "Twisted*",   SSH_BUG_DEBUG },
125
    /* MobaXterm's SFTP engine, see kex_input_newkeys */
126
25.8k
    { "SecureBlackbox*",  SSH_BUG_STRICT_KEX_REKEY },
127
25.8k
    { NULL,     0 }
128
25.8k
  };
129
130
  /* process table, return first match */
131
25.8k
  ssh->compat = 0;
132
573k
  for (i = 0; check[i].pat; i++) {
133
553k
    if (match_pattern_list(version, check[i].pat, 0) == 1) {
134
5.82k
      debug_f("match: %s pat %s compat 0x%08x",
135
5.82k
          version, check[i].pat, check[i].bugs);
136
5.82k
      ssh->compat = check[i].bugs;
137
      /* Check to see if the remote side is OpenSSH and not HPN */
138
      /* TODO: See if we can work this into the new method for bug checks */
139
5.82k
      if (strstr(version, "OpenSSH") != NULL) {
140
        /* check if the remote is hpn and if the version
141
         * uses hpn prefixed binaries */
142
5.71k
        const char *op;
143
5.71k
        if ((op = strstr(version, "hpn")) != NULL) {
144
1.57k
          int hpnver = 0;
145
1.57k
          ssh->compat |= SSH_HPNSSH;
146
1.57k
          debug("Remote is HPN enabled");
147
1.57k
          if (sscanf(op, "hpn%d", &hpnver) == 1 &&
148
1.48k
              hpnver >= 16) {
149
465
            ssh->compat |= SSH_HPNSSH_PREFIX;
150
465
            debug("Remote uses HPNSSH prefixes.");
151
465
          }
152
1.57k
        }
153
        /* Restrict advertised window for non-HPN OpenSSH >= 8.9. */
154
5.71k
        if (!(ssh->compat & SSH_HPNSSH)) {
155
4.14k
          const char *op;
156
4.14k
          int omaj = 0, omin = 0;
157
4.14k
          if ((op = strstr(version, "OpenSSH_")) != NULL &&
158
3.97k
              sscanf(op, "OpenSSH_%d.%d", &omaj, &omin) == 2 &&
159
3.63k
              (omaj >= 9 || (omaj == 8 && omin >= 9))) {
160
675
            ssh->compat |= SSH_RESTRICT_WINDOW;
161
675
            debug("Restricting advertised window size.");
162
675
          }
163
4.14k
        }
164
5.71k
      }
165
5.82k
      debug("ssh->compat is %u", ssh->compat);
166
5.82k
      return;
167
5.82k
    }
168
553k
  }
169
20.0k
  debug_f("no match: %s", version);
170
20.0k
}
171
172
/* Always returns pointer to allocated memory, caller must free. */
173
char *
174
compat_kex_proposal(struct ssh *ssh, const char *p)
175
67.8k
{
176
67.8k
  char *cp = NULL, *cp2 = NULL;
177
178
67.8k
  if ((ssh->compat & (SSH_BUG_CURVE25519PAD|SSH_OLD_DHGEX)) == 0)
179
67.8k
    return xstrdup(p);
180
0
  debug2_f("original KEX proposal: %s", p);
181
0
  if ((ssh->compat & SSH_BUG_CURVE25519PAD) != 0)
182
0
    if ((cp = match_filter_denylist(p,
183
0
        "curve25519-sha256@libssh.org")) == NULL)
184
0
      fatal("match_filter_denylist failed");
185
0
  if ((ssh->compat & SSH_OLD_DHGEX) != 0) {
186
0
    if ((cp2 = match_filter_denylist(cp ? cp : p,
187
0
        "diffie-hellman-group-exchange-sha256,"
188
0
        "diffie-hellman-group-exchange-sha1")) == NULL)
189
0
      fatal("match_filter_denylist failed");
190
0
    free(cp);
191
0
    cp = cp2;
192
0
  }
193
0
  if (cp == NULL || *cp == '\0')
194
0
    fatal("No supported key exchange algorithms found");
195
0
  debug2_f("compat KEX proposal: %s", cp);
196
0
  return cp;
197
0
}