sshkey.h 12 KB

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