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