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