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