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