1 /* 2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 3 * 2013. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2013 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #ifndef OPENSSL_HEADER_X509_INTERNAL_H 60 #define OPENSSL_HEADER_X509_INTERNAL_H 61 62 #include <openssl/base.h> 63 #include <openssl/evp.h> 64 #include <openssl/x509.h> 65 66 #include "../asn1/internal.h" 67 68 #if defined(__cplusplus) 69 extern "C" { 70 #endif 71 72 73 // Internal structures. 74 75 typedef struct X509_val_st { 76 ASN1_TIME *notBefore; 77 ASN1_TIME *notAfter; 78 } X509_VAL; 79 80 DECLARE_ASN1_FUNCTIONS_const(X509_VAL) 81 82 struct X509_pubkey_st { 83 X509_ALGOR *algor; 84 ASN1_BIT_STRING *public_key; 85 EVP_PKEY *pkey; 86 } /* X509_PUBKEY */; 87 88 struct X509_name_entry_st { 89 ASN1_OBJECT *object; 90 ASN1_STRING *value; 91 int set; 92 } /* X509_NAME_ENTRY */; 93 94 // we always keep X509_NAMEs in 2 forms. 95 struct X509_name_st { 96 STACK_OF(X509_NAME_ENTRY) *entries; 97 int modified; // true if 'bytes' needs to be built 98 BUF_MEM *bytes; 99 // unsigned long hash; Keep the hash around for lookups 100 unsigned char *canon_enc; 101 int canon_enclen; 102 } /* X509_NAME */; 103 104 struct x509_attributes_st { 105 ASN1_OBJECT *object; 106 STACK_OF(ASN1_TYPE) *set; 107 } /* X509_ATTRIBUTE */; 108 109 typedef struct x509_cert_aux_st { 110 STACK_OF(ASN1_OBJECT) *trust; // trusted uses 111 STACK_OF(ASN1_OBJECT) *reject; // rejected uses 112 ASN1_UTF8STRING *alias; // "friendly name" 113 ASN1_OCTET_STRING *keyid; // key id of private key 114 } X509_CERT_AUX; 115 116 DECLARE_ASN1_FUNCTIONS_const(X509_CERT_AUX) 117 118 struct X509_extension_st { 119 ASN1_OBJECT *object; 120 ASN1_BOOLEAN critical; 121 ASN1_OCTET_STRING *value; 122 } /* X509_EXTENSION */; 123 124 typedef struct { 125 ASN1_INTEGER *version; // [ 0 ] default of v1 126 ASN1_INTEGER *serialNumber; 127 X509_ALGOR *signature; 128 X509_NAME *issuer; 129 X509_VAL *validity; 130 X509_NAME *subject; 131 X509_PUBKEY *key; 132 ASN1_BIT_STRING *issuerUID; // [ 1 ] optional in v2 133 ASN1_BIT_STRING *subjectUID; // [ 2 ] optional in v2 134 STACK_OF(X509_EXTENSION) *extensions; // [ 3 ] optional in v3 135 ASN1_ENCODING enc; 136 } X509_CINF; 137 138 // TODO(https://crbug.com/boringssl/407): This is not const because it contains 139 // an |X509_NAME|. 140 DECLARE_ASN1_FUNCTIONS(X509_CINF) 141 142 struct x509_st { 143 X509_CINF *cert_info; 144 X509_ALGOR *sig_alg; 145 ASN1_BIT_STRING *signature; 146 CRYPTO_refcount_t references; 147 CRYPTO_EX_DATA ex_data; 148 // These contain copies of various extension values 149 long ex_pathlen; 150 uint32_t ex_flags; 151 uint32_t ex_kusage; 152 uint32_t ex_xkusage; 153 uint32_t ex_nscert; 154 ASN1_OCTET_STRING *skid; 155 AUTHORITY_KEYID *akid; 156 STACK_OF(DIST_POINT) *crldp; 157 STACK_OF(GENERAL_NAME) *altname; 158 NAME_CONSTRAINTS *nc; 159 unsigned char cert_hash[SHA256_DIGEST_LENGTH]; 160 X509_CERT_AUX *aux; 161 CRYPTO_MUTEX lock; 162 } /* X509 */; 163 164 typedef struct { 165 ASN1_ENCODING enc; 166 ASN1_INTEGER *version; 167 X509_NAME *subject; 168 X509_PUBKEY *pubkey; 169 // d=2 hl=2 l= 0 cons: cont: 00 170 STACK_OF(X509_ATTRIBUTE) *attributes; // [ 0 ] 171 } X509_REQ_INFO; 172 173 // TODO(https://crbug.com/boringssl/407): This is not const because it contains 174 // an |X509_NAME|. 175 DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO) 176 177 struct X509_req_st { 178 X509_REQ_INFO *req_info; 179 X509_ALGOR *sig_alg; 180 ASN1_BIT_STRING *signature; 181 } /* X509_REQ */; 182 183 struct x509_revoked_st { 184 ASN1_INTEGER *serialNumber; 185 ASN1_TIME *revocationDate; 186 STACK_OF(X509_EXTENSION) /* optional */ *extensions; 187 // Set up if indirect CRL 188 STACK_OF(GENERAL_NAME) *issuer; 189 // Revocation reason 190 int reason; 191 } /* X509_REVOKED */; 192 193 typedef struct { 194 ASN1_INTEGER *version; 195 X509_ALGOR *sig_alg; 196 X509_NAME *issuer; 197 ASN1_TIME *lastUpdate; 198 ASN1_TIME *nextUpdate; 199 STACK_OF(X509_REVOKED) *revoked; 200 STACK_OF(X509_EXTENSION) /* [0] */ *extensions; 201 ASN1_ENCODING enc; 202 } X509_CRL_INFO; 203 204 // TODO(https://crbug.com/boringssl/407): This is not const because it contains 205 // an |X509_NAME|. 206 DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO) 207 208 struct X509_crl_st { 209 // actual signature 210 X509_CRL_INFO *crl; 211 X509_ALGOR *sig_alg; 212 ASN1_BIT_STRING *signature; 213 CRYPTO_refcount_t references; 214 int flags; 215 // Copies of various extensions 216 AUTHORITY_KEYID *akid; 217 ISSUING_DIST_POINT *idp; 218 // Convenient breakdown of IDP 219 int idp_flags; 220 int idp_reasons; 221 // CRL and base CRL numbers for delta processing 222 ASN1_INTEGER *crl_number; 223 ASN1_INTEGER *base_crl_number; 224 unsigned char crl_hash[SHA256_DIGEST_LENGTH]; 225 STACK_OF(GENERAL_NAMES) *issuers; 226 } /* X509_CRL */; 227 228 struct X509_VERIFY_PARAM_st { 229 char *name; 230 int64_t check_time; // POSIX time to use 231 unsigned long inh_flags; // Inheritance flags 232 unsigned long flags; // Various verify flags 233 int purpose; // purpose to check untrusted certificates 234 int trust; // trust setting to check 235 int depth; // Verify depth 236 STACK_OF(ASN1_OBJECT) *policies; // Permissible policies 237 // The following fields specify acceptable peer identities. 238 STACK_OF(OPENSSL_STRING) *hosts; // Set of acceptable names 239 unsigned int hostflags; // Flags to control matching features 240 char *peername; // Matching hostname in peer certificate 241 char *email; // If not NULL email address to match 242 size_t emaillen; 243 unsigned char *ip; // If not NULL IP address to match 244 size_t iplen; // Length of IP address 245 unsigned char poison; // Fail all verifications at name checking 246 } /* X509_VERIFY_PARAM */; 247 248 struct x509_object_st { 249 // one of the above types 250 int type; 251 union { 252 char *ptr; 253 X509 *x509; 254 X509_CRL *crl; 255 EVP_PKEY *pkey; 256 } data; 257 } /* X509_OBJECT */; 258 259 // This is a static that defines the function interface 260 struct x509_lookup_method_st { 261 const char *name; 262 int (*new_item)(X509_LOOKUP *ctx); 263 void (*free)(X509_LOOKUP *ctx); 264 int (*init)(X509_LOOKUP *ctx); 265 int (*shutdown)(X509_LOOKUP *ctx); 266 int (*ctrl)(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, 267 char **ret); 268 int (*get_by_subject)(X509_LOOKUP *ctx, int type, X509_NAME *name, 269 X509_OBJECT *ret); 270 } /* X509_LOOKUP_METHOD */; 271 272 // This is used to hold everything. It is used for all certificate 273 // validation. Once we have a certificate chain, the 'verify' 274 // function is then called to actually check the cert chain. 275 struct x509_store_st { 276 // The following is a cache of trusted certs 277 int cache; // if true, stash any hits 278 STACK_OF(X509_OBJECT) *objs; // Cache of all objects 279 CRYPTO_MUTEX objs_lock; 280 281 // These are external lookup methods 282 STACK_OF(X509_LOOKUP) *get_cert_methods; 283 284 X509_VERIFY_PARAM *param; 285 286 // Callbacks for various operations 287 X509_STORE_CTX_verify_fn verify; // called to verify a certificate 288 X509_STORE_CTX_verify_cb verify_cb; // error callback 289 X509_STORE_CTX_get_issuer_fn get_issuer; // get issuers cert from ctx 290 X509_STORE_CTX_check_issued_fn check_issued; // check issued 291 X509_STORE_CTX_check_revocation_fn 292 check_revocation; // Check revocation status of chain 293 X509_STORE_CTX_get_crl_fn get_crl; // retrieve CRL 294 X509_STORE_CTX_check_crl_fn check_crl; // Check CRL validity 295 X509_STORE_CTX_cert_crl_fn cert_crl; // Check certificate against CRL 296 X509_STORE_CTX_lookup_certs_fn lookup_certs; 297 X509_STORE_CTX_lookup_crls_fn lookup_crls; 298 X509_STORE_CTX_cleanup_fn cleanup; 299 300 CRYPTO_refcount_t references; 301 } /* X509_STORE */; 302 303 304 // This is the functions plus an instance of the local variables. 305 struct x509_lookup_st { 306 int init; // have we been started 307 int skip; // don't use us. 308 X509_LOOKUP_METHOD *method; // the functions 309 void *method_data; // method data 310 311 X509_STORE *store_ctx; // who owns us 312 } /* X509_LOOKUP */; 313 314 // This is a used when verifying cert chains. Since the 315 // gathering of the cert chain can take some time (and have to be 316 // 'retried', this needs to be kept and passed around. 317 struct x509_store_ctx_st { 318 X509_STORE *ctx; 319 320 // The following are set by the caller 321 X509 *cert; // The cert to check 322 STACK_OF(X509) *untrusted; // chain of X509s - untrusted - passed in 323 STACK_OF(X509_CRL) *crls; // set of CRLs passed in 324 325 X509_VERIFY_PARAM *param; 326 void *other_ctx; // Other info for use with get_issuer() 327 328 // Callbacks for various operations 329 X509_STORE_CTX_verify_fn verify; // called to verify a certificate 330 X509_STORE_CTX_verify_cb verify_cb; // error callback 331 X509_STORE_CTX_get_issuer_fn get_issuer; // get issuers cert from ctx 332 X509_STORE_CTX_check_issued_fn check_issued; // check issued 333 X509_STORE_CTX_check_revocation_fn 334 check_revocation; // Check revocation status of chain 335 X509_STORE_CTX_get_crl_fn get_crl; // retrieve CRL 336 X509_STORE_CTX_check_crl_fn check_crl; // Check CRL validity 337 X509_STORE_CTX_cert_crl_fn cert_crl; // Check certificate against CRL 338 X509_STORE_CTX_check_policy_fn check_policy; 339 X509_STORE_CTX_lookup_certs_fn lookup_certs; 340 X509_STORE_CTX_lookup_crls_fn lookup_crls; 341 X509_STORE_CTX_cleanup_fn cleanup; 342 343 // The following is built up 344 int valid; // if 0, rebuild chain 345 int last_untrusted; // index of last untrusted cert 346 STACK_OF(X509) *chain; // chain of X509s - built up and trusted 347 348 // When something goes wrong, this is why 349 int error_depth; 350 int error; 351 X509 *current_cert; 352 X509 *current_issuer; // cert currently being tested as valid issuer 353 X509_CRL *current_crl; // current CRL 354 355 int current_crl_score; // score of current CRL 356 unsigned int current_reasons; // Reason mask 357 358 X509_STORE_CTX *parent; // For CRL path validation: parent context 359 360 CRYPTO_EX_DATA ex_data; 361 } /* X509_STORE_CTX */; 362 363 ASN1_TYPE *ASN1_generate_v3(const char *str, const X509V3_CTX *cnf); 364 365 int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent); 366 367 368 // RSA-PSS functions. 369 370 // x509_rsa_pss_to_ctx configures |ctx| for an RSA-PSS operation based on 371 // signature algorithm parameters in |sigalg| (which must have type 372 // |NID_rsassaPss|) and key |pkey|. It returns one on success and zero on 373 // error. 374 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, 375 EVP_PKEY *pkey); 376 377 // x509_rsa_pss_to_ctx sets |algor| to the signature algorithm parameters for 378 // |ctx|, which must have been configured for an RSA-PSS signing operation. It 379 // returns one on success and zero on error. 380 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor); 381 382 // x509_print_rsa_pss_params prints a human-readable representation of RSA-PSS 383 // parameters in |sigalg| to |bp|. It returns one on success and zero on 384 // error. 385 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, 386 ASN1_PCTX *pctx); 387 388 389 // Signature algorithm functions. 390 391 // x509_digest_sign_algorithm encodes the signing parameters of |ctx| as an 392 // AlgorithmIdentifer and saves the result in |algor|. It returns one on 393 // success, or zero on error. 394 int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor); 395 396 // x509_digest_verify_init sets up |ctx| for a signature verification operation 397 // with public key |pkey| and parameters from |algor|. The |ctx| argument must 398 // have been initialised with |EVP_MD_CTX_init|. It returns one on success, or 399 // zero on error. 400 int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, 401 EVP_PKEY *pkey); 402 403 404 // Path-building functions. 405 406 // X509_policy_check checks certificate policies in |certs|. |user_policies| is 407 // the user-initial-policy-set. If |user_policies| is NULL or empty, it is 408 // interpreted as anyPolicy. |flags| is a set of |X509_V_FLAG_*| values to 409 // apply. It returns |X509_V_OK| on success and |X509_V_ERR_*| on error. It 410 // additionally sets |*out_current_cert| to the certificate where the error 411 // occurred. If the function succeeded, or the error applies to the entire 412 // chain, it sets |*out_current_cert| to NULL. 413 int X509_policy_check(const STACK_OF(X509) *certs, 414 const STACK_OF(ASN1_OBJECT) *user_policies, 415 unsigned long flags, X509 **out_current_cert); 416 417 418 #if defined(__cplusplus) 419 } // extern C 420 #endif 421 422 #endif // OPENSSL_HEADER_X509_INTERNAL_H 423