xref: /openbsd-src/usr.bin/ssh/sshkey.h (revision dcc91c2622318df8f66a9bca2d2864253df1bfc3)
1 /* $OpenBSD: sshkey.h,v 1.64 2024/08/15 00:51:51 djm Exp $ */
2 
3 /*
4  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef SSHKEY_H
27 #define SSHKEY_H
28 
29 #include <sys/types.h>
30 
31 #ifdef WITH_OPENSSL
32 #include <openssl/rsa.h>
33 #include <openssl/dsa.h>
34 #include <openssl/ec.h>
35 #include <openssl/ecdsa.h>
36 #include <openssl/evp.h>
37 #define SSH_OPENSSL_VERSION OpenSSL_version(OPENSSL_VERSION)
38 #else /* OPENSSL */
39 #define BIGNUM		void
40 #define RSA		void
41 #define DSA		void
42 #define EC_KEY		void
43 #define EC_GROUP	void
44 #define EC_POINT	void
45 #define EVP_PKEY	void
46 #define SSH_OPENSSL_VERSION "without OpenSSL"
47 #endif /* WITH_OPENSSL */
48 
49 #define SSH_RSA_MINIMUM_MODULUS_SIZE	1024
50 #define SSH_KEY_MAX_SIGN_DATA_SIZE	(1 << 20)
51 
52 struct sshbuf;
53 
54 /* Key types */
55 enum sshkey_types {
56 	KEY_RSA,
57 	KEY_DSA,
58 	KEY_ECDSA,
59 	KEY_ED25519,
60 	KEY_RSA_CERT,
61 	KEY_DSA_CERT,
62 	KEY_ECDSA_CERT,
63 	KEY_ED25519_CERT,
64 	KEY_XMSS,
65 	KEY_XMSS_CERT,
66 	KEY_ECDSA_SK,
67 	KEY_ECDSA_SK_CERT,
68 	KEY_ED25519_SK,
69 	KEY_ED25519_SK_CERT,
70 	KEY_UNSPEC
71 };
72 
73 /* Default fingerprint hash */
74 #define SSH_FP_HASH_DEFAULT	SSH_DIGEST_SHA256
75 
76 /* Fingerprint representation formats */
77 enum sshkey_fp_rep {
78 	SSH_FP_DEFAULT = 0,
79 	SSH_FP_HEX,
80 	SSH_FP_BASE64,
81 	SSH_FP_BUBBLEBABBLE,
82 	SSH_FP_RANDOMART
83 };
84 
85 /* Private key serialisation formats, used on the wire */
86 enum sshkey_serialize_rep {
87 	SSHKEY_SERIALIZE_DEFAULT = 0,
88 	SSHKEY_SERIALIZE_STATE = 1,	/* only state is serialized */
89 	SSHKEY_SERIALIZE_FULL = 2,	/* include keys for saving to disk */
90 	SSHKEY_SERIALIZE_SHIELD = 3,	/* everything, for encrypting in ram */
91 	SSHKEY_SERIALIZE_INFO = 254,	/* minimal information */
92 };
93 
94 /* Private key disk formats */
95 enum sshkey_private_format {
96 	SSHKEY_PRIVATE_OPENSSH = 0,
97 	SSHKEY_PRIVATE_PEM = 1,
98 	SSHKEY_PRIVATE_PKCS8 = 2,
99 };
100 
101 /* key is stored in external hardware */
102 #define SSHKEY_FLAG_EXT		0x0001
103 
104 #define SSHKEY_CERT_MAX_PRINCIPALS	256
105 /* XXX opaquify? */
106 struct sshkey_cert {
107 	struct sshbuf	*certblob; /* Kept around for use on wire */
108 	u_int		 type; /* SSH2_CERT_TYPE_USER or SSH2_CERT_TYPE_HOST */
109 	u_int64_t	 serial;
110 	char		*key_id;
111 	u_int		 nprincipals;
112 	char		**principals;
113 	u_int64_t	 valid_after, valid_before;
114 	struct sshbuf	*critical;
115 	struct sshbuf	*extensions;
116 	struct sshkey	*signature_key;
117 	char		*signature_type;
118 };
119 
120 /* XXX opaquify? */
121 struct sshkey {
122 	int	 type;
123 	int	 flags;
124 	/* KEY_DSA */
125 	DSA	*dsa;
126 	/* KEY_ECDSA and KEY_ECDSA_SK */
127 	int	 ecdsa_nid;	/* NID of curve */
128 	/* libcrypto-backed keys */
129 	EVP_PKEY *pkey;
130 	/* KEY_ED25519 and KEY_ED25519_SK */
131 	u_char	*ed25519_sk;
132 	u_char	*ed25519_pk;
133 	/* KEY_XMSS */
134 	char	*xmss_name;
135 	char	*xmss_filename;	/* for state file updates */
136 	void	*xmss_state;	/* depends on xmss_name, opaque */
137 	u_char	*xmss_sk;
138 	u_char	*xmss_pk;
139 	/* KEY_ECDSA_SK and KEY_ED25519_SK */
140 	char	*sk_application;
141 	uint8_t	sk_flags;
142 	struct sshbuf *sk_key_handle;
143 	struct sshbuf *sk_reserved;
144 	/* Certificates */
145 	struct sshkey_cert *cert;
146 	/* Private key shielding */
147 	u_char	*shielded_private;
148 	size_t	shielded_len;
149 	u_char	*shield_prekey;
150 	size_t	shield_prekey_len;
151 };
152 
153 #define	ED25519_SK_SZ	crypto_sign_ed25519_SECRETKEYBYTES
154 #define	ED25519_PK_SZ	crypto_sign_ed25519_PUBLICKEYBYTES
155 
156 /* Additional fields contained in signature */
157 struct sshkey_sig_details {
158 	uint32_t sk_counter;	/* U2F signature counter */
159 	uint8_t sk_flags;	/* U2F signature flags; see ssh-sk.h */
160 };
161 
162 struct sshkey_impl_funcs {
163 	u_int (*size)(const struct sshkey *);	/* optional */
164 	int (*alloc)(struct sshkey *);		/* optional */
165 	void (*cleanup)(struct sshkey *);	/* optional */
166 	int (*equal)(const struct sshkey *, const struct sshkey *);
167 	int (*serialize_public)(const struct sshkey *, struct sshbuf *,
168 	    enum sshkey_serialize_rep);
169 	int (*deserialize_public)(const char *, struct sshbuf *,
170 	    struct sshkey *);
171 	int (*serialize_private)(const struct sshkey *, struct sshbuf *,
172 	    enum sshkey_serialize_rep);
173 	int (*deserialize_private)(const char *, struct sshbuf *,
174 	    struct sshkey *);
175 	int (*generate)(struct sshkey *, int);	/* optional */
176 	int (*copy_public)(const struct sshkey *, struct sshkey *);
177 	int (*sign)(struct sshkey *, u_char **, size_t *,
178 	    const u_char *, size_t, const char *,
179 	    const char *, const char *, u_int); /* optional */
180 	int (*verify)(const struct sshkey *, const u_char *, size_t,
181 	    const u_char *, size_t, const char *, u_int,
182 	    struct sshkey_sig_details **);
183 };
184 
185 struct sshkey_impl {
186 	const char *name;
187 	const char *shortname;
188 	const char *sigalg;
189 	int type;
190 	int nid;
191 	int cert;
192 	int sigonly;
193 	int keybits;
194 	const struct sshkey_impl_funcs *funcs;
195 };
196 
197 struct sshkey	*sshkey_new(int);
198 void		 sshkey_free(struct sshkey *);
199 int		 sshkey_equal_public(const struct sshkey *,
200     const struct sshkey *);
201 int		 sshkey_equal(const struct sshkey *, const struct sshkey *);
202 char		*sshkey_fingerprint(const struct sshkey *,
203     int, enum sshkey_fp_rep);
204 int		 sshkey_fingerprint_raw(const struct sshkey *k,
205     int, u_char **retp, size_t *lenp);
206 const char	*sshkey_type(const struct sshkey *);
207 const char	*sshkey_cert_type(const struct sshkey *);
208 int		 sshkey_format_text(const struct sshkey *, struct sshbuf *);
209 int		 sshkey_write(const struct sshkey *, FILE *);
210 int		 sshkey_read(struct sshkey *, char **);
211 u_int		 sshkey_size(const struct sshkey *);
212 
213 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
214 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
215 
216 int		 sshkey_is_shielded(struct sshkey *);
217 int		 sshkey_shield_private(struct sshkey *);
218 int		 sshkey_unshield_private(struct sshkey *);
219 
220 int	 sshkey_type_from_name(const char *);
221 int	 sshkey_is_cert(const struct sshkey *);
222 int	 sshkey_is_sk(const struct sshkey *);
223 int	 sshkey_type_is_cert(int);
224 int	 sshkey_type_plain(int);
225 
226 /* Returns non-zero if key name match sigalgs pattern list. (handles RSA) */
227 int	 sshkey_match_keyname_to_sigalgs(const char *, const char *);
228 
229 int	 sshkey_to_certified(struct sshkey *);
230 int	 sshkey_drop_cert(struct sshkey *);
231 int	 sshkey_cert_copy(const struct sshkey *, struct sshkey *);
232 int	 sshkey_cert_check_authority(const struct sshkey *, int, int, int,
233     uint64_t, const char *, const char **);
234 int	 sshkey_cert_check_authority_now(const struct sshkey *, int, int, int,
235     const char *, const char **);
236 int	 sshkey_cert_check_host(const struct sshkey *, const char *,
237     int , const char *, const char **);
238 size_t	 sshkey_format_cert_validity(const struct sshkey_cert *,
239     char *, size_t) __attribute__((__bounded__(__string__, 2, 3)));
240 int	 sshkey_check_cert_sigtype(const struct sshkey *, const char *);
241 
242 int	 sshkey_certify(struct sshkey *, struct sshkey *,
243     const char *, const char *, const char *);
244 /* Variant allowing use of a custom signature function (e.g. for ssh-agent) */
245 typedef int sshkey_certify_signer(struct sshkey *, u_char **, size_t *,
246     const u_char *, size_t, const char *, const char *, const char *,
247     u_int, void *);
248 int	 sshkey_certify_custom(struct sshkey *, struct sshkey *, const char *,
249     const char *, const char *, sshkey_certify_signer *, void *);
250 
251 int		 sshkey_ecdsa_nid_from_name(const char *);
252 int		 sshkey_curve_name_to_nid(const char *);
253 const char *	 sshkey_curve_nid_to_name(int);
254 u_int		 sshkey_curve_nid_to_bits(int);
255 int		 sshkey_ecdsa_bits_to_nid(int);
256 int		 sshkey_ecdsa_key_to_nid(const EC_KEY *);
257 int		 sshkey_ecdsa_pkey_to_nid(EVP_PKEY *);
258 int		 sshkey_ec_nid_to_hash_alg(int nid);
259 int		 sshkey_ec_validate_public(const EC_GROUP *, const EC_POINT *);
260 int		 sshkey_ec_validate_private(const EC_KEY *);
261 const char	*sshkey_ssh_name(const struct sshkey *);
262 const char	*sshkey_ssh_name_plain(const struct sshkey *);
263 int		 sshkey_names_valid2(const char *, int, int);
264 char		*sshkey_alg_list(int, int, int, char);
265 
266 int	 sshkey_from_blob(const u_char *, size_t, struct sshkey **);
267 int	 sshkey_fromb(struct sshbuf *, struct sshkey **);
268 int	 sshkey_froms(struct sshbuf *, struct sshkey **);
269 int	 sshkey_to_blob(const struct sshkey *, u_char **, size_t *);
270 int	 sshkey_to_base64(const struct sshkey *, char **);
271 int	 sshkey_putb(const struct sshkey *, struct sshbuf *);
272 int	 sshkey_puts(const struct sshkey *, struct sshbuf *);
273 int	 sshkey_puts_opts(const struct sshkey *, struct sshbuf *,
274     enum sshkey_serialize_rep);
275 int	 sshkey_plain_to_blob(const struct sshkey *, u_char **, size_t *);
276 int	 sshkey_putb_plain(const struct sshkey *, struct sshbuf *);
277 
278 int	 sshkey_sign(struct sshkey *, u_char **, size_t *,
279     const u_char *, size_t, const char *, const char *, const char *, u_int);
280 int	 sshkey_verify(const struct sshkey *, const u_char *, size_t,
281     const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **);
282 int	 sshkey_check_sigtype(const u_char *, size_t, const char *);
283 const char *sshkey_sigalg_by_name(const char *);
284 int	 sshkey_get_sigtype(const u_char *, size_t, char **);
285 
286 /* Signing and verification backend for libcrypto-backed keys */
287 int	sshkey_pkey_digest_sign(EVP_PKEY*, int, u_char **,
288     size_t *, const u_char *, size_t);
289 int	sshkey_pkey_digest_verify(EVP_PKEY *, int, const u_char *,
290     size_t, u_char *, size_t);
291 
292 /* for debug */
293 void	sshkey_dump_ec_point(const EC_GROUP *, const EC_POINT *);
294 void	sshkey_dump_ec_key(const EC_KEY *);
295 
296 /* private key parsing and serialisation */
297 int	sshkey_private_serialize(struct sshkey *key, struct sshbuf *buf);
298 int	sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
299     enum sshkey_serialize_rep);
300 int	sshkey_private_deserialize(struct sshbuf *buf,  struct sshkey **keyp);
301 
302 /* private key file format parsing and serialisation */
303 int	sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
304     const char *passphrase, const char *comment,
305     int format, const char *openssh_format_cipher, int openssh_format_rounds);
306 int	sshkey_parse_private_fileblob(struct sshbuf *buffer,
307     const char *passphrase, struct sshkey **keyp, char **commentp);
308 int	sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
309     const char *passphrase, struct sshkey **keyp, char **commentp);
310 int	sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob,
311     int type, struct sshkey **pubkeyp);
312 
313 int sshkey_check_rsa_length(const struct sshkey *, int);
314 /* XXX should be internal, but used by ssh-keygen */
315 int ssh_rsa_complete_crt_parameters(const BIGNUM *, const BIGNUM *,
316     const BIGNUM *, const BIGNUM *, BIGNUM **, BIGNUM **);
317 
318 /* stateful keys (e.g. XMSS) */
319 int	 sshkey_set_filename(struct sshkey *, const char *);
320 int	 sshkey_enable_maxsign(struct sshkey *, u_int32_t);
321 u_int32_t sshkey_signatures_left(const struct sshkey *);
322 int	 sshkey_private_serialize_maxsign(struct sshkey *key,
323     struct sshbuf *buf, u_int32_t maxsign, int);
324 
325 void	 sshkey_sig_details_free(struct sshkey_sig_details *);
326 
327 #ifdef WITH_OPENSSL
328 int	sshkey_ecdsa_fixup_group(EVP_PKEY *k); /* ssh-ecdsa.c */
329 #endif
330 
331 #ifdef SSHKEY_INTERNAL
332 int	sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b);
333 void	sshkey_sk_cleanup(struct sshkey *k);
334 int	sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b);
335 int	sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to);
336 int	sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key);
337 int	sshkey_serialize_private_sk(const struct sshkey *key,
338     struct sshbuf *buf);
339 int	sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k);
340 #ifdef WITH_OPENSSL
341 int	check_rsa_length(const RSA *rsa); /* XXX remove */
342 #endif
343 #endif
344 
345 #ifndef WITH_OPENSSL
346 #undef RSA
347 #undef DSA
348 #undef EC_KEY
349 #undef EC_GROUP
350 #undef EC_POINT
351 #undef EVP_PKEY
352 #endif /* WITH_OPENSSL */
353 
354 #endif /* SSHKEY_H */
355