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