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