xref: /freebsd/crypto/openssh/sshkey.c (revision a91a2465)
1a91a2465SEd Maste /* $OpenBSD: sshkey.c,v 1.142 2024/01/11 01:45:36 djm Exp $ */
2a0ee8cc6SDag-Erling Smørgrav /*
3a0ee8cc6SDag-Erling Smørgrav  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
4a0ee8cc6SDag-Erling Smørgrav  * Copyright (c) 2008 Alexander von Gernler.  All rights reserved.
5a0ee8cc6SDag-Erling Smørgrav  * Copyright (c) 2010,2011 Damien Miller.  All rights reserved.
6a0ee8cc6SDag-Erling Smørgrav  *
7a0ee8cc6SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
8a0ee8cc6SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
9a0ee8cc6SDag-Erling Smørgrav  * are met:
10a0ee8cc6SDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
11a0ee8cc6SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
12a0ee8cc6SDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
13a0ee8cc6SDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
14a0ee8cc6SDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
15a0ee8cc6SDag-Erling Smørgrav  *
16a0ee8cc6SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a0ee8cc6SDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a0ee8cc6SDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a0ee8cc6SDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a0ee8cc6SDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21a0ee8cc6SDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22a0ee8cc6SDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23a0ee8cc6SDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24a0ee8cc6SDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25a0ee8cc6SDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26a0ee8cc6SDag-Erling Smørgrav  */
27a0ee8cc6SDag-Erling Smørgrav 
28a0ee8cc6SDag-Erling Smørgrav #include "includes.h"
29a0ee8cc6SDag-Erling Smørgrav 
30a0ee8cc6SDag-Erling Smørgrav #include <sys/types.h>
31bc5531deSDag-Erling Smørgrav #include <netinet/in.h>
32a0ee8cc6SDag-Erling Smørgrav 
33bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
34a0ee8cc6SDag-Erling Smørgrav #include <openssl/evp.h>
35a0ee8cc6SDag-Erling Smørgrav #include <openssl/err.h>
36a0ee8cc6SDag-Erling Smørgrav #include <openssl/pem.h>
37bc5531deSDag-Erling Smørgrav #endif
38a0ee8cc6SDag-Erling Smørgrav 
39a0ee8cc6SDag-Erling Smørgrav #include "crypto_api.h"
40a0ee8cc6SDag-Erling Smørgrav 
41a0ee8cc6SDag-Erling Smørgrav #include <errno.h>
42bc5531deSDag-Erling Smørgrav #include <limits.h>
43a0ee8cc6SDag-Erling Smørgrav #include <stdio.h>
44edf85781SEd Maste #include <stdlib.h>
45a0ee8cc6SDag-Erling Smørgrav #include <string.h>
46bc5531deSDag-Erling Smørgrav #include <resolv.h>
4719261079SEd Maste #include <time.h>
48a0ee8cc6SDag-Erling Smørgrav #ifdef HAVE_UTIL_H
49a0ee8cc6SDag-Erling Smørgrav #include <util.h>
50a0ee8cc6SDag-Erling Smørgrav #endif /* HAVE_UTIL_H */
51a0ee8cc6SDag-Erling Smørgrav 
52a0ee8cc6SDag-Erling Smørgrav #include "ssh2.h"
53a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
54a0ee8cc6SDag-Erling Smørgrav #include "misc.h"
55a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
56a0ee8cc6SDag-Erling Smørgrav #include "cipher.h"
57a0ee8cc6SDag-Erling Smørgrav #include "digest.h"
58a0ee8cc6SDag-Erling Smørgrav #define SSHKEY_INTERNAL
59a0ee8cc6SDag-Erling Smørgrav #include "sshkey.h"
60bc5531deSDag-Erling Smørgrav #include "match.h"
6119261079SEd Maste #include "ssh-sk.h"
62a0ee8cc6SDag-Erling Smørgrav 
6319261079SEd Maste #ifdef WITH_XMSS
6419261079SEd Maste #include "sshkey-xmss.h"
6547dd1d1bSDag-Erling Smørgrav #include "xmss_fast.h"
6619261079SEd Maste #endif
6747dd1d1bSDag-Erling Smørgrav 
682a01feabSEd Maste #include "openbsd-compat/openssl-compat.h"
692a01feabSEd Maste 
70a0ee8cc6SDag-Erling Smørgrav /* openssh private key file format */
71a0ee8cc6SDag-Erling Smørgrav #define MARK_BEGIN		"-----BEGIN OPENSSH PRIVATE KEY-----\n"
72a0ee8cc6SDag-Erling Smørgrav #define MARK_END		"-----END OPENSSH PRIVATE KEY-----\n"
73a0ee8cc6SDag-Erling Smørgrav #define MARK_BEGIN_LEN		(sizeof(MARK_BEGIN) - 1)
74a0ee8cc6SDag-Erling Smørgrav #define MARK_END_LEN		(sizeof(MARK_END) - 1)
75a0ee8cc6SDag-Erling Smørgrav #define KDFNAME			"bcrypt"
76a0ee8cc6SDag-Erling Smørgrav #define AUTH_MAGIC		"openssh-key-v1"
77a0ee8cc6SDag-Erling Smørgrav #define SALT_LEN		16
784f52dfbbSDag-Erling Smørgrav #define DEFAULT_CIPHERNAME	"aes256-ctr"
79535af610SEd Maste #define	DEFAULT_ROUNDS		24
80a0ee8cc6SDag-Erling Smørgrav 
81a0ee8cc6SDag-Erling Smørgrav /* Version identification string for SSH v1 identity files. */
82a0ee8cc6SDag-Erling Smørgrav #define LEGACY_BEGIN		"SSH PRIVATE KEY FILE FORMAT 1.1\n"
83a0ee8cc6SDag-Erling Smørgrav 
8419261079SEd Maste /*
8519261079SEd Maste  * Constants relating to "shielding" support; protection of keys expected
8619261079SEd Maste  * to remain in memory for long durations
8719261079SEd Maste  */
8819261079SEd Maste #define SSHKEY_SHIELD_PREKEY_LEN	(16 * 1024)
8919261079SEd Maste #define SSHKEY_SHIELD_CIPHER		"aes256-ctr" /* XXX want AES-EME* */
9019261079SEd Maste #define SSHKEY_SHIELD_PREKEY_HASH	SSH_DIGEST_SHA512
9119261079SEd Maste 
9219261079SEd Maste int	sshkey_private_serialize_opt(struct sshkey *key,
9347dd1d1bSDag-Erling Smørgrav     struct sshbuf *buf, enum sshkey_serialize_rep);
94bc5531deSDag-Erling Smørgrav static int sshkey_from_blob_internal(struct sshbuf *buf,
95a0ee8cc6SDag-Erling Smørgrav     struct sshkey **keyp, int allow_cert);
96a0ee8cc6SDag-Erling Smørgrav 
97a0ee8cc6SDag-Erling Smørgrav /* Supported key types */
98f374ba41SEd Maste extern const struct sshkey_impl sshkey_ed25519_impl;
99f374ba41SEd Maste extern const struct sshkey_impl sshkey_ed25519_cert_impl;
100f374ba41SEd Maste extern const struct sshkey_impl sshkey_ed25519_sk_impl;
101f374ba41SEd Maste extern const struct sshkey_impl sshkey_ed25519_sk_cert_impl;
102a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
103a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_ECC
1041323ec57SEd Maste #  ifdef ENABLE_SK
105f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_sk_impl;
106f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_sk_cert_impl;
107f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl;
1081323ec57SEd Maste #  endif /* ENABLE_SK */
109f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp256_impl;
110f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp256_cert_impl;
111f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp384_impl;
112f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp384_cert_impl;
113a0ee8cc6SDag-Erling Smørgrav #  ifdef OPENSSL_HAS_NISTP521
114f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp521_impl;
115f374ba41SEd Maste extern const struct sshkey_impl sshkey_ecdsa_nistp521_cert_impl;
116a0ee8cc6SDag-Erling Smørgrav #  endif /* OPENSSL_HAS_NISTP521 */
117a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_ECC */
118f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_impl;
119f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_cert_impl;
120f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_sha256_impl;
121f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_sha256_cert_impl;
122f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_sha512_impl;
123f374ba41SEd Maste extern const struct sshkey_impl sshkey_rsa_sha512_cert_impl;
124a91a2465SEd Maste # ifdef WITH_DSA
125f374ba41SEd Maste extern const struct sshkey_impl sshkey_dss_impl;
126f374ba41SEd Maste extern const struct sshkey_impl sshkey_dsa_cert_impl;
127a91a2465SEd Maste # endif
128a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
129f374ba41SEd Maste #ifdef WITH_XMSS
130f374ba41SEd Maste extern const struct sshkey_impl sshkey_xmss_impl;
131f374ba41SEd Maste extern const struct sshkey_impl sshkey_xmss_cert_impl;
132f374ba41SEd Maste #endif
133f374ba41SEd Maste 
134f374ba41SEd Maste const struct sshkey_impl * const keyimpls[] = {
135f374ba41SEd Maste 	&sshkey_ed25519_impl,
136f374ba41SEd Maste 	&sshkey_ed25519_cert_impl,
137f374ba41SEd Maste #ifdef ENABLE_SK
138f374ba41SEd Maste 	&sshkey_ed25519_sk_impl,
139f374ba41SEd Maste 	&sshkey_ed25519_sk_cert_impl,
140f374ba41SEd Maste #endif
141f374ba41SEd Maste #ifdef WITH_OPENSSL
142f374ba41SEd Maste # ifdef OPENSSL_HAS_ECC
143f374ba41SEd Maste 	&sshkey_ecdsa_nistp256_impl,
144f374ba41SEd Maste 	&sshkey_ecdsa_nistp256_cert_impl,
145f374ba41SEd Maste 	&sshkey_ecdsa_nistp384_impl,
146f374ba41SEd Maste 	&sshkey_ecdsa_nistp384_cert_impl,
147f374ba41SEd Maste #  ifdef OPENSSL_HAS_NISTP521
148f374ba41SEd Maste 	&sshkey_ecdsa_nistp521_impl,
149f374ba41SEd Maste 	&sshkey_ecdsa_nistp521_cert_impl,
150f374ba41SEd Maste #  endif /* OPENSSL_HAS_NISTP521 */
151f374ba41SEd Maste #  ifdef ENABLE_SK
152f374ba41SEd Maste 	&sshkey_ecdsa_sk_impl,
153f374ba41SEd Maste 	&sshkey_ecdsa_sk_cert_impl,
154f374ba41SEd Maste 	&sshkey_ecdsa_sk_webauthn_impl,
155f374ba41SEd Maste #  endif /* ENABLE_SK */
156f374ba41SEd Maste # endif /* OPENSSL_HAS_ECC */
157a91a2465SEd Maste # ifdef WITH_DSA
158f374ba41SEd Maste 	&sshkey_dss_impl,
159f374ba41SEd Maste 	&sshkey_dsa_cert_impl,
160a91a2465SEd Maste # endif
161f374ba41SEd Maste 	&sshkey_rsa_impl,
162f374ba41SEd Maste 	&sshkey_rsa_cert_impl,
163f374ba41SEd Maste 	&sshkey_rsa_sha256_impl,
164f374ba41SEd Maste 	&sshkey_rsa_sha256_cert_impl,
165f374ba41SEd Maste 	&sshkey_rsa_sha512_impl,
166f374ba41SEd Maste 	&sshkey_rsa_sha512_cert_impl,
167f374ba41SEd Maste #endif /* WITH_OPENSSL */
168f374ba41SEd Maste #ifdef WITH_XMSS
169f374ba41SEd Maste 	&sshkey_xmss_impl,
170f374ba41SEd Maste 	&sshkey_xmss_cert_impl,
171f374ba41SEd Maste #endif
172f374ba41SEd Maste 	NULL
173a0ee8cc6SDag-Erling Smørgrav };
174a0ee8cc6SDag-Erling Smørgrav 
175f374ba41SEd Maste static const struct sshkey_impl *
sshkey_impl_from_type(int type)176f374ba41SEd Maste sshkey_impl_from_type(int type)
177f374ba41SEd Maste {
178f374ba41SEd Maste 	int i;
179f374ba41SEd Maste 
180f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
181f374ba41SEd Maste 		if (keyimpls[i]->type == type)
182f374ba41SEd Maste 			return keyimpls[i];
183f374ba41SEd Maste 	}
184f374ba41SEd Maste 	return NULL;
185f374ba41SEd Maste }
186f374ba41SEd Maste 
187f374ba41SEd Maste static const struct sshkey_impl *
sshkey_impl_from_type_nid(int type,int nid)188f374ba41SEd Maste sshkey_impl_from_type_nid(int type, int nid)
189f374ba41SEd Maste {
190f374ba41SEd Maste 	int i;
191f374ba41SEd Maste 
192f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
193f374ba41SEd Maste 		if (keyimpls[i]->type == type &&
194f374ba41SEd Maste 		    (keyimpls[i]->nid == 0 || keyimpls[i]->nid == nid))
195f374ba41SEd Maste 			return keyimpls[i];
196f374ba41SEd Maste 	}
197f374ba41SEd Maste 	return NULL;
198f374ba41SEd Maste }
199f374ba41SEd Maste 
200f374ba41SEd Maste static const struct sshkey_impl *
sshkey_impl_from_key(const struct sshkey * k)201f374ba41SEd Maste sshkey_impl_from_key(const struct sshkey *k)
202f374ba41SEd Maste {
203f374ba41SEd Maste 	if (k == NULL)
204f374ba41SEd Maste 		return NULL;
205f374ba41SEd Maste 	return sshkey_impl_from_type_nid(k->type, k->ecdsa_nid);
206f374ba41SEd Maste }
207f374ba41SEd Maste 
208a0ee8cc6SDag-Erling Smørgrav const char *
sshkey_type(const struct sshkey * k)209a0ee8cc6SDag-Erling Smørgrav sshkey_type(const struct sshkey *k)
210a0ee8cc6SDag-Erling Smørgrav {
211f374ba41SEd Maste 	const struct sshkey_impl *impl;
212a0ee8cc6SDag-Erling Smørgrav 
213f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(k)) == NULL)
214a0ee8cc6SDag-Erling Smørgrav 		return "unknown";
215f374ba41SEd Maste 	return impl->shortname;
216a0ee8cc6SDag-Erling Smørgrav }
217a0ee8cc6SDag-Erling Smørgrav 
218a0ee8cc6SDag-Erling Smørgrav static const char *
sshkey_ssh_name_from_type_nid(int type,int nid)219a0ee8cc6SDag-Erling Smørgrav sshkey_ssh_name_from_type_nid(int type, int nid)
220a0ee8cc6SDag-Erling Smørgrav {
221f374ba41SEd Maste 	const struct sshkey_impl *impl;
222a0ee8cc6SDag-Erling Smørgrav 
223f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type_nid(type, nid)) == NULL)
224a0ee8cc6SDag-Erling Smørgrav 		return "ssh-unknown";
225f374ba41SEd Maste 	return impl->name;
226a0ee8cc6SDag-Erling Smørgrav }
227a0ee8cc6SDag-Erling Smørgrav 
228a0ee8cc6SDag-Erling Smørgrav int
sshkey_type_is_cert(int type)229a0ee8cc6SDag-Erling Smørgrav sshkey_type_is_cert(int type)
230a0ee8cc6SDag-Erling Smørgrav {
231f374ba41SEd Maste 	const struct sshkey_impl *impl;
232a0ee8cc6SDag-Erling Smørgrav 
233f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL)
234a0ee8cc6SDag-Erling Smørgrav 		return 0;
235f374ba41SEd Maste 	return impl->cert;
236a0ee8cc6SDag-Erling Smørgrav }
237a0ee8cc6SDag-Erling Smørgrav 
238a0ee8cc6SDag-Erling Smørgrav const char *
sshkey_ssh_name(const struct sshkey * k)239a0ee8cc6SDag-Erling Smørgrav sshkey_ssh_name(const struct sshkey *k)
240a0ee8cc6SDag-Erling Smørgrav {
241a0ee8cc6SDag-Erling Smørgrav 	return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
242a0ee8cc6SDag-Erling Smørgrav }
243a0ee8cc6SDag-Erling Smørgrav 
244a0ee8cc6SDag-Erling Smørgrav const char *
sshkey_ssh_name_plain(const struct sshkey * k)245a0ee8cc6SDag-Erling Smørgrav sshkey_ssh_name_plain(const struct sshkey *k)
246a0ee8cc6SDag-Erling Smørgrav {
247a0ee8cc6SDag-Erling Smørgrav 	return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
248a0ee8cc6SDag-Erling Smørgrav 	    k->ecdsa_nid);
249a0ee8cc6SDag-Erling Smørgrav }
250a0ee8cc6SDag-Erling Smørgrav 
251a0ee8cc6SDag-Erling Smørgrav int
sshkey_type_from_name(const char * name)252a0ee8cc6SDag-Erling Smørgrav sshkey_type_from_name(const char *name)
253a0ee8cc6SDag-Erling Smørgrav {
254f374ba41SEd Maste 	int i;
255f374ba41SEd Maste 	const struct sshkey_impl *impl;
256a0ee8cc6SDag-Erling Smørgrav 
257f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
258f374ba41SEd Maste 		impl = keyimpls[i];
259a0ee8cc6SDag-Erling Smørgrav 		/* Only allow shortname matches for plain key types */
260f374ba41SEd Maste 		if ((impl->name != NULL && strcmp(name, impl->name) == 0) ||
261f374ba41SEd Maste 		    (!impl->cert && strcasecmp(impl->shortname, name) == 0))
262f374ba41SEd Maste 			return impl->type;
263a0ee8cc6SDag-Erling Smørgrav 	}
264a0ee8cc6SDag-Erling Smørgrav 	return KEY_UNSPEC;
265a0ee8cc6SDag-Erling Smørgrav }
266a0ee8cc6SDag-Erling Smørgrav 
26719261079SEd Maste static int
key_type_is_ecdsa_variant(int type)26819261079SEd Maste key_type_is_ecdsa_variant(int type)
26919261079SEd Maste {
27019261079SEd Maste 	switch (type) {
27119261079SEd Maste 	case KEY_ECDSA:
27219261079SEd Maste 	case KEY_ECDSA_CERT:
27319261079SEd Maste 	case KEY_ECDSA_SK:
27419261079SEd Maste 	case KEY_ECDSA_SK_CERT:
27519261079SEd Maste 		return 1;
27619261079SEd Maste 	}
27719261079SEd Maste 	return 0;
27819261079SEd Maste }
27919261079SEd Maste 
280a0ee8cc6SDag-Erling Smørgrav int
sshkey_ecdsa_nid_from_name(const char * name)281a0ee8cc6SDag-Erling Smørgrav sshkey_ecdsa_nid_from_name(const char *name)
282a0ee8cc6SDag-Erling Smørgrav {
283f374ba41SEd Maste 	int i;
284a0ee8cc6SDag-Erling Smørgrav 
285f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
286f374ba41SEd Maste 		if (!key_type_is_ecdsa_variant(keyimpls[i]->type))
287a0ee8cc6SDag-Erling Smørgrav 			continue;
288f374ba41SEd Maste 		if (keyimpls[i]->name != NULL &&
289f374ba41SEd Maste 		    strcmp(name, keyimpls[i]->name) == 0)
290f374ba41SEd Maste 			return keyimpls[i]->nid;
291a0ee8cc6SDag-Erling Smørgrav 	}
292a0ee8cc6SDag-Erling Smørgrav 	return -1;
293a0ee8cc6SDag-Erling Smørgrav }
294a0ee8cc6SDag-Erling Smørgrav 
2951323ec57SEd Maste int
sshkey_match_keyname_to_sigalgs(const char * keyname,const char * sigalgs)2961323ec57SEd Maste sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
2971323ec57SEd Maste {
2981323ec57SEd Maste 	int ktype;
2991323ec57SEd Maste 
3001323ec57SEd Maste 	if (sigalgs == NULL || *sigalgs == '\0' ||
3011323ec57SEd Maste 	    (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
3021323ec57SEd Maste 		return 0;
3031323ec57SEd Maste 	else if (ktype == KEY_RSA) {
3041323ec57SEd Maste 		return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
3051323ec57SEd Maste 		    match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
3061323ec57SEd Maste 		    match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
3071323ec57SEd Maste 	} else if (ktype == KEY_RSA_CERT) {
3081323ec57SEd Maste 		return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
3091323ec57SEd Maste 		    sigalgs, 0) == 1 ||
3101323ec57SEd Maste 		    match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
3111323ec57SEd Maste 		    sigalgs, 0) == 1 ||
3121323ec57SEd Maste 		    match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
3131323ec57SEd Maste 		    sigalgs, 0) == 1;
3141323ec57SEd Maste 	} else
3151323ec57SEd Maste 		return match_pattern_list(keyname, sigalgs, 0) == 1;
3161323ec57SEd Maste }
3171323ec57SEd Maste 
318a0ee8cc6SDag-Erling Smørgrav char *
sshkey_alg_list(int certs_only,int plain_only,int include_sigonly,char sep)319d93a896eSDag-Erling Smørgrav sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
320a0ee8cc6SDag-Erling Smørgrav {
321a0ee8cc6SDag-Erling Smørgrav 	char *tmp, *ret = NULL;
322f374ba41SEd Maste 	size_t i, nlen, rlen = 0;
323f374ba41SEd Maste 	const struct sshkey_impl *impl;
324a0ee8cc6SDag-Erling Smørgrav 
325f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
326f374ba41SEd Maste 		impl = keyimpls[i];
327f374ba41SEd Maste 		if (impl->name == NULL)
328d93a896eSDag-Erling Smørgrav 			continue;
329f374ba41SEd Maste 		if (!include_sigonly && impl->sigonly)
330a0ee8cc6SDag-Erling Smørgrav 			continue;
331f374ba41SEd Maste 		if ((certs_only && !impl->cert) || (plain_only && impl->cert))
332a0ee8cc6SDag-Erling Smørgrav 			continue;
333a0ee8cc6SDag-Erling Smørgrav 		if (ret != NULL)
334ca86bcf2SDag-Erling Smørgrav 			ret[rlen++] = sep;
335f374ba41SEd Maste 		nlen = strlen(impl->name);
336a0ee8cc6SDag-Erling Smørgrav 		if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
337a0ee8cc6SDag-Erling Smørgrav 			free(ret);
338a0ee8cc6SDag-Erling Smørgrav 			return NULL;
339a0ee8cc6SDag-Erling Smørgrav 		}
340a0ee8cc6SDag-Erling Smørgrav 		ret = tmp;
341f374ba41SEd Maste 		memcpy(ret + rlen, impl->name, nlen + 1);
342a0ee8cc6SDag-Erling Smørgrav 		rlen += nlen;
343a0ee8cc6SDag-Erling Smørgrav 	}
344a0ee8cc6SDag-Erling Smørgrav 	return ret;
345a0ee8cc6SDag-Erling Smørgrav }
346a0ee8cc6SDag-Erling Smørgrav 
347a0ee8cc6SDag-Erling Smørgrav int
sshkey_names_valid2(const char * names,int allow_wildcard,int plain_only)348535af610SEd Maste sshkey_names_valid2(const char *names, int allow_wildcard, int plain_only)
349a0ee8cc6SDag-Erling Smørgrav {
350a0ee8cc6SDag-Erling Smørgrav 	char *s, *cp, *p;
351f374ba41SEd Maste 	const struct sshkey_impl *impl;
352f374ba41SEd Maste 	int i, type;
353a0ee8cc6SDag-Erling Smørgrav 
354a0ee8cc6SDag-Erling Smørgrav 	if (names == NULL || strcmp(names, "") == 0)
355a0ee8cc6SDag-Erling Smørgrav 		return 0;
356a0ee8cc6SDag-Erling Smørgrav 	if ((s = cp = strdup(names)) == NULL)
357a0ee8cc6SDag-Erling Smørgrav 		return 0;
358a0ee8cc6SDag-Erling Smørgrav 	for ((p = strsep(&cp, ",")); p && *p != '\0';
359a0ee8cc6SDag-Erling Smørgrav 	    (p = strsep(&cp, ","))) {
360bc5531deSDag-Erling Smørgrav 		type = sshkey_type_from_name(p);
361bc5531deSDag-Erling Smørgrav 		if (type == KEY_UNSPEC) {
362bc5531deSDag-Erling Smørgrav 			if (allow_wildcard) {
363bc5531deSDag-Erling Smørgrav 				/*
364bc5531deSDag-Erling Smørgrav 				 * Try matching key types against the string.
365bc5531deSDag-Erling Smørgrav 				 * If any has a positive or negative match then
366bc5531deSDag-Erling Smørgrav 				 * the component is accepted.
367bc5531deSDag-Erling Smørgrav 				 */
368f374ba41SEd Maste 				impl = NULL;
369f374ba41SEd Maste 				for (i = 0; keyimpls[i] != NULL; i++) {
370f374ba41SEd Maste 					if (match_pattern_list(
371f374ba41SEd Maste 					    keyimpls[i]->name, p, 0) != 0) {
372f374ba41SEd Maste 						impl = keyimpls[i];
373bc5531deSDag-Erling Smørgrav 						break;
374bc5531deSDag-Erling Smørgrav 					}
375f374ba41SEd Maste 				}
376f374ba41SEd Maste 				if (impl != NULL)
377bc5531deSDag-Erling Smørgrav 					continue;
378bc5531deSDag-Erling Smørgrav 			}
379a0ee8cc6SDag-Erling Smørgrav 			free(s);
380a0ee8cc6SDag-Erling Smørgrav 			return 0;
381535af610SEd Maste 		} else if (plain_only && sshkey_type_is_cert(type)) {
382535af610SEd Maste 			free(s);
383535af610SEd Maste 			return 0;
384a0ee8cc6SDag-Erling Smørgrav 		}
385a0ee8cc6SDag-Erling Smørgrav 	}
386a0ee8cc6SDag-Erling Smørgrav 	free(s);
387a0ee8cc6SDag-Erling Smørgrav 	return 1;
388a0ee8cc6SDag-Erling Smørgrav }
389a0ee8cc6SDag-Erling Smørgrav 
390a0ee8cc6SDag-Erling Smørgrav u_int
sshkey_size(const struct sshkey * k)391a0ee8cc6SDag-Erling Smørgrav sshkey_size(const struct sshkey *k)
392a0ee8cc6SDag-Erling Smørgrav {
393f374ba41SEd Maste 	const struct sshkey_impl *impl;
3942a01feabSEd Maste 
395f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(k)) == NULL)
3962a01feabSEd Maste 		return 0;
397f374ba41SEd Maste 	if (impl->funcs->size != NULL)
398f374ba41SEd Maste 		return impl->funcs->size(k);
399f374ba41SEd Maste 	return impl->keybits;
400a0ee8cc6SDag-Erling Smørgrav }
401a0ee8cc6SDag-Erling Smørgrav 
402a0ee8cc6SDag-Erling Smørgrav static int
sshkey_type_is_valid_ca(int type)403a0ee8cc6SDag-Erling Smørgrav sshkey_type_is_valid_ca(int type)
404a0ee8cc6SDag-Erling Smørgrav {
405f374ba41SEd Maste 	const struct sshkey_impl *impl;
406f374ba41SEd Maste 
407f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL)
408a0ee8cc6SDag-Erling Smørgrav 		return 0;
409f374ba41SEd Maste 	/* All non-certificate types may act as CAs */
410f374ba41SEd Maste 	return !impl->cert;
411a0ee8cc6SDag-Erling Smørgrav }
412a0ee8cc6SDag-Erling Smørgrav 
413a0ee8cc6SDag-Erling Smørgrav int
sshkey_is_cert(const struct sshkey * k)414a0ee8cc6SDag-Erling Smørgrav sshkey_is_cert(const struct sshkey *k)
415a0ee8cc6SDag-Erling Smørgrav {
416a0ee8cc6SDag-Erling Smørgrav 	if (k == NULL)
417a0ee8cc6SDag-Erling Smørgrav 		return 0;
418a0ee8cc6SDag-Erling Smørgrav 	return sshkey_type_is_cert(k->type);
419a0ee8cc6SDag-Erling Smørgrav }
420a0ee8cc6SDag-Erling Smørgrav 
42119261079SEd Maste int
sshkey_is_sk(const struct sshkey * k)42219261079SEd Maste sshkey_is_sk(const struct sshkey *k)
42319261079SEd Maste {
42419261079SEd Maste 	if (k == NULL)
42519261079SEd Maste 		return 0;
42619261079SEd Maste 	switch (sshkey_type_plain(k->type)) {
42719261079SEd Maste 	case KEY_ECDSA_SK:
42819261079SEd Maste 	case KEY_ED25519_SK:
42919261079SEd Maste 		return 1;
43019261079SEd Maste 	default:
43119261079SEd Maste 		return 0;
43219261079SEd Maste 	}
43319261079SEd Maste }
43419261079SEd Maste 
435a0ee8cc6SDag-Erling Smørgrav /* Return the cert-less equivalent to a certified key type */
436a0ee8cc6SDag-Erling Smørgrav int
sshkey_type_plain(int type)437a0ee8cc6SDag-Erling Smørgrav sshkey_type_plain(int type)
438a0ee8cc6SDag-Erling Smørgrav {
439a0ee8cc6SDag-Erling Smørgrav 	switch (type) {
440a0ee8cc6SDag-Erling Smørgrav 	case KEY_RSA_CERT:
441a0ee8cc6SDag-Erling Smørgrav 		return KEY_RSA;
442a0ee8cc6SDag-Erling Smørgrav 	case KEY_DSA_CERT:
443a0ee8cc6SDag-Erling Smørgrav 		return KEY_DSA;
444a0ee8cc6SDag-Erling Smørgrav 	case KEY_ECDSA_CERT:
445a0ee8cc6SDag-Erling Smørgrav 		return KEY_ECDSA;
44619261079SEd Maste 	case KEY_ECDSA_SK_CERT:
44719261079SEd Maste 		return KEY_ECDSA_SK;
448a0ee8cc6SDag-Erling Smørgrav 	case KEY_ED25519_CERT:
449a0ee8cc6SDag-Erling Smørgrav 		return KEY_ED25519;
45019261079SEd Maste 	case KEY_ED25519_SK_CERT:
45119261079SEd Maste 		return KEY_ED25519_SK;
45247dd1d1bSDag-Erling Smørgrav 	case KEY_XMSS_CERT:
45347dd1d1bSDag-Erling Smørgrav 		return KEY_XMSS;
454a0ee8cc6SDag-Erling Smørgrav 	default:
455a0ee8cc6SDag-Erling Smørgrav 		return type;
456a0ee8cc6SDag-Erling Smørgrav 	}
457a0ee8cc6SDag-Erling Smørgrav }
458a0ee8cc6SDag-Erling Smørgrav 
459f374ba41SEd Maste /* Return the cert equivalent to a plain key type */
460f374ba41SEd Maste static int
sshkey_type_certified(int type)461f374ba41SEd Maste sshkey_type_certified(int type)
462f374ba41SEd Maste {
463f374ba41SEd Maste 	switch (type) {
464f374ba41SEd Maste 	case KEY_RSA:
465f374ba41SEd Maste 		return KEY_RSA_CERT;
466f374ba41SEd Maste 	case KEY_DSA:
467f374ba41SEd Maste 		return KEY_DSA_CERT;
468f374ba41SEd Maste 	case KEY_ECDSA:
469f374ba41SEd Maste 		return KEY_ECDSA_CERT;
470f374ba41SEd Maste 	case KEY_ECDSA_SK:
471f374ba41SEd Maste 		return KEY_ECDSA_SK_CERT;
472f374ba41SEd Maste 	case KEY_ED25519:
473f374ba41SEd Maste 		return KEY_ED25519_CERT;
474f374ba41SEd Maste 	case KEY_ED25519_SK:
475f374ba41SEd Maste 		return KEY_ED25519_SK_CERT;
476f374ba41SEd Maste 	case KEY_XMSS:
477f374ba41SEd Maste 		return KEY_XMSS_CERT;
478f374ba41SEd Maste 	default:
479f374ba41SEd Maste 		return -1;
480f374ba41SEd Maste 	}
481f374ba41SEd Maste }
482f374ba41SEd Maste 
483a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
484a0ee8cc6SDag-Erling Smørgrav /* XXX: these are really begging for a table-driven approach */
485a0ee8cc6SDag-Erling Smørgrav int
sshkey_curve_name_to_nid(const char * name)486a0ee8cc6SDag-Erling Smørgrav sshkey_curve_name_to_nid(const char *name)
487a0ee8cc6SDag-Erling Smørgrav {
488a0ee8cc6SDag-Erling Smørgrav 	if (strcmp(name, "nistp256") == 0)
489a0ee8cc6SDag-Erling Smørgrav 		return NID_X9_62_prime256v1;
490a0ee8cc6SDag-Erling Smørgrav 	else if (strcmp(name, "nistp384") == 0)
491a0ee8cc6SDag-Erling Smørgrav 		return NID_secp384r1;
492a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_NISTP521
493a0ee8cc6SDag-Erling Smørgrav 	else if (strcmp(name, "nistp521") == 0)
494a0ee8cc6SDag-Erling Smørgrav 		return NID_secp521r1;
495a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_NISTP521 */
496a0ee8cc6SDag-Erling Smørgrav 	else
497a0ee8cc6SDag-Erling Smørgrav 		return -1;
498a0ee8cc6SDag-Erling Smørgrav }
499a0ee8cc6SDag-Erling Smørgrav 
500a0ee8cc6SDag-Erling Smørgrav u_int
sshkey_curve_nid_to_bits(int nid)501a0ee8cc6SDag-Erling Smørgrav sshkey_curve_nid_to_bits(int nid)
502a0ee8cc6SDag-Erling Smørgrav {
503a0ee8cc6SDag-Erling Smørgrav 	switch (nid) {
504a0ee8cc6SDag-Erling Smørgrav 	case NID_X9_62_prime256v1:
505a0ee8cc6SDag-Erling Smørgrav 		return 256;
506a0ee8cc6SDag-Erling Smørgrav 	case NID_secp384r1:
507a0ee8cc6SDag-Erling Smørgrav 		return 384;
508a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_NISTP521
509a0ee8cc6SDag-Erling Smørgrav 	case NID_secp521r1:
510a0ee8cc6SDag-Erling Smørgrav 		return 521;
511a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_NISTP521 */
512a0ee8cc6SDag-Erling Smørgrav 	default:
513a0ee8cc6SDag-Erling Smørgrav 		return 0;
514a0ee8cc6SDag-Erling Smørgrav 	}
515a0ee8cc6SDag-Erling Smørgrav }
516a0ee8cc6SDag-Erling Smørgrav 
517a0ee8cc6SDag-Erling Smørgrav int
sshkey_ecdsa_bits_to_nid(int bits)518a0ee8cc6SDag-Erling Smørgrav sshkey_ecdsa_bits_to_nid(int bits)
519a0ee8cc6SDag-Erling Smørgrav {
520a0ee8cc6SDag-Erling Smørgrav 	switch (bits) {
521a0ee8cc6SDag-Erling Smørgrav 	case 256:
522a0ee8cc6SDag-Erling Smørgrav 		return NID_X9_62_prime256v1;
523a0ee8cc6SDag-Erling Smørgrav 	case 384:
524a0ee8cc6SDag-Erling Smørgrav 		return NID_secp384r1;
525a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_NISTP521
526a0ee8cc6SDag-Erling Smørgrav 	case 521:
527a0ee8cc6SDag-Erling Smørgrav 		return NID_secp521r1;
528a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_NISTP521 */
529a0ee8cc6SDag-Erling Smørgrav 	default:
530a0ee8cc6SDag-Erling Smørgrav 		return -1;
531a0ee8cc6SDag-Erling Smørgrav 	}
532a0ee8cc6SDag-Erling Smørgrav }
533a0ee8cc6SDag-Erling Smørgrav 
534a0ee8cc6SDag-Erling Smørgrav const char *
sshkey_curve_nid_to_name(int nid)535a0ee8cc6SDag-Erling Smørgrav sshkey_curve_nid_to_name(int nid)
536a0ee8cc6SDag-Erling Smørgrav {
537a0ee8cc6SDag-Erling Smørgrav 	switch (nid) {
538a0ee8cc6SDag-Erling Smørgrav 	case NID_X9_62_prime256v1:
539a0ee8cc6SDag-Erling Smørgrav 		return "nistp256";
540a0ee8cc6SDag-Erling Smørgrav 	case NID_secp384r1:
541a0ee8cc6SDag-Erling Smørgrav 		return "nistp384";
542a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_NISTP521
543a0ee8cc6SDag-Erling Smørgrav 	case NID_secp521r1:
544a0ee8cc6SDag-Erling Smørgrav 		return "nistp521";
545a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_NISTP521 */
546a0ee8cc6SDag-Erling Smørgrav 	default:
547a0ee8cc6SDag-Erling Smørgrav 		return NULL;
548a0ee8cc6SDag-Erling Smørgrav 	}
549a0ee8cc6SDag-Erling Smørgrav }
550a0ee8cc6SDag-Erling Smørgrav 
551a0ee8cc6SDag-Erling Smørgrav int
sshkey_ec_nid_to_hash_alg(int nid)552a0ee8cc6SDag-Erling Smørgrav sshkey_ec_nid_to_hash_alg(int nid)
553a0ee8cc6SDag-Erling Smørgrav {
554a0ee8cc6SDag-Erling Smørgrav 	int kbits = sshkey_curve_nid_to_bits(nid);
555a0ee8cc6SDag-Erling Smørgrav 
556a0ee8cc6SDag-Erling Smørgrav 	if (kbits <= 0)
557a0ee8cc6SDag-Erling Smørgrav 		return -1;
558a0ee8cc6SDag-Erling Smørgrav 
559a0ee8cc6SDag-Erling Smørgrav 	/* RFC5656 section 6.2.1 */
560a0ee8cc6SDag-Erling Smørgrav 	if (kbits <= 256)
561a0ee8cc6SDag-Erling Smørgrav 		return SSH_DIGEST_SHA256;
562a0ee8cc6SDag-Erling Smørgrav 	else if (kbits <= 384)
563a0ee8cc6SDag-Erling Smørgrav 		return SSH_DIGEST_SHA384;
564a0ee8cc6SDag-Erling Smørgrav 	else
565a0ee8cc6SDag-Erling Smørgrav 		return SSH_DIGEST_SHA512;
566a0ee8cc6SDag-Erling Smørgrav }
567a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
568a0ee8cc6SDag-Erling Smørgrav 
569a0ee8cc6SDag-Erling Smørgrav static void
cert_free(struct sshkey_cert * cert)570a0ee8cc6SDag-Erling Smørgrav cert_free(struct sshkey_cert *cert)
571a0ee8cc6SDag-Erling Smørgrav {
572a0ee8cc6SDag-Erling Smørgrav 	u_int i;
573a0ee8cc6SDag-Erling Smørgrav 
574a0ee8cc6SDag-Erling Smørgrav 	if (cert == NULL)
575a0ee8cc6SDag-Erling Smørgrav 		return;
576a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(cert->certblob);
577a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(cert->critical);
578a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(cert->extensions);
579a0ee8cc6SDag-Erling Smørgrav 	free(cert->key_id);
580a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; i < cert->nprincipals; i++)
581a0ee8cc6SDag-Erling Smørgrav 		free(cert->principals[i]);
582a0ee8cc6SDag-Erling Smørgrav 	free(cert->principals);
583a0ee8cc6SDag-Erling Smørgrav 	sshkey_free(cert->signature_key);
5842f513db7SEd Maste 	free(cert->signature_type);
58547dd1d1bSDag-Erling Smørgrav 	freezero(cert, sizeof(*cert));
586a0ee8cc6SDag-Erling Smørgrav }
587a0ee8cc6SDag-Erling Smørgrav 
588a0ee8cc6SDag-Erling Smørgrav static struct sshkey_cert *
cert_new(void)589a0ee8cc6SDag-Erling Smørgrav cert_new(void)
590a0ee8cc6SDag-Erling Smørgrav {
591a0ee8cc6SDag-Erling Smørgrav 	struct sshkey_cert *cert;
592a0ee8cc6SDag-Erling Smørgrav 
593a0ee8cc6SDag-Erling Smørgrav 	if ((cert = calloc(1, sizeof(*cert))) == NULL)
594a0ee8cc6SDag-Erling Smørgrav 		return NULL;
595a0ee8cc6SDag-Erling Smørgrav 	if ((cert->certblob = sshbuf_new()) == NULL ||
596a0ee8cc6SDag-Erling Smørgrav 	    (cert->critical = sshbuf_new()) == NULL ||
597a0ee8cc6SDag-Erling Smørgrav 	    (cert->extensions = sshbuf_new()) == NULL) {
598a0ee8cc6SDag-Erling Smørgrav 		cert_free(cert);
599a0ee8cc6SDag-Erling Smørgrav 		return NULL;
600a0ee8cc6SDag-Erling Smørgrav 	}
601a0ee8cc6SDag-Erling Smørgrav 	cert->key_id = NULL;
602a0ee8cc6SDag-Erling Smørgrav 	cert->principals = NULL;
603a0ee8cc6SDag-Erling Smørgrav 	cert->signature_key = NULL;
6042f513db7SEd Maste 	cert->signature_type = NULL;
605a0ee8cc6SDag-Erling Smørgrav 	return cert;
606a0ee8cc6SDag-Erling Smørgrav }
607a0ee8cc6SDag-Erling Smørgrav 
608a0ee8cc6SDag-Erling Smørgrav struct sshkey *
sshkey_new(int type)609a0ee8cc6SDag-Erling Smørgrav sshkey_new(int type)
610a0ee8cc6SDag-Erling Smørgrav {
611a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *k;
612f374ba41SEd Maste 	const struct sshkey_impl *impl = NULL;
613a0ee8cc6SDag-Erling Smørgrav 
614f374ba41SEd Maste 	if (type != KEY_UNSPEC &&
615f374ba41SEd Maste 	    (impl = sshkey_impl_from_type(type)) == NULL)
616f374ba41SEd Maste 		return NULL;
617f374ba41SEd Maste 
618f374ba41SEd Maste 	/* All non-certificate types may act as CAs */
619a0ee8cc6SDag-Erling Smørgrav 	if ((k = calloc(1, sizeof(*k))) == NULL)
620a0ee8cc6SDag-Erling Smørgrav 		return NULL;
621a0ee8cc6SDag-Erling Smørgrav 	k->type = type;
622a0ee8cc6SDag-Erling Smørgrav 	k->ecdsa_nid = -1;
623f374ba41SEd Maste 	if (impl != NULL && impl->funcs->alloc != NULL) {
624f374ba41SEd Maste 		if (impl->funcs->alloc(k) != 0) {
625a0ee8cc6SDag-Erling Smørgrav 			free(k);
626a0ee8cc6SDag-Erling Smørgrav 			return NULL;
627a0ee8cc6SDag-Erling Smørgrav 		}
628a0ee8cc6SDag-Erling Smørgrav 	}
629a0ee8cc6SDag-Erling Smørgrav 	if (sshkey_is_cert(k)) {
630a0ee8cc6SDag-Erling Smørgrav 		if ((k->cert = cert_new()) == NULL) {
631a0ee8cc6SDag-Erling Smørgrav 			sshkey_free(k);
632a0ee8cc6SDag-Erling Smørgrav 			return NULL;
633a0ee8cc6SDag-Erling Smørgrav 		}
634a0ee8cc6SDag-Erling Smørgrav 	}
635a0ee8cc6SDag-Erling Smørgrav 
636a0ee8cc6SDag-Erling Smørgrav 	return k;
637a0ee8cc6SDag-Erling Smørgrav }
638a0ee8cc6SDag-Erling Smørgrav 
639f374ba41SEd Maste /* Frees common FIDO fields */
640a0ee8cc6SDag-Erling Smørgrav void
sshkey_sk_cleanup(struct sshkey * k)641f374ba41SEd Maste sshkey_sk_cleanup(struct sshkey *k)
642a0ee8cc6SDag-Erling Smørgrav {
643f374ba41SEd Maste 	free(k->sk_application);
644f374ba41SEd Maste 	sshbuf_free(k->sk_key_handle);
645f374ba41SEd Maste 	sshbuf_free(k->sk_reserved);
646f374ba41SEd Maste 	k->sk_application = NULL;
647f374ba41SEd Maste 	k->sk_key_handle = k->sk_reserved = NULL;
648f374ba41SEd Maste }
649f374ba41SEd Maste 
650f374ba41SEd Maste static void
sshkey_free_contents(struct sshkey * k)651f374ba41SEd Maste sshkey_free_contents(struct sshkey *k)
652f374ba41SEd Maste {
653f374ba41SEd Maste 	const struct sshkey_impl *impl;
654f374ba41SEd Maste 
655a0ee8cc6SDag-Erling Smørgrav 	if (k == NULL)
656a0ee8cc6SDag-Erling Smørgrav 		return;
657f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(k->type)) != NULL &&
658f374ba41SEd Maste 	    impl->funcs->cleanup != NULL)
659f374ba41SEd Maste 		impl->funcs->cleanup(k);
660a0ee8cc6SDag-Erling Smørgrav 	if (sshkey_is_cert(k))
661a0ee8cc6SDag-Erling Smørgrav 		cert_free(k->cert);
66219261079SEd Maste 	freezero(k->shielded_private, k->shielded_len);
66319261079SEd Maste 	freezero(k->shield_prekey, k->shield_prekey_len);
664f374ba41SEd Maste }
665f374ba41SEd Maste 
666f374ba41SEd Maste void
sshkey_free(struct sshkey * k)667f374ba41SEd Maste sshkey_free(struct sshkey *k)
668f374ba41SEd Maste {
669f374ba41SEd Maste 	sshkey_free_contents(k);
67047dd1d1bSDag-Erling Smørgrav 	freezero(k, sizeof(*k));
671a0ee8cc6SDag-Erling Smørgrav }
672a0ee8cc6SDag-Erling Smørgrav 
673a0ee8cc6SDag-Erling Smørgrav static int
cert_compare(struct sshkey_cert * a,struct sshkey_cert * b)674a0ee8cc6SDag-Erling Smørgrav cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
675a0ee8cc6SDag-Erling Smørgrav {
676a0ee8cc6SDag-Erling Smørgrav 	if (a == NULL && b == NULL)
677a0ee8cc6SDag-Erling Smørgrav 		return 1;
678a0ee8cc6SDag-Erling Smørgrav 	if (a == NULL || b == NULL)
679a0ee8cc6SDag-Erling Smørgrav 		return 0;
680a0ee8cc6SDag-Erling Smørgrav 	if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
681a0ee8cc6SDag-Erling Smørgrav 		return 0;
682a0ee8cc6SDag-Erling Smørgrav 	if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
683a0ee8cc6SDag-Erling Smørgrav 	    sshbuf_len(a->certblob)) != 0)
684a0ee8cc6SDag-Erling Smørgrav 		return 0;
685a0ee8cc6SDag-Erling Smørgrav 	return 1;
686a0ee8cc6SDag-Erling Smørgrav }
687a0ee8cc6SDag-Erling Smørgrav 
688f374ba41SEd Maste /* Compares FIDO-specific pubkey fields only */
689f374ba41SEd Maste int
sshkey_sk_fields_equal(const struct sshkey * a,const struct sshkey * b)690f374ba41SEd Maste sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b)
691f374ba41SEd Maste {
692f374ba41SEd Maste 	if (a->sk_application == NULL || b->sk_application == NULL)
693f374ba41SEd Maste 		return 0;
694f374ba41SEd Maste 	if (strcmp(a->sk_application, b->sk_application) != 0)
695f374ba41SEd Maste 		return 0;
696f374ba41SEd Maste 	return 1;
697f374ba41SEd Maste }
698f374ba41SEd Maste 
699a0ee8cc6SDag-Erling Smørgrav /*
700a0ee8cc6SDag-Erling Smørgrav  * Compare public portions of key only, allowing comparisons between
701a0ee8cc6SDag-Erling Smørgrav  * certificates and plain keys too.
702a0ee8cc6SDag-Erling Smørgrav  */
703a0ee8cc6SDag-Erling Smørgrav int
sshkey_equal_public(const struct sshkey * a,const struct sshkey * b)704a0ee8cc6SDag-Erling Smørgrav sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
705a0ee8cc6SDag-Erling Smørgrav {
706f374ba41SEd Maste 	const struct sshkey_impl *impl;
707a0ee8cc6SDag-Erling Smørgrav 
708a0ee8cc6SDag-Erling Smørgrav 	if (a == NULL || b == NULL ||
709a0ee8cc6SDag-Erling Smørgrav 	    sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
710a0ee8cc6SDag-Erling Smørgrav 		return 0;
711f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(a->type)) == NULL)
7122a01feabSEd Maste 		return 0;
713f374ba41SEd Maste 	return impl->funcs->equal(a, b);
714a0ee8cc6SDag-Erling Smørgrav }
715a0ee8cc6SDag-Erling Smørgrav 
716a0ee8cc6SDag-Erling Smørgrav int
sshkey_equal(const struct sshkey * a,const struct sshkey * b)717a0ee8cc6SDag-Erling Smørgrav sshkey_equal(const struct sshkey *a, const struct sshkey *b)
718a0ee8cc6SDag-Erling Smørgrav {
719a0ee8cc6SDag-Erling Smørgrav 	if (a == NULL || b == NULL || a->type != b->type)
720a0ee8cc6SDag-Erling Smørgrav 		return 0;
721a0ee8cc6SDag-Erling Smørgrav 	if (sshkey_is_cert(a)) {
722a0ee8cc6SDag-Erling Smørgrav 		if (!cert_compare(a->cert, b->cert))
723a0ee8cc6SDag-Erling Smørgrav 			return 0;
724a0ee8cc6SDag-Erling Smørgrav 	}
725a0ee8cc6SDag-Erling Smørgrav 	return sshkey_equal_public(a, b);
726a0ee8cc6SDag-Erling Smørgrav }
727a0ee8cc6SDag-Erling Smørgrav 
728f374ba41SEd Maste 
729f374ba41SEd Maste /* Serialise common FIDO key parts */
730f374ba41SEd Maste int
sshkey_serialize_sk(const struct sshkey * key,struct sshbuf * b)731f374ba41SEd Maste sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b)
732f374ba41SEd Maste {
733f374ba41SEd Maste 	int r;
734f374ba41SEd Maste 
735f374ba41SEd Maste 	if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0)
736f374ba41SEd Maste 		return r;
737f374ba41SEd Maste 
738f374ba41SEd Maste 	return 0;
739f374ba41SEd Maste }
740f374ba41SEd Maste 
741a0ee8cc6SDag-Erling Smørgrav static int
to_blob_buf(const struct sshkey * key,struct sshbuf * b,int force_plain,enum sshkey_serialize_rep opts)74247dd1d1bSDag-Erling Smørgrav to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
74347dd1d1bSDag-Erling Smørgrav   enum sshkey_serialize_rep opts)
744a0ee8cc6SDag-Erling Smørgrav {
745a0ee8cc6SDag-Erling Smørgrav 	int type, ret = SSH_ERR_INTERNAL_ERROR;
746a0ee8cc6SDag-Erling Smørgrav 	const char *typename;
747f374ba41SEd Maste 	const struct sshkey_impl *impl;
748a0ee8cc6SDag-Erling Smørgrav 
749a0ee8cc6SDag-Erling Smørgrav 	if (key == NULL)
750a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
751a0ee8cc6SDag-Erling Smørgrav 
752f374ba41SEd Maste 	type = force_plain ? sshkey_type_plain(key->type) : key->type;
753f374ba41SEd Maste 
754f374ba41SEd Maste 	if (sshkey_type_is_cert(type)) {
755557f75e5SDag-Erling Smørgrav 		if (key->cert == NULL)
756557f75e5SDag-Erling Smørgrav 			return SSH_ERR_EXPECTED_CERT;
757557f75e5SDag-Erling Smørgrav 		if (sshbuf_len(key->cert->certblob) == 0)
758557f75e5SDag-Erling Smørgrav 			return SSH_ERR_KEY_LACKS_CERTBLOB;
759a0ee8cc6SDag-Erling Smørgrav 		/* Use the existing blob */
760a0ee8cc6SDag-Erling Smørgrav 		if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
761a0ee8cc6SDag-Erling Smørgrav 			return ret;
762a0ee8cc6SDag-Erling Smørgrav 		return 0;
763a0ee8cc6SDag-Erling Smørgrav 	}
764f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL)
765f374ba41SEd Maste 		return SSH_ERR_KEY_TYPE_UNKNOWN;
766f374ba41SEd Maste 
767f374ba41SEd Maste 	typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
768f374ba41SEd Maste 	if ((ret = sshbuf_put_cstring(b, typename)) != 0)
769f374ba41SEd Maste 		return ret;
770f374ba41SEd Maste 	return impl->funcs->serialize_public(key, b, opts);
771f374ba41SEd Maste }
772a0ee8cc6SDag-Erling Smørgrav 
773a0ee8cc6SDag-Erling Smørgrav int
sshkey_putb(const struct sshkey * key,struct sshbuf * b)774bc5531deSDag-Erling Smørgrav sshkey_putb(const struct sshkey *key, struct sshbuf *b)
775a0ee8cc6SDag-Erling Smørgrav {
77647dd1d1bSDag-Erling Smørgrav 	return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
777a0ee8cc6SDag-Erling Smørgrav }
778a0ee8cc6SDag-Erling Smørgrav 
779a0ee8cc6SDag-Erling Smørgrav int
sshkey_puts_opts(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)78047dd1d1bSDag-Erling Smørgrav sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
78147dd1d1bSDag-Erling Smørgrav     enum sshkey_serialize_rep opts)
782bc5531deSDag-Erling Smørgrav {
783bc5531deSDag-Erling Smørgrav 	struct sshbuf *tmp;
784bc5531deSDag-Erling Smørgrav 	int r;
785bc5531deSDag-Erling Smørgrav 
786bc5531deSDag-Erling Smørgrav 	if ((tmp = sshbuf_new()) == NULL)
787bc5531deSDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
78847dd1d1bSDag-Erling Smørgrav 	r = to_blob_buf(key, tmp, 0, opts);
789bc5531deSDag-Erling Smørgrav 	if (r == 0)
790bc5531deSDag-Erling Smørgrav 		r = sshbuf_put_stringb(b, tmp);
791bc5531deSDag-Erling Smørgrav 	sshbuf_free(tmp);
792bc5531deSDag-Erling Smørgrav 	return r;
793bc5531deSDag-Erling Smørgrav }
794bc5531deSDag-Erling Smørgrav 
795bc5531deSDag-Erling Smørgrav int
sshkey_puts(const struct sshkey * key,struct sshbuf * b)79647dd1d1bSDag-Erling Smørgrav sshkey_puts(const struct sshkey *key, struct sshbuf *b)
79747dd1d1bSDag-Erling Smørgrav {
79847dd1d1bSDag-Erling Smørgrav 	return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
79947dd1d1bSDag-Erling Smørgrav }
80047dd1d1bSDag-Erling Smørgrav 
80147dd1d1bSDag-Erling Smørgrav int
sshkey_putb_plain(const struct sshkey * key,struct sshbuf * b)802bc5531deSDag-Erling Smørgrav sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
803a0ee8cc6SDag-Erling Smørgrav {
80447dd1d1bSDag-Erling Smørgrav 	return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
805a0ee8cc6SDag-Erling Smørgrav }
806a0ee8cc6SDag-Erling Smørgrav 
807a0ee8cc6SDag-Erling Smørgrav static int
to_blob(const struct sshkey * key,u_char ** blobp,size_t * lenp,int force_plain,enum sshkey_serialize_rep opts)80847dd1d1bSDag-Erling Smørgrav to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
80947dd1d1bSDag-Erling Smørgrav     enum sshkey_serialize_rep opts)
810a0ee8cc6SDag-Erling Smørgrav {
811a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_INTERNAL_ERROR;
812a0ee8cc6SDag-Erling Smørgrav 	size_t len;
813a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *b = NULL;
814a0ee8cc6SDag-Erling Smørgrav 
815a0ee8cc6SDag-Erling Smørgrav 	if (lenp != NULL)
816a0ee8cc6SDag-Erling Smørgrav 		*lenp = 0;
817a0ee8cc6SDag-Erling Smørgrav 	if (blobp != NULL)
818a0ee8cc6SDag-Erling Smørgrav 		*blobp = NULL;
819a0ee8cc6SDag-Erling Smørgrav 	if ((b = sshbuf_new()) == NULL)
820a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
82147dd1d1bSDag-Erling Smørgrav 	if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
822a0ee8cc6SDag-Erling Smørgrav 		goto out;
823a0ee8cc6SDag-Erling Smørgrav 	len = sshbuf_len(b);
824a0ee8cc6SDag-Erling Smørgrav 	if (lenp != NULL)
825a0ee8cc6SDag-Erling Smørgrav 		*lenp = len;
826a0ee8cc6SDag-Erling Smørgrav 	if (blobp != NULL) {
827a0ee8cc6SDag-Erling Smørgrav 		if ((*blobp = malloc(len)) == NULL) {
828a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_ALLOC_FAIL;
829a0ee8cc6SDag-Erling Smørgrav 			goto out;
830a0ee8cc6SDag-Erling Smørgrav 		}
831a0ee8cc6SDag-Erling Smørgrav 		memcpy(*blobp, sshbuf_ptr(b), len);
832a0ee8cc6SDag-Erling Smørgrav 	}
833a0ee8cc6SDag-Erling Smørgrav 	ret = 0;
834a0ee8cc6SDag-Erling Smørgrav  out:
835a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(b);
836a0ee8cc6SDag-Erling Smørgrav 	return ret;
837a0ee8cc6SDag-Erling Smørgrav }
838a0ee8cc6SDag-Erling Smørgrav 
839a0ee8cc6SDag-Erling Smørgrav int
sshkey_to_blob(const struct sshkey * key,u_char ** blobp,size_t * lenp)840a0ee8cc6SDag-Erling Smørgrav sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
841a0ee8cc6SDag-Erling Smørgrav {
84247dd1d1bSDag-Erling Smørgrav 	return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
843a0ee8cc6SDag-Erling Smørgrav }
844a0ee8cc6SDag-Erling Smørgrav 
845a0ee8cc6SDag-Erling Smørgrav int
sshkey_plain_to_blob(const struct sshkey * key,u_char ** blobp,size_t * lenp)846a0ee8cc6SDag-Erling Smørgrav sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
847a0ee8cc6SDag-Erling Smørgrav {
84847dd1d1bSDag-Erling Smørgrav 	return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
849a0ee8cc6SDag-Erling Smørgrav }
850a0ee8cc6SDag-Erling Smørgrav 
851a0ee8cc6SDag-Erling Smørgrav int
sshkey_fingerprint_raw(const struct sshkey * k,int dgst_alg,u_char ** retp,size_t * lenp)852bc5531deSDag-Erling Smørgrav sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
853a0ee8cc6SDag-Erling Smørgrav     u_char **retp, size_t *lenp)
854a0ee8cc6SDag-Erling Smørgrav {
855a0ee8cc6SDag-Erling Smørgrav 	u_char *blob = NULL, *ret = NULL;
856a0ee8cc6SDag-Erling Smørgrav 	size_t blob_len = 0;
857bc5531deSDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
858a0ee8cc6SDag-Erling Smørgrav 
859a0ee8cc6SDag-Erling Smørgrav 	if (retp != NULL)
860a0ee8cc6SDag-Erling Smørgrav 		*retp = NULL;
861a0ee8cc6SDag-Erling Smørgrav 	if (lenp != NULL)
862a0ee8cc6SDag-Erling Smørgrav 		*lenp = 0;
863bc5531deSDag-Erling Smørgrav 	if (ssh_digest_bytes(dgst_alg) == 0) {
864a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_ARGUMENT;
865a0ee8cc6SDag-Erling Smørgrav 		goto out;
866a0ee8cc6SDag-Erling Smørgrav 	}
86747dd1d1bSDag-Erling Smørgrav 	if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
86847dd1d1bSDag-Erling Smørgrav 	    != 0)
869a0ee8cc6SDag-Erling Smørgrav 		goto out;
870a0ee8cc6SDag-Erling Smørgrav 	if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
871a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
872a0ee8cc6SDag-Erling Smørgrav 		goto out;
873a0ee8cc6SDag-Erling Smørgrav 	}
874bc5531deSDag-Erling Smørgrav 	if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
875a0ee8cc6SDag-Erling Smørgrav 	    ret, SSH_DIGEST_MAX_LENGTH)) != 0)
876a0ee8cc6SDag-Erling Smørgrav 		goto out;
877a0ee8cc6SDag-Erling Smørgrav 	/* success */
878a0ee8cc6SDag-Erling Smørgrav 	if (retp != NULL) {
879a0ee8cc6SDag-Erling Smørgrav 		*retp = ret;
880a0ee8cc6SDag-Erling Smørgrav 		ret = NULL;
881a0ee8cc6SDag-Erling Smørgrav 	}
882a0ee8cc6SDag-Erling Smørgrav 	if (lenp != NULL)
883bc5531deSDag-Erling Smørgrav 		*lenp = ssh_digest_bytes(dgst_alg);
884a0ee8cc6SDag-Erling Smørgrav 	r = 0;
885a0ee8cc6SDag-Erling Smørgrav  out:
886a0ee8cc6SDag-Erling Smørgrav 	free(ret);
88719261079SEd Maste 	if (blob != NULL)
88819261079SEd Maste 		freezero(blob, blob_len);
889a0ee8cc6SDag-Erling Smørgrav 	return r;
890a0ee8cc6SDag-Erling Smørgrav }
891a0ee8cc6SDag-Erling Smørgrav 
892a0ee8cc6SDag-Erling Smørgrav static char *
fingerprint_b64(const char * alg,u_char * dgst_raw,size_t dgst_raw_len)893bc5531deSDag-Erling Smørgrav fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
894a0ee8cc6SDag-Erling Smørgrav {
895bc5531deSDag-Erling Smørgrav 	char *ret;
896bc5531deSDag-Erling Smørgrav 	size_t plen = strlen(alg) + 1;
897bc5531deSDag-Erling Smørgrav 	size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
898a0ee8cc6SDag-Erling Smørgrav 
899bc5531deSDag-Erling Smørgrav 	if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
900a0ee8cc6SDag-Erling Smørgrav 		return NULL;
901bc5531deSDag-Erling Smørgrav 	strlcpy(ret, alg, rlen);
902bc5531deSDag-Erling Smørgrav 	strlcat(ret, ":", rlen);
903bc5531deSDag-Erling Smørgrav 	if (dgst_raw_len == 0)
904bc5531deSDag-Erling Smørgrav 		return ret;
90519261079SEd Maste 	if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
90647dd1d1bSDag-Erling Smørgrav 		freezero(ret, rlen);
907bc5531deSDag-Erling Smørgrav 		return NULL;
908bc5531deSDag-Erling Smørgrav 	}
909bc5531deSDag-Erling Smørgrav 	/* Trim padding characters from end */
910bc5531deSDag-Erling Smørgrav 	ret[strcspn(ret, "=")] = '\0';
911bc5531deSDag-Erling Smørgrav 	return ret;
912a0ee8cc6SDag-Erling Smørgrav }
913a0ee8cc6SDag-Erling Smørgrav 
914bc5531deSDag-Erling Smørgrav static char *
fingerprint_hex(const char * alg,u_char * dgst_raw,size_t dgst_raw_len)915bc5531deSDag-Erling Smørgrav fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
916bc5531deSDag-Erling Smørgrav {
917bc5531deSDag-Erling Smørgrav 	char *retval, hex[5];
918bc5531deSDag-Erling Smørgrav 	size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
919bc5531deSDag-Erling Smørgrav 
920bc5531deSDag-Erling Smørgrav 	if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
921bc5531deSDag-Erling Smørgrav 		return NULL;
922bc5531deSDag-Erling Smørgrav 	strlcpy(retval, alg, rlen);
923bc5531deSDag-Erling Smørgrav 	strlcat(retval, ":", rlen);
924bc5531deSDag-Erling Smørgrav 	for (i = 0; i < dgst_raw_len; i++) {
925bc5531deSDag-Erling Smørgrav 		snprintf(hex, sizeof(hex), "%s%02x",
926bc5531deSDag-Erling Smørgrav 		    i > 0 ? ":" : "", dgst_raw[i]);
927bc5531deSDag-Erling Smørgrav 		strlcat(retval, hex, rlen);
928bc5531deSDag-Erling Smørgrav 	}
929a0ee8cc6SDag-Erling Smørgrav 	return retval;
930a0ee8cc6SDag-Erling Smørgrav }
931a0ee8cc6SDag-Erling Smørgrav 
932a0ee8cc6SDag-Erling Smørgrav static char *
fingerprint_bubblebabble(u_char * dgst_raw,size_t dgst_raw_len)933a0ee8cc6SDag-Erling Smørgrav fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
934a0ee8cc6SDag-Erling Smørgrav {
935a0ee8cc6SDag-Erling Smørgrav 	char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
936a0ee8cc6SDag-Erling Smørgrav 	char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
937a0ee8cc6SDag-Erling Smørgrav 	    'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
938a0ee8cc6SDag-Erling Smørgrav 	u_int i, j = 0, rounds, seed = 1;
939a0ee8cc6SDag-Erling Smørgrav 	char *retval;
940a0ee8cc6SDag-Erling Smørgrav 
941a0ee8cc6SDag-Erling Smørgrav 	rounds = (dgst_raw_len / 2) + 1;
942a0ee8cc6SDag-Erling Smørgrav 	if ((retval = calloc(rounds, 6)) == NULL)
943a0ee8cc6SDag-Erling Smørgrav 		return NULL;
944a0ee8cc6SDag-Erling Smørgrav 	retval[j++] = 'x';
945a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; i < rounds; i++) {
946a0ee8cc6SDag-Erling Smørgrav 		u_int idx0, idx1, idx2, idx3, idx4;
947a0ee8cc6SDag-Erling Smørgrav 		if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
948a0ee8cc6SDag-Erling Smørgrav 			idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
949a0ee8cc6SDag-Erling Smørgrav 			    seed) % 6;
950a0ee8cc6SDag-Erling Smørgrav 			idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
951a0ee8cc6SDag-Erling Smørgrav 			idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
952a0ee8cc6SDag-Erling Smørgrav 			    (seed / 6)) % 6;
953a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = vowels[idx0];
954a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = consonants[idx1];
955a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = vowels[idx2];
956a0ee8cc6SDag-Erling Smørgrav 			if ((i + 1) < rounds) {
957a0ee8cc6SDag-Erling Smørgrav 				idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
958a0ee8cc6SDag-Erling Smørgrav 				idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
959a0ee8cc6SDag-Erling Smørgrav 				retval[j++] = consonants[idx3];
960a0ee8cc6SDag-Erling Smørgrav 				retval[j++] = '-';
961a0ee8cc6SDag-Erling Smørgrav 				retval[j++] = consonants[idx4];
962a0ee8cc6SDag-Erling Smørgrav 				seed = ((seed * 5) +
963a0ee8cc6SDag-Erling Smørgrav 				    ((((u_int)(dgst_raw[2 * i])) * 7) +
964a0ee8cc6SDag-Erling Smørgrav 				    ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
965a0ee8cc6SDag-Erling Smørgrav 			}
966a0ee8cc6SDag-Erling Smørgrav 		} else {
967a0ee8cc6SDag-Erling Smørgrav 			idx0 = seed % 6;
968a0ee8cc6SDag-Erling Smørgrav 			idx1 = 16;
969a0ee8cc6SDag-Erling Smørgrav 			idx2 = seed / 6;
970a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = vowels[idx0];
971a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = consonants[idx1];
972a0ee8cc6SDag-Erling Smørgrav 			retval[j++] = vowels[idx2];
973a0ee8cc6SDag-Erling Smørgrav 		}
974a0ee8cc6SDag-Erling Smørgrav 	}
975a0ee8cc6SDag-Erling Smørgrav 	retval[j++] = 'x';
976a0ee8cc6SDag-Erling Smørgrav 	retval[j++] = '\0';
977a0ee8cc6SDag-Erling Smørgrav 	return retval;
978a0ee8cc6SDag-Erling Smørgrav }
979a0ee8cc6SDag-Erling Smørgrav 
980a0ee8cc6SDag-Erling Smørgrav /*
981a0ee8cc6SDag-Erling Smørgrav  * Draw an ASCII-Art representing the fingerprint so human brain can
982a0ee8cc6SDag-Erling Smørgrav  * profit from its built-in pattern recognition ability.
983a0ee8cc6SDag-Erling Smørgrav  * This technique is called "random art" and can be found in some
984a0ee8cc6SDag-Erling Smørgrav  * scientific publications like this original paper:
985a0ee8cc6SDag-Erling Smørgrav  *
986a0ee8cc6SDag-Erling Smørgrav  * "Hash Visualization: a New Technique to improve Real-World Security",
987a0ee8cc6SDag-Erling Smørgrav  * Perrig A. and Song D., 1999, International Workshop on Cryptographic
988a0ee8cc6SDag-Erling Smørgrav  * Techniques and E-Commerce (CrypTEC '99)
989a0ee8cc6SDag-Erling Smørgrav  * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
990a0ee8cc6SDag-Erling Smørgrav  *
991a0ee8cc6SDag-Erling Smørgrav  * The subject came up in a talk by Dan Kaminsky, too.
992a0ee8cc6SDag-Erling Smørgrav  *
993a0ee8cc6SDag-Erling Smørgrav  * If you see the picture is different, the key is different.
994a0ee8cc6SDag-Erling Smørgrav  * If the picture looks the same, you still know nothing.
995a0ee8cc6SDag-Erling Smørgrav  *
996a0ee8cc6SDag-Erling Smørgrav  * The algorithm used here is a worm crawling over a discrete plane,
997a0ee8cc6SDag-Erling Smørgrav  * leaving a trace (augmenting the field) everywhere it goes.
998a0ee8cc6SDag-Erling Smørgrav  * Movement is taken from dgst_raw 2bit-wise.  Bumping into walls
999a0ee8cc6SDag-Erling Smørgrav  * makes the respective movement vector be ignored for this turn.
1000a0ee8cc6SDag-Erling Smørgrav  * Graphs are not unambiguous, because circles in graphs can be
1001a0ee8cc6SDag-Erling Smørgrav  * walked in either direction.
1002a0ee8cc6SDag-Erling Smørgrav  */
1003a0ee8cc6SDag-Erling Smørgrav 
1004a0ee8cc6SDag-Erling Smørgrav /*
1005a0ee8cc6SDag-Erling Smørgrav  * Field sizes for the random art.  Have to be odd, so the starting point
1006a0ee8cc6SDag-Erling Smørgrav  * can be in the exact middle of the picture, and FLDBASE should be >=8 .
1007a0ee8cc6SDag-Erling Smørgrav  * Else pictures would be too dense, and drawing the frame would
1008a0ee8cc6SDag-Erling Smørgrav  * fail, too, because the key type would not fit in anymore.
1009a0ee8cc6SDag-Erling Smørgrav  */
1010a0ee8cc6SDag-Erling Smørgrav #define	FLDBASE		8
1011a0ee8cc6SDag-Erling Smørgrav #define	FLDSIZE_Y	(FLDBASE + 1)
1012a0ee8cc6SDag-Erling Smørgrav #define	FLDSIZE_X	(FLDBASE * 2 + 1)
1013a0ee8cc6SDag-Erling Smørgrav static char *
fingerprint_randomart(const char * alg,u_char * dgst_raw,size_t dgst_raw_len,const struct sshkey * k)1014bc5531deSDag-Erling Smørgrav fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
1015a0ee8cc6SDag-Erling Smørgrav     const struct sshkey *k)
1016a0ee8cc6SDag-Erling Smørgrav {
1017a0ee8cc6SDag-Erling Smørgrav 	/*
1018a0ee8cc6SDag-Erling Smørgrav 	 * Chars to be used after each other every time the worm
1019a0ee8cc6SDag-Erling Smørgrav 	 * intersects with itself.  Matter of taste.
1020a0ee8cc6SDag-Erling Smørgrav 	 */
1021a0ee8cc6SDag-Erling Smørgrav 	char	*augmentation_string = " .o+=*BOX@%&#/^SE";
1022bc5531deSDag-Erling Smørgrav 	char	*retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
1023a0ee8cc6SDag-Erling Smørgrav 	u_char	 field[FLDSIZE_X][FLDSIZE_Y];
1024bc5531deSDag-Erling Smørgrav 	size_t	 i, tlen, hlen;
1025a0ee8cc6SDag-Erling Smørgrav 	u_int	 b;
1026a0ee8cc6SDag-Erling Smørgrav 	int	 x, y, r;
1027a0ee8cc6SDag-Erling Smørgrav 	size_t	 len = strlen(augmentation_string) - 1;
1028a0ee8cc6SDag-Erling Smørgrav 
1029a0ee8cc6SDag-Erling Smørgrav 	if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
1030a0ee8cc6SDag-Erling Smørgrav 		return NULL;
1031a0ee8cc6SDag-Erling Smørgrav 
1032a0ee8cc6SDag-Erling Smørgrav 	/* initialize field */
1033a0ee8cc6SDag-Erling Smørgrav 	memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1034a0ee8cc6SDag-Erling Smørgrav 	x = FLDSIZE_X / 2;
1035a0ee8cc6SDag-Erling Smørgrav 	y = FLDSIZE_Y / 2;
1036a0ee8cc6SDag-Erling Smørgrav 
1037a0ee8cc6SDag-Erling Smørgrav 	/* process raw key */
1038a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; i < dgst_raw_len; i++) {
1039a0ee8cc6SDag-Erling Smørgrav 		int input;
1040a0ee8cc6SDag-Erling Smørgrav 		/* each byte conveys four 2-bit move commands */
1041a0ee8cc6SDag-Erling Smørgrav 		input = dgst_raw[i];
1042a0ee8cc6SDag-Erling Smørgrav 		for (b = 0; b < 4; b++) {
1043a0ee8cc6SDag-Erling Smørgrav 			/* evaluate 2 bit, rest is shifted later */
1044a0ee8cc6SDag-Erling Smørgrav 			x += (input & 0x1) ? 1 : -1;
1045a0ee8cc6SDag-Erling Smørgrav 			y += (input & 0x2) ? 1 : -1;
1046a0ee8cc6SDag-Erling Smørgrav 
1047a0ee8cc6SDag-Erling Smørgrav 			/* assure we are still in bounds */
1048ca86bcf2SDag-Erling Smørgrav 			x = MAXIMUM(x, 0);
1049ca86bcf2SDag-Erling Smørgrav 			y = MAXIMUM(y, 0);
1050ca86bcf2SDag-Erling Smørgrav 			x = MINIMUM(x, FLDSIZE_X - 1);
1051ca86bcf2SDag-Erling Smørgrav 			y = MINIMUM(y, FLDSIZE_Y - 1);
1052a0ee8cc6SDag-Erling Smørgrav 
1053a0ee8cc6SDag-Erling Smørgrav 			/* augment the field */
1054a0ee8cc6SDag-Erling Smørgrav 			if (field[x][y] < len - 2)
1055a0ee8cc6SDag-Erling Smørgrav 				field[x][y]++;
1056a0ee8cc6SDag-Erling Smørgrav 			input = input >> 2;
1057a0ee8cc6SDag-Erling Smørgrav 		}
1058a0ee8cc6SDag-Erling Smørgrav 	}
1059a0ee8cc6SDag-Erling Smørgrav 
1060a0ee8cc6SDag-Erling Smørgrav 	/* mark starting point and end point*/
1061a0ee8cc6SDag-Erling Smørgrav 	field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
1062a0ee8cc6SDag-Erling Smørgrav 	field[x][y] = len;
1063a0ee8cc6SDag-Erling Smørgrav 
1064a0ee8cc6SDag-Erling Smørgrav 	/* assemble title */
1065a0ee8cc6SDag-Erling Smørgrav 	r = snprintf(title, sizeof(title), "[%s %u]",
1066a0ee8cc6SDag-Erling Smørgrav 		sshkey_type(k), sshkey_size(k));
1067a0ee8cc6SDag-Erling Smørgrav 	/* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
1068a0ee8cc6SDag-Erling Smørgrav 	if (r < 0 || r > (int)sizeof(title))
1069bc5531deSDag-Erling Smørgrav 		r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
1070bc5531deSDag-Erling Smørgrav 	tlen = (r <= 0) ? 0 : strlen(title);
1071bc5531deSDag-Erling Smørgrav 
1072bc5531deSDag-Erling Smørgrav 	/* assemble hash ID. */
1073bc5531deSDag-Erling Smørgrav 	r = snprintf(hash, sizeof(hash), "[%s]", alg);
1074bc5531deSDag-Erling Smørgrav 	hlen = (r <= 0) ? 0 : strlen(hash);
1075a0ee8cc6SDag-Erling Smørgrav 
1076a0ee8cc6SDag-Erling Smørgrav 	/* output upper border */
1077a0ee8cc6SDag-Erling Smørgrav 	p = retval;
1078a0ee8cc6SDag-Erling Smørgrav 	*p++ = '+';
1079a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
1080a0ee8cc6SDag-Erling Smørgrav 		*p++ = '-';
1081a0ee8cc6SDag-Erling Smørgrav 	memcpy(p, title, tlen);
1082a0ee8cc6SDag-Erling Smørgrav 	p += tlen;
1083bc5531deSDag-Erling Smørgrav 	for (i += tlen; i < FLDSIZE_X; i++)
1084a0ee8cc6SDag-Erling Smørgrav 		*p++ = '-';
1085a0ee8cc6SDag-Erling Smørgrav 	*p++ = '+';
1086a0ee8cc6SDag-Erling Smørgrav 	*p++ = '\n';
1087a0ee8cc6SDag-Erling Smørgrav 
1088a0ee8cc6SDag-Erling Smørgrav 	/* output content */
1089a0ee8cc6SDag-Erling Smørgrav 	for (y = 0; y < FLDSIZE_Y; y++) {
1090a0ee8cc6SDag-Erling Smørgrav 		*p++ = '|';
1091a0ee8cc6SDag-Erling Smørgrav 		for (x = 0; x < FLDSIZE_X; x++)
1092ca86bcf2SDag-Erling Smørgrav 			*p++ = augmentation_string[MINIMUM(field[x][y], len)];
1093a0ee8cc6SDag-Erling Smørgrav 		*p++ = '|';
1094a0ee8cc6SDag-Erling Smørgrav 		*p++ = '\n';
1095a0ee8cc6SDag-Erling Smørgrav 	}
1096a0ee8cc6SDag-Erling Smørgrav 
1097a0ee8cc6SDag-Erling Smørgrav 	/* output lower border */
1098a0ee8cc6SDag-Erling Smørgrav 	*p++ = '+';
1099bc5531deSDag-Erling Smørgrav 	for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
1100bc5531deSDag-Erling Smørgrav 		*p++ = '-';
1101bc5531deSDag-Erling Smørgrav 	memcpy(p, hash, hlen);
1102bc5531deSDag-Erling Smørgrav 	p += hlen;
1103bc5531deSDag-Erling Smørgrav 	for (i += hlen; i < FLDSIZE_X; i++)
1104a0ee8cc6SDag-Erling Smørgrav 		*p++ = '-';
1105a0ee8cc6SDag-Erling Smørgrav 	*p++ = '+';
1106a0ee8cc6SDag-Erling Smørgrav 
1107a0ee8cc6SDag-Erling Smørgrav 	return retval;
1108a0ee8cc6SDag-Erling Smørgrav }
1109a0ee8cc6SDag-Erling Smørgrav 
1110a0ee8cc6SDag-Erling Smørgrav char *
sshkey_fingerprint(const struct sshkey * k,int dgst_alg,enum sshkey_fp_rep dgst_rep)1111bc5531deSDag-Erling Smørgrav sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
1112a0ee8cc6SDag-Erling Smørgrav     enum sshkey_fp_rep dgst_rep)
1113a0ee8cc6SDag-Erling Smørgrav {
1114a0ee8cc6SDag-Erling Smørgrav 	char *retval = NULL;
1115a0ee8cc6SDag-Erling Smørgrav 	u_char *dgst_raw;
1116a0ee8cc6SDag-Erling Smørgrav 	size_t dgst_raw_len;
1117a0ee8cc6SDag-Erling Smørgrav 
1118bc5531deSDag-Erling Smørgrav 	if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
1119a0ee8cc6SDag-Erling Smørgrav 		return NULL;
1120a0ee8cc6SDag-Erling Smørgrav 	switch (dgst_rep) {
1121bc5531deSDag-Erling Smørgrav 	case SSH_FP_DEFAULT:
1122bc5531deSDag-Erling Smørgrav 		if (dgst_alg == SSH_DIGEST_MD5) {
1123bc5531deSDag-Erling Smørgrav 			retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1124bc5531deSDag-Erling Smørgrav 			    dgst_raw, dgst_raw_len);
1125bc5531deSDag-Erling Smørgrav 		} else {
1126bc5531deSDag-Erling Smørgrav 			retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1127bc5531deSDag-Erling Smørgrav 			    dgst_raw, dgst_raw_len);
1128bc5531deSDag-Erling Smørgrav 		}
1129bc5531deSDag-Erling Smørgrav 		break;
1130a0ee8cc6SDag-Erling Smørgrav 	case SSH_FP_HEX:
1131bc5531deSDag-Erling Smørgrav 		retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
1132bc5531deSDag-Erling Smørgrav 		    dgst_raw, dgst_raw_len);
1133bc5531deSDag-Erling Smørgrav 		break;
1134bc5531deSDag-Erling Smørgrav 	case SSH_FP_BASE64:
1135bc5531deSDag-Erling Smørgrav 		retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
1136bc5531deSDag-Erling Smørgrav 		    dgst_raw, dgst_raw_len);
1137a0ee8cc6SDag-Erling Smørgrav 		break;
1138a0ee8cc6SDag-Erling Smørgrav 	case SSH_FP_BUBBLEBABBLE:
1139a0ee8cc6SDag-Erling Smørgrav 		retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1140a0ee8cc6SDag-Erling Smørgrav 		break;
1141a0ee8cc6SDag-Erling Smørgrav 	case SSH_FP_RANDOMART:
1142bc5531deSDag-Erling Smørgrav 		retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
1143bc5531deSDag-Erling Smørgrav 		    dgst_raw, dgst_raw_len, k);
1144a0ee8cc6SDag-Erling Smørgrav 		break;
1145a0ee8cc6SDag-Erling Smørgrav 	default:
114619261079SEd Maste 		freezero(dgst_raw, dgst_raw_len);
1147a0ee8cc6SDag-Erling Smørgrav 		return NULL;
1148a0ee8cc6SDag-Erling Smørgrav 	}
114919261079SEd Maste 	freezero(dgst_raw, dgst_raw_len);
1150a0ee8cc6SDag-Erling Smørgrav 	return retval;
1151a0ee8cc6SDag-Erling Smørgrav }
1152a0ee8cc6SDag-Erling Smørgrav 
115347dd1d1bSDag-Erling Smørgrav static int
peek_type_nid(const char * s,size_t l,int * nid)115447dd1d1bSDag-Erling Smørgrav peek_type_nid(const char *s, size_t l, int *nid)
115547dd1d1bSDag-Erling Smørgrav {
1156f374ba41SEd Maste 	const struct sshkey_impl *impl;
1157f374ba41SEd Maste 	int i;
1158a0ee8cc6SDag-Erling Smørgrav 
1159f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
1160f374ba41SEd Maste 		impl = keyimpls[i];
1161f374ba41SEd Maste 		if (impl->name == NULL || strlen(impl->name) != l)
116247dd1d1bSDag-Erling Smørgrav 			continue;
1163f374ba41SEd Maste 		if (memcmp(s, impl->name, l) == 0) {
116447dd1d1bSDag-Erling Smørgrav 			*nid = -1;
1165f374ba41SEd Maste 			if (key_type_is_ecdsa_variant(impl->type))
1166f374ba41SEd Maste 				*nid = impl->nid;
1167f374ba41SEd Maste 			return impl->type;
116847dd1d1bSDag-Erling Smørgrav 		}
116947dd1d1bSDag-Erling Smørgrav 	}
117047dd1d1bSDag-Erling Smørgrav 	return KEY_UNSPEC;
117147dd1d1bSDag-Erling Smørgrav }
117247dd1d1bSDag-Erling Smørgrav 
117347dd1d1bSDag-Erling Smørgrav /* XXX this can now be made const char * */
1174a0ee8cc6SDag-Erling Smørgrav int
sshkey_read(struct sshkey * ret,char ** cpp)1175a0ee8cc6SDag-Erling Smørgrav sshkey_read(struct sshkey *ret, char **cpp)
1176a0ee8cc6SDag-Erling Smørgrav {
1177a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *k;
117847dd1d1bSDag-Erling Smørgrav 	char *cp, *blobcopy;
117947dd1d1bSDag-Erling Smørgrav 	size_t space;
1180a0ee8cc6SDag-Erling Smørgrav 	int r, type, curve_nid = -1;
1181a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *blob;
1182a0ee8cc6SDag-Erling Smørgrav 
1183d93a896eSDag-Erling Smørgrav 	if (ret == NULL)
1184d93a896eSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
1185f374ba41SEd Maste 	if (ret->type != KEY_UNSPEC && sshkey_impl_from_type(ret->type) == NULL)
118647dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
118747dd1d1bSDag-Erling Smørgrav 
118847dd1d1bSDag-Erling Smørgrav 	/* Decode type */
118947dd1d1bSDag-Erling Smørgrav 	cp = *cpp;
119047dd1d1bSDag-Erling Smørgrav 	space = strcspn(cp, " \t");
119147dd1d1bSDag-Erling Smørgrav 	if (space == strlen(cp))
1192a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_FORMAT;
119347dd1d1bSDag-Erling Smørgrav 	if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
1194a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_FORMAT;
119547dd1d1bSDag-Erling Smørgrav 
119647dd1d1bSDag-Erling Smørgrav 	/* skip whitespace */
119747dd1d1bSDag-Erling Smørgrav 	for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
119847dd1d1bSDag-Erling Smørgrav 		;
1199a0ee8cc6SDag-Erling Smørgrav 	if (*cp == '\0')
1200a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_FORMAT;
1201bc5531deSDag-Erling Smørgrav 	if (ret->type != KEY_UNSPEC && ret->type != type)
1202a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_MISMATCH;
1203a0ee8cc6SDag-Erling Smørgrav 	if ((blob = sshbuf_new()) == NULL)
1204a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
120547dd1d1bSDag-Erling Smørgrav 
120647dd1d1bSDag-Erling Smørgrav 	/* find end of keyblob and decode */
120747dd1d1bSDag-Erling Smørgrav 	space = strcspn(cp, " \t");
120847dd1d1bSDag-Erling Smørgrav 	if ((blobcopy = strndup(cp, space)) == NULL) {
120947dd1d1bSDag-Erling Smørgrav 		sshbuf_free(blob);
121047dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
121147dd1d1bSDag-Erling Smørgrav 	}
121247dd1d1bSDag-Erling Smørgrav 	if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
121347dd1d1bSDag-Erling Smørgrav 		free(blobcopy);
1214a0ee8cc6SDag-Erling Smørgrav 		sshbuf_free(blob);
1215a0ee8cc6SDag-Erling Smørgrav 		return r;
1216a0ee8cc6SDag-Erling Smørgrav 	}
121747dd1d1bSDag-Erling Smørgrav 	free(blobcopy);
121847dd1d1bSDag-Erling Smørgrav 	if ((r = sshkey_fromb(blob, &k)) != 0) {
1219a0ee8cc6SDag-Erling Smørgrav 		sshbuf_free(blob);
1220a0ee8cc6SDag-Erling Smørgrav 		return r;
1221a0ee8cc6SDag-Erling Smørgrav 	}
1222a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(blob);
122347dd1d1bSDag-Erling Smørgrav 
122447dd1d1bSDag-Erling Smørgrav 	/* skip whitespace and leave cp at start of comment */
122547dd1d1bSDag-Erling Smørgrav 	for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
122647dd1d1bSDag-Erling Smørgrav 		;
122747dd1d1bSDag-Erling Smørgrav 
122847dd1d1bSDag-Erling Smørgrav 	/* ensure type of blob matches type at start of line */
1229a0ee8cc6SDag-Erling Smørgrav 	if (k->type != type) {
1230a0ee8cc6SDag-Erling Smørgrav 		sshkey_free(k);
1231a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_MISMATCH;
1232a0ee8cc6SDag-Erling Smørgrav 	}
123319261079SEd Maste 	if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
1234a0ee8cc6SDag-Erling Smørgrav 		sshkey_free(k);
1235a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_EC_CURVE_MISMATCH;
1236a0ee8cc6SDag-Erling Smørgrav 	}
123747dd1d1bSDag-Erling Smørgrav 
123847dd1d1bSDag-Erling Smørgrav 	/* Fill in ret from parsed key */
1239f374ba41SEd Maste 	sshkey_free_contents(ret);
1240f374ba41SEd Maste 	*ret = *k;
1241f374ba41SEd Maste 	freezero(k, sizeof(*k));
124247dd1d1bSDag-Erling Smørgrav 
124347dd1d1bSDag-Erling Smørgrav 	/* success */
124447dd1d1bSDag-Erling Smørgrav 	*cpp = cp;
124547dd1d1bSDag-Erling Smørgrav 	return 0;
1246a0ee8cc6SDag-Erling Smørgrav }
1247a0ee8cc6SDag-Erling Smørgrav 
1248a0ee8cc6SDag-Erling Smørgrav int
sshkey_to_base64(const struct sshkey * key,char ** b64p)1249557f75e5SDag-Erling Smørgrav sshkey_to_base64(const struct sshkey *key, char **b64p)
1250a0ee8cc6SDag-Erling Smørgrav {
1251557f75e5SDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
1252557f75e5SDag-Erling Smørgrav 	struct sshbuf *b = NULL;
1253a0ee8cc6SDag-Erling Smørgrav 	char *uu = NULL;
1254557f75e5SDag-Erling Smørgrav 
1255557f75e5SDag-Erling Smørgrav 	if (b64p != NULL)
1256557f75e5SDag-Erling Smørgrav 		*b64p = NULL;
1257557f75e5SDag-Erling Smørgrav 	if ((b = sshbuf_new()) == NULL)
1258557f75e5SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
1259557f75e5SDag-Erling Smørgrav 	if ((r = sshkey_putb(key, b)) != 0)
1260557f75e5SDag-Erling Smørgrav 		goto out;
126119261079SEd Maste 	if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
1262557f75e5SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
1263557f75e5SDag-Erling Smørgrav 		goto out;
1264557f75e5SDag-Erling Smørgrav 	}
1265557f75e5SDag-Erling Smørgrav 	/* Success */
1266557f75e5SDag-Erling Smørgrav 	if (b64p != NULL) {
1267557f75e5SDag-Erling Smørgrav 		*b64p = uu;
1268557f75e5SDag-Erling Smørgrav 		uu = NULL;
1269557f75e5SDag-Erling Smørgrav 	}
1270557f75e5SDag-Erling Smørgrav 	r = 0;
1271557f75e5SDag-Erling Smørgrav  out:
1272557f75e5SDag-Erling Smørgrav 	sshbuf_free(b);
1273557f75e5SDag-Erling Smørgrav 	free(uu);
1274557f75e5SDag-Erling Smørgrav 	return r;
1275557f75e5SDag-Erling Smørgrav }
1276557f75e5SDag-Erling Smørgrav 
12774f52dfbbSDag-Erling Smørgrav int
sshkey_format_text(const struct sshkey * key,struct sshbuf * b)1278557f75e5SDag-Erling Smørgrav sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
1279557f75e5SDag-Erling Smørgrav {
1280557f75e5SDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
1281557f75e5SDag-Erling Smørgrav 	char *uu = NULL;
1282557f75e5SDag-Erling Smørgrav 
1283557f75e5SDag-Erling Smørgrav 	if ((r = sshkey_to_base64(key, &uu)) != 0)
1284557f75e5SDag-Erling Smørgrav 		goto out;
1285557f75e5SDag-Erling Smørgrav 	if ((r = sshbuf_putf(b, "%s %s",
1286557f75e5SDag-Erling Smørgrav 	    sshkey_ssh_name(key), uu)) != 0)
1287557f75e5SDag-Erling Smørgrav 		goto out;
1288557f75e5SDag-Erling Smørgrav 	r = 0;
1289557f75e5SDag-Erling Smørgrav  out:
1290557f75e5SDag-Erling Smørgrav 	free(uu);
1291557f75e5SDag-Erling Smørgrav 	return r;
1292557f75e5SDag-Erling Smørgrav }
1293557f75e5SDag-Erling Smørgrav 
1294557f75e5SDag-Erling Smørgrav int
sshkey_write(const struct sshkey * key,FILE * f)1295557f75e5SDag-Erling Smørgrav sshkey_write(const struct sshkey *key, FILE *f)
1296557f75e5SDag-Erling Smørgrav {
1297557f75e5SDag-Erling Smørgrav 	struct sshbuf *b = NULL;
1298557f75e5SDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
1299557f75e5SDag-Erling Smørgrav 
1300557f75e5SDag-Erling Smørgrav 	if ((b = sshbuf_new()) == NULL)
1301557f75e5SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
1302557f75e5SDag-Erling Smørgrav 	if ((r = sshkey_format_text(key, b)) != 0)
1303557f75e5SDag-Erling Smørgrav 		goto out;
1304557f75e5SDag-Erling Smørgrav 	if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
1305557f75e5SDag-Erling Smørgrav 		if (feof(f))
1306557f75e5SDag-Erling Smørgrav 			errno = EPIPE;
1307557f75e5SDag-Erling Smørgrav 		r = SSH_ERR_SYSTEM_ERROR;
1308557f75e5SDag-Erling Smørgrav 		goto out;
1309557f75e5SDag-Erling Smørgrav 	}
1310557f75e5SDag-Erling Smørgrav 	/* Success */
1311557f75e5SDag-Erling Smørgrav 	r = 0;
1312557f75e5SDag-Erling Smørgrav  out:
1313557f75e5SDag-Erling Smørgrav 	sshbuf_free(b);
1314557f75e5SDag-Erling Smørgrav 	return r;
1315a0ee8cc6SDag-Erling Smørgrav }
1316a0ee8cc6SDag-Erling Smørgrav 
1317a0ee8cc6SDag-Erling Smørgrav const char *
sshkey_cert_type(const struct sshkey * k)1318a0ee8cc6SDag-Erling Smørgrav sshkey_cert_type(const struct sshkey *k)
1319a0ee8cc6SDag-Erling Smørgrav {
1320a0ee8cc6SDag-Erling Smørgrav 	switch (k->cert->type) {
1321a0ee8cc6SDag-Erling Smørgrav 	case SSH2_CERT_TYPE_USER:
1322a0ee8cc6SDag-Erling Smørgrav 		return "user";
1323a0ee8cc6SDag-Erling Smørgrav 	case SSH2_CERT_TYPE_HOST:
1324a0ee8cc6SDag-Erling Smørgrav 		return "host";
1325a0ee8cc6SDag-Erling Smørgrav 	default:
1326a0ee8cc6SDag-Erling Smørgrav 		return "unknown";
1327a0ee8cc6SDag-Erling Smørgrav 	}
1328a0ee8cc6SDag-Erling Smørgrav }
1329a0ee8cc6SDag-Erling Smørgrav 
1330f374ba41SEd Maste int
sshkey_check_rsa_length(const struct sshkey * k,int min_size)1331f374ba41SEd Maste sshkey_check_rsa_length(const struct sshkey *k, int min_size)
1332f374ba41SEd Maste {
1333a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
1334f374ba41SEd Maste 	const BIGNUM *rsa_n;
1335f374ba41SEd Maste 	int nbits;
1336a0ee8cc6SDag-Erling Smørgrav 
1337f374ba41SEd Maste 	if (k == NULL || k->rsa == NULL ||
1338f374ba41SEd Maste 	    (k->type != KEY_RSA && k->type != KEY_RSA_CERT))
1339f374ba41SEd Maste 		return 0;
1340f374ba41SEd Maste 	RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
1341f374ba41SEd Maste 	nbits = BN_num_bits(rsa_n);
1342f374ba41SEd Maste 	if (nbits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
1343f374ba41SEd Maste 	    (min_size > 0 && nbits < min_size))
13444f52dfbbSDag-Erling Smørgrav 		return SSH_ERR_KEY_LENGTH;
1345f374ba41SEd Maste #endif /* WITH_OPENSSL */
1346f374ba41SEd Maste 	return 0;
1347a0ee8cc6SDag-Erling Smørgrav }
1348a0ee8cc6SDag-Erling Smørgrav 
1349f374ba41SEd Maste #ifdef WITH_OPENSSL
1350a0ee8cc6SDag-Erling Smørgrav # ifdef OPENSSL_HAS_ECC
1351a0ee8cc6SDag-Erling Smørgrav int
sshkey_ecdsa_key_to_nid(EC_KEY * k)1352a0ee8cc6SDag-Erling Smørgrav sshkey_ecdsa_key_to_nid(EC_KEY *k)
1353a0ee8cc6SDag-Erling Smørgrav {
1354a0ee8cc6SDag-Erling Smørgrav 	EC_GROUP *eg;
1355a0ee8cc6SDag-Erling Smørgrav 	int nids[] = {
1356a0ee8cc6SDag-Erling Smørgrav 		NID_X9_62_prime256v1,
1357a0ee8cc6SDag-Erling Smørgrav 		NID_secp384r1,
1358a0ee8cc6SDag-Erling Smørgrav #  ifdef OPENSSL_HAS_NISTP521
1359a0ee8cc6SDag-Erling Smørgrav 		NID_secp521r1,
1360a0ee8cc6SDag-Erling Smørgrav #  endif /* OPENSSL_HAS_NISTP521 */
1361a0ee8cc6SDag-Erling Smørgrav 		-1
1362a0ee8cc6SDag-Erling Smørgrav 	};
1363a0ee8cc6SDag-Erling Smørgrav 	int nid;
1364a0ee8cc6SDag-Erling Smørgrav 	u_int i;
1365a0ee8cc6SDag-Erling Smørgrav 	const EC_GROUP *g = EC_KEY_get0_group(k);
1366a0ee8cc6SDag-Erling Smørgrav 
1367a0ee8cc6SDag-Erling Smørgrav 	/*
1368a0ee8cc6SDag-Erling Smørgrav 	 * The group may be stored in a ASN.1 encoded private key in one of two
1369a0ee8cc6SDag-Erling Smørgrav 	 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1370a0ee8cc6SDag-Erling Smørgrav 	 * or explicit group parameters encoded into the key blob. Only the
1371a0ee8cc6SDag-Erling Smørgrav 	 * "named group" case sets the group NID for us, but we can figure
1372a0ee8cc6SDag-Erling Smørgrav 	 * it out for the other case by comparing against all the groups that
1373a0ee8cc6SDag-Erling Smørgrav 	 * are supported.
1374a0ee8cc6SDag-Erling Smørgrav 	 */
1375a0ee8cc6SDag-Erling Smørgrav 	if ((nid = EC_GROUP_get_curve_name(g)) > 0)
1376a0ee8cc6SDag-Erling Smørgrav 		return nid;
1377a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; nids[i] != -1; i++) {
137819261079SEd Maste 		if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
1379a0ee8cc6SDag-Erling Smørgrav 			return -1;
138019261079SEd Maste 		if (EC_GROUP_cmp(g, eg, NULL) == 0)
1381a0ee8cc6SDag-Erling Smørgrav 			break;
1382a0ee8cc6SDag-Erling Smørgrav 		EC_GROUP_free(eg);
1383a0ee8cc6SDag-Erling Smørgrav 	}
1384a0ee8cc6SDag-Erling Smørgrav 	if (nids[i] != -1) {
1385a0ee8cc6SDag-Erling Smørgrav 		/* Use the group with the NID attached */
1386a0ee8cc6SDag-Erling Smørgrav 		EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
1387a0ee8cc6SDag-Erling Smørgrav 		if (EC_KEY_set_group(k, eg) != 1) {
1388a0ee8cc6SDag-Erling Smørgrav 			EC_GROUP_free(eg);
1389a0ee8cc6SDag-Erling Smørgrav 			return -1;
1390a0ee8cc6SDag-Erling Smørgrav 		}
1391a0ee8cc6SDag-Erling Smørgrav 	}
1392a0ee8cc6SDag-Erling Smørgrav 	return nids[i];
1393a0ee8cc6SDag-Erling Smørgrav }
1394a0ee8cc6SDag-Erling Smørgrav # endif /* OPENSSL_HAS_ECC */
1395a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
1396a0ee8cc6SDag-Erling Smørgrav 
1397a0ee8cc6SDag-Erling Smørgrav int
sshkey_generate(int type,u_int bits,struct sshkey ** keyp)1398a0ee8cc6SDag-Erling Smørgrav sshkey_generate(int type, u_int bits, struct sshkey **keyp)
1399a0ee8cc6SDag-Erling Smørgrav {
1400a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *k;
1401a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_INTERNAL_ERROR;
1402f374ba41SEd Maste 	const struct sshkey_impl *impl;
1403a0ee8cc6SDag-Erling Smørgrav 
1404f374ba41SEd Maste 	if (keyp == NULL || sshkey_type_is_cert(type))
1405a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
1406a0ee8cc6SDag-Erling Smørgrav 	*keyp = NULL;
1407f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL)
1408f374ba41SEd Maste 		return SSH_ERR_KEY_TYPE_UNKNOWN;
1409f374ba41SEd Maste 	if (impl->funcs->generate == NULL)
1410f374ba41SEd Maste 		return SSH_ERR_FEATURE_UNSUPPORTED;
1411a0ee8cc6SDag-Erling Smørgrav 	if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
1412a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
1413a0ee8cc6SDag-Erling Smørgrav 	k->type = type;
1414f374ba41SEd Maste 	if ((ret = impl->funcs->generate(k, bits)) != 0) {
1415a0ee8cc6SDag-Erling Smørgrav 		sshkey_free(k);
1416a0ee8cc6SDag-Erling Smørgrav 		return ret;
1417a0ee8cc6SDag-Erling Smørgrav 	}
1418f374ba41SEd Maste 	/* success */
1419f374ba41SEd Maste 	*keyp = k;
1420f374ba41SEd Maste 	return 0;
1421f374ba41SEd Maste }
1422a0ee8cc6SDag-Erling Smørgrav 
1423a0ee8cc6SDag-Erling Smørgrav int
sshkey_cert_copy(const struct sshkey * from_key,struct sshkey * to_key)1424a0ee8cc6SDag-Erling Smørgrav sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
1425a0ee8cc6SDag-Erling Smørgrav {
1426a0ee8cc6SDag-Erling Smørgrav 	u_int i;
1427a0ee8cc6SDag-Erling Smørgrav 	const struct sshkey_cert *from;
1428a0ee8cc6SDag-Erling Smørgrav 	struct sshkey_cert *to;
14292f513db7SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
1430a0ee8cc6SDag-Erling Smørgrav 
14312f513db7SEd Maste 	if (to_key == NULL || (from = from_key->cert) == NULL)
1432a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
1433a0ee8cc6SDag-Erling Smørgrav 
14342f513db7SEd Maste 	if ((to = cert_new()) == NULL)
1435a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
1436a0ee8cc6SDag-Erling Smørgrav 
14372f513db7SEd Maste 	if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
14382f513db7SEd Maste 	    (r = sshbuf_putb(to->critical, from->critical)) != 0 ||
14392f513db7SEd Maste 	    (r = sshbuf_putb(to->extensions, from->extensions)) != 0)
14402f513db7SEd Maste 		goto out;
1441a0ee8cc6SDag-Erling Smørgrav 
1442a0ee8cc6SDag-Erling Smørgrav 	to->serial = from->serial;
1443a0ee8cc6SDag-Erling Smørgrav 	to->type = from->type;
1444a0ee8cc6SDag-Erling Smørgrav 	if (from->key_id == NULL)
1445a0ee8cc6SDag-Erling Smørgrav 		to->key_id = NULL;
14462f513db7SEd Maste 	else if ((to->key_id = strdup(from->key_id)) == NULL) {
14472f513db7SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
14482f513db7SEd Maste 		goto out;
14492f513db7SEd Maste 	}
1450a0ee8cc6SDag-Erling Smørgrav 	to->valid_after = from->valid_after;
1451a0ee8cc6SDag-Erling Smørgrav 	to->valid_before = from->valid_before;
1452a0ee8cc6SDag-Erling Smørgrav 	if (from->signature_key == NULL)
1453a0ee8cc6SDag-Erling Smørgrav 		to->signature_key = NULL;
14542f513db7SEd Maste 	else if ((r = sshkey_from_private(from->signature_key,
1455a0ee8cc6SDag-Erling Smørgrav 	    &to->signature_key)) != 0)
14562f513db7SEd Maste 		goto out;
14572f513db7SEd Maste 	if (from->signature_type != NULL &&
14582f513db7SEd Maste 	    (to->signature_type = strdup(from->signature_type)) == NULL) {
14592f513db7SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
14602f513db7SEd Maste 		goto out;
14612f513db7SEd Maste 	}
14622f513db7SEd Maste 	if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
14632f513db7SEd Maste 		r = SSH_ERR_INVALID_ARGUMENT;
14642f513db7SEd Maste 		goto out;
14652f513db7SEd Maste 	}
1466a0ee8cc6SDag-Erling Smørgrav 	if (from->nprincipals > 0) {
1467a0ee8cc6SDag-Erling Smørgrav 		if ((to->principals = calloc(from->nprincipals,
14682f513db7SEd Maste 		    sizeof(*to->principals))) == NULL) {
14692f513db7SEd Maste 			r = SSH_ERR_ALLOC_FAIL;
14702f513db7SEd Maste 			goto out;
14712f513db7SEd Maste 		}
1472a0ee8cc6SDag-Erling Smørgrav 		for (i = 0; i < from->nprincipals; i++) {
1473a0ee8cc6SDag-Erling Smørgrav 			to->principals[i] = strdup(from->principals[i]);
1474a0ee8cc6SDag-Erling Smørgrav 			if (to->principals[i] == NULL) {
1475a0ee8cc6SDag-Erling Smørgrav 				to->nprincipals = i;
14762f513db7SEd Maste 				r = SSH_ERR_ALLOC_FAIL;
14772f513db7SEd Maste 				goto out;
1478a0ee8cc6SDag-Erling Smørgrav 			}
1479a0ee8cc6SDag-Erling Smørgrav 		}
1480a0ee8cc6SDag-Erling Smørgrav 	}
1481a0ee8cc6SDag-Erling Smørgrav 	to->nprincipals = from->nprincipals;
14822f513db7SEd Maste 
14832f513db7SEd Maste 	/* success */
14842f513db7SEd Maste 	cert_free(to_key->cert);
14852f513db7SEd Maste 	to_key->cert = to;
14862f513db7SEd Maste 	to = NULL;
14872f513db7SEd Maste 	r = 0;
14882f513db7SEd Maste  out:
14892f513db7SEd Maste 	cert_free(to);
14902f513db7SEd Maste 	return r;
1491a0ee8cc6SDag-Erling Smørgrav }
1492a0ee8cc6SDag-Erling Smørgrav 
1493a0ee8cc6SDag-Erling Smørgrav int
sshkey_copy_public_sk(const struct sshkey * from,struct sshkey * to)1494f374ba41SEd Maste sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to)
1495f374ba41SEd Maste {
1496f374ba41SEd Maste 	/* Append security-key application string */
1497f374ba41SEd Maste 	if ((to->sk_application = strdup(from->sk_application)) == NULL)
1498f374ba41SEd Maste 		return SSH_ERR_ALLOC_FAIL;
1499f374ba41SEd Maste 	return 0;
1500f374ba41SEd Maste }
1501f374ba41SEd Maste 
1502f374ba41SEd Maste int
sshkey_from_private(const struct sshkey * k,struct sshkey ** pkp)1503a0ee8cc6SDag-Erling Smørgrav sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
1504a0ee8cc6SDag-Erling Smørgrav {
1505a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *n = NULL;
15062a01feabSEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
1507f374ba41SEd Maste 	const struct sshkey_impl *impl;
1508a0ee8cc6SDag-Erling Smørgrav 
1509a0ee8cc6SDag-Erling Smørgrav 	*pkp = NULL;
1510f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(k)) == NULL)
1511f374ba41SEd Maste 		return SSH_ERR_KEY_TYPE_UNKNOWN;
15122a01feabSEd Maste 	if ((n = sshkey_new(k->type)) == NULL) {
15132a01feabSEd Maste 		r = SSH_ERR_ALLOC_FAIL;
15142a01feabSEd Maste 		goto out;
1515a0ee8cc6SDag-Erling Smørgrav 	}
1516f374ba41SEd Maste 	if ((r = impl->funcs->copy_public(k, n)) != 0)
15172a01feabSEd Maste 		goto out;
15182a01feabSEd Maste 	if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
15192a01feabSEd Maste 		goto out;
15202a01feabSEd Maste 	/* success */
1521a0ee8cc6SDag-Erling Smørgrav 	*pkp = n;
15222a01feabSEd Maste 	n = NULL;
15232a01feabSEd Maste 	r = 0;
15242a01feabSEd Maste  out:
15252a01feabSEd Maste 	sshkey_free(n);
15262a01feabSEd Maste 	return r;
1527a0ee8cc6SDag-Erling Smørgrav }
1528a0ee8cc6SDag-Erling Smørgrav 
152919261079SEd Maste int
sshkey_is_shielded(struct sshkey * k)153019261079SEd Maste sshkey_is_shielded(struct sshkey *k)
153119261079SEd Maste {
153219261079SEd Maste 	return k != NULL && k->shielded_private != NULL;
153319261079SEd Maste }
153419261079SEd Maste 
153519261079SEd Maste int
sshkey_shield_private(struct sshkey * k)153619261079SEd Maste sshkey_shield_private(struct sshkey *k)
153719261079SEd Maste {
153819261079SEd Maste 	struct sshbuf *prvbuf = NULL;
153919261079SEd Maste 	u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
154019261079SEd Maste 	struct sshcipher_ctx *cctx = NULL;
154119261079SEd Maste 	const struct sshcipher *cipher;
154219261079SEd Maste 	size_t i, enclen = 0;
154319261079SEd Maste 	struct sshkey *kswap = NULL, tmp;
154419261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
154519261079SEd Maste 
154619261079SEd Maste #ifdef DEBUG_PK
154719261079SEd Maste 	fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
154819261079SEd Maste #endif
154919261079SEd Maste 	if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
155019261079SEd Maste 		r = SSH_ERR_INVALID_ARGUMENT;
155119261079SEd Maste 		goto out;
155219261079SEd Maste 	}
155319261079SEd Maste 	if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
155419261079SEd Maste 	    ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
155519261079SEd Maste 		r = SSH_ERR_INTERNAL_ERROR;
155619261079SEd Maste 		goto out;
155719261079SEd Maste 	}
155819261079SEd Maste 
155919261079SEd Maste 	/* Prepare a random pre-key, and from it an ephemeral key */
156019261079SEd Maste 	if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) {
156119261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
156219261079SEd Maste 		goto out;
156319261079SEd Maste 	}
156419261079SEd Maste 	arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
156519261079SEd Maste 	if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
156619261079SEd Maste 	    prekey, SSHKEY_SHIELD_PREKEY_LEN,
156719261079SEd Maste 	    keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
156819261079SEd Maste 		goto out;
156919261079SEd Maste #ifdef DEBUG_PK
157019261079SEd Maste 	fprintf(stderr, "%s: key+iv\n", __func__);
157119261079SEd Maste 	sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
157219261079SEd Maste 	    stderr);
157319261079SEd Maste #endif
157419261079SEd Maste 	if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
157519261079SEd Maste 	    keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
157619261079SEd Maste 		goto out;
157719261079SEd Maste 
157819261079SEd Maste 	/* Serialise and encrypt the private key using the ephemeral key */
157919261079SEd Maste 	if ((prvbuf = sshbuf_new()) == NULL) {
158019261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
158119261079SEd Maste 		goto out;
158219261079SEd Maste 	}
158319261079SEd Maste 	if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
158419261079SEd Maste 		goto out;
158519261079SEd Maste 	if ((r = sshkey_private_serialize_opt(k, prvbuf,
158619261079SEd Maste 	    SSHKEY_SERIALIZE_SHIELD)) != 0)
158719261079SEd Maste 		goto out;
158819261079SEd Maste 	/* pad to cipher blocksize */
158919261079SEd Maste 	i = 0;
159019261079SEd Maste 	while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
159119261079SEd Maste 		if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
159219261079SEd Maste 			goto out;
159319261079SEd Maste 	}
159419261079SEd Maste #ifdef DEBUG_PK
159519261079SEd Maste 	fprintf(stderr, "%s: serialised\n", __func__);
159619261079SEd Maste 	sshbuf_dump(prvbuf, stderr);
159719261079SEd Maste #endif
159819261079SEd Maste 	/* encrypt */
159919261079SEd Maste 	enclen = sshbuf_len(prvbuf);
160019261079SEd Maste 	if ((enc = malloc(enclen)) == NULL) {
160119261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
160219261079SEd Maste 		goto out;
160319261079SEd Maste 	}
160419261079SEd Maste 	if ((r = cipher_crypt(cctx, 0, enc,
160519261079SEd Maste 	    sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
160619261079SEd Maste 		goto out;
160719261079SEd Maste #ifdef DEBUG_PK
160819261079SEd Maste 	fprintf(stderr, "%s: encrypted\n", __func__);
160919261079SEd Maste 	sshbuf_dump_data(enc, enclen, stderr);
161019261079SEd Maste #endif
161119261079SEd Maste 
161219261079SEd Maste 	/* Make a scrubbed, public-only copy of our private key argument */
161319261079SEd Maste 	if ((r = sshkey_from_private(k, &kswap)) != 0)
161419261079SEd Maste 		goto out;
161519261079SEd Maste 
161619261079SEd Maste 	/* Swap the private key out (it will be destroyed below) */
161719261079SEd Maste 	tmp = *kswap;
161819261079SEd Maste 	*kswap = *k;
161919261079SEd Maste 	*k = tmp;
162019261079SEd Maste 
162119261079SEd Maste 	/* Insert the shielded key into our argument */
162219261079SEd Maste 	k->shielded_private = enc;
162319261079SEd Maste 	k->shielded_len = enclen;
162419261079SEd Maste 	k->shield_prekey = prekey;
162519261079SEd Maste 	k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
162619261079SEd Maste 	enc = prekey = NULL; /* transferred */
162719261079SEd Maste 	enclen = 0;
162819261079SEd Maste 
162919261079SEd Maste 	/* preserve key fields that are required for correct operation */
163019261079SEd Maste 	k->sk_flags = kswap->sk_flags;
163119261079SEd Maste 
163219261079SEd Maste 	/* success */
163319261079SEd Maste 	r = 0;
163419261079SEd Maste 
163519261079SEd Maste  out:
163619261079SEd Maste 	/* XXX behaviour on error - invalidate original private key? */
163719261079SEd Maste 	cipher_free(cctx);
163819261079SEd Maste 	explicit_bzero(keyiv, sizeof(keyiv));
163919261079SEd Maste 	explicit_bzero(&tmp, sizeof(tmp));
164019261079SEd Maste 	freezero(enc, enclen);
164119261079SEd Maste 	freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN);
164219261079SEd Maste 	sshkey_free(kswap);
164319261079SEd Maste 	sshbuf_free(prvbuf);
164419261079SEd Maste 	return r;
164519261079SEd Maste }
164619261079SEd Maste 
164738a52bd3SEd Maste /* Check deterministic padding after private key */
164838a52bd3SEd Maste static int
private2_check_padding(struct sshbuf * decrypted)164938a52bd3SEd Maste private2_check_padding(struct sshbuf *decrypted)
165038a52bd3SEd Maste {
165138a52bd3SEd Maste 	u_char pad;
165238a52bd3SEd Maste 	size_t i;
165338a52bd3SEd Maste 	int r;
165438a52bd3SEd Maste 
165538a52bd3SEd Maste 	i = 0;
165638a52bd3SEd Maste 	while (sshbuf_len(decrypted)) {
165738a52bd3SEd Maste 		if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
165838a52bd3SEd Maste 			goto out;
165938a52bd3SEd Maste 		if (pad != (++i & 0xff)) {
166038a52bd3SEd Maste 			r = SSH_ERR_INVALID_FORMAT;
166138a52bd3SEd Maste 			goto out;
166238a52bd3SEd Maste 		}
166338a52bd3SEd Maste 	}
166438a52bd3SEd Maste 	/* success */
166538a52bd3SEd Maste 	r = 0;
166638a52bd3SEd Maste  out:
166738a52bd3SEd Maste 	explicit_bzero(&pad, sizeof(pad));
166838a52bd3SEd Maste 	explicit_bzero(&i, sizeof(i));
166938a52bd3SEd Maste 	return r;
167038a52bd3SEd Maste }
167138a52bd3SEd Maste 
167219261079SEd Maste int
sshkey_unshield_private(struct sshkey * k)167319261079SEd Maste sshkey_unshield_private(struct sshkey *k)
167419261079SEd Maste {
167519261079SEd Maste 	struct sshbuf *prvbuf = NULL;
167638a52bd3SEd Maste 	u_char *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
167719261079SEd Maste 	struct sshcipher_ctx *cctx = NULL;
167819261079SEd Maste 	const struct sshcipher *cipher;
167919261079SEd Maste 	struct sshkey *kswap = NULL, tmp;
168019261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
168119261079SEd Maste 
168219261079SEd Maste #ifdef DEBUG_PK
168319261079SEd Maste 	fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
168419261079SEd Maste #endif
168519261079SEd Maste 	if (!sshkey_is_shielded(k))
168619261079SEd Maste 		return 0; /* nothing to do */
168719261079SEd Maste 
168819261079SEd Maste 	if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
168919261079SEd Maste 		r = SSH_ERR_INVALID_ARGUMENT;
169019261079SEd Maste 		goto out;
169119261079SEd Maste 	}
169219261079SEd Maste 	if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
169319261079SEd Maste 	    ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
169419261079SEd Maste 		r = SSH_ERR_INTERNAL_ERROR;
169519261079SEd Maste 		goto out;
169619261079SEd Maste 	}
169719261079SEd Maste 	/* check size of shielded key blob */
169819261079SEd Maste 	if (k->shielded_len < cipher_blocksize(cipher) ||
169919261079SEd Maste 	    (k->shielded_len % cipher_blocksize(cipher)) != 0) {
170019261079SEd Maste 		r = SSH_ERR_INVALID_FORMAT;
170119261079SEd Maste 		goto out;
170219261079SEd Maste 	}
170319261079SEd Maste 
170419261079SEd Maste 	/* Calculate the ephemeral key from the prekey */
170519261079SEd Maste 	if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
170619261079SEd Maste 	    k->shield_prekey, k->shield_prekey_len,
170719261079SEd Maste 	    keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
170819261079SEd Maste 		goto out;
170919261079SEd Maste 	if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
171019261079SEd Maste 	    keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
171119261079SEd Maste 		goto out;
171219261079SEd Maste #ifdef DEBUG_PK
171319261079SEd Maste 	fprintf(stderr, "%s: key+iv\n", __func__);
171419261079SEd Maste 	sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
171519261079SEd Maste 	    stderr);
171619261079SEd Maste #endif
171719261079SEd Maste 
171819261079SEd Maste 	/* Decrypt and parse the shielded private key using the ephemeral key */
171919261079SEd Maste 	if ((prvbuf = sshbuf_new()) == NULL) {
172019261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
172119261079SEd Maste 		goto out;
172219261079SEd Maste 	}
172319261079SEd Maste 	if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
172419261079SEd Maste 		goto out;
172519261079SEd Maste 	/* decrypt */
172619261079SEd Maste #ifdef DEBUG_PK
172719261079SEd Maste 	fprintf(stderr, "%s: encrypted\n", __func__);
172819261079SEd Maste 	sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
172919261079SEd Maste #endif
173019261079SEd Maste 	if ((r = cipher_crypt(cctx, 0, cp,
173119261079SEd Maste 	    k->shielded_private, k->shielded_len, 0, 0)) != 0)
173219261079SEd Maste 		goto out;
173319261079SEd Maste #ifdef DEBUG_PK
173419261079SEd Maste 	fprintf(stderr, "%s: serialised\n", __func__);
173519261079SEd Maste 	sshbuf_dump(prvbuf, stderr);
173619261079SEd Maste #endif
173719261079SEd Maste 	/* Parse private key */
173819261079SEd Maste 	if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
173919261079SEd Maste 		goto out;
174038a52bd3SEd Maste 
174138a52bd3SEd Maste 	if ((r = private2_check_padding(prvbuf)) != 0)
174219261079SEd Maste 		goto out;
174319261079SEd Maste 
174419261079SEd Maste 	/* Swap the parsed key back into place */
174519261079SEd Maste 	tmp = *kswap;
174619261079SEd Maste 	*kswap = *k;
174719261079SEd Maste 	*k = tmp;
174819261079SEd Maste 
174919261079SEd Maste 	/* success */
175019261079SEd Maste 	r = 0;
175119261079SEd Maste 
175219261079SEd Maste  out:
175319261079SEd Maste 	cipher_free(cctx);
175419261079SEd Maste 	explicit_bzero(keyiv, sizeof(keyiv));
175519261079SEd Maste 	explicit_bzero(&tmp, sizeof(tmp));
175619261079SEd Maste 	sshkey_free(kswap);
175719261079SEd Maste 	sshbuf_free(prvbuf);
175819261079SEd Maste 	return r;
175919261079SEd Maste }
176019261079SEd Maste 
1761a0ee8cc6SDag-Erling Smørgrav static int
cert_parse(struct sshbuf * b,struct sshkey * key,struct sshbuf * certbuf)1762bc5531deSDag-Erling Smørgrav cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
1763a0ee8cc6SDag-Erling Smørgrav {
1764bc5531deSDag-Erling Smørgrav 	struct sshbuf *principals = NULL, *crit = NULL;
1765bc5531deSDag-Erling Smørgrav 	struct sshbuf *exts = NULL, *ca = NULL;
1766bc5531deSDag-Erling Smørgrav 	u_char *sig = NULL;
1767bc5531deSDag-Erling Smørgrav 	size_t signed_len = 0, slen = 0, kidlen = 0;
1768a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_INTERNAL_ERROR;
1769a0ee8cc6SDag-Erling Smørgrav 
1770a0ee8cc6SDag-Erling Smørgrav 	/* Copy the entire key blob for verification and later serialisation */
1771bc5531deSDag-Erling Smørgrav 	if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
1772a0ee8cc6SDag-Erling Smørgrav 		return ret;
1773a0ee8cc6SDag-Erling Smørgrav 
1774eccfee6eSDag-Erling Smørgrav 	/* Parse body of certificate up to signature */
1775eccfee6eSDag-Erling Smørgrav 	if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
1776a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
1777a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
1778bc5531deSDag-Erling Smørgrav 	    (ret = sshbuf_froms(b, &principals)) != 0 ||
1779a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
1780a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
1781bc5531deSDag-Erling Smørgrav 	    (ret = sshbuf_froms(b, &crit)) != 0 ||
1782eccfee6eSDag-Erling Smørgrav 	    (ret = sshbuf_froms(b, &exts)) != 0 ||
1783a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
1784bc5531deSDag-Erling Smørgrav 	    (ret = sshbuf_froms(b, &ca)) != 0) {
1785a0ee8cc6SDag-Erling Smørgrav 		/* XXX debug print error for ret */
1786a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_INVALID_FORMAT;
1787a0ee8cc6SDag-Erling Smørgrav 		goto out;
1788a0ee8cc6SDag-Erling Smørgrav 	}
1789a0ee8cc6SDag-Erling Smørgrav 
1790a0ee8cc6SDag-Erling Smørgrav 	/* Signature is left in the buffer so we can calculate this length */
1791a0ee8cc6SDag-Erling Smørgrav 	signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
1792a0ee8cc6SDag-Erling Smørgrav 
1793a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
1794a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_INVALID_FORMAT;
1795a0ee8cc6SDag-Erling Smørgrav 		goto out;
1796a0ee8cc6SDag-Erling Smørgrav 	}
1797a0ee8cc6SDag-Erling Smørgrav 
1798a0ee8cc6SDag-Erling Smørgrav 	if (key->cert->type != SSH2_CERT_TYPE_USER &&
1799a0ee8cc6SDag-Erling Smørgrav 	    key->cert->type != SSH2_CERT_TYPE_HOST) {
1800a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
1801a0ee8cc6SDag-Erling Smørgrav 		goto out;
1802a0ee8cc6SDag-Erling Smørgrav 	}
1803a0ee8cc6SDag-Erling Smørgrav 
1804bc5531deSDag-Erling Smørgrav 	/* Parse principals section */
1805bc5531deSDag-Erling Smørgrav 	while (sshbuf_len(principals) > 0) {
1806bc5531deSDag-Erling Smørgrav 		char *principal = NULL;
1807bc5531deSDag-Erling Smørgrav 		char **oprincipals = NULL;
1808bc5531deSDag-Erling Smørgrav 
1809a0ee8cc6SDag-Erling Smørgrav 		if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
1810a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_INVALID_FORMAT;
1811a0ee8cc6SDag-Erling Smørgrav 			goto out;
1812a0ee8cc6SDag-Erling Smørgrav 		}
1813bc5531deSDag-Erling Smørgrav 		if ((ret = sshbuf_get_cstring(principals, &principal,
1814bc5531deSDag-Erling Smørgrav 		    NULL)) != 0) {
1815a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_INVALID_FORMAT;
1816a0ee8cc6SDag-Erling Smørgrav 			goto out;
1817a0ee8cc6SDag-Erling Smørgrav 		}
1818a0ee8cc6SDag-Erling Smørgrav 		oprincipals = key->cert->principals;
18194f52dfbbSDag-Erling Smørgrav 		key->cert->principals = recallocarray(key->cert->principals,
18204f52dfbbSDag-Erling Smørgrav 		    key->cert->nprincipals, key->cert->nprincipals + 1,
18214f52dfbbSDag-Erling Smørgrav 		    sizeof(*key->cert->principals));
1822a0ee8cc6SDag-Erling Smørgrav 		if (key->cert->principals == NULL) {
1823a0ee8cc6SDag-Erling Smørgrav 			free(principal);
1824a0ee8cc6SDag-Erling Smørgrav 			key->cert->principals = oprincipals;
1825a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_ALLOC_FAIL;
1826a0ee8cc6SDag-Erling Smørgrav 			goto out;
1827a0ee8cc6SDag-Erling Smørgrav 		}
1828a0ee8cc6SDag-Erling Smørgrav 		key->cert->principals[key->cert->nprincipals++] = principal;
1829a0ee8cc6SDag-Erling Smørgrav 	}
1830a0ee8cc6SDag-Erling Smørgrav 
1831bc5531deSDag-Erling Smørgrav 	/*
1832bc5531deSDag-Erling Smørgrav 	 * Stash a copies of the critical options and extensions sections
1833bc5531deSDag-Erling Smørgrav 	 * for later use.
1834bc5531deSDag-Erling Smørgrav 	 */
1835bc5531deSDag-Erling Smørgrav 	if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
1836bc5531deSDag-Erling Smørgrav 	    (exts != NULL &&
1837bc5531deSDag-Erling Smørgrav 	    (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
1838a0ee8cc6SDag-Erling Smørgrav 		goto out;
1839a0ee8cc6SDag-Erling Smørgrav 
1840bc5531deSDag-Erling Smørgrav 	/*
1841bc5531deSDag-Erling Smørgrav 	 * Validate critical options and extensions sections format.
1842bc5531deSDag-Erling Smørgrav 	 */
1843bc5531deSDag-Erling Smørgrav 	while (sshbuf_len(crit) != 0) {
1844bc5531deSDag-Erling Smørgrav 		if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
1845bc5531deSDag-Erling Smørgrav 		    (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
1846bc5531deSDag-Erling Smørgrav 			sshbuf_reset(key->cert->critical);
1847a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_INVALID_FORMAT;
1848a0ee8cc6SDag-Erling Smørgrav 			goto out;
1849a0ee8cc6SDag-Erling Smørgrav 		}
1850a0ee8cc6SDag-Erling Smørgrav 	}
1851bc5531deSDag-Erling Smørgrav 	while (exts != NULL && sshbuf_len(exts) != 0) {
1852bc5531deSDag-Erling Smørgrav 		if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
1853bc5531deSDag-Erling Smørgrav 		    (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
1854bc5531deSDag-Erling Smørgrav 			sshbuf_reset(key->cert->extensions);
1855a0ee8cc6SDag-Erling Smørgrav 			ret = SSH_ERR_INVALID_FORMAT;
1856a0ee8cc6SDag-Erling Smørgrav 			goto out;
1857a0ee8cc6SDag-Erling Smørgrav 		}
1858a0ee8cc6SDag-Erling Smørgrav 	}
1859a0ee8cc6SDag-Erling Smørgrav 
1860bc5531deSDag-Erling Smørgrav 	/* Parse CA key and check signature */
1861bc5531deSDag-Erling Smørgrav 	if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
1862a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1863a0ee8cc6SDag-Erling Smørgrav 		goto out;
1864a0ee8cc6SDag-Erling Smørgrav 	}
1865a0ee8cc6SDag-Erling Smørgrav 	if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
1866a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1867a0ee8cc6SDag-Erling Smørgrav 		goto out;
1868a0ee8cc6SDag-Erling Smørgrav 	}
1869a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
187019261079SEd Maste 	    sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
1871a0ee8cc6SDag-Erling Smørgrav 		goto out;
187219261079SEd Maste 	if ((ret = sshkey_get_sigtype(sig, slen,
187319261079SEd Maste 	    &key->cert->signature_type)) != 0)
18742f513db7SEd Maste 		goto out;
1875a0ee8cc6SDag-Erling Smørgrav 
1876bc5531deSDag-Erling Smørgrav 	/* Success */
1877bc5531deSDag-Erling Smørgrav 	ret = 0;
1878a0ee8cc6SDag-Erling Smørgrav  out:
1879bc5531deSDag-Erling Smørgrav 	sshbuf_free(ca);
1880bc5531deSDag-Erling Smørgrav 	sshbuf_free(crit);
1881bc5531deSDag-Erling Smørgrav 	sshbuf_free(exts);
1882bc5531deSDag-Erling Smørgrav 	sshbuf_free(principals);
1883a0ee8cc6SDag-Erling Smørgrav 	free(sig);
1884a0ee8cc6SDag-Erling Smørgrav 	return ret;
1885a0ee8cc6SDag-Erling Smørgrav }
1886a0ee8cc6SDag-Erling Smørgrav 
188738a52bd3SEd Maste int
sshkey_deserialize_sk(struct sshbuf * b,struct sshkey * key)1888f374ba41SEd Maste sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key)
18892a01feabSEd Maste {
1890f374ba41SEd Maste 	/* Parse additional security-key application string */
1891f374ba41SEd Maste 	if (sshbuf_get_cstring(b, &key->sk_application, NULL) != 0)
1892f374ba41SEd Maste 		return SSH_ERR_INVALID_FORMAT;
18932a01feabSEd Maste 	return 0;
18942a01feabSEd Maste }
18952a01feabSEd Maste 
1896a0ee8cc6SDag-Erling Smørgrav static int
sshkey_from_blob_internal(struct sshbuf * b,struct sshkey ** keyp,int allow_cert)1897bc5531deSDag-Erling Smørgrav sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
1898bc5531deSDag-Erling Smørgrav     int allow_cert)
1899a0ee8cc6SDag-Erling Smørgrav {
1900bc5531deSDag-Erling Smørgrav 	int type, ret = SSH_ERR_INTERNAL_ERROR;
1901f374ba41SEd Maste 	char *ktype = NULL;
1902a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *key = NULL;
1903bc5531deSDag-Erling Smørgrav 	struct sshbuf *copy;
1904f374ba41SEd Maste 	const struct sshkey_impl *impl;
1905a0ee8cc6SDag-Erling Smørgrav 
1906a0ee8cc6SDag-Erling Smørgrav #ifdef DEBUG_PK /* XXX */
1907bc5531deSDag-Erling Smørgrav 	sshbuf_dump(b, stderr);
1908a0ee8cc6SDag-Erling Smørgrav #endif
1909076ad2f8SDag-Erling Smørgrav 	if (keyp != NULL)
1910a0ee8cc6SDag-Erling Smørgrav 		*keyp = NULL;
1911bc5531deSDag-Erling Smørgrav 	if ((copy = sshbuf_fromb(b)) == NULL) {
1912bc5531deSDag-Erling Smørgrav 		ret = SSH_ERR_ALLOC_FAIL;
1913bc5531deSDag-Erling Smørgrav 		goto out;
1914bc5531deSDag-Erling Smørgrav 	}
1915a0ee8cc6SDag-Erling Smørgrav 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
1916a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_INVALID_FORMAT;
1917a0ee8cc6SDag-Erling Smørgrav 		goto out;
1918a0ee8cc6SDag-Erling Smørgrav 	}
1919a0ee8cc6SDag-Erling Smørgrav 
1920a0ee8cc6SDag-Erling Smørgrav 	type = sshkey_type_from_name(ktype);
1921a0ee8cc6SDag-Erling Smørgrav 	if (!allow_cert && sshkey_type_is_cert(type)) {
1922a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
1923a0ee8cc6SDag-Erling Smørgrav 		goto out;
1924a0ee8cc6SDag-Erling Smørgrav 	}
1925f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL) {
1926a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_KEY_TYPE_UNKNOWN;
1927a0ee8cc6SDag-Erling Smørgrav 		goto out;
1928a0ee8cc6SDag-Erling Smørgrav 	}
1929f374ba41SEd Maste 	if ((key = sshkey_new(type)) == NULL) {
1930f374ba41SEd Maste 		ret = SSH_ERR_ALLOC_FAIL;
1931f374ba41SEd Maste 		goto out;
1932f374ba41SEd Maste 	}
1933f374ba41SEd Maste 	if (sshkey_type_is_cert(type)) {
1934a91a2465SEd Maste 		/* Skip nonce that precedes all certificates */
1935f374ba41SEd Maste 		if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
1936f374ba41SEd Maste 			ret = SSH_ERR_INVALID_FORMAT;
1937f374ba41SEd Maste 			goto out;
1938f374ba41SEd Maste 		}
1939f374ba41SEd Maste 	}
1940f374ba41SEd Maste 	if ((ret = impl->funcs->deserialize_public(ktype, b, key)) != 0)
1941f374ba41SEd Maste 		goto out;
1942a0ee8cc6SDag-Erling Smørgrav 
1943a0ee8cc6SDag-Erling Smørgrav 	/* Parse certificate potion */
1944bc5531deSDag-Erling Smørgrav 	if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
1945a0ee8cc6SDag-Erling Smørgrav 		goto out;
1946a0ee8cc6SDag-Erling Smørgrav 
1947a0ee8cc6SDag-Erling Smørgrav 	if (key != NULL && sshbuf_len(b) != 0) {
1948a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_INVALID_FORMAT;
1949a0ee8cc6SDag-Erling Smørgrav 		goto out;
1950a0ee8cc6SDag-Erling Smørgrav 	}
1951a0ee8cc6SDag-Erling Smørgrav 	ret = 0;
1952076ad2f8SDag-Erling Smørgrav 	if (keyp != NULL) {
1953a0ee8cc6SDag-Erling Smørgrav 		*keyp = key;
1954a0ee8cc6SDag-Erling Smørgrav 		key = NULL;
1955076ad2f8SDag-Erling Smørgrav 	}
1956a0ee8cc6SDag-Erling Smørgrav  out:
1957bc5531deSDag-Erling Smørgrav 	sshbuf_free(copy);
1958a0ee8cc6SDag-Erling Smørgrav 	sshkey_free(key);
1959a0ee8cc6SDag-Erling Smørgrav 	free(ktype);
1960a0ee8cc6SDag-Erling Smørgrav 	return ret;
1961a0ee8cc6SDag-Erling Smørgrav }
1962a0ee8cc6SDag-Erling Smørgrav 
1963a0ee8cc6SDag-Erling Smørgrav int
sshkey_from_blob(const u_char * blob,size_t blen,struct sshkey ** keyp)1964a0ee8cc6SDag-Erling Smørgrav sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
1965a0ee8cc6SDag-Erling Smørgrav {
1966bc5531deSDag-Erling Smørgrav 	struct sshbuf *b;
1967bc5531deSDag-Erling Smørgrav 	int r;
1968bc5531deSDag-Erling Smørgrav 
1969bc5531deSDag-Erling Smørgrav 	if ((b = sshbuf_from(blob, blen)) == NULL)
1970bc5531deSDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
1971bc5531deSDag-Erling Smørgrav 	r = sshkey_from_blob_internal(b, keyp, 1);
1972bc5531deSDag-Erling Smørgrav 	sshbuf_free(b);
1973bc5531deSDag-Erling Smørgrav 	return r;
1974bc5531deSDag-Erling Smørgrav }
1975bc5531deSDag-Erling Smørgrav 
1976bc5531deSDag-Erling Smørgrav int
sshkey_fromb(struct sshbuf * b,struct sshkey ** keyp)1977bc5531deSDag-Erling Smørgrav sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
1978bc5531deSDag-Erling Smørgrav {
1979bc5531deSDag-Erling Smørgrav 	return sshkey_from_blob_internal(b, keyp, 1);
1980bc5531deSDag-Erling Smørgrav }
1981bc5531deSDag-Erling Smørgrav 
1982bc5531deSDag-Erling Smørgrav int
sshkey_froms(struct sshbuf * buf,struct sshkey ** keyp)1983bc5531deSDag-Erling Smørgrav sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
1984bc5531deSDag-Erling Smørgrav {
1985bc5531deSDag-Erling Smørgrav 	struct sshbuf *b;
1986bc5531deSDag-Erling Smørgrav 	int r;
1987bc5531deSDag-Erling Smørgrav 
1988bc5531deSDag-Erling Smørgrav 	if ((r = sshbuf_froms(buf, &b)) != 0)
1989bc5531deSDag-Erling Smørgrav 		return r;
1990bc5531deSDag-Erling Smørgrav 	r = sshkey_from_blob_internal(b, keyp, 1);
1991bc5531deSDag-Erling Smørgrav 	sshbuf_free(b);
1992bc5531deSDag-Erling Smørgrav 	return r;
1993a0ee8cc6SDag-Erling Smørgrav }
1994a0ee8cc6SDag-Erling Smørgrav 
199519261079SEd Maste int
sshkey_get_sigtype(const u_char * sig,size_t siglen,char ** sigtypep)199619261079SEd Maste sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
199747dd1d1bSDag-Erling Smørgrav {
199847dd1d1bSDag-Erling Smørgrav 	int r;
199947dd1d1bSDag-Erling Smørgrav 	struct sshbuf *b = NULL;
200047dd1d1bSDag-Erling Smørgrav 	char *sigtype = NULL;
200147dd1d1bSDag-Erling Smørgrav 
200247dd1d1bSDag-Erling Smørgrav 	if (sigtypep != NULL)
200347dd1d1bSDag-Erling Smørgrav 		*sigtypep = NULL;
200447dd1d1bSDag-Erling Smørgrav 	if ((b = sshbuf_from(sig, siglen)) == NULL)
200547dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
200647dd1d1bSDag-Erling Smørgrav 	if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
200747dd1d1bSDag-Erling Smørgrav 		goto out;
200847dd1d1bSDag-Erling Smørgrav 	/* success */
200947dd1d1bSDag-Erling Smørgrav 	if (sigtypep != NULL) {
201047dd1d1bSDag-Erling Smørgrav 		*sigtypep = sigtype;
201147dd1d1bSDag-Erling Smørgrav 		sigtype = NULL;
201247dd1d1bSDag-Erling Smørgrav 	}
201347dd1d1bSDag-Erling Smørgrav 	r = 0;
201447dd1d1bSDag-Erling Smørgrav  out:
201547dd1d1bSDag-Erling Smørgrav 	free(sigtype);
201647dd1d1bSDag-Erling Smørgrav 	sshbuf_free(b);
201747dd1d1bSDag-Erling Smørgrav 	return r;
201847dd1d1bSDag-Erling Smørgrav }
201947dd1d1bSDag-Erling Smørgrav 
2020190cef3dSDag-Erling Smørgrav /*
20212f513db7SEd Maste  *
20222f513db7SEd Maste  * Checks whether a certificate's signature type is allowed.
20232f513db7SEd Maste  * Returns 0 (success) if the certificate signature type appears in the
20242f513db7SEd Maste  * "allowed" pattern-list, or the key is not a certificate to begin with.
20252f513db7SEd Maste  * Otherwise returns a ssherr.h code.
20262f513db7SEd Maste  */
20272f513db7SEd Maste int
sshkey_check_cert_sigtype(const struct sshkey * key,const char * allowed)20282f513db7SEd Maste sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
20292f513db7SEd Maste {
20302f513db7SEd Maste 	if (key == NULL || allowed == NULL)
20312f513db7SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
20322f513db7SEd Maste 	if (!sshkey_type_is_cert(key->type))
20332f513db7SEd Maste 		return 0;
20342f513db7SEd Maste 	if (key->cert == NULL || key->cert->signature_type == NULL)
20352f513db7SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
20362f513db7SEd Maste 	if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
20372f513db7SEd Maste 		return SSH_ERR_SIGN_ALG_UNSUPPORTED;
20382f513db7SEd Maste 	return 0;
20392f513db7SEd Maste }
20402f513db7SEd Maste 
20412f513db7SEd Maste /*
2042190cef3dSDag-Erling Smørgrav  * Returns the expected signature algorithm for a given public key algorithm.
2043190cef3dSDag-Erling Smørgrav  */
2044190cef3dSDag-Erling Smørgrav const char *
sshkey_sigalg_by_name(const char * name)2045190cef3dSDag-Erling Smørgrav sshkey_sigalg_by_name(const char *name)
2046190cef3dSDag-Erling Smørgrav {
2047f374ba41SEd Maste 	const struct sshkey_impl *impl;
2048f374ba41SEd Maste 	int i;
2049190cef3dSDag-Erling Smørgrav 
2050f374ba41SEd Maste 	for (i = 0; keyimpls[i] != NULL; i++) {
2051f374ba41SEd Maste 		impl = keyimpls[i];
2052f374ba41SEd Maste 		if (strcmp(impl->name, name) != 0)
2053190cef3dSDag-Erling Smørgrav 			continue;
2054f374ba41SEd Maste 		if (impl->sigalg != NULL)
2055f374ba41SEd Maste 			return impl->sigalg;
2056f374ba41SEd Maste 		if (!impl->cert)
2057f374ba41SEd Maste 			return impl->name;
2058190cef3dSDag-Erling Smørgrav 		return sshkey_ssh_name_from_type_nid(
2059f374ba41SEd Maste 		    sshkey_type_plain(impl->type), impl->nid);
2060190cef3dSDag-Erling Smørgrav 	}
2061190cef3dSDag-Erling Smørgrav 	return NULL;
2062190cef3dSDag-Erling Smørgrav }
2063190cef3dSDag-Erling Smørgrav 
2064190cef3dSDag-Erling Smørgrav /*
2065190cef3dSDag-Erling Smørgrav  * Verifies that the signature algorithm appearing inside the signature blob
2066190cef3dSDag-Erling Smørgrav  * matches that which was requested.
2067190cef3dSDag-Erling Smørgrav  */
2068190cef3dSDag-Erling Smørgrav int
sshkey_check_sigtype(const u_char * sig,size_t siglen,const char * requested_alg)2069190cef3dSDag-Erling Smørgrav sshkey_check_sigtype(const u_char *sig, size_t siglen,
2070190cef3dSDag-Erling Smørgrav     const char *requested_alg)
2071190cef3dSDag-Erling Smørgrav {
2072190cef3dSDag-Erling Smørgrav 	const char *expected_alg;
2073190cef3dSDag-Erling Smørgrav 	char *sigtype = NULL;
2074190cef3dSDag-Erling Smørgrav 	int r;
2075190cef3dSDag-Erling Smørgrav 
2076190cef3dSDag-Erling Smørgrav 	if (requested_alg == NULL)
2077190cef3dSDag-Erling Smørgrav 		return 0;
2078190cef3dSDag-Erling Smørgrav 	if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
2079190cef3dSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
208019261079SEd Maste 	if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
2081190cef3dSDag-Erling Smørgrav 		return r;
2082190cef3dSDag-Erling Smørgrav 	r = strcmp(expected_alg, sigtype) == 0;
2083190cef3dSDag-Erling Smørgrav 	free(sigtype);
2084190cef3dSDag-Erling Smørgrav 	return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
2085190cef3dSDag-Erling Smørgrav }
2086190cef3dSDag-Erling Smørgrav 
208747dd1d1bSDag-Erling Smørgrav int
sshkey_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat)208819261079SEd Maste sshkey_sign(struct sshkey *key,
2089a0ee8cc6SDag-Erling Smørgrav     u_char **sigp, size_t *lenp,
209019261079SEd Maste     const u_char *data, size_t datalen,
209119261079SEd Maste     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
2092a0ee8cc6SDag-Erling Smørgrav {
209319261079SEd Maste 	int was_shielded = sshkey_is_shielded(key);
209419261079SEd Maste 	int r2, r = SSH_ERR_INTERNAL_ERROR;
2095f374ba41SEd Maste 	const struct sshkey_impl *impl;
209619261079SEd Maste 
2097a0ee8cc6SDag-Erling Smørgrav 	if (sigp != NULL)
2098a0ee8cc6SDag-Erling Smørgrav 		*sigp = NULL;
2099a0ee8cc6SDag-Erling Smørgrav 	if (lenp != NULL)
2100a0ee8cc6SDag-Erling Smørgrav 		*lenp = 0;
2101a0ee8cc6SDag-Erling Smørgrav 	if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2102a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
2103f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(key)) == NULL)
2104f374ba41SEd Maste 		return SSH_ERR_KEY_TYPE_UNKNOWN;
210519261079SEd Maste 	if ((r = sshkey_unshield_private(key)) != 0)
210619261079SEd Maste 		return r;
2107f374ba41SEd Maste 	if (sshkey_is_sk(key)) {
210819261079SEd Maste 		r = sshsk_sign(sk_provider, key, sigp, lenp, data,
210919261079SEd Maste 		    datalen, compat, sk_pin);
2110f374ba41SEd Maste 	} else {
2111f374ba41SEd Maste 		if (impl->funcs->sign == NULL)
2112f374ba41SEd Maste 			r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
2113f374ba41SEd Maste 		else {
2114f374ba41SEd Maste 			r = impl->funcs->sign(key, sigp, lenp, data, datalen,
2115f374ba41SEd Maste 			    alg, sk_provider, sk_pin, compat);
2116f374ba41SEd Maste 		 }
2117a0ee8cc6SDag-Erling Smørgrav 	}
211819261079SEd Maste 	if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
211919261079SEd Maste 		return r2;
212019261079SEd Maste 	return r;
2121a0ee8cc6SDag-Erling Smørgrav }
2122a0ee8cc6SDag-Erling Smørgrav 
2123a0ee8cc6SDag-Erling Smørgrav /*
2124a0ee8cc6SDag-Erling Smørgrav  * ssh_key_verify returns 0 for a correct signature  and < 0 on error.
212547dd1d1bSDag-Erling Smørgrav  * If "alg" specified, then the signature must use that algorithm.
2126a0ee8cc6SDag-Erling Smørgrav  */
2127a0ee8cc6SDag-Erling Smørgrav int
sshkey_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t dlen,const char * alg,u_int compat,struct sshkey_sig_details ** detailsp)2128a0ee8cc6SDag-Erling Smørgrav sshkey_verify(const struct sshkey *key,
2129a0ee8cc6SDag-Erling Smørgrav     const u_char *sig, size_t siglen,
213019261079SEd Maste     const u_char *data, size_t dlen, const char *alg, u_int compat,
213119261079SEd Maste     struct sshkey_sig_details **detailsp)
2132a0ee8cc6SDag-Erling Smørgrav {
2133f374ba41SEd Maste 	const struct sshkey_impl *impl;
2134f374ba41SEd Maste 
213519261079SEd Maste 	if (detailsp != NULL)
213619261079SEd Maste 		*detailsp = NULL;
2137bc5531deSDag-Erling Smørgrav 	if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
2138a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
2139f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(key)) == NULL)
2140a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_UNKNOWN;
2141f374ba41SEd Maste 	return impl->funcs->verify(key, sig, siglen, data, dlen,
2142f374ba41SEd Maste 	    alg, compat, detailsp);
2143a0ee8cc6SDag-Erling Smørgrav }
2144a0ee8cc6SDag-Erling Smørgrav 
2145a0ee8cc6SDag-Erling Smørgrav /* Convert a plain key to their _CERT equivalent */
2146a0ee8cc6SDag-Erling Smørgrav int
sshkey_to_certified(struct sshkey * k)2147eccfee6eSDag-Erling Smørgrav sshkey_to_certified(struct sshkey *k)
2148a0ee8cc6SDag-Erling Smørgrav {
2149a0ee8cc6SDag-Erling Smørgrav 	int newtype;
2150a0ee8cc6SDag-Erling Smørgrav 
2151f374ba41SEd Maste 	if ((newtype = sshkey_type_certified(k->type)) == -1)
2152a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
2153a0ee8cc6SDag-Erling Smørgrav 	if ((k->cert = cert_new()) == NULL)
2154a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
2155a0ee8cc6SDag-Erling Smørgrav 	k->type = newtype;
2156a0ee8cc6SDag-Erling Smørgrav 	return 0;
2157a0ee8cc6SDag-Erling Smørgrav }
2158a0ee8cc6SDag-Erling Smørgrav 
2159a0ee8cc6SDag-Erling Smørgrav /* Convert a certificate to its raw key equivalent */
2160a0ee8cc6SDag-Erling Smørgrav int
sshkey_drop_cert(struct sshkey * k)2161a0ee8cc6SDag-Erling Smørgrav sshkey_drop_cert(struct sshkey *k)
2162a0ee8cc6SDag-Erling Smørgrav {
2163a0ee8cc6SDag-Erling Smørgrav 	if (!sshkey_type_is_cert(k->type))
2164a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_UNKNOWN;
2165a0ee8cc6SDag-Erling Smørgrav 	cert_free(k->cert);
2166a0ee8cc6SDag-Erling Smørgrav 	k->cert = NULL;
2167a0ee8cc6SDag-Erling Smørgrav 	k->type = sshkey_type_plain(k->type);
2168a0ee8cc6SDag-Erling Smørgrav 	return 0;
2169a0ee8cc6SDag-Erling Smørgrav }
2170a0ee8cc6SDag-Erling Smørgrav 
2171a0ee8cc6SDag-Erling Smørgrav /* Sign a certified key, (re-)generating the signed certblob. */
2172a0ee8cc6SDag-Erling Smørgrav int
sshkey_certify_custom(struct sshkey * k,struct sshkey * ca,const char * alg,const char * sk_provider,const char * sk_pin,sshkey_certify_signer * signer,void * signer_ctx)21734f52dfbbSDag-Erling Smørgrav sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
217419261079SEd Maste     const char *sk_provider, const char *sk_pin,
21754f52dfbbSDag-Erling Smørgrav     sshkey_certify_signer *signer, void *signer_ctx)
2176a0ee8cc6SDag-Erling Smørgrav {
2177f374ba41SEd Maste 	const struct sshkey_impl *impl;
2178a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *principals = NULL;
2179a0ee8cc6SDag-Erling Smørgrav 	u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
2180a0ee8cc6SDag-Erling Smørgrav 	size_t i, ca_len, sig_len;
2181a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_INTERNAL_ERROR;
21822f513db7SEd Maste 	struct sshbuf *cert = NULL;
21832f513db7SEd Maste 	char *sigtype = NULL;
2184a0ee8cc6SDag-Erling Smørgrav 
2185a0ee8cc6SDag-Erling Smørgrav 	if (k == NULL || k->cert == NULL ||
2186a0ee8cc6SDag-Erling Smørgrav 	    k->cert->certblob == NULL || ca == NULL)
2187a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
2188a0ee8cc6SDag-Erling Smørgrav 	if (!sshkey_is_cert(k))
2189a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_UNKNOWN;
2190a0ee8cc6SDag-Erling Smørgrav 	if (!sshkey_type_is_valid_ca(ca->type))
2191a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2192f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(k)) == NULL)
2193f374ba41SEd Maste 		return SSH_ERR_INTERNAL_ERROR;
2194a0ee8cc6SDag-Erling Smørgrav 
21952f513db7SEd Maste 	/*
21962f513db7SEd Maste 	 * If no alg specified as argument but a signature_type was set,
21972f513db7SEd Maste 	 * then prefer that. If both were specified, then they must match.
21982f513db7SEd Maste 	 */
21992f513db7SEd Maste 	if (alg == NULL)
22002f513db7SEd Maste 		alg = k->cert->signature_type;
22012f513db7SEd Maste 	else if (k->cert->signature_type != NULL &&
22022f513db7SEd Maste 	    strcmp(alg, k->cert->signature_type) != 0)
22032f513db7SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
22042f513db7SEd Maste 
220519261079SEd Maste 	/*
220619261079SEd Maste 	 * If no signing algorithm or signature_type was specified and we're
220719261079SEd Maste 	 * using a RSA key, then default to a good signature algorithm.
220819261079SEd Maste 	 */
220919261079SEd Maste 	if (alg == NULL && ca->type == KEY_RSA)
221019261079SEd Maste 		alg = "rsa-sha2-512";
221119261079SEd Maste 
2212a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
2213a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
2214a0ee8cc6SDag-Erling Smørgrav 
2215a0ee8cc6SDag-Erling Smørgrav 	cert = k->cert->certblob; /* for readability */
2216a0ee8cc6SDag-Erling Smørgrav 	sshbuf_reset(cert);
2217a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
2218a0ee8cc6SDag-Erling Smørgrav 		goto out;
2219a0ee8cc6SDag-Erling Smørgrav 
2220a0ee8cc6SDag-Erling Smørgrav 	/* -v01 certs put nonce first */
2221a0ee8cc6SDag-Erling Smørgrav 	arc4random_buf(&nonce, sizeof(nonce));
2222a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
2223a0ee8cc6SDag-Erling Smørgrav 		goto out;
2224a0ee8cc6SDag-Erling Smørgrav 
2225f374ba41SEd Maste 	/* Public key next */
2226f374ba41SEd Maste 	if ((ret = impl->funcs->serialize_public(k, cert,
2227f374ba41SEd Maste 	    SSHKEY_SERIALIZE_DEFAULT)) != 0)
2228a0ee8cc6SDag-Erling Smørgrav 		goto out;
2229a0ee8cc6SDag-Erling Smørgrav 
2230f374ba41SEd Maste 	/* Then remaining cert fields */
2231eccfee6eSDag-Erling Smørgrav 	if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
2232eccfee6eSDag-Erling Smørgrav 	    (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
2233a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
2234a0ee8cc6SDag-Erling Smørgrav 		goto out;
2235a0ee8cc6SDag-Erling Smørgrav 
2236a0ee8cc6SDag-Erling Smørgrav 	if ((principals = sshbuf_new()) == NULL) {
2237a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_ALLOC_FAIL;
2238a0ee8cc6SDag-Erling Smørgrav 		goto out;
2239a0ee8cc6SDag-Erling Smørgrav 	}
2240a0ee8cc6SDag-Erling Smørgrav 	for (i = 0; i < k->cert->nprincipals; i++) {
2241a0ee8cc6SDag-Erling Smørgrav 		if ((ret = sshbuf_put_cstring(principals,
2242a0ee8cc6SDag-Erling Smørgrav 		    k->cert->principals[i])) != 0)
2243a0ee8cc6SDag-Erling Smørgrav 			goto out;
2244a0ee8cc6SDag-Erling Smørgrav 	}
2245a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
2246a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
2247a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
2248eccfee6eSDag-Erling Smørgrav 	    (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
2249eccfee6eSDag-Erling Smørgrav 	    (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
2250eccfee6eSDag-Erling Smørgrav 	    (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
2251a0ee8cc6SDag-Erling Smørgrav 	    (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
2252a0ee8cc6SDag-Erling Smørgrav 		goto out;
2253a0ee8cc6SDag-Erling Smørgrav 
2254a0ee8cc6SDag-Erling Smørgrav 	/* Sign the whole mess */
22554f52dfbbSDag-Erling Smørgrav 	if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
225619261079SEd Maste 	    sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
2257a0ee8cc6SDag-Erling Smørgrav 		goto out;
22582f513db7SEd Maste 	/* Check and update signature_type against what was actually used */
225919261079SEd Maste 	if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
22602f513db7SEd Maste 		goto out;
22612f513db7SEd Maste 	if (alg != NULL && strcmp(alg, sigtype) != 0) {
22622f513db7SEd Maste 		ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
22632f513db7SEd Maste 		goto out;
22642f513db7SEd Maste 	}
22652f513db7SEd Maste 	if (k->cert->signature_type == NULL) {
22662f513db7SEd Maste 		k->cert->signature_type = sigtype;
22672f513db7SEd Maste 		sigtype = NULL;
22682f513db7SEd Maste 	}
2269a0ee8cc6SDag-Erling Smørgrav 	/* Append signature and we are done */
2270a0ee8cc6SDag-Erling Smørgrav 	if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
2271a0ee8cc6SDag-Erling Smørgrav 		goto out;
2272a0ee8cc6SDag-Erling Smørgrav 	ret = 0;
2273a0ee8cc6SDag-Erling Smørgrav  out:
2274a0ee8cc6SDag-Erling Smørgrav 	if (ret != 0)
2275a0ee8cc6SDag-Erling Smørgrav 		sshbuf_reset(cert);
2276a0ee8cc6SDag-Erling Smørgrav 	free(sig_blob);
2277a0ee8cc6SDag-Erling Smørgrav 	free(ca_blob);
22782f513db7SEd Maste 	free(sigtype);
2279a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(principals);
2280a0ee8cc6SDag-Erling Smørgrav 	return ret;
2281a0ee8cc6SDag-Erling Smørgrav }
2282a0ee8cc6SDag-Erling Smørgrav 
22834f52dfbbSDag-Erling Smørgrav static int
default_key_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat,void * ctx)228419261079SEd Maste default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
22854f52dfbbSDag-Erling Smørgrav     const u_char *data, size_t datalen,
228619261079SEd Maste     const char *alg, const char *sk_provider, const char *sk_pin,
228719261079SEd Maste     u_int compat, void *ctx)
22884f52dfbbSDag-Erling Smørgrav {
22894f52dfbbSDag-Erling Smørgrav 	if (ctx != NULL)
22904f52dfbbSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
229119261079SEd Maste 	return sshkey_sign(key, sigp, lenp, data, datalen, alg,
229219261079SEd Maste 	    sk_provider, sk_pin, compat);
22934f52dfbbSDag-Erling Smørgrav }
22944f52dfbbSDag-Erling Smørgrav 
22954f52dfbbSDag-Erling Smørgrav int
sshkey_certify(struct sshkey * k,struct sshkey * ca,const char * alg,const char * sk_provider,const char * sk_pin)229619261079SEd Maste sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
229719261079SEd Maste     const char *sk_provider, const char *sk_pin)
22984f52dfbbSDag-Erling Smørgrav {
229919261079SEd Maste 	return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
230019261079SEd Maste 	    default_key_sign, NULL);
23014f52dfbbSDag-Erling Smørgrav }
23024f52dfbbSDag-Erling Smørgrav 
2303a0ee8cc6SDag-Erling Smørgrav int
sshkey_cert_check_authority(const struct sshkey * k,int want_host,int require_principal,int wildcard_pattern,uint64_t verify_time,const char * name,const char ** reason)2304a0ee8cc6SDag-Erling Smørgrav sshkey_cert_check_authority(const struct sshkey *k,
230519261079SEd Maste     int want_host, int require_principal, int wildcard_pattern,
230619261079SEd Maste     uint64_t verify_time, const char *name, const char **reason)
2307a0ee8cc6SDag-Erling Smørgrav {
2308a0ee8cc6SDag-Erling Smørgrav 	u_int i, principal_matches;
2309a0ee8cc6SDag-Erling Smørgrav 
231019261079SEd Maste 	if (reason == NULL)
231119261079SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
231219261079SEd Maste 	if (!sshkey_is_cert(k)) {
231319261079SEd Maste 		*reason = "Key is not a certificate";
231419261079SEd Maste 		return SSH_ERR_KEY_CERT_INVALID;
231519261079SEd Maste 	}
2316a0ee8cc6SDag-Erling Smørgrav 	if (want_host) {
2317a0ee8cc6SDag-Erling Smørgrav 		if (k->cert->type != SSH2_CERT_TYPE_HOST) {
2318a0ee8cc6SDag-Erling Smørgrav 			*reason = "Certificate invalid: not a host certificate";
2319a0ee8cc6SDag-Erling Smørgrav 			return SSH_ERR_KEY_CERT_INVALID;
2320a0ee8cc6SDag-Erling Smørgrav 		}
2321a0ee8cc6SDag-Erling Smørgrav 	} else {
2322a0ee8cc6SDag-Erling Smørgrav 		if (k->cert->type != SSH2_CERT_TYPE_USER) {
2323a0ee8cc6SDag-Erling Smørgrav 			*reason = "Certificate invalid: not a user certificate";
2324a0ee8cc6SDag-Erling Smørgrav 			return SSH_ERR_KEY_CERT_INVALID;
2325a0ee8cc6SDag-Erling Smørgrav 		}
2326a0ee8cc6SDag-Erling Smørgrav 	}
232719261079SEd Maste 	if (verify_time < k->cert->valid_after) {
2328a0ee8cc6SDag-Erling Smørgrav 		*reason = "Certificate invalid: not yet valid";
2329a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_CERT_INVALID;
2330a0ee8cc6SDag-Erling Smørgrav 	}
233119261079SEd Maste 	if (verify_time >= k->cert->valid_before) {
2332a0ee8cc6SDag-Erling Smørgrav 		*reason = "Certificate invalid: expired";
2333a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_CERT_INVALID;
2334a0ee8cc6SDag-Erling Smørgrav 	}
2335a0ee8cc6SDag-Erling Smørgrav 	if (k->cert->nprincipals == 0) {
2336a0ee8cc6SDag-Erling Smørgrav 		if (require_principal) {
2337a0ee8cc6SDag-Erling Smørgrav 			*reason = "Certificate lacks principal list";
2338a0ee8cc6SDag-Erling Smørgrav 			return SSH_ERR_KEY_CERT_INVALID;
2339a0ee8cc6SDag-Erling Smørgrav 		}
2340a0ee8cc6SDag-Erling Smørgrav 	} else if (name != NULL) {
2341a0ee8cc6SDag-Erling Smørgrav 		principal_matches = 0;
2342a0ee8cc6SDag-Erling Smørgrav 		for (i = 0; i < k->cert->nprincipals; i++) {
234319261079SEd Maste 			if (wildcard_pattern) {
234419261079SEd Maste 				if (match_pattern(k->cert->principals[i],
234519261079SEd Maste 				    name)) {
234619261079SEd Maste 					principal_matches = 1;
234719261079SEd Maste 					break;
234819261079SEd Maste 				}
234919261079SEd Maste 			} else if (strcmp(name, k->cert->principals[i]) == 0) {
2350a0ee8cc6SDag-Erling Smørgrav 				principal_matches = 1;
2351a0ee8cc6SDag-Erling Smørgrav 				break;
2352a0ee8cc6SDag-Erling Smørgrav 			}
2353a0ee8cc6SDag-Erling Smørgrav 		}
2354a0ee8cc6SDag-Erling Smørgrav 		if (!principal_matches) {
2355a0ee8cc6SDag-Erling Smørgrav 			*reason = "Certificate invalid: name is not a listed "
2356a0ee8cc6SDag-Erling Smørgrav 			    "principal";
2357a0ee8cc6SDag-Erling Smørgrav 			return SSH_ERR_KEY_CERT_INVALID;
2358a0ee8cc6SDag-Erling Smørgrav 		}
2359a0ee8cc6SDag-Erling Smørgrav 	}
2360a0ee8cc6SDag-Erling Smørgrav 	return 0;
2361a0ee8cc6SDag-Erling Smørgrav }
2362a0ee8cc6SDag-Erling Smørgrav 
236319261079SEd Maste int
sshkey_cert_check_authority_now(const struct sshkey * k,int want_host,int require_principal,int wildcard_pattern,const char * name,const char ** reason)236419261079SEd Maste sshkey_cert_check_authority_now(const struct sshkey *k,
236519261079SEd Maste     int want_host, int require_principal, int wildcard_pattern,
236619261079SEd Maste     const char *name, const char **reason)
236719261079SEd Maste {
236819261079SEd Maste 	time_t now;
236919261079SEd Maste 
237019261079SEd Maste 	if ((now = time(NULL)) < 0) {
237119261079SEd Maste 		/* yikes - system clock before epoch! */
237219261079SEd Maste 		*reason = "Certificate invalid: not yet valid";
237319261079SEd Maste 		return SSH_ERR_KEY_CERT_INVALID;
237419261079SEd Maste 	}
237519261079SEd Maste 	return sshkey_cert_check_authority(k, want_host, require_principal,
237619261079SEd Maste 	    wildcard_pattern, (uint64_t)now, name, reason);
237719261079SEd Maste }
237819261079SEd Maste 
237919261079SEd Maste int
sshkey_cert_check_host(const struct sshkey * key,const char * host,int wildcard_principals,const char * ca_sign_algorithms,const char ** reason)238019261079SEd Maste sshkey_cert_check_host(const struct sshkey *key, const char *host,
238119261079SEd Maste     int wildcard_principals, const char *ca_sign_algorithms,
238219261079SEd Maste     const char **reason)
238319261079SEd Maste {
238419261079SEd Maste 	int r;
238519261079SEd Maste 
238619261079SEd Maste 	if ((r = sshkey_cert_check_authority_now(key, 1, 0, wildcard_principals,
238719261079SEd Maste 	    host, reason)) != 0)
238819261079SEd Maste 		return r;
238919261079SEd Maste 	if (sshbuf_len(key->cert->critical) != 0) {
239019261079SEd Maste 		*reason = "Certificate contains unsupported critical options";
239119261079SEd Maste 		return SSH_ERR_KEY_CERT_INVALID;
239219261079SEd Maste 	}
239319261079SEd Maste 	if (ca_sign_algorithms != NULL &&
239419261079SEd Maste 	    (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
239519261079SEd Maste 		*reason = "Certificate signed with disallowed algorithm";
239619261079SEd Maste 		return SSH_ERR_KEY_CERT_INVALID;
239719261079SEd Maste 	}
239819261079SEd Maste 	return 0;
239919261079SEd Maste }
240019261079SEd Maste 
2401acc1a9efSDag-Erling Smørgrav size_t
sshkey_format_cert_validity(const struct sshkey_cert * cert,char * s,size_t l)2402acc1a9efSDag-Erling Smørgrav sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
2403acc1a9efSDag-Erling Smørgrav {
240419261079SEd Maste 	char from[32], to[32], ret[128];
2405acc1a9efSDag-Erling Smørgrav 
2406acc1a9efSDag-Erling Smørgrav 	*from = *to = '\0';
2407acc1a9efSDag-Erling Smørgrav 	if (cert->valid_after == 0 &&
2408acc1a9efSDag-Erling Smørgrav 	    cert->valid_before == 0xffffffffffffffffULL)
2409acc1a9efSDag-Erling Smørgrav 		return strlcpy(s, "forever", l);
2410acc1a9efSDag-Erling Smørgrav 
241119261079SEd Maste 	if (cert->valid_after != 0)
241219261079SEd Maste 		format_absolute_time(cert->valid_after, from, sizeof(from));
241319261079SEd Maste 	if (cert->valid_before != 0xffffffffffffffffULL)
241419261079SEd Maste 		format_absolute_time(cert->valid_before, to, sizeof(to));
2415acc1a9efSDag-Erling Smørgrav 
2416acc1a9efSDag-Erling Smørgrav 	if (cert->valid_after == 0)
2417acc1a9efSDag-Erling Smørgrav 		snprintf(ret, sizeof(ret), "before %s", to);
2418acc1a9efSDag-Erling Smørgrav 	else if (cert->valid_before == 0xffffffffffffffffULL)
2419acc1a9efSDag-Erling Smørgrav 		snprintf(ret, sizeof(ret), "after %s", from);
2420acc1a9efSDag-Erling Smørgrav 	else
2421acc1a9efSDag-Erling Smørgrav 		snprintf(ret, sizeof(ret), "from %s to %s", from, to);
2422acc1a9efSDag-Erling Smørgrav 
2423acc1a9efSDag-Erling Smørgrav 	return strlcpy(s, ret, l);
2424acc1a9efSDag-Erling Smørgrav }
2425acc1a9efSDag-Erling Smørgrav 
2426f374ba41SEd Maste /* Common serialization for FIDO private keys */
2427f374ba41SEd Maste int
sshkey_serialize_private_sk(const struct sshkey * key,struct sshbuf * b)2428f374ba41SEd Maste sshkey_serialize_private_sk(const struct sshkey *key, struct sshbuf *b)
2429f374ba41SEd Maste {
2430f374ba41SEd Maste 	int r;
2431f374ba41SEd Maste 
2432f374ba41SEd Maste 	if ((r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
2433f374ba41SEd Maste 	    (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
2434f374ba41SEd Maste 	    (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
2435f374ba41SEd Maste 	    (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
2436f374ba41SEd Maste 		return r;
2437f374ba41SEd Maste 
2438f374ba41SEd Maste 	return 0;
2439f374ba41SEd Maste }
2440f374ba41SEd Maste 
2441a0ee8cc6SDag-Erling Smørgrav int
sshkey_private_serialize_opt(struct sshkey * key,struct sshbuf * buf,enum sshkey_serialize_rep opts)244219261079SEd Maste sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
244347dd1d1bSDag-Erling Smørgrav     enum sshkey_serialize_rep opts)
2444a0ee8cc6SDag-Erling Smørgrav {
2445a0ee8cc6SDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
244619261079SEd Maste 	int was_shielded = sshkey_is_shielded(key);
244719261079SEd Maste 	struct sshbuf *b = NULL;
2448f374ba41SEd Maste 	const struct sshkey_impl *impl;
2449a0ee8cc6SDag-Erling Smørgrav 
2450f374ba41SEd Maste 	if ((impl = sshkey_impl_from_key(key)) == NULL)
2451f374ba41SEd Maste 		return SSH_ERR_INTERNAL_ERROR;
245219261079SEd Maste 	if ((r = sshkey_unshield_private(key)) != 0)
245319261079SEd Maste 		return r;
245419261079SEd Maste 	if ((b = sshbuf_new()) == NULL)
245519261079SEd Maste 		return SSH_ERR_ALLOC_FAIL;
2456a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
2457a0ee8cc6SDag-Erling Smørgrav 		goto out;
2458f374ba41SEd Maste 	if (sshkey_is_cert(key)) {
2459f374ba41SEd Maste 		if (key->cert == NULL ||
2460f374ba41SEd Maste 		    sshbuf_len(key->cert->certblob) == 0) {
2461a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_INVALID_ARGUMENT;
2462a0ee8cc6SDag-Erling Smørgrav 			goto out;
2463a0ee8cc6SDag-Erling Smørgrav 		}
2464f374ba41SEd Maste 		if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0)
2465a0ee8cc6SDag-Erling Smørgrav 			goto out;
2466a0ee8cc6SDag-Erling Smørgrav 	}
2467f374ba41SEd Maste 	if ((r = impl->funcs->serialize_private(key, b, opts)) != 0)
2468a0ee8cc6SDag-Erling Smørgrav 		goto out;
2469f374ba41SEd Maste 
247019261079SEd Maste 	/*
247119261079SEd Maste 	 * success (but we still need to append the output to buf after
247219261079SEd Maste 	 * possibly re-shielding the private key)
247319261079SEd Maste 	 */
2474a0ee8cc6SDag-Erling Smørgrav 	r = 0;
2475a0ee8cc6SDag-Erling Smørgrav  out:
247619261079SEd Maste 	if (was_shielded)
247719261079SEd Maste 		r = sshkey_shield_private(key);
247819261079SEd Maste 	if (r == 0)
247919261079SEd Maste 		r = sshbuf_putb(buf, b);
248019261079SEd Maste 	sshbuf_free(b);
248119261079SEd Maste 
2482a0ee8cc6SDag-Erling Smørgrav 	return r;
2483a0ee8cc6SDag-Erling Smørgrav }
2484a0ee8cc6SDag-Erling Smørgrav 
2485a0ee8cc6SDag-Erling Smørgrav int
sshkey_private_serialize(struct sshkey * key,struct sshbuf * b)248619261079SEd Maste sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
248747dd1d1bSDag-Erling Smørgrav {
248847dd1d1bSDag-Erling Smørgrav 	return sshkey_private_serialize_opt(key, b,
248947dd1d1bSDag-Erling Smørgrav 	    SSHKEY_SERIALIZE_DEFAULT);
249047dd1d1bSDag-Erling Smørgrav }
249147dd1d1bSDag-Erling Smørgrav 
2492f374ba41SEd Maste /* Shared deserialization of FIDO private key components */
2493f374ba41SEd Maste int
sshkey_private_deserialize_sk(struct sshbuf * buf,struct sshkey * k)2494f374ba41SEd Maste sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k)
2495f374ba41SEd Maste {
2496f374ba41SEd Maste 	int r;
2497f374ba41SEd Maste 
2498f374ba41SEd Maste 	if ((k->sk_key_handle = sshbuf_new()) == NULL ||
2499f374ba41SEd Maste 	    (k->sk_reserved = sshbuf_new()) == NULL)
2500f374ba41SEd Maste 		return SSH_ERR_ALLOC_FAIL;
2501f374ba41SEd Maste 	if ((r = sshbuf_get_cstring(buf, &k->sk_application, NULL)) != 0 ||
2502f374ba41SEd Maste 	    (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
2503f374ba41SEd Maste 	    (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
2504f374ba41SEd Maste 	    (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
2505f374ba41SEd Maste 		return r;
2506f374ba41SEd Maste 
2507f374ba41SEd Maste 	return 0;
2508f374ba41SEd Maste }
2509f374ba41SEd Maste 
251047dd1d1bSDag-Erling Smørgrav int
sshkey_private_deserialize(struct sshbuf * buf,struct sshkey ** kp)2511a0ee8cc6SDag-Erling Smørgrav sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
2512a0ee8cc6SDag-Erling Smørgrav {
2513f374ba41SEd Maste 	const struct sshkey_impl *impl;
2514f374ba41SEd Maste 	char *tname = NULL;
251519261079SEd Maste 	char *expect_sk_application = NULL;
251619261079SEd Maste 	u_char *expect_ed25519_pk = NULL;
2517f374ba41SEd Maste 	struct sshkey *k = NULL;
2518f374ba41SEd Maste 	int type, r = SSH_ERR_INTERNAL_ERROR;
2519a0ee8cc6SDag-Erling Smørgrav 
2520a0ee8cc6SDag-Erling Smørgrav 	if (kp != NULL)
2521a0ee8cc6SDag-Erling Smørgrav 		*kp = NULL;
2522a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
2523a0ee8cc6SDag-Erling Smørgrav 		goto out;
2524a0ee8cc6SDag-Erling Smørgrav 	type = sshkey_type_from_name(tname);
252519261079SEd Maste 	if (sshkey_type_is_cert(type)) {
252619261079SEd Maste 		/*
252719261079SEd Maste 		 * Certificate key private keys begin with the certificate
252819261079SEd Maste 		 * itself. Make sure this matches the type of the enclosing
252919261079SEd Maste 		 * private key.
253019261079SEd Maste 		 */
253119261079SEd Maste 		if ((r = sshkey_froms(buf, &k)) != 0)
253219261079SEd Maste 			goto out;
253319261079SEd Maste 		if (k->type != type) {
253419261079SEd Maste 			r = SSH_ERR_KEY_CERT_MISMATCH;
253519261079SEd Maste 			goto out;
253619261079SEd Maste 		}
253719261079SEd Maste 		/* For ECDSA keys, the group must match too */
253819261079SEd Maste 		if (k->type == KEY_ECDSA &&
253919261079SEd Maste 		    k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
254019261079SEd Maste 			r = SSH_ERR_KEY_CERT_MISMATCH;
254119261079SEd Maste 			goto out;
254219261079SEd Maste 		}
254319261079SEd Maste 		/*
254419261079SEd Maste 		 * Several fields are redundant between certificate and
254519261079SEd Maste 		 * private key body, we require these to match.
254619261079SEd Maste 		 */
254719261079SEd Maste 		expect_sk_application = k->sk_application;
254819261079SEd Maste 		expect_ed25519_pk = k->ed25519_pk;
254919261079SEd Maste 		k->sk_application = NULL;
255019261079SEd Maste 		k->ed25519_pk = NULL;
2551f374ba41SEd Maste 		/* XXX xmss too or refactor */
255219261079SEd Maste 	} else {
25532f513db7SEd Maste 		if ((k = sshkey_new(type)) == NULL) {
2554a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_ALLOC_FAIL;
2555a0ee8cc6SDag-Erling Smørgrav 			goto out;
2556a0ee8cc6SDag-Erling Smørgrav 		}
25572a01feabSEd Maste 	}
2558f374ba41SEd Maste 	if ((impl = sshkey_impl_from_type(type)) == NULL) {
2559f374ba41SEd Maste 		r = SSH_ERR_INTERNAL_ERROR;
25602a01feabSEd Maste 		goto out;
25612a01feabSEd Maste 	}
2562f374ba41SEd Maste 	if ((r = impl->funcs->deserialize_private(tname, buf, k)) != 0)
25632a01feabSEd Maste 		goto out;
2564f374ba41SEd Maste 
2565f374ba41SEd Maste 	/* XXX xmss too or refactor */
256619261079SEd Maste 	if ((expect_sk_application != NULL && (k->sk_application == NULL ||
256719261079SEd Maste 	    strcmp(expect_sk_application, k->sk_application) != 0)) ||
256819261079SEd Maste 	    (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
256919261079SEd Maste 	    memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
257019261079SEd Maste 		r = SSH_ERR_KEY_CERT_MISMATCH;
257119261079SEd Maste 		goto out;
257219261079SEd Maste 	}
2573a0ee8cc6SDag-Erling Smørgrav 	/* success */
2574a0ee8cc6SDag-Erling Smørgrav 	r = 0;
2575a0ee8cc6SDag-Erling Smørgrav 	if (kp != NULL) {
2576a0ee8cc6SDag-Erling Smørgrav 		*kp = k;
2577a0ee8cc6SDag-Erling Smørgrav 		k = NULL;
2578a0ee8cc6SDag-Erling Smørgrav 	}
2579a0ee8cc6SDag-Erling Smørgrav  out:
2580a0ee8cc6SDag-Erling Smørgrav 	free(tname);
2581a0ee8cc6SDag-Erling Smørgrav 	sshkey_free(k);
258219261079SEd Maste 	free(expect_sk_application);
258319261079SEd Maste 	free(expect_ed25519_pk);
2584a0ee8cc6SDag-Erling Smørgrav 	return r;
2585a0ee8cc6SDag-Erling Smørgrav }
2586a0ee8cc6SDag-Erling Smørgrav 
2587a0ee8cc6SDag-Erling Smørgrav #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
2588a0ee8cc6SDag-Erling Smørgrav int
sshkey_ec_validate_public(const EC_GROUP * group,const EC_POINT * public)2589a0ee8cc6SDag-Erling Smørgrav sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
2590a0ee8cc6SDag-Erling Smørgrav {
2591a0ee8cc6SDag-Erling Smørgrav 	EC_POINT *nq = NULL;
259219261079SEd Maste 	BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
2593a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2594a0ee8cc6SDag-Erling Smørgrav 
2595ca86bcf2SDag-Erling Smørgrav 	/*
2596ca86bcf2SDag-Erling Smørgrav 	 * NB. This assumes OpenSSL has already verified that the public
2597ca86bcf2SDag-Erling Smørgrav 	 * point lies on the curve. This is done by EC_POINT_oct2point()
2598ca86bcf2SDag-Erling Smørgrav 	 * implicitly calling EC_POINT_is_on_curve(). If this code is ever
2599ca86bcf2SDag-Erling Smørgrav 	 * reachable with public points not unmarshalled using
2600ca86bcf2SDag-Erling Smørgrav 	 * EC_POINT_oct2point then the caller will need to explicitly check.
2601ca86bcf2SDag-Erling Smørgrav 	 */
2602ca86bcf2SDag-Erling Smørgrav 
2603a0ee8cc6SDag-Erling Smørgrav 	/*
2604a0ee8cc6SDag-Erling Smørgrav 	 * We shouldn't ever hit this case because bignum_get_ecpoint()
2605a0ee8cc6SDag-Erling Smørgrav 	 * refuses to load GF2m points.
2606a0ee8cc6SDag-Erling Smørgrav 	 */
2607a0ee8cc6SDag-Erling Smørgrav 	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2608a0ee8cc6SDag-Erling Smørgrav 	    NID_X9_62_prime_field)
2609a0ee8cc6SDag-Erling Smørgrav 		goto out;
2610a0ee8cc6SDag-Erling Smørgrav 
2611a0ee8cc6SDag-Erling Smørgrav 	/* Q != infinity */
2612a0ee8cc6SDag-Erling Smørgrav 	if (EC_POINT_is_at_infinity(group, public))
2613a0ee8cc6SDag-Erling Smørgrav 		goto out;
2614a0ee8cc6SDag-Erling Smørgrav 
261519261079SEd Maste 	if ((x = BN_new()) == NULL ||
261619261079SEd Maste 	    (y = BN_new()) == NULL ||
261719261079SEd Maste 	    (order = BN_new()) == NULL ||
261819261079SEd Maste 	    (tmp = BN_new()) == NULL) {
2619a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_ALLOC_FAIL;
2620a0ee8cc6SDag-Erling Smørgrav 		goto out;
2621a0ee8cc6SDag-Erling Smørgrav 	}
2622a0ee8cc6SDag-Erling Smørgrav 
2623a0ee8cc6SDag-Erling Smørgrav 	/* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
262419261079SEd Maste 	if (EC_GROUP_get_order(group, order, NULL) != 1 ||
2625a0ee8cc6SDag-Erling Smørgrav 	    EC_POINT_get_affine_coordinates_GFp(group, public,
262619261079SEd Maste 	    x, y, NULL) != 1) {
2627a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_LIBCRYPTO_ERROR;
2628a0ee8cc6SDag-Erling Smørgrav 		goto out;
2629a0ee8cc6SDag-Erling Smørgrav 	}
2630a0ee8cc6SDag-Erling Smørgrav 	if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
2631a0ee8cc6SDag-Erling Smørgrav 	    BN_num_bits(y) <= BN_num_bits(order) / 2)
2632a0ee8cc6SDag-Erling Smørgrav 		goto out;
2633a0ee8cc6SDag-Erling Smørgrav 
2634a0ee8cc6SDag-Erling Smørgrav 	/* nQ == infinity (n == order of subgroup) */
2635a0ee8cc6SDag-Erling Smørgrav 	if ((nq = EC_POINT_new(group)) == NULL) {
2636a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_ALLOC_FAIL;
2637a0ee8cc6SDag-Erling Smørgrav 		goto out;
2638a0ee8cc6SDag-Erling Smørgrav 	}
263919261079SEd Maste 	if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
2640a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_LIBCRYPTO_ERROR;
2641a0ee8cc6SDag-Erling Smørgrav 		goto out;
2642a0ee8cc6SDag-Erling Smørgrav 	}
2643a0ee8cc6SDag-Erling Smørgrav 	if (EC_POINT_is_at_infinity(group, nq) != 1)
2644a0ee8cc6SDag-Erling Smørgrav 		goto out;
2645a0ee8cc6SDag-Erling Smørgrav 
2646a0ee8cc6SDag-Erling Smørgrav 	/* x < order - 1, y < order - 1 */
2647a0ee8cc6SDag-Erling Smørgrav 	if (!BN_sub(tmp, order, BN_value_one())) {
2648a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_LIBCRYPTO_ERROR;
2649a0ee8cc6SDag-Erling Smørgrav 		goto out;
2650a0ee8cc6SDag-Erling Smørgrav 	}
2651a0ee8cc6SDag-Erling Smørgrav 	if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
2652a0ee8cc6SDag-Erling Smørgrav 		goto out;
2653a0ee8cc6SDag-Erling Smørgrav 	ret = 0;
2654a0ee8cc6SDag-Erling Smørgrav  out:
265519261079SEd Maste 	BN_clear_free(x);
265619261079SEd Maste 	BN_clear_free(y);
265719261079SEd Maste 	BN_clear_free(order);
265819261079SEd Maste 	BN_clear_free(tmp);
2659a0ee8cc6SDag-Erling Smørgrav 	EC_POINT_free(nq);
2660a0ee8cc6SDag-Erling Smørgrav 	return ret;
2661a0ee8cc6SDag-Erling Smørgrav }
2662a0ee8cc6SDag-Erling Smørgrav 
2663a0ee8cc6SDag-Erling Smørgrav int
sshkey_ec_validate_private(const EC_KEY * key)2664a0ee8cc6SDag-Erling Smørgrav sshkey_ec_validate_private(const EC_KEY *key)
2665a0ee8cc6SDag-Erling Smørgrav {
266619261079SEd Maste 	BIGNUM *order = NULL, *tmp = NULL;
2667a0ee8cc6SDag-Erling Smørgrav 	int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
2668a0ee8cc6SDag-Erling Smørgrav 
266919261079SEd Maste 	if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
2670a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_ALLOC_FAIL;
2671a0ee8cc6SDag-Erling Smørgrav 		goto out;
2672a0ee8cc6SDag-Erling Smørgrav 	}
2673a0ee8cc6SDag-Erling Smørgrav 
2674a0ee8cc6SDag-Erling Smørgrav 	/* log2(private) > log2(order)/2 */
267519261079SEd Maste 	if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
2676a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_LIBCRYPTO_ERROR;
2677a0ee8cc6SDag-Erling Smørgrav 		goto out;
2678a0ee8cc6SDag-Erling Smørgrav 	}
2679a0ee8cc6SDag-Erling Smørgrav 	if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
2680a0ee8cc6SDag-Erling Smørgrav 	    BN_num_bits(order) / 2)
2681a0ee8cc6SDag-Erling Smørgrav 		goto out;
2682a0ee8cc6SDag-Erling Smørgrav 
2683a0ee8cc6SDag-Erling Smørgrav 	/* private < order - 1 */
2684a0ee8cc6SDag-Erling Smørgrav 	if (!BN_sub(tmp, order, BN_value_one())) {
2685a0ee8cc6SDag-Erling Smørgrav 		ret = SSH_ERR_LIBCRYPTO_ERROR;
2686a0ee8cc6SDag-Erling Smørgrav 		goto out;
2687a0ee8cc6SDag-Erling Smørgrav 	}
2688a0ee8cc6SDag-Erling Smørgrav 	if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
2689a0ee8cc6SDag-Erling Smørgrav 		goto out;
2690a0ee8cc6SDag-Erling Smørgrav 	ret = 0;
2691a0ee8cc6SDag-Erling Smørgrav  out:
269219261079SEd Maste 	BN_clear_free(order);
269319261079SEd Maste 	BN_clear_free(tmp);
2694a0ee8cc6SDag-Erling Smørgrav 	return ret;
2695a0ee8cc6SDag-Erling Smørgrav }
2696a0ee8cc6SDag-Erling Smørgrav 
2697a0ee8cc6SDag-Erling Smørgrav void
sshkey_dump_ec_point(const EC_GROUP * group,const EC_POINT * point)2698a0ee8cc6SDag-Erling Smørgrav sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
2699a0ee8cc6SDag-Erling Smørgrav {
270019261079SEd Maste 	BIGNUM *x = NULL, *y = NULL;
2701a0ee8cc6SDag-Erling Smørgrav 
2702a0ee8cc6SDag-Erling Smørgrav 	if (point == NULL) {
2703a0ee8cc6SDag-Erling Smørgrav 		fputs("point=(NULL)\n", stderr);
2704a0ee8cc6SDag-Erling Smørgrav 		return;
2705a0ee8cc6SDag-Erling Smørgrav 	}
270619261079SEd Maste 	if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
270719261079SEd Maste 		fprintf(stderr, "%s: BN_new failed\n", __func__);
270819261079SEd Maste 		goto out;
2709a0ee8cc6SDag-Erling Smørgrav 	}
2710a0ee8cc6SDag-Erling Smørgrav 	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
2711a0ee8cc6SDag-Erling Smørgrav 	    NID_X9_62_prime_field) {
2712a0ee8cc6SDag-Erling Smørgrav 		fprintf(stderr, "%s: group is not a prime field\n", __func__);
271319261079SEd Maste 		goto out;
2714a0ee8cc6SDag-Erling Smørgrav 	}
271519261079SEd Maste 	if (EC_POINT_get_affine_coordinates_GFp(group, point,
271619261079SEd Maste 	    x, y, NULL) != 1) {
2717a0ee8cc6SDag-Erling Smørgrav 		fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
2718a0ee8cc6SDag-Erling Smørgrav 		    __func__);
271919261079SEd Maste 		goto out;
2720a0ee8cc6SDag-Erling Smørgrav 	}
2721a0ee8cc6SDag-Erling Smørgrav 	fputs("x=", stderr);
2722a0ee8cc6SDag-Erling Smørgrav 	BN_print_fp(stderr, x);
2723a0ee8cc6SDag-Erling Smørgrav 	fputs("\ny=", stderr);
2724a0ee8cc6SDag-Erling Smørgrav 	BN_print_fp(stderr, y);
2725a0ee8cc6SDag-Erling Smørgrav 	fputs("\n", stderr);
272619261079SEd Maste  out:
272719261079SEd Maste 	BN_clear_free(x);
272819261079SEd Maste 	BN_clear_free(y);
2729a0ee8cc6SDag-Erling Smørgrav }
2730a0ee8cc6SDag-Erling Smørgrav 
2731a0ee8cc6SDag-Erling Smørgrav void
sshkey_dump_ec_key(const EC_KEY * key)2732a0ee8cc6SDag-Erling Smørgrav sshkey_dump_ec_key(const EC_KEY *key)
2733a0ee8cc6SDag-Erling Smørgrav {
2734a0ee8cc6SDag-Erling Smørgrav 	const BIGNUM *exponent;
2735a0ee8cc6SDag-Erling Smørgrav 
2736a0ee8cc6SDag-Erling Smørgrav 	sshkey_dump_ec_point(EC_KEY_get0_group(key),
2737a0ee8cc6SDag-Erling Smørgrav 	    EC_KEY_get0_public_key(key));
2738a0ee8cc6SDag-Erling Smørgrav 	fputs("exponent=", stderr);
2739a0ee8cc6SDag-Erling Smørgrav 	if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
2740a0ee8cc6SDag-Erling Smørgrav 		fputs("(NULL)", stderr);
2741a0ee8cc6SDag-Erling Smørgrav 	else
2742a0ee8cc6SDag-Erling Smørgrav 		BN_print_fp(stderr, EC_KEY_get0_private_key(key));
2743a0ee8cc6SDag-Erling Smørgrav 	fputs("\n", stderr);
2744a0ee8cc6SDag-Erling Smørgrav }
2745a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
2746a0ee8cc6SDag-Erling Smørgrav 
2747a0ee8cc6SDag-Erling Smørgrav static int
sshkey_private_to_blob2(struct sshkey * prv,struct sshbuf * blob,const char * passphrase,const char * comment,const char * ciphername,int rounds)274819261079SEd Maste sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
2749a0ee8cc6SDag-Erling Smørgrav     const char *passphrase, const char *comment, const char *ciphername,
2750a0ee8cc6SDag-Erling Smørgrav     int rounds)
2751a0ee8cc6SDag-Erling Smørgrav {
2752bc5531deSDag-Erling Smørgrav 	u_char *cp, *key = NULL, *pubkeyblob = NULL;
2753a0ee8cc6SDag-Erling Smørgrav 	u_char salt[SALT_LEN];
2754a0ee8cc6SDag-Erling Smørgrav 	size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
2755a0ee8cc6SDag-Erling Smørgrav 	u_int check;
2756a0ee8cc6SDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
2757ca86bcf2SDag-Erling Smørgrav 	struct sshcipher_ctx *ciphercontext = NULL;
2758a0ee8cc6SDag-Erling Smørgrav 	const struct sshcipher *cipher;
2759a0ee8cc6SDag-Erling Smørgrav 	const char *kdfname = KDFNAME;
2760a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
2761a0ee8cc6SDag-Erling Smørgrav 
2762a0ee8cc6SDag-Erling Smørgrav 	if (rounds <= 0)
2763a0ee8cc6SDag-Erling Smørgrav 		rounds = DEFAULT_ROUNDS;
2764a0ee8cc6SDag-Erling Smørgrav 	if (passphrase == NULL || !strlen(passphrase)) {
2765a0ee8cc6SDag-Erling Smørgrav 		ciphername = "none";
2766a0ee8cc6SDag-Erling Smørgrav 		kdfname = "none";
2767a0ee8cc6SDag-Erling Smørgrav 	} else if (ciphername == NULL)
2768a0ee8cc6SDag-Erling Smørgrav 		ciphername = DEFAULT_CIPHERNAME;
2769a0ee8cc6SDag-Erling Smørgrav 	if ((cipher = cipher_by_name(ciphername)) == NULL) {
27704f52dfbbSDag-Erling Smørgrav 		r = SSH_ERR_INVALID_ARGUMENT;
2771a0ee8cc6SDag-Erling Smørgrav 		goto out;
2772a0ee8cc6SDag-Erling Smørgrav 	}
2773a0ee8cc6SDag-Erling Smørgrav 
2774a0ee8cc6SDag-Erling Smørgrav 	if ((kdf = sshbuf_new()) == NULL ||
2775a0ee8cc6SDag-Erling Smørgrav 	    (encoded = sshbuf_new()) == NULL ||
2776a0ee8cc6SDag-Erling Smørgrav 	    (encrypted = sshbuf_new()) == NULL) {
2777a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
2778a0ee8cc6SDag-Erling Smørgrav 		goto out;
2779a0ee8cc6SDag-Erling Smørgrav 	}
2780a0ee8cc6SDag-Erling Smørgrav 	blocksize = cipher_blocksize(cipher);
2781a0ee8cc6SDag-Erling Smørgrav 	keylen = cipher_keylen(cipher);
2782a0ee8cc6SDag-Erling Smørgrav 	ivlen = cipher_ivlen(cipher);
2783a0ee8cc6SDag-Erling Smørgrav 	authlen = cipher_authlen(cipher);
2784a0ee8cc6SDag-Erling Smørgrav 	if ((key = calloc(1, keylen + ivlen)) == NULL) {
2785a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
2786a0ee8cc6SDag-Erling Smørgrav 		goto out;
2787a0ee8cc6SDag-Erling Smørgrav 	}
2788a0ee8cc6SDag-Erling Smørgrav 	if (strcmp(kdfname, "bcrypt") == 0) {
2789a0ee8cc6SDag-Erling Smørgrav 		arc4random_buf(salt, SALT_LEN);
2790a0ee8cc6SDag-Erling Smørgrav 		if (bcrypt_pbkdf(passphrase, strlen(passphrase),
2791a0ee8cc6SDag-Erling Smørgrav 		    salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
2792a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_INVALID_ARGUMENT;
2793a0ee8cc6SDag-Erling Smørgrav 			goto out;
2794a0ee8cc6SDag-Erling Smørgrav 		}
2795a0ee8cc6SDag-Erling Smørgrav 		if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
2796a0ee8cc6SDag-Erling Smørgrav 		    (r = sshbuf_put_u32(kdf, rounds)) != 0)
2797a0ee8cc6SDag-Erling Smørgrav 			goto out;
2798a0ee8cc6SDag-Erling Smørgrav 	} else if (strcmp(kdfname, "none") != 0) {
2799a0ee8cc6SDag-Erling Smørgrav 		/* Unsupported KDF type */
2800a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2801a0ee8cc6SDag-Erling Smørgrav 		goto out;
2802a0ee8cc6SDag-Erling Smørgrav 	}
2803a0ee8cc6SDag-Erling Smørgrav 	if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
2804a0ee8cc6SDag-Erling Smørgrav 	    key + keylen, ivlen, 1)) != 0)
2805a0ee8cc6SDag-Erling Smørgrav 		goto out;
2806a0ee8cc6SDag-Erling Smørgrav 
2807a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
2808a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
2809a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
2810a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
2811a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_u32(encoded, 1)) != 0 ||	/* number of keys */
2812a0ee8cc6SDag-Erling Smørgrav 	    (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
2813a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
2814a0ee8cc6SDag-Erling Smørgrav 		goto out;
2815a0ee8cc6SDag-Erling Smørgrav 
2816a0ee8cc6SDag-Erling Smørgrav 	/* set up the buffer that will be encrypted */
2817a0ee8cc6SDag-Erling Smørgrav 
2818a0ee8cc6SDag-Erling Smørgrav 	/* Random check bytes */
2819a0ee8cc6SDag-Erling Smørgrav 	check = arc4random();
2820a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
2821a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_u32(encrypted, check)) != 0)
2822a0ee8cc6SDag-Erling Smørgrav 		goto out;
2823a0ee8cc6SDag-Erling Smørgrav 
2824a0ee8cc6SDag-Erling Smørgrav 	/* append private key and comment*/
282547dd1d1bSDag-Erling Smørgrav 	if ((r = sshkey_private_serialize_opt(prv, encrypted,
282647dd1d1bSDag-Erling Smørgrav 	    SSHKEY_SERIALIZE_FULL)) != 0 ||
2827a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_put_cstring(encrypted, comment)) != 0)
2828a0ee8cc6SDag-Erling Smørgrav 		goto out;
2829a0ee8cc6SDag-Erling Smørgrav 
2830a0ee8cc6SDag-Erling Smørgrav 	/* padding */
2831a0ee8cc6SDag-Erling Smørgrav 	i = 0;
2832a0ee8cc6SDag-Erling Smørgrav 	while (sshbuf_len(encrypted) % blocksize) {
2833a0ee8cc6SDag-Erling Smørgrav 		if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
2834a0ee8cc6SDag-Erling Smørgrav 			goto out;
2835a0ee8cc6SDag-Erling Smørgrav 	}
2836a0ee8cc6SDag-Erling Smørgrav 
2837a0ee8cc6SDag-Erling Smørgrav 	/* length in destination buffer */
2838a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
2839a0ee8cc6SDag-Erling Smørgrav 		goto out;
2840a0ee8cc6SDag-Erling Smørgrav 
2841a0ee8cc6SDag-Erling Smørgrav 	/* encrypt */
2842a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_reserve(encoded,
2843a0ee8cc6SDag-Erling Smørgrav 	    sshbuf_len(encrypted) + authlen, &cp)) != 0)
2844a0ee8cc6SDag-Erling Smørgrav 		goto out;
2845ca86bcf2SDag-Erling Smørgrav 	if ((r = cipher_crypt(ciphercontext, 0, cp,
2846a0ee8cc6SDag-Erling Smørgrav 	    sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
2847a0ee8cc6SDag-Erling Smørgrav 		goto out;
2848a0ee8cc6SDag-Erling Smørgrav 
2849a0ee8cc6SDag-Erling Smørgrav 	sshbuf_reset(blob);
285019261079SEd Maste 
285119261079SEd Maste 	/* assemble uuencoded key */
285219261079SEd Maste 	if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
285319261079SEd Maste 	    (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
285419261079SEd Maste 	    (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
2855a0ee8cc6SDag-Erling Smørgrav 		goto out;
2856a0ee8cc6SDag-Erling Smørgrav 
2857a0ee8cc6SDag-Erling Smørgrav 	/* success */
2858a0ee8cc6SDag-Erling Smørgrav 	r = 0;
2859a0ee8cc6SDag-Erling Smørgrav 
2860a0ee8cc6SDag-Erling Smørgrav  out:
2861a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(kdf);
2862a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(encoded);
2863a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(encrypted);
2864ca86bcf2SDag-Erling Smørgrav 	cipher_free(ciphercontext);
2865a0ee8cc6SDag-Erling Smørgrav 	explicit_bzero(salt, sizeof(salt));
286619261079SEd Maste 	if (key != NULL)
286719261079SEd Maste 		freezero(key, keylen + ivlen);
286819261079SEd Maste 	if (pubkeyblob != NULL)
286919261079SEd Maste 		freezero(pubkeyblob, pubkeylen);
2870a0ee8cc6SDag-Erling Smørgrav 	return r;
2871a0ee8cc6SDag-Erling Smørgrav }
2872a0ee8cc6SDag-Erling Smørgrav 
2873a0ee8cc6SDag-Erling Smørgrav static int
private2_uudecode(struct sshbuf * blob,struct sshbuf ** decodedp)287419261079SEd Maste private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
2875a0ee8cc6SDag-Erling Smørgrav {
2876a0ee8cc6SDag-Erling Smørgrav 	const u_char *cp;
2877a0ee8cc6SDag-Erling Smørgrav 	size_t encoded_len;
287819261079SEd Maste 	int r;
287919261079SEd Maste 	u_char last;
2880a0ee8cc6SDag-Erling Smørgrav 	struct sshbuf *encoded = NULL, *decoded = NULL;
2881a0ee8cc6SDag-Erling Smørgrav 
288219261079SEd Maste 	if (blob == NULL || decodedp == NULL)
288319261079SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
288419261079SEd Maste 
288519261079SEd Maste 	*decodedp = NULL;
2886a0ee8cc6SDag-Erling Smørgrav 
2887a0ee8cc6SDag-Erling Smørgrav 	if ((encoded = sshbuf_new()) == NULL ||
288819261079SEd Maste 	    (decoded = sshbuf_new()) == NULL) {
2889a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
2890a0ee8cc6SDag-Erling Smørgrav 		goto out;
2891a0ee8cc6SDag-Erling Smørgrav 	}
2892a0ee8cc6SDag-Erling Smørgrav 
2893a0ee8cc6SDag-Erling Smørgrav 	/* check preamble */
2894a0ee8cc6SDag-Erling Smørgrav 	cp = sshbuf_ptr(blob);
2895a0ee8cc6SDag-Erling Smørgrav 	encoded_len = sshbuf_len(blob);
2896a0ee8cc6SDag-Erling Smørgrav 	if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
2897a0ee8cc6SDag-Erling Smørgrav 	    memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
2898a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
2899a0ee8cc6SDag-Erling Smørgrav 		goto out;
2900a0ee8cc6SDag-Erling Smørgrav 	}
2901a0ee8cc6SDag-Erling Smørgrav 	cp += MARK_BEGIN_LEN;
2902a0ee8cc6SDag-Erling Smørgrav 	encoded_len -= MARK_BEGIN_LEN;
2903a0ee8cc6SDag-Erling Smørgrav 
2904a0ee8cc6SDag-Erling Smørgrav 	/* Look for end marker, removing whitespace as we go */
2905a0ee8cc6SDag-Erling Smørgrav 	while (encoded_len > 0) {
2906a0ee8cc6SDag-Erling Smørgrav 		if (*cp != '\n' && *cp != '\r') {
2907a0ee8cc6SDag-Erling Smørgrav 			if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
2908a0ee8cc6SDag-Erling Smørgrav 				goto out;
2909a0ee8cc6SDag-Erling Smørgrav 		}
2910a0ee8cc6SDag-Erling Smørgrav 		last = *cp;
2911a0ee8cc6SDag-Erling Smørgrav 		encoded_len--;
2912a0ee8cc6SDag-Erling Smørgrav 		cp++;
2913a0ee8cc6SDag-Erling Smørgrav 		if (last == '\n') {
2914a0ee8cc6SDag-Erling Smørgrav 			if (encoded_len >= MARK_END_LEN &&
2915a0ee8cc6SDag-Erling Smørgrav 			    memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
2916a0ee8cc6SDag-Erling Smørgrav 				/* \0 terminate */
2917a0ee8cc6SDag-Erling Smørgrav 				if ((r = sshbuf_put_u8(encoded, 0)) != 0)
2918a0ee8cc6SDag-Erling Smørgrav 					goto out;
2919a0ee8cc6SDag-Erling Smørgrav 				break;
2920a0ee8cc6SDag-Erling Smørgrav 			}
2921a0ee8cc6SDag-Erling Smørgrav 		}
2922a0ee8cc6SDag-Erling Smørgrav 	}
2923a0ee8cc6SDag-Erling Smørgrav 	if (encoded_len == 0) {
2924a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
2925a0ee8cc6SDag-Erling Smørgrav 		goto out;
2926a0ee8cc6SDag-Erling Smørgrav 	}
2927a0ee8cc6SDag-Erling Smørgrav 
2928a0ee8cc6SDag-Erling Smørgrav 	/* decode base64 */
2929bc5531deSDag-Erling Smørgrav 	if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
2930a0ee8cc6SDag-Erling Smørgrav 		goto out;
2931a0ee8cc6SDag-Erling Smørgrav 
2932a0ee8cc6SDag-Erling Smørgrav 	/* check magic */
2933a0ee8cc6SDag-Erling Smørgrav 	if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
2934a0ee8cc6SDag-Erling Smørgrav 	    memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
2935a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
2936a0ee8cc6SDag-Erling Smørgrav 		goto out;
2937a0ee8cc6SDag-Erling Smørgrav 	}
293819261079SEd Maste 	/* success */
293919261079SEd Maste 	*decodedp = decoded;
294019261079SEd Maste 	decoded = NULL;
294119261079SEd Maste 	r = 0;
294219261079SEd Maste  out:
294319261079SEd Maste 	sshbuf_free(encoded);
294419261079SEd Maste 	sshbuf_free(decoded);
294519261079SEd Maste 	return r;
294619261079SEd Maste }
294719261079SEd Maste 
294819261079SEd Maste static int
private2_decrypt(struct sshbuf * decoded,const char * passphrase,struct sshbuf ** decryptedp,struct sshkey ** pubkeyp)294919261079SEd Maste private2_decrypt(struct sshbuf *decoded, const char *passphrase,
295019261079SEd Maste     struct sshbuf **decryptedp, struct sshkey **pubkeyp)
295119261079SEd Maste {
295219261079SEd Maste 	char *ciphername = NULL, *kdfname = NULL;
295319261079SEd Maste 	const struct sshcipher *cipher = NULL;
295419261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
295519261079SEd Maste 	size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
295619261079SEd Maste 	struct sshbuf *kdf = NULL, *decrypted = NULL;
295719261079SEd Maste 	struct sshcipher_ctx *ciphercontext = NULL;
295819261079SEd Maste 	struct sshkey *pubkey = NULL;
295919261079SEd Maste 	u_char *key = NULL, *salt = NULL, *dp;
296019261079SEd Maste 	u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
296119261079SEd Maste 
296219261079SEd Maste 	if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
296319261079SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
296419261079SEd Maste 
296519261079SEd Maste 	*decryptedp = NULL;
296619261079SEd Maste 	*pubkeyp = NULL;
296719261079SEd Maste 
296819261079SEd Maste 	if ((decrypted = sshbuf_new()) == NULL) {
296919261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
297019261079SEd Maste 		goto out;
297119261079SEd Maste 	}
297219261079SEd Maste 
2973a0ee8cc6SDag-Erling Smørgrav 	/* parse public portion of key */
2974a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
2975a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
2976a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
2977a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_froms(decoded, &kdf)) != 0 ||
297819261079SEd Maste 	    (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
297919261079SEd Maste 		goto out;
298019261079SEd Maste 
298119261079SEd Maste 	if (nkeys != 1) {
298219261079SEd Maste 		/* XXX only one key supported at present */
298319261079SEd Maste 		r = SSH_ERR_INVALID_FORMAT;
298419261079SEd Maste 		goto out;
298519261079SEd Maste 	}
298619261079SEd Maste 
298719261079SEd Maste 	if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
2988a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
2989a0ee8cc6SDag-Erling Smørgrav 		goto out;
2990a0ee8cc6SDag-Erling Smørgrav 
2991a0ee8cc6SDag-Erling Smørgrav 	if ((cipher = cipher_by_name(ciphername)) == NULL) {
2992a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2993a0ee8cc6SDag-Erling Smørgrav 		goto out;
2994a0ee8cc6SDag-Erling Smørgrav 	}
2995a0ee8cc6SDag-Erling Smørgrav 	if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
2996a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_KEY_UNKNOWN_CIPHER;
2997a0ee8cc6SDag-Erling Smørgrav 		goto out;
2998a0ee8cc6SDag-Erling Smørgrav 	}
299919261079SEd Maste 	if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
3000a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
3001a0ee8cc6SDag-Erling Smørgrav 		goto out;
3002a0ee8cc6SDag-Erling Smørgrav 	}
300319261079SEd Maste 	if ((passphrase == NULL || strlen(passphrase) == 0) &&
300419261079SEd Maste 	    strcmp(kdfname, "none") != 0) {
300519261079SEd Maste 		/* passphrase required */
300619261079SEd Maste 		r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3007a0ee8cc6SDag-Erling Smørgrav 		goto out;
3008a0ee8cc6SDag-Erling Smørgrav 	}
3009a0ee8cc6SDag-Erling Smørgrav 
3010a0ee8cc6SDag-Erling Smørgrav 	/* check size of encrypted key blob */
3011a0ee8cc6SDag-Erling Smørgrav 	blocksize = cipher_blocksize(cipher);
3012a0ee8cc6SDag-Erling Smørgrav 	if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
3013a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
3014a0ee8cc6SDag-Erling Smørgrav 		goto out;
3015a0ee8cc6SDag-Erling Smørgrav 	}
3016a0ee8cc6SDag-Erling Smørgrav 
3017a0ee8cc6SDag-Erling Smørgrav 	/* setup key */
3018a0ee8cc6SDag-Erling Smørgrav 	keylen = cipher_keylen(cipher);
3019a0ee8cc6SDag-Erling Smørgrav 	ivlen = cipher_ivlen(cipher);
3020557f75e5SDag-Erling Smørgrav 	authlen = cipher_authlen(cipher);
3021a0ee8cc6SDag-Erling Smørgrav 	if ((key = calloc(1, keylen + ivlen)) == NULL) {
3022a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
3023a0ee8cc6SDag-Erling Smørgrav 		goto out;
3024a0ee8cc6SDag-Erling Smørgrav 	}
3025a0ee8cc6SDag-Erling Smørgrav 	if (strcmp(kdfname, "bcrypt") == 0) {
3026a0ee8cc6SDag-Erling Smørgrav 		if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
3027a0ee8cc6SDag-Erling Smørgrav 		    (r = sshbuf_get_u32(kdf, &rounds)) != 0)
3028a0ee8cc6SDag-Erling Smørgrav 			goto out;
3029a0ee8cc6SDag-Erling Smørgrav 		if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
3030a0ee8cc6SDag-Erling Smørgrav 		    key, keylen + ivlen, rounds) < 0) {
3031a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_INVALID_FORMAT;
3032a0ee8cc6SDag-Erling Smørgrav 			goto out;
3033a0ee8cc6SDag-Erling Smørgrav 		}
3034a0ee8cc6SDag-Erling Smørgrav 	}
3035a0ee8cc6SDag-Erling Smørgrav 
3036557f75e5SDag-Erling Smørgrav 	/* check that an appropriate amount of auth data is present */
303719261079SEd Maste 	if (sshbuf_len(decoded) < authlen ||
303819261079SEd Maste 	    sshbuf_len(decoded) - authlen < encrypted_len) {
3039557f75e5SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
3040557f75e5SDag-Erling Smørgrav 		goto out;
3041557f75e5SDag-Erling Smørgrav 	}
3042557f75e5SDag-Erling Smørgrav 
3043a0ee8cc6SDag-Erling Smørgrav 	/* decrypt private portion of key */
3044a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
3045a0ee8cc6SDag-Erling Smørgrav 	    (r = cipher_init(&ciphercontext, cipher, key, keylen,
3046a0ee8cc6SDag-Erling Smørgrav 	    key + keylen, ivlen, 0)) != 0)
3047a0ee8cc6SDag-Erling Smørgrav 		goto out;
3048ca86bcf2SDag-Erling Smørgrav 	if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
3049557f75e5SDag-Erling Smørgrav 	    encrypted_len, 0, authlen)) != 0) {
3050a0ee8cc6SDag-Erling Smørgrav 		/* an integrity error here indicates an incorrect passphrase */
3051a0ee8cc6SDag-Erling Smørgrav 		if (r == SSH_ERR_MAC_INVALID)
3052a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3053a0ee8cc6SDag-Erling Smørgrav 		goto out;
3054a0ee8cc6SDag-Erling Smørgrav 	}
3055557f75e5SDag-Erling Smørgrav 	if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
3056a0ee8cc6SDag-Erling Smørgrav 		goto out;
3057a0ee8cc6SDag-Erling Smørgrav 	/* there should be no trailing data */
3058a0ee8cc6SDag-Erling Smørgrav 	if (sshbuf_len(decoded) != 0) {
3059a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
3060a0ee8cc6SDag-Erling Smørgrav 		goto out;
3061a0ee8cc6SDag-Erling Smørgrav 	}
3062a0ee8cc6SDag-Erling Smørgrav 
3063a0ee8cc6SDag-Erling Smørgrav 	/* check check bytes */
3064a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
3065a0ee8cc6SDag-Erling Smørgrav 	    (r = sshbuf_get_u32(decrypted, &check2)) != 0)
3066a0ee8cc6SDag-Erling Smørgrav 		goto out;
3067a0ee8cc6SDag-Erling Smørgrav 	if (check1 != check2) {
3068a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_KEY_WRONG_PASSPHRASE;
3069a0ee8cc6SDag-Erling Smørgrav 		goto out;
3070a0ee8cc6SDag-Erling Smørgrav 	}
307119261079SEd Maste 	/* success */
307219261079SEd Maste 	*decryptedp = decrypted;
307319261079SEd Maste 	decrypted = NULL;
307419261079SEd Maste 	*pubkeyp = pubkey;
307519261079SEd Maste 	pubkey = NULL;
307619261079SEd Maste 	r = 0;
307719261079SEd Maste  out:
307819261079SEd Maste 	cipher_free(ciphercontext);
307919261079SEd Maste 	free(ciphername);
308019261079SEd Maste 	free(kdfname);
308119261079SEd Maste 	sshkey_free(pubkey);
308219261079SEd Maste 	if (salt != NULL) {
308319261079SEd Maste 		explicit_bzero(salt, slen);
308419261079SEd Maste 		free(salt);
308519261079SEd Maste 	}
308619261079SEd Maste 	if (key != NULL) {
308719261079SEd Maste 		explicit_bzero(key, keylen + ivlen);
308819261079SEd Maste 		free(key);
308919261079SEd Maste 	}
309019261079SEd Maste 	sshbuf_free(kdf);
309119261079SEd Maste 	sshbuf_free(decrypted);
309219261079SEd Maste 	return r;
309319261079SEd Maste }
3094a0ee8cc6SDag-Erling Smørgrav 
309519261079SEd Maste static int
sshkey_parse_private2(struct sshbuf * blob,int type,const char * passphrase,struct sshkey ** keyp,char ** commentp)309619261079SEd Maste sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
309719261079SEd Maste     struct sshkey **keyp, char **commentp)
309819261079SEd Maste {
309919261079SEd Maste 	char *comment = NULL;
310019261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
310119261079SEd Maste 	struct sshbuf *decoded = NULL, *decrypted = NULL;
310219261079SEd Maste 	struct sshkey *k = NULL, *pubkey = NULL;
310319261079SEd Maste 
310419261079SEd Maste 	if (keyp != NULL)
310519261079SEd Maste 		*keyp = NULL;
310619261079SEd Maste 	if (commentp != NULL)
310719261079SEd Maste 		*commentp = NULL;
310819261079SEd Maste 
310919261079SEd Maste 	/* Undo base64 encoding and decrypt the private section */
311019261079SEd Maste 	if ((r = private2_uudecode(blob, &decoded)) != 0 ||
311119261079SEd Maste 	    (r = private2_decrypt(decoded, passphrase,
311219261079SEd Maste 	    &decrypted, &pubkey)) != 0)
311319261079SEd Maste 		goto out;
311419261079SEd Maste 
311519261079SEd Maste 	if (type != KEY_UNSPEC &&
311619261079SEd Maste 	    sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
311719261079SEd Maste 		r = SSH_ERR_KEY_TYPE_MISMATCH;
311819261079SEd Maste 		goto out;
311919261079SEd Maste 	}
312019261079SEd Maste 
312119261079SEd Maste 	/* Load the private key and comment */
312219261079SEd Maste 	if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
312319261079SEd Maste 	    (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
312419261079SEd Maste 		goto out;
312519261079SEd Maste 
312619261079SEd Maste 	/* Check deterministic padding after private section */
312719261079SEd Maste 	if ((r = private2_check_padding(decrypted)) != 0)
312819261079SEd Maste 		goto out;
312919261079SEd Maste 
313019261079SEd Maste 	/* Check that the public key in the envelope matches the private key */
313119261079SEd Maste 	if (!sshkey_equal(pubkey, k)) {
313219261079SEd Maste 		r = SSH_ERR_INVALID_FORMAT;
313319261079SEd Maste 		goto out;
313419261079SEd Maste 	}
3135a0ee8cc6SDag-Erling Smørgrav 
3136a0ee8cc6SDag-Erling Smørgrav 	/* success */
3137a0ee8cc6SDag-Erling Smørgrav 	r = 0;
3138a0ee8cc6SDag-Erling Smørgrav 	if (keyp != NULL) {
3139a0ee8cc6SDag-Erling Smørgrav 		*keyp = k;
3140a0ee8cc6SDag-Erling Smørgrav 		k = NULL;
3141a0ee8cc6SDag-Erling Smørgrav 	}
3142a0ee8cc6SDag-Erling Smørgrav 	if (commentp != NULL) {
3143a0ee8cc6SDag-Erling Smørgrav 		*commentp = comment;
3144a0ee8cc6SDag-Erling Smørgrav 		comment = NULL;
3145a0ee8cc6SDag-Erling Smørgrav 	}
3146a0ee8cc6SDag-Erling Smørgrav  out:
3147a0ee8cc6SDag-Erling Smørgrav 	free(comment);
3148a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(decoded);
3149a0ee8cc6SDag-Erling Smørgrav 	sshbuf_free(decrypted);
3150a0ee8cc6SDag-Erling Smørgrav 	sshkey_free(k);
315119261079SEd Maste 	sshkey_free(pubkey);
3152a0ee8cc6SDag-Erling Smørgrav 	return r;
3153a0ee8cc6SDag-Erling Smørgrav }
3154a0ee8cc6SDag-Erling Smørgrav 
315519261079SEd Maste static int
sshkey_parse_private2_pubkey(struct sshbuf * blob,int type,struct sshkey ** keyp)315619261079SEd Maste sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
315719261079SEd Maste     struct sshkey **keyp)
315819261079SEd Maste {
315919261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
316019261079SEd Maste 	struct sshbuf *decoded = NULL;
316119261079SEd Maste 	struct sshkey *pubkey = NULL;
316219261079SEd Maste 	u_int nkeys = 0;
316319261079SEd Maste 
316419261079SEd Maste 	if (keyp != NULL)
316519261079SEd Maste 		*keyp = NULL;
316619261079SEd Maste 
316719261079SEd Maste 	if ((r = private2_uudecode(blob, &decoded)) != 0)
316819261079SEd Maste 		goto out;
316919261079SEd Maste 	/* parse public key from unencrypted envelope */
317019261079SEd Maste 	if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
317119261079SEd Maste 	    (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
317219261079SEd Maste 	    (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
317319261079SEd Maste 	    (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
317419261079SEd Maste 	    (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
317519261079SEd Maste 		goto out;
317619261079SEd Maste 
317719261079SEd Maste 	if (nkeys != 1) {
317819261079SEd Maste 		/* XXX only one key supported at present */
317919261079SEd Maste 		r = SSH_ERR_INVALID_FORMAT;
318019261079SEd Maste 		goto out;
318119261079SEd Maste 	}
318219261079SEd Maste 
318319261079SEd Maste 	/* Parse the public key */
318419261079SEd Maste 	if ((r = sshkey_froms(decoded, &pubkey)) != 0)
318519261079SEd Maste 		goto out;
318619261079SEd Maste 
318719261079SEd Maste 	if (type != KEY_UNSPEC &&
318819261079SEd Maste 	    sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
318919261079SEd Maste 		r = SSH_ERR_KEY_TYPE_MISMATCH;
319019261079SEd Maste 		goto out;
319119261079SEd Maste 	}
319219261079SEd Maste 
319319261079SEd Maste 	/* success */
319419261079SEd Maste 	r = 0;
319519261079SEd Maste 	if (keyp != NULL) {
319619261079SEd Maste 		*keyp = pubkey;
319719261079SEd Maste 		pubkey = NULL;
319819261079SEd Maste 	}
319919261079SEd Maste  out:
320019261079SEd Maste 	sshbuf_free(decoded);
320119261079SEd Maste 	sshkey_free(pubkey);
320219261079SEd Maste 	return r;
320319261079SEd Maste }
3204a0ee8cc6SDag-Erling Smørgrav 
3205a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
320619261079SEd Maste /* convert SSH v2 key to PEM or PKCS#8 format */
3207a0ee8cc6SDag-Erling Smørgrav static int
sshkey_private_to_blob_pem_pkcs8(struct sshkey * key,struct sshbuf * buf,int format,const char * _passphrase,const char * comment)320819261079SEd Maste sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
320919261079SEd Maste     int format, const char *_passphrase, const char *comment)
3210a0ee8cc6SDag-Erling Smørgrav {
321119261079SEd Maste 	int was_shielded = sshkey_is_shielded(key);
3212a0ee8cc6SDag-Erling Smørgrav 	int success, r;
3213a0ee8cc6SDag-Erling Smørgrav 	int blen, len = strlen(_passphrase);
3214a0ee8cc6SDag-Erling Smørgrav 	u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
3215a0ee8cc6SDag-Erling Smørgrav 	const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
321647dd1d1bSDag-Erling Smørgrav 	char *bptr;
3217a0ee8cc6SDag-Erling Smørgrav 	BIO *bio = NULL;
321819261079SEd Maste 	struct sshbuf *blob;
321919261079SEd Maste 	EVP_PKEY *pkey = NULL;
3220a0ee8cc6SDag-Erling Smørgrav 
3221a0ee8cc6SDag-Erling Smørgrav 	if (len > 0 && len <= 4)
3222a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_PASSPHRASE_TOO_SHORT;
322319261079SEd Maste 	if ((blob = sshbuf_new()) == NULL)
3224a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
322519261079SEd Maste 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
322619261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
322719261079SEd Maste 		goto out;
322819261079SEd Maste 	}
322919261079SEd Maste 	if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
323019261079SEd Maste 		r = SSH_ERR_ALLOC_FAIL;
323119261079SEd Maste 		goto out;
323219261079SEd Maste 	}
323319261079SEd Maste 	if ((r = sshkey_unshield_private(key)) != 0)
323419261079SEd Maste 		goto out;
3235a0ee8cc6SDag-Erling Smørgrav 
3236a0ee8cc6SDag-Erling Smørgrav 	switch (key->type) {
3237a91a2465SEd Maste #ifdef WITH_DSA
3238a0ee8cc6SDag-Erling Smørgrav 	case KEY_DSA:
323919261079SEd Maste 		if (format == SSHKEY_PRIVATE_PEM) {
3240a0ee8cc6SDag-Erling Smørgrav 			success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
3241a0ee8cc6SDag-Erling Smørgrav 			    cipher, passphrase, len, NULL, NULL);
324219261079SEd Maste 		} else {
324319261079SEd Maste 			success = EVP_PKEY_set1_DSA(pkey, key->dsa);
324419261079SEd Maste 		}
3245a0ee8cc6SDag-Erling Smørgrav 		break;
3246a91a2465SEd Maste #endif
3247a0ee8cc6SDag-Erling Smørgrav #ifdef OPENSSL_HAS_ECC
3248a0ee8cc6SDag-Erling Smørgrav 	case KEY_ECDSA:
324919261079SEd Maste 		if (format == SSHKEY_PRIVATE_PEM) {
3250a0ee8cc6SDag-Erling Smørgrav 			success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
3251a0ee8cc6SDag-Erling Smørgrav 			    cipher, passphrase, len, NULL, NULL);
325219261079SEd Maste 		} else {
325319261079SEd Maste 			success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
325419261079SEd Maste 		}
3255a0ee8cc6SDag-Erling Smørgrav 		break;
3256a0ee8cc6SDag-Erling Smørgrav #endif
3257a0ee8cc6SDag-Erling Smørgrav 	case KEY_RSA:
325819261079SEd Maste 		if (format == SSHKEY_PRIVATE_PEM) {
3259a0ee8cc6SDag-Erling Smørgrav 			success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
3260a0ee8cc6SDag-Erling Smørgrav 			    cipher, passphrase, len, NULL, NULL);
326119261079SEd Maste 		} else {
326219261079SEd Maste 			success = EVP_PKEY_set1_RSA(pkey, key->rsa);
326319261079SEd Maste 		}
3264a0ee8cc6SDag-Erling Smørgrav 		break;
3265a0ee8cc6SDag-Erling Smørgrav 	default:
3266a0ee8cc6SDag-Erling Smørgrav 		success = 0;
3267a0ee8cc6SDag-Erling Smørgrav 		break;
3268a0ee8cc6SDag-Erling Smørgrav 	}
3269a0ee8cc6SDag-Erling Smørgrav 	if (success == 0) {
3270a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_LIBCRYPTO_ERROR;
3271a0ee8cc6SDag-Erling Smørgrav 		goto out;
3272a0ee8cc6SDag-Erling Smørgrav 	}
327319261079SEd Maste 	if (format == SSHKEY_PRIVATE_PKCS8) {
327419261079SEd Maste 		if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
327519261079SEd Maste 		    passphrase, len, NULL, NULL)) == 0) {
327619261079SEd Maste 			r = SSH_ERR_LIBCRYPTO_ERROR;
327719261079SEd Maste 			goto out;
327819261079SEd Maste 		}
327919261079SEd Maste 	}
3280a0ee8cc6SDag-Erling Smørgrav 	if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
3281a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INTERNAL_ERROR;
3282a0ee8cc6SDag-Erling Smørgrav 		goto out;
3283a0ee8cc6SDag-Erling Smørgrav 	}
3284a0ee8cc6SDag-Erling Smørgrav 	if ((r = sshbuf_put(blob, bptr, blen)) != 0)
3285a0ee8cc6SDag-Erling Smørgrav 		goto out;
3286a0ee8cc6SDag-Erling Smørgrav 	r = 0;
3287a0ee8cc6SDag-Erling Smørgrav  out:
328819261079SEd Maste 	if (was_shielded)
328919261079SEd Maste 		r = sshkey_shield_private(key);
329019261079SEd Maste 	if (r == 0)
329119261079SEd Maste 		r = sshbuf_putb(buf, blob);
329219261079SEd Maste 
329319261079SEd Maste 	EVP_PKEY_free(pkey);
329419261079SEd Maste 	sshbuf_free(blob);
3295a0ee8cc6SDag-Erling Smørgrav 	BIO_free(bio);
3296a0ee8cc6SDag-Erling Smørgrav 	return r;
3297a0ee8cc6SDag-Erling Smørgrav }
3298a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
3299a0ee8cc6SDag-Erling Smørgrav 
3300a0ee8cc6SDag-Erling Smørgrav /* Serialise "key" to buffer "blob" */
3301a0ee8cc6SDag-Erling Smørgrav int
sshkey_private_to_fileblob(struct sshkey * key,struct sshbuf * blob,const char * passphrase,const char * comment,int format,const char * openssh_format_cipher,int openssh_format_rounds)3302a0ee8cc6SDag-Erling Smørgrav sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
3303a0ee8cc6SDag-Erling Smørgrav     const char *passphrase, const char *comment,
330419261079SEd Maste     int format, const char *openssh_format_cipher, int openssh_format_rounds)
3305a0ee8cc6SDag-Erling Smørgrav {
3306a0ee8cc6SDag-Erling Smørgrav 	switch (key->type) {
3307bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
3308a0ee8cc6SDag-Erling Smørgrav 	case KEY_DSA:
3309a0ee8cc6SDag-Erling Smørgrav 	case KEY_ECDSA:
3310a0ee8cc6SDag-Erling Smørgrav 	case KEY_RSA:
331119261079SEd Maste 		break; /* see below */
3312a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
3313a0ee8cc6SDag-Erling Smørgrav 	case KEY_ED25519:
331419261079SEd Maste 	case KEY_ED25519_SK:
331547dd1d1bSDag-Erling Smørgrav #ifdef WITH_XMSS
331647dd1d1bSDag-Erling Smørgrav 	case KEY_XMSS:
331747dd1d1bSDag-Erling Smørgrav #endif /* WITH_XMSS */
331819261079SEd Maste #ifdef WITH_OPENSSL
331919261079SEd Maste 	case KEY_ECDSA_SK:
332019261079SEd Maste #endif /* WITH_OPENSSL */
3321a0ee8cc6SDag-Erling Smørgrav 		return sshkey_private_to_blob2(key, blob, passphrase,
332219261079SEd Maste 		    comment, openssh_format_cipher, openssh_format_rounds);
3323a0ee8cc6SDag-Erling Smørgrav 	default:
3324a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_KEY_TYPE_UNKNOWN;
3325a0ee8cc6SDag-Erling Smørgrav 	}
3326a0ee8cc6SDag-Erling Smørgrav 
332719261079SEd Maste #ifdef WITH_OPENSSL
332819261079SEd Maste 	switch (format) {
332919261079SEd Maste 	case SSHKEY_PRIVATE_OPENSSH:
333019261079SEd Maste 		return sshkey_private_to_blob2(key, blob, passphrase,
333119261079SEd Maste 		    comment, openssh_format_cipher, openssh_format_rounds);
333219261079SEd Maste 	case SSHKEY_PRIVATE_PEM:
333319261079SEd Maste 	case SSHKEY_PRIVATE_PKCS8:
333419261079SEd Maste 		return sshkey_private_to_blob_pem_pkcs8(key, blob,
333519261079SEd Maste 		    format, passphrase, comment);
333619261079SEd Maste 	default:
333719261079SEd Maste 		return SSH_ERR_INVALID_ARGUMENT;
333819261079SEd Maste 	}
333919261079SEd Maste #endif /* WITH_OPENSSL */
334019261079SEd Maste }
3341a0ee8cc6SDag-Erling Smørgrav 
3342a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
3343bc5531deSDag-Erling Smørgrav static int
translate_libcrypto_error(unsigned long pem_err)33444f52dfbbSDag-Erling Smørgrav translate_libcrypto_error(unsigned long pem_err)
33454f52dfbbSDag-Erling Smørgrav {
33464f52dfbbSDag-Erling Smørgrav 	int pem_reason = ERR_GET_REASON(pem_err);
33474f52dfbbSDag-Erling Smørgrav 
33484f52dfbbSDag-Erling Smørgrav 	switch (ERR_GET_LIB(pem_err)) {
33494f52dfbbSDag-Erling Smørgrav 	case ERR_LIB_PEM:
33504f52dfbbSDag-Erling Smørgrav 		switch (pem_reason) {
33514f52dfbbSDag-Erling Smørgrav 		case PEM_R_BAD_PASSWORD_READ:
3352535af610SEd Maste #ifdef PEM_R_PROBLEMS_GETTING_PASSWORD
33534f52dfbbSDag-Erling Smørgrav 		case PEM_R_PROBLEMS_GETTING_PASSWORD:
3354535af610SEd Maste #endif
3355535af610SEd Maste #ifdef PEM_R_BAD_DECRYPT
33564f52dfbbSDag-Erling Smørgrav 		case PEM_R_BAD_DECRYPT:
3357535af610SEd Maste #endif
33584f52dfbbSDag-Erling Smørgrav 			return SSH_ERR_KEY_WRONG_PASSPHRASE;
33594f52dfbbSDag-Erling Smørgrav 		default:
33604f52dfbbSDag-Erling Smørgrav 			return SSH_ERR_INVALID_FORMAT;
33614f52dfbbSDag-Erling Smørgrav 		}
33624f52dfbbSDag-Erling Smørgrav 	case ERR_LIB_EVP:
33634f52dfbbSDag-Erling Smørgrav 		switch (pem_reason) {
3364535af610SEd Maste #ifdef EVP_R_BAD_DECRYPT
33654f52dfbbSDag-Erling Smørgrav 		case EVP_R_BAD_DECRYPT:
33664f52dfbbSDag-Erling Smørgrav 			return SSH_ERR_KEY_WRONG_PASSPHRASE;
3367535af610SEd Maste #endif
33682a01feabSEd Maste #ifdef EVP_R_BN_DECODE_ERROR
33694f52dfbbSDag-Erling Smørgrav 		case EVP_R_BN_DECODE_ERROR:
33702a01feabSEd Maste #endif
33714f52dfbbSDag-Erling Smørgrav 		case EVP_R_DECODE_ERROR:
33724f52dfbbSDag-Erling Smørgrav #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
33734f52dfbbSDag-Erling Smørgrav 		case EVP_R_PRIVATE_KEY_DECODE_ERROR:
33744f52dfbbSDag-Erling Smørgrav #endif
33754f52dfbbSDag-Erling Smørgrav 			return SSH_ERR_INVALID_FORMAT;
33764f52dfbbSDag-Erling Smørgrav 		default:
33774f52dfbbSDag-Erling Smørgrav 			return SSH_ERR_LIBCRYPTO_ERROR;
33784f52dfbbSDag-Erling Smørgrav 		}
33794f52dfbbSDag-Erling Smørgrav 	case ERR_LIB_ASN1:
33804f52dfbbSDag-Erling Smørgrav 		return SSH_ERR_INVALID_FORMAT;
33814f52dfbbSDag-Erling Smørgrav 	}
33824f52dfbbSDag-Erling Smørgrav 	return SSH_ERR_LIBCRYPTO_ERROR;
33834f52dfbbSDag-Erling Smørgrav }
33844f52dfbbSDag-Erling Smørgrav 
33854f52dfbbSDag-Erling Smørgrav static void
clear_libcrypto_errors(void)33864f52dfbbSDag-Erling Smørgrav clear_libcrypto_errors(void)
33874f52dfbbSDag-Erling Smørgrav {
33884f52dfbbSDag-Erling Smørgrav 	while (ERR_get_error() != 0)
33894f52dfbbSDag-Erling Smørgrav 		;
33904f52dfbbSDag-Erling Smørgrav }
33914f52dfbbSDag-Erling Smørgrav 
33924f52dfbbSDag-Erling Smørgrav /*
33934f52dfbbSDag-Erling Smørgrav  * Translate OpenSSL error codes to determine whether
33944f52dfbbSDag-Erling Smørgrav  * passphrase is required/incorrect.
33954f52dfbbSDag-Erling Smørgrav  */
33964f52dfbbSDag-Erling Smørgrav static int
convert_libcrypto_error(void)33974f52dfbbSDag-Erling Smørgrav convert_libcrypto_error(void)
33984f52dfbbSDag-Erling Smørgrav {
33994f52dfbbSDag-Erling Smørgrav 	/*
34004f52dfbbSDag-Erling Smørgrav 	 * Some password errors are reported at the beginning
34014f52dfbbSDag-Erling Smørgrav 	 * of the error queue.
34024f52dfbbSDag-Erling Smørgrav 	 */
34034f52dfbbSDag-Erling Smørgrav 	if (translate_libcrypto_error(ERR_peek_error()) ==
34044f52dfbbSDag-Erling Smørgrav 	    SSH_ERR_KEY_WRONG_PASSPHRASE)
34054f52dfbbSDag-Erling Smørgrav 		return SSH_ERR_KEY_WRONG_PASSPHRASE;
34064f52dfbbSDag-Erling Smørgrav 	return translate_libcrypto_error(ERR_peek_last_error());
34074f52dfbbSDag-Erling Smørgrav }
34084f52dfbbSDag-Erling Smørgrav 
34094f52dfbbSDag-Erling Smørgrav static int
pem_passphrase_cb(char * buf,int size,int rwflag,void * u)34102f513db7SEd Maste pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
34112f513db7SEd Maste {
34122f513db7SEd Maste 	char *p = (char *)u;
34132f513db7SEd Maste 	size_t len;
34142f513db7SEd Maste 
34152f513db7SEd Maste 	if (p == NULL || (len = strlen(p)) == 0)
34162f513db7SEd Maste 		return -1;
34172f513db7SEd Maste 	if (size < 0 || len > (size_t)size)
34182f513db7SEd Maste 		return -1;
34192f513db7SEd Maste 	memcpy(buf, p, len);
34202f513db7SEd Maste 	return (int)len;
34212f513db7SEd Maste }
34222f513db7SEd Maste 
34232f513db7SEd Maste static int
sshkey_parse_private_pem_fileblob(struct sshbuf * blob,int type,const char * passphrase,struct sshkey ** keyp)3424a0ee8cc6SDag-Erling Smørgrav sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
3425bc5531deSDag-Erling Smørgrav     const char *passphrase, struct sshkey **keyp)
3426a0ee8cc6SDag-Erling Smørgrav {
3427a0ee8cc6SDag-Erling Smørgrav 	EVP_PKEY *pk = NULL;
3428a0ee8cc6SDag-Erling Smørgrav 	struct sshkey *prv = NULL;
3429a0ee8cc6SDag-Erling Smørgrav 	BIO *bio = NULL;
3430a0ee8cc6SDag-Erling Smørgrav 	int r;
3431a0ee8cc6SDag-Erling Smørgrav 
3432076ad2f8SDag-Erling Smørgrav 	if (keyp != NULL)
3433a0ee8cc6SDag-Erling Smørgrav 		*keyp = NULL;
3434a0ee8cc6SDag-Erling Smørgrav 
3435a0ee8cc6SDag-Erling Smørgrav 	if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
3436a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
3437a0ee8cc6SDag-Erling Smørgrav 	if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
3438a0ee8cc6SDag-Erling Smørgrav 	    (int)sshbuf_len(blob)) {
3439a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_ALLOC_FAIL;
3440a0ee8cc6SDag-Erling Smørgrav 		goto out;
3441a0ee8cc6SDag-Erling Smørgrav 	}
3442a0ee8cc6SDag-Erling Smørgrav 
34434f52dfbbSDag-Erling Smørgrav 	clear_libcrypto_errors();
34442f513db7SEd Maste 	if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
3445a0ee8cc6SDag-Erling Smørgrav 	    (char *)passphrase)) == NULL) {
34462f513db7SEd Maste 		/*
34472f513db7SEd Maste 		 * libcrypto may return various ASN.1 errors when attempting
34482f513db7SEd Maste 		 * to parse a key with an incorrect passphrase.
34492f513db7SEd Maste 		 * Treat all format errors as "incorrect passphrase" if a
34502f513db7SEd Maste 		 * passphrase was supplied.
34512f513db7SEd Maste 		 */
34522f513db7SEd Maste 		if (passphrase != NULL && *passphrase != '\0')
34532f513db7SEd Maste 			r = SSH_ERR_KEY_WRONG_PASSPHRASE;
34542f513db7SEd Maste 		else
34554f52dfbbSDag-Erling Smørgrav 			r = convert_libcrypto_error();
3456d93a896eSDag-Erling Smørgrav 		goto out;
3457a0ee8cc6SDag-Erling Smørgrav 	}
34582a01feabSEd Maste 	if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
3459a0ee8cc6SDag-Erling Smørgrav 	    (type == KEY_UNSPEC || type == KEY_RSA)) {
3460a0ee8cc6SDag-Erling Smørgrav 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3461a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_ALLOC_FAIL;
3462a0ee8cc6SDag-Erling Smørgrav 			goto out;
3463a0ee8cc6SDag-Erling Smørgrav 		}
3464a0ee8cc6SDag-Erling Smørgrav 		prv->rsa = EVP_PKEY_get1_RSA(pk);
3465a0ee8cc6SDag-Erling Smørgrav 		prv->type = KEY_RSA;
3466a0ee8cc6SDag-Erling Smørgrav #ifdef DEBUG_PK
3467a0ee8cc6SDag-Erling Smørgrav 		RSA_print_fp(stderr, prv->rsa, 8);
3468a0ee8cc6SDag-Erling Smørgrav #endif
3469a0ee8cc6SDag-Erling Smørgrav 		if (RSA_blinding_on(prv->rsa, NULL) != 1) {
3470a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_LIBCRYPTO_ERROR;
3471a0ee8cc6SDag-Erling Smørgrav 			goto out;
3472a0ee8cc6SDag-Erling Smørgrav 		}
347338a52bd3SEd Maste 		if ((r = sshkey_check_rsa_length(prv, 0)) != 0)
34744f52dfbbSDag-Erling Smørgrav 			goto out;
3475a91a2465SEd Maste #ifdef WITH_DSA
34762a01feabSEd Maste 	} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
3477a0ee8cc6SDag-Erling Smørgrav 	    (type == KEY_UNSPEC || type == KEY_DSA)) {
3478a0ee8cc6SDag-Erling Smørgrav 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3479a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_ALLOC_FAIL;
3480a0ee8cc6SDag-Erling Smørgrav 			goto out;
3481a0ee8cc6SDag-Erling Smørgrav 		}
3482a0ee8cc6SDag-Erling Smørgrav 		prv->dsa = EVP_PKEY_get1_DSA(pk);
3483a0ee8cc6SDag-Erling Smørgrav 		prv->type = KEY_DSA;
3484a0ee8cc6SDag-Erling Smørgrav #ifdef DEBUG_PK
3485a0ee8cc6SDag-Erling Smørgrav 		DSA_print_fp(stderr, prv->dsa, 8);
3486a0ee8cc6SDag-Erling Smørgrav #endif
3487a91a2465SEd Maste #endif
3488a0ee8cc6SDag-Erling Smørgrav #ifdef OPENSSL_HAS_ECC
34892a01feabSEd Maste 	} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
3490a0ee8cc6SDag-Erling Smørgrav 	    (type == KEY_UNSPEC || type == KEY_ECDSA)) {
3491a0ee8cc6SDag-Erling Smørgrav 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
3492a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_ALLOC_FAIL;
3493a0ee8cc6SDag-Erling Smørgrav 			goto out;
3494a0ee8cc6SDag-Erling Smørgrav 		}
3495a0ee8cc6SDag-Erling Smørgrav 		prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
3496a0ee8cc6SDag-Erling Smørgrav 		prv->type = KEY_ECDSA;
3497a0ee8cc6SDag-Erling Smørgrav 		prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
3498a0ee8cc6SDag-Erling Smørgrav 		if (prv->ecdsa_nid == -1 ||
3499a0ee8cc6SDag-Erling Smørgrav 		    sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
3500a0ee8cc6SDag-Erling Smørgrav 		    sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
3501a0ee8cc6SDag-Erling Smørgrav 		    EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
3502a0ee8cc6SDag-Erling Smørgrav 		    sshkey_ec_validate_private(prv->ecdsa) != 0) {
3503a0ee8cc6SDag-Erling Smørgrav 			r = SSH_ERR_INVALID_FORMAT;
3504a0ee8cc6SDag-Erling Smørgrav 			goto out;
3505a0ee8cc6SDag-Erling Smørgrav 		}
3506a0ee8cc6SDag-Erling Smørgrav # ifdef DEBUG_PK
3507a0ee8cc6SDag-Erling Smørgrav 		if (prv != NULL && prv->ecdsa != NULL)
3508a0ee8cc6SDag-Erling Smørgrav 			sshkey_dump_ec_key(prv->ecdsa);
3509a0ee8cc6SDag-Erling Smørgrav # endif
3510a0ee8cc6SDag-Erling Smørgrav #endif /* OPENSSL_HAS_ECC */
3511069ac184SEd Maste #ifdef OPENSSL_HAS_ED25519
3512069ac184SEd Maste 	} else if (EVP_PKEY_base_id(pk) == EVP_PKEY_ED25519 &&
3513069ac184SEd Maste 	    (type == KEY_UNSPEC || type == KEY_ED25519)) {
3514069ac184SEd Maste 		size_t len;
3515069ac184SEd Maste 
3516069ac184SEd Maste 		if ((prv = sshkey_new(KEY_UNSPEC)) == NULL ||
3517069ac184SEd Maste 		    (prv->ed25519_sk = calloc(1, ED25519_SK_SZ)) == NULL ||
3518069ac184SEd Maste 		    (prv->ed25519_pk = calloc(1, ED25519_PK_SZ)) == NULL) {
3519069ac184SEd Maste 			r = SSH_ERR_ALLOC_FAIL;
3520069ac184SEd Maste 			goto out;
3521069ac184SEd Maste 		}
3522069ac184SEd Maste 		prv->type = KEY_ED25519;
3523069ac184SEd Maste 		len = ED25519_PK_SZ;
3524069ac184SEd Maste 		if (!EVP_PKEY_get_raw_public_key(pk, prv->ed25519_pk, &len)) {
3525069ac184SEd Maste 			r = SSH_ERR_LIBCRYPTO_ERROR;
3526069ac184SEd Maste 			goto out;
3527069ac184SEd Maste 		}
3528069ac184SEd Maste 		if (len != ED25519_PK_SZ) {
3529069ac184SEd Maste 			r = SSH_ERR_INVALID_FORMAT;
3530069ac184SEd Maste 			goto out;
3531069ac184SEd Maste 		}
3532069ac184SEd Maste 		len = ED25519_SK_SZ - ED25519_PK_SZ;
3533069ac184SEd Maste 		if (!EVP_PKEY_get_raw_private_key(pk, prv->ed25519_sk, &len)) {
3534069ac184SEd Maste 			r = SSH_ERR_LIBCRYPTO_ERROR;
3535069ac184SEd Maste 			goto out;
3536069ac184SEd Maste 		}
3537069ac184SEd Maste 		if (len != ED25519_SK_SZ - ED25519_PK_SZ) {
3538069ac184SEd Maste 			r = SSH_ERR_INVALID_FORMAT;
3539069ac184SEd Maste 			goto out;
3540069ac184SEd Maste 		}
3541069ac184SEd Maste 		/* Append the public key to our private key */
3542069ac184SEd Maste 		memcpy(prv->ed25519_sk + (ED25519_SK_SZ - ED25519_PK_SZ),
3543069ac184SEd Maste 		    prv->ed25519_pk, ED25519_PK_SZ);
3544069ac184SEd Maste # ifdef DEBUG_PK
3545069ac184SEd Maste 		sshbuf_dump_data(prv->ed25519_sk, ED25519_SK_SZ, stderr);
3546069ac184SEd Maste # endif
3547069ac184SEd Maste #endif /* OPENSSL_HAS_ED25519 */
3548a0ee8cc6SDag-Erling Smørgrav 	} else {
3549a0ee8cc6SDag-Erling Smørgrav 		r = SSH_ERR_INVALID_FORMAT;
3550a0ee8cc6SDag-Erling Smørgrav 		goto out;
3551a0ee8cc6SDag-Erling Smørgrav 	}
3552a0ee8cc6SDag-Erling Smørgrav 	r = 0;
3553076ad2f8SDag-Erling Smørgrav 	if (keyp != NULL) {
3554a0ee8cc6SDag-Erling Smørgrav 		*keyp = prv;
3555a0ee8cc6SDag-Erling Smørgrav 		prv = NULL;
3556076ad2f8SDag-Erling Smørgrav 	}
3557a0ee8cc6SDag-Erling Smørgrav  out:
3558a0ee8cc6SDag-Erling Smørgrav 	BIO_free(bio);
3559a0ee8cc6SDag-Erling Smørgrav 	EVP_PKEY_free(pk);
3560a0ee8cc6SDag-Erling Smørgrav 	sshkey_free(prv);
3561a0ee8cc6SDag-Erling Smørgrav 	return r;
3562a0ee8cc6SDag-Erling Smørgrav }
3563a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
3564a0ee8cc6SDag-Erling Smørgrav 
3565a0ee8cc6SDag-Erling Smørgrav int
sshkey_parse_private_fileblob_type(struct sshbuf * blob,int type,const char * passphrase,struct sshkey ** keyp,char ** commentp)3566a0ee8cc6SDag-Erling Smørgrav sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
3567a0ee8cc6SDag-Erling Smørgrav     const char *passphrase, struct sshkey **keyp, char **commentp)
3568a0ee8cc6SDag-Erling Smørgrav {
3569d93a896eSDag-Erling Smørgrav 	int r = SSH_ERR_INTERNAL_ERROR;
3570d93a896eSDag-Erling Smørgrav 
3571076ad2f8SDag-Erling Smørgrav 	if (keyp != NULL)
3572a0ee8cc6SDag-Erling Smørgrav 		*keyp = NULL;
3573a0ee8cc6SDag-Erling Smørgrav 	if (commentp != NULL)
3574a0ee8cc6SDag-Erling Smørgrav 		*commentp = NULL;
3575a0ee8cc6SDag-Erling Smørgrav 
3576a0ee8cc6SDag-Erling Smørgrav 	switch (type) {
357747dd1d1bSDag-Erling Smørgrav 	case KEY_XMSS:
357819261079SEd Maste 		/* No fallback for new-format-only keys */
3579a0ee8cc6SDag-Erling Smørgrav 		return sshkey_parse_private2(blob, type, passphrase,
3580a0ee8cc6SDag-Erling Smørgrav 		    keyp, commentp);
358119261079SEd Maste 	default:
3582d93a896eSDag-Erling Smørgrav 		r = sshkey_parse_private2(blob, type, passphrase, keyp,
3583d93a896eSDag-Erling Smørgrav 		    commentp);
358419261079SEd Maste 		/* Only fallback to PEM parser if a format error occurred. */
358519261079SEd Maste 		if (r != SSH_ERR_INVALID_FORMAT)
3586d93a896eSDag-Erling Smørgrav 			return r;
3587a0ee8cc6SDag-Erling Smørgrav #ifdef WITH_OPENSSL
3588bc5531deSDag-Erling Smørgrav 		return sshkey_parse_private_pem_fileblob(blob, type,
3589bc5531deSDag-Erling Smørgrav 		    passphrase, keyp);
3590a0ee8cc6SDag-Erling Smørgrav #else
3591a0ee8cc6SDag-Erling Smørgrav 		return SSH_ERR_INVALID_FORMAT;
3592a0ee8cc6SDag-Erling Smørgrav #endif /* WITH_OPENSSL */
3593a0ee8cc6SDag-Erling Smørgrav 	}
3594a0ee8cc6SDag-Erling Smørgrav }
3595a0ee8cc6SDag-Erling Smørgrav 
3596a0ee8cc6SDag-Erling Smørgrav int
sshkey_parse_private_fileblob(struct sshbuf * buffer,const char * passphrase,struct sshkey ** keyp,char ** commentp)3597a0ee8cc6SDag-Erling Smørgrav sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
3598acc1a9efSDag-Erling Smørgrav     struct sshkey **keyp, char **commentp)
3599a0ee8cc6SDag-Erling Smørgrav {
3600a0ee8cc6SDag-Erling Smørgrav 	if (keyp != NULL)
3601a0ee8cc6SDag-Erling Smørgrav 		*keyp = NULL;
3602a0ee8cc6SDag-Erling Smørgrav 	if (commentp != NULL)
3603a0ee8cc6SDag-Erling Smørgrav 		*commentp = NULL;
3604a0ee8cc6SDag-Erling Smørgrav 
3605acc1a9efSDag-Erling Smørgrav 	return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
3606acc1a9efSDag-Erling Smørgrav 	    passphrase, keyp, commentp);
3607a0ee8cc6SDag-Erling Smørgrav }
360847dd1d1bSDag-Erling Smørgrav 
360919261079SEd Maste void
sshkey_sig_details_free(struct sshkey_sig_details * details)361019261079SEd Maste sshkey_sig_details_free(struct sshkey_sig_details *details)
361119261079SEd Maste {
361219261079SEd Maste 	freezero(details, sizeof(*details));
361319261079SEd Maste }
361419261079SEd Maste 
361519261079SEd Maste int
sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf * blob,int type,struct sshkey ** pubkeyp)361619261079SEd Maste sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
361719261079SEd Maste     struct sshkey **pubkeyp)
361819261079SEd Maste {
361919261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
362019261079SEd Maste 
362119261079SEd Maste 	if (pubkeyp != NULL)
362219261079SEd Maste 		*pubkeyp = NULL;
362319261079SEd Maste 	/* only new-format private keys bundle a public key inside */
362419261079SEd Maste 	if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
362519261079SEd Maste 		return r;
362619261079SEd Maste 	return 0;
362719261079SEd Maste }
362819261079SEd Maste 
362947dd1d1bSDag-Erling Smørgrav #ifdef WITH_XMSS
363047dd1d1bSDag-Erling Smørgrav /*
363147dd1d1bSDag-Erling Smørgrav  * serialize the key with the current state and forward the state
363247dd1d1bSDag-Erling Smørgrav  * maxsign times.
363347dd1d1bSDag-Erling Smørgrav  */
363447dd1d1bSDag-Erling Smørgrav int
sshkey_private_serialize_maxsign(struct sshkey * k,struct sshbuf * b,u_int32_t maxsign,int printerror)363519261079SEd Maste sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
363619261079SEd Maste     u_int32_t maxsign, int printerror)
363747dd1d1bSDag-Erling Smørgrav {
363847dd1d1bSDag-Erling Smørgrav 	int r, rupdate;
363947dd1d1bSDag-Erling Smørgrav 
364047dd1d1bSDag-Erling Smørgrav 	if (maxsign == 0 ||
364147dd1d1bSDag-Erling Smørgrav 	    sshkey_type_plain(k->type) != KEY_XMSS)
364247dd1d1bSDag-Erling Smørgrav 		return sshkey_private_serialize_opt(k, b,
364347dd1d1bSDag-Erling Smørgrav 		    SSHKEY_SERIALIZE_DEFAULT);
364419261079SEd Maste 	if ((r = sshkey_xmss_get_state(k, printerror)) != 0 ||
364547dd1d1bSDag-Erling Smørgrav 	    (r = sshkey_private_serialize_opt(k, b,
364647dd1d1bSDag-Erling Smørgrav 	    SSHKEY_SERIALIZE_STATE)) != 0 ||
364747dd1d1bSDag-Erling Smørgrav 	    (r = sshkey_xmss_forward_state(k, maxsign)) != 0)
364847dd1d1bSDag-Erling Smørgrav 		goto out;
364947dd1d1bSDag-Erling Smørgrav 	r = 0;
365047dd1d1bSDag-Erling Smørgrav out:
365119261079SEd Maste 	if ((rupdate = sshkey_xmss_update_state(k, printerror)) != 0) {
365247dd1d1bSDag-Erling Smørgrav 		if (r == 0)
365347dd1d1bSDag-Erling Smørgrav 			r = rupdate;
365447dd1d1bSDag-Erling Smørgrav 	}
365547dd1d1bSDag-Erling Smørgrav 	return r;
365647dd1d1bSDag-Erling Smørgrav }
365747dd1d1bSDag-Erling Smørgrav 
365847dd1d1bSDag-Erling Smørgrav u_int32_t
sshkey_signatures_left(const struct sshkey * k)365947dd1d1bSDag-Erling Smørgrav sshkey_signatures_left(const struct sshkey *k)
366047dd1d1bSDag-Erling Smørgrav {
366147dd1d1bSDag-Erling Smørgrav 	if (sshkey_type_plain(k->type) == KEY_XMSS)
366247dd1d1bSDag-Erling Smørgrav 		return sshkey_xmss_signatures_left(k);
366347dd1d1bSDag-Erling Smørgrav 	return 0;
366447dd1d1bSDag-Erling Smørgrav }
366547dd1d1bSDag-Erling Smørgrav 
366647dd1d1bSDag-Erling Smørgrav int
sshkey_enable_maxsign(struct sshkey * k,u_int32_t maxsign)366747dd1d1bSDag-Erling Smørgrav sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
366847dd1d1bSDag-Erling Smørgrav {
366947dd1d1bSDag-Erling Smørgrav 	if (sshkey_type_plain(k->type) != KEY_XMSS)
367047dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
367147dd1d1bSDag-Erling Smørgrav 	return sshkey_xmss_enable_maxsign(k, maxsign);
367247dd1d1bSDag-Erling Smørgrav }
367347dd1d1bSDag-Erling Smørgrav 
367447dd1d1bSDag-Erling Smørgrav int
sshkey_set_filename(struct sshkey * k,const char * filename)367547dd1d1bSDag-Erling Smørgrav sshkey_set_filename(struct sshkey *k, const char *filename)
367647dd1d1bSDag-Erling Smørgrav {
367747dd1d1bSDag-Erling Smørgrav 	if (k == NULL)
367847dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
367947dd1d1bSDag-Erling Smørgrav 	if (sshkey_type_plain(k->type) != KEY_XMSS)
368047dd1d1bSDag-Erling Smørgrav 		return 0;
368147dd1d1bSDag-Erling Smørgrav 	if (filename == NULL)
368247dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
368347dd1d1bSDag-Erling Smørgrav 	if ((k->xmss_filename = strdup(filename)) == NULL)
368447dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_ALLOC_FAIL;
368547dd1d1bSDag-Erling Smørgrav 	return 0;
368647dd1d1bSDag-Erling Smørgrav }
368747dd1d1bSDag-Erling Smørgrav #else
368847dd1d1bSDag-Erling Smørgrav int
sshkey_private_serialize_maxsign(struct sshkey * k,struct sshbuf * b,u_int32_t maxsign,int printerror)368919261079SEd Maste sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
369019261079SEd Maste     u_int32_t maxsign, int printerror)
369147dd1d1bSDag-Erling Smørgrav {
369247dd1d1bSDag-Erling Smørgrav 	return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
369347dd1d1bSDag-Erling Smørgrav }
369447dd1d1bSDag-Erling Smørgrav 
369547dd1d1bSDag-Erling Smørgrav u_int32_t
sshkey_signatures_left(const struct sshkey * k)369647dd1d1bSDag-Erling Smørgrav sshkey_signatures_left(const struct sshkey *k)
369747dd1d1bSDag-Erling Smørgrav {
369847dd1d1bSDag-Erling Smørgrav 	return 0;
369947dd1d1bSDag-Erling Smørgrav }
370047dd1d1bSDag-Erling Smørgrav 
370147dd1d1bSDag-Erling Smørgrav int
sshkey_enable_maxsign(struct sshkey * k,u_int32_t maxsign)370247dd1d1bSDag-Erling Smørgrav sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
370347dd1d1bSDag-Erling Smørgrav {
370447dd1d1bSDag-Erling Smørgrav 	return SSH_ERR_INVALID_ARGUMENT;
370547dd1d1bSDag-Erling Smørgrav }
370647dd1d1bSDag-Erling Smørgrav 
370747dd1d1bSDag-Erling Smørgrav int
sshkey_set_filename(struct sshkey * k,const char * filename)370847dd1d1bSDag-Erling Smørgrav sshkey_set_filename(struct sshkey *k, const char *filename)
370947dd1d1bSDag-Erling Smørgrav {
371047dd1d1bSDag-Erling Smørgrav 	if (k == NULL)
371147dd1d1bSDag-Erling Smørgrav 		return SSH_ERR_INVALID_ARGUMENT;
371247dd1d1bSDag-Erling Smørgrav 	return 0;
371347dd1d1bSDag-Erling Smørgrav }
371447dd1d1bSDag-Erling Smørgrav #endif /* WITH_XMSS */
3715