blob: 06e11c6c6d7c827c6cc00af5ddc9c6f9da54181b [file] [log] [blame]
dtucker@openbsd.org7fadbb62017-03-10 03:48:57 +00001/* $OpenBSD: sshkey.c,v 1.44 2017/03/10 03:48:57 dtucker 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
dtucker@openbsd.orgecc35892017-02-17 02:31:14 +000092# ifdef WITH_SSH1
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000093 { NULL, "RSA1", KEY_RSA1, 0, 0, 0 },
dtucker@openbsd.orgecc35892017-02-17 02:31:14 +000094# endif
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +000095 { "ssh-rsa", "RSA", KEY_RSA, 0, 0, 0 },
96 { "rsa-sha2-256", "RSA", KEY_RSA, 0, 0, 1 },
97 { "rsa-sha2-512", "RSA", KEY_RSA, 0, 0, 1 },
98 { "ssh-dss", "DSA", KEY_DSA, 0, 0, 0 },
Damien Miller86687062014-07-02 15:28:02 +100099# ifdef OPENSSL_HAS_ECC
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000100 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 },
101 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000102# ifdef OPENSSL_HAS_NISTP521
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000103 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000104# endif /* OPENSSL_HAS_NISTP521 */
105# endif /* OPENSSL_HAS_ECC */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000106 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1, 0 },
107 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000108# ifdef OPENSSL_HAS_ECC
109 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000110 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000111 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000112 KEY_ECDSA_CERT, NID_secp384r1, 1, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000113# ifdef OPENSSL_HAS_NISTP521
114 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000115 KEY_ECDSA_CERT, NID_secp521r1, 1, 0 },
Damien Miller86687062014-07-02 15:28:02 +1000116# endif /* OPENSSL_HAS_NISTP521 */
117# endif /* OPENSSL_HAS_ECC */
Damien Miller86687062014-07-02 15:28:02 +1000118#endif /* WITH_OPENSSL */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000119 { NULL, NULL, -1, -1, 0, 0 }
Damien Miller86687062014-07-02 15:28:02 +1000120};
121
122const char *
123sshkey_type(const struct sshkey *k)
124{
125 const struct keytype *kt;
126
127 for (kt = keytypes; kt->type != -1; kt++) {
128 if (kt->type == k->type)
129 return kt->shortname;
130 }
131 return "unknown";
132}
133
134static const char *
135sshkey_ssh_name_from_type_nid(int type, int nid)
136{
137 const struct keytype *kt;
138
139 for (kt = keytypes; kt->type != -1; kt++) {
140 if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
141 return kt->name;
142 }
143 return "ssh-unknown";
144}
145
146int
147sshkey_type_is_cert(int type)
148{
149 const struct keytype *kt;
150
151 for (kt = keytypes; kt->type != -1; kt++) {
152 if (kt->type == type)
153 return kt->cert;
154 }
155 return 0;
156}
157
158const char *
159sshkey_ssh_name(const struct sshkey *k)
160{
161 return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
162}
163
164const char *
165sshkey_ssh_name_plain(const struct sshkey *k)
166{
167 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
168 k->ecdsa_nid);
169}
170
171int
172sshkey_type_from_name(const char *name)
173{
174 const struct keytype *kt;
175
176 for (kt = keytypes; kt->type != -1; kt++) {
177 /* Only allow shortname matches for plain key types */
178 if ((kt->name != NULL && strcmp(name, kt->name) == 0) ||
179 (!kt->cert && strcasecmp(kt->shortname, name) == 0))
180 return kt->type;
181 }
182 return KEY_UNSPEC;
183}
184
185int
186sshkey_ecdsa_nid_from_name(const char *name)
187{
188 const struct keytype *kt;
189
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +0000190 for (kt = keytypes; kt->type != -1; kt++) {
191 if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT)
192 continue;
193 if (kt->name != NULL && strcmp(name, kt->name) == 0)
194 return kt->nid;
195 }
Damien Miller86687062014-07-02 15:28:02 +1000196 return -1;
197}
198
199char *
djm@openbsd.org130f5df2016-09-12 23:31:27 +0000200sshkey_alg_list(int certs_only, int plain_only, char sep)
Damien Miller86687062014-07-02 15:28:02 +1000201{
202 char *tmp, *ret = NULL;
203 size_t nlen, rlen = 0;
204 const struct keytype *kt;
205
206 for (kt = keytypes; kt->type != -1; kt++) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000207 if (kt->name == NULL || kt->sigonly)
Damien Miller86687062014-07-02 15:28:02 +1000208 continue;
209 if ((certs_only && !kt->cert) || (plain_only && kt->cert))
210 continue;
211 if (ret != NULL)
djm@openbsd.org130f5df2016-09-12 23:31:27 +0000212 ret[rlen++] = sep;
Damien Miller86687062014-07-02 15:28:02 +1000213 nlen = strlen(kt->name);
214 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
215 free(ret);
216 return NULL;
217 }
218 ret = tmp;
219 memcpy(ret + rlen, kt->name, nlen + 1);
220 rlen += nlen;
221 }
222 return ret;
223}
224
225int
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000226sshkey_names_valid2(const char *names, int allow_wildcard)
Damien Miller86687062014-07-02 15:28:02 +1000227{
228 char *s, *cp, *p;
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000229 const struct keytype *kt;
230 int type;
Damien Miller86687062014-07-02 15:28:02 +1000231
232 if (names == NULL || strcmp(names, "") == 0)
233 return 0;
234 if ((s = cp = strdup(names)) == NULL)
235 return 0;
236 for ((p = strsep(&cp, ",")); p && *p != '\0';
237 (p = strsep(&cp, ","))) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000238 type = sshkey_type_from_name(p);
239 if (type == KEY_RSA1) {
240 free(s);
241 return 0;
242 }
243 if (type == KEY_UNSPEC) {
244 if (allow_wildcard) {
245 /*
246 * Try matching key types against the string.
247 * If any has a positive or negative match then
248 * the component is accepted.
249 */
250 for (kt = keytypes; kt->type != -1; kt++) {
251 if (kt->type == KEY_RSA1)
252 continue;
253 if (match_pattern_list(kt->name,
djm@openbsd.orge661a862015-05-04 06:10:48 +0000254 p, 0) != 0)
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000255 break;
256 }
257 if (kt->type != -1)
258 continue;
259 }
Damien Miller86687062014-07-02 15:28:02 +1000260 free(s);
261 return 0;
262 }
263 }
264 free(s);
265 return 1;
266}
267
268u_int
269sshkey_size(const struct sshkey *k)
270{
271 switch (k->type) {
272#ifdef WITH_OPENSSL
273 case KEY_RSA1:
274 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +1000275 case KEY_RSA_CERT:
276 return BN_num_bits(k->rsa->n);
277 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +1000278 case KEY_DSA_CERT:
279 return BN_num_bits(k->dsa->p);
280 case KEY_ECDSA:
281 case KEY_ECDSA_CERT:
282 return sshkey_curve_nid_to_bits(k->ecdsa_nid);
283#endif /* WITH_OPENSSL */
284 case KEY_ED25519:
285 case KEY_ED25519_CERT:
286 return 256; /* XXX */
287 }
288 return 0;
289}
290
Damien Miller86687062014-07-02 15:28:02 +1000291static int
292sshkey_type_is_valid_ca(int type)
293{
294 switch (type) {
295 case KEY_RSA:
296 case KEY_DSA:
297 case KEY_ECDSA:
298 case KEY_ED25519:
299 return 1;
300 default:
301 return 0;
302 }
303}
304
305int
306sshkey_is_cert(const struct sshkey *k)
307{
308 if (k == NULL)
309 return 0;
310 return sshkey_type_is_cert(k->type);
311}
312
313/* Return the cert-less equivalent to a certified key type */
314int
315sshkey_type_plain(int type)
316{
317 switch (type) {
Damien Miller86687062014-07-02 15:28:02 +1000318 case KEY_RSA_CERT:
319 return KEY_RSA;
Damien Miller86687062014-07-02 15:28:02 +1000320 case KEY_DSA_CERT:
321 return KEY_DSA;
322 case KEY_ECDSA_CERT:
323 return KEY_ECDSA;
324 case KEY_ED25519_CERT:
325 return KEY_ED25519;
326 default:
327 return type;
328 }
329}
330
331#ifdef WITH_OPENSSL
332/* XXX: these are really begging for a table-driven approach */
333int
334sshkey_curve_name_to_nid(const char *name)
335{
336 if (strcmp(name, "nistp256") == 0)
337 return NID_X9_62_prime256v1;
338 else if (strcmp(name, "nistp384") == 0)
339 return NID_secp384r1;
340# ifdef OPENSSL_HAS_NISTP521
341 else if (strcmp(name, "nistp521") == 0)
342 return NID_secp521r1;
343# endif /* OPENSSL_HAS_NISTP521 */
344 else
345 return -1;
346}
347
348u_int
349sshkey_curve_nid_to_bits(int nid)
350{
351 switch (nid) {
352 case NID_X9_62_prime256v1:
353 return 256;
354 case NID_secp384r1:
355 return 384;
356# ifdef OPENSSL_HAS_NISTP521
357 case NID_secp521r1:
358 return 521;
359# endif /* OPENSSL_HAS_NISTP521 */
360 default:
361 return 0;
362 }
363}
364
365int
366sshkey_ecdsa_bits_to_nid(int bits)
367{
368 switch (bits) {
369 case 256:
370 return NID_X9_62_prime256v1;
371 case 384:
372 return NID_secp384r1;
373# ifdef OPENSSL_HAS_NISTP521
374 case 521:
375 return NID_secp521r1;
376# endif /* OPENSSL_HAS_NISTP521 */
377 default:
378 return -1;
379 }
380}
381
382const char *
383sshkey_curve_nid_to_name(int nid)
384{
385 switch (nid) {
386 case NID_X9_62_prime256v1:
387 return "nistp256";
388 case NID_secp384r1:
389 return "nistp384";
390# ifdef OPENSSL_HAS_NISTP521
391 case NID_secp521r1:
392 return "nistp521";
393# endif /* OPENSSL_HAS_NISTP521 */
394 default:
395 return NULL;
396 }
397}
398
399int
400sshkey_ec_nid_to_hash_alg(int nid)
401{
402 int kbits = sshkey_curve_nid_to_bits(nid);
403
404 if (kbits <= 0)
405 return -1;
406
407 /* RFC5656 section 6.2.1 */
408 if (kbits <= 256)
409 return SSH_DIGEST_SHA256;
410 else if (kbits <= 384)
411 return SSH_DIGEST_SHA384;
412 else
413 return SSH_DIGEST_SHA512;
414}
415#endif /* WITH_OPENSSL */
416
417static void
418cert_free(struct sshkey_cert *cert)
419{
420 u_int i;
421
422 if (cert == NULL)
423 return;
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000424 sshbuf_free(cert->certblob);
425 sshbuf_free(cert->critical);
426 sshbuf_free(cert->extensions);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000427 free(cert->key_id);
Damien Miller86687062014-07-02 15:28:02 +1000428 for (i = 0; i < cert->nprincipals; i++)
429 free(cert->principals[i]);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +0000430 free(cert->principals);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +0000431 sshkey_free(cert->signature_key);
Damien Miller86687062014-07-02 15:28:02 +1000432 explicit_bzero(cert, sizeof(*cert));
433 free(cert);
434}
435
436static struct sshkey_cert *
437cert_new(void)
438{
439 struct sshkey_cert *cert;
440
441 if ((cert = calloc(1, sizeof(*cert))) == NULL)
442 return NULL;
443 if ((cert->certblob = sshbuf_new()) == NULL ||
444 (cert->critical = sshbuf_new()) == NULL ||
445 (cert->extensions = sshbuf_new()) == NULL) {
446 cert_free(cert);
447 return NULL;
448 }
449 cert->key_id = NULL;
450 cert->principals = NULL;
451 cert->signature_key = NULL;
452 return cert;
453}
454
455struct sshkey *
456sshkey_new(int type)
457{
458 struct sshkey *k;
459#ifdef WITH_OPENSSL
460 RSA *rsa;
461 DSA *dsa;
462#endif /* WITH_OPENSSL */
463
464 if ((k = calloc(1, sizeof(*k))) == NULL)
465 return NULL;
466 k->type = type;
467 k->ecdsa = NULL;
468 k->ecdsa_nid = -1;
469 k->dsa = NULL;
470 k->rsa = NULL;
471 k->cert = NULL;
472 k->ed25519_sk = NULL;
473 k->ed25519_pk = NULL;
474 switch (k->type) {
475#ifdef WITH_OPENSSL
476 case KEY_RSA1:
477 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +1000478 case KEY_RSA_CERT:
479 if ((rsa = RSA_new()) == NULL ||
480 (rsa->n = BN_new()) == NULL ||
481 (rsa->e = BN_new()) == NULL) {
482 if (rsa != NULL)
483 RSA_free(rsa);
484 free(k);
485 return NULL;
486 }
487 k->rsa = rsa;
488 break;
489 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +1000490 case KEY_DSA_CERT:
491 if ((dsa = DSA_new()) == NULL ||
492 (dsa->p = BN_new()) == NULL ||
493 (dsa->q = BN_new()) == NULL ||
494 (dsa->g = BN_new()) == NULL ||
495 (dsa->pub_key = BN_new()) == NULL) {
496 if (dsa != NULL)
497 DSA_free(dsa);
498 free(k);
499 return NULL;
500 }
501 k->dsa = dsa;
502 break;
503 case KEY_ECDSA:
504 case KEY_ECDSA_CERT:
505 /* Cannot do anything until we know the group */
506 break;
507#endif /* WITH_OPENSSL */
508 case KEY_ED25519:
509 case KEY_ED25519_CERT:
510 /* no need to prealloc */
511 break;
512 case KEY_UNSPEC:
513 break;
514 default:
515 free(k);
516 return NULL;
Damien Miller86687062014-07-02 15:28:02 +1000517 }
518
519 if (sshkey_is_cert(k)) {
520 if ((k->cert = cert_new()) == NULL) {
521 sshkey_free(k);
522 return NULL;
523 }
524 }
525
526 return k;
527}
528
529int
530sshkey_add_private(struct sshkey *k)
531{
532 switch (k->type) {
533#ifdef WITH_OPENSSL
534 case KEY_RSA1:
535 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +1000536 case KEY_RSA_CERT:
537#define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
538 if (bn_maybe_alloc_failed(k->rsa->d) ||
539 bn_maybe_alloc_failed(k->rsa->iqmp) ||
540 bn_maybe_alloc_failed(k->rsa->q) ||
541 bn_maybe_alloc_failed(k->rsa->p) ||
542 bn_maybe_alloc_failed(k->rsa->dmq1) ||
543 bn_maybe_alloc_failed(k->rsa->dmp1))
544 return SSH_ERR_ALLOC_FAIL;
545 break;
546 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +1000547 case KEY_DSA_CERT:
548 if (bn_maybe_alloc_failed(k->dsa->priv_key))
549 return SSH_ERR_ALLOC_FAIL;
550 break;
551#undef bn_maybe_alloc_failed
552 case KEY_ECDSA:
553 case KEY_ECDSA_CERT:
554 /* Cannot do anything until we know the group */
555 break;
556#endif /* WITH_OPENSSL */
557 case KEY_ED25519:
558 case KEY_ED25519_CERT:
559 /* no need to prealloc */
560 break;
561 case KEY_UNSPEC:
562 break;
563 default:
564 return SSH_ERR_INVALID_ARGUMENT;
565 }
566 return 0;
567}
568
569struct sshkey *
570sshkey_new_private(int type)
571{
572 struct sshkey *k = sshkey_new(type);
573
574 if (k == NULL)
575 return NULL;
576 if (sshkey_add_private(k) != 0) {
577 sshkey_free(k);
578 return NULL;
579 }
580 return k;
581}
582
583void
584sshkey_free(struct sshkey *k)
585{
586 if (k == NULL)
587 return;
588 switch (k->type) {
589#ifdef WITH_OPENSSL
590 case KEY_RSA1:
591 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +1000592 case KEY_RSA_CERT:
593 if (k->rsa != NULL)
594 RSA_free(k->rsa);
595 k->rsa = NULL;
596 break;
597 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +1000598 case KEY_DSA_CERT:
599 if (k->dsa != NULL)
600 DSA_free(k->dsa);
601 k->dsa = NULL;
602 break;
603# ifdef OPENSSL_HAS_ECC
604 case KEY_ECDSA:
605 case KEY_ECDSA_CERT:
606 if (k->ecdsa != NULL)
607 EC_KEY_free(k->ecdsa);
608 k->ecdsa = NULL;
609 break;
610# endif /* OPENSSL_HAS_ECC */
611#endif /* WITH_OPENSSL */
612 case KEY_ED25519:
613 case KEY_ED25519_CERT:
614 if (k->ed25519_pk) {
615 explicit_bzero(k->ed25519_pk, ED25519_PK_SZ);
616 free(k->ed25519_pk);
617 k->ed25519_pk = NULL;
618 }
619 if (k->ed25519_sk) {
620 explicit_bzero(k->ed25519_sk, ED25519_SK_SZ);
621 free(k->ed25519_sk);
622 k->ed25519_sk = NULL;
623 }
624 break;
625 case KEY_UNSPEC:
626 break;
627 default:
628 break;
629 }
630 if (sshkey_is_cert(k))
631 cert_free(k->cert);
632 explicit_bzero(k, sizeof(*k));
633 free(k);
634}
635
636static int
637cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
638{
639 if (a == NULL && b == NULL)
640 return 1;
641 if (a == NULL || b == NULL)
642 return 0;
643 if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
644 return 0;
645 if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
646 sshbuf_len(a->certblob)) != 0)
647 return 0;
648 return 1;
649}
650
651/*
652 * Compare public portions of key only, allowing comparisons between
653 * certificates and plain keys too.
654 */
655int
656sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
657{
Darren Tucker948a1772014-07-22 01:07:11 +1000658#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
Damien Miller86687062014-07-02 15:28:02 +1000659 BN_CTX *bnctx;
Darren Tucker948a1772014-07-22 01:07:11 +1000660#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
Damien Miller86687062014-07-02 15:28:02 +1000661
662 if (a == NULL || b == NULL ||
663 sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
664 return 0;
665
666 switch (a->type) {
667#ifdef WITH_OPENSSL
668 case KEY_RSA1:
Damien Miller86687062014-07-02 15:28:02 +1000669 case KEY_RSA_CERT:
670 case KEY_RSA:
671 return a->rsa != NULL && b->rsa != NULL &&
672 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
673 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller86687062014-07-02 15:28:02 +1000674 case KEY_DSA_CERT:
675 case KEY_DSA:
676 return a->dsa != NULL && b->dsa != NULL &&
677 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
678 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
679 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
680 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
681# ifdef OPENSSL_HAS_ECC
682 case KEY_ECDSA_CERT:
683 case KEY_ECDSA:
684 if (a->ecdsa == NULL || b->ecdsa == NULL ||
685 EC_KEY_get0_public_key(a->ecdsa) == NULL ||
686 EC_KEY_get0_public_key(b->ecdsa) == NULL)
687 return 0;
688 if ((bnctx = BN_CTX_new()) == NULL)
689 return 0;
690 if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
691 EC_KEY_get0_group(b->ecdsa), bnctx) != 0 ||
692 EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
693 EC_KEY_get0_public_key(a->ecdsa),
694 EC_KEY_get0_public_key(b->ecdsa), bnctx) != 0) {
695 BN_CTX_free(bnctx);
696 return 0;
697 }
698 BN_CTX_free(bnctx);
699 return 1;
700# endif /* OPENSSL_HAS_ECC */
701#endif /* WITH_OPENSSL */
702 case KEY_ED25519:
703 case KEY_ED25519_CERT:
704 return a->ed25519_pk != NULL && b->ed25519_pk != NULL &&
705 memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0;
706 default:
707 return 0;
708 }
709 /* NOTREACHED */
710}
711
712int
713sshkey_equal(const struct sshkey *a, const struct sshkey *b)
714{
715 if (a == NULL || b == NULL || a->type != b->type)
716 return 0;
717 if (sshkey_is_cert(a)) {
718 if (!cert_compare(a->cert, b->cert))
719 return 0;
720 }
721 return sshkey_equal_public(a, b);
722}
723
724static int
725to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain)
726{
727 int type, ret = SSH_ERR_INTERNAL_ERROR;
728 const char *typename;
729
730 if (key == NULL)
731 return SSH_ERR_INVALID_ARGUMENT;
732
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +0000733 if (sshkey_is_cert(key)) {
734 if (key->cert == NULL)
735 return SSH_ERR_EXPECTED_CERT;
736 if (sshbuf_len(key->cert->certblob) == 0)
737 return SSH_ERR_KEY_LACKS_CERTBLOB;
738 }
Damien Miller86687062014-07-02 15:28:02 +1000739 type = force_plain ? sshkey_type_plain(key->type) : key->type;
740 typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
741
742 switch (type) {
743#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +1000744 case KEY_DSA_CERT:
745 case KEY_ECDSA_CERT:
746 case KEY_RSA_CERT:
747#endif /* WITH_OPENSSL */
748 case KEY_ED25519_CERT:
749 /* Use the existing blob */
750 /* XXX modified flag? */
751 if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
752 return ret;
753 break;
754#ifdef WITH_OPENSSL
755 case KEY_DSA:
756 if (key->dsa == NULL)
757 return SSH_ERR_INVALID_ARGUMENT;
758 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
759 (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
760 (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
761 (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
762 (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
763 return ret;
764 break;
Darren Tuckerd1a04212014-07-19 07:23:55 +1000765# ifdef OPENSSL_HAS_ECC
Damien Miller86687062014-07-02 15:28:02 +1000766 case KEY_ECDSA:
767 if (key->ecdsa == NULL)
768 return SSH_ERR_INVALID_ARGUMENT;
769 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
770 (ret = sshbuf_put_cstring(b,
771 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
772 (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0)
773 return ret;
774 break;
Darren Tuckerd1a04212014-07-19 07:23:55 +1000775# endif
Damien Miller86687062014-07-02 15:28:02 +1000776 case KEY_RSA:
777 if (key->rsa == NULL)
778 return SSH_ERR_INVALID_ARGUMENT;
779 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
780 (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
781 (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
782 return ret;
783 break;
784#endif /* WITH_OPENSSL */
785 case KEY_ED25519:
786 if (key->ed25519_pk == NULL)
787 return SSH_ERR_INVALID_ARGUMENT;
788 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
789 (ret = sshbuf_put_string(b,
790 key->ed25519_pk, ED25519_PK_SZ)) != 0)
791 return ret;
792 break;
793 default:
794 return SSH_ERR_KEY_TYPE_UNKNOWN;
795 }
796 return 0;
797}
798
799int
djm@openbsd.org60b18252015-01-26 02:59:11 +0000800sshkey_putb(const struct sshkey *key, struct sshbuf *b)
Damien Miller86687062014-07-02 15:28:02 +1000801{
802 return to_blob_buf(key, b, 0);
803}
804
805int
djm@openbsd.org60b18252015-01-26 02:59:11 +0000806sshkey_puts(const struct sshkey *key, struct sshbuf *b)
807{
808 struct sshbuf *tmp;
809 int r;
810
811 if ((tmp = sshbuf_new()) == NULL)
812 return SSH_ERR_ALLOC_FAIL;
813 r = to_blob_buf(key, tmp, 0);
814 if (r == 0)
815 r = sshbuf_put_stringb(b, tmp);
816 sshbuf_free(tmp);
817 return r;
818}
819
820int
821sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
Damien Miller86687062014-07-02 15:28:02 +1000822{
823 return to_blob_buf(key, b, 1);
824}
825
826static int
827to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain)
828{
829 int ret = SSH_ERR_INTERNAL_ERROR;
830 size_t len;
831 struct sshbuf *b = NULL;
832
833 if (lenp != NULL)
834 *lenp = 0;
835 if (blobp != NULL)
836 *blobp = NULL;
837 if ((b = sshbuf_new()) == NULL)
838 return SSH_ERR_ALLOC_FAIL;
839 if ((ret = to_blob_buf(key, b, force_plain)) != 0)
840 goto out;
841 len = sshbuf_len(b);
842 if (lenp != NULL)
843 *lenp = len;
844 if (blobp != NULL) {
845 if ((*blobp = malloc(len)) == NULL) {
846 ret = SSH_ERR_ALLOC_FAIL;
847 goto out;
848 }
849 memcpy(*blobp, sshbuf_ptr(b), len);
850 }
851 ret = 0;
852 out:
853 sshbuf_free(b);
854 return ret;
855}
856
857int
858sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
859{
860 return to_blob(key, blobp, lenp, 0);
861}
862
863int
864sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
865{
866 return to_blob(key, blobp, lenp, 1);
867}
868
869int
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000870sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
Damien Miller86687062014-07-02 15:28:02 +1000871 u_char **retp, size_t *lenp)
872{
873 u_char *blob = NULL, *ret = NULL;
874 size_t blob_len = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000875 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +1000876
877 if (retp != NULL)
878 *retp = NULL;
879 if (lenp != NULL)
880 *lenp = 0;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000881 if (ssh_digest_bytes(dgst_alg) == 0) {
Damien Miller86687062014-07-02 15:28:02 +1000882 r = SSH_ERR_INVALID_ARGUMENT;
883 goto out;
884 }
885
886 if (k->type == KEY_RSA1) {
887#ifdef WITH_OPENSSL
888 int nlen = BN_num_bytes(k->rsa->n);
889 int elen = BN_num_bytes(k->rsa->e);
890
djm@openbsd.org27c3a9c2016-09-26 21:16:11 +0000891 if (nlen < 0 || elen < 0 || nlen >= INT_MAX - elen) {
892 r = SSH_ERR_INVALID_FORMAT;
893 goto out;
894 }
Damien Miller86687062014-07-02 15:28:02 +1000895 blob_len = nlen + elen;
djm@openbsd.org27c3a9c2016-09-26 21:16:11 +0000896 if ((blob = malloc(blob_len)) == NULL) {
Damien Miller86687062014-07-02 15:28:02 +1000897 r = SSH_ERR_ALLOC_FAIL;
898 goto out;
899 }
900 BN_bn2bin(k->rsa->n, blob);
901 BN_bn2bin(k->rsa->e, blob + nlen);
902#endif /* WITH_OPENSSL */
903 } else if ((r = to_blob(k, &blob, &blob_len, 1)) != 0)
904 goto out;
905 if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
906 r = SSH_ERR_ALLOC_FAIL;
907 goto out;
908 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000909 if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
Damien Miller86687062014-07-02 15:28:02 +1000910 ret, SSH_DIGEST_MAX_LENGTH)) != 0)
911 goto out;
912 /* success */
913 if (retp != NULL) {
914 *retp = ret;
915 ret = NULL;
916 }
917 if (lenp != NULL)
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000918 *lenp = ssh_digest_bytes(dgst_alg);
Damien Miller86687062014-07-02 15:28:02 +1000919 r = 0;
920 out:
921 free(ret);
922 if (blob != NULL) {
923 explicit_bzero(blob, blob_len);
924 free(blob);
925 }
926 return r;
927}
928
929static char *
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000930fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
Damien Miller86687062014-07-02 15:28:02 +1000931{
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000932 char *ret;
933 size_t plen = strlen(alg) + 1;
934 size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
935 int r;
Damien Miller86687062014-07-02 15:28:02 +1000936
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000937 if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000938 return NULL;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000939 strlcpy(ret, alg, rlen);
940 strlcat(ret, ":", rlen);
941 if (dgst_raw_len == 0)
942 return ret;
943 if ((r = b64_ntop(dgst_raw, dgst_raw_len,
944 ret + plen, rlen - plen)) == -1) {
945 explicit_bzero(ret, rlen);
946 free(ret);
947 return NULL;
Damien Miller86687062014-07-02 15:28:02 +1000948 }
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000949 /* Trim padding characters from end */
950 ret[strcspn(ret, "=")] = '\0';
951 return ret;
952}
Damien Miller86687062014-07-02 15:28:02 +1000953
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000954static char *
955fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
956{
957 char *retval, hex[5];
958 size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
959
960 if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
961 return NULL;
962 strlcpy(retval, alg, rlen);
963 strlcat(retval, ":", rlen);
964 for (i = 0; i < dgst_raw_len; i++) {
965 snprintf(hex, sizeof(hex), "%s%02x",
966 i > 0 ? ":" : "", dgst_raw[i]);
967 strlcat(retval, hex, rlen);
968 }
Damien Miller86687062014-07-02 15:28:02 +1000969 return retval;
970}
971
972static char *
973fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
974{
975 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
976 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
977 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
978 u_int i, j = 0, rounds, seed = 1;
979 char *retval;
980
981 rounds = (dgst_raw_len / 2) + 1;
982 if ((retval = calloc(rounds, 6)) == NULL)
983 return NULL;
984 retval[j++] = 'x';
985 for (i = 0; i < rounds; i++) {
986 u_int idx0, idx1, idx2, idx3, idx4;
987 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
988 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
989 seed) % 6;
990 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
991 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
992 (seed / 6)) % 6;
993 retval[j++] = vowels[idx0];
994 retval[j++] = consonants[idx1];
995 retval[j++] = vowels[idx2];
996 if ((i + 1) < rounds) {
997 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
998 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
999 retval[j++] = consonants[idx3];
1000 retval[j++] = '-';
1001 retval[j++] = consonants[idx4];
1002 seed = ((seed * 5) +
1003 ((((u_int)(dgst_raw[2 * i])) * 7) +
1004 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1005 }
1006 } else {
1007 idx0 = seed % 6;
1008 idx1 = 16;
1009 idx2 = seed / 6;
1010 retval[j++] = vowels[idx0];
1011 retval[j++] = consonants[idx1];
1012 retval[j++] = vowels[idx2];
1013 }
1014 }
1015 retval[j++] = 'x';
1016 retval[j++] = '\0';
1017 return retval;
1018}
1019
1020/*
1021 * Draw an ASCII-Art representing the fingerprint so human brain can
1022 * profit from its built-in pattern recognition ability.
1023 * This technique is called "random art" and can be found in some
1024 * scientific publications like this original paper:
1025 *
1026 * "Hash Visualization: a New Technique to improve Real-World Security",
1027 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
1028 * Techniques and E-Commerce (CrypTEC '99)
1029 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
1030 *
1031 * The subject came up in a talk by Dan Kaminsky, too.
1032 *
1033 * If you see the picture is different, the key is different.
1034 * If the picture looks the same, you still know nothing.
1035 *
1036 * The algorithm used here is a worm crawling over a discrete plane,
1037 * leaving a trace (augmenting the field) everywhere it goes.
1038 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
1039 * makes the respective movement vector be ignored for this turn.
1040 * Graphs are not unambiguous, because circles in graphs can be
1041 * walked in either direction.
1042 */
1043
1044/*
1045 * Field sizes for the random art. Have to be odd, so the starting point
1046 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1047 * Else pictures would be too dense, and drawing the frame would
1048 * fail, too, because the key type would not fit in anymore.
1049 */
1050#define FLDBASE 8
1051#define FLDSIZE_Y (FLDBASE + 1)
1052#define FLDSIZE_X (FLDBASE * 2 + 1)
1053static char *
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001054fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
Damien Miller86687062014-07-02 15:28:02 +10001055 const struct sshkey *k)
1056{
1057 /*
1058 * Chars to be used after each other every time the worm
1059 * intersects with itself. Matter of taste.
1060 */
1061 char *augmentation_string = " .o+=*BOX@%&#/^SE";
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001062 char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
Damien Miller86687062014-07-02 15:28:02 +10001063 u_char field[FLDSIZE_X][FLDSIZE_Y];
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001064 size_t i, tlen, hlen;
Damien Miller86687062014-07-02 15:28:02 +10001065 u_int b;
Damien Miller61e28e52014-07-03 21:22:22 +10001066 int x, y, r;
Damien Miller86687062014-07-02 15:28:02 +10001067 size_t len = strlen(augmentation_string) - 1;
1068
1069 if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1070 return NULL;
1071
1072 /* initialize field */
1073 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1074 x = FLDSIZE_X / 2;
1075 y = FLDSIZE_Y / 2;
1076
1077 /* process raw key */
1078 for (i = 0; i < dgst_raw_len; i++) {
1079 int input;
1080 /* each byte conveys four 2-bit move commands */
1081 input = dgst_raw[i];
1082 for (b = 0; b < 4; b++) {
1083 /* evaluate 2 bit, rest is shifted later */
1084 x += (input & 0x1) ? 1 : -1;
1085 y += (input & 0x2) ? 1 : -1;
1086
1087 /* assure we are still in bounds */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001088 x = MAXIMUM(x, 0);
1089 y = MAXIMUM(y, 0);
1090 x = MINIMUM(x, FLDSIZE_X - 1);
1091 y = MINIMUM(y, FLDSIZE_Y - 1);
Damien Miller86687062014-07-02 15:28:02 +10001092
1093 /* augment the field */
1094 if (field[x][y] < len - 2)
1095 field[x][y]++;
1096 input = input >> 2;
1097 }
1098 }
1099
1100 /* mark starting point and end point*/
1101 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1102 field[x][y] = len;
1103
Damien Miller61e28e52014-07-03 21:22:22 +10001104 /* assemble title */
1105 r = snprintf(title, sizeof(title), "[%s %u]",
1106 sshkey_type(k), sshkey_size(k));
1107 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1108 if (r < 0 || r > (int)sizeof(title))
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001109 r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1110 tlen = (r <= 0) ? 0 : strlen(title);
1111
1112 /* assemble hash ID. */
1113 r = snprintf(hash, sizeof(hash), "[%s]", alg);
1114 hlen = (r <= 0) ? 0 : strlen(hash);
Damien Miller86687062014-07-02 15:28:02 +10001115
1116 /* output upper border */
Damien Miller61e28e52014-07-03 21:22:22 +10001117 p = retval;
1118 *p++ = '+';
1119 for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1120 *p++ = '-';
1121 memcpy(p, title, tlen);
1122 p += tlen;
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001123 for (i += tlen; i < FLDSIZE_X; i++)
Damien Miller86687062014-07-02 15:28:02 +10001124 *p++ = '-';
1125 *p++ = '+';
1126 *p++ = '\n';
1127
1128 /* output content */
1129 for (y = 0; y < FLDSIZE_Y; y++) {
1130 *p++ = '|';
1131 for (x = 0; x < FLDSIZE_X; x++)
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001132 *p++ = augmentation_string[MINIMUM(field[x][y], len)];
Damien Miller86687062014-07-02 15:28:02 +10001133 *p++ = '|';
1134 *p++ = '\n';
1135 }
1136
1137 /* output lower border */
1138 *p++ = '+';
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001139 for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1140 *p++ = '-';
1141 memcpy(p, hash, hlen);
1142 p += hlen;
1143 for (i += hlen; i < FLDSIZE_X; i++)
Damien Miller86687062014-07-02 15:28:02 +10001144 *p++ = '-';
1145 *p++ = '+';
1146
1147 return retval;
1148}
1149
1150char *
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001151sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
Damien Miller86687062014-07-02 15:28:02 +10001152 enum sshkey_fp_rep dgst_rep)
1153{
1154 char *retval = NULL;
1155 u_char *dgst_raw;
1156 size_t dgst_raw_len;
1157
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001158 if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
Damien Miller86687062014-07-02 15:28:02 +10001159 return NULL;
1160 switch (dgst_rep) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001161 case SSH_FP_DEFAULT:
1162 if (dgst_alg == SSH_DIGEST_MD5) {
1163 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1164 dgst_raw, dgst_raw_len);
1165 } else {
1166 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1167 dgst_raw, dgst_raw_len);
1168 }
1169 break;
Damien Miller86687062014-07-02 15:28:02 +10001170 case SSH_FP_HEX:
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001171 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1172 dgst_raw, dgst_raw_len);
1173 break;
1174 case SSH_FP_BASE64:
1175 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1176 dgst_raw, dgst_raw_len);
Damien Miller86687062014-07-02 15:28:02 +10001177 break;
1178 case SSH_FP_BUBBLEBABBLE:
1179 retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1180 break;
1181 case SSH_FP_RANDOMART:
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001182 retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1183 dgst_raw, dgst_raw_len, k);
Damien Miller86687062014-07-02 15:28:02 +10001184 break;
1185 default:
1186 explicit_bzero(dgst_raw, dgst_raw_len);
1187 free(dgst_raw);
1188 return NULL;
1189 }
1190 explicit_bzero(dgst_raw, dgst_raw_len);
1191 free(dgst_raw);
1192 return retval;
1193}
1194
1195#ifdef WITH_SSH1
1196/*
1197 * Reads a multiple-precision integer in decimal from the buffer, and advances
1198 * the pointer. The integer must already be initialized. This function is
1199 * permitted to modify the buffer. This leaves *cpp to point just beyond the
1200 * last processed character.
1201 */
1202static int
1203read_decimal_bignum(char **cpp, BIGNUM *v)
1204{
1205 char *cp;
1206 size_t e;
1207 int skip = 1; /* skip white space */
1208
1209 cp = *cpp;
1210 while (*cp == ' ' || *cp == '\t')
1211 cp++;
1212 e = strspn(cp, "0123456789");
1213 if (e == 0)
1214 return SSH_ERR_INVALID_FORMAT;
1215 if (e > SSHBUF_MAX_BIGNUM * 3)
1216 return SSH_ERR_BIGNUM_TOO_LARGE;
1217 if (cp[e] == '\0')
1218 skip = 0;
millert@openbsd.org259adb62015-11-16 23:47:52 +00001219 else if (strchr(" \t\r\n", cp[e]) == NULL)
Damien Miller86687062014-07-02 15:28:02 +10001220 return SSH_ERR_INVALID_FORMAT;
1221 cp[e] = '\0';
1222 if (BN_dec2bn(&v, cp) <= 0)
1223 return SSH_ERR_INVALID_FORMAT;
1224 *cpp = cp + e + skip;
1225 return 0;
1226}
1227#endif /* WITH_SSH1 */
1228
1229/* returns 0 ok, and < 0 error */
1230int
1231sshkey_read(struct sshkey *ret, char **cpp)
1232{
1233 struct sshkey *k;
1234 int retval = SSH_ERR_INVALID_FORMAT;
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001235 char *ep, *cp, *space;
Damien Miller86687062014-07-02 15:28:02 +10001236 int r, type, curve_nid = -1;
1237 struct sshbuf *blob;
1238#ifdef WITH_SSH1
Damien Miller86687062014-07-02 15:28:02 +10001239 u_long bits;
1240#endif /* WITH_SSH1 */
1241
dtucker@openbsd.org7fadbb62017-03-10 03:48:57 +00001242 if (ret == NULL)
1243 return SSH_ERR_INVALID_ARGUMENT;
1244
Damien Miller86687062014-07-02 15:28:02 +10001245 cp = *cpp;
1246
1247 switch (ret->type) {
1248 case KEY_RSA1:
1249#ifdef WITH_SSH1
1250 /* Get number of bits. */
1251 bits = strtoul(cp, &ep, 10);
millert@openbsd.org259adb62015-11-16 23:47:52 +00001252 if (*cp == '\0' || strchr(" \t\r\n", *ep) == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001253 bits == 0 || bits > SSHBUF_MAX_BIGNUM * 8)
1254 return SSH_ERR_INVALID_FORMAT; /* Bad bit count... */
1255 /* Get public exponent, public modulus. */
1256 if ((r = read_decimal_bignum(&ep, ret->rsa->e)) < 0)
1257 return r;
1258 if ((r = read_decimal_bignum(&ep, ret->rsa->n)) < 0)
1259 return r;
Damien Miller86687062014-07-02 15:28:02 +10001260 /* validate the claimed number of bits */
1261 if (BN_num_bits(ret->rsa->n) != (int)bits)
1262 return SSH_ERR_KEY_BITS_MISMATCH;
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001263 *cpp = ep;
Damien Miller86687062014-07-02 15:28:02 +10001264 retval = 0;
1265#endif /* WITH_SSH1 */
1266 break;
1267 case KEY_UNSPEC:
1268 case KEY_RSA:
1269 case KEY_DSA:
1270 case KEY_ECDSA:
1271 case KEY_ED25519:
Damien Miller86687062014-07-02 15:28:02 +10001272 case KEY_DSA_CERT:
1273 case KEY_ECDSA_CERT:
1274 case KEY_RSA_CERT:
1275 case KEY_ED25519_CERT:
1276 space = strchr(cp, ' ');
1277 if (space == NULL)
1278 return SSH_ERR_INVALID_FORMAT;
1279 *space = '\0';
1280 type = sshkey_type_from_name(cp);
1281 if (sshkey_type_plain(type) == KEY_ECDSA &&
1282 (curve_nid = sshkey_ecdsa_nid_from_name(cp)) == -1)
1283 return SSH_ERR_EC_CURVE_INVALID;
1284 *space = ' ';
1285 if (type == KEY_UNSPEC)
1286 return SSH_ERR_INVALID_FORMAT;
1287 cp = space+1;
1288 if (*cp == '\0')
1289 return SSH_ERR_INVALID_FORMAT;
djm@openbsd.orgd2d51002014-11-18 01:02:25 +00001290 if (ret->type != KEY_UNSPEC && ret->type != type)
Damien Miller86687062014-07-02 15:28:02 +10001291 return SSH_ERR_KEY_TYPE_MISMATCH;
1292 if ((blob = sshbuf_new()) == NULL)
1293 return SSH_ERR_ALLOC_FAIL;
1294 /* trim comment */
1295 space = strchr(cp, ' ');
markus@openbsd.org816d1532015-01-12 20:13:27 +00001296 if (space) {
1297 /* advance 'space': skip whitespace */
1298 *space++ = '\0';
1299 while (*space == ' ' || *space == '\t')
1300 space++;
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001301 ep = space;
markus@openbsd.org816d1532015-01-12 20:13:27 +00001302 } else
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001303 ep = cp + strlen(cp);
Damien Miller86687062014-07-02 15:28:02 +10001304 if ((r = sshbuf_b64tod(blob, cp)) != 0) {
1305 sshbuf_free(blob);
1306 return r;
1307 }
1308 if ((r = sshkey_from_blob(sshbuf_ptr(blob),
1309 sshbuf_len(blob), &k)) != 0) {
1310 sshbuf_free(blob);
1311 return r;
1312 }
1313 sshbuf_free(blob);
1314 if (k->type != type) {
1315 sshkey_free(k);
1316 return SSH_ERR_KEY_TYPE_MISMATCH;
1317 }
1318 if (sshkey_type_plain(type) == KEY_ECDSA &&
1319 curve_nid != k->ecdsa_nid) {
1320 sshkey_free(k);
1321 return SSH_ERR_EC_CURVE_MISMATCH;
1322 }
djm@openbsd.orgd2d51002014-11-18 01:02:25 +00001323 ret->type = type;
Damien Miller86687062014-07-02 15:28:02 +10001324 if (sshkey_is_cert(ret)) {
1325 if (!sshkey_is_cert(k)) {
1326 sshkey_free(k);
1327 return SSH_ERR_EXPECTED_CERT;
1328 }
1329 if (ret->cert != NULL)
1330 cert_free(ret->cert);
1331 ret->cert = k->cert;
1332 k->cert = NULL;
1333 }
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001334 switch (sshkey_type_plain(ret->type)) {
Damien Miller86687062014-07-02 15:28:02 +10001335#ifdef WITH_OPENSSL
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001336 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +10001337 if (ret->rsa != NULL)
1338 RSA_free(ret->rsa);
1339 ret->rsa = k->rsa;
1340 k->rsa = NULL;
1341#ifdef DEBUG_PK
1342 RSA_print_fp(stderr, ret->rsa, 8);
1343#endif
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001344 break;
1345 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +10001346 if (ret->dsa != NULL)
1347 DSA_free(ret->dsa);
1348 ret->dsa = k->dsa;
1349 k->dsa = NULL;
1350#ifdef DEBUG_PK
1351 DSA_print_fp(stderr, ret->dsa, 8);
1352#endif
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001353 break;
Damien Miller86687062014-07-02 15:28:02 +10001354# ifdef OPENSSL_HAS_ECC
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001355 case KEY_ECDSA:
Damien Miller86687062014-07-02 15:28:02 +10001356 if (ret->ecdsa != NULL)
1357 EC_KEY_free(ret->ecdsa);
1358 ret->ecdsa = k->ecdsa;
1359 ret->ecdsa_nid = k->ecdsa_nid;
1360 k->ecdsa = NULL;
1361 k->ecdsa_nid = -1;
1362#ifdef DEBUG_PK
1363 sshkey_dump_ec_key(ret->ecdsa);
1364#endif
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001365 break;
Damien Miller86687062014-07-02 15:28:02 +10001366# endif /* OPENSSL_HAS_ECC */
1367#endif /* WITH_OPENSSL */
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001368 case KEY_ED25519:
Damien Miller86687062014-07-02 15:28:02 +10001369 free(ret->ed25519_pk);
1370 ret->ed25519_pk = k->ed25519_pk;
1371 k->ed25519_pk = NULL;
1372#ifdef DEBUG_PK
1373 /* XXX */
1374#endif
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001375 break;
Damien Miller86687062014-07-02 15:28:02 +10001376 }
djm@openbsd.org3a9f84b2015-11-16 22:50:01 +00001377 *cpp = ep;
Damien Miller86687062014-07-02 15:28:02 +10001378 retval = 0;
1379/*XXXX*/
1380 sshkey_free(k);
1381 if (retval != 0)
1382 break;
Damien Miller86687062014-07-02 15:28:02 +10001383 break;
1384 default:
1385 return SSH_ERR_INVALID_ARGUMENT;
1386 }
1387 return retval;
1388}
1389
1390int
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001391sshkey_to_base64(const struct sshkey *key, char **b64p)
Damien Miller86687062014-07-02 15:28:02 +10001392{
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001393 int r = SSH_ERR_INTERNAL_ERROR;
1394 struct sshbuf *b = NULL;
Damien Miller86687062014-07-02 15:28:02 +10001395 char *uu = NULL;
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001396
1397 if (b64p != NULL)
1398 *b64p = NULL;
1399 if ((b = sshbuf_new()) == NULL)
1400 return SSH_ERR_ALLOC_FAIL;
1401 if ((r = sshkey_putb(key, b)) != 0)
1402 goto out;
1403 if ((uu = sshbuf_dtob64(b)) == NULL) {
1404 r = SSH_ERR_ALLOC_FAIL;
1405 goto out;
1406 }
1407 /* Success */
1408 if (b64p != NULL) {
1409 *b64p = uu;
1410 uu = NULL;
1411 }
1412 r = 0;
1413 out:
1414 sshbuf_free(b);
1415 free(uu);
1416 return r;
1417}
1418
1419static int
1420sshkey_format_rsa1(const struct sshkey *key, struct sshbuf *b)
1421{
1422 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +10001423#ifdef WITH_SSH1
1424 u_int bits = 0;
1425 char *dec_e = NULL, *dec_n = NULL;
Damien Miller86687062014-07-02 15:28:02 +10001426
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001427 if (key->rsa == NULL || key->rsa->e == NULL ||
1428 key->rsa->n == NULL) {
1429 r = SSH_ERR_INVALID_ARGUMENT;
Damien Miller86687062014-07-02 15:28:02 +10001430 goto out;
1431 }
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001432 if ((dec_e = BN_bn2dec(key->rsa->e)) == NULL ||
1433 (dec_n = BN_bn2dec(key->rsa->n)) == NULL) {
1434 r = SSH_ERR_ALLOC_FAIL;
Damien Miller86687062014-07-02 15:28:02 +10001435 goto out;
1436 }
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001437 /* size of modulus 'n' */
1438 if ((bits = BN_num_bits(key->rsa->n)) <= 0) {
1439 r = SSH_ERR_INVALID_ARGUMENT;
1440 goto out;
1441 }
1442 if ((r = sshbuf_putf(b, "%u %s %s", bits, dec_e, dec_n)) != 0)
1443 goto out;
1444
1445 /* Success */
1446 r = 0;
Damien Miller86687062014-07-02 15:28:02 +10001447 out:
Damien Miller86687062014-07-02 15:28:02 +10001448 if (dec_e != NULL)
1449 OPENSSL_free(dec_e);
1450 if (dec_n != NULL)
1451 OPENSSL_free(dec_n);
1452#endif /* WITH_SSH1 */
djm@openbsd.orgd80fbe42015-05-21 04:55:51 +00001453
1454 return r;
1455}
1456
1457static int
1458sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1459{
1460 int r = SSH_ERR_INTERNAL_ERROR;
1461 char *uu = NULL;
1462
1463 if (key->type == KEY_RSA1) {
1464 if ((r = sshkey_format_rsa1(key, b)) != 0)
1465 goto out;
1466 } else {
1467 /* Unsupported key types handled in sshkey_to_base64() */
1468 if ((r = sshkey_to_base64(key, &uu)) != 0)
1469 goto out;
1470 if ((r = sshbuf_putf(b, "%s %s",
1471 sshkey_ssh_name(key), uu)) != 0)
1472 goto out;
1473 }
1474 r = 0;
1475 out:
1476 free(uu);
1477 return r;
1478}
1479
1480int
1481sshkey_write(const struct sshkey *key, FILE *f)
1482{
1483 struct sshbuf *b = NULL;
1484 int r = SSH_ERR_INTERNAL_ERROR;
1485
1486 if ((b = sshbuf_new()) == NULL)
1487 return SSH_ERR_ALLOC_FAIL;
1488 if ((r = sshkey_format_text(key, b)) != 0)
1489 goto out;
1490 if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1491 if (feof(f))
1492 errno = EPIPE;
1493 r = SSH_ERR_SYSTEM_ERROR;
1494 goto out;
1495 }
1496 /* Success */
1497 r = 0;
1498 out:
1499 sshbuf_free(b);
1500 return r;
Damien Miller86687062014-07-02 15:28:02 +10001501}
1502
1503const char *
1504sshkey_cert_type(const struct sshkey *k)
1505{
1506 switch (k->cert->type) {
1507 case SSH2_CERT_TYPE_USER:
1508 return "user";
1509 case SSH2_CERT_TYPE_HOST:
1510 return "host";
1511 default:
1512 return "unknown";
1513 }
1514}
1515
1516#ifdef WITH_OPENSSL
1517static int
1518rsa_generate_private_key(u_int bits, RSA **rsap)
1519{
1520 RSA *private = NULL;
1521 BIGNUM *f4 = NULL;
1522 int ret = SSH_ERR_INTERNAL_ERROR;
1523
1524 if (rsap == NULL ||
1525 bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1526 bits > SSHBUF_MAX_BIGNUM * 8)
1527 return SSH_ERR_INVALID_ARGUMENT;
1528 *rsap = NULL;
1529 if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
1530 ret = SSH_ERR_ALLOC_FAIL;
1531 goto out;
1532 }
1533 if (!BN_set_word(f4, RSA_F4) ||
1534 !RSA_generate_key_ex(private, bits, f4, NULL)) {
1535 ret = SSH_ERR_LIBCRYPTO_ERROR;
1536 goto out;
1537 }
1538 *rsap = private;
1539 private = NULL;
1540 ret = 0;
1541 out:
1542 if (private != NULL)
1543 RSA_free(private);
1544 if (f4 != NULL)
1545 BN_free(f4);
1546 return ret;
1547}
1548
1549static int
1550dsa_generate_private_key(u_int bits, DSA **dsap)
1551{
1552 DSA *private;
1553 int ret = SSH_ERR_INTERNAL_ERROR;
1554
1555 if (dsap == NULL || bits != 1024)
1556 return SSH_ERR_INVALID_ARGUMENT;
1557 if ((private = DSA_new()) == NULL) {
1558 ret = SSH_ERR_ALLOC_FAIL;
1559 goto out;
1560 }
1561 *dsap = NULL;
1562 if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
1563 NULL, NULL) || !DSA_generate_key(private)) {
Damien Miller86687062014-07-02 15:28:02 +10001564 ret = SSH_ERR_LIBCRYPTO_ERROR;
1565 goto out;
1566 }
1567 *dsap = private;
1568 private = NULL;
1569 ret = 0;
1570 out:
1571 if (private != NULL)
1572 DSA_free(private);
1573 return ret;
1574}
1575
1576# ifdef OPENSSL_HAS_ECC
1577int
1578sshkey_ecdsa_key_to_nid(EC_KEY *k)
1579{
1580 EC_GROUP *eg;
1581 int nids[] = {
1582 NID_X9_62_prime256v1,
1583 NID_secp384r1,
1584# ifdef OPENSSL_HAS_NISTP521
1585 NID_secp521r1,
1586# endif /* OPENSSL_HAS_NISTP521 */
1587 -1
1588 };
1589 int nid;
1590 u_int i;
1591 BN_CTX *bnctx;
1592 const EC_GROUP *g = EC_KEY_get0_group(k);
1593
1594 /*
1595 * The group may be stored in a ASN.1 encoded private key in one of two
1596 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1597 * or explicit group parameters encoded into the key blob. Only the
1598 * "named group" case sets the group NID for us, but we can figure
1599 * it out for the other case by comparing against all the groups that
1600 * are supported.
1601 */
1602 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1603 return nid;
1604 if ((bnctx = BN_CTX_new()) == NULL)
1605 return -1;
1606 for (i = 0; nids[i] != -1; i++) {
1607 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) {
1608 BN_CTX_free(bnctx);
1609 return -1;
1610 }
1611 if (EC_GROUP_cmp(g, eg, bnctx) == 0)
1612 break;
1613 EC_GROUP_free(eg);
1614 }
1615 BN_CTX_free(bnctx);
1616 if (nids[i] != -1) {
1617 /* Use the group with the NID attached */
1618 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1619 if (EC_KEY_set_group(k, eg) != 1) {
1620 EC_GROUP_free(eg);
1621 return -1;
1622 }
1623 }
1624 return nids[i];
1625}
1626
1627static int
1628ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap)
1629{
1630 EC_KEY *private;
1631 int ret = SSH_ERR_INTERNAL_ERROR;
1632
1633 if (nid == NULL || ecdsap == NULL ||
1634 (*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
1635 return SSH_ERR_INVALID_ARGUMENT;
1636 *ecdsap = NULL;
1637 if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) {
1638 ret = SSH_ERR_ALLOC_FAIL;
1639 goto out;
1640 }
1641 if (EC_KEY_generate_key(private) != 1) {
1642 ret = SSH_ERR_LIBCRYPTO_ERROR;
1643 goto out;
1644 }
1645 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
1646 *ecdsap = private;
1647 private = NULL;
1648 ret = 0;
1649 out:
1650 if (private != NULL)
1651 EC_KEY_free(private);
1652 return ret;
1653}
1654# endif /* OPENSSL_HAS_ECC */
1655#endif /* WITH_OPENSSL */
1656
1657int
1658sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1659{
1660 struct sshkey *k;
1661 int ret = SSH_ERR_INTERNAL_ERROR;
1662
1663 if (keyp == NULL)
1664 return SSH_ERR_INVALID_ARGUMENT;
1665 *keyp = NULL;
1666 if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1667 return SSH_ERR_ALLOC_FAIL;
1668 switch (type) {
1669 case KEY_ED25519:
1670 if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
1671 (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) {
1672 ret = SSH_ERR_ALLOC_FAIL;
1673 break;
1674 }
1675 crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
1676 ret = 0;
1677 break;
1678#ifdef WITH_OPENSSL
1679 case KEY_DSA:
1680 ret = dsa_generate_private_key(bits, &k->dsa);
1681 break;
1682# ifdef OPENSSL_HAS_ECC
1683 case KEY_ECDSA:
1684 ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid,
1685 &k->ecdsa);
1686 break;
1687# endif /* OPENSSL_HAS_ECC */
1688 case KEY_RSA:
1689 case KEY_RSA1:
1690 ret = rsa_generate_private_key(bits, &k->rsa);
1691 break;
1692#endif /* WITH_OPENSSL */
1693 default:
1694 ret = SSH_ERR_INVALID_ARGUMENT;
1695 }
1696 if (ret == 0) {
1697 k->type = type;
1698 *keyp = k;
1699 } else
1700 sshkey_free(k);
1701 return ret;
1702}
1703
1704int
1705sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1706{
1707 u_int i;
1708 const struct sshkey_cert *from;
1709 struct sshkey_cert *to;
1710 int ret = SSH_ERR_INTERNAL_ERROR;
1711
1712 if (to_key->cert != NULL) {
1713 cert_free(to_key->cert);
1714 to_key->cert = NULL;
1715 }
1716
1717 if ((from = from_key->cert) == NULL)
1718 return SSH_ERR_INVALID_ARGUMENT;
1719
1720 if ((to = to_key->cert = cert_new()) == NULL)
1721 return SSH_ERR_ALLOC_FAIL;
1722
1723 if ((ret = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
1724 (ret = sshbuf_putb(to->critical, from->critical)) != 0 ||
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +00001725 (ret = sshbuf_putb(to->extensions, from->extensions)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10001726 return ret;
1727
1728 to->serial = from->serial;
1729 to->type = from->type;
1730 if (from->key_id == NULL)
1731 to->key_id = NULL;
1732 else if ((to->key_id = strdup(from->key_id)) == NULL)
1733 return SSH_ERR_ALLOC_FAIL;
1734 to->valid_after = from->valid_after;
1735 to->valid_before = from->valid_before;
1736 if (from->signature_key == NULL)
1737 to->signature_key = NULL;
1738 else if ((ret = sshkey_from_private(from->signature_key,
1739 &to->signature_key)) != 0)
1740 return ret;
1741
1742 if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS)
1743 return SSH_ERR_INVALID_ARGUMENT;
1744 if (from->nprincipals > 0) {
1745 if ((to->principals = calloc(from->nprincipals,
1746 sizeof(*to->principals))) == NULL)
1747 return SSH_ERR_ALLOC_FAIL;
1748 for (i = 0; i < from->nprincipals; i++) {
1749 to->principals[i] = strdup(from->principals[i]);
1750 if (to->principals[i] == NULL) {
1751 to->nprincipals = i;
1752 return SSH_ERR_ALLOC_FAIL;
1753 }
1754 }
1755 }
1756 to->nprincipals = from->nprincipals;
1757 return 0;
1758}
1759
1760int
1761sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1762{
1763 struct sshkey *n = NULL;
1764 int ret = SSH_ERR_INTERNAL_ERROR;
1765
djm@openbsd.org1a2663a2015-10-15 23:08:23 +00001766 *pkp = NULL;
Damien Miller86687062014-07-02 15:28:02 +10001767 switch (k->type) {
1768#ifdef WITH_OPENSSL
1769 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +10001770 case KEY_DSA_CERT:
1771 if ((n = sshkey_new(k->type)) == NULL)
1772 return SSH_ERR_ALLOC_FAIL;
1773 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
1774 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
1775 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
1776 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
1777 sshkey_free(n);
1778 return SSH_ERR_ALLOC_FAIL;
1779 }
1780 break;
1781# ifdef OPENSSL_HAS_ECC
1782 case KEY_ECDSA:
1783 case KEY_ECDSA_CERT:
1784 if ((n = sshkey_new(k->type)) == NULL)
1785 return SSH_ERR_ALLOC_FAIL;
1786 n->ecdsa_nid = k->ecdsa_nid;
1787 n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
1788 if (n->ecdsa == NULL) {
1789 sshkey_free(n);
1790 return SSH_ERR_ALLOC_FAIL;
1791 }
1792 if (EC_KEY_set_public_key(n->ecdsa,
1793 EC_KEY_get0_public_key(k->ecdsa)) != 1) {
1794 sshkey_free(n);
1795 return SSH_ERR_LIBCRYPTO_ERROR;
1796 }
1797 break;
1798# endif /* OPENSSL_HAS_ECC */
1799 case KEY_RSA:
1800 case KEY_RSA1:
Damien Miller86687062014-07-02 15:28:02 +10001801 case KEY_RSA_CERT:
1802 if ((n = sshkey_new(k->type)) == NULL)
1803 return SSH_ERR_ALLOC_FAIL;
1804 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
1805 (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
1806 sshkey_free(n);
1807 return SSH_ERR_ALLOC_FAIL;
1808 }
1809 break;
1810#endif /* WITH_OPENSSL */
1811 case KEY_ED25519:
1812 case KEY_ED25519_CERT:
1813 if ((n = sshkey_new(k->type)) == NULL)
1814 return SSH_ERR_ALLOC_FAIL;
1815 if (k->ed25519_pk != NULL) {
1816 if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
1817 sshkey_free(n);
1818 return SSH_ERR_ALLOC_FAIL;
1819 }
1820 memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
1821 }
1822 break;
1823 default:
1824 return SSH_ERR_KEY_TYPE_UNKNOWN;
1825 }
1826 if (sshkey_is_cert(k)) {
1827 if ((ret = sshkey_cert_copy(k, n)) != 0) {
1828 sshkey_free(n);
1829 return ret;
1830 }
1831 }
1832 *pkp = n;
1833 return 0;
1834}
1835
1836static int
djm@openbsd.org60b18252015-01-26 02:59:11 +00001837cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
Damien Miller86687062014-07-02 15:28:02 +10001838{
djm@openbsd.org60b18252015-01-26 02:59:11 +00001839 struct sshbuf *principals = NULL, *crit = NULL;
1840 struct sshbuf *exts = NULL, *ca = NULL;
1841 u_char *sig = NULL;
1842 size_t signed_len = 0, slen = 0, kidlen = 0;
Damien Miller86687062014-07-02 15:28:02 +10001843 int ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +10001844
1845 /* Copy the entire key blob for verification and later serialisation */
djm@openbsd.org60b18252015-01-26 02:59:11 +00001846 if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10001847 return ret;
1848
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001849 /* Parse body of certificate up to signature */
1850 if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10001851 (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
1852 (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001853 (ret = sshbuf_froms(b, &principals)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10001854 (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
1855 (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001856 (ret = sshbuf_froms(b, &crit)) != 0 ||
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001857 (ret = sshbuf_froms(b, &exts)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10001858 (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
djm@openbsd.org60b18252015-01-26 02:59:11 +00001859 (ret = sshbuf_froms(b, &ca)) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10001860 /* XXX debug print error for ret */
1861 ret = SSH_ERR_INVALID_FORMAT;
1862 goto out;
1863 }
1864
1865 /* Signature is left in the buffer so we can calculate this length */
1866 signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
1867
1868 if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
1869 ret = SSH_ERR_INVALID_FORMAT;
1870 goto out;
1871 }
1872
1873 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1874 key->cert->type != SSH2_CERT_TYPE_HOST) {
1875 ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
1876 goto out;
1877 }
1878
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001879 /* Parse principals section */
1880 while (sshbuf_len(principals) > 0) {
1881 char *principal = NULL;
1882 char **oprincipals = NULL;
1883
Damien Miller86687062014-07-02 15:28:02 +10001884 if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
1885 ret = SSH_ERR_INVALID_FORMAT;
1886 goto out;
1887 }
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001888 if ((ret = sshbuf_get_cstring(principals, &principal,
1889 NULL)) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10001890 ret = SSH_ERR_INVALID_FORMAT;
1891 goto out;
1892 }
1893 oprincipals = key->cert->principals;
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001894 key->cert->principals = reallocarray(key->cert->principals,
1895 key->cert->nprincipals + 1, sizeof(*key->cert->principals));
Damien Miller86687062014-07-02 15:28:02 +10001896 if (key->cert->principals == NULL) {
1897 free(principal);
1898 key->cert->principals = oprincipals;
1899 ret = SSH_ERR_ALLOC_FAIL;
1900 goto out;
1901 }
1902 key->cert->principals[key->cert->nprincipals++] = principal;
1903 }
1904
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001905 /*
1906 * Stash a copies of the critical options and extensions sections
1907 * for later use.
1908 */
1909 if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
1910 (exts != NULL &&
1911 (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
Damien Miller86687062014-07-02 15:28:02 +10001912 goto out;
1913
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001914 /*
1915 * Validate critical options and extensions sections format.
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001916 */
1917 while (sshbuf_len(crit) != 0) {
1918 if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
1919 (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
1920 sshbuf_reset(key->cert->critical);
Damien Miller86687062014-07-02 15:28:02 +10001921 ret = SSH_ERR_INVALID_FORMAT;
1922 goto out;
1923 }
1924 }
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001925 while (exts != NULL && sshbuf_len(exts) != 0) {
1926 if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
1927 (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
1928 sshbuf_reset(key->cert->extensions);
Damien Miller86687062014-07-02 15:28:02 +10001929 ret = SSH_ERR_INVALID_FORMAT;
1930 goto out;
1931 }
1932 }
Damien Miller86687062014-07-02 15:28:02 +10001933
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001934 /* Parse CA key and check signature */
djm@openbsd.org60b18252015-01-26 02:59:11 +00001935 if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10001936 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1937 goto out;
1938 }
1939 if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
1940 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1941 goto out;
1942 }
Damien Miller86687062014-07-02 15:28:02 +10001943 if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
1944 sshbuf_ptr(key->cert->certblob), signed_len, 0)) != 0)
1945 goto out;
Damien Miller86687062014-07-02 15:28:02 +10001946
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001947 /* Success */
1948 ret = 0;
Damien Miller86687062014-07-02 15:28:02 +10001949 out:
djm@openbsd.org60b18252015-01-26 02:59:11 +00001950 sshbuf_free(ca);
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00001951 sshbuf_free(crit);
1952 sshbuf_free(exts);
1953 sshbuf_free(principals);
Damien Miller86687062014-07-02 15:28:02 +10001954 free(sig);
1955 return ret;
1956}
1957
1958static int
djm@openbsd.org60b18252015-01-26 02:59:11 +00001959sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
1960 int allow_cert)
Damien Miller86687062014-07-02 15:28:02 +10001961{
djm@openbsd.org54924b52015-01-14 10:46:28 +00001962 int type, ret = SSH_ERR_INTERNAL_ERROR;
Damien Miller86687062014-07-02 15:28:02 +10001963 char *ktype = NULL, *curve = NULL;
1964 struct sshkey *key = NULL;
1965 size_t len;
1966 u_char *pk = NULL;
djm@openbsd.org60b18252015-01-26 02:59:11 +00001967 struct sshbuf *copy;
Damien Miller86687062014-07-02 15:28:02 +10001968#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
1969 EC_POINT *q = NULL;
1970#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
1971
1972#ifdef DEBUG_PK /* XXX */
djm@openbsd.org60b18252015-01-26 02:59:11 +00001973 sshbuf_dump(b, stderr);
Damien Miller86687062014-07-02 15:28:02 +10001974#endif
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00001975 if (keyp != NULL)
1976 *keyp = NULL;
djm@openbsd.org60b18252015-01-26 02:59:11 +00001977 if ((copy = sshbuf_fromb(b)) == NULL) {
1978 ret = SSH_ERR_ALLOC_FAIL;
1979 goto out;
1980 }
Damien Miller86687062014-07-02 15:28:02 +10001981 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
1982 ret = SSH_ERR_INVALID_FORMAT;
1983 goto out;
1984 }
1985
1986 type = sshkey_type_from_name(ktype);
Damien Miller86687062014-07-02 15:28:02 +10001987 if (!allow_cert && sshkey_type_is_cert(type)) {
1988 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1989 goto out;
1990 }
1991 switch (type) {
1992#ifdef WITH_OPENSSL
1993 case KEY_RSA_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00001994 /* Skip nonce */
Damien Miller86687062014-07-02 15:28:02 +10001995 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
1996 ret = SSH_ERR_INVALID_FORMAT;
1997 goto out;
1998 }
1999 /* FALLTHROUGH */
2000 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +10002001 if ((key = sshkey_new(type)) == NULL) {
2002 ret = SSH_ERR_ALLOC_FAIL;
2003 goto out;
2004 }
djm@openbsd.org3f4ea3c2015-04-03 22:17:27 +00002005 if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
2006 sshbuf_get_bignum2(b, key->rsa->n) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10002007 ret = SSH_ERR_INVALID_FORMAT;
2008 goto out;
2009 }
2010#ifdef DEBUG_PK
2011 RSA_print_fp(stderr, key->rsa, 8);
2012#endif
2013 break;
2014 case KEY_DSA_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002015 /* Skip nonce */
Damien Miller86687062014-07-02 15:28:02 +10002016 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2017 ret = SSH_ERR_INVALID_FORMAT;
2018 goto out;
2019 }
2020 /* FALLTHROUGH */
2021 case KEY_DSA:
Damien Miller86687062014-07-02 15:28:02 +10002022 if ((key = sshkey_new(type)) == NULL) {
2023 ret = SSH_ERR_ALLOC_FAIL;
2024 goto out;
2025 }
djm@openbsd.org3f4ea3c2015-04-03 22:17:27 +00002026 if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
2027 sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
2028 sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
2029 sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10002030 ret = SSH_ERR_INVALID_FORMAT;
2031 goto out;
2032 }
2033#ifdef DEBUG_PK
2034 DSA_print_fp(stderr, key->dsa, 8);
2035#endif
2036 break;
2037 case KEY_ECDSA_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002038 /* Skip nonce */
Damien Miller86687062014-07-02 15:28:02 +10002039 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2040 ret = SSH_ERR_INVALID_FORMAT;
2041 goto out;
2042 }
2043 /* FALLTHROUGH */
2044# ifdef OPENSSL_HAS_ECC
2045 case KEY_ECDSA:
2046 if ((key = sshkey_new(type)) == NULL) {
2047 ret = SSH_ERR_ALLOC_FAIL;
2048 goto out;
2049 }
djm@openbsd.org54924b52015-01-14 10:46:28 +00002050 key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype);
Damien Miller86687062014-07-02 15:28:02 +10002051 if (sshbuf_get_cstring(b, &curve, NULL) != 0) {
2052 ret = SSH_ERR_INVALID_FORMAT;
2053 goto out;
2054 }
2055 if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
2056 ret = SSH_ERR_EC_CURVE_MISMATCH;
2057 goto out;
2058 }
2059 if (key->ecdsa != NULL)
2060 EC_KEY_free(key->ecdsa);
2061 if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
2062 == NULL) {
2063 ret = SSH_ERR_EC_CURVE_INVALID;
2064 goto out;
2065 }
2066 if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) {
2067 ret = SSH_ERR_ALLOC_FAIL;
2068 goto out;
2069 }
2070 if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) {
2071 ret = SSH_ERR_INVALID_FORMAT;
2072 goto out;
2073 }
2074 if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
2075 q) != 0) {
2076 ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2077 goto out;
2078 }
2079 if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
2080 /* XXX assume it is a allocation error */
2081 ret = SSH_ERR_ALLOC_FAIL;
2082 goto out;
2083 }
2084#ifdef DEBUG_PK
2085 sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
2086#endif
2087 break;
2088# endif /* OPENSSL_HAS_ECC */
2089#endif /* WITH_OPENSSL */
2090 case KEY_ED25519_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002091 /* Skip nonce */
Damien Miller86687062014-07-02 15:28:02 +10002092 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2093 ret = SSH_ERR_INVALID_FORMAT;
2094 goto out;
2095 }
2096 /* FALLTHROUGH */
2097 case KEY_ED25519:
2098 if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
2099 goto out;
2100 if (len != ED25519_PK_SZ) {
2101 ret = SSH_ERR_INVALID_FORMAT;
2102 goto out;
2103 }
2104 if ((key = sshkey_new(type)) == NULL) {
2105 ret = SSH_ERR_ALLOC_FAIL;
2106 goto out;
2107 }
2108 key->ed25519_pk = pk;
2109 pk = NULL;
2110 break;
2111 case KEY_UNSPEC:
2112 if ((key = sshkey_new(type)) == NULL) {
2113 ret = SSH_ERR_ALLOC_FAIL;
2114 goto out;
2115 }
2116 break;
2117 default:
2118 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
2119 goto out;
2120 }
2121
2122 /* Parse certificate potion */
djm@openbsd.org60b18252015-01-26 02:59:11 +00002123 if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10002124 goto out;
2125
2126 if (key != NULL && sshbuf_len(b) != 0) {
2127 ret = SSH_ERR_INVALID_FORMAT;
2128 goto out;
2129 }
2130 ret = 0;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00002131 if (keyp != NULL) {
2132 *keyp = key;
2133 key = NULL;
2134 }
Damien Miller86687062014-07-02 15:28:02 +10002135 out:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002136 sshbuf_free(copy);
Damien Miller86687062014-07-02 15:28:02 +10002137 sshkey_free(key);
2138 free(ktype);
2139 free(curve);
2140 free(pk);
2141#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2142 if (q != NULL)
2143 EC_POINT_free(q);
2144#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2145 return ret;
2146}
2147
2148int
2149sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
2150{
djm@openbsd.org60b18252015-01-26 02:59:11 +00002151 struct sshbuf *b;
2152 int r;
2153
2154 if ((b = sshbuf_from(blob, blen)) == NULL)
2155 return SSH_ERR_ALLOC_FAIL;
2156 r = sshkey_from_blob_internal(b, keyp, 1);
2157 sshbuf_free(b);
2158 return r;
2159}
2160
2161int
2162sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
2163{
2164 return sshkey_from_blob_internal(b, keyp, 1);
2165}
2166
2167int
2168sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
2169{
2170 struct sshbuf *b;
2171 int r;
2172
2173 if ((r = sshbuf_froms(buf, &b)) != 0)
2174 return r;
2175 r = sshkey_from_blob_internal(b, keyp, 1);
2176 sshbuf_free(b);
2177 return r;
Damien Miller86687062014-07-02 15:28:02 +10002178}
2179
2180int
2181sshkey_sign(const struct sshkey *key,
2182 u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00002183 const u_char *data, size_t datalen, const char *alg, u_int compat)
Damien Miller86687062014-07-02 15:28:02 +10002184{
2185 if (sigp != NULL)
2186 *sigp = NULL;
2187 if (lenp != NULL)
2188 *lenp = 0;
2189 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2190 return SSH_ERR_INVALID_ARGUMENT;
2191 switch (key->type) {
2192#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10002193 case KEY_DSA_CERT:
2194 case KEY_DSA:
2195 return ssh_dss_sign(key, sigp, lenp, data, datalen, compat);
2196# ifdef OPENSSL_HAS_ECC
2197 case KEY_ECDSA_CERT:
2198 case KEY_ECDSA:
2199 return ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat);
2200# endif /* OPENSSL_HAS_ECC */
Damien Miller86687062014-07-02 15:28:02 +10002201 case KEY_RSA_CERT:
2202 case KEY_RSA:
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00002203 return ssh_rsa_sign(key, sigp, lenp, data, datalen, alg);
Damien Miller86687062014-07-02 15:28:02 +10002204#endif /* WITH_OPENSSL */
2205 case KEY_ED25519:
2206 case KEY_ED25519_CERT:
2207 return ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat);
2208 default:
2209 return SSH_ERR_KEY_TYPE_UNKNOWN;
2210 }
2211}
2212
2213/*
2214 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2215 */
2216int
2217sshkey_verify(const struct sshkey *key,
2218 const u_char *sig, size_t siglen,
2219 const u_char *data, size_t dlen, u_int compat)
2220{
djm@openbsd.org4cf87f42014-12-10 01:24:09 +00002221 if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
Damien Miller86687062014-07-02 15:28:02 +10002222 return SSH_ERR_INVALID_ARGUMENT;
2223 switch (key->type) {
2224#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10002225 case KEY_DSA_CERT:
2226 case KEY_DSA:
2227 return ssh_dss_verify(key, sig, siglen, data, dlen, compat);
2228# ifdef OPENSSL_HAS_ECC
2229 case KEY_ECDSA_CERT:
2230 case KEY_ECDSA:
2231 return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat);
2232# endif /* OPENSSL_HAS_ECC */
Damien Miller86687062014-07-02 15:28:02 +10002233 case KEY_RSA_CERT:
2234 case KEY_RSA:
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00002235 return ssh_rsa_verify(key, sig, siglen, data, dlen);
Damien Miller86687062014-07-02 15:28:02 +10002236#endif /* WITH_OPENSSL */
2237 case KEY_ED25519:
2238 case KEY_ED25519_CERT:
2239 return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
2240 default:
2241 return SSH_ERR_KEY_TYPE_UNKNOWN;
2242 }
2243}
2244
2245/* Converts a private to a public key */
2246int
2247sshkey_demote(const struct sshkey *k, struct sshkey **dkp)
2248{
2249 struct sshkey *pk;
2250 int ret = SSH_ERR_INTERNAL_ERROR;
2251
djm@openbsd.org1a2663a2015-10-15 23:08:23 +00002252 *dkp = NULL;
Damien Miller86687062014-07-02 15:28:02 +10002253 if ((pk = calloc(1, sizeof(*pk))) == NULL)
2254 return SSH_ERR_ALLOC_FAIL;
2255 pk->type = k->type;
2256 pk->flags = k->flags;
2257 pk->ecdsa_nid = k->ecdsa_nid;
2258 pk->dsa = NULL;
2259 pk->ecdsa = NULL;
2260 pk->rsa = NULL;
2261 pk->ed25519_pk = NULL;
2262 pk->ed25519_sk = NULL;
2263
2264 switch (k->type) {
2265#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10002266 case KEY_RSA_CERT:
2267 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2268 goto fail;
2269 /* FALLTHROUGH */
2270 case KEY_RSA1:
2271 case KEY_RSA:
2272 if ((pk->rsa = RSA_new()) == NULL ||
2273 (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
2274 (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
2275 ret = SSH_ERR_ALLOC_FAIL;
2276 goto fail;
2277 }
2278 break;
Damien Miller86687062014-07-02 15:28:02 +10002279 case KEY_DSA_CERT:
2280 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2281 goto fail;
2282 /* FALLTHROUGH */
2283 case KEY_DSA:
2284 if ((pk->dsa = DSA_new()) == NULL ||
2285 (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
2286 (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
2287 (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
2288 (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
2289 ret = SSH_ERR_ALLOC_FAIL;
2290 goto fail;
2291 }
2292 break;
2293 case KEY_ECDSA_CERT:
2294 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2295 goto fail;
2296 /* FALLTHROUGH */
2297# ifdef OPENSSL_HAS_ECC
2298 case KEY_ECDSA:
2299 pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid);
2300 if (pk->ecdsa == NULL) {
2301 ret = SSH_ERR_ALLOC_FAIL;
2302 goto fail;
2303 }
2304 if (EC_KEY_set_public_key(pk->ecdsa,
2305 EC_KEY_get0_public_key(k->ecdsa)) != 1) {
2306 ret = SSH_ERR_LIBCRYPTO_ERROR;
2307 goto fail;
2308 }
2309 break;
2310# endif /* OPENSSL_HAS_ECC */
2311#endif /* WITH_OPENSSL */
2312 case KEY_ED25519_CERT:
2313 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2314 goto fail;
2315 /* FALLTHROUGH */
2316 case KEY_ED25519:
2317 if (k->ed25519_pk != NULL) {
2318 if ((pk->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
2319 ret = SSH_ERR_ALLOC_FAIL;
2320 goto fail;
2321 }
2322 memcpy(pk->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
2323 }
2324 break;
2325 default:
2326 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
2327 fail:
2328 sshkey_free(pk);
2329 return ret;
2330 }
2331 *dkp = pk;
2332 return 0;
2333}
2334
2335/* Convert a plain key to their _CERT equivalent */
2336int
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002337sshkey_to_certified(struct sshkey *k)
Damien Miller86687062014-07-02 15:28:02 +10002338{
2339 int newtype;
2340
2341 switch (k->type) {
2342#ifdef WITH_OPENSSL
2343 case KEY_RSA:
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002344 newtype = KEY_RSA_CERT;
Damien Miller86687062014-07-02 15:28:02 +10002345 break;
2346 case KEY_DSA:
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002347 newtype = KEY_DSA_CERT;
Damien Miller86687062014-07-02 15:28:02 +10002348 break;
2349 case KEY_ECDSA:
Damien Miller86687062014-07-02 15:28:02 +10002350 newtype = KEY_ECDSA_CERT;
2351 break;
2352#endif /* WITH_OPENSSL */
2353 case KEY_ED25519:
Damien Miller86687062014-07-02 15:28:02 +10002354 newtype = KEY_ED25519_CERT;
2355 break;
2356 default:
2357 return SSH_ERR_INVALID_ARGUMENT;
2358 }
2359 if ((k->cert = cert_new()) == NULL)
2360 return SSH_ERR_ALLOC_FAIL;
2361 k->type = newtype;
2362 return 0;
2363}
2364
2365/* Convert a certificate to its raw key equivalent */
2366int
2367sshkey_drop_cert(struct sshkey *k)
2368{
2369 if (!sshkey_type_is_cert(k->type))
2370 return SSH_ERR_KEY_TYPE_UNKNOWN;
2371 cert_free(k->cert);
2372 k->cert = NULL;
2373 k->type = sshkey_type_plain(k->type);
2374 return 0;
2375}
2376
2377/* Sign a certified key, (re-)generating the signed certblob. */
2378int
djm@openbsd.org57464e32016-05-02 09:36:42 +00002379sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg)
Damien Miller86687062014-07-02 15:28:02 +10002380{
2381 struct sshbuf *principals = NULL;
2382 u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2383 size_t i, ca_len, sig_len;
2384 int ret = SSH_ERR_INTERNAL_ERROR;
2385 struct sshbuf *cert;
2386
2387 if (k == NULL || k->cert == NULL ||
2388 k->cert->certblob == NULL || ca == NULL)
2389 return SSH_ERR_INVALID_ARGUMENT;
2390 if (!sshkey_is_cert(k))
2391 return SSH_ERR_KEY_TYPE_UNKNOWN;
2392 if (!sshkey_type_is_valid_ca(ca->type))
2393 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2394
2395 if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2396 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2397
2398 cert = k->cert->certblob; /* for readability */
2399 sshbuf_reset(cert);
2400 if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2401 goto out;
2402
2403 /* -v01 certs put nonce first */
2404 arc4random_buf(&nonce, sizeof(nonce));
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002405 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2406 goto out;
Damien Miller86687062014-07-02 15:28:02 +10002407
2408 /* XXX this substantially duplicates to_blob(); refactor */
2409 switch (k->type) {
2410#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10002411 case KEY_DSA_CERT:
2412 if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
2413 (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
2414 (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
2415 (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
2416 goto out;
2417 break;
2418# ifdef OPENSSL_HAS_ECC
2419 case KEY_ECDSA_CERT:
2420 if ((ret = sshbuf_put_cstring(cert,
2421 sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 ||
2422 (ret = sshbuf_put_ec(cert,
2423 EC_KEY_get0_public_key(k->ecdsa),
2424 EC_KEY_get0_group(k->ecdsa))) != 0)
2425 goto out;
2426 break;
2427# endif /* OPENSSL_HAS_ECC */
Damien Miller86687062014-07-02 15:28:02 +10002428 case KEY_RSA_CERT:
2429 if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
2430 (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
2431 goto out;
2432 break;
2433#endif /* WITH_OPENSSL */
2434 case KEY_ED25519_CERT:
2435 if ((ret = sshbuf_put_string(cert,
2436 k->ed25519_pk, ED25519_PK_SZ)) != 0)
2437 goto out;
2438 break;
2439 default:
2440 ret = SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org55e5bde2015-03-06 01:40:56 +00002441 goto out;
Damien Miller86687062014-07-02 15:28:02 +10002442 }
2443
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002444 if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
2445 (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002446 (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
2447 goto out;
2448
2449 if ((principals = sshbuf_new()) == NULL) {
2450 ret = SSH_ERR_ALLOC_FAIL;
2451 goto out;
2452 }
2453 for (i = 0; i < k->cert->nprincipals; i++) {
2454 if ((ret = sshbuf_put_cstring(principals,
2455 k->cert->principals[i])) != 0)
2456 goto out;
2457 }
2458 if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
2459 (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
2460 (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00002461 (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
2462 (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
2463 (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
Damien Miller86687062014-07-02 15:28:02 +10002464 (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
2465 goto out;
2466
2467 /* Sign the whole mess */
2468 if ((ret = sshkey_sign(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
djm@openbsd.org57464e32016-05-02 09:36:42 +00002469 sshbuf_len(cert), alg, 0)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10002470 goto out;
2471
2472 /* Append signature and we are done */
2473 if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
2474 goto out;
2475 ret = 0;
2476 out:
2477 if (ret != 0)
2478 sshbuf_reset(cert);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00002479 free(sig_blob);
2480 free(ca_blob);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00002481 sshbuf_free(principals);
Damien Miller86687062014-07-02 15:28:02 +10002482 return ret;
2483}
2484
2485int
2486sshkey_cert_check_authority(const struct sshkey *k,
2487 int want_host, int require_principal,
2488 const char *name, const char **reason)
2489{
2490 u_int i, principal_matches;
2491 time_t now = time(NULL);
2492
2493 if (reason != NULL)
2494 *reason = NULL;
2495
2496 if (want_host) {
2497 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
2498 *reason = "Certificate invalid: not a host certificate";
2499 return SSH_ERR_KEY_CERT_INVALID;
2500 }
2501 } else {
2502 if (k->cert->type != SSH2_CERT_TYPE_USER) {
2503 *reason = "Certificate invalid: not a user certificate";
2504 return SSH_ERR_KEY_CERT_INVALID;
2505 }
2506 }
2507 if (now < 0) {
2508 /* yikes - system clock before epoch! */
2509 *reason = "Certificate invalid: not yet valid";
2510 return SSH_ERR_KEY_CERT_INVALID;
2511 }
2512 if ((u_int64_t)now < k->cert->valid_after) {
2513 *reason = "Certificate invalid: not yet valid";
2514 return SSH_ERR_KEY_CERT_INVALID;
2515 }
2516 if ((u_int64_t)now >= k->cert->valid_before) {
2517 *reason = "Certificate invalid: expired";
2518 return SSH_ERR_KEY_CERT_INVALID;
2519 }
2520 if (k->cert->nprincipals == 0) {
2521 if (require_principal) {
2522 *reason = "Certificate lacks principal list";
2523 return SSH_ERR_KEY_CERT_INVALID;
2524 }
2525 } else if (name != NULL) {
2526 principal_matches = 0;
2527 for (i = 0; i < k->cert->nprincipals; i++) {
2528 if (strcmp(name, k->cert->principals[i]) == 0) {
2529 principal_matches = 1;
2530 break;
2531 }
2532 }
2533 if (!principal_matches) {
2534 *reason = "Certificate invalid: name is not a listed "
2535 "principal";
2536 return SSH_ERR_KEY_CERT_INVALID;
2537 }
2538 }
2539 return 0;
2540}
2541
djm@openbsd.org499cf362015-11-19 01:08:55 +00002542size_t
2543sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
2544{
2545 char from[32], to[32], ret[64];
2546 time_t tt;
2547 struct tm *tm;
2548
2549 *from = *to = '\0';
2550 if (cert->valid_after == 0 &&
2551 cert->valid_before == 0xffffffffffffffffULL)
2552 return strlcpy(s, "forever", l);
2553
2554 if (cert->valid_after != 0) {
2555 /* XXX revisit INT_MAX in 2038 :) */
2556 tt = cert->valid_after > INT_MAX ?
2557 INT_MAX : cert->valid_after;
2558 tm = localtime(&tt);
2559 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
2560 }
2561 if (cert->valid_before != 0xffffffffffffffffULL) {
2562 /* XXX revisit INT_MAX in 2038 :) */
2563 tt = cert->valid_before > INT_MAX ?
2564 INT_MAX : cert->valid_before;
2565 tm = localtime(&tt);
2566 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
2567 }
2568
2569 if (cert->valid_after == 0)
2570 snprintf(ret, sizeof(ret), "before %s", to);
2571 else if (cert->valid_before == 0xffffffffffffffffULL)
2572 snprintf(ret, sizeof(ret), "after %s", from);
2573 else
2574 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
2575
2576 return strlcpy(s, ret, l);
2577}
2578
Damien Miller86687062014-07-02 15:28:02 +10002579int
2580sshkey_private_serialize(const struct sshkey *key, struct sshbuf *b)
2581{
2582 int r = SSH_ERR_INTERNAL_ERROR;
2583
2584 if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
2585 goto out;
2586 switch (key->type) {
2587#ifdef WITH_OPENSSL
2588 case KEY_RSA:
2589 if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
2590 (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
2591 (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
2592 (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
2593 (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
2594 (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
2595 goto out;
2596 break;
Damien Miller86687062014-07-02 15:28:02 +10002597 case KEY_RSA_CERT:
2598 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2599 r = SSH_ERR_INVALID_ARGUMENT;
2600 goto out;
2601 }
2602 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2603 (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
2604 (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
2605 (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
2606 (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
2607 goto out;
2608 break;
2609 case KEY_DSA:
2610 if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
2611 (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
2612 (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
2613 (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
2614 (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
2615 goto out;
2616 break;
Damien Miller86687062014-07-02 15:28:02 +10002617 case KEY_DSA_CERT:
2618 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2619 r = SSH_ERR_INVALID_ARGUMENT;
2620 goto out;
2621 }
2622 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2623 (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
2624 goto out;
2625 break;
2626# ifdef OPENSSL_HAS_ECC
2627 case KEY_ECDSA:
2628 if ((r = sshbuf_put_cstring(b,
2629 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
2630 (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
2631 (r = sshbuf_put_bignum2(b,
2632 EC_KEY_get0_private_key(key->ecdsa))) != 0)
2633 goto out;
2634 break;
2635 case KEY_ECDSA_CERT:
2636 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2637 r = SSH_ERR_INVALID_ARGUMENT;
2638 goto out;
2639 }
2640 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2641 (r = sshbuf_put_bignum2(b,
2642 EC_KEY_get0_private_key(key->ecdsa))) != 0)
2643 goto out;
2644 break;
2645# endif /* OPENSSL_HAS_ECC */
2646#endif /* WITH_OPENSSL */
2647 case KEY_ED25519:
2648 if ((r = sshbuf_put_string(b, key->ed25519_pk,
2649 ED25519_PK_SZ)) != 0 ||
2650 (r = sshbuf_put_string(b, key->ed25519_sk,
2651 ED25519_SK_SZ)) != 0)
2652 goto out;
2653 break;
2654 case KEY_ED25519_CERT:
2655 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2656 r = SSH_ERR_INVALID_ARGUMENT;
2657 goto out;
2658 }
2659 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2660 (r = sshbuf_put_string(b, key->ed25519_pk,
2661 ED25519_PK_SZ)) != 0 ||
2662 (r = sshbuf_put_string(b, key->ed25519_sk,
2663 ED25519_SK_SZ)) != 0)
2664 goto out;
2665 break;
2666 default:
2667 r = SSH_ERR_INVALID_ARGUMENT;
2668 goto out;
2669 }
2670 /* success */
2671 r = 0;
2672 out:
2673 return r;
2674}
2675
2676int
2677sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
2678{
2679 char *tname = NULL, *curve = NULL;
2680 struct sshkey *k = NULL;
djm@openbsd.org60b18252015-01-26 02:59:11 +00002681 size_t pklen = 0, sklen = 0;
Damien Miller86687062014-07-02 15:28:02 +10002682 int type, r = SSH_ERR_INTERNAL_ERROR;
2683 u_char *ed25519_pk = NULL, *ed25519_sk = NULL;
2684#ifdef WITH_OPENSSL
2685 BIGNUM *exponent = NULL;
2686#endif /* WITH_OPENSSL */
2687
2688 if (kp != NULL)
2689 *kp = NULL;
2690 if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
2691 goto out;
2692 type = sshkey_type_from_name(tname);
2693 switch (type) {
2694#ifdef WITH_OPENSSL
2695 case KEY_DSA:
2696 if ((k = sshkey_new_private(type)) == NULL) {
2697 r = SSH_ERR_ALLOC_FAIL;
2698 goto out;
2699 }
2700 if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
2701 (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
2702 (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
2703 (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
2704 (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
2705 goto out;
2706 break;
Damien Miller86687062014-07-02 15:28:02 +10002707 case KEY_DSA_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002708 if ((r = sshkey_froms(buf, &k)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002709 (r = sshkey_add_private(k)) != 0 ||
2710 (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
2711 goto out;
2712 break;
2713# ifdef OPENSSL_HAS_ECC
2714 case KEY_ECDSA:
2715 if ((k = sshkey_new_private(type)) == NULL) {
2716 r = SSH_ERR_ALLOC_FAIL;
2717 goto out;
2718 }
2719 if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
2720 r = SSH_ERR_INVALID_ARGUMENT;
2721 goto out;
2722 }
2723 if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
2724 goto out;
2725 if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
2726 r = SSH_ERR_EC_CURVE_MISMATCH;
2727 goto out;
2728 }
2729 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
2730 if (k->ecdsa == NULL || (exponent = BN_new()) == NULL) {
2731 r = SSH_ERR_LIBCRYPTO_ERROR;
2732 goto out;
2733 }
2734 if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 ||
2735 (r = sshbuf_get_bignum2(buf, exponent)))
2736 goto out;
2737 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
2738 r = SSH_ERR_LIBCRYPTO_ERROR;
2739 goto out;
2740 }
2741 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +00002742 EC_KEY_get0_public_key(k->ecdsa))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002743 (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
2744 goto out;
2745 break;
2746 case KEY_ECDSA_CERT:
2747 if ((exponent = BN_new()) == NULL) {
2748 r = SSH_ERR_LIBCRYPTO_ERROR;
2749 goto out;
2750 }
djm@openbsd.org60b18252015-01-26 02:59:11 +00002751 if ((r = sshkey_froms(buf, &k)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002752 (r = sshkey_add_private(k)) != 0 ||
2753 (r = sshbuf_get_bignum2(buf, exponent)) != 0)
2754 goto out;
2755 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
2756 r = SSH_ERR_LIBCRYPTO_ERROR;
2757 goto out;
2758 }
2759 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +00002760 EC_KEY_get0_public_key(k->ecdsa))) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002761 (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
2762 goto out;
2763 break;
2764# endif /* OPENSSL_HAS_ECC */
2765 case KEY_RSA:
2766 if ((k = sshkey_new_private(type)) == NULL) {
2767 r = SSH_ERR_ALLOC_FAIL;
2768 goto out;
2769 }
2770 if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
2771 (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
2772 (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
2773 (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
2774 (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
2775 (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
2776 (r = rsa_generate_additional_parameters(k->rsa)) != 0)
2777 goto out;
2778 break;
Damien Miller86687062014-07-02 15:28:02 +10002779 case KEY_RSA_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002780 if ((r = sshkey_froms(buf, &k)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002781 (r = sshkey_add_private(k)) != 0 ||
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +00002782 (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
2783 (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
2784 (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
2785 (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002786 (r = rsa_generate_additional_parameters(k->rsa)) != 0)
2787 goto out;
2788 break;
2789#endif /* WITH_OPENSSL */
2790 case KEY_ED25519:
2791 if ((k = sshkey_new_private(type)) == NULL) {
2792 r = SSH_ERR_ALLOC_FAIL;
2793 goto out;
2794 }
2795 if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
2796 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
2797 goto out;
2798 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
2799 r = SSH_ERR_INVALID_FORMAT;
2800 goto out;
2801 }
2802 k->ed25519_pk = ed25519_pk;
2803 k->ed25519_sk = ed25519_sk;
2804 ed25519_pk = ed25519_sk = NULL;
2805 break;
2806 case KEY_ED25519_CERT:
djm@openbsd.org60b18252015-01-26 02:59:11 +00002807 if ((r = sshkey_froms(buf, &k)) != 0 ||
Damien Miller86687062014-07-02 15:28:02 +10002808 (r = sshkey_add_private(k)) != 0 ||
2809 (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
2810 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
2811 goto out;
2812 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
2813 r = SSH_ERR_INVALID_FORMAT;
2814 goto out;
2815 }
2816 k->ed25519_pk = ed25519_pk;
2817 k->ed25519_sk = ed25519_sk;
2818 ed25519_pk = ed25519_sk = NULL;
2819 break;
2820 default:
2821 r = SSH_ERR_KEY_TYPE_UNKNOWN;
2822 goto out;
2823 }
2824#ifdef WITH_OPENSSL
2825 /* enable blinding */
2826 switch (k->type) {
2827 case KEY_RSA:
Damien Miller86687062014-07-02 15:28:02 +10002828 case KEY_RSA_CERT:
2829 case KEY_RSA1:
2830 if (RSA_blinding_on(k->rsa, NULL) != 1) {
2831 r = SSH_ERR_LIBCRYPTO_ERROR;
2832 goto out;
2833 }
2834 break;
2835 }
2836#endif /* WITH_OPENSSL */
2837 /* success */
2838 r = 0;
2839 if (kp != NULL) {
2840 *kp = k;
2841 k = NULL;
2842 }
2843 out:
2844 free(tname);
2845 free(curve);
2846#ifdef WITH_OPENSSL
2847 if (exponent != NULL)
2848 BN_clear_free(exponent);
2849#endif /* WITH_OPENSSL */
2850 sshkey_free(k);
2851 if (ed25519_pk != NULL) {
2852 explicit_bzero(ed25519_pk, pklen);
2853 free(ed25519_pk);
2854 }
2855 if (ed25519_sk != NULL) {
2856 explicit_bzero(ed25519_sk, sklen);
2857 free(ed25519_sk);
2858 }
2859 return r;
2860}
2861
2862#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2863int
2864sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2865{
2866 BN_CTX *bnctx;
2867 EC_POINT *nq = NULL;
2868 BIGNUM *order, *x, *y, *tmp;
2869 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2870
djm@openbsd.orga571dbc2016-10-04 21:34:40 +00002871 /*
2872 * NB. This assumes OpenSSL has already verified that the public
2873 * point lies on the curve. This is done by EC_POINT_oct2point()
2874 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
2875 * reachable with public points not unmarshalled using
2876 * EC_POINT_oct2point then the caller will need to explicitly check.
2877 */
2878
Damien Miller86687062014-07-02 15:28:02 +10002879 if ((bnctx = BN_CTX_new()) == NULL)
2880 return SSH_ERR_ALLOC_FAIL;
2881 BN_CTX_start(bnctx);
2882
2883 /*
2884 * We shouldn't ever hit this case because bignum_get_ecpoint()
2885 * refuses to load GF2m points.
2886 */
2887 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2888 NID_X9_62_prime_field)
2889 goto out;
2890
2891 /* Q != infinity */
2892 if (EC_POINT_is_at_infinity(group, public))
2893 goto out;
2894
2895 if ((x = BN_CTX_get(bnctx)) == NULL ||
2896 (y = BN_CTX_get(bnctx)) == NULL ||
2897 (order = BN_CTX_get(bnctx)) == NULL ||
2898 (tmp = BN_CTX_get(bnctx)) == NULL) {
2899 ret = SSH_ERR_ALLOC_FAIL;
2900 goto out;
2901 }
2902
2903 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2904 if (EC_GROUP_get_order(group, order, bnctx) != 1 ||
2905 EC_POINT_get_affine_coordinates_GFp(group, public,
2906 x, y, bnctx) != 1) {
2907 ret = SSH_ERR_LIBCRYPTO_ERROR;
2908 goto out;
2909 }
2910 if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
2911 BN_num_bits(y) <= BN_num_bits(order) / 2)
2912 goto out;
2913
2914 /* nQ == infinity (n == order of subgroup) */
2915 if ((nq = EC_POINT_new(group)) == NULL) {
2916 ret = SSH_ERR_ALLOC_FAIL;
2917 goto out;
2918 }
2919 if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1) {
2920 ret = SSH_ERR_LIBCRYPTO_ERROR;
2921 goto out;
2922 }
2923 if (EC_POINT_is_at_infinity(group, nq) != 1)
2924 goto out;
2925
2926 /* x < order - 1, y < order - 1 */
2927 if (!BN_sub(tmp, order, BN_value_one())) {
2928 ret = SSH_ERR_LIBCRYPTO_ERROR;
2929 goto out;
2930 }
2931 if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
2932 goto out;
2933 ret = 0;
2934 out:
2935 BN_CTX_free(bnctx);
2936 if (nq != NULL)
2937 EC_POINT_free(nq);
2938 return ret;
2939}
2940
2941int
2942sshkey_ec_validate_private(const EC_KEY *key)
2943{
2944 BN_CTX *bnctx;
2945 BIGNUM *order, *tmp;
2946 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2947
2948 if ((bnctx = BN_CTX_new()) == NULL)
2949 return SSH_ERR_ALLOC_FAIL;
2950 BN_CTX_start(bnctx);
2951
2952 if ((order = BN_CTX_get(bnctx)) == NULL ||
2953 (tmp = BN_CTX_get(bnctx)) == NULL) {
2954 ret = SSH_ERR_ALLOC_FAIL;
2955 goto out;
2956 }
2957
2958 /* log2(private) > log2(order)/2 */
2959 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1) {
2960 ret = SSH_ERR_LIBCRYPTO_ERROR;
2961 goto out;
2962 }
2963 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2964 BN_num_bits(order) / 2)
2965 goto out;
2966
2967 /* private < order - 1 */
2968 if (!BN_sub(tmp, order, BN_value_one())) {
2969 ret = SSH_ERR_LIBCRYPTO_ERROR;
2970 goto out;
2971 }
2972 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
2973 goto out;
2974 ret = 0;
2975 out:
2976 BN_CTX_free(bnctx);
2977 return ret;
2978}
2979
2980void
2981sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2982{
2983 BIGNUM *x, *y;
2984 BN_CTX *bnctx;
2985
2986 if (point == NULL) {
2987 fputs("point=(NULL)\n", stderr);
2988 return;
2989 }
2990 if ((bnctx = BN_CTX_new()) == NULL) {
2991 fprintf(stderr, "%s: BN_CTX_new failed\n", __func__);
2992 return;
2993 }
2994 BN_CTX_start(bnctx);
2995 if ((x = BN_CTX_get(bnctx)) == NULL ||
2996 (y = BN_CTX_get(bnctx)) == NULL) {
2997 fprintf(stderr, "%s: BN_CTX_get failed\n", __func__);
2998 return;
2999 }
3000 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
3001 NID_X9_62_prime_field) {
3002 fprintf(stderr, "%s: group is not a prime field\n", __func__);
3003 return;
3004 }
3005 if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y,
3006 bnctx) != 1) {
3007 fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
3008 __func__);
3009 return;
3010 }
3011 fputs("x=", stderr);
3012 BN_print_fp(stderr, x);
3013 fputs("\ny=", stderr);
3014 BN_print_fp(stderr, y);
3015 fputs("\n", stderr);
3016 BN_CTX_free(bnctx);
3017}
3018
3019void
3020sshkey_dump_ec_key(const EC_KEY *key)
3021{
3022 const BIGNUM *exponent;
3023
3024 sshkey_dump_ec_point(EC_KEY_get0_group(key),
3025 EC_KEY_get0_public_key(key));
3026 fputs("exponent=", stderr);
3027 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
3028 fputs("(NULL)", stderr);
3029 else
3030 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
3031 fputs("\n", stderr);
3032}
3033#endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
3034
3035static int
3036sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob,
3037 const char *passphrase, const char *comment, const char *ciphername,
3038 int rounds)
3039{
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00003040 u_char *cp, *key = NULL, *pubkeyblob = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003041 u_char salt[SALT_LEN];
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00003042 char *b64 = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003043 size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
3044 u_int check;
3045 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003046 struct sshcipher_ctx *ciphercontext = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003047 const struct sshcipher *cipher;
3048 const char *kdfname = KDFNAME;
3049 struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
3050
Damien Miller86687062014-07-02 15:28:02 +10003051 if (rounds <= 0)
3052 rounds = DEFAULT_ROUNDS;
3053 if (passphrase == NULL || !strlen(passphrase)) {
3054 ciphername = "none";
3055 kdfname = "none";
3056 } else if (ciphername == NULL)
3057 ciphername = DEFAULT_CIPHERNAME;
3058 else if (cipher_number(ciphername) != SSH_CIPHER_SSH2) {
3059 r = SSH_ERR_INVALID_ARGUMENT;
3060 goto out;
3061 }
3062 if ((cipher = cipher_by_name(ciphername)) == NULL) {
3063 r = SSH_ERR_INTERNAL_ERROR;
3064 goto out;
3065 }
3066
3067 if ((kdf = sshbuf_new()) == NULL ||
3068 (encoded = sshbuf_new()) == NULL ||
3069 (encrypted = sshbuf_new()) == NULL) {
3070 r = SSH_ERR_ALLOC_FAIL;
3071 goto out;
3072 }
3073 blocksize = cipher_blocksize(cipher);
3074 keylen = cipher_keylen(cipher);
3075 ivlen = cipher_ivlen(cipher);
3076 authlen = cipher_authlen(cipher);
3077 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3078 r = SSH_ERR_ALLOC_FAIL;
3079 goto out;
3080 }
3081 if (strcmp(kdfname, "bcrypt") == 0) {
3082 arc4random_buf(salt, SALT_LEN);
3083 if (bcrypt_pbkdf(passphrase, strlen(passphrase),
3084 salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
3085 r = SSH_ERR_INVALID_ARGUMENT;
3086 goto out;
3087 }
3088 if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
3089 (r = sshbuf_put_u32(kdf, rounds)) != 0)
3090 goto out;
3091 } else if (strcmp(kdfname, "none") != 0) {
3092 /* Unsupported KDF type */
3093 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3094 goto out;
3095 }
3096 if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
3097 key + keylen, ivlen, 1)) != 0)
3098 goto out;
3099
3100 if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
3101 (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
3102 (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
3103 (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
3104 (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
3105 (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
3106 (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
3107 goto out;
3108
3109 /* set up the buffer that will be encrypted */
3110
3111 /* Random check bytes */
3112 check = arc4random();
3113 if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
3114 (r = sshbuf_put_u32(encrypted, check)) != 0)
3115 goto out;
3116
3117 /* append private key and comment*/
3118 if ((r = sshkey_private_serialize(prv, encrypted)) != 0 ||
3119 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
3120 goto out;
3121
3122 /* padding */
3123 i = 0;
3124 while (sshbuf_len(encrypted) % blocksize) {
3125 if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
3126 goto out;
3127 }
3128
3129 /* length in destination buffer */
3130 if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
3131 goto out;
3132
3133 /* encrypt */
3134 if ((r = sshbuf_reserve(encoded,
3135 sshbuf_len(encrypted) + authlen, &cp)) != 0)
3136 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003137 if ((r = cipher_crypt(ciphercontext, 0, cp,
Damien Miller86687062014-07-02 15:28:02 +10003138 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
3139 goto out;
3140
3141 /* uuencode */
3142 if ((b64 = sshbuf_dtob64(encoded)) == NULL) {
3143 r = SSH_ERR_ALLOC_FAIL;
3144 goto out;
3145 }
3146
3147 sshbuf_reset(blob);
3148 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0)
3149 goto out;
3150 for (i = 0; i < strlen(b64); i++) {
3151 if ((r = sshbuf_put_u8(blob, b64[i])) != 0)
3152 goto out;
3153 /* insert line breaks */
3154 if (i % 70 == 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3155 goto out;
3156 }
3157 if (i % 70 != 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3158 goto out;
3159 if ((r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
3160 goto out;
3161
3162 /* success */
3163 r = 0;
3164
3165 out:
3166 sshbuf_free(kdf);
3167 sshbuf_free(encoded);
3168 sshbuf_free(encrypted);
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003169 cipher_free(ciphercontext);
Damien Miller86687062014-07-02 15:28:02 +10003170 explicit_bzero(salt, sizeof(salt));
3171 if (key != NULL) {
3172 explicit_bzero(key, keylen + ivlen);
3173 free(key);
3174 }
3175 if (pubkeyblob != NULL) {
3176 explicit_bzero(pubkeyblob, pubkeylen);
3177 free(pubkeyblob);
3178 }
3179 if (b64 != NULL) {
3180 explicit_bzero(b64, strlen(b64));
3181 free(b64);
3182 }
3183 return r;
3184}
3185
3186static int
3187sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
3188 struct sshkey **keyp, char **commentp)
3189{
3190 char *comment = NULL, *ciphername = NULL, *kdfname = NULL;
3191 const struct sshcipher *cipher = NULL;
3192 const u_char *cp;
3193 int r = SSH_ERR_INTERNAL_ERROR;
3194 size_t encoded_len;
djm@openbsd.org63ebf012015-05-08 03:17:49 +00003195 size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0;
Damien Miller86687062014-07-02 15:28:02 +10003196 struct sshbuf *encoded = NULL, *decoded = NULL;
3197 struct sshbuf *kdf = NULL, *decrypted = NULL;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003198 struct sshcipher_ctx *ciphercontext = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003199 struct sshkey *k = NULL;
3200 u_char *key = NULL, *salt = NULL, *dp, pad, last;
3201 u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
3202
Damien Miller86687062014-07-02 15:28:02 +10003203 if (keyp != NULL)
3204 *keyp = NULL;
3205 if (commentp != NULL)
3206 *commentp = NULL;
3207
3208 if ((encoded = sshbuf_new()) == NULL ||
3209 (decoded = sshbuf_new()) == NULL ||
3210 (decrypted = sshbuf_new()) == NULL) {
3211 r = SSH_ERR_ALLOC_FAIL;
3212 goto out;
3213 }
3214
3215 /* check preamble */
3216 cp = sshbuf_ptr(blob);
3217 encoded_len = sshbuf_len(blob);
3218 if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
3219 memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
3220 r = SSH_ERR_INVALID_FORMAT;
3221 goto out;
3222 }
3223 cp += MARK_BEGIN_LEN;
3224 encoded_len -= MARK_BEGIN_LEN;
3225
3226 /* Look for end marker, removing whitespace as we go */
3227 while (encoded_len > 0) {
3228 if (*cp != '\n' && *cp != '\r') {
3229 if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
3230 goto out;
3231 }
3232 last = *cp;
3233 encoded_len--;
3234 cp++;
3235 if (last == '\n') {
3236 if (encoded_len >= MARK_END_LEN &&
3237 memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
3238 /* \0 terminate */
3239 if ((r = sshbuf_put_u8(encoded, 0)) != 0)
3240 goto out;
3241 break;
3242 }
3243 }
3244 }
3245 if (encoded_len == 0) {
3246 r = SSH_ERR_INVALID_FORMAT;
3247 goto out;
3248 }
3249
3250 /* decode base64 */
djm@openbsd.org3cc1fbb2014-10-08 21:45:48 +00003251 if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
Damien Miller86687062014-07-02 15:28:02 +10003252 goto out;
3253
3254 /* check magic */
3255 if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
3256 memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
3257 r = SSH_ERR_INVALID_FORMAT;
3258 goto out;
3259 }
3260 /* parse public portion of key */
3261 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
3262 (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
3263 (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
3264 (r = sshbuf_froms(decoded, &kdf)) != 0 ||
3265 (r = sshbuf_get_u32(decoded, &nkeys)) != 0 ||
3266 (r = sshbuf_skip_string(decoded)) != 0 || /* pubkey */
3267 (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
3268 goto out;
3269
3270 if ((cipher = cipher_by_name(ciphername)) == NULL) {
3271 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3272 goto out;
3273 }
3274 if ((passphrase == NULL || strlen(passphrase) == 0) &&
3275 strcmp(ciphername, "none") != 0) {
3276 /* passphrase required */
3277 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3278 goto out;
3279 }
3280 if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
3281 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3282 goto out;
3283 }
3284 if (!strcmp(kdfname, "none") && strcmp(ciphername, "none") != 0) {
3285 r = SSH_ERR_INVALID_FORMAT;
3286 goto out;
3287 }
3288 if (nkeys != 1) {
3289 /* XXX only one key supported */
3290 r = SSH_ERR_INVALID_FORMAT;
3291 goto out;
3292 }
3293
3294 /* check size of encrypted key blob */
3295 blocksize = cipher_blocksize(cipher);
3296 if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
3297 r = SSH_ERR_INVALID_FORMAT;
3298 goto out;
3299 }
3300
3301 /* setup key */
3302 keylen = cipher_keylen(cipher);
3303 ivlen = cipher_ivlen(cipher);
djm@openbsd.org63ebf012015-05-08 03:17:49 +00003304 authlen = cipher_authlen(cipher);
Damien Miller86687062014-07-02 15:28:02 +10003305 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3306 r = SSH_ERR_ALLOC_FAIL;
3307 goto out;
3308 }
3309 if (strcmp(kdfname, "bcrypt") == 0) {
3310 if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
3311 (r = sshbuf_get_u32(kdf, &rounds)) != 0)
3312 goto out;
3313 if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
3314 key, keylen + ivlen, rounds) < 0) {
3315 r = SSH_ERR_INVALID_FORMAT;
3316 goto out;
3317 }
3318 }
3319
djm@openbsd.org63ebf012015-05-08 03:17:49 +00003320 /* check that an appropriate amount of auth data is present */
3321 if (sshbuf_len(decoded) < encrypted_len + authlen) {
3322 r = SSH_ERR_INVALID_FORMAT;
3323 goto out;
3324 }
3325
Damien Miller86687062014-07-02 15:28:02 +10003326 /* decrypt private portion of key */
3327 if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
3328 (r = cipher_init(&ciphercontext, cipher, key, keylen,
3329 key + keylen, ivlen, 0)) != 0)
3330 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003331 if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
djm@openbsd.org63ebf012015-05-08 03:17:49 +00003332 encrypted_len, 0, authlen)) != 0) {
Damien Miller86687062014-07-02 15:28:02 +10003333 /* an integrity error here indicates an incorrect passphrase */
3334 if (r == SSH_ERR_MAC_INVALID)
3335 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3336 goto out;
3337 }
djm@openbsd.org63ebf012015-05-08 03:17:49 +00003338 if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10003339 goto out;
3340 /* there should be no trailing data */
3341 if (sshbuf_len(decoded) != 0) {
3342 r = SSH_ERR_INVALID_FORMAT;
3343 goto out;
3344 }
3345
3346 /* check check bytes */
3347 if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
3348 (r = sshbuf_get_u32(decrypted, &check2)) != 0)
3349 goto out;
3350 if (check1 != check2) {
3351 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3352 goto out;
3353 }
3354
3355 /* Load the private key and comment */
3356 if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
3357 (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
3358 goto out;
3359
3360 /* Check deterministic padding */
3361 i = 0;
3362 while (sshbuf_len(decrypted)) {
3363 if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
3364 goto out;
3365 if (pad != (++i & 0xff)) {
3366 r = SSH_ERR_INVALID_FORMAT;
3367 goto out;
3368 }
3369 }
3370
3371 /* XXX decode pubkey and check against private */
3372
3373 /* success */
3374 r = 0;
3375 if (keyp != NULL) {
3376 *keyp = k;
3377 k = NULL;
3378 }
3379 if (commentp != NULL) {
3380 *commentp = comment;
3381 comment = NULL;
3382 }
3383 out:
3384 pad = 0;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003385 cipher_free(ciphercontext);
Damien Miller86687062014-07-02 15:28:02 +10003386 free(ciphername);
3387 free(kdfname);
3388 free(comment);
3389 if (salt != NULL) {
3390 explicit_bzero(salt, slen);
3391 free(salt);
3392 }
3393 if (key != NULL) {
3394 explicit_bzero(key, keylen + ivlen);
3395 free(key);
3396 }
3397 sshbuf_free(encoded);
3398 sshbuf_free(decoded);
3399 sshbuf_free(kdf);
3400 sshbuf_free(decrypted);
3401 sshkey_free(k);
3402 return r;
3403}
3404
3405#if WITH_SSH1
3406/*
3407 * Serialises the authentication (private) key to a blob, encrypting it with
3408 * passphrase. The identification of the blob (lowest 64 bits of n) will
3409 * precede the key to provide identification of the key without needing a
3410 * passphrase.
3411 */
3412static int
3413sshkey_private_rsa1_to_blob(struct sshkey *key, struct sshbuf *blob,
3414 const char *passphrase, const char *comment)
3415{
3416 struct sshbuf *buffer = NULL, *encrypted = NULL;
3417 u_char buf[8];
3418 int r, cipher_num;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003419 struct sshcipher_ctx *ciphercontext = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003420 const struct sshcipher *cipher;
3421 u_char *cp;
3422
3423 /*
3424 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
3425 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
3426 */
3427 cipher_num = (strcmp(passphrase, "") == 0) ?
3428 SSH_CIPHER_NONE : SSH_CIPHER_3DES;
3429 if ((cipher = cipher_by_number(cipher_num)) == NULL)
3430 return SSH_ERR_INTERNAL_ERROR;
3431
3432 /* This buffer is used to build the secret part of the private key. */
3433 if ((buffer = sshbuf_new()) == NULL)
3434 return SSH_ERR_ALLOC_FAIL;
3435
3436 /* Put checkbytes for checking passphrase validity. */
3437 if ((r = sshbuf_reserve(buffer, 4, &cp)) != 0)
3438 goto out;
3439 arc4random_buf(cp, 2);
3440 memcpy(cp + 2, cp, 2);
3441
3442 /*
3443 * Store the private key (n and e will not be stored because they
3444 * will be stored in plain text, and storing them also in encrypted
3445 * format would just give known plaintext).
3446 * Note: q and p are stored in reverse order to SSL.
3447 */
3448 if ((r = sshbuf_put_bignum1(buffer, key->rsa->d)) != 0 ||
3449 (r = sshbuf_put_bignum1(buffer, key->rsa->iqmp)) != 0 ||
3450 (r = sshbuf_put_bignum1(buffer, key->rsa->q)) != 0 ||
3451 (r = sshbuf_put_bignum1(buffer, key->rsa->p)) != 0)
3452 goto out;
3453
3454 /* Pad the part to be encrypted to a size that is a multiple of 8. */
3455 explicit_bzero(buf, 8);
3456 if ((r = sshbuf_put(buffer, buf, 8 - (sshbuf_len(buffer) % 8))) != 0)
3457 goto out;
3458
3459 /* This buffer will be used to contain the data in the file. */
3460 if ((encrypted = sshbuf_new()) == NULL) {
3461 r = SSH_ERR_ALLOC_FAIL;
3462 goto out;
3463 }
3464
3465 /* First store keyfile id string. */
3466 if ((r = sshbuf_put(encrypted, LEGACY_BEGIN,
3467 sizeof(LEGACY_BEGIN))) != 0)
3468 goto out;
3469
3470 /* Store cipher type and "reserved" field. */
3471 if ((r = sshbuf_put_u8(encrypted, cipher_num)) != 0 ||
3472 (r = sshbuf_put_u32(encrypted, 0)) != 0)
3473 goto out;
3474
3475 /* Store public key. This will be in plain text. */
3476 if ((r = sshbuf_put_u32(encrypted, BN_num_bits(key->rsa->n))) != 0 ||
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +00003477 (r = sshbuf_put_bignum1(encrypted, key->rsa->n)) != 0 ||
3478 (r = sshbuf_put_bignum1(encrypted, key->rsa->e)) != 0 ||
3479 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10003480 goto out;
3481
3482 /* Allocate space for the private part of the key in the buffer. */
3483 if ((r = sshbuf_reserve(encrypted, sshbuf_len(buffer), &cp)) != 0)
3484 goto out;
3485
3486 if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase,
3487 CIPHER_ENCRYPT)) != 0)
3488 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003489 if ((r = cipher_crypt(ciphercontext, 0, cp,
Damien Miller86687062014-07-02 15:28:02 +10003490 sshbuf_ptr(buffer), sshbuf_len(buffer), 0, 0)) != 0)
3491 goto out;
Damien Miller86687062014-07-02 15:28:02 +10003492
3493 r = sshbuf_putb(blob, encrypted);
3494
3495 out:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003496 cipher_free(ciphercontext);
Damien Miller86687062014-07-02 15:28:02 +10003497 explicit_bzero(buf, sizeof(buf));
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00003498 sshbuf_free(buffer);
3499 sshbuf_free(encrypted);
Damien Miller86687062014-07-02 15:28:02 +10003500
3501 return r;
3502}
3503#endif /* WITH_SSH1 */
3504
3505#ifdef WITH_OPENSSL
3506/* convert SSH v2 key in OpenSSL PEM format */
3507static int
3508sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
3509 const char *_passphrase, const char *comment)
3510{
3511 int success, r;
3512 int blen, len = strlen(_passphrase);
3513 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
3514#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
3515 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
3516#else
3517 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
3518#endif
3519 const u_char *bptr;
3520 BIO *bio = NULL;
3521
3522 if (len > 0 && len <= 4)
3523 return SSH_ERR_PASSPHRASE_TOO_SHORT;
3524 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3525 return SSH_ERR_ALLOC_FAIL;
3526
3527 switch (key->type) {
3528 case KEY_DSA:
3529 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
3530 cipher, passphrase, len, NULL, NULL);
3531 break;
3532#ifdef OPENSSL_HAS_ECC
3533 case KEY_ECDSA:
3534 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
3535 cipher, passphrase, len, NULL, NULL);
3536 break;
3537#endif
3538 case KEY_RSA:
3539 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
3540 cipher, passphrase, len, NULL, NULL);
3541 break;
3542 default:
3543 success = 0;
3544 break;
3545 }
3546 if (success == 0) {
3547 r = SSH_ERR_LIBCRYPTO_ERROR;
3548 goto out;
3549 }
3550 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
3551 r = SSH_ERR_INTERNAL_ERROR;
3552 goto out;
3553 }
3554 if ((r = sshbuf_put(blob, bptr, blen)) != 0)
3555 goto out;
3556 r = 0;
3557 out:
3558 BIO_free(bio);
3559 return r;
3560}
3561#endif /* WITH_OPENSSL */
3562
3563/* Serialise "key" to buffer "blob" */
3564int
3565sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
3566 const char *passphrase, const char *comment,
3567 int force_new_format, const char *new_format_cipher, int new_format_rounds)
3568{
3569 switch (key->type) {
markus@openbsd.orgf067cca2015-01-12 13:29:27 +00003570#ifdef WITH_SSH1
Damien Miller86687062014-07-02 15:28:02 +10003571 case KEY_RSA1:
3572 return sshkey_private_rsa1_to_blob(key, blob,
3573 passphrase, comment);
markus@openbsd.orgf067cca2015-01-12 13:29:27 +00003574#endif /* WITH_SSH1 */
3575#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10003576 case KEY_DSA:
3577 case KEY_ECDSA:
3578 case KEY_RSA:
3579 if (force_new_format) {
3580 return sshkey_private_to_blob2(key, blob, passphrase,
3581 comment, new_format_cipher, new_format_rounds);
3582 }
3583 return sshkey_private_pem_to_blob(key, blob,
3584 passphrase, comment);
3585#endif /* WITH_OPENSSL */
3586 case KEY_ED25519:
3587 return sshkey_private_to_blob2(key, blob, passphrase,
3588 comment, new_format_cipher, new_format_rounds);
3589 default:
3590 return SSH_ERR_KEY_TYPE_UNKNOWN;
3591 }
3592}
3593
3594#ifdef WITH_SSH1
3595/*
3596 * Parse the public, unencrypted portion of a RSA1 key.
3597 */
3598int
3599sshkey_parse_public_rsa1_fileblob(struct sshbuf *blob,
3600 struct sshkey **keyp, char **commentp)
3601{
3602 int r;
3603 struct sshkey *pub = NULL;
3604 struct sshbuf *copy = NULL;
3605
3606 if (keyp != NULL)
3607 *keyp = NULL;
3608 if (commentp != NULL)
3609 *commentp = NULL;
3610
3611 /* Check that it is at least big enough to contain the ID string. */
3612 if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN))
3613 return SSH_ERR_INVALID_FORMAT;
3614
3615 /*
3616 * Make sure it begins with the id string. Consume the id string
3617 * from the buffer.
3618 */
3619 if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0)
3620 return SSH_ERR_INVALID_FORMAT;
3621 /* Make a working copy of the keyblob and skip past the magic */
3622 if ((copy = sshbuf_fromb(blob)) == NULL)
3623 return SSH_ERR_ALLOC_FAIL;
3624 if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0)
3625 goto out;
3626
3627 /* Skip cipher type, reserved data and key bits. */
3628 if ((r = sshbuf_get_u8(copy, NULL)) != 0 || /* cipher type */
3629 (r = sshbuf_get_u32(copy, NULL)) != 0 || /* reserved */
3630 (r = sshbuf_get_u32(copy, NULL)) != 0) /* key bits */
3631 goto out;
3632
3633 /* Read the public key from the buffer. */
3634 if ((pub = sshkey_new(KEY_RSA1)) == NULL ||
3635 (r = sshbuf_get_bignum1(copy, pub->rsa->n)) != 0 ||
3636 (r = sshbuf_get_bignum1(copy, pub->rsa->e)) != 0)
3637 goto out;
3638
3639 /* Finally, the comment */
3640 if ((r = sshbuf_get_string(copy, (u_char**)commentp, NULL)) != 0)
3641 goto out;
3642
3643 /* The encrypted private part is not parsed by this function. */
3644
3645 r = 0;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003646 if (keyp != NULL) {
Damien Miller86687062014-07-02 15:28:02 +10003647 *keyp = pub;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003648 pub = NULL;
3649 }
Damien Miller86687062014-07-02 15:28:02 +10003650 out:
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00003651 sshbuf_free(copy);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00003652 sshkey_free(pub);
Damien Miller86687062014-07-02 15:28:02 +10003653 return r;
3654}
3655
3656static int
3657sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase,
3658 struct sshkey **keyp, char **commentp)
3659{
3660 int r;
3661 u_int16_t check1, check2;
3662 u_int8_t cipher_type;
3663 struct sshbuf *decrypted = NULL, *copy = NULL;
3664 u_char *cp;
3665 char *comment = NULL;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003666 struct sshcipher_ctx *ciphercontext = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003667 const struct sshcipher *cipher;
3668 struct sshkey *prv = NULL;
3669
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003670 if (keyp != NULL)
3671 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003672 if (commentp != NULL)
3673 *commentp = NULL;
3674
3675 /* Check that it is at least big enough to contain the ID string. */
3676 if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN))
3677 return SSH_ERR_INVALID_FORMAT;
3678
3679 /*
3680 * Make sure it begins with the id string. Consume the id string
3681 * from the buffer.
3682 */
3683 if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0)
3684 return SSH_ERR_INVALID_FORMAT;
3685
3686 if ((prv = sshkey_new_private(KEY_RSA1)) == NULL) {
3687 r = SSH_ERR_ALLOC_FAIL;
3688 goto out;
3689 }
3690 if ((copy = sshbuf_fromb(blob)) == NULL ||
3691 (decrypted = sshbuf_new()) == NULL) {
3692 r = SSH_ERR_ALLOC_FAIL;
3693 goto out;
3694 }
3695 if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0)
3696 goto out;
3697
3698 /* Read cipher type. */
3699 if ((r = sshbuf_get_u8(copy, &cipher_type)) != 0 ||
3700 (r = sshbuf_get_u32(copy, NULL)) != 0) /* reserved */
3701 goto out;
3702
3703 /* Read the public key and comment from the buffer. */
3704 if ((r = sshbuf_get_u32(copy, NULL)) != 0 || /* key bits */
3705 (r = sshbuf_get_bignum1(copy, prv->rsa->n)) != 0 ||
3706 (r = sshbuf_get_bignum1(copy, prv->rsa->e)) != 0 ||
3707 (r = sshbuf_get_cstring(copy, &comment, NULL)) != 0)
3708 goto out;
3709
3710 /* Check that it is a supported cipher. */
3711 cipher = cipher_by_number(cipher_type);
3712 if (cipher == NULL) {
3713 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3714 goto out;
3715 }
3716 /* Initialize space for decrypted data. */
3717 if ((r = sshbuf_reserve(decrypted, sshbuf_len(copy), &cp)) != 0)
3718 goto out;
3719
3720 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
3721 if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase,
3722 CIPHER_DECRYPT)) != 0)
3723 goto out;
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003724 if ((r = cipher_crypt(ciphercontext, 0, cp,
3725 sshbuf_ptr(copy), sshbuf_len(copy), 0, 0)) != 0)
Damien Miller86687062014-07-02 15:28:02 +10003726 goto out;
3727
3728 if ((r = sshbuf_get_u16(decrypted, &check1)) != 0 ||
3729 (r = sshbuf_get_u16(decrypted, &check2)) != 0)
3730 goto out;
3731 if (check1 != check2) {
3732 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3733 goto out;
3734 }
3735
3736 /* Read the rest of the private key. */
3737 if ((r = sshbuf_get_bignum1(decrypted, prv->rsa->d)) != 0 ||
3738 (r = sshbuf_get_bignum1(decrypted, prv->rsa->iqmp)) != 0 ||
3739 (r = sshbuf_get_bignum1(decrypted, prv->rsa->q)) != 0 ||
3740 (r = sshbuf_get_bignum1(decrypted, prv->rsa->p)) != 0)
3741 goto out;
3742
3743 /* calculate p-1 and q-1 */
3744 if ((r = rsa_generate_additional_parameters(prv->rsa)) != 0)
3745 goto out;
3746
3747 /* enable blinding */
3748 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3749 r = SSH_ERR_LIBCRYPTO_ERROR;
3750 goto out;
3751 }
3752 r = 0;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003753 if (keyp != NULL) {
3754 *keyp = prv;
3755 prv = NULL;
3756 }
Damien Miller86687062014-07-02 15:28:02 +10003757 if (commentp != NULL) {
3758 *commentp = comment;
3759 comment = NULL;
3760 }
3761 out:
djm@openbsd.org4706c1d2016-08-03 05:41:57 +00003762 cipher_free(ciphercontext);
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00003763 free(comment);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00003764 sshkey_free(prv);
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00003765 sshbuf_free(copy);
3766 sshbuf_free(decrypted);
Damien Miller86687062014-07-02 15:28:02 +10003767 return r;
3768}
3769#endif /* WITH_SSH1 */
3770
3771#ifdef WITH_OPENSSL
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00003772static int
Damien Miller86687062014-07-02 15:28:02 +10003773sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00003774 const char *passphrase, struct sshkey **keyp)
Damien Miller86687062014-07-02 15:28:02 +10003775{
3776 EVP_PKEY *pk = NULL;
3777 struct sshkey *prv = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003778 BIO *bio = NULL;
3779 int r;
3780
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003781 if (keyp != NULL)
3782 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003783
3784 if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
3785 return SSH_ERR_ALLOC_FAIL;
3786 if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
3787 (int)sshbuf_len(blob)) {
3788 r = SSH_ERR_ALLOC_FAIL;
3789 goto out;
3790 }
3791
3792 if ((pk = PEM_read_bio_PrivateKey(bio, NULL, NULL,
3793 (char *)passphrase)) == NULL) {
djm@openbsd.org155d5402017-02-10 04:34:50 +00003794 unsigned long pem_err = ERR_peek_last_error();
3795 int pem_reason = ERR_GET_REASON(pem_err);
3796
3797 /*
3798 * Translate OpenSSL error codes to determine whether
3799 * passphrase is required/incorrect.
3800 */
3801 switch (ERR_GET_LIB(pem_err)) {
3802 case ERR_LIB_PEM:
3803 switch (pem_reason) {
3804 case PEM_R_BAD_PASSWORD_READ:
3805 case PEM_R_PROBLEMS_GETTING_PASSWORD:
3806 case PEM_R_BAD_DECRYPT:
3807 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3808 goto out;
3809 default:
3810 r = SSH_ERR_INVALID_FORMAT;
3811 goto out;
3812 }
3813 case ERR_LIB_EVP:
3814 switch (pem_reason) {
3815 case EVP_R_BAD_DECRYPT:
3816 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3817 goto out;
3818 case EVP_R_BN_DECODE_ERROR:
3819 case EVP_R_DECODE_ERROR:
Darren Tuckerbd5d7d22017-02-12 15:45:15 +11003820#ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
djm@openbsd.org155d5402017-02-10 04:34:50 +00003821 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
Darren Tuckerbd5d7d22017-02-12 15:45:15 +11003822#endif
djm@openbsd.org155d5402017-02-10 04:34:50 +00003823 r = SSH_ERR_INVALID_FORMAT;
3824 goto out;
3825 default:
3826 r = SSH_ERR_LIBCRYPTO_ERROR;
3827 goto out;
3828 }
3829 case ERR_LIB_ASN1:
3830 r = SSH_ERR_INVALID_FORMAT;
3831 goto out;
3832 }
3833 r = SSH_ERR_LIBCRYPTO_ERROR;
Damien Miller86687062014-07-02 15:28:02 +10003834 goto out;
3835 }
3836 if (pk->type == EVP_PKEY_RSA &&
3837 (type == KEY_UNSPEC || type == KEY_RSA)) {
3838 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3839 r = SSH_ERR_ALLOC_FAIL;
3840 goto out;
3841 }
3842 prv->rsa = EVP_PKEY_get1_RSA(pk);
3843 prv->type = KEY_RSA;
Damien Miller86687062014-07-02 15:28:02 +10003844#ifdef DEBUG_PK
3845 RSA_print_fp(stderr, prv->rsa, 8);
3846#endif
3847 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3848 r = SSH_ERR_LIBCRYPTO_ERROR;
3849 goto out;
3850 }
3851 } else if (pk->type == EVP_PKEY_DSA &&
3852 (type == KEY_UNSPEC || type == KEY_DSA)) {
3853 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3854 r = SSH_ERR_ALLOC_FAIL;
3855 goto out;
3856 }
3857 prv->dsa = EVP_PKEY_get1_DSA(pk);
3858 prv->type = KEY_DSA;
Damien Miller86687062014-07-02 15:28:02 +10003859#ifdef DEBUG_PK
3860 DSA_print_fp(stderr, prv->dsa, 8);
3861#endif
3862#ifdef OPENSSL_HAS_ECC
3863 } else if (pk->type == EVP_PKEY_EC &&
3864 (type == KEY_UNSPEC || type == KEY_ECDSA)) {
3865 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3866 r = SSH_ERR_ALLOC_FAIL;
3867 goto out;
3868 }
3869 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
3870 prv->type = KEY_ECDSA;
3871 prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
3872 if (prv->ecdsa_nid == -1 ||
3873 sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
3874 sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
3875 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
3876 sshkey_ec_validate_private(prv->ecdsa) != 0) {
3877 r = SSH_ERR_INVALID_FORMAT;
3878 goto out;
3879 }
Damien Miller86687062014-07-02 15:28:02 +10003880# ifdef DEBUG_PK
3881 if (prv != NULL && prv->ecdsa != NULL)
3882 sshkey_dump_ec_key(prv->ecdsa);
3883# endif
3884#endif /* OPENSSL_HAS_ECC */
3885 } else {
3886 r = SSH_ERR_INVALID_FORMAT;
3887 goto out;
3888 }
Damien Miller86687062014-07-02 15:28:02 +10003889 r = 0;
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003890 if (keyp != NULL) {
3891 *keyp = prv;
3892 prv = NULL;
3893 }
Damien Miller86687062014-07-02 15:28:02 +10003894 out:
3895 BIO_free(bio);
3896 if (pk != NULL)
3897 EVP_PKEY_free(pk);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00003898 sshkey_free(prv);
Damien Miller86687062014-07-02 15:28:02 +10003899 return r;
3900}
3901#endif /* WITH_OPENSSL */
3902
3903int
3904sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
3905 const char *passphrase, struct sshkey **keyp, char **commentp)
3906{
djm@openbsd.org155d5402017-02-10 04:34:50 +00003907 int r = SSH_ERR_INTERNAL_ERROR;
3908
djm@openbsd.orgdce19bf2016-04-09 12:39:30 +00003909 if (keyp != NULL)
3910 *keyp = NULL;
Damien Miller86687062014-07-02 15:28:02 +10003911 if (commentp != NULL)
3912 *commentp = NULL;
3913
3914 switch (type) {
markus@openbsd.orgf067cca2015-01-12 13:29:27 +00003915#ifdef WITH_SSH1
Damien Miller86687062014-07-02 15:28:02 +10003916 case KEY_RSA1:
3917 return sshkey_parse_private_rsa1(blob, passphrase,
3918 keyp, commentp);
markus@openbsd.orgf067cca2015-01-12 13:29:27 +00003919#endif /* WITH_SSH1 */
3920#ifdef WITH_OPENSSL
Damien Miller86687062014-07-02 15:28:02 +10003921 case KEY_DSA:
3922 case KEY_ECDSA:
3923 case KEY_RSA:
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00003924 return sshkey_parse_private_pem_fileblob(blob, type,
3925 passphrase, keyp);
Damien Miller86687062014-07-02 15:28:02 +10003926#endif /* WITH_OPENSSL */
3927 case KEY_ED25519:
3928 return sshkey_parse_private2(blob, type, passphrase,
3929 keyp, commentp);
3930 case KEY_UNSPEC:
djm@openbsd.org155d5402017-02-10 04:34:50 +00003931 r = sshkey_parse_private2(blob, type, passphrase, keyp,
3932 commentp);
3933 /* Do not fallback to PEM parser if only passphrase is wrong. */
3934 if (r == 0 || r == SSH_ERR_KEY_WRONG_PASSPHRASE)
3935 return r;
Damien Miller86687062014-07-02 15:28:02 +10003936#ifdef WITH_OPENSSL
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00003937 return sshkey_parse_private_pem_fileblob(blob, type,
3938 passphrase, keyp);
Damien Miller86687062014-07-02 15:28:02 +10003939#else
3940 return SSH_ERR_INVALID_FORMAT;
3941#endif /* WITH_OPENSSL */
3942 default:
3943 return SSH_ERR_KEY_TYPE_UNKNOWN;
3944 }
3945}
3946
3947int
3948sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
tim@openbsd.org3c019a92015-09-13 14:39:16 +00003949 struct sshkey **keyp, char **commentp)
Damien Miller86687062014-07-02 15:28:02 +10003950{
Damien Miller86687062014-07-02 15:28:02 +10003951 if (keyp != NULL)
3952 *keyp = NULL;
3953 if (commentp != NULL)
3954 *commentp = NULL;
3955
3956#ifdef WITH_SSH1
3957 /* it's a SSH v1 key if the public key part is readable */
tim@openbsd.org3c019a92015-09-13 14:39:16 +00003958 if (sshkey_parse_public_rsa1_fileblob(buffer, NULL, NULL) == 0) {
Damien Miller86687062014-07-02 15:28:02 +10003959 return sshkey_parse_private_fileblob_type(buffer, KEY_RSA1,
3960 passphrase, keyp, commentp);
3961 }
3962#endif /* WITH_SSH1 */
tim@openbsd.org3c019a92015-09-13 14:39:16 +00003963 return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
3964 passphrase, keyp, commentp);
Damien Miller86687062014-07-02 15:28:02 +10003965}