1 /* $OpenBSD: sshkey.c,v 1.134 2022/10/28 02:47:04 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"
49 #endif /* HAVE_UTIL_H */
57 #define SSHKEY_INTERNAL
63 #include "sshkey-xmss.h"
64 #include "xmss_fast.h"
67 #include "openbsd-compat/openssl-compat.h"
69 /* openssh private key file format */
70 #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
71 #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
72 #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
73 #define MARK_END_LEN (sizeof(MARK_END) - 1)
74 #define KDFNAME "bcrypt"
75 #define AUTH_MAGIC "openssh-key-v1"
77 #define DEFAULT_CIPHERNAME "aes256-ctr"
78 #define DEFAULT_ROUNDS 16
80 /* Version identification string for SSH v1 identity files. */
81 #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
84 * Constants relating to "shielding" support; protection of keys expected
85 * to remain in memory for long durations
87 #define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024)
88 #define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */
89 #define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512
91 int sshkey_private_serialize_opt(struct sshkey *key,
92 struct sshbuf *buf, enum sshkey_serialize_rep);
93 static int sshkey_from_blob_internal(struct sshbuf *buf,
94 struct sshkey **keyp, int allow_cert);
96 /* Supported key types */
97 extern const struct sshkey_impl sshkey_ed25519_impl;
98 extern const struct sshkey_impl sshkey_ed25519_cert_impl;
99 extern const struct sshkey_impl sshkey_ed25519_sk_impl;
100 extern const struct sshkey_impl sshkey_ed25519_sk_cert_impl;
102 # ifdef OPENSSL_HAS_ECC
104 extern const struct sshkey_impl sshkey_ecdsa_sk_impl;
105 extern const struct sshkey_impl sshkey_ecdsa_sk_cert_impl;
106 extern const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl;
107 # endif /* ENABLE_SK */
108 extern const struct sshkey_impl sshkey_ecdsa_nistp256_impl;
109 extern const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl;
110 extern const struct sshkey_impl sshkey_ecdsa_nistp384_impl;
111 extern const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl;
112 # ifdef OPENSSL_HAS_NISTP521
113 extern const struct sshkey_impl sshkey_ecdsa_nistp521_impl;
114 extern const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl;
115 # endif /* OPENSSL_HAS_NISTP521 */
116 # endif /* OPENSSL_HAS_ECC */
117 extern const struct sshkey_impl sshkey_rsa_impl;
118 extern const struct sshkey_impl sshkey_rsa_cert_impl;
119 extern const struct sshkey_impl sshkey_rsa_sha256_impl;
120 extern const struct sshkey_impl sshkey_rsa_sha256_cert_impl;
121 extern const struct sshkey_impl sshkey_rsa_sha512_impl;
122 extern const struct sshkey_impl sshkey_rsa_sha512_cert_impl;
123 extern const struct sshkey_impl sshkey_dss_impl;
124 extern const struct sshkey_impl sshkey_dsa_cert_impl;
125 #endif /* WITH_OPENSSL */
127 extern const struct sshkey_impl sshkey_xmss_impl;
128 extern const struct sshkey_impl sshkey_xmss_cert_impl;
131 const struct sshkey_impl * const keyimpls[] = {
132 &sshkey_ed25519_impl,
133 &sshkey_ed25519_cert_impl,
135 &sshkey_ed25519_sk_impl,
136 &sshkey_ed25519_sk_cert_impl,
139 # ifdef OPENSSL_HAS_ECC
140 &sshkey_ecdsa_nistp256_impl,
141 &sshkey_ecdsa_nistp256_cert_impl,
142 &sshkey_ecdsa_nistp384_impl,
143 &sshkey_ecdsa_nistp384_cert_impl,
144 # ifdef OPENSSL_HAS_NISTP521
145 &sshkey_ecdsa_nistp521_impl,
146 &sshkey_ecdsa_nistp521_cert_impl,
147 # endif /* OPENSSL_HAS_NISTP521 */
149 &sshkey_ecdsa_sk_impl,
150 &sshkey_ecdsa_sk_cert_impl,
151 &sshkey_ecdsa_sk_webauthn_impl,
152 # endif /* ENABLE_SK */
153 # endif /* OPENSSL_HAS_ECC */
155 &sshkey_dsa_cert_impl,
157 &sshkey_rsa_cert_impl,
158 &sshkey_rsa_sha256_impl,
159 &sshkey_rsa_sha256_cert_impl,
160 &sshkey_rsa_sha512_impl,
161 &sshkey_rsa_sha512_cert_impl,
162 #endif /* WITH_OPENSSL */
165 &sshkey_xmss_cert_impl,
170 static const struct sshkey_impl *
171 sshkey_impl_from_type(int type)
175 for (i = 0; keyimpls[i] != NULL; i++) {
176 if (keyimpls[i]->type == type)
182 static const struct sshkey_impl *
183 sshkey_impl_from_type_nid(int type, int nid)
187 for (i = 0; keyimpls[i] != NULL; i++) {
188 if (keyimpls[i]->type == type &&
189 (keyimpls[i]->nid == 0 || keyimpls[i]->nid == nid))
195 static const struct sshkey_impl *
196 sshkey_impl_from_key(const struct sshkey *k)
200 return sshkey_impl_from_type_nid(k->type, k->ecdsa_nid);
204 sshkey_type(const struct sshkey *k)
206 const struct sshkey_impl *impl;
208 if ((impl = sshkey_impl_from_key(k)) == NULL)
210 return impl->shortname;
214 sshkey_ssh_name_from_type_nid(int type, int nid)
216 const struct sshkey_impl *impl;
218 if ((impl = sshkey_impl_from_type_nid(type, nid)) == NULL)
219 return "ssh-unknown";
224 sshkey_type_is_cert(int type)
226 const struct sshkey_impl *impl;
228 if ((impl = sshkey_impl_from_type(type)) == NULL)
234 sshkey_ssh_name(const struct sshkey *k)
236 return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
240 sshkey_ssh_name_plain(const struct sshkey *k)
242 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
247 sshkey_type_from_name(const char *name)
250 const struct sshkey_impl *impl;
252 for (i = 0; keyimpls[i] != NULL; i++) {
254 /* Only allow shortname matches for plain key types */
255 if ((impl->name != NULL && strcmp(name, impl->name) == 0) ||
256 (!impl->cert && strcasecmp(impl->shortname, name) == 0))
263 key_type_is_ecdsa_variant(int type)
269 case KEY_ECDSA_SK_CERT:
276 sshkey_ecdsa_nid_from_name(const char *name)
280 for (i = 0; keyimpls[i] != NULL; i++) {
281 if (!key_type_is_ecdsa_variant(keyimpls[i]->type))
283 if (keyimpls[i]->name != NULL &&
284 strcmp(name, keyimpls[i]->name) == 0)
285 return keyimpls[i]->nid;
291 sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
295 if (sigalgs == NULL || *sigalgs == '\0' ||
296 (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
298 else if (ktype == KEY_RSA) {
299 return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
300 match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
301 match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
302 } else if (ktype == KEY_RSA_CERT) {
303 return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
305 match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
307 match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
310 return match_pattern_list(keyname, sigalgs, 0) == 1;
314 sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
316 char *tmp, *ret = NULL;
317 size_t i, nlen, rlen = 0;
318 const struct sshkey_impl *impl;
320 for (i = 0; keyimpls[i] != NULL; i++) {
322 if (impl->name == NULL)
324 if (!include_sigonly && impl->sigonly)
326 if ((certs_only && !impl->cert) || (plain_only && impl->cert))
330 nlen = strlen(impl->name);
331 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
336 memcpy(ret + rlen, impl->name, nlen + 1);
343 sshkey_names_valid2(const char *names, int allow_wildcard)
346 const struct sshkey_impl *impl;
349 if (names == NULL || strcmp(names, "") == 0)
351 if ((s = cp = strdup(names)) == NULL)
353 for ((p = strsep(&cp, ",")); p && *p != '\0';
354 (p = strsep(&cp, ","))) {
355 type = sshkey_type_from_name(p);
356 if (type == KEY_UNSPEC) {
357 if (allow_wildcard) {
359 * Try matching key types against the string.
360 * If any has a positive or negative match then
361 * the component is accepted.
364 for (i = 0; keyimpls[i] != NULL; i++) {
365 if (match_pattern_list(
366 keyimpls[i]->name, p, 0) != 0) {
383 sshkey_size(const struct sshkey *k)
385 const struct sshkey_impl *impl;
387 if ((impl = sshkey_impl_from_key(k)) == NULL)
389 if (impl->funcs->size != NULL)
390 return impl->funcs->size(k);
391 return impl->keybits;
395 sshkey_type_is_valid_ca(int type)
397 const struct sshkey_impl *impl;
399 if ((impl = sshkey_impl_from_type(type)) == NULL)
401 /* All non-certificate types may act as CAs */
406 sshkey_is_cert(const struct sshkey *k)
410 return sshkey_type_is_cert(k->type);
414 sshkey_is_sk(const struct sshkey *k)
418 switch (sshkey_type_plain(k->type)) {
427 /* Return the cert-less equivalent to a certified key type */
429 sshkey_type_plain(int type)
438 case KEY_ECDSA_SK_CERT:
440 case KEY_ED25519_CERT:
442 case KEY_ED25519_SK_CERT:
443 return KEY_ED25519_SK;
451 /* Return the cert equivalent to a plain key type */
453 sshkey_type_certified(int type)
461 return KEY_ECDSA_CERT;
463 return KEY_ECDSA_SK_CERT;
465 return KEY_ED25519_CERT;
467 return KEY_ED25519_SK_CERT;
469 return KEY_XMSS_CERT;
476 /* XXX: these are really begging for a table-driven approach */
478 sshkey_curve_name_to_nid(const char *name)
480 if (strcmp(name, "nistp256") == 0)
481 return NID_X9_62_prime256v1;
482 else if (strcmp(name, "nistp384") == 0)
483 return NID_secp384r1;
484 # ifdef OPENSSL_HAS_NISTP521
485 else if (strcmp(name, "nistp521") == 0)
486 return NID_secp521r1;
487 # endif /* OPENSSL_HAS_NISTP521 */
493 sshkey_curve_nid_to_bits(int nid)
496 case NID_X9_62_prime256v1:
500 # ifdef OPENSSL_HAS_NISTP521
503 # endif /* OPENSSL_HAS_NISTP521 */
510 sshkey_ecdsa_bits_to_nid(int bits)
514 return NID_X9_62_prime256v1;
516 return NID_secp384r1;
517 # ifdef OPENSSL_HAS_NISTP521
519 return NID_secp521r1;
520 # endif /* OPENSSL_HAS_NISTP521 */
527 sshkey_curve_nid_to_name(int nid)
530 case NID_X9_62_prime256v1:
534 # ifdef OPENSSL_HAS_NISTP521
537 # endif /* OPENSSL_HAS_NISTP521 */
544 sshkey_ec_nid_to_hash_alg(int nid)
546 int kbits = sshkey_curve_nid_to_bits(nid);
551 /* RFC5656 section 6.2.1 */
553 return SSH_DIGEST_SHA256;
554 else if (kbits <= 384)
555 return SSH_DIGEST_SHA384;
557 return SSH_DIGEST_SHA512;
559 #endif /* WITH_OPENSSL */
562 cert_free(struct sshkey_cert *cert)
568 sshbuf_free(cert->certblob);
569 sshbuf_free(cert->critical);
570 sshbuf_free(cert->extensions);
572 for (i = 0; i < cert->nprincipals; i++)
573 free(cert->principals[i]);
574 free(cert->principals);
575 sshkey_free(cert->signature_key);
576 free(cert->signature_type);
577 freezero(cert, sizeof(*cert));
580 static struct sshkey_cert *
583 struct sshkey_cert *cert;
585 if ((cert = calloc(1, sizeof(*cert))) == NULL)
587 if ((cert->certblob = sshbuf_new()) == NULL ||
588 (cert->critical = sshbuf_new()) == NULL ||
589 (cert->extensions = sshbuf_new()) == NULL) {
594 cert->principals = NULL;
595 cert->signature_key = NULL;
596 cert->signature_type = NULL;
604 const struct sshkey_impl *impl = NULL;
606 if (type != KEY_UNSPEC &&
607 (impl = sshkey_impl_from_type(type)) == NULL)
610 /* All non-certificate types may act as CAs */
611 if ((k = calloc(1, sizeof(*k))) == NULL)
615 if (impl != NULL && impl->funcs->alloc != NULL) {
616 if (impl->funcs->alloc(k) != 0) {
621 if (sshkey_is_cert(k)) {
622 if ((k->cert = cert_new()) == NULL) {
631 /* Frees common FIDO fields */
633 sshkey_sk_cleanup(struct sshkey *k)
635 free(k->sk_application);
636 sshbuf_free(k->sk_key_handle);
637 sshbuf_free(k->sk_reserved);
638 k->sk_application = NULL;
639 k->sk_key_handle = k->sk_reserved = NULL;
643 sshkey_free_contents(struct sshkey *k)
645 const struct sshkey_impl *impl;
649 if ((impl = sshkey_impl_from_type(k->type)) != NULL &&
650 impl->funcs->cleanup != NULL)
651 impl->funcs->cleanup(k);
652 if (sshkey_is_cert(k))
654 freezero(k->shielded_private, k->shielded_len);
655 freezero(k->shield_prekey, k->shield_prekey_len);
659 sshkey_free(struct sshkey *k)
661 sshkey_free_contents(k);
662 freezero(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)
680 /* Compares FIDO-specific pubkey fields only */
682 sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b)
684 if (a->sk_application == NULL || b->sk_application == NULL)
686 if (strcmp(a->sk_application, b->sk_application) != 0)
692 * Compare public portions of key only, allowing comparisons between
693 * certificates and plain keys too.
696 sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
698 const struct sshkey_impl *impl;
700 if (a == NULL || b == NULL ||
701 sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
703 if ((impl = sshkey_impl_from_type(a->type)) == NULL)
705 return impl->funcs->equal(a, b);
709 sshkey_equal(const struct sshkey *a, const struct sshkey *b)
711 if (a == NULL || b == NULL || a->type != b->type)
713 if (sshkey_is_cert(a)) {
714 if (!cert_compare(a->cert, b->cert))
717 return sshkey_equal_public(a, b);
721 /* Serialise common FIDO key parts */
723 sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b)
727 if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0)
734 to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
735 enum sshkey_serialize_rep opts)
737 int type, ret = SSH_ERR_INTERNAL_ERROR;
738 const char *typename;
739 const struct sshkey_impl *impl;
742 return SSH_ERR_INVALID_ARGUMENT;
744 type = force_plain ? sshkey_type_plain(key->type) : key->type;
746 if (sshkey_type_is_cert(type)) {
747 if (key->cert == NULL)
748 return SSH_ERR_EXPECTED_CERT;
749 if (sshbuf_len(key->cert->certblob) == 0)
750 return SSH_ERR_KEY_LACKS_CERTBLOB;
751 /* Use the existing blob */
752 if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
756 if ((impl = sshkey_impl_from_type(type)) == NULL)
757 return SSH_ERR_KEY_TYPE_UNKNOWN;
759 typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
760 if ((ret = sshbuf_put_cstring(b, typename)) != 0)
762 return impl->funcs->serialize_public(key, b, opts);
766 sshkey_putb(const struct sshkey *key, struct sshbuf *b)
768 return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
772 sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
773 enum sshkey_serialize_rep opts)
778 if ((tmp = sshbuf_new()) == NULL)
779 return SSH_ERR_ALLOC_FAIL;
780 r = to_blob_buf(key, tmp, 0, opts);
782 r = sshbuf_put_stringb(b, tmp);
788 sshkey_puts(const struct sshkey *key, struct sshbuf *b)
790 return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
794 sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
796 return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
800 to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
801 enum sshkey_serialize_rep opts)
803 int ret = SSH_ERR_INTERNAL_ERROR;
805 struct sshbuf *b = NULL;
811 if ((b = sshbuf_new()) == NULL)
812 return SSH_ERR_ALLOC_FAIL;
813 if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
819 if ((*blobp = malloc(len)) == NULL) {
820 ret = SSH_ERR_ALLOC_FAIL;
823 memcpy(*blobp, sshbuf_ptr(b), len);
832 sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
834 return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
838 sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
840 return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
844 sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
845 u_char **retp, size_t *lenp)
847 u_char *blob = NULL, *ret = NULL;
849 int r = SSH_ERR_INTERNAL_ERROR;
855 if (ssh_digest_bytes(dgst_alg) == 0) {
856 r = SSH_ERR_INVALID_ARGUMENT;
859 if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
862 if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
863 r = SSH_ERR_ALLOC_FAIL;
866 if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
867 ret, SSH_DIGEST_MAX_LENGTH)) != 0)
875 *lenp = ssh_digest_bytes(dgst_alg);
880 freezero(blob, blob_len);
885 fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
888 size_t plen = strlen(alg) + 1;
889 size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
891 if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
893 strlcpy(ret, alg, rlen);
894 strlcat(ret, ":", rlen);
895 if (dgst_raw_len == 0)
897 if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
901 /* Trim padding characters from end */
902 ret[strcspn(ret, "=")] = '\0';
907 fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
909 char *retval, hex[5];
910 size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
912 if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
914 strlcpy(retval, alg, rlen);
915 strlcat(retval, ":", rlen);
916 for (i = 0; i < dgst_raw_len; i++) {
917 snprintf(hex, sizeof(hex), "%s%02x",
918 i > 0 ? ":" : "", dgst_raw[i]);
919 strlcat(retval, hex, rlen);
925 fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
927 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
928 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
929 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
930 u_int i, j = 0, rounds, seed = 1;
933 rounds = (dgst_raw_len / 2) + 1;
934 if ((retval = calloc(rounds, 6)) == NULL)
937 for (i = 0; i < rounds; i++) {
938 u_int idx0, idx1, idx2, idx3, idx4;
939 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
940 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
942 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
943 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
945 retval[j++] = vowels[idx0];
946 retval[j++] = consonants[idx1];
947 retval[j++] = vowels[idx2];
948 if ((i + 1) < rounds) {
949 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
950 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
951 retval[j++] = consonants[idx3];
953 retval[j++] = consonants[idx4];
955 ((((u_int)(dgst_raw[2 * i])) * 7) +
956 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
962 retval[j++] = vowels[idx0];
963 retval[j++] = consonants[idx1];
964 retval[j++] = vowels[idx2];
973 * Draw an ASCII-Art representing the fingerprint so human brain can
974 * profit from its built-in pattern recognition ability.
975 * This technique is called "random art" and can be found in some
976 * scientific publications like this original paper:
978 * "Hash Visualization: a New Technique to improve Real-World Security",
979 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
980 * Techniques and E-Commerce (CrypTEC '99)
981 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
983 * The subject came up in a talk by Dan Kaminsky, too.
985 * If you see the picture is different, the key is different.
986 * If the picture looks the same, you still know nothing.
988 * The algorithm used here is a worm crawling over a discrete plane,
989 * leaving a trace (augmenting the field) everywhere it goes.
990 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
991 * makes the respective movement vector be ignored for this turn.
992 * Graphs are not unambiguous, because circles in graphs can be
993 * walked in either direction.
997 * Field sizes for the random art. Have to be odd, so the starting point
998 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
999 * Else pictures would be too dense, and drawing the frame would
1000 * fail, too, because the key type would not fit in anymore.
1003 #define FLDSIZE_Y (FLDBASE + 1)
1004 #define FLDSIZE_X (FLDBASE * 2 + 1)
1006 fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
1007 const struct sshkey *k)
1010 * Chars to be used after each other every time the worm
1011 * intersects with itself. Matter of taste.
1013 char *augmentation_string = " .o+=*BOX@%&#/^SE";
1014 char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
1015 u_char field[FLDSIZE_X][FLDSIZE_Y];
1016 size_t i, tlen, hlen;
1019 size_t len = strlen(augmentation_string) - 1;
1021 if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1024 /* initialize field */
1025 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1029 /* process raw key */
1030 for (i = 0; i < dgst_raw_len; i++) {
1032 /* each byte conveys four 2-bit move commands */
1033 input = dgst_raw[i];
1034 for (b = 0; b < 4; b++) {
1035 /* evaluate 2 bit, rest is shifted later */
1036 x += (input & 0x1) ? 1 : -1;
1037 y += (input & 0x2) ? 1 : -1;
1039 /* assure we are still in bounds */
1042 x = MINIMUM(x, FLDSIZE_X - 1);
1043 y = MINIMUM(y, FLDSIZE_Y - 1);
1045 /* augment the field */
1046 if (field[x][y] < len - 2)
1052 /* mark starting point and end point*/
1053 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1056 /* assemble title */
1057 r = snprintf(title, sizeof(title), "[%s %u]",
1058 sshkey_type(k), sshkey_size(k));
1059 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1060 if (r < 0 || r > (int)sizeof(title))
1061 r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1062 tlen = (r <= 0) ? 0 : strlen(title);
1064 /* assemble hash ID. */
1065 r = snprintf(hash, sizeof(hash), "[%s]", alg);
1066 hlen = (r <= 0) ? 0 : strlen(hash);
1068 /* output upper border */
1071 for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1073 memcpy(p, title, tlen);
1075 for (i += tlen; i < FLDSIZE_X; i++)
1080 /* output content */
1081 for (y = 0; y < FLDSIZE_Y; y++) {
1083 for (x = 0; x < FLDSIZE_X; x++)
1084 *p++ = augmentation_string[MINIMUM(field[x][y], len)];
1089 /* output lower border */
1091 for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1093 memcpy(p, hash, hlen);
1095 for (i += hlen; i < FLDSIZE_X; i++)
1103 sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
1104 enum sshkey_fp_rep dgst_rep)
1106 char *retval = NULL;
1108 size_t dgst_raw_len;
1110 if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
1113 case SSH_FP_DEFAULT:
1114 if (dgst_alg == SSH_DIGEST_MD5) {
1115 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1116 dgst_raw, dgst_raw_len);
1118 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1119 dgst_raw, dgst_raw_len);
1123 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1124 dgst_raw, dgst_raw_len);
1127 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1128 dgst_raw, dgst_raw_len);
1130 case SSH_FP_BUBBLEBABBLE:
1131 retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1133 case SSH_FP_RANDOMART:
1134 retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1135 dgst_raw, dgst_raw_len, k);
1138 freezero(dgst_raw, dgst_raw_len);
1141 freezero(dgst_raw, dgst_raw_len);
1146 peek_type_nid(const char *s, size_t l, int *nid)
1148 const struct sshkey_impl *impl;
1151 for (i = 0; keyimpls[i] != NULL; i++) {
1153 if (impl->name == NULL || strlen(impl->name) != l)
1155 if (memcmp(s, impl->name, l) == 0) {
1157 if (key_type_is_ecdsa_variant(impl->type))
1165 /* XXX this can now be made const char * */
1167 sshkey_read(struct sshkey *ret, char **cpp)
1170 char *cp, *blobcopy;
1172 int r, type, curve_nid = -1;
1173 struct sshbuf *blob;
1176 return SSH_ERR_INVALID_ARGUMENT;
1177 if (ret->type != KEY_UNSPEC && sshkey_impl_from_type(ret->type) == NULL)
1178 return SSH_ERR_INVALID_ARGUMENT;
1182 space = strcspn(cp, " \t");
1183 if (space == strlen(cp))
1184 return SSH_ERR_INVALID_FORMAT;
1185 if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
1186 return SSH_ERR_INVALID_FORMAT;
1188 /* skip whitespace */
1189 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1192 return SSH_ERR_INVALID_FORMAT;
1193 if (ret->type != KEY_UNSPEC && ret->type != type)
1194 return SSH_ERR_KEY_TYPE_MISMATCH;
1195 if ((blob = sshbuf_new()) == NULL)
1196 return SSH_ERR_ALLOC_FAIL;
1198 /* find end of keyblob and decode */
1199 space = strcspn(cp, " \t");
1200 if ((blobcopy = strndup(cp, space)) == NULL) {
1202 return SSH_ERR_ALLOC_FAIL;
1204 if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
1210 if ((r = sshkey_fromb(blob, &k)) != 0) {
1216 /* skip whitespace and leave cp at start of comment */
1217 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1220 /* ensure type of blob matches type at start of line */
1221 if (k->type != type) {
1223 return SSH_ERR_KEY_TYPE_MISMATCH;
1225 if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
1227 return SSH_ERR_EC_CURVE_MISMATCH;
1230 /* Fill in ret from parsed key */
1231 sshkey_free_contents(ret);
1233 freezero(k, sizeof(*k));
1241 sshkey_to_base64(const struct sshkey *key, char **b64p)
1243 int r = SSH_ERR_INTERNAL_ERROR;
1244 struct sshbuf *b = NULL;
1249 if ((b = sshbuf_new()) == NULL)
1250 return SSH_ERR_ALLOC_FAIL;
1251 if ((r = sshkey_putb(key, b)) != 0)
1253 if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
1254 r = SSH_ERR_ALLOC_FAIL;
1270 sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1272 int r = SSH_ERR_INTERNAL_ERROR;
1275 if ((r = sshkey_to_base64(key, &uu)) != 0)
1277 if ((r = sshbuf_putf(b, "%s %s",
1278 sshkey_ssh_name(key), uu)) != 0)
1287 sshkey_write(const struct sshkey *key, FILE *f)
1289 struct sshbuf *b = NULL;
1290 int r = SSH_ERR_INTERNAL_ERROR;
1292 if ((b = sshbuf_new()) == NULL)
1293 return SSH_ERR_ALLOC_FAIL;
1294 if ((r = sshkey_format_text(key, b)) != 0)
1296 if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1299 r = SSH_ERR_SYSTEM_ERROR;
1310 sshkey_cert_type(const struct sshkey *k)
1312 switch (k->cert->type) {
1313 case SSH2_CERT_TYPE_USER:
1315 case SSH2_CERT_TYPE_HOST:
1323 sshkey_check_rsa_length(const struct sshkey *k, int min_size)
1326 const BIGNUM *rsa_n;
1329 if (k == NULL || k->rsa == NULL ||
1330 (k->type != KEY_RSA && k->type != KEY_RSA_CERT))
1332 RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
1333 nbits = BN_num_bits(rsa_n);
1334 if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1335 (min_size > 0 && nbits < min_size))
1336 return SSH_ERR_KEY_LENGTH;
1337 #endif /* WITH_OPENSSL */
1342 # ifdef OPENSSL_HAS_ECC
1344 sshkey_ecdsa_key_to_nid(EC_KEY *k)
1348 NID_X9_62_prime256v1,
1350 # ifdef OPENSSL_HAS_NISTP521
1352 # endif /* OPENSSL_HAS_NISTP521 */
1357 const EC_GROUP *g = EC_KEY_get0_group(k);
1360 * The group may be stored in a ASN.1 encoded private key in one of two
1361 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1362 * or explicit group parameters encoded into the key blob. Only the
1363 * "named group" case sets the group NID for us, but we can figure
1364 * it out for the other case by comparing against all the groups that
1367 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1369 for (i = 0; nids[i] != -1; i++) {
1370 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1372 if (EC_GROUP_cmp(g, eg, NULL) == 0)
1376 if (nids[i] != -1) {
1377 /* Use the group with the NID attached */
1378 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1379 if (EC_KEY_set_group(k, eg) != 1) {
1386 # endif /* OPENSSL_HAS_ECC */
1387 #endif /* WITH_OPENSSL */
1390 sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1393 int ret = SSH_ERR_INTERNAL_ERROR;
1394 const struct sshkey_impl *impl;
1396 if (keyp == NULL || sshkey_type_is_cert(type))
1397 return SSH_ERR_INVALID_ARGUMENT;
1399 if ((impl = sshkey_impl_from_type(type)) == NULL)
1400 return SSH_ERR_KEY_TYPE_UNKNOWN;
1401 if (impl->funcs->generate == NULL)
1402 return SSH_ERR_FEATURE_UNSUPPORTED;
1403 if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1404 return SSH_ERR_ALLOC_FAIL;
1406 if ((ret = impl->funcs->generate(k, bits)) != 0) {
1416 sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1419 const struct sshkey_cert *from;
1420 struct sshkey_cert *to;
1421 int r = SSH_ERR_INTERNAL_ERROR;
1423 if (to_key == NULL || (from = from_key->cert) == NULL)
1424 return SSH_ERR_INVALID_ARGUMENT;
1426 if ((to = cert_new()) == NULL)
1427 return SSH_ERR_ALLOC_FAIL;
1429 if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
1430 (r = sshbuf_putb(to->critical, from->critical)) != 0 ||
1431 (r = sshbuf_putb(to->extensions, from->extensions)) != 0)
1434 to->serial = from->serial;
1435 to->type = from->type;
1436 if (from->key_id == NULL)
1438 else if ((to->key_id = strdup(from->key_id)) == NULL) {
1439 r = SSH_ERR_ALLOC_FAIL;
1442 to->valid_after = from->valid_after;
1443 to->valid_before = from->valid_before;
1444 if (from->signature_key == NULL)
1445 to->signature_key = NULL;
1446 else if ((r = sshkey_from_private(from->signature_key,
1447 &to->signature_key)) != 0)
1449 if (from->signature_type != NULL &&
1450 (to->signature_type = strdup(from->signature_type)) == NULL) {
1451 r = SSH_ERR_ALLOC_FAIL;
1454 if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
1455 r = SSH_ERR_INVALID_ARGUMENT;
1458 if (from->nprincipals > 0) {
1459 if ((to->principals = calloc(from->nprincipals,
1460 sizeof(*to->principals))) == NULL) {
1461 r = SSH_ERR_ALLOC_FAIL;
1464 for (i = 0; i < from->nprincipals; i++) {
1465 to->principals[i] = strdup(from->principals[i]);
1466 if (to->principals[i] == NULL) {
1467 to->nprincipals = i;
1468 r = SSH_ERR_ALLOC_FAIL;
1473 to->nprincipals = from->nprincipals;
1476 cert_free(to_key->cert);
1486 sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to)
1488 /* Append security-key application string */
1489 if ((to->sk_application = strdup(from->sk_application)) == NULL)
1490 return SSH_ERR_ALLOC_FAIL;
1495 sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1497 struct sshkey *n = NULL;
1498 int r = SSH_ERR_INTERNAL_ERROR;
1499 const struct sshkey_impl *impl;
1502 if ((impl = sshkey_impl_from_key(k)) == NULL)
1503 return SSH_ERR_KEY_TYPE_UNKNOWN;
1504 if ((n = sshkey_new(k->type)) == NULL) {
1505 r = SSH_ERR_ALLOC_FAIL;
1508 if ((r = impl->funcs->copy_public(k, n)) != 0)
1510 if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
1522 sshkey_is_shielded(struct sshkey *k)
1524 return k != NULL && k->shielded_private != NULL;
1528 sshkey_shield_private(struct sshkey *k)
1530 struct sshbuf *prvbuf = NULL;
1531 u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
1532 struct sshcipher_ctx *cctx = NULL;
1533 const struct sshcipher *cipher;
1534 size_t i, enclen = 0;
1535 struct sshkey *kswap = NULL, tmp;
1536 int r = SSH_ERR_INTERNAL_ERROR;
1539 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
1541 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
1542 r = SSH_ERR_INVALID_ARGUMENT;
1545 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
1546 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
1547 r = SSH_ERR_INTERNAL_ERROR;
1551 /* Prepare a random pre-key, and from it an ephemeral key */
1552 if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) {
1553 r = SSH_ERR_ALLOC_FAIL;
1556 arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
1557 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
1558 prekey, SSHKEY_SHIELD_PREKEY_LEN,
1559 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
1562 fprintf(stderr, "%s: key+iv\n", __func__);
1563 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
1566 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
1567 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
1570 /* Serialise and encrypt the private key using the ephemeral key */
1571 if ((prvbuf = sshbuf_new()) == NULL) {
1572 r = SSH_ERR_ALLOC_FAIL;
1575 if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
1577 if ((r = sshkey_private_serialize_opt(k, prvbuf,
1578 SSHKEY_SERIALIZE_SHIELD)) != 0)
1580 /* pad to cipher blocksize */
1582 while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
1583 if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
1587 fprintf(stderr, "%s: serialised\n", __func__);
1588 sshbuf_dump(prvbuf, stderr);
1591 enclen = sshbuf_len(prvbuf);
1592 if ((enc = malloc(enclen)) == NULL) {
1593 r = SSH_ERR_ALLOC_FAIL;
1596 if ((r = cipher_crypt(cctx, 0, enc,
1597 sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
1600 fprintf(stderr, "%s: encrypted\n", __func__);
1601 sshbuf_dump_data(enc, enclen, stderr);
1604 /* Make a scrubbed, public-only copy of our private key argument */
1605 if ((r = sshkey_from_private(k, &kswap)) != 0)
1608 /* Swap the private key out (it will be destroyed below) */
1613 /* Insert the shielded key into our argument */
1614 k->shielded_private = enc;
1615 k->shielded_len = enclen;
1616 k->shield_prekey = prekey;
1617 k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
1618 enc = prekey = NULL; /* transferred */
1621 /* preserve key fields that are required for correct operation */
1622 k->sk_flags = kswap->sk_flags;
1628 /* XXX behaviour on error - invalidate original private key? */
1630 explicit_bzero(keyiv, sizeof(keyiv));
1631 explicit_bzero(&tmp, sizeof(tmp));
1632 freezero(enc, enclen);
1633 freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN);
1635 sshbuf_free(prvbuf);
1639 /* Check deterministic padding after private key */
1641 private2_check_padding(struct sshbuf *decrypted)
1648 while (sshbuf_len(decrypted)) {
1649 if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
1651 if (pad != (++i & 0xff)) {
1652 r = SSH_ERR_INVALID_FORMAT;
1659 explicit_bzero(&pad, sizeof(pad));
1660 explicit_bzero(&i, sizeof(i));
1665 sshkey_unshield_private(struct sshkey *k)
1667 struct sshbuf *prvbuf = NULL;
1668 u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
1669 struct sshcipher_ctx *cctx = NULL;
1670 const struct sshcipher *cipher;
1671 struct sshkey *kswap = NULL, tmp;
1672 int r = SSH_ERR_INTERNAL_ERROR;
1675 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
1677 if (!sshkey_is_shielded(k))
1678 return 0; /* nothing to do */
1680 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
1681 r = SSH_ERR_INVALID_ARGUMENT;
1684 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
1685 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
1686 r = SSH_ERR_INTERNAL_ERROR;
1689 /* check size of shielded key blob */
1690 if (k->shielded_len < cipher_blocksize(cipher) ||
1691 (k->shielded_len % cipher_blocksize(cipher)) != 0) {
1692 r = SSH_ERR_INVALID_FORMAT;
1696 /* Calculate the ephemeral key from the prekey */
1697 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
1698 k->shield_prekey, k->shield_prekey_len,
1699 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
1701 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
1702 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
1705 fprintf(stderr, "%s: key+iv\n", __func__);
1706 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
1710 /* Decrypt and parse the shielded private key using the ephemeral key */
1711 if ((prvbuf = sshbuf_new()) == NULL) {
1712 r = SSH_ERR_ALLOC_FAIL;
1715 if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
1719 fprintf(stderr, "%s: encrypted\n", __func__);
1720 sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
1722 if ((r = cipher_crypt(cctx, 0, cp,
1723 k->shielded_private, k->shielded_len, 0, 0)) != 0)
1726 fprintf(stderr, "%s: serialised\n", __func__);
1727 sshbuf_dump(prvbuf, stderr);
1729 /* Parse private key */
1730 if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
1733 if ((r = private2_check_padding(prvbuf)) != 0)
1736 /* Swap the parsed key back into place */
1746 explicit_bzero(keyiv, sizeof(keyiv));
1747 explicit_bzero(&tmp, sizeof(tmp));
1749 sshbuf_free(prvbuf);
1754 cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
1756 struct sshbuf *principals = NULL, *crit = NULL;
1757 struct sshbuf *exts = NULL, *ca = NULL;
1759 size_t signed_len = 0, slen = 0, kidlen = 0;
1760 int ret = SSH_ERR_INTERNAL_ERROR;
1762 /* Copy the entire key blob for verification and later serialisation */
1763 if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
1766 /* Parse body of certificate up to signature */
1767 if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
1768 (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
1769 (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
1770 (ret = sshbuf_froms(b, &principals)) != 0 ||
1771 (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
1772 (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
1773 (ret = sshbuf_froms(b, &crit)) != 0 ||
1774 (ret = sshbuf_froms(b, &exts)) != 0 ||
1775 (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
1776 (ret = sshbuf_froms(b, &ca)) != 0) {
1777 /* XXX debug print error for ret */
1778 ret = SSH_ERR_INVALID_FORMAT;
1782 /* Signature is left in the buffer so we can calculate this length */
1783 signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
1785 if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
1786 ret = SSH_ERR_INVALID_FORMAT;
1790 if (key->cert->type != SSH2_CERT_TYPE_USER &&
1791 key->cert->type != SSH2_CERT_TYPE_HOST) {
1792 ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
1796 /* Parse principals section */
1797 while (sshbuf_len(principals) > 0) {
1798 char *principal = NULL;
1799 char **oprincipals = NULL;
1801 if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
1802 ret = SSH_ERR_INVALID_FORMAT;
1805 if ((ret = sshbuf_get_cstring(principals, &principal,
1807 ret = SSH_ERR_INVALID_FORMAT;
1810 oprincipals = key->cert->principals;
1811 key->cert->principals = recallocarray(key->cert->principals,
1812 key->cert->nprincipals, key->cert->nprincipals + 1,
1813 sizeof(*key->cert->principals));
1814 if (key->cert->principals == NULL) {
1816 key->cert->principals = oprincipals;
1817 ret = SSH_ERR_ALLOC_FAIL;
1820 key->cert->principals[key->cert->nprincipals++] = principal;
1824 * Stash a copies of the critical options and extensions sections
1827 if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
1829 (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
1833 * Validate critical options and extensions sections format.
1835 while (sshbuf_len(crit) != 0) {
1836 if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
1837 (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
1838 sshbuf_reset(key->cert->critical);
1839 ret = SSH_ERR_INVALID_FORMAT;
1843 while (exts != NULL && sshbuf_len(exts) != 0) {
1844 if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
1845 (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
1846 sshbuf_reset(key->cert->extensions);
1847 ret = SSH_ERR_INVALID_FORMAT;
1852 /* Parse CA key and check signature */
1853 if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
1854 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1857 if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
1858 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1861 if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
1862 sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
1864 if ((ret = sshkey_get_sigtype(sig, slen,
1865 &key->cert->signature_type)) != 0)
1874 sshbuf_free(principals);
1880 sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key)
1882 /* Parse additional security-key application string */
1883 if (sshbuf_get_cstring(b, &key->sk_application, NULL) != 0)
1884 return SSH_ERR_INVALID_FORMAT;
1889 sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
1892 int type, ret = SSH_ERR_INTERNAL_ERROR;
1894 struct sshkey *key = NULL;
1895 struct sshbuf *copy;
1896 const struct sshkey_impl *impl;
1898 #ifdef DEBUG_PK /* XXX */
1899 sshbuf_dump(b, stderr);
1903 if ((copy = sshbuf_fromb(b)) == NULL) {
1904 ret = SSH_ERR_ALLOC_FAIL;
1907 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
1908 ret = SSH_ERR_INVALID_FORMAT;
1912 type = sshkey_type_from_name(ktype);
1913 if (!allow_cert && sshkey_type_is_cert(type)) {
1914 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1917 if ((impl = sshkey_impl_from_type(type)) == NULL) {
1918 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
1921 if ((key = sshkey_new(type)) == NULL) {
1922 ret = SSH_ERR_ALLOC_FAIL;
1925 if (sshkey_type_is_cert(type)) {
1926 /* Skip nonce that preceeds all certificates */
1927 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
1928 ret = SSH_ERR_INVALID_FORMAT;
1932 if ((ret = impl->funcs->deserialize_public(ktype, b, key)) != 0)
1935 /* Parse certificate potion */
1936 if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
1939 if (key != NULL && sshbuf_len(b) != 0) {
1940 ret = SSH_ERR_INVALID_FORMAT;
1956 sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
1961 if ((b = sshbuf_from(blob, blen)) == NULL)
1962 return SSH_ERR_ALLOC_FAIL;
1963 r = sshkey_from_blob_internal(b, keyp, 1);
1969 sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
1971 return sshkey_from_blob_internal(b, keyp, 1);
1975 sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
1980 if ((r = sshbuf_froms(buf, &b)) != 0)
1982 r = sshkey_from_blob_internal(b, keyp, 1);
1988 sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
1991 struct sshbuf *b = NULL;
1992 char *sigtype = NULL;
1994 if (sigtypep != NULL)
1996 if ((b = sshbuf_from(sig, siglen)) == NULL)
1997 return SSH_ERR_ALLOC_FAIL;
1998 if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
2001 if (sigtypep != NULL) {
2002 *sigtypep = sigtype;
2014 * Checks whether a certificate's signature type is allowed.
2015 * Returns 0 (success) if the certificate signature type appears in the
2016 * "allowed" pattern-list, or the key is not a certificate to begin with.
2017 * Otherwise returns a ssherr.h code.
2020 sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
2022 if (key == NULL || allowed == NULL)
2023 return SSH_ERR_INVALID_ARGUMENT;
2024 if (!sshkey_type_is_cert(key->type))
2026 if (key->cert == NULL || key->cert->signature_type == NULL)
2027 return SSH_ERR_INVALID_ARGUMENT;
2028 if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
2029 return SSH_ERR_SIGN_ALG_UNSUPPORTED;
2034 * Returns the expected signature algorithm for a given public key algorithm.
2037 sshkey_sigalg_by_name(const char *name)
2039 const struct sshkey_impl *impl;
2042 for (i = 0; keyimpls[i] != NULL; i++) {
2044 if (strcmp(impl->name, name) != 0)
2046 if (impl->sigalg != NULL)
2047 return impl->sigalg;
2050 return sshkey_ssh_name_from_type_nid(
2051 sshkey_type_plain(impl->type), impl->nid);
2057 * Verifies that the signature algorithm appearing inside the signature blob
2058 * matches that which was requested.
2061 sshkey_check_sigtype(const u_char *sig, size_t siglen,
2062 const char *requested_alg)
2064 const char *expected_alg;
2065 char *sigtype = NULL;
2068 if (requested_alg == NULL)
2070 if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
2071 return SSH_ERR_INVALID_ARGUMENT;
2072 if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
2074 r = strcmp(expected_alg, sigtype) == 0;
2076 return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
2080 sshkey_sign(struct sshkey *key,
2081 u_char **sigp, size_t *lenp,
2082 const u_char *data, size_t datalen,
2083 const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
2085 int was_shielded = sshkey_is_shielded(key);
2086 int r2, r = SSH_ERR_INTERNAL_ERROR;
2087 const struct sshkey_impl *impl;
2093 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2094 return SSH_ERR_INVALID_ARGUMENT;
2095 if ((impl = sshkey_impl_from_key(key)) == NULL)
2096 return SSH_ERR_KEY_TYPE_UNKNOWN;
2097 if ((r = sshkey_unshield_private(key)) != 0)
2099 if (sshkey_is_sk(key)) {
2100 r = sshsk_sign(sk_provider, key, sigp, lenp, data,
2101 datalen, compat, sk_pin);
2103 if (impl->funcs->sign == NULL)
2104 r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
2106 r = impl->funcs->sign(key, sigp, lenp, data, datalen,
2107 alg, sk_provider, sk_pin, compat);
2110 if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
2116 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2117 * If "alg" specified, then the signature must use that algorithm.
2120 sshkey_verify(const struct sshkey *key,
2121 const u_char *sig, size_t siglen,
2122 const u_char *data, size_t dlen, const char *alg, u_int compat,
2123 struct sshkey_sig_details **detailsp)
2125 const struct sshkey_impl *impl;
2127 if (detailsp != NULL)
2129 if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2130 return SSH_ERR_INVALID_ARGUMENT;
2131 if ((impl = sshkey_impl_from_key(key)) == NULL)
2132 return SSH_ERR_KEY_TYPE_UNKNOWN;
2133 return impl->funcs->verify(key, sig, siglen, data, dlen,
2134 alg, compat, detailsp);
2137 /* Convert a plain key to their _CERT equivalent */
2139 sshkey_to_certified(struct sshkey *k)
2143 if ((newtype = sshkey_type_certified(k->type)) == -1)
2144 return SSH_ERR_INVALID_ARGUMENT;
2145 if ((k->cert = cert_new()) == NULL)
2146 return SSH_ERR_ALLOC_FAIL;
2151 /* Convert a certificate to its raw key equivalent */
2153 sshkey_drop_cert(struct sshkey *k)
2155 if (!sshkey_type_is_cert(k->type))
2156 return SSH_ERR_KEY_TYPE_UNKNOWN;
2159 k->type = sshkey_type_plain(k->type);
2163 /* Sign a certified key, (re-)generating the signed certblob. */
2165 sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
2166 const char *sk_provider, const char *sk_pin,
2167 sshkey_certify_signer *signer, void *signer_ctx)
2169 const struct sshkey_impl *impl;
2170 struct sshbuf *principals = NULL;
2171 u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2172 size_t i, ca_len, sig_len;
2173 int ret = SSH_ERR_INTERNAL_ERROR;
2174 struct sshbuf *cert = NULL;
2175 char *sigtype = NULL;
2177 if (k == NULL || k->cert == NULL ||
2178 k->cert->certblob == NULL || ca == NULL)
2179 return SSH_ERR_INVALID_ARGUMENT;
2180 if (!sshkey_is_cert(k))
2181 return SSH_ERR_KEY_TYPE_UNKNOWN;
2182 if (!sshkey_type_is_valid_ca(ca->type))
2183 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2184 if ((impl = sshkey_impl_from_key(k)) == NULL)
2185 return SSH_ERR_INTERNAL_ERROR;
2188 * If no alg specified as argument but a signature_type was set,
2189 * then prefer that. If both were specified, then they must match.
2192 alg = k->cert->signature_type;
2193 else if (k->cert->signature_type != NULL &&
2194 strcmp(alg, k->cert->signature_type) != 0)
2195 return SSH_ERR_INVALID_ARGUMENT;
2198 * If no signing algorithm or signature_type was specified and we're
2199 * using a RSA key, then default to a good signature algorithm.
2201 if (alg == NULL && ca->type == KEY_RSA)
2202 alg = "rsa-sha2-512";
2204 if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2205 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2207 cert = k->cert->certblob; /* for readability */
2209 if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2212 /* -v01 certs put nonce first */
2213 arc4random_buf(&nonce, sizeof(nonce));
2214 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2217 /* Public key next */
2218 if ((ret = impl->funcs->serialize_public(k, cert,
2219 SSHKEY_SERIALIZE_DEFAULT)) != 0)
2222 /* Then remaining cert fields */
2223 if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
2224 (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
2225 (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
2228 if ((principals = sshbuf_new()) == NULL) {
2229 ret = SSH_ERR_ALLOC_FAIL;
2232 for (i = 0; i < k->cert->nprincipals; i++) {
2233 if ((ret = sshbuf_put_cstring(principals,
2234 k->cert->principals[i])) != 0)
2237 if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
2238 (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
2239 (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
2240 (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
2241 (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
2242 (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
2243 (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
2246 /* Sign the whole mess */
2247 if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
2248 sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
2250 /* Check and update signature_type against what was actually used */
2251 if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
2253 if (alg != NULL && strcmp(alg, sigtype) != 0) {
2254 ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
2257 if (k->cert->signature_type == NULL) {
2258 k->cert->signature_type = sigtype;
2261 /* Append signature and we are done */
2262 if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
2271 sshbuf_free(principals);
2276 default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
2277 const u_char *data, size_t datalen,
2278 const char *alg, const char *sk_provider, const char *sk_pin,
2279 u_int compat, void *ctx)
2282 return SSH_ERR_INVALID_ARGUMENT;
2283 return sshkey_sign(key, sigp, lenp, data, datalen, alg,
2284 sk_provider, sk_pin, compat);
2288 sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
2289 const char *sk_provider, const char *sk_pin)
2291 return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
2292 default_key_sign, NULL);
2296 sshkey_cert_check_authority(const struct sshkey *k,
2297 int want_host, int require_principal, int wildcard_pattern,
2298 uint64_t verify_time, const char *name, const char **reason)
2300 u_int i, principal_matches;
2303 return SSH_ERR_INVALID_ARGUMENT;
2304 if (!sshkey_is_cert(k)) {
2305 *reason = "Key is not a certificate";
2306 return SSH_ERR_KEY_CERT_INVALID;
2309 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
2310 *reason = "Certificate invalid: not a host certificate";
2311 return SSH_ERR_KEY_CERT_INVALID;
2314 if (k->cert->type != SSH2_CERT_TYPE_USER) {
2315 *reason = "Certificate invalid: not a user certificate";
2316 return SSH_ERR_KEY_CERT_INVALID;
2319 if (verify_time < k->cert->valid_after) {
2320 *reason = "Certificate invalid: not yet valid";
2321 return SSH_ERR_KEY_CERT_INVALID;
2323 if (verify_time >= k->cert->valid_before) {
2324 *reason = "Certificate invalid: expired";
2325 return SSH_ERR_KEY_CERT_INVALID;
2327 if (k->cert->nprincipals == 0) {
2328 if (require_principal) {
2329 *reason = "Certificate lacks principal list";
2330 return SSH_ERR_KEY_CERT_INVALID;
2332 } else if (name != NULL) {
2333 principal_matches = 0;
2334 for (i = 0; i < k->cert->nprincipals; i++) {
2335 if (wildcard_pattern) {
2336 if (match_pattern(k->cert->principals[i],
2338 principal_matches = 1;
2341 } else if (strcmp(name, k->cert->principals[i]) == 0) {
2342 principal_matches = 1;
2346 if (!principal_matches) {
2347 *reason = "Certificate invalid: name is not a listed "
2349 return SSH_ERR_KEY_CERT_INVALID;
2356 sshkey_cert_check_authority_now(const struct sshkey *k,
2357 int want_host, int require_principal, int wildcard_pattern,
2358 const char *name, const char **reason)
2362 if ((now = time(NULL)) < 0) {
2363 /* yikes - system clock before epoch! */
2364 *reason = "Certificate invalid: not yet valid";
2365 return SSH_ERR_KEY_CERT_INVALID;
2367 return sshkey_cert_check_authority(k, want_host, require_principal,
2368 wildcard_pattern, (uint64_t)now, name, reason);
2372 sshkey_cert_check_host(const struct sshkey *key, const char *host,
2373 int wildcard_principals, const char *ca_sign_algorithms,
2374 const char **reason)
2378 if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals,
2379 host, reason)) != 0)
2381 if (sshbuf_len(key->cert->critical) != 0) {
2382 *reason = "Certificate contains unsupported critical options";
2383 return SSH_ERR_KEY_CERT_INVALID;
2385 if (ca_sign_algorithms != NULL &&
2386 (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
2387 *reason = "Certificate signed with disallowed algorithm";
2388 return SSH_ERR_KEY_CERT_INVALID;
2394 sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
2396 char from[32], to[32], ret[128];
2399 if (cert->valid_after == 0 &&
2400 cert->valid_before == 0xffffffffffffffffULL)
2401 return strlcpy(s, "forever", l);
2403 if (cert->valid_after != 0)
2404 format_absolute_time(cert->valid_after, from, sizeof(from));
2405 if (cert->valid_before != 0xffffffffffffffffULL)
2406 format_absolute_time(cert->valid_before, to, sizeof(to));
2408 if (cert->valid_after == 0)
2409 snprintf(ret, sizeof(ret), "before %s", to);
2410 else if (cert->valid_before == 0xffffffffffffffffULL)
2411 snprintf(ret, sizeof(ret), "after %s", from);
2413 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
2415 return strlcpy(s, ret, l);
2418 /* Common serialization for FIDO private keys */
2420 sshkey_serialize_private_sk(const struct sshkey *key, struct sshbuf *b)
2424 if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
2425 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
2426 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
2427 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
2434 sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
2435 enum sshkey_serialize_rep opts)
2437 int r = SSH_ERR_INTERNAL_ERROR;
2438 int was_shielded = sshkey_is_shielded(key);
2439 struct sshbuf *b = NULL;
2440 const struct sshkey_impl *impl;
2442 if ((impl = sshkey_impl_from_key(key)) == NULL)
2443 return SSH_ERR_INTERNAL_ERROR;
2444 if ((r = sshkey_unshield_private(key)) != 0)
2446 if ((b = sshbuf_new()) == NULL)
2447 return SSH_ERR_ALLOC_FAIL;
2448 if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
2450 if (sshkey_is_cert(key)) {
2451 if (key->cert == NULL ||
2452 sshbuf_len(key->cert->certblob) == 0) {
2453 r = SSH_ERR_INVALID_ARGUMENT;
2456 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0)
2459 if ((r = impl->funcs->serialize_private(key, b, opts)) != 0)
2463 * success (but we still need to append the output to buf after
2464 * possibly re-shielding the private key)
2469 r = sshkey_shield_private(key);
2471 r = sshbuf_putb(buf, b);
2478 sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
2480 return sshkey_private_serialize_opt(key, b,
2481 SSHKEY_SERIALIZE_DEFAULT);
2484 /* Shared deserialization of FIDO private key components */
2486 sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k)
2490 if ((k->sk_key_handle = sshbuf_new()) == NULL ||
2491 (k->sk_reserved = sshbuf_new()) == NULL)
2492 return SSH_ERR_ALLOC_FAIL;
2493 if ((r = sshbuf_get_cstring(buf, &k->sk_application, NULL)) != 0 ||
2494 (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
2495 (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
2496 (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
2503 sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
2505 const struct sshkey_impl *impl;
2507 char *expect_sk_application = NULL;
2508 u_char *expect_ed25519_pk = NULL;
2509 struct sshkey *k = NULL;
2510 int type, r = SSH_ERR_INTERNAL_ERROR;
2514 if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
2516 type = sshkey_type_from_name(tname);
2517 if (sshkey_type_is_cert(type)) {
2519 * Certificate key private keys begin with the certificate
2520 * itself. Make sure this matches the type of the enclosing
2523 if ((r = sshkey_froms(buf, &k)) != 0)
2525 if (k->type != type) {
2526 r = SSH_ERR_KEY_CERT_MISMATCH;
2529 /* For ECDSA keys, the group must match too */
2530 if (k->type == KEY_ECDSA &&
2531 k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
2532 r = SSH_ERR_KEY_CERT_MISMATCH;
2536 * Several fields are redundant between certificate and
2537 * private key body, we require these to match.
2539 expect_sk_application = k->sk_application;
2540 expect_ed25519_pk = k->ed25519_pk;
2541 k->sk_application = NULL;
2542 k->ed25519_pk = NULL;
2543 /* XXX xmss too or refactor */
2545 if ((k = sshkey_new(type)) == NULL) {
2546 r = SSH_ERR_ALLOC_FAIL;
2550 if ((impl = sshkey_impl_from_type(type)) == NULL) {
2551 r = SSH_ERR_INTERNAL_ERROR;
2554 if ((r = impl->funcs->deserialize_private(tname, buf, k)) != 0)
2557 /* XXX xmss too or refactor */
2558 if ((expect_sk_application != NULL && (k->sk_application == NULL ||
2559 strcmp(expect_sk_application, k->sk_application) != 0)) ||
2560 (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
2561 memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
2562 r = SSH_ERR_KEY_CERT_MISMATCH;
2574 free(expect_sk_application);
2575 free(expect_ed25519_pk);
2579 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2581 sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2583 EC_POINT *nq = NULL;
2584 BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
2585 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2588 * NB. This assumes OpenSSL has already verified that the public
2589 * point lies on the curve. This is done by EC_POINT_oct2point()
2590 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
2591 * reachable with public points not unmarshalled using
2592 * EC_POINT_oct2point then the caller will need to explicitly check.
2596 * We shouldn't ever hit this case because bignum_get_ecpoint()
2597 * refuses to load GF2m points.
2599 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2600 NID_X9_62_prime_field)
2604 if (EC_POINT_is_at_infinity(group, public))
2607 if ((x = BN_new()) == NULL ||
2608 (y = BN_new()) == NULL ||
2609 (order = BN_new()) == NULL ||
2610 (tmp = BN_new()) == NULL) {
2611 ret = SSH_ERR_ALLOC_FAIL;
2615 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2616 if (EC_GROUP_get_order(group, order, NULL) != 1 ||
2617 EC_POINT_get_affine_coordinates_GFp(group, public,
2619 ret = SSH_ERR_LIBCRYPTO_ERROR;
2622 if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
2623 BN_num_bits(y) <= BN_num_bits(order) / 2)
2626 /* nQ == infinity (n == order of subgroup) */
2627 if ((nq = EC_POINT_new(group)) == NULL) {
2628 ret = SSH_ERR_ALLOC_FAIL;
2631 if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
2632 ret = SSH_ERR_LIBCRYPTO_ERROR;
2635 if (EC_POINT_is_at_infinity(group, nq) != 1)
2638 /* x < order - 1, y < order - 1 */
2639 if (!BN_sub(tmp, order, BN_value_one())) {
2640 ret = SSH_ERR_LIBCRYPTO_ERROR;
2643 if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
2649 BN_clear_free(order);
2656 sshkey_ec_validate_private(const EC_KEY *key)
2658 BIGNUM *order = NULL, *tmp = NULL;
2659 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2661 if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
2662 ret = SSH_ERR_ALLOC_FAIL;
2666 /* log2(private) > log2(order)/2 */
2667 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
2668 ret = SSH_ERR_LIBCRYPTO_ERROR;
2671 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2672 BN_num_bits(order) / 2)
2675 /* private < order - 1 */
2676 if (!BN_sub(tmp, order, BN_value_one())) {
2677 ret = SSH_ERR_LIBCRYPTO_ERROR;
2680 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
2684 BN_clear_free(order);
2690 sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2692 BIGNUM *x = NULL, *y = NULL;
2694 if (point == NULL) {
2695 fputs("point=(NULL)\n", stderr);
2698 if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
2699 fprintf(stderr, "%s: BN_new failed\n", __func__);
2702 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2703 NID_X9_62_prime_field) {
2704 fprintf(stderr, "%s: group is not a prime field\n", __func__);
2707 if (EC_POINT_get_affine_coordinates_GFp(group, point,
2709 fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
2713 fputs("x=", stderr);
2714 BN_print_fp(stderr, x);
2715 fputs("\ny=", stderr);
2716 BN_print_fp(stderr, y);
2717 fputs("\n", stderr);
2724 sshkey_dump_ec_key(const EC_KEY *key)
2726 const BIGNUM *exponent;
2728 sshkey_dump_ec_point(EC_KEY_get0_group(key),
2729 EC_KEY_get0_public_key(key));
2730 fputs("exponent=", stderr);
2731 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
2732 fputs("(NULL)", stderr);
2734 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
2735 fputs("\n", stderr);
2737 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2740 sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
2741 const char *passphrase, const char *comment, const char *ciphername,
2744 u_char *cp, *key = NULL, *pubkeyblob = NULL;
2745 u_char salt[SALT_LEN];
2747 size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
2749 int r = SSH_ERR_INTERNAL_ERROR;
2750 struct sshcipher_ctx *ciphercontext = NULL;
2751 const struct sshcipher *cipher;
2752 const char *kdfname = KDFNAME;
2753 struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
2756 rounds = DEFAULT_ROUNDS;
2757 if (passphrase == NULL || !strlen(passphrase)) {
2758 ciphername = "none";
2760 } else if (ciphername == NULL)
2761 ciphername = DEFAULT_CIPHERNAME;
2762 if ((cipher = cipher_by_name(ciphername)) == NULL) {
2763 r = SSH_ERR_INVALID_ARGUMENT;
2767 if ((kdf = sshbuf_new()) == NULL ||
2768 (encoded = sshbuf_new()) == NULL ||
2769 (encrypted = sshbuf_new()) == NULL) {
2770 r = SSH_ERR_ALLOC_FAIL;
2773 blocksize = cipher_blocksize(cipher);
2774 keylen = cipher_keylen(cipher);
2775 ivlen = cipher_ivlen(cipher);
2776 authlen = cipher_authlen(cipher);
2777 if ((key = calloc(1, keylen + ivlen)) == NULL) {
2778 r = SSH_ERR_ALLOC_FAIL;
2781 if (strcmp(kdfname, "bcrypt") == 0) {
2782 arc4random_buf(salt, SALT_LEN);
2783 if (bcrypt_pbkdf(passphrase, strlen(passphrase),
2784 salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
2785 r = SSH_ERR_INVALID_ARGUMENT;
2788 if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
2789 (r = sshbuf_put_u32(kdf, rounds)) != 0)
2791 } else if (strcmp(kdfname, "none") != 0) {
2792 /* Unsupported KDF type */
2793 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2796 if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
2797 key + keylen, ivlen, 1)) != 0)
2800 if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
2801 (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
2802 (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
2803 (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
2804 (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
2805 (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
2806 (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
2809 /* set up the buffer that will be encrypted */
2811 /* Random check bytes */
2812 check = arc4random();
2813 if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
2814 (r = sshbuf_put_u32(encrypted, check)) != 0)
2817 /* append private key and comment*/
2818 if ((r = sshkey_private_serialize_opt(prv, encrypted,
2819 SSHKEY_SERIALIZE_FULL)) != 0 ||
2820 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
2825 while (sshbuf_len(encrypted) % blocksize) {
2826 if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
2830 /* length in destination buffer */
2831 if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
2835 if ((r = sshbuf_reserve(encoded,
2836 sshbuf_len(encrypted) + authlen, &cp)) != 0)
2838 if ((r = cipher_crypt(ciphercontext, 0, cp,
2839 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
2844 /* assemble uuencoded key */
2845 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
2846 (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
2847 (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
2855 sshbuf_free(encoded);
2856 sshbuf_free(encrypted);
2857 cipher_free(ciphercontext);
2858 explicit_bzero(salt, sizeof(salt));
2860 freezero(key, keylen + ivlen);
2861 if (pubkeyblob != NULL)
2862 freezero(pubkeyblob, pubkeylen);
2864 freezero(b64, strlen(b64));
2869 private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
2875 struct sshbuf *encoded = NULL, *decoded = NULL;
2877 if (blob == NULL || decodedp == NULL)
2878 return SSH_ERR_INVALID_ARGUMENT;
2882 if ((encoded = sshbuf_new()) == NULL ||
2883 (decoded = sshbuf_new()) == NULL) {
2884 r = SSH_ERR_ALLOC_FAIL;
2888 /* check preamble */
2889 cp = sshbuf_ptr(blob);
2890 encoded_len = sshbuf_len(blob);
2891 if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
2892 memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
2893 r = SSH_ERR_INVALID_FORMAT;
2896 cp += MARK_BEGIN_LEN;
2897 encoded_len -= MARK_BEGIN_LEN;
2899 /* Look for end marker, removing whitespace as we go */
2900 while (encoded_len > 0) {
2901 if (*cp != '\n' && *cp != '\r') {
2902 if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
2909 if (encoded_len >= MARK_END_LEN &&
2910 memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
2912 if ((r = sshbuf_put_u8(encoded, 0)) != 0)
2918 if (encoded_len == 0) {
2919 r = SSH_ERR_INVALID_FORMAT;
2924 if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
2928 if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
2929 memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
2930 r = SSH_ERR_INVALID_FORMAT;
2934 *decodedp = decoded;
2938 sshbuf_free(encoded);
2939 sshbuf_free(decoded);
2944 private2_decrypt(struct sshbuf *decoded, const char *passphrase,
2945 struct sshbuf **decryptedp, struct sshkey **pubkeyp)
2947 char *ciphername = NULL, *kdfname = NULL;
2948 const struct sshcipher *cipher = NULL;
2949 int r = SSH_ERR_INTERNAL_ERROR;
2950 size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
2951 struct sshbuf *kdf = NULL, *decrypted = NULL;
2952 struct sshcipher_ctx *ciphercontext = NULL;
2953 struct sshkey *pubkey = NULL;
2954 u_char *key = NULL, *salt = NULL, *dp;
2955 u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
2957 if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
2958 return SSH_ERR_INVALID_ARGUMENT;
2963 if ((decrypted = sshbuf_new()) == NULL) {
2964 r = SSH_ERR_ALLOC_FAIL;
2968 /* parse public portion of key */
2969 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
2970 (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
2971 (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
2972 (r = sshbuf_froms(decoded, &kdf)) != 0 ||
2973 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
2977 /* XXX only one key supported at present */
2978 r = SSH_ERR_INVALID_FORMAT;
2982 if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
2983 (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
2986 if ((cipher = cipher_by_name(ciphername)) == NULL) {
2987 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2990 if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
2991 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2994 if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
2995 r = SSH_ERR_INVALID_FORMAT;
2998 if ((passphrase == NULL || strlen(passphrase) == 0) &&
2999 strcmp(kdfname, "none") != 0) {
3000 /* passphrase required */
3001 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3005 /* check size of encrypted key blob */
3006 blocksize = cipher_blocksize(cipher);
3007 if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
3008 r = SSH_ERR_INVALID_FORMAT;
3013 keylen = cipher_keylen(cipher);
3014 ivlen = cipher_ivlen(cipher);
3015 authlen = cipher_authlen(cipher);
3016 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3017 r = SSH_ERR_ALLOC_FAIL;
3020 if (strcmp(kdfname, "bcrypt") == 0) {
3021 if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
3022 (r = sshbuf_get_u32(kdf, &rounds)) != 0)
3024 if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
3025 key, keylen + ivlen, rounds) < 0) {
3026 r = SSH_ERR_INVALID_FORMAT;
3031 /* check that an appropriate amount of auth data is present */
3032 if (sshbuf_len(decoded) < authlen ||
3033 sshbuf_len(decoded) - authlen < encrypted_len) {
3034 r = SSH_ERR_INVALID_FORMAT;
3038 /* decrypt private portion of key */
3039 if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
3040 (r = cipher_init(&ciphercontext, cipher, key, keylen,
3041 key + keylen, ivlen, 0)) != 0)
3043 if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
3044 encrypted_len, 0, authlen)) != 0) {
3045 /* an integrity error here indicates an incorrect passphrase */
3046 if (r == SSH_ERR_MAC_INVALID)
3047 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3050 if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
3052 /* there should be no trailing data */
3053 if (sshbuf_len(decoded) != 0) {
3054 r = SSH_ERR_INVALID_FORMAT;
3058 /* check check bytes */
3059 if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
3060 (r = sshbuf_get_u32(decrypted, &check2)) != 0)
3062 if (check1 != check2) {
3063 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3067 *decryptedp = decrypted;
3073 cipher_free(ciphercontext);
3076 sshkey_free(pubkey);
3078 explicit_bzero(salt, slen);
3082 explicit_bzero(key, keylen + ivlen);
3086 sshbuf_free(decrypted);
3091 sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
3092 struct sshkey **keyp, char **commentp)
3094 char *comment = NULL;
3095 int r = SSH_ERR_INTERNAL_ERROR;
3096 struct sshbuf *decoded = NULL, *decrypted = NULL;
3097 struct sshkey *k = NULL, *pubkey = NULL;
3101 if (commentp != NULL)
3104 /* Undo base64 encoding and decrypt the private section */
3105 if ((r = private2_uudecode(blob, &decoded)) != 0 ||
3106 (r = private2_decrypt(decoded, passphrase,
3107 &decrypted, &pubkey)) != 0)
3110 if (type != KEY_UNSPEC &&
3111 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
3112 r = SSH_ERR_KEY_TYPE_MISMATCH;
3116 /* Load the private key and comment */
3117 if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
3118 (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
3121 /* Check deterministic padding after private section */
3122 if ((r = private2_check_padding(decrypted)) != 0)
3125 /* Check that the public key in the envelope matches the private key */
3126 if (!sshkey_equal(pubkey, k)) {
3127 r = SSH_ERR_INVALID_FORMAT;
3137 if (commentp != NULL) {
3138 *commentp = comment;
3143 sshbuf_free(decoded);
3144 sshbuf_free(decrypted);
3146 sshkey_free(pubkey);
3151 sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
3152 struct sshkey **keyp)
3154 int r = SSH_ERR_INTERNAL_ERROR;
3155 struct sshbuf *decoded = NULL;
3156 struct sshkey *pubkey = NULL;
3162 if ((r = private2_uudecode(blob, &decoded)) != 0)
3164 /* parse public key from unencrypted envelope */
3165 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
3166 (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
3167 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
3168 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
3169 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
3173 /* XXX only one key supported at present */
3174 r = SSH_ERR_INVALID_FORMAT;
3178 /* Parse the public key */
3179 if ((r = sshkey_froms(decoded, &pubkey)) != 0)
3182 if (type != KEY_UNSPEC &&
3183 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
3184 r = SSH_ERR_KEY_TYPE_MISMATCH;
3195 sshbuf_free(decoded);
3196 sshkey_free(pubkey);
3201 /* convert SSH v2 key to PEM or PKCS#8 format */
3203 sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
3204 int format, const char *_passphrase, const char *comment)
3206 int was_shielded = sshkey_is_shielded(key);
3208 int blen, len = strlen(_passphrase);
3209 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
3210 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
3213 struct sshbuf *blob;
3214 EVP_PKEY *pkey = NULL;
3216 if (len > 0 && len <= 4)
3217 return SSH_ERR_PASSPHRASE_TOO_SHORT;
3218 if ((blob = sshbuf_new()) == NULL)
3219 return SSH_ERR_ALLOC_FAIL;
3220 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
3221 r = SSH_ERR_ALLOC_FAIL;
3224 if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
3225 r = SSH_ERR_ALLOC_FAIL;
3228 if ((r = sshkey_unshield_private(key)) != 0)
3231 switch (key->type) {
3233 if (format == SSHKEY_PRIVATE_PEM) {
3234 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
3235 cipher, passphrase, len, NULL, NULL);
3237 success = EVP_PKEY_set1_DSA(pkey, key->dsa);
3240 #ifdef OPENSSL_HAS_ECC
3242 if (format == SSHKEY_PRIVATE_PEM) {
3243 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
3244 cipher, passphrase, len, NULL, NULL);
3246 success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
3251 if (format == SSHKEY_PRIVATE_PEM) {
3252 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
3253 cipher, passphrase, len, NULL, NULL);
3255 success = EVP_PKEY_set1_RSA(pkey, key->rsa);
3263 r = SSH_ERR_LIBCRYPTO_ERROR;
3266 if (format == SSHKEY_PRIVATE_PKCS8) {
3267 if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
3268 passphrase, len, NULL, NULL)) == 0) {
3269 r = SSH_ERR_LIBCRYPTO_ERROR;
3273 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
3274 r = SSH_ERR_INTERNAL_ERROR;
3277 if ((r = sshbuf_put(blob, bptr, blen)) != 0)
3282 r = sshkey_shield_private(key);
3284 r = sshbuf_putb(buf, blob);
3286 EVP_PKEY_free(pkey);
3291 #endif /* WITH_OPENSSL */
3293 /* Serialise "key" to buffer "blob" */
3295 sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
3296 const char *passphrase, const char *comment,
3297 int format, const char *openssh_format_cipher, int openssh_format_rounds)
3299 switch (key->type) {
3304 break; /* see below */
3305 #endif /* WITH_OPENSSL */
3307 case KEY_ED25519_SK:
3310 #endif /* WITH_XMSS */
3313 #endif /* WITH_OPENSSL */
3314 return sshkey_private_to_blob2(key, blob, passphrase,
3315 comment, openssh_format_cipher, openssh_format_rounds);
3317 return SSH_ERR_KEY_TYPE_UNKNOWN;
3322 case SSHKEY_PRIVATE_OPENSSH:
3323 return sshkey_private_to_blob2(key, blob, passphrase,
3324 comment, openssh_format_cipher, openssh_format_rounds);
3325 case SSHKEY_PRIVATE_PEM:
3326 case SSHKEY_PRIVATE_PKCS8:
3327 return sshkey_private_to_blob_pem_pkcs8(key, blob,
3328 format, passphrase, comment);
3330 return SSH_ERR_INVALID_ARGUMENT;
3332 #endif /* WITH_OPENSSL */
3337 translate_libcrypto_error(unsigned long pem_err)
3339 int pem_reason = ERR_GET_REASON(pem_err);
3341 switch (ERR_GET_LIB(pem_err)) {
3343 switch (pem_reason) {
3344 case PEM_R_BAD_PASSWORD_READ:
3345 case PEM_R_PROBLEMS_GETTING_PASSWORD:
3346 case PEM_R_BAD_DECRYPT:
3347 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3349 return SSH_ERR_INVALID_FORMAT;
3352 switch (pem_reason) {
3353 case EVP_R_BAD_DECRYPT:
3354 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3355 #ifdef EVP_R_BN_DECODE_ERROR
3356 case EVP_R_BN_DECODE_ERROR:
3358 case EVP_R_DECODE_ERROR:
3359 #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
3360 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
3362 return SSH_ERR_INVALID_FORMAT;
3364 return SSH_ERR_LIBCRYPTO_ERROR;
3367 return SSH_ERR_INVALID_FORMAT;
3369 return SSH_ERR_LIBCRYPTO_ERROR;
3373 clear_libcrypto_errors(void)
3375 while (ERR_get_error() != 0)
3380 * Translate OpenSSL error codes to determine whether
3381 * passphrase is required/incorrect.
3384 convert_libcrypto_error(void)
3387 * Some password errors are reported at the beginning
3388 * of the error queue.
3390 if (translate_libcrypto_error(ERR_peek_error()) ==
3391 SSH_ERR_KEY_WRONG_PASSPHRASE)
3392 return SSH_ERR_KEY_WRONG_PASSPHRASE;
3393 return translate_libcrypto_error(ERR_peek_last_error());
3397 pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
3399 char *p = (char *)u;
3402 if (p == NULL || (len = strlen(p)) == 0)
3404 if (size < 0 || len > (size_t)size)
3406 memcpy(buf, p, len);
3411 sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
3412 const char *passphrase, struct sshkey **keyp)
3414 EVP_PKEY *pk = NULL;
3415 struct sshkey *prv = NULL;
3422 if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
3423 return SSH_ERR_ALLOC_FAIL;
3424 if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
3425 (int)sshbuf_len(blob)) {
3426 r = SSH_ERR_ALLOC_FAIL;
3430 clear_libcrypto_errors();
3431 if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
3432 (char *)passphrase)) == NULL) {
3434 * libcrypto may return various ASN.1 errors when attempting
3435 * to parse a key with an incorrect passphrase.
3436 * Treat all format errors as "incorrect passphrase" if a
3437 * passphrase was supplied.
3439 if (passphrase != NULL && *passphrase != '\0')
3440 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3442 r = convert_libcrypto_error();
3445 if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
3446 (type == KEY_UNSPEC || type == KEY_RSA)) {
3447 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3448 r = SSH_ERR_ALLOC_FAIL;
3451 prv->rsa = EVP_PKEY_get1_RSA(pk);
3452 prv->type = KEY_RSA;
3454 RSA_print_fp(stderr, prv->rsa, 8);
3456 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3457 r = SSH_ERR_LIBCRYPTO_ERROR;
3460 if ((r = sshkey_check_rsa_length(prv, 0)) != 0)
3462 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
3463 (type == KEY_UNSPEC || type == KEY_DSA)) {
3464 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3465 r = SSH_ERR_ALLOC_FAIL;
3468 prv->dsa = EVP_PKEY_get1_DSA(pk);
3469 prv->type = KEY_DSA;
3471 DSA_print_fp(stderr, prv->dsa, 8);
3473 #ifdef OPENSSL_HAS_ECC
3474 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
3475 (type == KEY_UNSPEC || type == KEY_ECDSA)) {
3476 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3477 r = SSH_ERR_ALLOC_FAIL;
3480 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
3481 prv->type = KEY_ECDSA;
3482 prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
3483 if (prv->ecdsa_nid == -1 ||
3484 sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
3485 sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
3486 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
3487 sshkey_ec_validate_private(prv->ecdsa) != 0) {
3488 r = SSH_ERR_INVALID_FORMAT;
3492 if (prv != NULL && prv->ecdsa != NULL)
3493 sshkey_dump_ec_key(prv->ecdsa);
3495 #endif /* OPENSSL_HAS_ECC */
3497 r = SSH_ERR_INVALID_FORMAT;
3511 #endif /* WITH_OPENSSL */
3514 sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
3515 const char *passphrase, struct sshkey **keyp, char **commentp)
3517 int r = SSH_ERR_INTERNAL_ERROR;
3521 if (commentp != NULL)
3527 /* No fallback for new-format-only keys */
3528 return sshkey_parse_private2(blob, type, passphrase,
3531 r = sshkey_parse_private2(blob, type, passphrase, keyp,
3533 /* Only fallback to PEM parser if a format error occurred. */
3534 if (r != SSH_ERR_INVALID_FORMAT)
3537 return sshkey_parse_private_pem_fileblob(blob, type,
3540 return SSH_ERR_INVALID_FORMAT;
3541 #endif /* WITH_OPENSSL */
3546 sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
3547 struct sshkey **keyp, char **commentp)
3551 if (commentp != NULL)
3554 return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
3555 passphrase, keyp, commentp);
3559 sshkey_sig_details_free(struct sshkey_sig_details *details)
3561 freezero(details, sizeof(*details));
3565 sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
3566 struct sshkey **pubkeyp)
3568 int r = SSH_ERR_INTERNAL_ERROR;
3570 if (pubkeyp != NULL)
3572 /* only new-format private keys bundle a public key inside */
3573 if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
3580 * serialize the key with the current state and forward the state
3584 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
3585 u_int32_t maxsign, int printerror)
3590 sshkey_type_plain(k->type) != KEY_XMSS)
3591 return sshkey_private_serialize_opt(k, b,
3592 SSHKEY_SERIALIZE_DEFAULT);
3593 if ((r = sshkey_xmss_get_state(k, printerror)) != 0 ||
3594 (r = sshkey_private_serialize_opt(k, b,
3595 SSHKEY_SERIALIZE_STATE)) != 0 ||
3596 (r = sshkey_xmss_forward_state(k, maxsign)) != 0)
3600 if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) {
3608 sshkey_signatures_left(const struct sshkey *k)
3610 if (sshkey_type_plain(k->type) == KEY_XMSS)
3611 return sshkey_xmss_signatures_left(k);
3616 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
3618 if (sshkey_type_plain(k->type) != KEY_XMSS)
3619 return SSH_ERR_INVALID_ARGUMENT;
3620 return sshkey_xmss_enable_maxsign(k, maxsign);
3624 sshkey_set_filename(struct sshkey *k, const char *filename)
3627 return SSH_ERR_INVALID_ARGUMENT;
3628 if (sshkey_type_plain(k->type) != KEY_XMSS)
3630 if (filename == NULL)
3631 return SSH_ERR_INVALID_ARGUMENT;
3632 if ((k->xmss_filename = strdup(filename)) == NULL)
3633 return SSH_ERR_ALLOC_FAIL;
3638 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
3639 u_int32_t maxsign, int printerror)
3641 return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
3645 sshkey_signatures_left(const struct sshkey *k)
3651 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
3653 return SSH_ERR_INVALID_ARGUMENT;
3657 sshkey_set_filename(struct sshkey *k, const char *filename)
3660 return SSH_ERR_INVALID_ARGUMENT;
3663 #endif /* WITH_XMSS */