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