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