Coverage Report

Created: 2026-10-13 06:57

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.09k
{
63
1.09k
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST,
64
1.09k
      &input_kex_dh_gex_request);
65
1.09k
  debug("expecting SSH2_MSG_KEX_DH_GEX_REQUEST");
66
1.09k
  return 0;
67
1.09k
}
68
69
static int
70
input_kex_dh_gex_request(int type, uint32_t seq, struct ssh *ssh)
71
240
{
72
240
  struct kex *kex = ssh->kex;
73
240
  int r;
74
240
  u_int min = 0, max = 0, nbits = 0;
75
240
  const BIGNUM *dh_p, *dh_g;
76
77
240
  debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
78
240
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REQUEST, &kex_protocol_error);
79
80
240
  if ((r = sshpkt_get_u32(ssh, &min)) != 0 ||
81
239
      (r = sshpkt_get_u32(ssh, &nbits)) != 0 ||
82
238
      (r = sshpkt_get_u32(ssh, &max)) != 0 ||
83
237
      (r = sshpkt_get_end(ssh)) != 0)
84
4
    goto out;
85
236
  kex->nbits = nbits;
86
236
  kex->min = min;
87
236
  kex->max = max;
88
236
  min = MAXIMUM(DH_GRP_MIN, min);
89
236
  max = MINIMUM(DH_GRP_MAX, max);
90
236
  nbits = MAXIMUM(DH_GRP_MIN, nbits);
91
236
  nbits = MINIMUM(DH_GRP_MAX, nbits);
92
93
236
  if (kex->max < kex->min || kex->nbits < kex->min ||
94
199
      kex->max < kex->nbits || kex->max < DH_GRP_MIN) {
95
68
    r = SSH_ERR_DH_GEX_OUT_OF_RANGE;
96
68
    goto out;
97
68
  }
98
99
  /* Contact privileged parent */
100
168
  kex->dh = mm_choose_dh(min, nbits, max);
101
168
  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
168
  debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
107
168
  DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
108
168
  if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 ||
109
168
      (r = sshpkt_put_bignum2(ssh, dh_p)) != 0 ||
110
168
      (r = sshpkt_put_bignum2(ssh, dh_g)) != 0 ||
111
168
      (r = sshpkt_send(ssh)) != 0)
112
0
    goto out;
113
114
  /* Compute our exchange value in parallel with the client */
115
168
  if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
116
0
    goto out;
117
118
168
  debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
119
168
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init);
120
168
  r = 0;
121
240
 out:
122
240
  return r;
123
168
}
124
125
static int
126
input_kex_dh_gex_init(int type, uint32_t seq, struct ssh *ssh)
127
69
{
128
69
  struct kex *kex = ssh->kex;
129
69
  BIGNUM *dh_client_pub = NULL;
130
69
  const BIGNUM *pub_key, *dh_p, *dh_g;
131
69
  struct sshbuf *shared_secret = NULL;
132
69
  struct sshbuf *server_host_key_blob = NULL;
133
69
  struct sshkey *server_host_public, *server_host_private;
134
69
  u_char *signature = NULL;
135
69
  u_char hash[SSH_DIGEST_MAX_LENGTH];
136
69
  size_t slen, hashlen;
137
69
  int r;
138
139
69
  debug("SSH2_MSG_KEX_DH_GEX_INIT received");
140
69
  ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &kex_protocol_error);
141
142
69
  if ((r = kex_load_hostkey(ssh, &server_host_private,
143
69
      &server_host_public)) != 0)
144
0
    goto out;
145
146
  /* key, cert */
147
69
  if ((r = sshpkt_get_bignum2(ssh, &dh_client_pub)) != 0 ||
148
66
      (r = sshpkt_get_end(ssh)) != 0)
149
10
    goto out;
150
59
  if ((shared_secret = sshbuf_new()) == NULL) {
151
0
    r = SSH_ERR_ALLOC_FAIL;
152
0
    goto out;
153
0
  }
154
59
  if ((r = kex_dh_compute_key(kex, dh_client_pub, shared_secret)) != 0)
155
1
    goto out;
156
58
  if ((server_host_key_blob = sshbuf_new()) == NULL) {
157
0
    r = SSH_ERR_ALLOC_FAIL;
158
0
    goto out;
159
0
  }
160
58
  if ((r = sshkey_putb(server_host_public, server_host_key_blob)) != 0)
161
0
    goto out;
162
163
  /* calc H */
164
58
  DH_get0_key(kex->dh, &pub_key, NULL);
165
58
  DH_get0_pqg(kex->dh, &dh_p, NULL, &dh_g);
166
58
  hashlen = sizeof(hash);
167
58
  if ((r = kexgex_hash(
168
58
      kex->hash_alg,
169
58
      kex->client_version,
170
58
      kex->server_version,
171
58
      kex->peer,
172
58
      kex->my,
173
58
      server_host_key_blob,
174
58
      kex->min, kex->nbits, kex->max,
175
58
      dh_p, dh_g,
176
58
      dh_client_pub,
177
58
      pub_key,
178
58
      sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
179
58
      hash, &hashlen)) != 0)
180
0
    goto out;
181
182
  /* sign H */
183
58
  if ((r = kex->sign(ssh, server_host_private, server_host_public,
184
58
      &signature, &slen, hash, hashlen, kex->hostkey_alg)) < 0)
185
0
    goto out;
186
187
  /* send server hostkey, DH pubkey 'f' and signed H */
188
58
  if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_REPLY)) != 0 ||
189
58
      (r = sshpkt_put_stringb(ssh, server_host_key_blob)) != 0 ||
190
58
      (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||     /* f */
191
58
      (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
192
58
      (r = sshpkt_send(ssh)) != 0)
193
0
    goto out;
194
195
58
  if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) != 0 ||
196
58
      (r = kex_send_newkeys(ssh)) != 0)
197
0
    goto out;
198
199
  /* retain copy of hostkey used at initial KEX */
200
58
  if (kex->initial_hostkey == NULL &&
201
58
      (r = sshkey_from_private(server_host_public,
202
58
      &kex->initial_hostkey)) != 0)
203
0
    goto out;
204
  /* success */
205
69
 out:
206
69
  explicit_bzero(hash, sizeof(hash));
207
69
  DH_free(kex->dh);
208
  kex->dh = NULL;
209
69
  BN_clear_free(dh_client_pub);
210
69
  sshbuf_free(shared_secret);
211
69
  sshbuf_free(server_host_key_blob);
212
69
  free(signature);
213
69
  return r;
214
58
}
215
#endif /* WITH_OPENSSL */