1 /* $OpenBSD: sshkey.c,v 1.3 2014/07/03 01:45:38 djm Exp $ */
3 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
5 * Copyright (c) 2010,2011 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include <openssl/evp.h>
34 #include <openssl/err.h>
35 #include <openssl/pem.h>
37 #include "crypto_api.h"
44 #endif /* HAVE_UTIL_H */
53 #define SSHKEY_INTERNAL
56 /* openssh private key file format */
57 #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
58 #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
59 #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
60 #define MARK_END_LEN (sizeof(MARK_END) - 1)
61 #define KDFNAME "bcrypt"
62 #define AUTH_MAGIC "openssh-key-v1"
64 #define DEFAULT_CIPHERNAME "aes256-cbc"
65 #define DEFAULT_ROUNDS 16
67 /* Version identification string for SSH v1 identity files. */
68 #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
70 static int sshkey_from_blob_internal(const u_char
*blob
, size_t blen
,
71 struct sshkey
**keyp
, int allow_cert
);
73 /* Supported key types */
76 const char *shortname
;
81 static const struct keytype keytypes
[] = {
82 { "ssh-ed25519", "ED25519", KEY_ED25519
, 0, 0 },
83 { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT",
84 KEY_ED25519_CERT
, 0, 1 },
86 { NULL
, "RSA1", KEY_RSA1
, 0, 0 },
87 { "ssh-rsa", "RSA", KEY_RSA
, 0, 0 },
88 { "ssh-dss", "DSA", KEY_DSA
, 0, 0 },
89 # ifdef OPENSSL_HAS_ECC
90 { "ecdsa-sha2-nistp256", "ECDSA", KEY_ECDSA
, NID_X9_62_prime256v1
, 0 },
91 { "ecdsa-sha2-nistp384", "ECDSA", KEY_ECDSA
, NID_secp384r1
, 0 },
92 # ifdef OPENSSL_HAS_NISTP521
93 { "ecdsa-sha2-nistp521", "ECDSA", KEY_ECDSA
, NID_secp521r1
, 0 },
94 # endif /* OPENSSL_HAS_NISTP521 */
95 # endif /* OPENSSL_HAS_ECC */
96 { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", KEY_RSA_CERT
, 0, 1 },
97 { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", KEY_DSA_CERT
, 0, 1 },
98 # ifdef OPENSSL_HAS_ECC
99 { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT",
100 KEY_ECDSA_CERT
, NID_X9_62_prime256v1
, 1 },
101 { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT",
102 KEY_ECDSA_CERT
, NID_secp384r1
, 1 },
103 # ifdef OPENSSL_HAS_NISTP521
104 { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT",
105 KEY_ECDSA_CERT
, NID_secp521r1
, 1 },
106 # endif /* OPENSSL_HAS_NISTP521 */
107 # endif /* OPENSSL_HAS_ECC */
108 { "ssh-rsa-cert-v00@openssh.com", "RSA-CERT-V00",
109 KEY_RSA_CERT_V00
, 0, 1 },
110 { "ssh-dss-cert-v00@openssh.com", "DSA-CERT-V00",
111 KEY_DSA_CERT_V00
, 0, 1 },
112 #endif /* WITH_OPENSSL */
113 { NULL
, NULL
, -1, -1, 0 }
117 sshkey_type(const struct sshkey
*k
)
119 const struct keytype
*kt
;
121 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
122 if (kt
->type
== k
->type
)
123 return kt
->shortname
;
129 sshkey_ssh_name_from_type_nid(int type
, int nid
)
131 const struct keytype
*kt
;
133 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
134 if (kt
->type
== type
&& (kt
->nid
== 0 || kt
->nid
== nid
))
137 return "ssh-unknown";
141 sshkey_type_is_cert(int type
)
143 const struct keytype
*kt
;
145 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
146 if (kt
->type
== type
)
153 sshkey_ssh_name(const struct sshkey
*k
)
155 return sshkey_ssh_name_from_type_nid(k
->type
, k
->ecdsa_nid
);
159 sshkey_ssh_name_plain(const struct sshkey
*k
)
161 return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k
->type
),
166 sshkey_type_from_name(const char *name
)
168 const struct keytype
*kt
;
170 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
171 /* Only allow shortname matches for plain key types */
172 if ((kt
->name
!= NULL
&& strcmp(name
, kt
->name
) == 0) ||
173 (!kt
->cert
&& strcasecmp(kt
->shortname
, name
) == 0))
180 sshkey_ecdsa_nid_from_name(const char *name
)
182 const struct keytype
*kt
;
184 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
185 if (kt
->type
!= KEY_ECDSA
&& kt
->type
!= KEY_ECDSA_CERT
)
187 if (kt
->name
!= NULL
&& strcmp(name
, kt
->name
) == 0)
194 key_alg_list(int certs_only
, int plain_only
)
196 char *tmp
, *ret
= NULL
;
197 size_t nlen
, rlen
= 0;
198 const struct keytype
*kt
;
200 for (kt
= keytypes
; kt
->type
!= -1; kt
++) {
201 if (kt
->name
== NULL
)
203 if ((certs_only
&& !kt
->cert
) || (plain_only
&& kt
->cert
))
207 nlen
= strlen(kt
->name
);
208 if ((tmp
= realloc(ret
, rlen
+ nlen
+ 2)) == NULL
) {
213 memcpy(ret
+ rlen
, kt
->name
, nlen
+ 1);
220 sshkey_names_valid2(const char *names
)
224 if (names
== NULL
|| strcmp(names
, "") == 0)
226 if ((s
= cp
= strdup(names
)) == NULL
)
228 for ((p
= strsep(&cp
, ",")); p
&& *p
!= '\0';
229 (p
= strsep(&cp
, ","))) {
230 switch (sshkey_type_from_name(p
)) {
242 sshkey_size(const struct sshkey
*k
)
248 case KEY_RSA_CERT_V00
:
250 return BN_num_bits(k
->rsa
->n
);
252 case KEY_DSA_CERT_V00
:
254 return BN_num_bits(k
->dsa
->p
);
257 return sshkey_curve_nid_to_bits(k
->ecdsa_nid
);
258 #endif /* WITH_OPENSSL */
260 case KEY_ED25519_CERT
:
261 return 256; /* XXX */
267 sshkey_cert_is_legacy(const struct sshkey
*k
)
270 case KEY_DSA_CERT_V00
:
271 case KEY_RSA_CERT_V00
:
279 sshkey_type_is_valid_ca(int type
)
293 sshkey_is_cert(const struct sshkey
*k
)
297 return sshkey_type_is_cert(k
->type
);
300 /* Return the cert-less equivalent to a certified key type */
302 sshkey_type_plain(int type
)
305 case KEY_RSA_CERT_V00
:
308 case KEY_DSA_CERT_V00
:
313 case KEY_ED25519_CERT
:
321 /* XXX: these are really begging for a table-driven approach */
323 sshkey_curve_name_to_nid(const char *name
)
325 if (strcmp(name
, "nistp256") == 0)
326 return NID_X9_62_prime256v1
;
327 else if (strcmp(name
, "nistp384") == 0)
328 return NID_secp384r1
;
329 # ifdef OPENSSL_HAS_NISTP521
330 else if (strcmp(name
, "nistp521") == 0)
331 return NID_secp521r1
;
332 # endif /* OPENSSL_HAS_NISTP521 */
338 sshkey_curve_nid_to_bits(int nid
)
341 case NID_X9_62_prime256v1
:
345 # ifdef OPENSSL_HAS_NISTP521
348 # endif /* OPENSSL_HAS_NISTP521 */
355 sshkey_ecdsa_bits_to_nid(int bits
)
359 return NID_X9_62_prime256v1
;
361 return NID_secp384r1
;
362 # ifdef OPENSSL_HAS_NISTP521
364 return NID_secp521r1
;
365 # endif /* OPENSSL_HAS_NISTP521 */
372 sshkey_curve_nid_to_name(int nid
)
375 case NID_X9_62_prime256v1
:
379 # ifdef OPENSSL_HAS_NISTP521
382 # endif /* OPENSSL_HAS_NISTP521 */
389 sshkey_ec_nid_to_hash_alg(int nid
)
391 int kbits
= sshkey_curve_nid_to_bits(nid
);
396 /* RFC5656 section 6.2.1 */
398 return SSH_DIGEST_SHA256
;
399 else if (kbits
<= 384)
400 return SSH_DIGEST_SHA384
;
402 return SSH_DIGEST_SHA512
;
404 #endif /* WITH_OPENSSL */
407 cert_free(struct sshkey_cert
*cert
)
413 if (cert
->certblob
!= NULL
)
414 sshbuf_free(cert
->certblob
);
415 if (cert
->critical
!= NULL
)
416 sshbuf_free(cert
->critical
);
417 if (cert
->extensions
!= NULL
)
418 sshbuf_free(cert
->extensions
);
419 if (cert
->key_id
!= NULL
)
421 for (i
= 0; i
< cert
->nprincipals
; i
++)
422 free(cert
->principals
[i
]);
423 if (cert
->principals
!= NULL
)
424 free(cert
->principals
);
425 if (cert
->signature_key
!= NULL
)
426 sshkey_free(cert
->signature_key
);
427 explicit_bzero(cert
, sizeof(*cert
));
431 static struct sshkey_cert
*
434 struct sshkey_cert
*cert
;
436 if ((cert
= calloc(1, sizeof(*cert
))) == NULL
)
438 if ((cert
->certblob
= sshbuf_new()) == NULL
||
439 (cert
->critical
= sshbuf_new()) == NULL
||
440 (cert
->extensions
= sshbuf_new()) == NULL
) {
445 cert
->principals
= NULL
;
446 cert
->signature_key
= NULL
;
457 #endif /* WITH_OPENSSL */
459 if ((k
= calloc(1, sizeof(*k
))) == NULL
)
467 k
->ed25519_sk
= NULL
;
468 k
->ed25519_pk
= NULL
;
473 case KEY_RSA_CERT_V00
:
475 if ((rsa
= RSA_new()) == NULL
||
476 (rsa
->n
= BN_new()) == NULL
||
477 (rsa
->e
= BN_new()) == NULL
) {
486 case KEY_DSA_CERT_V00
:
488 if ((dsa
= DSA_new()) == NULL
||
489 (dsa
->p
= BN_new()) == NULL
||
490 (dsa
->q
= BN_new()) == NULL
||
491 (dsa
->g
= BN_new()) == NULL
||
492 (dsa
->pub_key
= BN_new()) == NULL
) {
502 /* Cannot do anything until we know the group */
504 #endif /* WITH_OPENSSL */
506 case KEY_ED25519_CERT
:
507 /* no need to prealloc */
517 if (sshkey_is_cert(k
)) {
518 if ((k
->cert
= cert_new()) == NULL
) {
528 sshkey_add_private(struct sshkey
*k
)
534 case KEY_RSA_CERT_V00
:
536 #define bn_maybe_alloc_failed(p) (p == NULL && (p = BN_new()) == NULL)
537 if (bn_maybe_alloc_failed(k
->rsa
->d
) ||
538 bn_maybe_alloc_failed(k
->rsa
->iqmp
) ||
539 bn_maybe_alloc_failed(k
->rsa
->q
) ||
540 bn_maybe_alloc_failed(k
->rsa
->p
) ||
541 bn_maybe_alloc_failed(k
->rsa
->dmq1
) ||
542 bn_maybe_alloc_failed(k
->rsa
->dmp1
))
543 return SSH_ERR_ALLOC_FAIL
;
546 case KEY_DSA_CERT_V00
:
548 if (bn_maybe_alloc_failed(k
->dsa
->priv_key
))
549 return SSH_ERR_ALLOC_FAIL
;
551 #undef bn_maybe_alloc_failed
554 /* Cannot do anything until we know the group */
556 #endif /* WITH_OPENSSL */
558 case KEY_ED25519_CERT
:
559 /* no need to prealloc */
564 return SSH_ERR_INVALID_ARGUMENT
;
570 sshkey_new_private(int type
)
572 struct sshkey
*k
= sshkey_new(type
);
576 if (sshkey_add_private(k
) != 0) {
584 sshkey_free(struct sshkey
*k
)
592 case KEY_RSA_CERT_V00
:
599 case KEY_DSA_CERT_V00
:
605 # ifdef OPENSSL_HAS_ECC
608 if (k
->ecdsa
!= NULL
)
609 EC_KEY_free(k
->ecdsa
);
612 # endif /* OPENSSL_HAS_ECC */
613 #endif /* WITH_OPENSSL */
615 case KEY_ED25519_CERT
:
617 explicit_bzero(k
->ed25519_pk
, ED25519_PK_SZ
);
619 k
->ed25519_pk
= NULL
;
622 explicit_bzero(k
->ed25519_sk
, ED25519_SK_SZ
);
624 k
->ed25519_sk
= NULL
;
632 if (sshkey_is_cert(k
))
634 explicit_bzero(k
, sizeof(*k
));
639 cert_compare(struct sshkey_cert
*a
, struct sshkey_cert
*b
)
641 if (a
== NULL
&& b
== NULL
)
643 if (a
== NULL
|| b
== NULL
)
645 if (sshbuf_len(a
->certblob
) != sshbuf_len(b
->certblob
))
647 if (timingsafe_bcmp(sshbuf_ptr(a
->certblob
), sshbuf_ptr(b
->certblob
),
648 sshbuf_len(a
->certblob
)) != 0)
654 * Compare public portions of key only, allowing comparisons between
655 * certificates and plain keys too.
658 sshkey_equal_public(const struct sshkey
*a
, const struct sshkey
*b
)
660 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
662 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
664 if (a
== NULL
|| b
== NULL
||
665 sshkey_type_plain(a
->type
) != sshkey_type_plain(b
->type
))
671 case KEY_RSA_CERT_V00
:
674 return a
->rsa
!= NULL
&& b
->rsa
!= NULL
&&
675 BN_cmp(a
->rsa
->e
, b
->rsa
->e
) == 0 &&
676 BN_cmp(a
->rsa
->n
, b
->rsa
->n
) == 0;
677 case KEY_DSA_CERT_V00
:
680 return a
->dsa
!= NULL
&& b
->dsa
!= NULL
&&
681 BN_cmp(a
->dsa
->p
, b
->dsa
->p
) == 0 &&
682 BN_cmp(a
->dsa
->q
, b
->dsa
->q
) == 0 &&
683 BN_cmp(a
->dsa
->g
, b
->dsa
->g
) == 0 &&
684 BN_cmp(a
->dsa
->pub_key
, b
->dsa
->pub_key
) == 0;
685 # ifdef OPENSSL_HAS_ECC
688 if (a
->ecdsa
== NULL
|| b
->ecdsa
== NULL
||
689 EC_KEY_get0_public_key(a
->ecdsa
) == NULL
||
690 EC_KEY_get0_public_key(b
->ecdsa
) == NULL
)
692 if ((bnctx
= BN_CTX_new()) == NULL
)
694 if (EC_GROUP_cmp(EC_KEY_get0_group(a
->ecdsa
),
695 EC_KEY_get0_group(b
->ecdsa
), bnctx
) != 0 ||
696 EC_POINT_cmp(EC_KEY_get0_group(a
->ecdsa
),
697 EC_KEY_get0_public_key(a
->ecdsa
),
698 EC_KEY_get0_public_key(b
->ecdsa
), bnctx
) != 0) {
704 # endif /* OPENSSL_HAS_ECC */
705 #endif /* WITH_OPENSSL */
707 case KEY_ED25519_CERT
:
708 return a
->ed25519_pk
!= NULL
&& b
->ed25519_pk
!= NULL
&&
709 memcmp(a
->ed25519_pk
, b
->ed25519_pk
, ED25519_PK_SZ
) == 0;
717 sshkey_equal(const struct sshkey
*a
, const struct sshkey
*b
)
719 if (a
== NULL
|| b
== NULL
|| a
->type
!= b
->type
)
721 if (sshkey_is_cert(a
)) {
722 if (!cert_compare(a
->cert
, b
->cert
))
725 return sshkey_equal_public(a
, b
);
729 to_blob_buf(const struct sshkey
*key
, struct sshbuf
*b
, int force_plain
)
731 int type
, ret
= SSH_ERR_INTERNAL_ERROR
;
732 const char *typename
;
735 return SSH_ERR_INVALID_ARGUMENT
;
737 type
= force_plain
? sshkey_type_plain(key
->type
) : key
->type
;
738 typename
= sshkey_ssh_name_from_type_nid(type
, key
->ecdsa_nid
);
742 case KEY_DSA_CERT_V00
:
743 case KEY_RSA_CERT_V00
:
747 #endif /* WITH_OPENSSL */
748 case KEY_ED25519_CERT
:
749 /* Use the existing blob */
750 /* XXX modified flag? */
751 if ((ret
= sshbuf_putb(b
, key
->cert
->certblob
)) != 0)
756 if (key
->dsa
== NULL
)
757 return SSH_ERR_INVALID_ARGUMENT
;
758 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
759 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->p
)) != 0 ||
760 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->q
)) != 0 ||
761 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->g
)) != 0 ||
762 (ret
= sshbuf_put_bignum2(b
, key
->dsa
->pub_key
)) != 0)
765 # ifdef OPENSSL_HAS_ECC
767 if (key
->ecdsa
== NULL
)
768 return SSH_ERR_INVALID_ARGUMENT
;
769 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
770 (ret
= sshbuf_put_cstring(b
,
771 sshkey_curve_nid_to_name(key
->ecdsa_nid
))) != 0 ||
772 (ret
= sshbuf_put_eckey(b
, key
->ecdsa
)) != 0)
777 if (key
->rsa
== NULL
)
778 return SSH_ERR_INVALID_ARGUMENT
;
779 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
780 (ret
= sshbuf_put_bignum2(b
, key
->rsa
->e
)) != 0 ||
781 (ret
= sshbuf_put_bignum2(b
, key
->rsa
->n
)) != 0)
784 #endif /* WITH_OPENSSL */
786 if (key
->ed25519_pk
== NULL
)
787 return SSH_ERR_INVALID_ARGUMENT
;
788 if ((ret
= sshbuf_put_cstring(b
, typename
)) != 0 ||
789 (ret
= sshbuf_put_string(b
,
790 key
->ed25519_pk
, ED25519_PK_SZ
)) != 0)
794 return SSH_ERR_KEY_TYPE_UNKNOWN
;
800 sshkey_to_blob_buf(const struct sshkey
*key
, struct sshbuf
*b
)
802 return to_blob_buf(key
, b
, 0);
806 sshkey_plain_to_blob_buf(const struct sshkey
*key
, struct sshbuf
*b
)
808 return to_blob_buf(key
, b
, 1);
812 to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
, int force_plain
)
814 int ret
= SSH_ERR_INTERNAL_ERROR
;
816 struct sshbuf
*b
= NULL
;
822 if ((b
= sshbuf_new()) == NULL
)
823 return SSH_ERR_ALLOC_FAIL
;
824 if ((ret
= to_blob_buf(key
, b
, force_plain
)) != 0)
830 if ((*blobp
= malloc(len
)) == NULL
) {
831 ret
= SSH_ERR_ALLOC_FAIL
;
834 memcpy(*blobp
, sshbuf_ptr(b
), len
);
843 sshkey_to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
)
845 return to_blob(key
, blobp
, lenp
, 0);
849 sshkey_plain_to_blob(const struct sshkey
*key
, u_char
**blobp
, size_t *lenp
)
851 return to_blob(key
, blobp
, lenp
, 1);
855 sshkey_fingerprint_raw(const struct sshkey
*k
, enum sshkey_fp_type dgst_type
,
856 u_char
**retp
, size_t *lenp
)
858 u_char
*blob
= NULL
, *ret
= NULL
;
860 int hash_alg
= -1, r
= SSH_ERR_INTERNAL_ERROR
;
869 hash_alg
= SSH_DIGEST_MD5
;
872 hash_alg
= SSH_DIGEST_SHA1
;
875 hash_alg
= SSH_DIGEST_SHA256
;
878 r
= SSH_ERR_INVALID_ARGUMENT
;
882 if (k
->type
== KEY_RSA1
) {
884 int nlen
= BN_num_bytes(k
->rsa
->n
);
885 int elen
= BN_num_bytes(k
->rsa
->e
);
887 blob_len
= nlen
+ elen
;
888 if (nlen
>= INT_MAX
- elen
||
889 (blob
= malloc(blob_len
)) == NULL
) {
890 r
= SSH_ERR_ALLOC_FAIL
;
893 BN_bn2bin(k
->rsa
->n
, blob
);
894 BN_bn2bin(k
->rsa
->e
, blob
+ nlen
);
895 #endif /* WITH_OPENSSL */
896 } else if ((r
= to_blob(k
, &blob
, &blob_len
, 1)) != 0)
898 if ((ret
= calloc(1, SSH_DIGEST_MAX_LENGTH
)) == NULL
) {
899 r
= SSH_ERR_ALLOC_FAIL
;
902 if ((r
= ssh_digest_memory(hash_alg
, blob
, blob_len
,
903 ret
, SSH_DIGEST_MAX_LENGTH
)) != 0)
911 *lenp
= ssh_digest_bytes(hash_alg
);
916 explicit_bzero(blob
, blob_len
);
923 fingerprint_hex(u_char
*dgst_raw
, size_t dgst_raw_len
)
928 if ((retval
= calloc(1, dgst_raw_len
* 3 + 1)) == NULL
)
930 for (i
= 0; i
< dgst_raw_len
; i
++) {
932 snprintf(hex
, sizeof(hex
), "%02x:", dgst_raw
[i
]);
933 strlcat(retval
, hex
, dgst_raw_len
* 3 + 1);
936 /* Remove the trailing ':' character */
937 retval
[(dgst_raw_len
* 3) - 1] = '\0';
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(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
];
1032 u_char field
[FLDSIZE_X
][FLDSIZE_Y
];
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
= MIN(x
, FLDSIZE_X
- 1);
1060 y
= MIN(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 snprintf(title
, sizeof(title
), "[%s]", sshkey_type(k
));
1079 tlen
= strlen(title
);
1081 /* output upper border */
1084 for (i
= 0; i
< (FLDSIZE_X
- tlen
) / 2; i
++)
1086 memcpy(p
, title
, tlen
);
1088 for (i
= p
- retval
- 1; i
< FLDSIZE_X
; i
++)
1093 /* output content */
1094 for (y
= 0; y
< FLDSIZE_Y
; y
++) {
1096 for (x
= 0; x
< FLDSIZE_X
; x
++)
1097 *p
++ = augmentation_string
[MIN(field
[x
][y
], len
)];
1102 /* output lower border */
1104 for (i
= 0; i
< FLDSIZE_X
; i
++)
1112 sshkey_fingerprint(const struct sshkey
*k
, enum sshkey_fp_type dgst_type
,
1113 enum sshkey_fp_rep dgst_rep
)
1115 char *retval
= NULL
;
1117 size_t dgst_raw_len
;
1119 if (sshkey_fingerprint_raw(k
, dgst_type
, &dgst_raw
, &dgst_raw_len
) != 0)
1123 retval
= fingerprint_hex(dgst_raw
, dgst_raw_len
);
1125 case SSH_FP_BUBBLEBABBLE
:
1126 retval
= fingerprint_bubblebabble(dgst_raw
, dgst_raw_len
);
1128 case SSH_FP_RANDOMART
:
1129 retval
= fingerprint_randomart(dgst_raw
, dgst_raw_len
, k
);
1132 explicit_bzero(dgst_raw
, dgst_raw_len
);
1136 explicit_bzero(dgst_raw
, dgst_raw_len
);
1143 * Reads a multiple-precision integer in decimal from the buffer, and advances
1144 * the pointer. The integer must already be initialized. This function is
1145 * permitted to modify the buffer. This leaves *cpp to point just beyond the
1146 * last processed character.
1149 read_decimal_bignum(char **cpp
, BIGNUM
*v
)
1153 int skip
= 1; /* skip white space */
1156 while (*cp
== ' ' || *cp
== '\t')
1158 e
= strspn(cp
, "0123456789");
1160 return SSH_ERR_INVALID_FORMAT
;
1161 if (e
> SSHBUF_MAX_BIGNUM
* 3)
1162 return SSH_ERR_BIGNUM_TOO_LARGE
;
1165 else if (index(" \t\r\n", cp
[e
]) == NULL
)
1166 return SSH_ERR_INVALID_FORMAT
;
1168 if (BN_dec2bn(&v
, cp
) <= 0)
1169 return SSH_ERR_INVALID_FORMAT
;
1170 *cpp
= cp
+ e
+ skip
;
1173 #endif /* WITH_SSH1 */
1175 /* returns 0 ok, and < 0 error */
1177 sshkey_read(struct sshkey
*ret
, char **cpp
)
1180 int retval
= SSH_ERR_INVALID_FORMAT
;
1182 int r
, type
, curve_nid
= -1;
1183 struct sshbuf
*blob
;
1187 #endif /* WITH_SSH1 */
1191 switch (ret
->type
) {
1194 /* Get number of bits. */
1195 bits
= strtoul(cp
, &ep
, 10);
1196 if (*cp
== '\0' || index(" \t\r\n", *ep
) == NULL
||
1197 bits
== 0 || bits
> SSHBUF_MAX_BIGNUM
* 8)
1198 return SSH_ERR_INVALID_FORMAT
; /* Bad bit count... */
1199 /* Get public exponent, public modulus. */
1200 if ((r
= read_decimal_bignum(&ep
, ret
->rsa
->e
)) < 0)
1202 if ((r
= read_decimal_bignum(&ep
, ret
->rsa
->n
)) < 0)
1205 /* validate the claimed number of bits */
1206 if (BN_num_bits(ret
->rsa
->n
) != (int)bits
)
1207 return SSH_ERR_KEY_BITS_MISMATCH
;
1209 #endif /* WITH_SSH1 */
1216 case KEY_DSA_CERT_V00
:
1217 case KEY_RSA_CERT_V00
:
1219 case KEY_ECDSA_CERT
:
1221 case KEY_ED25519_CERT
:
1222 space
= strchr(cp
, ' ');
1224 return SSH_ERR_INVALID_FORMAT
;
1226 type
= sshkey_type_from_name(cp
);
1227 if (sshkey_type_plain(type
) == KEY_ECDSA
&&
1228 (curve_nid
= sshkey_ecdsa_nid_from_name(cp
)) == -1)
1229 return SSH_ERR_EC_CURVE_INVALID
;
1231 if (type
== KEY_UNSPEC
)
1232 return SSH_ERR_INVALID_FORMAT
;
1235 return SSH_ERR_INVALID_FORMAT
;
1236 if (ret
->type
== KEY_UNSPEC
) {
1238 } else if (ret
->type
!= type
)
1239 return SSH_ERR_KEY_TYPE_MISMATCH
;
1240 if ((blob
= sshbuf_new()) == NULL
)
1241 return SSH_ERR_ALLOC_FAIL
;
1243 space
= strchr(cp
, ' ');
1246 if ((r
= sshbuf_b64tod(blob
, cp
)) != 0) {
1250 if ((r
= sshkey_from_blob(sshbuf_ptr(blob
),
1251 sshbuf_len(blob
), &k
)) != 0) {
1256 if (k
->type
!= type
) {
1258 return SSH_ERR_KEY_TYPE_MISMATCH
;
1260 if (sshkey_type_plain(type
) == KEY_ECDSA
&&
1261 curve_nid
!= k
->ecdsa_nid
) {
1263 return SSH_ERR_EC_CURVE_MISMATCH
;
1266 if (sshkey_is_cert(ret
)) {
1267 if (!sshkey_is_cert(k
)) {
1269 return SSH_ERR_EXPECTED_CERT
;
1271 if (ret
->cert
!= NULL
)
1272 cert_free(ret
->cert
);
1273 ret
->cert
= k
->cert
;
1277 if (sshkey_type_plain(ret
->type
) == KEY_RSA
) {
1278 if (ret
->rsa
!= NULL
)
1283 RSA_print_fp(stderr
, ret
->rsa
, 8);
1286 if (sshkey_type_plain(ret
->type
) == KEY_DSA
) {
1287 if (ret
->dsa
!= NULL
)
1292 DSA_print_fp(stderr
, ret
->dsa
, 8);
1295 # ifdef OPENSSL_HAS_ECC
1296 if (sshkey_type_plain(ret
->type
) == KEY_ECDSA
) {
1297 if (ret
->ecdsa
!= NULL
)
1298 EC_KEY_free(ret
->ecdsa
);
1299 ret
->ecdsa
= k
->ecdsa
;
1300 ret
->ecdsa_nid
= k
->ecdsa_nid
;
1304 sshkey_dump_ec_key(ret
->ecdsa
);
1307 # endif /* OPENSSL_HAS_ECC */
1308 #endif /* WITH_OPENSSL */
1309 if (sshkey_type_plain(ret
->type
) == KEY_ED25519
) {
1310 free(ret
->ed25519_pk
);
1311 ret
->ed25519_pk
= k
->ed25519_pk
;
1312 k
->ed25519_pk
= NULL
;
1322 /* advance cp: skip whitespace and data */
1323 while (*cp
== ' ' || *cp
== '\t')
1325 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
1330 return SSH_ERR_INVALID_ARGUMENT
;
1336 sshkey_write(const struct sshkey
*key
, FILE *f
)
1338 int ret
= SSH_ERR_INTERNAL_ERROR
;
1339 struct sshbuf
*b
= NULL
, *bb
= NULL
;
1343 char *dec_e
= NULL
, *dec_n
= NULL
;
1344 #endif /* WITH_SSH1 */
1346 if (sshkey_is_cert(key
)) {
1347 if (key
->cert
== NULL
)
1348 return SSH_ERR_EXPECTED_CERT
;
1349 if (sshbuf_len(key
->cert
->certblob
) == 0)
1350 return SSH_ERR_KEY_LACKS_CERTBLOB
;
1352 if ((b
= sshbuf_new()) == NULL
)
1353 return SSH_ERR_ALLOC_FAIL
;
1354 switch (key
->type
) {
1357 if (key
->rsa
== NULL
|| key
->rsa
->e
== NULL
||
1358 key
->rsa
->n
== NULL
) {
1359 ret
= SSH_ERR_INVALID_ARGUMENT
;
1362 if ((dec_e
= BN_bn2dec(key
->rsa
->e
)) == NULL
||
1363 (dec_n
= BN_bn2dec(key
->rsa
->n
)) == NULL
) {
1364 ret
= SSH_ERR_ALLOC_FAIL
;
1367 /* size of modulus 'n' */
1368 if ((bits
= BN_num_bits(key
->rsa
->n
)) <= 0) {
1369 ret
= SSH_ERR_INVALID_ARGUMENT
;
1372 if ((ret
= sshbuf_putf(b
, "%u %s %s", bits
, dec_e
, dec_n
)) != 0)
1374 #endif /* WITH_SSH1 */
1378 case KEY_DSA_CERT_V00
:
1381 case KEY_ECDSA_CERT
:
1383 case KEY_RSA_CERT_V00
:
1385 #endif /* WITH_OPENSSL */
1387 case KEY_ED25519_CERT
:
1388 if ((bb
= sshbuf_new()) == NULL
) {
1389 ret
= SSH_ERR_ALLOC_FAIL
;
1392 if ((ret
= sshkey_to_blob_buf(key
, bb
)) != 0)
1394 if ((uu
= sshbuf_dtob64(bb
)) == NULL
) {
1395 ret
= SSH_ERR_ALLOC_FAIL
;
1398 if ((ret
= sshbuf_putf(b
, "%s ", sshkey_ssh_name(key
))) != 0)
1400 if ((ret
= sshbuf_put(b
, uu
, strlen(uu
))) != 0)
1404 ret
= SSH_ERR_KEY_TYPE_UNKNOWN
;
1407 if (fwrite(sshbuf_ptr(b
), sshbuf_len(b
), 1, f
) != 1) {
1410 ret
= SSH_ERR_SYSTEM_ERROR
;
1423 OPENSSL_free(dec_e
);
1425 OPENSSL_free(dec_n
);
1426 #endif /* WITH_SSH1 */
1431 sshkey_cert_type(const struct sshkey
*k
)
1433 switch (k
->cert
->type
) {
1434 case SSH2_CERT_TYPE_USER
:
1436 case SSH2_CERT_TYPE_HOST
:
1445 rsa_generate_private_key(u_int bits
, RSA
**rsap
)
1447 RSA
*private = NULL
;
1449 int ret
= SSH_ERR_INTERNAL_ERROR
;
1452 bits
< SSH_RSA_MINIMUM_MODULUS_SIZE
||
1453 bits
> SSHBUF_MAX_BIGNUM
* 8)
1454 return SSH_ERR_INVALID_ARGUMENT
;
1456 if ((private = RSA_new()) == NULL
|| (f4
= BN_new()) == NULL
) {
1457 ret
= SSH_ERR_ALLOC_FAIL
;
1460 if (!BN_set_word(f4
, RSA_F4
) ||
1461 !RSA_generate_key_ex(private, bits
, f4
, NULL
)) {
1462 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1469 if (private != NULL
)
1477 dsa_generate_private_key(u_int bits
, DSA
**dsap
)
1480 int ret
= SSH_ERR_INTERNAL_ERROR
;
1482 if (dsap
== NULL
|| bits
!= 1024)
1483 return SSH_ERR_INVALID_ARGUMENT
;
1484 if ((private = DSA_new()) == NULL
) {
1485 ret
= SSH_ERR_ALLOC_FAIL
;
1489 if (!DSA_generate_parameters_ex(private, bits
, NULL
, 0, NULL
,
1490 NULL
, NULL
) || !DSA_generate_key(private)) {
1492 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1499 if (private != NULL
)
1504 # ifdef OPENSSL_HAS_ECC
1506 sshkey_ecdsa_key_to_nid(EC_KEY
*k
)
1510 NID_X9_62_prime256v1
,
1512 # ifdef OPENSSL_HAS_NISTP521
1514 # endif /* OPENSSL_HAS_NISTP521 */
1520 const EC_GROUP
*g
= EC_KEY_get0_group(k
);
1523 * The group may be stored in a ASN.1 encoded private key in one of two
1524 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1525 * or explicit group parameters encoded into the key blob. Only the
1526 * "named group" case sets the group NID for us, but we can figure
1527 * it out for the other case by comparing against all the groups that
1530 if ((nid
= EC_GROUP_get_curve_name(g
)) > 0)
1532 if ((bnctx
= BN_CTX_new()) == NULL
)
1534 for (i
= 0; nids
[i
] != -1; i
++) {
1535 if ((eg
= EC_GROUP_new_by_curve_name(nids
[i
])) == NULL
) {
1539 if (EC_GROUP_cmp(g
, eg
, bnctx
) == 0)
1544 if (nids
[i
] != -1) {
1545 /* Use the group with the NID attached */
1546 EC_GROUP_set_asn1_flag(eg
, OPENSSL_EC_NAMED_CURVE
);
1547 if (EC_KEY_set_group(k
, eg
) != 1) {
1556 ecdsa_generate_private_key(u_int bits
, int *nid
, EC_KEY
**ecdsap
)
1559 int ret
= SSH_ERR_INTERNAL_ERROR
;
1561 if (nid
== NULL
|| ecdsap
== NULL
||
1562 (*nid
= sshkey_ecdsa_bits_to_nid(bits
)) == -1)
1563 return SSH_ERR_INVALID_ARGUMENT
;
1565 if ((private = EC_KEY_new_by_curve_name(*nid
)) == NULL
) {
1566 ret
= SSH_ERR_ALLOC_FAIL
;
1569 if (EC_KEY_generate_key(private) != 1) {
1570 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
1573 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE
);
1578 if (private != NULL
)
1579 EC_KEY_free(private);
1582 # endif /* OPENSSL_HAS_ECC */
1583 #endif /* WITH_OPENSSL */
1586 sshkey_generate(int type
, u_int bits
, struct sshkey
**keyp
)
1589 int ret
= SSH_ERR_INTERNAL_ERROR
;
1592 return SSH_ERR_INVALID_ARGUMENT
;
1594 if ((k
= sshkey_new(KEY_UNSPEC
)) == NULL
)
1595 return SSH_ERR_ALLOC_FAIL
;
1598 if ((k
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
||
1599 (k
->ed25519_sk
= malloc(ED25519_SK_SZ
)) == NULL
) {
1600 ret
= SSH_ERR_ALLOC_FAIL
;
1603 crypto_sign_ed25519_keypair(k
->ed25519_pk
, k
->ed25519_sk
);
1608 ret
= dsa_generate_private_key(bits
, &k
->dsa
);
1610 # ifdef OPENSSL_HAS_ECC
1612 ret
= ecdsa_generate_private_key(bits
, &k
->ecdsa_nid
,
1615 # endif /* OPENSSL_HAS_ECC */
1618 ret
= rsa_generate_private_key(bits
, &k
->rsa
);
1620 #endif /* WITH_OPENSSL */
1622 ret
= SSH_ERR_INVALID_ARGUMENT
;
1633 sshkey_cert_copy(const struct sshkey
*from_key
, struct sshkey
*to_key
)
1636 const struct sshkey_cert
*from
;
1637 struct sshkey_cert
*to
;
1638 int ret
= SSH_ERR_INTERNAL_ERROR
;
1640 if (to_key
->cert
!= NULL
) {
1641 cert_free(to_key
->cert
);
1642 to_key
->cert
= NULL
;
1645 if ((from
= from_key
->cert
) == NULL
)
1646 return SSH_ERR_INVALID_ARGUMENT
;
1648 if ((to
= to_key
->cert
= cert_new()) == NULL
)
1649 return SSH_ERR_ALLOC_FAIL
;
1651 if ((ret
= sshbuf_putb(to
->certblob
, from
->certblob
)) != 0 ||
1652 (ret
= sshbuf_putb(to
->critical
, from
->critical
)) != 0 ||
1653 (ret
= sshbuf_putb(to
->extensions
, from
->extensions
) != 0))
1656 to
->serial
= from
->serial
;
1657 to
->type
= from
->type
;
1658 if (from
->key_id
== NULL
)
1660 else if ((to
->key_id
= strdup(from
->key_id
)) == NULL
)
1661 return SSH_ERR_ALLOC_FAIL
;
1662 to
->valid_after
= from
->valid_after
;
1663 to
->valid_before
= from
->valid_before
;
1664 if (from
->signature_key
== NULL
)
1665 to
->signature_key
= NULL
;
1666 else if ((ret
= sshkey_from_private(from
->signature_key
,
1667 &to
->signature_key
)) != 0)
1670 if (from
->nprincipals
> SSHKEY_CERT_MAX_PRINCIPALS
)
1671 return SSH_ERR_INVALID_ARGUMENT
;
1672 if (from
->nprincipals
> 0) {
1673 if ((to
->principals
= calloc(from
->nprincipals
,
1674 sizeof(*to
->principals
))) == NULL
)
1675 return SSH_ERR_ALLOC_FAIL
;
1676 for (i
= 0; i
< from
->nprincipals
; i
++) {
1677 to
->principals
[i
] = strdup(from
->principals
[i
]);
1678 if (to
->principals
[i
] == NULL
) {
1679 to
->nprincipals
= i
;
1680 return SSH_ERR_ALLOC_FAIL
;
1684 to
->nprincipals
= from
->nprincipals
;
1689 sshkey_from_private(const struct sshkey
*k
, struct sshkey
**pkp
)
1691 struct sshkey
*n
= NULL
;
1692 int ret
= SSH_ERR_INTERNAL_ERROR
;
1700 case KEY_DSA_CERT_V00
:
1702 if ((n
= sshkey_new(k
->type
)) == NULL
)
1703 return SSH_ERR_ALLOC_FAIL
;
1704 if ((BN_copy(n
->dsa
->p
, k
->dsa
->p
) == NULL
) ||
1705 (BN_copy(n
->dsa
->q
, k
->dsa
->q
) == NULL
) ||
1706 (BN_copy(n
->dsa
->g
, k
->dsa
->g
) == NULL
) ||
1707 (BN_copy(n
->dsa
->pub_key
, k
->dsa
->pub_key
) == NULL
)) {
1709 return SSH_ERR_ALLOC_FAIL
;
1712 # ifdef OPENSSL_HAS_ECC
1714 case KEY_ECDSA_CERT
:
1715 if ((n
= sshkey_new(k
->type
)) == NULL
)
1716 return SSH_ERR_ALLOC_FAIL
;
1717 n
->ecdsa_nid
= k
->ecdsa_nid
;
1718 n
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
);
1719 if (n
->ecdsa
== NULL
) {
1721 return SSH_ERR_ALLOC_FAIL
;
1723 if (EC_KEY_set_public_key(n
->ecdsa
,
1724 EC_KEY_get0_public_key(k
->ecdsa
)) != 1) {
1726 return SSH_ERR_LIBCRYPTO_ERROR
;
1729 # endif /* OPENSSL_HAS_ECC */
1732 case KEY_RSA_CERT_V00
:
1734 if ((n
= sshkey_new(k
->type
)) == NULL
)
1735 return SSH_ERR_ALLOC_FAIL
;
1736 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
1737 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
)) {
1739 return SSH_ERR_ALLOC_FAIL
;
1742 #endif /* WITH_OPENSSL */
1744 case KEY_ED25519_CERT
:
1745 if ((n
= sshkey_new(k
->type
)) == NULL
)
1746 return SSH_ERR_ALLOC_FAIL
;
1747 if (k
->ed25519_pk
!= NULL
) {
1748 if ((n
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
) {
1750 return SSH_ERR_ALLOC_FAIL
;
1752 memcpy(n
->ed25519_pk
, k
->ed25519_pk
, ED25519_PK_SZ
);
1756 return SSH_ERR_KEY_TYPE_UNKNOWN
;
1758 if (sshkey_is_cert(k
)) {
1759 if ((ret
= sshkey_cert_copy(k
, n
)) != 0) {
1769 cert_parse(struct sshbuf
*b
, struct sshkey
*key
, const u_char
*blob
,
1772 u_char
*principals
= NULL
, *critical
= NULL
, *exts
= NULL
;
1773 u_char
*sig_key
= NULL
, *sig
= NULL
;
1774 size_t signed_len
, plen
, clen
, sklen
, slen
, kidlen
, elen
;
1777 int ret
= SSH_ERR_INTERNAL_ERROR
;
1778 int v00
= sshkey_cert_is_legacy(key
);
1781 if ((tmp
= sshbuf_new()) == NULL
)
1782 return SSH_ERR_ALLOC_FAIL
;
1784 /* Copy the entire key blob for verification and later serialisation */
1785 if ((ret
= sshbuf_put(key
->cert
->certblob
, blob
, blen
)) != 0)
1788 elen
= 0; /* Not touched for v00 certs */
1789 principals
= exts
= critical
= sig_key
= sig
= NULL
;
1790 if ((!v00
&& (ret
= sshbuf_get_u64(b
, &key
->cert
->serial
)) != 0) ||
1791 (ret
= sshbuf_get_u32(b
, &key
->cert
->type
)) != 0 ||
1792 (ret
= sshbuf_get_cstring(b
, &key
->cert
->key_id
, &kidlen
)) != 0 ||
1793 (ret
= sshbuf_get_string(b
, &principals
, &plen
)) != 0 ||
1794 (ret
= sshbuf_get_u64(b
, &key
->cert
->valid_after
)) != 0 ||
1795 (ret
= sshbuf_get_u64(b
, &key
->cert
->valid_before
)) != 0 ||
1796 (ret
= sshbuf_get_string(b
, &critical
, &clen
)) != 0 ||
1797 (!v00
&& (ret
= sshbuf_get_string(b
, &exts
, &elen
)) != 0) ||
1798 (v00
&& (ret
= sshbuf_get_string_direct(b
, NULL
, NULL
)) != 0) ||
1799 (ret
= sshbuf_get_string_direct(b
, NULL
, NULL
)) != 0 ||
1800 (ret
= sshbuf_get_string(b
, &sig_key
, &sklen
)) != 0) {
1801 /* XXX debug print error for ret */
1802 ret
= SSH_ERR_INVALID_FORMAT
;
1806 /* Signature is left in the buffer so we can calculate this length */
1807 signed_len
= sshbuf_len(key
->cert
->certblob
) - sshbuf_len(b
);
1809 if ((ret
= sshbuf_get_string(b
, &sig
, &slen
)) != 0) {
1810 ret
= SSH_ERR_INVALID_FORMAT
;
1814 if (key
->cert
->type
!= SSH2_CERT_TYPE_USER
&&
1815 key
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1816 ret
= SSH_ERR_KEY_CERT_UNKNOWN_TYPE
;
1820 if ((ret
= sshbuf_put(tmp
, principals
, plen
)) != 0)
1822 while (sshbuf_len(tmp
) > 0) {
1823 if (key
->cert
->nprincipals
>= SSHKEY_CERT_MAX_PRINCIPALS
) {
1824 ret
= SSH_ERR_INVALID_FORMAT
;
1827 if ((ret
= sshbuf_get_cstring(tmp
, &principal
, &plen
)) != 0) {
1828 ret
= SSH_ERR_INVALID_FORMAT
;
1831 oprincipals
= key
->cert
->principals
;
1832 key
->cert
->principals
= realloc(key
->cert
->principals
,
1833 (key
->cert
->nprincipals
+ 1) *
1834 sizeof(*key
->cert
->principals
));
1835 if (key
->cert
->principals
== NULL
) {
1837 key
->cert
->principals
= oprincipals
;
1838 ret
= SSH_ERR_ALLOC_FAIL
;
1841 key
->cert
->principals
[key
->cert
->nprincipals
++] = principal
;
1846 if ((ret
= sshbuf_put(key
->cert
->critical
, critical
, clen
)) != 0 ||
1847 (ret
= sshbuf_put(tmp
, critical
, clen
)) != 0)
1850 /* validate structure */
1851 while (sshbuf_len(tmp
) != 0) {
1852 if ((ret
= sshbuf_get_string_direct(tmp
, NULL
, NULL
)) != 0 ||
1853 (ret
= sshbuf_get_string_direct(tmp
, NULL
, NULL
)) != 0) {
1854 ret
= SSH_ERR_INVALID_FORMAT
;
1860 if ((ret
= sshbuf_put(key
->cert
->extensions
, exts
, elen
)) != 0 ||
1861 (ret
= sshbuf_put(tmp
, exts
, elen
)) != 0)
1864 /* validate structure */
1865 while (sshbuf_len(tmp
) != 0) {
1866 if ((ret
= sshbuf_get_string_direct(tmp
, NULL
, NULL
)) != 0 ||
1867 (ret
= sshbuf_get_string_direct(tmp
, NULL
, NULL
)) != 0) {
1868 ret
= SSH_ERR_INVALID_FORMAT
;
1874 if (sshkey_from_blob_internal(sig_key
, sklen
,
1875 &key
->cert
->signature_key
, 0) != 0) {
1876 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1879 if (!sshkey_type_is_valid_ca(key
->cert
->signature_key
->type
)) {
1880 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1884 if ((ret
= sshkey_verify(key
->cert
->signature_key
, sig
, slen
,
1885 sshbuf_ptr(key
->cert
->certblob
), signed_len
, 0)) != 0)
1900 sshkey_from_blob_internal(const u_char
*blob
, size_t blen
,
1901 struct sshkey
**keyp
, int allow_cert
)
1903 struct sshbuf
*b
= NULL
;
1904 int type
, nid
= -1, ret
= SSH_ERR_INTERNAL_ERROR
;
1905 char *ktype
= NULL
, *curve
= NULL
;
1906 struct sshkey
*key
= NULL
;
1909 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
1911 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
1913 #ifdef DEBUG_PK /* XXX */
1914 dump_base64(stderr
, blob
, blen
);
1917 if ((b
= sshbuf_from(blob
, blen
)) == NULL
)
1918 return SSH_ERR_ALLOC_FAIL
;
1919 if (sshbuf_get_cstring(b
, &ktype
, NULL
) != 0) {
1920 ret
= SSH_ERR_INVALID_FORMAT
;
1924 type
= sshkey_type_from_name(ktype
);
1925 if (sshkey_type_plain(type
) == KEY_ECDSA
)
1926 nid
= sshkey_ecdsa_nid_from_name(ktype
);
1927 if (!allow_cert
&& sshkey_type_is_cert(type
)) {
1928 ret
= SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
1934 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1935 ret
= SSH_ERR_INVALID_FORMAT
;
1940 case KEY_RSA_CERT_V00
:
1941 if ((key
= sshkey_new(type
)) == NULL
) {
1942 ret
= SSH_ERR_ALLOC_FAIL
;
1945 if (sshbuf_get_bignum2(b
, key
->rsa
->e
) == -1 ||
1946 sshbuf_get_bignum2(b
, key
->rsa
->n
) == -1) {
1947 ret
= SSH_ERR_INVALID_FORMAT
;
1951 RSA_print_fp(stderr
, key
->rsa
, 8);
1955 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1956 ret
= SSH_ERR_INVALID_FORMAT
;
1961 case KEY_DSA_CERT_V00
:
1962 if ((key
= sshkey_new(type
)) == NULL
) {
1963 ret
= SSH_ERR_ALLOC_FAIL
;
1966 if (sshbuf_get_bignum2(b
, key
->dsa
->p
) == -1 ||
1967 sshbuf_get_bignum2(b
, key
->dsa
->q
) == -1 ||
1968 sshbuf_get_bignum2(b
, key
->dsa
->g
) == -1 ||
1969 sshbuf_get_bignum2(b
, key
->dsa
->pub_key
) == -1) {
1970 ret
= SSH_ERR_INVALID_FORMAT
;
1974 DSA_print_fp(stderr
, key
->dsa
, 8);
1977 case KEY_ECDSA_CERT
:
1978 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
1979 ret
= SSH_ERR_INVALID_FORMAT
;
1983 # ifdef OPENSSL_HAS_ECC
1985 if ((key
= sshkey_new(type
)) == NULL
) {
1986 ret
= SSH_ERR_ALLOC_FAIL
;
1989 key
->ecdsa_nid
= nid
;
1990 if (sshbuf_get_cstring(b
, &curve
, NULL
) != 0) {
1991 ret
= SSH_ERR_INVALID_FORMAT
;
1994 if (key
->ecdsa_nid
!= sshkey_curve_name_to_nid(curve
)) {
1995 ret
= SSH_ERR_EC_CURVE_MISMATCH
;
1998 if (key
->ecdsa
!= NULL
)
1999 EC_KEY_free(key
->ecdsa
);
2000 if ((key
->ecdsa
= EC_KEY_new_by_curve_name(key
->ecdsa_nid
))
2002 ret
= SSH_ERR_EC_CURVE_INVALID
;
2005 if ((q
= EC_POINT_new(EC_KEY_get0_group(key
->ecdsa
))) == NULL
) {
2006 ret
= SSH_ERR_ALLOC_FAIL
;
2009 if (sshbuf_get_ec(b
, q
, EC_KEY_get0_group(key
->ecdsa
)) != 0) {
2010 ret
= SSH_ERR_INVALID_FORMAT
;
2013 if (sshkey_ec_validate_public(EC_KEY_get0_group(key
->ecdsa
),
2015 ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
2018 if (EC_KEY_set_public_key(key
->ecdsa
, q
) != 1) {
2019 /* XXX assume it is a allocation error */
2020 ret
= SSH_ERR_ALLOC_FAIL
;
2024 sshkey_dump_ec_point(EC_KEY_get0_group(key
->ecdsa
), q
);
2027 # endif /* OPENSSL_HAS_ECC */
2028 #endif /* WITH_OPENSSL */
2029 case KEY_ED25519_CERT
:
2030 if (sshbuf_get_string_direct(b
, NULL
, NULL
) != 0) {
2031 ret
= SSH_ERR_INVALID_FORMAT
;
2036 if ((ret
= sshbuf_get_string(b
, &pk
, &len
)) != 0)
2038 if (len
!= ED25519_PK_SZ
) {
2039 ret
= SSH_ERR_INVALID_FORMAT
;
2042 if ((key
= sshkey_new(type
)) == NULL
) {
2043 ret
= SSH_ERR_ALLOC_FAIL
;
2046 key
->ed25519_pk
= pk
;
2050 if ((key
= sshkey_new(type
)) == NULL
) {
2051 ret
= SSH_ERR_ALLOC_FAIL
;
2056 ret
= SSH_ERR_KEY_TYPE_UNKNOWN
;
2060 /* Parse certificate potion */
2061 if (sshkey_is_cert(key
) &&
2062 (ret
= cert_parse(b
, key
, blob
, blen
)) != 0)
2065 if (key
!= NULL
&& sshbuf_len(b
) != 0) {
2066 ret
= SSH_ERR_INVALID_FORMAT
;
2078 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2081 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2086 sshkey_from_blob(const u_char
*blob
, size_t blen
, struct sshkey
**keyp
)
2088 return sshkey_from_blob_internal(blob
, blen
, keyp
, 1);
2092 sshkey_sign(const struct sshkey
*key
,
2093 u_char
**sigp
, size_t *lenp
,
2094 const u_char
*data
, size_t datalen
, u_int compat
)
2100 if (datalen
> SSH_KEY_MAX_SIGN_DATA_SIZE
)
2101 return SSH_ERR_INVALID_ARGUMENT
;
2102 switch (key
->type
) {
2104 case KEY_DSA_CERT_V00
:
2107 return ssh_dss_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2108 # ifdef OPENSSL_HAS_ECC
2109 case KEY_ECDSA_CERT
:
2111 return ssh_ecdsa_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2112 # endif /* OPENSSL_HAS_ECC */
2113 case KEY_RSA_CERT_V00
:
2116 return ssh_rsa_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2117 #endif /* WITH_OPENSSL */
2119 case KEY_ED25519_CERT
:
2120 return ssh_ed25519_sign(key
, sigp
, lenp
, data
, datalen
, compat
);
2122 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2127 * ssh_key_verify returns 0 for a correct signature and < 0 on error.
2130 sshkey_verify(const struct sshkey
*key
,
2131 const u_char
*sig
, size_t siglen
,
2132 const u_char
*data
, size_t dlen
, u_int compat
)
2137 if (dlen
> SSH_KEY_MAX_SIGN_DATA_SIZE
)
2138 return SSH_ERR_INVALID_ARGUMENT
;
2139 switch (key
->type
) {
2141 case KEY_DSA_CERT_V00
:
2144 return ssh_dss_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2145 # ifdef OPENSSL_HAS_ECC
2146 case KEY_ECDSA_CERT
:
2148 return ssh_ecdsa_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2149 # endif /* OPENSSL_HAS_ECC */
2150 case KEY_RSA_CERT_V00
:
2153 return ssh_rsa_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2154 #endif /* WITH_OPENSSL */
2156 case KEY_ED25519_CERT
:
2157 return ssh_ed25519_verify(key
, sig
, siglen
, data
, dlen
, compat
);
2159 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2163 /* Converts a private to a public key */
2165 sshkey_demote(const struct sshkey
*k
, struct sshkey
**dkp
)
2168 int ret
= SSH_ERR_INTERNAL_ERROR
;
2173 if ((pk
= calloc(1, sizeof(*pk
))) == NULL
)
2174 return SSH_ERR_ALLOC_FAIL
;
2176 pk
->flags
= k
->flags
;
2177 pk
->ecdsa_nid
= k
->ecdsa_nid
;
2181 pk
->ed25519_pk
= NULL
;
2182 pk
->ed25519_sk
= NULL
;
2186 case KEY_RSA_CERT_V00
:
2188 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2193 if ((pk
->rsa
= RSA_new()) == NULL
||
2194 (pk
->rsa
->e
= BN_dup(k
->rsa
->e
)) == NULL
||
2195 (pk
->rsa
->n
= BN_dup(k
->rsa
->n
)) == NULL
) {
2196 ret
= SSH_ERR_ALLOC_FAIL
;
2200 case KEY_DSA_CERT_V00
:
2202 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2206 if ((pk
->dsa
= DSA_new()) == NULL
||
2207 (pk
->dsa
->p
= BN_dup(k
->dsa
->p
)) == NULL
||
2208 (pk
->dsa
->q
= BN_dup(k
->dsa
->q
)) == NULL
||
2209 (pk
->dsa
->g
= BN_dup(k
->dsa
->g
)) == NULL
||
2210 (pk
->dsa
->pub_key
= BN_dup(k
->dsa
->pub_key
)) == NULL
) {
2211 ret
= SSH_ERR_ALLOC_FAIL
;
2215 case KEY_ECDSA_CERT
:
2216 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2219 # ifdef OPENSSL_HAS_ECC
2221 pk
->ecdsa
= EC_KEY_new_by_curve_name(pk
->ecdsa_nid
);
2222 if (pk
->ecdsa
== NULL
) {
2223 ret
= SSH_ERR_ALLOC_FAIL
;
2226 if (EC_KEY_set_public_key(pk
->ecdsa
,
2227 EC_KEY_get0_public_key(k
->ecdsa
)) != 1) {
2228 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2232 # endif /* OPENSSL_HAS_ECC */
2233 #endif /* WITH_OPENSSL */
2234 case KEY_ED25519_CERT
:
2235 if ((ret
= sshkey_cert_copy(k
, pk
)) != 0)
2239 if (k
->ed25519_pk
!= NULL
) {
2240 if ((pk
->ed25519_pk
= malloc(ED25519_PK_SZ
)) == NULL
) {
2241 ret
= SSH_ERR_ALLOC_FAIL
;
2244 memcpy(pk
->ed25519_pk
, k
->ed25519_pk
, ED25519_PK_SZ
);
2248 ret
= SSH_ERR_KEY_TYPE_UNKNOWN
;
2257 /* Convert a plain key to their _CERT equivalent */
2259 sshkey_to_certified(struct sshkey
*k
, int legacy
)
2266 newtype
= legacy
? KEY_RSA_CERT_V00
: KEY_RSA_CERT
;
2269 newtype
= legacy
? KEY_DSA_CERT_V00
: KEY_DSA_CERT
;
2273 return SSH_ERR_INVALID_ARGUMENT
;
2274 newtype
= KEY_ECDSA_CERT
;
2276 #endif /* WITH_OPENSSL */
2279 return SSH_ERR_INVALID_ARGUMENT
;
2280 newtype
= KEY_ED25519_CERT
;
2283 return SSH_ERR_INVALID_ARGUMENT
;
2285 if ((k
->cert
= cert_new()) == NULL
)
2286 return SSH_ERR_ALLOC_FAIL
;
2291 /* Convert a certificate to its raw key equivalent */
2293 sshkey_drop_cert(struct sshkey
*k
)
2295 if (!sshkey_type_is_cert(k
->type
))
2296 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2299 k
->type
= sshkey_type_plain(k
->type
);
2303 /* Sign a certified key, (re-)generating the signed certblob. */
2305 sshkey_certify(struct sshkey
*k
, struct sshkey
*ca
)
2307 struct sshbuf
*principals
= NULL
;
2308 u_char
*ca_blob
= NULL
, *sig_blob
= NULL
, nonce
[32];
2309 size_t i
, ca_len
, sig_len
;
2310 int ret
= SSH_ERR_INTERNAL_ERROR
;
2311 struct sshbuf
*cert
;
2313 if (k
== NULL
|| k
->cert
== NULL
||
2314 k
->cert
->certblob
== NULL
|| ca
== NULL
)
2315 return SSH_ERR_INVALID_ARGUMENT
;
2316 if (!sshkey_is_cert(k
))
2317 return SSH_ERR_KEY_TYPE_UNKNOWN
;
2318 if (!sshkey_type_is_valid_ca(ca
->type
))
2319 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
2321 if ((ret
= sshkey_to_blob(ca
, &ca_blob
, &ca_len
)) != 0)
2322 return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY
;
2324 cert
= k
->cert
->certblob
; /* for readability */
2326 if ((ret
= sshbuf_put_cstring(cert
, sshkey_ssh_name(k
))) != 0)
2329 /* -v01 certs put nonce first */
2330 arc4random_buf(&nonce
, sizeof(nonce
));
2331 if (!sshkey_cert_is_legacy(k
)) {
2332 if ((ret
= sshbuf_put_string(cert
, nonce
, sizeof(nonce
))) != 0)
2336 /* XXX this substantially duplicates to_blob(); refactor */
2339 case KEY_DSA_CERT_V00
:
2341 if ((ret
= sshbuf_put_bignum2(cert
, k
->dsa
->p
)) != 0 ||
2342 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->q
)) != 0 ||
2343 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->g
)) != 0 ||
2344 (ret
= sshbuf_put_bignum2(cert
, k
->dsa
->pub_key
)) != 0)
2347 # ifdef OPENSSL_HAS_ECC
2348 case KEY_ECDSA_CERT
:
2349 if ((ret
= sshbuf_put_cstring(cert
,
2350 sshkey_curve_nid_to_name(k
->ecdsa_nid
))) != 0 ||
2351 (ret
= sshbuf_put_ec(cert
,
2352 EC_KEY_get0_public_key(k
->ecdsa
),
2353 EC_KEY_get0_group(k
->ecdsa
))) != 0)
2356 # endif /* OPENSSL_HAS_ECC */
2357 case KEY_RSA_CERT_V00
:
2359 if ((ret
= sshbuf_put_bignum2(cert
, k
->rsa
->e
)) != 0 ||
2360 (ret
= sshbuf_put_bignum2(cert
, k
->rsa
->n
)) != 0)
2363 #endif /* WITH_OPENSSL */
2364 case KEY_ED25519_CERT
:
2365 if ((ret
= sshbuf_put_string(cert
,
2366 k
->ed25519_pk
, ED25519_PK_SZ
)) != 0)
2370 ret
= SSH_ERR_INVALID_ARGUMENT
;
2373 /* -v01 certs have a serial number next */
2374 if (!sshkey_cert_is_legacy(k
)) {
2375 if ((ret
= sshbuf_put_u64(cert
, k
->cert
->serial
)) != 0)
2379 if ((ret
= sshbuf_put_u32(cert
, k
->cert
->type
)) != 0 ||
2380 (ret
= sshbuf_put_cstring(cert
, k
->cert
->key_id
)) != 0)
2383 if ((principals
= sshbuf_new()) == NULL
) {
2384 ret
= SSH_ERR_ALLOC_FAIL
;
2387 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
2388 if ((ret
= sshbuf_put_cstring(principals
,
2389 k
->cert
->principals
[i
])) != 0)
2392 if ((ret
= sshbuf_put_stringb(cert
, principals
)) != 0 ||
2393 (ret
= sshbuf_put_u64(cert
, k
->cert
->valid_after
)) != 0 ||
2394 (ret
= sshbuf_put_u64(cert
, k
->cert
->valid_before
)) != 0 ||
2395 (ret
= sshbuf_put_stringb(cert
, k
->cert
->critical
)) != 0)
2398 /* -v01 certs have non-critical options here */
2399 if (!sshkey_cert_is_legacy(k
)) {
2400 if ((ret
= sshbuf_put_stringb(cert
, k
->cert
->extensions
)) != 0)
2404 /* -v00 certs put the nonce at the end */
2405 if (sshkey_cert_is_legacy(k
)) {
2406 if ((ret
= sshbuf_put_string(cert
, nonce
, sizeof(nonce
))) != 0)
2410 if ((ret
= sshbuf_put_string(cert
, NULL
, 0)) != 0 || /* Reserved */
2411 (ret
= sshbuf_put_string(cert
, ca_blob
, ca_len
)) != 0)
2414 /* Sign the whole mess */
2415 if ((ret
= sshkey_sign(ca
, &sig_blob
, &sig_len
, sshbuf_ptr(cert
),
2416 sshbuf_len(cert
), 0)) != 0)
2419 /* Append signature and we are done */
2420 if ((ret
= sshbuf_put_string(cert
, sig_blob
, sig_len
)) != 0)
2426 if (sig_blob
!= NULL
)
2428 if (ca_blob
!= NULL
)
2430 if (principals
!= NULL
)
2431 sshbuf_free(principals
);
2436 sshkey_cert_check_authority(const struct sshkey
*k
,
2437 int want_host
, int require_principal
,
2438 const char *name
, const char **reason
)
2440 u_int i
, principal_matches
;
2441 time_t now
= time(NULL
);
2447 if (k
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
2448 *reason
= "Certificate invalid: not a host certificate";
2449 return SSH_ERR_KEY_CERT_INVALID
;
2452 if (k
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
2453 *reason
= "Certificate invalid: not a user certificate";
2454 return SSH_ERR_KEY_CERT_INVALID
;
2458 /* yikes - system clock before epoch! */
2459 *reason
= "Certificate invalid: not yet valid";
2460 return SSH_ERR_KEY_CERT_INVALID
;
2462 if ((u_int64_t
)now
< k
->cert
->valid_after
) {
2463 *reason
= "Certificate invalid: not yet valid";
2464 return SSH_ERR_KEY_CERT_INVALID
;
2466 if ((u_int64_t
)now
>= k
->cert
->valid_before
) {
2467 *reason
= "Certificate invalid: expired";
2468 return SSH_ERR_KEY_CERT_INVALID
;
2470 if (k
->cert
->nprincipals
== 0) {
2471 if (require_principal
) {
2472 *reason
= "Certificate lacks principal list";
2473 return SSH_ERR_KEY_CERT_INVALID
;
2475 } else if (name
!= NULL
) {
2476 principal_matches
= 0;
2477 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
2478 if (strcmp(name
, k
->cert
->principals
[i
]) == 0) {
2479 principal_matches
= 1;
2483 if (!principal_matches
) {
2484 *reason
= "Certificate invalid: name is not a listed "
2486 return SSH_ERR_KEY_CERT_INVALID
;
2493 sshkey_private_serialize(const struct sshkey
*key
, struct sshbuf
*b
)
2495 int r
= SSH_ERR_INTERNAL_ERROR
;
2497 if ((r
= sshbuf_put_cstring(b
, sshkey_ssh_name(key
))) != 0)
2499 switch (key
->type
) {
2502 if ((r
= sshbuf_put_bignum2(b
, key
->rsa
->n
)) != 0 ||
2503 (r
= sshbuf_put_bignum2(b
, key
->rsa
->e
)) != 0 ||
2504 (r
= sshbuf_put_bignum2(b
, key
->rsa
->d
)) != 0 ||
2505 (r
= sshbuf_put_bignum2(b
, key
->rsa
->iqmp
)) != 0 ||
2506 (r
= sshbuf_put_bignum2(b
, key
->rsa
->p
)) != 0 ||
2507 (r
= sshbuf_put_bignum2(b
, key
->rsa
->q
)) != 0)
2510 case KEY_RSA_CERT_V00
:
2512 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2513 r
= SSH_ERR_INVALID_ARGUMENT
;
2516 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2517 (r
= sshbuf_put_bignum2(b
, key
->rsa
->d
)) != 0 ||
2518 (r
= sshbuf_put_bignum2(b
, key
->rsa
->iqmp
)) != 0 ||
2519 (r
= sshbuf_put_bignum2(b
, key
->rsa
->p
)) != 0 ||
2520 (r
= sshbuf_put_bignum2(b
, key
->rsa
->q
)) != 0)
2524 if ((r
= sshbuf_put_bignum2(b
, key
->dsa
->p
)) != 0 ||
2525 (r
= sshbuf_put_bignum2(b
, key
->dsa
->q
)) != 0 ||
2526 (r
= sshbuf_put_bignum2(b
, key
->dsa
->g
)) != 0 ||
2527 (r
= sshbuf_put_bignum2(b
, key
->dsa
->pub_key
)) != 0 ||
2528 (r
= sshbuf_put_bignum2(b
, key
->dsa
->priv_key
)) != 0)
2531 case KEY_DSA_CERT_V00
:
2533 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2534 r
= SSH_ERR_INVALID_ARGUMENT
;
2537 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2538 (r
= sshbuf_put_bignum2(b
, key
->dsa
->priv_key
)) != 0)
2541 # ifdef OPENSSL_HAS_ECC
2543 if ((r
= sshbuf_put_cstring(b
,
2544 sshkey_curve_nid_to_name(key
->ecdsa_nid
))) != 0 ||
2545 (r
= sshbuf_put_eckey(b
, key
->ecdsa
)) != 0 ||
2546 (r
= sshbuf_put_bignum2(b
,
2547 EC_KEY_get0_private_key(key
->ecdsa
))) != 0)
2550 case KEY_ECDSA_CERT
:
2551 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2552 r
= SSH_ERR_INVALID_ARGUMENT
;
2555 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2556 (r
= sshbuf_put_bignum2(b
,
2557 EC_KEY_get0_private_key(key
->ecdsa
))) != 0)
2560 # endif /* OPENSSL_HAS_ECC */
2561 #endif /* WITH_OPENSSL */
2563 if ((r
= sshbuf_put_string(b
, key
->ed25519_pk
,
2564 ED25519_PK_SZ
)) != 0 ||
2565 (r
= sshbuf_put_string(b
, key
->ed25519_sk
,
2566 ED25519_SK_SZ
)) != 0)
2569 case KEY_ED25519_CERT
:
2570 if (key
->cert
== NULL
|| sshbuf_len(key
->cert
->certblob
) == 0) {
2571 r
= SSH_ERR_INVALID_ARGUMENT
;
2574 if ((r
= sshbuf_put_stringb(b
, key
->cert
->certblob
)) != 0 ||
2575 (r
= sshbuf_put_string(b
, key
->ed25519_pk
,
2576 ED25519_PK_SZ
)) != 0 ||
2577 (r
= sshbuf_put_string(b
, key
->ed25519_sk
,
2578 ED25519_SK_SZ
)) != 0)
2582 r
= SSH_ERR_INVALID_ARGUMENT
;
2592 sshkey_private_deserialize(struct sshbuf
*buf
, struct sshkey
**kp
)
2594 char *tname
= NULL
, *curve
= NULL
;
2595 struct sshkey
*k
= NULL
;
2597 size_t len
, pklen
= 0, sklen
= 0;
2598 int type
, r
= SSH_ERR_INTERNAL_ERROR
;
2599 u_char
*ed25519_pk
= NULL
, *ed25519_sk
= NULL
;
2601 BIGNUM
*exponent
= NULL
;
2602 #endif /* WITH_OPENSSL */
2606 if ((r
= sshbuf_get_cstring(buf
, &tname
, NULL
)) != 0)
2608 type
= sshkey_type_from_name(tname
);
2612 if ((k
= sshkey_new_private(type
)) == NULL
) {
2613 r
= SSH_ERR_ALLOC_FAIL
;
2616 if ((r
= sshbuf_get_bignum2(buf
, k
->dsa
->p
)) != 0 ||
2617 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->q
)) != 0 ||
2618 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->g
)) != 0 ||
2619 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->pub_key
)) != 0 ||
2620 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->priv_key
)) != 0)
2623 case KEY_DSA_CERT_V00
:
2625 if ((r
= sshbuf_get_string_direct(buf
, &cert
, &len
)) != 0 ||
2626 (r
= sshkey_from_blob(cert
, len
, &k
)) != 0 ||
2627 (r
= sshkey_add_private(k
)) != 0 ||
2628 (r
= sshbuf_get_bignum2(buf
, k
->dsa
->priv_key
)) != 0)
2631 # ifdef OPENSSL_HAS_ECC
2633 if ((k
= sshkey_new_private(type
)) == NULL
) {
2634 r
= SSH_ERR_ALLOC_FAIL
;
2637 if ((k
->ecdsa_nid
= sshkey_ecdsa_nid_from_name(tname
)) == -1) {
2638 r
= SSH_ERR_INVALID_ARGUMENT
;
2641 if ((r
= sshbuf_get_cstring(buf
, &curve
, NULL
)) != 0)
2643 if (k
->ecdsa_nid
!= sshkey_curve_name_to_nid(curve
)) {
2644 r
= SSH_ERR_EC_CURVE_MISMATCH
;
2647 k
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
);
2648 if (k
->ecdsa
== NULL
|| (exponent
= BN_new()) == NULL
) {
2649 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2652 if ((r
= sshbuf_get_eckey(buf
, k
->ecdsa
)) != 0 ||
2653 (r
= sshbuf_get_bignum2(buf
, exponent
)))
2655 if (EC_KEY_set_private_key(k
->ecdsa
, exponent
) != 1) {
2656 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2659 if ((r
= sshkey_ec_validate_public(EC_KEY_get0_group(k
->ecdsa
),
2660 EC_KEY_get0_public_key(k
->ecdsa
)) != 0) ||
2661 (r
= sshkey_ec_validate_private(k
->ecdsa
)) != 0)
2664 case KEY_ECDSA_CERT
:
2665 if ((exponent
= BN_new()) == NULL
) {
2666 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2669 if ((r
= sshbuf_get_string_direct(buf
, &cert
, &len
)) != 0 ||
2670 (r
= sshkey_from_blob(cert
, len
, &k
)) != 0 ||
2671 (r
= sshkey_add_private(k
)) != 0 ||
2672 (r
= sshbuf_get_bignum2(buf
, exponent
)) != 0)
2674 if (EC_KEY_set_private_key(k
->ecdsa
, exponent
) != 1) {
2675 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2678 if ((r
= sshkey_ec_validate_public(EC_KEY_get0_group(k
->ecdsa
),
2679 EC_KEY_get0_public_key(k
->ecdsa
)) != 0) ||
2680 (r
= sshkey_ec_validate_private(k
->ecdsa
)) != 0)
2683 # endif /* OPENSSL_HAS_ECC */
2685 if ((k
= sshkey_new_private(type
)) == NULL
) {
2686 r
= SSH_ERR_ALLOC_FAIL
;
2689 if ((r
= sshbuf_get_bignum2(buf
, k
->rsa
->n
)) != 0 ||
2690 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->e
)) != 0 ||
2691 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->d
)) != 0 ||
2692 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->iqmp
)) != 0 ||
2693 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->p
)) != 0 ||
2694 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->q
)) != 0 ||
2695 (r
= rsa_generate_additional_parameters(k
->rsa
)) != 0)
2698 case KEY_RSA_CERT_V00
:
2700 if ((r
= sshbuf_get_string_direct(buf
, &cert
, &len
)) != 0 ||
2701 (r
= sshkey_from_blob(cert
, len
, &k
)) != 0 ||
2702 (r
= sshkey_add_private(k
)) != 0 ||
2703 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->d
) != 0) ||
2704 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->iqmp
) != 0) ||
2705 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->p
) != 0) ||
2706 (r
= sshbuf_get_bignum2(buf
, k
->rsa
->q
) != 0) ||
2707 (r
= rsa_generate_additional_parameters(k
->rsa
)) != 0)
2710 #endif /* WITH_OPENSSL */
2712 if ((k
= sshkey_new_private(type
)) == NULL
) {
2713 r
= SSH_ERR_ALLOC_FAIL
;
2716 if ((r
= sshbuf_get_string(buf
, &ed25519_pk
, &pklen
)) != 0 ||
2717 (r
= sshbuf_get_string(buf
, &ed25519_sk
, &sklen
)) != 0)
2719 if (pklen
!= ED25519_PK_SZ
|| sklen
!= ED25519_SK_SZ
) {
2720 r
= SSH_ERR_INVALID_FORMAT
;
2723 k
->ed25519_pk
= ed25519_pk
;
2724 k
->ed25519_sk
= ed25519_sk
;
2725 ed25519_pk
= ed25519_sk
= NULL
;
2727 case KEY_ED25519_CERT
:
2728 if ((r
= sshbuf_get_string_direct(buf
, &cert
, &len
)) != 0 ||
2729 (r
= sshkey_from_blob(cert
, len
, &k
)) != 0 ||
2730 (r
= sshkey_add_private(k
)) != 0 ||
2731 (r
= sshbuf_get_string(buf
, &ed25519_pk
, &pklen
)) != 0 ||
2732 (r
= sshbuf_get_string(buf
, &ed25519_sk
, &sklen
)) != 0)
2734 if (pklen
!= ED25519_PK_SZ
|| sklen
!= ED25519_SK_SZ
) {
2735 r
= SSH_ERR_INVALID_FORMAT
;
2738 k
->ed25519_pk
= ed25519_pk
;
2739 k
->ed25519_sk
= ed25519_sk
;
2740 ed25519_pk
= ed25519_sk
= NULL
;
2743 r
= SSH_ERR_KEY_TYPE_UNKNOWN
;
2747 /* enable blinding */
2750 case KEY_RSA_CERT_V00
:
2753 if (RSA_blinding_on(k
->rsa
, NULL
) != 1) {
2754 r
= SSH_ERR_LIBCRYPTO_ERROR
;
2759 #endif /* WITH_OPENSSL */
2770 if (exponent
!= NULL
)
2771 BN_clear_free(exponent
);
2772 #endif /* WITH_OPENSSL */
2774 if (ed25519_pk
!= NULL
) {
2775 explicit_bzero(ed25519_pk
, pklen
);
2778 if (ed25519_sk
!= NULL
) {
2779 explicit_bzero(ed25519_sk
, sklen
);
2785 #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2787 sshkey_ec_validate_public(const EC_GROUP
*group
, const EC_POINT
*public)
2790 EC_POINT
*nq
= NULL
;
2791 BIGNUM
*order
, *x
, *y
, *tmp
;
2792 int ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
2794 if ((bnctx
= BN_CTX_new()) == NULL
)
2795 return SSH_ERR_ALLOC_FAIL
;
2796 BN_CTX_start(bnctx
);
2799 * We shouldn't ever hit this case because bignum_get_ecpoint()
2800 * refuses to load GF2m points.
2802 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2803 NID_X9_62_prime_field
)
2807 if (EC_POINT_is_at_infinity(group
, public))
2810 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2811 (y
= BN_CTX_get(bnctx
)) == NULL
||
2812 (order
= BN_CTX_get(bnctx
)) == NULL
||
2813 (tmp
= BN_CTX_get(bnctx
)) == NULL
) {
2814 ret
= SSH_ERR_ALLOC_FAIL
;
2818 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2819 if (EC_GROUP_get_order(group
, order
, bnctx
) != 1 ||
2820 EC_POINT_get_affine_coordinates_GFp(group
, public,
2821 x
, y
, bnctx
) != 1) {
2822 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2825 if (BN_num_bits(x
) <= BN_num_bits(order
) / 2 ||
2826 BN_num_bits(y
) <= BN_num_bits(order
) / 2)
2829 /* nQ == infinity (n == order of subgroup) */
2830 if ((nq
= EC_POINT_new(group
)) == NULL
) {
2831 ret
= SSH_ERR_ALLOC_FAIL
;
2834 if (EC_POINT_mul(group
, nq
, NULL
, public, order
, bnctx
) != 1) {
2835 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2838 if (EC_POINT_is_at_infinity(group
, nq
) != 1)
2841 /* x < order - 1, y < order - 1 */
2842 if (!BN_sub(tmp
, order
, BN_value_one())) {
2843 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2846 if (BN_cmp(x
, tmp
) >= 0 || BN_cmp(y
, tmp
) >= 0)
2857 sshkey_ec_validate_private(const EC_KEY
*key
)
2860 BIGNUM
*order
, *tmp
;
2861 int ret
= SSH_ERR_KEY_INVALID_EC_VALUE
;
2863 if ((bnctx
= BN_CTX_new()) == NULL
)
2864 return SSH_ERR_ALLOC_FAIL
;
2865 BN_CTX_start(bnctx
);
2867 if ((order
= BN_CTX_get(bnctx
)) == NULL
||
2868 (tmp
= BN_CTX_get(bnctx
)) == NULL
) {
2869 ret
= SSH_ERR_ALLOC_FAIL
;
2873 /* log2(private) > log2(order)/2 */
2874 if (EC_GROUP_get_order(EC_KEY_get0_group(key
), order
, bnctx
) != 1) {
2875 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2878 if (BN_num_bits(EC_KEY_get0_private_key(key
)) <=
2879 BN_num_bits(order
) / 2)
2882 /* private < order - 1 */
2883 if (!BN_sub(tmp
, order
, BN_value_one())) {
2884 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
2887 if (BN_cmp(EC_KEY_get0_private_key(key
), tmp
) >= 0)
2896 sshkey_dump_ec_point(const EC_GROUP
*group
, const EC_POINT
*point
)
2901 if (point
== NULL
) {
2902 fputs("point=(NULL)\n", stderr
);
2905 if ((bnctx
= BN_CTX_new()) == NULL
) {
2906 fprintf(stderr
, "%s: BN_CTX_new failed\n", __func__
);
2909 BN_CTX_start(bnctx
);
2910 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2911 (y
= BN_CTX_get(bnctx
)) == NULL
) {
2912 fprintf(stderr
, "%s: BN_CTX_get failed\n", __func__
);
2915 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2916 NID_X9_62_prime_field
) {
2917 fprintf(stderr
, "%s: group is not a prime field\n", __func__
);
2920 if (EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
,
2922 fprintf(stderr
, "%s: EC_POINT_get_affine_coordinates_GFp\n",
2926 fputs("x=", stderr
);
2927 BN_print_fp(stderr
, x
);
2928 fputs("\ny=", stderr
);
2929 BN_print_fp(stderr
, y
);
2930 fputs("\n", stderr
);
2935 sshkey_dump_ec_key(const EC_KEY
*key
)
2937 const BIGNUM
*exponent
;
2939 sshkey_dump_ec_point(EC_KEY_get0_group(key
),
2940 EC_KEY_get0_public_key(key
));
2941 fputs("exponent=", stderr
);
2942 if ((exponent
= EC_KEY_get0_private_key(key
)) == NULL
)
2943 fputs("(NULL)", stderr
);
2945 BN_print_fp(stderr
, EC_KEY_get0_private_key(key
));
2946 fputs("\n", stderr
);
2948 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2951 sshkey_private_to_blob2(const struct sshkey
*prv
, struct sshbuf
*blob
,
2952 const char *passphrase
, const char *comment
, const char *ciphername
,
2955 u_char
*cp
, *b64
= NULL
, *key
= NULL
, *pubkeyblob
= NULL
;
2956 u_char salt
[SALT_LEN
];
2957 size_t i
, pubkeylen
, keylen
, ivlen
, blocksize
, authlen
;
2959 int r
= SSH_ERR_INTERNAL_ERROR
;
2960 struct sshcipher_ctx ciphercontext
;
2961 const struct sshcipher
*cipher
;
2962 const char *kdfname
= KDFNAME
;
2963 struct sshbuf
*encoded
= NULL
, *encrypted
= NULL
, *kdf
= NULL
;
2965 memset(&ciphercontext
, 0, sizeof(ciphercontext
));
2968 rounds
= DEFAULT_ROUNDS
;
2969 if (passphrase
== NULL
|| !strlen(passphrase
)) {
2970 ciphername
= "none";
2972 } else if (ciphername
== NULL
)
2973 ciphername
= DEFAULT_CIPHERNAME
;
2974 else if (cipher_number(ciphername
) != SSH_CIPHER_SSH2
) {
2975 r
= SSH_ERR_INVALID_ARGUMENT
;
2978 if ((cipher
= cipher_by_name(ciphername
)) == NULL
) {
2979 r
= SSH_ERR_INTERNAL_ERROR
;
2983 if ((kdf
= sshbuf_new()) == NULL
||
2984 (encoded
= sshbuf_new()) == NULL
||
2985 (encrypted
= sshbuf_new()) == NULL
) {
2986 r
= SSH_ERR_ALLOC_FAIL
;
2989 blocksize
= cipher_blocksize(cipher
);
2990 keylen
= cipher_keylen(cipher
);
2991 ivlen
= cipher_ivlen(cipher
);
2992 authlen
= cipher_authlen(cipher
);
2993 if ((key
= calloc(1, keylen
+ ivlen
)) == NULL
) {
2994 r
= SSH_ERR_ALLOC_FAIL
;
2997 if (strcmp(kdfname
, "bcrypt") == 0) {
2998 arc4random_buf(salt
, SALT_LEN
);
2999 if (bcrypt_pbkdf(passphrase
, strlen(passphrase
),
3000 salt
, SALT_LEN
, key
, keylen
+ ivlen
, rounds
) < 0) {
3001 r
= SSH_ERR_INVALID_ARGUMENT
;
3004 if ((r
= sshbuf_put_string(kdf
, salt
, SALT_LEN
)) != 0 ||
3005 (r
= sshbuf_put_u32(kdf
, rounds
)) != 0)
3007 } else if (strcmp(kdfname
, "none") != 0) {
3008 /* Unsupported KDF type */
3009 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3012 if ((r
= cipher_init(&ciphercontext
, cipher
, key
, keylen
,
3013 key
+ keylen
, ivlen
, 1)) != 0)
3016 if ((r
= sshbuf_put(encoded
, AUTH_MAGIC
, sizeof(AUTH_MAGIC
))) != 0 ||
3017 (r
= sshbuf_put_cstring(encoded
, ciphername
)) != 0 ||
3018 (r
= sshbuf_put_cstring(encoded
, kdfname
)) != 0 ||
3019 (r
= sshbuf_put_stringb(encoded
, kdf
)) != 0 ||
3020 (r
= sshbuf_put_u32(encoded
, 1)) != 0 || /* number of keys */
3021 (r
= sshkey_to_blob(prv
, &pubkeyblob
, &pubkeylen
)) != 0 ||
3022 (r
= sshbuf_put_string(encoded
, pubkeyblob
, pubkeylen
)) != 0)
3025 /* set up the buffer that will be encrypted */
3027 /* Random check bytes */
3028 check
= arc4random();
3029 if ((r
= sshbuf_put_u32(encrypted
, check
)) != 0 ||
3030 (r
= sshbuf_put_u32(encrypted
, check
)) != 0)
3033 /* append private key and comment*/
3034 if ((r
= sshkey_private_serialize(prv
, encrypted
)) != 0 ||
3035 (r
= sshbuf_put_cstring(encrypted
, comment
)) != 0)
3040 while (sshbuf_len(encrypted
) % blocksize
) {
3041 if ((r
= sshbuf_put_u8(encrypted
, ++i
& 0xff)) != 0)
3045 /* length in destination buffer */
3046 if ((r
= sshbuf_put_u32(encoded
, sshbuf_len(encrypted
))) != 0)
3050 if ((r
= sshbuf_reserve(encoded
,
3051 sshbuf_len(encrypted
) + authlen
, &cp
)) != 0)
3053 if ((r
= cipher_crypt(&ciphercontext
, 0, cp
,
3054 sshbuf_ptr(encrypted
), sshbuf_len(encrypted
), 0, authlen
)) != 0)
3058 if ((b64
= sshbuf_dtob64(encoded
)) == NULL
) {
3059 r
= SSH_ERR_ALLOC_FAIL
;
3064 if ((r
= sshbuf_put(blob
, MARK_BEGIN
, MARK_BEGIN_LEN
)) != 0)
3066 for (i
= 0; i
< strlen(b64
); i
++) {
3067 if ((r
= sshbuf_put_u8(blob
, b64
[i
])) != 0)
3069 /* insert line breaks */
3070 if (i
% 70 == 69 && (r
= sshbuf_put_u8(blob
, '\n')) != 0)
3073 if (i
% 70 != 69 && (r
= sshbuf_put_u8(blob
, '\n')) != 0)
3075 if ((r
= sshbuf_put(blob
, MARK_END
, MARK_END_LEN
)) != 0)
3083 sshbuf_free(encoded
);
3084 sshbuf_free(encrypted
);
3085 cipher_cleanup(&ciphercontext
);
3086 explicit_bzero(salt
, sizeof(salt
));
3088 explicit_bzero(key
, keylen
+ ivlen
);
3091 if (pubkeyblob
!= NULL
) {
3092 explicit_bzero(pubkeyblob
, pubkeylen
);
3096 explicit_bzero(b64
, strlen(b64
));
3103 sshkey_parse_private2(struct sshbuf
*blob
, int type
, const char *passphrase
,
3104 struct sshkey
**keyp
, char **commentp
)
3106 char *comment
= NULL
, *ciphername
= NULL
, *kdfname
= NULL
;
3107 const struct sshcipher
*cipher
= NULL
;
3109 int r
= SSH_ERR_INTERNAL_ERROR
;
3111 size_t i
, keylen
= 0, ivlen
= 0, slen
= 0;
3112 struct sshbuf
*encoded
= NULL
, *decoded
= NULL
;
3113 struct sshbuf
*kdf
= NULL
, *decrypted
= NULL
;
3114 struct sshcipher_ctx ciphercontext
;
3115 struct sshkey
*k
= NULL
;
3116 u_char
*key
= NULL
, *salt
= NULL
, *dp
, pad
, last
;
3117 u_int blocksize
, rounds
, nkeys
, encrypted_len
, check1
, check2
;
3119 memset(&ciphercontext
, 0, sizeof(ciphercontext
));
3122 if (commentp
!= NULL
)
3125 if ((encoded
= sshbuf_new()) == NULL
||
3126 (decoded
= sshbuf_new()) == NULL
||
3127 (decrypted
= sshbuf_new()) == NULL
) {
3128 r
= SSH_ERR_ALLOC_FAIL
;
3132 /* check preamble */
3133 cp
= sshbuf_ptr(blob
);
3134 encoded_len
= sshbuf_len(blob
);
3135 if (encoded_len
< (MARK_BEGIN_LEN
+ MARK_END_LEN
) ||
3136 memcmp(cp
, MARK_BEGIN
, MARK_BEGIN_LEN
) != 0) {
3137 r
= SSH_ERR_INVALID_FORMAT
;
3140 cp
+= MARK_BEGIN_LEN
;
3141 encoded_len
-= MARK_BEGIN_LEN
;
3143 /* Look for end marker, removing whitespace as we go */
3144 while (encoded_len
> 0) {
3145 if (*cp
!= '\n' && *cp
!= '\r') {
3146 if ((r
= sshbuf_put_u8(encoded
, *cp
)) != 0)
3153 if (encoded_len
>= MARK_END_LEN
&&
3154 memcmp(cp
, MARK_END
, MARK_END_LEN
) == 0) {
3156 if ((r
= sshbuf_put_u8(encoded
, 0)) != 0)
3162 if (encoded_len
== 0) {
3163 r
= SSH_ERR_INVALID_FORMAT
;
3168 if ((r
= sshbuf_b64tod(decoded
, sshbuf_ptr(encoded
))) != 0)
3172 if (sshbuf_len(decoded
) < sizeof(AUTH_MAGIC
) ||
3173 memcmp(sshbuf_ptr(decoded
), AUTH_MAGIC
, sizeof(AUTH_MAGIC
))) {
3174 r
= SSH_ERR_INVALID_FORMAT
;
3177 /* parse public portion of key */
3178 if ((r
= sshbuf_consume(decoded
, sizeof(AUTH_MAGIC
))) != 0 ||
3179 (r
= sshbuf_get_cstring(decoded
, &ciphername
, NULL
)) != 0 ||
3180 (r
= sshbuf_get_cstring(decoded
, &kdfname
, NULL
)) != 0 ||
3181 (r
= sshbuf_froms(decoded
, &kdf
)) != 0 ||
3182 (r
= sshbuf_get_u32(decoded
, &nkeys
)) != 0 ||
3183 (r
= sshbuf_skip_string(decoded
)) != 0 || /* pubkey */
3184 (r
= sshbuf_get_u32(decoded
, &encrypted_len
)) != 0)
3187 if ((cipher
= cipher_by_name(ciphername
)) == NULL
) {
3188 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3191 if ((passphrase
== NULL
|| strlen(passphrase
) == 0) &&
3192 strcmp(ciphername
, "none") != 0) {
3193 /* passphrase required */
3194 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3197 if (strcmp(kdfname
, "none") != 0 && strcmp(kdfname
, "bcrypt") != 0) {
3198 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3201 if (!strcmp(kdfname
, "none") && strcmp(ciphername
, "none") != 0) {
3202 r
= SSH_ERR_INVALID_FORMAT
;
3206 /* XXX only one key supported */
3207 r
= SSH_ERR_INVALID_FORMAT
;
3211 /* check size of encrypted key blob */
3212 blocksize
= cipher_blocksize(cipher
);
3213 if (encrypted_len
< blocksize
|| (encrypted_len
% blocksize
) != 0) {
3214 r
= SSH_ERR_INVALID_FORMAT
;
3219 keylen
= cipher_keylen(cipher
);
3220 ivlen
= cipher_ivlen(cipher
);
3221 if ((key
= calloc(1, keylen
+ ivlen
)) == NULL
) {
3222 r
= SSH_ERR_ALLOC_FAIL
;
3225 if (strcmp(kdfname
, "bcrypt") == 0) {
3226 if ((r
= sshbuf_get_string(kdf
, &salt
, &slen
)) != 0 ||
3227 (r
= sshbuf_get_u32(kdf
, &rounds
)) != 0)
3229 if (bcrypt_pbkdf(passphrase
, strlen(passphrase
), salt
, slen
,
3230 key
, keylen
+ ivlen
, rounds
) < 0) {
3231 r
= SSH_ERR_INVALID_FORMAT
;
3236 /* decrypt private portion of key */
3237 if ((r
= sshbuf_reserve(decrypted
, encrypted_len
, &dp
)) != 0 ||
3238 (r
= cipher_init(&ciphercontext
, cipher
, key
, keylen
,
3239 key
+ keylen
, ivlen
, 0)) != 0)
3241 if ((r
= cipher_crypt(&ciphercontext
, 0, dp
, sshbuf_ptr(decoded
),
3242 sshbuf_len(decoded
), 0, cipher_authlen(cipher
))) != 0) {
3243 /* an integrity error here indicates an incorrect passphrase */
3244 if (r
== SSH_ERR_MAC_INVALID
)
3245 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3248 if ((r
= sshbuf_consume(decoded
, encrypted_len
)) != 0)
3250 /* there should be no trailing data */
3251 if (sshbuf_len(decoded
) != 0) {
3252 r
= SSH_ERR_INVALID_FORMAT
;
3256 /* check check bytes */
3257 if ((r
= sshbuf_get_u32(decrypted
, &check1
)) != 0 ||
3258 (r
= sshbuf_get_u32(decrypted
, &check2
)) != 0)
3260 if (check1
!= check2
) {
3261 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3265 /* Load the private key and comment */
3266 if ((r
= sshkey_private_deserialize(decrypted
, &k
)) != 0 ||
3267 (r
= sshbuf_get_cstring(decrypted
, &comment
, NULL
)) != 0)
3270 /* Check deterministic padding */
3272 while (sshbuf_len(decrypted
)) {
3273 if ((r
= sshbuf_get_u8(decrypted
, &pad
)) != 0)
3275 if (pad
!= (++i
& 0xff)) {
3276 r
= SSH_ERR_INVALID_FORMAT
;
3281 /* XXX decode pubkey and check against private */
3289 if (commentp
!= NULL
) {
3290 *commentp
= comment
;
3295 cipher_cleanup(&ciphercontext
);
3300 explicit_bzero(salt
, slen
);
3304 explicit_bzero(key
, keylen
+ ivlen
);
3307 sshbuf_free(encoded
);
3308 sshbuf_free(decoded
);
3310 sshbuf_free(decrypted
);
3317 * Serialises the authentication (private) key to a blob, encrypting it with
3318 * passphrase. The identification of the blob (lowest 64 bits of n) will
3319 * precede the key to provide identification of the key without needing a
3323 sshkey_private_rsa1_to_blob(struct sshkey
*key
, struct sshbuf
*blob
,
3324 const char *passphrase
, const char *comment
)
3326 struct sshbuf
*buffer
= NULL
, *encrypted
= NULL
;
3329 struct sshcipher_ctx ciphercontext
;
3330 const struct sshcipher
*cipher
;
3334 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
3335 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
3337 cipher_num
= (strcmp(passphrase
, "") == 0) ?
3338 SSH_CIPHER_NONE
: SSH_CIPHER_3DES
;
3339 if ((cipher
= cipher_by_number(cipher_num
)) == NULL
)
3340 return SSH_ERR_INTERNAL_ERROR
;
3342 /* This buffer is used to build the secret part of the private key. */
3343 if ((buffer
= sshbuf_new()) == NULL
)
3344 return SSH_ERR_ALLOC_FAIL
;
3346 /* Put checkbytes for checking passphrase validity. */
3347 if ((r
= sshbuf_reserve(buffer
, 4, &cp
)) != 0)
3349 arc4random_buf(cp
, 2);
3350 memcpy(cp
+ 2, cp
, 2);
3353 * Store the private key (n and e will not be stored because they
3354 * will be stored in plain text, and storing them also in encrypted
3355 * format would just give known plaintext).
3356 * Note: q and p are stored in reverse order to SSL.
3358 if ((r
= sshbuf_put_bignum1(buffer
, key
->rsa
->d
)) != 0 ||
3359 (r
= sshbuf_put_bignum1(buffer
, key
->rsa
->iqmp
)) != 0 ||
3360 (r
= sshbuf_put_bignum1(buffer
, key
->rsa
->q
)) != 0 ||
3361 (r
= sshbuf_put_bignum1(buffer
, key
->rsa
->p
)) != 0)
3364 /* Pad the part to be encrypted to a size that is a multiple of 8. */
3365 explicit_bzero(buf
, 8);
3366 if ((r
= sshbuf_put(buffer
, buf
, 8 - (sshbuf_len(buffer
) % 8))) != 0)
3369 /* This buffer will be used to contain the data in the file. */
3370 if ((encrypted
= sshbuf_new()) == NULL
) {
3371 r
= SSH_ERR_ALLOC_FAIL
;
3375 /* First store keyfile id string. */
3376 if ((r
= sshbuf_put(encrypted
, LEGACY_BEGIN
,
3377 sizeof(LEGACY_BEGIN
))) != 0)
3380 /* Store cipher type and "reserved" field. */
3381 if ((r
= sshbuf_put_u8(encrypted
, cipher_num
)) != 0 ||
3382 (r
= sshbuf_put_u32(encrypted
, 0)) != 0)
3385 /* Store public key. This will be in plain text. */
3386 if ((r
= sshbuf_put_u32(encrypted
, BN_num_bits(key
->rsa
->n
))) != 0 ||
3387 (r
= sshbuf_put_bignum1(encrypted
, key
->rsa
->n
) != 0) ||
3388 (r
= sshbuf_put_bignum1(encrypted
, key
->rsa
->e
) != 0) ||
3389 (r
= sshbuf_put_cstring(encrypted
, comment
) != 0))
3392 /* Allocate space for the private part of the key in the buffer. */
3393 if ((r
= sshbuf_reserve(encrypted
, sshbuf_len(buffer
), &cp
)) != 0)
3396 if ((r
= cipher_set_key_string(&ciphercontext
, cipher
, passphrase
,
3397 CIPHER_ENCRYPT
)) != 0)
3399 if ((r
= cipher_crypt(&ciphercontext
, 0, cp
,
3400 sshbuf_ptr(buffer
), sshbuf_len(buffer
), 0, 0)) != 0)
3402 if ((r
= cipher_cleanup(&ciphercontext
)) != 0)
3405 r
= sshbuf_putb(blob
, encrypted
);
3408 explicit_bzero(&ciphercontext
, sizeof(ciphercontext
));
3409 explicit_bzero(buf
, sizeof(buf
));
3411 sshbuf_free(buffer
);
3412 if (encrypted
!= NULL
)
3413 sshbuf_free(encrypted
);
3417 #endif /* WITH_SSH1 */
3420 /* convert SSH v2 key in OpenSSL PEM format */
3422 sshkey_private_pem_to_blob(struct sshkey
*key
, struct sshbuf
*blob
,
3423 const char *_passphrase
, const char *comment
)
3426 int blen
, len
= strlen(_passphrase
);
3427 u_char
*passphrase
= (len
> 0) ? (u_char
*)_passphrase
: NULL
;
3428 #if (OPENSSL_VERSION_NUMBER < 0x00907000L)
3429 const EVP_CIPHER
*cipher
= (len
> 0) ? EVP_des_ede3_cbc() : NULL
;
3431 const EVP_CIPHER
*cipher
= (len
> 0) ? EVP_aes_128_cbc() : NULL
;
3436 if (len
> 0 && len
<= 4)
3437 return SSH_ERR_PASSPHRASE_TOO_SHORT
;
3438 if ((bio
= BIO_new(BIO_s_mem())) == NULL
)
3439 return SSH_ERR_ALLOC_FAIL
;
3441 switch (key
->type
) {
3443 success
= PEM_write_bio_DSAPrivateKey(bio
, key
->dsa
,
3444 cipher
, passphrase
, len
, NULL
, NULL
);
3446 #ifdef OPENSSL_HAS_ECC
3448 success
= PEM_write_bio_ECPrivateKey(bio
, key
->ecdsa
,
3449 cipher
, passphrase
, len
, NULL
, NULL
);
3453 success
= PEM_write_bio_RSAPrivateKey(bio
, key
->rsa
,
3454 cipher
, passphrase
, len
, NULL
, NULL
);
3461 r
= SSH_ERR_LIBCRYPTO_ERROR
;
3464 if ((blen
= BIO_get_mem_data(bio
, &bptr
)) <= 0) {
3465 r
= SSH_ERR_INTERNAL_ERROR
;
3468 if ((r
= sshbuf_put(blob
, bptr
, blen
)) != 0)
3475 #endif /* WITH_OPENSSL */
3477 /* Serialise "key" to buffer "blob" */
3479 sshkey_private_to_fileblob(struct sshkey
*key
, struct sshbuf
*blob
,
3480 const char *passphrase
, const char *comment
,
3481 int force_new_format
, const char *new_format_cipher
, int new_format_rounds
)
3483 switch (key
->type
) {
3486 return sshkey_private_rsa1_to_blob(key
, blob
,
3487 passphrase
, comment
);
3491 if (force_new_format
) {
3492 return sshkey_private_to_blob2(key
, blob
, passphrase
,
3493 comment
, new_format_cipher
, new_format_rounds
);
3495 return sshkey_private_pem_to_blob(key
, blob
,
3496 passphrase
, comment
);
3497 #endif /* WITH_OPENSSL */
3499 return sshkey_private_to_blob2(key
, blob
, passphrase
,
3500 comment
, new_format_cipher
, new_format_rounds
);
3502 return SSH_ERR_KEY_TYPE_UNKNOWN
;
3508 * Parse the public, unencrypted portion of a RSA1 key.
3511 sshkey_parse_public_rsa1_fileblob(struct sshbuf
*blob
,
3512 struct sshkey
**keyp
, char **commentp
)
3515 struct sshkey
*pub
= NULL
;
3516 struct sshbuf
*copy
= NULL
;
3520 if (commentp
!= NULL
)
3523 /* Check that it is at least big enough to contain the ID string. */
3524 if (sshbuf_len(blob
) < sizeof(LEGACY_BEGIN
))
3525 return SSH_ERR_INVALID_FORMAT
;
3528 * Make sure it begins with the id string. Consume the id string
3531 if (memcmp(sshbuf_ptr(blob
), LEGACY_BEGIN
, sizeof(LEGACY_BEGIN
)) != 0)
3532 return SSH_ERR_INVALID_FORMAT
;
3533 /* Make a working copy of the keyblob and skip past the magic */
3534 if ((copy
= sshbuf_fromb(blob
)) == NULL
)
3535 return SSH_ERR_ALLOC_FAIL
;
3536 if ((r
= sshbuf_consume(copy
, sizeof(LEGACY_BEGIN
))) != 0)
3539 /* Skip cipher type, reserved data and key bits. */
3540 if ((r
= sshbuf_get_u8(copy
, NULL
)) != 0 || /* cipher type */
3541 (r
= sshbuf_get_u32(copy
, NULL
)) != 0 || /* reserved */
3542 (r
= sshbuf_get_u32(copy
, NULL
)) != 0) /* key bits */
3545 /* Read the public key from the buffer. */
3546 if ((pub
= sshkey_new(KEY_RSA1
)) == NULL
||
3547 (r
= sshbuf_get_bignum1(copy
, pub
->rsa
->n
)) != 0 ||
3548 (r
= sshbuf_get_bignum1(copy
, pub
->rsa
->e
)) != 0)
3551 /* Finally, the comment */
3552 if ((r
= sshbuf_get_string(copy
, (u_char
**)commentp
, NULL
)) != 0)
3555 /* The encrypted private part is not parsed by this function. */
3573 sshkey_parse_private_rsa1(struct sshbuf
*blob
, const char *passphrase
,
3574 struct sshkey
**keyp
, char **commentp
)
3577 u_int16_t check1
, check2
;
3578 u_int8_t cipher_type
;
3579 struct sshbuf
*decrypted
= NULL
, *copy
= NULL
;
3581 char *comment
= NULL
;
3582 struct sshcipher_ctx ciphercontext
;
3583 const struct sshcipher
*cipher
;
3584 struct sshkey
*prv
= NULL
;
3587 if (commentp
!= NULL
)
3590 /* Check that it is at least big enough to contain the ID string. */
3591 if (sshbuf_len(blob
) < sizeof(LEGACY_BEGIN
))
3592 return SSH_ERR_INVALID_FORMAT
;
3595 * Make sure it begins with the id string. Consume the id string
3598 if (memcmp(sshbuf_ptr(blob
), LEGACY_BEGIN
, sizeof(LEGACY_BEGIN
)) != 0)
3599 return SSH_ERR_INVALID_FORMAT
;
3601 if ((prv
= sshkey_new_private(KEY_RSA1
)) == NULL
) {
3602 r
= SSH_ERR_ALLOC_FAIL
;
3605 if ((copy
= sshbuf_fromb(blob
)) == NULL
||
3606 (decrypted
= sshbuf_new()) == NULL
) {
3607 r
= SSH_ERR_ALLOC_FAIL
;
3610 if ((r
= sshbuf_consume(copy
, sizeof(LEGACY_BEGIN
))) != 0)
3613 /* Read cipher type. */
3614 if ((r
= sshbuf_get_u8(copy
, &cipher_type
)) != 0 ||
3615 (r
= sshbuf_get_u32(copy
, NULL
)) != 0) /* reserved */
3618 /* Read the public key and comment from the buffer. */
3619 if ((r
= sshbuf_get_u32(copy
, NULL
)) != 0 || /* key bits */
3620 (r
= sshbuf_get_bignum1(copy
, prv
->rsa
->n
)) != 0 ||
3621 (r
= sshbuf_get_bignum1(copy
, prv
->rsa
->e
)) != 0 ||
3622 (r
= sshbuf_get_cstring(copy
, &comment
, NULL
)) != 0)
3625 /* Check that it is a supported cipher. */
3626 cipher
= cipher_by_number(cipher_type
);
3627 if (cipher
== NULL
) {
3628 r
= SSH_ERR_KEY_UNKNOWN_CIPHER
;
3631 /* Initialize space for decrypted data. */
3632 if ((r
= sshbuf_reserve(decrypted
, sshbuf_len(copy
), &cp
)) != 0)
3635 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
3636 if ((r
= cipher_set_key_string(&ciphercontext
, cipher
, passphrase
,
3637 CIPHER_DECRYPT
)) != 0)
3639 if ((r
= cipher_crypt(&ciphercontext
, 0, cp
,
3640 sshbuf_ptr(copy
), sshbuf_len(copy
), 0, 0)) != 0) {
3641 cipher_cleanup(&ciphercontext
);
3644 if ((r
= cipher_cleanup(&ciphercontext
)) != 0)
3647 if ((r
= sshbuf_get_u16(decrypted
, &check1
)) != 0 ||
3648 (r
= sshbuf_get_u16(decrypted
, &check2
)) != 0)
3650 if (check1
!= check2
) {
3651 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3655 /* Read the rest of the private key. */
3656 if ((r
= sshbuf_get_bignum1(decrypted
, prv
->rsa
->d
)) != 0 ||
3657 (r
= sshbuf_get_bignum1(decrypted
, prv
->rsa
->iqmp
)) != 0 ||
3658 (r
= sshbuf_get_bignum1(decrypted
, prv
->rsa
->q
)) != 0 ||
3659 (r
= sshbuf_get_bignum1(decrypted
, prv
->rsa
->p
)) != 0)
3662 /* calculate p-1 and q-1 */
3663 if ((r
= rsa_generate_additional_parameters(prv
->rsa
)) != 0)
3666 /* enable blinding */
3667 if (RSA_blinding_on(prv
->rsa
, NULL
) != 1) {
3668 r
= SSH_ERR_LIBCRYPTO_ERROR
;
3674 if (commentp
!= NULL
) {
3675 *commentp
= comment
;
3679 explicit_bzero(&ciphercontext
, sizeof(ciphercontext
));
3680 if (comment
!= NULL
)
3686 if (decrypted
!= NULL
)
3687 sshbuf_free(decrypted
);
3690 #endif /* WITH_SSH1 */
3693 /* XXX make private once ssh-keysign.c fixed */
3695 sshkey_parse_private_pem_fileblob(struct sshbuf
*blob
, int type
,
3696 const char *passphrase
, struct sshkey
**keyp
, char **commentp
)
3698 EVP_PKEY
*pk
= NULL
;
3699 struct sshkey
*prv
= NULL
;
3700 char *name
= "<no key>";
3705 if (commentp
!= NULL
)
3708 if ((bio
= BIO_new(BIO_s_mem())) == NULL
|| sshbuf_len(blob
) > INT_MAX
)
3709 return SSH_ERR_ALLOC_FAIL
;
3710 if (BIO_write(bio
, sshbuf_ptr(blob
), sshbuf_len(blob
)) !=
3711 (int)sshbuf_len(blob
)) {
3712 r
= SSH_ERR_ALLOC_FAIL
;
3716 if ((pk
= PEM_read_bio_PrivateKey(bio
, NULL
, NULL
,
3717 (char *)passphrase
)) == NULL
) {
3718 r
= SSH_ERR_KEY_WRONG_PASSPHRASE
;
3721 if (pk
->type
== EVP_PKEY_RSA
&&
3722 (type
== KEY_UNSPEC
|| type
== KEY_RSA
)) {
3723 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3724 r
= SSH_ERR_ALLOC_FAIL
;
3727 prv
->rsa
= EVP_PKEY_get1_RSA(pk
);
3728 prv
->type
= KEY_RSA
;
3729 name
= "rsa w/o comment";
3731 RSA_print_fp(stderr
, prv
->rsa
, 8);
3733 if (RSA_blinding_on(prv
->rsa
, NULL
) != 1) {
3734 r
= SSH_ERR_LIBCRYPTO_ERROR
;
3737 } else if (pk
->type
== EVP_PKEY_DSA
&&
3738 (type
== KEY_UNSPEC
|| type
== KEY_DSA
)) {
3739 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3740 r
= SSH_ERR_ALLOC_FAIL
;
3743 prv
->dsa
= EVP_PKEY_get1_DSA(pk
);
3744 prv
->type
= KEY_DSA
;
3745 name
= "dsa w/o comment";
3747 DSA_print_fp(stderr
, prv
->dsa
, 8);
3749 #ifdef OPENSSL_HAS_ECC
3750 } else if (pk
->type
== EVP_PKEY_EC
&&
3751 (type
== KEY_UNSPEC
|| type
== KEY_ECDSA
)) {
3752 if ((prv
= sshkey_new(KEY_UNSPEC
)) == NULL
) {
3753 r
= SSH_ERR_ALLOC_FAIL
;
3756 prv
->ecdsa
= EVP_PKEY_get1_EC_KEY(pk
);
3757 prv
->type
= KEY_ECDSA
;
3758 prv
->ecdsa_nid
= sshkey_ecdsa_key_to_nid(prv
->ecdsa
);
3759 if (prv
->ecdsa_nid
== -1 ||
3760 sshkey_curve_nid_to_name(prv
->ecdsa_nid
) == NULL
||
3761 sshkey_ec_validate_public(EC_KEY_get0_group(prv
->ecdsa
),
3762 EC_KEY_get0_public_key(prv
->ecdsa
)) != 0 ||
3763 sshkey_ec_validate_private(prv
->ecdsa
) != 0) {
3764 r
= SSH_ERR_INVALID_FORMAT
;
3767 name
= "ecdsa w/o comment";
3769 if (prv
!= NULL
&& prv
->ecdsa
!= NULL
)
3770 sshkey_dump_ec_key(prv
->ecdsa
);
3772 #endif /* OPENSSL_HAS_ECC */
3774 r
= SSH_ERR_INVALID_FORMAT
;
3777 if (commentp
!= NULL
&&
3778 (*commentp
= strdup(name
)) == NULL
) {
3779 r
= SSH_ERR_ALLOC_FAIL
;
3793 #endif /* WITH_OPENSSL */
3796 sshkey_parse_private_fileblob_type(struct sshbuf
*blob
, int type
,
3797 const char *passphrase
, struct sshkey
**keyp
, char **commentp
)
3802 if (commentp
!= NULL
)
3808 return sshkey_parse_private_rsa1(blob
, passphrase
,
3813 return sshkey_parse_private_pem_fileblob(blob
, type
, passphrase
,
3815 #endif /* WITH_OPENSSL */
3817 return sshkey_parse_private2(blob
, type
, passphrase
,
3820 if ((r
= sshkey_parse_private2(blob
, type
, passphrase
, keyp
,
3824 return sshkey_parse_private_pem_fileblob(blob
, type
, passphrase
,
3827 return SSH_ERR_INVALID_FORMAT
;
3828 #endif /* WITH_OPENSSL */
3830 return SSH_ERR_KEY_TYPE_UNKNOWN
;
3835 sshkey_parse_private_fileblob(struct sshbuf
*buffer
, const char *passphrase
,
3836 const char *filename
, struct sshkey
**keyp
, char **commentp
)
3842 if (commentp
!= NULL
)
3846 /* it's a SSH v1 key if the public key part is readable */
3847 if ((r
= sshkey_parse_public_rsa1_fileblob(buffer
, NULL
, NULL
)) == 0) {
3848 return sshkey_parse_private_fileblob_type(buffer
, KEY_RSA1
,
3849 passphrase
, keyp
, commentp
);
3851 #endif /* WITH_SSH1 */
3852 if ((r
= sshkey_parse_private_fileblob_type(buffer
, KEY_UNSPEC
,
3853 passphrase
, keyp
, commentp
)) == 0)