Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: kex-names.c,v 1.4 2024/09/09 02:39:57 djm Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2000, 2001 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 | | #include <stdio.h> |
29 | | #include <stdlib.h> |
30 | | #include <string.h> |
31 | | #include <unistd.h> |
32 | | #include <signal.h> |
33 | | |
34 | | #ifdef WITH_OPENSSL |
35 | | #include <openssl/crypto.h> |
36 | | #include <openssl/evp.h> |
37 | | #endif |
38 | | |
39 | | #include "kex.h" |
40 | | #include "log.h" |
41 | | #include "match.h" |
42 | | #include "digest.h" |
43 | | #include "misc.h" |
44 | | |
45 | | #include "ssherr.h" |
46 | | #include "xmalloc.h" |
47 | | |
48 | | struct kexalg { |
49 | | char *name; |
50 | | u_int type; |
51 | | int ec_nid; |
52 | | int hash_alg; |
53 | | }; |
54 | | static const struct kexalg kexalgs[] = { |
55 | | #ifdef WITH_OPENSSL |
56 | | { KEX_DH1, KEX_DH_GRP1_SHA1, 0, SSH_DIGEST_SHA1 }, |
57 | | { KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 }, |
58 | | { KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 }, |
59 | | { KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 }, |
60 | | { KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 }, |
61 | | { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 }, |
62 | | #ifdef HAVE_EVP_SHA256 |
63 | | { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 }, |
64 | | #endif /* HAVE_EVP_SHA256 */ |
65 | | #ifdef OPENSSL_HAS_ECC |
66 | | { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, |
67 | | NID_X9_62_prime256v1, SSH_DIGEST_SHA256 }, |
68 | | { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, |
69 | | SSH_DIGEST_SHA384 }, |
70 | | # ifdef OPENSSL_HAS_NISTP521 |
71 | | { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, |
72 | | SSH_DIGEST_SHA512 }, |
73 | | # endif /* OPENSSL_HAS_NISTP521 */ |
74 | | #endif /* OPENSSL_HAS_ECC */ |
75 | | #endif /* WITH_OPENSSL */ |
76 | | #if defined(HAVE_EVP_SHA256) || !defined(WITH_OPENSSL) |
77 | | { KEX_CURVE25519_SHA256, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 }, |
78 | | { KEX_CURVE25519_SHA256_OLD, KEX_C25519_SHA256, 0, SSH_DIGEST_SHA256 }, |
79 | | #ifdef USE_SNTRUP761X25519 |
80 | | { KEX_SNTRUP761X25519_SHA512, KEX_KEM_SNTRUP761X25519_SHA512, 0, |
81 | | SSH_DIGEST_SHA512 }, |
82 | | { KEX_SNTRUP761X25519_SHA512_OLD, KEX_KEM_SNTRUP761X25519_SHA512, 0, |
83 | | SSH_DIGEST_SHA512 }, |
84 | | #endif |
85 | | #ifdef USE_MLKEM768X25519 |
86 | | { KEX_MLKEM768X25519_SHA256, KEX_KEM_MLKEM768X25519_SHA256, 0, |
87 | | SSH_DIGEST_SHA256 }, |
88 | | #endif |
89 | | #endif /* HAVE_EVP_SHA256 || !WITH_OPENSSL */ |
90 | | { NULL, 0, -1, -1}, |
91 | | }; |
92 | | |
93 | | char * |
94 | | kex_alg_list(char sep) |
95 | 0 | { |
96 | 0 | char *ret = NULL, *tmp; |
97 | 0 | size_t nlen, rlen = 0; |
98 | 0 | const struct kexalg *k; |
99 | |
|
100 | 0 | for (k = kexalgs; k->name != NULL; k++) { |
101 | 0 | if (ret != NULL) |
102 | 0 | ret[rlen++] = sep; |
103 | 0 | nlen = strlen(k->name); |
104 | 0 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
105 | 0 | free(ret); |
106 | 0 | return NULL; |
107 | 0 | } |
108 | 0 | ret = tmp; |
109 | 0 | memcpy(ret + rlen, k->name, nlen + 1); |
110 | 0 | rlen += nlen; |
111 | 0 | } |
112 | 0 | return ret; |
113 | 0 | } |
114 | | |
115 | | static const struct kexalg * |
116 | | kex_alg_by_name(const char *name) |
117 | 85.4k | { |
118 | 85.4k | const struct kexalg *k; |
119 | | |
120 | 718k | for (k = kexalgs; k->name != NULL; k++) { |
121 | 718k | if (strcmp(k->name, name) == 0) |
122 | 85.3k | return k; |
123 | 718k | } |
124 | 90 | return NULL; |
125 | 85.4k | } |
126 | | |
127 | | int |
128 | | kex_name_valid(const char *name) |
129 | 21.4k | { |
130 | 21.4k | return kex_alg_by_name(name) != NULL; |
131 | 21.4k | } |
132 | | |
133 | | u_int |
134 | | kex_type_from_name(const char *name) |
135 | 21.3k | { |
136 | 21.3k | const struct kexalg *k; |
137 | | |
138 | 21.3k | if ((k = kex_alg_by_name(name)) == NULL) |
139 | 0 | return 0; |
140 | 21.3k | return k->type; |
141 | 21.3k | } |
142 | | |
143 | | int |
144 | | kex_hash_from_name(const char *name) |
145 | 21.3k | { |
146 | 21.3k | const struct kexalg *k; |
147 | | |
148 | 21.3k | if ((k = kex_alg_by_name(name)) == NULL) |
149 | 0 | return -1; |
150 | 21.3k | return k->hash_alg; |
151 | 21.3k | } |
152 | | |
153 | | int |
154 | | kex_nid_from_name(const char *name) |
155 | 21.3k | { |
156 | 21.3k | const struct kexalg *k; |
157 | | |
158 | 21.3k | if ((k = kex_alg_by_name(name)) == NULL) |
159 | 0 | return -1; |
160 | 21.3k | return k->ec_nid; |
161 | 21.3k | } |
162 | | |
163 | | /* Validate KEX method name list */ |
164 | | int |
165 | | kex_names_valid(const char *names) |
166 | 0 | { |
167 | 0 | char *s, *cp, *p; |
168 | |
|
169 | 0 | if (names == NULL || strcmp(names, "") == 0) |
170 | 0 | return 0; |
171 | 0 | if ((s = cp = strdup(names)) == NULL) |
172 | 0 | return 0; |
173 | 0 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
174 | 0 | (p = strsep(&cp, ","))) { |
175 | 0 | if (kex_alg_by_name(p) == NULL) { |
176 | 0 | error("Unsupported KEX algorithm \"%.100s\"", p); |
177 | 0 | free(s); |
178 | 0 | return 0; |
179 | 0 | } |
180 | 0 | } |
181 | 0 | debug3("kex names ok: [%s]", names); |
182 | 0 | free(s); |
183 | 0 | return 1; |
184 | 0 | } |
185 | | |
186 | | /* returns non-zero if proposal contains any algorithm from algs */ |
187 | | int |
188 | | kex_has_any_alg(const char *proposal, const char *algs) |
189 | 581k | { |
190 | 581k | char *cp; |
191 | | |
192 | 581k | if ((cp = match_list(proposal, algs, NULL)) == NULL) |
193 | 563k | return 0; |
194 | 17.5k | free(cp); |
195 | 17.5k | return 1; |
196 | 581k | } |
197 | | |
198 | | /* |
199 | | * Concatenate algorithm names, avoiding duplicates in the process. |
200 | | * Caller must free returned string. |
201 | | */ |
202 | | char * |
203 | | kex_names_cat(const char *a, const char *b) |
204 | 179k | { |
205 | 179k | char *ret = NULL, *tmp = NULL, *cp, *p; |
206 | 179k | size_t len; |
207 | | |
208 | 179k | if (a == NULL || *a == '\0') |
209 | 0 | return strdup(b); |
210 | 179k | if (b == NULL || *b == '\0') |
211 | 0 | return strdup(a); |
212 | 179k | if (strlen(b) > 1024*1024) |
213 | 0 | return NULL; |
214 | 179k | len = strlen(a) + strlen(b) + 2; |
215 | 179k | if ((tmp = cp = strdup(b)) == NULL || |
216 | 179k | (ret = calloc(1, len)) == NULL) { |
217 | 0 | free(tmp); |
218 | 0 | return NULL; |
219 | 0 | } |
220 | 179k | strlcpy(ret, a, len); |
221 | 537k | for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) { |
222 | 358k | if (kex_has_any_alg(ret, p)) |
223 | 0 | continue; /* Algorithm already present */ |
224 | 358k | if (strlcat(ret, ",", len) >= len || |
225 | 358k | strlcat(ret, p, len) >= len) { |
226 | 0 | free(tmp); |
227 | 0 | free(ret); |
228 | 0 | return NULL; /* Shouldn't happen */ |
229 | 0 | } |
230 | 358k | } |
231 | 179k | free(tmp); |
232 | 179k | return ret; |
233 | 179k | } |
234 | | |
235 | | /* |
236 | | * Assemble a list of algorithms from a default list and a string from a |
237 | | * configuration file. The user-provided string may begin with '+' to |
238 | | * indicate that it should be appended to the default, '-' that the |
239 | | * specified names should be removed, or '^' that they should be placed |
240 | | * at the head. |
241 | | */ |
242 | | int |
243 | | kex_assemble_names(char **listp, const char *def, const char *all) |
244 | 0 | { |
245 | 0 | char *cp, *tmp, *patterns; |
246 | 0 | char *list = NULL, *ret = NULL, *matching = NULL, *opatterns = NULL; |
247 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
248 | |
|
249 | 0 | if (listp == NULL || def == NULL || all == NULL) |
250 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
251 | | |
252 | 0 | if (*listp == NULL || **listp == '\0') { |
253 | 0 | if ((*listp = strdup(def)) == NULL) |
254 | 0 | return SSH_ERR_ALLOC_FAIL; |
255 | 0 | return 0; |
256 | 0 | } |
257 | | |
258 | 0 | list = *listp; |
259 | 0 | *listp = NULL; |
260 | 0 | if (*list == '+') { |
261 | | /* Append names to default list */ |
262 | 0 | if ((tmp = kex_names_cat(def, list + 1)) == NULL) { |
263 | 0 | r = SSH_ERR_ALLOC_FAIL; |
264 | 0 | goto fail; |
265 | 0 | } |
266 | 0 | free(list); |
267 | 0 | list = tmp; |
268 | 0 | } else if (*list == '-') { |
269 | | /* Remove names from default list */ |
270 | 0 | if ((*listp = match_filter_denylist(def, list + 1)) == NULL) { |
271 | 0 | r = SSH_ERR_ALLOC_FAIL; |
272 | 0 | goto fail; |
273 | 0 | } |
274 | 0 | free(list); |
275 | | /* filtering has already been done */ |
276 | 0 | return 0; |
277 | 0 | } else if (*list == '^') { |
278 | | /* Place names at head of default list */ |
279 | 0 | if ((tmp = kex_names_cat(list + 1, def)) == NULL) { |
280 | 0 | r = SSH_ERR_ALLOC_FAIL; |
281 | 0 | goto fail; |
282 | 0 | } |
283 | 0 | free(list); |
284 | 0 | list = tmp; |
285 | 0 | } else { |
286 | | /* Explicit list, overrides default - just use "list" as is */ |
287 | 0 | } |
288 | | |
289 | | /* |
290 | | * The supplied names may be a pattern-list. For the -list case, |
291 | | * the patterns are applied above. For the +list and explicit list |
292 | | * cases we need to do it now. |
293 | | */ |
294 | 0 | ret = NULL; |
295 | 0 | if ((patterns = opatterns = strdup(list)) == NULL) { |
296 | 0 | r = SSH_ERR_ALLOC_FAIL; |
297 | 0 | goto fail; |
298 | 0 | } |
299 | | /* Apply positive (i.e. non-negated) patterns from the list */ |
300 | 0 | while ((cp = strsep(&patterns, ",")) != NULL) { |
301 | 0 | if (*cp == '!') { |
302 | | /* negated matches are not supported here */ |
303 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
304 | 0 | goto fail; |
305 | 0 | } |
306 | 0 | free(matching); |
307 | 0 | if ((matching = match_filter_allowlist(all, cp)) == NULL) { |
308 | 0 | r = SSH_ERR_ALLOC_FAIL; |
309 | 0 | goto fail; |
310 | 0 | } |
311 | 0 | if ((tmp = kex_names_cat(ret, matching)) == NULL) { |
312 | 0 | r = SSH_ERR_ALLOC_FAIL; |
313 | 0 | goto fail; |
314 | 0 | } |
315 | 0 | free(ret); |
316 | 0 | ret = tmp; |
317 | 0 | } |
318 | 0 | if (ret == NULL || *ret == '\0') { |
319 | | /* An empty name-list is an error */ |
320 | | /* XXX better error code? */ |
321 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
322 | 0 | goto fail; |
323 | 0 | } |
324 | | |
325 | | /* success */ |
326 | 0 | *listp = ret; |
327 | 0 | ret = NULL; |
328 | 0 | r = 0; |
329 | |
|
330 | 0 | fail: |
331 | 0 | free(matching); |
332 | 0 | free(opatterns); |
333 | 0 | free(list); |
334 | 0 | free(ret); |
335 | 0 | return r; |
336 | 0 | } |