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