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