Coverage Report

Created: 2025-07-18 06:58

/src/hpn-ssh/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
0
{
50
0
  switch (kex->kex_type) {
51
0
  case KEX_DH_GRP1_SHA1:
52
0
    kex->dh = dh_new_group1();
53
0
    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
0
  }
67
0
  if (kex->dh == NULL)
68
0
    return SSH_ERR_ALLOC_FAIL;
69
0
  return (dh_gen_key(kex->dh, kex->we_need * 8));
70
0
}
71
72
int
73
kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
74
0
{
75
0
  BIGNUM *shared_secret = NULL;
76
0
  u_char *kbuf = NULL;
77
0
  size_t klen = 0;
78
0
  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
0
  if (!dh_pub_is_valid(kex->dh, dh_pub)) {
90
0
    r = SSH_ERR_MESSAGE_INCOMPLETE;
91
0
    goto out;
92
0
  }
93
0
  klen = DH_size(kex->dh);
94
0
  if ((kbuf = malloc(klen)) == NULL ||
95
0
      (shared_secret = BN_new()) == NULL) {
96
0
    r = SSH_ERR_ALLOC_FAIL;
97
0
    goto out;
98
0
  }
99
0
  if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
100
0
      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
0
  r = sshbuf_put_bignum2(out, shared_secret);
108
0
 out:
109
0
  freezero(kbuf, klen);
110
0
  BN_clear_free(shared_secret);
111
0
  return r;
112
0
}
113
114
int
115
kex_dh_keypair(struct kex *kex)
116
0
{
117
0
  const BIGNUM *pub_key;
118
0
  struct sshbuf *buf = NULL;
119
0
  int r;
120
121
0
  if ((r = kex_dh_keygen(kex)) != 0)
122
0
    return r;
123
0
  DH_get0_key(kex->dh, &pub_key, NULL);
124
0
  if ((buf = sshbuf_new()) == NULL)
125
0
    return SSH_ERR_ALLOC_FAIL;
126
0
  if ((r = sshbuf_put_bignum2(buf, pub_key)) != 0 ||
127
0
      (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
0
  kex->client_pub = buf;
136
0
  buf = NULL;
137
0
 out:
138
0
  sshbuf_free(buf);
139
0
  return r;
140
0
}
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
0
{
146
0
  const BIGNUM *pub_key;
147
0
  struct sshbuf *server_blob = NULL;
148
0
  int r;
149
150
0
  *server_blobp = NULL;
151
0
  *shared_secretp = NULL;
152
153
0
  if ((r = kex_dh_keygen(kex)) != 0)
154
0
    goto out;
155
0
  DH_get0_key(kex->dh, &pub_key, NULL);
156
0
  if ((server_blob = sshbuf_new()) == NULL) {
157
0
    r = SSH_ERR_ALLOC_FAIL;
158
0
    goto out;
159
0
  }
160
0
  if ((r = sshbuf_put_bignum2(server_blob, pub_key)) != 0 ||
161
0
      (r = sshbuf_get_u32(server_blob, NULL)) != 0)
162
0
    goto out;
163
0
  if ((r = kex_dh_dec(kex, client_blob, shared_secretp)) != 0)
164
0
    goto out;
165
0
  *server_blobp = server_blob;
166
0
  server_blob = NULL;
167
0
 out:
168
0
  DH_free(kex->dh);
169
0
  kex->dh = NULL;
170
0
  sshbuf_free(server_blob);
171
0
  return r;
172
0
}
173
174
int
175
kex_dh_dec(struct kex *kex, const struct sshbuf *dh_blob,
176
    struct sshbuf **shared_secretp)
177
0
{
178
0
  struct sshbuf *buf = NULL;
179
0
  BIGNUM *dh_pub = NULL;
180
0
  int r;
181
182
0
  *shared_secretp = NULL;
183
184
0
  if ((buf = sshbuf_new()) == NULL) {
185
0
    r = SSH_ERR_ALLOC_FAIL;
186
0
    goto out;
187
0
  }
188
0
  if ((r = sshbuf_put_stringb(buf, dh_blob)) != 0 ||
189
0
      (r = sshbuf_get_bignum2(buf, &dh_pub)) != 0)
190
0
    goto out;
191
0
  sshbuf_reset(buf);
192
0
  if ((r = kex_dh_compute_key(kex, dh_pub, buf)) != 0)
193
0
    goto out;
194
0
  *shared_secretp = buf;
195
0
  buf = NULL;
196
0
 out:
197
0
  BN_free(dh_pub);
198
0
  DH_free(kex->dh);
199
0
  kex->dh = NULL;
200
0
  sshbuf_free(buf);
201
0
  return r;
202
0
}
203
#endif /* WITH_OPENSSL */