1/* $NetBSD: sshkey.h,v 1.19 2023/10/25 20:19:57 christos Exp $ */ 2/* $OpenBSD: sshkey.h,v 1.62 2023/06/21 05:10:26 djm Exp $ */ 3 4/* 5 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27#ifndef SSHKEY_H 28#define SSHKEY_H 29 30#include "includes.h" 31#include <sys/types.h> 32 33#ifdef WITH_OPENSSL 34#include <openssl/rsa.h> 35#include <openssl/dsa.h> 36#include <openssl/ec.h> 37#include <openssl/ecdsa.h> 38#define SSH_OPENSSL_VERSION OpenSSL_version(OPENSSL_VERSION) 39#else /* OPENSSL */ 40#define BIGNUM void 41#define RSA void 42#define DSA void 43#define EC_KEY void 44#define EC_GROUP void 45#define EC_POINT 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 52struct sshbuf; 53 54/* Key types */ 55enum 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 */ 77enum 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 */ 86enum 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 */ 95enum 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? */ 106struct 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? */ 121struct sshkey { 122 int type; 123 int flags; 124 /* KEY_RSA */ 125 RSA *rsa; 126 /* KEY_DSA */ 127 DSA *dsa; 128 /* KEY_ECDSA and KEY_ECDSA_SK */ 129 int ecdsa_nid; /* NID of curve */ 130 EC_KEY *ecdsa; 131 /* KEY_ED25519 and KEY_ED25519_SK */ 132 u_char *ed25519_sk; 133 u_char *ed25519_pk; 134 /* KEY_XMSS */ 135 char *xmss_name; 136 char *xmss_filename; /* for state file updates */ 137 void *xmss_state; /* depends on xmss_name, opaque */ 138 u_char *xmss_sk; 139 u_char *xmss_pk; 140 /* KEY_ECDSA_SK and KEY_ED25519_SK */ 141 char *sk_application; 142 uint8_t sk_flags; 143 struct sshbuf *sk_key_handle; 144 struct sshbuf *sk_reserved; 145 /* Certificates */ 146 struct sshkey_cert *cert; 147 /* Private key shielding */ 148 u_char *shielded_private; 149 size_t shielded_len; 150 u_char *shield_prekey; 151 size_t shield_prekey_len; 152}; 153 154#define ED25519_SK_SZ crypto_sign_ed25519_SECRETKEYBYTES 155#define ED25519_PK_SZ crypto_sign_ed25519_PUBLICKEYBYTES 156 157/* Additional fields contained in signature */ 158struct sshkey_sig_details { 159 uint32_t sk_counter; /* U2F signature counter */ 160 uint8_t sk_flags; /* U2F signature flags; see ssh-sk.h */ 161}; 162 163struct sshkey_impl_funcs { 164 u_int (*size)(const struct sshkey *); /* optional */ 165 int (*alloc)(struct sshkey *); /* optional */ 166 void (*cleanup)(struct sshkey *); /* optional */ 167 int (*equal)(const struct sshkey *, const struct sshkey *); 168 int (*serialize_public)(const struct sshkey *, struct sshbuf *, 169 enum sshkey_serialize_rep); 170 int (*deserialize_public)(const char *, struct sshbuf *, 171 struct sshkey *); 172 int (*serialize_private)(const struct sshkey *, struct sshbuf *, 173 enum sshkey_serialize_rep); 174 int (*deserialize_private)(const char *, struct sshbuf *, 175 struct sshkey *); 176 int (*generate)(struct sshkey *, int); /* optional */ 177 int (*copy_public)(const struct sshkey *, struct sshkey *); 178 int (*sign)(struct sshkey *, u_char **, size_t *, 179 const u_char *, size_t, const char *, 180 const char *, const char *, u_int); /* optional */ 181 int (*verify)(const struct sshkey *, const u_char *, size_t, 182 const u_char *, size_t, const char *, u_int, 183 struct sshkey_sig_details **); 184}; 185 186struct sshkey_impl { 187 const char *name; 188 const char *shortname; 189 const char *sigalg; 190 int type; 191 int nid; 192 int cert; 193 int sigonly; 194 int keybits; 195 const struct sshkey_impl_funcs *funcs; 196}; 197 198struct sshkey *sshkey_new(int); 199void sshkey_free(struct sshkey *); 200int sshkey_equal_public(const struct sshkey *, 201 const struct sshkey *); 202int sshkey_equal(const struct sshkey *, const struct sshkey *); 203char *sshkey_fingerprint(const struct sshkey *, 204 int, enum sshkey_fp_rep); 205int sshkey_fingerprint_raw(const struct sshkey *k, 206 int, u_char **retp, size_t *lenp); 207const char *sshkey_type(const struct sshkey *); 208const char *sshkey_cert_type(const struct sshkey *); 209int sshkey_format_text(const struct sshkey *, struct sshbuf *); 210int sshkey_write(const struct sshkey *, FILE *); 211int sshkey_read(struct sshkey *, char **); 212u_int sshkey_size(const struct sshkey *); 213 214int sshkey_generate(int type, u_int bits, struct sshkey **keyp); 215int sshkey_from_private(const struct sshkey *, struct sshkey **); 216 217int sshkey_is_shielded(struct sshkey *); 218int sshkey_shield_private(struct sshkey *); 219int sshkey_unshield_private(struct sshkey *); 220 221int sshkey_type_from_name(const char *); 222int sshkey_is_cert(const struct sshkey *); 223int sshkey_is_sk(const struct sshkey *); 224int sshkey_type_is_cert(int); 225int sshkey_type_plain(int); 226 227/* Returns non-zero if key name match sigalgs pattern list. (handles RSA) */ 228int sshkey_match_keyname_to_sigalgs(const char *, const char *); 229 230int sshkey_to_certified(struct sshkey *); 231int sshkey_drop_cert(struct sshkey *); 232int sshkey_cert_copy(const struct sshkey *, struct sshkey *); 233int sshkey_cert_check_authority(const struct sshkey *, int, int, int, 234 uint64_t, const char *, const char **); 235int sshkey_cert_check_authority_now(const struct sshkey *, int, int, int, 236 const char *, const char **); 237int sshkey_cert_check_host(const struct sshkey *, const char *, 238 int , const char *, const char **); 239size_t sshkey_format_cert_validity(const struct sshkey_cert *, 240 char *, size_t) __attribute__((__bounded__(__string__, 2, 3))); 241int sshkey_check_cert_sigtype(const struct sshkey *, const char *); 242 243int sshkey_certify(struct sshkey *, struct sshkey *, 244 const char *, const char *, const char *); 245/* Variant allowing use of a custom signature function (e.g. for ssh-agent) */ 246typedef int sshkey_certify_signer(struct sshkey *, u_char **, size_t *, 247 const u_char *, size_t, const char *, const char *, const char *, 248 u_int, void *); 249int sshkey_certify_custom(struct sshkey *, struct sshkey *, const char *, 250 const char *, const char *, sshkey_certify_signer *, void *); 251 252int sshkey_ecdsa_nid_from_name(const char *); 253int sshkey_curve_name_to_nid(const char *); 254const char * sshkey_curve_nid_to_name(int); 255u_int sshkey_curve_nid_to_bits(int); 256int sshkey_ecdsa_bits_to_nid(int); 257int sshkey_ecdsa_key_to_nid(EC_KEY *); 258int sshkey_ec_nid_to_hash_alg(int nid); 259int sshkey_ec_validate_public(const EC_GROUP *, const EC_POINT *); 260int sshkey_ec_validate_private(const EC_KEY *); 261const char *sshkey_ssh_name(const struct sshkey *); 262const char *sshkey_ssh_name_plain(const struct sshkey *); 263int sshkey_names_valid2(const char *, int, int); 264char *sshkey_alg_list(int, int, int, char); 265 266int sshkey_from_blob(const u_char *, size_t, struct sshkey **); 267int sshkey_fromb(struct sshbuf *, struct sshkey **); 268int sshkey_froms(struct sshbuf *, struct sshkey **); 269int sshkey_to_blob(const struct sshkey *, u_char **, size_t *); 270int sshkey_to_base64(const struct sshkey *, char **); 271int sshkey_putb(const struct sshkey *, struct sshbuf *); 272int sshkey_puts(const struct sshkey *, struct sshbuf *); 273int sshkey_puts_opts(const struct sshkey *, struct sshbuf *, 274 enum sshkey_serialize_rep); 275int sshkey_plain_to_blob(const struct sshkey *, u_char **, size_t *); 276int sshkey_putb_plain(const struct sshkey *, struct sshbuf *); 277 278int sshkey_sign(struct sshkey *, u_char **, size_t *, 279 const u_char *, size_t, const char *, const char *, const char *, u_int); 280int sshkey_verify(const struct sshkey *, const u_char *, size_t, 281 const u_char *, size_t, const char *, u_int, struct sshkey_sig_details **); 282int sshkey_check_sigtype(const u_char *, size_t, const char *); 283const char *sshkey_sigalg_by_name(const char *); 284int sshkey_get_sigtype(const u_char *, size_t, char **); 285 286/* for debug */ 287void sshkey_dump_ec_point(const EC_GROUP *, const EC_POINT *); 288void sshkey_dump_ec_key(const EC_KEY *); 289 290/* private key parsing and serialisation */ 291int sshkey_private_serialize(struct sshkey *key, struct sshbuf *buf); 292int sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf, 293 enum sshkey_serialize_rep); 294int sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **keyp); 295 296/* private key file format parsing and serialisation */ 297int sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob, 298 const char *passphrase, const char *comment, 299 int format, const char *openssh_format_cipher, int openssh_format_rounds); 300int sshkey_parse_private_fileblob(struct sshbuf *buffer, 301 const char *passphrase, struct sshkey **keyp, char **commentp); 302int sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type, 303 const char *passphrase, struct sshkey **keyp, char **commentp); 304int sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, 305 int type, struct sshkey **pubkeyp); 306 307int sshkey_check_rsa_length(const struct sshkey *, int); 308/* XXX should be internal, but used by ssh-keygen */ 309int ssh_rsa_complete_crt_parameters(struct sshkey *, const BIGNUM *); 310 311/* stateful keys (e.g. XMSS) */ 312int sshkey_set_filename(struct sshkey *, const char *); 313int sshkey_enable_maxsign(struct sshkey *, u_int32_t); 314u_int32_t sshkey_signatures_left(const struct sshkey *); 315int sshkey_forward_state(const struct sshkey *, u_int32_t, int); 316int sshkey_private_serialize_maxsign(struct sshkey *key, 317 struct sshbuf *buf, u_int32_t maxsign, int); 318 319void sshkey_sig_details_free(struct sshkey_sig_details *); 320 321#ifdef SSHKEY_INTERNAL 322int sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b); 323void sshkey_sk_cleanup(struct sshkey *k); 324int sshkey_serialize_sk(const struct sshkey *key, struct sshbuf *b); 325int sshkey_copy_public_sk(const struct sshkey *from, struct sshkey *to); 326int sshkey_deserialize_sk(struct sshbuf *b, struct sshkey *key); 327int sshkey_serialize_private_sk(const struct sshkey *key, 328 struct sshbuf *buf); 329int sshkey_private_deserialize_sk(struct sshbuf *buf, struct sshkey *k); 330#ifdef WITH_OPENSSL 331int check_rsa_length(const RSA *rsa); /* XXX remove */ 332#endif 333#endif 334 335#ifndef WITH_OPENSSL 336#undef RSA 337#undef DSA 338#undef EC_KEY 339#undef EC_GROUP 340#undef EC_POINT 341#endif /* WITH_OPENSSL */ 342 343#endif /* SSHKEY_H */ 344