x509_int.h 9.2 KB

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