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