1 /* $OpenBSD: sshkey.c,v 1.142 2024/01/11 01:45:36 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/types.h>
31 #include <netinet/in.h>
34 #include <openssl/evp.h>
35 #include <openssl/err.h>
36 #include <openssl/pem.h>
39 #include "crypto_api.h"
50 #endif /* HAVE_UTIL_H */
58 #define SSHKEY_INTERNAL
64 #include "sshkey-xmss.h"
65 #include "xmss_fast.h"
68 #include "openbsd-compat/openssl-compat.h"
70 /* openssh private key file format */
71 #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
72 #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
73 #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
74 #define MARK_END_LEN (sizeof(MARK_END) - 1)
75 #define KDFNAME "bcrypt"
76 #define AUTH_MAGIC "openssh-key-v1"
78 #define DEFAULT_CIPHERNAME "aes256-ctr"
79 #define DEFAULT_ROUNDS 24
81 /* Version identification string for SSH v1 identity files. */
82 #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
85 * Constants relating to "shielding" support; protection of keys expected
86 * to remain in memory for long durations
88 #define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024)
89 #define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */
90 #define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512
92 int sshkey_private_serialize_opt(struct sshkey *key,
93 struct sshbuf *buf, enum sshkey_serialize_rep);
94 static int sshkey_from_blob_internal(struct sshbuf *buf,
95 struct sshkey **keyp, int allow_cert);
97 /* Supported key types */
98 extern const struct sshkey_impl sshkey_ed25519_impl;
99 extern const struct sshkey_impl sshkey_ed25519_cert_impl;
100 extern const struct sshkey_impl sshkey_ed25519_sk_impl;
101 extern const struct sshkey_impl sshkey_ed25519_sk_cert_impl;
103 # ifdef OPENSSL_HAS_ECC
105 extern const struct sshkey_impl sshkey_ecdsa_sk_impl;
106 extern const struct sshkey_impl sshkey_ecdsa_sk_cert_impl;
107 extern const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl;
108 # endif /* ENABLE_SK */
109 extern const struct sshkey_impl sshkey_ecdsa_nistp256_impl;
110 extern const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl;
111 extern const struct sshkey_impl sshkey_ecdsa_nistp384_impl;
112 extern const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl;
113 # ifdef OPENSSL_HAS_NISTP521
114 extern const struct sshkey_impl sshkey_ecdsa_nistp521_impl;
115 extern const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl;
116 # endif /* OPENSSL_HAS_NISTP521 */
117 # endif /* OPENSSL_HAS_ECC */
118 extern const struct sshkey_impl sshkey_rsa_impl;
119 extern const struct sshkey_impl sshkey_rsa_cert_impl;
120 extern const struct sshkey_impl sshkey_rsa_sha256_impl;
121 extern const struct sshkey_impl sshkey_rsa_sha256_cert_impl;
122 extern const struct sshkey_impl sshkey_rsa_sha512_impl;
123 extern const struct sshkey_impl sshkey_rsa_sha512_cert_impl;
125 extern const struct sshkey_impl sshkey_dss_impl;
126 extern const struct sshkey_impl sshkey_dsa_cert_impl;
128 #endif /* WITH_OPENSSL */
130 extern const struct sshkey_impl sshkey_xmss_impl;
131 extern const struct sshkey_impl sshkey_xmss_cert_impl;
134 const struct sshkey_impl * const keyimpls[] = {
135 &sshkey_ed25519_impl,
136 &sshkey_ed25519_cert_impl,
138 &sshkey_ed25519_sk_impl,
139 &sshkey_ed25519_sk_cert_impl,
142 # ifdef OPENSSL_HAS_ECC
143 &sshkey_ecdsa_nistp256_impl,
144 &sshkey_ecdsa_nistp256_cert_impl,
145 &sshkey_ecdsa_nistp384_impl,
146 &sshkey_ecdsa_nistp384_cert_impl,
147 # ifdef OPENSSL_HAS_NISTP521
148 &sshkey_ecdsa_nistp521_impl,
149 &sshkey_ecdsa_nistp521_cert_impl,
150 # endif /* OPENSSL_HAS_NISTP521 */
152 &sshkey_ecdsa_sk_impl,
153 &sshkey_ecdsa_sk_cert_impl,
154 &sshkey_ecdsa_sk_webauthn_impl,
155 # endif /* ENABLE_SK */
156 # endif /* OPENSSL_HAS_ECC */
159 &sshkey_dsa_cert_impl,
162 &sshkey_rsa_cert_impl,
163 &sshkey_rsa_sha256_impl,
164 &sshkey_rsa_sha256_cert_impl,
165 &sshkey_rsa_sha512_impl,
166 &sshkey_rsa_sha512_cert_impl,
167 #endif /* WITH_OPENSSL */
170 &sshkey_xmss_cert_impl,
175 static const struct sshkey_impl *
176 sshkey_impl_from_type(int type)
180 for (i = 0; keyimpls[i] != NULL; i++) {
181 if (keyimpls[i]->type == type)
187 static const struct sshkey_impl *
188 sshkey_impl_from_type_nid(int type, int nid)
192 for (i = 0; keyimpls[i] != NULL; i++) {
193 if (keyimpls[i]->type == type &&
194 (keyimpls[i]->nid == 0 || keyimpls[i]->nid == nid))
200 static const struct sshkey_impl *
201 sshkey_impl_from_key(const struct sshkey *k)
205 return sshkey_impl_from_type_nid(k->type, k->ecdsa_nid);
209 sshkey_type(const struct sshkey *k)
211 const struct sshkey_impl *impl;
213 if ((impl = sshkey_impl_from_key(k)) == NULL)
215 return impl->shortname;
219 sshkey_ssh_name_from_type_nid(int type, int nid)
221 const struct sshkey_impl *impl;
223 if ((impl = sshkey_impl_from_type_nid(type, nid)) == NULL)
224 return "ssh-unknown";
229 sshkey_type_is_cert(int type)
231 const struct sshkey_impl *impl;
233 if ((impl = sshkey_impl_from_type(type)) == NULL)
239 sshkey_ssh_name(const struct sshkey *k)
241 return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
245 sshkey_ssh_name_plain(const struct sshkey *k)
247 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
252 sshkey_type_from_name(const char *name)
255 const struct sshkey_impl *impl;
257 for (i = 0; keyimpls[i] != NULL; i++) {
259 /* Only allow shortname matches for plain key types */
260 if ((impl->name != NULL && strcmp(name, impl->name) == 0) ||
261 (!impl->cert && strcasecmp(impl->shortname, name) == 0))
268 key_type_is_ecdsa_variant(int type)
274 case KEY_ECDSA_SK_CERT:
281 sshkey_ecdsa_nid_from_name(const char *name)
285 for (i = 0; keyimpls[i] != NULL; i++) {
286 if (!key_type_is_ecdsa_variant(keyimpls[i]->type))
288 if (keyimpls[i]->name != NULL &&
289 strcmp(name, keyimpls[i]->name) == 0)
290 return keyimpls[i]->nid;
296 sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
300 if (sigalgs == NULL || *sigalgs == '\0' ||
301 (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
303 else if (ktype == KEY_RSA) {
304 return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
305 match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
306 match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
307 } else if (ktype == KEY_RSA_CERT) {
308 return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
310 match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
312 match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
315 return match_pattern_list(keyname, sigalgs, 0) == 1;
319 sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
321 char *tmp, *ret = NULL;
322 size_t i, nlen, rlen = 0;
323 const struct sshkey_impl *impl;
325 for (i = 0; keyimpls[i] != NULL; i++) {
327 if (impl->name == NULL)
329 if (!include_sigonly && impl->sigonly)
331 if ((certs_only && !impl->cert) || (plain_only && impl->cert))
335 nlen = strlen(impl->name);
336 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
341 memcpy(ret + rlen, impl->name, nlen + 1);
348 sshkey_names_valid2(const char *names, int allow_wildcard, int plain_only)
351 const struct sshkey_impl *impl;
354 if (names == NULL || strcmp(names, "") == 0)
356 if ((s = cp = strdup(names)) == NULL)
358 for ((p = strsep(&cp, ",")); p && *p != '\0';
359 (p = strsep(&cp, ","))) {
360 type = sshkey_type_from_name(p);
361 if (type == KEY_UNSPEC) {
362 if (allow_wildcard) {
364 * Try matching key types against the string.
365 * If any has a positive or negative match then
366 * the component is accepted.
369 for (i = 0; keyimpls[i] != NULL; i++) {
370 if (match_pattern_list(
371 keyimpls[i]->name, p, 0) != 0) {
381 } else if (plain_only && sshkey_type_is_cert(type)) {
391 sshkey_size(const struct sshkey *k)
393 const struct sshkey_impl *impl;
395 if ((impl = sshkey_impl_from_key(k)) == NULL)
397 if (impl->funcs->size != NULL)
398 return impl->funcs->size(k);
399 return impl->keybits;
403 sshkey_type_is_valid_ca(int type)
405 const struct sshkey_impl *impl;
407 if ((impl = sshkey_impl_from_type(type)) == NULL)
409 /* All non-certificate types may act as CAs */
414 sshkey_is_cert(const struct sshkey *k)
418 return sshkey_type_is_cert(k->type);
422 sshkey_is_sk(const struct sshkey *k)
426 switch (sshkey_type_plain(k->type)) {
435 /* Return the cert-less equivalent to a certified key type */
437 sshkey_type_plain(int type)
446 case KEY_ECDSA_SK_CERT:
448 case KEY_ED25519_CERT:
450 case KEY_ED25519_SK_CERT:
451 return KEY_ED25519_SK;
459 /* Return the cert equivalent to a plain key type */
461 sshkey_type_certified(int type)
469 return KEY_ECDSA_CERT;
471 return KEY_ECDSA_SK_CERT;
473 return KEY_ED25519_CERT;
475 return KEY_ED25519_SK_CERT;
477 return KEY_XMSS_CERT;
484 /* XXX: these are really begging for a table-driven approach */
486 sshkey_curve_name_to_nid(const char *name)
488 if (strcmp(name, "nistp256") == 0)
489 return NID_X9_62_prime256v1;
490 else if (strcmp(name, "nistp384") == 0)
491 return NID_secp384r1;
492 # ifdef OPENSSL_HAS_NISTP521
493 else if (strcmp(name, "nistp521") == 0)
494 return NID_secp521r1;
495 # endif /* OPENSSL_HAS_NISTP521 */
501 sshkey_curve_nid_to_bits(int nid)
504 case NID_X9_62_prime256v1:
508 # ifdef OPENSSL_HAS_NISTP521
511 # endif /* OPENSSL_HAS_NISTP521 */
518 sshkey_ecdsa_bits_to_nid(int bits)
522 return NID_X9_62_prime256v1;
524 return NID_secp384r1;
525 # ifdef OPENSSL_HAS_NISTP521
527 return NID_secp521r1;
528 # endif /* OPENSSL_HAS_NISTP521 */
535 sshkey_curve_nid_to_name(int nid)
538 case NID_X9_62_prime256v1:
542 # ifdef OPENSSL_HAS_NISTP521
545 # endif /* OPENSSL_HAS_NISTP521 */
552 sshkey_ec_nid_to_hash_alg(int nid)
554 int kbits = sshkey_curve_nid_to_bits(nid);
559 /* RFC5656 section 6.2.1 */
561 return SSH_DIGEST_SHA256;
562 else if (kbits <= 384)
563 return SSH_DIGEST_SHA384;
565 return SSH_DIGEST_SHA512;
567 #endif /* WITH_OPENSSL */
570 cert_free(struct sshkey_cert *cert)
576 sshbuf_free(cert->certblob);
577 sshbuf_free(cert->critical);
578 sshbuf_free(cert->extensions);
580 for (i = 0; i < cert->nprincipals; i++)
581 free(cert->principals[i]);
582 free(cert->principals);
583 sshkey_free(cert->signature_key);
584 free(cert->signature_type);
585 freezero(cert, sizeof(*cert));
588 static struct sshkey_cert *
591 struct sshkey_cert *cert;
593 if ((cert = calloc(1, sizeof(*cert))) == NULL)
595 if ((cert->certblob = sshbuf_new()) == NULL ||
596 (cert->critical = sshbuf_new()) == NULL ||
597 (cert->extensions = sshbuf_new()) == NULL) {
602 cert->principals = NULL;
603 cert->signature_key = NULL;
604 cert->signature_type = NULL;
612 const struct sshkey_impl *impl = NULL;
614 if (type != KEY_UNSPEC &&
615 (impl = sshkey_impl_from_type(type)) == NULL)
618 /* All non-certificate types may act as CAs */
619 if ((k = calloc(1, sizeof(*k))) == NULL)
623 if (impl != NULL && impl->funcs->alloc != NULL) {
624 if (impl->funcs->alloc(k) != 0) {
629 if (sshkey_is_cert(k)) {
630 if ((k->cert = cert_new()) == NULL) {
639 /* Frees common FIDO fields */
641 sshkey_sk_cleanup(struct sshkey *k)
643 free(k->sk_application);
644 sshbuf_free(k->sk_key_handle);
645 sshbuf_free(k->sk_reserved);
646 k->sk_application = NULL;
647 k->sk_key_handle = k->sk_reserved = NULL;
651 sshkey_free_contents(struct sshkey *k)
653 const struct sshkey_impl *impl;
657 if ((impl = sshkey_impl_from_type(k->type)) != NULL &&
658 impl->funcs->cleanup != NULL)
659 impl->funcs->cleanup(k);
660 if (sshkey_is_cert(k))
662 freezero(k->shielded_private, k->shielded_len);
663 freezero(k->shield_prekey, k->shield_prekey_len);
667 sshkey_free(struct sshkey *k)
669 sshkey_free_contents(k);
670 freezero(k, sizeof(*k));
674 cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
676 if (a == NULL && b == NULL)
678 if (a == NULL || b == NULL)
680 if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
682 if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
683 sshbuf_len(a->certblob)) != 0)
688 /* Compares FIDO-specific pubkey fields only */
690 sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b)
692 if (a->sk_application == NULL || b->sk_application == NULL)
694 if (strcmp(a->sk_application, b->sk_application) != 0)
700 * Compare public portions of key only, allowing comparisons between
701 * certificates and plain keys too.
704 sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
706 const struct sshkey_impl *impl;
708 if (a == NULL || b == NULL ||
709 sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
711 if ((impl = sshkey_impl_from_type(a->type)) == NULL)
713 return impl->funcs->equal(a, b);
717 sshkey_equal(const struct sshkey *a, const struct sshkey *b)
719 if (a == NULL || b == NULL || a->type != b->type)
721 if (sshkey_is_cert(a)) {
722 if (!cert_compare(a->cert, b->cert))
725 return sshkey_equal_public(a, b);
729 /* Serialise common FIDO key parts */
731 sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b)
735 if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0)
742 to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
743 enum sshkey_serialize_rep opts)
745 int type, ret = SSH_ERR_INTERNAL_ERROR;
746 const char *typename;
747 const struct sshkey_impl *impl;
750 return SSH_ERR_INVALID_ARGUMENT;
752 type = force_plain ? sshkey_type_plain(key->type) : key->type;
754 if (sshkey_type_is_cert(type)) {
755 if (key->cert == NULL)
756 return SSH_ERR_EXPECTED_CERT;
757 if (sshbuf_len(key->cert->certblob) == 0)
758 return SSH_ERR_KEY_LACKS_CERTBLOB;
759 /* Use the existing blob */
760 if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
764 if ((impl = sshkey_impl_from_type(type)) == NULL)
765 return SSH_ERR_KEY_TYPE_UNKNOWN;
767 typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
768 if ((ret = sshbuf_put_cstring(b, typename)) != 0)
770 return impl->funcs->serialize_public(key, b, opts);
774 sshkey_putb(const struct sshkey *key, struct sshbuf *b)
776 return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
780 sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
781 enum sshkey_serialize_rep opts)
786 if ((tmp = sshbuf_new()) == NULL)
787 return SSH_ERR_ALLOC_FAIL;
788 r = to_blob_buf(key, tmp, 0, opts);
790 r = sshbuf_put_stringb(b, tmp);
796 sshkey_puts(const struct sshkey *key, struct sshbuf *b)
798 return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
802 sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
804 return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
808 to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
809 enum sshkey_serialize_rep opts)
811 int ret = SSH_ERR_INTERNAL_ERROR;
813 struct sshbuf *b = NULL;
819 if ((b = sshbuf_new()) == NULL)
820 return SSH_ERR_ALLOC_FAIL;
821 if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
827 if ((*blobp = malloc(len)) == NULL) {
828 ret = SSH_ERR_ALLOC_FAIL;
831 memcpy(*blobp, sshbuf_ptr(b), len);
840 sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
842 return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
846 sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
848 return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
852 sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
853 u_char **retp, size_t *lenp)
855 u_char *blob = NULL, *ret = NULL;
857 int r = SSH_ERR_INTERNAL_ERROR;
863 if (ssh_digest_bytes(dgst_alg) == 0) {
864 r = SSH_ERR_INVALID_ARGUMENT;
867 if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
870 if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
871 r = SSH_ERR_ALLOC_FAIL;
874 if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
875 ret, SSH_DIGEST_MAX_LENGTH)) != 0)
883 *lenp = ssh_digest_bytes(dgst_alg);
888 freezero(blob, blob_len);
893 fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
896 size_t plen = strlen(alg) + 1;
897 size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
899 if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
901 strlcpy(ret, alg, rlen);
902 strlcat(ret, ":", rlen);
903 if (dgst_raw_len == 0)
905 if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
909 /* Trim padding characters from end */
910 ret[strcspn(ret, "=")] = '\0';
915 fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
917 char *retval, hex[5];
918 size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
920 if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
922 strlcpy(retval, alg, rlen);
923 strlcat(retval, ":", rlen);
924 for (i = 0; i < dgst_raw_len; i++) {
925 snprintf(hex, sizeof(hex), "%s%02x",
926 i > 0 ? ":" : "", dgst_raw[i]);
927 strlcat(retval, hex, rlen);
933 fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
935 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
936 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
937 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
938 u_int i, j = 0, rounds, seed = 1;
941 rounds = (dgst_raw_len / 2) + 1;
942 if ((retval = calloc(rounds, 6)) == NULL)
945 for (i = 0; i < rounds; i++) {
946 u_int idx0, idx1, idx2, idx3, idx4;
947 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
948 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
950 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
951 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
953 retval[j++] = vowels[idx0];
954 retval[j++] = consonants[idx1];
955 retval[j++] = vowels[idx2];
956 if ((i + 1) < rounds) {
957 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
958 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
959 retval[j++] = consonants[idx3];
961 retval[j++] = consonants[idx4];
963 ((((u_int)(dgst_raw[2 * i])) * 7) +
964 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
970 retval[j++] = vowels[idx0];
971 retval[j++] = consonants[idx1];
972 retval[j++] = vowels[idx2];
981 * Draw an ASCII-Art representing the fingerprint so human brain can
982 * profit from its built-in pattern recognition ability.
983 * This technique is called "random art" and can be found in some
984 * scientific publications like this original paper:
986 * "Hash Visualization: a New Technique to improve Real-World Security",
987 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
988 * Techniques and E-Commerce (CrypTEC '99)
989 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
991 * The subject came up in a talk by Dan Kaminsky, too.
993 * If you see the picture is different, the key is different.
994 * If the picture looks the same, you still know nothing.
996 * The algorithm used here is a worm crawling over a discrete plane,
997 * leaving a trace (augmenting the field) everywhere it goes.
998 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
999 * makes the respective movement vector be ignored for this turn.
1000 * Graphs are not unambiguous, because circles in graphs can be
1001 * walked in either direction.
1005 * Field sizes for the random art. Have to be odd, so the starting point
1006 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1007 * Else pictures would be too dense, and drawing the frame would
1008 * fail, too, because the key type would not fit in anymore.
1011 #define FLDSIZE_Y (FLDBASE + 1)
1012 #define FLDSIZE_X (FLDBASE * 2 + 1)
1014 fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
1015 const struct sshkey *k)
1018 * Chars to be used after each other every time the worm
1019 * intersects with itself. Matter of taste.
1021 char *augmentation_string = " .o+=*BOX@%&#/^SE";
1022 char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
1023 u_char field[FLDSIZE_X][FLDSIZE_Y];
1024 size_t i, tlen, hlen;
1027 size_t len = strlen(augmentation_string) - 1;
1029 if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1032 /* initialize field */
1033 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1037 /* process raw key */
1038 for (i = 0; i < dgst_raw_len; i++) {
1040 /* each byte conveys four 2-bit move commands */
1041 input = dgst_raw[i];
1042 for (b = 0; b < 4; b++) {
1043 /* evaluate 2 bit, rest is shifted later */
1044 x += (input & 0x1) ? 1 : -1;
1045 y += (input & 0x2) ? 1 : -1;
1047 /* assure we are still in bounds */
1050 x = MINIMUM(x, FLDSIZE_X - 1);
1051 y = MINIMUM(y, FLDSIZE_Y - 1);
1053 /* augment the field */
1054 if (field[x][y] < len - 2)
1060 /* mark starting point and end point*/
1061 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1064 /* assemble title */
1065 r = snprintf(title, sizeof(title), "[%s %u]",
1066 sshkey_type(k), sshkey_size(k));
1067 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1068 if (r < 0 || r > (int)sizeof(title))
1069 r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1070 tlen = (r <= 0) ? 0 : strlen(title);
1072 /* assemble hash ID. */
1073 r = snprintf(hash, sizeof(hash), "[%s]", alg);
1074 hlen = (r <= 0) ? 0 : strlen(hash);
1076 /* output upper border */
1079 for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1081 memcpy(p, title, tlen);
1083 for (i += tlen; i < FLDSIZE_X; i++)
1088 /* output content */
1089 for (y = 0; y < FLDSIZE_Y; y++) {
1091 for (x = 0; x < FLDSIZE_X; x++)
1092 *p++ = augmentation_string[MINIMUM(field[x][y], len)];
1097 /* output lower border */
1099 for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1101 memcpy(p, hash, hlen);
1103 for (i += hlen; i < FLDSIZE_X; i++)
1111 sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
1112 enum sshkey_fp_rep dgst_rep)
1114 char *retval = NULL;
1116 size_t dgst_raw_len;
1118 if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
1121 case SSH_FP_DEFAULT:
1122 if (dgst_alg == SSH_DIGEST_MD5) {
1123 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1124 dgst_raw, dgst_raw_len);
1126 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1127 dgst_raw, dgst_raw_len);
1131 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1132 dgst_raw, dgst_raw_len);
1135 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1136 dgst_raw, dgst_raw_len);
1138 case SSH_FP_BUBBLEBABBLE:
1139 retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1141 case SSH_FP_RANDOMART:
1142 retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1143 dgst_raw, dgst_raw_len, k);
1146 freezero(dgst_raw, dgst_raw_len);
1149 freezero(dgst_raw, dgst_raw_len);
1154 peek_type_nid(const char *s, size_t l, int *nid)
1156 const struct sshkey_impl *impl;
1159 for (i = 0; keyimpls[i] != NULL; i++) {
1161 if (impl->name == NULL || strlen(impl->name) != l)
1163 if (memcmp(s, impl->name, l) == 0) {
1165 if (key_type_is_ecdsa_variant(impl->type))
1173 /* XXX this can now be made const char * */
1175 sshkey_read(struct sshkey *ret, char **cpp)
1178 char *cp, *blobcopy;
1180 int r, type, curve_nid = -1;
1181 struct sshbuf *blob;
1184 return SSH_ERR_INVALID_ARGUMENT;
1185 if (ret->type != KEY_UNSPEC && sshkey_impl_from_type(ret->type) == NULL)
1186 return SSH_ERR_INVALID_ARGUMENT;
1190 space = strcspn(cp, " \t");
1191 if (space == strlen(cp))
1192 return SSH_ERR_INVALID_FORMAT;
1193 if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
1194 return SSH_ERR_INVALID_FORMAT;
1196 /* skip whitespace */
1197 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1200 return SSH_ERR_INVALID_FORMAT;
1201 if (ret->type != KEY_UNSPEC && ret->type != type)
1202 return SSH_ERR_KEY_TYPE_MISMATCH;
1203 if ((blob = sshbuf_new()) == NULL)
1204 return SSH_ERR_ALLOC_FAIL;
1206 /* find end of keyblob and decode */
1207 space = strcspn(cp, " \t");
1208 if ((blobcopy = strndup(cp, space)) == NULL) {
1210 return SSH_ERR_ALLOC_FAIL;
1212 if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
1218 if ((r = sshkey_fromb(blob, &k)) != 0) {
1224 /* skip whitespace and leave cp at start of comment */
1225 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1228 /* ensure type of blob matches type at start of line */
1229 if (k->type != type) {
1231 return SSH_ERR_KEY_TYPE_MISMATCH;
1233 if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
1235 return SSH_ERR_EC_CURVE_MISMATCH;
1238 /* Fill in ret from parsed key */
1239 sshkey_free_contents(ret);
1241 freezero(k, sizeof(*k));
1249 sshkey_to_base64(const struct sshkey *key, char **b64p)
1251 int r = SSH_ERR_INTERNAL_ERROR;
1252 struct sshbuf *b = NULL;
1257 if ((b = sshbuf_new()) == NULL)
1258 return SSH_ERR_ALLOC_FAIL;
1259 if ((r = sshkey_putb(key, b)) != 0)
1261 if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
1262 r = SSH_ERR_ALLOC_FAIL;
1278 sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1280 int r = SSH_ERR_INTERNAL_ERROR;
1283 if ((r = sshkey_to_base64(key, &uu)) != 0)
1285 if ((r = sshbuf_putf(b, "%s %s",
1286 sshkey_ssh_name(key), uu)) != 0)
1295 sshkey_write(const struct sshkey *key, FILE *f)
1297 struct sshbuf *b = NULL;
1298 int r = SSH_ERR_INTERNAL_ERROR;
1300 if ((b = sshbuf_new()) == NULL)
1301 return SSH_ERR_ALLOC_FAIL;
1302 if ((r = sshkey_format_text(key, b)) != 0)
1304 if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1307 r = SSH_ERR_SYSTEM_ERROR;
1318 sshkey_cert_type(const struct sshkey *k)
1320 switch (k->cert->type) {
1321 case SSH2_CERT_TYPE_USER:
1323 case SSH2_CERT_TYPE_HOST:
1331 sshkey_check_rsa_length(const struct sshkey *k, int min_size)
1334 const BIGNUM *rsa_n;
1337 if (k == NULL || k->rsa == NULL ||
1338 (k->type != KEY_RSA && k->type != KEY_RSA_CERT))
1340 RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
1341 nbits = BN_num_bits(rsa_n);
1342 if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1343 (min_size > 0 && nbits < min_size))
1344 return SSH_ERR_KEY_LENGTH;
1345 #endif /* WITH_OPENSSL */
1350 # ifdef OPENSSL_HAS_ECC
1352 sshkey_ecdsa_key_to_nid(EC_KEY *k)
1356 NID_X9_62_prime256v1,
1358 # ifdef OPENSSL_HAS_NISTP521
1360 # endif /* OPENSSL_HAS_NISTP521 */
1365 const EC_GROUP *g = EC_KEY_get0_group(k);
1368 * The group may be stored in a ASN.1 encoded private key in one of two
1369 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1370 * or explicit group parameters encoded into the key blob. Only the
1371 * "named group" case sets the group NID for us, but we can figure
1372 * it out for the other case by comparing against all the groups that
1375 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1377 for (i = 0; nids[i] != -1; i++) {
1378 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1380 if (EC_GROUP_cmp(g, eg, NULL) == 0)
1384 if (nids[i] != -1) {
1385 /* Use the group with the NID attached */
1386 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1387 if (EC_KEY_set_group(k, eg) != 1) {
1394 # endif /* OPENSSL_HAS_ECC */
1395 #endif /* WITH_OPENSSL */
1398 sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1401 int ret = SSH_ERR_INTERNAL_ERROR;
1402 const struct sshkey_impl *impl;
1404 if (keyp == NULL || sshkey_type_is_cert(type))
1405 return SSH_ERR_INVALID_ARGUMENT;
1407 if ((impl = sshkey_impl_from_type(type)) == NULL)
1408 return SSH_ERR_KEY_TYPE_UNKNOWN;
1409 if (impl->funcs->generate == NULL)
1410 return SSH_ERR_FEATURE_UNSUPPORTED;
1411 if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1412 return SSH_ERR_ALLOC_FAIL;
1414 if ((ret = impl->funcs->generate(k, bits)) != 0) {
1424 sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1427 const struct sshkey_cert *from;
1428 struct sshkey_cert *to;
1429 int r = SSH_ERR_INTERNAL_ERROR;
1431 if (to_key == NULL || (from = from_key->cert) == NULL)
1432 return SSH_ERR_INVALID_ARGUMENT;
1434 if ((to = cert_new()) == NULL)
1435 return SSH_ERR_ALLOC_FAIL;
1437 if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
1438 (r = sshbuf_putb(to->critical, from->critical)) != 0 ||
1439 (r = sshbuf_putb(to->extensions, from->extensions)) != 0)
1442 to->serial = from->serial;
1443 to->type = from->type;
1444 if (from->key_id == NULL)
1446 else if ((to->key_id = strdup(from->key_id)) == NULL) {
1447 r = SSH_ERR_ALLOC_FAIL;
1450 to->valid_after = from->valid_after;
1451 to->valid_before = from->valid_before;
1452 if (from->signature_key == NULL)
1453 to->signature_key = NULL;
1454 else if ((r = sshkey_from_private(from->signature_key,
1455 &to->signature_key)) != 0)
1457 if (from->signature_type != NULL &&
1458 (to->signature_type = strdup(from->signature_type)) == NULL) {
1459 r = SSH_ERR_ALLOC_FAIL;
1462 if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
1463 r = SSH_ERR_INVALID_ARGUMENT;
1466 if (from->nprincipals > 0) {
1467 if ((to->principals = calloc(from->nprincipals,
1468 sizeof(*to->principals))) == NULL) {
1469 r = SSH_ERR_ALLOC_FAIL;
1472 for (i = 0; i < from->nprincipals; i++) {
1473 to->principals[i] = strdup(from->principals[i]);
1474 if (to->principals[i] == NULL) {
1475 to->nprincipals = i;
1476 r = SSH_ERR_ALLOC_FAIL;
1481 to->nprincipals = from->nprincipals;
1484 cert_free(to_key->cert);
1494 sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to)
1496 /* Append security-key application string */
1497 if ((to->sk_application = strdup(from->sk_application)) == NULL)
1498 return SSH_ERR_ALLOC_FAIL;
1503 sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1505 struct sshkey *n = NULL;
1506 int r = SSH_ERR_INTERNAL_ERROR;
1507 const struct sshkey_impl *impl;
1510 if ((impl = sshkey_impl_from_key(k)) == NULL)
1511 return SSH_ERR_KEY_TYPE_UNKNOWN;
1512 if ((n = sshkey_new(k->type)) == NULL) {
1513 r = SSH_ERR_ALLOC_FAIL;
1516 if ((r = impl->funcs->copy_public(k, n)) != 0)
1518 if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
1530 sshkey_is_shielded(struct sshkey *k)
1532 return k != NULL && k->shielded_private != NULL;
1536 sshkey_shield_private(struct sshkey *k)
1538 struct sshbuf *prvbuf = NULL;
1539 u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
1540 struct sshcipher_ctx *cctx = NULL;
1541 const struct sshcipher *cipher;
1542 size_t i, enclen = 0;
1543 struct sshkey *kswap = NULL, tmp;
1544 int r = SSH_ERR_INTERNAL_ERROR;
1547 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
1549 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
1550 r = SSH_ERR_INVALID_ARGUMENT;
1553 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
1554 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
1555 r = SSH_ERR_INTERNAL_ERROR;
1559 /* Prepare a random pre-key, and from it an ephemeral key */
1560 if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) {
1561 r = SSH_ERR_ALLOC_FAIL;
1564 arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
1565 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
1566 prekey, SSHKEY_SHIELD_PREKEY_LEN,
1567 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
1570 fprintf(stderr, "%s: key+iv\n", __func__);
1571 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
1574 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
1575 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
1578 /* Serialise and encrypt the private key using the ephemeral key */
1579 if ((prvbuf = sshbuf_new()) == NULL) {
1580 r = SSH_ERR_ALLOC_FAIL;
1583 if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
1585 if ((r = sshkey_private_serialize_opt(k, prvbuf,
1586 SSHKEY_SERIALIZE_SHIELD)) != 0)
1588 /* pad to cipher blocksize */
1590 while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
1591 if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
1595 fprintf(stderr, "%s: serialised\n", __func__);
1596 sshbuf_dump(prvbuf, stderr);
1599 enclen = sshbuf_len(prvbuf);
1600 if ((enc = malloc(enclen)) == NULL) {
1601 r = SSH_ERR_ALLOC_FAIL;
1604 if ((r = cipher_crypt(cctx, 0, enc,
1605 sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
1608 fprintf(stderr, "%s: encrypted\n", __func__);
1609 sshbuf_dump_data(enc, enclen, stderr);
1612 /* Make a scrubbed, public-only copy of our private key argument */
1613 if ((r = sshkey_from_private(k, &kswap)) != 0)
1616 /* Swap the private key out (it will be destroyed below) */
1621 /* Insert the shielded key into our argument */
1622 k->shielded_private = enc;
1623 k->shielded_len = enclen;
1624 k->shield_prekey = prekey;
1625 k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
1626 enc = prekey = NULL; /* transferred */
1629 /* preserve key fields that are required for correct operation */
1630 k->sk_flags = kswap->sk_flags;
1636 /* XXX behaviour on error - invalidate original private key? */
1638 explicit_bzero(keyiv, sizeof(keyiv));
1639 explicit_bzero(&tmp, sizeof(tmp));
1640 freezero(enc, enclen);
1641 freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN);
1643 sshbuf_free(prvbuf);
1647 /* Check deterministic padding after private key */
1649 private2_check_padding(struct sshbuf *decrypted)
1656 while (sshbuf_len(decrypted)) {
1657 if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
1659 if (pad != (++i & 0xff)) {
1660 r = SSH_ERR_INVALID_FORMAT;
1667 explicit_bzero(&pad, sizeof(pad));
1668 explicit_bzero(&i, sizeof(i));
1673 sshkey_unshield_private(struct sshkey *k)
1675 struct sshbuf *prvbuf = NULL;
1676 u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
1677 struct sshcipher_ctx *cctx = NULL;
1678 const struct sshcipher *cipher;
1679 struct sshkey *kswap = NULL, tmp;
1680 int r = SSH_ERR_INTERNAL_ERROR;
1683 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
1685 if (!sshkey_is_shielded(k))
1686 return 0; /* nothing to do */
1688 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
1689 r = SSH_ERR_INVALID_ARGUMENT;
1692 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
1693 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
1694 r = SSH_ERR_INTERNAL_ERROR;
1697 /* check size of shielded key blob */
1698 if (k->shielded_len < cipher_blocksize(cipher) ||
1699 (k->shielded_len % cipher_blocksize(cipher)) != 0) {
1700 r = SSH_ERR_INVALID_FORMAT;
1704 /* Calculate the ephemeral key from the prekey */
1705 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
1706 k->shield_prekey, k->shield_prekey_len,
1707 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
1709 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
1710 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
1713 fprintf(stderr, "%s: key+iv\n", __func__);
1714 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
1718 /* Decrypt and parse the shielded private key using the ephemeral key */
1719 if ((prvbuf = sshbuf_new()) == NULL) {
1720 r = SSH_ERR_ALLOC_FAIL;
1723 if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
1727 fprintf(stderr, "%s: encrypted\n", __func__);
1728 sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
1730 if ((r = cipher_crypt(cctx, 0, cp,
1731 k->shielded_private, k->shielded_len, 0, 0)) != 0)
1734 fprintf(stderr, "%s: serialised\n", __func__);
1735 sshbuf_dump(prvbuf, stderr);
1737 /* Parse private key */
1738 if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
1741 if ((r = private2_check_padding(prvbuf)) != 0)
1744 /* Swap the parsed key back into place */
1754 explicit_bzero(keyiv, sizeof(keyiv));
1755 explicit_bzero(&tmp, sizeof(tmp));
1757 sshbuf_free(prvbuf);
1762 cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
1764 struct sshbuf *principals = NULL, *crit = NULL;
1765 struct sshbuf *exts = NULL, *ca = NULL;
1767 size_t signed_len = 0, slen = 0, kidlen = 0;
1768 int ret = SSH_ERR_INTERNAL_ERROR;
1770 /* Copy the entire key blob for verification and later serialisation */
1771 if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
1774 /* Parse body of certificate up to signature */
1775 if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
1776 (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
1777 (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
1778 (ret = sshbuf_froms(b, &principals)) != 0 ||
1779 (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
1780 (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
1781 (ret = sshbuf_froms(b, &crit)) != 0 ||
1782 (ret = sshbuf_froms(b, &exts)) != 0 ||
1783 (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
1784 (ret = sshbuf_froms(b, &ca)) != 0) {
1785 /* XXX debug print error for ret */
1786 ret = SSH_ERR_INVALID_FORMAT;
1790 /* Signature is left in the buffer so we can calculate this length */
1791 signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
1793 if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
1794 ret = SSH_ERR_INVALID_FORMAT;
1798 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1799 key->cert->type != SSH2_CERT_TYPE_HOST) {
1800 ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
1804 /* Parse principals section */
1805 while (sshbuf_len(principals) > 0) {
1806 char *principal = NULL;
1807 char **oprincipals = NULL;
1809 if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
1810 ret = SSH_ERR_INVALID_FORMAT;
1813 if ((ret = sshbuf_get_cstring(principals, &principal,
1815 ret = SSH_ERR_INVALID_FORMAT;
1818 oprincipals = key->cert->principals;
1819 key->cert->principals = recallocarray(key->cert->principals,
1820 key->cert->nprincipals, key->cert->nprincipals + 1,
1821 sizeof(*key->cert->principals));
1822 if (key->cert->principals == NULL) {
1824 key->cert->principals = oprincipals;
1825 ret = SSH_ERR_ALLOC_FAIL;
1828 key->cert->principals[key->cert->nprincipals++] = principal;
1832 * Stash a copies of the critical options and extensions sections
1835 if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
1837 (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
1841 * Validate critical options and extensions sections format.
1843 while (sshbuf_len(crit) != 0) {
1844 if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
1845 (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
1846 sshbuf_reset(key->cert->critical);
1847 ret = SSH_ERR_INVALID_FORMAT;
1851 while (exts != NULL && sshbuf_len(exts) != 0) {
1852 if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
1853 (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
1854 sshbuf_reset(key->cert->extensions);
1855 ret = SSH_ERR_INVALID_FORMAT;
1860 /* Parse CA key and check signature */
1861 if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
1862 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1865 if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
1866 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1869 if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
1870 sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
1872 if ((ret = sshkey_get_sigtype(sig, slen,
1873 &key->cert->signature_type)) != 0)
1882 sshbuf_free(principals);
1888 sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key)
1890 /* Parse additional security-key application string */
1891 if (sshbuf_get_cstring(b, &key->sk_application, NULL) != 0)
1892 return SSH_ERR_INVALID_FORMAT;
1897 sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
1900 int type, ret = SSH_ERR_INTERNAL_ERROR;
1902 struct sshkey *key = NULL;
1903 struct sshbuf *copy;
1904 const struct sshkey_impl *impl;
1906 #ifdef DEBUG_PK /* XXX */
1907 sshbuf_dump(b, stderr);
1911 if ((copy = sshbuf_fromb(b)) == NULL) {
1912 ret = SSH_ERR_ALLOC_FAIL;
1915 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
1916 ret = SSH_ERR_INVALID_FORMAT;
1920 type = sshkey_type_from_name(ktype);
1921 if (!allow_cert && sshkey_type_is_cert(type)) {
1922 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1925 if ((impl = sshkey_impl_from_type(type)) == NULL) {
1926 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
1929 if ((key = sshkey_new(type)) == NULL) {
1930 ret = SSH_ERR_ALLOC_FAIL;
1933 if (sshkey_type_is_cert(type)) {
1934 /* Skip nonce that precedes all certificates */
1935 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
1936 ret = SSH_ERR_INVALID_FORMAT;
1940 if ((ret = impl->funcs->deserialize_public(ktype, b, key)) != 0)
1943 /* Parse certificate potion */
1944 if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
1947 if (key != NULL && sshbuf_len(b) != 0) {
1948 ret = SSH_ERR_INVALID_FORMAT;
1964 sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
1969 if ((b = sshbuf_from(blob, blen)) == NULL)
1970 return SSH_ERR_ALLOC_FAIL;
1971 r = sshkey_from_blob_internal(b, keyp, 1);
1977 sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
1979 return sshkey_from_blob_internal(b, keyp, 1);
1983 sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
1988 if ((r = sshbuf_froms(buf, &b)) != 0)
1990 r = sshkey_from_blob_internal(b, keyp, 1);
1996 sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
1999 struct sshbuf *b = NULL;
2000 char *sigtype = NULL;
2002 if (sigtypep != NULL)
2004 if ((b = sshbuf_from(sig, siglen)) == NULL)
2005 return SSH_ERR_ALLOC_FAIL;
2006 if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
2009 if (sigtypep != NULL) {
2010 *sigtypep = sigtype;
2022 * Checks whether a certificate's signature type is allowed.
2023 * Returns 0 (success) if the certificate signature type appears in the
2024 * "allowed" pattern-list, or the key is not a certificate to begin with.
2025 * Otherwise returns a ssherr.h code.
2028 sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
2030 if (key == NULL || allowed == NULL)
2031 return SSH_ERR_INVALID_ARGUMENT;
2032 if (!sshkey_type_is_cert(key->type))
2034 if (key->cert == NULL || key->cert->signature_type == NULL)
2035 return SSH_ERR_INVALID_ARGUMENT;
2036 if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
2037 return SSH_ERR_SIGN_ALG_UNSUPPORTED;
2042 * Returns the expected signature algorithm for a given public key algorithm.
2045 sshkey_sigalg_by_name(const char *name)
2047 const struct sshkey_impl *impl;
2050 for (i = 0; keyimpls[i] != NULL; i++) {
2052 if (strcmp(impl->name, name) != 0)
2054 if (impl->sigalg != NULL)
2055 return impl->sigalg;
2058 return sshkey_ssh_name_from_type_nid(
2059 sshkey_type_plain(impl->type), impl->nid);
2065 * Verifies that the signature algorithm appearing inside the signature blob
2066 * matches that which was requested.
2069 sshkey_check_sigtype(const u_char *sig, size_t siglen,
2070 const char *requested_alg)
2072 const char *expected_alg;
2073 char *sigtype = NULL;
2076 if (requested_alg == NULL)
2078 if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
2079 return SSH_ERR_INVALID_ARGUMENT;
2080 if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
2082 r = strcmp(expected_alg, sigtype) == 0;
2084 return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
2088 sshkey_sign(struct sshkey *key,
2089 u_char **sigp, size_t *lenp,
2090 const u_char *data, size_t datalen,
2091 const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
2093 int was_shielded = sshkey_is_shielded(key);
2094 int r2, r = SSH_ERR_INTERNAL_ERROR;
2095 const struct sshkey_impl *impl;
2101 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2102 return SSH_ERR_INVALID_ARGUMENT;
2103 if ((impl = sshkey_impl_from_key(key)) == NULL)
2104 return SSH_ERR_KEY_TYPE_UNKNOWN;
2105 if ((r = sshkey_unshield_private(key)) != 0)
2107 if (sshkey_is_sk(key)) {
2108 r = sshsk_sign(sk_provider, key, sigp, lenp, data,
2109 datalen, compat, sk_pin);
2111 if (impl->funcs->sign == NULL)
2112 r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
2114 r = impl->funcs->sign(key, sigp, lenp, data, datalen,
2115 alg, sk_provider, sk_pin, compat);
2118 if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
2124 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2125 * If "alg" specified, then the signature must use that algorithm.
2128 sshkey_verify(const struct sshkey *key,
2129 const u_char *sig, size_t siglen,
2130 const u_char *data, size_t dlen, const char *alg, u_int compat,
2131 struct sshkey_sig_details **detailsp)
2133 const struct sshkey_impl *impl;
2135 if (detailsp != NULL)
2137 if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2138 return SSH_ERR_INVALID_ARGUMENT;
2139 if ((impl = sshkey_impl_from_key(key)) == NULL)
2140 return SSH_ERR_KEY_TYPE_UNKNOWN;
2141 return impl->funcs->verify(key, sig, siglen, data, dlen,
2142 alg, compat, detailsp);
2145 /* Convert a plain key to their _CERT equivalent */
2147 sshkey_to_certified(struct sshkey *k)
2151 if ((newtype = sshkey_type_certified(k->type)) == -1)
2152 return SSH_ERR_INVALID_ARGUMENT;
2153 if ((k->cert = cert_new()) == NULL)
2154 return SSH_ERR_ALLOC_FAIL;
2159 /* Convert a certificate to its raw key equivalent */
2161 sshkey_drop_cert(struct sshkey *k)
2163 if (!sshkey_type_is_cert(k->type))
2164 return SSH_ERR_KEY_TYPE_UNKNOWN;
2167 k->type = sshkey_type_plain(k->type);
2171 /* Sign a certified key, (re-)generating the signed certblob. */
2173 sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
2174 const char *sk_provider, const char *sk_pin,
2175 sshkey_certify_signer *signer, void *signer_ctx)
2177 const struct sshkey_impl *impl;
2178 struct sshbuf *principals = NULL;
2179 u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2180 size_t i, ca_len, sig_len;
2181 int ret = SSH_ERR_INTERNAL_ERROR;
2182 struct sshbuf *cert = NULL;
2183 char *sigtype = NULL;
2185 if (k == NULL || k->cert == NULL ||
2186 k->cert->certblob == NULL || ca == NULL)
2187 return SSH_ERR_INVALID_ARGUMENT;
2188 if (!sshkey_is_cert(k))
2189 return SSH_ERR_KEY_TYPE_UNKNOWN;
2190 if (!sshkey_type_is_valid_ca(ca->type))
2191 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2192 if ((impl = sshkey_impl_from_key(k)) == NULL)
2193 return SSH_ERR_INTERNAL_ERROR;
2196 * If no alg specified as argument but a signature_type was set,
2197 * then prefer that. If both were specified, then they must match.
2200 alg = k->cert->signature_type;
2201 else if (k->cert->signature_type != NULL &&
2202 strcmp(alg, k->cert->signature_type) != 0)
2203 return SSH_ERR_INVALID_ARGUMENT;
2206 * If no signing algorithm or signature_type was specified and we're
2207 * using a RSA key, then default to a good signature algorithm.
2209 if (alg == NULL && ca->type == KEY_RSA)
2210 alg = "rsa-sha2-512";
2212 if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2213 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2215 cert = k->cert->certblob; /* for readability */
2217 if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2220 /* -v01 certs put nonce first */
2221 arc4random_buf(&nonce, sizeof(nonce));
2222 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2225 /* Public key next */
2226 if ((ret = impl->funcs->serialize_public(k, cert,
2227 SSHKEY_SERIALIZE_DEFAULT)) != 0)
2230 /* Then remaining cert fields */
2231 if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
2232 (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
2233 (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
2236 if ((principals = sshbuf_new()) == NULL) {
2237 ret = SSH_ERR_ALLOC_FAIL;
2240 for (i = 0; i < k->cert->nprincipals; i++) {
2241 if ((ret = sshbuf_put_cstring(principals,
2242 k->cert->principals[i])) != 0)
2245 if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
2246 (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
2247 (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
2248 (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
2249 (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
2250 (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
2251 (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
2254 /* Sign the whole mess */
2255 if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
2256 sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
2258 /* Check and update signature_type against what was actually used */
2259 if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
2261 if (alg != NULL && strcmp(alg, sigtype) != 0) {
2262 ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
2265 if (k->cert->signature_type == NULL) {
2266 k->cert->signature_type = sigtype;
2269 /* Append signature and we are done */
2270 if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
2279 sshbuf_free(principals);
2284 default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
2285 const u_char *data, size_t datalen,
2286 const char *alg, const char *sk_provider, const char *sk_pin,
2287 u_int compat, void *ctx)
2290 return SSH_ERR_INVALID_ARGUMENT;
2291 return sshkey_sign(key, sigp, lenp, data, datalen, alg,
2292 sk_provider, sk_pin, compat);
2296 sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
2297 const char *sk_provider, const char *sk_pin)
2299 return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
2300 default_key_sign, NULL);
2304 sshkey_cert_check_authority(const struct sshkey *k,
2305 int want_host, int require_principal, int wildcard_pattern,
2306 uint64_t verify_time, const char *name, const char **reason)
2308 u_int i, principal_matches;
2311 return SSH_ERR_INVALID_ARGUMENT;
2312 if (!sshkey_is_cert(k)) {
2313 *reason = "Key is not a certificate";
2314 return SSH_ERR_KEY_CERT_INVALID;
2317 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
2318 *reason = "Certificate invalid: not a host certificate";
2319 return SSH_ERR_KEY_CERT_INVALID;
2322 if (k->cert->type != SSH2_CERT_TYPE_USER) {
2323 *reason = "Certificate invalid: not a user certificate";
2324 return SSH_ERR_KEY_CERT_INVALID;
2327 if (verify_time < k->cert->valid_after) {
2328 *reason = "Certificate invalid: not yet valid";
2329 return SSH_ERR_KEY_CERT_INVALID;
2331 if (verify_time >= k->cert->valid_before) {
2332 *reason = "Certificate invalid: expired";
2333 return SSH_ERR_KEY_CERT_INVALID;
2335 if (k->cert->nprincipals == 0) {
2336 if (require_principal) {
2337 *reason = "Certificate lacks principal list";
2338 return SSH_ERR_KEY_CERT_INVALID;
2340 } else if (name != NULL) {
2341 principal_matches = 0;
2342 for (i = 0; i < k->cert->nprincipals; i++) {
2343 if (wildcard_pattern) {
2344 if (match_pattern(k->cert->principals[i],
2346 principal_matches = 1;
2349 } else if (strcmp(name, k->cert->principals[i]) == 0) {
2350 principal_matches = 1;
2354 if (!principal_matches) {
2355 *reason = "Certificate invalid: name is not a listed "
2357 return SSH_ERR_KEY_CERT_INVALID;
2364 sshkey_cert_check_authority_now(const struct sshkey *k,
2365 int want_host, int require_principal, int wildcard_pattern,
2366 const char *name, const char **reason)
2370 if ((now = time(NULL)) < 0) {
2371 /* yikes - system clock before epoch! */
2372 *reason = "Certificate invalid: not yet valid";
2373 return SSH_ERR_KEY_CERT_INVALID;
2375 return sshkey_cert_check_authority(k, want_host, require_principal,
2376 wildcard_pattern, (uint64_t)now, name, reason);
2380 sshkey_cert_check_host(const struct sshkey *key, const char *host,
2381 int wildcard_principals, const char *ca_sign_algorithms,
2382 const char **reason)
2386 if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals,
2387 host, reason)) != 0)
2389 if (sshbuf_len(key->cert->critical) != 0) {
2390 *reason = "Certificate contains unsupported critical options";
2391 return SSH_ERR_KEY_CERT_INVALID;
2393 if (ca_sign_algorithms != NULL &&
2394 (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
2395 *reason = "Certificate signed with disallowed algorithm";
2396 return SSH_ERR_KEY_CERT_INVALID;
2402 sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
2404 char from[32], to[32], ret[128];
2407 if (cert->valid_after == 0 &&
2408 cert->valid_before == 0xffffffffffffffffULL)
2409 return strlcpy(s, "forever", l);
2411 if (cert->valid_after != 0)
2412 format_absolute_time(cert->valid_after, from, sizeof(from));
2413 if (cert->valid_before != 0xffffffffffffffffULL)
2414 format_absolute_time(cert->valid_before, to, sizeof(to));
2416 if (cert->valid_after == 0)
2417 snprintf(ret, sizeof(ret), "before %s", to);
2418 else if (cert->valid_before == 0xffffffffffffffffULL)
2419 snprintf(ret, sizeof(ret), "after %s", from);
2421 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
2423 return strlcpy(s, ret, l);
2426 /* Common serialization for FIDO private keys */
2428 sshkey_serialize_private_sk(const struct sshkey *key, struct sshbuf *b)
2432 if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
2433 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
2434 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
2435 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
2442 sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
2443 enum sshkey_serialize_rep opts)
2445 int r = SSH_ERR_INTERNAL_ERROR;
2446 int was_shielded = sshkey_is_shielded(key);
2447 struct sshbuf *b = NULL;
2448 const struct sshkey_impl *impl;
2450 if ((impl = sshkey_impl_from_key(key)) == NULL)
2451 return SSH_ERR_INTERNAL_ERROR;
2452 if ((r = sshkey_unshield_private(key)) != 0)
2454 if ((b = sshbuf_new()) == NULL)
2455 return SSH_ERR_ALLOC_FAIL;
2456 if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
2458 if (sshkey_is_cert(key)) {
2459 if (key->cert == NULL ||
2460 sshbuf_len(key->cert->certblob) == 0) {
2461 r = SSH_ERR_INVALID_ARGUMENT;
2464 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0)
2467 if ((r = impl->funcs->serialize_private(key, b, opts)) != 0)
2471 * success (but we still need to append the output to buf after
2472 * possibly re-shielding the private key)
2477 r = sshkey_shield_private(key);
2479 r = sshbuf_putb(buf, b);
2486 sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
2488 return sshkey_private_serialize_opt(key, b,
2489 SSHKEY_SERIALIZE_DEFAULT);
2492 /* Shared deserialization of FIDO private key components */
2494 sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k)
2498 if ((k->sk_key_handle = sshbuf_new()) == NULL ||
2499 (k->sk_reserved = sshbuf_new()) == NULL)
2500 return SSH_ERR_ALLOC_FAIL;
2501 if ((r = sshbuf_get_cstring(buf, &k->sk_application, NULL)) != 0 ||
2502 (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
2503 (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
2504 (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
2511 sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
2513 const struct sshkey_impl *impl;
2515 char *expect_sk_application = NULL;
2516 u_char *expect_ed25519_pk = NULL;
2517 struct sshkey *k = NULL;
2518 int type, r = SSH_ERR_INTERNAL_ERROR;
2522 if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
2524 type = sshkey_type_from_name(tname);
2525 if (sshkey_type_is_cert(type)) {
2527 * Certificate key private keys begin with the certificate
2528 * itself. Make sure this matches the type of the enclosing
2531 if ((r = sshkey_froms(buf, &k)) != 0)
2533 if (k->type != type) {
2534 r = SSH_ERR_KEY_CERT_MISMATCH;
2537 /* For ECDSA keys, the group must match too */
2538 if (k->type == KEY_ECDSA &&
2539 k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
2540 r = SSH_ERR_KEY_CERT_MISMATCH;
2544 * Several fields are redundant between certificate and
2545 * private key body, we require these to match.
2547 expect_sk_application = k->sk_application;
2548 expect_ed25519_pk = k->ed25519_pk;
2549 k->sk_application = NULL;
2550 k->ed25519_pk = NULL;
2551 /* XXX xmss too or refactor */
2553 if ((k = sshkey_new(type)) == NULL) {
2554 r = SSH_ERR_ALLOC_FAIL;
2558 if ((impl = sshkey_impl_from_type(type)) == NULL) {
2559 r = SSH_ERR_INTERNAL_ERROR;
2562 if ((r = impl->funcs->deserialize_private(tname, buf, k)) != 0)
2565 /* XXX xmss too or refactor */
2566 if ((expect_sk_application != NULL && (k->sk_application == NULL ||
2567 strcmp(expect_sk_application, k->sk_application) != 0)) ||
2568 (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
2569 memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
2570 r = SSH_ERR_KEY_CERT_MISMATCH;
2582 free(expect_sk_application);
2583 free(expect_ed25519_pk);
2587 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2589 sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2591 EC_POINT *nq = NULL;
2592 BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
2593 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2596 * NB. This assumes OpenSSL has already verified that the public
2597 * point lies on the curve. This is done by EC_POINT_oct2point()
2598 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
2599 * reachable with public points not unmarshalled using
2600 * EC_POINT_oct2point then the caller will need to explicitly check.
2604 * We shouldn't ever hit this case because bignum_get_ecpoint()
2605 * refuses to load GF2m points.
2607 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2608 NID_X9_62_prime_field)
2612 if (EC_POINT_is_at_infinity(group, public))
2615 if ((x = BN_new()) == NULL ||
2616 (y = BN_new()) == NULL ||
2617 (order = BN_new()) == NULL ||
2618 (tmp = BN_new()) == NULL) {
2619 ret = SSH_ERR_ALLOC_FAIL;
2623 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2624 if (EC_GROUP_get_order(group, order, NULL) != 1 ||
2625 EC_POINT_get_affine_coordinates_GFp(group, public,
2627 ret = SSH_ERR_LIBCRYPTO_ERROR;
2630 if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
2631 BN_num_bits(y) <= BN_num_bits(order) / 2)
2634 /* nQ == infinity (n == order of subgroup) */
2635 if ((nq = EC_POINT_new(group)) == NULL) {
2636 ret = SSH_ERR_ALLOC_FAIL;
2639 if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
2640 ret = SSH_ERR_LIBCRYPTO_ERROR;
2643 if (EC_POINT_is_at_infinity(group, nq) != 1)
2646 /* x < order - 1, y < order - 1 */
2647 if (!BN_sub(tmp, order, BN_value_one())) {
2648 ret = SSH_ERR_LIBCRYPTO_ERROR;
2651 if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
2657 BN_clear_free(order);
2664 sshkey_ec_validate_private(const EC_KEY *key)
2666 BIGNUM *order = NULL, *tmp = NULL;
2667 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2669 if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
2670 ret = SSH_ERR_ALLOC_FAIL;
2674 /* log2(private) > log2(order)/2 */
2675 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
2676 ret = SSH_ERR_LIBCRYPTO_ERROR;
2679 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2680 BN_num_bits(order) / 2)
2683 /* private < order - 1 */
2684 if (!BN_sub(tmp, order, BN_value_one())) {
2685 ret = SSH_ERR_LIBCRYPTO_ERROR;
2688 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
2692 BN_clear_free(order);
2698 sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2700 BIGNUM *x = NULL, *y = NULL;
2702 if (point == NULL) {
2703 fputs("point=(NULL)\n", stderr);
2706 if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
2707 fprintf(stderr, "%s: BN_new failed\n", __func__);
2710 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2711 NID_X9_62_prime_field) {
2712 fprintf(stderr, "%s: group is not a prime field\n", __func__);
2715 if (EC_POINT_get_affine_coordinates_GFp(group, point,
2717 fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
2721 fputs("x=", stderr);
2722 BN_print_fp(stderr, x);
2723 fputs("\ny=", stderr);
2724 BN_print_fp(stderr, y);
2725 fputs("\n", stderr);
2732 sshkey_dump_ec_key(const EC_KEY *key)
2734 const BIGNUM *exponent;
2736 sshkey_dump_ec_point(EC_KEY_get0_group(key),
2737 EC_KEY_get0_public_key(key));
2738 fputs("exponent=", stderr);
2739 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
2740 fputs("(NULL)", stderr);
2742 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
2743 fputs("\n", stderr);
2745 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2748 sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
2749 const char *passphrase, const char *comment, const char *ciphername,
2752 u_char *cp, *key = NULL, *pubkeyblob = NULL;
2753 u_char salt[SALT_LEN];
2754 size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
2756 int r = SSH_ERR_INTERNAL_ERROR;
2757 struct sshcipher_ctx *ciphercontext = NULL;
2758 const struct sshcipher *cipher;
2759 const char *kdfname = KDFNAME;
2760 struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
2763 rounds = DEFAULT_ROUNDS;
2764 if (passphrase == NULL || !strlen(passphrase)) {
2765 ciphername = "none";
2767 } else if (ciphername == NULL)
2768 ciphername = DEFAULT_CIPHERNAME;
2769 if ((cipher = cipher_by_name(ciphername)) == NULL) {
2770 r = SSH_ERR_INVALID_ARGUMENT;
2774 if ((kdf = sshbuf_new()) == NULL ||
2775 (encoded = sshbuf_new()) == NULL ||
2776 (encrypted = sshbuf_new()) == NULL) {
2777 r = SSH_ERR_ALLOC_FAIL;
2780 blocksize = cipher_blocksize(cipher);
2781 keylen = cipher_keylen(cipher);
2782 ivlen = cipher_ivlen(cipher);
2783 authlen = cipher_authlen(cipher);
2784 if ((key = calloc(1, keylen + ivlen)) == NULL) {
2785 r = SSH_ERR_ALLOC_FAIL;
2788 if (strcmp(kdfname, "bcrypt") == 0) {
2789 arc4random_buf(salt, SALT_LEN);
2790 if (bcrypt_pbkdf(passphrase, strlen(passphrase),
2791 salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
2792 r = SSH_ERR_INVALID_ARGUMENT;
2795 if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
2796 (r = sshbuf_put_u32(kdf, rounds)) != 0)
2798 } else if (strcmp(kdfname, "none") != 0) {
2799 /* Unsupported KDF type */
2800 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2803 if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
2804 key + keylen, ivlen, 1)) != 0)
2807 if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
2808 (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
2809 (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
2810 (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
2811 (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
2812 (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
2813 (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
2816 /* set up the buffer that will be encrypted */
2818 /* Random check bytes */
2819 check = arc4random();
2820 if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
2821 (r = sshbuf_put_u32(encrypted, check)) != 0)
2824 /* append private key and comment*/
2825 if ((r = sshkey_private_serialize_opt(prv, encrypted,
2826 SSHKEY_SERIALIZE_FULL)) != 0 ||
2827 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
2832 while (sshbuf_len(encrypted) % blocksize) {
2833 if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
2837 /* length in destination buffer */
2838 if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
2842 if ((r = sshbuf_reserve(encoded,
2843 sshbuf_len(encrypted) + authlen, &cp)) != 0)
2845 if ((r = cipher_crypt(ciphercontext, 0, cp,
2846 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
2851 /* assemble uuencoded key */
2852 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
2853 (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
2854 (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
2862 sshbuf_free(encoded);
2863 sshbuf_free(encrypted);
2864 cipher_free(ciphercontext);
2865 explicit_bzero(salt, sizeof(salt));
2867 freezero(key, keylen + ivlen);
2868 if (pubkeyblob != NULL)
2869 freezero(pubkeyblob, pubkeylen);
2874 private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
2880 struct sshbuf *encoded = NULL, *decoded = NULL;
2882 if (blob == NULL || decodedp == NULL)
2883 return SSH_ERR_INVALID_ARGUMENT;
2887 if ((encoded = sshbuf_new()) == NULL ||
2888 (decoded = sshbuf_new()) == NULL) {
2889 r = SSH_ERR_ALLOC_FAIL;
2893 /* check preamble */
2894 cp = sshbuf_ptr(blob);
2895 encoded_len = sshbuf_len(blob);
2896 if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
2897 memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
2898 r = SSH_ERR_INVALID_FORMAT;
2901 cp += MARK_BEGIN_LEN;
2902 encoded_len -= MARK_BEGIN_LEN;
2904 /* Look for end marker, removing whitespace as we go */
2905 while (encoded_len > 0) {
2906 if (*cp != '\n' && *cp != '\r') {
2907 if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
2914 if (encoded_len >= MARK_END_LEN &&
2915 memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
2917 if ((r = sshbuf_put_u8(encoded, 0)) != 0)
2923 if (encoded_len == 0) {
2924 r = SSH_ERR_INVALID_FORMAT;
2929 if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
2933 if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
2934 memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
2935 r = SSH_ERR_INVALID_FORMAT;
2939 *decodedp = decoded;
2943 sshbuf_free(encoded);
2944 sshbuf_free(decoded);
2949 private2_decrypt(struct sshbuf *decoded, const char *passphrase,
2950 struct sshbuf **decryptedp, struct sshkey **pubkeyp)
2952 char *ciphername = NULL, *kdfname = NULL;
2953 const struct sshcipher *cipher = NULL;
2954 int r = SSH_ERR_INTERNAL_ERROR;
2955 size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
2956 struct sshbuf *kdf = NULL, *decrypted = NULL;
2957 struct sshcipher_ctx *ciphercontext = NULL;
2958 struct sshkey *pubkey = NULL;
2959 u_char *key = NULL, *salt = NULL, *dp;
2960 u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
2962 if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
2963 return SSH_ERR_INVALID_ARGUMENT;
2968 if ((decrypted = sshbuf_new()) == NULL) {
2969 r = SSH_ERR_ALLOC_FAIL;
2973 /* parse public portion of key */
2974 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
2975 (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
2976 (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
2977 (r = sshbuf_froms(decoded, &kdf)) != 0 ||
2978 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
2982 /* XXX only one key supported at present */
2983 r = SSH_ERR_INVALID_FORMAT;
2987 if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
2988 (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
2991 if ((cipher = cipher_by_name(ciphername)) == NULL) {
2992 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2995 if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
2996 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2999 if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
3000 r = SSH_ERR_INVALID_FORMAT;
3003 if ((passphrase == NULL || strlen(passphrase) == 0) &&
3004 strcmp(kdfname, "none") != 0) {
3005 /* passphrase required */
3006 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3010 /* check size of encrypted key blob */
3011 blocksize = cipher_blocksize(cipher);
3012 if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
3013 r = SSH_ERR_INVALID_FORMAT;
3018 keylen = cipher_keylen(cipher);
3019 ivlen = cipher_ivlen(cipher);
3020 authlen = cipher_authlen(cipher);
3021 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3022 r = SSH_ERR_ALLOC_FAIL;
3025 if (strcmp(kdfname, "bcrypt") == 0) {
3026 if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
3027 (r = sshbuf_get_u32(kdf, &rounds)) != 0)
3029 if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
3030 key, keylen + ivlen, rounds) < 0) {
3031 r = SSH_ERR_INVALID_FORMAT;
3036 /* check that an appropriate amount of auth data is present */
3037 if (sshbuf_len(decoded) < authlen ||
3038 sshbuf_len(decoded) - authlen < encrypted_len) {
3039 r = SSH_ERR_INVALID_FORMAT;
3043 /* decrypt private portion of key */
3044 if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
3045 (r = cipher_init(&ciphercontext, cipher, key, keylen,
3046 key + keylen, ivlen, 0)) != 0)
3048 if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
3049 encrypted_len, 0, authlen)) != 0) {
3050 /* an integrity error here indicates an incorrect passphrase */
3051 if (r == SSH_ERR_MAC_INVALID)
3052 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3055 if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
3057 /* there should be no trailing data */
3058 if (sshbuf_len(decoded) != 0) {
3059 r = SSH_ERR_INVALID_FORMAT;
3063 /* check check bytes */
3064 if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
3065 (r = sshbuf_get_u32(decrypted, &check2)) != 0)
3067 if (check1 != check2) {
3068 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3072 *decryptedp = decrypted;
3078 cipher_free(ciphercontext);
3081 sshkey_free(pubkey);
3083 explicit_bzero(salt, slen);
3087 explicit_bzero(key, keylen + ivlen);
3091 sshbuf_free(decrypted);
3096 sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
3097 struct sshkey **keyp, char **commentp)
3099 char *comment = NULL;
3100 int r = SSH_ERR_INTERNAL_ERROR;
3101 struct sshbuf *decoded = NULL, *decrypted = NULL;
3102 struct sshkey *k = NULL, *pubkey = NULL;
3106 if (commentp != NULL)
3109 /* Undo base64 encoding and decrypt the private section */
3110 if ((r = private2_uudecode(blob, &decoded)) != 0 ||
3111 (r = private2_decrypt(decoded, passphrase,
3112 &decrypted, &pubkey)) != 0)
3115 if (type != KEY_UNSPEC &&
3116 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
3117 r = SSH_ERR_KEY_TYPE_MISMATCH;
3121 /* Load the private key and comment */
3122 if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
3123 (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
3126 /* Check deterministic padding after private section */
3127 if ((r = private2_check_padding(decrypted)) != 0)
3130 /* Check that the public key in the envelope matches the private key */
3131 if (!sshkey_equal(pubkey, k)) {
3132 r = SSH_ERR_INVALID_FORMAT;
3142 if (commentp != NULL) {
3143 *commentp = comment;
3148 sshbuf_free(decoded);
3149 sshbuf_free(decrypted);
3151 sshkey_free(pubkey);
3156 sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
3157 struct sshkey **keyp)
3159 int r = SSH_ERR_INTERNAL_ERROR;
3160 struct sshbuf *decoded = NULL;
3161 struct sshkey *pubkey = NULL;
3167 if ((r = private2_uudecode(blob, &decoded)) != 0)
3169 /* parse public key from unencrypted envelope */
3170 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
3171 (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
3172 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
3173 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
3174 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
3178 /* XXX only one key supported at present */
3179 r = SSH_ERR_INVALID_FORMAT;
3183 /* Parse the public key */
3184 if ((r = sshkey_froms(decoded, &pubkey)) != 0)
3187 if (type != KEY_UNSPEC &&
3188 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
3189 r = SSH_ERR_KEY_TYPE_MISMATCH;
3200 sshbuf_free(decoded);
3201 sshkey_free(pubkey);
3206 /* convert SSH v2 key to PEM or PKCS#8 format */
3208 sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
3209 int format, const char *_passphrase, const char *comment)
3211 int was_shielded = sshkey_is_shielded(key);
3213 int blen, len = strlen(_passphrase);
3214 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
3215 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
3218 struct sshbuf *blob;
3219 EVP_PKEY *pkey = NULL;
3221 if (len > 0 && len <= 4)
3222 return SSH_ERR_PASSPHRASE_TOO_SHORT;
3223 if ((blob = sshbuf_new()) == NULL)
3224 return SSH_ERR_ALLOC_FAIL;
3225 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
3226 r = SSH_ERR_ALLOC_FAIL;
3229 if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
3230 r = SSH_ERR_ALLOC_FAIL;
3233 if ((r = sshkey_unshield_private(key)) != 0)
3236 switch (key->type) {
3239 if (format == SSHKEY_PRIVATE_PEM) {
3240 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
3241 cipher, passphrase, len, NULL, NULL);
3243 success = EVP_PKEY_set1_DSA(pkey, key->dsa);
3247 #ifdef OPENSSL_HAS_ECC
3249 if (format == SSHKEY_PRIVATE_PEM) {
3250 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
3251 cipher, passphrase, len, NULL, NULL);
3253 success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
3258 if (format == SSHKEY_PRIVATE_PEM) {
3259 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
3260 cipher, passphrase, len, NULL, NULL);
3262 success = EVP_PKEY_set1_RSA(pkey, key->rsa);
3270 r = SSH_ERR_LIBCRYPTO_ERROR;
3273 if (format == SSHKEY_PRIVATE_PKCS8) {
3274 if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
3275 passphrase, len, NULL, NULL)) == 0) {
3276 r = SSH_ERR_LIBCRYPTO_ERROR;
3280 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
3281 r = SSH_ERR_INTERNAL_ERROR;
3284 if ((r = sshbuf_put(blob, bptr, blen)) != 0)
3289 r = sshkey_shield_private(key);
3291 r = sshbuf_putb(buf, blob);
3293 EVP_PKEY_free(pkey);
3298 #endif /* WITH_OPENSSL */
3300 /* Serialise "key" to buffer "blob" */
3302 sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
3303 const char *passphrase, const char *comment,
3304 int format, const char *openssh_format_cipher, int openssh_format_rounds)
3306 switch (key->type) {
3311 break; /* see below */
3312 #endif /* WITH_OPENSSL */
3314 case KEY_ED25519_SK:
3317 #endif /* WITH_XMSS */
3320 #endif /* WITH_OPENSSL */
3321 return sshkey_private_to_blob2(key, blob, passphrase,
3322 comment, openssh_format_cipher, openssh_format_rounds);
3324 return SSH_ERR_KEY_TYPE_UNKNOWN;
3329 case SSHKEY_PRIVATE_OPENSSH:
3330 return sshkey_private_to_blob2(key, blob, passphrase,
3331 comment, openssh_format_cipher, openssh_format_rounds);
3332 case SSHKEY_PRIVATE_PEM:
3333 case SSHKEY_PRIVATE_PKCS8:
3334 return sshkey_private_to_blob_pem_pkcs8(key, blob,
3335 format, passphrase, comment);
3337 return SSH_ERR_INVALID_ARGUMENT;
3339 #endif /* WITH_OPENSSL */
3344 translate_libcrypto_error(unsigned long pem_err)
3346 int pem_reason = ERR_GET_REASON(pem_err);
3348 switch (ERR_GET_LIB(pem_err)) {
3350 switch (pem_reason) {
3351 case PEM_R_BAD_PASSWORD_READ:
3352 #ifdef PEM_R_PROBLEMS_GETTING_PASSWORD
3353 case PEM_R_PROBLEMS_GETTING_PASSWORD:
3355 #ifdef PEM_R_BAD_DECRYPT
3356 case PEM_R_BAD_DECRYPT:
3358 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3360 return SSH_ERR_INVALID_FORMAT;
3363 switch (pem_reason) {
3364 #ifdef EVP_R_BAD_DECRYPT
3365 case EVP_R_BAD_DECRYPT:
3366 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3368 #ifdef EVP_R_BN_DECODE_ERROR
3369 case EVP_R_BN_DECODE_ERROR:
3371 case EVP_R_DECODE_ERROR:
3372 #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
3373 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
3375 return SSH_ERR_INVALID_FORMAT;
3377 return SSH_ERR_LIBCRYPTO_ERROR;
3380 return SSH_ERR_INVALID_FORMAT;
3382 return SSH_ERR_LIBCRYPTO_ERROR;
3386 clear_libcrypto_errors(void)
3388 while (ERR_get_error() != 0)
3393 * Translate OpenSSL error codes to determine whether
3394 * passphrase is required/incorrect.
3397 convert_libcrypto_error(void)
3400 * Some password errors are reported at the beginning
3401 * of the error queue.
3403 if (translate_libcrypto_error(ERR_peek_error()) ==
3404 SSH_ERR_KEY_WRONG_PASSPHRASE)
3405 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3406 return translate_libcrypto_error(ERR_peek_last_error());
3410 pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
3412 char *p = (char *)u;
3415 if (p == NULL || (len = strlen(p)) == 0)
3417 if (size < 0 || len > (size_t)size)
3419 memcpy(buf, p, len);
3424 sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
3425 const char *passphrase, struct sshkey **keyp)
3427 EVP_PKEY *pk = NULL;
3428 struct sshkey *prv = NULL;
3435 if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
3436 return SSH_ERR_ALLOC_FAIL;
3437 if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
3438 (int)sshbuf_len(blob)) {
3439 r = SSH_ERR_ALLOC_FAIL;
3443 clear_libcrypto_errors();
3444 if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
3445 (char *)passphrase)) == NULL) {
3447 * libcrypto may return various ASN.1 errors when attempting
3448 * to parse a key with an incorrect passphrase.
3449 * Treat all format errors as "incorrect passphrase" if a
3450 * passphrase was supplied.
3452 if (passphrase != NULL && *passphrase != '\0')
3453 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3455 r = convert_libcrypto_error();
3458 if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
3459 (type == KEY_UNSPEC || type == KEY_RSA)) {
3460 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3461 r = SSH_ERR_ALLOC_FAIL;
3464 prv->rsa = EVP_PKEY_get1_RSA(pk);
3465 prv->type = KEY_RSA;
3467 RSA_print_fp(stderr, prv->rsa, 8);
3469 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3470 r = SSH_ERR_LIBCRYPTO_ERROR;
3473 if ((r = sshkey_check_rsa_length(prv, 0)) != 0)
3476 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
3477 (type == KEY_UNSPEC || type == KEY_DSA)) {
3478 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3479 r = SSH_ERR_ALLOC_FAIL;
3482 prv->dsa = EVP_PKEY_get1_DSA(pk);
3483 prv->type = KEY_DSA;
3485 DSA_print_fp(stderr, prv->dsa, 8);
3488 #ifdef OPENSSL_HAS_ECC
3489 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
3490 (type == KEY_UNSPEC || type == KEY_ECDSA)) {
3491 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3492 r = SSH_ERR_ALLOC_FAIL;
3495 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
3496 prv->type = KEY_ECDSA;
3497 prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
3498 if (prv->ecdsa_nid == -1 ||
3499 sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
3500 sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
3501 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
3502 sshkey_ec_validate_private(prv->ecdsa) != 0) {
3503 r = SSH_ERR_INVALID_FORMAT;
3507 if (prv != NULL && prv->ecdsa != NULL)
3508 sshkey_dump_ec_key(prv->ecdsa);
3510 #endif /* OPENSSL_HAS_ECC */
3511 #ifdef OPENSSL_HAS_ED25519
3512 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_ED25519 &&
3513 (type == KEY_UNSPEC || type == KEY_ED25519)) {
3516 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL ||
3517 (prv->ed25519_sk = calloc(1, ED25519_SK_SZ)) == NULL ||
3518 (prv->ed25519_pk = calloc(1, ED25519_PK_SZ)) == NULL) {
3519 r = SSH_ERR_ALLOC_FAIL;
3522 prv->type = KEY_ED25519;
3523 len = ED25519_PK_SZ;
3524 if (!EVP_PKEY_get_raw_public_key(pk, prv->ed25519_pk, &len)) {
3525 r = SSH_ERR_LIBCRYPTO_ERROR;
3528 if (len != ED25519_PK_SZ) {
3529 r = SSH_ERR_INVALID_FORMAT;
3532 len = ED25519_SK_SZ - ED25519_PK_SZ;
3533 if (!EVP_PKEY_get_raw_private_key(pk, prv->ed25519_sk, &len)) {
3534 r = SSH_ERR_LIBCRYPTO_ERROR;
3537 if (len != ED25519_SK_SZ - ED25519_PK_SZ) {
3538 r = SSH_ERR_INVALID_FORMAT;
3541 /* Append the public key to our private key */
3542 memcpy(prv->ed25519_sk + (ED25519_SK_SZ - ED25519_PK_SZ),
3543 prv->ed25519_pk, ED25519_PK_SZ);
3545 sshbuf_dump_data(prv->ed25519_sk, ED25519_SK_SZ, stderr);
3547 #endif /* OPENSSL_HAS_ED25519 */
3549 r = SSH_ERR_INVALID_FORMAT;
3563 #endif /* WITH_OPENSSL */
3566 sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
3567 const char *passphrase, struct sshkey **keyp, char **commentp)
3569 int r = SSH_ERR_INTERNAL_ERROR;
3573 if (commentp != NULL)
3578 /* No fallback for new-format-only keys */
3579 return sshkey_parse_private2(blob, type, passphrase,
3582 r = sshkey_parse_private2(blob, type, passphrase, keyp,
3584 /* Only fallback to PEM parser if a format error occurred. */
3585 if (r != SSH_ERR_INVALID_FORMAT)
3588 return sshkey_parse_private_pem_fileblob(blob, type,
3591 return SSH_ERR_INVALID_FORMAT;
3592 #endif /* WITH_OPENSSL */
3597 sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
3598 struct sshkey **keyp, char **commentp)
3602 if (commentp != NULL)
3605 return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
3606 passphrase, keyp, commentp);
3610 sshkey_sig_details_free(struct sshkey_sig_details *details)
3612 freezero(details, sizeof(*details));
3616 sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
3617 struct sshkey **pubkeyp)
3619 int r = SSH_ERR_INTERNAL_ERROR;
3621 if (pubkeyp != NULL)
3623 /* only new-format private keys bundle a public key inside */
3624 if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
3631 * serialize the key with the current state and forward the state
3635 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
3636 u_int32_t maxsign, int printerror)
3641 sshkey_type_plain(k->type) != KEY_XMSS)
3642 return sshkey_private_serialize_opt(k, b,
3643 SSHKEY_SERIALIZE_DEFAULT);
3644 if ((r = sshkey_xmss_get_state(k, printerror)) != 0 ||
3645 (r = sshkey_private_serialize_opt(k, b,
3646 SSHKEY_SERIALIZE_STATE)) != 0 ||
3647 (r = sshkey_xmss_forward_state(k, maxsign)) != 0)
3651 if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) {
3659 sshkey_signatures_left(const struct sshkey *k)
3661 if (sshkey_type_plain(k->type) == KEY_XMSS)
3662 return sshkey_xmss_signatures_left(k);
3667 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
3669 if (sshkey_type_plain(k->type) != KEY_XMSS)
3670 return SSH_ERR_INVALID_ARGUMENT;
3671 return sshkey_xmss_enable_maxsign(k, maxsign);
3675 sshkey_set_filename(struct sshkey *k, const char *filename)
3678 return SSH_ERR_INVALID_ARGUMENT;
3679 if (sshkey_type_plain(k->type) != KEY_XMSS)
3681 if (filename == NULL)
3682 return SSH_ERR_INVALID_ARGUMENT;
3683 if ((k->xmss_filename = strdup(filename)) == NULL)
3684 return SSH_ERR_ALLOC_FAIL;
3689 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
3690 u_int32_t maxsign, int printerror)
3692 return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
3696 sshkey_signatures_left(const struct sshkey *k)
3702 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
3704 return SSH_ERR_INVALID_ARGUMENT;
3708 sshkey_set_filename(struct sshkey *k, const char *filename)
3711 return SSH_ERR_INVALID_ARGUMENT;
3714 #endif /* WITH_XMSS */