1 /* $OpenBSD: sshkey.c,v 1.122 2022/09/17 10:30:45 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 */
99 const char *shortname;
106 static const struct keytype keytypes[] = {
107 { "ssh-ed25519", "ED25519", NULL, KEY_ED25519, 0, 0, 0 },
108 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", NULL,
109 KEY_ED25519_CERT, 0, 1, 0 },
111 { "sk-ssh-ed25519@openssh.com", "ED25519-SK", NULL,
112 KEY_ED25519_SK, 0, 0, 0 },
113 { "sk-ssh-ed25519-cert-v01@openssh.com", "ED25519-SK-CERT", NULL,
114 KEY_ED25519_SK_CERT, 0, 1, 0 },
117 { "ssh-xmss@openssh.com", "XMSS", NULL, KEY_XMSS, 0, 0, 0 },
118 { "ssh-xmss-cert-v01@openssh.com", "XMSS-CERT", NULL,
119 KEY_XMSS_CERT, 0, 1, 0 },
120 #endif /* WITH_XMSS */
122 { "ssh-rsa", "RSA", NULL, KEY_RSA, 0, 0, 0 },
123 { "rsa-sha2-256", "RSA", NULL, KEY_RSA, 0, 0, 1 },
124 { "rsa-sha2-512", "RSA", NULL, KEY_RSA, 0, 0, 1 },
125 { "ssh-dss", "DSA", NULL, KEY_DSA, 0, 0, 0 },
126 # ifdef OPENSSL_HAS_ECC
127 { "ecdsa-sha2-nistp256", "ECDSA", NULL,
128 KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 },
129 { "ecdsa-sha2-nistp384", "ECDSA", NULL,
130 KEY_ECDSA, NID_secp384r1, 0, 0 },
131 # ifdef OPENSSL_HAS_NISTP521
132 { "ecdsa-sha2-nistp521", "ECDSA", NULL,
133 KEY_ECDSA, NID_secp521r1, 0, 0 },
134 # endif /* OPENSSL_HAS_NISTP521 */
136 { "sk-ecdsa-sha2-nistp256@openssh.com", "ECDSA-SK", NULL,
137 KEY_ECDSA_SK, NID_X9_62_prime256v1, 0, 0 },
138 { "webauthn-sk-ecdsa-sha2-nistp256@openssh.com", "ECDSA-SK", NULL,
139 KEY_ECDSA_SK, NID_X9_62_prime256v1, 0, 1 },
140 # endif /* ENABLE_SK */
141 # endif /* OPENSSL_HAS_ECC */
142 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", NULL,
143 KEY_RSA_CERT, 0, 1, 0 },
144 { "rsa-sha2-256-cert-v01@openssh.com", "RSA-CERT",
145 "rsa-sha2-256", KEY_RSA_CERT, 0, 1, 1 },
146 { "rsa-sha2-512-cert-v01@openssh.com", "RSA-CERT",
147 "rsa-sha2-512", KEY_RSA_CERT, 0, 1, 1 },
148 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", NULL,
149 KEY_DSA_CERT, 0, 1, 0 },
150 # ifdef OPENSSL_HAS_ECC
151 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", NULL,
152 KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 },
153 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", NULL,
154 KEY_ECDSA_CERT, NID_secp384r1, 1, 0 },
155 # ifdef OPENSSL_HAS_NISTP521
156 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", NULL,
157 KEY_ECDSA_CERT, NID_secp521r1, 1, 0 },
158 # endif /* OPENSSL_HAS_NISTP521 */
160 { "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-SK-CERT", NULL,
161 KEY_ECDSA_SK_CERT, NID_X9_62_prime256v1, 1, 0 },
162 # endif /* ENABLE_SK */
163 # endif /* OPENSSL_HAS_ECC */
164 #endif /* WITH_OPENSSL */
165 { NULL, NULL, NULL, -1, -1, 0, 0 }
169 sshkey_type(const struct sshkey *k)
171 const struct keytype *kt;
173 for (kt = keytypes; kt->type != -1; kt++) {
174 if (kt->type == k->type)
175 return kt->shortname;
181 sshkey_ssh_name_from_type_nid(int type, int nid)
183 const struct keytype *kt;
185 for (kt = keytypes; kt->type != -1; kt++) {
186 if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
189 return "ssh-unknown";
193 sshkey_type_is_cert(int type)
195 const struct keytype *kt;
197 for (kt = keytypes; kt->type != -1; kt++) {
198 if (kt->type == type)
205 sshkey_ssh_name(const struct sshkey *k)
207 return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
211 sshkey_ssh_name_plain(const struct sshkey *k)
213 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
218 sshkey_type_from_name(const char *name)
220 const struct keytype *kt;
222 for (kt = keytypes; kt->type != -1; kt++) {
223 /* Only allow shortname matches for plain key types */
224 if ((kt->name != NULL && strcmp(name, kt->name) == 0) ||
225 (!kt->cert && strcasecmp(kt->shortname, name) == 0))
232 key_type_is_ecdsa_variant(int type)
238 case KEY_ECDSA_SK_CERT:
245 sshkey_ecdsa_nid_from_name(const char *name)
247 const struct keytype *kt;
249 for (kt = keytypes; kt->type != -1; kt++) {
250 if (!key_type_is_ecdsa_variant(kt->type))
252 if (kt->name != NULL && strcmp(name, kt->name) == 0)
259 sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
263 if (sigalgs == NULL || *sigalgs == '\0' ||
264 (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
266 else if (ktype == KEY_RSA) {
267 return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
268 match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
269 match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
270 } else if (ktype == KEY_RSA_CERT) {
271 return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
273 match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
275 match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
278 return match_pattern_list(keyname, sigalgs, 0) == 1;
282 sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
284 char *tmp, *ret = NULL;
285 size_t nlen, rlen = 0;
286 const struct keytype *kt;
288 for (kt = keytypes; kt->type != -1; kt++) {
289 if (kt->name == NULL)
291 if (!include_sigonly && kt->sigonly)
293 if ((certs_only && !kt->cert) || (plain_only && kt->cert))
297 nlen = strlen(kt->name);
298 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
303 memcpy(ret + rlen, kt->name, nlen + 1);
310 sshkey_names_valid2(const char *names, int allow_wildcard)
313 const struct keytype *kt;
316 if (names == NULL || strcmp(names, "") == 0)
318 if ((s = cp = strdup(names)) == NULL)
320 for ((p = strsep(&cp, ",")); p && *p != '\0';
321 (p = strsep(&cp, ","))) {
322 type = sshkey_type_from_name(p);
323 if (type == KEY_UNSPEC) {
324 if (allow_wildcard) {
326 * Try matching key types against the string.
327 * If any has a positive or negative match then
328 * the component is accepted.
330 for (kt = keytypes; kt->type != -1; kt++) {
331 if (match_pattern_list(kt->name,
347 sshkey_size(const struct sshkey *k)
350 const BIGNUM *rsa_n, *dsa_p;
351 #endif /* WITH_OPENSSL */
359 RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
360 return BN_num_bits(rsa_n);
365 DSA_get0_pqg(k->dsa, &dsa_p, NULL, NULL);
366 return BN_num_bits(dsa_p);
370 case KEY_ECDSA_SK_CERT:
371 return sshkey_curve_nid_to_bits(k->ecdsa_nid);
372 #endif /* WITH_OPENSSL */
374 case KEY_ED25519_CERT:
376 case KEY_ED25519_SK_CERT:
379 return 256; /* XXX */
385 sshkey_type_is_valid_ca(int type)
402 sshkey_is_cert(const struct sshkey *k)
406 return sshkey_type_is_cert(k->type);
410 sshkey_is_sk(const struct sshkey *k)
414 switch (sshkey_type_plain(k->type)) {
423 /* Return the cert-less equivalent to a certified key type */
425 sshkey_type_plain(int type)
434 case KEY_ECDSA_SK_CERT:
436 case KEY_ED25519_CERT:
438 case KEY_ED25519_SK_CERT:
439 return KEY_ED25519_SK;
448 /* XXX: these are really begging for a table-driven approach */
450 sshkey_curve_name_to_nid(const char *name)
452 if (strcmp(name, "nistp256") == 0)
453 return NID_X9_62_prime256v1;
454 else if (strcmp(name, "nistp384") == 0)
455 return NID_secp384r1;
456 # ifdef OPENSSL_HAS_NISTP521
457 else if (strcmp(name, "nistp521") == 0)
458 return NID_secp521r1;
459 # endif /* OPENSSL_HAS_NISTP521 */
465 sshkey_curve_nid_to_bits(int nid)
468 case NID_X9_62_prime256v1:
472 # ifdef OPENSSL_HAS_NISTP521
475 # endif /* OPENSSL_HAS_NISTP521 */
482 sshkey_ecdsa_bits_to_nid(int bits)
486 return NID_X9_62_prime256v1;
488 return NID_secp384r1;
489 # ifdef OPENSSL_HAS_NISTP521
491 return NID_secp521r1;
492 # endif /* OPENSSL_HAS_NISTP521 */
499 sshkey_curve_nid_to_name(int nid)
502 case NID_X9_62_prime256v1:
506 # ifdef OPENSSL_HAS_NISTP521
509 # endif /* OPENSSL_HAS_NISTP521 */
516 sshkey_ec_nid_to_hash_alg(int nid)
518 int kbits = sshkey_curve_nid_to_bits(nid);
523 /* RFC5656 section 6.2.1 */
525 return SSH_DIGEST_SHA256;
526 else if (kbits <= 384)
527 return SSH_DIGEST_SHA384;
529 return SSH_DIGEST_SHA512;
531 #endif /* WITH_OPENSSL */
534 cert_free(struct sshkey_cert *cert)
540 sshbuf_free(cert->certblob);
541 sshbuf_free(cert->critical);
542 sshbuf_free(cert->extensions);
544 for (i = 0; i < cert->nprincipals; i++)
545 free(cert->principals[i]);
546 free(cert->principals);
547 sshkey_free(cert->signature_key);
548 free(cert->signature_type);
549 freezero(cert, sizeof(*cert));
552 static struct sshkey_cert *
555 struct sshkey_cert *cert;
557 if ((cert = calloc(1, sizeof(*cert))) == NULL)
559 if ((cert->certblob = sshbuf_new()) == NULL ||
560 (cert->critical = sshbuf_new()) == NULL ||
561 (cert->extensions = sshbuf_new()) == NULL) {
566 cert->principals = NULL;
567 cert->signature_key = NULL;
568 cert->signature_type = NULL;
579 #endif /* WITH_OPENSSL */
581 if ((k = calloc(1, sizeof(*k))) == NULL)
589 k->ed25519_sk = NULL;
590 k->ed25519_pk = NULL;
597 if ((rsa = RSA_new()) == NULL) {
605 if ((dsa = DSA_new()) == NULL) {
614 case KEY_ECDSA_SK_CERT:
615 /* Cannot do anything until we know the group */
617 #endif /* WITH_OPENSSL */
619 case KEY_ED25519_CERT:
621 case KEY_ED25519_SK_CERT:
624 /* no need to prealloc */
633 if (sshkey_is_cert(k)) {
634 if ((k->cert = cert_new()) == NULL) {
644 sshkey_free(struct sshkey *k)
660 # ifdef OPENSSL_HAS_ECC
662 case KEY_ECDSA_SK_CERT:
663 free(k->sk_application);
664 sshbuf_free(k->sk_key_handle);
665 sshbuf_free(k->sk_reserved);
669 EC_KEY_free(k->ecdsa);
672 # endif /* OPENSSL_HAS_ECC */
673 #endif /* WITH_OPENSSL */
675 case KEY_ED25519_SK_CERT:
676 free(k->sk_application);
677 sshbuf_free(k->sk_key_handle);
678 sshbuf_free(k->sk_reserved);
681 case KEY_ED25519_CERT:
682 freezero(k->ed25519_pk, ED25519_PK_SZ);
683 k->ed25519_pk = NULL;
684 freezero(k->ed25519_sk, ED25519_SK_SZ);
685 k->ed25519_sk = NULL;
690 freezero(k->xmss_pk, sshkey_xmss_pklen(k));
692 freezero(k->xmss_sk, sshkey_xmss_sklen(k));
694 sshkey_xmss_free_state(k);
697 free(k->xmss_filename);
698 k->xmss_filename = NULL;
700 #endif /* WITH_XMSS */
706 if (sshkey_is_cert(k))
708 freezero(k->shielded_private, k->shielded_len);
709 freezero(k->shield_prekey, k->shield_prekey_len);
710 freezero(k, sizeof(*k));
714 cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
716 if (a == NULL && b == NULL)
718 if (a == NULL || b == NULL)
720 if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
722 if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
723 sshbuf_len(a->certblob)) != 0)
729 * Compare public portions of key only, allowing comparisons between
730 * certificates and plain keys too.
733 sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
735 #if defined(WITH_OPENSSL)
736 const BIGNUM *rsa_e_a, *rsa_n_a;
737 const BIGNUM *rsa_e_b, *rsa_n_b;
738 const BIGNUM *dsa_p_a, *dsa_q_a, *dsa_g_a, *dsa_pub_key_a;
739 const BIGNUM *dsa_p_b, *dsa_q_b, *dsa_g_b, *dsa_pub_key_b;
740 #endif /* WITH_OPENSSL */
742 if (a == NULL || b == NULL ||
743 sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
750 if (a->rsa == NULL || b->rsa == NULL)
752 RSA_get0_key(a->rsa, &rsa_n_a, &rsa_e_a, NULL);
753 RSA_get0_key(b->rsa, &rsa_n_b, &rsa_e_b, NULL);
754 return BN_cmp(rsa_e_a, rsa_e_b) == 0 &&
755 BN_cmp(rsa_n_a, rsa_n_b) == 0;
758 if (a->dsa == NULL || b->dsa == NULL)
760 DSA_get0_pqg(a->dsa, &dsa_p_a, &dsa_q_a, &dsa_g_a);
761 DSA_get0_pqg(b->dsa, &dsa_p_b, &dsa_q_b, &dsa_g_b);
762 DSA_get0_key(a->dsa, &dsa_pub_key_a, NULL);
763 DSA_get0_key(b->dsa, &dsa_pub_key_b, NULL);
764 return BN_cmp(dsa_p_a, dsa_p_b) == 0 &&
765 BN_cmp(dsa_q_a, dsa_q_b) == 0 &&
766 BN_cmp(dsa_g_a, dsa_g_b) == 0 &&
767 BN_cmp(dsa_pub_key_a, dsa_pub_key_b) == 0;
768 # ifdef OPENSSL_HAS_ECC
770 case KEY_ECDSA_SK_CERT:
771 if (a->sk_application == NULL || b->sk_application == NULL)
773 if (strcmp(a->sk_application, b->sk_application) != 0)
778 if (a->ecdsa == NULL || b->ecdsa == NULL ||
779 EC_KEY_get0_public_key(a->ecdsa) == NULL ||
780 EC_KEY_get0_public_key(b->ecdsa) == NULL)
782 if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
783 EC_KEY_get0_group(b->ecdsa), NULL) != 0 ||
784 EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
785 EC_KEY_get0_public_key(a->ecdsa),
786 EC_KEY_get0_public_key(b->ecdsa), NULL) != 0)
789 # endif /* OPENSSL_HAS_ECC */
790 #endif /* WITH_OPENSSL */
792 case KEY_ED25519_SK_CERT:
793 if (a->sk_application == NULL || b->sk_application == NULL)
795 if (strcmp(a->sk_application, b->sk_application) != 0)
799 case KEY_ED25519_CERT:
800 return a->ed25519_pk != NULL && b->ed25519_pk != NULL &&
801 memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0;
805 return a->xmss_pk != NULL && b->xmss_pk != NULL &&
806 sshkey_xmss_pklen(a) == sshkey_xmss_pklen(b) &&
807 memcmp(a->xmss_pk, b->xmss_pk, sshkey_xmss_pklen(a)) == 0;
808 #endif /* WITH_XMSS */
816 sshkey_equal(const struct sshkey *a, const struct sshkey *b)
818 if (a == NULL || b == NULL || a->type != b->type)
820 if (sshkey_is_cert(a)) {
821 if (!cert_compare(a->cert, b->cert))
824 return sshkey_equal_public(a, b);
828 to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
829 enum sshkey_serialize_rep opts)
831 int type, ret = SSH_ERR_INTERNAL_ERROR;
832 const char *typename;
834 const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
835 #endif /* WITH_OPENSSL */
838 return SSH_ERR_INVALID_ARGUMENT;
840 if (sshkey_is_cert(key)) {
841 if (key->cert == NULL)
842 return SSH_ERR_EXPECTED_CERT;
843 if (sshbuf_len(key->cert->certblob) == 0)
844 return SSH_ERR_KEY_LACKS_CERTBLOB;
846 type = force_plain ? sshkey_type_plain(key->type) : key->type;
847 typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
853 case KEY_ECDSA_SK_CERT:
855 #endif /* WITH_OPENSSL */
856 case KEY_ED25519_CERT:
857 case KEY_ED25519_SK_CERT:
860 #endif /* WITH_XMSS */
861 /* Use the existing blob */
862 /* XXX modified flag? */
863 if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
868 if (key->dsa == NULL)
869 return SSH_ERR_INVALID_ARGUMENT;
870 DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g);
871 DSA_get0_key(key->dsa, &dsa_pub_key, NULL);
872 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
873 (ret = sshbuf_put_bignum2(b, dsa_p)) != 0 ||
874 (ret = sshbuf_put_bignum2(b, dsa_q)) != 0 ||
875 (ret = sshbuf_put_bignum2(b, dsa_g)) != 0 ||
876 (ret = sshbuf_put_bignum2(b, dsa_pub_key)) != 0)
879 # ifdef OPENSSL_HAS_ECC
882 if (key->ecdsa == NULL)
883 return SSH_ERR_INVALID_ARGUMENT;
884 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
885 (ret = sshbuf_put_cstring(b,
886 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
887 (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0)
889 if (type == KEY_ECDSA_SK) {
890 if ((ret = sshbuf_put_cstring(b,
891 key->sk_application)) != 0)
897 if (key->rsa == NULL)
898 return SSH_ERR_INVALID_ARGUMENT;
899 RSA_get0_key(key->rsa, &rsa_n, &rsa_e, NULL);
900 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
901 (ret = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
902 (ret = sshbuf_put_bignum2(b, rsa_n)) != 0)
905 #endif /* WITH_OPENSSL */
908 if (key->ed25519_pk == NULL)
909 return SSH_ERR_INVALID_ARGUMENT;
910 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
911 (ret = sshbuf_put_string(b,
912 key->ed25519_pk, ED25519_PK_SZ)) != 0)
914 if (type == KEY_ED25519_SK) {
915 if ((ret = sshbuf_put_cstring(b,
916 key->sk_application)) != 0)
922 if (key->xmss_name == NULL || key->xmss_pk == NULL ||
923 sshkey_xmss_pklen(key) == 0)
924 return SSH_ERR_INVALID_ARGUMENT;
925 if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
926 (ret = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
927 (ret = sshbuf_put_string(b,
928 key->xmss_pk, sshkey_xmss_pklen(key))) != 0 ||
929 (ret = sshkey_xmss_serialize_pk_info(key, b, opts)) != 0)
932 #endif /* WITH_XMSS */
934 return SSH_ERR_KEY_TYPE_UNKNOWN;
940 sshkey_putb(const struct sshkey *key, struct sshbuf *b)
942 return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
946 sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
947 enum sshkey_serialize_rep opts)
952 if ((tmp = sshbuf_new()) == NULL)
953 return SSH_ERR_ALLOC_FAIL;
954 r = to_blob_buf(key, tmp, 0, opts);
956 r = sshbuf_put_stringb(b, tmp);
962 sshkey_puts(const struct sshkey *key, struct sshbuf *b)
964 return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
968 sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
970 return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
974 to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
975 enum sshkey_serialize_rep opts)
977 int ret = SSH_ERR_INTERNAL_ERROR;
979 struct sshbuf *b = NULL;
985 if ((b = sshbuf_new()) == NULL)
986 return SSH_ERR_ALLOC_FAIL;
987 if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
993 if ((*blobp = malloc(len)) == NULL) {
994 ret = SSH_ERR_ALLOC_FAIL;
997 memcpy(*blobp, sshbuf_ptr(b), len);
1006 sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
1008 return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
1012 sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
1014 return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
1018 sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
1019 u_char **retp, size_t *lenp)
1021 u_char *blob = NULL, *ret = NULL;
1022 size_t blob_len = 0;
1023 int r = SSH_ERR_INTERNAL_ERROR;
1029 if (ssh_digest_bytes(dgst_alg) == 0) {
1030 r = SSH_ERR_INVALID_ARGUMENT;
1033 if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
1036 if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
1037 r = SSH_ERR_ALLOC_FAIL;
1040 if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
1041 ret, SSH_DIGEST_MAX_LENGTH)) != 0)
1049 *lenp = ssh_digest_bytes(dgst_alg);
1054 freezero(blob, blob_len);
1059 fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
1062 size_t plen = strlen(alg) + 1;
1063 size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
1065 if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
1067 strlcpy(ret, alg, rlen);
1068 strlcat(ret, ":", rlen);
1069 if (dgst_raw_len == 0)
1071 if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
1072 freezero(ret, rlen);
1075 /* Trim padding characters from end */
1076 ret[strcspn(ret, "=")] = '\0';
1081 fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
1083 char *retval, hex[5];
1084 size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
1086 if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
1088 strlcpy(retval, alg, rlen);
1089 strlcat(retval, ":", rlen);
1090 for (i = 0; i < dgst_raw_len; i++) {
1091 snprintf(hex, sizeof(hex), "%s%02x",
1092 i > 0 ? ":" : "", dgst_raw[i]);
1093 strlcat(retval, hex, rlen);
1099 fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
1101 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
1102 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
1103 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1104 u_int i, j = 0, rounds, seed = 1;
1107 rounds = (dgst_raw_len / 2) + 1;
1108 if ((retval = calloc(rounds, 6)) == NULL)
1111 for (i = 0; i < rounds; i++) {
1112 u_int idx0, idx1, idx2, idx3, idx4;
1113 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
1114 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1116 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
1117 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1119 retval[j++] = vowels[idx0];
1120 retval[j++] = consonants[idx1];
1121 retval[j++] = vowels[idx2];
1122 if ((i + 1) < rounds) {
1123 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
1124 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
1125 retval[j++] = consonants[idx3];
1127 retval[j++] = consonants[idx4];
1128 seed = ((seed * 5) +
1129 ((((u_int)(dgst_raw[2 * i])) * 7) +
1130 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1136 retval[j++] = vowels[idx0];
1137 retval[j++] = consonants[idx1];
1138 retval[j++] = vowels[idx2];
1147 * Draw an ASCII-Art representing the fingerprint so human brain can
1148 * profit from its built-in pattern recognition ability.
1149 * This technique is called "random art" and can be found in some
1150 * scientific publications like this original paper:
1152 * "Hash Visualization: a New Technique to improve Real-World Security",
1153 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
1154 * Techniques and E-Commerce (CrypTEC '99)
1155 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
1157 * The subject came up in a talk by Dan Kaminsky, too.
1159 * If you see the picture is different, the key is different.
1160 * If the picture looks the same, you still know nothing.
1162 * The algorithm used here is a worm crawling over a discrete plane,
1163 * leaving a trace (augmenting the field) everywhere it goes.
1164 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
1165 * makes the respective movement vector be ignored for this turn.
1166 * Graphs are not unambiguous, because circles in graphs can be
1167 * walked in either direction.
1171 * Field sizes for the random art. Have to be odd, so the starting point
1172 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1173 * Else pictures would be too dense, and drawing the frame would
1174 * fail, too, because the key type would not fit in anymore.
1177 #define FLDSIZE_Y (FLDBASE + 1)
1178 #define FLDSIZE_X (FLDBASE * 2 + 1)
1180 fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
1181 const struct sshkey *k)
1184 * Chars to be used after each other every time the worm
1185 * intersects with itself. Matter of taste.
1187 char *augmentation_string = " .o+=*BOX@%&#/^SE";
1188 char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
1189 u_char field[FLDSIZE_X][FLDSIZE_Y];
1190 size_t i, tlen, hlen;
1193 size_t len = strlen(augmentation_string) - 1;
1195 if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1198 /* initialize field */
1199 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1203 /* process raw key */
1204 for (i = 0; i < dgst_raw_len; i++) {
1206 /* each byte conveys four 2-bit move commands */
1207 input = dgst_raw[i];
1208 for (b = 0; b < 4; b++) {
1209 /* evaluate 2 bit, rest is shifted later */
1210 x += (input & 0x1) ? 1 : -1;
1211 y += (input & 0x2) ? 1 : -1;
1213 /* assure we are still in bounds */
1216 x = MINIMUM(x, FLDSIZE_X - 1);
1217 y = MINIMUM(y, FLDSIZE_Y - 1);
1219 /* augment the field */
1220 if (field[x][y] < len - 2)
1226 /* mark starting point and end point*/
1227 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1230 /* assemble title */
1231 r = snprintf(title, sizeof(title), "[%s %u]",
1232 sshkey_type(k), sshkey_size(k));
1233 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1234 if (r < 0 || r > (int)sizeof(title))
1235 r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1236 tlen = (r <= 0) ? 0 : strlen(title);
1238 /* assemble hash ID. */
1239 r = snprintf(hash, sizeof(hash), "[%s]", alg);
1240 hlen = (r <= 0) ? 0 : strlen(hash);
1242 /* output upper border */
1245 for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1247 memcpy(p, title, tlen);
1249 for (i += tlen; i < FLDSIZE_X; i++)
1254 /* output content */
1255 for (y = 0; y < FLDSIZE_Y; y++) {
1257 for (x = 0; x < FLDSIZE_X; x++)
1258 *p++ = augmentation_string[MINIMUM(field[x][y], len)];
1263 /* output lower border */
1265 for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1267 memcpy(p, hash, hlen);
1269 for (i += hlen; i < FLDSIZE_X; i++)
1277 sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
1278 enum sshkey_fp_rep dgst_rep)
1280 char *retval = NULL;
1282 size_t dgst_raw_len;
1284 if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
1287 case SSH_FP_DEFAULT:
1288 if (dgst_alg == SSH_DIGEST_MD5) {
1289 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1290 dgst_raw, dgst_raw_len);
1292 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1293 dgst_raw, dgst_raw_len);
1297 retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1298 dgst_raw, dgst_raw_len);
1301 retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1302 dgst_raw, dgst_raw_len);
1304 case SSH_FP_BUBBLEBABBLE:
1305 retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1307 case SSH_FP_RANDOMART:
1308 retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1309 dgst_raw, dgst_raw_len, k);
1312 freezero(dgst_raw, dgst_raw_len);
1315 freezero(dgst_raw, dgst_raw_len);
1320 peek_type_nid(const char *s, size_t l, int *nid)
1322 const struct keytype *kt;
1324 for (kt = keytypes; kt->type != -1; kt++) {
1325 if (kt->name == NULL || strlen(kt->name) != l)
1327 if (memcmp(s, kt->name, l) == 0) {
1329 if (key_type_is_ecdsa_variant(kt->type))
1337 /* XXX this can now be made const char * */
1339 sshkey_read(struct sshkey *ret, char **cpp)
1342 char *cp, *blobcopy;
1344 int r, type, curve_nid = -1;
1345 struct sshbuf *blob;
1348 return SSH_ERR_INVALID_ARGUMENT;
1350 switch (ret->type) {
1357 case KEY_ED25519_SK:
1359 case KEY_ECDSA_CERT:
1360 case KEY_ECDSA_SK_CERT:
1362 case KEY_ED25519_CERT:
1363 case KEY_ED25519_SK_CERT:
1367 #endif /* WITH_XMSS */
1370 return SSH_ERR_INVALID_ARGUMENT;
1375 space = strcspn(cp, " \t");
1376 if (space == strlen(cp))
1377 return SSH_ERR_INVALID_FORMAT;
1378 if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
1379 return SSH_ERR_INVALID_FORMAT;
1381 /* skip whitespace */
1382 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1385 return SSH_ERR_INVALID_FORMAT;
1386 if (ret->type != KEY_UNSPEC && ret->type != type)
1387 return SSH_ERR_KEY_TYPE_MISMATCH;
1388 if ((blob = sshbuf_new()) == NULL)
1389 return SSH_ERR_ALLOC_FAIL;
1391 /* find end of keyblob and decode */
1392 space = strcspn(cp, " \t");
1393 if ((blobcopy = strndup(cp, space)) == NULL) {
1395 return SSH_ERR_ALLOC_FAIL;
1397 if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
1403 if ((r = sshkey_fromb(blob, &k)) != 0) {
1409 /* skip whitespace and leave cp at start of comment */
1410 for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
1413 /* ensure type of blob matches type at start of line */
1414 if (k->type != type) {
1416 return SSH_ERR_KEY_TYPE_MISMATCH;
1418 if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
1420 return SSH_ERR_EC_CURVE_MISMATCH;
1423 /* Fill in ret from parsed key */
1425 if (sshkey_is_cert(ret)) {
1426 if (!sshkey_is_cert(k)) {
1428 return SSH_ERR_EXPECTED_CERT;
1430 if (ret->cert != NULL)
1431 cert_free(ret->cert);
1432 ret->cert = k->cert;
1435 switch (sshkey_type_plain(ret->type)) {
1442 RSA_print_fp(stderr, ret->rsa, 8);
1450 DSA_print_fp(stderr, ret->dsa, 8);
1453 # ifdef OPENSSL_HAS_ECC
1455 EC_KEY_free(ret->ecdsa);
1456 ret->ecdsa = k->ecdsa;
1457 ret->ecdsa_nid = k->ecdsa_nid;
1461 sshkey_dump_ec_key(ret->ecdsa);
1465 EC_KEY_free(ret->ecdsa);
1466 ret->ecdsa = k->ecdsa;
1467 ret->ecdsa_nid = k->ecdsa_nid;
1468 ret->sk_application = k->sk_application;
1471 k->sk_application = NULL;
1473 sshkey_dump_ec_key(ret->ecdsa);
1474 fprintf(stderr, "App: %s\n", ret->sk_application);
1477 # endif /* OPENSSL_HAS_ECC */
1478 #endif /* WITH_OPENSSL */
1480 freezero(ret->ed25519_pk, ED25519_PK_SZ);
1481 ret->ed25519_pk = k->ed25519_pk;
1482 k->ed25519_pk = NULL;
1487 case KEY_ED25519_SK:
1488 freezero(ret->ed25519_pk, ED25519_PK_SZ);
1489 ret->ed25519_pk = k->ed25519_pk;
1490 ret->sk_application = k->sk_application;
1491 k->ed25519_pk = NULL;
1492 k->sk_application = NULL;
1497 ret->xmss_pk = k->xmss_pk;
1499 free(ret->xmss_state);
1500 ret->xmss_state = k->xmss_state;
1501 k->xmss_state = NULL;
1502 free(ret->xmss_name);
1503 ret->xmss_name = k->xmss_name;
1504 k->xmss_name = NULL;
1505 free(ret->xmss_filename);
1506 ret->xmss_filename = k->xmss_filename;
1507 k->xmss_filename = NULL;
1512 #endif /* WITH_XMSS */
1515 return SSH_ERR_INTERNAL_ERROR;
1526 sshkey_to_base64(const struct sshkey *key, char **b64p)
1528 int r = SSH_ERR_INTERNAL_ERROR;
1529 struct sshbuf *b = NULL;
1534 if ((b = sshbuf_new()) == NULL)
1535 return SSH_ERR_ALLOC_FAIL;
1536 if ((r = sshkey_putb(key, b)) != 0)
1538 if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
1539 r = SSH_ERR_ALLOC_FAIL;
1555 sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1557 int r = SSH_ERR_INTERNAL_ERROR;
1560 if ((r = sshkey_to_base64(key, &uu)) != 0)
1562 if ((r = sshbuf_putf(b, "%s %s",
1563 sshkey_ssh_name(key), uu)) != 0)
1572 sshkey_write(const struct sshkey *key, FILE *f)
1574 struct sshbuf *b = NULL;
1575 int r = SSH_ERR_INTERNAL_ERROR;
1577 if ((b = sshbuf_new()) == NULL)
1578 return SSH_ERR_ALLOC_FAIL;
1579 if ((r = sshkey_format_text(key, b)) != 0)
1581 if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1584 r = SSH_ERR_SYSTEM_ERROR;
1595 sshkey_cert_type(const struct sshkey *k)
1597 switch (k->cert->type) {
1598 case SSH2_CERT_TYPE_USER:
1600 case SSH2_CERT_TYPE_HOST:
1609 rsa_generate_private_key(u_int bits, RSA **rsap)
1611 RSA *private = NULL;
1613 int ret = SSH_ERR_INTERNAL_ERROR;
1616 return SSH_ERR_INVALID_ARGUMENT;
1617 if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1618 bits > SSHBUF_MAX_BIGNUM * 8)
1619 return SSH_ERR_KEY_LENGTH;
1621 if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
1622 ret = SSH_ERR_ALLOC_FAIL;
1625 if (!BN_set_word(f4, RSA_F4) ||
1626 !RSA_generate_key_ex(private, bits, f4, NULL)) {
1627 ret = SSH_ERR_LIBCRYPTO_ERROR;
1640 dsa_generate_private_key(u_int bits, DSA **dsap)
1643 int ret = SSH_ERR_INTERNAL_ERROR;
1646 return SSH_ERR_INVALID_ARGUMENT;
1648 return SSH_ERR_KEY_LENGTH;
1649 if ((private = DSA_new()) == NULL) {
1650 ret = SSH_ERR_ALLOC_FAIL;
1654 if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
1655 NULL, NULL) || !DSA_generate_key(private)) {
1656 ret = SSH_ERR_LIBCRYPTO_ERROR;
1667 # ifdef OPENSSL_HAS_ECC
1669 sshkey_ecdsa_key_to_nid(EC_KEY *k)
1673 NID_X9_62_prime256v1,
1675 # ifdef OPENSSL_HAS_NISTP521
1677 # endif /* OPENSSL_HAS_NISTP521 */
1682 const EC_GROUP *g = EC_KEY_get0_group(k);
1685 * The group may be stored in a ASN.1 encoded private key in one of two
1686 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1687 * or explicit group parameters encoded into the key blob. Only the
1688 * "named group" case sets the group NID for us, but we can figure
1689 * it out for the other case by comparing against all the groups that
1692 if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1694 for (i = 0; nids[i] != -1; i++) {
1695 if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1697 if (EC_GROUP_cmp(g, eg, NULL) == 0)
1701 if (nids[i] != -1) {
1702 /* Use the group with the NID attached */
1703 EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1704 if (EC_KEY_set_group(k, eg) != 1) {
1713 ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap)
1716 int ret = SSH_ERR_INTERNAL_ERROR;
1718 if (nid == NULL || ecdsap == NULL)
1719 return SSH_ERR_INVALID_ARGUMENT;
1720 if ((*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
1721 return SSH_ERR_KEY_LENGTH;
1723 if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) {
1724 ret = SSH_ERR_ALLOC_FAIL;
1727 if (EC_KEY_generate_key(private) != 1) {
1728 ret = SSH_ERR_LIBCRYPTO_ERROR;
1731 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
1736 EC_KEY_free(private);
1739 # endif /* OPENSSL_HAS_ECC */
1740 #endif /* WITH_OPENSSL */
1743 sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1746 int ret = SSH_ERR_INTERNAL_ERROR;
1749 return SSH_ERR_INVALID_ARGUMENT;
1751 if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1752 return SSH_ERR_ALLOC_FAIL;
1755 if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
1756 (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) {
1757 ret = SSH_ERR_ALLOC_FAIL;
1760 crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
1765 ret = sshkey_xmss_generate_private_key(k, bits);
1767 #endif /* WITH_XMSS */
1770 ret = dsa_generate_private_key(bits, &k->dsa);
1772 # ifdef OPENSSL_HAS_ECC
1774 ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid,
1777 # endif /* OPENSSL_HAS_ECC */
1779 ret = rsa_generate_private_key(bits, &k->rsa);
1781 #endif /* WITH_OPENSSL */
1783 ret = SSH_ERR_INVALID_ARGUMENT;
1794 sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1797 const struct sshkey_cert *from;
1798 struct sshkey_cert *to;
1799 int r = SSH_ERR_INTERNAL_ERROR;
1801 if (to_key == NULL || (from = from_key->cert) == NULL)
1802 return SSH_ERR_INVALID_ARGUMENT;
1804 if ((to = cert_new()) == NULL)
1805 return SSH_ERR_ALLOC_FAIL;
1807 if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
1808 (r = sshbuf_putb(to->critical, from->critical)) != 0 ||
1809 (r = sshbuf_putb(to->extensions, from->extensions)) != 0)
1812 to->serial = from->serial;
1813 to->type = from->type;
1814 if (from->key_id == NULL)
1816 else if ((to->key_id = strdup(from->key_id)) == NULL) {
1817 r = SSH_ERR_ALLOC_FAIL;
1820 to->valid_after = from->valid_after;
1821 to->valid_before = from->valid_before;
1822 if (from->signature_key == NULL)
1823 to->signature_key = NULL;
1824 else if ((r = sshkey_from_private(from->signature_key,
1825 &to->signature_key)) != 0)
1827 if (from->signature_type != NULL &&
1828 (to->signature_type = strdup(from->signature_type)) == NULL) {
1829 r = SSH_ERR_ALLOC_FAIL;
1832 if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
1833 r = SSH_ERR_INVALID_ARGUMENT;
1836 if (from->nprincipals > 0) {
1837 if ((to->principals = calloc(from->nprincipals,
1838 sizeof(*to->principals))) == NULL) {
1839 r = SSH_ERR_ALLOC_FAIL;
1842 for (i = 0; i < from->nprincipals; i++) {
1843 to->principals[i] = strdup(from->principals[i]);
1844 if (to->principals[i] == NULL) {
1845 to->nprincipals = i;
1846 r = SSH_ERR_ALLOC_FAIL;
1851 to->nprincipals = from->nprincipals;
1854 cert_free(to_key->cert);
1864 sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1866 struct sshkey *n = NULL;
1867 int r = SSH_ERR_INTERNAL_ERROR;
1869 const BIGNUM *rsa_n, *rsa_e;
1870 BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL;
1871 const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
1872 BIGNUM *dsa_p_dup = NULL, *dsa_q_dup = NULL, *dsa_g_dup = NULL;
1873 BIGNUM *dsa_pub_key_dup = NULL;
1874 #endif /* WITH_OPENSSL */
1877 if ((n = sshkey_new(k->type)) == NULL) {
1878 r = SSH_ERR_ALLOC_FAIL;
1885 DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g);
1886 DSA_get0_key(k->dsa, &dsa_pub_key, NULL);
1887 if ((dsa_p_dup = BN_dup(dsa_p)) == NULL ||
1888 (dsa_q_dup = BN_dup(dsa_q)) == NULL ||
1889 (dsa_g_dup = BN_dup(dsa_g)) == NULL ||
1890 (dsa_pub_key_dup = BN_dup(dsa_pub_key)) == NULL) {
1891 r = SSH_ERR_ALLOC_FAIL;
1894 if (!DSA_set0_pqg(n->dsa, dsa_p_dup, dsa_q_dup, dsa_g_dup)) {
1895 r = SSH_ERR_LIBCRYPTO_ERROR;
1898 dsa_p_dup = dsa_q_dup = dsa_g_dup = NULL; /* transferred */
1899 if (!DSA_set0_key(n->dsa, dsa_pub_key_dup, NULL)) {
1900 r = SSH_ERR_LIBCRYPTO_ERROR;
1903 dsa_pub_key_dup = NULL; /* transferred */
1906 # ifdef OPENSSL_HAS_ECC
1908 case KEY_ECDSA_CERT:
1910 case KEY_ECDSA_SK_CERT:
1911 n->ecdsa_nid = k->ecdsa_nid;
1912 n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
1913 if (n->ecdsa == NULL) {
1914 r = SSH_ERR_ALLOC_FAIL;
1917 if (EC_KEY_set_public_key(n->ecdsa,
1918 EC_KEY_get0_public_key(k->ecdsa)) != 1) {
1919 r = SSH_ERR_LIBCRYPTO_ERROR;
1922 if (k->type != KEY_ECDSA_SK && k->type != KEY_ECDSA_SK_CERT)
1924 /* Append security-key application string */
1925 if ((n->sk_application = strdup(k->sk_application)) == NULL)
1928 # endif /* OPENSSL_HAS_ECC */
1931 RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL);
1932 if ((rsa_n_dup = BN_dup(rsa_n)) == NULL ||
1933 (rsa_e_dup = BN_dup(rsa_e)) == NULL) {
1934 r = SSH_ERR_ALLOC_FAIL;
1937 if (!RSA_set0_key(n->rsa, rsa_n_dup, rsa_e_dup, NULL)) {
1938 r = SSH_ERR_LIBCRYPTO_ERROR;
1941 rsa_n_dup = rsa_e_dup = NULL; /* transferred */
1943 #endif /* WITH_OPENSSL */
1945 case KEY_ED25519_CERT:
1946 case KEY_ED25519_SK:
1947 case KEY_ED25519_SK_CERT:
1948 if (k->ed25519_pk != NULL) {
1949 if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
1950 r = SSH_ERR_ALLOC_FAIL;
1953 memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
1955 if (k->type != KEY_ED25519_SK &&
1956 k->type != KEY_ED25519_SK_CERT)
1958 /* Append security-key application string */
1959 if ((n->sk_application = strdup(k->sk_application)) == NULL)
1965 if ((r = sshkey_xmss_init(n, k->xmss_name)) != 0)
1967 if (k->xmss_pk != NULL) {
1969 size_t pklen = sshkey_xmss_pklen(k);
1970 if (pklen == 0 || sshkey_xmss_pklen(n) != pklen) {
1971 r = SSH_ERR_INTERNAL_ERROR;
1974 if ((n->xmss_pk = malloc(pklen)) == NULL) {
1975 r = SSH_ERR_ALLOC_FAIL;
1978 memcpy(n->xmss_pk, k->xmss_pk, pklen);
1979 /* simulate number of signatures left on pubkey */
1980 left = sshkey_xmss_signatures_left(k);
1982 sshkey_xmss_enable_maxsign(n, left);
1985 #endif /* WITH_XMSS */
1987 r = SSH_ERR_KEY_TYPE_UNKNOWN;
1990 if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
1999 BN_clear_free(rsa_n_dup);
2000 BN_clear_free(rsa_e_dup);
2001 BN_clear_free(dsa_p_dup);
2002 BN_clear_free(dsa_q_dup);
2003 BN_clear_free(dsa_g_dup);
2004 BN_clear_free(dsa_pub_key_dup);
2011 sshkey_is_shielded(struct sshkey *k)
2013 return k != NULL && k->shielded_private != NULL;
2017 sshkey_shield_private(struct sshkey *k)
2019 struct sshbuf *prvbuf = NULL;
2020 u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
2021 struct sshcipher_ctx *cctx = NULL;
2022 const struct sshcipher *cipher;
2023 size_t i, enclen = 0;
2024 struct sshkey *kswap = NULL, tmp;
2025 int r = SSH_ERR_INTERNAL_ERROR;
2028 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
2030 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
2031 r = SSH_ERR_INVALID_ARGUMENT;
2034 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
2035 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
2036 r = SSH_ERR_INTERNAL_ERROR;
2040 /* Prepare a random pre-key, and from it an ephemeral key */
2041 if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) {
2042 r = SSH_ERR_ALLOC_FAIL;
2045 arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
2046 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
2047 prekey, SSHKEY_SHIELD_PREKEY_LEN,
2048 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
2051 fprintf(stderr, "%s: key+iv\n", __func__);
2052 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
2055 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
2056 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
2059 /* Serialise and encrypt the private key using the ephemeral key */
2060 if ((prvbuf = sshbuf_new()) == NULL) {
2061 r = SSH_ERR_ALLOC_FAIL;
2064 if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
2066 if ((r = sshkey_private_serialize_opt(k, prvbuf,
2067 SSHKEY_SERIALIZE_SHIELD)) != 0)
2069 /* pad to cipher blocksize */
2071 while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
2072 if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
2076 fprintf(stderr, "%s: serialised\n", __func__);
2077 sshbuf_dump(prvbuf, stderr);
2080 enclen = sshbuf_len(prvbuf);
2081 if ((enc = malloc(enclen)) == NULL) {
2082 r = SSH_ERR_ALLOC_FAIL;
2085 if ((r = cipher_crypt(cctx, 0, enc,
2086 sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
2089 fprintf(stderr, "%s: encrypted\n", __func__);
2090 sshbuf_dump_data(enc, enclen, stderr);
2093 /* Make a scrubbed, public-only copy of our private key argument */
2094 if ((r = sshkey_from_private(k, &kswap)) != 0)
2097 /* Swap the private key out (it will be destroyed below) */
2102 /* Insert the shielded key into our argument */
2103 k->shielded_private = enc;
2104 k->shielded_len = enclen;
2105 k->shield_prekey = prekey;
2106 k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
2107 enc = prekey = NULL; /* transferred */
2110 /* preserve key fields that are required for correct operation */
2111 k->sk_flags = kswap->sk_flags;
2117 /* XXX behaviour on error - invalidate original private key? */
2119 explicit_bzero(keyiv, sizeof(keyiv));
2120 explicit_bzero(&tmp, sizeof(tmp));
2121 freezero(enc, enclen);
2122 freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN);
2124 sshbuf_free(prvbuf);
2128 /* Check deterministic padding after private key */
2130 private2_check_padding(struct sshbuf *decrypted)
2137 while (sshbuf_len(decrypted)) {
2138 if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
2140 if (pad != (++i & 0xff)) {
2141 r = SSH_ERR_INVALID_FORMAT;
2148 explicit_bzero(&pad, sizeof(pad));
2149 explicit_bzero(&i, sizeof(i));
2154 sshkey_unshield_private(struct sshkey *k)
2156 struct sshbuf *prvbuf = NULL;
2157 u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
2158 struct sshcipher_ctx *cctx = NULL;
2159 const struct sshcipher *cipher;
2160 struct sshkey *kswap = NULL, tmp;
2161 int r = SSH_ERR_INTERNAL_ERROR;
2164 fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
2166 if (!sshkey_is_shielded(k))
2167 return 0; /* nothing to do */
2169 if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
2170 r = SSH_ERR_INVALID_ARGUMENT;
2173 if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
2174 ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
2175 r = SSH_ERR_INTERNAL_ERROR;
2178 /* check size of shielded key blob */
2179 if (k->shielded_len < cipher_blocksize(cipher) ||
2180 (k->shielded_len % cipher_blocksize(cipher)) != 0) {
2181 r = SSH_ERR_INVALID_FORMAT;
2185 /* Calculate the ephemeral key from the prekey */
2186 if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
2187 k->shield_prekey, k->shield_prekey_len,
2188 keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
2190 if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
2191 keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
2194 fprintf(stderr, "%s: key+iv\n", __func__);
2195 sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
2199 /* Decrypt and parse the shielded private key using the ephemeral key */
2200 if ((prvbuf = sshbuf_new()) == NULL) {
2201 r = SSH_ERR_ALLOC_FAIL;
2204 if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
2208 fprintf(stderr, "%s: encrypted\n", __func__);
2209 sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
2211 if ((r = cipher_crypt(cctx, 0, cp,
2212 k->shielded_private, k->shielded_len, 0, 0)) != 0)
2215 fprintf(stderr, "%s: serialised\n", __func__);
2216 sshbuf_dump(prvbuf, stderr);
2218 /* Parse private key */
2219 if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
2222 if ((r = private2_check_padding(prvbuf)) != 0)
2225 /* Swap the parsed key back into place */
2235 explicit_bzero(keyiv, sizeof(keyiv));
2236 explicit_bzero(&tmp, sizeof(tmp));
2238 sshbuf_free(prvbuf);
2243 cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
2245 struct sshbuf *principals = NULL, *crit = NULL;
2246 struct sshbuf *exts = NULL, *ca = NULL;
2248 size_t signed_len = 0, slen = 0, kidlen = 0;
2249 int ret = SSH_ERR_INTERNAL_ERROR;
2251 /* Copy the entire key blob for verification and later serialisation */
2252 if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
2255 /* Parse body of certificate up to signature */
2256 if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
2257 (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
2258 (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
2259 (ret = sshbuf_froms(b, &principals)) != 0 ||
2260 (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
2261 (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
2262 (ret = sshbuf_froms(b, &crit)) != 0 ||
2263 (ret = sshbuf_froms(b, &exts)) != 0 ||
2264 (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
2265 (ret = sshbuf_froms(b, &ca)) != 0) {
2266 /* XXX debug print error for ret */
2267 ret = SSH_ERR_INVALID_FORMAT;
2271 /* Signature is left in the buffer so we can calculate this length */
2272 signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
2274 if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
2275 ret = SSH_ERR_INVALID_FORMAT;
2279 if (key->cert->type != SSH2_CERT_TYPE_USER &&
2280 key->cert->type != SSH2_CERT_TYPE_HOST) {
2281 ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
2285 /* Parse principals section */
2286 while (sshbuf_len(principals) > 0) {
2287 char *principal = NULL;
2288 char **oprincipals = NULL;
2290 if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
2291 ret = SSH_ERR_INVALID_FORMAT;
2294 if ((ret = sshbuf_get_cstring(principals, &principal,
2296 ret = SSH_ERR_INVALID_FORMAT;
2299 oprincipals = key->cert->principals;
2300 key->cert->principals = recallocarray(key->cert->principals,
2301 key->cert->nprincipals, key->cert->nprincipals + 1,
2302 sizeof(*key->cert->principals));
2303 if (key->cert->principals == NULL) {
2305 key->cert->principals = oprincipals;
2306 ret = SSH_ERR_ALLOC_FAIL;
2309 key->cert->principals[key->cert->nprincipals++] = principal;
2313 * Stash a copies of the critical options and extensions sections
2316 if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
2318 (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
2322 * Validate critical options and extensions sections format.
2324 while (sshbuf_len(crit) != 0) {
2325 if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
2326 (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
2327 sshbuf_reset(key->cert->critical);
2328 ret = SSH_ERR_INVALID_FORMAT;
2332 while (exts != NULL && sshbuf_len(exts) != 0) {
2333 if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
2334 (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
2335 sshbuf_reset(key->cert->extensions);
2336 ret = SSH_ERR_INVALID_FORMAT;
2341 /* Parse CA key and check signature */
2342 if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
2343 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2346 if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
2347 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2350 if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
2351 sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
2353 if ((ret = sshkey_get_sigtype(sig, slen,
2354 &key->cert->signature_type)) != 0)
2363 sshbuf_free(principals);
2369 sshkey_check_rsa_length(const struct sshkey *k, int min_size)
2372 const BIGNUM *rsa_n;
2375 if (k == NULL || k->rsa == NULL ||
2376 (k->type != KEY_RSA && k->type != KEY_RSA_CERT))
2378 RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
2379 nbits = BN_num_bits(rsa_n);
2380 if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
2381 (min_size > 0 && nbits < min_size))
2382 return SSH_ERR_KEY_LENGTH;
2383 #endif /* WITH_OPENSSL */
2388 sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
2391 int type, ret = SSH_ERR_INTERNAL_ERROR;
2392 char *ktype = NULL, *curve = NULL, *xmss_name = NULL;
2393 struct sshkey *key = NULL;
2396 struct sshbuf *copy;
2397 #if defined(WITH_OPENSSL)
2398 BIGNUM *rsa_n = NULL, *rsa_e = NULL;
2399 BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL, *dsa_pub_key = NULL;
2400 # if defined(OPENSSL_HAS_ECC)
2402 # endif /* OPENSSL_HAS_ECC */
2403 #endif /* WITH_OPENSSL */
2405 #ifdef DEBUG_PK /* XXX */
2406 sshbuf_dump(b, stderr);
2410 if ((copy = sshbuf_fromb(b)) == NULL) {
2411 ret = SSH_ERR_ALLOC_FAIL;
2414 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
2415 ret = SSH_ERR_INVALID_FORMAT;
2419 type = sshkey_type_from_name(ktype);
2420 if (!allow_cert && sshkey_type_is_cert(type)) {
2421 ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2428 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2429 ret = SSH_ERR_INVALID_FORMAT;
2434 if ((key = sshkey_new(type)) == NULL) {
2435 ret = SSH_ERR_ALLOC_FAIL;
2438 if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||
2439 sshbuf_get_bignum2(b, &rsa_n) != 0) {
2440 ret = SSH_ERR_INVALID_FORMAT;
2443 if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, NULL)) {
2444 ret = SSH_ERR_LIBCRYPTO_ERROR;
2447 rsa_n = rsa_e = NULL; /* transferred */
2448 if ((ret = sshkey_check_rsa_length(key, 0)) != 0)
2451 RSA_print_fp(stderr, key->rsa, 8);
2456 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2457 ret = SSH_ERR_INVALID_FORMAT;
2462 if ((key = sshkey_new(type)) == NULL) {
2463 ret = SSH_ERR_ALLOC_FAIL;
2466 if (sshbuf_get_bignum2(b, &dsa_p) != 0 ||
2467 sshbuf_get_bignum2(b, &dsa_q) != 0 ||
2468 sshbuf_get_bignum2(b, &dsa_g) != 0 ||
2469 sshbuf_get_bignum2(b, &dsa_pub_key) != 0) {
2470 ret = SSH_ERR_INVALID_FORMAT;
2473 if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g)) {
2474 ret = SSH_ERR_LIBCRYPTO_ERROR;
2477 dsa_p = dsa_q = dsa_g = NULL; /* transferred */
2478 if (!DSA_set0_key(key->dsa, dsa_pub_key, NULL)) {
2479 ret = SSH_ERR_LIBCRYPTO_ERROR;
2482 dsa_pub_key = NULL; /* transferred */
2484 DSA_print_fp(stderr, key->dsa, 8);
2487 # ifdef OPENSSL_HAS_ECC
2488 case KEY_ECDSA_CERT:
2489 case KEY_ECDSA_SK_CERT:
2491 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2492 ret = SSH_ERR_INVALID_FORMAT;
2498 if ((key = sshkey_new(type)) == NULL) {
2499 ret = SSH_ERR_ALLOC_FAIL;
2502 key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype);
2503 if (sshbuf_get_cstring(b, &curve, NULL) != 0) {
2504 ret = SSH_ERR_INVALID_FORMAT;
2507 if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
2508 ret = SSH_ERR_EC_CURVE_MISMATCH;
2511 EC_KEY_free(key->ecdsa);
2512 if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
2514 ret = SSH_ERR_EC_CURVE_INVALID;
2517 if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) {
2518 ret = SSH_ERR_ALLOC_FAIL;
2521 if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) {
2522 ret = SSH_ERR_INVALID_FORMAT;
2525 if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
2527 ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2530 if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
2531 /* XXX assume it is a allocation error */
2532 ret = SSH_ERR_ALLOC_FAIL;
2536 sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
2538 if (type == KEY_ECDSA_SK || type == KEY_ECDSA_SK_CERT) {
2539 /* Parse additional security-key application string */
2540 if (sshbuf_get_cstring(b, &key->sk_application,
2542 ret = SSH_ERR_INVALID_FORMAT;
2546 fprintf(stderr, "App: %s\n", key->sk_application);
2550 # endif /* OPENSSL_HAS_ECC */
2551 #endif /* WITH_OPENSSL */
2552 case KEY_ED25519_CERT:
2553 case KEY_ED25519_SK_CERT:
2555 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2556 ret = SSH_ERR_INVALID_FORMAT;
2561 case KEY_ED25519_SK:
2562 if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
2564 if (len != ED25519_PK_SZ) {
2565 ret = SSH_ERR_INVALID_FORMAT;
2568 if ((key = sshkey_new(type)) == NULL) {
2569 ret = SSH_ERR_ALLOC_FAIL;
2572 if (type == KEY_ED25519_SK || type == KEY_ED25519_SK_CERT) {
2573 /* Parse additional security-key application string */
2574 if (sshbuf_get_cstring(b, &key->sk_application,
2576 ret = SSH_ERR_INVALID_FORMAT;
2580 fprintf(stderr, "App: %s\n", key->sk_application);
2583 key->ed25519_pk = pk;
2589 if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
2590 ret = SSH_ERR_INVALID_FORMAT;
2595 if ((ret = sshbuf_get_cstring(b, &xmss_name, NULL)) != 0)
2597 if ((key = sshkey_new(type)) == NULL) {
2598 ret = SSH_ERR_ALLOC_FAIL;
2601 if ((ret = sshkey_xmss_init(key, xmss_name)) != 0)
2603 if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
2605 if (len == 0 || len != sshkey_xmss_pklen(key)) {
2606 ret = SSH_ERR_INVALID_FORMAT;
2611 if (type != KEY_XMSS_CERT &&
2612 (ret = sshkey_xmss_deserialize_pk_info(key, b)) != 0)
2615 #endif /* WITH_XMSS */
2618 ret = SSH_ERR_KEY_TYPE_UNKNOWN;
2622 /* Parse certificate potion */
2623 if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
2626 if (key != NULL && sshbuf_len(b) != 0) {
2627 ret = SSH_ERR_INVALID_FORMAT;
2642 #if defined(WITH_OPENSSL)
2643 BN_clear_free(rsa_n);
2644 BN_clear_free(rsa_e);
2645 BN_clear_free(dsa_p);
2646 BN_clear_free(dsa_q);
2647 BN_clear_free(dsa_g);
2648 BN_clear_free(dsa_pub_key);
2649 # if defined(OPENSSL_HAS_ECC)
2651 # endif /* OPENSSL_HAS_ECC */
2652 #endif /* WITH_OPENSSL */
2657 sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
2662 if ((b = sshbuf_from(blob, blen)) == NULL)
2663 return SSH_ERR_ALLOC_FAIL;
2664 r = sshkey_from_blob_internal(b, keyp, 1);
2670 sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
2672 return sshkey_from_blob_internal(b, keyp, 1);
2676 sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
2681 if ((r = sshbuf_froms(buf, &b)) != 0)
2683 r = sshkey_from_blob_internal(b, keyp, 1);
2689 sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
2692 struct sshbuf *b = NULL;
2693 char *sigtype = NULL;
2695 if (sigtypep != NULL)
2697 if ((b = sshbuf_from(sig, siglen)) == NULL)
2698 return SSH_ERR_ALLOC_FAIL;
2699 if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
2702 if (sigtypep != NULL) {
2703 *sigtypep = sigtype;
2715 * Checks whether a certificate's signature type is allowed.
2716 * Returns 0 (success) if the certificate signature type appears in the
2717 * "allowed" pattern-list, or the key is not a certificate to begin with.
2718 * Otherwise returns a ssherr.h code.
2721 sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
2723 if (key == NULL || allowed == NULL)
2724 return SSH_ERR_INVALID_ARGUMENT;
2725 if (!sshkey_type_is_cert(key->type))
2727 if (key->cert == NULL || key->cert->signature_type == NULL)
2728 return SSH_ERR_INVALID_ARGUMENT;
2729 if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
2730 return SSH_ERR_SIGN_ALG_UNSUPPORTED;
2735 * Returns the expected signature algorithm for a given public key algorithm.
2738 sshkey_sigalg_by_name(const char *name)
2740 const struct keytype *kt;
2742 for (kt = keytypes; kt->type != -1; kt++) {
2743 if (strcmp(kt->name, name) != 0)
2745 if (kt->sigalg != NULL)
2749 return sshkey_ssh_name_from_type_nid(
2750 sshkey_type_plain(kt->type), kt->nid);
2756 * Verifies that the signature algorithm appearing inside the signature blob
2757 * matches that which was requested.
2760 sshkey_check_sigtype(const u_char *sig, size_t siglen,
2761 const char *requested_alg)
2763 const char *expected_alg;
2764 char *sigtype = NULL;
2767 if (requested_alg == NULL)
2769 if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
2770 return SSH_ERR_INVALID_ARGUMENT;
2771 if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
2773 r = strcmp(expected_alg, sigtype) == 0;
2775 return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
2779 sshkey_sign(struct sshkey *key,
2780 u_char **sigp, size_t *lenp,
2781 const u_char *data, size_t datalen,
2782 const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
2784 int was_shielded = sshkey_is_shielded(key);
2785 int r2, r = SSH_ERR_INTERNAL_ERROR;
2791 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2792 return SSH_ERR_INVALID_ARGUMENT;
2793 if ((r = sshkey_unshield_private(key)) != 0)
2795 switch (key->type) {
2799 r = ssh_dss_sign(key, sigp, lenp, data, datalen, compat);
2801 # ifdef OPENSSL_HAS_ECC
2802 case KEY_ECDSA_CERT:
2804 r = ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat);
2806 # endif /* OPENSSL_HAS_ECC */
2809 r = ssh_rsa_sign(key, sigp, lenp, data, datalen, alg);
2811 #endif /* WITH_OPENSSL */
2813 case KEY_ED25519_CERT:
2814 r = ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat);
2816 case KEY_ED25519_SK:
2817 case KEY_ED25519_SK_CERT:
2818 case KEY_ECDSA_SK_CERT:
2820 r = sshsk_sign(sk_provider, key, sigp, lenp, data,
2821 datalen, compat, sk_pin);
2826 r = ssh_xmss_sign(key, sigp, lenp, data, datalen, compat);
2828 #endif /* WITH_XMSS */
2830 r = SSH_ERR_KEY_TYPE_UNKNOWN;
2833 if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
2839 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2840 * If "alg" specified, then the signature must use that algorithm.
2843 sshkey_verify(const struct sshkey *key,
2844 const u_char *sig, size_t siglen,
2845 const u_char *data, size_t dlen, const char *alg, u_int compat,
2846 struct sshkey_sig_details **detailsp)
2848 if (detailsp != NULL)
2850 if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2851 return SSH_ERR_INVALID_ARGUMENT;
2852 switch (key->type) {
2856 return ssh_dss_verify(key, sig, siglen, data, dlen, compat);
2857 # ifdef OPENSSL_HAS_ECC
2858 case KEY_ECDSA_CERT:
2860 return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat);
2861 case KEY_ECDSA_SK_CERT:
2863 return ssh_ecdsa_sk_verify(key, sig, siglen, data, dlen,
2865 # endif /* OPENSSL_HAS_ECC */
2868 return ssh_rsa_verify(key, sig, siglen, data, dlen, alg);
2869 #endif /* WITH_OPENSSL */
2871 case KEY_ED25519_CERT:
2872 return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
2873 case KEY_ED25519_SK:
2874 case KEY_ED25519_SK_CERT:
2875 return ssh_ed25519_sk_verify(key, sig, siglen, data, dlen,
2880 return ssh_xmss_verify(key, sig, siglen, data, dlen, compat);
2881 #endif /* WITH_XMSS */
2883 return SSH_ERR_KEY_TYPE_UNKNOWN;
2887 /* Convert a plain key to their _CERT equivalent */
2889 sshkey_to_certified(struct sshkey *k)
2896 newtype = KEY_RSA_CERT;
2899 newtype = KEY_DSA_CERT;
2902 newtype = KEY_ECDSA_CERT;
2905 newtype = KEY_ECDSA_SK_CERT;
2907 #endif /* WITH_OPENSSL */
2908 case KEY_ED25519_SK:
2909 newtype = KEY_ED25519_SK_CERT;
2912 newtype = KEY_ED25519_CERT;
2916 newtype = KEY_XMSS_CERT;
2918 #endif /* WITH_XMSS */
2920 return SSH_ERR_INVALID_ARGUMENT;
2922 if ((k->cert = cert_new()) == NULL)
2923 return SSH_ERR_ALLOC_FAIL;
2928 /* Convert a certificate to its raw key equivalent */
2930 sshkey_drop_cert(struct sshkey *k)
2932 if (!sshkey_type_is_cert(k->type))
2933 return SSH_ERR_KEY_TYPE_UNKNOWN;
2936 k->type = sshkey_type_plain(k->type);
2940 /* Sign a certified key, (re-)generating the signed certblob. */
2942 sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
2943 const char *sk_provider, const char *sk_pin,
2944 sshkey_certify_signer *signer, void *signer_ctx)
2946 struct sshbuf *principals = NULL;
2947 u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2948 size_t i, ca_len, sig_len;
2949 int ret = SSH_ERR_INTERNAL_ERROR;
2950 struct sshbuf *cert = NULL;
2951 char *sigtype = NULL;
2953 const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
2954 #endif /* WITH_OPENSSL */
2956 if (k == NULL || k->cert == NULL ||
2957 k->cert->certblob == NULL || ca == NULL)
2958 return SSH_ERR_INVALID_ARGUMENT;
2959 if (!sshkey_is_cert(k))
2960 return SSH_ERR_KEY_TYPE_UNKNOWN;
2961 if (!sshkey_type_is_valid_ca(ca->type))
2962 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2965 * If no alg specified as argument but a signature_type was set,
2966 * then prefer that. If both were specified, then they must match.
2969 alg = k->cert->signature_type;
2970 else if (k->cert->signature_type != NULL &&
2971 strcmp(alg, k->cert->signature_type) != 0)
2972 return SSH_ERR_INVALID_ARGUMENT;
2975 * If no signing algorithm or signature_type was specified and we're
2976 * using a RSA key, then default to a good signature algorithm.
2978 if (alg == NULL && ca->type == KEY_RSA)
2979 alg = "rsa-sha2-512";
2981 if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2982 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2984 cert = k->cert->certblob; /* for readability */
2986 if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2989 /* -v01 certs put nonce first */
2990 arc4random_buf(&nonce, sizeof(nonce));
2991 if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2994 /* XXX this substantially duplicates to_blob(); refactor */
2998 DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g);
2999 DSA_get0_key(k->dsa, &dsa_pub_key, NULL);
3000 if ((ret = sshbuf_put_bignum2(cert, dsa_p)) != 0 ||
3001 (ret = sshbuf_put_bignum2(cert, dsa_q)) != 0 ||
3002 (ret = sshbuf_put_bignum2(cert, dsa_g)) != 0 ||
3003 (ret = sshbuf_put_bignum2(cert, dsa_pub_key)) != 0)
3006 # ifdef OPENSSL_HAS_ECC
3007 case KEY_ECDSA_CERT:
3008 case KEY_ECDSA_SK_CERT:
3009 if ((ret = sshbuf_put_cstring(cert,
3010 sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 ||
3011 (ret = sshbuf_put_ec(cert,
3012 EC_KEY_get0_public_key(k->ecdsa),
3013 EC_KEY_get0_group(k->ecdsa))) != 0)
3015 if (k->type == KEY_ECDSA_SK_CERT) {
3016 if ((ret = sshbuf_put_cstring(cert,
3017 k->sk_application)) != 0)
3021 # endif /* OPENSSL_HAS_ECC */
3023 RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL);
3024 if ((ret = sshbuf_put_bignum2(cert, rsa_e)) != 0 ||
3025 (ret = sshbuf_put_bignum2(cert, rsa_n)) != 0)
3028 #endif /* WITH_OPENSSL */
3029 case KEY_ED25519_CERT:
3030 case KEY_ED25519_SK_CERT:
3031 if ((ret = sshbuf_put_string(cert,
3032 k->ed25519_pk, ED25519_PK_SZ)) != 0)
3034 if (k->type == KEY_ED25519_SK_CERT) {
3035 if ((ret = sshbuf_put_cstring(cert,
3036 k->sk_application)) != 0)
3042 if (k->xmss_name == NULL) {
3043 ret = SSH_ERR_INVALID_ARGUMENT;
3046 if ((ret = sshbuf_put_cstring(cert, k->xmss_name)) ||
3047 (ret = sshbuf_put_string(cert,
3048 k->xmss_pk, sshkey_xmss_pklen(k))) != 0)
3051 #endif /* WITH_XMSS */
3053 ret = SSH_ERR_INVALID_ARGUMENT;
3057 if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
3058 (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
3059 (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
3062 if ((principals = sshbuf_new()) == NULL) {
3063 ret = SSH_ERR_ALLOC_FAIL;
3066 for (i = 0; i < k->cert->nprincipals; i++) {
3067 if ((ret = sshbuf_put_cstring(principals,
3068 k->cert->principals[i])) != 0)
3071 if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
3072 (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
3073 (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
3074 (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
3075 (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
3076 (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
3077 (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
3080 /* Sign the whole mess */
3081 if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
3082 sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
3084 /* Check and update signature_type against what was actually used */
3085 if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
3087 if (alg != NULL && strcmp(alg, sigtype) != 0) {
3088 ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
3091 if (k->cert->signature_type == NULL) {
3092 k->cert->signature_type = sigtype;
3095 /* Append signature and we are done */
3096 if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
3105 sshbuf_free(principals);
3110 default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
3111 const u_char *data, size_t datalen,
3112 const char *alg, const char *sk_provider, const char *sk_pin,
3113 u_int compat, void *ctx)
3116 return SSH_ERR_INVALID_ARGUMENT;
3117 return sshkey_sign(key, sigp, lenp, data, datalen, alg,
3118 sk_provider, sk_pin, compat);
3122 sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
3123 const char *sk_provider, const char *sk_pin)
3125 return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
3126 default_key_sign, NULL);
3130 sshkey_cert_check_authority(const struct sshkey *k,
3131 int want_host, int require_principal, int wildcard_pattern,
3132 uint64_t verify_time, const char *name, const char **reason)
3134 u_int i, principal_matches;
3137 return SSH_ERR_INVALID_ARGUMENT;
3138 if (!sshkey_is_cert(k)) {
3139 *reason = "Key is not a certificate";
3140 return SSH_ERR_KEY_CERT_INVALID;
3143 if (k->cert->type != SSH2_CERT_TYPE_HOST) {
3144 *reason = "Certificate invalid: not a host certificate";
3145 return SSH_ERR_KEY_CERT_INVALID;
3148 if (k->cert->type != SSH2_CERT_TYPE_USER) {
3149 *reason = "Certificate invalid: not a user certificate";
3150 return SSH_ERR_KEY_CERT_INVALID;
3153 if (verify_time < k->cert->valid_after) {
3154 *reason = "Certificate invalid: not yet valid";
3155 return SSH_ERR_KEY_CERT_INVALID;
3157 if (verify_time >= k->cert->valid_before) {
3158 *reason = "Certificate invalid: expired";
3159 return SSH_ERR_KEY_CERT_INVALID;
3161 if (k->cert->nprincipals == 0) {
3162 if (require_principal) {
3163 *reason = "Certificate lacks principal list";
3164 return SSH_ERR_KEY_CERT_INVALID;
3166 } else if (name != NULL) {
3167 principal_matches = 0;
3168 for (i = 0; i < k->cert->nprincipals; i++) {
3169 if (wildcard_pattern) {
3170 if (match_pattern(k->cert->principals[i],
3172 principal_matches = 1;
3175 } else if (strcmp(name, k->cert->principals[i]) == 0) {
3176 principal_matches = 1;
3180 if (!principal_matches) {
3181 *reason = "Certificate invalid: name is not a listed "
3183 return SSH_ERR_KEY_CERT_INVALID;
3190 sshkey_cert_check_authority_now(const struct sshkey *k,
3191 int want_host, int require_principal, int wildcard_pattern,
3192 const char *name, const char **reason)
3196 if ((now = time(NULL)) < 0) {
3197 /* yikes - system clock before epoch! */
3198 *reason = "Certificate invalid: not yet valid";
3199 return SSH_ERR_KEY_CERT_INVALID;
3201 return sshkey_cert_check_authority(k, want_host, require_principal,
3202 wildcard_pattern, (uint64_t)now, name, reason);
3206 sshkey_cert_check_host(const struct sshkey *key, const char *host,
3207 int wildcard_principals, const char *ca_sign_algorithms,
3208 const char **reason)
3212 if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals,
3213 host, reason)) != 0)
3215 if (sshbuf_len(key->cert->critical) != 0) {
3216 *reason = "Certificate contains unsupported critical options";
3217 return SSH_ERR_KEY_CERT_INVALID;
3219 if (ca_sign_algorithms != NULL &&
3220 (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
3221 *reason = "Certificate signed with disallowed algorithm";
3222 return SSH_ERR_KEY_CERT_INVALID;
3228 sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
3230 char from[32], to[32], ret[128];
3233 if (cert->valid_after == 0 &&
3234 cert->valid_before == 0xffffffffffffffffULL)
3235 return strlcpy(s, "forever", l);
3237 if (cert->valid_after != 0)
3238 format_absolute_time(cert->valid_after, from, sizeof(from));
3239 if (cert->valid_before != 0xffffffffffffffffULL)
3240 format_absolute_time(cert->valid_before, to, sizeof(to));
3242 if (cert->valid_after == 0)
3243 snprintf(ret, sizeof(ret), "before %s", to);
3244 else if (cert->valid_before == 0xffffffffffffffffULL)
3245 snprintf(ret, sizeof(ret), "after %s", from);
3247 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
3249 return strlcpy(s, ret, l);
3253 sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
3254 enum sshkey_serialize_rep opts)
3256 int r = SSH_ERR_INTERNAL_ERROR;
3257 int was_shielded = sshkey_is_shielded(key);
3258 struct sshbuf *b = NULL;
3260 const BIGNUM *rsa_n, *rsa_e, *rsa_d, *rsa_iqmp, *rsa_p, *rsa_q;
3261 const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key, *dsa_priv_key;
3262 #endif /* WITH_OPENSSL */
3264 if ((r = sshkey_unshield_private(key)) != 0)
3266 if ((b = sshbuf_new()) == NULL)
3267 return SSH_ERR_ALLOC_FAIL;
3268 if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
3270 switch (key->type) {
3273 RSA_get0_key(key->rsa, &rsa_n, &rsa_e, &rsa_d);
3274 RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
3275 RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp);
3276 if ((r = sshbuf_put_bignum2(b, rsa_n)) != 0 ||
3277 (r = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
3278 (r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||
3279 (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||
3280 (r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||
3281 (r = sshbuf_put_bignum2(b, rsa_q)) != 0)
3285 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3286 r = SSH_ERR_INVALID_ARGUMENT;
3289 RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
3290 RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
3291 RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp);
3292 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3293 (r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||
3294 (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||
3295 (r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||
3296 (r = sshbuf_put_bignum2(b, rsa_q)) != 0)
3300 DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g);
3301 DSA_get0_key(key->dsa, &dsa_pub_key, &dsa_priv_key);
3302 if ((r = sshbuf_put_bignum2(b, dsa_p)) != 0 ||
3303 (r = sshbuf_put_bignum2(b, dsa_q)) != 0 ||
3304 (r = sshbuf_put_bignum2(b, dsa_g)) != 0 ||
3305 (r = sshbuf_put_bignum2(b, dsa_pub_key)) != 0 ||
3306 (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0)
3310 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3311 r = SSH_ERR_INVALID_ARGUMENT;
3314 DSA_get0_key(key->dsa, NULL, &dsa_priv_key);
3315 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3316 (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0)
3319 # ifdef OPENSSL_HAS_ECC
3321 if ((r = sshbuf_put_cstring(b,
3322 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
3323 (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
3324 (r = sshbuf_put_bignum2(b,
3325 EC_KEY_get0_private_key(key->ecdsa))) != 0)
3328 case KEY_ECDSA_CERT:
3329 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3330 r = SSH_ERR_INVALID_ARGUMENT;
3333 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3334 (r = sshbuf_put_bignum2(b,
3335 EC_KEY_get0_private_key(key->ecdsa))) != 0)
3339 if ((r = sshbuf_put_cstring(b,
3340 sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
3341 (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
3342 (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
3343 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
3344 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
3345 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
3348 case KEY_ECDSA_SK_CERT:
3349 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3350 r = SSH_ERR_INVALID_ARGUMENT;
3353 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3354 (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
3355 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
3356 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
3357 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
3360 # endif /* OPENSSL_HAS_ECC */
3361 #endif /* WITH_OPENSSL */
3363 if ((r = sshbuf_put_string(b, key->ed25519_pk,
3364 ED25519_PK_SZ)) != 0 ||
3365 (r = sshbuf_put_string(b, key->ed25519_sk,
3366 ED25519_SK_SZ)) != 0)
3369 case KEY_ED25519_CERT:
3370 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3371 r = SSH_ERR_INVALID_ARGUMENT;
3374 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3375 (r = sshbuf_put_string(b, key->ed25519_pk,
3376 ED25519_PK_SZ)) != 0 ||
3377 (r = sshbuf_put_string(b, key->ed25519_sk,
3378 ED25519_SK_SZ)) != 0)
3381 case KEY_ED25519_SK:
3382 if ((r = sshbuf_put_string(b, key->ed25519_pk,
3383 ED25519_PK_SZ)) != 0 ||
3384 (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
3385 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
3386 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
3387 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
3390 case KEY_ED25519_SK_CERT:
3391 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
3392 r = SSH_ERR_INVALID_ARGUMENT;
3395 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3396 (r = sshbuf_put_string(b, key->ed25519_pk,
3397 ED25519_PK_SZ)) != 0 ||
3398 (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
3399 (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
3400 (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
3401 (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
3406 if (key->xmss_name == NULL) {
3407 r = SSH_ERR_INVALID_ARGUMENT;
3410 if ((r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
3411 (r = sshbuf_put_string(b, key->xmss_pk,
3412 sshkey_xmss_pklen(key))) != 0 ||
3413 (r = sshbuf_put_string(b, key->xmss_sk,
3414 sshkey_xmss_sklen(key))) != 0 ||
3415 (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0)
3419 if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0 ||
3420 key->xmss_name == NULL) {
3421 r = SSH_ERR_INVALID_ARGUMENT;
3424 if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
3425 (r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
3426 (r = sshbuf_put_string(b, key->xmss_pk,
3427 sshkey_xmss_pklen(key))) != 0 ||
3428 (r = sshbuf_put_string(b, key->xmss_sk,
3429 sshkey_xmss_sklen(key))) != 0 ||
3430 (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0)
3433 #endif /* WITH_XMSS */
3435 r = SSH_ERR_INVALID_ARGUMENT;
3439 * success (but we still need to append the output to buf after
3440 * possibly re-shielding the private key)
3445 r = sshkey_shield_private(key);
3447 r = sshbuf_putb(buf, b);
3454 sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
3456 return sshkey_private_serialize_opt(key, b,
3457 SSHKEY_SERIALIZE_DEFAULT);
3461 sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
3463 char *tname = NULL, *curve = NULL, *xmss_name = NULL;
3464 char *expect_sk_application = NULL;
3465 struct sshkey *k = NULL;
3466 size_t pklen = 0, sklen = 0;
3467 int type, r = SSH_ERR_INTERNAL_ERROR;
3468 u_char *ed25519_pk = NULL, *ed25519_sk = NULL;
3469 u_char *expect_ed25519_pk = NULL;
3470 u_char *xmss_pk = NULL, *xmss_sk = NULL;
3472 BIGNUM *exponent = NULL;
3473 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
3474 BIGNUM *rsa_iqmp = NULL, *rsa_p = NULL, *rsa_q = NULL;
3475 BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
3476 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
3477 #endif /* WITH_OPENSSL */
3481 if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
3483 type = sshkey_type_from_name(tname);
3484 if (sshkey_type_is_cert(type)) {
3486 * Certificate key private keys begin with the certificate
3487 * itself. Make sure this matches the type of the enclosing
3490 if ((r = sshkey_froms(buf, &k)) != 0)
3492 if (k->type != type) {
3493 r = SSH_ERR_KEY_CERT_MISMATCH;
3496 /* For ECDSA keys, the group must match too */
3497 if (k->type == KEY_ECDSA &&
3498 k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
3499 r = SSH_ERR_KEY_CERT_MISMATCH;
3503 * Several fields are redundant between certificate and
3504 * private key body, we require these to match.
3506 expect_sk_application = k->sk_application;
3507 expect_ed25519_pk = k->ed25519_pk;
3508 k->sk_application = NULL;
3509 k->ed25519_pk = NULL;
3511 if ((k = sshkey_new(type)) == NULL) {
3512 r = SSH_ERR_ALLOC_FAIL;
3519 if ((r = sshbuf_get_bignum2(buf, &dsa_p)) != 0 ||
3520 (r = sshbuf_get_bignum2(buf, &dsa_q)) != 0 ||
3521 (r = sshbuf_get_bignum2(buf, &dsa_g)) != 0 ||
3522 (r = sshbuf_get_bignum2(buf, &dsa_pub_key)) != 0)
3524 if (!DSA_set0_pqg(k->dsa, dsa_p, dsa_q, dsa_g)) {
3525 r = SSH_ERR_LIBCRYPTO_ERROR;
3528 dsa_p = dsa_q = dsa_g = NULL; /* transferred */
3529 if (!DSA_set0_key(k->dsa, dsa_pub_key, NULL)) {
3530 r = SSH_ERR_LIBCRYPTO_ERROR;
3533 dsa_pub_key = NULL; /* transferred */
3536 if ((r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0)
3538 if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) {
3539 r = SSH_ERR_LIBCRYPTO_ERROR;
3542 dsa_priv_key = NULL; /* transferred */
3544 # ifdef OPENSSL_HAS_ECC
3546 if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
3547 r = SSH_ERR_INVALID_ARGUMENT;
3550 if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
3552 if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
3553 r = SSH_ERR_EC_CURVE_MISMATCH;
3556 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
3557 if (k->ecdsa == NULL) {
3558 r = SSH_ERR_LIBCRYPTO_ERROR;
3561 if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0)
3564 case KEY_ECDSA_CERT:
3565 if ((r = sshbuf_get_bignum2(buf, &exponent)) != 0)
3567 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
3568 r = SSH_ERR_LIBCRYPTO_ERROR;
3571 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
3572 EC_KEY_get0_public_key(k->ecdsa))) != 0 ||
3573 (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
3577 if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
3578 r = SSH_ERR_INVALID_ARGUMENT;
3581 if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
3583 if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
3584 r = SSH_ERR_EC_CURVE_MISMATCH;
3587 if ((k->sk_key_handle = sshbuf_new()) == NULL ||
3588 (k->sk_reserved = sshbuf_new()) == NULL) {
3589 r = SSH_ERR_ALLOC_FAIL;
3592 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
3593 if (k->ecdsa == NULL) {
3594 r = SSH_ERR_LIBCRYPTO_ERROR;
3597 if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 ||
3598 (r = sshbuf_get_cstring(buf, &k->sk_application,
3600 (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
3601 (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
3602 (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
3604 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
3605 EC_KEY_get0_public_key(k->ecdsa))) != 0)
3608 case KEY_ECDSA_SK_CERT:
3609 if ((k->sk_key_handle = sshbuf_new()) == NULL ||
3610 (k->sk_reserved = sshbuf_new()) == NULL) {
3611 r = SSH_ERR_ALLOC_FAIL;
3614 if ((r = sshbuf_get_cstring(buf, &k->sk_application,
3616 (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
3617 (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
3618 (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
3620 if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
3621 EC_KEY_get0_public_key(k->ecdsa))) != 0)
3624 # endif /* OPENSSL_HAS_ECC */
3626 if ((r = sshbuf_get_bignum2(buf, &rsa_n)) != 0 ||
3627 (r = sshbuf_get_bignum2(buf, &rsa_e)) != 0)
3629 if (!RSA_set0_key(k->rsa, rsa_n, rsa_e, NULL)) {
3630 r = SSH_ERR_LIBCRYPTO_ERROR;
3633 rsa_n = rsa_e = NULL; /* transferred */
3636 if ((r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 ||
3637 (r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 ||
3638 (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 ||
3639 (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0)
3641 if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) {
3642 r = SSH_ERR_LIBCRYPTO_ERROR;
3645 rsa_d = NULL; /* transferred */
3646 if (!RSA_set0_factors(k->rsa, rsa_p, rsa_q)) {
3647 r = SSH_ERR_LIBCRYPTO_ERROR;
3650 rsa_p = rsa_q = NULL; /* transferred */
3651 if ((r = sshkey_check_rsa_length(k, 0)) != 0)
3653 if ((r = ssh_rsa_complete_crt_parameters(k, rsa_iqmp)) != 0)
3656 #endif /* WITH_OPENSSL */
3658 case KEY_ED25519_CERT:
3659 if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
3660 (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
3662 if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
3663 r = SSH_ERR_INVALID_FORMAT;
3666 k->ed25519_pk = ed25519_pk;
3667 k->ed25519_sk = ed25519_sk;
3668 ed25519_pk = ed25519_sk = NULL; /* transferred */
3670 case KEY_ED25519_SK:
3671 case KEY_ED25519_SK_CERT:
3672 if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0)
3674 if (pklen != ED25519_PK_SZ) {
3675 r = SSH_ERR_INVALID_FORMAT;
3678 if ((k->sk_key_handle = sshbuf_new()) == NULL ||
3679 (k->sk_reserved = sshbuf_new()) == NULL) {
3680 r = SSH_ERR_ALLOC_FAIL;
3683 if ((r = sshbuf_get_cstring(buf, &k->sk_application,
3685 (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
3686 (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
3687 (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
3689 k->ed25519_pk = ed25519_pk;
3690 ed25519_pk = NULL; /* transferred */
3695 if ((r = sshbuf_get_cstring(buf, &xmss_name, NULL)) != 0 ||
3696 (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 ||
3697 (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0)
3699 if (type == KEY_XMSS &&
3700 (r = sshkey_xmss_init(k, xmss_name)) != 0)
3702 if (pklen != sshkey_xmss_pklen(k) ||
3703 sklen != sshkey_xmss_sklen(k)) {
3704 r = SSH_ERR_INVALID_FORMAT;
3707 k->xmss_pk = xmss_pk;
3708 k->xmss_sk = xmss_sk;
3709 xmss_pk = xmss_sk = NULL;
3710 /* optional internal state */
3711 if ((r = sshkey_xmss_deserialize_state_opt(k, buf)) != 0)
3714 #endif /* WITH_XMSS */
3716 r = SSH_ERR_KEY_TYPE_UNKNOWN;
3720 /* enable blinding */
3724 if (RSA_blinding_on(k->rsa, NULL) != 1) {
3725 r = SSH_ERR_LIBCRYPTO_ERROR;
3730 #endif /* WITH_OPENSSL */
3731 if ((expect_sk_application != NULL && (k->sk_application == NULL ||
3732 strcmp(expect_sk_application, k->sk_application) != 0)) ||
3733 (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
3734 memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
3735 r = SSH_ERR_KEY_CERT_MISMATCH;
3748 BN_clear_free(exponent);
3749 BN_clear_free(dsa_p);
3750 BN_clear_free(dsa_q);
3751 BN_clear_free(dsa_g);
3752 BN_clear_free(dsa_pub_key);
3753 BN_clear_free(dsa_priv_key);
3754 BN_clear_free(rsa_n);
3755 BN_clear_free(rsa_e);
3756 BN_clear_free(rsa_d);
3757 BN_clear_free(rsa_p);
3758 BN_clear_free(rsa_q);
3759 BN_clear_free(rsa_iqmp);
3760 #endif /* WITH_OPENSSL */
3762 freezero(ed25519_pk, pklen);
3763 freezero(ed25519_sk, sklen);
3765 freezero(xmss_pk, pklen);
3766 freezero(xmss_sk, sklen);
3767 free(expect_sk_application);
3768 free(expect_ed25519_pk);
3772 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
3774 sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
3776 EC_POINT *nq = NULL;
3777 BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
3778 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
3781 * NB. This assumes OpenSSL has already verified that the public
3782 * point lies on the curve. This is done by EC_POINT_oct2point()
3783 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
3784 * reachable with public points not unmarshalled using
3785 * EC_POINT_oct2point then the caller will need to explicitly check.
3789 * We shouldn't ever hit this case because bignum_get_ecpoint()
3790 * refuses to load GF2m points.
3792 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
3793 NID_X9_62_prime_field)
3797 if (EC_POINT_is_at_infinity(group, public))
3800 if ((x = BN_new()) == NULL ||
3801 (y = BN_new()) == NULL ||
3802 (order = BN_new()) == NULL ||
3803 (tmp = BN_new()) == NULL) {
3804 ret = SSH_ERR_ALLOC_FAIL;
3808 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
3809 if (EC_GROUP_get_order(group, order, NULL) != 1 ||
3810 EC_POINT_get_affine_coordinates_GFp(group, public,
3812 ret = SSH_ERR_LIBCRYPTO_ERROR;
3815 if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
3816 BN_num_bits(y) <= BN_num_bits(order) / 2)
3819 /* nQ == infinity (n == order of subgroup) */
3820 if ((nq = EC_POINT_new(group)) == NULL) {
3821 ret = SSH_ERR_ALLOC_FAIL;
3824 if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
3825 ret = SSH_ERR_LIBCRYPTO_ERROR;
3828 if (EC_POINT_is_at_infinity(group, nq) != 1)
3831 /* x < order - 1, y < order - 1 */
3832 if (!BN_sub(tmp, order, BN_value_one())) {
3833 ret = SSH_ERR_LIBCRYPTO_ERROR;
3836 if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
3842 BN_clear_free(order);
3849 sshkey_ec_validate_private(const EC_KEY *key)
3851 BIGNUM *order = NULL, *tmp = NULL;
3852 int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
3854 if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
3855 ret = SSH_ERR_ALLOC_FAIL;
3859 /* log2(private) > log2(order)/2 */
3860 if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
3861 ret = SSH_ERR_LIBCRYPTO_ERROR;
3864 if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
3865 BN_num_bits(order) / 2)
3868 /* private < order - 1 */
3869 if (!BN_sub(tmp, order, BN_value_one())) {
3870 ret = SSH_ERR_LIBCRYPTO_ERROR;
3873 if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
3877 BN_clear_free(order);
3883 sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
3885 BIGNUM *x = NULL, *y = NULL;
3887 if (point == NULL) {
3888 fputs("point=(NULL)\n", stderr);
3891 if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
3892 fprintf(stderr, "%s: BN_new failed\n", __func__);
3895 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
3896 NID_X9_62_prime_field) {
3897 fprintf(stderr, "%s: group is not a prime field\n", __func__);
3900 if (EC_POINT_get_affine_coordinates_GFp(group, point,
3902 fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
3906 fputs("x=", stderr);
3907 BN_print_fp(stderr, x);
3908 fputs("\ny=", stderr);
3909 BN_print_fp(stderr, y);
3910 fputs("\n", stderr);
3917 sshkey_dump_ec_key(const EC_KEY *key)
3919 const BIGNUM *exponent;
3921 sshkey_dump_ec_point(EC_KEY_get0_group(key),
3922 EC_KEY_get0_public_key(key));
3923 fputs("exponent=", stderr);
3924 if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
3925 fputs("(NULL)", stderr);
3927 BN_print_fp(stderr, EC_KEY_get0_private_key(key));
3928 fputs("\n", stderr);
3930 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
3933 sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
3934 const char *passphrase, const char *comment, const char *ciphername,
3937 u_char *cp, *key = NULL, *pubkeyblob = NULL;
3938 u_char salt[SALT_LEN];
3940 size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
3942 int r = SSH_ERR_INTERNAL_ERROR;
3943 struct sshcipher_ctx *ciphercontext = NULL;
3944 const struct sshcipher *cipher;
3945 const char *kdfname = KDFNAME;
3946 struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
3949 rounds = DEFAULT_ROUNDS;
3950 if (passphrase == NULL || !strlen(passphrase)) {
3951 ciphername = "none";
3953 } else if (ciphername == NULL)
3954 ciphername = DEFAULT_CIPHERNAME;
3955 if ((cipher = cipher_by_name(ciphername)) == NULL) {
3956 r = SSH_ERR_INVALID_ARGUMENT;
3960 if ((kdf = sshbuf_new()) == NULL ||
3961 (encoded = sshbuf_new()) == NULL ||
3962 (encrypted = sshbuf_new()) == NULL) {
3963 r = SSH_ERR_ALLOC_FAIL;
3966 blocksize = cipher_blocksize(cipher);
3967 keylen = cipher_keylen(cipher);
3968 ivlen = cipher_ivlen(cipher);
3969 authlen = cipher_authlen(cipher);
3970 if ((key = calloc(1, keylen + ivlen)) == NULL) {
3971 r = SSH_ERR_ALLOC_FAIL;
3974 if (strcmp(kdfname, "bcrypt") == 0) {
3975 arc4random_buf(salt, SALT_LEN);
3976 if (bcrypt_pbkdf(passphrase, strlen(passphrase),
3977 salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
3978 r = SSH_ERR_INVALID_ARGUMENT;
3981 if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
3982 (r = sshbuf_put_u32(kdf, rounds)) != 0)
3984 } else if (strcmp(kdfname, "none") != 0) {
3985 /* Unsupported KDF type */
3986 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
3989 if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
3990 key + keylen, ivlen, 1)) != 0)
3993 if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
3994 (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
3995 (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
3996 (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
3997 (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
3998 (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
3999 (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
4002 /* set up the buffer that will be encrypted */
4004 /* Random check bytes */
4005 check = arc4random();
4006 if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
4007 (r = sshbuf_put_u32(encrypted, check)) != 0)
4010 /* append private key and comment*/
4011 if ((r = sshkey_private_serialize_opt(prv, encrypted,
4012 SSHKEY_SERIALIZE_FULL)) != 0 ||
4013 (r = sshbuf_put_cstring(encrypted, comment)) != 0)
4018 while (sshbuf_len(encrypted) % blocksize) {
4019 if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
4023 /* length in destination buffer */
4024 if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
4028 if ((r = sshbuf_reserve(encoded,
4029 sshbuf_len(encrypted) + authlen, &cp)) != 0)
4031 if ((r = cipher_crypt(ciphercontext, 0, cp,
4032 sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
4037 /* assemble uuencoded key */
4038 if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
4039 (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
4040 (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
4048 sshbuf_free(encoded);
4049 sshbuf_free(encrypted);
4050 cipher_free(ciphercontext);
4051 explicit_bzero(salt, sizeof(salt));
4053 freezero(key, keylen + ivlen);
4054 if (pubkeyblob != NULL)
4055 freezero(pubkeyblob, pubkeylen);
4057 freezero(b64, strlen(b64));
4062 private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
4068 struct sshbuf *encoded = NULL, *decoded = NULL;
4070 if (blob == NULL || decodedp == NULL)
4071 return SSH_ERR_INVALID_ARGUMENT;
4075 if ((encoded = sshbuf_new()) == NULL ||
4076 (decoded = sshbuf_new()) == NULL) {
4077 r = SSH_ERR_ALLOC_FAIL;
4081 /* check preamble */
4082 cp = sshbuf_ptr(blob);
4083 encoded_len = sshbuf_len(blob);
4084 if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
4085 memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
4086 r = SSH_ERR_INVALID_FORMAT;
4089 cp += MARK_BEGIN_LEN;
4090 encoded_len -= MARK_BEGIN_LEN;
4092 /* Look for end marker, removing whitespace as we go */
4093 while (encoded_len > 0) {
4094 if (*cp != '\n' && *cp != '\r') {
4095 if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
4102 if (encoded_len >= MARK_END_LEN &&
4103 memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
4105 if ((r = sshbuf_put_u8(encoded, 0)) != 0)
4111 if (encoded_len == 0) {
4112 r = SSH_ERR_INVALID_FORMAT;
4117 if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
4121 if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
4122 memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
4123 r = SSH_ERR_INVALID_FORMAT;
4127 *decodedp = decoded;
4131 sshbuf_free(encoded);
4132 sshbuf_free(decoded);
4137 private2_decrypt(struct sshbuf *decoded, const char *passphrase,
4138 struct sshbuf **decryptedp, struct sshkey **pubkeyp)
4140 char *ciphername = NULL, *kdfname = NULL;
4141 const struct sshcipher *cipher = NULL;
4142 int r = SSH_ERR_INTERNAL_ERROR;
4143 size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
4144 struct sshbuf *kdf = NULL, *decrypted = NULL;
4145 struct sshcipher_ctx *ciphercontext = NULL;
4146 struct sshkey *pubkey = NULL;
4147 u_char *key = NULL, *salt = NULL, *dp;
4148 u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
4150 if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
4151 return SSH_ERR_INVALID_ARGUMENT;
4156 if ((decrypted = sshbuf_new()) == NULL) {
4157 r = SSH_ERR_ALLOC_FAIL;
4161 /* parse public portion of key */
4162 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
4163 (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
4164 (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
4165 (r = sshbuf_froms(decoded, &kdf)) != 0 ||
4166 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
4170 /* XXX only one key supported at present */
4171 r = SSH_ERR_INVALID_FORMAT;
4175 if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
4176 (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
4179 if ((cipher = cipher_by_name(ciphername)) == NULL) {
4180 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
4183 if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
4184 r = SSH_ERR_KEY_UNKNOWN_CIPHER;
4187 if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
4188 r = SSH_ERR_INVALID_FORMAT;
4191 if ((passphrase == NULL || strlen(passphrase) == 0) &&
4192 strcmp(kdfname, "none") != 0) {
4193 /* passphrase required */
4194 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
4198 /* check size of encrypted key blob */
4199 blocksize = cipher_blocksize(cipher);
4200 if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
4201 r = SSH_ERR_INVALID_FORMAT;
4206 keylen = cipher_keylen(cipher);
4207 ivlen = cipher_ivlen(cipher);
4208 authlen = cipher_authlen(cipher);
4209 if ((key = calloc(1, keylen + ivlen)) == NULL) {
4210 r = SSH_ERR_ALLOC_FAIL;
4213 if (strcmp(kdfname, "bcrypt") == 0) {
4214 if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
4215 (r = sshbuf_get_u32(kdf, &rounds)) != 0)
4217 if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
4218 key, keylen + ivlen, rounds) < 0) {
4219 r = SSH_ERR_INVALID_FORMAT;
4224 /* check that an appropriate amount of auth data is present */
4225 if (sshbuf_len(decoded) < authlen ||
4226 sshbuf_len(decoded) - authlen < encrypted_len) {
4227 r = SSH_ERR_INVALID_FORMAT;
4231 /* decrypt private portion of key */
4232 if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
4233 (r = cipher_init(&ciphercontext, cipher, key, keylen,
4234 key + keylen, ivlen, 0)) != 0)
4236 if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
4237 encrypted_len, 0, authlen)) != 0) {
4238 /* an integrity error here indicates an incorrect passphrase */
4239 if (r == SSH_ERR_MAC_INVALID)
4240 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
4243 if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
4245 /* there should be no trailing data */
4246 if (sshbuf_len(decoded) != 0) {
4247 r = SSH_ERR_INVALID_FORMAT;
4251 /* check check bytes */
4252 if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
4253 (r = sshbuf_get_u32(decrypted, &check2)) != 0)
4255 if (check1 != check2) {
4256 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
4260 *decryptedp = decrypted;
4266 cipher_free(ciphercontext);
4269 sshkey_free(pubkey);
4271 explicit_bzero(salt, slen);
4275 explicit_bzero(key, keylen + ivlen);
4279 sshbuf_free(decrypted);
4284 sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
4285 struct sshkey **keyp, char **commentp)
4287 char *comment = NULL;
4288 int r = SSH_ERR_INTERNAL_ERROR;
4289 struct sshbuf *decoded = NULL, *decrypted = NULL;
4290 struct sshkey *k = NULL, *pubkey = NULL;
4294 if (commentp != NULL)
4297 /* Undo base64 encoding and decrypt the private section */
4298 if ((r = private2_uudecode(blob, &decoded)) != 0 ||
4299 (r = private2_decrypt(decoded, passphrase,
4300 &decrypted, &pubkey)) != 0)
4303 if (type != KEY_UNSPEC &&
4304 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
4305 r = SSH_ERR_KEY_TYPE_MISMATCH;
4309 /* Load the private key and comment */
4310 if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
4311 (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
4314 /* Check deterministic padding after private section */
4315 if ((r = private2_check_padding(decrypted)) != 0)
4318 /* Check that the public key in the envelope matches the private key */
4319 if (!sshkey_equal(pubkey, k)) {
4320 r = SSH_ERR_INVALID_FORMAT;
4330 if (commentp != NULL) {
4331 *commentp = comment;
4336 sshbuf_free(decoded);
4337 sshbuf_free(decrypted);
4339 sshkey_free(pubkey);
4344 sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
4345 struct sshkey **keyp)
4347 int r = SSH_ERR_INTERNAL_ERROR;
4348 struct sshbuf *decoded = NULL;
4349 struct sshkey *pubkey = NULL;
4355 if ((r = private2_uudecode(blob, &decoded)) != 0)
4357 /* parse public key from unencrypted envelope */
4358 if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
4359 (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
4360 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
4361 (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
4362 (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
4366 /* XXX only one key supported at present */
4367 r = SSH_ERR_INVALID_FORMAT;
4371 /* Parse the public key */
4372 if ((r = sshkey_froms(decoded, &pubkey)) != 0)
4375 if (type != KEY_UNSPEC &&
4376 sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
4377 r = SSH_ERR_KEY_TYPE_MISMATCH;
4388 sshbuf_free(decoded);
4389 sshkey_free(pubkey);
4394 /* convert SSH v2 key to PEM or PKCS#8 format */
4396 sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
4397 int format, const char *_passphrase, const char *comment)
4399 int was_shielded = sshkey_is_shielded(key);
4401 int blen, len = strlen(_passphrase);
4402 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
4403 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
4406 struct sshbuf *blob;
4407 EVP_PKEY *pkey = NULL;
4409 if (len > 0 && len <= 4)
4410 return SSH_ERR_PASSPHRASE_TOO_SHORT;
4411 if ((blob = sshbuf_new()) == NULL)
4412 return SSH_ERR_ALLOC_FAIL;
4413 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
4414 r = SSH_ERR_ALLOC_FAIL;
4417 if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
4418 r = SSH_ERR_ALLOC_FAIL;
4421 if ((r = sshkey_unshield_private(key)) != 0)
4424 switch (key->type) {
4426 if (format == SSHKEY_PRIVATE_PEM) {
4427 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
4428 cipher, passphrase, len, NULL, NULL);
4430 success = EVP_PKEY_set1_DSA(pkey, key->dsa);
4433 #ifdef OPENSSL_HAS_ECC
4435 if (format == SSHKEY_PRIVATE_PEM) {
4436 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
4437 cipher, passphrase, len, NULL, NULL);
4439 success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
4444 if (format == SSHKEY_PRIVATE_PEM) {
4445 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
4446 cipher, passphrase, len, NULL, NULL);
4448 success = EVP_PKEY_set1_RSA(pkey, key->rsa);
4456 r = SSH_ERR_LIBCRYPTO_ERROR;
4459 if (format == SSHKEY_PRIVATE_PKCS8) {
4460 if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
4461 passphrase, len, NULL, NULL)) == 0) {
4462 r = SSH_ERR_LIBCRYPTO_ERROR;
4466 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
4467 r = SSH_ERR_INTERNAL_ERROR;
4470 if ((r = sshbuf_put(blob, bptr, blen)) != 0)
4475 r = sshkey_shield_private(key);
4477 r = sshbuf_putb(buf, blob);
4479 EVP_PKEY_free(pkey);
4484 #endif /* WITH_OPENSSL */
4486 /* Serialise "key" to buffer "blob" */
4488 sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
4489 const char *passphrase, const char *comment,
4490 int format, const char *openssh_format_cipher, int openssh_format_rounds)
4492 switch (key->type) {
4497 break; /* see below */
4498 #endif /* WITH_OPENSSL */
4500 case KEY_ED25519_SK:
4503 #endif /* WITH_XMSS */
4506 #endif /* WITH_OPENSSL */
4507 return sshkey_private_to_blob2(key, blob, passphrase,
4508 comment, openssh_format_cipher, openssh_format_rounds);
4510 return SSH_ERR_KEY_TYPE_UNKNOWN;
4515 case SSHKEY_PRIVATE_OPENSSH:
4516 return sshkey_private_to_blob2(key, blob, passphrase,
4517 comment, openssh_format_cipher, openssh_format_rounds);
4518 case SSHKEY_PRIVATE_PEM:
4519 case SSHKEY_PRIVATE_PKCS8:
4520 return sshkey_private_to_blob_pem_pkcs8(key, blob,
4521 format, passphrase, comment);
4523 return SSH_ERR_INVALID_ARGUMENT;
4525 #endif /* WITH_OPENSSL */
4530 translate_libcrypto_error(unsigned long pem_err)
4532 int pem_reason = ERR_GET_REASON(pem_err);
4534 switch (ERR_GET_LIB(pem_err)) {
4536 switch (pem_reason) {
4537 case PEM_R_BAD_PASSWORD_READ:
4538 case PEM_R_PROBLEMS_GETTING_PASSWORD:
4539 case PEM_R_BAD_DECRYPT:
4540 return SSH_ERR_KEY_WRONG_PASSPHRASE;
4542 return SSH_ERR_INVALID_FORMAT;
4545 switch (pem_reason) {
4546 case EVP_R_BAD_DECRYPT:
4547 return SSH_ERR_KEY_WRONG_PASSPHRASE;
4548 #ifdef EVP_R_BN_DECODE_ERROR
4549 case EVP_R_BN_DECODE_ERROR:
4551 case EVP_R_DECODE_ERROR:
4552 #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
4553 case EVP_R_PRIVATE_KEY_DECODE_ERROR:
4555 return SSH_ERR_INVALID_FORMAT;
4557 return SSH_ERR_LIBCRYPTO_ERROR;
4560 return SSH_ERR_INVALID_FORMAT;
4562 return SSH_ERR_LIBCRYPTO_ERROR;
4566 clear_libcrypto_errors(void)
4568 while (ERR_get_error() != 0)
4573 * Translate OpenSSL error codes to determine whether
4574 * passphrase is required/incorrect.
4577 convert_libcrypto_error(void)
4580 * Some password errors are reported at the beginning
4581 * of the error queue.
4583 if (translate_libcrypto_error(ERR_peek_error()) ==
4584 SSH_ERR_KEY_WRONG_PASSPHRASE)
4585 return SSH_ERR_KEY_WRONG_PASSPHRASE;
4586 return translate_libcrypto_error(ERR_peek_last_error());
4590 pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
4592 char *p = (char *)u;
4595 if (p == NULL || (len = strlen(p)) == 0)
4597 if (size < 0 || len > (size_t)size)
4599 memcpy(buf, p, len);
4604 sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
4605 const char *passphrase, struct sshkey **keyp)
4607 EVP_PKEY *pk = NULL;
4608 struct sshkey *prv = NULL;
4615 if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
4616 return SSH_ERR_ALLOC_FAIL;
4617 if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
4618 (int)sshbuf_len(blob)) {
4619 r = SSH_ERR_ALLOC_FAIL;
4623 clear_libcrypto_errors();
4624 if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
4625 (char *)passphrase)) == NULL) {
4627 * libcrypto may return various ASN.1 errors when attempting
4628 * to parse a key with an incorrect passphrase.
4629 * Treat all format errors as "incorrect passphrase" if a
4630 * passphrase was supplied.
4632 if (passphrase != NULL && *passphrase != '\0')
4633 r = SSH_ERR_KEY_WRONG_PASSPHRASE;
4635 r = convert_libcrypto_error();
4638 if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
4639 (type == KEY_UNSPEC || type == KEY_RSA)) {
4640 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
4641 r = SSH_ERR_ALLOC_FAIL;
4644 prv->rsa = EVP_PKEY_get1_RSA(pk);
4645 prv->type = KEY_RSA;
4647 RSA_print_fp(stderr, prv->rsa, 8);
4649 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
4650 r = SSH_ERR_LIBCRYPTO_ERROR;
4653 if ((r = sshkey_check_rsa_length(prv, 0)) != 0)
4655 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
4656 (type == KEY_UNSPEC || type == KEY_DSA)) {
4657 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
4658 r = SSH_ERR_ALLOC_FAIL;
4661 prv->dsa = EVP_PKEY_get1_DSA(pk);
4662 prv->type = KEY_DSA;
4664 DSA_print_fp(stderr, prv->dsa, 8);
4666 #ifdef OPENSSL_HAS_ECC
4667 } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
4668 (type == KEY_UNSPEC || type == KEY_ECDSA)) {
4669 if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
4670 r = SSH_ERR_ALLOC_FAIL;
4673 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
4674 prv->type = KEY_ECDSA;
4675 prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
4676 if (prv->ecdsa_nid == -1 ||
4677 sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
4678 sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
4679 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
4680 sshkey_ec_validate_private(prv->ecdsa) != 0) {
4681 r = SSH_ERR_INVALID_FORMAT;
4685 if (prv != NULL && prv->ecdsa != NULL)
4686 sshkey_dump_ec_key(prv->ecdsa);
4688 #endif /* OPENSSL_HAS_ECC */
4690 r = SSH_ERR_INVALID_FORMAT;
4704 #endif /* WITH_OPENSSL */
4707 sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
4708 const char *passphrase, struct sshkey **keyp, char **commentp)
4710 int r = SSH_ERR_INTERNAL_ERROR;
4714 if (commentp != NULL)
4720 /* No fallback for new-format-only keys */
4721 return sshkey_parse_private2(blob, type, passphrase,
4724 r = sshkey_parse_private2(blob, type, passphrase, keyp,
4726 /* Only fallback to PEM parser if a format error occurred. */
4727 if (r != SSH_ERR_INVALID_FORMAT)
4730 return sshkey_parse_private_pem_fileblob(blob, type,
4733 return SSH_ERR_INVALID_FORMAT;
4734 #endif /* WITH_OPENSSL */
4739 sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
4740 struct sshkey **keyp, char **commentp)
4744 if (commentp != NULL)
4747 return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
4748 passphrase, keyp, commentp);
4752 sshkey_sig_details_free(struct sshkey_sig_details *details)
4754 freezero(details, sizeof(*details));
4758 sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
4759 struct sshkey **pubkeyp)
4761 int r = SSH_ERR_INTERNAL_ERROR;
4763 if (pubkeyp != NULL)
4765 /* only new-format private keys bundle a public key inside */
4766 if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
4773 * serialize the key with the current state and forward the state
4777 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
4778 u_int32_t maxsign, int printerror)
4783 sshkey_type_plain(k->type) != KEY_XMSS)
4784 return sshkey_private_serialize_opt(k, b,
4785 SSHKEY_SERIALIZE_DEFAULT);
4786 if ((r = sshkey_xmss_get_state(k, printerror)) != 0 ||
4787 (r = sshkey_private_serialize_opt(k, b,
4788 SSHKEY_SERIALIZE_STATE)) != 0 ||
4789 (r = sshkey_xmss_forward_state(k, maxsign)) != 0)
4793 if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) {
4801 sshkey_signatures_left(const struct sshkey *k)
4803 if (sshkey_type_plain(k->type) == KEY_XMSS)
4804 return sshkey_xmss_signatures_left(k);
4809 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
4811 if (sshkey_type_plain(k->type) != KEY_XMSS)
4812 return SSH_ERR_INVALID_ARGUMENT;
4813 return sshkey_xmss_enable_maxsign(k, maxsign);
4817 sshkey_set_filename(struct sshkey *k, const char *filename)
4820 return SSH_ERR_INVALID_ARGUMENT;
4821 if (sshkey_type_plain(k->type) != KEY_XMSS)
4823 if (filename == NULL)
4824 return SSH_ERR_INVALID_ARGUMENT;
4825 if ((k->xmss_filename = strdup(filename)) == NULL)
4826 return SSH_ERR_ALLOC_FAIL;
4831 sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
4832 u_int32_t maxsign, int printerror)
4834 return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
4838 sshkey_signatures_left(const struct sshkey *k)
4844 sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
4846 return SSH_ERR_INVALID_ARGUMENT;
4850 sshkey_set_filename(struct sshkey *k, const char *filename)
4853 return SSH_ERR_INVALID_ARGUMENT;
4856 #endif /* WITH_XMSS */