Coverage Report

Created: 2026-03-09 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/kexgexs.c
Line
Count
Source
1
/* $OpenBSD: kexgexs.c,v 1.51 2026/03/03 09:57:25 dtucker Exp $ */
2
/*
3
 * Copyright (c) 2000 Niels Provos.  All rights reserved.
4
 * Copyright (c) 2001 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
29
#ifdef WITH_OPENSSL
30
#include "openbsd-compat/openssl-compat.h"
31
32
#include <stdarg.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <signal.h>
36
37
#include <openssl/bn.h>
38
#include <openssl/dh.h>
39
40
#include "sshkey.h"
41
#include "cipher.h"
42
#include "digest.h"
43
#include "kex.h"
44
#include "log.h"
45
#include "packet.h"
46
#include "dh.h"
47
#include "ssh2.h"
48
#ifdef GSSAPI
49
#include "ssh-gss.h"
50
#endif
51
#include "monitor_wrap.h"
52
#include "dispatch.h"
53
#include "ssherr.h"
54
#include "sshbuf.h"
55
#include "misc.h"
56
57
static int input_kex_dh_gex_request(int, uint32_t, struct ssh *);
58
static int input_kex_dh_gex_init(int, uint32_t, struct ssh *);
59
60
int
61
kexgex_server(struct ssh *ssh)
62
1.36k
{
63
1.36k
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
64
1.36k
      &input_kex_dh_gex_request);
65
1.36k
  debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
66
1.36k
  return 0;
67
1.36k
}
68
69
static int
70
input_kex_dh_gex_request(int type, uint32_t seq, struct ssh *ssh)
71
304
{
72
304
  struct kex *kex = ssh->kex;
73
304
  int r;
74
304
  u_int min = 0, max = 0, nbits = 0;
75
304
  const BIGNUM *dh_p, *dh_g;
76
77
304
  debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
78
304
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
79
80
304
  if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
81
303
      (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
82
302
      (r = sshpkt_get_u32(ssh, &max)) != 0 ||
83
301
      (r = sshpkt_get_end(ssh)) != 0)
84
4
    goto out;
85
300
  kex->nbits = nbits;
86
300
  kex->min = min;
87
300
  kex->max = max;
88
300
  min = MAXIMUM(DH_GRP_MIN, min);
89
300
  max = MINIMUM(DH_GRP_MAX, max);
90
300
  nbits = MAXIMUM(DH_GRP_MIN, nbits);
91
300
  nbits = MINIMUM(DH_GRP_MAX, nbits);
92
93
300
  if (kex->max < kex->min || kex->nbits < kex->min ||
94
256
      kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
95
99
    r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
96
99
    goto out;
97
99
  }
98
99
  /* Contact privileged parent */
100
201
  kex->dh = mm_choose_dh(min, nbits, max);
101
201
  if (kex->dh == NULL) {
102
0
    (void)sshpkt_disconnect(ssh, "no matching DH grp found");
103
0
    r = SSH_ERR_ALLOC_FAIL;
104
0
    goto out;
105
0
  }
106
201
  debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
107
201
  DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
108
201
  if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
109
201
      (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
110
201
      (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
111
201
      (r = sshpkt_send(ssh)) != 0)
112
0
    goto out;
113
114
  /* Compute our exchange value in parallel with the client */
115
201
  if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
116
0
    goto out;
117
118
201
  debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
119
201
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
120
201
  r = 0;
121
304
 out:
122
304
  return r;
123
201
}
124
125
static int
126
input_kex_dh_gex_init(int type, uint32_t seq, struct ssh *ssh)
127
71
{
128
71
  struct kex *kex = ssh->kex;
129
71
  BIGNUM *dh_client_pub = NULL;
130
71
  const BIGNUM *pub_key, *dh_p, *dh_g;
131
71
  struct sshbuf *shared_secret = NULL;
132
71
  struct sshbuf *server_host_key_blob = NULL;
133
71
  struct sshkey *server_host_public, *server_host_private;
134
71
  u_char *signature = NULL;
135
71
  u_char hash[SSH_DIGEST_MAX_LENGTH];
136
71
  size_t slen, hashlen;
137
71
  int r;
138
139
71
  debug("SSH2_MSG_KEX_DH_GEX_INIT received");
140
71
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
141
142
71
  if ((r = kex_load_hostkey(ssh, &server_host_private,
143
71
      &server_host_public)) != 0)
144
0
    goto out;
145
146
  /* key, cert */
147
71
  if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
148
69
      (r = sshpkt_get_end(ssh)) != 0)
149
8
    goto out;
150
63
  if ((shared_secret = sshbuf_new()) == NULL) {
151
0
    r = SSH_ERR_ALLOC_FAIL;
152
0
    goto out;
153
0
  }
154
63
  if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
155
1
    goto out;
156
62
  if ((server_host_key_blob = sshbuf_new()) == NULL) {
157
0
    r = SSH_ERR_ALLOC_FAIL;
158
0
    goto out;
159
0
  }
160
62
  if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
161
0
    goto out;
162
163
  /* calc H */
164
62
  DH_get0_key(kex->dh, &pub_key, NULL);
165
62
  DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
166
62
  hashlen = sizeof(hash);
167
62
  if ((r = kexgex_hash(
168
62
      kex->hash_alg,
169
62
      kex->client_version,
170
62
      kex->server_version,
171
62
      kex->peer,
172
62
      kex->my,
173
62
      server_host_key_blob,
174
62
      kex->min, kex->nbits, kex->max,
175
62
      dh_p, dh_g,
176
62
      dh_client_pub,
177
62
      pub_key,
178
62
      sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
179
62
      hash, &hashlen)) != 0)
180
0
    goto out;
181
182
  /* sign H */
183
62
  if ((r = kex->sign(ssh, server_host_private, server_host_public,
184
62
      &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
185
0
    goto out;
186
187
  /* send server hostkey, DH pubkey 'f' and signed H */
188
62
  if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
189
62
      (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
190
62
      (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
191
62
      (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
192
62
      (r = sshpkt_send(ssh)) != 0)
193
0
    goto out;
194
195
62
  if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
196
62
      (r = kex_send_newkeys(ssh)) != 0)
197
0
    goto out;
198
199
  /* retain copy of hostkey used at initial KEX */
200
62
  if (kex->initial_hostkey == NULL &&
201
62
      (r = sshkey_from_private(server_host_public,
202
62
      &kex->initial_hostkey)) != 0)
203
0
    goto out;
204
  /* success */
205
71
 out:
206
71
  explicit_bzero(hash, sizeof(hash));
207
71
  DH_free(kex->dh);
208
  kex->dh = NULL;
209
71
  BN_clear_free(dh_client_pub);
210
71
  sshbuf_free(shared_secret);
211
71
  sshbuf_free(server_host_key_blob);
212
71
  free(signature);
213
71
  return r;
214
62
}
215
#endif /* WITH_OPENSSL */