1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include "internal/deprecated.h" 11e1051a39Sopenharmony_ci 12e1051a39Sopenharmony_ci#include <stdio.h> 13e1051a39Sopenharmony_ci#include <time.h> 14e1051a39Sopenharmony_ci#include <errno.h> 15e1051a39Sopenharmony_ci#include <limits.h> 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ci#include "crypto/ctype.h" 18e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 19e1051a39Sopenharmony_ci#include <openssl/crypto.h> 20e1051a39Sopenharmony_ci#include <openssl/buffer.h> 21e1051a39Sopenharmony_ci#include <openssl/evp.h> 22e1051a39Sopenharmony_ci#include <openssl/asn1.h> 23e1051a39Sopenharmony_ci#include <openssl/x509.h> 24e1051a39Sopenharmony_ci#include <openssl/x509v3.h> 25e1051a39Sopenharmony_ci#include <openssl/objects.h> 26e1051a39Sopenharmony_ci#include <openssl/core_names.h> 27e1051a39Sopenharmony_ci#include "internal/dane.h" 28e1051a39Sopenharmony_ci#include "crypto/x509.h" 29e1051a39Sopenharmony_ci#include "x509_local.h" 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci/* CRL score values */ 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_ci#define CRL_SCORE_NOCRITICAL 0x100 /* No unhandled critical extensions */ 34e1051a39Sopenharmony_ci#define CRL_SCORE_SCOPE 0x080 /* certificate is within CRL scope */ 35e1051a39Sopenharmony_ci#define CRL_SCORE_TIME 0x040 /* CRL times valid */ 36e1051a39Sopenharmony_ci#define CRL_SCORE_ISSUER_NAME 0x020 /* Issuer name matches certificate */ 37e1051a39Sopenharmony_ci#define CRL_SCORE_VALID /* If this score or above CRL is probably valid */ \ 38e1051a39Sopenharmony_ci (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE) 39e1051a39Sopenharmony_ci#define CRL_SCORE_ISSUER_CERT 0x018 /* CRL issuer is certificate issuer */ 40e1051a39Sopenharmony_ci#define CRL_SCORE_SAME_PATH 0x008 /* CRL issuer is on certificate path */ 41e1051a39Sopenharmony_ci#define CRL_SCORE_AKID 0x004 /* CRL issuer matches CRL AKID */ 42e1051a39Sopenharmony_ci#define CRL_SCORE_TIME_DELTA 0x002 /* Have a delta CRL with valid times */ 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_cistatic int build_chain(X509_STORE_CTX *ctx); 45e1051a39Sopenharmony_cistatic int verify_chain(X509_STORE_CTX *ctx); 46e1051a39Sopenharmony_cistatic int dane_verify(X509_STORE_CTX *ctx); 47e1051a39Sopenharmony_cistatic int null_callback(int ok, X509_STORE_CTX *e); 48e1051a39Sopenharmony_cistatic int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer); 49e1051a39Sopenharmony_cistatic X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); 50e1051a39Sopenharmony_cistatic int check_extensions(X509_STORE_CTX *ctx); 51e1051a39Sopenharmony_cistatic int check_name_constraints(X509_STORE_CTX *ctx); 52e1051a39Sopenharmony_cistatic int check_id(X509_STORE_CTX *ctx); 53e1051a39Sopenharmony_cistatic int check_trust(X509_STORE_CTX *ctx, int num_untrusted); 54e1051a39Sopenharmony_cistatic int check_revocation(X509_STORE_CTX *ctx); 55e1051a39Sopenharmony_cistatic int check_cert(X509_STORE_CTX *ctx); 56e1051a39Sopenharmony_cistatic int check_policy(X509_STORE_CTX *ctx); 57e1051a39Sopenharmony_cistatic int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x); 58e1051a39Sopenharmony_cistatic int check_dane_issuer(X509_STORE_CTX *ctx, int depth); 59e1051a39Sopenharmony_cistatic int check_key_level(X509_STORE_CTX *ctx, X509 *cert); 60e1051a39Sopenharmony_cistatic int check_sig_level(X509_STORE_CTX *ctx, X509 *cert); 61e1051a39Sopenharmony_cistatic int check_curve(X509 *cert); 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_cistatic int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, 64e1051a39Sopenharmony_ci unsigned int *preasons, X509_CRL *crl, X509 *x); 65e1051a39Sopenharmony_cistatic int get_crl_delta(X509_STORE_CTX *ctx, 66e1051a39Sopenharmony_ci X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x); 67e1051a39Sopenharmony_cistatic void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, 68e1051a39Sopenharmony_ci int *pcrl_score, X509_CRL *base, 69e1051a39Sopenharmony_ci STACK_OF(X509_CRL) *crls); 70e1051a39Sopenharmony_cistatic void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, 71e1051a39Sopenharmony_ci int *pcrl_score); 72e1051a39Sopenharmony_cistatic int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, 73e1051a39Sopenharmony_ci unsigned int *preasons); 74e1051a39Sopenharmony_cistatic int check_crl_path(X509_STORE_CTX *ctx, X509 *x); 75e1051a39Sopenharmony_cistatic int check_crl_chain(X509_STORE_CTX *ctx, 76e1051a39Sopenharmony_ci STACK_OF(X509) *cert_path, 77e1051a39Sopenharmony_ci STACK_OF(X509) *crl_path); 78e1051a39Sopenharmony_ci 79e1051a39Sopenharmony_cistatic int internal_verify(X509_STORE_CTX *ctx); 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_cistatic int null_callback(int ok, X509_STORE_CTX *e) 82e1051a39Sopenharmony_ci{ 83e1051a39Sopenharmony_ci return ok; 84e1051a39Sopenharmony_ci} 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ci/*- 87e1051a39Sopenharmony_ci * Return 1 if given cert is considered self-signed, 0 if not, or -1 on error. 88e1051a39Sopenharmony_ci * This actually verifies self-signedness only if requested. 89e1051a39Sopenharmony_ci * It calls ossl_x509v3_cache_extensions() 90e1051a39Sopenharmony_ci * to match issuer and subject names (i.e., the cert being self-issued) and any 91e1051a39Sopenharmony_ci * present authority key identifier to match the subject key identifier, etc. 92e1051a39Sopenharmony_ci */ 93e1051a39Sopenharmony_ciint X509_self_signed(X509 *cert, int verify_signature) 94e1051a39Sopenharmony_ci{ 95e1051a39Sopenharmony_ci EVP_PKEY *pkey; 96e1051a39Sopenharmony_ci 97e1051a39Sopenharmony_ci if ((pkey = X509_get0_pubkey(cert)) == NULL) { /* handles cert == NULL */ 98e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); 99e1051a39Sopenharmony_ci return -1; 100e1051a39Sopenharmony_ci } 101e1051a39Sopenharmony_ci if (!ossl_x509v3_cache_extensions(cert)) 102e1051a39Sopenharmony_ci return -1; 103e1051a39Sopenharmony_ci if ((cert->ex_flags & EXFLAG_SS) == 0) 104e1051a39Sopenharmony_ci return 0; 105e1051a39Sopenharmony_ci if (!verify_signature) 106e1051a39Sopenharmony_ci return 1; 107e1051a39Sopenharmony_ci return X509_verify(cert, pkey); 108e1051a39Sopenharmony_ci} 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci/* 111e1051a39Sopenharmony_ci * Given a certificate, try and find an exact match in the store. 112e1051a39Sopenharmony_ci * Returns 1 on success, 0 on not found, -1 on internal error. 113e1051a39Sopenharmony_ci */ 114e1051a39Sopenharmony_cistatic int lookup_cert_match(X509 **result, X509_STORE_CTX *ctx, X509 *x) 115e1051a39Sopenharmony_ci{ 116e1051a39Sopenharmony_ci STACK_OF(X509) *certs; 117e1051a39Sopenharmony_ci X509 *xtmp = NULL; 118e1051a39Sopenharmony_ci int i, ret; 119e1051a39Sopenharmony_ci 120e1051a39Sopenharmony_ci *result = NULL; 121e1051a39Sopenharmony_ci /* Lookup all certs with matching subject name */ 122e1051a39Sopenharmony_ci ERR_set_mark(); 123e1051a39Sopenharmony_ci certs = ctx->lookup_certs(ctx, X509_get_subject_name(x)); 124e1051a39Sopenharmony_ci ERR_pop_to_mark(); 125e1051a39Sopenharmony_ci if (certs == NULL) 126e1051a39Sopenharmony_ci return -1; 127e1051a39Sopenharmony_ci /* Look for exact match */ 128e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(certs); i++) { 129e1051a39Sopenharmony_ci xtmp = sk_X509_value(certs, i); 130e1051a39Sopenharmony_ci if (X509_cmp(xtmp, x) == 0) 131e1051a39Sopenharmony_ci break; 132e1051a39Sopenharmony_ci xtmp = NULL; 133e1051a39Sopenharmony_ci } 134e1051a39Sopenharmony_ci ret = xtmp != NULL; 135e1051a39Sopenharmony_ci if (ret) { 136e1051a39Sopenharmony_ci if (!X509_up_ref(xtmp)) 137e1051a39Sopenharmony_ci ret = -1; 138e1051a39Sopenharmony_ci else 139e1051a39Sopenharmony_ci *result = xtmp; 140e1051a39Sopenharmony_ci } 141e1051a39Sopenharmony_ci sk_X509_pop_free(certs, X509_free); 142e1051a39Sopenharmony_ci return ret; 143e1051a39Sopenharmony_ci} 144e1051a39Sopenharmony_ci 145e1051a39Sopenharmony_ci/*- 146e1051a39Sopenharmony_ci * Inform the verify callback of an error. 147e1051a39Sopenharmony_ci * The error code is set to |err| if |err| is not X509_V_OK, else 148e1051a39Sopenharmony_ci * |ctx->error| is left unchanged (under the assumption it is set elsewhere). 149e1051a39Sopenharmony_ci * The error depth is |depth| if >= 0, else it defaults to |ctx->error_depth|. 150e1051a39Sopenharmony_ci * The error cert is |x| if not NULL, else defaults to the chain cert at depth. 151e1051a39Sopenharmony_ci * 152e1051a39Sopenharmony_ci * Returns 0 to abort verification with an error, non-zero to continue. 153e1051a39Sopenharmony_ci */ 154e1051a39Sopenharmony_cistatic int verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err) 155e1051a39Sopenharmony_ci{ 156e1051a39Sopenharmony_ci if (depth < 0) 157e1051a39Sopenharmony_ci depth = ctx->error_depth; 158e1051a39Sopenharmony_ci else 159e1051a39Sopenharmony_ci ctx->error_depth = depth; 160e1051a39Sopenharmony_ci ctx->current_cert = (x != NULL) ? x : sk_X509_value(ctx->chain, depth); 161e1051a39Sopenharmony_ci if (err != X509_V_OK) 162e1051a39Sopenharmony_ci ctx->error = err; 163e1051a39Sopenharmony_ci return ctx->verify_cb(0, ctx); 164e1051a39Sopenharmony_ci} 165e1051a39Sopenharmony_ci 166e1051a39Sopenharmony_ci#define CB_FAIL_IF(cond, ctx, cert, depth, err) \ 167e1051a39Sopenharmony_ci if ((cond) && verify_cb_cert(ctx, cert, depth, err) == 0) \ 168e1051a39Sopenharmony_ci return 0 169e1051a39Sopenharmony_ci 170e1051a39Sopenharmony_ci/*- 171e1051a39Sopenharmony_ci * Inform the verify callback of an error, CRL-specific variant. Here, the 172e1051a39Sopenharmony_ci * error depth and certificate are already set, we just specify the error 173e1051a39Sopenharmony_ci * number. 174e1051a39Sopenharmony_ci * 175e1051a39Sopenharmony_ci * Returns 0 to abort verification with an error, non-zero to continue. 176e1051a39Sopenharmony_ci */ 177e1051a39Sopenharmony_cistatic int verify_cb_crl(X509_STORE_CTX *ctx, int err) 178e1051a39Sopenharmony_ci{ 179e1051a39Sopenharmony_ci ctx->error = err; 180e1051a39Sopenharmony_ci return ctx->verify_cb(0, ctx); 181e1051a39Sopenharmony_ci} 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_cistatic int check_auth_level(X509_STORE_CTX *ctx) 184e1051a39Sopenharmony_ci{ 185e1051a39Sopenharmony_ci int i; 186e1051a39Sopenharmony_ci int num = sk_X509_num(ctx->chain); 187e1051a39Sopenharmony_ci 188e1051a39Sopenharmony_ci if (ctx->param->auth_level <= 0) 189e1051a39Sopenharmony_ci return 1; 190e1051a39Sopenharmony_ci 191e1051a39Sopenharmony_ci for (i = 0; i < num; ++i) { 192e1051a39Sopenharmony_ci X509 *cert = sk_X509_value(ctx->chain, i); 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci /* 195e1051a39Sopenharmony_ci * We've already checked the security of the leaf key, so here we only 196e1051a39Sopenharmony_ci * check the security of issuer keys. 197e1051a39Sopenharmony_ci */ 198e1051a39Sopenharmony_ci CB_FAIL_IF(i > 0 && !check_key_level(ctx, cert), 199e1051a39Sopenharmony_ci ctx, cert, i, X509_V_ERR_CA_KEY_TOO_SMALL); 200e1051a39Sopenharmony_ci /* 201e1051a39Sopenharmony_ci * We also check the signature algorithm security of all certificates 202e1051a39Sopenharmony_ci * except those of the trust anchor at index num-1. 203e1051a39Sopenharmony_ci */ 204e1051a39Sopenharmony_ci CB_FAIL_IF(i < num - 1 && !check_sig_level(ctx, cert), 205e1051a39Sopenharmony_ci ctx, cert, i, X509_V_ERR_CA_MD_TOO_WEAK); 206e1051a39Sopenharmony_ci } 207e1051a39Sopenharmony_ci return 1; 208e1051a39Sopenharmony_ci} 209e1051a39Sopenharmony_ci 210e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 211e1051a39Sopenharmony_cistatic int verify_chain(X509_STORE_CTX *ctx) 212e1051a39Sopenharmony_ci{ 213e1051a39Sopenharmony_ci int err; 214e1051a39Sopenharmony_ci int ok; 215e1051a39Sopenharmony_ci 216e1051a39Sopenharmony_ci if ((ok = build_chain(ctx)) <= 0 217e1051a39Sopenharmony_ci || (ok = check_extensions(ctx)) <= 0 218e1051a39Sopenharmony_ci || (ok = check_auth_level(ctx)) <= 0 219e1051a39Sopenharmony_ci || (ok = check_id(ctx)) <= 0 220e1051a39Sopenharmony_ci || (ok = X509_get_pubkey_parameters(NULL, ctx->chain) ? 1 : -1) <= 0 221e1051a39Sopenharmony_ci || (ok = ctx->check_revocation(ctx)) <= 0) 222e1051a39Sopenharmony_ci return ok; 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ci err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain, 225e1051a39Sopenharmony_ci ctx->param->flags); 226e1051a39Sopenharmony_ci CB_FAIL_IF(err != X509_V_OK, ctx, NULL, ctx->error_depth, err); 227e1051a39Sopenharmony_ci 228e1051a39Sopenharmony_ci /* Verify chain signatures and expiration times */ 229e1051a39Sopenharmony_ci ok = ctx->verify != NULL ? ctx->verify(ctx) : internal_verify(ctx); 230e1051a39Sopenharmony_ci if (ok <= 0) 231e1051a39Sopenharmony_ci return ok; 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ci if ((ok = check_name_constraints(ctx)) <= 0) 234e1051a39Sopenharmony_ci return ok; 235e1051a39Sopenharmony_ci 236e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_RFC3779 237e1051a39Sopenharmony_ci /* RFC 3779 path validation, now that CRL check has been done */ 238e1051a39Sopenharmony_ci if ((ok = X509v3_asid_validate_path(ctx)) <= 0) 239e1051a39Sopenharmony_ci return ok; 240e1051a39Sopenharmony_ci if ((ok = X509v3_addr_validate_path(ctx)) <= 0) 241e1051a39Sopenharmony_ci return ok; 242e1051a39Sopenharmony_ci#endif 243e1051a39Sopenharmony_ci 244e1051a39Sopenharmony_ci /* If we get this far evaluate policies */ 245e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_POLICY_CHECK) != 0) 246e1051a39Sopenharmony_ci ok = ctx->check_policy(ctx); 247e1051a39Sopenharmony_ci return ok; 248e1051a39Sopenharmony_ci} 249e1051a39Sopenharmony_ci 250e1051a39Sopenharmony_ciint X509_STORE_CTX_verify(X509_STORE_CTX *ctx) 251e1051a39Sopenharmony_ci{ 252e1051a39Sopenharmony_ci if (ctx == NULL) { 253e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); 254e1051a39Sopenharmony_ci return -1; 255e1051a39Sopenharmony_ci } 256e1051a39Sopenharmony_ci if (ctx->cert == NULL && sk_X509_num(ctx->untrusted) >= 1) 257e1051a39Sopenharmony_ci ctx->cert = sk_X509_value(ctx->untrusted, 0); 258e1051a39Sopenharmony_ci return X509_verify_cert(ctx); 259e1051a39Sopenharmony_ci} 260e1051a39Sopenharmony_ci 261e1051a39Sopenharmony_ciint X509_verify_cert(X509_STORE_CTX *ctx) 262e1051a39Sopenharmony_ci{ 263e1051a39Sopenharmony_ci int ret; 264e1051a39Sopenharmony_ci 265e1051a39Sopenharmony_ci if (ctx == NULL) { 266e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); 267e1051a39Sopenharmony_ci return -1; 268e1051a39Sopenharmony_ci } 269e1051a39Sopenharmony_ci if (ctx->cert == NULL) { 270e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); 271e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_INVALID_CALL; 272e1051a39Sopenharmony_ci return -1; 273e1051a39Sopenharmony_ci } 274e1051a39Sopenharmony_ci 275e1051a39Sopenharmony_ci if (ctx->chain != NULL) { 276e1051a39Sopenharmony_ci /* 277e1051a39Sopenharmony_ci * This X509_STORE_CTX has already been used to verify a cert. We 278e1051a39Sopenharmony_ci * cannot do another one. 279e1051a39Sopenharmony_ci */ 280e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 281e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_INVALID_CALL; 282e1051a39Sopenharmony_ci return -1; 283e1051a39Sopenharmony_ci } 284e1051a39Sopenharmony_ci 285e1051a39Sopenharmony_ci if (!ossl_x509_add_cert_new(&ctx->chain, ctx->cert, X509_ADD_FLAG_UP_REF)) { 286e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 287e1051a39Sopenharmony_ci return -1; 288e1051a39Sopenharmony_ci } 289e1051a39Sopenharmony_ci ctx->num_untrusted = 1; 290e1051a39Sopenharmony_ci 291e1051a39Sopenharmony_ci /* If the peer's public key is too weak, we can stop early. */ 292e1051a39Sopenharmony_ci CB_FAIL_IF(!check_key_level(ctx, ctx->cert), 293e1051a39Sopenharmony_ci ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL); 294e1051a39Sopenharmony_ci 295e1051a39Sopenharmony_ci ret = DANETLS_ENABLED(ctx->dane) ? dane_verify(ctx) : verify_chain(ctx); 296e1051a39Sopenharmony_ci 297e1051a39Sopenharmony_ci /* 298e1051a39Sopenharmony_ci * Safety-net. If we are returning an error, we must also set ctx->error, 299e1051a39Sopenharmony_ci * so that the chain is not considered verified should the error be ignored 300e1051a39Sopenharmony_ci * (e.g. TLS with SSL_VERIFY_NONE). 301e1051a39Sopenharmony_ci */ 302e1051a39Sopenharmony_ci if (ret <= 0 && ctx->error == X509_V_OK) 303e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_UNSPECIFIED; 304e1051a39Sopenharmony_ci return ret; 305e1051a39Sopenharmony_ci} 306e1051a39Sopenharmony_ci 307e1051a39Sopenharmony_cistatic int sk_X509_contains(STACK_OF(X509) *sk, X509 *cert) 308e1051a39Sopenharmony_ci{ 309e1051a39Sopenharmony_ci int i, n = sk_X509_num(sk); 310e1051a39Sopenharmony_ci 311e1051a39Sopenharmony_ci for (i = 0; i < n; i++) 312e1051a39Sopenharmony_ci if (X509_cmp(sk_X509_value(sk, i), cert) == 0) 313e1051a39Sopenharmony_ci return 1; 314e1051a39Sopenharmony_ci return 0; 315e1051a39Sopenharmony_ci} 316e1051a39Sopenharmony_ci 317e1051a39Sopenharmony_ci/* 318e1051a39Sopenharmony_ci * Find in given STACK_OF(X509) |sk| an issuer cert (if any) of given cert |x|. 319e1051a39Sopenharmony_ci * The issuer must not yet be in |ctx->chain|, yet allowing the exception that 320e1051a39Sopenharmony_ci * |x| is self-issued and |ctx->chain| has just one element. 321e1051a39Sopenharmony_ci * Prefer the first non-expired one, else take the most recently expired one. 322e1051a39Sopenharmony_ci */ 323e1051a39Sopenharmony_cistatic X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) 324e1051a39Sopenharmony_ci{ 325e1051a39Sopenharmony_ci int i; 326e1051a39Sopenharmony_ci X509 *issuer, *rv = NULL; 327e1051a39Sopenharmony_ci 328e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(sk); i++) { 329e1051a39Sopenharmony_ci issuer = sk_X509_value(sk, i); 330e1051a39Sopenharmony_ci if (ctx->check_issued(ctx, x, issuer) 331e1051a39Sopenharmony_ci && (((x->ex_flags & EXFLAG_SI) != 0 && sk_X509_num(ctx->chain) == 1) 332e1051a39Sopenharmony_ci || !sk_X509_contains(ctx->chain, issuer))) { 333e1051a39Sopenharmony_ci if (ossl_x509_check_cert_time(ctx, issuer, -1)) 334e1051a39Sopenharmony_ci return issuer; 335e1051a39Sopenharmony_ci if (rv == NULL || ASN1_TIME_compare(X509_get0_notAfter(issuer), 336e1051a39Sopenharmony_ci X509_get0_notAfter(rv)) > 0) 337e1051a39Sopenharmony_ci rv = issuer; 338e1051a39Sopenharmony_ci } 339e1051a39Sopenharmony_ci } 340e1051a39Sopenharmony_ci return rv; 341e1051a39Sopenharmony_ci} 342e1051a39Sopenharmony_ci 343e1051a39Sopenharmony_ci/* Check that the given certificate 'x' is issued by the certificate 'issuer' */ 344e1051a39Sopenharmony_cistatic int check_issued(ossl_unused X509_STORE_CTX *ctx, X509 *x, X509 *issuer) 345e1051a39Sopenharmony_ci{ 346e1051a39Sopenharmony_ci int err = ossl_x509_likely_issued(issuer, x); 347e1051a39Sopenharmony_ci 348e1051a39Sopenharmony_ci if (err == X509_V_OK) 349e1051a39Sopenharmony_ci return 1; 350e1051a39Sopenharmony_ci /* 351e1051a39Sopenharmony_ci * SUBJECT_ISSUER_MISMATCH just means 'x' is clearly not issued by 'issuer'. 352e1051a39Sopenharmony_ci * Every other error code likely indicates a real error. 353e1051a39Sopenharmony_ci */ 354e1051a39Sopenharmony_ci return 0; 355e1051a39Sopenharmony_ci} 356e1051a39Sopenharmony_ci 357e1051a39Sopenharmony_ci/*- 358e1051a39Sopenharmony_ci * Alternative get_issuer method: look up from a STACK_OF(X509) in other_ctx. 359e1051a39Sopenharmony_ci * Returns -1 on internal error. 360e1051a39Sopenharmony_ci */ 361e1051a39Sopenharmony_cistatic int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) 362e1051a39Sopenharmony_ci{ 363e1051a39Sopenharmony_ci *issuer = find_issuer(ctx, ctx->other_ctx, x); 364e1051a39Sopenharmony_ci if (*issuer != NULL) 365e1051a39Sopenharmony_ci return X509_up_ref(*issuer) ? 1 : -1; 366e1051a39Sopenharmony_ci return 0; 367e1051a39Sopenharmony_ci} 368e1051a39Sopenharmony_ci 369e1051a39Sopenharmony_ci/*- 370e1051a39Sopenharmony_ci * Alternative lookup method: look from a STACK stored in other_ctx. 371e1051a39Sopenharmony_ci * Returns NULL on internal error (such as out of memory). 372e1051a39Sopenharmony_ci */ 373e1051a39Sopenharmony_cistatic STACK_OF(X509) *lookup_certs_sk(X509_STORE_CTX *ctx, 374e1051a39Sopenharmony_ci const X509_NAME *nm) 375e1051a39Sopenharmony_ci{ 376e1051a39Sopenharmony_ci STACK_OF(X509) *sk = sk_X509_new_null(); 377e1051a39Sopenharmony_ci X509 *x; 378e1051a39Sopenharmony_ci int i; 379e1051a39Sopenharmony_ci 380e1051a39Sopenharmony_ci if (sk == NULL) 381e1051a39Sopenharmony_ci return NULL; 382e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(ctx->other_ctx); i++) { 383e1051a39Sopenharmony_ci x = sk_X509_value(ctx->other_ctx, i); 384e1051a39Sopenharmony_ci if (X509_NAME_cmp(nm, X509_get_subject_name(x)) == 0) { 385e1051a39Sopenharmony_ci if (!X509_add_cert(sk, x, X509_ADD_FLAG_UP_REF)) { 386e1051a39Sopenharmony_ci sk_X509_pop_free(sk, X509_free); 387e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 388e1051a39Sopenharmony_ci return NULL; 389e1051a39Sopenharmony_ci } 390e1051a39Sopenharmony_ci } 391e1051a39Sopenharmony_ci } 392e1051a39Sopenharmony_ci return sk; 393e1051a39Sopenharmony_ci} 394e1051a39Sopenharmony_ci 395e1051a39Sopenharmony_ci/* 396e1051a39Sopenharmony_ci * Check EE or CA certificate purpose. For trusted certificates explicit local 397e1051a39Sopenharmony_ci * auxiliary trust can be used to override EKU-restrictions. 398e1051a39Sopenharmony_ci * Sadly, returns 0 also on internal error. 399e1051a39Sopenharmony_ci */ 400e1051a39Sopenharmony_cistatic int check_purpose(X509_STORE_CTX *ctx, X509 *x, int purpose, int depth, 401e1051a39Sopenharmony_ci int must_be_ca) 402e1051a39Sopenharmony_ci{ 403e1051a39Sopenharmony_ci int tr_ok = X509_TRUST_UNTRUSTED; 404e1051a39Sopenharmony_ci 405e1051a39Sopenharmony_ci /* 406e1051a39Sopenharmony_ci * For trusted certificates we want to see whether any auxiliary trust 407e1051a39Sopenharmony_ci * settings trump the purpose constraints. 408e1051a39Sopenharmony_ci * 409e1051a39Sopenharmony_ci * This is complicated by the fact that the trust ordinals in 410e1051a39Sopenharmony_ci * ctx->param->trust are entirely independent of the purpose ordinals in 411e1051a39Sopenharmony_ci * ctx->param->purpose! 412e1051a39Sopenharmony_ci * 413e1051a39Sopenharmony_ci * What connects them is their mutual initialization via calls from 414e1051a39Sopenharmony_ci * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets 415e1051a39Sopenharmony_ci * related values of both param->trust and param->purpose. It is however 416e1051a39Sopenharmony_ci * typically possible to infer associated trust values from a purpose value 417e1051a39Sopenharmony_ci * via the X509_PURPOSE API. 418e1051a39Sopenharmony_ci * 419e1051a39Sopenharmony_ci * Therefore, we can only check for trust overrides when the purpose we're 420e1051a39Sopenharmony_ci * checking is the same as ctx->param->purpose and ctx->param->trust is 421e1051a39Sopenharmony_ci * also set. 422e1051a39Sopenharmony_ci */ 423e1051a39Sopenharmony_ci if (depth >= ctx->num_untrusted && purpose == ctx->param->purpose) 424e1051a39Sopenharmony_ci tr_ok = X509_check_trust(x, ctx->param->trust, X509_TRUST_NO_SS_COMPAT); 425e1051a39Sopenharmony_ci 426e1051a39Sopenharmony_ci switch (tr_ok) { 427e1051a39Sopenharmony_ci case X509_TRUST_TRUSTED: 428e1051a39Sopenharmony_ci return 1; 429e1051a39Sopenharmony_ci case X509_TRUST_REJECTED: 430e1051a39Sopenharmony_ci break; 431e1051a39Sopenharmony_ci default: 432e1051a39Sopenharmony_ci switch (X509_check_purpose(x, purpose, must_be_ca > 0)) { 433e1051a39Sopenharmony_ci case 1: 434e1051a39Sopenharmony_ci return 1; 435e1051a39Sopenharmony_ci case 0: 436e1051a39Sopenharmony_ci break; 437e1051a39Sopenharmony_ci default: 438e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) == 0) 439e1051a39Sopenharmony_ci return 1; 440e1051a39Sopenharmony_ci } 441e1051a39Sopenharmony_ci break; 442e1051a39Sopenharmony_ci } 443e1051a39Sopenharmony_ci 444e1051a39Sopenharmony_ci return verify_cb_cert(ctx, x, depth, X509_V_ERR_INVALID_PURPOSE); 445e1051a39Sopenharmony_ci} 446e1051a39Sopenharmony_ci 447e1051a39Sopenharmony_ci/* 448e1051a39Sopenharmony_ci * Check extensions of a cert chain for consistency with the supplied purpose. 449e1051a39Sopenharmony_ci * Sadly, returns 0 also on internal error. 450e1051a39Sopenharmony_ci */ 451e1051a39Sopenharmony_cistatic int check_extensions(X509_STORE_CTX *ctx) 452e1051a39Sopenharmony_ci{ 453e1051a39Sopenharmony_ci int i, must_be_ca, plen = 0; 454e1051a39Sopenharmony_ci X509 *x; 455e1051a39Sopenharmony_ci int ret, proxy_path_length = 0; 456e1051a39Sopenharmony_ci int purpose, allow_proxy_certs, num = sk_X509_num(ctx->chain); 457e1051a39Sopenharmony_ci 458e1051a39Sopenharmony_ci /*- 459e1051a39Sopenharmony_ci * must_be_ca can have 1 of 3 values: 460e1051a39Sopenharmony_ci * -1: we accept both CA and non-CA certificates, to allow direct 461e1051a39Sopenharmony_ci * use of self-signed certificates (which are marked as CA). 462e1051a39Sopenharmony_ci * 0: we only accept non-CA certificates. This is currently not 463e1051a39Sopenharmony_ci * used, but the possibility is present for future extensions. 464e1051a39Sopenharmony_ci * 1: we only accept CA certificates. This is currently used for 465e1051a39Sopenharmony_ci * all certificates in the chain except the leaf certificate. 466e1051a39Sopenharmony_ci */ 467e1051a39Sopenharmony_ci must_be_ca = -1; 468e1051a39Sopenharmony_ci 469e1051a39Sopenharmony_ci /* CRL path validation */ 470e1051a39Sopenharmony_ci if (ctx->parent != NULL) { 471e1051a39Sopenharmony_ci allow_proxy_certs = 0; 472e1051a39Sopenharmony_ci purpose = X509_PURPOSE_CRL_SIGN; 473e1051a39Sopenharmony_ci } else { 474e1051a39Sopenharmony_ci allow_proxy_certs = 475e1051a39Sopenharmony_ci (ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS) != 0; 476e1051a39Sopenharmony_ci purpose = ctx->param->purpose; 477e1051a39Sopenharmony_ci } 478e1051a39Sopenharmony_ci 479e1051a39Sopenharmony_ci for (i = 0; i < num; i++) { 480e1051a39Sopenharmony_ci x = sk_X509_value(ctx->chain, i); 481e1051a39Sopenharmony_ci CB_FAIL_IF((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0 482e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_CRITICAL) != 0, 483e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION); 484e1051a39Sopenharmony_ci CB_FAIL_IF(!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY) != 0, 485e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED); 486e1051a39Sopenharmony_ci ret = X509_check_ca(x); 487e1051a39Sopenharmony_ci switch (must_be_ca) { 488e1051a39Sopenharmony_ci case -1: 489e1051a39Sopenharmony_ci CB_FAIL_IF((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0 490e1051a39Sopenharmony_ci && ret != 1 && ret != 0, 491e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_INVALID_CA); 492e1051a39Sopenharmony_ci break; 493e1051a39Sopenharmony_ci case 0: 494e1051a39Sopenharmony_ci CB_FAIL_IF(ret != 0, ctx, x, i, X509_V_ERR_INVALID_NON_CA); 495e1051a39Sopenharmony_ci break; 496e1051a39Sopenharmony_ci default: 497e1051a39Sopenharmony_ci /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */ 498e1051a39Sopenharmony_ci CB_FAIL_IF(ret == 0 499e1051a39Sopenharmony_ci || ((i + 1 < num 500e1051a39Sopenharmony_ci || (ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0) 501e1051a39Sopenharmony_ci && ret != 1), ctx, x, i, X509_V_ERR_INVALID_CA); 502e1051a39Sopenharmony_ci break; 503e1051a39Sopenharmony_ci } 504e1051a39Sopenharmony_ci if (num > 1) { 505e1051a39Sopenharmony_ci /* Check for presence of explicit elliptic curve parameters */ 506e1051a39Sopenharmony_ci ret = check_curve(x); 507e1051a39Sopenharmony_ci CB_FAIL_IF(ret < 0, ctx, x, i, X509_V_ERR_UNSPECIFIED); 508e1051a39Sopenharmony_ci CB_FAIL_IF(ret == 0, ctx, x, i, X509_V_ERR_EC_KEY_EXPLICIT_PARAMS); 509e1051a39Sopenharmony_ci } 510e1051a39Sopenharmony_ci /* 511e1051a39Sopenharmony_ci * Do the following set of checks only if strict checking is requested 512e1051a39Sopenharmony_ci * and not for self-issued (including self-signed) EE (non-CA) certs 513e1051a39Sopenharmony_ci * because RFC 5280 does not apply to them according RFC 6818 section 2. 514e1051a39Sopenharmony_ci */ 515e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) != 0 516e1051a39Sopenharmony_ci && num > 1) { /* 517e1051a39Sopenharmony_ci * this should imply 518e1051a39Sopenharmony_ci * !(i == 0 && (x->ex_flags & EXFLAG_CA) == 0 519e1051a39Sopenharmony_ci * && (x->ex_flags & EXFLAG_SI) != 0) 520e1051a39Sopenharmony_ci */ 521e1051a39Sopenharmony_ci /* Check Basic Constraints according to RFC 5280 section 4.2.1.9 */ 522e1051a39Sopenharmony_ci if (x->ex_pathlen != -1) { 523e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_flags & EXFLAG_CA) == 0, 524e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA); 525e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_kusage & KU_KEY_CERT_SIGN) == 0, ctx, 526e1051a39Sopenharmony_ci x, i, X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN); 527e1051a39Sopenharmony_ci } 528e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_flags & EXFLAG_CA) != 0 529e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_BCONS) != 0 530e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_BCONS_CRITICAL) == 0, 531e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_CA_BCONS_NOT_CRITICAL); 532e1051a39Sopenharmony_ci /* Check Key Usage according to RFC 5280 section 4.2.1.3 */ 533e1051a39Sopenharmony_ci if ((x->ex_flags & EXFLAG_CA) != 0) { 534e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_flags & EXFLAG_KUSAGE) == 0, 535e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_CA_CERT_MISSING_KEY_USAGE); 536e1051a39Sopenharmony_ci } else { 537e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_kusage & KU_KEY_CERT_SIGN) != 0, ctx, x, i, 538e1051a39Sopenharmony_ci X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA); 539e1051a39Sopenharmony_ci } 540e1051a39Sopenharmony_ci /* Check issuer is non-empty acc. to RFC 5280 section 4.1.2.4 */ 541e1051a39Sopenharmony_ci CB_FAIL_IF(X509_NAME_entry_count(X509_get_issuer_name(x)) == 0, 542e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_ISSUER_NAME_EMPTY); 543e1051a39Sopenharmony_ci /* Check subject is non-empty acc. to RFC 5280 section 4.1.2.6 */ 544e1051a39Sopenharmony_ci CB_FAIL_IF(((x->ex_flags & EXFLAG_CA) != 0 545e1051a39Sopenharmony_ci || (x->ex_kusage & KU_CRL_SIGN) != 0 546e1051a39Sopenharmony_ci || x->altname == NULL) 547e1051a39Sopenharmony_ci && X509_NAME_entry_count(X509_get_subject_name(x)) == 0, 548e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_SUBJECT_NAME_EMPTY); 549e1051a39Sopenharmony_ci CB_FAIL_IF(X509_NAME_entry_count(X509_get_subject_name(x)) == 0 550e1051a39Sopenharmony_ci && x->altname != NULL 551e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_SAN_CRITICAL) == 0, 552e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL); 553e1051a39Sopenharmony_ci /* Check SAN is non-empty according to RFC 5280 section 4.2.1.6 */ 554e1051a39Sopenharmony_ci CB_FAIL_IF(x->altname != NULL 555e1051a39Sopenharmony_ci && sk_GENERAL_NAME_num(x->altname) <= 0, 556e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_EMPTY_SUBJECT_ALT_NAME); 557e1051a39Sopenharmony_ci /* Check sig alg consistency acc. to RFC 5280 section 4.1.1.2 */ 558e1051a39Sopenharmony_ci CB_FAIL_IF(X509_ALGOR_cmp(&x->sig_alg, &x->cert_info.signature) != 0, 559e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY); 560e1051a39Sopenharmony_ci CB_FAIL_IF(x->akid != NULL 561e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_AKID_CRITICAL) != 0, 562e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL); 563e1051a39Sopenharmony_ci CB_FAIL_IF(x->skid != NULL 564e1051a39Sopenharmony_ci && (x->ex_flags & EXFLAG_SKID_CRITICAL) != 0, 565e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL); 566e1051a39Sopenharmony_ci if (X509_get_version(x) >= X509_VERSION_3) { 567e1051a39Sopenharmony_ci /* Check AKID presence acc. to RFC 5280 section 4.2.1.1 */ 568e1051a39Sopenharmony_ci CB_FAIL_IF(i + 1 < num /* 569e1051a39Sopenharmony_ci * this means not last cert in chain, 570e1051a39Sopenharmony_ci * taken as "generated by conforming CAs" 571e1051a39Sopenharmony_ci */ 572e1051a39Sopenharmony_ci && (x->akid == NULL || x->akid->keyid == NULL), ctx, 573e1051a39Sopenharmony_ci x, i, X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER); 574e1051a39Sopenharmony_ci /* Check SKID presence acc. to RFC 5280 section 4.2.1.2 */ 575e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_flags & EXFLAG_CA) != 0 && x->skid == NULL, 576e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER); 577e1051a39Sopenharmony_ci } else { 578e1051a39Sopenharmony_ci CB_FAIL_IF(sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0, 579e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3); 580e1051a39Sopenharmony_ci } 581e1051a39Sopenharmony_ci } 582e1051a39Sopenharmony_ci 583e1051a39Sopenharmony_ci /* check_purpose() makes the callback as needed */ 584e1051a39Sopenharmony_ci if (purpose > 0 && !check_purpose(ctx, x, purpose, i, must_be_ca)) 585e1051a39Sopenharmony_ci return 0; 586e1051a39Sopenharmony_ci /* Check path length */ 587e1051a39Sopenharmony_ci CB_FAIL_IF(i > 1 && x->ex_pathlen != -1 588e1051a39Sopenharmony_ci && plen > x->ex_pathlen + proxy_path_length, 589e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_PATH_LENGTH_EXCEEDED); 590e1051a39Sopenharmony_ci /* Increment path length if not a self-issued intermediate CA */ 591e1051a39Sopenharmony_ci if (i > 0 && (x->ex_flags & EXFLAG_SI) == 0) 592e1051a39Sopenharmony_ci plen++; 593e1051a39Sopenharmony_ci /* 594e1051a39Sopenharmony_ci * If this certificate is a proxy certificate, the next certificate 595e1051a39Sopenharmony_ci * must be another proxy certificate or a EE certificate. If not, 596e1051a39Sopenharmony_ci * the next certificate must be a CA certificate. 597e1051a39Sopenharmony_ci */ 598e1051a39Sopenharmony_ci if (x->ex_flags & EXFLAG_PROXY) { 599e1051a39Sopenharmony_ci /* 600e1051a39Sopenharmony_ci * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint 601e1051a39Sopenharmony_ci * is less than max_path_length, the former should be copied to 602e1051a39Sopenharmony_ci * the latter, and 4.1.4 (a) stipulates that max_path_length 603e1051a39Sopenharmony_ci * should be verified to be larger than zero and decrement it. 604e1051a39Sopenharmony_ci * 605e1051a39Sopenharmony_ci * Because we're checking the certs in the reverse order, we start 606e1051a39Sopenharmony_ci * with verifying that proxy_path_length isn't larger than pcPLC, 607e1051a39Sopenharmony_ci * and copy the latter to the former if it is, and finally, 608e1051a39Sopenharmony_ci * increment proxy_path_length. 609e1051a39Sopenharmony_ci */ 610e1051a39Sopenharmony_ci if (x->ex_pcpathlen != -1) { 611e1051a39Sopenharmony_ci CB_FAIL_IF(proxy_path_length > x->ex_pcpathlen, 612e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED); 613e1051a39Sopenharmony_ci proxy_path_length = x->ex_pcpathlen; 614e1051a39Sopenharmony_ci } 615e1051a39Sopenharmony_ci proxy_path_length++; 616e1051a39Sopenharmony_ci must_be_ca = 0; 617e1051a39Sopenharmony_ci } else { 618e1051a39Sopenharmony_ci must_be_ca = 1; 619e1051a39Sopenharmony_ci } 620e1051a39Sopenharmony_ci } 621e1051a39Sopenharmony_ci return 1; 622e1051a39Sopenharmony_ci} 623e1051a39Sopenharmony_ci 624e1051a39Sopenharmony_cistatic int has_san_id(X509 *x, int gtype) 625e1051a39Sopenharmony_ci{ 626e1051a39Sopenharmony_ci int i; 627e1051a39Sopenharmony_ci int ret = 0; 628e1051a39Sopenharmony_ci GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); 629e1051a39Sopenharmony_ci 630e1051a39Sopenharmony_ci if (gs == NULL) 631e1051a39Sopenharmony_ci return 0; 632e1051a39Sopenharmony_ci 633e1051a39Sopenharmony_ci for (i = 0; i < sk_GENERAL_NAME_num(gs); i++) { 634e1051a39Sopenharmony_ci GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i); 635e1051a39Sopenharmony_ci 636e1051a39Sopenharmony_ci if (g->type == gtype) { 637e1051a39Sopenharmony_ci ret = 1; 638e1051a39Sopenharmony_ci break; 639e1051a39Sopenharmony_ci } 640e1051a39Sopenharmony_ci } 641e1051a39Sopenharmony_ci GENERAL_NAMES_free(gs); 642e1051a39Sopenharmony_ci return ret; 643e1051a39Sopenharmony_ci} 644e1051a39Sopenharmony_ci 645e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 646e1051a39Sopenharmony_cistatic int check_name_constraints(X509_STORE_CTX *ctx) 647e1051a39Sopenharmony_ci{ 648e1051a39Sopenharmony_ci int i; 649e1051a39Sopenharmony_ci 650e1051a39Sopenharmony_ci /* Check name constraints for all certificates */ 651e1051a39Sopenharmony_ci for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) { 652e1051a39Sopenharmony_ci X509 *x = sk_X509_value(ctx->chain, i); 653e1051a39Sopenharmony_ci int j; 654e1051a39Sopenharmony_ci 655e1051a39Sopenharmony_ci /* Ignore self-issued certs unless last in chain */ 656e1051a39Sopenharmony_ci if (i != 0 && (x->ex_flags & EXFLAG_SI) != 0) 657e1051a39Sopenharmony_ci continue; 658e1051a39Sopenharmony_ci 659e1051a39Sopenharmony_ci /* 660e1051a39Sopenharmony_ci * Proxy certificates policy has an extra constraint, where the 661e1051a39Sopenharmony_ci * certificate subject MUST be the issuer with a single CN entry 662e1051a39Sopenharmony_ci * added. 663e1051a39Sopenharmony_ci * (RFC 3820: 3.4, 4.1.3 (a)(4)) 664e1051a39Sopenharmony_ci */ 665e1051a39Sopenharmony_ci if ((x->ex_flags & EXFLAG_PROXY) != 0) { 666e1051a39Sopenharmony_ci X509_NAME *tmpsubject = X509_get_subject_name(x); 667e1051a39Sopenharmony_ci X509_NAME *tmpissuer = X509_get_issuer_name(x); 668e1051a39Sopenharmony_ci X509_NAME_ENTRY *tmpentry = NULL; 669e1051a39Sopenharmony_ci int last_nid = 0; 670e1051a39Sopenharmony_ci int err = X509_V_OK; 671e1051a39Sopenharmony_ci int last_loc = X509_NAME_entry_count(tmpsubject) - 1; 672e1051a39Sopenharmony_ci 673e1051a39Sopenharmony_ci /* Check that there are at least two RDNs */ 674e1051a39Sopenharmony_ci if (last_loc < 1) { 675e1051a39Sopenharmony_ci err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; 676e1051a39Sopenharmony_ci goto proxy_name_done; 677e1051a39Sopenharmony_ci } 678e1051a39Sopenharmony_ci 679e1051a39Sopenharmony_ci /* 680e1051a39Sopenharmony_ci * Check that there is exactly one more RDN in subject as 681e1051a39Sopenharmony_ci * there is in issuer. 682e1051a39Sopenharmony_ci */ 683e1051a39Sopenharmony_ci if (X509_NAME_entry_count(tmpsubject) 684e1051a39Sopenharmony_ci != X509_NAME_entry_count(tmpissuer) + 1) { 685e1051a39Sopenharmony_ci err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; 686e1051a39Sopenharmony_ci goto proxy_name_done; 687e1051a39Sopenharmony_ci } 688e1051a39Sopenharmony_ci 689e1051a39Sopenharmony_ci /* 690e1051a39Sopenharmony_ci * Check that the last subject component isn't part of a 691e1051a39Sopenharmony_ci * multi-valued RDN 692e1051a39Sopenharmony_ci */ 693e1051a39Sopenharmony_ci if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject, last_loc)) 694e1051a39Sopenharmony_ci == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject, 695e1051a39Sopenharmony_ci last_loc - 1))) { 696e1051a39Sopenharmony_ci err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; 697e1051a39Sopenharmony_ci goto proxy_name_done; 698e1051a39Sopenharmony_ci } 699e1051a39Sopenharmony_ci 700e1051a39Sopenharmony_ci /* 701e1051a39Sopenharmony_ci * Check that the last subject RDN is a commonName, and that 702e1051a39Sopenharmony_ci * all the previous RDNs match the issuer exactly 703e1051a39Sopenharmony_ci */ 704e1051a39Sopenharmony_ci tmpsubject = X509_NAME_dup(tmpsubject); 705e1051a39Sopenharmony_ci if (tmpsubject == NULL) { 706e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 707e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 708e1051a39Sopenharmony_ci return -1; 709e1051a39Sopenharmony_ci } 710e1051a39Sopenharmony_ci 711e1051a39Sopenharmony_ci tmpentry = X509_NAME_delete_entry(tmpsubject, last_loc); 712e1051a39Sopenharmony_ci last_nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry)); 713e1051a39Sopenharmony_ci 714e1051a39Sopenharmony_ci if (last_nid != NID_commonName 715e1051a39Sopenharmony_ci || X509_NAME_cmp(tmpsubject, tmpissuer) != 0) { 716e1051a39Sopenharmony_ci err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION; 717e1051a39Sopenharmony_ci } 718e1051a39Sopenharmony_ci 719e1051a39Sopenharmony_ci X509_NAME_ENTRY_free(tmpentry); 720e1051a39Sopenharmony_ci X509_NAME_free(tmpsubject); 721e1051a39Sopenharmony_ci 722e1051a39Sopenharmony_ci proxy_name_done: 723e1051a39Sopenharmony_ci CB_FAIL_IF(err != X509_V_OK, ctx, x, i, err); 724e1051a39Sopenharmony_ci } 725e1051a39Sopenharmony_ci 726e1051a39Sopenharmony_ci /* 727e1051a39Sopenharmony_ci * Check against constraints for all certificates higher in chain 728e1051a39Sopenharmony_ci * including trust anchor. Trust anchor not strictly speaking needed 729e1051a39Sopenharmony_ci * but if it includes constraints it is to be assumed it expects them 730e1051a39Sopenharmony_ci * to be obeyed. 731e1051a39Sopenharmony_ci */ 732e1051a39Sopenharmony_ci for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) { 733e1051a39Sopenharmony_ci NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc; 734e1051a39Sopenharmony_ci 735e1051a39Sopenharmony_ci if (nc) { 736e1051a39Sopenharmony_ci int rv = NAME_CONSTRAINTS_check(x, nc); 737e1051a39Sopenharmony_ci int ret = 1; 738e1051a39Sopenharmony_ci 739e1051a39Sopenharmony_ci /* If EE certificate check commonName too */ 740e1051a39Sopenharmony_ci if (rv == X509_V_OK && i == 0 741e1051a39Sopenharmony_ci && (ctx->param->hostflags 742e1051a39Sopenharmony_ci & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) == 0 743e1051a39Sopenharmony_ci && ((ctx->param->hostflags 744e1051a39Sopenharmony_ci & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) != 0 745e1051a39Sopenharmony_ci || (ret = has_san_id(x, GEN_DNS)) == 0)) 746e1051a39Sopenharmony_ci rv = NAME_CONSTRAINTS_check_CN(x, nc); 747e1051a39Sopenharmony_ci if (ret < 0) 748e1051a39Sopenharmony_ci return ret; 749e1051a39Sopenharmony_ci 750e1051a39Sopenharmony_ci switch (rv) { 751e1051a39Sopenharmony_ci case X509_V_OK: 752e1051a39Sopenharmony_ci break; 753e1051a39Sopenharmony_ci case X509_V_ERR_OUT_OF_MEM: 754e1051a39Sopenharmony_ci return -1; 755e1051a39Sopenharmony_ci default: 756e1051a39Sopenharmony_ci CB_FAIL_IF(1, ctx, x, i, rv); 757e1051a39Sopenharmony_ci break; 758e1051a39Sopenharmony_ci } 759e1051a39Sopenharmony_ci } 760e1051a39Sopenharmony_ci } 761e1051a39Sopenharmony_ci } 762e1051a39Sopenharmony_ci return 1; 763e1051a39Sopenharmony_ci} 764e1051a39Sopenharmony_ci 765e1051a39Sopenharmony_cistatic int check_id_error(X509_STORE_CTX *ctx, int errcode) 766e1051a39Sopenharmony_ci{ 767e1051a39Sopenharmony_ci return verify_cb_cert(ctx, ctx->cert, 0, errcode); 768e1051a39Sopenharmony_ci} 769e1051a39Sopenharmony_ci 770e1051a39Sopenharmony_cistatic int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm) 771e1051a39Sopenharmony_ci{ 772e1051a39Sopenharmony_ci int i; 773e1051a39Sopenharmony_ci int n = sk_OPENSSL_STRING_num(vpm->hosts); 774e1051a39Sopenharmony_ci char *name; 775e1051a39Sopenharmony_ci 776e1051a39Sopenharmony_ci if (vpm->peername != NULL) { 777e1051a39Sopenharmony_ci OPENSSL_free(vpm->peername); 778e1051a39Sopenharmony_ci vpm->peername = NULL; 779e1051a39Sopenharmony_ci } 780e1051a39Sopenharmony_ci for (i = 0; i < n; ++i) { 781e1051a39Sopenharmony_ci name = sk_OPENSSL_STRING_value(vpm->hosts, i); 782e1051a39Sopenharmony_ci if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0) 783e1051a39Sopenharmony_ci return 1; 784e1051a39Sopenharmony_ci } 785e1051a39Sopenharmony_ci return n == 0; 786e1051a39Sopenharmony_ci} 787e1051a39Sopenharmony_ci 788e1051a39Sopenharmony_cistatic int check_id(X509_STORE_CTX *ctx) 789e1051a39Sopenharmony_ci{ 790e1051a39Sopenharmony_ci X509_VERIFY_PARAM *vpm = ctx->param; 791e1051a39Sopenharmony_ci X509 *x = ctx->cert; 792e1051a39Sopenharmony_ci 793e1051a39Sopenharmony_ci if (vpm->hosts != NULL && check_hosts(x, vpm) <= 0) { 794e1051a39Sopenharmony_ci if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) 795e1051a39Sopenharmony_ci return 0; 796e1051a39Sopenharmony_ci } 797e1051a39Sopenharmony_ci if (vpm->email != NULL 798e1051a39Sopenharmony_ci && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) { 799e1051a39Sopenharmony_ci if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) 800e1051a39Sopenharmony_ci return 0; 801e1051a39Sopenharmony_ci } 802e1051a39Sopenharmony_ci if (vpm->ip != NULL && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) { 803e1051a39Sopenharmony_ci if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) 804e1051a39Sopenharmony_ci return 0; 805e1051a39Sopenharmony_ci } 806e1051a39Sopenharmony_ci return 1; 807e1051a39Sopenharmony_ci} 808e1051a39Sopenharmony_ci 809e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 810e1051a39Sopenharmony_cistatic int check_trust(X509_STORE_CTX *ctx, int num_untrusted) 811e1051a39Sopenharmony_ci{ 812e1051a39Sopenharmony_ci int i, res; 813e1051a39Sopenharmony_ci X509 *x = NULL; 814e1051a39Sopenharmony_ci X509 *mx; 815e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 816e1051a39Sopenharmony_ci int num = sk_X509_num(ctx->chain); 817e1051a39Sopenharmony_ci int trust; 818e1051a39Sopenharmony_ci 819e1051a39Sopenharmony_ci /* 820e1051a39Sopenharmony_ci * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2) 821e1051a39Sopenharmony_ci * match, we're done, otherwise we'll merely record the match depth. 822e1051a39Sopenharmony_ci */ 823e1051a39Sopenharmony_ci if (DANETLS_HAS_TA(dane) && num_untrusted > 0 && num_untrusted < num) { 824e1051a39Sopenharmony_ci trust = check_dane_issuer(ctx, num_untrusted); 825e1051a39Sopenharmony_ci if (trust != X509_TRUST_UNTRUSTED) 826e1051a39Sopenharmony_ci return trust; 827e1051a39Sopenharmony_ci } 828e1051a39Sopenharmony_ci 829e1051a39Sopenharmony_ci /* 830e1051a39Sopenharmony_ci * Check trusted certificates in chain at depth num_untrusted and up. 831e1051a39Sopenharmony_ci * Note, that depths 0..num_untrusted-1 may also contain trusted 832e1051a39Sopenharmony_ci * certificates, but the caller is expected to have already checked those, 833e1051a39Sopenharmony_ci * and wants to incrementally check just any added since. 834e1051a39Sopenharmony_ci */ 835e1051a39Sopenharmony_ci for (i = num_untrusted; i < num; i++) { 836e1051a39Sopenharmony_ci x = sk_X509_value(ctx->chain, i); 837e1051a39Sopenharmony_ci trust = X509_check_trust(x, ctx->param->trust, 0); 838e1051a39Sopenharmony_ci /* If explicitly trusted (so not neutral nor rejected) return trusted */ 839e1051a39Sopenharmony_ci if (trust == X509_TRUST_TRUSTED) 840e1051a39Sopenharmony_ci goto trusted; 841e1051a39Sopenharmony_ci if (trust == X509_TRUST_REJECTED) 842e1051a39Sopenharmony_ci goto rejected; 843e1051a39Sopenharmony_ci } 844e1051a39Sopenharmony_ci 845e1051a39Sopenharmony_ci /* 846e1051a39Sopenharmony_ci * If we are looking at a trusted certificate, and accept partial chains, 847e1051a39Sopenharmony_ci * the chain is PKIX trusted. 848e1051a39Sopenharmony_ci */ 849e1051a39Sopenharmony_ci if (num_untrusted < num) { 850e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) != 0) 851e1051a39Sopenharmony_ci goto trusted; 852e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 853e1051a39Sopenharmony_ci } 854e1051a39Sopenharmony_ci 855e1051a39Sopenharmony_ci if (num_untrusted == num 856e1051a39Sopenharmony_ci && (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) != 0) { 857e1051a39Sopenharmony_ci /* 858e1051a39Sopenharmony_ci * Last-resort call with no new trusted certificates, check the leaf 859e1051a39Sopenharmony_ci * for a direct trust store match. 860e1051a39Sopenharmony_ci */ 861e1051a39Sopenharmony_ci i = 0; 862e1051a39Sopenharmony_ci x = sk_X509_value(ctx->chain, i); 863e1051a39Sopenharmony_ci res = lookup_cert_match(&mx, ctx, x); 864e1051a39Sopenharmony_ci if (res < 0) 865e1051a39Sopenharmony_ci return res; 866e1051a39Sopenharmony_ci if (mx == NULL) 867e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 868e1051a39Sopenharmony_ci 869e1051a39Sopenharmony_ci /* 870e1051a39Sopenharmony_ci * Check explicit auxiliary trust/reject settings. If none are set, 871e1051a39Sopenharmony_ci * we'll accept X509_TRUST_UNTRUSTED when not self-signed. 872e1051a39Sopenharmony_ci */ 873e1051a39Sopenharmony_ci trust = X509_check_trust(mx, ctx->param->trust, 0); 874e1051a39Sopenharmony_ci if (trust == X509_TRUST_REJECTED) { 875e1051a39Sopenharmony_ci X509_free(mx); 876e1051a39Sopenharmony_ci goto rejected; 877e1051a39Sopenharmony_ci } 878e1051a39Sopenharmony_ci 879e1051a39Sopenharmony_ci /* Replace leaf with trusted match */ 880e1051a39Sopenharmony_ci (void)sk_X509_set(ctx->chain, 0, mx); 881e1051a39Sopenharmony_ci X509_free(x); 882e1051a39Sopenharmony_ci ctx->num_untrusted = 0; 883e1051a39Sopenharmony_ci goto trusted; 884e1051a39Sopenharmony_ci } 885e1051a39Sopenharmony_ci 886e1051a39Sopenharmony_ci /* 887e1051a39Sopenharmony_ci * If no trusted certs in chain at all return untrusted and allow 888e1051a39Sopenharmony_ci * standard (no issuer cert) etc errors to be indicated. 889e1051a39Sopenharmony_ci */ 890e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 891e1051a39Sopenharmony_ci 892e1051a39Sopenharmony_ci rejected: 893e1051a39Sopenharmony_ci return verify_cb_cert(ctx, x, i, X509_V_ERR_CERT_REJECTED) == 0 894e1051a39Sopenharmony_ci ? X509_TRUST_REJECTED : X509_TRUST_UNTRUSTED; 895e1051a39Sopenharmony_ci 896e1051a39Sopenharmony_ci trusted: 897e1051a39Sopenharmony_ci if (!DANETLS_ENABLED(dane)) 898e1051a39Sopenharmony_ci return X509_TRUST_TRUSTED; 899e1051a39Sopenharmony_ci if (dane->pdpth < 0) 900e1051a39Sopenharmony_ci dane->pdpth = num_untrusted; 901e1051a39Sopenharmony_ci /* With DANE, PKIX alone is not trusted until we have both */ 902e1051a39Sopenharmony_ci if (dane->mdpth >= 0) 903e1051a39Sopenharmony_ci return X509_TRUST_TRUSTED; 904e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 905e1051a39Sopenharmony_ci} 906e1051a39Sopenharmony_ci 907e1051a39Sopenharmony_ci/* Sadly, returns 0 also on internal error. */ 908e1051a39Sopenharmony_cistatic int check_revocation(X509_STORE_CTX *ctx) 909e1051a39Sopenharmony_ci{ 910e1051a39Sopenharmony_ci int i = 0, last = 0, ok = 0; 911e1051a39Sopenharmony_ci 912e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_CRL_CHECK) == 0) 913e1051a39Sopenharmony_ci return 1; 914e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) != 0) { 915e1051a39Sopenharmony_ci last = sk_X509_num(ctx->chain) - 1; 916e1051a39Sopenharmony_ci } else { 917e1051a39Sopenharmony_ci /* If checking CRL paths this isn't the EE certificate */ 918e1051a39Sopenharmony_ci if (ctx->parent) 919e1051a39Sopenharmony_ci return 1; 920e1051a39Sopenharmony_ci last = 0; 921e1051a39Sopenharmony_ci } 922e1051a39Sopenharmony_ci for (i = 0; i <= last; i++) { 923e1051a39Sopenharmony_ci ctx->error_depth = i; 924e1051a39Sopenharmony_ci ok = check_cert(ctx); 925e1051a39Sopenharmony_ci if (!ok) 926e1051a39Sopenharmony_ci return ok; 927e1051a39Sopenharmony_ci } 928e1051a39Sopenharmony_ci return 1; 929e1051a39Sopenharmony_ci} 930e1051a39Sopenharmony_ci 931e1051a39Sopenharmony_ci/* Sadly, returns 0 also on internal error. */ 932e1051a39Sopenharmony_cistatic int check_cert(X509_STORE_CTX *ctx) 933e1051a39Sopenharmony_ci{ 934e1051a39Sopenharmony_ci X509_CRL *crl = NULL, *dcrl = NULL; 935e1051a39Sopenharmony_ci int ok = 0; 936e1051a39Sopenharmony_ci int cnum = ctx->error_depth; 937e1051a39Sopenharmony_ci X509 *x = sk_X509_value(ctx->chain, cnum); 938e1051a39Sopenharmony_ci 939e1051a39Sopenharmony_ci ctx->current_cert = x; 940e1051a39Sopenharmony_ci ctx->current_issuer = NULL; 941e1051a39Sopenharmony_ci ctx->current_crl_score = 0; 942e1051a39Sopenharmony_ci ctx->current_reasons = 0; 943e1051a39Sopenharmony_ci 944e1051a39Sopenharmony_ci if ((x->ex_flags & EXFLAG_PROXY) != 0) 945e1051a39Sopenharmony_ci return 1; 946e1051a39Sopenharmony_ci 947e1051a39Sopenharmony_ci while (ctx->current_reasons != CRLDP_ALL_REASONS) { 948e1051a39Sopenharmony_ci unsigned int last_reasons = ctx->current_reasons; 949e1051a39Sopenharmony_ci 950e1051a39Sopenharmony_ci /* Try to retrieve relevant CRL */ 951e1051a39Sopenharmony_ci if (ctx->get_crl != NULL) 952e1051a39Sopenharmony_ci ok = ctx->get_crl(ctx, &crl, x); 953e1051a39Sopenharmony_ci else 954e1051a39Sopenharmony_ci ok = get_crl_delta(ctx, &crl, &dcrl, x); 955e1051a39Sopenharmony_ci /* If error looking up CRL, nothing we can do except notify callback */ 956e1051a39Sopenharmony_ci if (!ok) { 957e1051a39Sopenharmony_ci ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL); 958e1051a39Sopenharmony_ci goto done; 959e1051a39Sopenharmony_ci } 960e1051a39Sopenharmony_ci ctx->current_crl = crl; 961e1051a39Sopenharmony_ci ok = ctx->check_crl(ctx, crl); 962e1051a39Sopenharmony_ci if (!ok) 963e1051a39Sopenharmony_ci goto done; 964e1051a39Sopenharmony_ci 965e1051a39Sopenharmony_ci if (dcrl != NULL) { 966e1051a39Sopenharmony_ci ok = ctx->check_crl(ctx, dcrl); 967e1051a39Sopenharmony_ci if (!ok) 968e1051a39Sopenharmony_ci goto done; 969e1051a39Sopenharmony_ci ok = ctx->cert_crl(ctx, dcrl, x); 970e1051a39Sopenharmony_ci if (!ok) 971e1051a39Sopenharmony_ci goto done; 972e1051a39Sopenharmony_ci } else { 973e1051a39Sopenharmony_ci ok = 1; 974e1051a39Sopenharmony_ci } 975e1051a39Sopenharmony_ci 976e1051a39Sopenharmony_ci /* Don't look in full CRL if delta reason is removefromCRL */ 977e1051a39Sopenharmony_ci if (ok != 2) { 978e1051a39Sopenharmony_ci ok = ctx->cert_crl(ctx, crl, x); 979e1051a39Sopenharmony_ci if (!ok) 980e1051a39Sopenharmony_ci goto done; 981e1051a39Sopenharmony_ci } 982e1051a39Sopenharmony_ci 983e1051a39Sopenharmony_ci X509_CRL_free(crl); 984e1051a39Sopenharmony_ci X509_CRL_free(dcrl); 985e1051a39Sopenharmony_ci crl = NULL; 986e1051a39Sopenharmony_ci dcrl = NULL; 987e1051a39Sopenharmony_ci /* 988e1051a39Sopenharmony_ci * If reasons not updated we won't get anywhere by another iteration, 989e1051a39Sopenharmony_ci * so exit loop. 990e1051a39Sopenharmony_ci */ 991e1051a39Sopenharmony_ci if (last_reasons == ctx->current_reasons) { 992e1051a39Sopenharmony_ci ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL); 993e1051a39Sopenharmony_ci goto done; 994e1051a39Sopenharmony_ci } 995e1051a39Sopenharmony_ci } 996e1051a39Sopenharmony_ci done: 997e1051a39Sopenharmony_ci X509_CRL_free(crl); 998e1051a39Sopenharmony_ci X509_CRL_free(dcrl); 999e1051a39Sopenharmony_ci 1000e1051a39Sopenharmony_ci ctx->current_crl = NULL; 1001e1051a39Sopenharmony_ci return ok; 1002e1051a39Sopenharmony_ci} 1003e1051a39Sopenharmony_ci 1004e1051a39Sopenharmony_ci/* Check CRL times against values in X509_STORE_CTX */ 1005e1051a39Sopenharmony_cistatic int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) 1006e1051a39Sopenharmony_ci{ 1007e1051a39Sopenharmony_ci time_t *ptime; 1008e1051a39Sopenharmony_ci int i; 1009e1051a39Sopenharmony_ci 1010e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) != 0) 1011e1051a39Sopenharmony_ci ptime = &ctx->param->check_time; 1012e1051a39Sopenharmony_ci else if ((ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) != 0) 1013e1051a39Sopenharmony_ci return 1; 1014e1051a39Sopenharmony_ci else 1015e1051a39Sopenharmony_ci ptime = NULL; 1016e1051a39Sopenharmony_ci if (notify) 1017e1051a39Sopenharmony_ci ctx->current_crl = crl; 1018e1051a39Sopenharmony_ci 1019e1051a39Sopenharmony_ci i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime); 1020e1051a39Sopenharmony_ci if (i == 0) { 1021e1051a39Sopenharmony_ci if (!notify) 1022e1051a39Sopenharmony_ci return 0; 1023e1051a39Sopenharmony_ci if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD)) 1024e1051a39Sopenharmony_ci return 0; 1025e1051a39Sopenharmony_ci } 1026e1051a39Sopenharmony_ci 1027e1051a39Sopenharmony_ci if (i > 0) { 1028e1051a39Sopenharmony_ci if (!notify) 1029e1051a39Sopenharmony_ci return 0; 1030e1051a39Sopenharmony_ci if (!verify_cb_crl(ctx, X509_V_ERR_CRL_NOT_YET_VALID)) 1031e1051a39Sopenharmony_ci return 0; 1032e1051a39Sopenharmony_ci } 1033e1051a39Sopenharmony_ci 1034e1051a39Sopenharmony_ci if (X509_CRL_get0_nextUpdate(crl)) { 1035e1051a39Sopenharmony_ci i = X509_cmp_time(X509_CRL_get0_nextUpdate(crl), ptime); 1036e1051a39Sopenharmony_ci 1037e1051a39Sopenharmony_ci if (i == 0) { 1038e1051a39Sopenharmony_ci if (!notify) 1039e1051a39Sopenharmony_ci return 0; 1040e1051a39Sopenharmony_ci if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD)) 1041e1051a39Sopenharmony_ci return 0; 1042e1051a39Sopenharmony_ci } 1043e1051a39Sopenharmony_ci /* Ignore expiration of base CRL is delta is valid */ 1044e1051a39Sopenharmony_ci if (i < 0 && (ctx->current_crl_score & CRL_SCORE_TIME_DELTA) == 0) { 1045e1051a39Sopenharmony_ci if (!notify || !verify_cb_crl(ctx, X509_V_ERR_CRL_HAS_EXPIRED)) 1046e1051a39Sopenharmony_ci return 0; 1047e1051a39Sopenharmony_ci } 1048e1051a39Sopenharmony_ci } 1049e1051a39Sopenharmony_ci 1050e1051a39Sopenharmony_ci if (notify) 1051e1051a39Sopenharmony_ci ctx->current_crl = NULL; 1052e1051a39Sopenharmony_ci 1053e1051a39Sopenharmony_ci return 1; 1054e1051a39Sopenharmony_ci} 1055e1051a39Sopenharmony_ci 1056e1051a39Sopenharmony_cistatic int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, 1057e1051a39Sopenharmony_ci X509 **pissuer, int *pscore, unsigned int *preasons, 1058e1051a39Sopenharmony_ci STACK_OF(X509_CRL) *crls) 1059e1051a39Sopenharmony_ci{ 1060e1051a39Sopenharmony_ci int i, crl_score, best_score = *pscore; 1061e1051a39Sopenharmony_ci unsigned int reasons, best_reasons = 0; 1062e1051a39Sopenharmony_ci X509 *x = ctx->current_cert; 1063e1051a39Sopenharmony_ci X509_CRL *crl, *best_crl = NULL; 1064e1051a39Sopenharmony_ci X509 *crl_issuer = NULL, *best_crl_issuer = NULL; 1065e1051a39Sopenharmony_ci 1066e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_CRL_num(crls); i++) { 1067e1051a39Sopenharmony_ci crl = sk_X509_CRL_value(crls, i); 1068e1051a39Sopenharmony_ci reasons = *preasons; 1069e1051a39Sopenharmony_ci crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x); 1070e1051a39Sopenharmony_ci if (crl_score < best_score || crl_score == 0) 1071e1051a39Sopenharmony_ci continue; 1072e1051a39Sopenharmony_ci /* If current CRL is equivalent use it if it is newer */ 1073e1051a39Sopenharmony_ci if (crl_score == best_score && best_crl != NULL) { 1074e1051a39Sopenharmony_ci int day, sec; 1075e1051a39Sopenharmony_ci 1076e1051a39Sopenharmony_ci if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl), 1077e1051a39Sopenharmony_ci X509_CRL_get0_lastUpdate(crl)) == 0) 1078e1051a39Sopenharmony_ci continue; 1079e1051a39Sopenharmony_ci /* 1080e1051a39Sopenharmony_ci * ASN1_TIME_diff never returns inconsistent signs for |day| 1081e1051a39Sopenharmony_ci * and |sec|. 1082e1051a39Sopenharmony_ci */ 1083e1051a39Sopenharmony_ci if (day <= 0 && sec <= 0) 1084e1051a39Sopenharmony_ci continue; 1085e1051a39Sopenharmony_ci } 1086e1051a39Sopenharmony_ci best_crl = crl; 1087e1051a39Sopenharmony_ci best_crl_issuer = crl_issuer; 1088e1051a39Sopenharmony_ci best_score = crl_score; 1089e1051a39Sopenharmony_ci best_reasons = reasons; 1090e1051a39Sopenharmony_ci } 1091e1051a39Sopenharmony_ci 1092e1051a39Sopenharmony_ci if (best_crl != NULL) { 1093e1051a39Sopenharmony_ci X509_CRL_free(*pcrl); 1094e1051a39Sopenharmony_ci *pcrl = best_crl; 1095e1051a39Sopenharmony_ci *pissuer = best_crl_issuer; 1096e1051a39Sopenharmony_ci *pscore = best_score; 1097e1051a39Sopenharmony_ci *preasons = best_reasons; 1098e1051a39Sopenharmony_ci X509_CRL_up_ref(best_crl); 1099e1051a39Sopenharmony_ci X509_CRL_free(*pdcrl); 1100e1051a39Sopenharmony_ci *pdcrl = NULL; 1101e1051a39Sopenharmony_ci get_delta_sk(ctx, pdcrl, pscore, best_crl, crls); 1102e1051a39Sopenharmony_ci } 1103e1051a39Sopenharmony_ci 1104e1051a39Sopenharmony_ci if (best_score >= CRL_SCORE_VALID) 1105e1051a39Sopenharmony_ci return 1; 1106e1051a39Sopenharmony_ci 1107e1051a39Sopenharmony_ci return 0; 1108e1051a39Sopenharmony_ci} 1109e1051a39Sopenharmony_ci 1110e1051a39Sopenharmony_ci/* 1111e1051a39Sopenharmony_ci * Compare two CRL extensions for delta checking purposes. They should be 1112e1051a39Sopenharmony_ci * both present or both absent. If both present all fields must be identical. 1113e1051a39Sopenharmony_ci */ 1114e1051a39Sopenharmony_cistatic int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid) 1115e1051a39Sopenharmony_ci{ 1116e1051a39Sopenharmony_ci ASN1_OCTET_STRING *exta = NULL, *extb = NULL; 1117e1051a39Sopenharmony_ci int i = X509_CRL_get_ext_by_NID(a, nid, -1); 1118e1051a39Sopenharmony_ci 1119e1051a39Sopenharmony_ci if (i >= 0) { 1120e1051a39Sopenharmony_ci /* Can't have multiple occurrences */ 1121e1051a39Sopenharmony_ci if (X509_CRL_get_ext_by_NID(a, nid, i) != -1) 1122e1051a39Sopenharmony_ci return 0; 1123e1051a39Sopenharmony_ci exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i)); 1124e1051a39Sopenharmony_ci } 1125e1051a39Sopenharmony_ci 1126e1051a39Sopenharmony_ci i = X509_CRL_get_ext_by_NID(b, nid, -1); 1127e1051a39Sopenharmony_ci if (i >= 0) { 1128e1051a39Sopenharmony_ci if (X509_CRL_get_ext_by_NID(b, nid, i) != -1) 1129e1051a39Sopenharmony_ci return 0; 1130e1051a39Sopenharmony_ci extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i)); 1131e1051a39Sopenharmony_ci } 1132e1051a39Sopenharmony_ci 1133e1051a39Sopenharmony_ci if (exta == NULL && extb == NULL) 1134e1051a39Sopenharmony_ci return 1; 1135e1051a39Sopenharmony_ci 1136e1051a39Sopenharmony_ci if (exta == NULL || extb == NULL) 1137e1051a39Sopenharmony_ci return 0; 1138e1051a39Sopenharmony_ci 1139e1051a39Sopenharmony_ci return ASN1_OCTET_STRING_cmp(exta, extb) == 0; 1140e1051a39Sopenharmony_ci} 1141e1051a39Sopenharmony_ci 1142e1051a39Sopenharmony_ci/* See if a base and delta are compatible */ 1143e1051a39Sopenharmony_cistatic int check_delta_base(X509_CRL *delta, X509_CRL *base) 1144e1051a39Sopenharmony_ci{ 1145e1051a39Sopenharmony_ci /* Delta CRL must be a delta */ 1146e1051a39Sopenharmony_ci if (delta->base_crl_number == NULL) 1147e1051a39Sopenharmony_ci return 0; 1148e1051a39Sopenharmony_ci /* Base must have a CRL number */ 1149e1051a39Sopenharmony_ci if (base->crl_number == NULL) 1150e1051a39Sopenharmony_ci return 0; 1151e1051a39Sopenharmony_ci /* Issuer names must match */ 1152e1051a39Sopenharmony_ci if (X509_NAME_cmp(X509_CRL_get_issuer(base), 1153e1051a39Sopenharmony_ci X509_CRL_get_issuer(delta)) != 0) 1154e1051a39Sopenharmony_ci return 0; 1155e1051a39Sopenharmony_ci /* AKID and IDP must match */ 1156e1051a39Sopenharmony_ci if (!crl_extension_match(delta, base, NID_authority_key_identifier)) 1157e1051a39Sopenharmony_ci return 0; 1158e1051a39Sopenharmony_ci if (!crl_extension_match(delta, base, NID_issuing_distribution_point)) 1159e1051a39Sopenharmony_ci return 0; 1160e1051a39Sopenharmony_ci /* Delta CRL base number must not exceed Full CRL number. */ 1161e1051a39Sopenharmony_ci if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0) 1162e1051a39Sopenharmony_ci return 0; 1163e1051a39Sopenharmony_ci /* Delta CRL number must exceed full CRL number */ 1164e1051a39Sopenharmony_ci return ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0; 1165e1051a39Sopenharmony_ci} 1166e1051a39Sopenharmony_ci 1167e1051a39Sopenharmony_ci/* 1168e1051a39Sopenharmony_ci * For a given base CRL find a delta... maybe extend to delta scoring or 1169e1051a39Sopenharmony_ci * retrieve a chain of deltas... 1170e1051a39Sopenharmony_ci */ 1171e1051a39Sopenharmony_cistatic void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, 1172e1051a39Sopenharmony_ci X509_CRL *base, STACK_OF(X509_CRL) *crls) 1173e1051a39Sopenharmony_ci{ 1174e1051a39Sopenharmony_ci X509_CRL *delta; 1175e1051a39Sopenharmony_ci int i; 1176e1051a39Sopenharmony_ci 1177e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_USE_DELTAS) == 0) 1178e1051a39Sopenharmony_ci return; 1179e1051a39Sopenharmony_ci if (((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST) == 0) 1180e1051a39Sopenharmony_ci return; 1181e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_CRL_num(crls); i++) { 1182e1051a39Sopenharmony_ci delta = sk_X509_CRL_value(crls, i); 1183e1051a39Sopenharmony_ci if (check_delta_base(delta, base)) { 1184e1051a39Sopenharmony_ci if (check_crl_time(ctx, delta, 0)) 1185e1051a39Sopenharmony_ci *pscore |= CRL_SCORE_TIME_DELTA; 1186e1051a39Sopenharmony_ci X509_CRL_up_ref(delta); 1187e1051a39Sopenharmony_ci *dcrl = delta; 1188e1051a39Sopenharmony_ci return; 1189e1051a39Sopenharmony_ci } 1190e1051a39Sopenharmony_ci } 1191e1051a39Sopenharmony_ci *dcrl = NULL; 1192e1051a39Sopenharmony_ci} 1193e1051a39Sopenharmony_ci 1194e1051a39Sopenharmony_ci/* 1195e1051a39Sopenharmony_ci * For a given CRL return how suitable it is for the supplied certificate 1196e1051a39Sopenharmony_ci * 'x'. The return value is a mask of several criteria. If the issuer is not 1197e1051a39Sopenharmony_ci * the certificate issuer this is returned in *pissuer. The reasons mask is 1198e1051a39Sopenharmony_ci * also used to determine if the CRL is suitable: if no new reasons the CRL 1199e1051a39Sopenharmony_ci * is rejected, otherwise reasons is updated. 1200e1051a39Sopenharmony_ci */ 1201e1051a39Sopenharmony_cistatic int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, 1202e1051a39Sopenharmony_ci unsigned int *preasons, X509_CRL *crl, X509 *x) 1203e1051a39Sopenharmony_ci{ 1204e1051a39Sopenharmony_ci int crl_score = 0; 1205e1051a39Sopenharmony_ci unsigned int tmp_reasons = *preasons, crl_reasons; 1206e1051a39Sopenharmony_ci 1207e1051a39Sopenharmony_ci /* First see if we can reject CRL straight away */ 1208e1051a39Sopenharmony_ci 1209e1051a39Sopenharmony_ci /* Invalid IDP cannot be processed */ 1210e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_INVALID) != 0) 1211e1051a39Sopenharmony_ci return 0; 1212e1051a39Sopenharmony_ci /* Reason codes or indirect CRLs need extended CRL support */ 1213e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT) == 0) { 1214e1051a39Sopenharmony_ci if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) 1215e1051a39Sopenharmony_ci return 0; 1216e1051a39Sopenharmony_ci } else if ((crl->idp_flags & IDP_REASONS) != 0) { 1217e1051a39Sopenharmony_ci /* If no new reasons reject */ 1218e1051a39Sopenharmony_ci if ((crl->idp_reasons & ~tmp_reasons) == 0) 1219e1051a39Sopenharmony_ci return 0; 1220e1051a39Sopenharmony_ci } 1221e1051a39Sopenharmony_ci /* Don't process deltas at this stage */ 1222e1051a39Sopenharmony_ci else if (crl->base_crl_number != NULL) 1223e1051a39Sopenharmony_ci return 0; 1224e1051a39Sopenharmony_ci /* If issuer name doesn't match certificate need indirect CRL */ 1225e1051a39Sopenharmony_ci if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl)) != 0) { 1226e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_INDIRECT) == 0) 1227e1051a39Sopenharmony_ci return 0; 1228e1051a39Sopenharmony_ci } else { 1229e1051a39Sopenharmony_ci crl_score |= CRL_SCORE_ISSUER_NAME; 1230e1051a39Sopenharmony_ci } 1231e1051a39Sopenharmony_ci 1232e1051a39Sopenharmony_ci if ((crl->flags & EXFLAG_CRITICAL) == 0) 1233e1051a39Sopenharmony_ci crl_score |= CRL_SCORE_NOCRITICAL; 1234e1051a39Sopenharmony_ci 1235e1051a39Sopenharmony_ci /* Check expiration */ 1236e1051a39Sopenharmony_ci if (check_crl_time(ctx, crl, 0)) 1237e1051a39Sopenharmony_ci crl_score |= CRL_SCORE_TIME; 1238e1051a39Sopenharmony_ci 1239e1051a39Sopenharmony_ci /* Check authority key ID and locate certificate issuer */ 1240e1051a39Sopenharmony_ci crl_akid_check(ctx, crl, pissuer, &crl_score); 1241e1051a39Sopenharmony_ci 1242e1051a39Sopenharmony_ci /* If we can't locate certificate issuer at this point forget it */ 1243e1051a39Sopenharmony_ci if ((crl_score & CRL_SCORE_AKID) == 0) 1244e1051a39Sopenharmony_ci return 0; 1245e1051a39Sopenharmony_ci 1246e1051a39Sopenharmony_ci /* Check cert for matching CRL distribution points */ 1247e1051a39Sopenharmony_ci if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) { 1248e1051a39Sopenharmony_ci /* If no new reasons reject */ 1249e1051a39Sopenharmony_ci if ((crl_reasons & ~tmp_reasons) == 0) 1250e1051a39Sopenharmony_ci return 0; 1251e1051a39Sopenharmony_ci tmp_reasons |= crl_reasons; 1252e1051a39Sopenharmony_ci crl_score |= CRL_SCORE_SCOPE; 1253e1051a39Sopenharmony_ci } 1254e1051a39Sopenharmony_ci 1255e1051a39Sopenharmony_ci *preasons = tmp_reasons; 1256e1051a39Sopenharmony_ci 1257e1051a39Sopenharmony_ci return crl_score; 1258e1051a39Sopenharmony_ci 1259e1051a39Sopenharmony_ci} 1260e1051a39Sopenharmony_ci 1261e1051a39Sopenharmony_cistatic void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, 1262e1051a39Sopenharmony_ci X509 **pissuer, int *pcrl_score) 1263e1051a39Sopenharmony_ci{ 1264e1051a39Sopenharmony_ci X509 *crl_issuer = NULL; 1265e1051a39Sopenharmony_ci const X509_NAME *cnm = X509_CRL_get_issuer(crl); 1266e1051a39Sopenharmony_ci int cidx = ctx->error_depth; 1267e1051a39Sopenharmony_ci int i; 1268e1051a39Sopenharmony_ci 1269e1051a39Sopenharmony_ci if (cidx != sk_X509_num(ctx->chain) - 1) 1270e1051a39Sopenharmony_ci cidx++; 1271e1051a39Sopenharmony_ci 1272e1051a39Sopenharmony_ci crl_issuer = sk_X509_value(ctx->chain, cidx); 1273e1051a39Sopenharmony_ci 1274e1051a39Sopenharmony_ci if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { 1275e1051a39Sopenharmony_ci if (*pcrl_score & CRL_SCORE_ISSUER_NAME) { 1276e1051a39Sopenharmony_ci *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; 1277e1051a39Sopenharmony_ci *pissuer = crl_issuer; 1278e1051a39Sopenharmony_ci return; 1279e1051a39Sopenharmony_ci } 1280e1051a39Sopenharmony_ci } 1281e1051a39Sopenharmony_ci 1282e1051a39Sopenharmony_ci for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) { 1283e1051a39Sopenharmony_ci crl_issuer = sk_X509_value(ctx->chain, cidx); 1284e1051a39Sopenharmony_ci if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) 1285e1051a39Sopenharmony_ci continue; 1286e1051a39Sopenharmony_ci if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { 1287e1051a39Sopenharmony_ci *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; 1288e1051a39Sopenharmony_ci *pissuer = crl_issuer; 1289e1051a39Sopenharmony_ci return; 1290e1051a39Sopenharmony_ci } 1291e1051a39Sopenharmony_ci } 1292e1051a39Sopenharmony_ci 1293e1051a39Sopenharmony_ci /* Anything else needs extended CRL support */ 1294e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT) == 0) 1295e1051a39Sopenharmony_ci return; 1296e1051a39Sopenharmony_ci 1297e1051a39Sopenharmony_ci /* 1298e1051a39Sopenharmony_ci * Otherwise the CRL issuer is not on the path. Look for it in the set of 1299e1051a39Sopenharmony_ci * untrusted certificates. 1300e1051a39Sopenharmony_ci */ 1301e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(ctx->untrusted); i++) { 1302e1051a39Sopenharmony_ci crl_issuer = sk_X509_value(ctx->untrusted, i); 1303e1051a39Sopenharmony_ci if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm) != 0) 1304e1051a39Sopenharmony_ci continue; 1305e1051a39Sopenharmony_ci if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { 1306e1051a39Sopenharmony_ci *pissuer = crl_issuer; 1307e1051a39Sopenharmony_ci *pcrl_score |= CRL_SCORE_AKID; 1308e1051a39Sopenharmony_ci return; 1309e1051a39Sopenharmony_ci } 1310e1051a39Sopenharmony_ci } 1311e1051a39Sopenharmony_ci} 1312e1051a39Sopenharmony_ci 1313e1051a39Sopenharmony_ci/* 1314e1051a39Sopenharmony_ci * Check the path of a CRL issuer certificate. This creates a new 1315e1051a39Sopenharmony_ci * X509_STORE_CTX and populates it with most of the parameters from the 1316e1051a39Sopenharmony_ci * parent. This could be optimised somewhat since a lot of path checking will 1317e1051a39Sopenharmony_ci * be duplicated by the parent, but this will rarely be used in practice. 1318e1051a39Sopenharmony_ci */ 1319e1051a39Sopenharmony_cistatic int check_crl_path(X509_STORE_CTX *ctx, X509 *x) 1320e1051a39Sopenharmony_ci{ 1321e1051a39Sopenharmony_ci X509_STORE_CTX crl_ctx = {0}; 1322e1051a39Sopenharmony_ci int ret; 1323e1051a39Sopenharmony_ci 1324e1051a39Sopenharmony_ci /* Don't allow recursive CRL path validation */ 1325e1051a39Sopenharmony_ci if (ctx->parent != NULL) 1326e1051a39Sopenharmony_ci return 0; 1327e1051a39Sopenharmony_ci if (!X509_STORE_CTX_init(&crl_ctx, ctx->store, x, ctx->untrusted)) 1328e1051a39Sopenharmony_ci return -1; 1329e1051a39Sopenharmony_ci 1330e1051a39Sopenharmony_ci crl_ctx.crls = ctx->crls; 1331e1051a39Sopenharmony_ci /* Copy verify params across */ 1332e1051a39Sopenharmony_ci X509_STORE_CTX_set0_param(&crl_ctx, ctx->param); 1333e1051a39Sopenharmony_ci 1334e1051a39Sopenharmony_ci crl_ctx.parent = ctx; 1335e1051a39Sopenharmony_ci crl_ctx.verify_cb = ctx->verify_cb; 1336e1051a39Sopenharmony_ci 1337e1051a39Sopenharmony_ci /* Verify CRL issuer */ 1338e1051a39Sopenharmony_ci ret = X509_verify_cert(&crl_ctx); 1339e1051a39Sopenharmony_ci if (ret <= 0) 1340e1051a39Sopenharmony_ci goto err; 1341e1051a39Sopenharmony_ci 1342e1051a39Sopenharmony_ci /* Check chain is acceptable */ 1343e1051a39Sopenharmony_ci ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain); 1344e1051a39Sopenharmony_ci err: 1345e1051a39Sopenharmony_ci X509_STORE_CTX_cleanup(&crl_ctx); 1346e1051a39Sopenharmony_ci return ret; 1347e1051a39Sopenharmony_ci} 1348e1051a39Sopenharmony_ci 1349e1051a39Sopenharmony_ci/* 1350e1051a39Sopenharmony_ci * RFC3280 says nothing about the relationship between CRL path and 1351e1051a39Sopenharmony_ci * certificate path, which could lead to situations where a certificate could 1352e1051a39Sopenharmony_ci * be revoked or validated by a CA not authorized to do so. RFC5280 is more 1353e1051a39Sopenharmony_ci * strict and states that the two paths must end in the same trust anchor, 1354e1051a39Sopenharmony_ci * though some discussions remain... until this is resolved we use the 1355e1051a39Sopenharmony_ci * RFC5280 version 1356e1051a39Sopenharmony_ci */ 1357e1051a39Sopenharmony_cistatic int check_crl_chain(X509_STORE_CTX *ctx, 1358e1051a39Sopenharmony_ci STACK_OF(X509) *cert_path, 1359e1051a39Sopenharmony_ci STACK_OF(X509) *crl_path) 1360e1051a39Sopenharmony_ci{ 1361e1051a39Sopenharmony_ci X509 *cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1); 1362e1051a39Sopenharmony_ci X509 *crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1); 1363e1051a39Sopenharmony_ci 1364e1051a39Sopenharmony_ci return X509_cmp(cert_ta, crl_ta) == 0; 1365e1051a39Sopenharmony_ci} 1366e1051a39Sopenharmony_ci 1367e1051a39Sopenharmony_ci/*- 1368e1051a39Sopenharmony_ci * Check for match between two dist point names: three separate cases. 1369e1051a39Sopenharmony_ci * 1. Both are relative names and compare X509_NAME types. 1370e1051a39Sopenharmony_ci * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES. 1371e1051a39Sopenharmony_ci * 3. Both are full names and compare two GENERAL_NAMES. 1372e1051a39Sopenharmony_ci * 4. One is NULL: automatic match. 1373e1051a39Sopenharmony_ci */ 1374e1051a39Sopenharmony_cistatic int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) 1375e1051a39Sopenharmony_ci{ 1376e1051a39Sopenharmony_ci X509_NAME *nm = NULL; 1377e1051a39Sopenharmony_ci GENERAL_NAMES *gens = NULL; 1378e1051a39Sopenharmony_ci GENERAL_NAME *gena, *genb; 1379e1051a39Sopenharmony_ci int i, j; 1380e1051a39Sopenharmony_ci 1381e1051a39Sopenharmony_ci if (a == NULL || b == NULL) 1382e1051a39Sopenharmony_ci return 1; 1383e1051a39Sopenharmony_ci if (a->type == 1) { 1384e1051a39Sopenharmony_ci if (a->dpname == NULL) 1385e1051a39Sopenharmony_ci return 0; 1386e1051a39Sopenharmony_ci /* Case 1: two X509_NAME */ 1387e1051a39Sopenharmony_ci if (b->type == 1) { 1388e1051a39Sopenharmony_ci if (b->dpname == NULL) 1389e1051a39Sopenharmony_ci return 0; 1390e1051a39Sopenharmony_ci return X509_NAME_cmp(a->dpname, b->dpname) == 0; 1391e1051a39Sopenharmony_ci } 1392e1051a39Sopenharmony_ci /* Case 2: set name and GENERAL_NAMES appropriately */ 1393e1051a39Sopenharmony_ci nm = a->dpname; 1394e1051a39Sopenharmony_ci gens = b->name.fullname; 1395e1051a39Sopenharmony_ci } else if (b->type == 1) { 1396e1051a39Sopenharmony_ci if (b->dpname == NULL) 1397e1051a39Sopenharmony_ci return 0; 1398e1051a39Sopenharmony_ci /* Case 2: set name and GENERAL_NAMES appropriately */ 1399e1051a39Sopenharmony_ci gens = a->name.fullname; 1400e1051a39Sopenharmony_ci nm = b->dpname; 1401e1051a39Sopenharmony_ci } 1402e1051a39Sopenharmony_ci 1403e1051a39Sopenharmony_ci /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */ 1404e1051a39Sopenharmony_ci if (nm != NULL) { 1405e1051a39Sopenharmony_ci for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { 1406e1051a39Sopenharmony_ci gena = sk_GENERAL_NAME_value(gens, i); 1407e1051a39Sopenharmony_ci if (gena->type != GEN_DIRNAME) 1408e1051a39Sopenharmony_ci continue; 1409e1051a39Sopenharmony_ci if (X509_NAME_cmp(nm, gena->d.directoryName) == 0) 1410e1051a39Sopenharmony_ci return 1; 1411e1051a39Sopenharmony_ci } 1412e1051a39Sopenharmony_ci return 0; 1413e1051a39Sopenharmony_ci } 1414e1051a39Sopenharmony_ci 1415e1051a39Sopenharmony_ci /* Else case 3: two GENERAL_NAMES */ 1416e1051a39Sopenharmony_ci 1417e1051a39Sopenharmony_ci for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { 1418e1051a39Sopenharmony_ci gena = sk_GENERAL_NAME_value(a->name.fullname, i); 1419e1051a39Sopenharmony_ci for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { 1420e1051a39Sopenharmony_ci genb = sk_GENERAL_NAME_value(b->name.fullname, j); 1421e1051a39Sopenharmony_ci if (GENERAL_NAME_cmp(gena, genb) == 0) 1422e1051a39Sopenharmony_ci return 1; 1423e1051a39Sopenharmony_ci } 1424e1051a39Sopenharmony_ci } 1425e1051a39Sopenharmony_ci 1426e1051a39Sopenharmony_ci return 0; 1427e1051a39Sopenharmony_ci 1428e1051a39Sopenharmony_ci} 1429e1051a39Sopenharmony_ci 1430e1051a39Sopenharmony_cistatic int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score) 1431e1051a39Sopenharmony_ci{ 1432e1051a39Sopenharmony_ci int i; 1433e1051a39Sopenharmony_ci const X509_NAME *nm = X509_CRL_get_issuer(crl); 1434e1051a39Sopenharmony_ci 1435e1051a39Sopenharmony_ci /* If no CRLissuer return is successful iff don't need a match */ 1436e1051a39Sopenharmony_ci if (dp->CRLissuer == NULL) 1437e1051a39Sopenharmony_ci return (crl_score & CRL_SCORE_ISSUER_NAME) != 0; 1438e1051a39Sopenharmony_ci for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { 1439e1051a39Sopenharmony_ci GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); 1440e1051a39Sopenharmony_ci 1441e1051a39Sopenharmony_ci if (gen->type != GEN_DIRNAME) 1442e1051a39Sopenharmony_ci continue; 1443e1051a39Sopenharmony_ci if (X509_NAME_cmp(gen->d.directoryName, nm) == 0) 1444e1051a39Sopenharmony_ci return 1; 1445e1051a39Sopenharmony_ci } 1446e1051a39Sopenharmony_ci return 0; 1447e1051a39Sopenharmony_ci} 1448e1051a39Sopenharmony_ci 1449e1051a39Sopenharmony_ci/* Check CRLDP and IDP */ 1450e1051a39Sopenharmony_cistatic int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, 1451e1051a39Sopenharmony_ci unsigned int *preasons) 1452e1051a39Sopenharmony_ci{ 1453e1051a39Sopenharmony_ci int i; 1454e1051a39Sopenharmony_ci 1455e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_ONLYATTR) != 0) 1456e1051a39Sopenharmony_ci return 0; 1457e1051a39Sopenharmony_ci if ((x->ex_flags & EXFLAG_CA) != 0) { 1458e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_ONLYUSER) != 0) 1459e1051a39Sopenharmony_ci return 0; 1460e1051a39Sopenharmony_ci } else { 1461e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_ONLYCA) != 0) 1462e1051a39Sopenharmony_ci return 0; 1463e1051a39Sopenharmony_ci } 1464e1051a39Sopenharmony_ci *preasons = crl->idp_reasons; 1465e1051a39Sopenharmony_ci for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { 1466e1051a39Sopenharmony_ci DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i); 1467e1051a39Sopenharmony_ci 1468e1051a39Sopenharmony_ci if (crldp_check_crlissuer(dp, crl, crl_score)) { 1469e1051a39Sopenharmony_ci if (crl->idp == NULL 1470e1051a39Sopenharmony_ci || idp_check_dp(dp->distpoint, crl->idp->distpoint)) { 1471e1051a39Sopenharmony_ci *preasons &= dp->dp_reasons; 1472e1051a39Sopenharmony_ci return 1; 1473e1051a39Sopenharmony_ci } 1474e1051a39Sopenharmony_ci } 1475e1051a39Sopenharmony_ci } 1476e1051a39Sopenharmony_ci return (crl->idp == NULL || crl->idp->distpoint == NULL) 1477e1051a39Sopenharmony_ci && (crl_score & CRL_SCORE_ISSUER_NAME) != 0; 1478e1051a39Sopenharmony_ci} 1479e1051a39Sopenharmony_ci 1480e1051a39Sopenharmony_ci/* 1481e1051a39Sopenharmony_ci * Retrieve CRL corresponding to current certificate. If deltas enabled try 1482e1051a39Sopenharmony_ci * to find a delta CRL too 1483e1051a39Sopenharmony_ci */ 1484e1051a39Sopenharmony_cistatic int get_crl_delta(X509_STORE_CTX *ctx, 1485e1051a39Sopenharmony_ci X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x) 1486e1051a39Sopenharmony_ci{ 1487e1051a39Sopenharmony_ci int ok; 1488e1051a39Sopenharmony_ci X509 *issuer = NULL; 1489e1051a39Sopenharmony_ci int crl_score = 0; 1490e1051a39Sopenharmony_ci unsigned int reasons; 1491e1051a39Sopenharmony_ci X509_CRL *crl = NULL, *dcrl = NULL; 1492e1051a39Sopenharmony_ci STACK_OF(X509_CRL) *skcrl; 1493e1051a39Sopenharmony_ci const X509_NAME *nm = X509_get_issuer_name(x); 1494e1051a39Sopenharmony_ci 1495e1051a39Sopenharmony_ci reasons = ctx->current_reasons; 1496e1051a39Sopenharmony_ci ok = get_crl_sk(ctx, &crl, &dcrl, 1497e1051a39Sopenharmony_ci &issuer, &crl_score, &reasons, ctx->crls); 1498e1051a39Sopenharmony_ci if (ok) 1499e1051a39Sopenharmony_ci goto done; 1500e1051a39Sopenharmony_ci 1501e1051a39Sopenharmony_ci /* Lookup CRLs from store */ 1502e1051a39Sopenharmony_ci skcrl = ctx->lookup_crls(ctx, nm); 1503e1051a39Sopenharmony_ci 1504e1051a39Sopenharmony_ci /* If no CRLs found and a near match from get_crl_sk use that */ 1505e1051a39Sopenharmony_ci if (skcrl == NULL && crl != NULL) 1506e1051a39Sopenharmony_ci goto done; 1507e1051a39Sopenharmony_ci 1508e1051a39Sopenharmony_ci get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl); 1509e1051a39Sopenharmony_ci 1510e1051a39Sopenharmony_ci sk_X509_CRL_pop_free(skcrl, X509_CRL_free); 1511e1051a39Sopenharmony_ci 1512e1051a39Sopenharmony_ci done: 1513e1051a39Sopenharmony_ci /* If we got any kind of CRL use it and return success */ 1514e1051a39Sopenharmony_ci if (crl != NULL) { 1515e1051a39Sopenharmony_ci ctx->current_issuer = issuer; 1516e1051a39Sopenharmony_ci ctx->current_crl_score = crl_score; 1517e1051a39Sopenharmony_ci ctx->current_reasons = reasons; 1518e1051a39Sopenharmony_ci *pcrl = crl; 1519e1051a39Sopenharmony_ci *pdcrl = dcrl; 1520e1051a39Sopenharmony_ci return 1; 1521e1051a39Sopenharmony_ci } 1522e1051a39Sopenharmony_ci return 0; 1523e1051a39Sopenharmony_ci} 1524e1051a39Sopenharmony_ci 1525e1051a39Sopenharmony_ci/* Check CRL validity */ 1526e1051a39Sopenharmony_cistatic int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) 1527e1051a39Sopenharmony_ci{ 1528e1051a39Sopenharmony_ci X509 *issuer = NULL; 1529e1051a39Sopenharmony_ci EVP_PKEY *ikey = NULL; 1530e1051a39Sopenharmony_ci int cnum = ctx->error_depth; 1531e1051a39Sopenharmony_ci int chnum = sk_X509_num(ctx->chain) - 1; 1532e1051a39Sopenharmony_ci 1533e1051a39Sopenharmony_ci /* If we have an alternative CRL issuer cert use that */ 1534e1051a39Sopenharmony_ci if (ctx->current_issuer != NULL) { 1535e1051a39Sopenharmony_ci issuer = ctx->current_issuer; 1536e1051a39Sopenharmony_ci /* 1537e1051a39Sopenharmony_ci * Else find CRL issuer: if not last certificate then issuer is next 1538e1051a39Sopenharmony_ci * certificate in chain. 1539e1051a39Sopenharmony_ci */ 1540e1051a39Sopenharmony_ci } else if (cnum < chnum) { 1541e1051a39Sopenharmony_ci issuer = sk_X509_value(ctx->chain, cnum + 1); 1542e1051a39Sopenharmony_ci } else { 1543e1051a39Sopenharmony_ci issuer = sk_X509_value(ctx->chain, chnum); 1544e1051a39Sopenharmony_ci if (!ossl_assert(issuer != NULL)) 1545e1051a39Sopenharmony_ci return 0; 1546e1051a39Sopenharmony_ci /* If not self-issued, can't check signature */ 1547e1051a39Sopenharmony_ci if (!ctx->check_issued(ctx, issuer, issuer) && 1548e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER)) 1549e1051a39Sopenharmony_ci return 0; 1550e1051a39Sopenharmony_ci } 1551e1051a39Sopenharmony_ci 1552e1051a39Sopenharmony_ci if (issuer == NULL) 1553e1051a39Sopenharmony_ci return 1; 1554e1051a39Sopenharmony_ci 1555e1051a39Sopenharmony_ci /* 1556e1051a39Sopenharmony_ci * Skip most tests for deltas because they have already been done 1557e1051a39Sopenharmony_ci */ 1558e1051a39Sopenharmony_ci if (crl->base_crl_number == NULL) { 1559e1051a39Sopenharmony_ci /* Check for cRLSign bit if keyUsage present */ 1560e1051a39Sopenharmony_ci if ((issuer->ex_flags & EXFLAG_KUSAGE) != 0 && 1561e1051a39Sopenharmony_ci (issuer->ex_kusage & KU_CRL_SIGN) == 0 && 1562e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN)) 1563e1051a39Sopenharmony_ci return 0; 1564e1051a39Sopenharmony_ci 1565e1051a39Sopenharmony_ci if ((ctx->current_crl_score & CRL_SCORE_SCOPE) == 0 && 1566e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_DIFFERENT_CRL_SCOPE)) 1567e1051a39Sopenharmony_ci return 0; 1568e1051a39Sopenharmony_ci 1569e1051a39Sopenharmony_ci if ((ctx->current_crl_score & CRL_SCORE_SAME_PATH) == 0 && 1570e1051a39Sopenharmony_ci check_crl_path(ctx, ctx->current_issuer) <= 0 && 1571e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_CRL_PATH_VALIDATION_ERROR)) 1572e1051a39Sopenharmony_ci return 0; 1573e1051a39Sopenharmony_ci 1574e1051a39Sopenharmony_ci if ((crl->idp_flags & IDP_INVALID) != 0 && 1575e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_INVALID_EXTENSION)) 1576e1051a39Sopenharmony_ci return 0; 1577e1051a39Sopenharmony_ci } 1578e1051a39Sopenharmony_ci 1579e1051a39Sopenharmony_ci if ((ctx->current_crl_score & CRL_SCORE_TIME) == 0 && 1580e1051a39Sopenharmony_ci !check_crl_time(ctx, crl, 1)) 1581e1051a39Sopenharmony_ci return 0; 1582e1051a39Sopenharmony_ci 1583e1051a39Sopenharmony_ci /* Attempt to get issuer certificate public key */ 1584e1051a39Sopenharmony_ci ikey = X509_get0_pubkey(issuer); 1585e1051a39Sopenharmony_ci if (ikey == NULL && 1586e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY)) 1587e1051a39Sopenharmony_ci return 0; 1588e1051a39Sopenharmony_ci 1589e1051a39Sopenharmony_ci if (ikey != NULL) { 1590e1051a39Sopenharmony_ci int rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags); 1591e1051a39Sopenharmony_ci 1592e1051a39Sopenharmony_ci if (rv != X509_V_OK && !verify_cb_crl(ctx, rv)) 1593e1051a39Sopenharmony_ci return 0; 1594e1051a39Sopenharmony_ci /* Verify CRL signature */ 1595e1051a39Sopenharmony_ci if (X509_CRL_verify(crl, ikey) <= 0 && 1596e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE)) 1597e1051a39Sopenharmony_ci return 0; 1598e1051a39Sopenharmony_ci } 1599e1051a39Sopenharmony_ci return 1; 1600e1051a39Sopenharmony_ci} 1601e1051a39Sopenharmony_ci 1602e1051a39Sopenharmony_ci/* Check certificate against CRL */ 1603e1051a39Sopenharmony_cistatic int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) 1604e1051a39Sopenharmony_ci{ 1605e1051a39Sopenharmony_ci X509_REVOKED *rev; 1606e1051a39Sopenharmony_ci 1607e1051a39Sopenharmony_ci /* 1608e1051a39Sopenharmony_ci * The rules changed for this... previously if a CRL contained unhandled 1609e1051a39Sopenharmony_ci * critical extensions it could still be used to indicate a certificate 1610e1051a39Sopenharmony_ci * was revoked. This has since been changed since critical extensions can 1611e1051a39Sopenharmony_ci * change the meaning of CRL entries. 1612e1051a39Sopenharmony_ci */ 1613e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) == 0 1614e1051a39Sopenharmony_ci && (crl->flags & EXFLAG_CRITICAL) != 0 && 1615e1051a39Sopenharmony_ci !verify_cb_crl(ctx, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION)) 1616e1051a39Sopenharmony_ci return 0; 1617e1051a39Sopenharmony_ci /* 1618e1051a39Sopenharmony_ci * Look for serial number of certificate in CRL. If found, make sure 1619e1051a39Sopenharmony_ci * reason is not removeFromCRL. 1620e1051a39Sopenharmony_ci */ 1621e1051a39Sopenharmony_ci if (X509_CRL_get0_by_cert(crl, &rev, x)) { 1622e1051a39Sopenharmony_ci if (rev->reason == CRL_REASON_REMOVE_FROM_CRL) 1623e1051a39Sopenharmony_ci return 2; 1624e1051a39Sopenharmony_ci if (!verify_cb_crl(ctx, X509_V_ERR_CERT_REVOKED)) 1625e1051a39Sopenharmony_ci return 0; 1626e1051a39Sopenharmony_ci } 1627e1051a39Sopenharmony_ci 1628e1051a39Sopenharmony_ci return 1; 1629e1051a39Sopenharmony_ci} 1630e1051a39Sopenharmony_ci 1631e1051a39Sopenharmony_cistatic int check_policy(X509_STORE_CTX *ctx) 1632e1051a39Sopenharmony_ci{ 1633e1051a39Sopenharmony_ci int ret; 1634e1051a39Sopenharmony_ci 1635e1051a39Sopenharmony_ci if (ctx->parent) 1636e1051a39Sopenharmony_ci return 1; 1637e1051a39Sopenharmony_ci /* 1638e1051a39Sopenharmony_ci * With DANE, the trust anchor might be a bare public key, not a 1639e1051a39Sopenharmony_ci * certificate! In that case our chain does not have the trust anchor 1640e1051a39Sopenharmony_ci * certificate as a top-most element. This comports well with RFC5280 1641e1051a39Sopenharmony_ci * chain verification, since there too, the trust anchor is not part of the 1642e1051a39Sopenharmony_ci * chain to be verified. In particular, X509_policy_check() does not look 1643e1051a39Sopenharmony_ci * at the TA cert, but assumes that it is present as the top-most chain 1644e1051a39Sopenharmony_ci * element. We therefore temporarily push a NULL cert onto the chain if it 1645e1051a39Sopenharmony_ci * was verified via a bare public key, and pop it off right after the 1646e1051a39Sopenharmony_ci * X509_policy_check() call. 1647e1051a39Sopenharmony_ci */ 1648e1051a39Sopenharmony_ci if (ctx->bare_ta_signed && !sk_X509_push(ctx->chain, NULL)) 1649e1051a39Sopenharmony_ci goto memerr; 1650e1051a39Sopenharmony_ci ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain, 1651e1051a39Sopenharmony_ci ctx->param->policies, ctx->param->flags); 1652e1051a39Sopenharmony_ci if (ctx->bare_ta_signed) 1653e1051a39Sopenharmony_ci (void)sk_X509_pop(ctx->chain); 1654e1051a39Sopenharmony_ci 1655e1051a39Sopenharmony_ci if (ret == X509_PCY_TREE_INTERNAL) 1656e1051a39Sopenharmony_ci goto memerr; 1657e1051a39Sopenharmony_ci /* Invalid or inconsistent extensions */ 1658e1051a39Sopenharmony_ci if (ret == X509_PCY_TREE_INVALID) { 1659e1051a39Sopenharmony_ci int i, cbcalled = 0; 1660e1051a39Sopenharmony_ci 1661e1051a39Sopenharmony_ci /* Locate certificates with bad extensions and notify callback. */ 1662e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(ctx->chain); i++) { 1663e1051a39Sopenharmony_ci X509 *x = sk_X509_value(ctx->chain, i); 1664e1051a39Sopenharmony_ci 1665e1051a39Sopenharmony_ci if ((x->ex_flags & EXFLAG_INVALID_POLICY) != 0) 1666e1051a39Sopenharmony_ci cbcalled = 1; 1667e1051a39Sopenharmony_ci CB_FAIL_IF((x->ex_flags & EXFLAG_INVALID_POLICY) != 0, 1668e1051a39Sopenharmony_ci ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION); 1669e1051a39Sopenharmony_ci } 1670e1051a39Sopenharmony_ci if (!cbcalled) { 1671e1051a39Sopenharmony_ci /* Should not be able to get here */ 1672e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); 1673e1051a39Sopenharmony_ci return 0; 1674e1051a39Sopenharmony_ci } 1675e1051a39Sopenharmony_ci /* The callback ignored the error so we return success */ 1676e1051a39Sopenharmony_ci return 1; 1677e1051a39Sopenharmony_ci } 1678e1051a39Sopenharmony_ci if (ret == X509_PCY_TREE_FAILURE) { 1679e1051a39Sopenharmony_ci ctx->current_cert = NULL; 1680e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY; 1681e1051a39Sopenharmony_ci return ctx->verify_cb(0, ctx); 1682e1051a39Sopenharmony_ci } 1683e1051a39Sopenharmony_ci if (ret != X509_PCY_TREE_VALID) { 1684e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); 1685e1051a39Sopenharmony_ci return 0; 1686e1051a39Sopenharmony_ci } 1687e1051a39Sopenharmony_ci 1688e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) != 0) { 1689e1051a39Sopenharmony_ci ctx->current_cert = NULL; 1690e1051a39Sopenharmony_ci /* 1691e1051a39Sopenharmony_ci * Verification errors need to be "sticky", a callback may have allowed 1692e1051a39Sopenharmony_ci * an SSL handshake to continue despite an error, and we must then 1693e1051a39Sopenharmony_ci * remain in an error state. Therefore, we MUST NOT clear earlier 1694e1051a39Sopenharmony_ci * verification errors by setting the error to X509_V_OK. 1695e1051a39Sopenharmony_ci */ 1696e1051a39Sopenharmony_ci if (!ctx->verify_cb(2, ctx)) 1697e1051a39Sopenharmony_ci return 0; 1698e1051a39Sopenharmony_ci } 1699e1051a39Sopenharmony_ci 1700e1051a39Sopenharmony_ci return 1; 1701e1051a39Sopenharmony_ci 1702e1051a39Sopenharmony_ci memerr: 1703e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 1704e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 1705e1051a39Sopenharmony_ci return -1; 1706e1051a39Sopenharmony_ci} 1707e1051a39Sopenharmony_ci 1708e1051a39Sopenharmony_ci/*- 1709e1051a39Sopenharmony_ci * Check certificate validity times. 1710e1051a39Sopenharmony_ci * If depth >= 0, invoke verification callbacks on error, otherwise just return 1711e1051a39Sopenharmony_ci * the validation status. 1712e1051a39Sopenharmony_ci * 1713e1051a39Sopenharmony_ci * Return 1 on success, 0 otherwise. 1714e1051a39Sopenharmony_ci */ 1715e1051a39Sopenharmony_ciint ossl_x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth) 1716e1051a39Sopenharmony_ci{ 1717e1051a39Sopenharmony_ci time_t *ptime; 1718e1051a39Sopenharmony_ci int i; 1719e1051a39Sopenharmony_ci 1720e1051a39Sopenharmony_ci if ((ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) != 0) 1721e1051a39Sopenharmony_ci ptime = &ctx->param->check_time; 1722e1051a39Sopenharmony_ci else if ((ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) != 0) 1723e1051a39Sopenharmony_ci return 1; 1724e1051a39Sopenharmony_ci else 1725e1051a39Sopenharmony_ci ptime = NULL; 1726e1051a39Sopenharmony_ci 1727e1051a39Sopenharmony_ci i = X509_cmp_time(X509_get0_notBefore(x), ptime); 1728e1051a39Sopenharmony_ci if (i >= 0 && depth < 0) 1729e1051a39Sopenharmony_ci return 0; 1730e1051a39Sopenharmony_ci CB_FAIL_IF(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD); 1731e1051a39Sopenharmony_ci CB_FAIL_IF(i > 0, ctx, x, depth, X509_V_ERR_CERT_NOT_YET_VALID); 1732e1051a39Sopenharmony_ci 1733e1051a39Sopenharmony_ci i = X509_cmp_time(X509_get0_notAfter(x), ptime); 1734e1051a39Sopenharmony_ci if (i <= 0 && depth < 0) 1735e1051a39Sopenharmony_ci return 0; 1736e1051a39Sopenharmony_ci CB_FAIL_IF(i == 0, ctx, x, depth, X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD); 1737e1051a39Sopenharmony_ci CB_FAIL_IF(i < 0, ctx, x, depth, X509_V_ERR_CERT_HAS_EXPIRED); 1738e1051a39Sopenharmony_ci return 1; 1739e1051a39Sopenharmony_ci} 1740e1051a39Sopenharmony_ci 1741e1051a39Sopenharmony_ci/* 1742e1051a39Sopenharmony_ci * Verify the issuer signatures and cert times of ctx->chain. 1743e1051a39Sopenharmony_ci * Sadly, returns 0 also on internal error. 1744e1051a39Sopenharmony_ci */ 1745e1051a39Sopenharmony_cistatic int internal_verify(X509_STORE_CTX *ctx) 1746e1051a39Sopenharmony_ci{ 1747e1051a39Sopenharmony_ci int n = sk_X509_num(ctx->chain) - 1; 1748e1051a39Sopenharmony_ci X509 *xi = sk_X509_value(ctx->chain, n); 1749e1051a39Sopenharmony_ci X509 *xs = xi; 1750e1051a39Sopenharmony_ci 1751e1051a39Sopenharmony_ci ctx->error_depth = n; 1752e1051a39Sopenharmony_ci if (ctx->bare_ta_signed) { 1753e1051a39Sopenharmony_ci /* 1754e1051a39Sopenharmony_ci * With DANE-verified bare public key TA signatures, 1755e1051a39Sopenharmony_ci * on the top certificate we check only the timestamps. 1756e1051a39Sopenharmony_ci * We report the issuer as NULL because all we have is a bare key. 1757e1051a39Sopenharmony_ci */ 1758e1051a39Sopenharmony_ci xi = NULL; 1759e1051a39Sopenharmony_ci } else if (ossl_x509_likely_issued(xi, xi) != X509_V_OK 1760e1051a39Sopenharmony_ci /* exceptional case: last cert in the chain is not self-issued */ 1761e1051a39Sopenharmony_ci && ((ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) == 0)) { 1762e1051a39Sopenharmony_ci if (n > 0) { 1763e1051a39Sopenharmony_ci n--; 1764e1051a39Sopenharmony_ci ctx->error_depth = n; 1765e1051a39Sopenharmony_ci xs = sk_X509_value(ctx->chain, n); 1766e1051a39Sopenharmony_ci } else { 1767e1051a39Sopenharmony_ci CB_FAIL_IF(1, ctx, xi, 0, 1768e1051a39Sopenharmony_ci X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE); 1769e1051a39Sopenharmony_ci } 1770e1051a39Sopenharmony_ci /* 1771e1051a39Sopenharmony_ci * The below code will certainly not do a 1772e1051a39Sopenharmony_ci * self-signature check on xi because it is not self-issued. 1773e1051a39Sopenharmony_ci */ 1774e1051a39Sopenharmony_ci } 1775e1051a39Sopenharmony_ci 1776e1051a39Sopenharmony_ci /* 1777e1051a39Sopenharmony_ci * Do not clear error (by ctx->error = X509_V_OK), it must be "sticky", 1778e1051a39Sopenharmony_ci * only the user's callback is allowed to reset errors (at its own peril). 1779e1051a39Sopenharmony_ci */ 1780e1051a39Sopenharmony_ci while (n >= 0) { 1781e1051a39Sopenharmony_ci /*- 1782e1051a39Sopenharmony_ci * For each iteration of this loop: 1783e1051a39Sopenharmony_ci * n is the subject depth 1784e1051a39Sopenharmony_ci * xs is the subject cert, for which the signature is to be checked 1785e1051a39Sopenharmony_ci * xi is NULL for DANE-verified bare public key TA signatures 1786e1051a39Sopenharmony_ci * else the supposed issuer cert containing the public key to use 1787e1051a39Sopenharmony_ci * Initially xs == xi if the last cert in the chain is self-issued. 1788e1051a39Sopenharmony_ci */ 1789e1051a39Sopenharmony_ci /* 1790e1051a39Sopenharmony_ci * Do signature check for self-signed certificates only if explicitly 1791e1051a39Sopenharmony_ci * asked for because it does not add any security and just wastes time. 1792e1051a39Sopenharmony_ci */ 1793e1051a39Sopenharmony_ci if (xi != NULL 1794e1051a39Sopenharmony_ci && (xs != xi 1795e1051a39Sopenharmony_ci || ((ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE) != 0 1796e1051a39Sopenharmony_ci && (xi->ex_flags & EXFLAG_SS) != 0))) { 1797e1051a39Sopenharmony_ci EVP_PKEY *pkey; 1798e1051a39Sopenharmony_ci /* 1799e1051a39Sopenharmony_ci * If the issuer's public key is not available or its key usage 1800e1051a39Sopenharmony_ci * does not support issuing the subject cert, report the issuer 1801e1051a39Sopenharmony_ci * cert and its depth (rather than n, the depth of the subject). 1802e1051a39Sopenharmony_ci */ 1803e1051a39Sopenharmony_ci int issuer_depth = n + (xs == xi ? 0 : 1); 1804e1051a39Sopenharmony_ci /* 1805e1051a39Sopenharmony_ci * According to https://tools.ietf.org/html/rfc5280#section-6.1.4 1806e1051a39Sopenharmony_ci * step (n) we must check any given key usage extension in a CA cert 1807e1051a39Sopenharmony_ci * when preparing the verification of a certificate issued by it. 1808e1051a39Sopenharmony_ci * According to https://tools.ietf.org/html/rfc5280#section-4.2.1.3 1809e1051a39Sopenharmony_ci * we must not verify a certificate signature if the key usage of 1810e1051a39Sopenharmony_ci * the CA certificate that issued the certificate prohibits signing. 1811e1051a39Sopenharmony_ci * In case the 'issuing' certificate is the last in the chain and is 1812e1051a39Sopenharmony_ci * not a CA certificate but a 'self-issued' end-entity cert (i.e., 1813e1051a39Sopenharmony_ci * xs == xi && !(xi->ex_flags & EXFLAG_CA)) RFC 5280 does not apply 1814e1051a39Sopenharmony_ci * (see https://tools.ietf.org/html/rfc6818#section-2) and thus 1815e1051a39Sopenharmony_ci * we are free to ignore any key usage restrictions on such certs. 1816e1051a39Sopenharmony_ci */ 1817e1051a39Sopenharmony_ci int ret = xs == xi && (xi->ex_flags & EXFLAG_CA) == 0 1818e1051a39Sopenharmony_ci ? X509_V_OK : ossl_x509_signing_allowed(xi, xs); 1819e1051a39Sopenharmony_ci 1820e1051a39Sopenharmony_ci CB_FAIL_IF(ret != X509_V_OK, ctx, xi, issuer_depth, ret); 1821e1051a39Sopenharmony_ci if ((pkey = X509_get0_pubkey(xi)) == NULL) { 1822e1051a39Sopenharmony_ci CB_FAIL_IF(1, ctx, xi, issuer_depth, 1823e1051a39Sopenharmony_ci X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY); 1824e1051a39Sopenharmony_ci } else { 1825e1051a39Sopenharmony_ci CB_FAIL_IF(X509_verify(xs, pkey) <= 0, 1826e1051a39Sopenharmony_ci ctx, xs, n, X509_V_ERR_CERT_SIGNATURE_FAILURE); 1827e1051a39Sopenharmony_ci } 1828e1051a39Sopenharmony_ci } 1829e1051a39Sopenharmony_ci 1830e1051a39Sopenharmony_ci /* In addition to RFC 5280 requirements do also for trust anchor cert */ 1831e1051a39Sopenharmony_ci /* Calls verify callback as needed */ 1832e1051a39Sopenharmony_ci if (!ossl_x509_check_cert_time(ctx, xs, n)) 1833e1051a39Sopenharmony_ci return 0; 1834e1051a39Sopenharmony_ci 1835e1051a39Sopenharmony_ci /* 1836e1051a39Sopenharmony_ci * Signal success at this depth. However, the previous error (if any) 1837e1051a39Sopenharmony_ci * is retained. 1838e1051a39Sopenharmony_ci */ 1839e1051a39Sopenharmony_ci ctx->current_issuer = xi; 1840e1051a39Sopenharmony_ci ctx->current_cert = xs; 1841e1051a39Sopenharmony_ci ctx->error_depth = n; 1842e1051a39Sopenharmony_ci if (!ctx->verify_cb(1, ctx)) 1843e1051a39Sopenharmony_ci return 0; 1844e1051a39Sopenharmony_ci 1845e1051a39Sopenharmony_ci if (--n >= 0) { 1846e1051a39Sopenharmony_ci xi = xs; 1847e1051a39Sopenharmony_ci xs = sk_X509_value(ctx->chain, n); 1848e1051a39Sopenharmony_ci } 1849e1051a39Sopenharmony_ci } 1850e1051a39Sopenharmony_ci return 1; 1851e1051a39Sopenharmony_ci} 1852e1051a39Sopenharmony_ci 1853e1051a39Sopenharmony_ciint X509_cmp_current_time(const ASN1_TIME *ctm) 1854e1051a39Sopenharmony_ci{ 1855e1051a39Sopenharmony_ci return X509_cmp_time(ctm, NULL); 1856e1051a39Sopenharmony_ci} 1857e1051a39Sopenharmony_ci 1858e1051a39Sopenharmony_ciint X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time) 1859e1051a39Sopenharmony_ci{ 1860e1051a39Sopenharmony_ci static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1; 1861e1051a39Sopenharmony_ci static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1; 1862e1051a39Sopenharmony_ci ASN1_TIME *asn1_cmp_time = NULL; 1863e1051a39Sopenharmony_ci int i, day, sec, ret = 0; 1864e1051a39Sopenharmony_ci#ifdef CHARSET_EBCDIC 1865e1051a39Sopenharmony_ci const char upper_z = 0x5A; 1866e1051a39Sopenharmony_ci#else 1867e1051a39Sopenharmony_ci const char upper_z = 'Z'; 1868e1051a39Sopenharmony_ci#endif 1869e1051a39Sopenharmony_ci 1870e1051a39Sopenharmony_ci /*- 1871e1051a39Sopenharmony_ci * Note that ASN.1 allows much more slack in the time format than RFC5280. 1872e1051a39Sopenharmony_ci * In RFC5280, the representation is fixed: 1873e1051a39Sopenharmony_ci * UTCTime: YYMMDDHHMMSSZ 1874e1051a39Sopenharmony_ci * GeneralizedTime: YYYYMMDDHHMMSSZ 1875e1051a39Sopenharmony_ci * 1876e1051a39Sopenharmony_ci * We do NOT currently enforce the following RFC 5280 requirement: 1877e1051a39Sopenharmony_ci * "CAs conforming to this profile MUST always encode certificate 1878e1051a39Sopenharmony_ci * validity dates through the year 2049 as UTCTime; certificate validity 1879e1051a39Sopenharmony_ci * dates in 2050 or later MUST be encoded as GeneralizedTime." 1880e1051a39Sopenharmony_ci */ 1881e1051a39Sopenharmony_ci switch (ctm->type) { 1882e1051a39Sopenharmony_ci case V_ASN1_UTCTIME: 1883e1051a39Sopenharmony_ci if (ctm->length != (int)(utctime_length)) 1884e1051a39Sopenharmony_ci return 0; 1885e1051a39Sopenharmony_ci break; 1886e1051a39Sopenharmony_ci case V_ASN1_GENERALIZEDTIME: 1887e1051a39Sopenharmony_ci if (ctm->length != (int)(generalizedtime_length)) 1888e1051a39Sopenharmony_ci return 0; 1889e1051a39Sopenharmony_ci break; 1890e1051a39Sopenharmony_ci default: 1891e1051a39Sopenharmony_ci return 0; 1892e1051a39Sopenharmony_ci } 1893e1051a39Sopenharmony_ci 1894e1051a39Sopenharmony_ci /** 1895e1051a39Sopenharmony_ci * Verify the format: the ASN.1 functions we use below allow a more 1896e1051a39Sopenharmony_ci * flexible format than what's mandated by RFC 5280. 1897e1051a39Sopenharmony_ci * Digit and date ranges will be verified in the conversion methods. 1898e1051a39Sopenharmony_ci */ 1899e1051a39Sopenharmony_ci for (i = 0; i < ctm->length - 1; i++) { 1900e1051a39Sopenharmony_ci if (!ossl_ascii_isdigit(ctm->data[i])) 1901e1051a39Sopenharmony_ci return 0; 1902e1051a39Sopenharmony_ci } 1903e1051a39Sopenharmony_ci if (ctm->data[ctm->length - 1] != upper_z) 1904e1051a39Sopenharmony_ci return 0; 1905e1051a39Sopenharmony_ci 1906e1051a39Sopenharmony_ci /* 1907e1051a39Sopenharmony_ci * There is ASN1_UTCTIME_cmp_time_t but no 1908e1051a39Sopenharmony_ci * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t, 1909e1051a39Sopenharmony_ci * so we go through ASN.1 1910e1051a39Sopenharmony_ci */ 1911e1051a39Sopenharmony_ci asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time); 1912e1051a39Sopenharmony_ci if (asn1_cmp_time == NULL) 1913e1051a39Sopenharmony_ci goto err; 1914e1051a39Sopenharmony_ci if (ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time) == 0) 1915e1051a39Sopenharmony_ci goto err; 1916e1051a39Sopenharmony_ci 1917e1051a39Sopenharmony_ci /* 1918e1051a39Sopenharmony_ci * X509_cmp_time comparison is <=. 1919e1051a39Sopenharmony_ci * The return value 0 is reserved for errors. 1920e1051a39Sopenharmony_ci */ 1921e1051a39Sopenharmony_ci ret = (day >= 0 && sec >= 0) ? -1 : 1; 1922e1051a39Sopenharmony_ci 1923e1051a39Sopenharmony_ci err: 1924e1051a39Sopenharmony_ci ASN1_TIME_free(asn1_cmp_time); 1925e1051a39Sopenharmony_ci return ret; 1926e1051a39Sopenharmony_ci} 1927e1051a39Sopenharmony_ci 1928e1051a39Sopenharmony_ci/* 1929e1051a39Sopenharmony_ci * Return 0 if time should not be checked or reference time is in range, 1930e1051a39Sopenharmony_ci * or else 1 if it is past the end, or -1 if it is before the start 1931e1051a39Sopenharmony_ci */ 1932e1051a39Sopenharmony_ciint X509_cmp_timeframe(const X509_VERIFY_PARAM *vpm, 1933e1051a39Sopenharmony_ci const ASN1_TIME *start, const ASN1_TIME *end) 1934e1051a39Sopenharmony_ci{ 1935e1051a39Sopenharmony_ci time_t ref_time; 1936e1051a39Sopenharmony_ci time_t *time = NULL; 1937e1051a39Sopenharmony_ci unsigned long flags = vpm == NULL ? 0 : X509_VERIFY_PARAM_get_flags(vpm); 1938e1051a39Sopenharmony_ci 1939e1051a39Sopenharmony_ci if ((flags & X509_V_FLAG_USE_CHECK_TIME) != 0) { 1940e1051a39Sopenharmony_ci ref_time = X509_VERIFY_PARAM_get_time(vpm); 1941e1051a39Sopenharmony_ci time = &ref_time; 1942e1051a39Sopenharmony_ci } else if ((flags & X509_V_FLAG_NO_CHECK_TIME) != 0) { 1943e1051a39Sopenharmony_ci return 0; /* this means ok */ 1944e1051a39Sopenharmony_ci } /* else reference time is the current time */ 1945e1051a39Sopenharmony_ci 1946e1051a39Sopenharmony_ci if (end != NULL && X509_cmp_time(end, time) < 0) 1947e1051a39Sopenharmony_ci return 1; 1948e1051a39Sopenharmony_ci if (start != NULL && X509_cmp_time(start, time) > 0) 1949e1051a39Sopenharmony_ci return -1; 1950e1051a39Sopenharmony_ci return 0; 1951e1051a39Sopenharmony_ci} 1952e1051a39Sopenharmony_ci 1953e1051a39Sopenharmony_ciASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj) 1954e1051a39Sopenharmony_ci{ 1955e1051a39Sopenharmony_ci return X509_time_adj(s, adj, NULL); 1956e1051a39Sopenharmony_ci} 1957e1051a39Sopenharmony_ci 1958e1051a39Sopenharmony_ciASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm) 1959e1051a39Sopenharmony_ci{ 1960e1051a39Sopenharmony_ci return X509_time_adj_ex(s, 0, offset_sec, in_tm); 1961e1051a39Sopenharmony_ci} 1962e1051a39Sopenharmony_ci 1963e1051a39Sopenharmony_ciASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, 1964e1051a39Sopenharmony_ci int offset_day, long offset_sec, time_t *in_tm) 1965e1051a39Sopenharmony_ci{ 1966e1051a39Sopenharmony_ci time_t t; 1967e1051a39Sopenharmony_ci 1968e1051a39Sopenharmony_ci if (in_tm) 1969e1051a39Sopenharmony_ci t = *in_tm; 1970e1051a39Sopenharmony_ci else 1971e1051a39Sopenharmony_ci time(&t); 1972e1051a39Sopenharmony_ci 1973e1051a39Sopenharmony_ci if (s != NULL && (s->flags & ASN1_STRING_FLAG_MSTRING) == 0) { 1974e1051a39Sopenharmony_ci if (s->type == V_ASN1_UTCTIME) 1975e1051a39Sopenharmony_ci return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec); 1976e1051a39Sopenharmony_ci if (s->type == V_ASN1_GENERALIZEDTIME) 1977e1051a39Sopenharmony_ci return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec); 1978e1051a39Sopenharmony_ci } 1979e1051a39Sopenharmony_ci return ASN1_TIME_adj(s, t, offset_day, offset_sec); 1980e1051a39Sopenharmony_ci} 1981e1051a39Sopenharmony_ci 1982e1051a39Sopenharmony_ci/* Copy any missing public key parameters up the chain towards pkey */ 1983e1051a39Sopenharmony_ciint X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) 1984e1051a39Sopenharmony_ci{ 1985e1051a39Sopenharmony_ci EVP_PKEY *ktmp = NULL, *ktmp2; 1986e1051a39Sopenharmony_ci int i, j; 1987e1051a39Sopenharmony_ci 1988e1051a39Sopenharmony_ci if (pkey != NULL && !EVP_PKEY_missing_parameters(pkey)) 1989e1051a39Sopenharmony_ci return 1; 1990e1051a39Sopenharmony_ci 1991e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_num(chain); i++) { 1992e1051a39Sopenharmony_ci ktmp = X509_get0_pubkey(sk_X509_value(chain, i)); 1993e1051a39Sopenharmony_ci if (ktmp == NULL) { 1994e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); 1995e1051a39Sopenharmony_ci return 0; 1996e1051a39Sopenharmony_ci } 1997e1051a39Sopenharmony_ci if (!EVP_PKEY_missing_parameters(ktmp)) 1998e1051a39Sopenharmony_ci break; 1999e1051a39Sopenharmony_ci ktmp = NULL; 2000e1051a39Sopenharmony_ci } 2001e1051a39Sopenharmony_ci if (ktmp == NULL) { 2002e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN); 2003e1051a39Sopenharmony_ci return 0; 2004e1051a39Sopenharmony_ci } 2005e1051a39Sopenharmony_ci 2006e1051a39Sopenharmony_ci /* first, populate the other certs */ 2007e1051a39Sopenharmony_ci for (j = i - 1; j >= 0; j--) { 2008e1051a39Sopenharmony_ci ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j)); 2009e1051a39Sopenharmony_ci if (!EVP_PKEY_copy_parameters(ktmp2, ktmp)) 2010e1051a39Sopenharmony_ci return 0; 2011e1051a39Sopenharmony_ci } 2012e1051a39Sopenharmony_ci 2013e1051a39Sopenharmony_ci if (pkey != NULL) 2014e1051a39Sopenharmony_ci return EVP_PKEY_copy_parameters(pkey, ktmp); 2015e1051a39Sopenharmony_ci return 1; 2016e1051a39Sopenharmony_ci} 2017e1051a39Sopenharmony_ci 2018e1051a39Sopenharmony_ci/* 2019e1051a39Sopenharmony_ci * Make a delta CRL as the difference between two full CRLs. 2020e1051a39Sopenharmony_ci * Sadly, returns NULL also on internal error. 2021e1051a39Sopenharmony_ci */ 2022e1051a39Sopenharmony_ciX509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, 2023e1051a39Sopenharmony_ci EVP_PKEY *skey, const EVP_MD *md, unsigned int flags) 2024e1051a39Sopenharmony_ci{ 2025e1051a39Sopenharmony_ci X509_CRL *crl = NULL; 2026e1051a39Sopenharmony_ci int i; 2027e1051a39Sopenharmony_ci 2028e1051a39Sopenharmony_ci STACK_OF(X509_REVOKED) *revs = NULL; 2029e1051a39Sopenharmony_ci /* CRLs can't be delta already */ 2030e1051a39Sopenharmony_ci if (base->base_crl_number != NULL || newer->base_crl_number != NULL) { 2031e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_CRL_ALREADY_DELTA); 2032e1051a39Sopenharmony_ci return NULL; 2033e1051a39Sopenharmony_ci } 2034e1051a39Sopenharmony_ci /* Base and new CRL must have a CRL number */ 2035e1051a39Sopenharmony_ci if (base->crl_number == NULL || newer->crl_number == NULL) { 2036e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_NO_CRL_NUMBER); 2037e1051a39Sopenharmony_ci return NULL; 2038e1051a39Sopenharmony_ci } 2039e1051a39Sopenharmony_ci /* Issuer names must match */ 2040e1051a39Sopenharmony_ci if (X509_NAME_cmp(X509_CRL_get_issuer(base), 2041e1051a39Sopenharmony_ci X509_CRL_get_issuer(newer)) != 0) { 2042e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_ISSUER_MISMATCH); 2043e1051a39Sopenharmony_ci return NULL; 2044e1051a39Sopenharmony_ci } 2045e1051a39Sopenharmony_ci /* AKID and IDP must match */ 2046e1051a39Sopenharmony_ci if (!crl_extension_match(base, newer, NID_authority_key_identifier)) { 2047e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_AKID_MISMATCH); 2048e1051a39Sopenharmony_ci return NULL; 2049e1051a39Sopenharmony_ci } 2050e1051a39Sopenharmony_ci if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) { 2051e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_IDP_MISMATCH); 2052e1051a39Sopenharmony_ci return NULL; 2053e1051a39Sopenharmony_ci } 2054e1051a39Sopenharmony_ci /* Newer CRL number must exceed full CRL number */ 2055e1051a39Sopenharmony_ci if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) { 2056e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_NEWER_CRL_NOT_NEWER); 2057e1051a39Sopenharmony_ci return NULL; 2058e1051a39Sopenharmony_ci } 2059e1051a39Sopenharmony_ci /* CRLs must verify */ 2060e1051a39Sopenharmony_ci if (skey != NULL && (X509_CRL_verify(base, skey) <= 0 || 2061e1051a39Sopenharmony_ci X509_CRL_verify(newer, skey) <= 0)) { 2062e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_CRL_VERIFY_FAILURE); 2063e1051a39Sopenharmony_ci return NULL; 2064e1051a39Sopenharmony_ci } 2065e1051a39Sopenharmony_ci /* Create new CRL */ 2066e1051a39Sopenharmony_ci crl = X509_CRL_new_ex(base->libctx, base->propq); 2067e1051a39Sopenharmony_ci if (crl == NULL || !X509_CRL_set_version(crl, X509_CRL_VERSION_2)) 2068e1051a39Sopenharmony_ci goto memerr; 2069e1051a39Sopenharmony_ci /* Set issuer name */ 2070e1051a39Sopenharmony_ci if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer))) 2071e1051a39Sopenharmony_ci goto memerr; 2072e1051a39Sopenharmony_ci 2073e1051a39Sopenharmony_ci if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer))) 2074e1051a39Sopenharmony_ci goto memerr; 2075e1051a39Sopenharmony_ci if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer))) 2076e1051a39Sopenharmony_ci goto memerr; 2077e1051a39Sopenharmony_ci 2078e1051a39Sopenharmony_ci /* Set base CRL number: must be critical */ 2079e1051a39Sopenharmony_ci if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0)) 2080e1051a39Sopenharmony_ci goto memerr; 2081e1051a39Sopenharmony_ci 2082e1051a39Sopenharmony_ci /* 2083e1051a39Sopenharmony_ci * Copy extensions across from newest CRL to delta: this will set CRL 2084e1051a39Sopenharmony_ci * number to correct value too. 2085e1051a39Sopenharmony_ci */ 2086e1051a39Sopenharmony_ci for (i = 0; i < X509_CRL_get_ext_count(newer); i++) { 2087e1051a39Sopenharmony_ci X509_EXTENSION *ext = X509_CRL_get_ext(newer, i); 2088e1051a39Sopenharmony_ci 2089e1051a39Sopenharmony_ci if (!X509_CRL_add_ext(crl, ext, -1)) 2090e1051a39Sopenharmony_ci goto memerr; 2091e1051a39Sopenharmony_ci } 2092e1051a39Sopenharmony_ci 2093e1051a39Sopenharmony_ci /* Go through revoked entries, copying as needed */ 2094e1051a39Sopenharmony_ci revs = X509_CRL_get_REVOKED(newer); 2095e1051a39Sopenharmony_ci 2096e1051a39Sopenharmony_ci for (i = 0; i < sk_X509_REVOKED_num(revs); i++) { 2097e1051a39Sopenharmony_ci X509_REVOKED *rvn, *rvtmp; 2098e1051a39Sopenharmony_ci 2099e1051a39Sopenharmony_ci rvn = sk_X509_REVOKED_value(revs, i); 2100e1051a39Sopenharmony_ci /* 2101e1051a39Sopenharmony_ci * Add only if not also in base. 2102e1051a39Sopenharmony_ci * Need something cleverer here for some more complex CRLs covering 2103e1051a39Sopenharmony_ci * multiple CAs. 2104e1051a39Sopenharmony_ci */ 2105e1051a39Sopenharmony_ci if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) { 2106e1051a39Sopenharmony_ci rvtmp = X509_REVOKED_dup(rvn); 2107e1051a39Sopenharmony_ci if (rvtmp == NULL) 2108e1051a39Sopenharmony_ci goto memerr; 2109e1051a39Sopenharmony_ci if (!X509_CRL_add0_revoked(crl, rvtmp)) { 2110e1051a39Sopenharmony_ci X509_REVOKED_free(rvtmp); 2111e1051a39Sopenharmony_ci goto memerr; 2112e1051a39Sopenharmony_ci } 2113e1051a39Sopenharmony_ci } 2114e1051a39Sopenharmony_ci } 2115e1051a39Sopenharmony_ci 2116e1051a39Sopenharmony_ci if (skey != NULL && md != NULL && !X509_CRL_sign(crl, skey, md)) 2117e1051a39Sopenharmony_ci goto memerr; 2118e1051a39Sopenharmony_ci 2119e1051a39Sopenharmony_ci return crl; 2120e1051a39Sopenharmony_ci 2121e1051a39Sopenharmony_ci memerr: 2122e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2123e1051a39Sopenharmony_ci X509_CRL_free(crl); 2124e1051a39Sopenharmony_ci return NULL; 2125e1051a39Sopenharmony_ci} 2126e1051a39Sopenharmony_ci 2127e1051a39Sopenharmony_ciint X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) 2128e1051a39Sopenharmony_ci{ 2129e1051a39Sopenharmony_ci return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); 2130e1051a39Sopenharmony_ci} 2131e1051a39Sopenharmony_ci 2132e1051a39Sopenharmony_civoid *X509_STORE_CTX_get_ex_data(const X509_STORE_CTX *ctx, int idx) 2133e1051a39Sopenharmony_ci{ 2134e1051a39Sopenharmony_ci return CRYPTO_get_ex_data(&ctx->ex_data, idx); 2135e1051a39Sopenharmony_ci} 2136e1051a39Sopenharmony_ci 2137e1051a39Sopenharmony_ciint X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx) 2138e1051a39Sopenharmony_ci{ 2139e1051a39Sopenharmony_ci return ctx->error; 2140e1051a39Sopenharmony_ci} 2141e1051a39Sopenharmony_ci 2142e1051a39Sopenharmony_civoid X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) 2143e1051a39Sopenharmony_ci{ 2144e1051a39Sopenharmony_ci ctx->error = err; 2145e1051a39Sopenharmony_ci} 2146e1051a39Sopenharmony_ci 2147e1051a39Sopenharmony_ciint X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx) 2148e1051a39Sopenharmony_ci{ 2149e1051a39Sopenharmony_ci return ctx->error_depth; 2150e1051a39Sopenharmony_ci} 2151e1051a39Sopenharmony_ci 2152e1051a39Sopenharmony_civoid X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth) 2153e1051a39Sopenharmony_ci{ 2154e1051a39Sopenharmony_ci ctx->error_depth = depth; 2155e1051a39Sopenharmony_ci} 2156e1051a39Sopenharmony_ci 2157e1051a39Sopenharmony_ciX509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx) 2158e1051a39Sopenharmony_ci{ 2159e1051a39Sopenharmony_ci return ctx->current_cert; 2160e1051a39Sopenharmony_ci} 2161e1051a39Sopenharmony_ci 2162e1051a39Sopenharmony_civoid X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x) 2163e1051a39Sopenharmony_ci{ 2164e1051a39Sopenharmony_ci ctx->current_cert = x; 2165e1051a39Sopenharmony_ci} 2166e1051a39Sopenharmony_ci 2167e1051a39Sopenharmony_ciSTACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx) 2168e1051a39Sopenharmony_ci{ 2169e1051a39Sopenharmony_ci return ctx->chain; 2170e1051a39Sopenharmony_ci} 2171e1051a39Sopenharmony_ci 2172e1051a39Sopenharmony_ciSTACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) 2173e1051a39Sopenharmony_ci{ 2174e1051a39Sopenharmony_ci if (ctx->chain == NULL) 2175e1051a39Sopenharmony_ci return NULL; 2176e1051a39Sopenharmony_ci return X509_chain_up_ref(ctx->chain); 2177e1051a39Sopenharmony_ci} 2178e1051a39Sopenharmony_ci 2179e1051a39Sopenharmony_ciX509 *X509_STORE_CTX_get0_current_issuer(const X509_STORE_CTX *ctx) 2180e1051a39Sopenharmony_ci{ 2181e1051a39Sopenharmony_ci return ctx->current_issuer; 2182e1051a39Sopenharmony_ci} 2183e1051a39Sopenharmony_ci 2184e1051a39Sopenharmony_ciX509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) 2185e1051a39Sopenharmony_ci{ 2186e1051a39Sopenharmony_ci return ctx->current_crl; 2187e1051a39Sopenharmony_ci} 2188e1051a39Sopenharmony_ci 2189e1051a39Sopenharmony_ciX509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) 2190e1051a39Sopenharmony_ci{ 2191e1051a39Sopenharmony_ci return ctx->parent; 2192e1051a39Sopenharmony_ci} 2193e1051a39Sopenharmony_ci 2194e1051a39Sopenharmony_civoid X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) 2195e1051a39Sopenharmony_ci{ 2196e1051a39Sopenharmony_ci ctx->cert = x; 2197e1051a39Sopenharmony_ci} 2198e1051a39Sopenharmony_ci 2199e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) 2200e1051a39Sopenharmony_ci{ 2201e1051a39Sopenharmony_ci ctx->crls = sk; 2202e1051a39Sopenharmony_ci} 2203e1051a39Sopenharmony_ci 2204e1051a39Sopenharmony_ciint X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) 2205e1051a39Sopenharmony_ci{ 2206e1051a39Sopenharmony_ci /* 2207e1051a39Sopenharmony_ci * XXX: Why isn't this function always used to set the associated trust? 2208e1051a39Sopenharmony_ci * Should there even be a VPM->trust field at all? Or should the trust 2209e1051a39Sopenharmony_ci * always be inferred from the purpose by X509_STORE_CTX_init(). 2210e1051a39Sopenharmony_ci */ 2211e1051a39Sopenharmony_ci return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0); 2212e1051a39Sopenharmony_ci} 2213e1051a39Sopenharmony_ci 2214e1051a39Sopenharmony_ciint X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) 2215e1051a39Sopenharmony_ci{ 2216e1051a39Sopenharmony_ci /* 2217e1051a39Sopenharmony_ci * XXX: See above, this function would only be needed when the default 2218e1051a39Sopenharmony_ci * trust for the purpose needs an override in a corner case. 2219e1051a39Sopenharmony_ci */ 2220e1051a39Sopenharmony_ci return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust); 2221e1051a39Sopenharmony_ci} 2222e1051a39Sopenharmony_ci 2223e1051a39Sopenharmony_ci/* 2224e1051a39Sopenharmony_ci * This function is used to set the X509_STORE_CTX purpose and trust values. 2225e1051a39Sopenharmony_ci * This is intended to be used when another structure has its own trust and 2226e1051a39Sopenharmony_ci * purpose values which (if set) will be inherited by the ctx. If they aren't 2227e1051a39Sopenharmony_ci * set then we will usually have a default purpose in mind which should then 2228e1051a39Sopenharmony_ci * be used to set the trust value. An example of this is SSL use: an SSL 2229e1051a39Sopenharmony_ci * structure will have its own purpose and trust settings which the 2230e1051a39Sopenharmony_ci * application can set: if they aren't set then we use the default of SSL 2231e1051a39Sopenharmony_ci * client/server. 2232e1051a39Sopenharmony_ci */ 2233e1051a39Sopenharmony_ciint X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, 2234e1051a39Sopenharmony_ci int purpose, int trust) 2235e1051a39Sopenharmony_ci{ 2236e1051a39Sopenharmony_ci int idx; 2237e1051a39Sopenharmony_ci 2238e1051a39Sopenharmony_ci /* If purpose not set use default */ 2239e1051a39Sopenharmony_ci if (purpose == 0) 2240e1051a39Sopenharmony_ci purpose = def_purpose; 2241e1051a39Sopenharmony_ci /* 2242e1051a39Sopenharmony_ci * If purpose is set but we don't have a default then set the default to 2243e1051a39Sopenharmony_ci * the current purpose 2244e1051a39Sopenharmony_ci */ 2245e1051a39Sopenharmony_ci else if (def_purpose == 0) 2246e1051a39Sopenharmony_ci def_purpose = purpose; 2247e1051a39Sopenharmony_ci /* If we have a purpose then check it is valid */ 2248e1051a39Sopenharmony_ci if (purpose != 0) { 2249e1051a39Sopenharmony_ci X509_PURPOSE *ptmp; 2250e1051a39Sopenharmony_ci 2251e1051a39Sopenharmony_ci idx = X509_PURPOSE_get_by_id(purpose); 2252e1051a39Sopenharmony_ci if (idx == -1) { 2253e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID); 2254e1051a39Sopenharmony_ci return 0; 2255e1051a39Sopenharmony_ci } 2256e1051a39Sopenharmony_ci ptmp = X509_PURPOSE_get0(idx); 2257e1051a39Sopenharmony_ci if (ptmp->trust == X509_TRUST_DEFAULT) { 2258e1051a39Sopenharmony_ci idx = X509_PURPOSE_get_by_id(def_purpose); 2259e1051a39Sopenharmony_ci if (idx == -1) { 2260e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID); 2261e1051a39Sopenharmony_ci return 0; 2262e1051a39Sopenharmony_ci } 2263e1051a39Sopenharmony_ci ptmp = X509_PURPOSE_get0(idx); 2264e1051a39Sopenharmony_ci } 2265e1051a39Sopenharmony_ci /* If trust not set then get from purpose default */ 2266e1051a39Sopenharmony_ci if (trust == 0) 2267e1051a39Sopenharmony_ci trust = ptmp->trust; 2268e1051a39Sopenharmony_ci } 2269e1051a39Sopenharmony_ci if (trust != 0) { 2270e1051a39Sopenharmony_ci idx = X509_TRUST_get_by_id(trust); 2271e1051a39Sopenharmony_ci if (idx == -1) { 2272e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_TRUST_ID); 2273e1051a39Sopenharmony_ci return 0; 2274e1051a39Sopenharmony_ci } 2275e1051a39Sopenharmony_ci } 2276e1051a39Sopenharmony_ci 2277e1051a39Sopenharmony_ci if (ctx->param->purpose == 0 && purpose != 0) 2278e1051a39Sopenharmony_ci ctx->param->purpose = purpose; 2279e1051a39Sopenharmony_ci if (ctx->param->trust == 0 && trust != 0) 2280e1051a39Sopenharmony_ci ctx->param->trust = trust; 2281e1051a39Sopenharmony_ci return 1; 2282e1051a39Sopenharmony_ci} 2283e1051a39Sopenharmony_ci 2284e1051a39Sopenharmony_ciX509_STORE_CTX *X509_STORE_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq) 2285e1051a39Sopenharmony_ci{ 2286e1051a39Sopenharmony_ci X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); 2287e1051a39Sopenharmony_ci 2288e1051a39Sopenharmony_ci if (ctx == NULL) { 2289e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2290e1051a39Sopenharmony_ci return NULL; 2291e1051a39Sopenharmony_ci } 2292e1051a39Sopenharmony_ci 2293e1051a39Sopenharmony_ci ctx->libctx = libctx; 2294e1051a39Sopenharmony_ci if (propq != NULL) { 2295e1051a39Sopenharmony_ci ctx->propq = OPENSSL_strdup(propq); 2296e1051a39Sopenharmony_ci if (ctx->propq == NULL) { 2297e1051a39Sopenharmony_ci OPENSSL_free(ctx); 2298e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2299e1051a39Sopenharmony_ci return NULL; 2300e1051a39Sopenharmony_ci } 2301e1051a39Sopenharmony_ci } 2302e1051a39Sopenharmony_ci 2303e1051a39Sopenharmony_ci return ctx; 2304e1051a39Sopenharmony_ci} 2305e1051a39Sopenharmony_ci 2306e1051a39Sopenharmony_ciX509_STORE_CTX *X509_STORE_CTX_new(void) 2307e1051a39Sopenharmony_ci{ 2308e1051a39Sopenharmony_ci return X509_STORE_CTX_new_ex(NULL, NULL); 2309e1051a39Sopenharmony_ci} 2310e1051a39Sopenharmony_ci 2311e1051a39Sopenharmony_civoid X509_STORE_CTX_free(X509_STORE_CTX *ctx) 2312e1051a39Sopenharmony_ci{ 2313e1051a39Sopenharmony_ci if (ctx == NULL) 2314e1051a39Sopenharmony_ci return; 2315e1051a39Sopenharmony_ci 2316e1051a39Sopenharmony_ci X509_STORE_CTX_cleanup(ctx); 2317e1051a39Sopenharmony_ci 2318e1051a39Sopenharmony_ci /* libctx and propq survive X509_STORE_CTX_cleanup() */ 2319e1051a39Sopenharmony_ci OPENSSL_free(ctx->propq); 2320e1051a39Sopenharmony_ci OPENSSL_free(ctx); 2321e1051a39Sopenharmony_ci} 2322e1051a39Sopenharmony_ci 2323e1051a39Sopenharmony_ciint X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, 2324e1051a39Sopenharmony_ci STACK_OF(X509) *chain) 2325e1051a39Sopenharmony_ci{ 2326e1051a39Sopenharmony_ci if (ctx == NULL) { 2327e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); 2328e1051a39Sopenharmony_ci return 0; 2329e1051a39Sopenharmony_ci } 2330e1051a39Sopenharmony_ci X509_STORE_CTX_cleanup(ctx); 2331e1051a39Sopenharmony_ci 2332e1051a39Sopenharmony_ci ctx->store = store; 2333e1051a39Sopenharmony_ci ctx->cert = x509; 2334e1051a39Sopenharmony_ci ctx->untrusted = chain; 2335e1051a39Sopenharmony_ci ctx->crls = NULL; 2336e1051a39Sopenharmony_ci ctx->num_untrusted = 0; 2337e1051a39Sopenharmony_ci ctx->other_ctx = NULL; 2338e1051a39Sopenharmony_ci ctx->valid = 0; 2339e1051a39Sopenharmony_ci ctx->chain = NULL; 2340e1051a39Sopenharmony_ci ctx->error = X509_V_OK; 2341e1051a39Sopenharmony_ci ctx->explicit_policy = 0; 2342e1051a39Sopenharmony_ci ctx->error_depth = 0; 2343e1051a39Sopenharmony_ci ctx->current_cert = NULL; 2344e1051a39Sopenharmony_ci ctx->current_issuer = NULL; 2345e1051a39Sopenharmony_ci ctx->current_crl = NULL; 2346e1051a39Sopenharmony_ci ctx->current_crl_score = 0; 2347e1051a39Sopenharmony_ci ctx->current_reasons = 0; 2348e1051a39Sopenharmony_ci ctx->tree = NULL; 2349e1051a39Sopenharmony_ci ctx->parent = NULL; 2350e1051a39Sopenharmony_ci ctx->dane = NULL; 2351e1051a39Sopenharmony_ci ctx->bare_ta_signed = 0; 2352e1051a39Sopenharmony_ci /* Zero ex_data to make sure we're cleanup-safe */ 2353e1051a39Sopenharmony_ci memset(&ctx->ex_data, 0, sizeof(ctx->ex_data)); 2354e1051a39Sopenharmony_ci 2355e1051a39Sopenharmony_ci /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */ 2356e1051a39Sopenharmony_ci if (store != NULL) 2357e1051a39Sopenharmony_ci ctx->cleanup = store->cleanup; 2358e1051a39Sopenharmony_ci else 2359e1051a39Sopenharmony_ci ctx->cleanup = NULL; 2360e1051a39Sopenharmony_ci 2361e1051a39Sopenharmony_ci if (store != NULL && store->check_issued != NULL) 2362e1051a39Sopenharmony_ci ctx->check_issued = store->check_issued; 2363e1051a39Sopenharmony_ci else 2364e1051a39Sopenharmony_ci ctx->check_issued = check_issued; 2365e1051a39Sopenharmony_ci 2366e1051a39Sopenharmony_ci if (store != NULL && store->get_issuer != NULL) 2367e1051a39Sopenharmony_ci ctx->get_issuer = store->get_issuer; 2368e1051a39Sopenharmony_ci else 2369e1051a39Sopenharmony_ci ctx->get_issuer = X509_STORE_CTX_get1_issuer; 2370e1051a39Sopenharmony_ci 2371e1051a39Sopenharmony_ci if (store != NULL && store->verify_cb != NULL) 2372e1051a39Sopenharmony_ci ctx->verify_cb = store->verify_cb; 2373e1051a39Sopenharmony_ci else 2374e1051a39Sopenharmony_ci ctx->verify_cb = null_callback; 2375e1051a39Sopenharmony_ci 2376e1051a39Sopenharmony_ci if (store != NULL && store->verify != NULL) 2377e1051a39Sopenharmony_ci ctx->verify = store->verify; 2378e1051a39Sopenharmony_ci else 2379e1051a39Sopenharmony_ci ctx->verify = internal_verify; 2380e1051a39Sopenharmony_ci 2381e1051a39Sopenharmony_ci if (store != NULL && store->check_revocation != NULL) 2382e1051a39Sopenharmony_ci ctx->check_revocation = store->check_revocation; 2383e1051a39Sopenharmony_ci else 2384e1051a39Sopenharmony_ci ctx->check_revocation = check_revocation; 2385e1051a39Sopenharmony_ci 2386e1051a39Sopenharmony_ci if (store != NULL && store->get_crl != NULL) 2387e1051a39Sopenharmony_ci ctx->get_crl = store->get_crl; 2388e1051a39Sopenharmony_ci else 2389e1051a39Sopenharmony_ci ctx->get_crl = NULL; 2390e1051a39Sopenharmony_ci 2391e1051a39Sopenharmony_ci if (store != NULL && store->check_crl != NULL) 2392e1051a39Sopenharmony_ci ctx->check_crl = store->check_crl; 2393e1051a39Sopenharmony_ci else 2394e1051a39Sopenharmony_ci ctx->check_crl = check_crl; 2395e1051a39Sopenharmony_ci 2396e1051a39Sopenharmony_ci if (store != NULL && store->cert_crl != NULL) 2397e1051a39Sopenharmony_ci ctx->cert_crl = store->cert_crl; 2398e1051a39Sopenharmony_ci else 2399e1051a39Sopenharmony_ci ctx->cert_crl = cert_crl; 2400e1051a39Sopenharmony_ci 2401e1051a39Sopenharmony_ci if (store != NULL && store->check_policy != NULL) 2402e1051a39Sopenharmony_ci ctx->check_policy = store->check_policy; 2403e1051a39Sopenharmony_ci else 2404e1051a39Sopenharmony_ci ctx->check_policy = check_policy; 2405e1051a39Sopenharmony_ci 2406e1051a39Sopenharmony_ci if (store != NULL && store->lookup_certs != NULL) 2407e1051a39Sopenharmony_ci ctx->lookup_certs = store->lookup_certs; 2408e1051a39Sopenharmony_ci else 2409e1051a39Sopenharmony_ci ctx->lookup_certs = X509_STORE_CTX_get1_certs; 2410e1051a39Sopenharmony_ci 2411e1051a39Sopenharmony_ci if (store != NULL && store->lookup_crls != NULL) 2412e1051a39Sopenharmony_ci ctx->lookup_crls = store->lookup_crls; 2413e1051a39Sopenharmony_ci else 2414e1051a39Sopenharmony_ci ctx->lookup_crls = X509_STORE_CTX_get1_crls; 2415e1051a39Sopenharmony_ci 2416e1051a39Sopenharmony_ci ctx->param = X509_VERIFY_PARAM_new(); 2417e1051a39Sopenharmony_ci if (ctx->param == NULL) { 2418e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2419e1051a39Sopenharmony_ci goto err; 2420e1051a39Sopenharmony_ci } 2421e1051a39Sopenharmony_ci 2422e1051a39Sopenharmony_ci /* Inherit callbacks and flags from X509_STORE if not set use defaults. */ 2423e1051a39Sopenharmony_ci if (store == NULL) 2424e1051a39Sopenharmony_ci ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE; 2425e1051a39Sopenharmony_ci else if (X509_VERIFY_PARAM_inherit(ctx->param, store->param) == 0) 2426e1051a39Sopenharmony_ci goto err; 2427e1051a39Sopenharmony_ci 2428e1051a39Sopenharmony_ci if (!X509_STORE_CTX_set_default(ctx, "default")) 2429e1051a39Sopenharmony_ci goto err; 2430e1051a39Sopenharmony_ci 2431e1051a39Sopenharmony_ci /* 2432e1051a39Sopenharmony_ci * XXX: For now, continue to inherit trust from VPM, but infer from the 2433e1051a39Sopenharmony_ci * purpose if this still yields the default value. 2434e1051a39Sopenharmony_ci */ 2435e1051a39Sopenharmony_ci if (ctx->param->trust == X509_TRUST_DEFAULT) { 2436e1051a39Sopenharmony_ci int idx = X509_PURPOSE_get_by_id(ctx->param->purpose); 2437e1051a39Sopenharmony_ci X509_PURPOSE *xp = X509_PURPOSE_get0(idx); 2438e1051a39Sopenharmony_ci 2439e1051a39Sopenharmony_ci if (xp != NULL) 2440e1051a39Sopenharmony_ci ctx->param->trust = X509_PURPOSE_get_trust(xp); 2441e1051a39Sopenharmony_ci } 2442e1051a39Sopenharmony_ci 2443e1051a39Sopenharmony_ci if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, 2444e1051a39Sopenharmony_ci &ctx->ex_data)) 2445e1051a39Sopenharmony_ci return 1; 2446e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2447e1051a39Sopenharmony_ci 2448e1051a39Sopenharmony_ci err: 2449e1051a39Sopenharmony_ci /* 2450e1051a39Sopenharmony_ci * On error clean up allocated storage, if the store context was not 2451e1051a39Sopenharmony_ci * allocated with X509_STORE_CTX_new() this is our last chance to do so. 2452e1051a39Sopenharmony_ci */ 2453e1051a39Sopenharmony_ci X509_STORE_CTX_cleanup(ctx); 2454e1051a39Sopenharmony_ci return 0; 2455e1051a39Sopenharmony_ci} 2456e1051a39Sopenharmony_ci 2457e1051a39Sopenharmony_ci/* 2458e1051a39Sopenharmony_ci * Set alternative get_issuer method: just from a STACK of trusted certificates. 2459e1051a39Sopenharmony_ci * This avoids the complexity of X509_STORE where it is not needed. 2460e1051a39Sopenharmony_ci */ 2461e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) 2462e1051a39Sopenharmony_ci{ 2463e1051a39Sopenharmony_ci ctx->other_ctx = sk; 2464e1051a39Sopenharmony_ci ctx->get_issuer = get_issuer_sk; 2465e1051a39Sopenharmony_ci ctx->lookup_certs = lookup_certs_sk; 2466e1051a39Sopenharmony_ci} 2467e1051a39Sopenharmony_ci 2468e1051a39Sopenharmony_civoid X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) 2469e1051a39Sopenharmony_ci{ 2470e1051a39Sopenharmony_ci /* 2471e1051a39Sopenharmony_ci * We need to be idempotent because, unfortunately, free() also calls 2472e1051a39Sopenharmony_ci * cleanup(), so the natural call sequence new(), init(), cleanup(), free() 2473e1051a39Sopenharmony_ci * calls cleanup() for the same object twice! Thus we must zero the 2474e1051a39Sopenharmony_ci * pointers below after they're freed! 2475e1051a39Sopenharmony_ci */ 2476e1051a39Sopenharmony_ci /* Seems to always be NULL in OpenSSL, do this at most once. */ 2477e1051a39Sopenharmony_ci if (ctx->cleanup != NULL) { 2478e1051a39Sopenharmony_ci ctx->cleanup(ctx); 2479e1051a39Sopenharmony_ci ctx->cleanup = NULL; 2480e1051a39Sopenharmony_ci } 2481e1051a39Sopenharmony_ci if (ctx->param != NULL) { 2482e1051a39Sopenharmony_ci if (ctx->parent == NULL) 2483e1051a39Sopenharmony_ci X509_VERIFY_PARAM_free(ctx->param); 2484e1051a39Sopenharmony_ci ctx->param = NULL; 2485e1051a39Sopenharmony_ci } 2486e1051a39Sopenharmony_ci X509_policy_tree_free(ctx->tree); 2487e1051a39Sopenharmony_ci ctx->tree = NULL; 2488e1051a39Sopenharmony_ci sk_X509_pop_free(ctx->chain, X509_free); 2489e1051a39Sopenharmony_ci ctx->chain = NULL; 2490e1051a39Sopenharmony_ci CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data)); 2491e1051a39Sopenharmony_ci memset(&ctx->ex_data, 0, sizeof(ctx->ex_data)); 2492e1051a39Sopenharmony_ci} 2493e1051a39Sopenharmony_ci 2494e1051a39Sopenharmony_civoid X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) 2495e1051a39Sopenharmony_ci{ 2496e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_depth(ctx->param, depth); 2497e1051a39Sopenharmony_ci} 2498e1051a39Sopenharmony_ci 2499e1051a39Sopenharmony_civoid X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) 2500e1051a39Sopenharmony_ci{ 2501e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_flags(ctx->param, flags); 2502e1051a39Sopenharmony_ci} 2503e1051a39Sopenharmony_ci 2504e1051a39Sopenharmony_civoid X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, 2505e1051a39Sopenharmony_ci time_t t) 2506e1051a39Sopenharmony_ci{ 2507e1051a39Sopenharmony_ci X509_VERIFY_PARAM_set_time(ctx->param, t); 2508e1051a39Sopenharmony_ci} 2509e1051a39Sopenharmony_ci 2510e1051a39Sopenharmony_ciX509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx) 2511e1051a39Sopenharmony_ci{ 2512e1051a39Sopenharmony_ci return ctx->cert; 2513e1051a39Sopenharmony_ci} 2514e1051a39Sopenharmony_ci 2515e1051a39Sopenharmony_ciSTACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx) 2516e1051a39Sopenharmony_ci{ 2517e1051a39Sopenharmony_ci return ctx->untrusted; 2518e1051a39Sopenharmony_ci} 2519e1051a39Sopenharmony_ci 2520e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) 2521e1051a39Sopenharmony_ci{ 2522e1051a39Sopenharmony_ci ctx->untrusted = sk; 2523e1051a39Sopenharmony_ci} 2524e1051a39Sopenharmony_ci 2525e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) 2526e1051a39Sopenharmony_ci{ 2527e1051a39Sopenharmony_ci sk_X509_pop_free(ctx->chain, X509_free); 2528e1051a39Sopenharmony_ci ctx->chain = sk; 2529e1051a39Sopenharmony_ci} 2530e1051a39Sopenharmony_ci 2531e1051a39Sopenharmony_civoid X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx, 2532e1051a39Sopenharmony_ci X509_STORE_CTX_verify_cb verify_cb) 2533e1051a39Sopenharmony_ci{ 2534e1051a39Sopenharmony_ci ctx->verify_cb = verify_cb; 2535e1051a39Sopenharmony_ci} 2536e1051a39Sopenharmony_ci 2537e1051a39Sopenharmony_ciX509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(const X509_STORE_CTX *ctx) 2538e1051a39Sopenharmony_ci{ 2539e1051a39Sopenharmony_ci return ctx->verify_cb; 2540e1051a39Sopenharmony_ci} 2541e1051a39Sopenharmony_ci 2542e1051a39Sopenharmony_civoid X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx, 2543e1051a39Sopenharmony_ci X509_STORE_CTX_verify_fn verify) 2544e1051a39Sopenharmony_ci{ 2545e1051a39Sopenharmony_ci ctx->verify = verify; 2546e1051a39Sopenharmony_ci} 2547e1051a39Sopenharmony_ci 2548e1051a39Sopenharmony_ciX509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(const X509_STORE_CTX *ctx) 2549e1051a39Sopenharmony_ci{ 2550e1051a39Sopenharmony_ci return ctx->verify; 2551e1051a39Sopenharmony_ci} 2552e1051a39Sopenharmony_ci 2553e1051a39Sopenharmony_ciX509_STORE_CTX_get_issuer_fn 2554e1051a39Sopenharmony_ciX509_STORE_CTX_get_get_issuer(const X509_STORE_CTX *ctx) 2555e1051a39Sopenharmony_ci{ 2556e1051a39Sopenharmony_ci return ctx->get_issuer; 2557e1051a39Sopenharmony_ci} 2558e1051a39Sopenharmony_ci 2559e1051a39Sopenharmony_ciX509_STORE_CTX_check_issued_fn 2560e1051a39Sopenharmony_ciX509_STORE_CTX_get_check_issued(const X509_STORE_CTX *ctx) 2561e1051a39Sopenharmony_ci{ 2562e1051a39Sopenharmony_ci return ctx->check_issued; 2563e1051a39Sopenharmony_ci} 2564e1051a39Sopenharmony_ci 2565e1051a39Sopenharmony_ciX509_STORE_CTX_check_revocation_fn 2566e1051a39Sopenharmony_ciX509_STORE_CTX_get_check_revocation(const X509_STORE_CTX *ctx) 2567e1051a39Sopenharmony_ci{ 2568e1051a39Sopenharmony_ci return ctx->check_revocation; 2569e1051a39Sopenharmony_ci} 2570e1051a39Sopenharmony_ci 2571e1051a39Sopenharmony_ciX509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(const X509_STORE_CTX *ctx) 2572e1051a39Sopenharmony_ci{ 2573e1051a39Sopenharmony_ci return ctx->get_crl; 2574e1051a39Sopenharmony_ci} 2575e1051a39Sopenharmony_ci 2576e1051a39Sopenharmony_ciX509_STORE_CTX_check_crl_fn 2577e1051a39Sopenharmony_ciX509_STORE_CTX_get_check_crl(const X509_STORE_CTX *ctx) 2578e1051a39Sopenharmony_ci{ 2579e1051a39Sopenharmony_ci return ctx->check_crl; 2580e1051a39Sopenharmony_ci} 2581e1051a39Sopenharmony_ci 2582e1051a39Sopenharmony_ciX509_STORE_CTX_cert_crl_fn 2583e1051a39Sopenharmony_ciX509_STORE_CTX_get_cert_crl(const X509_STORE_CTX *ctx) 2584e1051a39Sopenharmony_ci{ 2585e1051a39Sopenharmony_ci return ctx->cert_crl; 2586e1051a39Sopenharmony_ci} 2587e1051a39Sopenharmony_ci 2588e1051a39Sopenharmony_ciX509_STORE_CTX_check_policy_fn 2589e1051a39Sopenharmony_ciX509_STORE_CTX_get_check_policy(const X509_STORE_CTX *ctx) 2590e1051a39Sopenharmony_ci{ 2591e1051a39Sopenharmony_ci return ctx->check_policy; 2592e1051a39Sopenharmony_ci} 2593e1051a39Sopenharmony_ci 2594e1051a39Sopenharmony_ciX509_STORE_CTX_lookup_certs_fn 2595e1051a39Sopenharmony_ciX509_STORE_CTX_get_lookup_certs(const X509_STORE_CTX *ctx) 2596e1051a39Sopenharmony_ci{ 2597e1051a39Sopenharmony_ci return ctx->lookup_certs; 2598e1051a39Sopenharmony_ci} 2599e1051a39Sopenharmony_ci 2600e1051a39Sopenharmony_ciX509_STORE_CTX_lookup_crls_fn 2601e1051a39Sopenharmony_ciX509_STORE_CTX_get_lookup_crls(const X509_STORE_CTX *ctx) 2602e1051a39Sopenharmony_ci{ 2603e1051a39Sopenharmony_ci return ctx->lookup_crls; 2604e1051a39Sopenharmony_ci} 2605e1051a39Sopenharmony_ci 2606e1051a39Sopenharmony_ciX509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(const X509_STORE_CTX *ctx) 2607e1051a39Sopenharmony_ci{ 2608e1051a39Sopenharmony_ci return ctx->cleanup; 2609e1051a39Sopenharmony_ci} 2610e1051a39Sopenharmony_ci 2611e1051a39Sopenharmony_ciX509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(const X509_STORE_CTX *ctx) 2612e1051a39Sopenharmony_ci{ 2613e1051a39Sopenharmony_ci return ctx->tree; 2614e1051a39Sopenharmony_ci} 2615e1051a39Sopenharmony_ci 2616e1051a39Sopenharmony_ciint X509_STORE_CTX_get_explicit_policy(const X509_STORE_CTX *ctx) 2617e1051a39Sopenharmony_ci{ 2618e1051a39Sopenharmony_ci return ctx->explicit_policy; 2619e1051a39Sopenharmony_ci} 2620e1051a39Sopenharmony_ci 2621e1051a39Sopenharmony_ciint X509_STORE_CTX_get_num_untrusted(const X509_STORE_CTX *ctx) 2622e1051a39Sopenharmony_ci{ 2623e1051a39Sopenharmony_ci return ctx->num_untrusted; 2624e1051a39Sopenharmony_ci} 2625e1051a39Sopenharmony_ci 2626e1051a39Sopenharmony_ciint X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) 2627e1051a39Sopenharmony_ci{ 2628e1051a39Sopenharmony_ci const X509_VERIFY_PARAM *param; 2629e1051a39Sopenharmony_ci 2630e1051a39Sopenharmony_ci param = X509_VERIFY_PARAM_lookup(name); 2631e1051a39Sopenharmony_ci if (param == NULL) { 2632e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_X509, X509_R_UNKNOWN_PURPOSE_ID, "name=%s", name); 2633e1051a39Sopenharmony_ci return 0; 2634e1051a39Sopenharmony_ci } 2635e1051a39Sopenharmony_ci return X509_VERIFY_PARAM_inherit(ctx->param, param); 2636e1051a39Sopenharmony_ci} 2637e1051a39Sopenharmony_ci 2638e1051a39Sopenharmony_ciX509_VERIFY_PARAM *X509_STORE_CTX_get0_param(const X509_STORE_CTX *ctx) 2639e1051a39Sopenharmony_ci{ 2640e1051a39Sopenharmony_ci return ctx->param; 2641e1051a39Sopenharmony_ci} 2642e1051a39Sopenharmony_ci 2643e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) 2644e1051a39Sopenharmony_ci{ 2645e1051a39Sopenharmony_ci X509_VERIFY_PARAM_free(ctx->param); 2646e1051a39Sopenharmony_ci ctx->param = param; 2647e1051a39Sopenharmony_ci} 2648e1051a39Sopenharmony_ci 2649e1051a39Sopenharmony_civoid X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane) 2650e1051a39Sopenharmony_ci{ 2651e1051a39Sopenharmony_ci ctx->dane = dane; 2652e1051a39Sopenharmony_ci} 2653e1051a39Sopenharmony_ci 2654e1051a39Sopenharmony_cistatic unsigned char *dane_i2d(X509 *cert, uint8_t selector, 2655e1051a39Sopenharmony_ci unsigned int *i2dlen) 2656e1051a39Sopenharmony_ci{ 2657e1051a39Sopenharmony_ci unsigned char *buf = NULL; 2658e1051a39Sopenharmony_ci int len; 2659e1051a39Sopenharmony_ci 2660e1051a39Sopenharmony_ci /* 2661e1051a39Sopenharmony_ci * Extract ASN.1 DER form of certificate or public key. 2662e1051a39Sopenharmony_ci */ 2663e1051a39Sopenharmony_ci switch (selector) { 2664e1051a39Sopenharmony_ci case DANETLS_SELECTOR_CERT: 2665e1051a39Sopenharmony_ci len = i2d_X509(cert, &buf); 2666e1051a39Sopenharmony_ci break; 2667e1051a39Sopenharmony_ci case DANETLS_SELECTOR_SPKI: 2668e1051a39Sopenharmony_ci len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf); 2669e1051a39Sopenharmony_ci break; 2670e1051a39Sopenharmony_ci default: 2671e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, X509_R_BAD_SELECTOR); 2672e1051a39Sopenharmony_ci return NULL; 2673e1051a39Sopenharmony_ci } 2674e1051a39Sopenharmony_ci 2675e1051a39Sopenharmony_ci if (len < 0 || buf == NULL) { 2676e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 2677e1051a39Sopenharmony_ci return NULL; 2678e1051a39Sopenharmony_ci } 2679e1051a39Sopenharmony_ci 2680e1051a39Sopenharmony_ci *i2dlen = (unsigned int)len; 2681e1051a39Sopenharmony_ci return buf; 2682e1051a39Sopenharmony_ci} 2683e1051a39Sopenharmony_ci 2684e1051a39Sopenharmony_ci#define DANETLS_NONE 256 /* impossible uint8_t */ 2685e1051a39Sopenharmony_ci 2686e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 2687e1051a39Sopenharmony_cistatic int dane_match(X509_STORE_CTX *ctx, X509 *cert, int depth) 2688e1051a39Sopenharmony_ci{ 2689e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 2690e1051a39Sopenharmony_ci unsigned usage = DANETLS_NONE; 2691e1051a39Sopenharmony_ci unsigned selector = DANETLS_NONE; 2692e1051a39Sopenharmony_ci unsigned ordinal = DANETLS_NONE; 2693e1051a39Sopenharmony_ci unsigned mtype = DANETLS_NONE; 2694e1051a39Sopenharmony_ci unsigned char *i2dbuf = NULL; 2695e1051a39Sopenharmony_ci unsigned int i2dlen = 0; 2696e1051a39Sopenharmony_ci unsigned char mdbuf[EVP_MAX_MD_SIZE]; 2697e1051a39Sopenharmony_ci unsigned char *cmpbuf = NULL; 2698e1051a39Sopenharmony_ci unsigned int cmplen = 0; 2699e1051a39Sopenharmony_ci int i; 2700e1051a39Sopenharmony_ci int recnum; 2701e1051a39Sopenharmony_ci int matched = 0; 2702e1051a39Sopenharmony_ci danetls_record *t = NULL; 2703e1051a39Sopenharmony_ci uint32_t mask; 2704e1051a39Sopenharmony_ci 2705e1051a39Sopenharmony_ci mask = (depth == 0) ? DANETLS_EE_MASK : DANETLS_TA_MASK; 2706e1051a39Sopenharmony_ci 2707e1051a39Sopenharmony_ci /* The trust store is not applicable with DANE-TA(2) */ 2708e1051a39Sopenharmony_ci if (depth >= ctx->num_untrusted) 2709e1051a39Sopenharmony_ci mask &= DANETLS_PKIX_MASK; 2710e1051a39Sopenharmony_ci 2711e1051a39Sopenharmony_ci /* 2712e1051a39Sopenharmony_ci * If we've previously matched a PKIX-?? record, no need to test any 2713e1051a39Sopenharmony_ci * further PKIX-?? records, it remains to just build the PKIX chain. 2714e1051a39Sopenharmony_ci * Had the match been a DANE-?? record, we'd be done already. 2715e1051a39Sopenharmony_ci */ 2716e1051a39Sopenharmony_ci if (dane->mdpth >= 0) 2717e1051a39Sopenharmony_ci mask &= ~DANETLS_PKIX_MASK; 2718e1051a39Sopenharmony_ci 2719e1051a39Sopenharmony_ci /*- 2720e1051a39Sopenharmony_ci * https://tools.ietf.org/html/rfc7671#section-5.1 2721e1051a39Sopenharmony_ci * https://tools.ietf.org/html/rfc7671#section-5.2 2722e1051a39Sopenharmony_ci * https://tools.ietf.org/html/rfc7671#section-5.3 2723e1051a39Sopenharmony_ci * https://tools.ietf.org/html/rfc7671#section-5.4 2724e1051a39Sopenharmony_ci * 2725e1051a39Sopenharmony_ci * We handle DANE-EE(3) records first as they require no chain building 2726e1051a39Sopenharmony_ci * and no expiration or hostname checks. We also process digests with 2727e1051a39Sopenharmony_ci * higher ordinals first and ignore lower priorities except Full(0) which 2728e1051a39Sopenharmony_ci * is always processed (last). If none match, we then process PKIX-EE(1). 2729e1051a39Sopenharmony_ci * 2730e1051a39Sopenharmony_ci * NOTE: This relies on DANE usages sorting before the corresponding PKIX 2731e1051a39Sopenharmony_ci * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest 2732e1051a39Sopenharmony_ci * priorities. See twin comment in ssl/ssl_lib.c. 2733e1051a39Sopenharmony_ci * 2734e1051a39Sopenharmony_ci * We expect that most TLSA RRsets will have just a single usage, so we 2735e1051a39Sopenharmony_ci * don't go out of our way to cache multiple selector-specific i2d buffers 2736e1051a39Sopenharmony_ci * across usages, but if the selector happens to remain the same as switch 2737e1051a39Sopenharmony_ci * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1", 2738e1051a39Sopenharmony_ci * records would result in us generating each of the certificate and public 2739e1051a39Sopenharmony_ci * key DER forms twice, but more typically we'd just see multiple "3 1 1" 2740e1051a39Sopenharmony_ci * or multiple "3 0 1" records. 2741e1051a39Sopenharmony_ci * 2742e1051a39Sopenharmony_ci * As soon as we find a match at any given depth, we stop, because either 2743e1051a39Sopenharmony_ci * we've matched a DANE-?? record and the peer is authenticated, or, after 2744e1051a39Sopenharmony_ci * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is 2745e1051a39Sopenharmony_ci * sufficient for DANE, and what remains to do is ordinary PKIX validation. 2746e1051a39Sopenharmony_ci */ 2747e1051a39Sopenharmony_ci recnum = (dane->umask & mask) != 0 ? sk_danetls_record_num(dane->trecs) : 0; 2748e1051a39Sopenharmony_ci for (i = 0; matched == 0 && i < recnum; ++i) { 2749e1051a39Sopenharmony_ci t = sk_danetls_record_value(dane->trecs, i); 2750e1051a39Sopenharmony_ci if ((DANETLS_USAGE_BIT(t->usage) & mask) == 0) 2751e1051a39Sopenharmony_ci continue; 2752e1051a39Sopenharmony_ci if (t->usage != usage) { 2753e1051a39Sopenharmony_ci usage = t->usage; 2754e1051a39Sopenharmony_ci 2755e1051a39Sopenharmony_ci /* Reset digest agility for each usage/selector pair */ 2756e1051a39Sopenharmony_ci mtype = DANETLS_NONE; 2757e1051a39Sopenharmony_ci ordinal = dane->dctx->mdord[t->mtype]; 2758e1051a39Sopenharmony_ci } 2759e1051a39Sopenharmony_ci if (t->selector != selector) { 2760e1051a39Sopenharmony_ci selector = t->selector; 2761e1051a39Sopenharmony_ci 2762e1051a39Sopenharmony_ci /* Update per-selector state */ 2763e1051a39Sopenharmony_ci OPENSSL_free(i2dbuf); 2764e1051a39Sopenharmony_ci i2dbuf = dane_i2d(cert, selector, &i2dlen); 2765e1051a39Sopenharmony_ci if (i2dbuf == NULL) 2766e1051a39Sopenharmony_ci return -1; 2767e1051a39Sopenharmony_ci 2768e1051a39Sopenharmony_ci /* Reset digest agility for each usage/selector pair */ 2769e1051a39Sopenharmony_ci mtype = DANETLS_NONE; 2770e1051a39Sopenharmony_ci ordinal = dane->dctx->mdord[t->mtype]; 2771e1051a39Sopenharmony_ci } else if (t->mtype != DANETLS_MATCHING_FULL) { 2772e1051a39Sopenharmony_ci /*- 2773e1051a39Sopenharmony_ci * Digest agility: 2774e1051a39Sopenharmony_ci * 2775e1051a39Sopenharmony_ci * <https://tools.ietf.org/html/rfc7671#section-9> 2776e1051a39Sopenharmony_ci * 2777e1051a39Sopenharmony_ci * For a fixed selector, after processing all records with the 2778e1051a39Sopenharmony_ci * highest mtype ordinal, ignore all mtypes with lower ordinals 2779e1051a39Sopenharmony_ci * other than "Full". 2780e1051a39Sopenharmony_ci */ 2781e1051a39Sopenharmony_ci if (dane->dctx->mdord[t->mtype] < ordinal) 2782e1051a39Sopenharmony_ci continue; 2783e1051a39Sopenharmony_ci } 2784e1051a39Sopenharmony_ci 2785e1051a39Sopenharmony_ci /* 2786e1051a39Sopenharmony_ci * Each time we hit a (new selector or) mtype, re-compute the relevant 2787e1051a39Sopenharmony_ci * digest, more complex caching is not worth the code space. 2788e1051a39Sopenharmony_ci */ 2789e1051a39Sopenharmony_ci if (t->mtype != mtype) { 2790e1051a39Sopenharmony_ci const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype]; 2791e1051a39Sopenharmony_ci 2792e1051a39Sopenharmony_ci cmpbuf = i2dbuf; 2793e1051a39Sopenharmony_ci cmplen = i2dlen; 2794e1051a39Sopenharmony_ci 2795e1051a39Sopenharmony_ci if (md != NULL) { 2796e1051a39Sopenharmony_ci cmpbuf = mdbuf; 2797e1051a39Sopenharmony_ci if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) { 2798e1051a39Sopenharmony_ci matched = -1; 2799e1051a39Sopenharmony_ci break; 2800e1051a39Sopenharmony_ci } 2801e1051a39Sopenharmony_ci } 2802e1051a39Sopenharmony_ci } 2803e1051a39Sopenharmony_ci 2804e1051a39Sopenharmony_ci /* 2805e1051a39Sopenharmony_ci * Squirrel away the certificate and depth if we have a match. Any 2806e1051a39Sopenharmony_ci * DANE match is dispositive, but with PKIX we still need to build a 2807e1051a39Sopenharmony_ci * full chain. 2808e1051a39Sopenharmony_ci */ 2809e1051a39Sopenharmony_ci if (cmplen == t->dlen && 2810e1051a39Sopenharmony_ci memcmp(cmpbuf, t->data, cmplen) == 0) { 2811e1051a39Sopenharmony_ci if (DANETLS_USAGE_BIT(usage) & DANETLS_DANE_MASK) 2812e1051a39Sopenharmony_ci matched = 1; 2813e1051a39Sopenharmony_ci if (matched || dane->mdpth < 0) { 2814e1051a39Sopenharmony_ci dane->mdpth = depth; 2815e1051a39Sopenharmony_ci dane->mtlsa = t; 2816e1051a39Sopenharmony_ci OPENSSL_free(dane->mcert); 2817e1051a39Sopenharmony_ci dane->mcert = cert; 2818e1051a39Sopenharmony_ci X509_up_ref(cert); 2819e1051a39Sopenharmony_ci } 2820e1051a39Sopenharmony_ci break; 2821e1051a39Sopenharmony_ci } 2822e1051a39Sopenharmony_ci } 2823e1051a39Sopenharmony_ci 2824e1051a39Sopenharmony_ci /* Clear the one-element DER cache */ 2825e1051a39Sopenharmony_ci OPENSSL_free(i2dbuf); 2826e1051a39Sopenharmony_ci return matched; 2827e1051a39Sopenharmony_ci} 2828e1051a39Sopenharmony_ci 2829e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 2830e1051a39Sopenharmony_cistatic int check_dane_issuer(X509_STORE_CTX *ctx, int depth) 2831e1051a39Sopenharmony_ci{ 2832e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 2833e1051a39Sopenharmony_ci int matched = 0; 2834e1051a39Sopenharmony_ci X509 *cert; 2835e1051a39Sopenharmony_ci 2836e1051a39Sopenharmony_ci if (!DANETLS_HAS_TA(dane) || depth == 0) 2837e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 2838e1051a39Sopenharmony_ci 2839e1051a39Sopenharmony_ci /* 2840e1051a39Sopenharmony_ci * Record any DANE trust anchor matches, for the first depth to test, if 2841e1051a39Sopenharmony_ci * there's one at that depth. (This'll be false for length 1 chains looking 2842e1051a39Sopenharmony_ci * for an exact match for the leaf certificate). 2843e1051a39Sopenharmony_ci */ 2844e1051a39Sopenharmony_ci cert = sk_X509_value(ctx->chain, depth); 2845e1051a39Sopenharmony_ci if (cert != NULL && (matched = dane_match(ctx, cert, depth)) < 0) 2846e1051a39Sopenharmony_ci return matched; 2847e1051a39Sopenharmony_ci if (matched > 0) { 2848e1051a39Sopenharmony_ci ctx->num_untrusted = depth - 1; 2849e1051a39Sopenharmony_ci return X509_TRUST_TRUSTED; 2850e1051a39Sopenharmony_ci } 2851e1051a39Sopenharmony_ci 2852e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 2853e1051a39Sopenharmony_ci} 2854e1051a39Sopenharmony_ci 2855e1051a39Sopenharmony_cistatic int check_dane_pkeys(X509_STORE_CTX *ctx) 2856e1051a39Sopenharmony_ci{ 2857e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 2858e1051a39Sopenharmony_ci danetls_record *t; 2859e1051a39Sopenharmony_ci int num = ctx->num_untrusted; 2860e1051a39Sopenharmony_ci X509 *cert = sk_X509_value(ctx->chain, num - 1); 2861e1051a39Sopenharmony_ci int recnum = sk_danetls_record_num(dane->trecs); 2862e1051a39Sopenharmony_ci int i; 2863e1051a39Sopenharmony_ci 2864e1051a39Sopenharmony_ci for (i = 0; i < recnum; ++i) { 2865e1051a39Sopenharmony_ci t = sk_danetls_record_value(dane->trecs, i); 2866e1051a39Sopenharmony_ci if (t->usage != DANETLS_USAGE_DANE_TA || 2867e1051a39Sopenharmony_ci t->selector != DANETLS_SELECTOR_SPKI || 2868e1051a39Sopenharmony_ci t->mtype != DANETLS_MATCHING_FULL || 2869e1051a39Sopenharmony_ci X509_verify(cert, t->spki) <= 0) 2870e1051a39Sopenharmony_ci continue; 2871e1051a39Sopenharmony_ci 2872e1051a39Sopenharmony_ci /* Clear any PKIX-?? matches that failed to extend to a full chain */ 2873e1051a39Sopenharmony_ci X509_free(dane->mcert); 2874e1051a39Sopenharmony_ci dane->mcert = NULL; 2875e1051a39Sopenharmony_ci 2876e1051a39Sopenharmony_ci /* Record match via a bare TA public key */ 2877e1051a39Sopenharmony_ci ctx->bare_ta_signed = 1; 2878e1051a39Sopenharmony_ci dane->mdpth = num - 1; 2879e1051a39Sopenharmony_ci dane->mtlsa = t; 2880e1051a39Sopenharmony_ci 2881e1051a39Sopenharmony_ci /* Prune any excess chain certificates */ 2882e1051a39Sopenharmony_ci num = sk_X509_num(ctx->chain); 2883e1051a39Sopenharmony_ci for (; num > ctx->num_untrusted; --num) 2884e1051a39Sopenharmony_ci X509_free(sk_X509_pop(ctx->chain)); 2885e1051a39Sopenharmony_ci 2886e1051a39Sopenharmony_ci return X509_TRUST_TRUSTED; 2887e1051a39Sopenharmony_ci } 2888e1051a39Sopenharmony_ci 2889e1051a39Sopenharmony_ci return X509_TRUST_UNTRUSTED; 2890e1051a39Sopenharmony_ci} 2891e1051a39Sopenharmony_ci 2892e1051a39Sopenharmony_cistatic void dane_reset(SSL_DANE *dane) 2893e1051a39Sopenharmony_ci{ 2894e1051a39Sopenharmony_ci /* Reset state to verify another chain, or clear after failure. */ 2895e1051a39Sopenharmony_ci X509_free(dane->mcert); 2896e1051a39Sopenharmony_ci dane->mcert = NULL; 2897e1051a39Sopenharmony_ci dane->mtlsa = NULL; 2898e1051a39Sopenharmony_ci dane->mdpth = -1; 2899e1051a39Sopenharmony_ci dane->pdpth = -1; 2900e1051a39Sopenharmony_ci} 2901e1051a39Sopenharmony_ci 2902e1051a39Sopenharmony_cistatic int check_leaf_suiteb(X509_STORE_CTX *ctx, X509 *cert) 2903e1051a39Sopenharmony_ci{ 2904e1051a39Sopenharmony_ci int err = X509_chain_check_suiteb(NULL, cert, NULL, ctx->param->flags); 2905e1051a39Sopenharmony_ci 2906e1051a39Sopenharmony_ci CB_FAIL_IF(err != X509_V_OK, ctx, cert, 0, err); 2907e1051a39Sopenharmony_ci return 1; 2908e1051a39Sopenharmony_ci} 2909e1051a39Sopenharmony_ci 2910e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 2911e1051a39Sopenharmony_cistatic int dane_verify(X509_STORE_CTX *ctx) 2912e1051a39Sopenharmony_ci{ 2913e1051a39Sopenharmony_ci X509 *cert = ctx->cert; 2914e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 2915e1051a39Sopenharmony_ci int matched; 2916e1051a39Sopenharmony_ci int done; 2917e1051a39Sopenharmony_ci 2918e1051a39Sopenharmony_ci dane_reset(dane); 2919e1051a39Sopenharmony_ci 2920e1051a39Sopenharmony_ci /*- 2921e1051a39Sopenharmony_ci * When testing the leaf certificate, if we match a DANE-EE(3) record, 2922e1051a39Sopenharmony_ci * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1) 2923e1051a39Sopenharmony_ci * record, the match depth and matching TLSA record are recorded, but the 2924e1051a39Sopenharmony_ci * return value is 0, because we still need to find a PKIX trust anchor. 2925e1051a39Sopenharmony_ci * Therefore, when DANE authentication is enabled (required), we're done 2926e1051a39Sopenharmony_ci * if: 2927e1051a39Sopenharmony_ci * + matched < 0, internal error. 2928e1051a39Sopenharmony_ci * + matched == 1, we matched a DANE-EE(3) record 2929e1051a39Sopenharmony_ci * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no 2930e1051a39Sopenharmony_ci * DANE-TA(2) or PKIX-TA(0) to test. 2931e1051a39Sopenharmony_ci */ 2932e1051a39Sopenharmony_ci matched = dane_match(ctx, ctx->cert, 0); 2933e1051a39Sopenharmony_ci done = matched != 0 || (!DANETLS_HAS_TA(dane) && dane->mdpth < 0); 2934e1051a39Sopenharmony_ci 2935e1051a39Sopenharmony_ci if (done && !X509_get_pubkey_parameters(NULL, ctx->chain)) 2936e1051a39Sopenharmony_ci return -1; 2937e1051a39Sopenharmony_ci 2938e1051a39Sopenharmony_ci if (matched > 0) { 2939e1051a39Sopenharmony_ci /* Callback invoked as needed */ 2940e1051a39Sopenharmony_ci if (!check_leaf_suiteb(ctx, cert)) 2941e1051a39Sopenharmony_ci return 0; 2942e1051a39Sopenharmony_ci /* Callback invoked as needed */ 2943e1051a39Sopenharmony_ci if ((dane->flags & DANE_FLAG_NO_DANE_EE_NAMECHECKS) == 0 && 2944e1051a39Sopenharmony_ci !check_id(ctx)) 2945e1051a39Sopenharmony_ci return 0; 2946e1051a39Sopenharmony_ci /* Bypass internal_verify(), issue depth 0 success callback */ 2947e1051a39Sopenharmony_ci ctx->error_depth = 0; 2948e1051a39Sopenharmony_ci ctx->current_cert = cert; 2949e1051a39Sopenharmony_ci return ctx->verify_cb(1, ctx); 2950e1051a39Sopenharmony_ci } 2951e1051a39Sopenharmony_ci 2952e1051a39Sopenharmony_ci if (matched < 0) { 2953e1051a39Sopenharmony_ci ctx->error_depth = 0; 2954e1051a39Sopenharmony_ci ctx->current_cert = cert; 2955e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 2956e1051a39Sopenharmony_ci return -1; 2957e1051a39Sopenharmony_ci } 2958e1051a39Sopenharmony_ci 2959e1051a39Sopenharmony_ci if (done) { 2960e1051a39Sopenharmony_ci /* Fail early, TA-based success is not possible */ 2961e1051a39Sopenharmony_ci if (!check_leaf_suiteb(ctx, cert)) 2962e1051a39Sopenharmony_ci return 0; 2963e1051a39Sopenharmony_ci return verify_cb_cert(ctx, cert, 0, X509_V_ERR_DANE_NO_MATCH); 2964e1051a39Sopenharmony_ci } 2965e1051a39Sopenharmony_ci 2966e1051a39Sopenharmony_ci /* 2967e1051a39Sopenharmony_ci * Chain verification for usages 0/1/2. TLSA record matching of depth > 0 2968e1051a39Sopenharmony_ci * certificates happens in-line with building the rest of the chain. 2969e1051a39Sopenharmony_ci */ 2970e1051a39Sopenharmony_ci return verify_chain(ctx); 2971e1051a39Sopenharmony_ci} 2972e1051a39Sopenharmony_ci 2973e1051a39Sopenharmony_ci/* 2974e1051a39Sopenharmony_ci * Get trusted issuer, without duplicate suppression 2975e1051a39Sopenharmony_ci * Returns -1 on internal error. 2976e1051a39Sopenharmony_ci */ 2977e1051a39Sopenharmony_cistatic int get1_trusted_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *cert) 2978e1051a39Sopenharmony_ci{ 2979e1051a39Sopenharmony_ci STACK_OF(X509) *saved_chain = ctx->chain; 2980e1051a39Sopenharmony_ci int ok; 2981e1051a39Sopenharmony_ci 2982e1051a39Sopenharmony_ci ctx->chain = NULL; 2983e1051a39Sopenharmony_ci ok = ctx->get_issuer(issuer, ctx, cert); 2984e1051a39Sopenharmony_ci ctx->chain = saved_chain; 2985e1051a39Sopenharmony_ci 2986e1051a39Sopenharmony_ci return ok; 2987e1051a39Sopenharmony_ci} 2988e1051a39Sopenharmony_ci 2989e1051a39Sopenharmony_ci/* Returns -1 on internal error */ 2990e1051a39Sopenharmony_cistatic int build_chain(X509_STORE_CTX *ctx) 2991e1051a39Sopenharmony_ci{ 2992e1051a39Sopenharmony_ci SSL_DANE *dane = ctx->dane; 2993e1051a39Sopenharmony_ci int num = sk_X509_num(ctx->chain); 2994e1051a39Sopenharmony_ci STACK_OF(X509) *sk_untrusted = NULL; 2995e1051a39Sopenharmony_ci unsigned int search; 2996e1051a39Sopenharmony_ci int may_trusted = 0; 2997e1051a39Sopenharmony_ci int may_alternate = 0; 2998e1051a39Sopenharmony_ci int trust = X509_TRUST_UNTRUSTED; 2999e1051a39Sopenharmony_ci int alt_untrusted = 0; 3000e1051a39Sopenharmony_ci int max_depth; 3001e1051a39Sopenharmony_ci int ok = 0; 3002e1051a39Sopenharmony_ci int i; 3003e1051a39Sopenharmony_ci 3004e1051a39Sopenharmony_ci /* Our chain starts with a single untrusted element. */ 3005e1051a39Sopenharmony_ci if (!ossl_assert(num == 1 && ctx->num_untrusted == num)) 3006e1051a39Sopenharmony_ci goto int_err; 3007e1051a39Sopenharmony_ci 3008e1051a39Sopenharmony_ci#define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */ 3009e1051a39Sopenharmony_ci#define S_DOTRUSTED (1 << 1) /* Search trusted store */ 3010e1051a39Sopenharmony_ci#define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */ 3011e1051a39Sopenharmony_ci /* 3012e1051a39Sopenharmony_ci * Set up search policy, untrusted if possible, trusted-first if enabled, 3013e1051a39Sopenharmony_ci * which is the default. 3014e1051a39Sopenharmony_ci * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the 3015e1051a39Sopenharmony_ci * trust_store, otherwise we might look there first. If not trusted-first, 3016e1051a39Sopenharmony_ci * and alternate chains are not disabled, try building an alternate chain 3017e1051a39Sopenharmony_ci * if no luck with untrusted first. 3018e1051a39Sopenharmony_ci */ 3019e1051a39Sopenharmony_ci search = ctx->untrusted != NULL ? S_DOUNTRUSTED : 0; 3020e1051a39Sopenharmony_ci if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) { 3021e1051a39Sopenharmony_ci if (search == 0 || (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) != 0) 3022e1051a39Sopenharmony_ci search |= S_DOTRUSTED; 3023e1051a39Sopenharmony_ci else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) 3024e1051a39Sopenharmony_ci may_alternate = 1; 3025e1051a39Sopenharmony_ci may_trusted = 1; 3026e1051a39Sopenharmony_ci } 3027e1051a39Sopenharmony_ci 3028e1051a39Sopenharmony_ci /* Initialize empty untrusted stack. */ 3029e1051a39Sopenharmony_ci if ((sk_untrusted = sk_X509_new_null()) == NULL) 3030e1051a39Sopenharmony_ci goto memerr; 3031e1051a39Sopenharmony_ci 3032e1051a39Sopenharmony_ci /* 3033e1051a39Sopenharmony_ci * If we got any "Cert(0) Full(0)" trust anchors from DNS, *prepend* them 3034e1051a39Sopenharmony_ci * to our working copy of the untrusted certificate stack. 3035e1051a39Sopenharmony_ci */ 3036e1051a39Sopenharmony_ci if (DANETLS_ENABLED(dane) && dane->certs != NULL 3037e1051a39Sopenharmony_ci && !X509_add_certs(sk_untrusted, dane->certs, X509_ADD_FLAG_DEFAULT)) 3038e1051a39Sopenharmony_ci goto memerr; 3039e1051a39Sopenharmony_ci 3040e1051a39Sopenharmony_ci /* 3041e1051a39Sopenharmony_ci * Shallow-copy the stack of untrusted certificates (with TLS, this is 3042e1051a39Sopenharmony_ci * typically the content of the peer's certificate message) so we can make 3043e1051a39Sopenharmony_ci * multiple passes over it, while free to remove elements as we go. 3044e1051a39Sopenharmony_ci */ 3045e1051a39Sopenharmony_ci if (!X509_add_certs(sk_untrusted, ctx->untrusted, X509_ADD_FLAG_DEFAULT)) 3046e1051a39Sopenharmony_ci goto memerr; 3047e1051a39Sopenharmony_ci 3048e1051a39Sopenharmony_ci /* 3049e1051a39Sopenharmony_ci * Still absurdly large, but arithmetically safe, a lower hard upper bound 3050e1051a39Sopenharmony_ci * might be reasonable. 3051e1051a39Sopenharmony_ci */ 3052e1051a39Sopenharmony_ci if (ctx->param->depth > INT_MAX / 2) 3053e1051a39Sopenharmony_ci ctx->param->depth = INT_MAX / 2; 3054e1051a39Sopenharmony_ci 3055e1051a39Sopenharmony_ci /* 3056e1051a39Sopenharmony_ci * Try to extend the chain until we reach an ultimately trusted issuer. 3057e1051a39Sopenharmony_ci * Build chains up to one longer the limit, later fail if we hit the limit, 3058e1051a39Sopenharmony_ci * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code. 3059e1051a39Sopenharmony_ci */ 3060e1051a39Sopenharmony_ci max_depth = ctx->param->depth + 1; 3061e1051a39Sopenharmony_ci 3062e1051a39Sopenharmony_ci while (search != 0) { 3063e1051a39Sopenharmony_ci X509 *curr, *issuer = NULL; 3064e1051a39Sopenharmony_ci 3065e1051a39Sopenharmony_ci num = sk_X509_num(ctx->chain); 3066e1051a39Sopenharmony_ci ctx->error_depth = num - 1; 3067e1051a39Sopenharmony_ci /* 3068e1051a39Sopenharmony_ci * Look in the trust store if enabled for first lookup, or we've run 3069e1051a39Sopenharmony_ci * out of untrusted issuers and search here is not disabled. When we 3070e1051a39Sopenharmony_ci * reach the depth limit, we stop extending the chain, if by that point 3071e1051a39Sopenharmony_ci * we've not found a trust anchor, any trusted chain would be too long. 3072e1051a39Sopenharmony_ci * 3073e1051a39Sopenharmony_ci * The error reported to the application verify callback is at the 3074e1051a39Sopenharmony_ci * maximal valid depth with the current certificate equal to the last 3075e1051a39Sopenharmony_ci * not ultimately-trusted issuer. For example, with verify_depth = 0, 3076e1051a39Sopenharmony_ci * the callback will report errors at depth=1 when the immediate issuer 3077e1051a39Sopenharmony_ci * of the leaf certificate is not a trust anchor. No attempt will be 3078e1051a39Sopenharmony_ci * made to locate an issuer for that certificate, since such a chain 3079e1051a39Sopenharmony_ci * would be a-priori too long. 3080e1051a39Sopenharmony_ci */ 3081e1051a39Sopenharmony_ci if ((search & S_DOTRUSTED) != 0) { 3082e1051a39Sopenharmony_ci i = num; 3083e1051a39Sopenharmony_ci if ((search & S_DOALTERNATE) != 0) { 3084e1051a39Sopenharmony_ci /* 3085e1051a39Sopenharmony_ci * As high up the chain as we can, look for an alternative 3086e1051a39Sopenharmony_ci * trusted issuer of an untrusted certificate that currently 3087e1051a39Sopenharmony_ci * has an untrusted issuer. We use the alt_untrusted variable 3088e1051a39Sopenharmony_ci * to track how far up the chain we find the first match. It 3089e1051a39Sopenharmony_ci * is only if and when we find a match, that we prune the chain 3090e1051a39Sopenharmony_ci * and reset ctx->num_untrusted to the reduced count of 3091e1051a39Sopenharmony_ci * untrusted certificates. While we're searching for such a 3092e1051a39Sopenharmony_ci * match (which may never be found), it is neither safe nor 3093e1051a39Sopenharmony_ci * wise to preemptively modify either the chain or 3094e1051a39Sopenharmony_ci * ctx->num_untrusted. 3095e1051a39Sopenharmony_ci * 3096e1051a39Sopenharmony_ci * Note, like ctx->num_untrusted, alt_untrusted is a count of 3097e1051a39Sopenharmony_ci * untrusted certificates, not a "depth". 3098e1051a39Sopenharmony_ci */ 3099e1051a39Sopenharmony_ci i = alt_untrusted; 3100e1051a39Sopenharmony_ci } 3101e1051a39Sopenharmony_ci curr = sk_X509_value(ctx->chain, i - 1); 3102e1051a39Sopenharmony_ci 3103e1051a39Sopenharmony_ci /* Note: get1_trusted_issuer() must be used even if self-signed. */ 3104e1051a39Sopenharmony_ci ok = num > max_depth ? 0 : get1_trusted_issuer(&issuer, ctx, curr); 3105e1051a39Sopenharmony_ci 3106e1051a39Sopenharmony_ci if (ok < 0) { 3107e1051a39Sopenharmony_ci trust = -1; 3108e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_STORE_LOOKUP; 3109e1051a39Sopenharmony_ci break; 3110e1051a39Sopenharmony_ci } 3111e1051a39Sopenharmony_ci 3112e1051a39Sopenharmony_ci if (ok > 0) { 3113e1051a39Sopenharmony_ci int self_signed = X509_self_signed(curr, 0); 3114e1051a39Sopenharmony_ci 3115e1051a39Sopenharmony_ci if (self_signed < 0) { 3116e1051a39Sopenharmony_ci X509_free(issuer); 3117e1051a39Sopenharmony_ci goto int_err; 3118e1051a39Sopenharmony_ci } 3119e1051a39Sopenharmony_ci /* 3120e1051a39Sopenharmony_ci * Alternative trusted issuer for a mid-chain untrusted cert? 3121e1051a39Sopenharmony_ci * Pop the untrusted cert's successors and retry. We might now 3122e1051a39Sopenharmony_ci * be able to complete a valid chain via the trust store. Note 3123e1051a39Sopenharmony_ci * that despite the current trust store match we might still 3124e1051a39Sopenharmony_ci * fail complete the chain to a suitable trust anchor, in which 3125e1051a39Sopenharmony_ci * case we may prune some more untrusted certificates and try 3126e1051a39Sopenharmony_ci * again. Thus the S_DOALTERNATE bit may yet be turned on 3127e1051a39Sopenharmony_ci * again with an even shorter untrusted chain! 3128e1051a39Sopenharmony_ci * 3129e1051a39Sopenharmony_ci * If in the process we threw away our matching PKIX-TA trust 3130e1051a39Sopenharmony_ci * anchor, reset DANE trust. We might find a suitable trusted 3131e1051a39Sopenharmony_ci * certificate among the ones from the trust store. 3132e1051a39Sopenharmony_ci */ 3133e1051a39Sopenharmony_ci if ((search & S_DOALTERNATE) != 0) { 3134e1051a39Sopenharmony_ci if (!ossl_assert(num > i && i > 0 && !self_signed)) { 3135e1051a39Sopenharmony_ci X509_free(issuer); 3136e1051a39Sopenharmony_ci goto int_err; 3137e1051a39Sopenharmony_ci } 3138e1051a39Sopenharmony_ci search &= ~S_DOALTERNATE; 3139e1051a39Sopenharmony_ci for (; num > i; --num) 3140e1051a39Sopenharmony_ci X509_free(sk_X509_pop(ctx->chain)); 3141e1051a39Sopenharmony_ci ctx->num_untrusted = num; 3142e1051a39Sopenharmony_ci 3143e1051a39Sopenharmony_ci if (DANETLS_ENABLED(dane) && 3144e1051a39Sopenharmony_ci dane->mdpth >= ctx->num_untrusted) { 3145e1051a39Sopenharmony_ci dane->mdpth = -1; 3146e1051a39Sopenharmony_ci X509_free(dane->mcert); 3147e1051a39Sopenharmony_ci dane->mcert = NULL; 3148e1051a39Sopenharmony_ci } 3149e1051a39Sopenharmony_ci if (DANETLS_ENABLED(dane) && 3150e1051a39Sopenharmony_ci dane->pdpth >= ctx->num_untrusted) 3151e1051a39Sopenharmony_ci dane->pdpth = -1; 3152e1051a39Sopenharmony_ci } 3153e1051a39Sopenharmony_ci 3154e1051a39Sopenharmony_ci /* 3155e1051a39Sopenharmony_ci * Self-signed untrusted certificates get replaced by their 3156e1051a39Sopenharmony_ci * trusted matching issuer. Otherwise, grow the chain. 3157e1051a39Sopenharmony_ci */ 3158e1051a39Sopenharmony_ci if (!self_signed) { 3159e1051a39Sopenharmony_ci if (!sk_X509_push(ctx->chain, issuer)) { 3160e1051a39Sopenharmony_ci X509_free(issuer); 3161e1051a39Sopenharmony_ci goto memerr; 3162e1051a39Sopenharmony_ci } 3163e1051a39Sopenharmony_ci if ((self_signed = X509_self_signed(issuer, 0)) < 0) 3164e1051a39Sopenharmony_ci goto int_err; 3165e1051a39Sopenharmony_ci } else { 3166e1051a39Sopenharmony_ci /* 3167e1051a39Sopenharmony_ci * We have a self-signed certificate that has the same 3168e1051a39Sopenharmony_ci * subject name (and perhaps keyid and/or serial number) as 3169e1051a39Sopenharmony_ci * a trust anchor. We must have an exact match to avoid 3170e1051a39Sopenharmony_ci * possible impersonation via key substitution etc. 3171e1051a39Sopenharmony_ci */ 3172e1051a39Sopenharmony_ci if (X509_cmp(curr, issuer) != 0) { 3173e1051a39Sopenharmony_ci /* Self-signed untrusted mimic. */ 3174e1051a39Sopenharmony_ci X509_free(issuer); 3175e1051a39Sopenharmony_ci ok = 0; 3176e1051a39Sopenharmony_ci } else { /* curr "==" issuer */ 3177e1051a39Sopenharmony_ci X509_free(curr); 3178e1051a39Sopenharmony_ci ctx->num_untrusted = --num; 3179e1051a39Sopenharmony_ci (void)sk_X509_set(ctx->chain, num, issuer); 3180e1051a39Sopenharmony_ci } 3181e1051a39Sopenharmony_ci } 3182e1051a39Sopenharmony_ci 3183e1051a39Sopenharmony_ci /* 3184e1051a39Sopenharmony_ci * We've added a new trusted certificate to the chain, re-check 3185e1051a39Sopenharmony_ci * trust. If not done, and not self-signed look deeper. 3186e1051a39Sopenharmony_ci * Whether or not we're doing "trusted first", we no longer 3187e1051a39Sopenharmony_ci * look for untrusted certificates from the peer's chain. 3188e1051a39Sopenharmony_ci * 3189e1051a39Sopenharmony_ci * At this point ctx->num_trusted and num must reflect the 3190e1051a39Sopenharmony_ci * correct number of untrusted certificates, since the DANE 3191e1051a39Sopenharmony_ci * logic in check_trust() depends on distinguishing CAs from 3192e1051a39Sopenharmony_ci * "the wire" from CAs from the trust store. In particular, the 3193e1051a39Sopenharmony_ci * certificate at depth "num" should be the new trusted 3194e1051a39Sopenharmony_ci * certificate with ctx->num_untrusted <= num. 3195e1051a39Sopenharmony_ci */ 3196e1051a39Sopenharmony_ci if (ok) { 3197e1051a39Sopenharmony_ci if (!ossl_assert(ctx->num_untrusted <= num)) 3198e1051a39Sopenharmony_ci goto int_err; 3199e1051a39Sopenharmony_ci search &= ~S_DOUNTRUSTED; 3200e1051a39Sopenharmony_ci trust = check_trust(ctx, num); 3201e1051a39Sopenharmony_ci if (trust != X509_TRUST_UNTRUSTED) 3202e1051a39Sopenharmony_ci break; 3203e1051a39Sopenharmony_ci if (!self_signed) 3204e1051a39Sopenharmony_ci continue; 3205e1051a39Sopenharmony_ci } 3206e1051a39Sopenharmony_ci } 3207e1051a39Sopenharmony_ci 3208e1051a39Sopenharmony_ci /* 3209e1051a39Sopenharmony_ci * No dispositive decision, and either self-signed or no match, if 3210e1051a39Sopenharmony_ci * we were doing untrusted-first, and alt-chains are not disabled, 3211e1051a39Sopenharmony_ci * do that, by repeatedly losing one untrusted element at a time, 3212e1051a39Sopenharmony_ci * and trying to extend the shorted chain. 3213e1051a39Sopenharmony_ci */ 3214e1051a39Sopenharmony_ci if ((search & S_DOUNTRUSTED) == 0) { 3215e1051a39Sopenharmony_ci /* Continue search for a trusted issuer of a shorter chain? */ 3216e1051a39Sopenharmony_ci if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0) 3217e1051a39Sopenharmony_ci continue; 3218e1051a39Sopenharmony_ci /* Still no luck and no fallbacks left? */ 3219e1051a39Sopenharmony_ci if (!may_alternate || (search & S_DOALTERNATE) != 0 || 3220e1051a39Sopenharmony_ci ctx->num_untrusted < 2) 3221e1051a39Sopenharmony_ci break; 3222e1051a39Sopenharmony_ci /* Search for a trusted issuer of a shorter chain */ 3223e1051a39Sopenharmony_ci search |= S_DOALTERNATE; 3224e1051a39Sopenharmony_ci alt_untrusted = ctx->num_untrusted - 1; 3225e1051a39Sopenharmony_ci } 3226e1051a39Sopenharmony_ci } 3227e1051a39Sopenharmony_ci 3228e1051a39Sopenharmony_ci /* 3229e1051a39Sopenharmony_ci * Extend chain with peer-provided untrusted certificates 3230e1051a39Sopenharmony_ci */ 3231e1051a39Sopenharmony_ci if ((search & S_DOUNTRUSTED) != 0) { 3232e1051a39Sopenharmony_ci num = sk_X509_num(ctx->chain); 3233e1051a39Sopenharmony_ci if (!ossl_assert(num == ctx->num_untrusted)) 3234e1051a39Sopenharmony_ci goto int_err; 3235e1051a39Sopenharmony_ci curr = sk_X509_value(ctx->chain, num - 1); 3236e1051a39Sopenharmony_ci issuer = (X509_self_signed(curr, 0) > 0 || num > max_depth) ? 3237e1051a39Sopenharmony_ci NULL : find_issuer(ctx, sk_untrusted, curr); 3238e1051a39Sopenharmony_ci if (issuer == NULL) { 3239e1051a39Sopenharmony_ci /* 3240e1051a39Sopenharmony_ci * Once we have reached a self-signed cert or num > max_depth 3241e1051a39Sopenharmony_ci * or can't find an issuer in the untrusted list we stop looking 3242e1051a39Sopenharmony_ci * there and start looking only in the trust store if enabled. 3243e1051a39Sopenharmony_ci */ 3244e1051a39Sopenharmony_ci search &= ~S_DOUNTRUSTED; 3245e1051a39Sopenharmony_ci if (may_trusted) 3246e1051a39Sopenharmony_ci search |= S_DOTRUSTED; 3247e1051a39Sopenharmony_ci continue; 3248e1051a39Sopenharmony_ci } 3249e1051a39Sopenharmony_ci 3250e1051a39Sopenharmony_ci /* Drop this issuer from future consideration */ 3251e1051a39Sopenharmony_ci (void)sk_X509_delete_ptr(sk_untrusted, issuer); 3252e1051a39Sopenharmony_ci 3253e1051a39Sopenharmony_ci if (!X509_add_cert(ctx->chain, issuer, X509_ADD_FLAG_UP_REF)) 3254e1051a39Sopenharmony_ci goto int_err; 3255e1051a39Sopenharmony_ci 3256e1051a39Sopenharmony_ci ++ctx->num_untrusted; 3257e1051a39Sopenharmony_ci 3258e1051a39Sopenharmony_ci /* Check for DANE-TA trust of the topmost untrusted certificate. */ 3259e1051a39Sopenharmony_ci trust = check_dane_issuer(ctx, ctx->num_untrusted - 1); 3260e1051a39Sopenharmony_ci if (trust == X509_TRUST_TRUSTED || trust == X509_TRUST_REJECTED) 3261e1051a39Sopenharmony_ci break; 3262e1051a39Sopenharmony_ci } 3263e1051a39Sopenharmony_ci } 3264e1051a39Sopenharmony_ci sk_X509_free(sk_untrusted); 3265e1051a39Sopenharmony_ci 3266e1051a39Sopenharmony_ci if (trust < 0) /* internal error */ 3267e1051a39Sopenharmony_ci return trust; 3268e1051a39Sopenharmony_ci 3269e1051a39Sopenharmony_ci /* 3270e1051a39Sopenharmony_ci * Last chance to make a trusted chain, either bare DANE-TA public-key 3271e1051a39Sopenharmony_ci * signers, or else direct leaf PKIX trust. 3272e1051a39Sopenharmony_ci */ 3273e1051a39Sopenharmony_ci num = sk_X509_num(ctx->chain); 3274e1051a39Sopenharmony_ci if (num <= max_depth) { 3275e1051a39Sopenharmony_ci if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane)) 3276e1051a39Sopenharmony_ci trust = check_dane_pkeys(ctx); 3277e1051a39Sopenharmony_ci if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted) 3278e1051a39Sopenharmony_ci trust = check_trust(ctx, num); 3279e1051a39Sopenharmony_ci } 3280e1051a39Sopenharmony_ci 3281e1051a39Sopenharmony_ci switch (trust) { 3282e1051a39Sopenharmony_ci case X509_TRUST_TRUSTED: 3283e1051a39Sopenharmony_ci return 1; 3284e1051a39Sopenharmony_ci case X509_TRUST_REJECTED: 3285e1051a39Sopenharmony_ci /* Callback already issued */ 3286e1051a39Sopenharmony_ci return 0; 3287e1051a39Sopenharmony_ci case X509_TRUST_UNTRUSTED: 3288e1051a39Sopenharmony_ci default: 3289e1051a39Sopenharmony_ci switch(ctx->error) { 3290e1051a39Sopenharmony_ci case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: 3291e1051a39Sopenharmony_ci case X509_V_ERR_CERT_NOT_YET_VALID: 3292e1051a39Sopenharmony_ci case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: 3293e1051a39Sopenharmony_ci case X509_V_ERR_CERT_HAS_EXPIRED: 3294e1051a39Sopenharmony_ci return 0; /* Callback already issued by ossl_x509_check_cert_time() */ 3295e1051a39Sopenharmony_ci default: /* A preliminary error has become final */ 3296e1051a39Sopenharmony_ci return verify_cb_cert(ctx, NULL, num - 1, ctx->error); 3297e1051a39Sopenharmony_ci case X509_V_OK: 3298e1051a39Sopenharmony_ci break; 3299e1051a39Sopenharmony_ci } 3300e1051a39Sopenharmony_ci CB_FAIL_IF(num > max_depth, 3301e1051a39Sopenharmony_ci ctx, NULL, num - 1, X509_V_ERR_CERT_CHAIN_TOO_LONG); 3302e1051a39Sopenharmony_ci CB_FAIL_IF(DANETLS_ENABLED(dane) 3303e1051a39Sopenharmony_ci && (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0), 3304e1051a39Sopenharmony_ci ctx, NULL, num - 1, X509_V_ERR_DANE_NO_MATCH); 3305e1051a39Sopenharmony_ci if (X509_self_signed(sk_X509_value(ctx->chain, num - 1), 0) > 0) 3306e1051a39Sopenharmony_ci return verify_cb_cert(ctx, NULL, num - 1, 3307e1051a39Sopenharmony_ci num == 1 3308e1051a39Sopenharmony_ci ? X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT 3309e1051a39Sopenharmony_ci : X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN); 3310e1051a39Sopenharmony_ci return verify_cb_cert(ctx, NULL, num - 1, 3311e1051a39Sopenharmony_ci ctx->num_untrusted < num 3312e1051a39Sopenharmony_ci ? X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT 3313e1051a39Sopenharmony_ci : X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY); 3314e1051a39Sopenharmony_ci } 3315e1051a39Sopenharmony_ci 3316e1051a39Sopenharmony_ci int_err: 3317e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); 3318e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_UNSPECIFIED; 3319e1051a39Sopenharmony_ci sk_X509_free(sk_untrusted); 3320e1051a39Sopenharmony_ci return -1; 3321e1051a39Sopenharmony_ci 3322e1051a39Sopenharmony_ci memerr: 3323e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); 3324e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 3325e1051a39Sopenharmony_ci sk_X509_free(sk_untrusted); 3326e1051a39Sopenharmony_ci return -1; 3327e1051a39Sopenharmony_ci} 3328e1051a39Sopenharmony_ci 3329e1051a39Sopenharmony_ciSTACK_OF(X509) *X509_build_chain(X509 *target, STACK_OF(X509) *certs, 3330e1051a39Sopenharmony_ci X509_STORE *store, int with_self_signed, 3331e1051a39Sopenharmony_ci OSSL_LIB_CTX *libctx, const char *propq) 3332e1051a39Sopenharmony_ci{ 3333e1051a39Sopenharmony_ci int finish_chain = store != NULL; 3334e1051a39Sopenharmony_ci X509_STORE_CTX *ctx; 3335e1051a39Sopenharmony_ci int flags = X509_ADD_FLAG_UP_REF; 3336e1051a39Sopenharmony_ci STACK_OF(X509) *result = NULL; 3337e1051a39Sopenharmony_ci 3338e1051a39Sopenharmony_ci if (target == NULL) { 3339e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); 3340e1051a39Sopenharmony_ci return NULL; 3341e1051a39Sopenharmony_ci } 3342e1051a39Sopenharmony_ci 3343e1051a39Sopenharmony_ci if ((ctx = X509_STORE_CTX_new_ex(libctx, propq)) == NULL) 3344e1051a39Sopenharmony_ci return NULL; 3345e1051a39Sopenharmony_ci if (!X509_STORE_CTX_init(ctx, store, target, finish_chain ? certs : NULL)) 3346e1051a39Sopenharmony_ci goto err; 3347e1051a39Sopenharmony_ci if (!finish_chain) 3348e1051a39Sopenharmony_ci X509_STORE_CTX_set0_trusted_stack(ctx, certs); 3349e1051a39Sopenharmony_ci if (!ossl_x509_add_cert_new(&ctx->chain, target, X509_ADD_FLAG_UP_REF)) { 3350e1051a39Sopenharmony_ci ctx->error = X509_V_ERR_OUT_OF_MEM; 3351e1051a39Sopenharmony_ci goto err; 3352e1051a39Sopenharmony_ci } 3353e1051a39Sopenharmony_ci ctx->num_untrusted = 1; 3354e1051a39Sopenharmony_ci 3355e1051a39Sopenharmony_ci if (!build_chain(ctx) && finish_chain) 3356e1051a39Sopenharmony_ci goto err; 3357e1051a39Sopenharmony_ci 3358e1051a39Sopenharmony_ci /* result list to store the up_ref'ed certificates */ 3359e1051a39Sopenharmony_ci if (sk_X509_num(ctx->chain) > 1 && !with_self_signed) 3360e1051a39Sopenharmony_ci flags |= X509_ADD_FLAG_NO_SS; 3361e1051a39Sopenharmony_ci if (!ossl_x509_add_certs_new(&result, ctx->chain, flags)) { 3362e1051a39Sopenharmony_ci sk_X509_free(result); 3363e1051a39Sopenharmony_ci result = NULL; 3364e1051a39Sopenharmony_ci } 3365e1051a39Sopenharmony_ci 3366e1051a39Sopenharmony_ci err: 3367e1051a39Sopenharmony_ci X509_STORE_CTX_free(ctx); 3368e1051a39Sopenharmony_ci return result; 3369e1051a39Sopenharmony_ci} 3370e1051a39Sopenharmony_ci 3371e1051a39Sopenharmony_ci/* 3372e1051a39Sopenharmony_ci * note that there's a corresponding minbits_table in ssl/ssl_cert.c 3373e1051a39Sopenharmony_ci * in ssl_get_security_level_bits that's used for selection of DH parameters 3374e1051a39Sopenharmony_ci */ 3375e1051a39Sopenharmony_cistatic const int minbits_table[] = { 80, 112, 128, 192, 256 }; 3376e1051a39Sopenharmony_cistatic const int NUM_AUTH_LEVELS = OSSL_NELEM(minbits_table); 3377e1051a39Sopenharmony_ci 3378e1051a39Sopenharmony_ci/*- 3379e1051a39Sopenharmony_ci * Check whether the public key of `cert` meets the security level of `ctx`. 3380e1051a39Sopenharmony_ci * Returns 1 on success, 0 otherwise. 3381e1051a39Sopenharmony_ci */ 3382e1051a39Sopenharmony_cistatic int check_key_level(X509_STORE_CTX *ctx, X509 *cert) 3383e1051a39Sopenharmony_ci{ 3384e1051a39Sopenharmony_ci EVP_PKEY *pkey = X509_get0_pubkey(cert); 3385e1051a39Sopenharmony_ci int level = ctx->param->auth_level; 3386e1051a39Sopenharmony_ci 3387e1051a39Sopenharmony_ci /* 3388e1051a39Sopenharmony_ci * At security level zero, return without checking for a supported public 3389e1051a39Sopenharmony_ci * key type. Some engines support key types not understood outside the 3390e1051a39Sopenharmony_ci * engine, and we only need to understand the key when enforcing a security 3391e1051a39Sopenharmony_ci * floor. 3392e1051a39Sopenharmony_ci */ 3393e1051a39Sopenharmony_ci if (level <= 0) 3394e1051a39Sopenharmony_ci return 1; 3395e1051a39Sopenharmony_ci 3396e1051a39Sopenharmony_ci /* Unsupported or malformed keys are not secure */ 3397e1051a39Sopenharmony_ci if (pkey == NULL) 3398e1051a39Sopenharmony_ci return 0; 3399e1051a39Sopenharmony_ci 3400e1051a39Sopenharmony_ci if (level > NUM_AUTH_LEVELS) 3401e1051a39Sopenharmony_ci level = NUM_AUTH_LEVELS; 3402e1051a39Sopenharmony_ci 3403e1051a39Sopenharmony_ci return EVP_PKEY_get_security_bits(pkey) >= minbits_table[level - 1]; 3404e1051a39Sopenharmony_ci} 3405e1051a39Sopenharmony_ci 3406e1051a39Sopenharmony_ci/*- 3407e1051a39Sopenharmony_ci * Check whether the public key of ``cert`` does not use explicit params 3408e1051a39Sopenharmony_ci * for an elliptic curve. 3409e1051a39Sopenharmony_ci * 3410e1051a39Sopenharmony_ci * Returns 1 on success, 0 if check fails, -1 for other errors. 3411e1051a39Sopenharmony_ci */ 3412e1051a39Sopenharmony_cistatic int check_curve(X509 *cert) 3413e1051a39Sopenharmony_ci{ 3414e1051a39Sopenharmony_ci EVP_PKEY *pkey = X509_get0_pubkey(cert); 3415e1051a39Sopenharmony_ci 3416e1051a39Sopenharmony_ci /* Unsupported or malformed key */ 3417e1051a39Sopenharmony_ci if (pkey == NULL) 3418e1051a39Sopenharmony_ci return -1; 3419e1051a39Sopenharmony_ci 3420e1051a39Sopenharmony_ci if (EVP_PKEY_get_id(pkey) == EVP_PKEY_EC) { 3421e1051a39Sopenharmony_ci int ret, val; 3422e1051a39Sopenharmony_ci 3423e1051a39Sopenharmony_ci ret = EVP_PKEY_get_int_param(pkey, 3424e1051a39Sopenharmony_ci OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, 3425e1051a39Sopenharmony_ci &val); 3426e1051a39Sopenharmony_ci return ret == 1 ? !val : -1; 3427e1051a39Sopenharmony_ci } 3428e1051a39Sopenharmony_ci 3429e1051a39Sopenharmony_ci return 1; 3430e1051a39Sopenharmony_ci} 3431e1051a39Sopenharmony_ci 3432e1051a39Sopenharmony_ci/*- 3433e1051a39Sopenharmony_ci * Check whether the signature digest algorithm of ``cert`` meets the security 3434e1051a39Sopenharmony_ci * level of ``ctx``. Should not be checked for trust anchors (whether 3435e1051a39Sopenharmony_ci * self-signed or otherwise). 3436e1051a39Sopenharmony_ci * 3437e1051a39Sopenharmony_ci * Returns 1 on success, 0 otherwise. 3438e1051a39Sopenharmony_ci */ 3439e1051a39Sopenharmony_cistatic int check_sig_level(X509_STORE_CTX *ctx, X509 *cert) 3440e1051a39Sopenharmony_ci{ 3441e1051a39Sopenharmony_ci int secbits = -1; 3442e1051a39Sopenharmony_ci int level = ctx->param->auth_level; 3443e1051a39Sopenharmony_ci 3444e1051a39Sopenharmony_ci if (level <= 0) 3445e1051a39Sopenharmony_ci return 1; 3446e1051a39Sopenharmony_ci if (level > NUM_AUTH_LEVELS) 3447e1051a39Sopenharmony_ci level = NUM_AUTH_LEVELS; 3448e1051a39Sopenharmony_ci 3449e1051a39Sopenharmony_ci if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL)) 3450e1051a39Sopenharmony_ci return 0; 3451e1051a39Sopenharmony_ci 3452e1051a39Sopenharmony_ci return secbits >= minbits_table[level - 1]; 3453e1051a39Sopenharmony_ci} 3454