1 /* $OpenBSD: sshkey.c,v 1.19 2015/05/21 04:55:51 djm Exp $ */
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/param.h> /* MIN MAX */
31 #include <sys/types.h>
32 #include <netinet/in.h>
35 #include <openssl/evp.h>
36 #include <openssl/err.h>
37 #include <openssl/pem.h>
40 #include "crypto_api.h"
49 #endif /* HAVE_UTIL_H */
58 #define SSHKEY_INTERNAL
62 /* openssh private key file format */
63 #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
64 #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
65 #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
66 #define MARK_END_LEN (sizeof(MARK_END) - 1)
67 #define KDFNAME "bcrypt"
68 #define AUTH_MAGIC "openssh-key-v1"
70 #define DEFAULT_CIPHERNAME "aes256-cbc"
71 #define DEFAULT_ROUNDS 16
73 /* Version identification string for SSH v1 identity files. */
74 #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
76 static int sshkey_from_blob_internal(struct sshbuf *buf,
77 struct sshkey **keyp, int allow_cert);
79 /* Supported key types */
82 const char *shortname;
87 static const struct keytype keytypes[] = {
88 { "ssh-ed25519", "ED25519", KEY_ED25519, 0, 0 },
89 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT",
90 KEY_ED25519_CERT, 0, 1 },
92 { NULL, "RSA1", KEY_RSA1, 0, 0 },
93 { "ssh-rsa", "RSA", KEY_RSA, 0, 0 },
94 { "ssh-dss", "DSA", KEY_DSA, 0, 0 },
95 # ifdef OPENSSL_HAS_ECC
96 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA, NID_X9_62_prime256v1, 0 },
97 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA, NID_secp384r1, 0 },
98 # ifdef OPENSSL_HAS_NISTP521
99 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA, NID_secp521r1, 0 },
100 # endif /* OPENSSL_HAS_NISTP521 */
101 # endif /* OPENSSL_HAS_ECC */
102 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT, 0, 1 },
103 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT, 0, 1 },
104 # ifdef OPENSSL_HAS_ECC
105 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
106 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1 },
107 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
108 KEY_ECDSA_CERT, NID_secp384r1, 1 },
109 # ifdef OPENSSL_HAS_NISTP521
110 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
111 KEY_ECDSA_CERT, NID_secp521r1, 1 },
112 # endif /* OPENSSL_HAS_NISTP521 */
113 # endif /* OPENSSL_HAS_ECC */
114 { "ssh-rsa-cert-v00@openssh.com", "RSA-CERT-V00",
115 KEY_RSA_CERT_V00, 0, 1 },
116 { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
117 KEY_DSA_CERT_V00, 0, 1 },
118 #endif /* WITH_OPENSSL */
119 { NULL, NULL, -1, -1, 0 }
123 sshkey_type(const struct sshkey *k)
125 const struct keytype *kt;
127 for (kt = keytypes; kt->type != -1; kt++) {
128 if (kt->type == k->type)
129 return kt->shortname;
135 sshkey_ssh_name_from_type_nid(int type, int nid)
137 const struct keytype *kt;
139 for (kt = keytypes; kt->type != -1; kt++) {
140 if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
143 return "ssh-unknown";
147 sshkey_type_is_cert(int type)
149 const struct keytype *kt;
151 for (kt = keytypes; kt->type != -1; kt++) {
152 if (kt->type == type)
159 sshkey_ssh_name(const struct sshkey *k)
161 return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
165 sshkey_ssh_name_plain(const struct sshkey *k)
167 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
172 sshkey_type_from_name(const char *name)
174 const struct keytype *kt;
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))
186 sshkey_ecdsa_nid_from_name(const char *name)
188 const struct keytype *kt;
190 for (kt = keytypes; kt->type != -1; kt++) {
191 if (kt->type != KEY_ECDSA && kt->type != KEY_ECDSA_CERT)
193 if (kt->name != NULL && strcmp(name, kt->name) == 0)
200 key_alg_list(int certs_only, int plain_only)
202 char *tmp, *ret = NULL;
203 size_t nlen, rlen = 0;
204 const struct keytype *kt;
206 for (kt = keytypes; kt->type != -1; kt++) {
207 if (kt->name == NULL)
209 if ((certs_only && !kt->cert) || (plain_only && kt->cert))
213 nlen = strlen(kt->name);
214 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
219 memcpy(ret + rlen, kt->name, nlen + 1);
226 sshkey_names_valid2(const char *names, int allow_wildcard)
229 const struct keytype *kt;
232 if (names == NULL || strcmp(names, "") == 0)
234 if ((s = cp = strdup(names)) == NULL)
236 for ((p = strsep(&cp, ",")); p && *p != '\0';
237 (p = strsep(&cp, ","))) {
238 type = sshkey_type_from_name(p);
239 if (type == KEY_RSA1) {
243 if (type == KEY_UNSPEC) {
244 if (allow_wildcard) {
246 * Try matching key types against the string.
247 * If any has a positive or negative match then
248 * the component is accepted.
250 for (kt = keytypes; kt->type != -1; kt++) {
251 if (kt->type == KEY_RSA1)
253 if (match_pattern_list(kt->name,
269 sshkey_size(const struct sshkey *k)
275 case KEY_RSA_CERT_V00:
277 return BN_num_bits(k->rsa->n);
279 case KEY_DSA_CERT_V00:
281 return BN_num_bits(k->dsa->p);
284 return sshkey_curve_nid_to_bits(k->ecdsa_nid);
285 #endif /* WITH_OPENSSL */
287 case KEY_ED25519_CERT:
288 return 256; /* XXX */
294 sshkey_cert_is_legacy(const struct sshkey *k)
297 case KEY_DSA_CERT_V00:
298 case KEY_RSA_CERT_V00:
306 sshkey_type_is_valid_ca(int type)
320 sshkey_is_cert(const struct sshkey *k)
324 return sshkey_type_is_cert(k->type);
327 /* Return the cert-less equivalent to a certified key type */
329 sshkey_type_plain(int type)
332 case KEY_RSA_CERT_V00:
335 case KEY_DSA_CERT_V00:
340 case KEY_ED25519_CERT:
348 /* XXX: these are really begging for a table-driven approach */
350 sshkey_curve_name_to_nid(const char *name)
352 if (strcmp(name, "nistp256") == 0)
353 return NID_X9_62_prime256v1;
354 else if (strcmp(name, "nistp384") == 0)
355 return NID_secp384r1;
356 # ifdef OPENSSL_HAS_NISTP521
357 else if (strcmp(name, "nistp521") == 0)
358 return NID_secp521r1;
359 # endif /* OPENSSL_HAS_NISTP521 */
365 sshkey_curve_nid_to_bits(int nid)
368 case NID_X9_62_prime256v1:
372 # ifdef OPENSSL_HAS_NISTP521
375 # endif /* OPENSSL_HAS_NISTP521 */
382 sshkey_ecdsa_bits_to_nid(int bits)
386 return NID_X9_62_prime256v1;
388 return NID_secp384r1;
389 # ifdef OPENSSL_HAS_NISTP521
391 return NID_secp521r1;
392 # endif /* OPENSSL_HAS_NISTP521 */
399 sshkey_curve_nid_to_name(int nid)
402 case NID_X9_62_prime256v1:
406 # ifdef OPENSSL_HAS_NISTP521
409 # endif /* OPENSSL_HAS_NISTP521 */
416 sshkey_ec_nid_to_hash_alg(int nid)
418 int kbits = sshkey_curve_nid_to_bits(nid);
423 /* RFC5656 section 6.2.1 */
425 return SSH_DIGEST_SHA256;
426 else if (kbits <= 384)
427 return SSH_DIGEST_SHA384;
429 return SSH_DIGEST_SHA512;
431 #endif /* WITH_OPENSSL */
434 cert_free(struct sshkey_cert *cert)
440 if (cert->certblob != NULL)
441 sshbuf_free(cert->certblob);
442 if (cert->critical != NULL)
443 sshbuf_free(cert->critical);
444 if (cert->extensions != NULL)
445 sshbuf_free(cert->extensions);
446 if (cert->key_id != NULL)
448 for (i = 0; i < cert->nprincipals; i++)
449 free(cert->principals[i]);
450 if (cert->principals != NULL)
451 free(cert->principals);
452 if (cert->signature_key != NULL)
453 sshkey_free(cert->signature_key);
454 explicit_bzero(cert, sizeof(*cert));
458 static struct sshkey_cert *
461 struct sshkey_cert *cert;
463 if ((cert = calloc(1, sizeof(*cert))) == NULL)
465 if ((cert->certblob = sshbuf_new()) == NULL ||
466 (cert->critical = sshbuf_new()) == NULL ||
467 (cert->extensions = sshbuf_new()) == NULL) {
472 cert->principals = NULL;
473 cert->signature_key = NULL;
484 #endif /* WITH_OPENSSL */
486 if ((k = calloc(1, sizeof(*k))) == NULL)
494 k->ed25519_sk = NULL;
495 k->ed25519_pk = NULL;
500 case KEY_RSA_CERT_V00:
502 if ((rsa = RSA_new()) == NULL ||
503 (rsa->n = BN_new()) == NULL ||
504 (rsa->e = BN_new()) == NULL) {
513 case KEY_DSA_CERT_V00:
515 if ((dsa = DSA_new()) == NULL ||
516 (dsa->p = BN_new()) == NULL ||
517 (dsa->q = BN_new()) == NULL ||
518 (dsa->g = BN_new()) == NULL ||
519 (dsa->pub_key = BN_new()) == NULL) {
529 /* Cannot do anything until we know the group */
531 #endif /* WITH_OPENSSL */
533 case KEY_ED25519_CERT:
534 /* no need to prealloc */
544 if (sshkey_is_cert(k)) {
545 if ((k->cert = cert_new()) == NULL) {
555 sshkey_add_private(struct sshkey *k)
561 case KEY_RSA_CERT_V00:
563 #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
564 if (bn_maybe_alloc_failed(k->rsa->d) ||
565 bn_maybe_alloc_failed(k->rsa->iqmp) ||
566 bn_maybe_alloc_failed(k->rsa->q) ||
567 bn_maybe_alloc_failed(k->rsa->p) ||
568 bn_maybe_alloc_failed(k->rsa->dmq1) ||
569 bn_maybe_alloc_failed(k->rsa->dmp1))
570 return SSH_ERR_ALLOC_FAIL;
573 case KEY_DSA_CERT_V00:
575 if (bn_maybe_alloc_failed(k->dsa->priv_key))
576 return SSH_ERR_ALLOC_FAIL;
578 #undef bn_maybe_alloc_failed
581 /* Cannot do anything until we know the group */
583 #endif /* WITH_OPENSSL */
585 case KEY_ED25519_CERT:
586 /* no need to prealloc */
591 return SSH_ERR_INVALID_ARGUMENT;
597 sshkey_new_private(int type)
599 struct sshkey *k = sshkey_new(type);
603 if (sshkey_add_private(k) != 0) {
611 sshkey_free(struct sshkey *k)
619 case KEY_RSA_CERT_V00:
626 case KEY_DSA_CERT_V00:
632 # ifdef OPENSSL_HAS_ECC
635 if (k->ecdsa != NULL)
636 EC_KEY_free(k->ecdsa);
639 # endif /* OPENSSL_HAS_ECC */
640 #endif /* WITH_OPENSSL */
642 case KEY_ED25519_CERT:
644 explicit_bzero(k->ed25519_pk, ED25519_PK_SZ);
646 k->ed25519_pk = NULL;
649 explicit_bzero(k->ed25519_sk, ED25519_SK_SZ);
651 k->ed25519_sk = NULL;
659 if (sshkey_is_cert(k))
661 explicit_bzero(k, sizeof(*k));
666 cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
668 if (a == NULL && b == NULL)
670 if (a == NULL || b == NULL)
672 if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
674 if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
675 sshbuf_len(a->certblob)) != 0)
681 * Compare public portions of key only, allowing comparisons between
682 * certificates and plain keys too.
685 sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
687 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
689 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
691 if (a == NULL || b == NULL ||
692 sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
698 case KEY_RSA_CERT_V00:
701 return a->rsa != NULL && b->rsa != NULL &&
702 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
703 BN_cmp(a->rsa->n, b->rsa->n) == 0;
704 case KEY_DSA_CERT_V00:
707 return a->dsa != NULL && b->dsa != NULL &&
708 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
709 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
710 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
711 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
712 # ifdef OPENSSL_HAS_ECC
715 if (a->ecdsa == NULL || b->ecdsa == NULL ||
716 EC_KEY_get0_public_key(a->ecdsa) == NULL ||
717 EC_KEY_get0_public_key(b->ecdsa) == NULL)
719 if ((bnctx = BN_CTX_new()) == NULL)
721 if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
722 EC_KEY_get0_group(b->ecdsa), bnctx) != 0 ||
723 EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
724 EC_KEY_get0_public_key(a->ecdsa),
725 EC_KEY_get0_public_key(b->ecdsa), bnctx) != 0) {
731 # endif /* OPENSSL_HAS_ECC */
732 #endif /* WITH_OPENSSL */
734 case KEY_ED25519_CERT:
735 return a->ed25519_pk != NULL && b->ed25519_pk != NULL &&
736 memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0;
744 sshkey_equal(const struct sshkey *a, const struct sshkey *b)
746 if (a == NULL || b == NULL || a->type != b->type)
748 if (sshkey_is_cert(a)) {
749 if (!cert_compare(a->cert, b->cert))
752 return sshkey_equal_public(a, b);
756 to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain)
758 int type, ret = SSH_ERR_INTERNAL_ERROR;
759 const char *typename;
762 return SSH_ERR_INVALID_ARGUMENT;
764 if (sshkey_is_cert(key)) {
765 if (key->cert == NULL)
766 return SSH_ERR_EXPECTED_CERT;
767 if (sshbuf_len(key->cert->certblob) == 0)
768 return SSH_ERR_KEY_LACKS_CERTBLOB;
770 type = force_plain ? sshkey_type_plain(key->type) : key->type;
771 typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
775 case KEY_DSA_CERT_V00:
776 case KEY_RSA_CERT_V00:
780 #endif /* WITH_OPENSSL */
781 case KEY_ED25519_CERT:
782 /* Use the existing blob */
783 /* XXX modified flag? */
784 if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
789 if (key->dsa == NULL)
790 return SSH_ERR_INVALID_ARGUMENT;
791 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
792 (ret = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
793 (ret = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
794 (ret = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
795 (ret = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0)
798 # ifdef OPENSSL_HAS_ECC
800 if (key->ecdsa == NULL)
801 return SSH_ERR_INVALID_ARGUMENT;
802 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
803 (ret = sshbuf_put_cstring(b,
804 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
805 (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0)
810 if (key->rsa == NULL)
811 return SSH_ERR_INVALID_ARGUMENT;
812 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
813 (ret = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
814 (ret = sshbuf_put_bignum2(b, key->rsa->n)) != 0)
817 #endif /* WITH_OPENSSL */
819 if (key->ed25519_pk == NULL)
820 return SSH_ERR_INVALID_ARGUMENT;
821 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
822 (ret = sshbuf_put_string(b,
823 key->ed25519_pk, ED25519_PK_SZ)) != 0)
827 return SSH_ERR_KEY_TYPE_UNKNOWN;
833 sshkey_putb(const struct sshkey *key, struct sshbuf *b)
835 return to_blob_buf(key, b, 0);
839 sshkey_puts(const struct sshkey *key, struct sshbuf *b)
844 if ((tmp = sshbuf_new()) == NULL)
845 return SSH_ERR_ALLOC_FAIL;
846 r = to_blob_buf(key, tmp, 0);
848 r = sshbuf_put_stringb(b, tmp);
854 sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
856 return to_blob_buf(key, b, 1);
860 to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain)
862 int ret = SSH_ERR_INTERNAL_ERROR;
864 struct sshbuf *b = NULL;
870 if ((b = sshbuf_new()) == NULL)
871 return SSH_ERR_ALLOC_FAIL;
872 if ((ret = to_blob_buf(key, b, force_plain)) != 0)
878 if ((*blobp = malloc(len)) == NULL) {
879 ret = SSH_ERR_ALLOC_FAIL;
882 memcpy(*blobp, sshbuf_ptr(b), len);
891 sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
893 return to_blob(key, blobp, lenp, 0);
897 sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
899 return to_blob(key, blobp, lenp, 1);
903 sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
904 u_char **retp, size_t *lenp)
906 u_char *blob = NULL, *ret = NULL;
908 int r = SSH_ERR_INTERNAL_ERROR;
914 if (ssh_digest_bytes(dgst_alg) == 0) {
915 r = SSH_ERR_INVALID_ARGUMENT;
919 if (k->type == KEY_RSA1) {
921 int nlen = BN_num_bytes(k->rsa->n);
922 int elen = BN_num_bytes(k->rsa->e);
924 blob_len = nlen + elen;
925 if (nlen >= INT_MAX - elen ||
926 (blob = malloc(blob_len)) == NULL) {
927 r = SSH_ERR_ALLOC_FAIL;
930 BN_bn2bin(k->rsa->n, blob);
931 BN_bn2bin(k->rsa->e, blob + nlen);
932 #endif /* WITH_OPENSSL */
933 } else if ((r = to_blob(k, &blob, &blob_len, 1)) != 0)
935 if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
936 r = SSH_ERR_ALLOC_FAIL;
939 if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
940 ret, SSH_DIGEST_MAX_LENGTH)) != 0)
948 *lenp = ssh_digest_bytes(dgst_alg);
953 explicit_bzero(blob, blob_len);
960 fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
963 size_t plen = strlen(alg) + 1;
964 size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
967 if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
969 strlcpy(ret, alg, rlen);
970 strlcat(ret, ":", rlen);
971 if (dgst_raw_len == 0)
973 if ((r = b64_ntop(dgst_raw, dgst_raw_len,
974 ret + plen, rlen - plen)) == -1) {
975 explicit_bzero(ret, rlen);
979 /* Trim padding characters from end */
980 ret[strcspn(ret, "=")] = '\0';
985 fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
987 char *retval, hex[5];
988 size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
990 if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
992 strlcpy(retval, alg, rlen);
993 strlcat(retval, ":", rlen);
994 for (i = 0; i < dgst_raw_len; i++) {
995 snprintf(hex, sizeof(hex), "%s%02x",
996 i > 0 ? ":" : "", dgst_raw[i]);
997 strlcat(retval, hex, rlen);
1003 fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
1005 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
1006 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
1007 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1008 u_int i, j = 0, rounds, seed = 1;
1011 rounds = (dgst_raw_len / 2) + 1;
1012 if ((retval = calloc(rounds, 6)) == NULL)
1015 for (i = 0; i < rounds; i++) {
1016 u_int idx0, idx1, idx2, idx3, idx4;
1017 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
1018 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1020 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
1021 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1023 retval[j++] = vowels[idx0];
1024 retval[j++] = consonants[idx1];
1025 retval[j++] = vowels[idx2];
1026 if ((i + 1) < rounds) {
1027 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
1028 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
1029 retval[j++] = consonants[idx3];
1031 retval[j++] = consonants[idx4];
1032 seed = ((seed * 5) +
1033 ((((u_int)(dgst_raw[2 * i])) * 7) +
1034 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1040 retval[j++] = vowels[idx0];
1041 retval[j++] = consonants[idx1];
1042 retval[j++] = vowels[idx2];
1051 * Draw an ASCII-Art representing the fingerprint so human brain can
1052 * profit from its built-in pattern recognition ability.
1053 * This technique is called "random art" and can be found in some
1054 * scientific publications like this original paper:
1056 * "Hash Visualization: a New Technique to improve Real-World Security",
1057 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
1058 * Techniques and E-Commerce (CrypTEC '99)
1059 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
1061 * The subject came up in a talk by Dan Kaminsky, too.
1063 * If you see the picture is different, the key is different.
1064 * If the picture looks the same, you still know nothing.
1066 * The algorithm used here is a worm crawling over a discrete plane,
1067 * leaving a trace (augmenting the field) everywhere it goes.
1068 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
1069 * makes the respective movement vector be ignored for this turn.
1070 * Graphs are not unambiguous, because circles in graphs can be
1071 * walked in either direction.
1075 * Field sizes for the random art. Have to be odd, so the starting point
1076 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1077 * Else pictures would be too dense, and drawing the frame would
1078 * fail, too, because the key type would not fit in anymore.
1081 #define FLDSIZE_Y (FLDBASE + 1)
1082 #define FLDSIZE_X (FLDBASE * 2 + 1)
1084 fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
1085 const struct sshkey *k)
1088 * Chars to be used after each other every time the worm
1089 * intersects with itself. Matter of taste.
1091 char *augmentation_string = " .o+=*BOX@%&#/^SE";
1092 char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
1093 u_char field[FLDSIZE_X][FLDSIZE_Y];
1094 size_t i, tlen, hlen;
1097 size_t len = strlen(augmentation_string) - 1;
1099 if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1102 /* initialize field */
1103 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1107 /* process raw key */
1108 for (i = 0; i < dgst_raw_len; i++) {
1110 /* each byte conveys four 2-bit move commands */
1111 input = dgst_raw[i];
1112 for (b = 0; b < 4; b++) {
1113 /* evaluate 2 bit, rest is shifted later */
1114 x += (input & 0x1) ? 1 : -1;
1115 y += (input & 0x2) ? 1 : -1;
1117 /* assure we are still in bounds */
1120 x = MIN(x, FLDSIZE_X - 1);
1121 y = MIN(y, FLDSIZE_Y - 1);
1123 /* augment the field */
1124 if (field[x][y] < len - 2)
1130 /* mark starting point and end point*/
1131 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1134 /* assemble title */
1135 r = snprintf(title, sizeof(title), "[%s %u]",
1136 sshkey_type(k), sshkey_size(k));
1137 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1138 if (r < 0 || r > (int)sizeof(title))
1139 r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1140 tlen = (r <= 0) ? 0 : strlen(title);
1142 /* assemble hash ID. */
1143 r = snprintf(hash, sizeof(hash), "[%s]", alg);
1144 hlen = (r <= 0) ? 0 : strlen(hash);
1146 /* output upper border */
1149 for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1151 memcpy(p, title, tlen);
1153 for (i += tlen; i < FLDSIZE_X; i++)
1158 /* output content */
1159 for (y = 0; y < FLDSIZE_Y; y++) {
1161 for (x = 0; x < FLDSIZE_X; x++)
1162 *p++ = augmentation_string[MIN(field[x][y], len)];
1167 /* output lower border */
1169 for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1171 memcpy(p, hash, hlen);
1173 for (i += hlen; i < FLDSIZE_X; i++)
1181 sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
1182 enum sshkey_fp_rep dgst_rep)
1184 char *retval = NULL;
1186 size_t dgst_raw_len;
1188 if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
1191 case SSH_FP_DEFAULT:
1192 if (dgst_alg == SSH_DIGEST_MD5) {
1193 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1194 dgst_raw, dgst_raw_len);
1196 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1197 dgst_raw, dgst_raw_len);
1201 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1202 dgst_raw, dgst_raw_len);
1205 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1206 dgst_raw, dgst_raw_len);
1208 case SSH_FP_BUBBLEBABBLE:
1209 retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1211 case SSH_FP_RANDOMART:
1212 retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1213 dgst_raw, dgst_raw_len, k);
1216 explicit_bzero(dgst_raw, dgst_raw_len);
1220 explicit_bzero(dgst_raw, dgst_raw_len);
1227 * Reads a multiple-precision integer in decimal from the buffer, and advances
1228 * the pointer. The integer must already be initialized. This function is
1229 * permitted to modify the buffer. This leaves *cpp to point just beyond the
1230 * last processed character.
1233 read_decimal_bignum(char **cpp, BIGNUM *v)
1237 int skip = 1; /* skip white space */
1240 while (*cp == ' ' || *cp == '\t')
1242 e = strspn(cp, "0123456789");
1244 return SSH_ERR_INVALID_FORMAT;
1245 if (e > SSHBUF_MAX_BIGNUM * 3)
1246 return SSH_ERR_BIGNUM_TOO_LARGE;
1249 else if (index(" \t\r\n", cp[e]) == NULL)
1250 return SSH_ERR_INVALID_FORMAT;
1252 if (BN_dec2bn(&v, cp) <= 0)
1253 return SSH_ERR_INVALID_FORMAT;
1254 *cpp = cp + e + skip;
1257 #endif /* WITH_SSH1 */
1259 /* returns 0 ok, and < 0 error */
1261 sshkey_read(struct sshkey *ret, char **cpp)
1264 int retval = SSH_ERR_INVALID_FORMAT;
1266 int r, type, curve_nid = -1;
1267 struct sshbuf *blob;
1271 #endif /* WITH_SSH1 */
1275 switch (ret->type) {
1278 /* Get number of bits. */
1279 bits = strtoul(cp, &ep, 10);
1280 if (*cp == '\0' || index(" \t\r\n", *ep) == NULL ||
1281 bits == 0 || bits > SSHBUF_MAX_BIGNUM * 8)
1282 return SSH_ERR_INVALID_FORMAT; /* Bad bit count... */
1283 /* Get public exponent, public modulus. */
1284 if ((r = read_decimal_bignum(&ep, ret->rsa->e)) < 0)
1286 if ((r = read_decimal_bignum(&ep, ret->rsa->n)) < 0)
1289 /* validate the claimed number of bits */
1290 if (BN_num_bits(ret->rsa->n) != (int)bits)
1291 return SSH_ERR_KEY_BITS_MISMATCH;
1293 #endif /* WITH_SSH1 */
1300 case KEY_DSA_CERT_V00:
1301 case KEY_RSA_CERT_V00:
1303 case KEY_ECDSA_CERT:
1305 case KEY_ED25519_CERT:
1306 space = strchr(cp, ' ');
1308 return SSH_ERR_INVALID_FORMAT;
1310 type = sshkey_type_from_name(cp);
1311 if (sshkey_type_plain(type) == KEY_ECDSA &&
1312 (curve_nid = sshkey_ecdsa_nid_from_name(cp)) == -1)
1313 return SSH_ERR_EC_CURVE_INVALID;
1315 if (type == KEY_UNSPEC)
1316 return SSH_ERR_INVALID_FORMAT;
1319 return SSH_ERR_INVALID_FORMAT;
1320 if (ret->type != KEY_UNSPEC && ret->type != type)
1321 return SSH_ERR_KEY_TYPE_MISMATCH;
1322 if ((blob = sshbuf_new()) == NULL)
1323 return SSH_ERR_ALLOC_FAIL;
1325 space = strchr(cp, ' ');
1327 /* advance 'space': skip whitespace */
1329 while (*space == ' ' || *space == '\t')
1333 *cpp = cp + strlen(cp);
1334 if ((r = sshbuf_b64tod(blob, cp)) != 0) {
1338 if ((r = sshkey_from_blob(sshbuf_ptr(blob),
1339 sshbuf_len(blob), &k)) != 0) {
1344 if (k->type != type) {
1346 return SSH_ERR_KEY_TYPE_MISMATCH;
1348 if (sshkey_type_plain(type) == KEY_ECDSA &&
1349 curve_nid != k->ecdsa_nid) {
1351 return SSH_ERR_EC_CURVE_MISMATCH;
1354 if (sshkey_is_cert(ret)) {
1355 if (!sshkey_is_cert(k)) {
1357 return SSH_ERR_EXPECTED_CERT;
1359 if (ret->cert != NULL)
1360 cert_free(ret->cert);
1361 ret->cert = k->cert;
1365 if (sshkey_type_plain(ret->type) == KEY_RSA) {
1366 if (ret->rsa != NULL)
1371 RSA_print_fp(stderr, ret->rsa, 8);
1374 if (sshkey_type_plain(ret->type) == KEY_DSA) {
1375 if (ret->dsa != NULL)
1380 DSA_print_fp(stderr, ret->dsa, 8);
1383 # ifdef OPENSSL_HAS_ECC
1384 if (sshkey_type_plain(ret->type) == KEY_ECDSA) {
1385 if (ret->ecdsa != NULL)
1386 EC_KEY_free(ret->ecdsa);
1387 ret->ecdsa = k->ecdsa;
1388 ret->ecdsa_nid = k->ecdsa_nid;
1392 sshkey_dump_ec_key(ret->ecdsa);
1395 # endif /* OPENSSL_HAS_ECC */
1396 #endif /* WITH_OPENSSL */
1397 if (sshkey_type_plain(ret->type) == KEY_ED25519) {
1398 free(ret->ed25519_pk);
1399 ret->ed25519_pk = k->ed25519_pk;
1400 k->ed25519_pk = NULL;
1412 return SSH_ERR_INVALID_ARGUMENT;
1418 sshkey_to_base64(const struct sshkey *key, char **b64p)
1420 int r = SSH_ERR_INTERNAL_ERROR;
1421 struct sshbuf *b = NULL;
1426 if ((b = sshbuf_new()) == NULL)
1427 return SSH_ERR_ALLOC_FAIL;
1428 if ((r = sshkey_putb(key, b)) != 0)
1430 if ((uu = sshbuf_dtob64(b)) == NULL) {
1431 r = SSH_ERR_ALLOC_FAIL;
1447 sshkey_format_rsa1(const struct sshkey *key, struct sshbuf *b)
1449 int r = SSH_ERR_INTERNAL_ERROR;
1452 char *dec_e = NULL, *dec_n = NULL;
1454 if (key->rsa == NULL || key->rsa->e == NULL ||
1455 key->rsa->n == NULL) {
1456 r = SSH_ERR_INVALID_ARGUMENT;
1459 if ((dec_e = BN_bn2dec(key->rsa->e)) == NULL ||
1460 (dec_n = BN_bn2dec(key->rsa->n)) == NULL) {
1461 r = SSH_ERR_ALLOC_FAIL;
1464 /* size of modulus 'n' */
1465 if ((bits = BN_num_bits(key->rsa->n)) <= 0) {
1466 r = SSH_ERR_INVALID_ARGUMENT;
1469 if ((r = sshbuf_putf(b, "%u %s %s", bits, dec_e, dec_n)) != 0)
1476 OPENSSL_free(dec_e);
1478 OPENSSL_free(dec_n);
1479 #endif /* WITH_SSH1 */
1485 sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1487 int r = SSH_ERR_INTERNAL_ERROR;
1490 if (key->type == KEY_RSA1) {
1491 if ((r = sshkey_format_rsa1(key, b)) != 0)
1494 /* Unsupported key types handled in sshkey_to_base64() */
1495 if ((r = sshkey_to_base64(key, &uu)) != 0)
1497 if ((r = sshbuf_putf(b, "%s %s",
1498 sshkey_ssh_name(key), uu)) != 0)
1508 sshkey_write(const struct sshkey *key, FILE *f)
1510 struct sshbuf *b = NULL;
1511 int r = SSH_ERR_INTERNAL_ERROR;
1513 if ((b = sshbuf_new()) == NULL)
1514 return SSH_ERR_ALLOC_FAIL;
1515 if ((r = sshkey_format_text(key, b)) != 0)
1517 if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1520 r = SSH_ERR_SYSTEM_ERROR;
1531 sshkey_cert_type(const struct sshkey *k)
1533 switch (k->cert->type) {
1534 case SSH2_CERT_TYPE_USER:
1536 case SSH2_CERT_TYPE_HOST:
1545 rsa_generate_private_key(u_int bits, RSA **rsap)
1547 RSA *private = NULL;
1549 int ret = SSH_ERR_INTERNAL_ERROR;
1552 bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1553 bits > SSHBUF_MAX_BIGNUM * 8)
1554 return SSH_ERR_INVALID_ARGUMENT;
1556 if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
1557 ret = SSH_ERR_ALLOC_FAIL;
1560 if (!BN_set_word(f4, RSA_F4) ||
1561 !RSA_generate_key_ex(private, bits, f4, NULL)) {
1562 ret = SSH_ERR_LIBCRYPTO_ERROR;
1569 if (private != NULL)
1577 dsa_generate_private_key(u_int bits, DSA **dsap)
1580 int ret = SSH_ERR_INTERNAL_ERROR;
1582 if (dsap == NULL || bits != 1024)
1583 return SSH_ERR_INVALID_ARGUMENT;
1584 if ((private = DSA_new()) == NULL) {
1585 ret = SSH_ERR_ALLOC_FAIL;
1589 if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
1590 NULL, NULL) || !DSA_generate_key(private)) {
1592 ret = SSH_ERR_LIBCRYPTO_ERROR;
1599 if (private != NULL)
1604 # ifdef OPENSSL_HAS_ECC
1606 sshkey_ecdsa_key_to_nid(EC_KEY *k)
1610 NID_X9_62_prime256v1,
1612 # ifdef OPENSSL_HAS_NISTP521
1614 # endif /* OPENSSL_HAS_NISTP521 */
1620 const EC_GROUP *g = EC_KEY_get0_group(k);
1623 * The group may be stored in a ASN.1 encoded private key in one of two
1624 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1625 * or explicit group parameters encoded into the key blob. Only the
1626 * "named group" case sets the group NID for us, but we can figure
1627 * it out for the other case by comparing against all the groups that
1630 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1632 if ((bnctx = BN_CTX_new()) == NULL)
1634 for (i = 0; nids[i] != -1; i++) {
1635 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL) {
1639 if (EC_GROUP_cmp(g, eg, bnctx) == 0)
1644 if (nids[i] != -1) {
1645 /* Use the group with the NID attached */
1646 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1647 if (EC_KEY_set_group(k, eg) != 1) {
1656 ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap)
1659 int ret = SSH_ERR_INTERNAL_ERROR;
1661 if (nid == NULL || ecdsap == NULL ||
1662 (*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
1663 return SSH_ERR_INVALID_ARGUMENT;
1665 if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) {
1666 ret = SSH_ERR_ALLOC_FAIL;
1669 if (EC_KEY_generate_key(private) != 1) {
1670 ret = SSH_ERR_LIBCRYPTO_ERROR;
1673 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
1678 if (private != NULL)
1679 EC_KEY_free(private);
1682 # endif /* OPENSSL_HAS_ECC */
1683 #endif /* WITH_OPENSSL */
1686 sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1689 int ret = SSH_ERR_INTERNAL_ERROR;
1692 return SSH_ERR_INVALID_ARGUMENT;
1694 if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1695 return SSH_ERR_ALLOC_FAIL;
1698 if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
1699 (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) {
1700 ret = SSH_ERR_ALLOC_FAIL;
1703 crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
1708 ret = dsa_generate_private_key(bits, &k->dsa);
1710 # ifdef OPENSSL_HAS_ECC
1712 ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid,
1715 # endif /* OPENSSL_HAS_ECC */
1718 ret = rsa_generate_private_key(bits, &k->rsa);
1720 #endif /* WITH_OPENSSL */
1722 ret = SSH_ERR_INVALID_ARGUMENT;
1733 sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1736 const struct sshkey_cert *from;
1737 struct sshkey_cert *to;
1738 int ret = SSH_ERR_INTERNAL_ERROR;
1740 if (to_key->cert != NULL) {
1741 cert_free(to_key->cert);
1742 to_key->cert = NULL;
1745 if ((from = from_key->cert) == NULL)
1746 return SSH_ERR_INVALID_ARGUMENT;
1748 if ((to = to_key->cert = cert_new()) == NULL)
1749 return SSH_ERR_ALLOC_FAIL;
1751 if ((ret = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
1752 (ret = sshbuf_putb(to->critical, from->critical)) != 0 ||
1753 (ret = sshbuf_putb(to->extensions, from->extensions) != 0))
1756 to->serial = from->serial;
1757 to->type = from->type;
1758 if (from->key_id == NULL)
1760 else if ((to->key_id = strdup(from->key_id)) == NULL)
1761 return SSH_ERR_ALLOC_FAIL;
1762 to->valid_after = from->valid_after;
1763 to->valid_before = from->valid_before;
1764 if (from->signature_key == NULL)
1765 to->signature_key = NULL;
1766 else if ((ret = sshkey_from_private(from->signature_key,
1767 &to->signature_key)) != 0)
1770 if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS)
1771 return SSH_ERR_INVALID_ARGUMENT;
1772 if (from->nprincipals > 0) {
1773 if ((to->principals = calloc(from->nprincipals,
1774 sizeof(*to->principals))) == NULL)
1775 return SSH_ERR_ALLOC_FAIL;
1776 for (i = 0; i < from->nprincipals; i++) {
1777 to->principals[i] = strdup(from->principals[i]);
1778 if (to->principals[i] == NULL) {
1779 to->nprincipals = i;
1780 return SSH_ERR_ALLOC_FAIL;
1784 to->nprincipals = from->nprincipals;
1789 sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1791 struct sshkey *n = NULL;
1792 int ret = SSH_ERR_INTERNAL_ERROR;
1800 case KEY_DSA_CERT_V00:
1802 if ((n = sshkey_new(k->type)) == NULL)
1803 return SSH_ERR_ALLOC_FAIL;
1804 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
1805 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
1806 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
1807 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) {
1809 return SSH_ERR_ALLOC_FAIL;
1812 # ifdef OPENSSL_HAS_ECC
1814 case KEY_ECDSA_CERT:
1815 if ((n = sshkey_new(k->type)) == NULL)
1816 return SSH_ERR_ALLOC_FAIL;
1817 n->ecdsa_nid = k->ecdsa_nid;
1818 n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
1819 if (n->ecdsa == NULL) {
1821 return SSH_ERR_ALLOC_FAIL;
1823 if (EC_KEY_set_public_key(n->ecdsa,
1824 EC_KEY_get0_public_key(k->ecdsa)) != 1) {
1826 return SSH_ERR_LIBCRYPTO_ERROR;
1829 # endif /* OPENSSL_HAS_ECC */
1832 case KEY_RSA_CERT_V00:
1834 if ((n = sshkey_new(k->type)) == NULL)
1835 return SSH_ERR_ALLOC_FAIL;
1836 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
1837 (BN_copy(n->rsa->e, k->rsa->e) == NULL)) {
1839 return SSH_ERR_ALLOC_FAIL;
1842 #endif /* WITH_OPENSSL */
1844 case KEY_ED25519_CERT:
1845 if ((n = sshkey_new(k->type)) == NULL)
1846 return SSH_ERR_ALLOC_FAIL;
1847 if (k->ed25519_pk != NULL) {
1848 if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
1850 return SSH_ERR_ALLOC_FAIL;
1852 memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
1856 return SSH_ERR_KEY_TYPE_UNKNOWN;
1858 if (sshkey_is_cert(k)) {
1859 if ((ret = sshkey_cert_copy(k, n)) != 0) {
1869 cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
1871 struct sshbuf *principals = NULL, *crit = NULL;
1872 struct sshbuf *exts = NULL, *ca = NULL;
1874 size_t signed_len = 0, slen = 0, kidlen = 0;
1875 int ret = SSH_ERR_INTERNAL_ERROR;
1876 int v00 = sshkey_cert_is_legacy(key);
1878 /* Copy the entire key blob for verification and later serialisation */
1879 if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
1882 if ((!v00 && (ret = sshbuf_get_u64(b, &key->cert->serial)) != 0) ||
1883 (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
1884 (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
1885 (ret = sshbuf_froms(b, &principals)) != 0 ||
1886 (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
1887 (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
1888 (ret = sshbuf_froms(b, &crit)) != 0 ||
1889 (!v00 && (ret = sshbuf_froms(b, &exts)) != 0) ||
1890 (v00 && (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0) ||
1891 (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
1892 (ret = sshbuf_froms(b, &ca)) != 0) {
1893 /* XXX debug print error for ret */
1894 ret = SSH_ERR_INVALID_FORMAT;
1898 /* Signature is left in the buffer so we can calculate this length */
1899 signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
1901 if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
1902 ret = SSH_ERR_INVALID_FORMAT;
1906 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1907 key->cert->type != SSH2_CERT_TYPE_HOST) {
1908 ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
1912 /* Parse principals section */
1913 while (sshbuf_len(principals) > 0) {
1914 char *principal = NULL;
1915 char **oprincipals = NULL;
1917 if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
1918 ret = SSH_ERR_INVALID_FORMAT;
1921 if ((ret = sshbuf_get_cstring(principals, &principal,
1923 ret = SSH_ERR_INVALID_FORMAT;
1926 oprincipals = key->cert->principals;
1927 key->cert->principals = realloc(key->cert->principals,
1928 (key->cert->nprincipals + 1) *
1929 sizeof(*key->cert->principals));
1930 if (key->cert->principals == NULL) {
1932 key->cert->principals = oprincipals;
1933 ret = SSH_ERR_ALLOC_FAIL;
1936 key->cert->principals[key->cert->nprincipals++] = principal;
1940 * Stash a copies of the critical options and extensions sections
1943 if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
1945 (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
1949 * Validate critical options and extensions sections format.
1950 * NB. extensions are not present in v00 certs.
1952 while (sshbuf_len(crit) != 0) {
1953 if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
1954 (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
1955 sshbuf_reset(key->cert->critical);
1956 ret = SSH_ERR_INVALID_FORMAT;
1960 while (exts != NULL && sshbuf_len(exts) != 0) {
1961 if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
1962 (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
1963 sshbuf_reset(key->cert->extensions);
1964 ret = SSH_ERR_INVALID_FORMAT;
1969 /* Parse CA key and check signature */
1970 if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
1971 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1974 if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
1975 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1978 if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
1979 sshbuf_ptr(key->cert->certblob), signed_len, 0)) != 0)
1988 sshbuf_free(principals);
1994 sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
1997 int type, ret = SSH_ERR_INTERNAL_ERROR;
1998 char *ktype = NULL, *curve = NULL;
1999 struct sshkey *key = NULL;
2002 struct sshbuf *copy;
2003 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2005 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2007 #ifdef DEBUG_PK /* XXX */
2008 sshbuf_dump(b, stderr);
2011 if ((copy = sshbuf_fromb(b)) == NULL) {
2012 ret = SSH_ERR_ALLOC_FAIL;
2015 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
2016 ret = SSH_ERR_INVALID_FORMAT;
2020 type = sshkey_type_from_name(ktype);
2021 if (!allow_cert && sshkey_type_is_cert(type)) {
2022 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2029 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2030 ret = SSH_ERR_INVALID_FORMAT;
2035 case KEY_RSA_CERT_V00:
2036 if ((key = sshkey_new(type)) == NULL) {
2037 ret = SSH_ERR_ALLOC_FAIL;
2040 if (sshbuf_get_bignum2(b, key->rsa->e) != 0 ||
2041 sshbuf_get_bignum2(b, key->rsa->n) != 0) {
2042 ret = SSH_ERR_INVALID_FORMAT;
2046 RSA_print_fp(stderr, key->rsa, 8);
2051 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2052 ret = SSH_ERR_INVALID_FORMAT;
2057 case KEY_DSA_CERT_V00:
2058 if ((key = sshkey_new(type)) == NULL) {
2059 ret = SSH_ERR_ALLOC_FAIL;
2062 if (sshbuf_get_bignum2(b, key->dsa->p) != 0 ||
2063 sshbuf_get_bignum2(b, key->dsa->q) != 0 ||
2064 sshbuf_get_bignum2(b, key->dsa->g) != 0 ||
2065 sshbuf_get_bignum2(b, key->dsa->pub_key) != 0) {
2066 ret = SSH_ERR_INVALID_FORMAT;
2070 DSA_print_fp(stderr, key->dsa, 8);
2073 case KEY_ECDSA_CERT:
2075 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2076 ret = SSH_ERR_INVALID_FORMAT;
2080 # ifdef OPENSSL_HAS_ECC
2082 if ((key = sshkey_new(type)) == NULL) {
2083 ret = SSH_ERR_ALLOC_FAIL;
2086 key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype);
2087 if (sshbuf_get_cstring(b, &curve, NULL) != 0) {
2088 ret = SSH_ERR_INVALID_FORMAT;
2091 if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
2092 ret = SSH_ERR_EC_CURVE_MISMATCH;
2095 if (key->ecdsa != NULL)
2096 EC_KEY_free(key->ecdsa);
2097 if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
2099 ret = SSH_ERR_EC_CURVE_INVALID;
2102 if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) {
2103 ret = SSH_ERR_ALLOC_FAIL;
2106 if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) {
2107 ret = SSH_ERR_INVALID_FORMAT;
2110 if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
2112 ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2115 if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
2116 /* XXX assume it is a allocation error */
2117 ret = SSH_ERR_ALLOC_FAIL;
2121 sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
2124 # endif /* OPENSSL_HAS_ECC */
2125 #endif /* WITH_OPENSSL */
2126 case KEY_ED25519_CERT:
2128 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2129 ret = SSH_ERR_INVALID_FORMAT;
2134 if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
2136 if (len != ED25519_PK_SZ) {
2137 ret = SSH_ERR_INVALID_FORMAT;
2140 if ((key = sshkey_new(type)) == NULL) {
2141 ret = SSH_ERR_ALLOC_FAIL;
2144 key->ed25519_pk = pk;
2148 if ((key = sshkey_new(type)) == NULL) {
2149 ret = SSH_ERR_ALLOC_FAIL;
2154 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
2158 /* Parse certificate potion */
2159 if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
2162 if (key != NULL && sshbuf_len(b) != 0) {
2163 ret = SSH_ERR_INVALID_FORMAT;
2175 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2178 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2183 sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
2188 if ((b = sshbuf_from(blob, blen)) == NULL)
2189 return SSH_ERR_ALLOC_FAIL;
2190 r = sshkey_from_blob_internal(b, keyp, 1);
2196 sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
2198 return sshkey_from_blob_internal(b, keyp, 1);
2202 sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
2207 if ((r = sshbuf_froms(buf, &b)) != 0)
2209 r = sshkey_from_blob_internal(b, keyp, 1);
2215 sshkey_sign(const struct sshkey *key,
2216 u_char **sigp, size_t *lenp,
2217 const u_char *data, size_t datalen, u_int compat)
2223 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2224 return SSH_ERR_INVALID_ARGUMENT;
2225 switch (key->type) {
2227 case KEY_DSA_CERT_V00:
2230 return ssh_dss_sign(key, sigp, lenp, data, datalen, compat);
2231 # ifdef OPENSSL_HAS_ECC
2232 case KEY_ECDSA_CERT:
2234 return ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat);
2235 # endif /* OPENSSL_HAS_ECC */
2236 case KEY_RSA_CERT_V00:
2239 return ssh_rsa_sign(key, sigp, lenp, data, datalen, compat);
2240 #endif /* WITH_OPENSSL */
2242 case KEY_ED25519_CERT:
2243 return ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat);
2245 return SSH_ERR_KEY_TYPE_UNKNOWN;
2250 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2253 sshkey_verify(const struct sshkey *key,
2254 const u_char *sig, size_t siglen,
2255 const u_char *data, size_t dlen, u_int compat)
2257 if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2258 return SSH_ERR_INVALID_ARGUMENT;
2259 switch (key->type) {
2261 case KEY_DSA_CERT_V00:
2264 return ssh_dss_verify(key, sig, siglen, data, dlen, compat);
2265 # ifdef OPENSSL_HAS_ECC
2266 case KEY_ECDSA_CERT:
2268 return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat);
2269 # endif /* OPENSSL_HAS_ECC */
2270 case KEY_RSA_CERT_V00:
2273 return ssh_rsa_verify(key, sig, siglen, data, dlen, compat);
2274 #endif /* WITH_OPENSSL */
2276 case KEY_ED25519_CERT:
2277 return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
2279 return SSH_ERR_KEY_TYPE_UNKNOWN;
2283 /* Converts a private to a public key */
2285 sshkey_demote(const struct sshkey *k, struct sshkey **dkp)
2288 int ret = SSH_ERR_INTERNAL_ERROR;
2293 if ((pk = calloc(1, sizeof(*pk))) == NULL)
2294 return SSH_ERR_ALLOC_FAIL;
2296 pk->flags = k->flags;
2297 pk->ecdsa_nid = k->ecdsa_nid;
2301 pk->ed25519_pk = NULL;
2302 pk->ed25519_sk = NULL;
2306 case KEY_RSA_CERT_V00:
2308 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2313 if ((pk->rsa = RSA_new()) == NULL ||
2314 (pk->rsa->e = BN_dup(k->rsa->e)) == NULL ||
2315 (pk->rsa->n = BN_dup(k->rsa->n)) == NULL) {
2316 ret = SSH_ERR_ALLOC_FAIL;
2320 case KEY_DSA_CERT_V00:
2322 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2326 if ((pk->dsa = DSA_new()) == NULL ||
2327 (pk->dsa->p = BN_dup(k->dsa->p)) == NULL ||
2328 (pk->dsa->q = BN_dup(k->dsa->q)) == NULL ||
2329 (pk->dsa->g = BN_dup(k->dsa->g)) == NULL ||
2330 (pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) {
2331 ret = SSH_ERR_ALLOC_FAIL;
2335 case KEY_ECDSA_CERT:
2336 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2339 # ifdef OPENSSL_HAS_ECC
2341 pk->ecdsa = EC_KEY_new_by_curve_name(pk->ecdsa_nid);
2342 if (pk->ecdsa == NULL) {
2343 ret = SSH_ERR_ALLOC_FAIL;
2346 if (EC_KEY_set_public_key(pk->ecdsa,
2347 EC_KEY_get0_public_key(k->ecdsa)) != 1) {
2348 ret = SSH_ERR_LIBCRYPTO_ERROR;
2352 # endif /* OPENSSL_HAS_ECC */
2353 #endif /* WITH_OPENSSL */
2354 case KEY_ED25519_CERT:
2355 if ((ret = sshkey_cert_copy(k, pk)) != 0)
2359 if (k->ed25519_pk != NULL) {
2360 if ((pk->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
2361 ret = SSH_ERR_ALLOC_FAIL;
2364 memcpy(pk->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
2368 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
2377 /* Convert a plain key to their _CERT equivalent */
2379 sshkey_to_certified(struct sshkey *k, int legacy)
2386 newtype = legacy ? KEY_RSA_CERT_V00 : KEY_RSA_CERT;
2389 newtype = legacy ? KEY_DSA_CERT_V00 : KEY_DSA_CERT;
2393 return SSH_ERR_INVALID_ARGUMENT;
2394 newtype = KEY_ECDSA_CERT;
2396 #endif /* WITH_OPENSSL */
2399 return SSH_ERR_INVALID_ARGUMENT;
2400 newtype = KEY_ED25519_CERT;
2403 return SSH_ERR_INVALID_ARGUMENT;
2405 if ((k->cert = cert_new()) == NULL)
2406 return SSH_ERR_ALLOC_FAIL;
2411 /* Convert a certificate to its raw key equivalent */
2413 sshkey_drop_cert(struct sshkey *k)
2415 if (!sshkey_type_is_cert(k->type))
2416 return SSH_ERR_KEY_TYPE_UNKNOWN;
2419 k->type = sshkey_type_plain(k->type);
2423 /* Sign a certified key, (re-)generating the signed certblob. */
2425 sshkey_certify(struct sshkey *k, struct sshkey *ca)
2427 struct sshbuf *principals = NULL;
2428 u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2429 size_t i, ca_len, sig_len;
2430 int ret = SSH_ERR_INTERNAL_ERROR;
2431 struct sshbuf *cert;
2433 if (k == NULL || k->cert == NULL ||
2434 k->cert->certblob == NULL || ca == NULL)
2435 return SSH_ERR_INVALID_ARGUMENT;
2436 if (!sshkey_is_cert(k))
2437 return SSH_ERR_KEY_TYPE_UNKNOWN;
2438 if (!sshkey_type_is_valid_ca(ca->type))
2439 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2441 if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2442 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2444 cert = k->cert->certblob; /* for readability */
2446 if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2449 /* -v01 certs put nonce first */
2450 arc4random_buf(&nonce, sizeof(nonce));
2451 if (!sshkey_cert_is_legacy(k)) {
2452 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2456 /* XXX this substantially duplicates to_blob(); refactor */
2459 case KEY_DSA_CERT_V00:
2461 if ((ret = sshbuf_put_bignum2(cert, k->dsa->p)) != 0 ||
2462 (ret = sshbuf_put_bignum2(cert, k->dsa->q)) != 0 ||
2463 (ret = sshbuf_put_bignum2(cert, k->dsa->g)) != 0 ||
2464 (ret = sshbuf_put_bignum2(cert, k->dsa->pub_key)) != 0)
2467 # ifdef OPENSSL_HAS_ECC
2468 case KEY_ECDSA_CERT:
2469 if ((ret = sshbuf_put_cstring(cert,
2470 sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 ||
2471 (ret = sshbuf_put_ec(cert,
2472 EC_KEY_get0_public_key(k->ecdsa),
2473 EC_KEY_get0_group(k->ecdsa))) != 0)
2476 # endif /* OPENSSL_HAS_ECC */
2477 case KEY_RSA_CERT_V00:
2479 if ((ret = sshbuf_put_bignum2(cert, k->rsa->e)) != 0 ||
2480 (ret = sshbuf_put_bignum2(cert, k->rsa->n)) != 0)
2483 #endif /* WITH_OPENSSL */
2484 case KEY_ED25519_CERT:
2485 if ((ret = sshbuf_put_string(cert,
2486 k->ed25519_pk, ED25519_PK_SZ)) != 0)
2490 ret = SSH_ERR_INVALID_ARGUMENT;
2494 /* -v01 certs have a serial number next */
2495 if (!sshkey_cert_is_legacy(k)) {
2496 if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0)
2500 if ((ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
2501 (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
2504 if ((principals = sshbuf_new()) == NULL) {
2505 ret = SSH_ERR_ALLOC_FAIL;
2508 for (i = 0; i < k->cert->nprincipals; i++) {
2509 if ((ret = sshbuf_put_cstring(principals,
2510 k->cert->principals[i])) != 0)
2513 if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
2514 (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
2515 (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
2516 (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0)
2519 /* -v01 certs have non-critical options here */
2520 if (!sshkey_cert_is_legacy(k)) {
2521 if ((ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0)
2525 /* -v00 certs put the nonce at the end */
2526 if (sshkey_cert_is_legacy(k)) {
2527 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2531 if ((ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
2532 (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
2535 /* Sign the whole mess */
2536 if ((ret = sshkey_sign(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
2537 sshbuf_len(cert), 0)) != 0)
2540 /* Append signature and we are done */
2541 if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
2547 if (sig_blob != NULL)
2549 if (ca_blob != NULL)
2551 if (principals != NULL)
2552 sshbuf_free(principals);
2557 sshkey_cert_check_authority(const struct sshkey *k,
2558 int want_host, int require_principal,
2559 const char *name, const char **reason)
2561 u_int i, principal_matches;
2562 time_t now = time(NULL);
2568 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
2569 *reason = "Certificate invalid: not a host certificate";
2570 return SSH_ERR_KEY_CERT_INVALID;
2573 if (k->cert->type != SSH2_CERT_TYPE_USER) {
2574 *reason = "Certificate invalid: not a user certificate";
2575 return SSH_ERR_KEY_CERT_INVALID;
2579 /* yikes - system clock before epoch! */
2580 *reason = "Certificate invalid: not yet valid";
2581 return SSH_ERR_KEY_CERT_INVALID;
2583 if ((u_int64_t)now < k->cert->valid_after) {
2584 *reason = "Certificate invalid: not yet valid";
2585 return SSH_ERR_KEY_CERT_INVALID;
2587 if ((u_int64_t)now >= k->cert->valid_before) {
2588 *reason = "Certificate invalid: expired";
2589 return SSH_ERR_KEY_CERT_INVALID;
2591 if (k->cert->nprincipals == 0) {
2592 if (require_principal) {
2593 *reason = "Certificate lacks principal list";
2594 return SSH_ERR_KEY_CERT_INVALID;
2596 } else if (name != NULL) {
2597 principal_matches = 0;
2598 for (i = 0; i < k->cert->nprincipals; i++) {
2599 if (strcmp(name, k->cert->principals[i]) == 0) {
2600 principal_matches = 1;
2604 if (!principal_matches) {
2605 *reason = "Certificate invalid: name is not a listed "
2607 return SSH_ERR_KEY_CERT_INVALID;
2614 sshkey_private_serialize(const struct sshkey *key, struct sshbuf *b)
2616 int r = SSH_ERR_INTERNAL_ERROR;
2618 if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
2620 switch (key->type) {
2623 if ((r = sshbuf_put_bignum2(b, key->rsa->n)) != 0 ||
2624 (r = sshbuf_put_bignum2(b, key->rsa->e)) != 0 ||
2625 (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
2626 (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
2627 (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
2628 (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
2631 case KEY_RSA_CERT_V00:
2633 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2634 r = SSH_ERR_INVALID_ARGUMENT;
2637 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2638 (r = sshbuf_put_bignum2(b, key->rsa->d)) != 0 ||
2639 (r = sshbuf_put_bignum2(b, key->rsa->iqmp)) != 0 ||
2640 (r = sshbuf_put_bignum2(b, key->rsa->p)) != 0 ||
2641 (r = sshbuf_put_bignum2(b, key->rsa->q)) != 0)
2645 if ((r = sshbuf_put_bignum2(b, key->dsa->p)) != 0 ||
2646 (r = sshbuf_put_bignum2(b, key->dsa->q)) != 0 ||
2647 (r = sshbuf_put_bignum2(b, key->dsa->g)) != 0 ||
2648 (r = sshbuf_put_bignum2(b, key->dsa->pub_key)) != 0 ||
2649 (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
2652 case KEY_DSA_CERT_V00:
2654 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2655 r = SSH_ERR_INVALID_ARGUMENT;
2658 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2659 (r = sshbuf_put_bignum2(b, key->dsa->priv_key)) != 0)
2662 # ifdef OPENSSL_HAS_ECC
2664 if ((r = sshbuf_put_cstring(b,
2665 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
2666 (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
2667 (r = sshbuf_put_bignum2(b,
2668 EC_KEY_get0_private_key(key->ecdsa))) != 0)
2671 case KEY_ECDSA_CERT:
2672 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2673 r = SSH_ERR_INVALID_ARGUMENT;
2676 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2677 (r = sshbuf_put_bignum2(b,
2678 EC_KEY_get0_private_key(key->ecdsa))) != 0)
2681 # endif /* OPENSSL_HAS_ECC */
2682 #endif /* WITH_OPENSSL */
2684 if ((r = sshbuf_put_string(b, key->ed25519_pk,
2685 ED25519_PK_SZ)) != 0 ||
2686 (r = sshbuf_put_string(b, key->ed25519_sk,
2687 ED25519_SK_SZ)) != 0)
2690 case KEY_ED25519_CERT:
2691 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
2692 r = SSH_ERR_INVALID_ARGUMENT;
2695 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
2696 (r = sshbuf_put_string(b, key->ed25519_pk,
2697 ED25519_PK_SZ)) != 0 ||
2698 (r = sshbuf_put_string(b, key->ed25519_sk,
2699 ED25519_SK_SZ)) != 0)
2703 r = SSH_ERR_INVALID_ARGUMENT;
2713 sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
2715 char *tname = NULL, *curve = NULL;
2716 struct sshkey *k = NULL;
2717 size_t pklen = 0, sklen = 0;
2718 int type, r = SSH_ERR_INTERNAL_ERROR;
2719 u_char *ed25519_pk = NULL, *ed25519_sk = NULL;
2721 BIGNUM *exponent = NULL;
2722 #endif /* WITH_OPENSSL */
2726 if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
2728 type = sshkey_type_from_name(tname);
2732 if ((k = sshkey_new_private(type)) == NULL) {
2733 r = SSH_ERR_ALLOC_FAIL;
2736 if ((r = sshbuf_get_bignum2(buf, k->dsa->p)) != 0 ||
2737 (r = sshbuf_get_bignum2(buf, k->dsa->q)) != 0 ||
2738 (r = sshbuf_get_bignum2(buf, k->dsa->g)) != 0 ||
2739 (r = sshbuf_get_bignum2(buf, k->dsa->pub_key)) != 0 ||
2740 (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
2743 case KEY_DSA_CERT_V00:
2745 if ((r = sshkey_froms(buf, &k)) != 0 ||
2746 (r = sshkey_add_private(k)) != 0 ||
2747 (r = sshbuf_get_bignum2(buf, k->dsa->priv_key)) != 0)
2750 # ifdef OPENSSL_HAS_ECC
2752 if ((k = sshkey_new_private(type)) == NULL) {
2753 r = SSH_ERR_ALLOC_FAIL;
2756 if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
2757 r = SSH_ERR_INVALID_ARGUMENT;
2760 if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
2762 if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
2763 r = SSH_ERR_EC_CURVE_MISMATCH;
2766 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
2767 if (k->ecdsa == NULL || (exponent = BN_new()) == NULL) {
2768 r = SSH_ERR_LIBCRYPTO_ERROR;
2771 if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 ||
2772 (r = sshbuf_get_bignum2(buf, exponent)))
2774 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
2775 r = SSH_ERR_LIBCRYPTO_ERROR;
2778 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
2779 EC_KEY_get0_public_key(k->ecdsa)) != 0) ||
2780 (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
2783 case KEY_ECDSA_CERT:
2784 if ((exponent = BN_new()) == NULL) {
2785 r = SSH_ERR_LIBCRYPTO_ERROR;
2788 if ((r = sshkey_froms(buf, &k)) != 0 ||
2789 (r = sshkey_add_private(k)) != 0 ||
2790 (r = sshbuf_get_bignum2(buf, exponent)) != 0)
2792 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
2793 r = SSH_ERR_LIBCRYPTO_ERROR;
2796 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
2797 EC_KEY_get0_public_key(k->ecdsa)) != 0) ||
2798 (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
2801 # endif /* OPENSSL_HAS_ECC */
2803 if ((k = sshkey_new_private(type)) == NULL) {
2804 r = SSH_ERR_ALLOC_FAIL;
2807 if ((r = sshbuf_get_bignum2(buf, k->rsa->n)) != 0 ||
2808 (r = sshbuf_get_bignum2(buf, k->rsa->e)) != 0 ||
2809 (r = sshbuf_get_bignum2(buf, k->rsa->d)) != 0 ||
2810 (r = sshbuf_get_bignum2(buf, k->rsa->iqmp)) != 0 ||
2811 (r = sshbuf_get_bignum2(buf, k->rsa->p)) != 0 ||
2812 (r = sshbuf_get_bignum2(buf, k->rsa->q)) != 0 ||
2813 (r = rsa_generate_additional_parameters(k->rsa)) != 0)
2816 case KEY_RSA_CERT_V00:
2818 if ((r = sshkey_froms(buf, &k)) != 0 ||
2819 (r = sshkey_add_private(k)) != 0 ||
2820 (r = sshbuf_get_bignum2(buf, k->rsa->d) != 0) ||
2821 (r = sshbuf_get_bignum2(buf, k->rsa->iqmp) != 0) ||
2822 (r = sshbuf_get_bignum2(buf, k->rsa->p) != 0) ||
2823 (r = sshbuf_get_bignum2(buf, k->rsa->q) != 0) ||
2824 (r = rsa_generate_additional_parameters(k->rsa)) != 0)
2827 #endif /* WITH_OPENSSL */
2829 if ((k = sshkey_new_private(type)) == NULL) {
2830 r = SSH_ERR_ALLOC_FAIL;
2833 if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
2834 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
2836 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
2837 r = SSH_ERR_INVALID_FORMAT;
2840 k->ed25519_pk = ed25519_pk;
2841 k->ed25519_sk = ed25519_sk;
2842 ed25519_pk = ed25519_sk = NULL;
2844 case KEY_ED25519_CERT:
2845 if ((r = sshkey_froms(buf, &k)) != 0 ||
2846 (r = sshkey_add_private(k)) != 0 ||
2847 (r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
2848 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
2850 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
2851 r = SSH_ERR_INVALID_FORMAT;
2854 k->ed25519_pk = ed25519_pk;
2855 k->ed25519_sk = ed25519_sk;
2856 ed25519_pk = ed25519_sk = NULL;
2859 r = SSH_ERR_KEY_TYPE_UNKNOWN;
2863 /* enable blinding */
2866 case KEY_RSA_CERT_V00:
2869 if (RSA_blinding_on(k->rsa, NULL) != 1) {
2870 r = SSH_ERR_LIBCRYPTO_ERROR;
2875 #endif /* WITH_OPENSSL */
2886 if (exponent != NULL)
2887 BN_clear_free(exponent);
2888 #endif /* WITH_OPENSSL */
2890 if (ed25519_pk != NULL) {
2891 explicit_bzero(ed25519_pk, pklen);
2894 if (ed25519_sk != NULL) {
2895 explicit_bzero(ed25519_sk, sklen);
2901 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2903 sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2906 EC_POINT *nq = NULL;
2907 BIGNUM *order, *x, *y, *tmp;
2908 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2910 if ((bnctx = BN_CTX_new()) == NULL)
2911 return SSH_ERR_ALLOC_FAIL;
2912 BN_CTX_start(bnctx);
2915 * We shouldn't ever hit this case because bignum_get_ecpoint()
2916 * refuses to load GF2m points.
2918 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2919 NID_X9_62_prime_field)
2923 if (EC_POINT_is_at_infinity(group, public))
2926 if ((x = BN_CTX_get(bnctx)) == NULL ||
2927 (y = BN_CTX_get(bnctx)) == NULL ||
2928 (order = BN_CTX_get(bnctx)) == NULL ||
2929 (tmp = BN_CTX_get(bnctx)) == NULL) {
2930 ret = SSH_ERR_ALLOC_FAIL;
2934 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2935 if (EC_GROUP_get_order(group, order, bnctx) != 1 ||
2936 EC_POINT_get_affine_coordinates_GFp(group, public,
2937 x, y, bnctx) != 1) {
2938 ret = SSH_ERR_LIBCRYPTO_ERROR;
2941 if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
2942 BN_num_bits(y) <= BN_num_bits(order) / 2)
2945 /* nQ == infinity (n == order of subgroup) */
2946 if ((nq = EC_POINT_new(group)) == NULL) {
2947 ret = SSH_ERR_ALLOC_FAIL;
2950 if (EC_POINT_mul(group, nq, NULL, public, order, bnctx) != 1) {
2951 ret = SSH_ERR_LIBCRYPTO_ERROR;
2954 if (EC_POINT_is_at_infinity(group, nq) != 1)
2957 /* x < order - 1, y < order - 1 */
2958 if (!BN_sub(tmp, order, BN_value_one())) {
2959 ret = SSH_ERR_LIBCRYPTO_ERROR;
2962 if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
2973 sshkey_ec_validate_private(const EC_KEY *key)
2976 BIGNUM *order, *tmp;
2977 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2979 if ((bnctx = BN_CTX_new()) == NULL)
2980 return SSH_ERR_ALLOC_FAIL;
2981 BN_CTX_start(bnctx);
2983 if ((order = BN_CTX_get(bnctx)) == NULL ||
2984 (tmp = BN_CTX_get(bnctx)) == NULL) {
2985 ret = SSH_ERR_ALLOC_FAIL;
2989 /* log2(private) > log2(order)/2 */
2990 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, bnctx) != 1) {
2991 ret = SSH_ERR_LIBCRYPTO_ERROR;
2994 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2995 BN_num_bits(order) / 2)
2998 /* private < order - 1 */
2999 if (!BN_sub(tmp, order, BN_value_one())) {
3000 ret = SSH_ERR_LIBCRYPTO_ERROR;
3003 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
3012 sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
3017 if (point == NULL) {
3018 fputs("point=(NULL)\n", stderr);
3021 if ((bnctx = BN_CTX_new()) == NULL) {
3022 fprintf(stderr, "%s: BN_CTX_new failed\n", __func__);
3025 BN_CTX_start(bnctx);
3026 if ((x = BN_CTX_get(bnctx)) == NULL ||
3027 (y = BN_CTX_get(bnctx)) == NULL) {
3028 fprintf(stderr, "%s: BN_CTX_get failed\n", __func__);
3031 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
3032 NID_X9_62_prime_field) {
3033 fprintf(stderr, "%s: group is not a prime field\n", __func__);
3036 if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y,
3038 fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
3042 fputs("x=", stderr);
3043 BN_print_fp(stderr, x);
3044 fputs("\ny=", stderr);
3045 BN_print_fp(stderr, y);
3046 fputs("\n", stderr);
3051 sshkey_dump_ec_key(const EC_KEY *key)
3053 const BIGNUM *exponent;
3055 sshkey_dump_ec_point(EC_KEY_get0_group(key),
3056 EC_KEY_get0_public_key(key));
3057 fputs("exponent=", stderr);
3058 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
3059 fputs("(NULL)", stderr);
3061 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
3062 fputs("\n", stderr);
3064 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
3067 sshkey_private_to_blob2(const struct sshkey *prv, struct sshbuf *blob,
3068 const char *passphrase, const char *comment, const char *ciphername,
3071 u_char *cp, *key = NULL, *pubkeyblob = NULL;
3072 u_char salt[SALT_LEN];
3074 size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
3076 int r = SSH_ERR_INTERNAL_ERROR;
3077 struct sshcipher_ctx ciphercontext;
3078 const struct sshcipher *cipher;
3079 const char *kdfname = KDFNAME;
3080 struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
3082 memset(&ciphercontext, 0, sizeof(ciphercontext));
3085 rounds = DEFAULT_ROUNDS;
3086 if (passphrase == NULL || !strlen(passphrase)) {
3087 ciphername = "none";
3089 } else if (ciphername == NULL)
3090 ciphername = DEFAULT_CIPHERNAME;
3091 else if (cipher_number(ciphername) != SSH_CIPHER_SSH2) {
3092 r = SSH_ERR_INVALID_ARGUMENT;
3095 if ((cipher = cipher_by_name(ciphername)) == NULL) {
3096 r = SSH_ERR_INTERNAL_ERROR;
3100 if ((kdf = sshbuf_new()) == NULL ||
3101 (encoded = sshbuf_new()) == NULL ||
3102 (encrypted = sshbuf_new()) == NULL) {
3103 r = SSH_ERR_ALLOC_FAIL;
3106 blocksize = cipher_blocksize(cipher);
3107 keylen = cipher_keylen(cipher);
3108 ivlen = cipher_ivlen(cipher);
3109 authlen = cipher_authlen(cipher);
3110 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3111 r = SSH_ERR_ALLOC_FAIL;
3114 if (strcmp(kdfname, "bcrypt") == 0) {
3115 arc4random_buf(salt, SALT_LEN);
3116 if (bcrypt_pbkdf(passphrase, strlen(passphrase),
3117 salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
3118 r = SSH_ERR_INVALID_ARGUMENT;
3121 if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
3122 (r = sshbuf_put_u32(kdf, rounds)) != 0)
3124 } else if (strcmp(kdfname, "none") != 0) {
3125 /* Unsupported KDF type */
3126 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3129 if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
3130 key + keylen, ivlen, 1)) != 0)
3133 if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
3134 (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
3135 (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
3136 (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
3137 (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
3138 (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
3139 (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
3142 /* set up the buffer that will be encrypted */
3144 /* Random check bytes */
3145 check = arc4random();
3146 if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
3147 (r = sshbuf_put_u32(encrypted, check)) != 0)
3150 /* append private key and comment*/
3151 if ((r = sshkey_private_serialize(prv, encrypted)) != 0 ||
3152 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
3157 while (sshbuf_len(encrypted) % blocksize) {
3158 if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
3162 /* length in destination buffer */
3163 if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
3167 if ((r = sshbuf_reserve(encoded,
3168 sshbuf_len(encrypted) + authlen, &cp)) != 0)
3170 if ((r = cipher_crypt(&ciphercontext, 0, cp,
3171 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
3175 if ((b64 = sshbuf_dtob64(encoded)) == NULL) {
3176 r = SSH_ERR_ALLOC_FAIL;
3181 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0)
3183 for (i = 0; i < strlen(b64); i++) {
3184 if ((r = sshbuf_put_u8(blob, b64[i])) != 0)
3186 /* insert line breaks */
3187 if (i % 70 == 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3190 if (i % 70 != 69 && (r = sshbuf_put_u8(blob, '\n')) != 0)
3192 if ((r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
3200 sshbuf_free(encoded);
3201 sshbuf_free(encrypted);
3202 cipher_cleanup(&ciphercontext);
3203 explicit_bzero(salt, sizeof(salt));
3205 explicit_bzero(key, keylen + ivlen);
3208 if (pubkeyblob != NULL) {
3209 explicit_bzero(pubkeyblob, pubkeylen);
3213 explicit_bzero(b64, strlen(b64));
3220 sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
3221 struct sshkey **keyp, char **commentp)
3223 char *comment = NULL, *ciphername = NULL, *kdfname = NULL;
3224 const struct sshcipher *cipher = NULL;
3226 int r = SSH_ERR_INTERNAL_ERROR;
3228 size_t i, keylen = 0, ivlen = 0, authlen = 0, slen = 0;
3229 struct sshbuf *encoded = NULL, *decoded = NULL;
3230 struct sshbuf *kdf = NULL, *decrypted = NULL;
3231 struct sshcipher_ctx ciphercontext;
3232 struct sshkey *k = NULL;
3233 u_char *key = NULL, *salt = NULL, *dp, pad, last;
3234 u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
3236 memset(&ciphercontext, 0, sizeof(ciphercontext));
3239 if (commentp != NULL)
3242 if ((encoded = sshbuf_new()) == NULL ||
3243 (decoded = sshbuf_new()) == NULL ||
3244 (decrypted = sshbuf_new()) == NULL) {
3245 r = SSH_ERR_ALLOC_FAIL;
3249 /* check preamble */
3250 cp = sshbuf_ptr(blob);
3251 encoded_len = sshbuf_len(blob);
3252 if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
3253 memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
3254 r = SSH_ERR_INVALID_FORMAT;
3257 cp += MARK_BEGIN_LEN;
3258 encoded_len -= MARK_BEGIN_LEN;
3260 /* Look for end marker, removing whitespace as we go */
3261 while (encoded_len > 0) {
3262 if (*cp != '\n' && *cp != '\r') {
3263 if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
3270 if (encoded_len >= MARK_END_LEN &&
3271 memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
3273 if ((r = sshbuf_put_u8(encoded, 0)) != 0)
3279 if (encoded_len == 0) {
3280 r = SSH_ERR_INVALID_FORMAT;
3285 if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
3289 if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
3290 memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
3291 r = SSH_ERR_INVALID_FORMAT;
3294 /* parse public portion of key */
3295 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
3296 (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
3297 (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
3298 (r = sshbuf_froms(decoded, &kdf)) != 0 ||
3299 (r = sshbuf_get_u32(decoded, &nkeys)) != 0 ||
3300 (r = sshbuf_skip_string(decoded)) != 0 || /* pubkey */
3301 (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
3304 if ((cipher = cipher_by_name(ciphername)) == NULL) {
3305 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3308 if ((passphrase == NULL || strlen(passphrase) == 0) &&
3309 strcmp(ciphername, "none") != 0) {
3310 /* passphrase required */
3311 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3314 if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
3315 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3318 if (!strcmp(kdfname, "none") && strcmp(ciphername, "none") != 0) {
3319 r = SSH_ERR_INVALID_FORMAT;
3323 /* XXX only one key supported */
3324 r = SSH_ERR_INVALID_FORMAT;
3328 /* check size of encrypted key blob */
3329 blocksize = cipher_blocksize(cipher);
3330 if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
3331 r = SSH_ERR_INVALID_FORMAT;
3336 keylen = cipher_keylen(cipher);
3337 ivlen = cipher_ivlen(cipher);
3338 authlen = cipher_authlen(cipher);
3339 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3340 r = SSH_ERR_ALLOC_FAIL;
3343 if (strcmp(kdfname, "bcrypt") == 0) {
3344 if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
3345 (r = sshbuf_get_u32(kdf, &rounds)) != 0)
3347 if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
3348 key, keylen + ivlen, rounds) < 0) {
3349 r = SSH_ERR_INVALID_FORMAT;
3354 /* check that an appropriate amount of auth data is present */
3355 if (sshbuf_len(decoded) < encrypted_len + authlen) {
3356 r = SSH_ERR_INVALID_FORMAT;
3360 /* decrypt private portion of key */
3361 if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
3362 (r = cipher_init(&ciphercontext, cipher, key, keylen,
3363 key + keylen, ivlen, 0)) != 0)
3365 if ((r = cipher_crypt(&ciphercontext, 0, dp, sshbuf_ptr(decoded),
3366 encrypted_len, 0, authlen)) != 0) {
3367 /* an integrity error here indicates an incorrect passphrase */
3368 if (r == SSH_ERR_MAC_INVALID)
3369 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3372 if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
3374 /* there should be no trailing data */
3375 if (sshbuf_len(decoded) != 0) {
3376 r = SSH_ERR_INVALID_FORMAT;
3380 /* check check bytes */
3381 if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
3382 (r = sshbuf_get_u32(decrypted, &check2)) != 0)
3384 if (check1 != check2) {
3385 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3389 /* Load the private key and comment */
3390 if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
3391 (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
3394 /* Check deterministic padding */
3396 while (sshbuf_len(decrypted)) {
3397 if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
3399 if (pad != (++i & 0xff)) {
3400 r = SSH_ERR_INVALID_FORMAT;
3405 /* XXX decode pubkey and check against private */
3413 if (commentp != NULL) {
3414 *commentp = comment;
3419 cipher_cleanup(&ciphercontext);
3424 explicit_bzero(salt, slen);
3428 explicit_bzero(key, keylen + ivlen);
3431 sshbuf_free(encoded);
3432 sshbuf_free(decoded);
3434 sshbuf_free(decrypted);
3441 * Serialises the authentication (private) key to a blob, encrypting it with
3442 * passphrase. The identification of the blob (lowest 64 bits of n) will
3443 * precede the key to provide identification of the key without needing a
3447 sshkey_private_rsa1_to_blob(struct sshkey *key, struct sshbuf *blob,
3448 const char *passphrase, const char *comment)
3450 struct sshbuf *buffer = NULL, *encrypted = NULL;
3453 struct sshcipher_ctx ciphercontext;
3454 const struct sshcipher *cipher;
3458 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
3459 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
3461 cipher_num = (strcmp(passphrase, "") == 0) ?
3462 SSH_CIPHER_NONE : SSH_CIPHER_3DES;
3463 if ((cipher = cipher_by_number(cipher_num)) == NULL)
3464 return SSH_ERR_INTERNAL_ERROR;
3466 /* This buffer is used to build the secret part of the private key. */
3467 if ((buffer = sshbuf_new()) == NULL)
3468 return SSH_ERR_ALLOC_FAIL;
3470 /* Put checkbytes for checking passphrase validity. */
3471 if ((r = sshbuf_reserve(buffer, 4, &cp)) != 0)
3473 arc4random_buf(cp, 2);
3474 memcpy(cp + 2, cp, 2);
3477 * Store the private key (n and e will not be stored because they
3478 * will be stored in plain text, and storing them also in encrypted
3479 * format would just give known plaintext).
3480 * Note: q and p are stored in reverse order to SSL.
3482 if ((r = sshbuf_put_bignum1(buffer, key->rsa->d)) != 0 ||
3483 (r = sshbuf_put_bignum1(buffer, key->rsa->iqmp)) != 0 ||
3484 (r = sshbuf_put_bignum1(buffer, key->rsa->q)) != 0 ||
3485 (r = sshbuf_put_bignum1(buffer, key->rsa->p)) != 0)
3488 /* Pad the part to be encrypted to a size that is a multiple of 8. */
3489 explicit_bzero(buf, 8);
3490 if ((r = sshbuf_put(buffer, buf, 8 - (sshbuf_len(buffer) % 8))) != 0)
3493 /* This buffer will be used to contain the data in the file. */
3494 if ((encrypted = sshbuf_new()) == NULL) {
3495 r = SSH_ERR_ALLOC_FAIL;
3499 /* First store keyfile id string. */
3500 if ((r = sshbuf_put(encrypted, LEGACY_BEGIN,
3501 sizeof(LEGACY_BEGIN))) != 0)
3504 /* Store cipher type and "reserved" field. */
3505 if ((r = sshbuf_put_u8(encrypted, cipher_num)) != 0 ||
3506 (r = sshbuf_put_u32(encrypted, 0)) != 0)
3509 /* Store public key. This will be in plain text. */
3510 if ((r = sshbuf_put_u32(encrypted, BN_num_bits(key->rsa->n))) != 0 ||
3511 (r = sshbuf_put_bignum1(encrypted, key->rsa->n) != 0) ||
3512 (r = sshbuf_put_bignum1(encrypted, key->rsa->e) != 0) ||
3513 (r = sshbuf_put_cstring(encrypted, comment) != 0))
3516 /* Allocate space for the private part of the key in the buffer. */
3517 if ((r = sshbuf_reserve(encrypted, sshbuf_len(buffer), &cp)) != 0)
3520 if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase,
3521 CIPHER_ENCRYPT)) != 0)
3523 if ((r = cipher_crypt(&ciphercontext, 0, cp,
3524 sshbuf_ptr(buffer), sshbuf_len(buffer), 0, 0)) != 0)
3526 if ((r = cipher_cleanup(&ciphercontext)) != 0)
3529 r = sshbuf_putb(blob, encrypted);
3532 explicit_bzero(&ciphercontext, sizeof(ciphercontext));
3533 explicit_bzero(buf, sizeof(buf));
3535 sshbuf_free(buffer);
3536 if (encrypted != NULL)
3537 sshbuf_free(encrypted);
3541 #endif /* WITH_SSH1 */
3544 /* convert SSH v2 key in OpenSSL PEM format */
3546 sshkey_private_pem_to_blob(struct sshkey *key, struct sshbuf *blob,
3547 const char *_passphrase, const char *comment)
3550 int blen, len = strlen(_passphrase);
3551 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
3552 #if (OPENSSL_VERSION_NUMBER < 0x00907000L)
3553 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
3555 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
3560 if (len > 0 && len <= 4)
3561 return SSH_ERR_PASSPHRASE_TOO_SHORT;
3562 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3563 return SSH_ERR_ALLOC_FAIL;
3565 switch (key->type) {
3567 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
3568 cipher, passphrase, len, NULL, NULL);
3570 #ifdef OPENSSL_HAS_ECC
3572 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
3573 cipher, passphrase, len, NULL, NULL);
3577 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
3578 cipher, passphrase, len, NULL, NULL);
3585 r = SSH_ERR_LIBCRYPTO_ERROR;
3588 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
3589 r = SSH_ERR_INTERNAL_ERROR;
3592 if ((r = sshbuf_put(blob, bptr, blen)) != 0)
3599 #endif /* WITH_OPENSSL */
3601 /* Serialise "key" to buffer "blob" */
3603 sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
3604 const char *passphrase, const char *comment,
3605 int force_new_format, const char *new_format_cipher, int new_format_rounds)
3607 switch (key->type) {
3610 return sshkey_private_rsa1_to_blob(key, blob,
3611 passphrase, comment);
3612 #endif /* WITH_SSH1 */
3617 if (force_new_format) {
3618 return sshkey_private_to_blob2(key, blob, passphrase,
3619 comment, new_format_cipher, new_format_rounds);
3621 return sshkey_private_pem_to_blob(key, blob,
3622 passphrase, comment);
3623 #endif /* WITH_OPENSSL */
3625 return sshkey_private_to_blob2(key, blob, passphrase,
3626 comment, new_format_cipher, new_format_rounds);
3628 return SSH_ERR_KEY_TYPE_UNKNOWN;
3634 * Parse the public, unencrypted portion of a RSA1 key.
3637 sshkey_parse_public_rsa1_fileblob(struct sshbuf *blob,
3638 struct sshkey **keyp, char **commentp)
3641 struct sshkey *pub = NULL;
3642 struct sshbuf *copy = NULL;
3646 if (commentp != NULL)
3649 /* Check that it is at least big enough to contain the ID string. */
3650 if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN))
3651 return SSH_ERR_INVALID_FORMAT;
3654 * Make sure it begins with the id string. Consume the id string
3657 if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0)
3658 return SSH_ERR_INVALID_FORMAT;
3659 /* Make a working copy of the keyblob and skip past the magic */
3660 if ((copy = sshbuf_fromb(blob)) == NULL)
3661 return SSH_ERR_ALLOC_FAIL;
3662 if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0)
3665 /* Skip cipher type, reserved data and key bits. */
3666 if ((r = sshbuf_get_u8(copy, NULL)) != 0 || /* cipher type */
3667 (r = sshbuf_get_u32(copy, NULL)) != 0 || /* reserved */
3668 (r = sshbuf_get_u32(copy, NULL)) != 0) /* key bits */
3671 /* Read the public key from the buffer. */
3672 if ((pub = sshkey_new(KEY_RSA1)) == NULL ||
3673 (r = sshbuf_get_bignum1(copy, pub->rsa->n)) != 0 ||
3674 (r = sshbuf_get_bignum1(copy, pub->rsa->e)) != 0)
3677 /* Finally, the comment */
3678 if ((r = sshbuf_get_string(copy, (u_char**)commentp, NULL)) != 0)
3681 /* The encrypted private part is not parsed by this function. */
3699 sshkey_parse_private_rsa1(struct sshbuf *blob, const char *passphrase,
3700 struct sshkey **keyp, char **commentp)
3703 u_int16_t check1, check2;
3704 u_int8_t cipher_type;
3705 struct sshbuf *decrypted = NULL, *copy = NULL;
3707 char *comment = NULL;
3708 struct sshcipher_ctx ciphercontext;
3709 const struct sshcipher *cipher;
3710 struct sshkey *prv = NULL;
3713 if (commentp != NULL)
3716 /* Check that it is at least big enough to contain the ID string. */
3717 if (sshbuf_len(blob) < sizeof(LEGACY_BEGIN))
3718 return SSH_ERR_INVALID_FORMAT;
3721 * Make sure it begins with the id string. Consume the id string
3724 if (memcmp(sshbuf_ptr(blob), LEGACY_BEGIN, sizeof(LEGACY_BEGIN)) != 0)
3725 return SSH_ERR_INVALID_FORMAT;
3727 if ((prv = sshkey_new_private(KEY_RSA1)) == NULL) {
3728 r = SSH_ERR_ALLOC_FAIL;
3731 if ((copy = sshbuf_fromb(blob)) == NULL ||
3732 (decrypted = sshbuf_new()) == NULL) {
3733 r = SSH_ERR_ALLOC_FAIL;
3736 if ((r = sshbuf_consume(copy, sizeof(LEGACY_BEGIN))) != 0)
3739 /* Read cipher type. */
3740 if ((r = sshbuf_get_u8(copy, &cipher_type)) != 0 ||
3741 (r = sshbuf_get_u32(copy, NULL)) != 0) /* reserved */
3744 /* Read the public key and comment from the buffer. */
3745 if ((r = sshbuf_get_u32(copy, NULL)) != 0 || /* key bits */
3746 (r = sshbuf_get_bignum1(copy, prv->rsa->n)) != 0 ||
3747 (r = sshbuf_get_bignum1(copy, prv->rsa->e)) != 0 ||
3748 (r = sshbuf_get_cstring(copy, &comment, NULL)) != 0)
3751 /* Check that it is a supported cipher. */
3752 cipher = cipher_by_number(cipher_type);
3753 if (cipher == NULL) {
3754 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3757 /* Initialize space for decrypted data. */
3758 if ((r = sshbuf_reserve(decrypted, sshbuf_len(copy), &cp)) != 0)
3761 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
3762 if ((r = cipher_set_key_string(&ciphercontext, cipher, passphrase,
3763 CIPHER_DECRYPT)) != 0)
3765 if ((r = cipher_crypt(&ciphercontext, 0, cp,
3766 sshbuf_ptr(copy), sshbuf_len(copy), 0, 0)) != 0) {
3767 cipher_cleanup(&ciphercontext);
3770 if ((r = cipher_cleanup(&ciphercontext)) != 0)
3773 if ((r = sshbuf_get_u16(decrypted, &check1)) != 0 ||
3774 (r = sshbuf_get_u16(decrypted, &check2)) != 0)
3776 if (check1 != check2) {
3777 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3781 /* Read the rest of the private key. */
3782 if ((r = sshbuf_get_bignum1(decrypted, prv->rsa->d)) != 0 ||
3783 (r = sshbuf_get_bignum1(decrypted, prv->rsa->iqmp)) != 0 ||
3784 (r = sshbuf_get_bignum1(decrypted, prv->rsa->q)) != 0 ||
3785 (r = sshbuf_get_bignum1(decrypted, prv->rsa->p)) != 0)
3788 /* calculate p-1 and q-1 */
3789 if ((r = rsa_generate_additional_parameters(prv->rsa)) != 0)
3792 /* enable blinding */
3793 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3794 r = SSH_ERR_LIBCRYPTO_ERROR;
3800 if (commentp != NULL) {
3801 *commentp = comment;
3805 explicit_bzero(&ciphercontext, sizeof(ciphercontext));
3806 if (comment != NULL)
3812 if (decrypted != NULL)
3813 sshbuf_free(decrypted);
3816 #endif /* WITH_SSH1 */
3820 sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
3821 const char *passphrase, struct sshkey **keyp)
3823 EVP_PKEY *pk = NULL;
3824 struct sshkey *prv = NULL;
3830 if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
3831 return SSH_ERR_ALLOC_FAIL;
3832 if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
3833 (int)sshbuf_len(blob)) {
3834 r = SSH_ERR_ALLOC_FAIL;
3838 if ((pk = PEM_read_bio_PrivateKey(bio, NULL, NULL,
3839 (char *)passphrase)) == NULL) {
3840 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3843 if (pk->type == EVP_PKEY_RSA &&
3844 (type == KEY_UNSPEC || type == KEY_RSA)) {
3845 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3846 r = SSH_ERR_ALLOC_FAIL;
3849 prv->rsa = EVP_PKEY_get1_RSA(pk);
3850 prv->type = KEY_RSA;
3852 RSA_print_fp(stderr, prv->rsa, 8);
3854 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3855 r = SSH_ERR_LIBCRYPTO_ERROR;
3858 } else if (pk->type == EVP_PKEY_DSA &&
3859 (type == KEY_UNSPEC || type == KEY_DSA)) {
3860 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3861 r = SSH_ERR_ALLOC_FAIL;
3864 prv->dsa = EVP_PKEY_get1_DSA(pk);
3865 prv->type = KEY_DSA;
3867 DSA_print_fp(stderr, prv->dsa, 8);
3869 #ifdef OPENSSL_HAS_ECC
3870 } else if (pk->type == EVP_PKEY_EC &&
3871 (type == KEY_UNSPEC || type == KEY_ECDSA)) {
3872 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3873 r = SSH_ERR_ALLOC_FAIL;
3876 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
3877 prv->type = KEY_ECDSA;
3878 prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
3879 if (prv->ecdsa_nid == -1 ||
3880 sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
3881 sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
3882 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
3883 sshkey_ec_validate_private(prv->ecdsa) != 0) {
3884 r = SSH_ERR_INVALID_FORMAT;
3888 if (prv != NULL && prv->ecdsa != NULL)
3889 sshkey_dump_ec_key(prv->ecdsa);
3891 #endif /* OPENSSL_HAS_ECC */
3893 r = SSH_ERR_INVALID_FORMAT;
3907 #endif /* WITH_OPENSSL */
3910 sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
3911 const char *passphrase, struct sshkey **keyp, char **commentp)
3916 if (commentp != NULL)
3922 return sshkey_parse_private_rsa1(blob, passphrase,
3924 #endif /* WITH_SSH1 */
3929 return sshkey_parse_private_pem_fileblob(blob, type,
3931 #endif /* WITH_OPENSSL */
3933 return sshkey_parse_private2(blob, type, passphrase,
3936 if ((r = sshkey_parse_private2(blob, type, passphrase, keyp,
3940 return sshkey_parse_private_pem_fileblob(blob, type,
3943 return SSH_ERR_INVALID_FORMAT;
3944 #endif /* WITH_OPENSSL */
3946 return SSH_ERR_KEY_TYPE_UNKNOWN;
3951 sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
3952 const char *filename, struct sshkey **keyp, char **commentp)
3958 if (commentp != NULL)
3962 /* it's a SSH v1 key if the public key part is readable */
3963 if ((r = sshkey_parse_public_rsa1_fileblob(buffer, NULL, NULL)) == 0) {
3964 return sshkey_parse_private_fileblob_type(buffer, KEY_RSA1,
3965 passphrase, keyp, commentp);
3967 #endif /* WITH_SSH1 */
3968 if ((r = sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
3969 passphrase, keyp, commentp)) == 0)