1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2006-2021 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 <stdio.h> 11e1051a39Sopenharmony_ci#include <openssl/objects.h> 12e1051a39Sopenharmony_ci#include <openssl/ts.h> 13e1051a39Sopenharmony_ci#include <openssl/pkcs7.h> 14e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 15e1051a39Sopenharmony_ci#include "internal/sizes.h" 16e1051a39Sopenharmony_ci#include "crypto/ess.h" 17e1051a39Sopenharmony_ci#include "ts_local.h" 18e1051a39Sopenharmony_ci 19e1051a39Sopenharmony_cistatic int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted, 20e1051a39Sopenharmony_ci X509 *signer, STACK_OF(X509) **chain); 21e1051a39Sopenharmony_cistatic int ts_check_signing_certs(const PKCS7_SIGNER_INFO *si, 22e1051a39Sopenharmony_ci const STACK_OF(X509) *chain); 23e1051a39Sopenharmony_ci 24e1051a39Sopenharmony_cistatic int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx, 25e1051a39Sopenharmony_ci PKCS7 *token, TS_TST_INFO *tst_info); 26e1051a39Sopenharmony_cistatic int ts_check_status_info(TS_RESP *response); 27e1051a39Sopenharmony_cistatic char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text); 28e1051a39Sopenharmony_cistatic int ts_check_policy(const ASN1_OBJECT *req_oid, 29e1051a39Sopenharmony_ci const TS_TST_INFO *tst_info); 30e1051a39Sopenharmony_cistatic int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info, 31e1051a39Sopenharmony_ci X509_ALGOR **md_alg, 32e1051a39Sopenharmony_ci unsigned char **imprint, unsigned *imprint_len); 33e1051a39Sopenharmony_cistatic int ts_check_imprints(X509_ALGOR *algor_a, 34e1051a39Sopenharmony_ci const unsigned char *imprint_a, unsigned len_a, 35e1051a39Sopenharmony_ci TS_TST_INFO *tst_info); 36e1051a39Sopenharmony_cistatic int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info); 37e1051a39Sopenharmony_cistatic int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer); 38e1051a39Sopenharmony_cistatic int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names, 39e1051a39Sopenharmony_ci GENERAL_NAME *name); 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ci/* 42e1051a39Sopenharmony_ci * This must be large enough to hold all values in ts_status_text (with 43e1051a39Sopenharmony_ci * comma separator) or all text fields in ts_failure_info (also with comma). 44e1051a39Sopenharmony_ci */ 45e1051a39Sopenharmony_ci#define TS_STATUS_BUF_SIZE 256 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_ci/* 48e1051a39Sopenharmony_ci * Local mapping between response codes and descriptions. 49e1051a39Sopenharmony_ci */ 50e1051a39Sopenharmony_cistatic const char *ts_status_text[] = { 51e1051a39Sopenharmony_ci "granted", 52e1051a39Sopenharmony_ci "grantedWithMods", 53e1051a39Sopenharmony_ci "rejection", 54e1051a39Sopenharmony_ci "waiting", 55e1051a39Sopenharmony_ci "revocationWarning", 56e1051a39Sopenharmony_ci "revocationNotification" 57e1051a39Sopenharmony_ci}; 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci#define TS_STATUS_TEXT_SIZE OSSL_NELEM(ts_status_text) 60e1051a39Sopenharmony_ci 61e1051a39Sopenharmony_cistatic struct { 62e1051a39Sopenharmony_ci int code; 63e1051a39Sopenharmony_ci const char *text; 64e1051a39Sopenharmony_ci} ts_failure_info[] = { 65e1051a39Sopenharmony_ci {TS_INFO_BAD_ALG, "badAlg"}, 66e1051a39Sopenharmony_ci {TS_INFO_BAD_REQUEST, "badRequest"}, 67e1051a39Sopenharmony_ci {TS_INFO_BAD_DATA_FORMAT, "badDataFormat"}, 68e1051a39Sopenharmony_ci {TS_INFO_TIME_NOT_AVAILABLE, "timeNotAvailable"}, 69e1051a39Sopenharmony_ci {TS_INFO_UNACCEPTED_POLICY, "unacceptedPolicy"}, 70e1051a39Sopenharmony_ci {TS_INFO_UNACCEPTED_EXTENSION, "unacceptedExtension"}, 71e1051a39Sopenharmony_ci {TS_INFO_ADD_INFO_NOT_AVAILABLE, "addInfoNotAvailable"}, 72e1051a39Sopenharmony_ci {TS_INFO_SYSTEM_FAILURE, "systemFailure"} 73e1051a39Sopenharmony_ci}; 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ci 76e1051a39Sopenharmony_ci/*- 77e1051a39Sopenharmony_ci * This function carries out the following tasks: 78e1051a39Sopenharmony_ci * - Checks if there is one and only one signer. 79e1051a39Sopenharmony_ci * - Search for the signing certificate in 'certs' and in the response. 80e1051a39Sopenharmony_ci * - Check the extended key usage and key usage fields of the signer 81e1051a39Sopenharmony_ci * certificate (done by the path validation). 82e1051a39Sopenharmony_ci * - Build and validate the certificate path. 83e1051a39Sopenharmony_ci * - Check if the certificate path meets the requirements of the 84e1051a39Sopenharmony_ci * SigningCertificate ESS signed attribute. 85e1051a39Sopenharmony_ci * - Verify the signature value. 86e1051a39Sopenharmony_ci * - Returns the signer certificate in 'signer', if 'signer' is not NULL. 87e1051a39Sopenharmony_ci */ 88e1051a39Sopenharmony_ciint TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs, 89e1051a39Sopenharmony_ci X509_STORE *store, X509 **signer_out) 90e1051a39Sopenharmony_ci{ 91e1051a39Sopenharmony_ci STACK_OF(PKCS7_SIGNER_INFO) *sinfos = NULL; 92e1051a39Sopenharmony_ci PKCS7_SIGNER_INFO *si; 93e1051a39Sopenharmony_ci STACK_OF(X509) *untrusted = NULL; 94e1051a39Sopenharmony_ci STACK_OF(X509) *signers = NULL; 95e1051a39Sopenharmony_ci X509 *signer; 96e1051a39Sopenharmony_ci STACK_OF(X509) *chain = NULL; 97e1051a39Sopenharmony_ci char buf[4096]; 98e1051a39Sopenharmony_ci int i, j = 0, ret = 0; 99e1051a39Sopenharmony_ci BIO *p7bio = NULL; 100e1051a39Sopenharmony_ci 101e1051a39Sopenharmony_ci /* Some sanity checks first. */ 102e1051a39Sopenharmony_ci if (!token) { 103e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_INVALID_NULL_POINTER); 104e1051a39Sopenharmony_ci goto err; 105e1051a39Sopenharmony_ci } 106e1051a39Sopenharmony_ci if (!PKCS7_type_is_signed(token)) { 107e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_WRONG_CONTENT_TYPE); 108e1051a39Sopenharmony_ci goto err; 109e1051a39Sopenharmony_ci } 110e1051a39Sopenharmony_ci sinfos = PKCS7_get_signer_info(token); 111e1051a39Sopenharmony_ci if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) { 112e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_THERE_MUST_BE_ONE_SIGNER); 113e1051a39Sopenharmony_ci goto err; 114e1051a39Sopenharmony_ci } 115e1051a39Sopenharmony_ci si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0); 116e1051a39Sopenharmony_ci if (PKCS7_get_detached(token)) { 117e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_NO_CONTENT); 118e1051a39Sopenharmony_ci goto err; 119e1051a39Sopenharmony_ci } 120e1051a39Sopenharmony_ci 121e1051a39Sopenharmony_ci /* 122e1051a39Sopenharmony_ci * Get hold of the signer certificate, search only internal certificates 123e1051a39Sopenharmony_ci * if it was requested. 124e1051a39Sopenharmony_ci */ 125e1051a39Sopenharmony_ci signers = PKCS7_get0_signers(token, certs, 0); 126e1051a39Sopenharmony_ci if (!signers || sk_X509_num(signers) != 1) 127e1051a39Sopenharmony_ci goto err; 128e1051a39Sopenharmony_ci signer = sk_X509_value(signers, 0); 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ci untrusted = sk_X509_new_reserve(NULL, sk_X509_num(certs) 131e1051a39Sopenharmony_ci + sk_X509_num(token->d.sign->cert)); 132e1051a39Sopenharmony_ci if (untrusted == NULL 133e1051a39Sopenharmony_ci || !X509_add_certs(untrusted, certs, 0) 134e1051a39Sopenharmony_ci || !X509_add_certs(untrusted, token->d.sign->cert, 0)) 135e1051a39Sopenharmony_ci goto err; 136e1051a39Sopenharmony_ci if (!ts_verify_cert(store, untrusted, signer, &chain)) 137e1051a39Sopenharmony_ci goto err; 138e1051a39Sopenharmony_ci if (!ts_check_signing_certs(si, chain)) 139e1051a39Sopenharmony_ci goto err; 140e1051a39Sopenharmony_ci p7bio = PKCS7_dataInit(token, NULL); 141e1051a39Sopenharmony_ci 142e1051a39Sopenharmony_ci /* We now have to 'read' from p7bio to calculate digests etc. */ 143e1051a39Sopenharmony_ci while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0) 144e1051a39Sopenharmony_ci continue; 145e1051a39Sopenharmony_ci 146e1051a39Sopenharmony_ci j = PKCS7_signatureVerify(p7bio, token, si, signer); 147e1051a39Sopenharmony_ci if (j <= 0) { 148e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_SIGNATURE_FAILURE); 149e1051a39Sopenharmony_ci goto err; 150e1051a39Sopenharmony_ci } 151e1051a39Sopenharmony_ci 152e1051a39Sopenharmony_ci if (signer_out) { 153e1051a39Sopenharmony_ci *signer_out = signer; 154e1051a39Sopenharmony_ci X509_up_ref(signer); 155e1051a39Sopenharmony_ci } 156e1051a39Sopenharmony_ci ret = 1; 157e1051a39Sopenharmony_ci 158e1051a39Sopenharmony_ci err: 159e1051a39Sopenharmony_ci BIO_free_all(p7bio); 160e1051a39Sopenharmony_ci sk_X509_free(untrusted); 161e1051a39Sopenharmony_ci sk_X509_pop_free(chain, X509_free); 162e1051a39Sopenharmony_ci sk_X509_free(signers); 163e1051a39Sopenharmony_ci 164e1051a39Sopenharmony_ci return ret; 165e1051a39Sopenharmony_ci} 166e1051a39Sopenharmony_ci 167e1051a39Sopenharmony_ci/* 168e1051a39Sopenharmony_ci * The certificate chain is returned in chain. Caller is responsible for 169e1051a39Sopenharmony_ci * freeing the vector. 170e1051a39Sopenharmony_ci */ 171e1051a39Sopenharmony_cistatic int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted, 172e1051a39Sopenharmony_ci X509 *signer, STACK_OF(X509) **chain) 173e1051a39Sopenharmony_ci{ 174e1051a39Sopenharmony_ci X509_STORE_CTX *cert_ctx = NULL; 175e1051a39Sopenharmony_ci int i; 176e1051a39Sopenharmony_ci int ret = 0; 177e1051a39Sopenharmony_ci 178e1051a39Sopenharmony_ci *chain = NULL; 179e1051a39Sopenharmony_ci cert_ctx = X509_STORE_CTX_new(); 180e1051a39Sopenharmony_ci if (cert_ctx == NULL) { 181e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE); 182e1051a39Sopenharmony_ci goto err; 183e1051a39Sopenharmony_ci } 184e1051a39Sopenharmony_ci if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted)) 185e1051a39Sopenharmony_ci goto end; 186e1051a39Sopenharmony_ci X509_STORE_CTX_set_purpose(cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN); 187e1051a39Sopenharmony_ci i = X509_verify_cert(cert_ctx); 188e1051a39Sopenharmony_ci if (i <= 0) { 189e1051a39Sopenharmony_ci int j = X509_STORE_CTX_get_error(cert_ctx); 190e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_TS, TS_R_CERTIFICATE_VERIFY_ERROR, 191e1051a39Sopenharmony_ci "Verify error:%s", X509_verify_cert_error_string(j)); 192e1051a39Sopenharmony_ci goto err; 193e1051a39Sopenharmony_ci } 194e1051a39Sopenharmony_ci *chain = X509_STORE_CTX_get1_chain(cert_ctx); 195e1051a39Sopenharmony_ci ret = 1; 196e1051a39Sopenharmony_ci goto end; 197e1051a39Sopenharmony_ci 198e1051a39Sopenharmony_cierr: 199e1051a39Sopenharmony_ci ret = 0; 200e1051a39Sopenharmony_ci 201e1051a39Sopenharmony_ciend: 202e1051a39Sopenharmony_ci X509_STORE_CTX_free(cert_ctx); 203e1051a39Sopenharmony_ci return ret; 204e1051a39Sopenharmony_ci} 205e1051a39Sopenharmony_ci 206e1051a39Sopenharmony_cistatic ESS_SIGNING_CERT *ossl_ess_get_signing_cert(const PKCS7_SIGNER_INFO *si) 207e1051a39Sopenharmony_ci{ 208e1051a39Sopenharmony_ci ASN1_TYPE *attr; 209e1051a39Sopenharmony_ci const unsigned char *p; 210e1051a39Sopenharmony_ci 211e1051a39Sopenharmony_ci attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate); 212e1051a39Sopenharmony_ci if (attr == NULL) 213e1051a39Sopenharmony_ci return NULL; 214e1051a39Sopenharmony_ci p = attr->value.sequence->data; 215e1051a39Sopenharmony_ci return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length); 216e1051a39Sopenharmony_ci} 217e1051a39Sopenharmony_ci 218e1051a39Sopenharmony_cistatic 219e1051a39Sopenharmony_ciESS_SIGNING_CERT_V2 *ossl_ess_get_signing_cert_v2(const PKCS7_SIGNER_INFO *si) 220e1051a39Sopenharmony_ci{ 221e1051a39Sopenharmony_ci ASN1_TYPE *attr; 222e1051a39Sopenharmony_ci const unsigned char *p; 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ci attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificateV2); 225e1051a39Sopenharmony_ci if (attr == NULL) 226e1051a39Sopenharmony_ci return NULL; 227e1051a39Sopenharmony_ci p = attr->value.sequence->data; 228e1051a39Sopenharmony_ci return d2i_ESS_SIGNING_CERT_V2(NULL, &p, attr->value.sequence->length); 229e1051a39Sopenharmony_ci} 230e1051a39Sopenharmony_ci 231e1051a39Sopenharmony_cistatic int ts_check_signing_certs(const PKCS7_SIGNER_INFO *si, 232e1051a39Sopenharmony_ci const STACK_OF(X509) *chain) 233e1051a39Sopenharmony_ci{ 234e1051a39Sopenharmony_ci ESS_SIGNING_CERT *ss = ossl_ess_get_signing_cert(si); 235e1051a39Sopenharmony_ci ESS_SIGNING_CERT_V2 *ssv2 = ossl_ess_get_signing_cert_v2(si); 236e1051a39Sopenharmony_ci int ret = OSSL_ESS_check_signing_certs(ss, ssv2, chain, 1) > 0; 237e1051a39Sopenharmony_ci 238e1051a39Sopenharmony_ci ESS_SIGNING_CERT_free(ss); 239e1051a39Sopenharmony_ci ESS_SIGNING_CERT_V2_free(ssv2); 240e1051a39Sopenharmony_ci return ret; 241e1051a39Sopenharmony_ci} 242e1051a39Sopenharmony_ci 243e1051a39Sopenharmony_ci/*- 244e1051a39Sopenharmony_ci * Verifies whether 'response' contains a valid response with regards 245e1051a39Sopenharmony_ci * to the settings of the context: 246e1051a39Sopenharmony_ci * - Gives an error message if the TS_TST_INFO is not present. 247e1051a39Sopenharmony_ci * - Calls _TS_RESP_verify_token to verify the token content. 248e1051a39Sopenharmony_ci */ 249e1051a39Sopenharmony_ciint TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response) 250e1051a39Sopenharmony_ci{ 251e1051a39Sopenharmony_ci PKCS7 *token = response->token; 252e1051a39Sopenharmony_ci TS_TST_INFO *tst_info = response->tst_info; 253e1051a39Sopenharmony_ci int ret = 0; 254e1051a39Sopenharmony_ci 255e1051a39Sopenharmony_ci if (!ts_check_status_info(response)) 256e1051a39Sopenharmony_ci goto err; 257e1051a39Sopenharmony_ci if (!int_ts_RESP_verify_token(ctx, token, tst_info)) 258e1051a39Sopenharmony_ci goto err; 259e1051a39Sopenharmony_ci ret = 1; 260e1051a39Sopenharmony_ci 261e1051a39Sopenharmony_ci err: 262e1051a39Sopenharmony_ci return ret; 263e1051a39Sopenharmony_ci} 264e1051a39Sopenharmony_ci 265e1051a39Sopenharmony_ci/* 266e1051a39Sopenharmony_ci * Tries to extract a TS_TST_INFO structure from the PKCS7 token and 267e1051a39Sopenharmony_ci * calls the internal int_TS_RESP_verify_token function for verifying it. 268e1051a39Sopenharmony_ci */ 269e1051a39Sopenharmony_ciint TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token) 270e1051a39Sopenharmony_ci{ 271e1051a39Sopenharmony_ci TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token); 272e1051a39Sopenharmony_ci int ret = 0; 273e1051a39Sopenharmony_ci if (tst_info) { 274e1051a39Sopenharmony_ci ret = int_ts_RESP_verify_token(ctx, token, tst_info); 275e1051a39Sopenharmony_ci TS_TST_INFO_free(tst_info); 276e1051a39Sopenharmony_ci } 277e1051a39Sopenharmony_ci return ret; 278e1051a39Sopenharmony_ci} 279e1051a39Sopenharmony_ci 280e1051a39Sopenharmony_ci/*- 281e1051a39Sopenharmony_ci * Verifies whether the 'token' contains a valid time stamp token 282e1051a39Sopenharmony_ci * with regards to the settings of the context. Only those checks are 283e1051a39Sopenharmony_ci * carried out that are specified in the context: 284e1051a39Sopenharmony_ci * - Verifies the signature of the TS_TST_INFO. 285e1051a39Sopenharmony_ci * - Checks the version number of the response. 286e1051a39Sopenharmony_ci * - Check if the requested and returned policies math. 287e1051a39Sopenharmony_ci * - Check if the message imprints are the same. 288e1051a39Sopenharmony_ci * - Check if the nonces are the same. 289e1051a39Sopenharmony_ci * - Check if the TSA name matches the signer. 290e1051a39Sopenharmony_ci * - Check if the TSA name is the expected TSA. 291e1051a39Sopenharmony_ci */ 292e1051a39Sopenharmony_cistatic int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx, 293e1051a39Sopenharmony_ci PKCS7 *token, TS_TST_INFO *tst_info) 294e1051a39Sopenharmony_ci{ 295e1051a39Sopenharmony_ci X509 *signer = NULL; 296e1051a39Sopenharmony_ci GENERAL_NAME *tsa_name = tst_info->tsa; 297e1051a39Sopenharmony_ci X509_ALGOR *md_alg = NULL; 298e1051a39Sopenharmony_ci unsigned char *imprint = NULL; 299e1051a39Sopenharmony_ci unsigned imprint_len = 0; 300e1051a39Sopenharmony_ci int ret = 0; 301e1051a39Sopenharmony_ci int flags = ctx->flags; 302e1051a39Sopenharmony_ci 303e1051a39Sopenharmony_ci /* Some options require us to also check the signature */ 304e1051a39Sopenharmony_ci if (((flags & TS_VFY_SIGNER) && tsa_name != NULL) 305e1051a39Sopenharmony_ci || (flags & TS_VFY_TSA_NAME)) { 306e1051a39Sopenharmony_ci flags |= TS_VFY_SIGNATURE; 307e1051a39Sopenharmony_ci } 308e1051a39Sopenharmony_ci 309e1051a39Sopenharmony_ci if ((flags & TS_VFY_SIGNATURE) 310e1051a39Sopenharmony_ci && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer)) 311e1051a39Sopenharmony_ci goto err; 312e1051a39Sopenharmony_ci if ((flags & TS_VFY_VERSION) 313e1051a39Sopenharmony_ci && TS_TST_INFO_get_version(tst_info) != 1) { 314e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_UNSUPPORTED_VERSION); 315e1051a39Sopenharmony_ci goto err; 316e1051a39Sopenharmony_ci } 317e1051a39Sopenharmony_ci if ((flags & TS_VFY_POLICY) 318e1051a39Sopenharmony_ci && !ts_check_policy(ctx->policy, tst_info)) 319e1051a39Sopenharmony_ci goto err; 320e1051a39Sopenharmony_ci if ((flags & TS_VFY_IMPRINT) 321e1051a39Sopenharmony_ci && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len, 322e1051a39Sopenharmony_ci tst_info)) 323e1051a39Sopenharmony_ci goto err; 324e1051a39Sopenharmony_ci if ((flags & TS_VFY_DATA) 325e1051a39Sopenharmony_ci && (!ts_compute_imprint(ctx->data, tst_info, 326e1051a39Sopenharmony_ci &md_alg, &imprint, &imprint_len) 327e1051a39Sopenharmony_ci || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info))) 328e1051a39Sopenharmony_ci goto err; 329e1051a39Sopenharmony_ci if ((flags & TS_VFY_NONCE) 330e1051a39Sopenharmony_ci && !ts_check_nonces(ctx->nonce, tst_info)) 331e1051a39Sopenharmony_ci goto err; 332e1051a39Sopenharmony_ci if ((flags & TS_VFY_SIGNER) 333e1051a39Sopenharmony_ci && tsa_name && !ts_check_signer_name(tsa_name, signer)) { 334e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_TSA_NAME_MISMATCH); 335e1051a39Sopenharmony_ci goto err; 336e1051a39Sopenharmony_ci } 337e1051a39Sopenharmony_ci if ((flags & TS_VFY_TSA_NAME) 338e1051a39Sopenharmony_ci && !ts_check_signer_name(ctx->tsa_name, signer)) { 339e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_TSA_UNTRUSTED); 340e1051a39Sopenharmony_ci goto err; 341e1051a39Sopenharmony_ci } 342e1051a39Sopenharmony_ci ret = 1; 343e1051a39Sopenharmony_ci 344e1051a39Sopenharmony_ci err: 345e1051a39Sopenharmony_ci X509_free(signer); 346e1051a39Sopenharmony_ci X509_ALGOR_free(md_alg); 347e1051a39Sopenharmony_ci OPENSSL_free(imprint); 348e1051a39Sopenharmony_ci return ret; 349e1051a39Sopenharmony_ci} 350e1051a39Sopenharmony_ci 351e1051a39Sopenharmony_cistatic int ts_check_status_info(TS_RESP *response) 352e1051a39Sopenharmony_ci{ 353e1051a39Sopenharmony_ci TS_STATUS_INFO *info = response->status_info; 354e1051a39Sopenharmony_ci long status = ASN1_INTEGER_get(info->status); 355e1051a39Sopenharmony_ci const char *status_text = NULL; 356e1051a39Sopenharmony_ci char *embedded_status_text = NULL; 357e1051a39Sopenharmony_ci char failure_text[TS_STATUS_BUF_SIZE] = ""; 358e1051a39Sopenharmony_ci 359e1051a39Sopenharmony_ci if (status == 0 || status == 1) 360e1051a39Sopenharmony_ci return 1; 361e1051a39Sopenharmony_ci 362e1051a39Sopenharmony_ci /* There was an error, get the description in status_text. */ 363e1051a39Sopenharmony_ci if (0 <= status && status < (long) OSSL_NELEM(ts_status_text)) 364e1051a39Sopenharmony_ci status_text = ts_status_text[status]; 365e1051a39Sopenharmony_ci else 366e1051a39Sopenharmony_ci status_text = "unknown code"; 367e1051a39Sopenharmony_ci 368e1051a39Sopenharmony_ci if (sk_ASN1_UTF8STRING_num(info->text) > 0 369e1051a39Sopenharmony_ci && (embedded_status_text = ts_get_status_text(info->text)) == NULL) 370e1051a39Sopenharmony_ci return 0; 371e1051a39Sopenharmony_ci 372e1051a39Sopenharmony_ci /* Fill in failure_text with the failure information. */ 373e1051a39Sopenharmony_ci if (info->failure_info) { 374e1051a39Sopenharmony_ci int i; 375e1051a39Sopenharmony_ci int first = 1; 376e1051a39Sopenharmony_ci for (i = 0; i < (int)OSSL_NELEM(ts_failure_info); ++i) { 377e1051a39Sopenharmony_ci if (ASN1_BIT_STRING_get_bit(info->failure_info, 378e1051a39Sopenharmony_ci ts_failure_info[i].code)) { 379e1051a39Sopenharmony_ci if (!first) 380e1051a39Sopenharmony_ci strcat(failure_text, ","); 381e1051a39Sopenharmony_ci else 382e1051a39Sopenharmony_ci first = 0; 383e1051a39Sopenharmony_ci strcat(failure_text, ts_failure_info[i].text); 384e1051a39Sopenharmony_ci } 385e1051a39Sopenharmony_ci } 386e1051a39Sopenharmony_ci } 387e1051a39Sopenharmony_ci if (failure_text[0] == '\0') 388e1051a39Sopenharmony_ci strcpy(failure_text, "unspecified"); 389e1051a39Sopenharmony_ci 390e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_TS, TS_R_NO_TIME_STAMP_TOKEN, 391e1051a39Sopenharmony_ci "status code: %s, status text: %s, failure codes: %s", 392e1051a39Sopenharmony_ci status_text, 393e1051a39Sopenharmony_ci embedded_status_text ? embedded_status_text : "unspecified", 394e1051a39Sopenharmony_ci failure_text); 395e1051a39Sopenharmony_ci OPENSSL_free(embedded_status_text); 396e1051a39Sopenharmony_ci 397e1051a39Sopenharmony_ci return 0; 398e1051a39Sopenharmony_ci} 399e1051a39Sopenharmony_ci 400e1051a39Sopenharmony_cistatic char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text) 401e1051a39Sopenharmony_ci{ 402e1051a39Sopenharmony_ci return ossl_sk_ASN1_UTF8STRING2text(text, "/", TS_MAX_STATUS_LENGTH); 403e1051a39Sopenharmony_ci} 404e1051a39Sopenharmony_ci 405e1051a39Sopenharmony_cistatic int ts_check_policy(const ASN1_OBJECT *req_oid, 406e1051a39Sopenharmony_ci const TS_TST_INFO *tst_info) 407e1051a39Sopenharmony_ci{ 408e1051a39Sopenharmony_ci const ASN1_OBJECT *resp_oid = tst_info->policy_id; 409e1051a39Sopenharmony_ci 410e1051a39Sopenharmony_ci if (OBJ_cmp(req_oid, resp_oid) != 0) { 411e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_POLICY_MISMATCH); 412e1051a39Sopenharmony_ci return 0; 413e1051a39Sopenharmony_ci } 414e1051a39Sopenharmony_ci 415e1051a39Sopenharmony_ci return 1; 416e1051a39Sopenharmony_ci} 417e1051a39Sopenharmony_ci 418e1051a39Sopenharmony_cistatic int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info, 419e1051a39Sopenharmony_ci X509_ALGOR **md_alg, 420e1051a39Sopenharmony_ci unsigned char **imprint, unsigned *imprint_len) 421e1051a39Sopenharmony_ci{ 422e1051a39Sopenharmony_ci TS_MSG_IMPRINT *msg_imprint = tst_info->msg_imprint; 423e1051a39Sopenharmony_ci X509_ALGOR *md_alg_resp = msg_imprint->hash_algo; 424e1051a39Sopenharmony_ci EVP_MD *md = NULL; 425e1051a39Sopenharmony_ci EVP_MD_CTX *md_ctx = NULL; 426e1051a39Sopenharmony_ci unsigned char buffer[4096]; 427e1051a39Sopenharmony_ci char name[OSSL_MAX_NAME_SIZE]; 428e1051a39Sopenharmony_ci int length; 429e1051a39Sopenharmony_ci 430e1051a39Sopenharmony_ci *md_alg = NULL; 431e1051a39Sopenharmony_ci *imprint = NULL; 432e1051a39Sopenharmony_ci 433e1051a39Sopenharmony_ci if ((*md_alg = X509_ALGOR_dup(md_alg_resp)) == NULL) 434e1051a39Sopenharmony_ci goto err; 435e1051a39Sopenharmony_ci 436e1051a39Sopenharmony_ci OBJ_obj2txt(name, sizeof(name), md_alg_resp->algorithm, 0); 437e1051a39Sopenharmony_ci 438e1051a39Sopenharmony_ci (void)ERR_set_mark(); 439e1051a39Sopenharmony_ci md = EVP_MD_fetch(NULL, name, NULL); 440e1051a39Sopenharmony_ci 441e1051a39Sopenharmony_ci if (md == NULL) 442e1051a39Sopenharmony_ci md = (EVP_MD *)EVP_get_digestbyname(name); 443e1051a39Sopenharmony_ci 444e1051a39Sopenharmony_ci if (md == NULL) { 445e1051a39Sopenharmony_ci (void)ERR_clear_last_mark(); 446e1051a39Sopenharmony_ci goto err; 447e1051a39Sopenharmony_ci } 448e1051a39Sopenharmony_ci (void)ERR_pop_to_mark(); 449e1051a39Sopenharmony_ci 450e1051a39Sopenharmony_ci length = EVP_MD_get_size(md); 451e1051a39Sopenharmony_ci if (length < 0) 452e1051a39Sopenharmony_ci goto err; 453e1051a39Sopenharmony_ci *imprint_len = length; 454e1051a39Sopenharmony_ci if ((*imprint = OPENSSL_malloc(*imprint_len)) == NULL) { 455e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE); 456e1051a39Sopenharmony_ci goto err; 457e1051a39Sopenharmony_ci } 458e1051a39Sopenharmony_ci 459e1051a39Sopenharmony_ci md_ctx = EVP_MD_CTX_new(); 460e1051a39Sopenharmony_ci if (md_ctx == NULL) { 461e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, ERR_R_MALLOC_FAILURE); 462e1051a39Sopenharmony_ci goto err; 463e1051a39Sopenharmony_ci } 464e1051a39Sopenharmony_ci if (!EVP_DigestInit(md_ctx, md)) 465e1051a39Sopenharmony_ci goto err; 466e1051a39Sopenharmony_ci EVP_MD_free(md); 467e1051a39Sopenharmony_ci md = NULL; 468e1051a39Sopenharmony_ci while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) { 469e1051a39Sopenharmony_ci if (!EVP_DigestUpdate(md_ctx, buffer, length)) 470e1051a39Sopenharmony_ci goto err; 471e1051a39Sopenharmony_ci } 472e1051a39Sopenharmony_ci if (!EVP_DigestFinal(md_ctx, *imprint, NULL)) 473e1051a39Sopenharmony_ci goto err; 474e1051a39Sopenharmony_ci EVP_MD_CTX_free(md_ctx); 475e1051a39Sopenharmony_ci 476e1051a39Sopenharmony_ci return 1; 477e1051a39Sopenharmony_ci err: 478e1051a39Sopenharmony_ci EVP_MD_CTX_free(md_ctx); 479e1051a39Sopenharmony_ci EVP_MD_free(md); 480e1051a39Sopenharmony_ci X509_ALGOR_free(*md_alg); 481e1051a39Sopenharmony_ci *md_alg = NULL; 482e1051a39Sopenharmony_ci OPENSSL_free(*imprint); 483e1051a39Sopenharmony_ci *imprint_len = 0; 484e1051a39Sopenharmony_ci *imprint = 0; 485e1051a39Sopenharmony_ci return 0; 486e1051a39Sopenharmony_ci} 487e1051a39Sopenharmony_ci 488e1051a39Sopenharmony_cistatic int ts_check_imprints(X509_ALGOR *algor_a, 489e1051a39Sopenharmony_ci const unsigned char *imprint_a, unsigned len_a, 490e1051a39Sopenharmony_ci TS_TST_INFO *tst_info) 491e1051a39Sopenharmony_ci{ 492e1051a39Sopenharmony_ci TS_MSG_IMPRINT *b = tst_info->msg_imprint; 493e1051a39Sopenharmony_ci X509_ALGOR *algor_b = b->hash_algo; 494e1051a39Sopenharmony_ci int ret = 0; 495e1051a39Sopenharmony_ci 496e1051a39Sopenharmony_ci if (algor_a) { 497e1051a39Sopenharmony_ci if (OBJ_cmp(algor_a->algorithm, algor_b->algorithm)) 498e1051a39Sopenharmony_ci goto err; 499e1051a39Sopenharmony_ci 500e1051a39Sopenharmony_ci /* The parameter must be NULL in both. */ 501e1051a39Sopenharmony_ci if ((algor_a->parameter 502e1051a39Sopenharmony_ci && ASN1_TYPE_get(algor_a->parameter) != V_ASN1_NULL) 503e1051a39Sopenharmony_ci || (algor_b->parameter 504e1051a39Sopenharmony_ci && ASN1_TYPE_get(algor_b->parameter) != V_ASN1_NULL)) 505e1051a39Sopenharmony_ci goto err; 506e1051a39Sopenharmony_ci } 507e1051a39Sopenharmony_ci 508e1051a39Sopenharmony_ci ret = len_a == (unsigned)ASN1_STRING_length(b->hashed_msg) && 509e1051a39Sopenharmony_ci memcmp(imprint_a, ASN1_STRING_get0_data(b->hashed_msg), len_a) == 0; 510e1051a39Sopenharmony_ci err: 511e1051a39Sopenharmony_ci if (!ret) 512e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_MESSAGE_IMPRINT_MISMATCH); 513e1051a39Sopenharmony_ci return ret; 514e1051a39Sopenharmony_ci} 515e1051a39Sopenharmony_ci 516e1051a39Sopenharmony_cistatic int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info) 517e1051a39Sopenharmony_ci{ 518e1051a39Sopenharmony_ci const ASN1_INTEGER *b = tst_info->nonce; 519e1051a39Sopenharmony_ci 520e1051a39Sopenharmony_ci if (!b) { 521e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_NONCE_NOT_RETURNED); 522e1051a39Sopenharmony_ci return 0; 523e1051a39Sopenharmony_ci } 524e1051a39Sopenharmony_ci 525e1051a39Sopenharmony_ci /* No error if a nonce is returned without being requested. */ 526e1051a39Sopenharmony_ci if (ASN1_INTEGER_cmp(a, b) != 0) { 527e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_TS, TS_R_NONCE_MISMATCH); 528e1051a39Sopenharmony_ci return 0; 529e1051a39Sopenharmony_ci } 530e1051a39Sopenharmony_ci 531e1051a39Sopenharmony_ci return 1; 532e1051a39Sopenharmony_ci} 533e1051a39Sopenharmony_ci 534e1051a39Sopenharmony_ci/* 535e1051a39Sopenharmony_ci * Check if the specified TSA name matches either the subject or one of the 536e1051a39Sopenharmony_ci * subject alternative names of the TSA certificate. 537e1051a39Sopenharmony_ci */ 538e1051a39Sopenharmony_cistatic int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer) 539e1051a39Sopenharmony_ci{ 540e1051a39Sopenharmony_ci STACK_OF(GENERAL_NAME) *gen_names = NULL; 541e1051a39Sopenharmony_ci int idx = -1; 542e1051a39Sopenharmony_ci int found = 0; 543e1051a39Sopenharmony_ci 544e1051a39Sopenharmony_ci if (tsa_name->type == GEN_DIRNAME 545e1051a39Sopenharmony_ci && X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0) 546e1051a39Sopenharmony_ci return 1; 547e1051a39Sopenharmony_ci gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx); 548e1051a39Sopenharmony_ci while (gen_names != NULL) { 549e1051a39Sopenharmony_ci found = ts_find_name(gen_names, tsa_name) >= 0; 550e1051a39Sopenharmony_ci if (found) 551e1051a39Sopenharmony_ci break; 552e1051a39Sopenharmony_ci /* 553e1051a39Sopenharmony_ci * Get the next subject alternative name, although there should be no 554e1051a39Sopenharmony_ci * more than one. 555e1051a39Sopenharmony_ci */ 556e1051a39Sopenharmony_ci GENERAL_NAMES_free(gen_names); 557e1051a39Sopenharmony_ci gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx); 558e1051a39Sopenharmony_ci } 559e1051a39Sopenharmony_ci GENERAL_NAMES_free(gen_names); 560e1051a39Sopenharmony_ci 561e1051a39Sopenharmony_ci return found; 562e1051a39Sopenharmony_ci} 563e1051a39Sopenharmony_ci 564e1051a39Sopenharmony_ci/* Returns 1 if name is in gen_names, 0 otherwise. */ 565e1051a39Sopenharmony_cistatic int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name) 566e1051a39Sopenharmony_ci{ 567e1051a39Sopenharmony_ci int i, found; 568e1051a39Sopenharmony_ci for (i = 0, found = 0; !found && i < sk_GENERAL_NAME_num(gen_names); ++i) { 569e1051a39Sopenharmony_ci GENERAL_NAME *current = sk_GENERAL_NAME_value(gen_names, i); 570e1051a39Sopenharmony_ci found = GENERAL_NAME_cmp(current, name) == 0; 571e1051a39Sopenharmony_ci } 572e1051a39Sopenharmony_ci return found ? i - 1 : -1; 573e1051a39Sopenharmony_ci} 574