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