Coverage Report

Created: 2025-07-01 07:01

/src/openssh/kexdh.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: kexdh.c,v 1.34 2020/12/04 02:29:25 djm Exp $ */
2
/*
3
 * Copyright (c) 2019 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
#ifdef WITH_OPENSSL
29
30
#include <sys/types.h>
31
32
#include <stdio.h>
33
#include <string.h>
34
#include <signal.h>
35
36
#include "openbsd-compat/openssl-compat.h"
37
#include <openssl/dh.h>
38
39
#include "sshkey.h"
40
#include "kex.h"
41
#include "sshbuf.h"
42
#include "digest.h"
43
#include "ssherr.h"
44
#include "dh.h"
45
#include "log.h"
46
47
int
48
kex_dh_keygen(struct kex *kex)
49
1.39k
{
50
1.39k
  switch (kex->kex_type) {
51
1.39k
  case KEX_DH_GRP1_SHA1:
52
1.39k
    kex->dh = dh_new_group1();
53
1.39k
    break;
54
0
  case KEX_DH_GRP14_SHA1:
55
0
  case KEX_DH_GRP14_SHA256:
56
0
    kex->dh = dh_new_group14();
57
0
    break;
58
0
  case KEX_DH_GRP16_SHA512:
59
0
    kex->dh = dh_new_group16();
60
0
    break;
61
0
  case KEX_DH_GRP18_SHA512:
62
0
    kex->dh = dh_new_group18();
63
0
    break;
64
0
  default:
65
0
    return SSH_ERR_INVALID_ARGUMENT;
66
1.39k
  }
67
1.39k
  if (kex->dh == NULL)
68
0
    return SSH_ERR_ALLOC_FAIL;
69
1.39k
  return (dh_gen_key(kex->dh, kex->we_need * 8));
70
1.39k
}
71
72
int
73
kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
74
371
{
75
371
  BIGNUM *shared_secret = NULL;
76
371
  u_char *kbuf = NULL;
77
371
  size_t klen = 0;
78
371
  int kout, r;
79
80
#ifdef DEBUG_KEXDH
81
  fprintf(stderr, "dh_pub= ");
82
  BN_print_fp(stderr, dh_pub);
83
  fprintf(stderr, "\n");
84
  debug("bits %d", BN_num_bits(dh_pub));
85
  DHparams_print_fp(stderr, kex->dh);
86
  fprintf(stderr, "\n");
87
#endif
88
89
371
  if (!dh_pub_is_valid(kex->dh, dh_pub)) {
90
6
    r = SSH_ERR_MESSAGE_INCOMPLETE;
91
6
    goto out;
92
6
  }
93
365
  klen = DH_size(kex->dh);
94
365
  if ((kbuf = malloc(klen)) == NULL ||
95
365
      (shared_secret = BN_new()) == NULL) {
96
0
    r = SSH_ERR_ALLOC_FAIL;
97
0
    goto out;
98
0
  }
99
365
  if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
100
365
      BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
101
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
102
0
    goto out;
103
0
  }
104
#ifdef DEBUG_KEXDH
105
  dump_digest("shared secret", kbuf, kout);
106
#endif
107
365
  r = sshbuf_put_bignum2(out, shared_secret);
108
371
 out:
109
371
  freezero(kbuf, klen);
110
371
  BN_clear_free(shared_secret);
111
371
  return r;
112
365
}
113
114
int
115
kex_dh_keypair(struct kex *kex)
116
1.13k
{
117
1.13k
  const BIGNUM *pub_key;
118
1.13k
  struct sshbuf *buf = NULL;
119
1.13k
  int r;
120
121
1.13k
  if ((r = kex_dh_keygen(kex)) != 0)
122
0
    return r;
123
1.13k
  DH_get0_key(kex->dh, &pub_key, NULL);
124
1.13k
  if ((buf = sshbuf_new()) == NULL)
125
0
    return SSH_ERR_ALLOC_FAIL;
126
1.13k
  if ((r = sshbuf_put_bignum2(buf, pub_key)) != 0 ||
127
1.13k
      (r = sshbuf_get_u32(buf, NULL)) != 0)
128
0
    goto out;
129
#ifdef DEBUG_KEXDH
130
  DHparams_print_fp(stderr, kex->dh);
131
  fprintf(stderr, "pub= ");
132
  BN_print_fp(stderr, pub_key);
133
  fprintf(stderr, "\n");
134
#endif
135
1.13k
  kex->client_pub = buf;
136
1.13k
  buf = NULL;
137
1.13k
 out:
138
1.13k
  sshbuf_free(buf);
139
1.13k
  return r;
140
1.13k
}
141
142
int
143
kex_dh_enc(struct kex *kex, const struct sshbuf *client_blob,
144
    struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
145
265
{
146
265
  const BIGNUM *pub_key;
147
265
  struct sshbuf *server_blob = NULL;
148
265
  int r;
149
150
265
  *server_blobp = NULL;
151
265
  *shared_secretp = NULL;
152
153
265
  if ((r = kex_dh_keygen(kex)) != 0)
154
0
    goto out;
155
265
  DH_get0_key(kex->dh, &pub_key, NULL);
156
265
  if ((server_blob = sshbuf_new()) == NULL) {
157
0
    r = SSH_ERR_ALLOC_FAIL;
158
0
    goto out;
159
0
  }
160
265
  if ((r = sshbuf_put_bignum2(server_blob, pub_key)) != 0 ||
161
265
      (r = sshbuf_get_u32(server_blob, NULL)) != 0)
162
0
    goto out;
163
265
  if ((r = kex_dh_dec(kex, client_blob, shared_secretp)) != 0)
164
9
    goto out;
165
256
  *server_blobp = server_blob;
166
256
  server_blob = NULL;
167
265
 out:
168
265
  DH_free(kex->dh);
169
265
  kex->dh = NULL;
170
265
  sshbuf_free(server_blob);
171
265
  return r;
172
256
}
173
174
int
175
kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
176
    struct sshbuf **shared_secretp)
177
309
{
178
309
  struct sshbuf *buf = NULL;
179
309
  BIGNUM *dh_pub = NULL;
180
309
  int r;
181
182
309
  *shared_secretp = NULL;
183
184
309
  if ((buf = sshbuf_new()) == NULL) {
185
0
    r = SSH_ERR_ALLOC_FAIL;
186
0
    goto out;
187
0
  }
188
309
  if ((r = sshbuf_put_stringb(buf, dh_blob)) != 0 ||
189
309
      (r = sshbuf_get_bignum2(buf, &dh_pub)) != 0)
190
6
    goto out;
191
303
  sshbuf_reset(buf);
192
303
  if ((r = kex_dh_compute_key(kex, dh_pub, buf)) != 0)
193
4
    goto out;
194
299
  *shared_secretp = buf;
195
299
  buf = NULL;
196
309
 out:
197
309
  BN_free(dh_pub);
198
309
  DH_free(kex->dh);
199
309
  kex->dh = NULL;
200
309
  sshbuf_free(buf);
201
309
  return r;
202
299
}
203
#endif /* WITH_OPENSSL */