Coverage Report

Created: 2026-09-07 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/kexecdh.c
Line
Count
Source
1
/* $OpenBSD: kexecdh.c,v 1.14 2026/08/03 06:43:16 djm Exp $ */
2
/*
3
 * Copyright (c) 2010 Damien Miller.  All rights reserved.
4
 * Copyright (c) 2019 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
#if defined(WITH_OPENSSL)
30
31
#include <sys/types.h>
32
33
#include <stdio.h>
34
#include <signal.h>
35
36
#include <openssl/bn.h>
37
#include <openssl/ecdh.h>
38
39
#include "sshkey.h"
40
#include "kex.h"
41
#include "sshbuf.h"
42
#include "ssherr.h"
43
44
int
45
kex_ecdh_keypair(struct kex *kex)
46
0
{
47
0
  EC_KEY *client_key = NULL;
48
0
  const EC_GROUP *group;
49
0
  const EC_POINT *public_key;
50
0
  struct sshbuf *buf = NULL;
51
0
  int r;
52
53
0
  if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
54
0
    r = SSH_ERR_ALLOC_FAIL;
55
0
    goto out;
56
0
  }
57
0
  if (EC_KEY_generate_key(client_key) != 1) {
58
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
59
0
    goto out;
60
0
  }
61
0
  group = EC_KEY_get0_group(client_key);
62
0
  public_key = EC_KEY_get0_public_key(client_key);
63
64
0
  if ((buf = sshbuf_new()) == NULL) {
65
0
    r = SSH_ERR_ALLOC_FAIL;
66
0
    goto out;
67
0
  }
68
0
  if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 ||
69
0
      (r = sshbuf_get_u32(buf, NULL)) != 0)
70
0
    goto out;
71
#ifdef DEBUG_KEXECDH
72
  fputs("client private key:\n", stderr);
73
  sshkey_dump_ec_key(client_key);
74
#endif
75
0
  kex->ec_client_key = client_key;
76
0
  kex->ec_group = group;
77
0
  client_key = NULL; /* owned by the kex */
78
0
  kex->client_pub = buf;
79
0
  buf = NULL;
80
0
 out:
81
0
  EC_KEY_free(client_key);
82
0
  sshbuf_free(buf);
83
0
  return r;
84
0
}
85
86
int
87
kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
88
    struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
89
0
{
90
0
  const EC_GROUP *group;
91
0
  const EC_POINT *pub_key;
92
0
  EC_KEY *server_key = NULL;
93
0
  struct sshbuf *server_blob = NULL;
94
0
  int r;
95
96
0
  *server_blobp = NULL;
97
0
  *shared_secretp = NULL;
98
99
0
  if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
100
0
    r = SSH_ERR_ALLOC_FAIL;
101
0
    goto out;
102
0
  }
103
0
  if (EC_KEY_generate_key(server_key) != 1) {
104
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
105
0
    goto out;
106
0
  }
107
0
  group = EC_KEY_get0_group(server_key);
108
109
#ifdef DEBUG_KEXECDH
110
  fputs("server private key:\n", stderr);
111
  sshkey_dump_ec_key(server_key);
112
#endif
113
0
  pub_key = EC_KEY_get0_public_key(server_key);
114
0
  if ((server_blob = sshbuf_new()) == NULL) {
115
0
    r = SSH_ERR_ALLOC_FAIL;
116
0
    goto out;
117
0
  }
118
0
  if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 ||
119
0
      (r = sshbuf_get_u32(server_blob, NULL)) != 0)
120
0
    goto out;
121
0
  if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group,
122
0
      0, shared_secretp)) != 0)
123
0
    goto out;
124
0
  *server_blobp = server_blob;
125
0
  server_blob = NULL;
126
0
 out:
127
0
  EC_KEY_free(server_key);
128
0
  sshbuf_free(server_blob);
129
0
  return r;
130
0
}
131
132
int
133
kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
134
    EC_KEY *key, const EC_GROUP *group, int raw, struct sshbuf **shared_secretp)
135
0
{
136
0
  struct sshbuf *buf = NULL;
137
0
  BIGNUM *shared_secret = NULL;
138
0
  EC_POINT *dh_pub = NULL;
139
0
  u_char *kbuf = NULL;
140
0
  size_t klen = 0;
141
0
  int r;
142
143
0
  *shared_secretp = NULL;
144
145
0
  if ((buf = sshbuf_new()) == NULL) {
146
0
    r = SSH_ERR_ALLOC_FAIL;
147
0
    goto out;
148
0
  }
149
0
  if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
150
0
    goto out;
151
0
  if ((dh_pub = EC_POINT_new(group)) == NULL) {
152
0
    r = SSH_ERR_ALLOC_FAIL;
153
0
    goto out;
154
0
  }
155
0
  if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) {
156
0
    goto out;
157
0
  }
158
0
  sshbuf_reset(buf);
159
160
#ifdef DEBUG_KEXECDH
161
  fputs("public key:\n", stderr);
162
  sshkey_dump_ec_point(group, dh_pub);
163
#endif
164
0
  if (sshkey_ec_validate_public(group, dh_pub) != 0) {
165
0
    r = SSH_ERR_MESSAGE_INCOMPLETE;
166
0
    goto out;
167
0
  }
168
0
  klen = (EC_GROUP_get_degree(group) + 7) / 8;
169
0
  if ((kbuf = malloc(klen)) == NULL) {
170
0
    r = SSH_ERR_ALLOC_FAIL;
171
0
    goto out;
172
0
  }
173
0
  if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen) {
174
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
175
0
    goto out;
176
0
  }
177
#ifdef DEBUG_KEXECDH
178
  dump_digest("shared secret", kbuf, klen);
179
#endif
180
0
  if (raw) {
181
0
    if ((r = sshbuf_put(buf, kbuf, klen)) != 0)
182
0
      goto out;
183
0
  } else {
184
0
    if ((shared_secret = BN_new()) == NULL) {
185
0
      r = SSH_ERR_ALLOC_FAIL;
186
0
      goto out;
187
0
    }
188
0
    if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
189
0
      r = SSH_ERR_LIBCRYPTO_ERROR;
190
0
      goto out;
191
0
    }
192
0
    if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)
193
0
      goto out;
194
0
  }
195
0
  *shared_secretp = buf;
196
0
  buf = NULL;
197
0
 out:
198
0
  EC_POINT_clear_free(dh_pub);
199
0
  BN_clear_free(shared_secret);
200
0
  freezero(kbuf, klen);
201
0
  sshbuf_free(buf);
202
0
  return r;
203
0
}
204
205
int
206
kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
207
    struct sshbuf **shared_secretp)
208
0
{
209
0
  int r;
210
211
0
  r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key,
212
0
      kex->ec_group, 0, shared_secretp);
213
0
  EC_KEY_free(kex->ec_client_key);
214
  kex->ec_client_key = NULL;
215
0
  return r;
216
0
}
217
218
#endif /* WITH_OPENSSL */