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