x509.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_X509_H
  10. # define OSSL_CRYPTO_X509_H
  11. # pragma once
  12. # include "internal/refcount.h"
  13. # include <openssl/asn1.h>
  14. # include <openssl/x509.h>
  15. # include <openssl/conf.h>
  16. # include "crypto/types.h"
  17. /* Internal X509 structures and functions: not for application use */
  18. /* Note: unless otherwise stated a field pointer is mandatory and should
  19. * never be set to NULL: the ASN.1 code and accessors rely on mandatory
  20. * fields never being NULL.
  21. */
  22. /*
  23. * name entry structure, equivalent to AttributeTypeAndValue defined
  24. * in RFC5280 et al.
  25. */
  26. struct X509_name_entry_st {
  27. ASN1_OBJECT *object; /* AttributeType */
  28. ASN1_STRING *value; /* AttributeValue */
  29. int set; /* index of RDNSequence for this entry */
  30. int size; /* temp variable */
  31. };
  32. /* Name from RFC 5280. */
  33. struct X509_name_st {
  34. STACK_OF(X509_NAME_ENTRY) *entries; /* DN components */
  35. int modified; /* true if 'bytes' needs to be built */
  36. BUF_MEM *bytes; /* cached encoding: cannot be NULL */
  37. /* canonical encoding used for rapid Name comparison */
  38. unsigned char *canon_enc;
  39. int canon_enclen;
  40. } /* X509_NAME */ ;
  41. /* Signature info structure */
  42. struct x509_sig_info_st {
  43. /* NID of message digest */
  44. int mdnid;
  45. /* NID of public key algorithm */
  46. int pknid;
  47. /* Security bits */
  48. int secbits;
  49. /* Various flags */
  50. uint32_t flags;
  51. };
  52. /* PKCS#10 certificate request */
  53. struct X509_req_info_st {
  54. ASN1_ENCODING enc; /* cached encoding of signed part */
  55. ASN1_INTEGER *version; /* version, defaults to v1(0) so can be NULL */
  56. X509_NAME *subject; /* certificate request DN */
  57. X509_PUBKEY *pubkey; /* public key of request */
  58. /*
  59. * Zero or more attributes.
  60. * NB: although attributes is a mandatory field some broken
  61. * encodings omit it so this may be NULL in that case.
  62. */
  63. STACK_OF(X509_ATTRIBUTE) *attributes;
  64. };
  65. struct X509_req_st {
  66. X509_REQ_INFO req_info; /* signed certificate request data */
  67. X509_ALGOR sig_alg; /* signature algorithm */
  68. ASN1_BIT_STRING *signature; /* signature */
  69. CRYPTO_REF_COUNT references;
  70. CRYPTO_RWLOCK *lock;
  71. /* Set on live certificates for authentication purposes */
  72. ASN1_OCTET_STRING *distinguishing_id;
  73. OSSL_LIB_CTX *libctx;
  74. char *propq;
  75. };
  76. struct X509_crl_info_st {
  77. ASN1_INTEGER *version; /* version: defaults to v1(0) so may be NULL */
  78. X509_ALGOR sig_alg; /* signature algorithm */
  79. X509_NAME *issuer; /* CRL issuer name */
  80. ASN1_TIME *lastUpdate; /* lastUpdate field */
  81. ASN1_TIME *nextUpdate; /* nextUpdate field: optional */
  82. STACK_OF(X509_REVOKED) *revoked; /* revoked entries: optional */
  83. STACK_OF(X509_EXTENSION) *extensions; /* extensions: optional */
  84. ASN1_ENCODING enc; /* encoding of signed portion of CRL */
  85. };
  86. struct X509_crl_st {
  87. X509_CRL_INFO crl; /* signed CRL data */
  88. X509_ALGOR sig_alg; /* CRL signature algorithm */
  89. ASN1_BIT_STRING signature; /* CRL signature */
  90. CRYPTO_REF_COUNT references;
  91. int flags;
  92. /*
  93. * Cached copies of decoded extension values, since extensions
  94. * are optional any of these can be NULL.
  95. */
  96. AUTHORITY_KEYID *akid;
  97. ISSUING_DIST_POINT *idp;
  98. /* Convenient breakdown of IDP */
  99. int idp_flags;
  100. int idp_reasons;
  101. /* CRL and base CRL numbers for delta processing */
  102. ASN1_INTEGER *crl_number;
  103. ASN1_INTEGER *base_crl_number;
  104. STACK_OF(GENERAL_NAMES) *issuers;
  105. /* hash of CRL */
  106. unsigned char sha1_hash[SHA_DIGEST_LENGTH];
  107. /* alternative method to handle this CRL */
  108. const X509_CRL_METHOD *meth;
  109. void *meth_data;
  110. CRYPTO_RWLOCK *lock;
  111. OSSL_LIB_CTX *libctx;
  112. char *propq;
  113. };
  114. struct x509_revoked_st {
  115. ASN1_INTEGER serialNumber; /* revoked entry serial number */
  116. ASN1_TIME *revocationDate; /* revocation date */
  117. STACK_OF(X509_EXTENSION) *extensions; /* CRL entry extensions: optional */
  118. /* decoded value of CRLissuer extension: set if indirect CRL */
  119. STACK_OF(GENERAL_NAME) *issuer;
  120. /* revocation reason: set to CRL_REASON_NONE if reason extension absent */
  121. int reason;
  122. /*
  123. * CRL entries are reordered for faster lookup of serial numbers. This
  124. * field contains the original load sequence for this entry.
  125. */
  126. int sequence;
  127. };
  128. /*
  129. * This stuff is certificate "auxiliary info": it contains details which are
  130. * useful in certificate stores and databases. When used this is tagged onto
  131. * the end of the certificate itself. OpenSSL specific structure not defined
  132. * in any RFC.
  133. */
  134. struct x509_cert_aux_st {
  135. STACK_OF(ASN1_OBJECT) *trust; /* trusted uses */
  136. STACK_OF(ASN1_OBJECT) *reject; /* rejected uses */
  137. ASN1_UTF8STRING *alias; /* "friendly name" */
  138. ASN1_OCTET_STRING *keyid; /* key id of private key */
  139. STACK_OF(X509_ALGOR) *other; /* other unspecified info */
  140. };
  141. struct x509_cinf_st {
  142. ASN1_INTEGER *version; /* [ 0 ] default of v1 */
  143. ASN1_INTEGER serialNumber;
  144. X509_ALGOR signature;
  145. X509_NAME *issuer;
  146. X509_VAL validity;
  147. X509_NAME *subject;
  148. X509_PUBKEY *key;
  149. ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
  150. ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
  151. STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */
  152. ASN1_ENCODING enc;
  153. };
  154. struct x509_st {
  155. X509_CINF cert_info;
  156. X509_ALGOR sig_alg;
  157. ASN1_BIT_STRING signature;
  158. X509_SIG_INFO siginf;
  159. CRYPTO_REF_COUNT references;
  160. CRYPTO_EX_DATA ex_data;
  161. /* These contain copies of various extension values */
  162. long ex_pathlen;
  163. long ex_pcpathlen;
  164. uint32_t ex_flags;
  165. uint32_t ex_kusage;
  166. uint32_t ex_xkusage;
  167. uint32_t ex_nscert;
  168. ASN1_OCTET_STRING *skid;
  169. AUTHORITY_KEYID *akid;
  170. X509_POLICY_CACHE *policy_cache;
  171. STACK_OF(DIST_POINT) *crldp;
  172. STACK_OF(GENERAL_NAME) *altname;
  173. NAME_CONSTRAINTS *nc;
  174. # ifndef OPENSSL_NO_RFC3779
  175. STACK_OF(IPAddressFamily) *rfc3779_addr;
  176. struct ASIdentifiers_st *rfc3779_asid;
  177. # endif
  178. unsigned char sha1_hash[SHA_DIGEST_LENGTH];
  179. X509_CERT_AUX *aux;
  180. CRYPTO_RWLOCK *lock;
  181. volatile int ex_cached;
  182. /* Set on live certificates for authentication purposes */
  183. ASN1_OCTET_STRING *distinguishing_id;
  184. OSSL_LIB_CTX *libctx;
  185. char *propq;
  186. } /* X509 */ ;
  187. /*
  188. * This is a used when verifying cert chains. Since the gathering of the
  189. * cert chain can take some time (and have to be 'retried', this needs to be
  190. * kept and passed around.
  191. */
  192. struct x509_store_ctx_st { /* X509_STORE_CTX */
  193. X509_STORE *store;
  194. /* The following are set by the caller */
  195. /* The cert to check */
  196. X509 *cert;
  197. /* chain of X509s - untrusted - passed in */
  198. STACK_OF(X509) *untrusted;
  199. /* set of CRLs passed in */
  200. STACK_OF(X509_CRL) *crls;
  201. X509_VERIFY_PARAM *param;
  202. /* Other info for use with get_issuer() */
  203. void *other_ctx;
  204. /* Callbacks for various operations */
  205. /* called to verify a certificate */
  206. int (*verify) (X509_STORE_CTX *ctx);
  207. /* error callback */
  208. int (*verify_cb) (int ok, X509_STORE_CTX *ctx);
  209. /* get issuers cert from ctx */
  210. int (*get_issuer) (X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
  211. /* check issued */
  212. int (*check_issued) (X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  213. /* Check revocation status of chain */
  214. int (*check_revocation) (X509_STORE_CTX *ctx);
  215. /* retrieve CRL */
  216. int (*get_crl) (X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x);
  217. /* Check CRL validity */
  218. int (*check_crl) (X509_STORE_CTX *ctx, X509_CRL *crl);
  219. /* Check certificate against CRL */
  220. int (*cert_crl) (X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
  221. /* Check policy status of the chain */
  222. int (*check_policy) (X509_STORE_CTX *ctx);
  223. STACK_OF(X509) *(*lookup_certs) (X509_STORE_CTX *ctx,
  224. const X509_NAME *nm);
  225. /* cannot constify 'ctx' param due to lookup_certs_sk() in x509_vfy.c */
  226. STACK_OF(X509_CRL) *(*lookup_crls) (const X509_STORE_CTX *ctx,
  227. const X509_NAME *nm);
  228. int (*cleanup) (X509_STORE_CTX *ctx);
  229. /* The following is built up */
  230. /* if 0, rebuild chain */
  231. int valid;
  232. /* number of untrusted certs */
  233. int num_untrusted;
  234. /* chain of X509s - built up and trusted */
  235. STACK_OF(X509) *chain;
  236. /* Valid policy tree */
  237. X509_POLICY_TREE *tree;
  238. /* Require explicit policy value */
  239. int explicit_policy;
  240. /* When something goes wrong, this is why */
  241. int error_depth;
  242. int error;
  243. X509 *current_cert;
  244. /* cert currently being tested as valid issuer */
  245. X509 *current_issuer;
  246. /* current CRL */
  247. X509_CRL *current_crl;
  248. /* score of current CRL */
  249. int current_crl_score;
  250. /* Reason mask */
  251. unsigned int current_reasons;
  252. /* For CRL path validation: parent context */
  253. X509_STORE_CTX *parent;
  254. CRYPTO_EX_DATA ex_data;
  255. SSL_DANE *dane;
  256. /* signed via bare TA public key, rather than CA certificate */
  257. int bare_ta_signed;
  258. /* Raw Public Key */
  259. EVP_PKEY *rpk;
  260. OSSL_LIB_CTX *libctx;
  261. char *propq;
  262. };
  263. /* PKCS#8 private key info structure */
  264. struct pkcs8_priv_key_info_st {
  265. ASN1_INTEGER *version;
  266. X509_ALGOR *pkeyalg;
  267. ASN1_OCTET_STRING *pkey;
  268. STACK_OF(X509_ATTRIBUTE) *attributes;
  269. };
  270. struct X509_sig_st {
  271. X509_ALGOR *algor;
  272. ASN1_OCTET_STRING *digest;
  273. };
  274. struct x509_object_st {
  275. /* one of the above types */
  276. X509_LOOKUP_TYPE type;
  277. union {
  278. char *ptr;
  279. X509 *x509;
  280. X509_CRL *crl;
  281. EVP_PKEY *pkey;
  282. } data;
  283. };
  284. int ossl_a2i_ipadd(unsigned char *ipout, const char *ipasc);
  285. int ossl_x509_set1_time(int *modified, ASN1_TIME **ptm, const ASN1_TIME *tm);
  286. int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags);
  287. int ossl_x509v3_cache_extensions(X509 *x);
  288. int ossl_x509_init_sig_info(X509 *x);
  289. int ossl_x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq);
  290. int ossl_x509_crl_set0_libctx(X509_CRL *x, OSSL_LIB_CTX *libctx,
  291. const char *propq);
  292. int ossl_x509_req_set0_libctx(X509_REQ *x, OSSL_LIB_CTX *libctx,
  293. const char *propq);
  294. int ossl_asn1_item_digest_ex(const ASN1_ITEM *it, const EVP_MD *type,
  295. void *data, unsigned char *md, unsigned int *len,
  296. OSSL_LIB_CTX *libctx, const char *propq);
  297. int ossl_x509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags);
  298. int ossl_x509_add_certs_new(STACK_OF(X509) **p_sk, STACK_OF(X509) *certs,
  299. int flags);
  300. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x);
  301. int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
  302. const X509_PUBKEY *key);
  303. /* Calculate default key identifier according to RFC 5280 section 4.2.1.2 (1) */
  304. ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey);
  305. X509_PUBKEY *ossl_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp,
  306. long len, OSSL_LIB_CTX *libctx,
  307. const char *propq);
  308. void ossl_X509_PUBKEY_INTERNAL_free(X509_PUBKEY *xpub);
  309. RSA *ossl_d2i_RSA_PSS_PUBKEY(RSA **a, const unsigned char **pp, long length);
  310. int ossl_i2d_RSA_PSS_PUBKEY(const RSA *a, unsigned char **pp);
  311. # ifndef OPENSSL_NO_DSA
  312. DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length);
  313. # endif /* OPENSSL_NO_DSA */
  314. # ifndef OPENSSL_NO_DH
  315. DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length);
  316. int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp);
  317. DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length);
  318. int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp);
  319. # endif /* OPENSSL_NO_DH */
  320. # ifndef OPENSSL_NO_EC
  321. ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
  322. const unsigned char **pp, long length);
  323. int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp);
  324. ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
  325. const unsigned char **pp, long length);
  326. int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp);
  327. ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
  328. const unsigned char **pp, long length);
  329. int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp);
  330. ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
  331. const unsigned char **pp, long length);
  332. int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp);
  333. # endif /* OPENSSL_NO_EC */
  334. EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
  335. long length);
  336. int ossl_x509_check_private_key(const EVP_PKEY *k, const EVP_PKEY *pkey);
  337. int x509v3_add_len_value_uchar(const char *name, const unsigned char *value,
  338. size_t vallen, STACK_OF(CONF_VALUE) **extlist);
  339. #endif /* OSSL_CRYPTO_X509_H */