1 /* $OpenBSD: sshkey.c,v 1.56 2017/08/12 06:42:52 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"
48 #endif /* HAVE_UTIL_H */
56 #define SSHKEY_INTERNAL
60 /* openssh private key file format */
61 #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
62 #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
63 #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
64 #define MARK_END_LEN (sizeof(MARK_END) - 1)
65 #define KDFNAME "bcrypt"
66 #define AUTH_MAGIC "openssh-key-v1"
68 #define DEFAULT_CIPHERNAME "aes256-ctr"
69 #define DEFAULT_ROUNDS 16
71 /* Version identification string for SSH v1 identity files. */
72 #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
74 static int sshkey_from_blob_internal(struct sshbuf
*buf
,
75 struct sshkey
**keyp
, int allow_cert
);
77 /* Supported key types */
80 const char *shortname
;
86 static const struct keytype keytypes
[] = {
87 { "ssh-ed25519", "ED25519", KEY_ED25519
, 0, 0, 0 },
88 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT",
89 KEY_ED25519_CERT
, 0, 1, 0 },
91 { "ssh-rsa", "RSA", KEY_RSA
, 0, 0, 0 },
92 { "rsa-sha2-256", "RSA", KEY_RSA
, 0, 0, 1 },
93 { "rsa-sha2-512", "RSA", KEY_RSA
, 0, 0, 1 },
94 { "ssh-dss", "DSA", KEY_DSA
, 0, 0, 0 },
95 # ifdef OPENSSL_HAS_ECC
96 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA
, NID_X9_62_prime256v1
, 0, 0 },
97 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA
, NID_secp384r1
, 0, 0 },
98 # ifdef OPENSSL_HAS_NISTP521
99 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA
, NID_secp521r1
, 0, 0 },
100 # endif /* OPENSSL_HAS_NISTP521 */
101 # endif /* OPENSSL_HAS_ECC */
102 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT
, 0, 1, 0 },
103 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT
, 0, 1, 0 },
104 # ifdef OPENSSL_HAS_ECC
105 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
106 KEY_ECDSA_CERT
, NID_X9_62_prime256v1
, 1, 0 },
107 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
108 KEY_ECDSA_CERT
, NID_secp384r1
, 1, 0 },
109 # ifdef OPENSSL_HAS_NISTP521
110 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
111 KEY_ECDSA_CERT
, NID_secp521r1
, 1, 0 },
112 # endif /* OPENSSL_HAS_NISTP521 */
113 # endif /* OPENSSL_HAS_ECC */
114 #endif /* WITH_OPENSSL */
115 { NULL
, NULL
, -1, -1, 0, 0 }
119 sshkey_type(const struct sshkey
*k
)
121 const struct keytype
*kt
;
123 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
124 if (kt
->type
== k
->type
)
125 return kt
->shortname
;
131 sshkey_ssh_name_from_type_nid(int type
, int nid
)
133 const struct keytype
*kt
;
135 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
136 if (kt
->type
== type
&& (kt
->nid
== 0 || kt
->nid
== nid
))
139 return "ssh-unknown";
143 sshkey_type_is_cert(int type
)
145 const struct keytype
*kt
;
147 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
148 if (kt
->type
== type
)
155 sshkey_ssh_name(const struct sshkey
*k
)
157 return sshkey_ssh_name_from_type_nid(k
->type
, k
->ecdsa_nid
);
161 sshkey_ssh_name_plain(const struct sshkey
*k
)
163 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k
->type
),
168 sshkey_type_from_name(const char *name
)
170 const struct keytype
*kt
;
172 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
173 /* Only allow shortname matches for plain key types */
174 if ((kt
->name
!= NULL
&& strcmp(name
, kt
->name
) == 0) ||
175 (!kt
->cert
&& strcasecmp(kt
->shortname
, name
) == 0))
182 sshkey_ecdsa_nid_from_name(const char *name
)
184 const struct keytype
*kt
;
186 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
187 if (kt
->type
!= KEY_ECDSA
&& kt
->type
!= KEY_ECDSA_CERT
)
189 if (kt
->name
!= NULL
&& strcmp(name
, kt
->name
) == 0)
196 sshkey_alg_list(int certs_only
, int plain_only
, int include_sigonly
, char sep
)
198 char *tmp
, *ret
= NULL
;
199 size_t nlen
, rlen
= 0;
200 const struct keytype
*kt
;
202 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
203 if (kt
->name
== NULL
)
205 if (!include_sigonly
&& kt
->sigonly
)
207 if ((certs_only
&& !kt
->cert
) || (plain_only
&& kt
->cert
))
211 nlen
= strlen(kt
->name
);
212 if ((tmp
= realloc(ret
, rlen
+ nlen
+ 2)) == NULL
) {
217 memcpy(ret
+ rlen
, kt
->name
, nlen
+ 1);
224 sshkey_names_valid2(const char *names
, int allow_wildcard
)
227 const struct keytype
*kt
;
230 if (names
== NULL
|| strcmp(names
, "") == 0)
232 if ((s
= cp
= strdup(names
)) == NULL
)
234 for ((p
= strsep(&cp
, ",")); p
&& *p
!= '\0';
235 (p
= strsep(&cp
, ","))) {
236 type
= sshkey_type_from_name(p
);
237 if (type
== KEY_UNSPEC
) {
238 if (allow_wildcard
) {
240 * Try matching key types against the string.
241 * If any has a positive or negative match then
242 * the component is accepted.
244 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
245 if (match_pattern_list(kt
->name
,
261 sshkey_size(const struct sshkey
*k
)
267 return BN_num_bits(k
->rsa
->n
);
270 return BN_num_bits(k
->dsa
->p
);
273 return sshkey_curve_nid_to_bits(k
->ecdsa_nid
);
274 #endif /* WITH_OPENSSL */
276 case KEY_ED25519_CERT
:
277 return 256; /* XXX */
283 sshkey_type_is_valid_ca(int type
)
297 sshkey_is_cert(const struct sshkey
*k
)
301 return sshkey_type_is_cert(k
->type
);
304 /* Return the cert-less equivalent to a certified key type */
306 sshkey_type_plain(int type
)
315 case KEY_ED25519_CERT
:
323 /* XXX: these are really begging for a table-driven approach */
325 sshkey_curve_name_to_nid(const char *name
)
327 if (strcmp(name
, "nistp256") == 0)
328 return NID_X9_62_prime256v1
;
329 else if (strcmp(name
, "nistp384") == 0)
330 return NID_secp384r1
;
331 # ifdef OPENSSL_HAS_NISTP521
332 else if (strcmp(name
, "nistp521") == 0)
333 return NID_secp521r1
;
334 # endif /* OPENSSL_HAS_NISTP521 */
340 sshkey_curve_nid_to_bits(int nid
)
343 case NID_X9_62_prime256v1
:
347 # ifdef OPENSSL_HAS_NISTP521
350 # endif /* OPENSSL_HAS_NISTP521 */
357 sshkey_ecdsa_bits_to_nid(int bits
)
361 return NID_X9_62_prime256v1
;
363 return NID_secp384r1
;
364 # ifdef OPENSSL_HAS_NISTP521
366 return NID_secp521r1
;
367 # endif /* OPENSSL_HAS_NISTP521 */
374 sshkey_curve_nid_to_name(int nid
)
377 case NID_X9_62_prime256v1
:
381 # ifdef OPENSSL_HAS_NISTP521
384 # endif /* OPENSSL_HAS_NISTP521 */
391 sshkey_ec_nid_to_hash_alg(int nid
)
393 int kbits
= sshkey_curve_nid_to_bits(nid
);
398 /* RFC5656 section 6.2.1 */
400 return SSH_DIGEST_SHA256
;
401 else if (kbits
<= 384)
402 return SSH_DIGEST_SHA384
;
404 return SSH_DIGEST_SHA512
;
406 #endif /* WITH_OPENSSL */
409 cert_free(struct sshkey_cert
*cert
)
415 sshbuf_free(cert
->certblob
);
416 sshbuf_free(cert
->critical
);
417 sshbuf_free(cert
->extensions
);
419 for (i
= 0; i
< cert
->nprincipals
; i
++)
420 free(cert
->principals
[i
]);
421 free(cert
->principals
);
422 sshkey_free(cert
->signature_key
);
423 explicit_bzero(cert
, sizeof(*cert
));
427 static struct sshkey_cert
*
430 struct sshkey_cert
*cert
;
432 if ((cert
= calloc(1, sizeof(*cert
))) == NULL
)
434 if ((cert
->certblob
= sshbuf_new()) == NULL
||
435 (cert
->critical
= sshbuf_new()) == NULL
||
436 (cert
->extensions
= sshbuf_new()) == NULL
) {
441 cert
->principals
= NULL
;
442 cert
->signature_key
= NULL
;
453 #endif /* WITH_OPENSSL */
455 if ((k
= calloc(1, sizeof(*k
))) == NULL
)
463 k
->ed25519_sk
= NULL
;
464 k
->ed25519_pk
= NULL
;
469 if ((rsa
= RSA_new()) == NULL
||
470 (rsa
->n
= BN_new()) == NULL
||
471 (rsa
->e
= BN_new()) == NULL
) {
481 if ((dsa
= DSA_new()) == NULL
||
482 (dsa
->p
= BN_new()) == NULL
||
483 (dsa
->q
= BN_new()) == NULL
||
484 (dsa
->g
= BN_new()) == NULL
||
485 (dsa
->pub_key
= BN_new()) == NULL
) {
495 /* Cannot do anything until we know the group */
497 #endif /* WITH_OPENSSL */
499 case KEY_ED25519_CERT
:
500 /* no need to prealloc */
509 if (sshkey_is_cert(k
)) {
510 if ((k
->cert
= cert_new()) == NULL
) {
520 sshkey_add_private(struct sshkey
*k
)
526 #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
527 if (bn_maybe_alloc_failed(k
->rsa
->d
) ||
528 bn_maybe_alloc_failed(k
->rsa
->iqmp
) ||
529 bn_maybe_alloc_failed(k
->rsa
->q
) ||
530 bn_maybe_alloc_failed(k
->rsa
->p
) ||
531 bn_maybe_alloc_failed(k
->rsa
->dmq1
) ||
532 bn_maybe_alloc_failed(k
->rsa
->dmp1
))
533 return SSH_ERR_ALLOC_FAIL
;
537 if (bn_maybe_alloc_failed(k
->dsa
->priv_key
))
538 return SSH_ERR_ALLOC_FAIL
;
540 #undef bn_maybe_alloc_failed
543 /* Cannot do anything until we know the group */
545 #endif /* WITH_OPENSSL */
547 case KEY_ED25519_CERT
:
548 /* no need to prealloc */
553 return SSH_ERR_INVALID_ARGUMENT
;
559 sshkey_new_private(int type
)
561 struct sshkey
*k
= sshkey_new(type
);
565 if (sshkey_add_private(k
) != 0) {
573 sshkey_free(struct sshkey
*k
)
591 # ifdef OPENSSL_HAS_ECC
594 if (k
->ecdsa
!= NULL
)
595 EC_KEY_free(k
->ecdsa
);
598 # endif /* OPENSSL_HAS_ECC */
599 #endif /* WITH_OPENSSL */
601 case KEY_ED25519_CERT
:
603 explicit_bzero(k
->ed25519_pk
, ED25519_PK_SZ
);
605 k
->ed25519_pk
= NULL
;
608 explicit_bzero(k
->ed25519_sk
, ED25519_SK_SZ
);
610 k
->ed25519_sk
= NULL
;
618 if (sshkey_is_cert(k
))
620 explicit_bzero(k
, sizeof(*k
));
625 cert_compare(struct sshkey_cert
*a
, struct sshkey_cert
*b
)
627 if (a
== NULL
&& b
== NULL
)
629 if (a
== NULL
|| b
== NULL
)
631 if (sshbuf_len(a
->certblob
) != sshbuf_len(b
->certblob
))
633 if (timingsafe_bcmp(sshbuf_ptr(a
->certblob
), sshbuf_ptr(b
->certblob
),
634 sshbuf_len(a
->certblob
)) != 0)
640 * Compare public portions of key only, allowing comparisons between
641 * certificates and plain keys too.
644 sshkey_equal_public(const struct sshkey
*a
, const struct sshkey
*b
)
646 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
648 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
650 if (a
== NULL
|| b
== NULL
||
651 sshkey_type_plain(a
->type
) != sshkey_type_plain(b
->type
))
658 return a
->rsa
!= NULL
&& b
->rsa
!= NULL
&&
659 BN_cmp(a
->rsa
->e
, b
->rsa
->e
) == 0 &&
660 BN_cmp(a
->rsa
->n
, b
->rsa
->n
) == 0;
663 return a
->dsa
!= NULL
&& b
->dsa
!= NULL
&&
664 BN_cmp(a
->dsa
->p
, b
->dsa
->p
) == 0 &&
665 BN_cmp(a
->dsa
->q
, b
->dsa
->q
) == 0 &&
666 BN_cmp(a
->dsa
->g
, b
->dsa
->g
) == 0 &&
667 BN_cmp(a
->dsa
->pub_key
, b
->dsa
->pub_key
) == 0;
668 # ifdef OPENSSL_HAS_ECC
671 if (a
->ecdsa
== NULL
|| b
->ecdsa
== NULL
||
672 EC_KEY_get0_public_key(a
->ecdsa
) == NULL
||
673 EC_KEY_get0_public_key(b
->ecdsa
) == NULL
)
675 if ((bnctx
= BN_CTX_new()) == NULL
)
677 if (EC_GROUP_cmp(EC_KEY_get0_group(a
->ecdsa
),
678 EC_KEY_get0_group(b
->ecdsa
), bnctx
) != 0 ||
679 EC_POINT_cmp(EC_KEY_get0_group(a
->ecdsa
),
680 EC_KEY_get0_public_key(a
->ecdsa
),
681 EC_KEY_get0_public_key(b
->ecdsa
), bnctx
) != 0) {
687 # endif /* OPENSSL_HAS_ECC */
688 #endif /* WITH_OPENSSL */
690 case KEY_ED25519_CERT
:
691 return a
->ed25519_pk
!= NULL
&& b
->ed25519_pk
!= NULL
&&
692 memcmp(a
->ed25519_pk
, b
->ed25519_pk
, ED25519_PK_SZ
) == 0;
700 sshkey_equal(const struct sshkey
*a
, const struct sshkey
*b
)
702 if (a
== NULL
|| b
== NULL
|| a
->type
!= b
->type
)
704 if (sshkey_is_cert(a
)) {
705 if (!cert_compare(a
->cert
, b
->cert
))
708 return sshkey_equal_public(a
, b
);
712 to_blob_buf(const struct sshkey
*key
, struct sshbuf
*b
, int force_plain
)
714 int type
, ret
= SSH_ERR_INTERNAL_ERROR
;
715 const char *typename
;
718 return SSH_ERR_INVALID_ARGUMENT
;
720 if (sshkey_is_cert(key
)) {
721 if (key
->cert
== NULL
)
722 return SSH_ERR_EXPECTED_CERT
;
723 if (sshbuf_len(key
->cert
->certblob
) == 0)
724 return SSH_ERR_KEY_LACKS_CERTBLOB
;
726 type
= force_plain
? sshkey_type_plain(key
->type
) : key
->type
;
727 typename
= sshkey_ssh_name_from_type_nid(type
, key
->ecdsa_nid
);
734 #endif /* WITH_OPENSSL */
735 case KEY_ED25519_CERT
:
736 /* Use the existing blob */
737 /* XXX modified flag? */
738 if ((ret
= sshbuf_putb(b
, key
->cert
->certblob
)) != 0)
743 if (key
->dsa
== NULL
)
744 return SSH_ERR_INVALID_ARGUMENT
;
745 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
746 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->p
)) != 0 ||
747 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->q
)) != 0 ||
748 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->g
)) != 0 ||
749 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->pub_key
)) != 0)
752 # ifdef OPENSSL_HAS_ECC
754 if (key
->ecdsa
== NULL
)
755 return SSH_ERR_INVALID_ARGUMENT
;
756 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
757 (ret
= sshbuf_put_cstring(b
,
758 sshkey_curve_nid_to_name(key
->ecdsa_nid
))) != 0 ||
759 (ret
= sshbuf_put_eckey(b
, key
->ecdsa
)) != 0)
764 if (key
->rsa
== NULL
)
765 return SSH_ERR_INVALID_ARGUMENT
;
766 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
767 (ret
= sshbuf_put_bignum2(b
, key
->rsa
->e
)) != 0 ||
768 (ret
= sshbuf_put_bignum2(b
, key
->rsa
->n
)) != 0)
771 #endif /* WITH_OPENSSL */
773 if (key
->ed25519_pk
== NULL
)
774 return SSH_ERR_INVALID_ARGUMENT
;
775 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
776 (ret
= sshbuf_put_string(b
,
777 key
->ed25519_pk
, ED25519_PK_SZ
)) != 0)
781 return SSH_ERR_KEY_TYPE_UNKNOWN
;
787 sshkey_putb(const struct sshkey
*key
, struct sshbuf
*b
)
789 return to_blob_buf(key
, b
, 0);
793 sshkey_puts(const struct sshkey
*key
, struct sshbuf
*b
)
798 if ((tmp
= sshbuf_new()) == NULL
)
799 return SSH_ERR_ALLOC_FAIL
;
800 r
= to_blob_buf(key
, tmp
, 0);
802 r
= sshbuf_put_stringb(b
, tmp
);
808 sshkey_putb_plain(const struct sshkey
*key
, struct sshbuf
*b
)
810 return to_blob_buf(key
, b
, 1);
814 to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
, int force_plain
)
816 int ret
= SSH_ERR_INTERNAL_ERROR
;
818 struct sshbuf
*b
= NULL
;
824 if ((b
= sshbuf_new()) == NULL
)
825 return SSH_ERR_ALLOC_FAIL
;
826 if ((ret
= to_blob_buf(key
, b
, force_plain
)) != 0)
832 if ((*blobp
= malloc(len
)) == NULL
) {
833 ret
= SSH_ERR_ALLOC_FAIL
;
836 memcpy(*blobp
, sshbuf_ptr(b
), len
);
845 sshkey_to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
)
847 return to_blob(key
, blobp
, lenp
, 0);
851 sshkey_plain_to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
)
853 return to_blob(key
, blobp
, lenp
, 1);
857 sshkey_fingerprint_raw(const struct sshkey
*k
, int dgst_alg
,
858 u_char
**retp
, size_t *lenp
)
860 u_char
*blob
= NULL
, *ret
= NULL
;
862 int r
= SSH_ERR_INTERNAL_ERROR
;
868 if (ssh_digest_bytes(dgst_alg
) == 0) {
869 r
= SSH_ERR_INVALID_ARGUMENT
;
872 if ((r
= to_blob(k
, &blob
, &blob_len
, 1)) != 0)
874 if ((ret
= calloc(1, SSH_DIGEST_MAX_LENGTH
)) == NULL
) {
875 r
= SSH_ERR_ALLOC_FAIL
;
878 if ((r
= ssh_digest_memory(dgst_alg
, blob
, blob_len
,
879 ret
, SSH_DIGEST_MAX_LENGTH
)) != 0)
887 *lenp
= ssh_digest_bytes(dgst_alg
);
892 explicit_bzero(blob
, blob_len
);
899 fingerprint_b64(const char *alg
, u_char
*dgst_raw
, size_t dgst_raw_len
)
902 size_t plen
= strlen(alg
) + 1;
903 size_t rlen
= ((dgst_raw_len
+ 2) / 3) * 4 + plen
+ 1;
906 if (dgst_raw_len
> 65536 || (ret
= calloc(1, rlen
)) == NULL
)
908 strlcpy(ret
, alg
, rlen
);
909 strlcat(ret
, ":", rlen
);
910 if (dgst_raw_len
== 0)
912 if ((r
= b64_ntop(dgst_raw
, dgst_raw_len
,
913 ret
+ plen
, rlen
- plen
)) == -1) {
914 explicit_bzero(ret
, rlen
);
918 /* Trim padding characters from end */
919 ret
[strcspn(ret
, "=")] = '\0';
924 fingerprint_hex(const char *alg
, u_char
*dgst_raw
, size_t dgst_raw_len
)
926 char *retval
, hex
[5];
927 size_t i
, rlen
= dgst_raw_len
* 3 + strlen(alg
) + 2;
929 if (dgst_raw_len
> 65536 || (retval
= calloc(1, rlen
)) == NULL
)
931 strlcpy(retval
, alg
, rlen
);
932 strlcat(retval
, ":", rlen
);
933 for (i
= 0; i
< dgst_raw_len
; i
++) {
934 snprintf(hex
, sizeof(hex
), "%s%02x",
935 i
> 0 ? ":" : "", dgst_raw
[i
]);
936 strlcat(retval
, hex
, rlen
);
942 fingerprint_bubblebabble(u_char
*dgst_raw
, size_t dgst_raw_len
)
944 char vowels
[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
945 char consonants
[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
946 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
947 u_int i
, j
= 0, rounds
, seed
= 1;
950 rounds
= (dgst_raw_len
/ 2) + 1;
951 if ((retval
= calloc(rounds
, 6)) == NULL
)
954 for (i
= 0; i
< rounds
; i
++) {
955 u_int idx0
, idx1
, idx2
, idx3
, idx4
;
956 if ((i
+ 1 < rounds
) || (dgst_raw_len
% 2 != 0)) {
957 idx0
= (((((u_int
)(dgst_raw
[2 * i
])) >> 6) & 3) +
959 idx1
= (((u_int
)(dgst_raw
[2 * i
])) >> 2) & 15;
960 idx2
= ((((u_int
)(dgst_raw
[2 * i
])) & 3) +
962 retval
[j
++] = vowels
[idx0
];
963 retval
[j
++] = consonants
[idx1
];
964 retval
[j
++] = vowels
[idx2
];
965 if ((i
+ 1) < rounds
) {
966 idx3
= (((u_int
)(dgst_raw
[(2 * i
) + 1])) >> 4) & 15;
967 idx4
= (((u_int
)(dgst_raw
[(2 * i
) + 1]))) & 15;
968 retval
[j
++] = consonants
[idx3
];
970 retval
[j
++] = consonants
[idx4
];
972 ((((u_int
)(dgst_raw
[2 * i
])) * 7) +
973 ((u_int
)(dgst_raw
[(2 * i
) + 1])))) % 36;
979 retval
[j
++] = vowels
[idx0
];
980 retval
[j
++] = consonants
[idx1
];
981 retval
[j
++] = vowels
[idx2
];
990 * Draw an ASCII-Art representing the fingerprint so human brain can
991 * profit from its built-in pattern recognition ability.
992 * This technique is called "random art" and can be found in some
993 * scientific publications like this original paper:
995 * "Hash Visualization: a New Technique to improve Real-World Security",
996 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
997 * Techniques and E-Commerce (CrypTEC '99)
998 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
1000 * The subject came up in a talk by Dan Kaminsky, too.
1002 * If you see the picture is different, the key is different.
1003 * If the picture looks the same, you still know nothing.
1005 * The algorithm used here is a worm crawling over a discrete plane,
1006 * leaving a trace (augmenting the field) everywhere it goes.
1007 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
1008 * makes the respective movement vector be ignored for this turn.
1009 * Graphs are not unambiguous, because circles in graphs can be
1010 * walked in either direction.
1014 * Field sizes for the random art. Have to be odd, so the starting point
1015 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1016 * Else pictures would be too dense, and drawing the frame would
1017 * fail, too, because the key type would not fit in anymore.
1020 #define FLDSIZE_Y (FLDBASE + 1)
1021 #define FLDSIZE_X (FLDBASE * 2 + 1)
1023 fingerprint_randomart(const char *alg
, u_char
*dgst_raw
, size_t dgst_raw_len
,
1024 const struct sshkey
*k
)
1027 * Chars to be used after each other every time the worm
1028 * intersects with itself. Matter of taste.
1030 char *augmentation_string
= " .o+=*BOX@%&#/^SE";
1031 char *retval
, *p
, title
[FLDSIZE_X
], hash
[FLDSIZE_X
];
1032 u_char field
[FLDSIZE_X
][FLDSIZE_Y
];
1033 size_t i
, tlen
, hlen
;
1036 size_t len
= strlen(augmentation_string
) - 1;
1038 if ((retval
= calloc((FLDSIZE_X
+ 3), (FLDSIZE_Y
+ 2))) == NULL
)
1041 /* initialize field */
1042 memset(field
, 0, FLDSIZE_X
* FLDSIZE_Y
* sizeof(char));
1046 /* process raw key */
1047 for (i
= 0; i
< dgst_raw_len
; i
++) {
1049 /* each byte conveys four 2-bit move commands */
1050 input
= dgst_raw
[i
];
1051 for (b
= 0; b
< 4; b
++) {
1052 /* evaluate 2 bit, rest is shifted later */
1053 x
+= (input
& 0x1) ? 1 : -1;
1054 y
+= (input
& 0x2) ? 1 : -1;
1056 /* assure we are still in bounds */
1059 x
= MINIMUM(x
, FLDSIZE_X
- 1);
1060 y
= MINIMUM(y
, FLDSIZE_Y
- 1);
1062 /* augment the field */
1063 if (field
[x
][y
] < len
- 2)
1069 /* mark starting point and end point*/
1070 field
[FLDSIZE_X
/ 2][FLDSIZE_Y
/ 2] = len
- 1;
1073 /* assemble title */
1074 r
= snprintf(title
, sizeof(title
), "[%s %u]",
1075 sshkey_type(k
), sshkey_size(k
));
1076 /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1077 if (r
< 0 || r
> (int)sizeof(title
))
1078 r
= snprintf(title
, sizeof(title
), "[%s]", sshkey_type(k
));
1079 tlen
= (r
<= 0) ? 0 : strlen(title
);
1081 /* assemble hash ID. */
1082 r
= snprintf(hash
, sizeof(hash
), "[%s]", alg
);
1083 hlen
= (r
<= 0) ? 0 : strlen(hash
);
1085 /* output upper border */
1088 for (i
= 0; i
< (FLDSIZE_X
- tlen
) / 2; i
++)
1090 memcpy(p
, title
, tlen
);
1092 for (i
+= tlen
; i
< FLDSIZE_X
; i
++)
1097 /* output content */
1098 for (y
= 0; y
< FLDSIZE_Y
; y
++) {
1100 for (x
= 0; x
< FLDSIZE_X
; x
++)
1101 *p
++ = augmentation_string
[MINIMUM(field
[x
][y
], len
)];
1106 /* output lower border */
1108 for (i
= 0; i
< (FLDSIZE_X
- hlen
) / 2; i
++)
1110 memcpy(p
, hash
, hlen
);
1112 for (i
+= hlen
; i
< FLDSIZE_X
; i
++)
1120 sshkey_fingerprint(const struct sshkey
*k
, int dgst_alg
,
1121 enum sshkey_fp_rep dgst_rep
)
1123 char *retval
= NULL
;
1125 size_t dgst_raw_len
;
1127 if (sshkey_fingerprint_raw(k
, dgst_alg
, &dgst_raw
, &dgst_raw_len
) != 0)
1130 case SSH_FP_DEFAULT
:
1131 if (dgst_alg
== SSH_DIGEST_MD5
) {
1132 retval
= fingerprint_hex(ssh_digest_alg_name(dgst_alg
),
1133 dgst_raw
, dgst_raw_len
);
1135 retval
= fingerprint_b64(ssh_digest_alg_name(dgst_alg
),
1136 dgst_raw
, dgst_raw_len
);
1140 retval
= fingerprint_hex(ssh_digest_alg_name(dgst_alg
),
1141 dgst_raw
, dgst_raw_len
);
1144 retval
= fingerprint_b64(ssh_digest_alg_name(dgst_alg
),
1145 dgst_raw
, dgst_raw_len
);
1147 case SSH_FP_BUBBLEBABBLE
:
1148 retval
= fingerprint_bubblebabble(dgst_raw
, dgst_raw_len
);
1150 case SSH_FP_RANDOMART
:
1151 retval
= fingerprint_randomart(ssh_digest_alg_name(dgst_alg
),
1152 dgst_raw
, dgst_raw_len
, k
);
1155 explicit_bzero(dgst_raw
, dgst_raw_len
);
1159 explicit_bzero(dgst_raw
, dgst_raw_len
);
1165 /* returns 0 ok, and < 0 error */
1167 sshkey_read(struct sshkey
*ret
, char **cpp
)
1170 int retval
= SSH_ERR_INVALID_FORMAT
;
1171 char *ep
, *cp
, *space
;
1172 int r
, type
, curve_nid
= -1;
1173 struct sshbuf
*blob
;
1176 return SSH_ERR_INVALID_ARGUMENT
;
1180 switch (ret
->type
) {
1187 case KEY_ECDSA_CERT
:
1189 case KEY_ED25519_CERT
:
1190 space
= strchr(cp
, ' ');
1192 return SSH_ERR_INVALID_FORMAT
;
1194 type
= sshkey_type_from_name(cp
);
1195 if (sshkey_type_plain(type
) == KEY_ECDSA
&&
1196 (curve_nid
= sshkey_ecdsa_nid_from_name(cp
)) == -1)
1197 return SSH_ERR_EC_CURVE_INVALID
;
1199 if (type
== KEY_UNSPEC
)
1200 return SSH_ERR_INVALID_FORMAT
;
1203 return SSH_ERR_INVALID_FORMAT
;
1204 if (ret
->type
!= KEY_UNSPEC
&& ret
->type
!= type
)
1205 return SSH_ERR_KEY_TYPE_MISMATCH
;
1206 if ((blob
= sshbuf_new()) == NULL
)
1207 return SSH_ERR_ALLOC_FAIL
;
1209 space
= strchr(cp
, ' ');
1211 /* advance 'space': skip whitespace */
1213 while (*space
== ' ' || *space
== '\t')
1217 ep
= cp
+ strlen(cp
);
1218 if ((r
= sshbuf_b64tod(blob
, cp
)) != 0) {
1222 if ((r
= sshkey_from_blob(sshbuf_ptr(blob
),
1223 sshbuf_len(blob
), &k
)) != 0) {
1228 if (k
->type
!= type
) {
1230 return SSH_ERR_KEY_TYPE_MISMATCH
;
1232 if (sshkey_type_plain(type
) == KEY_ECDSA
&&
1233 curve_nid
!= k
->ecdsa_nid
) {
1235 return SSH_ERR_EC_CURVE_MISMATCH
;
1238 if (sshkey_is_cert(ret
)) {
1239 if (!sshkey_is_cert(k
)) {
1241 return SSH_ERR_EXPECTED_CERT
;
1243 if (ret
->cert
!= NULL
)
1244 cert_free(ret
->cert
);
1245 ret
->cert
= k
->cert
;
1248 switch (sshkey_type_plain(ret
->type
)) {
1251 if (ret
->rsa
!= NULL
)
1256 RSA_print_fp(stderr
, ret
->rsa
, 8);
1260 if (ret
->dsa
!= NULL
)
1265 DSA_print_fp(stderr
, ret
->dsa
, 8);
1268 # ifdef OPENSSL_HAS_ECC
1270 if (ret
->ecdsa
!= NULL
)
1271 EC_KEY_free(ret
->ecdsa
);
1272 ret
->ecdsa
= k
->ecdsa
;
1273 ret
->ecdsa_nid
= k
->ecdsa_nid
;
1277 sshkey_dump_ec_key(ret
->ecdsa
);
1280 # endif /* OPENSSL_HAS_ECC */
1281 #endif /* WITH_OPENSSL */
1283 free(ret
->ed25519_pk
);
1284 ret
->ed25519_pk
= k
->ed25519_pk
;
1285 k
->ed25519_pk
= NULL
;
1299 return SSH_ERR_INVALID_ARGUMENT
;
1305 sshkey_to_base64(const struct sshkey
*key
, char **b64p
)
1307 int r
= SSH_ERR_INTERNAL_ERROR
;
1308 struct sshbuf
*b
= NULL
;
1313 if ((b
= sshbuf_new()) == NULL
)
1314 return SSH_ERR_ALLOC_FAIL
;
1315 if ((r
= sshkey_putb(key
, b
)) != 0)
1317 if ((uu
= sshbuf_dtob64(b
)) == NULL
) {
1318 r
= SSH_ERR_ALLOC_FAIL
;
1334 sshkey_format_text(const struct sshkey
*key
, struct sshbuf
*b
)
1336 int r
= SSH_ERR_INTERNAL_ERROR
;
1339 if ((r
= sshkey_to_base64(key
, &uu
)) != 0)
1341 if ((r
= sshbuf_putf(b
, "%s %s",
1342 sshkey_ssh_name(key
), uu
)) != 0)
1351 sshkey_write(const struct sshkey
*key
, FILE *f
)
1353 struct sshbuf
*b
= NULL
;
1354 int r
= SSH_ERR_INTERNAL_ERROR
;
1356 if ((b
= sshbuf_new()) == NULL
)
1357 return SSH_ERR_ALLOC_FAIL
;
1358 if ((r
= sshkey_format_text(key
, b
)) != 0)
1360 if (fwrite(sshbuf_ptr(b
), sshbuf_len(b
), 1, f
) != 1) {
1363 r
= SSH_ERR_SYSTEM_ERROR
;
1374 sshkey_cert_type(const struct sshkey
*k
)
1376 switch (k
->cert
->type
) {
1377 case SSH2_CERT_TYPE_USER
:
1379 case SSH2_CERT_TYPE_HOST
:
1388 rsa_generate_private_key(u_int bits
, RSA
**rsap
)
1390 RSA
*private = NULL
;
1392 int ret
= SSH_ERR_INTERNAL_ERROR
;
1395 return SSH_ERR_INVALID_ARGUMENT
;
1396 if (bits
< SSH_RSA_MINIMUM_MODULUS_SIZE
||
1397 bits
> SSHBUF_MAX_BIGNUM
* 8)
1398 return SSH_ERR_KEY_LENGTH
;
1400 if ((private = RSA_new()) == NULL
|| (f4
= BN_new()) == NULL
) {
1401 ret
= SSH_ERR_ALLOC_FAIL
;
1404 if (!BN_set_word(f4
, RSA_F4
) ||
1405 !RSA_generate_key_ex(private, bits
, f4
, NULL
)) {
1406 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1413 if (private != NULL
)
1421 dsa_generate_private_key(u_int bits
, DSA
**dsap
)
1424 int ret
= SSH_ERR_INTERNAL_ERROR
;
1427 return SSH_ERR_INVALID_ARGUMENT
;
1429 return SSH_ERR_KEY_LENGTH
;
1430 if ((private = DSA_new()) == NULL
) {
1431 ret
= SSH_ERR_ALLOC_FAIL
;
1435 if (!DSA_generate_parameters_ex(private, bits
, NULL
, 0, NULL
,
1436 NULL
, NULL
) || !DSA_generate_key(private)) {
1437 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1444 if (private != NULL
)
1449 # ifdef OPENSSL_HAS_ECC
1451 sshkey_ecdsa_key_to_nid(EC_KEY
*k
)
1455 NID_X9_62_prime256v1
,
1457 # ifdef OPENSSL_HAS_NISTP521
1459 # endif /* OPENSSL_HAS_NISTP521 */
1465 const EC_GROUP
*g
= EC_KEY_get0_group(k
);
1468 * The group may be stored in a ASN.1 encoded private key in one of two
1469 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1470 * or explicit group parameters encoded into the key blob. Only the
1471 * "named group" case sets the group NID for us, but we can figure
1472 * it out for the other case by comparing against all the groups that
1475 if ((nid
= EC_GROUP_get_curve_name(g
)) > 0)
1477 if ((bnctx
= BN_CTX_new()) == NULL
)
1479 for (i
= 0; nids
[i
] != -1; i
++) {
1480 if ((eg
= EC_GROUP_new_by_curve_name(nids
[i
])) == NULL
) {
1484 if (EC_GROUP_cmp(g
, eg
, bnctx
) == 0)
1489 if (nids
[i
] != -1) {
1490 /* Use the group with the NID attached */
1491 EC_GROUP_set_asn1_flag(eg
, OPENSSL_EC_NAMED_CURVE
);
1492 if (EC_KEY_set_group(k
, eg
) != 1) {
1501 ecdsa_generate_private_key(u_int bits
, int *nid
, EC_KEY
**ecdsap
)
1504 int ret
= SSH_ERR_INTERNAL_ERROR
;
1506 if (nid
== NULL
|| ecdsap
== NULL
)
1507 return SSH_ERR_INVALID_ARGUMENT
;
1508 if ((*nid
= sshkey_ecdsa_bits_to_nid(bits
)) == -1)
1509 return SSH_ERR_KEY_LENGTH
;
1511 if ((private = EC_KEY_new_by_curve_name(*nid
)) == NULL
) {
1512 ret
= SSH_ERR_ALLOC_FAIL
;
1515 if (EC_KEY_generate_key(private) != 1) {
1516 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1519 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE
);
1524 if (private != NULL
)
1525 EC_KEY_free(private);
1528 # endif /* OPENSSL_HAS_ECC */
1529 #endif /* WITH_OPENSSL */
1532 sshkey_generate(int type
, u_int bits
, struct sshkey
**keyp
)
1535 int ret
= SSH_ERR_INTERNAL_ERROR
;
1538 return SSH_ERR_INVALID_ARGUMENT
;
1540 if ((k
= sshkey_new(KEY_UNSPEC
)) == NULL
)
1541 return SSH_ERR_ALLOC_FAIL
;
1544 if ((k
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
||
1545 (k
->ed25519_sk
= malloc(ED25519_SK_SZ
)) == NULL
) {
1546 ret
= SSH_ERR_ALLOC_FAIL
;
1549 crypto_sign_ed25519_keypair(k
->ed25519_pk
, k
->ed25519_sk
);
1554 ret
= dsa_generate_private_key(bits
, &k
->dsa
);
1556 # ifdef OPENSSL_HAS_ECC
1558 ret
= ecdsa_generate_private_key(bits
, &k
->ecdsa_nid
,
1561 # endif /* OPENSSL_HAS_ECC */
1563 ret
= rsa_generate_private_key(bits
, &k
->rsa
);
1565 #endif /* WITH_OPENSSL */
1567 ret
= SSH_ERR_INVALID_ARGUMENT
;
1578 sshkey_cert_copy(const struct sshkey
*from_key
, struct sshkey
*to_key
)
1581 const struct sshkey_cert
*from
;
1582 struct sshkey_cert
*to
;
1583 int ret
= SSH_ERR_INTERNAL_ERROR
;
1585 if (to_key
->cert
!= NULL
) {
1586 cert_free(to_key
->cert
);
1587 to_key
->cert
= NULL
;
1590 if ((from
= from_key
->cert
) == NULL
)
1591 return SSH_ERR_INVALID_ARGUMENT
;
1593 if ((to
= to_key
->cert
= cert_new()) == NULL
)
1594 return SSH_ERR_ALLOC_FAIL
;
1596 if ((ret
= sshbuf_putb(to
->certblob
, from
->certblob
)) != 0 ||
1597 (ret
= sshbuf_putb(to
->critical
, from
->critical
)) != 0 ||
1598 (ret
= sshbuf_putb(to
->extensions
, from
->extensions
)) != 0)
1601 to
->serial
= from
->serial
;
1602 to
->type
= from
->type
;
1603 if (from
->key_id
== NULL
)
1605 else if ((to
->key_id
= strdup(from
->key_id
)) == NULL
)
1606 return SSH_ERR_ALLOC_FAIL
;
1607 to
->valid_after
= from
->valid_after
;
1608 to
->valid_before
= from
->valid_before
;
1609 if (from
->signature_key
== NULL
)
1610 to
->signature_key
= NULL
;
1611 else if ((ret
= sshkey_from_private(from
->signature_key
,
1612 &to
->signature_key
)) != 0)
1615 if (from
->nprincipals
> SSHKEY_CERT_MAX_PRINCIPALS
)
1616 return SSH_ERR_INVALID_ARGUMENT
;
1617 if (from
->nprincipals
> 0) {
1618 if ((to
->principals
= calloc(from
->nprincipals
,
1619 sizeof(*to
->principals
))) == NULL
)
1620 return SSH_ERR_ALLOC_FAIL
;
1621 for (i
= 0; i
< from
->nprincipals
; i
++) {
1622 to
->principals
[i
] = strdup(from
->principals
[i
]);
1623 if (to
->principals
[i
] == NULL
) {
1624 to
->nprincipals
= i
;
1625 return SSH_ERR_ALLOC_FAIL
;
1629 to
->nprincipals
= from
->nprincipals
;
1634 sshkey_from_private(const struct sshkey
*k
, struct sshkey
**pkp
)
1636 struct sshkey
*n
= NULL
;
1637 int ret
= SSH_ERR_INTERNAL_ERROR
;
1644 if ((n
= sshkey_new(k
->type
)) == NULL
)
1645 return SSH_ERR_ALLOC_FAIL
;
1646 if ((BN_copy(n
->dsa
->p
, k
->dsa
->p
) == NULL
) ||
1647 (BN_copy(n
->dsa
->q
, k
->dsa
->q
) == NULL
) ||
1648 (BN_copy(n
->dsa
->g
, k
->dsa
->g
) == NULL
) ||
1649 (BN_copy(n
->dsa
->pub_key
, k
->dsa
->pub_key
) == NULL
)) {
1651 return SSH_ERR_ALLOC_FAIL
;
1654 # ifdef OPENSSL_HAS_ECC
1656 case KEY_ECDSA_CERT
:
1657 if ((n
= sshkey_new(k
->type
)) == NULL
)
1658 return SSH_ERR_ALLOC_FAIL
;
1659 n
->ecdsa_nid
= k
->ecdsa_nid
;
1660 n
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
);
1661 if (n
->ecdsa
== NULL
) {
1663 return SSH_ERR_ALLOC_FAIL
;
1665 if (EC_KEY_set_public_key(n
->ecdsa
,
1666 EC_KEY_get0_public_key(k
->ecdsa
)) != 1) {
1668 return SSH_ERR_LIBCRYPTO_ERROR
;
1671 # endif /* OPENSSL_HAS_ECC */
1674 if ((n
= sshkey_new(k
->type
)) == NULL
)
1675 return SSH_ERR_ALLOC_FAIL
;
1676 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
1677 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
)) {
1679 return SSH_ERR_ALLOC_FAIL
;
1682 #endif /* WITH_OPENSSL */
1684 case KEY_ED25519_CERT
:
1685 if ((n
= sshkey_new(k
->type
)) == NULL
)
1686 return SSH_ERR_ALLOC_FAIL
;
1687 if (k
->ed25519_pk
!= NULL
) {
1688 if ((n
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
) {
1690 return SSH_ERR_ALLOC_FAIL
;
1692 memcpy(n
->ed25519_pk
, k
->ed25519_pk
, ED25519_PK_SZ
);
1696 return SSH_ERR_KEY_TYPE_UNKNOWN
;
1698 if (sshkey_is_cert(k
)) {
1699 if ((ret
= sshkey_cert_copy(k
, n
)) != 0) {
1709 cert_parse(struct sshbuf
*b
, struct sshkey
*key
, struct sshbuf
*certbuf
)
1711 struct sshbuf
*principals
= NULL
, *crit
= NULL
;
1712 struct sshbuf
*exts
= NULL
, *ca
= NULL
;
1714 size_t signed_len
= 0, slen
= 0, kidlen
= 0;
1715 int ret
= SSH_ERR_INTERNAL_ERROR
;
1717 /* Copy the entire key blob for verification and later serialisation */
1718 if ((ret
= sshbuf_putb(key
->cert
->certblob
, certbuf
)) != 0)
1721 /* Parse body of certificate up to signature */
1722 if ((ret
= sshbuf_get_u64(b
, &key
->cert
->serial
)) != 0 ||
1723 (ret
= sshbuf_get_u32(b
, &key
->cert
->type
)) != 0 ||
1724 (ret
= sshbuf_get_cstring(b
, &key
->cert
->key_id
, &kidlen
)) != 0 ||
1725 (ret
= sshbuf_froms(b
, &principals
)) != 0 ||
1726 (ret
= sshbuf_get_u64(b
, &key
->cert
->valid_after
)) != 0 ||
1727 (ret
= sshbuf_get_u64(b
, &key
->cert
->valid_before
)) != 0 ||
1728 (ret
= sshbuf_froms(b
, &crit
)) != 0 ||
1729 (ret
= sshbuf_froms(b
, &exts
)) != 0 ||
1730 (ret
= sshbuf_get_string_direct(b
, NULL
, NULL
)) != 0 ||
1731 (ret
= sshbuf_froms(b
, &ca
)) != 0) {
1732 /* XXX debug print error for ret */
1733 ret
= SSH_ERR_INVALID_FORMAT
;
1737 /* Signature is left in the buffer so we can calculate this length */
1738 signed_len
= sshbuf_len(key
->cert
->certblob
) - sshbuf_len(b
);
1740 if ((ret
= sshbuf_get_string(b
, &sig
, &slen
)) != 0) {
1741 ret
= SSH_ERR_INVALID_FORMAT
;
1745 if (key
->cert
->type
!= SSH2_CERT_TYPE_USER
&&
1746 key
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1747 ret
= SSH_ERR_KEY_CERT_UNKNOWN_TYPE
;
1751 /* Parse principals section */
1752 while (sshbuf_len(principals
) > 0) {
1753 char *principal
= NULL
;
1754 char **oprincipals
= NULL
;
1756 if (key
->cert
->nprincipals
>= SSHKEY_CERT_MAX_PRINCIPALS
) {
1757 ret
= SSH_ERR_INVALID_FORMAT
;
1760 if ((ret
= sshbuf_get_cstring(principals
, &principal
,
1762 ret
= SSH_ERR_INVALID_FORMAT
;
1765 oprincipals
= key
->cert
->principals
;
1766 key
->cert
->principals
= recallocarray(key
->cert
->principals
,
1767 key
->cert
->nprincipals
, key
->cert
->nprincipals
+ 1,
1768 sizeof(*key
->cert
->principals
));
1769 if (key
->cert
->principals
== NULL
) {
1771 key
->cert
->principals
= oprincipals
;
1772 ret
= SSH_ERR_ALLOC_FAIL
;
1775 key
->cert
->principals
[key
->cert
->nprincipals
++] = principal
;
1779 * Stash a copies of the critical options and extensions sections
1782 if ((ret
= sshbuf_putb(key
->cert
->critical
, crit
)) != 0 ||
1784 (ret
= sshbuf_putb(key
->cert
->extensions
, exts
)) != 0))
1788 * Validate critical options and extensions sections format.
1790 while (sshbuf_len(crit
) != 0) {
1791 if ((ret
= sshbuf_get_string_direct(crit
, NULL
, NULL
)) != 0 ||
1792 (ret
= sshbuf_get_string_direct(crit
, NULL
, NULL
)) != 0) {
1793 sshbuf_reset(key
->cert
->critical
);
1794 ret
= SSH_ERR_INVALID_FORMAT
;
1798 while (exts
!= NULL
&& sshbuf_len(exts
) != 0) {
1799 if ((ret
= sshbuf_get_string_direct(exts
, NULL
, NULL
)) != 0 ||
1800 (ret
= sshbuf_get_string_direct(exts
, NULL
, NULL
)) != 0) {
1801 sshbuf_reset(key
->cert
->extensions
);
1802 ret
= SSH_ERR_INVALID_FORMAT
;
1807 /* Parse CA key and check signature */
1808 if (sshkey_from_blob_internal(ca
, &key
->cert
->signature_key
, 0) != 0) {
1809 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1812 if (!sshkey_type_is_valid_ca(key
->cert
->signature_key
->type
)) {
1813 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1816 if ((ret
= sshkey_verify(key
->cert
->signature_key
, sig
, slen
,
1817 sshbuf_ptr(key
->cert
->certblob
), signed_len
, 0)) != 0)
1826 sshbuf_free(principals
);
1832 sshkey_from_blob_internal(struct sshbuf
*b
, struct sshkey
**keyp
,
1835 int type
, ret
= SSH_ERR_INTERNAL_ERROR
;
1836 char *ktype
= NULL
, *curve
= NULL
;
1837 struct sshkey
*key
= NULL
;
1840 struct sshbuf
*copy
;
1841 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
1843 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
1845 #ifdef DEBUG_PK /* XXX */
1846 sshbuf_dump(b
, stderr
);
1850 if ((copy
= sshbuf_fromb(b
)) == NULL
) {
1851 ret
= SSH_ERR_ALLOC_FAIL
;
1854 if (sshbuf_get_cstring(b
, &ktype
, NULL
) != 0) {
1855 ret
= SSH_ERR_INVALID_FORMAT
;
1859 type
= sshkey_type_from_name(ktype
);
1860 if (!allow_cert
&& sshkey_type_is_cert(type
)) {
1861 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1868 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1869 ret
= SSH_ERR_INVALID_FORMAT
;
1874 if ((key
= sshkey_new(type
)) == NULL
) {
1875 ret
= SSH_ERR_ALLOC_FAIL
;
1878 if (sshbuf_get_bignum2(b
, key
->rsa
->e
) != 0 ||
1879 sshbuf_get_bignum2(b
, key
->rsa
->n
) != 0) {
1880 ret
= SSH_ERR_INVALID_FORMAT
;
1883 if (BN_num_bits(key
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
) {
1884 ret
= SSH_ERR_KEY_LENGTH
;
1888 RSA_print_fp(stderr
, key
->rsa
, 8);
1893 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1894 ret
= SSH_ERR_INVALID_FORMAT
;
1899 if ((key
= sshkey_new(type
)) == NULL
) {
1900 ret
= SSH_ERR_ALLOC_FAIL
;
1903 if (sshbuf_get_bignum2(b
, key
->dsa
->p
) != 0 ||
1904 sshbuf_get_bignum2(b
, key
->dsa
->q
) != 0 ||
1905 sshbuf_get_bignum2(b
, key
->dsa
->g
) != 0 ||
1906 sshbuf_get_bignum2(b
, key
->dsa
->pub_key
) != 0) {
1907 ret
= SSH_ERR_INVALID_FORMAT
;
1911 DSA_print_fp(stderr
, key
->dsa
, 8);
1914 case KEY_ECDSA_CERT
:
1916 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1917 ret
= SSH_ERR_INVALID_FORMAT
;
1921 # ifdef OPENSSL_HAS_ECC
1923 if ((key
= sshkey_new(type
)) == NULL
) {
1924 ret
= SSH_ERR_ALLOC_FAIL
;
1927 key
->ecdsa_nid
= sshkey_ecdsa_nid_from_name(ktype
);
1928 if (sshbuf_get_cstring(b
, &curve
, NULL
) != 0) {
1929 ret
= SSH_ERR_INVALID_FORMAT
;
1932 if (key
->ecdsa_nid
!= sshkey_curve_name_to_nid(curve
)) {
1933 ret
= SSH_ERR_EC_CURVE_MISMATCH
;
1936 if (key
->ecdsa
!= NULL
)
1937 EC_KEY_free(key
->ecdsa
);
1938 if ((key
->ecdsa
= EC_KEY_new_by_curve_name(key
->ecdsa_nid
))
1940 ret
= SSH_ERR_EC_CURVE_INVALID
;
1943 if ((q
= EC_POINT_new(EC_KEY_get0_group(key
->ecdsa
))) == NULL
) {
1944 ret
= SSH_ERR_ALLOC_FAIL
;
1947 if (sshbuf_get_ec(b
, q
, EC_KEY_get0_group(key
->ecdsa
)) != 0) {
1948 ret
= SSH_ERR_INVALID_FORMAT
;
1951 if (sshkey_ec_validate_public(EC_KEY_get0_group(key
->ecdsa
),
1953 ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
1956 if (EC_KEY_set_public_key(key
->ecdsa
, q
) != 1) {
1957 /* XXX assume it is a allocation error */
1958 ret
= SSH_ERR_ALLOC_FAIL
;
1962 sshkey_dump_ec_point(EC_KEY_get0_group(key
->ecdsa
), q
);
1965 # endif /* OPENSSL_HAS_ECC */
1966 #endif /* WITH_OPENSSL */
1967 case KEY_ED25519_CERT
:
1969 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1970 ret
= SSH_ERR_INVALID_FORMAT
;
1975 if ((ret
= sshbuf_get_string(b
, &pk
, &len
)) != 0)
1977 if (len
!= ED25519_PK_SZ
) {
1978 ret
= SSH_ERR_INVALID_FORMAT
;
1981 if ((key
= sshkey_new(type
)) == NULL
) {
1982 ret
= SSH_ERR_ALLOC_FAIL
;
1985 key
->ed25519_pk
= pk
;
1990 ret
= SSH_ERR_KEY_TYPE_UNKNOWN
;
1994 /* Parse certificate potion */
1995 if (sshkey_is_cert(key
) && (ret
= cert_parse(b
, key
, copy
)) != 0)
1998 if (key
!= NULL
&& sshbuf_len(b
) != 0) {
1999 ret
= SSH_ERR_INVALID_FORMAT
;
2013 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2016 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2021 sshkey_from_blob(const u_char
*blob
, size_t blen
, struct sshkey
**keyp
)
2026 if ((b
= sshbuf_from(blob
, blen
)) == NULL
)
2027 return SSH_ERR_ALLOC_FAIL
;
2028 r
= sshkey_from_blob_internal(b
, keyp
, 1);
2034 sshkey_fromb(struct sshbuf
*b
, struct sshkey
**keyp
)
2036 return sshkey_from_blob_internal(b
, keyp
, 1);
2040 sshkey_froms(struct sshbuf
*buf
, struct sshkey
**keyp
)
2045 if ((r
= sshbuf_froms(buf
, &b
)) != 0)
2047 r
= sshkey_from_blob_internal(b
, keyp
, 1);
2053 sshkey_sign(const struct sshkey
*key
,
2054 u_char
**sigp
, size_t *lenp
,
2055 const u_char
*data
, size_t datalen
, const char *alg
, u_int compat
)
2061 if (datalen
> SSH_KEY_MAX_SIGN_DATA_SIZE
)
2062 return SSH_ERR_INVALID_ARGUMENT
;
2063 switch (key
->type
) {
2067 return ssh_dss_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2068 # ifdef OPENSSL_HAS_ECC
2069 case KEY_ECDSA_CERT
:
2071 return ssh_ecdsa_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2072 # endif /* OPENSSL_HAS_ECC */
2075 return ssh_rsa_sign(key
, sigp
, lenp
, data
, datalen
, alg
);
2076 #endif /* WITH_OPENSSL */
2078 case KEY_ED25519_CERT
:
2079 return ssh_ed25519_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2081 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2086 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2089 sshkey_verify(const struct sshkey
*key
,
2090 const u_char
*sig
, size_t siglen
,
2091 const u_char
*data
, size_t dlen
, u_int compat
)
2093 if (siglen
== 0 || dlen
> SSH_KEY_MAX_SIGN_DATA_SIZE
)
2094 return SSH_ERR_INVALID_ARGUMENT
;
2095 switch (key
->type
) {
2099 return ssh_dss_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2100 # ifdef OPENSSL_HAS_ECC
2101 case KEY_ECDSA_CERT
:
2103 return ssh_ecdsa_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2104 # endif /* OPENSSL_HAS_ECC */
2107 return ssh_rsa_verify(key
, sig
, siglen
, data
, dlen
);
2108 #endif /* WITH_OPENSSL */
2110 case KEY_ED25519_CERT
:
2111 return ssh_ed25519_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2113 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2117 /* Converts a private to a public key */
2119 sshkey_demote(const struct sshkey
*k
, struct sshkey
**dkp
)
2122 int ret
= SSH_ERR_INTERNAL_ERROR
;
2125 if ((pk
= calloc(1, sizeof(*pk
))) == NULL
)
2126 return SSH_ERR_ALLOC_FAIL
;
2128 pk
->flags
= k
->flags
;
2129 pk
->ecdsa_nid
= k
->ecdsa_nid
;
2133 pk
->ed25519_pk
= NULL
;
2134 pk
->ed25519_sk
= NULL
;
2139 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2143 if ((pk
->rsa
= RSA_new()) == NULL
||
2144 (pk
->rsa
->e
= BN_dup(k
->rsa
->e
)) == NULL
||
2145 (pk
->rsa
->n
= BN_dup(k
->rsa
->n
)) == NULL
) {
2146 ret
= SSH_ERR_ALLOC_FAIL
;
2151 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2155 if ((pk
->dsa
= DSA_new()) == NULL
||
2156 (pk
->dsa
->p
= BN_dup(k
->dsa
->p
)) == NULL
||
2157 (pk
->dsa
->q
= BN_dup(k
->dsa
->q
)) == NULL
||
2158 (pk
->dsa
->g
= BN_dup(k
->dsa
->g
)) == NULL
||
2159 (pk
->dsa
->pub_key
= BN_dup(k
->dsa
->pub_key
)) == NULL
) {
2160 ret
= SSH_ERR_ALLOC_FAIL
;
2164 case KEY_ECDSA_CERT
:
2165 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2168 # ifdef OPENSSL_HAS_ECC
2170 pk
->ecdsa
= EC_KEY_new_by_curve_name(pk
->ecdsa_nid
);
2171 if (pk
->ecdsa
== NULL
) {
2172 ret
= SSH_ERR_ALLOC_FAIL
;
2175 if (EC_KEY_set_public_key(pk
->ecdsa
,
2176 EC_KEY_get0_public_key(k
->ecdsa
)) != 1) {
2177 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2181 # endif /* OPENSSL_HAS_ECC */
2182 #endif /* WITH_OPENSSL */
2183 case KEY_ED25519_CERT
:
2184 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2188 if (k
->ed25519_pk
!= NULL
) {
2189 if ((pk
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
) {
2190 ret
= SSH_ERR_ALLOC_FAIL
;
2193 memcpy(pk
->ed25519_pk
, k
->ed25519_pk
, ED25519_PK_SZ
);
2197 ret
= SSH_ERR_KEY_TYPE_UNKNOWN
;
2206 /* Convert a plain key to their _CERT equivalent */
2208 sshkey_to_certified(struct sshkey
*k
)
2215 newtype
= KEY_RSA_CERT
;
2218 newtype
= KEY_DSA_CERT
;
2221 newtype
= KEY_ECDSA_CERT
;
2223 #endif /* WITH_OPENSSL */
2225 newtype
= KEY_ED25519_CERT
;
2228 return SSH_ERR_INVALID_ARGUMENT
;
2230 if ((k
->cert
= cert_new()) == NULL
)
2231 return SSH_ERR_ALLOC_FAIL
;
2236 /* Convert a certificate to its raw key equivalent */
2238 sshkey_drop_cert(struct sshkey
*k
)
2240 if (!sshkey_type_is_cert(k
->type
))
2241 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2244 k
->type
= sshkey_type_plain(k
->type
);
2248 /* Sign a certified key, (re-)generating the signed certblob. */
2250 sshkey_certify_custom(struct sshkey
*k
, struct sshkey
*ca
, const char *alg
,
2251 sshkey_certify_signer
*signer
, void *signer_ctx
)
2253 struct sshbuf
*principals
= NULL
;
2254 u_char
*ca_blob
= NULL
, *sig_blob
= NULL
, nonce
[32];
2255 size_t i
, ca_len
, sig_len
;
2256 int ret
= SSH_ERR_INTERNAL_ERROR
;
2257 struct sshbuf
*cert
;
2259 if (k
== NULL
|| k
->cert
== NULL
||
2260 k
->cert
->certblob
== NULL
|| ca
== NULL
)
2261 return SSH_ERR_INVALID_ARGUMENT
;
2262 if (!sshkey_is_cert(k
))
2263 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2264 if (!sshkey_type_is_valid_ca(ca
->type
))
2265 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
2267 if ((ret
= sshkey_to_blob(ca
, &ca_blob
, &ca_len
)) != 0)
2268 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
2270 cert
= k
->cert
->certblob
; /* for readability */
2272 if ((ret
= sshbuf_put_cstring(cert
, sshkey_ssh_name(k
))) != 0)
2275 /* -v01 certs put nonce first */
2276 arc4random_buf(&nonce
, sizeof(nonce
));
2277 if ((ret
= sshbuf_put_string(cert
, nonce
, sizeof(nonce
))) != 0)
2280 /* XXX this substantially duplicates to_blob(); refactor */
2284 if ((ret
= sshbuf_put_bignum2(cert
, k
->dsa
->p
)) != 0 ||
2285 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->q
)) != 0 ||
2286 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->g
)) != 0 ||
2287 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->pub_key
)) != 0)
2290 # ifdef OPENSSL_HAS_ECC
2291 case KEY_ECDSA_CERT
:
2292 if ((ret
= sshbuf_put_cstring(cert
,
2293 sshkey_curve_nid_to_name(k
->ecdsa_nid
))) != 0 ||
2294 (ret
= sshbuf_put_ec(cert
,
2295 EC_KEY_get0_public_key(k
->ecdsa
),
2296 EC_KEY_get0_group(k
->ecdsa
))) != 0)
2299 # endif /* OPENSSL_HAS_ECC */
2301 if ((ret
= sshbuf_put_bignum2(cert
, k
->rsa
->e
)) != 0 ||
2302 (ret
= sshbuf_put_bignum2(cert
, k
->rsa
->n
)) != 0)
2305 #endif /* WITH_OPENSSL */
2306 case KEY_ED25519_CERT
:
2307 if ((ret
= sshbuf_put_string(cert
,
2308 k
->ed25519_pk
, ED25519_PK_SZ
)) != 0)
2312 ret
= SSH_ERR_INVALID_ARGUMENT
;
2316 if ((ret
= sshbuf_put_u64(cert
, k
->cert
->serial
)) != 0 ||
2317 (ret
= sshbuf_put_u32(cert
, k
->cert
->type
)) != 0 ||
2318 (ret
= sshbuf_put_cstring(cert
, k
->cert
->key_id
)) != 0)
2321 if ((principals
= sshbuf_new()) == NULL
) {
2322 ret
= SSH_ERR_ALLOC_FAIL
;
2325 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
2326 if ((ret
= sshbuf_put_cstring(principals
,
2327 k
->cert
->principals
[i
])) != 0)
2330 if ((ret
= sshbuf_put_stringb(cert
, principals
)) != 0 ||
2331 (ret
= sshbuf_put_u64(cert
, k
->cert
->valid_after
)) != 0 ||
2332 (ret
= sshbuf_put_u64(cert
, k
->cert
->valid_before
)) != 0 ||
2333 (ret
= sshbuf_put_stringb(cert
, k
->cert
->critical
)) != 0 ||
2334 (ret
= sshbuf_put_stringb(cert
, k
->cert
->extensions
)) != 0 ||
2335 (ret
= sshbuf_put_string(cert
, NULL
, 0)) != 0 || /* Reserved */
2336 (ret
= sshbuf_put_string(cert
, ca_blob
, ca_len
)) != 0)
2339 /* Sign the whole mess */
2340 if ((ret
= signer(ca
, &sig_blob
, &sig_len
, sshbuf_ptr(cert
),
2341 sshbuf_len(cert
), alg
, 0, signer_ctx
)) != 0)
2344 /* Append signature and we are done */
2345 if ((ret
= sshbuf_put_string(cert
, sig_blob
, sig_len
)) != 0)
2353 sshbuf_free(principals
);
2358 default_key_sign(const struct sshkey
*key
, u_char
**sigp
, size_t *lenp
,
2359 const u_char
*data
, size_t datalen
,
2360 const char *alg
, u_int compat
, void *ctx
)
2363 return SSH_ERR_INVALID_ARGUMENT
;
2364 return sshkey_sign(key
, sigp
, lenp
, data
, datalen
, alg
, compat
);
2368 sshkey_certify(struct sshkey
*k
, struct sshkey
*ca
, const char *alg
)
2370 return sshkey_certify_custom(k
, ca
, alg
, default_key_sign
, NULL
);
2374 sshkey_cert_check_authority(const struct sshkey
*k
,
2375 int want_host
, int require_principal
,
2376 const char *name
, const char **reason
)
2378 u_int i
, principal_matches
;
2379 time_t now
= time(NULL
);
2385 if (k
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
2386 *reason
= "Certificate invalid: not a host certificate";
2387 return SSH_ERR_KEY_CERT_INVALID
;
2390 if (k
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
2391 *reason
= "Certificate invalid: not a user certificate";
2392 return SSH_ERR_KEY_CERT_INVALID
;
2396 /* yikes - system clock before epoch! */
2397 *reason
= "Certificate invalid: not yet valid";
2398 return SSH_ERR_KEY_CERT_INVALID
;
2400 if ((u_int64_t
)now
< k
->cert
->valid_after
) {
2401 *reason
= "Certificate invalid: not yet valid";
2402 return SSH_ERR_KEY_CERT_INVALID
;
2404 if ((u_int64_t
)now
>= k
->cert
->valid_before
) {
2405 *reason
= "Certificate invalid: expired";
2406 return SSH_ERR_KEY_CERT_INVALID
;
2408 if (k
->cert
->nprincipals
== 0) {
2409 if (require_principal
) {
2410 *reason
= "Certificate lacks principal list";
2411 return SSH_ERR_KEY_CERT_INVALID
;
2413 } else if (name
!= NULL
) {
2414 principal_matches
= 0;
2415 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
2416 if (strcmp(name
, k
->cert
->principals
[i
]) == 0) {
2417 principal_matches
= 1;
2421 if (!principal_matches
) {
2422 *reason
= "Certificate invalid: name is not a listed "
2424 return SSH_ERR_KEY_CERT_INVALID
;
2431 sshkey_format_cert_validity(const struct sshkey_cert
*cert
, char *s
, size_t l
)
2433 char from
[32], to
[32], ret
[64];
2438 if (cert
->valid_after
== 0 &&
2439 cert
->valid_before
== 0xffffffffffffffffULL
)
2440 return strlcpy(s
, "forever", l
);
2442 if (cert
->valid_after
!= 0) {
2443 /* XXX revisit INT_MAX in 2038 :) */
2444 tt
= cert
->valid_after
> INT_MAX
?
2445 INT_MAX
: cert
->valid_after
;
2446 tm
= localtime(&tt
);
2447 strftime(from
, sizeof(from
), "%Y-%m-%dT%H:%M:%S", tm
);
2449 if (cert
->valid_before
!= 0xffffffffffffffffULL
) {
2450 /* XXX revisit INT_MAX in 2038 :) */
2451 tt
= cert
->valid_before
> INT_MAX
?
2452 INT_MAX
: cert
->valid_before
;
2453 tm
= localtime(&tt
);
2454 strftime(to
, sizeof(to
), "%Y-%m-%dT%H:%M:%S", tm
);
2457 if (cert
->valid_after
== 0)
2458 snprintf(ret
, sizeof(ret
), "before %s", to
);
2459 else if (cert
->valid_before
== 0xffffffffffffffffULL
)
2460 snprintf(ret
, sizeof(ret
), "after %s", from
);
2462 snprintf(ret
, sizeof(ret
), "from %s to %s", from
, to
);
2464 return strlcpy(s
, ret
, l
);
2468 sshkey_private_serialize(const struct sshkey
*key
, struct sshbuf
*b
)
2470 int r
= SSH_ERR_INTERNAL_ERROR
;
2472 if ((r
= sshbuf_put_cstring(b
, sshkey_ssh_name(key
))) != 0)
2474 switch (key
->type
) {
2477 if ((r
= sshbuf_put_bignum2(b
, key
->rsa
->n
)) != 0 ||
2478 (r
= sshbuf_put_bignum2(b
, key
->rsa
->e
)) != 0 ||
2479 (r
= sshbuf_put_bignum2(b
, key
->rsa
->d
)) != 0 ||
2480 (r
= sshbuf_put_bignum2(b
, key
->rsa
->iqmp
)) != 0 ||
2481 (r
= sshbuf_put_bignum2(b
, key
->rsa
->p
)) != 0 ||
2482 (r
= sshbuf_put_bignum2(b
, key
->rsa
->q
)) != 0)
2486 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2487 r
= SSH_ERR_INVALID_ARGUMENT
;
2490 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2491 (r
= sshbuf_put_bignum2(b
, key
->rsa
->d
)) != 0 ||
2492 (r
= sshbuf_put_bignum2(b
, key
->rsa
->iqmp
)) != 0 ||
2493 (r
= sshbuf_put_bignum2(b
, key
->rsa
->p
)) != 0 ||
2494 (r
= sshbuf_put_bignum2(b
, key
->rsa
->q
)) != 0)
2498 if ((r
= sshbuf_put_bignum2(b
, key
->dsa
->p
)) != 0 ||
2499 (r
= sshbuf_put_bignum2(b
, key
->dsa
->q
)) != 0 ||
2500 (r
= sshbuf_put_bignum2(b
, key
->dsa
->g
)) != 0 ||
2501 (r
= sshbuf_put_bignum2(b
, key
->dsa
->pub_key
)) != 0 ||
2502 (r
= sshbuf_put_bignum2(b
, key
->dsa
->priv_key
)) != 0)
2506 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2507 r
= SSH_ERR_INVALID_ARGUMENT
;
2510 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2511 (r
= sshbuf_put_bignum2(b
, key
->dsa
->priv_key
)) != 0)
2514 # ifdef OPENSSL_HAS_ECC
2516 if ((r
= sshbuf_put_cstring(b
,
2517 sshkey_curve_nid_to_name(key
->ecdsa_nid
))) != 0 ||
2518 (r
= sshbuf_put_eckey(b
, key
->ecdsa
)) != 0 ||
2519 (r
= sshbuf_put_bignum2(b
,
2520 EC_KEY_get0_private_key(key
->ecdsa
))) != 0)
2523 case KEY_ECDSA_CERT
:
2524 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2525 r
= SSH_ERR_INVALID_ARGUMENT
;
2528 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2529 (r
= sshbuf_put_bignum2(b
,
2530 EC_KEY_get0_private_key(key
->ecdsa
))) != 0)
2533 # endif /* OPENSSL_HAS_ECC */
2534 #endif /* WITH_OPENSSL */
2536 if ((r
= sshbuf_put_string(b
, key
->ed25519_pk
,
2537 ED25519_PK_SZ
)) != 0 ||
2538 (r
= sshbuf_put_string(b
, key
->ed25519_sk
,
2539 ED25519_SK_SZ
)) != 0)
2542 case KEY_ED25519_CERT
:
2543 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2544 r
= SSH_ERR_INVALID_ARGUMENT
;
2547 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2548 (r
= sshbuf_put_string(b
, key
->ed25519_pk
,
2549 ED25519_PK_SZ
)) != 0 ||
2550 (r
= sshbuf_put_string(b
, key
->ed25519_sk
,
2551 ED25519_SK_SZ
)) != 0)
2555 r
= SSH_ERR_INVALID_ARGUMENT
;
2565 sshkey_private_deserialize(struct sshbuf
*buf
, struct sshkey
**kp
)
2567 char *tname
= NULL
, *curve
= NULL
;
2568 struct sshkey
*k
= NULL
;
2569 size_t pklen
= 0, sklen
= 0;
2570 int type
, r
= SSH_ERR_INTERNAL_ERROR
;
2571 u_char
*ed25519_pk
= NULL
, *ed25519_sk
= NULL
;
2573 BIGNUM
*exponent
= NULL
;
2574 #endif /* WITH_OPENSSL */
2578 if ((r
= sshbuf_get_cstring(buf
, &tname
, NULL
)) != 0)
2580 type
= sshkey_type_from_name(tname
);
2584 if ((k
= sshkey_new_private(type
)) == NULL
) {
2585 r
= SSH_ERR_ALLOC_FAIL
;
2588 if ((r
= sshbuf_get_bignum2(buf
, k
->dsa
->p
)) != 0 ||
2589 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->q
)) != 0 ||
2590 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->g
)) != 0 ||
2591 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->pub_key
)) != 0 ||
2592 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->priv_key
)) != 0)
2596 if ((r
= sshkey_froms(buf
, &k
)) != 0 ||
2597 (r
= sshkey_add_private(k
)) != 0 ||
2598 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->priv_key
)) != 0)
2601 # ifdef OPENSSL_HAS_ECC
2603 if ((k
= sshkey_new_private(type
)) == NULL
) {
2604 r
= SSH_ERR_ALLOC_FAIL
;
2607 if ((k
->ecdsa_nid
= sshkey_ecdsa_nid_from_name(tname
)) == -1) {
2608 r
= SSH_ERR_INVALID_ARGUMENT
;
2611 if ((r
= sshbuf_get_cstring(buf
, &curve
, NULL
)) != 0)
2613 if (k
->ecdsa_nid
!= sshkey_curve_name_to_nid(curve
)) {
2614 r
= SSH_ERR_EC_CURVE_MISMATCH
;
2617 k
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
);
2618 if (k
->ecdsa
== NULL
|| (exponent
= BN_new()) == NULL
) {
2619 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2622 if ((r
= sshbuf_get_eckey(buf
, k
->ecdsa
)) != 0 ||
2623 (r
= sshbuf_get_bignum2(buf
, exponent
)))
2625 if (EC_KEY_set_private_key(k
->ecdsa
, exponent
) != 1) {
2626 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2629 if ((r
= sshkey_ec_validate_public(EC_KEY_get0_group(k
->ecdsa
),
2630 EC_KEY_get0_public_key(k
->ecdsa
))) != 0 ||
2631 (r
= sshkey_ec_validate_private(k
->ecdsa
)) != 0)
2634 case KEY_ECDSA_CERT
:
2635 if ((exponent
= BN_new()) == NULL
) {
2636 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2639 if ((r
= sshkey_froms(buf
, &k
)) != 0 ||
2640 (r
= sshkey_add_private(k
)) != 0 ||
2641 (r
= sshbuf_get_bignum2(buf
, exponent
)) != 0)
2643 if (EC_KEY_set_private_key(k
->ecdsa
, exponent
) != 1) {
2644 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2647 if ((r
= sshkey_ec_validate_public(EC_KEY_get0_group(k
->ecdsa
),
2648 EC_KEY_get0_public_key(k
->ecdsa
))) != 0 ||
2649 (r
= sshkey_ec_validate_private(k
->ecdsa
)) != 0)
2652 # endif /* OPENSSL_HAS_ECC */
2654 if ((k
= sshkey_new_private(type
)) == NULL
) {
2655 r
= SSH_ERR_ALLOC_FAIL
;
2658 if ((r
= sshbuf_get_bignum2(buf
, k
->rsa
->n
)) != 0 ||
2659 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->e
)) != 0 ||
2660 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->d
)) != 0 ||
2661 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->iqmp
)) != 0 ||
2662 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->p
)) != 0 ||
2663 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->q
)) != 0 ||
2664 (r
= ssh_rsa_generate_additional_parameters(k
)) != 0)
2666 if (BN_num_bits(k
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
) {
2667 r
= SSH_ERR_KEY_LENGTH
;
2672 if ((r
= sshkey_froms(buf
, &k
)) != 0 ||
2673 (r
= sshkey_add_private(k
)) != 0 ||
2674 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->d
)) != 0 ||
2675 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->iqmp
)) != 0 ||
2676 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->p
)) != 0 ||
2677 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->q
)) != 0 ||
2678 (r
= ssh_rsa_generate_additional_parameters(k
)) != 0)
2680 if (BN_num_bits(k
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
) {
2681 r
= SSH_ERR_KEY_LENGTH
;
2685 #endif /* WITH_OPENSSL */
2687 if ((k
= sshkey_new_private(type
)) == NULL
) {
2688 r
= SSH_ERR_ALLOC_FAIL
;
2691 if ((r
= sshbuf_get_string(buf
, &ed25519_pk
, &pklen
)) != 0 ||
2692 (r
= sshbuf_get_string(buf
, &ed25519_sk
, &sklen
)) != 0)
2694 if (pklen
!= ED25519_PK_SZ
|| sklen
!= ED25519_SK_SZ
) {
2695 r
= SSH_ERR_INVALID_FORMAT
;
2698 k
->ed25519_pk
= ed25519_pk
;
2699 k
->ed25519_sk
= ed25519_sk
;
2700 ed25519_pk
= ed25519_sk
= NULL
;
2702 case KEY_ED25519_CERT
:
2703 if ((r
= sshkey_froms(buf
, &k
)) != 0 ||
2704 (r
= sshkey_add_private(k
)) != 0 ||
2705 (r
= sshbuf_get_string(buf
, &ed25519_pk
, &pklen
)) != 0 ||
2706 (r
= sshbuf_get_string(buf
, &ed25519_sk
, &sklen
)) != 0)
2708 if (pklen
!= ED25519_PK_SZ
|| sklen
!= ED25519_SK_SZ
) {
2709 r
= SSH_ERR_INVALID_FORMAT
;
2712 k
->ed25519_pk
= ed25519_pk
;
2713 k
->ed25519_sk
= ed25519_sk
;
2714 ed25519_pk
= ed25519_sk
= NULL
;
2717 r
= SSH_ERR_KEY_TYPE_UNKNOWN
;
2721 /* enable blinding */
2725 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
2726 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2731 #endif /* WITH_OPENSSL */
2742 if (exponent
!= NULL
)
2743 BN_clear_free(exponent
);
2744 #endif /* WITH_OPENSSL */
2746 if (ed25519_pk
!= NULL
) {
2747 explicit_bzero(ed25519_pk
, pklen
);
2750 if (ed25519_sk
!= NULL
) {
2751 explicit_bzero(ed25519_sk
, sklen
);
2757 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2759 sshkey_ec_validate_public(const EC_GROUP
*group
, const EC_POINT
*public)
2762 EC_POINT
*nq
= NULL
;
2763 BIGNUM
*order
, *x
, *y
, *tmp
;
2764 int ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
2767 * NB. This assumes OpenSSL has already verified that the public
2768 * point lies on the curve. This is done by EC_POINT_oct2point()
2769 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
2770 * reachable with public points not unmarshalled using
2771 * EC_POINT_oct2point then the caller will need to explicitly check.
2774 if ((bnctx
= BN_CTX_new()) == NULL
)
2775 return SSH_ERR_ALLOC_FAIL
;
2776 BN_CTX_start(bnctx
);
2779 * We shouldn't ever hit this case because bignum_get_ecpoint()
2780 * refuses to load GF2m points.
2782 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2783 NID_X9_62_prime_field
)
2787 if (EC_POINT_is_at_infinity(group
, public))
2790 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2791 (y
= BN_CTX_get(bnctx
)) == NULL
||
2792 (order
= BN_CTX_get(bnctx
)) == NULL
||
2793 (tmp
= BN_CTX_get(bnctx
)) == NULL
) {
2794 ret
= SSH_ERR_ALLOC_FAIL
;
2798 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2799 if (EC_GROUP_get_order(group
, order
, bnctx
) != 1 ||
2800 EC_POINT_get_affine_coordinates_GFp(group
, public,
2801 x
, y
, bnctx
) != 1) {
2802 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2805 if (BN_num_bits(x
) <= BN_num_bits(order
) / 2 ||
2806 BN_num_bits(y
) <= BN_num_bits(order
) / 2)
2809 /* nQ == infinity (n == order of subgroup) */
2810 if ((nq
= EC_POINT_new(group
)) == NULL
) {
2811 ret
= SSH_ERR_ALLOC_FAIL
;
2814 if (EC_POINT_mul(group
, nq
, NULL
, public, order
, bnctx
) != 1) {
2815 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2818 if (EC_POINT_is_at_infinity(group
, nq
) != 1)
2821 /* x < order - 1, y < order - 1 */
2822 if (!BN_sub(tmp
, order
, BN_value_one())) {
2823 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2826 if (BN_cmp(x
, tmp
) >= 0 || BN_cmp(y
, tmp
) >= 0)
2837 sshkey_ec_validate_private(const EC_KEY
*key
)
2840 BIGNUM
*order
, *tmp
;
2841 int ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
2843 if ((bnctx
= BN_CTX_new()) == NULL
)
2844 return SSH_ERR_ALLOC_FAIL
;
2845 BN_CTX_start(bnctx
);
2847 if ((order
= BN_CTX_get(bnctx
)) == NULL
||
2848 (tmp
= BN_CTX_get(bnctx
)) == NULL
) {
2849 ret
= SSH_ERR_ALLOC_FAIL
;
2853 /* log2(private) > log2(order)/2 */
2854 if (EC_GROUP_get_order(EC_KEY_get0_group(key
), order
, bnctx
) != 1) {
2855 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2858 if (BN_num_bits(EC_KEY_get0_private_key(key
)) <=
2859 BN_num_bits(order
) / 2)
2862 /* private < order - 1 */
2863 if (!BN_sub(tmp
, order
, BN_value_one())) {
2864 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2867 if (BN_cmp(EC_KEY_get0_private_key(key
), tmp
) >= 0)
2876 sshkey_dump_ec_point(const EC_GROUP
*group
, const EC_POINT
*point
)
2881 if (point
== NULL
) {
2882 fputs("point=(NULL)\n", stderr
);
2885 if ((bnctx
= BN_CTX_new()) == NULL
) {
2886 fprintf(stderr
, "%s: BN_CTX_new failed\n", __func__
);
2889 BN_CTX_start(bnctx
);
2890 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2891 (y
= BN_CTX_get(bnctx
)) == NULL
) {
2892 fprintf(stderr
, "%s: BN_CTX_get failed\n", __func__
);
2895 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2896 NID_X9_62_prime_field
) {
2897 fprintf(stderr
, "%s: group is not a prime field\n", __func__
);
2900 if (EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
,
2902 fprintf(stderr
, "%s: EC_POINT_get_affine_coordinates_GFp\n",
2906 fputs("x=", stderr
);
2907 BN_print_fp(stderr
, x
);
2908 fputs("\ny=", stderr
);
2909 BN_print_fp(stderr
, y
);
2910 fputs("\n", stderr
);
2915 sshkey_dump_ec_key(const EC_KEY
*key
)
2917 const BIGNUM
*exponent
;
2919 sshkey_dump_ec_point(EC_KEY_get0_group(key
),
2920 EC_KEY_get0_public_key(key
));
2921 fputs("exponent=", stderr
);
2922 if ((exponent
= EC_KEY_get0_private_key(key
)) == NULL
)
2923 fputs("(NULL)", stderr
);
2925 BN_print_fp(stderr
, EC_KEY_get0_private_key(key
));
2926 fputs("\n", stderr
);
2928 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2931 sshkey_private_to_blob2(const struct sshkey
*prv
, struct sshbuf
*blob
,
2932 const char *passphrase
, const char *comment
, const char *ciphername
,
2935 u_char
*cp
, *key
= NULL
, *pubkeyblob
= NULL
;
2936 u_char salt
[SALT_LEN
];
2938 size_t i
, pubkeylen
, keylen
, ivlen
, blocksize
, authlen
;
2940 int r
= SSH_ERR_INTERNAL_ERROR
;
2941 struct sshcipher_ctx
*ciphercontext
= NULL
;
2942 const struct sshcipher
*cipher
;
2943 const char *kdfname
= KDFNAME
;
2944 struct sshbuf
*encoded
= NULL
, *encrypted
= NULL
, *kdf
= NULL
;
2947 rounds
= DEFAULT_ROUNDS
;
2948 if (passphrase
== NULL
|| !strlen(passphrase
)) {
2949 ciphername
= "none";
2951 } else if (ciphername
== NULL
)
2952 ciphername
= DEFAULT_CIPHERNAME
;
2953 if ((cipher
= cipher_by_name(ciphername
)) == NULL
) {
2954 r
= SSH_ERR_INVALID_ARGUMENT
;
2958 if ((kdf
= sshbuf_new()) == NULL
||
2959 (encoded
= sshbuf_new()) == NULL
||
2960 (encrypted
= sshbuf_new()) == NULL
) {
2961 r
= SSH_ERR_ALLOC_FAIL
;
2964 blocksize
= cipher_blocksize(cipher
);
2965 keylen
= cipher_keylen(cipher
);
2966 ivlen
= cipher_ivlen(cipher
);
2967 authlen
= cipher_authlen(cipher
);
2968 if ((key
= calloc(1, keylen
+ ivlen
)) == NULL
) {
2969 r
= SSH_ERR_ALLOC_FAIL
;
2972 if (strcmp(kdfname
, "bcrypt") == 0) {
2973 arc4random_buf(salt
, SALT_LEN
);
2974 if (bcrypt_pbkdf(passphrase
, strlen(passphrase
),
2975 salt
, SALT_LEN
, key
, keylen
+ ivlen
, rounds
) < 0) {
2976 r
= SSH_ERR_INVALID_ARGUMENT
;
2979 if ((r
= sshbuf_put_string(kdf
, salt
, SALT_LEN
)) != 0 ||
2980 (r
= sshbuf_put_u32(kdf
, rounds
)) != 0)
2982 } else if (strcmp(kdfname
, "none") != 0) {
2983 /* Unsupported KDF type */
2984 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
2987 if ((r
= cipher_init(&ciphercontext
, cipher
, key
, keylen
,
2988 key
+ keylen
, ivlen
, 1)) != 0)
2991 if ((r
= sshbuf_put(encoded
, AUTH_MAGIC
, sizeof(AUTH_MAGIC
))) != 0 ||
2992 (r
= sshbuf_put_cstring(encoded
, ciphername
)) != 0 ||
2993 (r
= sshbuf_put_cstring(encoded
, kdfname
)) != 0 ||
2994 (r
= sshbuf_put_stringb(encoded
, kdf
)) != 0 ||
2995 (r
= sshbuf_put_u32(encoded
, 1)) != 0 || /* number of keys */
2996 (r
= sshkey_to_blob(prv
, &pubkeyblob
, &pubkeylen
)) != 0 ||
2997 (r
= sshbuf_put_string(encoded
, pubkeyblob
, pubkeylen
)) != 0)
3000 /* set up the buffer that will be encrypted */
3002 /* Random check bytes */
3003 check
= arc4random();
3004 if ((r
= sshbuf_put_u32(encrypted
, check
)) != 0 ||
3005 (r
= sshbuf_put_u32(encrypted
, check
)) != 0)
3008 /* append private key and comment*/
3009 if ((r
= sshkey_private_serialize(prv
, encrypted
)) != 0 ||
3010 (r
= sshbuf_put_cstring(encrypted
, comment
)) != 0)
3015 while (sshbuf_len(encrypted
) % blocksize
) {
3016 if ((r
= sshbuf_put_u8(encrypted
, ++i
& 0xff)) != 0)
3020 /* length in destination buffer */
3021 if ((r
= sshbuf_put_u32(encoded
, sshbuf_len(encrypted
))) != 0)
3025 if ((r
= sshbuf_reserve(encoded
,
3026 sshbuf_len(encrypted
) + authlen
, &cp
)) != 0)
3028 if ((r
= cipher_crypt(ciphercontext
, 0, cp
,
3029 sshbuf_ptr(encrypted
), sshbuf_len(encrypted
), 0, authlen
)) != 0)
3033 if ((b64
= sshbuf_dtob64(encoded
)) == NULL
) {
3034 r
= SSH_ERR_ALLOC_FAIL
;
3039 if ((r
= sshbuf_put(blob
, MARK_BEGIN
, MARK_BEGIN_LEN
)) != 0)
3041 for (i
= 0; i
< strlen(b64
); i
++) {
3042 if ((r
= sshbuf_put_u8(blob
, b64
[i
])) != 0)
3044 /* insert line breaks */
3045 if (i
% 70 == 69 && (r
= sshbuf_put_u8(blob
, '\n')) != 0)
3048 if (i
% 70 != 69 && (r
= sshbuf_put_u8(blob
, '\n')) != 0)
3050 if ((r
= sshbuf_put(blob
, MARK_END
, MARK_END_LEN
)) != 0)
3058 sshbuf_free(encoded
);
3059 sshbuf_free(encrypted
);
3060 cipher_free(ciphercontext
);
3061 explicit_bzero(salt
, sizeof(salt
));
3063 explicit_bzero(key
, keylen
+ ivlen
);
3066 if (pubkeyblob
!= NULL
) {
3067 explicit_bzero(pubkeyblob
, pubkeylen
);
3071 explicit_bzero(b64
, strlen(b64
));
3078 sshkey_parse_private2(struct sshbuf
*blob
, int type
, const char *passphrase
,
3079 struct sshkey
**keyp
, char **commentp
)
3081 char *comment
= NULL
, *ciphername
= NULL
, *kdfname
= NULL
;
3082 const struct sshcipher
*cipher
= NULL
;
3084 int r
= SSH_ERR_INTERNAL_ERROR
;
3086 size_t i
, keylen
= 0, ivlen
= 0, authlen
= 0, slen
= 0;
3087 struct sshbuf
*encoded
= NULL
, *decoded
= NULL
;
3088 struct sshbuf
*kdf
= NULL
, *decrypted
= NULL
;
3089 struct sshcipher_ctx
*ciphercontext
= NULL
;
3090 struct sshkey
*k
= NULL
;
3091 u_char
*key
= NULL
, *salt
= NULL
, *dp
, pad
, last
;
3092 u_int blocksize
, rounds
, nkeys
, encrypted_len
, check1
, check2
;
3096 if (commentp
!= NULL
)
3099 if ((encoded
= sshbuf_new()) == NULL
||
3100 (decoded
= sshbuf_new()) == NULL
||
3101 (decrypted
= sshbuf_new()) == NULL
) {
3102 r
= SSH_ERR_ALLOC_FAIL
;
3106 /* check preamble */
3107 cp
= sshbuf_ptr(blob
);
3108 encoded_len
= sshbuf_len(blob
);
3109 if (encoded_len
< (MARK_BEGIN_LEN
+ MARK_END_LEN
) ||
3110 memcmp(cp
, MARK_BEGIN
, MARK_BEGIN_LEN
) != 0) {
3111 r
= SSH_ERR_INVALID_FORMAT
;
3114 cp
+= MARK_BEGIN_LEN
;
3115 encoded_len
-= MARK_BEGIN_LEN
;
3117 /* Look for end marker, removing whitespace as we go */
3118 while (encoded_len
> 0) {
3119 if (*cp
!= '\n' && *cp
!= '\r') {
3120 if ((r
= sshbuf_put_u8(encoded
, *cp
)) != 0)
3127 if (encoded_len
>= MARK_END_LEN
&&
3128 memcmp(cp
, MARK_END
, MARK_END_LEN
) == 0) {
3130 if ((r
= sshbuf_put_u8(encoded
, 0)) != 0)
3136 if (encoded_len
== 0) {
3137 r
= SSH_ERR_INVALID_FORMAT
;
3142 if ((r
= sshbuf_b64tod(decoded
, (char *)sshbuf_ptr(encoded
))) != 0)
3146 if (sshbuf_len(decoded
) < sizeof(AUTH_MAGIC
) ||
3147 memcmp(sshbuf_ptr(decoded
), AUTH_MAGIC
, sizeof(AUTH_MAGIC
))) {
3148 r
= SSH_ERR_INVALID_FORMAT
;
3151 /* parse public portion of key */
3152 if ((r
= sshbuf_consume(decoded
, sizeof(AUTH_MAGIC
))) != 0 ||
3153 (r
= sshbuf_get_cstring(decoded
, &ciphername
, NULL
)) != 0 ||
3154 (r
= sshbuf_get_cstring(decoded
, &kdfname
, NULL
)) != 0 ||
3155 (r
= sshbuf_froms(decoded
, &kdf
)) != 0 ||
3156 (r
= sshbuf_get_u32(decoded
, &nkeys
)) != 0 ||
3157 (r
= sshbuf_skip_string(decoded
)) != 0 || /* pubkey */
3158 (r
= sshbuf_get_u32(decoded
, &encrypted_len
)) != 0)
3161 if ((cipher
= cipher_by_name(ciphername
)) == NULL
) {
3162 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3165 if ((passphrase
== NULL
|| strlen(passphrase
) == 0) &&
3166 strcmp(ciphername
, "none") != 0) {
3167 /* passphrase required */
3168 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3171 if (strcmp(kdfname
, "none") != 0 && strcmp(kdfname
, "bcrypt") != 0) {
3172 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3175 if (!strcmp(kdfname
, "none") && strcmp(ciphername
, "none") != 0) {
3176 r
= SSH_ERR_INVALID_FORMAT
;
3180 /* XXX only one key supported */
3181 r
= SSH_ERR_INVALID_FORMAT
;
3185 /* check size of encrypted key blob */
3186 blocksize
= cipher_blocksize(cipher
);
3187 if (encrypted_len
< blocksize
|| (encrypted_len
% blocksize
) != 0) {
3188 r
= SSH_ERR_INVALID_FORMAT
;
3193 keylen
= cipher_keylen(cipher
);
3194 ivlen
= cipher_ivlen(cipher
);
3195 authlen
= cipher_authlen(cipher
);
3196 if ((key
= calloc(1, keylen
+ ivlen
)) == NULL
) {
3197 r
= SSH_ERR_ALLOC_FAIL
;
3200 if (strcmp(kdfname
, "bcrypt") == 0) {
3201 if ((r
= sshbuf_get_string(kdf
, &salt
, &slen
)) != 0 ||
3202 (r
= sshbuf_get_u32(kdf
, &rounds
)) != 0)
3204 if (bcrypt_pbkdf(passphrase
, strlen(passphrase
), salt
, slen
,
3205 key
, keylen
+ ivlen
, rounds
) < 0) {
3206 r
= SSH_ERR_INVALID_FORMAT
;
3211 /* check that an appropriate amount of auth data is present */
3212 if (sshbuf_len(decoded
) < encrypted_len
+ authlen
) {
3213 r
= SSH_ERR_INVALID_FORMAT
;
3217 /* decrypt private portion of key */
3218 if ((r
= sshbuf_reserve(decrypted
, encrypted_len
, &dp
)) != 0 ||
3219 (r
= cipher_init(&ciphercontext
, cipher
, key
, keylen
,
3220 key
+ keylen
, ivlen
, 0)) != 0)
3222 if ((r
= cipher_crypt(ciphercontext
, 0, dp
, sshbuf_ptr(decoded
),
3223 encrypted_len
, 0, authlen
)) != 0) {
3224 /* an integrity error here indicates an incorrect passphrase */
3225 if (r
== SSH_ERR_MAC_INVALID
)
3226 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3229 if ((r
= sshbuf_consume(decoded
, encrypted_len
+ authlen
)) != 0)
3231 /* there should be no trailing data */
3232 if (sshbuf_len(decoded
) != 0) {
3233 r
= SSH_ERR_INVALID_FORMAT
;
3237 /* check check bytes */
3238 if ((r
= sshbuf_get_u32(decrypted
, &check1
)) != 0 ||
3239 (r
= sshbuf_get_u32(decrypted
, &check2
)) != 0)
3241 if (check1
!= check2
) {
3242 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3246 /* Load the private key and comment */
3247 if ((r
= sshkey_private_deserialize(decrypted
, &k
)) != 0 ||
3248 (r
= sshbuf_get_cstring(decrypted
, &comment
, NULL
)) != 0)
3251 /* Check deterministic padding */
3253 while (sshbuf_len(decrypted
)) {
3254 if ((r
= sshbuf_get_u8(decrypted
, &pad
)) != 0)
3256 if (pad
!= (++i
& 0xff)) {
3257 r
= SSH_ERR_INVALID_FORMAT
;
3262 /* XXX decode pubkey and check against private */
3270 if (commentp
!= NULL
) {
3271 *commentp
= comment
;
3276 cipher_free(ciphercontext
);
3281 explicit_bzero(salt
, slen
);
3285 explicit_bzero(key
, keylen
+ ivlen
);
3288 sshbuf_free(encoded
);
3289 sshbuf_free(decoded
);
3291 sshbuf_free(decrypted
);
3298 /* convert SSH v2 key in OpenSSL PEM format */
3300 sshkey_private_pem_to_blob(struct sshkey
*key
, struct sshbuf
*blob
,
3301 const char *_passphrase
, const char *comment
)
3304 int blen
, len
= strlen(_passphrase
);
3305 u_char
*passphrase
= (len
> 0) ? (u_char
*)_passphrase
: NULL
;
3306 const EVP_CIPHER
*cipher
= (len
> 0) ? EVP_aes_128_cbc() : NULL
;
3310 if (len
> 0 && len
<= 4)
3311 return SSH_ERR_PASSPHRASE_TOO_SHORT
;
3312 if ((bio
= BIO_new(BIO_s_mem())) == NULL
)
3313 return SSH_ERR_ALLOC_FAIL
;
3315 switch (key
->type
) {
3317 success
= PEM_write_bio_DSAPrivateKey(bio
, key
->dsa
,
3318 cipher
, passphrase
, len
, NULL
, NULL
);
3320 #ifdef OPENSSL_HAS_ECC
3322 success
= PEM_write_bio_ECPrivateKey(bio
, key
->ecdsa
,
3323 cipher
, passphrase
, len
, NULL
, NULL
);
3327 success
= PEM_write_bio_RSAPrivateKey(bio
, key
->rsa
,
3328 cipher
, passphrase
, len
, NULL
, NULL
);
3335 r
= SSH_ERR_LIBCRYPTO_ERROR
;
3338 if ((blen
= BIO_get_mem_data(bio
, &bptr
)) <= 0) {
3339 r
= SSH_ERR_INTERNAL_ERROR
;
3342 if ((r
= sshbuf_put(blob
, bptr
, blen
)) != 0)
3349 #endif /* WITH_OPENSSL */
3351 /* Serialise "key" to buffer "blob" */
3353 sshkey_private_to_fileblob(struct sshkey
*key
, struct sshbuf
*blob
,
3354 const char *passphrase
, const char *comment
,
3355 int force_new_format
, const char *new_format_cipher
, int new_format_rounds
)
3357 switch (key
->type
) {
3362 if (force_new_format
) {
3363 return sshkey_private_to_blob2(key
, blob
, passphrase
,
3364 comment
, new_format_cipher
, new_format_rounds
);
3366 return sshkey_private_pem_to_blob(key
, blob
,
3367 passphrase
, comment
);
3368 #endif /* WITH_OPENSSL */
3370 return sshkey_private_to_blob2(key
, blob
, passphrase
,
3371 comment
, new_format_cipher
, new_format_rounds
);
3373 return SSH_ERR_KEY_TYPE_UNKNOWN
;
3380 translate_libcrypto_error(unsigned long pem_err
)
3382 int pem_reason
= ERR_GET_REASON(pem_err
);
3384 switch (ERR_GET_LIB(pem_err
)) {
3386 switch (pem_reason
) {
3387 case PEM_R_BAD_PASSWORD_READ
:
3388 case PEM_R_PROBLEMS_GETTING_PASSWORD
:
3389 case PEM_R_BAD_DECRYPT
:
3390 return SSH_ERR_KEY_WRONG_PASSPHRASE
;
3392 return SSH_ERR_INVALID_FORMAT
;
3395 switch (pem_reason
) {
3396 case EVP_R_BAD_DECRYPT
:
3397 return SSH_ERR_KEY_WRONG_PASSPHRASE
;
3398 case EVP_R_BN_DECODE_ERROR
:
3399 case EVP_R_DECODE_ERROR
:
3400 #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
3401 case EVP_R_PRIVATE_KEY_DECODE_ERROR
:
3403 return SSH_ERR_INVALID_FORMAT
;
3405 return SSH_ERR_LIBCRYPTO_ERROR
;
3408 return SSH_ERR_INVALID_FORMAT
;
3410 return SSH_ERR_LIBCRYPTO_ERROR
;
3414 clear_libcrypto_errors(void)
3416 while (ERR_get_error() != 0)
3421 * Translate OpenSSL error codes to determine whether
3422 * passphrase is required/incorrect.
3425 convert_libcrypto_error(void)
3428 * Some password errors are reported at the beginning
3429 * of the error queue.
3431 if (translate_libcrypto_error(ERR_peek_error()) ==
3432 SSH_ERR_KEY_WRONG_PASSPHRASE
)
3433 return SSH_ERR_KEY_WRONG_PASSPHRASE
;
3434 return translate_libcrypto_error(ERR_peek_last_error());
3438 sshkey_parse_private_pem_fileblob(struct sshbuf
*blob
, int type
,
3439 const char *passphrase
, struct sshkey
**keyp
)
3441 EVP_PKEY
*pk
= NULL
;
3442 struct sshkey
*prv
= NULL
;
3449 if ((bio
= BIO_new(BIO_s_mem())) == NULL
|| sshbuf_len(blob
) > INT_MAX
)
3450 return SSH_ERR_ALLOC_FAIL
;
3451 if (BIO_write(bio
, sshbuf_ptr(blob
), sshbuf_len(blob
)) !=
3452 (int)sshbuf_len(blob
)) {
3453 r
= SSH_ERR_ALLOC_FAIL
;
3457 clear_libcrypto_errors();
3458 if ((pk
= PEM_read_bio_PrivateKey(bio
, NULL
, NULL
,
3459 (char *)passphrase
)) == NULL
) {
3460 r
= convert_libcrypto_error();
3463 if (pk
->type
== EVP_PKEY_RSA
&&
3464 (type
== KEY_UNSPEC
|| type
== KEY_RSA
)) {
3465 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3466 r
= SSH_ERR_ALLOC_FAIL
;
3469 prv
->rsa
= EVP_PKEY_get1_RSA(pk
);
3470 prv
->type
= KEY_RSA
;
3472 RSA_print_fp(stderr
, prv
->rsa
, 8);
3474 if (RSA_blinding_on(prv
->rsa
, NULL
) != 1) {
3475 r
= SSH_ERR_LIBCRYPTO_ERROR
;
3478 if (BN_num_bits(prv
->rsa
->n
) < SSH_RSA_MINIMUM_MODULUS_SIZE
) {
3479 r
= SSH_ERR_KEY_LENGTH
;
3482 } else if (pk
->type
== EVP_PKEY_DSA
&&
3483 (type
== KEY_UNSPEC
|| type
== KEY_DSA
)) {
3484 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3485 r
= SSH_ERR_ALLOC_FAIL
;
3488 prv
->dsa
= EVP_PKEY_get1_DSA(pk
);
3489 prv
->type
= KEY_DSA
;
3491 DSA_print_fp(stderr
, prv
->dsa
, 8);
3493 #ifdef OPENSSL_HAS_ECC
3494 } else if (pk
->type
== EVP_PKEY_EC
&&
3495 (type
== KEY_UNSPEC
|| type
== KEY_ECDSA
)) {
3496 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3497 r
= SSH_ERR_ALLOC_FAIL
;
3500 prv
->ecdsa
= EVP_PKEY_get1_EC_KEY(pk
);
3501 prv
->type
= KEY_ECDSA
;
3502 prv
->ecdsa_nid
= sshkey_ecdsa_key_to_nid(prv
->ecdsa
);
3503 if (prv
->ecdsa_nid
== -1 ||
3504 sshkey_curve_nid_to_name(prv
->ecdsa_nid
) == NULL
||
3505 sshkey_ec_validate_public(EC_KEY_get0_group(prv
->ecdsa
),
3506 EC_KEY_get0_public_key(prv
->ecdsa
)) != 0 ||
3507 sshkey_ec_validate_private(prv
->ecdsa
) != 0) {
3508 r
= SSH_ERR_INVALID_FORMAT
;
3512 if (prv
!= NULL
&& prv
->ecdsa
!= NULL
)
3513 sshkey_dump_ec_key(prv
->ecdsa
);
3515 #endif /* OPENSSL_HAS_ECC */
3517 r
= SSH_ERR_INVALID_FORMAT
;
3532 #endif /* WITH_OPENSSL */
3535 sshkey_parse_private_fileblob_type(struct sshbuf
*blob
, int type
,
3536 const char *passphrase
, struct sshkey
**keyp
, char **commentp
)
3538 int r
= SSH_ERR_INTERNAL_ERROR
;
3542 if (commentp
!= NULL
)
3550 return sshkey_parse_private_pem_fileblob(blob
, type
,
3552 #endif /* WITH_OPENSSL */
3554 return sshkey_parse_private2(blob
, type
, passphrase
,
3557 r
= sshkey_parse_private2(blob
, type
, passphrase
, keyp
,
3559 /* Do not fallback to PEM parser if only passphrase is wrong. */
3560 if (r
== 0 || r
== SSH_ERR_KEY_WRONG_PASSPHRASE
)
3563 return sshkey_parse_private_pem_fileblob(blob
, type
,
3566 return SSH_ERR_INVALID_FORMAT
;
3567 #endif /* WITH_OPENSSL */
3569 return SSH_ERR_KEY_TYPE_UNKNOWN
;
3574 sshkey_parse_private_fileblob(struct sshbuf
*buffer
, const char *passphrase
,
3575 struct sshkey
**keyp
, char **commentp
)
3579 if (commentp
!= NULL
)
3582 return sshkey_parse_private_fileblob_type(buffer
, KEY_UNSPEC
,
3583 passphrase
, keyp
, commentp
);