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 #if defined(__cplusplus) 67 extern "C" { 68 #endif 69 70 71 /* Internal structures. */ 72 73 struct X509_val_st { 74 ASN1_TIME *notBefore; 75 ASN1_TIME *notAfter; 76 } /* X509_VAL */; 77 78 struct X509_pubkey_st { 79 X509_ALGOR *algor; 80 ASN1_BIT_STRING *public_key; 81 EVP_PKEY *pkey; 82 } /* X509_PUBKEY */; 83 84 struct X509_name_entry_st { 85 ASN1_OBJECT *object; 86 ASN1_STRING *value; 87 int set; 88 } /* X509_NAME_ENTRY */; 89 90 // we always keep X509_NAMEs in 2 forms. 91 struct X509_name_st { 92 STACK_OF(X509_NAME_ENTRY) *entries; 93 int modified; // true if 'bytes' needs to be built 94 BUF_MEM *bytes; 95 // unsigned long hash; Keep the hash around for lookups 96 unsigned char *canon_enc; 97 int canon_enclen; 98 } /* X509_NAME */; 99 100 struct x509_attributes_st { 101 ASN1_OBJECT *object; 102 STACK_OF(ASN1_TYPE) *set; 103 } /* X509_ATTRIBUTE */; 104 105 struct x509_cert_aux_st { 106 STACK_OF(ASN1_OBJECT) *trust; // trusted uses 107 STACK_OF(ASN1_OBJECT) *reject; // rejected uses 108 ASN1_UTF8STRING *alias; // "friendly name" 109 ASN1_OCTET_STRING *keyid; // key id of private key 110 STACK_OF(X509_ALGOR) *other; // other unspecified info 111 } /* X509_CERT_AUX */; 112 113 struct X509_extension_st { 114 ASN1_OBJECT *object; 115 ASN1_BOOLEAN critical; 116 ASN1_OCTET_STRING *value; 117 } /* X509_EXTENSION */; 118 119 typedef struct { 120 ASN1_INTEGER *version; // [ 0 ] default of v1 121 ASN1_INTEGER *serialNumber; 122 X509_ALGOR *signature; 123 X509_NAME *issuer; 124 X509_VAL *validity; 125 X509_NAME *subject; 126 X509_PUBKEY *key; 127 ASN1_BIT_STRING *issuerUID; // [ 1 ] optional in v2 128 ASN1_BIT_STRING *subjectUID; // [ 2 ] optional in v2 129 STACK_OF(X509_EXTENSION) *extensions; // [ 3 ] optional in v3 130 ASN1_ENCODING enc; 131 } X509_CINF; 132 133 DECLARE_ASN1_FUNCTIONS(X509_CINF) 134 135 struct x509_st { 136 X509_CINF *cert_info; 137 X509_ALGOR *sig_alg; 138 ASN1_BIT_STRING *signature; 139 CRYPTO_refcount_t references; 140 CRYPTO_EX_DATA ex_data; 141 // These contain copies of various extension values 142 long ex_pathlen; 143 long ex_pcpathlen; 144 unsigned long ex_flags; 145 unsigned long ex_kusage; 146 unsigned long ex_xkusage; 147 unsigned long ex_nscert; 148 ASN1_OCTET_STRING *skid; 149 AUTHORITY_KEYID *akid; 150 X509_POLICY_CACHE *policy_cache; 151 STACK_OF(DIST_POINT) *crldp; 152 STACK_OF(GENERAL_NAME) *altname; 153 NAME_CONSTRAINTS *nc; 154 unsigned char sha1_hash[SHA_DIGEST_LENGTH]; 155 X509_CERT_AUX *aux; 156 CRYPTO_BUFFER *buf; 157 CRYPTO_MUTEX lock; 158 } /* X509 */; 159 160 typedef struct { 161 ASN1_ENCODING enc; 162 ASN1_INTEGER *version; 163 X509_NAME *subject; 164 X509_PUBKEY *pubkey; 165 // d=2 hl=2 l= 0 cons: cont: 00 166 STACK_OF(X509_ATTRIBUTE) *attributes; // [ 0 ] 167 } X509_REQ_INFO; 168 169 DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO) 170 171 struct X509_req_st { 172 X509_REQ_INFO *req_info; 173 X509_ALGOR *sig_alg; 174 ASN1_BIT_STRING *signature; 175 CRYPTO_refcount_t references; 176 } /* X509_REQ */; 177 178 typedef struct { 179 ASN1_INTEGER *version; 180 X509_ALGOR *sig_alg; 181 X509_NAME *issuer; 182 ASN1_TIME *lastUpdate; 183 ASN1_TIME *nextUpdate; 184 STACK_OF(X509_REVOKED) *revoked; 185 STACK_OF(X509_EXTENSION) /* [0] */ *extensions; 186 ASN1_ENCODING enc; 187 } X509_CRL_INFO; 188 189 DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO) 190 191 struct X509_crl_st { 192 // actual signature 193 X509_CRL_INFO *crl; 194 X509_ALGOR *sig_alg; 195 ASN1_BIT_STRING *signature; 196 CRYPTO_refcount_t references; 197 int flags; 198 // Copies of various extensions 199 AUTHORITY_KEYID *akid; 200 ISSUING_DIST_POINT *idp; 201 // Convenient breakdown of IDP 202 int idp_flags; 203 int idp_reasons; 204 // CRL and base CRL numbers for delta processing 205 ASN1_INTEGER *crl_number; 206 ASN1_INTEGER *base_crl_number; 207 unsigned char sha1_hash[SHA_DIGEST_LENGTH]; 208 STACK_OF(GENERAL_NAMES) *issuers; 209 const X509_CRL_METHOD *meth; 210 void *meth_data; 211 } /* X509_CRL */; 212 213 struct X509_VERIFY_PARAM_st { 214 char *name; 215 time_t check_time; // Time to use 216 unsigned long inh_flags; // Inheritance flags 217 unsigned long flags; // Various verify flags 218 int purpose; // purpose to check untrusted certificates 219 int trust; // trust setting to check 220 int depth; // Verify depth 221 STACK_OF(ASN1_OBJECT) *policies; // Permissible policies 222 // The following fields specify acceptable peer identities. 223 STACK_OF(OPENSSL_STRING) *hosts; // Set of acceptable names 224 unsigned int hostflags; // Flags to control matching features 225 char *peername; // Matching hostname in peer certificate 226 char *email; // If not NULL email address to match 227 size_t emaillen; 228 unsigned char *ip; // If not NULL IP address to match 229 size_t iplen; // Length of IP address 230 unsigned char poison; // Fail all verifications at name checking 231 } /* X509_VERIFY_PARAM */; 232 233 struct x509_object_st { 234 // one of the above types 235 int type; 236 union { 237 char *ptr; 238 X509 *x509; 239 X509_CRL *crl; 240 EVP_PKEY *pkey; 241 } data; 242 } /* X509_OBJECT */; 243 244 // This is a static that defines the function interface 245 struct x509_lookup_method_st { 246 const char *name; 247 int (*new_item)(X509_LOOKUP *ctx); 248 void (*free)(X509_LOOKUP *ctx); 249 int (*init)(X509_LOOKUP *ctx); 250 int (*shutdown)(X509_LOOKUP *ctx); 251 int (*ctrl)(X509_LOOKUP *ctx, int cmd, const char *argc, long argl, 252 char **ret); 253 int (*get_by_subject)(X509_LOOKUP *ctx, int type, X509_NAME *name, 254 X509_OBJECT *ret); 255 int (*get_by_issuer_serial)(X509_LOOKUP *ctx, int type, X509_NAME *name, 256 ASN1_INTEGER *serial, X509_OBJECT *ret); 257 int (*get_by_fingerprint)(X509_LOOKUP *ctx, int type, unsigned char *bytes, 258 int len, X509_OBJECT *ret); 259 int (*get_by_alias)(X509_LOOKUP *ctx, int type, char *str, int len, 260 X509_OBJECT *ret); 261 } /* X509_LOOKUP_METHOD */; 262 263 // This is used to hold everything. It is used for all certificate 264 // validation. Once we have a certificate chain, the 'verify' 265 // function is then called to actually check the cert chain. 266 struct x509_store_st { 267 // The following is a cache of trusted certs 268 int cache; // if true, stash any hits 269 STACK_OF(X509_OBJECT) *objs; // Cache of all objects 270 CRYPTO_MUTEX objs_lock; 271 272 // These are external lookup methods 273 STACK_OF(X509_LOOKUP) *get_cert_methods; 274 275 X509_VERIFY_PARAM *param; 276 277 // Callbacks for various operations 278 X509_STORE_CTX_verify_fn verify; // called to verify a certificate 279 X509_STORE_CTX_verify_cb verify_cb; // error callback 280 X509_STORE_CTX_get_issuer_fn get_issuer; // get issuers cert from ctx 281 X509_STORE_CTX_check_issued_fn check_issued; // check issued 282 X509_STORE_CTX_check_revocation_fn 283 check_revocation; // Check revocation status of chain 284 X509_STORE_CTX_get_crl_fn get_crl; // retrieve CRL 285 X509_STORE_CTX_check_crl_fn check_crl; // Check CRL validity 286 X509_STORE_CTX_cert_crl_fn cert_crl; // Check certificate against CRL 287 X509_STORE_CTX_lookup_certs_fn lookup_certs; 288 X509_STORE_CTX_lookup_crls_fn lookup_crls; 289 X509_STORE_CTX_cleanup_fn cleanup; 290 291 CRYPTO_refcount_t references; 292 } /* X509_STORE */; 293 294 295 // This is the functions plus an instance of the local variables. 296 struct x509_lookup_st { 297 int init; // have we been started 298 int skip; // don't use us. 299 X509_LOOKUP_METHOD *method; // the functions 300 char *method_data; // method data 301 302 X509_STORE *store_ctx; // who owns us 303 } /* X509_LOOKUP */; 304 305 // This is a used when verifying cert chains. Since the 306 // gathering of the cert chain can take some time (and have to be 307 // 'retried', this needs to be kept and passed around. 308 struct x509_store_ctx_st { 309 X509_STORE *ctx; 310 311 // The following are set by the caller 312 X509 *cert; // The cert to check 313 STACK_OF(X509) *untrusted; // chain of X509s - untrusted - passed in 314 STACK_OF(X509_CRL) *crls; // set of CRLs passed in 315 316 X509_VERIFY_PARAM *param; 317 void *other_ctx; // Other info for use with get_issuer() 318 319 // Callbacks for various operations 320 X509_STORE_CTX_verify_fn verify; // called to verify a certificate 321 X509_STORE_CTX_verify_cb verify_cb; // error callback 322 X509_STORE_CTX_get_issuer_fn get_issuer; // get issuers cert from ctx 323 X509_STORE_CTX_check_issued_fn check_issued; // check issued 324 X509_STORE_CTX_check_revocation_fn 325 check_revocation; // Check revocation status of chain 326 X509_STORE_CTX_get_crl_fn get_crl; // retrieve CRL 327 X509_STORE_CTX_check_crl_fn check_crl; // Check CRL validity 328 X509_STORE_CTX_cert_crl_fn cert_crl; // Check certificate against CRL 329 X509_STORE_CTX_check_policy_fn check_policy; 330 X509_STORE_CTX_lookup_certs_fn lookup_certs; 331 X509_STORE_CTX_lookup_crls_fn lookup_crls; 332 X509_STORE_CTX_cleanup_fn cleanup; 333 334 // The following is built up 335 int valid; // if 0, rebuild chain 336 int last_untrusted; // index of last untrusted cert 337 STACK_OF(X509) *chain; // chain of X509s - built up and trusted 338 X509_POLICY_TREE *tree; // Valid policy tree 339 340 int explicit_policy; // Require explicit policy value 341 342 // When something goes wrong, this is why 343 int error_depth; 344 int error; 345 X509 *current_cert; 346 X509 *current_issuer; // cert currently being tested as valid issuer 347 X509_CRL *current_crl; // current CRL 348 349 int current_crl_score; // score of current CRL 350 unsigned int current_reasons; // Reason mask 351 352 X509_STORE_CTX *parent; // For CRL path validation: parent context 353 354 CRYPTO_EX_DATA ex_data; 355 } /* X509_STORE_CTX */; 356 357 358 /* RSA-PSS functions. */ 359 360 /* x509_rsa_pss_to_ctx configures |ctx| for an RSA-PSS operation based on 361 * signature algorithm parameters in |sigalg| (which must have type 362 * |NID_rsassaPss|) and key |pkey|. It returns one on success and zero on 363 * error. */ 364 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, 365 EVP_PKEY *pkey); 366 367 /* x509_rsa_pss_to_ctx sets |algor| to the signature algorithm parameters for 368 * |ctx|, which must have been configured for an RSA-PSS signing operation. It 369 * returns one on success and zero on error. */ 370 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor); 371 372 /* x509_print_rsa_pss_params prints a human-readable representation of RSA-PSS 373 * parameters in |sigalg| to |bp|. It returns one on success and zero on 374 * error. */ 375 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent, 376 ASN1_PCTX *pctx); 377 378 379 /* Signature algorithm functions. */ 380 381 /* x509_digest_sign_algorithm encodes the signing parameters of |ctx| as an 382 * AlgorithmIdentifer and saves the result in |algor|. It returns one on 383 * success, or zero on error. */ 384 int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor); 385 386 /* x509_digest_verify_init sets up |ctx| for a signature verification operation 387 * with public key |pkey| and parameters from |algor|. The |ctx| argument must 388 * have been initialised with |EVP_MD_CTX_init|. It returns one on success, or 389 * zero on error. */ 390 int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, 391 EVP_PKEY *pkey); 392 393 394 #if defined(__cplusplus) 395 } /* extern C */ 396 #endif 397 398 #endif /* OPENSSL_HEADER_X509_INTERNAL_H */ 399