Coverage Report

Created: 2026-12-01 06:13

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