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