x509.h 11 KB

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