Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: sshkey.c,v 1.150 2025/05/12 05:41:20 tb Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
4 | | * Copyright (c) 2008 Alexander von Gernler. All rights reserved. |
5 | | * Copyright (c) 2010,2011 Damien Miller. All rights reserved. |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * 2. Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "includes.h" |
29 | | |
30 | | #include <sys/types.h> |
31 | | #include <sys/mman.h> |
32 | | #include <netinet/in.h> |
33 | | |
34 | | #ifdef WITH_OPENSSL |
35 | | #include <openssl/evp.h> |
36 | | #include <openssl/err.h> |
37 | | #include <openssl/pem.h> |
38 | | #endif |
39 | | |
40 | | #include "crypto_api.h" |
41 | | |
42 | | #include <errno.h> |
43 | | #include <limits.h> |
44 | | #include <stdio.h> |
45 | | #include <stdlib.h> |
46 | | #include <string.h> |
47 | | #include <resolv.h> |
48 | | #include <time.h> |
49 | | #ifdef HAVE_UTIL_H |
50 | | #include <util.h> |
51 | | #endif /* HAVE_UTIL_H */ |
52 | | |
53 | | #include "ssh2.h" |
54 | | #include "ssherr.h" |
55 | | #include "misc.h" |
56 | | #include "sshbuf.h" |
57 | | #include "cipher.h" |
58 | | #include "digest.h" |
59 | | #define SSHKEY_INTERNAL |
60 | | #include "sshkey.h" |
61 | | #include "match.h" |
62 | | #include "ssh-sk.h" |
63 | | |
64 | | #ifdef WITH_XMSS |
65 | | #include "sshkey-xmss.h" |
66 | | #include "xmss_fast.h" |
67 | | #endif |
68 | | |
69 | | #include "openbsd-compat/openssl-compat.h" |
70 | | |
71 | | /* openssh private key file format */ |
72 | 0 | #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n" |
73 | 0 | #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n" |
74 | 0 | #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1) |
75 | 0 | #define MARK_END_LEN (sizeof(MARK_END) - 1) |
76 | 0 | #define KDFNAME "bcrypt" |
77 | 0 | #define AUTH_MAGIC "openssh-key-v1" |
78 | 0 | #define SALT_LEN 16 |
79 | 0 | #define DEFAULT_CIPHERNAME "aes256-ctr" |
80 | 0 | #define DEFAULT_ROUNDS 24 |
81 | | |
82 | | /* |
83 | | * Constants relating to "shielding" support; protection of keys expected |
84 | | * to remain in memory for long durations |
85 | | */ |
86 | 0 | #define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024) |
87 | 0 | #define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */ |
88 | 0 | #define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512 |
89 | | |
90 | | int sshkey_private_serialize_opt(struct sshkey *key, |
91 | | struct sshbuf *buf, enum sshkey_serialize_rep); |
92 | | static int sshkey_from_blob_internal(struct sshbuf *buf, |
93 | | struct sshkey **keyp, int allow_cert); |
94 | | |
95 | | /* Supported key types */ |
96 | | extern const struct sshkey_impl sshkey_ed25519_impl; |
97 | | extern const struct sshkey_impl sshkey_ed25519_cert_impl; |
98 | | extern const struct sshkey_impl sshkey_ed25519_sk_impl; |
99 | | extern const struct sshkey_impl sshkey_ed25519_sk_cert_impl; |
100 | | #ifdef WITH_OPENSSL |
101 | | # ifdef OPENSSL_HAS_ECC |
102 | | # ifdef ENABLE_SK |
103 | | extern const struct sshkey_impl sshkey_ecdsa_sk_impl; |
104 | | extern const struct sshkey_impl sshkey_ecdsa_sk_cert_impl; |
105 | | extern const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl; |
106 | | # endif /* ENABLE_SK */ |
107 | | extern const struct sshkey_impl sshkey_ecdsa_nistp256_impl; |
108 | | extern const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl; |
109 | | extern const struct sshkey_impl sshkey_ecdsa_nistp384_impl; |
110 | | extern const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl; |
111 | | # ifdef OPENSSL_HAS_NISTP521 |
112 | | extern const struct sshkey_impl sshkey_ecdsa_nistp521_impl; |
113 | | extern const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl; |
114 | | # endif /* OPENSSL_HAS_NISTP521 */ |
115 | | # endif /* OPENSSL_HAS_ECC */ |
116 | | extern const struct sshkey_impl sshkey_rsa_impl; |
117 | | extern const struct sshkey_impl sshkey_rsa_cert_impl; |
118 | | extern const struct sshkey_impl sshkey_rsa_sha256_impl; |
119 | | extern const struct sshkey_impl sshkey_rsa_sha256_cert_impl; |
120 | | extern const struct sshkey_impl sshkey_rsa_sha512_impl; |
121 | | extern const struct sshkey_impl sshkey_rsa_sha512_cert_impl; |
122 | | #endif /* WITH_OPENSSL */ |
123 | | #ifdef WITH_XMSS |
124 | | extern const struct sshkey_impl sshkey_xmss_impl; |
125 | | extern const struct sshkey_impl sshkey_xmss_cert_impl; |
126 | | #endif |
127 | | |
128 | | const struct sshkey_impl * const keyimpls[] = { |
129 | | &sshkey_ed25519_impl, |
130 | | &sshkey_ed25519_cert_impl, |
131 | | #ifdef ENABLE_SK |
132 | | &sshkey_ed25519_sk_impl, |
133 | | &sshkey_ed25519_sk_cert_impl, |
134 | | #endif |
135 | | #ifdef WITH_OPENSSL |
136 | | # ifdef OPENSSL_HAS_ECC |
137 | | &sshkey_ecdsa_nistp256_impl, |
138 | | &sshkey_ecdsa_nistp256_cert_impl, |
139 | | &sshkey_ecdsa_nistp384_impl, |
140 | | &sshkey_ecdsa_nistp384_cert_impl, |
141 | | # ifdef OPENSSL_HAS_NISTP521 |
142 | | &sshkey_ecdsa_nistp521_impl, |
143 | | &sshkey_ecdsa_nistp521_cert_impl, |
144 | | # endif /* OPENSSL_HAS_NISTP521 */ |
145 | | # ifdef ENABLE_SK |
146 | | &sshkey_ecdsa_sk_impl, |
147 | | &sshkey_ecdsa_sk_cert_impl, |
148 | | &sshkey_ecdsa_sk_webauthn_impl, |
149 | | # endif /* ENABLE_SK */ |
150 | | # endif /* OPENSSL_HAS_ECC */ |
151 | | &sshkey_rsa_impl, |
152 | | &sshkey_rsa_cert_impl, |
153 | | &sshkey_rsa_sha256_impl, |
154 | | &sshkey_rsa_sha256_cert_impl, |
155 | | &sshkey_rsa_sha512_impl, |
156 | | &sshkey_rsa_sha512_cert_impl, |
157 | | #endif /* WITH_OPENSSL */ |
158 | | #ifdef WITH_XMSS |
159 | | &sshkey_xmss_impl, |
160 | | &sshkey_xmss_cert_impl, |
161 | | #endif |
162 | | NULL |
163 | | }; |
164 | | |
165 | | static const struct sshkey_impl * |
166 | | sshkey_impl_from_type(int type) |
167 | 0 | { |
168 | 0 | int i; |
169 | |
|
170 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
171 | 0 | if (keyimpls[i]->type == type) |
172 | 0 | return keyimpls[i]; |
173 | 0 | } |
174 | 0 | return NULL; |
175 | 0 | } |
176 | | |
177 | | static const struct sshkey_impl * |
178 | | sshkey_impl_from_type_nid(int type, int nid) |
179 | 0 | { |
180 | 0 | int i; |
181 | |
|
182 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
183 | 0 | if (keyimpls[i]->type == type && |
184 | 0 | (keyimpls[i]->nid == 0 || keyimpls[i]->nid == nid)) |
185 | 0 | return keyimpls[i]; |
186 | 0 | } |
187 | 0 | return NULL; |
188 | 0 | } |
189 | | |
190 | | static const struct sshkey_impl * |
191 | | sshkey_impl_from_key(const struct sshkey *k) |
192 | 0 | { |
193 | 0 | if (k == NULL) |
194 | 0 | return NULL; |
195 | 0 | return sshkey_impl_from_type_nid(k->type, k->ecdsa_nid); |
196 | 0 | } |
197 | | |
198 | | const char * |
199 | | sshkey_type(const struct sshkey *k) |
200 | 0 | { |
201 | 0 | const struct sshkey_impl *impl; |
202 | |
|
203 | 0 | if ((impl = sshkey_impl_from_key(k)) == NULL) |
204 | 0 | return "unknown"; |
205 | 0 | return impl->shortname; |
206 | 0 | } |
207 | | |
208 | | static const char * |
209 | | sshkey_ssh_name_from_type_nid(int type, int nid) |
210 | 0 | { |
211 | 0 | const struct sshkey_impl *impl; |
212 | |
|
213 | 0 | if ((impl = sshkey_impl_from_type_nid(type, nid)) == NULL) |
214 | 0 | return "ssh-unknown"; |
215 | 0 | return impl->name; |
216 | 0 | } |
217 | | |
218 | | int |
219 | | sshkey_type_is_cert(int type) |
220 | 0 | { |
221 | 0 | const struct sshkey_impl *impl; |
222 | |
|
223 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) |
224 | 0 | return 0; |
225 | 0 | return impl->cert; |
226 | 0 | } |
227 | | |
228 | | const char * |
229 | | sshkey_ssh_name(const struct sshkey *k) |
230 | 0 | { |
231 | 0 | return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid); |
232 | 0 | } |
233 | | |
234 | | const char * |
235 | | sshkey_ssh_name_plain(const struct sshkey *k) |
236 | 0 | { |
237 | 0 | return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type), |
238 | 0 | k->ecdsa_nid); |
239 | 0 | } |
240 | | |
241 | | static int |
242 | | type_from_name(const char *name, int allow_short) |
243 | 0 | { |
244 | 0 | int i; |
245 | 0 | const struct sshkey_impl *impl; |
246 | |
|
247 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
248 | 0 | impl = keyimpls[i]; |
249 | 0 | if (impl->name != NULL && strcmp(name, impl->name) == 0) |
250 | 0 | return impl->type; |
251 | | /* Only allow shortname matches for plain key types */ |
252 | 0 | if (allow_short && !impl->cert && impl->shortname != NULL && |
253 | 0 | strcasecmp(impl->shortname, name) == 0) |
254 | 0 | return impl->type; |
255 | 0 | } |
256 | 0 | return KEY_UNSPEC; |
257 | 0 | } |
258 | | |
259 | | int |
260 | | sshkey_type_from_name(const char *name) |
261 | 0 | { |
262 | 0 | return type_from_name(name, 0); |
263 | 0 | } |
264 | | |
265 | | int |
266 | | sshkey_type_from_shortname(const char *name) |
267 | 0 | { |
268 | 0 | return type_from_name(name, 1); |
269 | 0 | } |
270 | | |
271 | | static int |
272 | | key_type_is_ecdsa_variant(int type) |
273 | 0 | { |
274 | 0 | switch (type) { |
275 | 0 | case KEY_ECDSA: |
276 | 0 | case KEY_ECDSA_CERT: |
277 | 0 | case KEY_ECDSA_SK: |
278 | 0 | case KEY_ECDSA_SK_CERT: |
279 | 0 | return 1; |
280 | 0 | } |
281 | 0 | return 0; |
282 | 0 | } |
283 | | |
284 | | int |
285 | | sshkey_ecdsa_nid_from_name(const char *name) |
286 | 0 | { |
287 | 0 | int i; |
288 | |
|
289 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
290 | 0 | if (!key_type_is_ecdsa_variant(keyimpls[i]->type)) |
291 | 0 | continue; |
292 | 0 | if (keyimpls[i]->name != NULL && |
293 | 0 | strcmp(name, keyimpls[i]->name) == 0) |
294 | 0 | return keyimpls[i]->nid; |
295 | 0 | } |
296 | 0 | return -1; |
297 | 0 | } |
298 | | |
299 | | int |
300 | | sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs) |
301 | 0 | { |
302 | 0 | int ktype; |
303 | |
|
304 | 0 | if (sigalgs == NULL || *sigalgs == '\0' || |
305 | 0 | (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC) |
306 | 0 | return 0; |
307 | 0 | else if (ktype == KEY_RSA) { |
308 | 0 | return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 || |
309 | 0 | match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 || |
310 | 0 | match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1; |
311 | 0 | } else if (ktype == KEY_RSA_CERT) { |
312 | 0 | return match_pattern_list("ssh-rsa-cert-v01@openssh.com", |
313 | 0 | sigalgs, 0) == 1 || |
314 | 0 | match_pattern_list("rsa-sha2-256-cert-v01@openssh.com", |
315 | 0 | sigalgs, 0) == 1 || |
316 | 0 | match_pattern_list("rsa-sha2-512-cert-v01@openssh.com", |
317 | 0 | sigalgs, 0) == 1; |
318 | 0 | } else |
319 | 0 | return match_pattern_list(keyname, sigalgs, 0) == 1; |
320 | 0 | } |
321 | | |
322 | | char * |
323 | | sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep) |
324 | 0 | { |
325 | 0 | char *tmp, *ret = NULL; |
326 | 0 | size_t i, nlen, rlen = 0; |
327 | 0 | const struct sshkey_impl *impl; |
328 | |
|
329 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
330 | 0 | impl = keyimpls[i]; |
331 | 0 | if (impl->name == NULL) |
332 | 0 | continue; |
333 | 0 | if (!include_sigonly && impl->sigonly) |
334 | 0 | continue; |
335 | 0 | if ((certs_only && !impl->cert) || (plain_only && impl->cert)) |
336 | 0 | continue; |
337 | 0 | if (ret != NULL) |
338 | 0 | ret[rlen++] = sep; |
339 | 0 | nlen = strlen(impl->name); |
340 | 0 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
341 | 0 | free(ret); |
342 | 0 | return NULL; |
343 | 0 | } |
344 | 0 | ret = tmp; |
345 | 0 | memcpy(ret + rlen, impl->name, nlen + 1); |
346 | 0 | rlen += nlen; |
347 | 0 | } |
348 | 0 | return ret; |
349 | 0 | } |
350 | | |
351 | | int |
352 | | sshkey_names_valid2(const char *names, int allow_wildcard, int plain_only) |
353 | 0 | { |
354 | 0 | char *s, *cp, *p; |
355 | 0 | const struct sshkey_impl *impl; |
356 | 0 | int i, type; |
357 | |
|
358 | 0 | if (names == NULL || strcmp(names, "") == 0) |
359 | 0 | return 0; |
360 | 0 | if ((s = cp = strdup(names)) == NULL) |
361 | 0 | return 0; |
362 | 0 | for ((p = strsep(&cp, ",")); p && *p != '\0'; |
363 | 0 | (p = strsep(&cp, ","))) { |
364 | 0 | type = sshkey_type_from_name(p); |
365 | 0 | if (type == KEY_UNSPEC) { |
366 | 0 | if (allow_wildcard) { |
367 | | /* |
368 | | * Try matching key types against the string. |
369 | | * If any has a positive or negative match then |
370 | | * the component is accepted. |
371 | | */ |
372 | 0 | impl = NULL; |
373 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
374 | 0 | if (match_pattern_list( |
375 | 0 | keyimpls[i]->name, p, 0) != 0) { |
376 | 0 | impl = keyimpls[i]; |
377 | 0 | break; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | if (impl != NULL) |
381 | 0 | continue; |
382 | 0 | } |
383 | 0 | free(s); |
384 | 0 | return 0; |
385 | 0 | } else if (plain_only && sshkey_type_is_cert(type)) { |
386 | 0 | free(s); |
387 | 0 | return 0; |
388 | 0 | } |
389 | 0 | } |
390 | 0 | free(s); |
391 | 0 | return 1; |
392 | 0 | } |
393 | | |
394 | | u_int |
395 | | sshkey_size(const struct sshkey *k) |
396 | 0 | { |
397 | 0 | const struct sshkey_impl *impl; |
398 | |
|
399 | 0 | if ((impl = sshkey_impl_from_key(k)) == NULL) |
400 | 0 | return 0; |
401 | 0 | if (impl->funcs->size != NULL) |
402 | 0 | return impl->funcs->size(k); |
403 | 0 | return impl->keybits; |
404 | 0 | } |
405 | | |
406 | | static int |
407 | | sshkey_type_is_valid_ca(int type) |
408 | 0 | { |
409 | 0 | const struct sshkey_impl *impl; |
410 | |
|
411 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) |
412 | 0 | return 0; |
413 | | /* All non-certificate types may act as CAs */ |
414 | 0 | return !impl->cert; |
415 | 0 | } |
416 | | |
417 | | int |
418 | | sshkey_is_cert(const struct sshkey *k) |
419 | 0 | { |
420 | 0 | if (k == NULL) |
421 | 0 | return 0; |
422 | 0 | return sshkey_type_is_cert(k->type); |
423 | 0 | } |
424 | | |
425 | | int |
426 | | sshkey_is_sk(const struct sshkey *k) |
427 | 0 | { |
428 | 0 | if (k == NULL) |
429 | 0 | return 0; |
430 | 0 | switch (sshkey_type_plain(k->type)) { |
431 | 0 | case KEY_ECDSA_SK: |
432 | 0 | case KEY_ED25519_SK: |
433 | 0 | return 1; |
434 | 0 | default: |
435 | 0 | return 0; |
436 | 0 | } |
437 | 0 | } |
438 | | |
439 | | /* Return the cert-less equivalent to a certified key type */ |
440 | | int |
441 | | sshkey_type_plain(int type) |
442 | 0 | { |
443 | 0 | switch (type) { |
444 | 0 | case KEY_RSA_CERT: |
445 | 0 | return KEY_RSA; |
446 | 0 | case KEY_ECDSA_CERT: |
447 | 0 | return KEY_ECDSA; |
448 | 0 | case KEY_ECDSA_SK_CERT: |
449 | 0 | return KEY_ECDSA_SK; |
450 | 0 | case KEY_ED25519_CERT: |
451 | 0 | return KEY_ED25519; |
452 | 0 | case KEY_ED25519_SK_CERT: |
453 | 0 | return KEY_ED25519_SK; |
454 | 0 | case KEY_XMSS_CERT: |
455 | 0 | return KEY_XMSS; |
456 | 0 | default: |
457 | 0 | return type; |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | | /* Return the cert equivalent to a plain key type */ |
462 | | static int |
463 | | sshkey_type_certified(int type) |
464 | 0 | { |
465 | 0 | switch (type) { |
466 | 0 | case KEY_RSA: |
467 | 0 | return KEY_RSA_CERT; |
468 | 0 | case KEY_ECDSA: |
469 | 0 | return KEY_ECDSA_CERT; |
470 | 0 | case KEY_ECDSA_SK: |
471 | 0 | return KEY_ECDSA_SK_CERT; |
472 | 0 | case KEY_ED25519: |
473 | 0 | return KEY_ED25519_CERT; |
474 | 0 | case KEY_ED25519_SK: |
475 | 0 | return KEY_ED25519_SK_CERT; |
476 | 0 | case KEY_XMSS: |
477 | 0 | return KEY_XMSS_CERT; |
478 | 0 | default: |
479 | 0 | return -1; |
480 | 0 | } |
481 | 0 | } |
482 | | |
483 | | #ifdef WITH_OPENSSL |
484 | | static const EVP_MD * |
485 | | ssh_digest_to_md(int hash_alg) |
486 | 0 | { |
487 | 0 | switch (hash_alg) { |
488 | 0 | case SSH_DIGEST_SHA1: |
489 | 0 | return EVP_sha1(); |
490 | 0 | case SSH_DIGEST_SHA256: |
491 | 0 | return EVP_sha256(); |
492 | 0 | case SSH_DIGEST_SHA384: |
493 | 0 | return EVP_sha384(); |
494 | 0 | case SSH_DIGEST_SHA512: |
495 | 0 | return EVP_sha512(); |
496 | 0 | } |
497 | 0 | return NULL; |
498 | 0 | } |
499 | | |
500 | | int |
501 | | sshkey_pkey_digest_sign(EVP_PKEY *pkey, int hash_alg, u_char **sigp, |
502 | | size_t *lenp, const u_char *data, size_t datalen) |
503 | 0 | { |
504 | 0 | EVP_MD_CTX *ctx = NULL; |
505 | 0 | u_char *sig = NULL; |
506 | 0 | int ret; |
507 | 0 | size_t slen; |
508 | 0 | const EVP_MD *evpmd; |
509 | |
|
510 | 0 | *sigp = NULL; |
511 | 0 | *lenp = 0; |
512 | |
|
513 | 0 | slen = EVP_PKEY_size(pkey); |
514 | 0 | if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM || |
515 | 0 | (evpmd = ssh_digest_to_md(hash_alg)) == NULL) |
516 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
517 | | |
518 | 0 | if ((sig = malloc(slen)) == NULL) |
519 | 0 | return SSH_ERR_ALLOC_FAIL; |
520 | | |
521 | 0 | if ((ctx = EVP_MD_CTX_new()) == NULL) { |
522 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
523 | 0 | goto out; |
524 | 0 | } |
525 | 0 | if (EVP_DigestSignInit(ctx, NULL, evpmd, NULL, pkey) != 1 || |
526 | 0 | EVP_DigestSign(ctx, sig, &slen, data, datalen) != 1) { |
527 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
528 | 0 | goto out; |
529 | 0 | } |
530 | | |
531 | 0 | *sigp = sig; |
532 | 0 | *lenp = slen; |
533 | | /* Now owned by the caller */ |
534 | 0 | sig = NULL; |
535 | 0 | ret = 0; |
536 | |
|
537 | 0 | out: |
538 | 0 | EVP_MD_CTX_free(ctx); |
539 | 0 | free(sig); |
540 | 0 | return ret; |
541 | 0 | } |
542 | | |
543 | | int |
544 | | sshkey_pkey_digest_verify(EVP_PKEY *pkey, int hash_alg, const u_char *data, |
545 | | size_t datalen, u_char *sigbuf, size_t siglen) |
546 | 0 | { |
547 | 0 | EVP_MD_CTX *ctx = NULL; |
548 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
549 | 0 | const EVP_MD *evpmd; |
550 | |
|
551 | 0 | if ((evpmd = ssh_digest_to_md(hash_alg)) == NULL) |
552 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
553 | 0 | if ((ctx = EVP_MD_CTX_new()) == NULL) |
554 | 0 | return SSH_ERR_ALLOC_FAIL; |
555 | 0 | if (EVP_DigestVerifyInit(ctx, NULL, evpmd, NULL, pkey) != 1) { |
556 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
557 | 0 | goto out; |
558 | 0 | } |
559 | 0 | switch (EVP_DigestVerify(ctx, sigbuf, siglen, data, datalen)) { |
560 | 0 | case 1: |
561 | 0 | ret = 0; |
562 | 0 | break; |
563 | 0 | case 0: |
564 | 0 | ret = SSH_ERR_SIGNATURE_INVALID; |
565 | 0 | break; |
566 | 0 | default: |
567 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
568 | 0 | break; |
569 | 0 | } |
570 | | |
571 | 0 | out: |
572 | 0 | EVP_MD_CTX_free(ctx); |
573 | 0 | return ret; |
574 | 0 | } |
575 | | |
576 | | /* XXX: these are really begging for a table-driven approach */ |
577 | | int |
578 | | sshkey_curve_name_to_nid(const char *name) |
579 | 0 | { |
580 | 0 | if (strcmp(name, "nistp256") == 0) |
581 | 0 | return NID_X9_62_prime256v1; |
582 | 0 | else if (strcmp(name, "nistp384") == 0) |
583 | 0 | return NID_secp384r1; |
584 | 0 | # ifdef OPENSSL_HAS_NISTP521 |
585 | 0 | else if (strcmp(name, "nistp521") == 0) |
586 | 0 | return NID_secp521r1; |
587 | 0 | # endif /* OPENSSL_HAS_NISTP521 */ |
588 | 0 | else |
589 | 0 | return -1; |
590 | 0 | } |
591 | | |
592 | | u_int |
593 | | sshkey_curve_nid_to_bits(int nid) |
594 | 0 | { |
595 | 0 | switch (nid) { |
596 | 0 | case NID_X9_62_prime256v1: |
597 | 0 | return 256; |
598 | 0 | case NID_secp384r1: |
599 | 0 | return 384; |
600 | 0 | # ifdef OPENSSL_HAS_NISTP521 |
601 | 0 | case NID_secp521r1: |
602 | 0 | return 521; |
603 | 0 | # endif /* OPENSSL_HAS_NISTP521 */ |
604 | 0 | default: |
605 | 0 | return 0; |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | | int |
610 | | sshkey_ecdsa_bits_to_nid(int bits) |
611 | 0 | { |
612 | 0 | switch (bits) { |
613 | 0 | case 256: |
614 | 0 | return NID_X9_62_prime256v1; |
615 | 0 | case 384: |
616 | 0 | return NID_secp384r1; |
617 | 0 | # ifdef OPENSSL_HAS_NISTP521 |
618 | 0 | case 521: |
619 | 0 | return NID_secp521r1; |
620 | 0 | # endif /* OPENSSL_HAS_NISTP521 */ |
621 | 0 | default: |
622 | 0 | return -1; |
623 | 0 | } |
624 | 0 | } |
625 | | |
626 | | const char * |
627 | | sshkey_curve_nid_to_name(int nid) |
628 | 0 | { |
629 | 0 | switch (nid) { |
630 | 0 | case NID_X9_62_prime256v1: |
631 | 0 | return "nistp256"; |
632 | 0 | case NID_secp384r1: |
633 | 0 | return "nistp384"; |
634 | 0 | # ifdef OPENSSL_HAS_NISTP521 |
635 | 0 | case NID_secp521r1: |
636 | 0 | return "nistp521"; |
637 | 0 | # endif /* OPENSSL_HAS_NISTP521 */ |
638 | 0 | default: |
639 | 0 | return NULL; |
640 | 0 | } |
641 | 0 | } |
642 | | |
643 | | int |
644 | | sshkey_ec_nid_to_hash_alg(int nid) |
645 | 0 | { |
646 | 0 | int kbits = sshkey_curve_nid_to_bits(nid); |
647 | |
|
648 | 0 | if (kbits <= 0) |
649 | 0 | return -1; |
650 | | |
651 | | /* RFC5656 section 6.2.1 */ |
652 | 0 | if (kbits <= 256) |
653 | 0 | return SSH_DIGEST_SHA256; |
654 | 0 | else if (kbits <= 384) |
655 | 0 | return SSH_DIGEST_SHA384; |
656 | 0 | else |
657 | 0 | return SSH_DIGEST_SHA512; |
658 | 0 | } |
659 | | #endif /* WITH_OPENSSL */ |
660 | | |
661 | | static void |
662 | | cert_free(struct sshkey_cert *cert) |
663 | 0 | { |
664 | 0 | u_int i; |
665 | |
|
666 | 0 | if (cert == NULL) |
667 | 0 | return; |
668 | 0 | sshbuf_free(cert->certblob); |
669 | 0 | sshbuf_free(cert->critical); |
670 | 0 | sshbuf_free(cert->extensions); |
671 | 0 | free(cert->key_id); |
672 | 0 | for (i = 0; i < cert->nprincipals; i++) |
673 | 0 | free(cert->principals[i]); |
674 | 0 | free(cert->principals); |
675 | 0 | sshkey_free(cert->signature_key); |
676 | 0 | free(cert->signature_type); |
677 | 0 | freezero(cert, sizeof(*cert)); |
678 | 0 | } |
679 | | |
680 | | static struct sshkey_cert * |
681 | | cert_new(void) |
682 | 0 | { |
683 | 0 | struct sshkey_cert *cert; |
684 | |
|
685 | 0 | if ((cert = calloc(1, sizeof(*cert))) == NULL) |
686 | 0 | return NULL; |
687 | 0 | if ((cert->certblob = sshbuf_new()) == NULL || |
688 | 0 | (cert->critical = sshbuf_new()) == NULL || |
689 | 0 | (cert->extensions = sshbuf_new()) == NULL) { |
690 | 0 | cert_free(cert); |
691 | 0 | return NULL; |
692 | 0 | } |
693 | 0 | cert->key_id = NULL; |
694 | 0 | cert->principals = NULL; |
695 | 0 | cert->signature_key = NULL; |
696 | 0 | cert->signature_type = NULL; |
697 | 0 | return cert; |
698 | 0 | } |
699 | | |
700 | | struct sshkey * |
701 | | sshkey_new(int type) |
702 | 0 | { |
703 | 0 | struct sshkey *k; |
704 | 0 | const struct sshkey_impl *impl = NULL; |
705 | |
|
706 | 0 | if (type != KEY_UNSPEC && |
707 | 0 | (impl = sshkey_impl_from_type(type)) == NULL) |
708 | 0 | return NULL; |
709 | | |
710 | | /* All non-certificate types may act as CAs */ |
711 | 0 | if ((k = calloc(1, sizeof(*k))) == NULL) |
712 | 0 | return NULL; |
713 | 0 | k->type = type; |
714 | 0 | k->ecdsa_nid = -1; |
715 | 0 | if (impl != NULL && impl->funcs->alloc != NULL) { |
716 | 0 | if (impl->funcs->alloc(k) != 0) { |
717 | 0 | free(k); |
718 | 0 | return NULL; |
719 | 0 | } |
720 | 0 | } |
721 | 0 | if (sshkey_is_cert(k)) { |
722 | 0 | if ((k->cert = cert_new()) == NULL) { |
723 | 0 | sshkey_free(k); |
724 | 0 | return NULL; |
725 | 0 | } |
726 | 0 | } |
727 | | |
728 | 0 | return k; |
729 | 0 | } |
730 | | |
731 | | /* Frees common FIDO fields */ |
732 | | void |
733 | | sshkey_sk_cleanup(struct sshkey *k) |
734 | 0 | { |
735 | 0 | free(k->sk_application); |
736 | 0 | sshbuf_free(k->sk_key_handle); |
737 | 0 | sshbuf_free(k->sk_reserved); |
738 | 0 | k->sk_application = NULL; |
739 | 0 | k->sk_key_handle = k->sk_reserved = NULL; |
740 | 0 | } |
741 | | |
742 | | #if defined(MAP_CONCEAL) |
743 | | # define PREKEY_MMAP_FLAG MAP_CONCEAL |
744 | | #elif defined(MAP_NOCORE) |
745 | | # define PREKEY_MMAP_FLAG MAP_NOCORE |
746 | | #else |
747 | 0 | # define PREKEY_MMAP_FLAG 0 |
748 | | #endif |
749 | | |
750 | | static int |
751 | | sshkey_prekey_alloc(u_char **prekeyp, size_t len) |
752 | 0 | { |
753 | 0 | u_char *prekey; |
754 | |
|
755 | 0 | *prekeyp = NULL; |
756 | 0 | if ((prekey = mmap(NULL, len, PROT_READ|PROT_WRITE, |
757 | 0 | MAP_ANON|MAP_PRIVATE|PREKEY_MMAP_FLAG, -1, 0)) == MAP_FAILED) |
758 | 0 | return SSH_ERR_SYSTEM_ERROR; |
759 | 0 | #if defined(MADV_DONTDUMP) && !defined(MAP_CONCEAL) && !defined(MAP_NOCORE) |
760 | 0 | (void)madvise(prekey, len, MADV_DONTDUMP); |
761 | 0 | #endif |
762 | 0 | *prekeyp = prekey; |
763 | 0 | return 0; |
764 | 0 | } |
765 | | |
766 | | static void |
767 | | sshkey_prekey_free(void *prekey, size_t len) |
768 | 0 | { |
769 | 0 | if (prekey == NULL) |
770 | 0 | return; |
771 | 0 | munmap(prekey, len); |
772 | 0 | } |
773 | | |
774 | | static void |
775 | | sshkey_free_contents(struct sshkey *k) |
776 | 0 | { |
777 | 0 | const struct sshkey_impl *impl; |
778 | |
|
779 | 0 | if (k == NULL) |
780 | 0 | return; |
781 | 0 | if ((impl = sshkey_impl_from_type(k->type)) != NULL && |
782 | 0 | impl->funcs->cleanup != NULL) |
783 | 0 | impl->funcs->cleanup(k); |
784 | 0 | if (sshkey_is_cert(k)) |
785 | 0 | cert_free(k->cert); |
786 | 0 | freezero(k->shielded_private, k->shielded_len); |
787 | 0 | sshkey_prekey_free(k->shield_prekey, k->shield_prekey_len); |
788 | 0 | } |
789 | | |
790 | | void |
791 | | sshkey_free(struct sshkey *k) |
792 | 0 | { |
793 | 0 | sshkey_free_contents(k); |
794 | 0 | freezero(k, sizeof(*k)); |
795 | 0 | } |
796 | | |
797 | | static int |
798 | | cert_compare(struct sshkey_cert *a, struct sshkey_cert *b) |
799 | 0 | { |
800 | 0 | if (a == NULL && b == NULL) |
801 | 0 | return 1; |
802 | 0 | if (a == NULL || b == NULL) |
803 | 0 | return 0; |
804 | 0 | if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob)) |
805 | 0 | return 0; |
806 | 0 | if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob), |
807 | 0 | sshbuf_len(a->certblob)) != 0) |
808 | 0 | return 0; |
809 | 0 | return 1; |
810 | 0 | } |
811 | | |
812 | | /* Compares FIDO-specific pubkey fields only */ |
813 | | int |
814 | | sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b) |
815 | 0 | { |
816 | 0 | if (a->sk_application == NULL || b->sk_application == NULL) |
817 | 0 | return 0; |
818 | 0 | if (strcmp(a->sk_application, b->sk_application) != 0) |
819 | 0 | return 0; |
820 | 0 | return 1; |
821 | 0 | } |
822 | | |
823 | | /* |
824 | | * Compare public portions of key only, allowing comparisons between |
825 | | * certificates and plain keys too. |
826 | | */ |
827 | | int |
828 | | sshkey_equal_public(const struct sshkey *a, const struct sshkey *b) |
829 | 0 | { |
830 | 0 | const struct sshkey_impl *impl; |
831 | |
|
832 | 0 | if (a == NULL || b == NULL || |
833 | 0 | sshkey_type_plain(a->type) != sshkey_type_plain(b->type)) |
834 | 0 | return 0; |
835 | 0 | if ((impl = sshkey_impl_from_type(a->type)) == NULL) |
836 | 0 | return 0; |
837 | 0 | return impl->funcs->equal(a, b); |
838 | 0 | } |
839 | | |
840 | | int |
841 | | sshkey_equal(const struct sshkey *a, const struct sshkey *b) |
842 | 0 | { |
843 | 0 | if (a == NULL || b == NULL || a->type != b->type) |
844 | 0 | return 0; |
845 | 0 | if (sshkey_is_cert(a)) { |
846 | 0 | if (!cert_compare(a->cert, b->cert)) |
847 | 0 | return 0; |
848 | 0 | } |
849 | 0 | return sshkey_equal_public(a, b); |
850 | 0 | } |
851 | | |
852 | | |
853 | | /* Serialise common FIDO key parts */ |
854 | | int |
855 | | sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b) |
856 | 0 | { |
857 | 0 | int r; |
858 | |
|
859 | 0 | if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0) |
860 | 0 | return r; |
861 | | |
862 | 0 | return 0; |
863 | 0 | } |
864 | | |
865 | | static int |
866 | | to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain, |
867 | | enum sshkey_serialize_rep opts) |
868 | 0 | { |
869 | 0 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
870 | 0 | const char *typename; |
871 | 0 | const struct sshkey_impl *impl; |
872 | |
|
873 | 0 | if (key == NULL) |
874 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
875 | | |
876 | 0 | type = force_plain ? sshkey_type_plain(key->type) : key->type; |
877 | |
|
878 | 0 | if (sshkey_type_is_cert(type)) { |
879 | 0 | if (key->cert == NULL) |
880 | 0 | return SSH_ERR_EXPECTED_CERT; |
881 | 0 | if (sshbuf_len(key->cert->certblob) == 0) |
882 | 0 | return SSH_ERR_KEY_LACKS_CERTBLOB; |
883 | | /* Use the existing blob */ |
884 | 0 | if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0) |
885 | 0 | return ret; |
886 | 0 | return 0; |
887 | 0 | } |
888 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) |
889 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
890 | | |
891 | 0 | typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid); |
892 | 0 | if ((ret = sshbuf_put_cstring(b, typename)) != 0) |
893 | 0 | return ret; |
894 | 0 | return impl->funcs->serialize_public(key, b, opts); |
895 | 0 | } |
896 | | |
897 | | int |
898 | | sshkey_putb(const struct sshkey *key, struct sshbuf *b) |
899 | 0 | { |
900 | 0 | return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT); |
901 | 0 | } |
902 | | |
903 | | int |
904 | | sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b, |
905 | | enum sshkey_serialize_rep opts) |
906 | 0 | { |
907 | 0 | struct sshbuf *tmp; |
908 | 0 | int r; |
909 | |
|
910 | 0 | if ((tmp = sshbuf_new()) == NULL) |
911 | 0 | return SSH_ERR_ALLOC_FAIL; |
912 | 0 | r = to_blob_buf(key, tmp, 0, opts); |
913 | 0 | if (r == 0) |
914 | 0 | r = sshbuf_put_stringb(b, tmp); |
915 | 0 | sshbuf_free(tmp); |
916 | 0 | return r; |
917 | 0 | } |
918 | | |
919 | | int |
920 | | sshkey_puts(const struct sshkey *key, struct sshbuf *b) |
921 | 0 | { |
922 | 0 | return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT); |
923 | 0 | } |
924 | | |
925 | | int |
926 | | sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b) |
927 | 0 | { |
928 | 0 | return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT); |
929 | 0 | } |
930 | | |
931 | | static int |
932 | | to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain, |
933 | | enum sshkey_serialize_rep opts) |
934 | 0 | { |
935 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
936 | 0 | size_t len; |
937 | 0 | struct sshbuf *b = NULL; |
938 | |
|
939 | 0 | if (lenp != NULL) |
940 | 0 | *lenp = 0; |
941 | 0 | if (blobp != NULL) |
942 | 0 | *blobp = NULL; |
943 | 0 | if ((b = sshbuf_new()) == NULL) |
944 | 0 | return SSH_ERR_ALLOC_FAIL; |
945 | 0 | if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0) |
946 | 0 | goto out; |
947 | 0 | len = sshbuf_len(b); |
948 | 0 | if (lenp != NULL) |
949 | 0 | *lenp = len; |
950 | 0 | if (blobp != NULL) { |
951 | 0 | if ((*blobp = malloc(len)) == NULL) { |
952 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
953 | 0 | goto out; |
954 | 0 | } |
955 | 0 | memcpy(*blobp, sshbuf_ptr(b), len); |
956 | 0 | } |
957 | 0 | ret = 0; |
958 | 0 | out: |
959 | 0 | sshbuf_free(b); |
960 | 0 | return ret; |
961 | 0 | } |
962 | | |
963 | | int |
964 | | sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
965 | 0 | { |
966 | 0 | return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT); |
967 | 0 | } |
968 | | |
969 | | int |
970 | | sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp) |
971 | 0 | { |
972 | 0 | return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT); |
973 | 0 | } |
974 | | |
975 | | int |
976 | | sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg, |
977 | | u_char **retp, size_t *lenp) |
978 | 0 | { |
979 | 0 | u_char *blob = NULL, *ret = NULL; |
980 | 0 | size_t blob_len = 0; |
981 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
982 | |
|
983 | 0 | if (retp != NULL) |
984 | 0 | *retp = NULL; |
985 | 0 | if (lenp != NULL) |
986 | 0 | *lenp = 0; |
987 | 0 | if (ssh_digest_bytes(dgst_alg) == 0) { |
988 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
989 | 0 | goto out; |
990 | 0 | } |
991 | 0 | if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT)) |
992 | 0 | != 0) |
993 | 0 | goto out; |
994 | 0 | if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) { |
995 | 0 | r = SSH_ERR_ALLOC_FAIL; |
996 | 0 | goto out; |
997 | 0 | } |
998 | 0 | if ((r = ssh_digest_memory(dgst_alg, blob, blob_len, |
999 | 0 | ret, SSH_DIGEST_MAX_LENGTH)) != 0) |
1000 | 0 | goto out; |
1001 | | /* success */ |
1002 | 0 | if (retp != NULL) { |
1003 | 0 | *retp = ret; |
1004 | 0 | ret = NULL; |
1005 | 0 | } |
1006 | 0 | if (lenp != NULL) |
1007 | 0 | *lenp = ssh_digest_bytes(dgst_alg); |
1008 | 0 | r = 0; |
1009 | 0 | out: |
1010 | 0 | free(ret); |
1011 | 0 | if (blob != NULL) |
1012 | 0 | freezero(blob, blob_len); |
1013 | 0 | return r; |
1014 | 0 | } |
1015 | | |
1016 | | static char * |
1017 | | fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
1018 | 0 | { |
1019 | 0 | char *ret; |
1020 | 0 | size_t plen = strlen(alg) + 1; |
1021 | 0 | size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1; |
1022 | |
|
1023 | 0 | if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL) |
1024 | 0 | return NULL; |
1025 | 0 | strlcpy(ret, alg, rlen); |
1026 | 0 | strlcat(ret, ":", rlen); |
1027 | 0 | if (dgst_raw_len == 0) |
1028 | 0 | return ret; |
1029 | 0 | if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) { |
1030 | 0 | freezero(ret, rlen); |
1031 | 0 | return NULL; |
1032 | 0 | } |
1033 | | /* Trim padding characters from end */ |
1034 | 0 | ret[strcspn(ret, "=")] = '\0'; |
1035 | 0 | return ret; |
1036 | 0 | } |
1037 | | |
1038 | | static char * |
1039 | | fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len) |
1040 | 0 | { |
1041 | 0 | char *retval, hex[5]; |
1042 | 0 | size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2; |
1043 | |
|
1044 | 0 | if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL) |
1045 | 0 | return NULL; |
1046 | 0 | strlcpy(retval, alg, rlen); |
1047 | 0 | strlcat(retval, ":", rlen); |
1048 | 0 | for (i = 0; i < dgst_raw_len; i++) { |
1049 | 0 | snprintf(hex, sizeof(hex), "%s%02x", |
1050 | 0 | i > 0 ? ":" : "", dgst_raw[i]); |
1051 | 0 | strlcat(retval, hex, rlen); |
1052 | 0 | } |
1053 | 0 | return retval; |
1054 | 0 | } |
1055 | | |
1056 | | static char * |
1057 | | fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len) |
1058 | 0 | { |
1059 | 0 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
1060 | 0 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
1061 | 0 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
1062 | 0 | u_int i, j = 0, rounds, seed = 1; |
1063 | 0 | char *retval; |
1064 | |
|
1065 | 0 | rounds = (dgst_raw_len / 2) + 1; |
1066 | 0 | if ((retval = calloc(rounds, 6)) == NULL) |
1067 | 0 | return NULL; |
1068 | 0 | retval[j++] = 'x'; |
1069 | 0 | for (i = 0; i < rounds; i++) { |
1070 | 0 | u_int idx0, idx1, idx2, idx3, idx4; |
1071 | 0 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
1072 | 0 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
1073 | 0 | seed) % 6; |
1074 | 0 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
1075 | 0 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
1076 | 0 | (seed / 6)) % 6; |
1077 | 0 | retval[j++] = vowels[idx0]; |
1078 | 0 | retval[j++] = consonants[idx1]; |
1079 | 0 | retval[j++] = vowels[idx2]; |
1080 | 0 | if ((i + 1) < rounds) { |
1081 | 0 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
1082 | 0 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
1083 | 0 | retval[j++] = consonants[idx3]; |
1084 | 0 | retval[j++] = '-'; |
1085 | 0 | retval[j++] = consonants[idx4]; |
1086 | 0 | seed = ((seed * 5) + |
1087 | 0 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
1088 | 0 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
1089 | 0 | } |
1090 | 0 | } else { |
1091 | 0 | idx0 = seed % 6; |
1092 | 0 | idx1 = 16; |
1093 | 0 | idx2 = seed / 6; |
1094 | 0 | retval[j++] = vowels[idx0]; |
1095 | 0 | retval[j++] = consonants[idx1]; |
1096 | 0 | retval[j++] = vowels[idx2]; |
1097 | 0 | } |
1098 | 0 | } |
1099 | 0 | retval[j++] = 'x'; |
1100 | 0 | retval[j++] = '\0'; |
1101 | 0 | return retval; |
1102 | 0 | } |
1103 | | |
1104 | | /* |
1105 | | * Draw an ASCII-Art representing the fingerprint so human brain can |
1106 | | * profit from its built-in pattern recognition ability. |
1107 | | * This technique is called "random art" and can be found in some |
1108 | | * scientific publications like this original paper: |
1109 | | * |
1110 | | * "Hash Visualization: a New Technique to improve Real-World Security", |
1111 | | * Perrig A. and Song D., 1999, International Workshop on Cryptographic |
1112 | | * Techniques and E-Commerce (CrypTEC '99) |
1113 | | * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf |
1114 | | * |
1115 | | * The subject came up in a talk by Dan Kaminsky, too. |
1116 | | * |
1117 | | * If you see the picture is different, the key is different. |
1118 | | * If the picture looks the same, you still know nothing. |
1119 | | * |
1120 | | * The algorithm used here is a worm crawling over a discrete plane, |
1121 | | * leaving a trace (augmenting the field) everywhere it goes. |
1122 | | * Movement is taken from dgst_raw 2bit-wise. Bumping into walls |
1123 | | * makes the respective movement vector be ignored for this turn. |
1124 | | * Graphs are not unambiguous, because circles in graphs can be |
1125 | | * walked in either direction. |
1126 | | */ |
1127 | | |
1128 | | /* |
1129 | | * Field sizes for the random art. Have to be odd, so the starting point |
1130 | | * can be in the exact middle of the picture, and FLDBASE should be >=8 . |
1131 | | * Else pictures would be too dense, and drawing the frame would |
1132 | | * fail, too, because the key type would not fit in anymore. |
1133 | | */ |
1134 | 0 | #define FLDBASE 8 |
1135 | 0 | #define FLDSIZE_Y (FLDBASE + 1) |
1136 | 0 | #define FLDSIZE_X (FLDBASE * 2 + 1) |
1137 | | static char * |
1138 | | fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len, |
1139 | | const struct sshkey *k) |
1140 | 0 | { |
1141 | | /* |
1142 | | * Chars to be used after each other every time the worm |
1143 | | * intersects with itself. Matter of taste. |
1144 | | */ |
1145 | 0 | char *augmentation_string = " .o+=*BOX@%&#/^SE"; |
1146 | 0 | char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X]; |
1147 | 0 | u_char field[FLDSIZE_X][FLDSIZE_Y]; |
1148 | 0 | size_t i, tlen, hlen; |
1149 | 0 | u_int b; |
1150 | 0 | int x, y, r; |
1151 | 0 | size_t len = strlen(augmentation_string) - 1; |
1152 | |
|
1153 | 0 | if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL) |
1154 | 0 | return NULL; |
1155 | | |
1156 | | /* initialize field */ |
1157 | 0 | memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char)); |
1158 | 0 | x = FLDSIZE_X / 2; |
1159 | 0 | y = FLDSIZE_Y / 2; |
1160 | | |
1161 | | /* process raw key */ |
1162 | 0 | for (i = 0; i < dgst_raw_len; i++) { |
1163 | 0 | int input; |
1164 | | /* each byte conveys four 2-bit move commands */ |
1165 | 0 | input = dgst_raw[i]; |
1166 | 0 | for (b = 0; b < 4; b++) { |
1167 | | /* evaluate 2 bit, rest is shifted later */ |
1168 | 0 | x += (input & 0x1) ? 1 : -1; |
1169 | 0 | y += (input & 0x2) ? 1 : -1; |
1170 | | |
1171 | | /* assure we are still in bounds */ |
1172 | 0 | x = MAXIMUM(x, 0); |
1173 | 0 | y = MAXIMUM(y, 0); |
1174 | 0 | x = MINIMUM(x, FLDSIZE_X - 1); |
1175 | 0 | y = MINIMUM(y, FLDSIZE_Y - 1); |
1176 | | |
1177 | | /* augment the field */ |
1178 | 0 | if (field[x][y] < len - 2) |
1179 | 0 | field[x][y]++; |
1180 | 0 | input = input >> 2; |
1181 | 0 | } |
1182 | 0 | } |
1183 | | |
1184 | | /* mark starting point and end point*/ |
1185 | 0 | field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1; |
1186 | 0 | field[x][y] = len; |
1187 | | |
1188 | | /* assemble title */ |
1189 | 0 | r = snprintf(title, sizeof(title), "[%s %u]", |
1190 | 0 | sshkey_type(k), sshkey_size(k)); |
1191 | | /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */ |
1192 | 0 | if (r < 0 || r > (int)sizeof(title)) |
1193 | 0 | r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k)); |
1194 | 0 | tlen = (r <= 0) ? 0 : strlen(title); |
1195 | | |
1196 | | /* assemble hash ID. */ |
1197 | 0 | r = snprintf(hash, sizeof(hash), "[%s]", alg); |
1198 | 0 | hlen = (r <= 0) ? 0 : strlen(hash); |
1199 | | |
1200 | | /* output upper border */ |
1201 | 0 | p = retval; |
1202 | 0 | *p++ = '+'; |
1203 | 0 | for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++) |
1204 | 0 | *p++ = '-'; |
1205 | 0 | memcpy(p, title, tlen); |
1206 | 0 | p += tlen; |
1207 | 0 | for (i += tlen; i < FLDSIZE_X; i++) |
1208 | 0 | *p++ = '-'; |
1209 | 0 | *p++ = '+'; |
1210 | 0 | *p++ = '\n'; |
1211 | | |
1212 | | /* output content */ |
1213 | 0 | for (y = 0; y < FLDSIZE_Y; y++) { |
1214 | 0 | *p++ = '|'; |
1215 | 0 | for (x = 0; x < FLDSIZE_X; x++) |
1216 | 0 | *p++ = augmentation_string[MINIMUM(field[x][y], len)]; |
1217 | 0 | *p++ = '|'; |
1218 | 0 | *p++ = '\n'; |
1219 | 0 | } |
1220 | | |
1221 | | /* output lower border */ |
1222 | 0 | *p++ = '+'; |
1223 | 0 | for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++) |
1224 | 0 | *p++ = '-'; |
1225 | 0 | memcpy(p, hash, hlen); |
1226 | 0 | p += hlen; |
1227 | 0 | for (i += hlen; i < FLDSIZE_X; i++) |
1228 | 0 | *p++ = '-'; |
1229 | 0 | *p++ = '+'; |
1230 | |
|
1231 | 0 | return retval; |
1232 | 0 | } |
1233 | | |
1234 | | char * |
1235 | | sshkey_fingerprint(const struct sshkey *k, int dgst_alg, |
1236 | | enum sshkey_fp_rep dgst_rep) |
1237 | 0 | { |
1238 | 0 | char *retval = NULL; |
1239 | 0 | u_char *dgst_raw; |
1240 | 0 | size_t dgst_raw_len; |
1241 | |
|
1242 | 0 | if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0) |
1243 | 0 | return NULL; |
1244 | 0 | switch (dgst_rep) { |
1245 | 0 | case SSH_FP_DEFAULT: |
1246 | 0 | if (dgst_alg == SSH_DIGEST_MD5) { |
1247 | 0 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
1248 | 0 | dgst_raw, dgst_raw_len); |
1249 | 0 | } else { |
1250 | 0 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
1251 | 0 | dgst_raw, dgst_raw_len); |
1252 | 0 | } |
1253 | 0 | break; |
1254 | 0 | case SSH_FP_HEX: |
1255 | 0 | retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg), |
1256 | 0 | dgst_raw, dgst_raw_len); |
1257 | 0 | break; |
1258 | 0 | case SSH_FP_BASE64: |
1259 | 0 | retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg), |
1260 | 0 | dgst_raw, dgst_raw_len); |
1261 | 0 | break; |
1262 | 0 | case SSH_FP_BUBBLEBABBLE: |
1263 | 0 | retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
1264 | 0 | break; |
1265 | 0 | case SSH_FP_RANDOMART: |
1266 | 0 | retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg), |
1267 | 0 | dgst_raw, dgst_raw_len, k); |
1268 | 0 | break; |
1269 | 0 | default: |
1270 | 0 | freezero(dgst_raw, dgst_raw_len); |
1271 | 0 | return NULL; |
1272 | 0 | } |
1273 | 0 | freezero(dgst_raw, dgst_raw_len); |
1274 | 0 | return retval; |
1275 | 0 | } |
1276 | | |
1277 | | static int |
1278 | | peek_type_nid(const char *s, size_t l, int *nid) |
1279 | 0 | { |
1280 | 0 | const struct sshkey_impl *impl; |
1281 | 0 | int i; |
1282 | |
|
1283 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
1284 | 0 | impl = keyimpls[i]; |
1285 | 0 | if (impl->name == NULL || strlen(impl->name) != l) |
1286 | 0 | continue; |
1287 | 0 | if (memcmp(s, impl->name, l) == 0) { |
1288 | 0 | *nid = -1; |
1289 | 0 | if (key_type_is_ecdsa_variant(impl->type)) |
1290 | 0 | *nid = impl->nid; |
1291 | 0 | return impl->type; |
1292 | 0 | } |
1293 | 0 | } |
1294 | 0 | return KEY_UNSPEC; |
1295 | 0 | } |
1296 | | |
1297 | | /* XXX this can now be made const char * */ |
1298 | | int |
1299 | | sshkey_read(struct sshkey *ret, char **cpp) |
1300 | 0 | { |
1301 | 0 | struct sshkey *k; |
1302 | 0 | char *cp, *blobcopy; |
1303 | 0 | size_t space; |
1304 | 0 | int r, type, curve_nid = -1; |
1305 | 0 | struct sshbuf *blob; |
1306 | |
|
1307 | 0 | if (ret == NULL) |
1308 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1309 | 0 | if (ret->type != KEY_UNSPEC && sshkey_impl_from_type(ret->type) == NULL) |
1310 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1311 | | |
1312 | | /* Decode type */ |
1313 | 0 | cp = *cpp; |
1314 | 0 | space = strcspn(cp, " \t"); |
1315 | 0 | if (space == strlen(cp)) |
1316 | 0 | return SSH_ERR_INVALID_FORMAT; |
1317 | 0 | if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC) |
1318 | 0 | return SSH_ERR_INVALID_FORMAT; |
1319 | | |
1320 | | /* skip whitespace */ |
1321 | 0 | for (cp += space; *cp == ' ' || *cp == '\t'; cp++) |
1322 | 0 | ; |
1323 | 0 | if (*cp == '\0') |
1324 | 0 | return SSH_ERR_INVALID_FORMAT; |
1325 | 0 | if (ret->type != KEY_UNSPEC && ret->type != type) |
1326 | 0 | return SSH_ERR_KEY_TYPE_MISMATCH; |
1327 | 0 | if ((blob = sshbuf_new()) == NULL) |
1328 | 0 | return SSH_ERR_ALLOC_FAIL; |
1329 | | |
1330 | | /* find end of keyblob and decode */ |
1331 | 0 | space = strcspn(cp, " \t"); |
1332 | 0 | if ((blobcopy = strndup(cp, space)) == NULL) { |
1333 | 0 | sshbuf_free(blob); |
1334 | 0 | return SSH_ERR_ALLOC_FAIL; |
1335 | 0 | } |
1336 | 0 | if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) { |
1337 | 0 | free(blobcopy); |
1338 | 0 | sshbuf_free(blob); |
1339 | 0 | return r; |
1340 | 0 | } |
1341 | 0 | free(blobcopy); |
1342 | 0 | if ((r = sshkey_fromb(blob, &k)) != 0) { |
1343 | 0 | sshbuf_free(blob); |
1344 | 0 | return r; |
1345 | 0 | } |
1346 | 0 | sshbuf_free(blob); |
1347 | | |
1348 | | /* skip whitespace and leave cp at start of comment */ |
1349 | 0 | for (cp += space; *cp == ' ' || *cp == '\t'; cp++) |
1350 | 0 | ; |
1351 | | |
1352 | | /* ensure type of blob matches type at start of line */ |
1353 | 0 | if (k->type != type) { |
1354 | 0 | sshkey_free(k); |
1355 | 0 | return SSH_ERR_KEY_TYPE_MISMATCH; |
1356 | 0 | } |
1357 | 0 | if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) { |
1358 | 0 | sshkey_free(k); |
1359 | 0 | return SSH_ERR_EC_CURVE_MISMATCH; |
1360 | 0 | } |
1361 | | |
1362 | | /* Fill in ret from parsed key */ |
1363 | 0 | sshkey_free_contents(ret); |
1364 | 0 | *ret = *k; |
1365 | 0 | freezero(k, sizeof(*k)); |
1366 | | |
1367 | | /* success */ |
1368 | 0 | *cpp = cp; |
1369 | 0 | return 0; |
1370 | 0 | } |
1371 | | |
1372 | | int |
1373 | | sshkey_to_base64(const struct sshkey *key, char **b64p) |
1374 | 0 | { |
1375 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1376 | 0 | struct sshbuf *b = NULL; |
1377 | 0 | char *uu = NULL; |
1378 | |
|
1379 | 0 | if (b64p != NULL) |
1380 | 0 | *b64p = NULL; |
1381 | 0 | if ((b = sshbuf_new()) == NULL) |
1382 | 0 | return SSH_ERR_ALLOC_FAIL; |
1383 | 0 | if ((r = sshkey_putb(key, b)) != 0) |
1384 | 0 | goto out; |
1385 | 0 | if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) { |
1386 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1387 | 0 | goto out; |
1388 | 0 | } |
1389 | | /* Success */ |
1390 | 0 | if (b64p != NULL) { |
1391 | 0 | *b64p = uu; |
1392 | 0 | uu = NULL; |
1393 | 0 | } |
1394 | 0 | r = 0; |
1395 | 0 | out: |
1396 | 0 | sshbuf_free(b); |
1397 | 0 | free(uu); |
1398 | 0 | return r; |
1399 | 0 | } |
1400 | | |
1401 | | int |
1402 | | sshkey_format_text(const struct sshkey *key, struct sshbuf *b) |
1403 | 0 | { |
1404 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1405 | 0 | char *uu = NULL; |
1406 | |
|
1407 | 0 | if ((r = sshkey_to_base64(key, &uu)) != 0) |
1408 | 0 | goto out; |
1409 | 0 | if ((r = sshbuf_putf(b, "%s %s", |
1410 | 0 | sshkey_ssh_name(key), uu)) != 0) |
1411 | 0 | goto out; |
1412 | 0 | r = 0; |
1413 | 0 | out: |
1414 | 0 | free(uu); |
1415 | 0 | return r; |
1416 | 0 | } |
1417 | | |
1418 | | int |
1419 | | sshkey_write(const struct sshkey *key, FILE *f) |
1420 | 0 | { |
1421 | 0 | struct sshbuf *b = NULL; |
1422 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1423 | |
|
1424 | 0 | if ((b = sshbuf_new()) == NULL) |
1425 | 0 | return SSH_ERR_ALLOC_FAIL; |
1426 | 0 | if ((r = sshkey_format_text(key, b)) != 0) |
1427 | 0 | goto out; |
1428 | 0 | if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) { |
1429 | 0 | if (feof(f)) |
1430 | 0 | errno = EPIPE; |
1431 | 0 | r = SSH_ERR_SYSTEM_ERROR; |
1432 | 0 | goto out; |
1433 | 0 | } |
1434 | | /* Success */ |
1435 | 0 | r = 0; |
1436 | 0 | out: |
1437 | 0 | sshbuf_free(b); |
1438 | 0 | return r; |
1439 | 0 | } |
1440 | | |
1441 | | const char * |
1442 | | sshkey_cert_type(const struct sshkey *k) |
1443 | 0 | { |
1444 | 0 | switch (k->cert->type) { |
1445 | 0 | case SSH2_CERT_TYPE_USER: |
1446 | 0 | return "user"; |
1447 | 0 | case SSH2_CERT_TYPE_HOST: |
1448 | 0 | return "host"; |
1449 | 0 | default: |
1450 | 0 | return "unknown"; |
1451 | 0 | } |
1452 | 0 | } |
1453 | | |
1454 | | int |
1455 | | sshkey_check_rsa_length(const struct sshkey *k, int min_size) |
1456 | 0 | { |
1457 | 0 | #ifdef WITH_OPENSSL |
1458 | 0 | int nbits; |
1459 | |
|
1460 | 0 | if (k == NULL || k->pkey == NULL || |
1461 | 0 | (k->type != KEY_RSA && k->type != KEY_RSA_CERT)) |
1462 | 0 | return 0; |
1463 | 0 | nbits = EVP_PKEY_bits(k->pkey); |
1464 | 0 | if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE || |
1465 | 0 | (min_size > 0 && nbits < min_size)) |
1466 | 0 | return SSH_ERR_KEY_LENGTH; |
1467 | 0 | #endif /* WITH_OPENSSL */ |
1468 | 0 | return 0; |
1469 | 0 | } |
1470 | | |
1471 | | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
1472 | | int |
1473 | | sshkey_ecdsa_key_to_nid(const EC_KEY *k) |
1474 | 0 | { |
1475 | 0 | const EC_GROUP *g; |
1476 | 0 | int nid; |
1477 | |
|
1478 | 0 | if (k == NULL || (g = EC_KEY_get0_group(k)) == NULL) |
1479 | 0 | return -1; |
1480 | 0 | if ((nid = EC_GROUP_get_curve_name(g)) <= 0) |
1481 | 0 | return -1; |
1482 | 0 | return nid; |
1483 | 0 | } |
1484 | | |
1485 | | int |
1486 | | sshkey_ecdsa_pkey_to_nid(EVP_PKEY *pkey) |
1487 | 0 | { |
1488 | 0 | return sshkey_ecdsa_key_to_nid(EVP_PKEY_get0_EC_KEY(pkey)); |
1489 | 0 | } |
1490 | | #endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ |
1491 | | |
1492 | | int |
1493 | | sshkey_generate(int type, u_int bits, struct sshkey **keyp) |
1494 | 0 | { |
1495 | 0 | struct sshkey *k; |
1496 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
1497 | 0 | const struct sshkey_impl *impl; |
1498 | |
|
1499 | 0 | if (keyp == NULL || sshkey_type_is_cert(type)) |
1500 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1501 | 0 | *keyp = NULL; |
1502 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) |
1503 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
1504 | 0 | if (impl->funcs->generate == NULL) |
1505 | 0 | return SSH_ERR_FEATURE_UNSUPPORTED; |
1506 | 0 | if ((k = sshkey_new(KEY_UNSPEC)) == NULL) |
1507 | 0 | return SSH_ERR_ALLOC_FAIL; |
1508 | 0 | k->type = type; |
1509 | 0 | if ((ret = impl->funcs->generate(k, bits)) != 0) { |
1510 | 0 | sshkey_free(k); |
1511 | 0 | return ret; |
1512 | 0 | } |
1513 | | /* success */ |
1514 | 0 | *keyp = k; |
1515 | 0 | return 0; |
1516 | 0 | } |
1517 | | |
1518 | | int |
1519 | | sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key) |
1520 | 0 | { |
1521 | 0 | u_int i; |
1522 | 0 | const struct sshkey_cert *from; |
1523 | 0 | struct sshkey_cert *to; |
1524 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1525 | |
|
1526 | 0 | if (to_key == NULL || (from = from_key->cert) == NULL) |
1527 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
1528 | | |
1529 | 0 | if ((to = cert_new()) == NULL) |
1530 | 0 | return SSH_ERR_ALLOC_FAIL; |
1531 | | |
1532 | 0 | if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 || |
1533 | 0 | (r = sshbuf_putb(to->critical, from->critical)) != 0 || |
1534 | 0 | (r = sshbuf_putb(to->extensions, from->extensions)) != 0) |
1535 | 0 | goto out; |
1536 | | |
1537 | 0 | to->serial = from->serial; |
1538 | 0 | to->type = from->type; |
1539 | 0 | if (from->key_id == NULL) |
1540 | 0 | to->key_id = NULL; |
1541 | 0 | else if ((to->key_id = strdup(from->key_id)) == NULL) { |
1542 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1543 | 0 | goto out; |
1544 | 0 | } |
1545 | 0 | to->valid_after = from->valid_after; |
1546 | 0 | to->valid_before = from->valid_before; |
1547 | 0 | if (from->signature_key == NULL) |
1548 | 0 | to->signature_key = NULL; |
1549 | 0 | else if ((r = sshkey_from_private(from->signature_key, |
1550 | 0 | &to->signature_key)) != 0) |
1551 | 0 | goto out; |
1552 | 0 | if (from->signature_type != NULL && |
1553 | 0 | (to->signature_type = strdup(from->signature_type)) == NULL) { |
1554 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1555 | 0 | goto out; |
1556 | 0 | } |
1557 | 0 | if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) { |
1558 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
1559 | 0 | goto out; |
1560 | 0 | } |
1561 | 0 | if (from->nprincipals > 0) { |
1562 | 0 | if ((to->principals = calloc(from->nprincipals, |
1563 | 0 | sizeof(*to->principals))) == NULL) { |
1564 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1565 | 0 | goto out; |
1566 | 0 | } |
1567 | 0 | for (i = 0; i < from->nprincipals; i++) { |
1568 | 0 | to->principals[i] = strdup(from->principals[i]); |
1569 | 0 | if (to->principals[i] == NULL) { |
1570 | 0 | to->nprincipals = i; |
1571 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1572 | 0 | goto out; |
1573 | 0 | } |
1574 | 0 | } |
1575 | 0 | } |
1576 | 0 | to->nprincipals = from->nprincipals; |
1577 | | |
1578 | | /* success */ |
1579 | 0 | cert_free(to_key->cert); |
1580 | 0 | to_key->cert = to; |
1581 | 0 | to = NULL; |
1582 | 0 | r = 0; |
1583 | 0 | out: |
1584 | 0 | cert_free(to); |
1585 | 0 | return r; |
1586 | 0 | } |
1587 | | |
1588 | | int |
1589 | | sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to) |
1590 | 0 | { |
1591 | | /* Append security-key application string */ |
1592 | 0 | if ((to->sk_application = strdup(from->sk_application)) == NULL) |
1593 | 0 | return SSH_ERR_ALLOC_FAIL; |
1594 | 0 | return 0; |
1595 | 0 | } |
1596 | | |
1597 | | int |
1598 | | sshkey_from_private(const struct sshkey *k, struct sshkey **pkp) |
1599 | 0 | { |
1600 | 0 | struct sshkey *n = NULL; |
1601 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1602 | 0 | const struct sshkey_impl *impl; |
1603 | |
|
1604 | 0 | *pkp = NULL; |
1605 | 0 | if ((impl = sshkey_impl_from_key(k)) == NULL) |
1606 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
1607 | 0 | if ((n = sshkey_new(k->type)) == NULL) { |
1608 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1609 | 0 | goto out; |
1610 | 0 | } |
1611 | 0 | if ((r = impl->funcs->copy_public(k, n)) != 0) |
1612 | 0 | goto out; |
1613 | 0 | if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0) |
1614 | 0 | goto out; |
1615 | | /* success */ |
1616 | 0 | *pkp = n; |
1617 | 0 | n = NULL; |
1618 | 0 | r = 0; |
1619 | 0 | out: |
1620 | 0 | sshkey_free(n); |
1621 | 0 | return r; |
1622 | 0 | } |
1623 | | |
1624 | | int |
1625 | | sshkey_is_shielded(struct sshkey *k) |
1626 | 0 | { |
1627 | 0 | return k != NULL && k->shielded_private != NULL; |
1628 | 0 | } |
1629 | | |
1630 | | int |
1631 | | sshkey_shield_private(struct sshkey *k) |
1632 | 0 | { |
1633 | 0 | struct sshbuf *prvbuf = NULL; |
1634 | 0 | u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH]; |
1635 | 0 | struct sshcipher_ctx *cctx = NULL; |
1636 | 0 | const struct sshcipher *cipher; |
1637 | 0 | size_t i, enclen = 0; |
1638 | 0 | struct sshkey *kswap = NULL, tmp; |
1639 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1640 | |
|
1641 | | #ifdef DEBUG_PK |
1642 | | fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k)); |
1643 | | #endif |
1644 | 0 | if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) { |
1645 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
1646 | 0 | goto out; |
1647 | 0 | } |
1648 | 0 | if (cipher_keylen(cipher) + cipher_ivlen(cipher) > |
1649 | 0 | ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) { |
1650 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
1651 | 0 | goto out; |
1652 | 0 | } |
1653 | | |
1654 | | /* Prepare a random pre-key, and from it an ephemeral key */ |
1655 | 0 | if ((r = sshkey_prekey_alloc(&prekey, SSHKEY_SHIELD_PREKEY_LEN)) != 0) |
1656 | 0 | goto out; |
1657 | 0 | arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN); |
1658 | 0 | if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH, |
1659 | 0 | prekey, SSHKEY_SHIELD_PREKEY_LEN, |
1660 | 0 | keyiv, SSH_DIGEST_MAX_LENGTH)) != 0) |
1661 | 0 | goto out; |
1662 | | #ifdef DEBUG_PK |
1663 | | fprintf(stderr, "%s: key+iv\n", __func__); |
1664 | | sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH), |
1665 | | stderr); |
1666 | | #endif |
1667 | 0 | if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher), |
1668 | 0 | keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0) |
1669 | 0 | goto out; |
1670 | | |
1671 | | /* Serialise and encrypt the private key using the ephemeral key */ |
1672 | 0 | if ((prvbuf = sshbuf_new()) == NULL) { |
1673 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1674 | 0 | goto out; |
1675 | 0 | } |
1676 | 0 | if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0) |
1677 | 0 | goto out; |
1678 | 0 | if ((r = sshkey_private_serialize_opt(k, prvbuf, |
1679 | 0 | SSHKEY_SERIALIZE_SHIELD)) != 0) |
1680 | 0 | goto out; |
1681 | | /* pad to cipher blocksize */ |
1682 | 0 | i = 0; |
1683 | 0 | while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) { |
1684 | 0 | if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0) |
1685 | 0 | goto out; |
1686 | 0 | } |
1687 | | #ifdef DEBUG_PK |
1688 | | fprintf(stderr, "%s: serialised\n", __func__); |
1689 | | sshbuf_dump(prvbuf, stderr); |
1690 | | #endif |
1691 | | /* encrypt */ |
1692 | 0 | enclen = sshbuf_len(prvbuf); |
1693 | 0 | if ((enc = malloc(enclen)) == NULL) { |
1694 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1695 | 0 | goto out; |
1696 | 0 | } |
1697 | 0 | if ((r = cipher_crypt(cctx, 0, enc, |
1698 | 0 | sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0) |
1699 | 0 | goto out; |
1700 | | #ifdef DEBUG_PK |
1701 | | fprintf(stderr, "%s: encrypted\n", __func__); |
1702 | | sshbuf_dump_data(enc, enclen, stderr); |
1703 | | #endif |
1704 | | |
1705 | | /* Make a scrubbed, public-only copy of our private key argument */ |
1706 | 0 | if ((r = sshkey_from_private(k, &kswap)) != 0) |
1707 | 0 | goto out; |
1708 | | |
1709 | | /* Swap the private key out (it will be destroyed below) */ |
1710 | 0 | tmp = *kswap; |
1711 | 0 | *kswap = *k; |
1712 | 0 | *k = tmp; |
1713 | | |
1714 | | /* Insert the shielded key into our argument */ |
1715 | 0 | k->shielded_private = enc; |
1716 | 0 | k->shielded_len = enclen; |
1717 | 0 | k->shield_prekey = prekey; |
1718 | 0 | k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN; |
1719 | 0 | enc = prekey = NULL; /* transferred */ |
1720 | 0 | enclen = 0; |
1721 | | |
1722 | | /* preserve key fields that are required for correct operation */ |
1723 | 0 | k->sk_flags = kswap->sk_flags; |
1724 | | |
1725 | | /* success */ |
1726 | 0 | r = 0; |
1727 | |
|
1728 | 0 | out: |
1729 | | /* XXX behaviour on error - invalidate original private key? */ |
1730 | 0 | cipher_free(cctx); |
1731 | 0 | explicit_bzero(keyiv, sizeof(keyiv)); |
1732 | 0 | explicit_bzero(&tmp, sizeof(tmp)); |
1733 | 0 | freezero(enc, enclen); |
1734 | 0 | sshkey_prekey_free(prekey, SSHKEY_SHIELD_PREKEY_LEN); |
1735 | 0 | sshkey_free(kswap); |
1736 | 0 | sshbuf_free(prvbuf); |
1737 | 0 | return r; |
1738 | 0 | } |
1739 | | |
1740 | | /* Check deterministic padding after private key */ |
1741 | | static int |
1742 | | private2_check_padding(struct sshbuf *decrypted) |
1743 | 0 | { |
1744 | 0 | u_char pad; |
1745 | 0 | size_t i; |
1746 | 0 | int r; |
1747 | |
|
1748 | 0 | i = 0; |
1749 | 0 | while (sshbuf_len(decrypted)) { |
1750 | 0 | if ((r = sshbuf_get_u8(decrypted, &pad)) != 0) |
1751 | 0 | goto out; |
1752 | 0 | if (pad != (++i & 0xff)) { |
1753 | 0 | r = SSH_ERR_INVALID_FORMAT; |
1754 | 0 | goto out; |
1755 | 0 | } |
1756 | 0 | } |
1757 | | /* success */ |
1758 | 0 | r = 0; |
1759 | 0 | out: |
1760 | 0 | explicit_bzero(&pad, sizeof(pad)); |
1761 | 0 | explicit_bzero(&i, sizeof(i)); |
1762 | 0 | return r; |
1763 | 0 | } |
1764 | | |
1765 | | int |
1766 | | sshkey_unshield_private(struct sshkey *k) |
1767 | 0 | { |
1768 | 0 | struct sshbuf *prvbuf = NULL; |
1769 | 0 | u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH]; |
1770 | 0 | struct sshcipher_ctx *cctx = NULL; |
1771 | 0 | const struct sshcipher *cipher; |
1772 | 0 | struct sshkey *kswap = NULL, tmp; |
1773 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
1774 | |
|
1775 | | #ifdef DEBUG_PK |
1776 | | fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k)); |
1777 | | #endif |
1778 | 0 | if (!sshkey_is_shielded(k)) |
1779 | 0 | return 0; /* nothing to do */ |
1780 | | |
1781 | 0 | if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) { |
1782 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
1783 | 0 | goto out; |
1784 | 0 | } |
1785 | 0 | if (cipher_keylen(cipher) + cipher_ivlen(cipher) > |
1786 | 0 | ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) { |
1787 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
1788 | 0 | goto out; |
1789 | 0 | } |
1790 | | /* check size of shielded key blob */ |
1791 | 0 | if (k->shielded_len < cipher_blocksize(cipher) || |
1792 | 0 | (k->shielded_len % cipher_blocksize(cipher)) != 0) { |
1793 | 0 | r = SSH_ERR_INVALID_FORMAT; |
1794 | 0 | goto out; |
1795 | 0 | } |
1796 | | |
1797 | | /* Calculate the ephemeral key from the prekey */ |
1798 | 0 | if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH, |
1799 | 0 | k->shield_prekey, k->shield_prekey_len, |
1800 | 0 | keyiv, SSH_DIGEST_MAX_LENGTH)) != 0) |
1801 | 0 | goto out; |
1802 | 0 | if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher), |
1803 | 0 | keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0) |
1804 | 0 | goto out; |
1805 | | #ifdef DEBUG_PK |
1806 | | fprintf(stderr, "%s: key+iv\n", __func__); |
1807 | | sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH), |
1808 | | stderr); |
1809 | | #endif |
1810 | | |
1811 | | /* Decrypt and parse the shielded private key using the ephemeral key */ |
1812 | 0 | if ((prvbuf = sshbuf_new()) == NULL) { |
1813 | 0 | r = SSH_ERR_ALLOC_FAIL; |
1814 | 0 | goto out; |
1815 | 0 | } |
1816 | 0 | if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0) |
1817 | 0 | goto out; |
1818 | | /* decrypt */ |
1819 | | #ifdef DEBUG_PK |
1820 | | fprintf(stderr, "%s: encrypted\n", __func__); |
1821 | | sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr); |
1822 | | #endif |
1823 | 0 | if ((r = cipher_crypt(cctx, 0, cp, |
1824 | 0 | k->shielded_private, k->shielded_len, 0, 0)) != 0) |
1825 | 0 | goto out; |
1826 | | #ifdef DEBUG_PK |
1827 | | fprintf(stderr, "%s: serialised\n", __func__); |
1828 | | sshbuf_dump(prvbuf, stderr); |
1829 | | #endif |
1830 | | /* Parse private key */ |
1831 | 0 | if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0) |
1832 | 0 | goto out; |
1833 | | |
1834 | 0 | if ((r = private2_check_padding(prvbuf)) != 0) |
1835 | 0 | goto out; |
1836 | | |
1837 | | /* Swap the parsed key back into place */ |
1838 | 0 | tmp = *kswap; |
1839 | 0 | *kswap = *k; |
1840 | 0 | *k = tmp; |
1841 | | |
1842 | | /* success */ |
1843 | 0 | r = 0; |
1844 | |
|
1845 | 0 | out: |
1846 | 0 | cipher_free(cctx); |
1847 | 0 | explicit_bzero(keyiv, sizeof(keyiv)); |
1848 | 0 | explicit_bzero(&tmp, sizeof(tmp)); |
1849 | 0 | sshkey_free(kswap); |
1850 | 0 | sshbuf_free(prvbuf); |
1851 | 0 | return r; |
1852 | 0 | } |
1853 | | |
1854 | | static int |
1855 | | cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf) |
1856 | 0 | { |
1857 | 0 | struct sshbuf *principals = NULL, *crit = NULL; |
1858 | 0 | struct sshbuf *exts = NULL, *ca = NULL; |
1859 | 0 | u_char *sig = NULL; |
1860 | 0 | size_t signed_len = 0, slen = 0, kidlen = 0; |
1861 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
1862 | | |
1863 | | /* Copy the entire key blob for verification and later serialisation */ |
1864 | 0 | if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0) |
1865 | 0 | return ret; |
1866 | | |
1867 | | /* Parse body of certificate up to signature */ |
1868 | 0 | if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 || |
1869 | 0 | (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 || |
1870 | 0 | (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 || |
1871 | 0 | (ret = sshbuf_froms(b, &principals)) != 0 || |
1872 | 0 | (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 || |
1873 | 0 | (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 || |
1874 | 0 | (ret = sshbuf_froms(b, &crit)) != 0 || |
1875 | 0 | (ret = sshbuf_froms(b, &exts)) != 0 || |
1876 | 0 | (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 || |
1877 | 0 | (ret = sshbuf_froms(b, &ca)) != 0) { |
1878 | | /* XXX debug print error for ret */ |
1879 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1880 | 0 | goto out; |
1881 | 0 | } |
1882 | | |
1883 | | /* Signature is left in the buffer so we can calculate this length */ |
1884 | 0 | signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b); |
1885 | |
|
1886 | 0 | if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) { |
1887 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1888 | 0 | goto out; |
1889 | 0 | } |
1890 | | |
1891 | 0 | if (key->cert->type != SSH2_CERT_TYPE_USER && |
1892 | 0 | key->cert->type != SSH2_CERT_TYPE_HOST) { |
1893 | 0 | ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE; |
1894 | 0 | goto out; |
1895 | 0 | } |
1896 | | |
1897 | | /* Parse principals section */ |
1898 | 0 | while (sshbuf_len(principals) > 0) { |
1899 | 0 | char *principal = NULL; |
1900 | 0 | char **oprincipals = NULL; |
1901 | |
|
1902 | 0 | if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) { |
1903 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1904 | 0 | goto out; |
1905 | 0 | } |
1906 | 0 | if ((ret = sshbuf_get_cstring(principals, &principal, |
1907 | 0 | NULL)) != 0) { |
1908 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1909 | 0 | goto out; |
1910 | 0 | } |
1911 | 0 | oprincipals = key->cert->principals; |
1912 | 0 | key->cert->principals = recallocarray(key->cert->principals, |
1913 | 0 | key->cert->nprincipals, key->cert->nprincipals + 1, |
1914 | 0 | sizeof(*key->cert->principals)); |
1915 | 0 | if (key->cert->principals == NULL) { |
1916 | 0 | free(principal); |
1917 | 0 | key->cert->principals = oprincipals; |
1918 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
1919 | 0 | goto out; |
1920 | 0 | } |
1921 | 0 | key->cert->principals[key->cert->nprincipals++] = principal; |
1922 | 0 | } |
1923 | | |
1924 | | /* |
1925 | | * Stash a copies of the critical options and extensions sections |
1926 | | * for later use. |
1927 | | */ |
1928 | 0 | if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 || |
1929 | 0 | (exts != NULL && |
1930 | 0 | (ret = sshbuf_putb(key->cert->extensions, exts)) != 0)) |
1931 | 0 | goto out; |
1932 | | |
1933 | | /* |
1934 | | * Validate critical options and extensions sections format. |
1935 | | */ |
1936 | 0 | while (sshbuf_len(crit) != 0) { |
1937 | 0 | if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 || |
1938 | 0 | (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) { |
1939 | 0 | sshbuf_reset(key->cert->critical); |
1940 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1941 | 0 | goto out; |
1942 | 0 | } |
1943 | 0 | } |
1944 | 0 | while (exts != NULL && sshbuf_len(exts) != 0) { |
1945 | 0 | if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 || |
1946 | 0 | (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) { |
1947 | 0 | sshbuf_reset(key->cert->extensions); |
1948 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
1949 | 0 | goto out; |
1950 | 0 | } |
1951 | 0 | } |
1952 | | |
1953 | | /* Parse CA key and check signature */ |
1954 | 0 | if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) { |
1955 | 0 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
1956 | 0 | goto out; |
1957 | 0 | } |
1958 | 0 | if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) { |
1959 | 0 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
1960 | 0 | goto out; |
1961 | 0 | } |
1962 | 0 | if ((ret = sshkey_verify(key->cert->signature_key, sig, slen, |
1963 | 0 | sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0) |
1964 | 0 | goto out; |
1965 | 0 | if ((ret = sshkey_get_sigtype(sig, slen, |
1966 | 0 | &key->cert->signature_type)) != 0) |
1967 | 0 | goto out; |
1968 | | |
1969 | | /* Success */ |
1970 | 0 | ret = 0; |
1971 | 0 | out: |
1972 | 0 | sshbuf_free(ca); |
1973 | 0 | sshbuf_free(crit); |
1974 | 0 | sshbuf_free(exts); |
1975 | 0 | sshbuf_free(principals); |
1976 | 0 | free(sig); |
1977 | 0 | return ret; |
1978 | 0 | } |
1979 | | |
1980 | | int |
1981 | | sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key) |
1982 | 0 | { |
1983 | | /* Parse additional security-key application string */ |
1984 | 0 | if (sshbuf_get_cstring(b, &key->sk_application, NULL) != 0) |
1985 | 0 | return SSH_ERR_INVALID_FORMAT; |
1986 | 0 | return 0; |
1987 | 0 | } |
1988 | | |
1989 | | static int |
1990 | | sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp, |
1991 | | int allow_cert) |
1992 | 0 | { |
1993 | 0 | int type, ret = SSH_ERR_INTERNAL_ERROR; |
1994 | 0 | char *ktype = NULL; |
1995 | 0 | struct sshkey *key = NULL; |
1996 | 0 | struct sshbuf *copy; |
1997 | 0 | const struct sshkey_impl *impl; |
1998 | |
|
1999 | | #ifdef DEBUG_PK /* XXX */ |
2000 | | sshbuf_dump(b, stderr); |
2001 | | #endif |
2002 | 0 | if (keyp != NULL) |
2003 | 0 | *keyp = NULL; |
2004 | 0 | if ((copy = sshbuf_fromb(b)) == NULL) { |
2005 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2006 | 0 | goto out; |
2007 | 0 | } |
2008 | 0 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0) { |
2009 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
2010 | 0 | goto out; |
2011 | 0 | } |
2012 | | |
2013 | 0 | type = sshkey_type_from_name(ktype); |
2014 | 0 | if (!allow_cert && sshkey_type_is_cert(type)) { |
2015 | 0 | ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
2016 | 0 | goto out; |
2017 | 0 | } |
2018 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) { |
2019 | 0 | ret = SSH_ERR_KEY_TYPE_UNKNOWN; |
2020 | 0 | goto out; |
2021 | 0 | } |
2022 | 0 | if ((key = sshkey_new(type)) == NULL) { |
2023 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2024 | 0 | goto out; |
2025 | 0 | } |
2026 | 0 | if (sshkey_type_is_cert(type)) { |
2027 | | /* Skip nonce that precedes all certificates */ |
2028 | 0 | if (sshbuf_get_string_direct(b, NULL, NULL) != 0) { |
2029 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
2030 | 0 | goto out; |
2031 | 0 | } |
2032 | 0 | } |
2033 | 0 | if ((ret = impl->funcs->deserialize_public(ktype, b, key)) != 0) |
2034 | 0 | goto out; |
2035 | | |
2036 | | /* Parse certificate potion */ |
2037 | 0 | if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0) |
2038 | 0 | goto out; |
2039 | | |
2040 | 0 | if (key != NULL && sshbuf_len(b) != 0) { |
2041 | 0 | ret = SSH_ERR_INVALID_FORMAT; |
2042 | 0 | goto out; |
2043 | 0 | } |
2044 | 0 | ret = 0; |
2045 | 0 | if (keyp != NULL) { |
2046 | 0 | *keyp = key; |
2047 | 0 | key = NULL; |
2048 | 0 | } |
2049 | 0 | out: |
2050 | 0 | sshbuf_free(copy); |
2051 | 0 | sshkey_free(key); |
2052 | 0 | free(ktype); |
2053 | 0 | return ret; |
2054 | 0 | } |
2055 | | |
2056 | | int |
2057 | | sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp) |
2058 | 0 | { |
2059 | 0 | struct sshbuf *b; |
2060 | 0 | int r; |
2061 | |
|
2062 | 0 | if ((b = sshbuf_from(blob, blen)) == NULL) |
2063 | 0 | return SSH_ERR_ALLOC_FAIL; |
2064 | 0 | r = sshkey_from_blob_internal(b, keyp, 1); |
2065 | 0 | sshbuf_free(b); |
2066 | 0 | return r; |
2067 | 0 | } |
2068 | | |
2069 | | int |
2070 | | sshkey_fromb(struct sshbuf *b, struct sshkey **keyp) |
2071 | 0 | { |
2072 | 0 | return sshkey_from_blob_internal(b, keyp, 1); |
2073 | 0 | } |
2074 | | |
2075 | | int |
2076 | | sshkey_froms(struct sshbuf *buf, struct sshkey **keyp) |
2077 | 0 | { |
2078 | 0 | struct sshbuf *b; |
2079 | 0 | int r; |
2080 | |
|
2081 | 0 | if ((r = sshbuf_froms(buf, &b)) != 0) |
2082 | 0 | return r; |
2083 | 0 | r = sshkey_from_blob_internal(b, keyp, 1); |
2084 | 0 | sshbuf_free(b); |
2085 | 0 | return r; |
2086 | 0 | } |
2087 | | |
2088 | | int |
2089 | | sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep) |
2090 | 0 | { |
2091 | 0 | int r; |
2092 | 0 | struct sshbuf *b = NULL; |
2093 | 0 | char *sigtype = NULL; |
2094 | |
|
2095 | 0 | if (sigtypep != NULL) |
2096 | 0 | *sigtypep = NULL; |
2097 | 0 | if ((b = sshbuf_from(sig, siglen)) == NULL) |
2098 | 0 | return SSH_ERR_ALLOC_FAIL; |
2099 | 0 | if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0) |
2100 | 0 | goto out; |
2101 | | /* success */ |
2102 | 0 | if (sigtypep != NULL) { |
2103 | 0 | *sigtypep = sigtype; |
2104 | 0 | sigtype = NULL; |
2105 | 0 | } |
2106 | 0 | r = 0; |
2107 | 0 | out: |
2108 | 0 | free(sigtype); |
2109 | 0 | sshbuf_free(b); |
2110 | 0 | return r; |
2111 | 0 | } |
2112 | | |
2113 | | /* |
2114 | | * |
2115 | | * Checks whether a certificate's signature type is allowed. |
2116 | | * Returns 0 (success) if the certificate signature type appears in the |
2117 | | * "allowed" pattern-list, or the key is not a certificate to begin with. |
2118 | | * Otherwise returns a ssherr.h code. |
2119 | | */ |
2120 | | int |
2121 | | sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed) |
2122 | 0 | { |
2123 | 0 | if (key == NULL || allowed == NULL) |
2124 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2125 | 0 | if (!sshkey_type_is_cert(key->type)) |
2126 | 0 | return 0; |
2127 | 0 | if (key->cert == NULL || key->cert->signature_type == NULL) |
2128 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2129 | 0 | if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1) |
2130 | 0 | return SSH_ERR_SIGN_ALG_UNSUPPORTED; |
2131 | 0 | return 0; |
2132 | 0 | } |
2133 | | |
2134 | | /* |
2135 | | * Returns the expected signature algorithm for a given public key algorithm. |
2136 | | */ |
2137 | | const char * |
2138 | | sshkey_sigalg_by_name(const char *name) |
2139 | 0 | { |
2140 | 0 | const struct sshkey_impl *impl; |
2141 | 0 | int i; |
2142 | |
|
2143 | 0 | for (i = 0; keyimpls[i] != NULL; i++) { |
2144 | 0 | impl = keyimpls[i]; |
2145 | 0 | if (strcmp(impl->name, name) != 0) |
2146 | 0 | continue; |
2147 | 0 | if (impl->sigalg != NULL) |
2148 | 0 | return impl->sigalg; |
2149 | 0 | if (!impl->cert) |
2150 | 0 | return impl->name; |
2151 | 0 | return sshkey_ssh_name_from_type_nid( |
2152 | 0 | sshkey_type_plain(impl->type), impl->nid); |
2153 | 0 | } |
2154 | 0 | return NULL; |
2155 | 0 | } |
2156 | | |
2157 | | /* |
2158 | | * Verifies that the signature algorithm appearing inside the signature blob |
2159 | | * matches that which was requested. |
2160 | | */ |
2161 | | int |
2162 | | sshkey_check_sigtype(const u_char *sig, size_t siglen, |
2163 | | const char *requested_alg) |
2164 | 0 | { |
2165 | 0 | const char *expected_alg; |
2166 | 0 | char *sigtype = NULL; |
2167 | 0 | int r; |
2168 | |
|
2169 | 0 | if (requested_alg == NULL) |
2170 | 0 | return 0; |
2171 | 0 | if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL) |
2172 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2173 | 0 | if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) |
2174 | 0 | return r; |
2175 | 0 | r = strcmp(expected_alg, sigtype) == 0; |
2176 | 0 | free(sigtype); |
2177 | 0 | return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED; |
2178 | 0 | } |
2179 | | |
2180 | | int |
2181 | | sshkey_sign(struct sshkey *key, |
2182 | | u_char **sigp, size_t *lenp, |
2183 | | const u_char *data, size_t datalen, |
2184 | | const char *alg, const char *sk_provider, const char *sk_pin, u_int compat) |
2185 | 0 | { |
2186 | 0 | int was_shielded = sshkey_is_shielded(key); |
2187 | 0 | int r2, r = SSH_ERR_INTERNAL_ERROR; |
2188 | 0 | const struct sshkey_impl *impl; |
2189 | |
|
2190 | 0 | if (sigp != NULL) |
2191 | 0 | *sigp = NULL; |
2192 | 0 | if (lenp != NULL) |
2193 | 0 | *lenp = 0; |
2194 | 0 | if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
2195 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2196 | 0 | if ((impl = sshkey_impl_from_key(key)) == NULL) |
2197 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
2198 | 0 | if ((r = sshkey_unshield_private(key)) != 0) |
2199 | 0 | return r; |
2200 | 0 | if (sshkey_is_sk(key)) { |
2201 | 0 | r = sshsk_sign(sk_provider, key, sigp, lenp, data, |
2202 | 0 | datalen, compat, sk_pin); |
2203 | 0 | } else { |
2204 | 0 | if (impl->funcs->sign == NULL) |
2205 | 0 | r = SSH_ERR_SIGN_ALG_UNSUPPORTED; |
2206 | 0 | else { |
2207 | 0 | r = impl->funcs->sign(key, sigp, lenp, data, datalen, |
2208 | 0 | alg, sk_provider, sk_pin, compat); |
2209 | 0 | } |
2210 | 0 | } |
2211 | 0 | if (was_shielded && (r2 = sshkey_shield_private(key)) != 0) |
2212 | 0 | return r2; |
2213 | 0 | return r; |
2214 | 0 | } |
2215 | | |
2216 | | /* |
2217 | | * ssh_key_verify returns 0 for a correct signature and < 0 on error. |
2218 | | * If "alg" specified, then the signature must use that algorithm. |
2219 | | */ |
2220 | | int |
2221 | | sshkey_verify(const struct sshkey *key, |
2222 | | const u_char *sig, size_t siglen, |
2223 | | const u_char *data, size_t dlen, const char *alg, u_int compat, |
2224 | | struct sshkey_sig_details **detailsp) |
2225 | 0 | { |
2226 | 0 | const struct sshkey_impl *impl; |
2227 | |
|
2228 | 0 | if (detailsp != NULL) |
2229 | 0 | *detailsp = NULL; |
2230 | 0 | if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
2231 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2232 | 0 | if ((impl = sshkey_impl_from_key(key)) == NULL) |
2233 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
2234 | 0 | return impl->funcs->verify(key, sig, siglen, data, dlen, |
2235 | 0 | alg, compat, detailsp); |
2236 | 0 | } |
2237 | | |
2238 | | /* Convert a plain key to their _CERT equivalent */ |
2239 | | int |
2240 | | sshkey_to_certified(struct sshkey *k) |
2241 | 0 | { |
2242 | 0 | int newtype; |
2243 | |
|
2244 | 0 | if ((newtype = sshkey_type_certified(k->type)) == -1) |
2245 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2246 | 0 | if ((k->cert = cert_new()) == NULL) |
2247 | 0 | return SSH_ERR_ALLOC_FAIL; |
2248 | 0 | k->type = newtype; |
2249 | 0 | return 0; |
2250 | 0 | } |
2251 | | |
2252 | | /* Convert a certificate to its raw key equivalent */ |
2253 | | int |
2254 | | sshkey_drop_cert(struct sshkey *k) |
2255 | 0 | { |
2256 | 0 | if (!sshkey_type_is_cert(k->type)) |
2257 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
2258 | 0 | cert_free(k->cert); |
2259 | 0 | k->cert = NULL; |
2260 | 0 | k->type = sshkey_type_plain(k->type); |
2261 | 0 | return 0; |
2262 | 0 | } |
2263 | | |
2264 | | /* Sign a certified key, (re-)generating the signed certblob. */ |
2265 | | int |
2266 | | sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg, |
2267 | | const char *sk_provider, const char *sk_pin, |
2268 | | sshkey_certify_signer *signer, void *signer_ctx) |
2269 | 0 | { |
2270 | 0 | const struct sshkey_impl *impl; |
2271 | 0 | struct sshbuf *principals = NULL; |
2272 | 0 | u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32]; |
2273 | 0 | size_t i, ca_len, sig_len; |
2274 | 0 | int ret = SSH_ERR_INTERNAL_ERROR; |
2275 | 0 | struct sshbuf *cert = NULL; |
2276 | 0 | char *sigtype = NULL; |
2277 | |
|
2278 | 0 | if (k == NULL || k->cert == NULL || |
2279 | 0 | k->cert->certblob == NULL || ca == NULL) |
2280 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2281 | 0 | if (!sshkey_is_cert(k)) |
2282 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
2283 | 0 | if (!sshkey_type_is_valid_ca(ca->type)) |
2284 | 0 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
2285 | 0 | if ((impl = sshkey_impl_from_key(k)) == NULL) |
2286 | 0 | return SSH_ERR_INTERNAL_ERROR; |
2287 | | |
2288 | | /* |
2289 | | * If no alg specified as argument but a signature_type was set, |
2290 | | * then prefer that. If both were specified, then they must match. |
2291 | | */ |
2292 | 0 | if (alg == NULL) |
2293 | 0 | alg = k->cert->signature_type; |
2294 | 0 | else if (k->cert->signature_type != NULL && |
2295 | 0 | strcmp(alg, k->cert->signature_type) != 0) |
2296 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2297 | | |
2298 | | /* |
2299 | | * If no signing algorithm or signature_type was specified and we're |
2300 | | * using a RSA key, then default to a good signature algorithm. |
2301 | | */ |
2302 | 0 | if (alg == NULL && ca->type == KEY_RSA) |
2303 | 0 | alg = "rsa-sha2-512"; |
2304 | |
|
2305 | 0 | if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0) |
2306 | 0 | return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY; |
2307 | | |
2308 | 0 | cert = k->cert->certblob; /* for readability */ |
2309 | 0 | sshbuf_reset(cert); |
2310 | 0 | if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0) |
2311 | 0 | goto out; |
2312 | | |
2313 | | /* -v01 certs put nonce first */ |
2314 | 0 | arc4random_buf(&nonce, sizeof(nonce)); |
2315 | 0 | if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0) |
2316 | 0 | goto out; |
2317 | | |
2318 | | /* Public key next */ |
2319 | 0 | if ((ret = impl->funcs->serialize_public(k, cert, |
2320 | 0 | SSHKEY_SERIALIZE_DEFAULT)) != 0) |
2321 | 0 | goto out; |
2322 | | |
2323 | | /* Then remaining cert fields */ |
2324 | 0 | if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 || |
2325 | 0 | (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 || |
2326 | 0 | (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0) |
2327 | 0 | goto out; |
2328 | | |
2329 | 0 | if ((principals = sshbuf_new()) == NULL) { |
2330 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2331 | 0 | goto out; |
2332 | 0 | } |
2333 | 0 | for (i = 0; i < k->cert->nprincipals; i++) { |
2334 | 0 | if ((ret = sshbuf_put_cstring(principals, |
2335 | 0 | k->cert->principals[i])) != 0) |
2336 | 0 | goto out; |
2337 | 0 | } |
2338 | 0 | if ((ret = sshbuf_put_stringb(cert, principals)) != 0 || |
2339 | 0 | (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 || |
2340 | 0 | (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 || |
2341 | 0 | (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 || |
2342 | 0 | (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 || |
2343 | 0 | (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */ |
2344 | 0 | (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0) |
2345 | 0 | goto out; |
2346 | | |
2347 | | /* Sign the whole mess */ |
2348 | 0 | if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert), |
2349 | 0 | sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0) |
2350 | 0 | goto out; |
2351 | | /* Check and update signature_type against what was actually used */ |
2352 | 0 | if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0) |
2353 | 0 | goto out; |
2354 | 0 | if (alg != NULL && strcmp(alg, sigtype) != 0) { |
2355 | 0 | ret = SSH_ERR_SIGN_ALG_UNSUPPORTED; |
2356 | 0 | goto out; |
2357 | 0 | } |
2358 | 0 | if (k->cert->signature_type == NULL) { |
2359 | 0 | k->cert->signature_type = sigtype; |
2360 | 0 | sigtype = NULL; |
2361 | 0 | } |
2362 | | /* Append signature and we are done */ |
2363 | 0 | if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0) |
2364 | 0 | goto out; |
2365 | 0 | ret = 0; |
2366 | 0 | out: |
2367 | 0 | if (ret != 0) |
2368 | 0 | sshbuf_reset(cert); |
2369 | 0 | free(sig_blob); |
2370 | 0 | free(ca_blob); |
2371 | 0 | free(sigtype); |
2372 | 0 | sshbuf_free(principals); |
2373 | 0 | return ret; |
2374 | 0 | } |
2375 | | |
2376 | | static int |
2377 | | default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp, |
2378 | | const u_char *data, size_t datalen, |
2379 | | const char *alg, const char *sk_provider, const char *sk_pin, |
2380 | | u_int compat, void *ctx) |
2381 | 0 | { |
2382 | 0 | if (ctx != NULL) |
2383 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2384 | 0 | return sshkey_sign(key, sigp, lenp, data, datalen, alg, |
2385 | 0 | sk_provider, sk_pin, compat); |
2386 | 0 | } |
2387 | | |
2388 | | int |
2389 | | sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg, |
2390 | | const char *sk_provider, const char *sk_pin) |
2391 | 0 | { |
2392 | 0 | return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin, |
2393 | 0 | default_key_sign, NULL); |
2394 | 0 | } |
2395 | | |
2396 | | int |
2397 | | sshkey_cert_check_authority(const struct sshkey *k, |
2398 | | int want_host, int require_principal, int wildcard_pattern, |
2399 | | uint64_t verify_time, const char *name, const char **reason) |
2400 | 0 | { |
2401 | 0 | u_int i, principal_matches; |
2402 | |
|
2403 | 0 | if (reason == NULL) |
2404 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2405 | 0 | if (!sshkey_is_cert(k)) { |
2406 | 0 | *reason = "Key is not a certificate"; |
2407 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2408 | 0 | } |
2409 | 0 | if (want_host) { |
2410 | 0 | if (k->cert->type != SSH2_CERT_TYPE_HOST) { |
2411 | 0 | *reason = "Certificate invalid: not a host certificate"; |
2412 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2413 | 0 | } |
2414 | 0 | } else { |
2415 | 0 | if (k->cert->type != SSH2_CERT_TYPE_USER) { |
2416 | 0 | *reason = "Certificate invalid: not a user certificate"; |
2417 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2418 | 0 | } |
2419 | 0 | } |
2420 | 0 | if (verify_time < k->cert->valid_after) { |
2421 | 0 | *reason = "Certificate invalid: not yet valid"; |
2422 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2423 | 0 | } |
2424 | 0 | if (verify_time >= k->cert->valid_before) { |
2425 | 0 | *reason = "Certificate invalid: expired"; |
2426 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2427 | 0 | } |
2428 | 0 | if (k->cert->nprincipals == 0) { |
2429 | 0 | if (require_principal) { |
2430 | 0 | *reason = "Certificate lacks principal list"; |
2431 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2432 | 0 | } |
2433 | 0 | } else if (name != NULL) { |
2434 | 0 | principal_matches = 0; |
2435 | 0 | for (i = 0; i < k->cert->nprincipals; i++) { |
2436 | 0 | if (wildcard_pattern) { |
2437 | 0 | if (match_pattern(k->cert->principals[i], |
2438 | 0 | name)) { |
2439 | 0 | principal_matches = 1; |
2440 | 0 | break; |
2441 | 0 | } |
2442 | 0 | } else if (strcmp(name, k->cert->principals[i]) == 0) { |
2443 | 0 | principal_matches = 1; |
2444 | 0 | break; |
2445 | 0 | } |
2446 | 0 | } |
2447 | 0 | if (!principal_matches) { |
2448 | 0 | *reason = "Certificate invalid: name is not a listed " |
2449 | 0 | "principal"; |
2450 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2451 | 0 | } |
2452 | 0 | } |
2453 | 0 | return 0; |
2454 | 0 | } |
2455 | | |
2456 | | int |
2457 | | sshkey_cert_check_authority_now(const struct sshkey *k, |
2458 | | int want_host, int require_principal, int wildcard_pattern, |
2459 | | const char *name, const char **reason) |
2460 | 0 | { |
2461 | 0 | time_t now; |
2462 | |
|
2463 | 0 | if ((now = time(NULL)) < 0) { |
2464 | | /* yikes - system clock before epoch! */ |
2465 | 0 | *reason = "Certificate invalid: not yet valid"; |
2466 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2467 | 0 | } |
2468 | 0 | return sshkey_cert_check_authority(k, want_host, require_principal, |
2469 | 0 | wildcard_pattern, (uint64_t)now, name, reason); |
2470 | 0 | } |
2471 | | |
2472 | | int |
2473 | | sshkey_cert_check_host(const struct sshkey *key, const char *host, |
2474 | | int wildcard_principals, const char *ca_sign_algorithms, |
2475 | | const char **reason) |
2476 | 0 | { |
2477 | 0 | int r; |
2478 | |
|
2479 | 0 | if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals, |
2480 | 0 | host, reason)) != 0) |
2481 | 0 | return r; |
2482 | 0 | if (sshbuf_len(key->cert->critical) != 0) { |
2483 | 0 | *reason = "Certificate contains unsupported critical options"; |
2484 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2485 | 0 | } |
2486 | 0 | if (ca_sign_algorithms != NULL && |
2487 | 0 | (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) { |
2488 | 0 | *reason = "Certificate signed with disallowed algorithm"; |
2489 | 0 | return SSH_ERR_KEY_CERT_INVALID; |
2490 | 0 | } |
2491 | 0 | return 0; |
2492 | 0 | } |
2493 | | |
2494 | | size_t |
2495 | | sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l) |
2496 | 0 | { |
2497 | 0 | char from[32], to[32], ret[128]; |
2498 | |
|
2499 | 0 | *from = *to = '\0'; |
2500 | 0 | if (cert->valid_after == 0 && |
2501 | 0 | cert->valid_before == 0xffffffffffffffffULL) |
2502 | 0 | return strlcpy(s, "forever", l); |
2503 | | |
2504 | 0 | if (cert->valid_after != 0) |
2505 | 0 | format_absolute_time(cert->valid_after, from, sizeof(from)); |
2506 | 0 | if (cert->valid_before != 0xffffffffffffffffULL) |
2507 | 0 | format_absolute_time(cert->valid_before, to, sizeof(to)); |
2508 | |
|
2509 | 0 | if (cert->valid_after == 0) |
2510 | 0 | snprintf(ret, sizeof(ret), "before %s", to); |
2511 | 0 | else if (cert->valid_before == 0xffffffffffffffffULL) |
2512 | 0 | snprintf(ret, sizeof(ret), "after %s", from); |
2513 | 0 | else |
2514 | 0 | snprintf(ret, sizeof(ret), "from %s to %s", from, to); |
2515 | |
|
2516 | 0 | return strlcpy(s, ret, l); |
2517 | 0 | } |
2518 | | |
2519 | | /* Common serialization for FIDO private keys */ |
2520 | | int |
2521 | | sshkey_serialize_private_sk(const struct sshkey *key, struct sshbuf *b) |
2522 | 0 | { |
2523 | 0 | int r; |
2524 | |
|
2525 | 0 | if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0 || |
2526 | 0 | (r = sshbuf_put_u8(b, key->sk_flags)) != 0 || |
2527 | 0 | (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 || |
2528 | 0 | (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0) |
2529 | 0 | return r; |
2530 | | |
2531 | 0 | return 0; |
2532 | 0 | } |
2533 | | |
2534 | | int |
2535 | | sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf, |
2536 | | enum sshkey_serialize_rep opts) |
2537 | 0 | { |
2538 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
2539 | 0 | int was_shielded = sshkey_is_shielded(key); |
2540 | 0 | struct sshbuf *b = NULL; |
2541 | 0 | const struct sshkey_impl *impl; |
2542 | |
|
2543 | 0 | if ((impl = sshkey_impl_from_key(key)) == NULL) |
2544 | 0 | return SSH_ERR_INTERNAL_ERROR; |
2545 | 0 | if ((r = sshkey_unshield_private(key)) != 0) |
2546 | 0 | return r; |
2547 | 0 | if ((b = sshbuf_new()) == NULL) |
2548 | 0 | return SSH_ERR_ALLOC_FAIL; |
2549 | 0 | if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0) |
2550 | 0 | goto out; |
2551 | 0 | if (sshkey_is_cert(key)) { |
2552 | 0 | if (key->cert == NULL || |
2553 | 0 | sshbuf_len(key->cert->certblob) == 0) { |
2554 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
2555 | 0 | goto out; |
2556 | 0 | } |
2557 | 0 | if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0) |
2558 | 0 | goto out; |
2559 | 0 | } |
2560 | 0 | if ((r = impl->funcs->serialize_private(key, b, opts)) != 0) |
2561 | 0 | goto out; |
2562 | | |
2563 | | /* |
2564 | | * success (but we still need to append the output to buf after |
2565 | | * possibly re-shielding the private key) |
2566 | | */ |
2567 | 0 | r = 0; |
2568 | 0 | out: |
2569 | 0 | if (was_shielded) |
2570 | 0 | r = sshkey_shield_private(key); |
2571 | 0 | if (r == 0) |
2572 | 0 | r = sshbuf_putb(buf, b); |
2573 | 0 | sshbuf_free(b); |
2574 | |
|
2575 | 0 | return r; |
2576 | 0 | } |
2577 | | |
2578 | | int |
2579 | | sshkey_private_serialize(struct sshkey *key, struct sshbuf *b) |
2580 | 0 | { |
2581 | 0 | return sshkey_private_serialize_opt(key, b, |
2582 | 0 | SSHKEY_SERIALIZE_DEFAULT); |
2583 | 0 | } |
2584 | | |
2585 | | /* Shared deserialization of FIDO private key components */ |
2586 | | int |
2587 | | sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k) |
2588 | 0 | { |
2589 | 0 | int r; |
2590 | |
|
2591 | 0 | if ((k->sk_key_handle = sshbuf_new()) == NULL || |
2592 | 0 | (k->sk_reserved = sshbuf_new()) == NULL) |
2593 | 0 | return SSH_ERR_ALLOC_FAIL; |
2594 | 0 | if ((r = sshbuf_get_cstring(buf, &k->sk_application, NULL)) != 0 || |
2595 | 0 | (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 || |
2596 | 0 | (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 || |
2597 | 0 | (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0) |
2598 | 0 | return r; |
2599 | | |
2600 | 0 | return 0; |
2601 | 0 | } |
2602 | | |
2603 | | int |
2604 | | sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp) |
2605 | 0 | { |
2606 | 0 | const struct sshkey_impl *impl; |
2607 | 0 | char *tname = NULL; |
2608 | 0 | char *expect_sk_application = NULL; |
2609 | 0 | u_char *expect_ed25519_pk = NULL; |
2610 | 0 | struct sshkey *k = NULL; |
2611 | 0 | int type, r = SSH_ERR_INTERNAL_ERROR; |
2612 | |
|
2613 | 0 | if (kp != NULL) |
2614 | 0 | *kp = NULL; |
2615 | 0 | if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0) |
2616 | 0 | goto out; |
2617 | 0 | type = sshkey_type_from_name(tname); |
2618 | 0 | if (sshkey_type_is_cert(type)) { |
2619 | | /* |
2620 | | * Certificate key private keys begin with the certificate |
2621 | | * itself. Make sure this matches the type of the enclosing |
2622 | | * private key. |
2623 | | */ |
2624 | 0 | if ((r = sshkey_froms(buf, &k)) != 0) |
2625 | 0 | goto out; |
2626 | 0 | if (k->type != type) { |
2627 | 0 | r = SSH_ERR_KEY_CERT_MISMATCH; |
2628 | 0 | goto out; |
2629 | 0 | } |
2630 | | /* For ECDSA keys, the group must match too */ |
2631 | 0 | if (k->type == KEY_ECDSA && |
2632 | 0 | k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) { |
2633 | 0 | r = SSH_ERR_KEY_CERT_MISMATCH; |
2634 | 0 | goto out; |
2635 | 0 | } |
2636 | | /* |
2637 | | * Several fields are redundant between certificate and |
2638 | | * private key body, we require these to match. |
2639 | | */ |
2640 | 0 | expect_sk_application = k->sk_application; |
2641 | 0 | expect_ed25519_pk = k->ed25519_pk; |
2642 | 0 | k->sk_application = NULL; |
2643 | 0 | k->ed25519_pk = NULL; |
2644 | | /* XXX xmss too or refactor */ |
2645 | 0 | } else { |
2646 | 0 | if ((k = sshkey_new(type)) == NULL) { |
2647 | 0 | r = SSH_ERR_ALLOC_FAIL; |
2648 | 0 | goto out; |
2649 | 0 | } |
2650 | 0 | } |
2651 | 0 | if ((impl = sshkey_impl_from_type(type)) == NULL) { |
2652 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
2653 | 0 | goto out; |
2654 | 0 | } |
2655 | 0 | if ((r = impl->funcs->deserialize_private(tname, buf, k)) != 0) |
2656 | 0 | goto out; |
2657 | | |
2658 | | /* XXX xmss too or refactor */ |
2659 | 0 | if ((expect_sk_application != NULL && (k->sk_application == NULL || |
2660 | 0 | strcmp(expect_sk_application, k->sk_application) != 0)) || |
2661 | 0 | (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL || |
2662 | 0 | memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) { |
2663 | 0 | r = SSH_ERR_KEY_CERT_MISMATCH; |
2664 | 0 | goto out; |
2665 | 0 | } |
2666 | | /* success */ |
2667 | 0 | r = 0; |
2668 | 0 | if (kp != NULL) { |
2669 | 0 | *kp = k; |
2670 | 0 | k = NULL; |
2671 | 0 | } |
2672 | 0 | out: |
2673 | 0 | free(tname); |
2674 | 0 | sshkey_free(k); |
2675 | 0 | free(expect_sk_application); |
2676 | 0 | free(expect_ed25519_pk); |
2677 | 0 | return r; |
2678 | 0 | } |
2679 | | |
2680 | | #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) |
2681 | | int |
2682 | | sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public) |
2683 | 0 | { |
2684 | 0 | EC_POINT *nq = NULL; |
2685 | 0 | BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL; |
2686 | 0 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
2687 | | |
2688 | | /* |
2689 | | * NB. This assumes OpenSSL has already verified that the public |
2690 | | * point lies on the curve. This is done by EC_POINT_oct2point() |
2691 | | * implicitly calling EC_POINT_is_on_curve(). If this code is ever |
2692 | | * reachable with public points not unmarshalled using |
2693 | | * EC_POINT_oct2point then the caller will need to explicitly check. |
2694 | | */ |
2695 | | |
2696 | | /* Q != infinity */ |
2697 | 0 | if (EC_POINT_is_at_infinity(group, public)) |
2698 | 0 | goto out; |
2699 | | |
2700 | 0 | if ((x = BN_new()) == NULL || |
2701 | 0 | (y = BN_new()) == NULL || |
2702 | 0 | (order = BN_new()) == NULL || |
2703 | 0 | (tmp = BN_new()) == NULL) { |
2704 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2705 | 0 | goto out; |
2706 | 0 | } |
2707 | | |
2708 | | /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */ |
2709 | 0 | if (EC_GROUP_get_order(group, order, NULL) != 1 || |
2710 | 0 | EC_POINT_get_affine_coordinates(group, public, x, y, NULL) != 1) { |
2711 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
2712 | 0 | goto out; |
2713 | 0 | } |
2714 | 0 | if (BN_num_bits(x) <= BN_num_bits(order) / 2 || |
2715 | 0 | BN_num_bits(y) <= BN_num_bits(order) / 2) |
2716 | 0 | goto out; |
2717 | | |
2718 | | /* nQ == infinity (n == order of subgroup) */ |
2719 | 0 | if ((nq = EC_POINT_new(group)) == NULL) { |
2720 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2721 | 0 | goto out; |
2722 | 0 | } |
2723 | 0 | if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) { |
2724 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
2725 | 0 | goto out; |
2726 | 0 | } |
2727 | 0 | if (EC_POINT_is_at_infinity(group, nq) != 1) |
2728 | 0 | goto out; |
2729 | | |
2730 | | /* x < order - 1, y < order - 1 */ |
2731 | 0 | if (!BN_sub(tmp, order, BN_value_one())) { |
2732 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
2733 | 0 | goto out; |
2734 | 0 | } |
2735 | 0 | if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0) |
2736 | 0 | goto out; |
2737 | 0 | ret = 0; |
2738 | 0 | out: |
2739 | 0 | BN_clear_free(x); |
2740 | 0 | BN_clear_free(y); |
2741 | 0 | BN_clear_free(order); |
2742 | 0 | BN_clear_free(tmp); |
2743 | 0 | EC_POINT_free(nq); |
2744 | 0 | return ret; |
2745 | 0 | } |
2746 | | |
2747 | | int |
2748 | | sshkey_ec_validate_private(const EC_KEY *key) |
2749 | 0 | { |
2750 | 0 | BIGNUM *order = NULL, *tmp = NULL; |
2751 | 0 | int ret = SSH_ERR_KEY_INVALID_EC_VALUE; |
2752 | |
|
2753 | 0 | if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) { |
2754 | 0 | ret = SSH_ERR_ALLOC_FAIL; |
2755 | 0 | goto out; |
2756 | 0 | } |
2757 | | |
2758 | | /* log2(private) > log2(order)/2 */ |
2759 | 0 | if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) { |
2760 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
2761 | 0 | goto out; |
2762 | 0 | } |
2763 | 0 | if (BN_num_bits(EC_KEY_get0_private_key(key)) <= |
2764 | 0 | BN_num_bits(order) / 2) |
2765 | 0 | goto out; |
2766 | | |
2767 | | /* private < order - 1 */ |
2768 | 0 | if (!BN_sub(tmp, order, BN_value_one())) { |
2769 | 0 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
2770 | 0 | goto out; |
2771 | 0 | } |
2772 | 0 | if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0) |
2773 | 0 | goto out; |
2774 | 0 | ret = 0; |
2775 | 0 | out: |
2776 | 0 | BN_clear_free(order); |
2777 | 0 | BN_clear_free(tmp); |
2778 | 0 | return ret; |
2779 | 0 | } |
2780 | | |
2781 | | void |
2782 | | sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point) |
2783 | 0 | { |
2784 | 0 | BIGNUM *x = NULL, *y = NULL; |
2785 | |
|
2786 | 0 | if (point == NULL) { |
2787 | 0 | fputs("point=(NULL)\n", stderr); |
2788 | 0 | return; |
2789 | 0 | } |
2790 | 0 | if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) { |
2791 | 0 | fprintf(stderr, "%s: BN_new failed\n", __func__); |
2792 | 0 | goto out; |
2793 | 0 | } |
2794 | 0 | if (EC_POINT_get_affine_coordinates(group, point, x, y, NULL) != 1) { |
2795 | 0 | fprintf(stderr, "%s: EC_POINT_get_affine_coordinates\n", |
2796 | 0 | __func__); |
2797 | 0 | goto out; |
2798 | 0 | } |
2799 | 0 | fputs("x=", stderr); |
2800 | 0 | BN_print_fp(stderr, x); |
2801 | 0 | fputs("\ny=", stderr); |
2802 | 0 | BN_print_fp(stderr, y); |
2803 | 0 | fputs("\n", stderr); |
2804 | 0 | out: |
2805 | 0 | BN_clear_free(x); |
2806 | 0 | BN_clear_free(y); |
2807 | 0 | } |
2808 | | |
2809 | | void |
2810 | | sshkey_dump_ec_key(const EC_KEY *key) |
2811 | 0 | { |
2812 | 0 | const BIGNUM *exponent; |
2813 | |
|
2814 | 0 | sshkey_dump_ec_point(EC_KEY_get0_group(key), |
2815 | 0 | EC_KEY_get0_public_key(key)); |
2816 | 0 | fputs("exponent=", stderr); |
2817 | 0 | if ((exponent = EC_KEY_get0_private_key(key)) == NULL) |
2818 | 0 | fputs("(NULL)", stderr); |
2819 | 0 | else |
2820 | 0 | BN_print_fp(stderr, EC_KEY_get0_private_key(key)); |
2821 | 0 | fputs("\n", stderr); |
2822 | 0 | } |
2823 | | #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */ |
2824 | | |
2825 | | static int |
2826 | | sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob, |
2827 | | const char *passphrase, const char *comment, const char *ciphername, |
2828 | | int rounds) |
2829 | 0 | { |
2830 | 0 | u_char *cp, *key = NULL, *pubkeyblob = NULL; |
2831 | 0 | u_char salt[SALT_LEN]; |
2832 | 0 | size_t i, pubkeylen, keylen, ivlen, blocksize, authlen; |
2833 | 0 | u_int check; |
2834 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
2835 | 0 | struct sshcipher_ctx *ciphercontext = NULL; |
2836 | 0 | const struct sshcipher *cipher; |
2837 | 0 | const char *kdfname = KDFNAME; |
2838 | 0 | struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL; |
2839 | |
|
2840 | 0 | if (rounds <= 0) |
2841 | 0 | rounds = DEFAULT_ROUNDS; |
2842 | 0 | if (passphrase == NULL || !strlen(passphrase)) { |
2843 | 0 | ciphername = "none"; |
2844 | 0 | kdfname = "none"; |
2845 | 0 | } else if (ciphername == NULL) |
2846 | 0 | ciphername = DEFAULT_CIPHERNAME; |
2847 | 0 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
2848 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
2849 | 0 | goto out; |
2850 | 0 | } |
2851 | | |
2852 | 0 | if ((kdf = sshbuf_new()) == NULL || |
2853 | 0 | (encoded = sshbuf_new()) == NULL || |
2854 | 0 | (encrypted = sshbuf_new()) == NULL) { |
2855 | 0 | r = SSH_ERR_ALLOC_FAIL; |
2856 | 0 | goto out; |
2857 | 0 | } |
2858 | 0 | blocksize = cipher_blocksize(cipher); |
2859 | 0 | keylen = cipher_keylen(cipher); |
2860 | 0 | ivlen = cipher_ivlen(cipher); |
2861 | 0 | authlen = cipher_authlen(cipher); |
2862 | 0 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
2863 | 0 | r = SSH_ERR_ALLOC_FAIL; |
2864 | 0 | goto out; |
2865 | 0 | } |
2866 | 0 | if (strcmp(kdfname, "bcrypt") == 0) { |
2867 | 0 | arc4random_buf(salt, SALT_LEN); |
2868 | 0 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), |
2869 | 0 | salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) { |
2870 | 0 | r = SSH_ERR_INVALID_ARGUMENT; |
2871 | 0 | goto out; |
2872 | 0 | } |
2873 | 0 | if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 || |
2874 | 0 | (r = sshbuf_put_u32(kdf, rounds)) != 0) |
2875 | 0 | goto out; |
2876 | 0 | } else if (strcmp(kdfname, "none") != 0) { |
2877 | | /* Unsupported KDF type */ |
2878 | 0 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
2879 | 0 | goto out; |
2880 | 0 | } |
2881 | 0 | if ((r = cipher_init(&ciphercontext, cipher, key, keylen, |
2882 | 0 | key + keylen, ivlen, 1)) != 0) |
2883 | 0 | goto out; |
2884 | | |
2885 | 0 | if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 || |
2886 | 0 | (r = sshbuf_put_cstring(encoded, ciphername)) != 0 || |
2887 | 0 | (r = sshbuf_put_cstring(encoded, kdfname)) != 0 || |
2888 | 0 | (r = sshbuf_put_stringb(encoded, kdf)) != 0 || |
2889 | 0 | (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */ |
2890 | 0 | (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 || |
2891 | 0 | (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0) |
2892 | 0 | goto out; |
2893 | | |
2894 | | /* set up the buffer that will be encrypted */ |
2895 | | |
2896 | | /* Random check bytes */ |
2897 | 0 | check = arc4random(); |
2898 | 0 | if ((r = sshbuf_put_u32(encrypted, check)) != 0 || |
2899 | 0 | (r = sshbuf_put_u32(encrypted, check)) != 0) |
2900 | 0 | goto out; |
2901 | | |
2902 | | /* append private key and comment*/ |
2903 | 0 | if ((r = sshkey_private_serialize_opt(prv, encrypted, |
2904 | 0 | SSHKEY_SERIALIZE_FULL)) != 0 || |
2905 | 0 | (r = sshbuf_put_cstring(encrypted, comment)) != 0) |
2906 | 0 | goto out; |
2907 | | |
2908 | | /* padding */ |
2909 | 0 | i = 0; |
2910 | 0 | while (sshbuf_len(encrypted) % blocksize) { |
2911 | 0 | if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0) |
2912 | 0 | goto out; |
2913 | 0 | } |
2914 | | |
2915 | | /* length in destination buffer */ |
2916 | 0 | if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0) |
2917 | 0 | goto out; |
2918 | | |
2919 | | /* encrypt */ |
2920 | 0 | if ((r = sshbuf_reserve(encoded, |
2921 | 0 | sshbuf_len(encrypted) + authlen, &cp)) != 0) |
2922 | 0 | goto out; |
2923 | 0 | if ((r = cipher_crypt(ciphercontext, 0, cp, |
2924 | 0 | sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0) |
2925 | 0 | goto out; |
2926 | | |
2927 | 0 | sshbuf_reset(blob); |
2928 | | |
2929 | | /* assemble uuencoded key */ |
2930 | 0 | if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 || |
2931 | 0 | (r = sshbuf_dtob64(encoded, blob, 1)) != 0 || |
2932 | 0 | (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0) |
2933 | 0 | goto out; |
2934 | | |
2935 | | /* success */ |
2936 | 0 | r = 0; |
2937 | |
|
2938 | 0 | out: |
2939 | 0 | sshbuf_free(kdf); |
2940 | 0 | sshbuf_free(encoded); |
2941 | 0 | sshbuf_free(encrypted); |
2942 | 0 | cipher_free(ciphercontext); |
2943 | 0 | explicit_bzero(salt, sizeof(salt)); |
2944 | 0 | if (key != NULL) |
2945 | 0 | freezero(key, keylen + ivlen); |
2946 | 0 | if (pubkeyblob != NULL) |
2947 | 0 | freezero(pubkeyblob, pubkeylen); |
2948 | 0 | return r; |
2949 | 0 | } |
2950 | | |
2951 | | static int |
2952 | | private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp) |
2953 | 0 | { |
2954 | 0 | const u_char *cp; |
2955 | 0 | size_t encoded_len; |
2956 | 0 | int r; |
2957 | 0 | u_char last; |
2958 | 0 | struct sshbuf *encoded = NULL, *decoded = NULL; |
2959 | |
|
2960 | 0 | if (blob == NULL || decodedp == NULL) |
2961 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
2962 | | |
2963 | 0 | *decodedp = NULL; |
2964 | |
|
2965 | 0 | if ((encoded = sshbuf_new()) == NULL || |
2966 | 0 | (decoded = sshbuf_new()) == NULL) { |
2967 | 0 | r = SSH_ERR_ALLOC_FAIL; |
2968 | 0 | goto out; |
2969 | 0 | } |
2970 | | |
2971 | | /* check preamble */ |
2972 | 0 | cp = sshbuf_ptr(blob); |
2973 | 0 | encoded_len = sshbuf_len(blob); |
2974 | 0 | if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) || |
2975 | 0 | memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) { |
2976 | 0 | r = SSH_ERR_INVALID_FORMAT; |
2977 | 0 | goto out; |
2978 | 0 | } |
2979 | 0 | cp += MARK_BEGIN_LEN; |
2980 | 0 | encoded_len -= MARK_BEGIN_LEN; |
2981 | | |
2982 | | /* Look for end marker, removing whitespace as we go */ |
2983 | 0 | while (encoded_len > 0) { |
2984 | 0 | if (*cp != '\n' && *cp != '\r') { |
2985 | 0 | if ((r = sshbuf_put_u8(encoded, *cp)) != 0) |
2986 | 0 | goto out; |
2987 | 0 | } |
2988 | 0 | last = *cp; |
2989 | 0 | encoded_len--; |
2990 | 0 | cp++; |
2991 | 0 | if (last == '\n') { |
2992 | 0 | if (encoded_len >= MARK_END_LEN && |
2993 | 0 | memcmp(cp, MARK_END, MARK_END_LEN) == 0) { |
2994 | | /* \0 terminate */ |
2995 | 0 | if ((r = sshbuf_put_u8(encoded, 0)) != 0) |
2996 | 0 | goto out; |
2997 | 0 | break; |
2998 | 0 | } |
2999 | 0 | } |
3000 | 0 | } |
3001 | 0 | if (encoded_len == 0) { |
3002 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3003 | 0 | goto out; |
3004 | 0 | } |
3005 | | |
3006 | | /* decode base64 */ |
3007 | 0 | if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0) |
3008 | 0 | goto out; |
3009 | | |
3010 | | /* check magic */ |
3011 | 0 | if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) || |
3012 | 0 | memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) { |
3013 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3014 | 0 | goto out; |
3015 | 0 | } |
3016 | | /* success */ |
3017 | 0 | *decodedp = decoded; |
3018 | 0 | decoded = NULL; |
3019 | 0 | r = 0; |
3020 | 0 | out: |
3021 | 0 | sshbuf_free(encoded); |
3022 | 0 | sshbuf_free(decoded); |
3023 | 0 | return r; |
3024 | 0 | } |
3025 | | |
3026 | | static int |
3027 | | private2_decrypt(struct sshbuf *decoded, const char *passphrase, |
3028 | | struct sshbuf **decryptedp, struct sshkey **pubkeyp) |
3029 | 0 | { |
3030 | 0 | char *ciphername = NULL, *kdfname = NULL; |
3031 | 0 | const struct sshcipher *cipher = NULL; |
3032 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
3033 | 0 | size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0; |
3034 | 0 | struct sshbuf *kdf = NULL, *decrypted = NULL; |
3035 | 0 | struct sshcipher_ctx *ciphercontext = NULL; |
3036 | 0 | struct sshkey *pubkey = NULL; |
3037 | 0 | u_char *key = NULL, *salt = NULL, *dp; |
3038 | 0 | u_int blocksize, rounds, nkeys, encrypted_len, check1, check2; |
3039 | |
|
3040 | 0 | if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL) |
3041 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
3042 | | |
3043 | 0 | *decryptedp = NULL; |
3044 | 0 | *pubkeyp = NULL; |
3045 | |
|
3046 | 0 | if ((decrypted = sshbuf_new()) == NULL) { |
3047 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3048 | 0 | goto out; |
3049 | 0 | } |
3050 | | |
3051 | | /* parse public portion of key */ |
3052 | 0 | if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 || |
3053 | 0 | (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 || |
3054 | 0 | (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 || |
3055 | 0 | (r = sshbuf_froms(decoded, &kdf)) != 0 || |
3056 | 0 | (r = sshbuf_get_u32(decoded, &nkeys)) != 0) |
3057 | 0 | goto out; |
3058 | | |
3059 | 0 | if (nkeys != 1) { |
3060 | | /* XXX only one key supported at present */ |
3061 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3062 | 0 | goto out; |
3063 | 0 | } |
3064 | | |
3065 | 0 | if ((r = sshkey_froms(decoded, &pubkey)) != 0 || |
3066 | 0 | (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0) |
3067 | 0 | goto out; |
3068 | | |
3069 | 0 | if ((cipher = cipher_by_name(ciphername)) == NULL) { |
3070 | 0 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
3071 | 0 | goto out; |
3072 | 0 | } |
3073 | 0 | if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) { |
3074 | 0 | r = SSH_ERR_KEY_UNKNOWN_CIPHER; |
3075 | 0 | goto out; |
3076 | 0 | } |
3077 | 0 | if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) { |
3078 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3079 | 0 | goto out; |
3080 | 0 | } |
3081 | 0 | if ((passphrase == NULL || strlen(passphrase) == 0) && |
3082 | 0 | strcmp(kdfname, "none") != 0) { |
3083 | | /* passphrase required */ |
3084 | 0 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
3085 | 0 | goto out; |
3086 | 0 | } |
3087 | | |
3088 | | /* check size of encrypted key blob */ |
3089 | 0 | blocksize = cipher_blocksize(cipher); |
3090 | 0 | if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) { |
3091 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3092 | 0 | goto out; |
3093 | 0 | } |
3094 | | |
3095 | | /* setup key */ |
3096 | 0 | keylen = cipher_keylen(cipher); |
3097 | 0 | ivlen = cipher_ivlen(cipher); |
3098 | 0 | authlen = cipher_authlen(cipher); |
3099 | 0 | if ((key = calloc(1, keylen + ivlen)) == NULL) { |
3100 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3101 | 0 | goto out; |
3102 | 0 | } |
3103 | 0 | if (strcmp(kdfname, "bcrypt") == 0) { |
3104 | 0 | if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 || |
3105 | 0 | (r = sshbuf_get_u32(kdf, &rounds)) != 0) |
3106 | 0 | goto out; |
3107 | 0 | if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen, |
3108 | 0 | key, keylen + ivlen, rounds) < 0) { |
3109 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3110 | 0 | goto out; |
3111 | 0 | } |
3112 | 0 | } |
3113 | | |
3114 | | /* check that an appropriate amount of auth data is present */ |
3115 | 0 | if (sshbuf_len(decoded) < authlen || |
3116 | 0 | sshbuf_len(decoded) - authlen < encrypted_len) { |
3117 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3118 | 0 | goto out; |
3119 | 0 | } |
3120 | | |
3121 | | /* decrypt private portion of key */ |
3122 | 0 | if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 || |
3123 | 0 | (r = cipher_init(&ciphercontext, cipher, key, keylen, |
3124 | 0 | key + keylen, ivlen, 0)) != 0) |
3125 | 0 | goto out; |
3126 | 0 | if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded), |
3127 | 0 | encrypted_len, 0, authlen)) != 0) { |
3128 | | /* an integrity error here indicates an incorrect passphrase */ |
3129 | 0 | if (r == SSH_ERR_MAC_INVALID) |
3130 | 0 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
3131 | 0 | goto out; |
3132 | 0 | } |
3133 | 0 | if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0) |
3134 | 0 | goto out; |
3135 | | /* there should be no trailing data */ |
3136 | 0 | if (sshbuf_len(decoded) != 0) { |
3137 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3138 | 0 | goto out; |
3139 | 0 | } |
3140 | | |
3141 | | /* check check bytes */ |
3142 | 0 | if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 || |
3143 | 0 | (r = sshbuf_get_u32(decrypted, &check2)) != 0) |
3144 | 0 | goto out; |
3145 | 0 | if (check1 != check2) { |
3146 | 0 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
3147 | 0 | goto out; |
3148 | 0 | } |
3149 | | /* success */ |
3150 | 0 | *decryptedp = decrypted; |
3151 | 0 | decrypted = NULL; |
3152 | 0 | *pubkeyp = pubkey; |
3153 | 0 | pubkey = NULL; |
3154 | 0 | r = 0; |
3155 | 0 | out: |
3156 | 0 | cipher_free(ciphercontext); |
3157 | 0 | free(ciphername); |
3158 | 0 | free(kdfname); |
3159 | 0 | sshkey_free(pubkey); |
3160 | 0 | if (salt != NULL) { |
3161 | 0 | explicit_bzero(salt, slen); |
3162 | 0 | free(salt); |
3163 | 0 | } |
3164 | 0 | if (key != NULL) { |
3165 | 0 | explicit_bzero(key, keylen + ivlen); |
3166 | 0 | free(key); |
3167 | 0 | } |
3168 | 0 | sshbuf_free(kdf); |
3169 | 0 | sshbuf_free(decrypted); |
3170 | 0 | return r; |
3171 | 0 | } |
3172 | | |
3173 | | static int |
3174 | | sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase, |
3175 | | struct sshkey **keyp, char **commentp) |
3176 | 0 | { |
3177 | 0 | char *comment = NULL; |
3178 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
3179 | 0 | struct sshbuf *decoded = NULL, *decrypted = NULL; |
3180 | 0 | struct sshkey *k = NULL, *pubkey = NULL; |
3181 | |
|
3182 | 0 | if (keyp != NULL) |
3183 | 0 | *keyp = NULL; |
3184 | 0 | if (commentp != NULL) |
3185 | 0 | *commentp = NULL; |
3186 | | |
3187 | | /* Undo base64 encoding and decrypt the private section */ |
3188 | 0 | if ((r = private2_uudecode(blob, &decoded)) != 0 || |
3189 | 0 | (r = private2_decrypt(decoded, passphrase, |
3190 | 0 | &decrypted, &pubkey)) != 0) |
3191 | 0 | goto out; |
3192 | | |
3193 | 0 | if (type != KEY_UNSPEC && |
3194 | 0 | sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) { |
3195 | 0 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
3196 | 0 | goto out; |
3197 | 0 | } |
3198 | | |
3199 | | /* Load the private key and comment */ |
3200 | 0 | if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 || |
3201 | 0 | (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0) |
3202 | 0 | goto out; |
3203 | | |
3204 | | /* Check deterministic padding after private section */ |
3205 | 0 | if ((r = private2_check_padding(decrypted)) != 0) |
3206 | 0 | goto out; |
3207 | | |
3208 | | /* Check that the public key in the envelope matches the private key */ |
3209 | 0 | if (!sshkey_equal(pubkey, k)) { |
3210 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3211 | 0 | goto out; |
3212 | 0 | } |
3213 | | |
3214 | | /* success */ |
3215 | 0 | r = 0; |
3216 | 0 | if (keyp != NULL) { |
3217 | 0 | *keyp = k; |
3218 | 0 | k = NULL; |
3219 | 0 | } |
3220 | 0 | if (commentp != NULL) { |
3221 | 0 | *commentp = comment; |
3222 | 0 | comment = NULL; |
3223 | 0 | } |
3224 | 0 | out: |
3225 | 0 | free(comment); |
3226 | 0 | sshbuf_free(decoded); |
3227 | 0 | sshbuf_free(decrypted); |
3228 | 0 | sshkey_free(k); |
3229 | 0 | sshkey_free(pubkey); |
3230 | 0 | return r; |
3231 | 0 | } |
3232 | | |
3233 | | static int |
3234 | | sshkey_parse_private2_pubkey(struct sshbuf *blob, int type, |
3235 | | struct sshkey **keyp) |
3236 | 0 | { |
3237 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
3238 | 0 | struct sshbuf *decoded = NULL; |
3239 | 0 | struct sshkey *pubkey = NULL; |
3240 | 0 | u_int nkeys = 0; |
3241 | |
|
3242 | 0 | if (keyp != NULL) |
3243 | 0 | *keyp = NULL; |
3244 | |
|
3245 | 0 | if ((r = private2_uudecode(blob, &decoded)) != 0) |
3246 | 0 | goto out; |
3247 | | /* parse public key from unencrypted envelope */ |
3248 | 0 | if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 || |
3249 | 0 | (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */ |
3250 | 0 | (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */ |
3251 | 0 | (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */ |
3252 | 0 | (r = sshbuf_get_u32(decoded, &nkeys)) != 0) |
3253 | 0 | goto out; |
3254 | | |
3255 | 0 | if (nkeys != 1) { |
3256 | | /* XXX only one key supported at present */ |
3257 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3258 | 0 | goto out; |
3259 | 0 | } |
3260 | | |
3261 | | /* Parse the public key */ |
3262 | 0 | if ((r = sshkey_froms(decoded, &pubkey)) != 0) |
3263 | 0 | goto out; |
3264 | | |
3265 | 0 | if (type != KEY_UNSPEC && |
3266 | 0 | sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) { |
3267 | 0 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
3268 | 0 | goto out; |
3269 | 0 | } |
3270 | | |
3271 | | /* success */ |
3272 | 0 | r = 0; |
3273 | 0 | if (keyp != NULL) { |
3274 | 0 | *keyp = pubkey; |
3275 | 0 | pubkey = NULL; |
3276 | 0 | } |
3277 | 0 | out: |
3278 | 0 | sshbuf_free(decoded); |
3279 | 0 | sshkey_free(pubkey); |
3280 | 0 | return r; |
3281 | 0 | } |
3282 | | |
3283 | | #ifdef WITH_OPENSSL |
3284 | | /* convert SSH v2 key to PEM or PKCS#8 format */ |
3285 | | static int |
3286 | | sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf, |
3287 | | int format, const char *_passphrase, const char *comment) |
3288 | 0 | { |
3289 | 0 | int was_shielded = sshkey_is_shielded(key); |
3290 | 0 | int success, r; |
3291 | 0 | int blen, len = strlen(_passphrase); |
3292 | 0 | u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL; |
3293 | 0 | const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL; |
3294 | 0 | char *bptr; |
3295 | 0 | BIO *bio = NULL; |
3296 | 0 | struct sshbuf *blob; |
3297 | 0 | EVP_PKEY *pkey = NULL; |
3298 | |
|
3299 | 0 | if (len > 0 && len <= 4) |
3300 | 0 | return SSH_ERR_PASSPHRASE_TOO_SHORT; |
3301 | 0 | if ((blob = sshbuf_new()) == NULL) |
3302 | 0 | return SSH_ERR_ALLOC_FAIL; |
3303 | 0 | if ((bio = BIO_new(BIO_s_mem())) == NULL) { |
3304 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3305 | 0 | goto out; |
3306 | 0 | } |
3307 | 0 | if ((r = sshkey_unshield_private(key)) != 0) |
3308 | 0 | goto out; |
3309 | | |
3310 | 0 | switch (key->type) { |
3311 | 0 | #ifdef OPENSSL_HAS_ECC |
3312 | 0 | case KEY_ECDSA: |
3313 | 0 | if (format == SSHKEY_PRIVATE_PEM) { |
3314 | 0 | success = PEM_write_bio_ECPrivateKey(bio, |
3315 | 0 | EVP_PKEY_get0_EC_KEY(key->pkey), |
3316 | 0 | cipher, passphrase, len, NULL, NULL); |
3317 | 0 | } else { |
3318 | 0 | pkey = key->pkey; |
3319 | 0 | EVP_PKEY_up_ref(key->pkey); |
3320 | 0 | success = 1; |
3321 | 0 | } |
3322 | 0 | break; |
3323 | 0 | #endif |
3324 | 0 | case KEY_RSA: |
3325 | 0 | if (format == SSHKEY_PRIVATE_PEM) { |
3326 | 0 | success = PEM_write_bio_RSAPrivateKey(bio, |
3327 | 0 | EVP_PKEY_get0_RSA(key->pkey), |
3328 | 0 | cipher, passphrase, len, NULL, NULL); |
3329 | 0 | } else { |
3330 | 0 | pkey = key->pkey; |
3331 | 0 | EVP_PKEY_up_ref(key->pkey); |
3332 | 0 | success = 1; |
3333 | 0 | } |
3334 | 0 | break; |
3335 | 0 | default: |
3336 | 0 | success = 0; |
3337 | 0 | break; |
3338 | 0 | } |
3339 | 0 | if (success == 0) { |
3340 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3341 | 0 | goto out; |
3342 | 0 | } |
3343 | 0 | if (format == SSHKEY_PRIVATE_PKCS8) { |
3344 | 0 | if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher, |
3345 | 0 | passphrase, len, NULL, NULL)) == 0) { |
3346 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3347 | 0 | goto out; |
3348 | 0 | } |
3349 | 0 | } |
3350 | 0 | if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) { |
3351 | 0 | r = SSH_ERR_INTERNAL_ERROR; |
3352 | 0 | goto out; |
3353 | 0 | } |
3354 | 0 | if ((r = sshbuf_put(blob, bptr, blen)) != 0) |
3355 | 0 | goto out; |
3356 | 0 | r = 0; |
3357 | 0 | out: |
3358 | 0 | if (was_shielded) |
3359 | 0 | r = sshkey_shield_private(key); |
3360 | 0 | if (r == 0) |
3361 | 0 | r = sshbuf_putb(buf, blob); |
3362 | |
|
3363 | 0 | EVP_PKEY_free(pkey); |
3364 | 0 | sshbuf_free(blob); |
3365 | 0 | BIO_free(bio); |
3366 | 0 | return r; |
3367 | 0 | } |
3368 | | #endif /* WITH_OPENSSL */ |
3369 | | |
3370 | | /* Serialise "key" to buffer "blob" */ |
3371 | | int |
3372 | | sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob, |
3373 | | const char *passphrase, const char *comment, |
3374 | | int format, const char *openssh_format_cipher, int openssh_format_rounds) |
3375 | 0 | { |
3376 | 0 | switch (key->type) { |
3377 | 0 | #ifdef WITH_OPENSSL |
3378 | 0 | case KEY_ECDSA: |
3379 | 0 | case KEY_RSA: |
3380 | 0 | break; /* see below */ |
3381 | 0 | #endif /* WITH_OPENSSL */ |
3382 | 0 | case KEY_ED25519: |
3383 | 0 | case KEY_ED25519_SK: |
3384 | 0 | #ifdef WITH_XMSS |
3385 | 0 | case KEY_XMSS: |
3386 | 0 | #endif /* WITH_XMSS */ |
3387 | 0 | #ifdef WITH_OPENSSL |
3388 | 0 | case KEY_ECDSA_SK: |
3389 | 0 | #endif /* WITH_OPENSSL */ |
3390 | 0 | return sshkey_private_to_blob2(key, blob, passphrase, |
3391 | 0 | comment, openssh_format_cipher, openssh_format_rounds); |
3392 | 0 | default: |
3393 | 0 | return SSH_ERR_KEY_TYPE_UNKNOWN; |
3394 | 0 | } |
3395 | | |
3396 | 0 | #ifdef WITH_OPENSSL |
3397 | 0 | switch (format) { |
3398 | 0 | case SSHKEY_PRIVATE_OPENSSH: |
3399 | 0 | return sshkey_private_to_blob2(key, blob, passphrase, |
3400 | 0 | comment, openssh_format_cipher, openssh_format_rounds); |
3401 | 0 | case SSHKEY_PRIVATE_PEM: |
3402 | 0 | case SSHKEY_PRIVATE_PKCS8: |
3403 | 0 | return sshkey_private_to_blob_pem_pkcs8(key, blob, |
3404 | 0 | format, passphrase, comment); |
3405 | 0 | default: |
3406 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
3407 | 0 | } |
3408 | 0 | #endif /* WITH_OPENSSL */ |
3409 | 0 | } |
3410 | | |
3411 | | #ifdef WITH_OPENSSL |
3412 | | static int |
3413 | | translate_libcrypto_error(unsigned long pem_err) |
3414 | 0 | { |
3415 | 0 | int pem_reason = ERR_GET_REASON(pem_err); |
3416 | |
|
3417 | 0 | switch (ERR_GET_LIB(pem_err)) { |
3418 | 0 | case ERR_LIB_PEM: |
3419 | 0 | switch (pem_reason) { |
3420 | 0 | case PEM_R_BAD_PASSWORD_READ: |
3421 | 0 | #ifdef PEM_R_PROBLEMS_GETTING_PASSWORD |
3422 | 0 | case PEM_R_PROBLEMS_GETTING_PASSWORD: |
3423 | 0 | #endif |
3424 | 0 | #ifdef PEM_R_BAD_DECRYPT |
3425 | 0 | case PEM_R_BAD_DECRYPT: |
3426 | 0 | #endif |
3427 | 0 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
3428 | 0 | default: |
3429 | 0 | return SSH_ERR_INVALID_FORMAT; |
3430 | 0 | } |
3431 | 0 | case ERR_LIB_EVP: |
3432 | 0 | switch (pem_reason) { |
3433 | 0 | #ifdef EVP_R_BAD_DECRYPT |
3434 | 0 | case EVP_R_BAD_DECRYPT: |
3435 | 0 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
3436 | 0 | #endif |
3437 | | #ifdef EVP_R_BN_DECODE_ERROR |
3438 | | case EVP_R_BN_DECODE_ERROR: |
3439 | | #endif |
3440 | 0 | case EVP_R_DECODE_ERROR: |
3441 | 0 | #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR |
3442 | 0 | case EVP_R_PRIVATE_KEY_DECODE_ERROR: |
3443 | 0 | #endif |
3444 | 0 | return SSH_ERR_INVALID_FORMAT; |
3445 | 0 | default: |
3446 | 0 | return SSH_ERR_LIBCRYPTO_ERROR; |
3447 | 0 | } |
3448 | 0 | case ERR_LIB_ASN1: |
3449 | 0 | return SSH_ERR_INVALID_FORMAT; |
3450 | 0 | } |
3451 | 0 | return SSH_ERR_LIBCRYPTO_ERROR; |
3452 | 0 | } |
3453 | | |
3454 | | static void |
3455 | | clear_libcrypto_errors(void) |
3456 | 0 | { |
3457 | 0 | while (ERR_get_error() != 0) |
3458 | 0 | ; |
3459 | 0 | } |
3460 | | |
3461 | | /* |
3462 | | * Translate OpenSSL error codes to determine whether |
3463 | | * passphrase is required/incorrect. |
3464 | | */ |
3465 | | static int |
3466 | | convert_libcrypto_error(void) |
3467 | 0 | { |
3468 | | /* |
3469 | | * Some password errors are reported at the beginning |
3470 | | * of the error queue. |
3471 | | */ |
3472 | 0 | if (translate_libcrypto_error(ERR_peek_error()) == |
3473 | 0 | SSH_ERR_KEY_WRONG_PASSPHRASE) |
3474 | 0 | return SSH_ERR_KEY_WRONG_PASSPHRASE; |
3475 | 0 | return translate_libcrypto_error(ERR_peek_last_error()); |
3476 | 0 | } |
3477 | | |
3478 | | static int |
3479 | | pem_passphrase_cb(char *buf, int size, int rwflag, void *u) |
3480 | 0 | { |
3481 | 0 | char *p = (char *)u; |
3482 | 0 | size_t len; |
3483 | |
|
3484 | 0 | if (p == NULL || (len = strlen(p)) == 0) |
3485 | 0 | return -1; |
3486 | 0 | if (size < 0 || len > (size_t)size) |
3487 | 0 | return -1; |
3488 | 0 | memcpy(buf, p, len); |
3489 | 0 | return (int)len; |
3490 | 0 | } |
3491 | | |
3492 | | static int |
3493 | | sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type, |
3494 | | const char *passphrase, struct sshkey **keyp) |
3495 | 0 | { |
3496 | 0 | EVP_PKEY *pk = NULL; |
3497 | 0 | struct sshkey *prv = NULL; |
3498 | 0 | BIO *bio = NULL; |
3499 | 0 | int r; |
3500 | 0 | RSA *rsa = NULL; |
3501 | 0 | EC_KEY *ecdsa = NULL; |
3502 | |
|
3503 | 0 | if (keyp != NULL) |
3504 | 0 | *keyp = NULL; |
3505 | |
|
3506 | 0 | if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX) |
3507 | 0 | return SSH_ERR_ALLOC_FAIL; |
3508 | 0 | if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) != |
3509 | 0 | (int)sshbuf_len(blob)) { |
3510 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3511 | 0 | goto out; |
3512 | 0 | } |
3513 | | |
3514 | 0 | clear_libcrypto_errors(); |
3515 | 0 | if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb, |
3516 | 0 | (char *)passphrase)) == NULL) { |
3517 | | /* |
3518 | | * libcrypto may return various ASN.1 errors when attempting |
3519 | | * to parse a key with an incorrect passphrase. |
3520 | | * Treat all format errors as "incorrect passphrase" if a |
3521 | | * passphrase was supplied. |
3522 | | */ |
3523 | 0 | if (passphrase != NULL && *passphrase != '\0') |
3524 | 0 | r = SSH_ERR_KEY_WRONG_PASSPHRASE; |
3525 | 0 | else |
3526 | 0 | r = convert_libcrypto_error(); |
3527 | 0 | goto out; |
3528 | 0 | } |
3529 | 0 | if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA && |
3530 | 0 | (type == KEY_UNSPEC || type == KEY_RSA)) { |
3531 | 0 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
3532 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3533 | 0 | goto out; |
3534 | 0 | } |
3535 | 0 | if ((rsa = EVP_PKEY_get1_RSA(pk)) == NULL) { |
3536 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3537 | 0 | goto out; |
3538 | 0 | } |
3539 | 0 | prv->type = KEY_RSA; |
3540 | | #ifdef DEBUG_PK |
3541 | | RSA_print_fp(stderr, rsa, 8); |
3542 | | #endif |
3543 | 0 | if (RSA_blinding_on(rsa, NULL) != 1 || |
3544 | 0 | EVP_PKEY_set1_RSA(pk, rsa) != 1) { |
3545 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3546 | 0 | goto out; |
3547 | 0 | } |
3548 | 0 | EVP_PKEY_up_ref(pk); |
3549 | 0 | prv->pkey = pk; |
3550 | 0 | if ((r = sshkey_check_rsa_length(prv, 0)) != 0) |
3551 | 0 | goto out; |
3552 | 0 | #ifdef OPENSSL_HAS_ECC |
3553 | 0 | } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC && |
3554 | 0 | (type == KEY_UNSPEC || type == KEY_ECDSA)) { |
3555 | 0 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) { |
3556 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3557 | 0 | goto out; |
3558 | 0 | } |
3559 | 0 | if ((prv->ecdsa_nid = sshkey_ecdsa_fixup_group(pk)) == -1 || |
3560 | 0 | (ecdsa = EVP_PKEY_get1_EC_KEY(pk)) == NULL) { |
3561 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3562 | 0 | goto out; |
3563 | 0 | } |
3564 | 0 | prv->type = KEY_ECDSA; |
3565 | 0 | if (sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL || |
3566 | 0 | sshkey_ec_validate_public(EC_KEY_get0_group(ecdsa), |
3567 | 0 | EC_KEY_get0_public_key(ecdsa)) != 0 || |
3568 | 0 | sshkey_ec_validate_private(ecdsa) != 0) { |
3569 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3570 | 0 | goto out; |
3571 | 0 | } |
3572 | 0 | EVP_PKEY_up_ref(pk); |
3573 | 0 | prv->pkey = pk; |
3574 | | #ifdef DEBUG_PK |
3575 | | if (prv != NULL && prv->pkey != NULL) |
3576 | | sshkey_dump_ec_key(EVP_PKEY_get0_EC_KEY(prv->pkey)); |
3577 | | #endif |
3578 | 0 | #endif /* OPENSSL_HAS_ECC */ |
3579 | 0 | #ifdef OPENSSL_HAS_ED25519 |
3580 | 0 | } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_ED25519 && |
3581 | 0 | (type == KEY_UNSPEC || type == KEY_ED25519)) { |
3582 | 0 | size_t len; |
3583 | |
|
3584 | 0 | if ((prv = sshkey_new(KEY_UNSPEC)) == NULL || |
3585 | 0 | (prv->ed25519_sk = calloc(1, ED25519_SK_SZ)) == NULL || |
3586 | 0 | (prv->ed25519_pk = calloc(1, ED25519_PK_SZ)) == NULL) { |
3587 | 0 | r = SSH_ERR_ALLOC_FAIL; |
3588 | 0 | goto out; |
3589 | 0 | } |
3590 | 0 | prv->type = KEY_ED25519; |
3591 | 0 | len = ED25519_PK_SZ; |
3592 | 0 | if (!EVP_PKEY_get_raw_public_key(pk, prv->ed25519_pk, &len)) { |
3593 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3594 | 0 | goto out; |
3595 | 0 | } |
3596 | 0 | if (len != ED25519_PK_SZ) { |
3597 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3598 | 0 | goto out; |
3599 | 0 | } |
3600 | 0 | len = ED25519_SK_SZ - ED25519_PK_SZ; |
3601 | 0 | if (!EVP_PKEY_get_raw_private_key(pk, prv->ed25519_sk, &len)) { |
3602 | 0 | r = SSH_ERR_LIBCRYPTO_ERROR; |
3603 | 0 | goto out; |
3604 | 0 | } |
3605 | 0 | if (len != ED25519_SK_SZ - ED25519_PK_SZ) { |
3606 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3607 | 0 | goto out; |
3608 | 0 | } |
3609 | | /* Append the public key to our private key */ |
3610 | 0 | memcpy(prv->ed25519_sk + (ED25519_SK_SZ - ED25519_PK_SZ), |
3611 | 0 | prv->ed25519_pk, ED25519_PK_SZ); |
3612 | | #ifdef DEBUG_PK |
3613 | | sshbuf_dump_data(prv->ed25519_sk, ED25519_SK_SZ, stderr); |
3614 | | #endif |
3615 | 0 | #endif /* OPENSSL_HAS_ED25519 */ |
3616 | 0 | } else { |
3617 | 0 | r = SSH_ERR_INVALID_FORMAT; |
3618 | 0 | goto out; |
3619 | 0 | } |
3620 | 0 | r = 0; |
3621 | 0 | if (keyp != NULL) { |
3622 | 0 | *keyp = prv; |
3623 | 0 | prv = NULL; |
3624 | 0 | } |
3625 | 0 | out: |
3626 | 0 | BIO_free(bio); |
3627 | 0 | EVP_PKEY_free(pk); |
3628 | 0 | RSA_free(rsa); |
3629 | 0 | #ifdef OPENSSL_HAS_ECC |
3630 | 0 | EC_KEY_free(ecdsa); |
3631 | 0 | #endif |
3632 | 0 | sshkey_free(prv); |
3633 | 0 | return r; |
3634 | 0 | } |
3635 | | #endif /* WITH_OPENSSL */ |
3636 | | |
3637 | | int |
3638 | | sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type, |
3639 | | const char *passphrase, struct sshkey **keyp, char **commentp) |
3640 | 0 | { |
3641 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
3642 | |
|
3643 | 0 | if (keyp != NULL) |
3644 | 0 | *keyp = NULL; |
3645 | 0 | if (commentp != NULL) |
3646 | 0 | *commentp = NULL; |
3647 | |
|
3648 | 0 | switch (type) { |
3649 | 0 | case KEY_XMSS: |
3650 | | /* No fallback for new-format-only keys */ |
3651 | 0 | return sshkey_parse_private2(blob, type, passphrase, |
3652 | 0 | keyp, commentp); |
3653 | 0 | default: |
3654 | 0 | r = sshkey_parse_private2(blob, type, passphrase, keyp, |
3655 | 0 | commentp); |
3656 | | /* Only fallback to PEM parser if a format error occurred. */ |
3657 | 0 | if (r != SSH_ERR_INVALID_FORMAT) |
3658 | 0 | return r; |
3659 | 0 | #ifdef WITH_OPENSSL |
3660 | 0 | return sshkey_parse_private_pem_fileblob(blob, type, |
3661 | 0 | passphrase, keyp); |
3662 | | #else |
3663 | | return SSH_ERR_INVALID_FORMAT; |
3664 | | #endif /* WITH_OPENSSL */ |
3665 | 0 | } |
3666 | 0 | } |
3667 | | |
3668 | | int |
3669 | | sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase, |
3670 | | struct sshkey **keyp, char **commentp) |
3671 | 0 | { |
3672 | 0 | if (keyp != NULL) |
3673 | 0 | *keyp = NULL; |
3674 | 0 | if (commentp != NULL) |
3675 | 0 | *commentp = NULL; |
3676 | |
|
3677 | 0 | return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC, |
3678 | 0 | passphrase, keyp, commentp); |
3679 | 0 | } |
3680 | | |
3681 | | void |
3682 | | sshkey_sig_details_free(struct sshkey_sig_details *details) |
3683 | 0 | { |
3684 | 0 | freezero(details, sizeof(*details)); |
3685 | 0 | } |
3686 | | |
3687 | | int |
3688 | | sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type, |
3689 | | struct sshkey **pubkeyp) |
3690 | 0 | { |
3691 | 0 | int r = SSH_ERR_INTERNAL_ERROR; |
3692 | |
|
3693 | 0 | if (pubkeyp != NULL) |
3694 | 0 | *pubkeyp = NULL; |
3695 | | /* only new-format private keys bundle a public key inside */ |
3696 | 0 | if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0) |
3697 | 0 | return r; |
3698 | 0 | return 0; |
3699 | 0 | } |
3700 | | |
3701 | | #ifdef WITH_XMSS |
3702 | | /* |
3703 | | * serialize the key with the current state and forward the state |
3704 | | * maxsign times. |
3705 | | */ |
3706 | | int |
3707 | | sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b, |
3708 | | u_int32_t maxsign, int printerror) |
3709 | 0 | { |
3710 | 0 | int r, rupdate; |
3711 | |
|
3712 | 0 | if (maxsign == 0 || |
3713 | 0 | sshkey_type_plain(k->type) != KEY_XMSS) |
3714 | 0 | return sshkey_private_serialize_opt(k, b, |
3715 | 0 | SSHKEY_SERIALIZE_DEFAULT); |
3716 | 0 | if ((r = sshkey_xmss_get_state(k, printerror)) != 0 || |
3717 | 0 | (r = sshkey_private_serialize_opt(k, b, |
3718 | 0 | SSHKEY_SERIALIZE_STATE)) != 0 || |
3719 | 0 | (r = sshkey_xmss_forward_state(k, maxsign)) != 0) |
3720 | 0 | goto out; |
3721 | 0 | r = 0; |
3722 | 0 | out: |
3723 | 0 | if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) { |
3724 | 0 | if (r == 0) |
3725 | 0 | r = rupdate; |
3726 | 0 | } |
3727 | 0 | return r; |
3728 | 0 | } |
3729 | | |
3730 | | u_int32_t |
3731 | | sshkey_signatures_left(const struct sshkey *k) |
3732 | 0 | { |
3733 | 0 | if (sshkey_type_plain(k->type) == KEY_XMSS) |
3734 | 0 | return sshkey_xmss_signatures_left(k); |
3735 | 0 | return 0; |
3736 | 0 | } |
3737 | | |
3738 | | int |
3739 | | sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign) |
3740 | 0 | { |
3741 | 0 | if (sshkey_type_plain(k->type) != KEY_XMSS) |
3742 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
3743 | 0 | return sshkey_xmss_enable_maxsign(k, maxsign); |
3744 | 0 | } |
3745 | | |
3746 | | int |
3747 | | sshkey_set_filename(struct sshkey *k, const char *filename) |
3748 | 0 | { |
3749 | 0 | if (k == NULL) |
3750 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
3751 | 0 | if (sshkey_type_plain(k->type) != KEY_XMSS) |
3752 | 0 | return 0; |
3753 | 0 | if (filename == NULL) |
3754 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
3755 | 0 | if ((k->xmss_filename = strdup(filename)) == NULL) |
3756 | 0 | return SSH_ERR_ALLOC_FAIL; |
3757 | 0 | return 0; |
3758 | 0 | } |
3759 | | #else |
3760 | | int |
3761 | | sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b, |
3762 | | u_int32_t maxsign, int printerror) |
3763 | | { |
3764 | | return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT); |
3765 | | } |
3766 | | |
3767 | | u_int32_t |
3768 | | sshkey_signatures_left(const struct sshkey *k) |
3769 | | { |
3770 | | return 0; |
3771 | | } |
3772 | | |
3773 | | int |
3774 | | sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign) |
3775 | | { |
3776 | | return SSH_ERR_INVALID_ARGUMENT; |
3777 | | } |
3778 | | |
3779 | | int |
3780 | | sshkey_set_filename(struct sshkey *k, const char *filename) |
3781 | | { |
3782 | | if (k == NULL) |
3783 | | return SSH_ERR_INVALID_ARGUMENT; |
3784 | | return 0; |
3785 | | } |
3786 | | #endif /* WITH_XMSS */ |