2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2016-05-18 02:51:04 +08:00
|
|
|
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2006-02-13 07:11:56 +08:00
|
|
|
*
|
2016-05-18 02:51:04 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2006-02-13 07:11:56 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2006-02-13 07:11:56 +08:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/ts.h>
|
|
|
|
#include <openssl/pkcs7.h>
|
2015-06-11 02:07:40 +08:00
|
|
|
#include "ts_lcl.h"
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509 *signer, STACK_OF(X509) **chain);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(X509) *chain);
|
2015-06-04 10:04:48 +08:00
|
|
|
static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si);
|
|
|
|
static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert);
|
2015-09-03 05:01:18 +08:00
|
|
|
static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
|
2015-01-22 11:40:55 +08:00
|
|
|
PKCS7 *token, TS_TST_INFO *tst_info);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_status_info(TS_RESP *response);
|
|
|
|
static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text);
|
2016-08-01 01:38:09 +08:00
|
|
|
static int ts_check_policy(const ASN1_OBJECT *req_oid,
|
|
|
|
const TS_TST_INFO *tst_info);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR **md_alg,
|
|
|
|
unsigned char **imprint, unsigned *imprint_len);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_imprints(X509_ALGOR *algor_a,
|
2016-08-01 01:38:09 +08:00
|
|
|
const unsigned char *imprint_a, unsigned len_a,
|
2015-01-22 11:40:55 +08:00
|
|
|
TS_TST_INFO *tst_info);
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info);
|
|
|
|
static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer);
|
|
|
|
static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names,
|
2015-01-22 11:40:55 +08:00
|
|
|
GENERAL_NAME *name);
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-11-02 09:15:23 +08:00
|
|
|
/*
|
|
|
|
* This must be large enough to hold all values in ts_status_text (with
|
|
|
|
* comma separator) or all text fields in ts_failure_info (also with comma).
|
|
|
|
*/
|
|
|
|
#define TS_STATUS_BUF_SIZE 256
|
|
|
|
|
2006-02-13 07:11:56 +08:00
|
|
|
/*
|
|
|
|
* Local mapping between response codes and descriptions.
|
|
|
|
*/
|
2015-11-02 09:15:23 +08:00
|
|
|
static const char *ts_status_text[] = {
|
|
|
|
"granted",
|
2015-01-22 11:40:55 +08:00
|
|
|
"grantedWithMods",
|
|
|
|
"rejection",
|
|
|
|
"waiting",
|
|
|
|
"revocationWarning",
|
|
|
|
"revocationNotification"
|
|
|
|
};
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
#define TS_STATUS_TEXT_SIZE OSSL_NELEM(ts_status_text)
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static struct {
|
|
|
|
int code;
|
|
|
|
const char *text;
|
2015-06-04 10:04:48 +08:00
|
|
|
} ts_failure_info[] = {
|
|
|
|
{TS_INFO_BAD_ALG, "badAlg"},
|
|
|
|
{TS_INFO_BAD_REQUEST, "badRequest"},
|
|
|
|
{TS_INFO_BAD_DATA_FORMAT, "badDataFormat"},
|
|
|
|
{TS_INFO_TIME_NOT_AVAILABLE, "timeNotAvailable"},
|
|
|
|
{TS_INFO_UNACCEPTED_POLICY, "unacceptedPolicy"},
|
|
|
|
{TS_INFO_UNACCEPTED_EXTENSION, "unacceptedExtension"},
|
|
|
|
{TS_INFO_ADD_INFO_NOT_AVAILABLE, "addInfoNotAvailable"},
|
|
|
|
{TS_INFO_SYSTEM_FAILURE, "systemFailure"}
|
2015-01-22 11:40:55 +08:00
|
|
|
};
|
|
|
|
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-16 17:21:50 +08:00
|
|
|
/*-
|
2006-02-13 07:11:56 +08:00
|
|
|
* This function carries out the following tasks:
|
2015-01-22 11:40:55 +08:00
|
|
|
* - Checks if there is one and only one signer.
|
|
|
|
* - Search for the signing certificate in 'certs' and in the response.
|
|
|
|
* - Check the extended key usage and key usage fields of the signer
|
|
|
|
* certificate (done by the path validation).
|
|
|
|
* - Build and validate the certificate path.
|
|
|
|
* - Check if the certificate path meets the requirements of the
|
|
|
|
* SigningCertificate ESS signed attribute.
|
|
|
|
* - Verify the signature value.
|
|
|
|
* - Returns the signer certificate in 'signer', if 'signer' is not NULL.
|
2006-02-13 07:11:56 +08:00
|
|
|
*/
|
|
|
|
int TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_STORE *store, X509 **signer_out)
|
|
|
|
{
|
|
|
|
STACK_OF(PKCS7_SIGNER_INFO) *sinfos = NULL;
|
|
|
|
PKCS7_SIGNER_INFO *si;
|
|
|
|
STACK_OF(X509) *signers = NULL;
|
|
|
|
X509 *signer;
|
|
|
|
STACK_OF(X509) *chain = NULL;
|
|
|
|
char buf[4096];
|
|
|
|
int i, j = 0, ret = 0;
|
|
|
|
BIO *p7bio = NULL;
|
|
|
|
|
|
|
|
/* Some sanity checks first. */
|
|
|
|
if (!token) {
|
|
|
|
TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_INVALID_NULL_POINTER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!PKCS7_type_is_signed(token)) {
|
|
|
|
TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_WRONG_CONTENT_TYPE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sinfos = PKCS7_get_signer_info(token);
|
|
|
|
if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) {
|
|
|
|
TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_THERE_MUST_BE_ONE_SIGNER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0);
|
|
|
|
if (PKCS7_get_detached(token)) {
|
|
|
|
TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_NO_CONTENT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get hold of the signer certificate, search only internal certificates
|
|
|
|
* if it was requested.
|
|
|
|
*/
|
|
|
|
signers = PKCS7_get0_signers(token, certs, 0);
|
|
|
|
if (!signers || sk_X509_num(signers) != 1)
|
|
|
|
goto err;
|
|
|
|
signer = sk_X509_value(signers, 0);
|
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
if (!ts_verify_cert(store, certs, signer, &chain))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-06-04 10:04:48 +08:00
|
|
|
if (!ts_check_signing_certs(si, chain))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
p7bio = PKCS7_dataInit(token, NULL);
|
|
|
|
|
|
|
|
/* We now have to 'read' from p7bio to calculate digests etc. */
|
2015-05-08 11:41:07 +08:00
|
|
|
while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0)
|
|
|
|
continue;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
j = PKCS7_signatureVerify(p7bio, token, si, signer);
|
|
|
|
if (j <= 0) {
|
|
|
|
TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_SIGNATURE_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signer_out) {
|
|
|
|
*signer_out = signer;
|
2015-09-01 03:29:57 +08:00
|
|
|
X509_up_ref(signer);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
ret = 1;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_free_all(p7bio);
|
|
|
|
sk_X509_pop_free(chain, X509_free);
|
|
|
|
sk_X509_free(signers);
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The certificate chain is returned in chain. Caller is responsible for
|
|
|
|
* freeing the vector.
|
|
|
|
*/
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509 *signer, STACK_OF(X509) **chain)
|
|
|
|
{
|
2016-04-15 11:59:26 +08:00
|
|
|
X509_STORE_CTX *cert_ctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
int i;
|
2016-04-15 11:59:26 +08:00
|
|
|
int ret = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
*chain = NULL;
|
2016-04-15 11:59:26 +08:00
|
|
|
cert_ctx = X509_STORE_CTX_new();
|
|
|
|
if (cert_ctx == NULL) {
|
|
|
|
TSerr(TS_F_TS_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted))
|
|
|
|
goto end;
|
|
|
|
X509_STORE_CTX_set_purpose(cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);
|
|
|
|
i = X509_verify_cert(cert_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (i <= 0) {
|
2016-04-15 11:59:26 +08:00
|
|
|
int j = X509_STORE_CTX_get_error(cert_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);
|
|
|
|
ERR_add_error_data(2, "Verify error:",
|
|
|
|
X509_verify_cert_error_string(j));
|
2016-04-15 11:59:26 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2016-04-15 11:59:26 +08:00
|
|
|
*chain = X509_STORE_CTX_get1_chain(cert_ctx);
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-04-15 11:59:26 +08:00
|
|
|
err:
|
|
|
|
ret = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-04-15 11:59:26 +08:00
|
|
|
end:
|
|
|
|
X509_STORE_CTX_free(cert_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(X509) *chain)
|
|
|
|
{
|
2015-06-04 10:04:48 +08:00
|
|
|
ESS_SIGNING_CERT *ss = ess_get_signing_cert(si);
|
2015-01-22 11:40:55 +08:00
|
|
|
STACK_OF(ESS_CERT_ID) *cert_ids = NULL;
|
|
|
|
X509 *cert;
|
|
|
|
int i = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!ss)
|
|
|
|
goto err;
|
|
|
|
cert_ids = ss->cert_ids;
|
|
|
|
cert = sk_X509_value(chain, 0);
|
2015-06-04 10:04:48 +08:00
|
|
|
if (ts_find_cert(cert_ids, cert) != 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the other certificates of the chain if there are more than one
|
|
|
|
* certificate ids in cert_ids.
|
|
|
|
*/
|
|
|
|
if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
|
|
|
|
for (i = 1; i < sk_X509_num(chain); ++i) {
|
|
|
|
cert = sk_X509_value(chain, i);
|
2015-06-04 10:04:48 +08:00
|
|
|
if (ts_find_cert(cert_ids, cert) < 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 1;
|
2006-02-13 07:11:56 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!ret)
|
|
|
|
TSerr(TS_F_TS_CHECK_SIGNING_CERTS,
|
|
|
|
TS_R_ESS_SIGNING_CERTIFICATE_ERROR);
|
|
|
|
ESS_SIGNING_CERT_free(ss);
|
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ASN1_TYPE *attr;
|
|
|
|
const unsigned char *p;
|
|
|
|
attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate);
|
|
|
|
if (!attr)
|
|
|
|
return NULL;
|
|
|
|
p = attr->value.sequence->data;
|
|
|
|
return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
|
|
|
/* Returns < 0 if certificate is not found, certificate index otherwise. */
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
2015-09-23 01:37:57 +08:00
|
|
|
unsigned char cert_sha1[SHA_DIGEST_LENGTH];
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (!cert_ids || !cert)
|
|
|
|
return -1;
|
|
|
|
|
2015-09-23 01:37:57 +08:00
|
|
|
X509_digest(cert, EVP_sha1(), cert_sha1, NULL);
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Recompute SHA1 hash of certificate if necessary (side effect). */
|
|
|
|
X509_check_purpose(cert, -1, 0);
|
|
|
|
|
|
|
|
/* Look for cert in the cert_ids vector. */
|
|
|
|
for (i = 0; i < sk_ESS_CERT_ID_num(cert_ids); ++i) {
|
|
|
|
ESS_CERT_ID *cid = sk_ESS_CERT_ID_value(cert_ids, i);
|
|
|
|
|
2015-09-23 01:37:57 +08:00
|
|
|
if (cid->hash->length == SHA_DIGEST_LENGTH
|
|
|
|
&& memcmp(cid->hash->data, cert_sha1, SHA_DIGEST_LENGTH) == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ESS_ISSUER_SERIAL *is = cid->issuer_serial;
|
2015-09-03 05:01:18 +08:00
|
|
|
if (!is || !ts_issuer_serial_cmp(is, cert))
|
2015-01-22 11:40:55 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-09-03 05:01:18 +08:00
|
|
|
static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
GENERAL_NAME *issuer;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-09-03 05:01:18 +08:00
|
|
|
if (!is || !cert || sk_GENERAL_NAME_num(is->issuer) != 1)
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
issuer = sk_GENERAL_NAME_value(is->issuer, 0);
|
|
|
|
if (issuer->type != GEN_DIRNAME
|
2015-09-03 05:01:18 +08:00
|
|
|
|| X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)))
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-09-03 05:01:18 +08:00
|
|
|
if (ASN1_INTEGER_cmp(is->serial, X509_get_serialNumber(cert)))
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-16 17:21:50 +08:00
|
|
|
/*-
|
2015-01-22 11:40:55 +08:00
|
|
|
* Verifies whether 'response' contains a valid response with regards
|
2006-02-13 07:11:56 +08:00
|
|
|
* to the settings of the context:
|
2015-01-22 11:40:55 +08:00
|
|
|
* - Gives an error message if the TS_TST_INFO is not present.
|
|
|
|
* - Calls _TS_RESP_verify_token to verify the token content.
|
2006-02-13 07:11:56 +08:00
|
|
|
*/
|
|
|
|
int TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-06-11 02:07:40 +08:00
|
|
|
PKCS7 *token = response->token;
|
|
|
|
TS_TST_INFO *tst_info = response->tst_info;
|
2015-01-22 11:40:55 +08:00
|
|
|
int ret = 0;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
if (!ts_check_status_info(response))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-06-04 10:04:48 +08:00
|
|
|
if (!int_ts_RESP_verify_token(ctx, token, tst_info))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
ret = 1;
|
2015-05-08 11:41:07 +08:00
|
|
|
|
2006-02-13 07:11:56 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tries to extract a TS_TST_INFO structure from the PKCS7 token and
|
2007-04-24 09:06:19 +08:00
|
|
|
* calls the internal int_TS_RESP_verify_token function for verifying it.
|
2006-02-13 07:11:56 +08:00
|
|
|
*/
|
|
|
|
int TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token);
|
|
|
|
int ret = 0;
|
|
|
|
if (tst_info) {
|
2015-06-04 10:04:48 +08:00
|
|
|
ret = int_ts_RESP_verify_token(ctx, token, tst_info);
|
2015-01-22 11:40:55 +08:00
|
|
|
TS_TST_INFO_free(tst_info);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2014-12-28 10:48:40 +08:00
|
|
|
/*-
|
2015-01-22 11:40:55 +08:00
|
|
|
* Verifies whether the 'token' contains a valid time stamp token
|
2006-02-13 07:11:56 +08:00
|
|
|
* with regards to the settings of the context. Only those checks are
|
|
|
|
* carried out that are specified in the context:
|
2015-01-22 11:40:55 +08:00
|
|
|
* - Verifies the signature of the TS_TST_INFO.
|
|
|
|
* - Checks the version number of the response.
|
|
|
|
* - Check if the requested and returned policies math.
|
|
|
|
* - Check if the message imprints are the same.
|
|
|
|
* - Check if the nonces are the same.
|
|
|
|
* - Check if the TSA name matches the signer.
|
|
|
|
* - Check if the TSA name is the expected TSA.
|
2006-02-13 07:11:56 +08:00
|
|
|
*/
|
2015-06-04 10:04:48 +08:00
|
|
|
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
|
2015-01-22 11:40:55 +08:00
|
|
|
PKCS7 *token, TS_TST_INFO *tst_info)
|
|
|
|
{
|
|
|
|
X509 *signer = NULL;
|
2015-06-11 02:07:40 +08:00
|
|
|
GENERAL_NAME *tsa_name = tst_info->tsa;
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR *md_alg = NULL;
|
|
|
|
unsigned char *imprint = NULL;
|
|
|
|
unsigned imprint_len = 0;
|
|
|
|
int ret = 0;
|
2016-06-10 21:25:15 +08:00
|
|
|
int flags = ctx->flags;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-06-10 21:25:15 +08:00
|
|
|
/* Some options require us to also check the signature */
|
|
|
|
if (((flags & TS_VFY_SIGNER) && tsa_name != NULL)
|
|
|
|
|| (flags & TS_VFY_TSA_NAME)) {
|
|
|
|
flags |= TS_VFY_SIGNATURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & TS_VFY_SIGNATURE)
|
2015-01-22 11:40:55 +08:00
|
|
|
&& !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
|
|
|
|
goto err;
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_VERSION)
|
2015-01-22 11:40:55 +08:00
|
|
|
&& TS_TST_INFO_get_version(tst_info) != 1) {
|
|
|
|
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_POLICY)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& !ts_check_policy(ctx->policy, tst_info))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_IMPRINT)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
|
2015-01-22 11:40:55 +08:00
|
|
|
tst_info))
|
|
|
|
goto err;
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_DATA)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& (!ts_compute_imprint(ctx->data, tst_info,
|
2015-01-22 11:40:55 +08:00
|
|
|
&md_alg, &imprint, &imprint_len)
|
2015-06-04 10:04:48 +08:00
|
|
|
|| !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_NONCE)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& !ts_check_nonces(ctx->nonce, tst_info))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_SIGNER)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& tsa_name && !ts_check_signer_name(tsa_name, signer)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-06-10 21:25:15 +08:00
|
|
|
if ((flags & TS_VFY_TSA_NAME)
|
2015-06-04 10:04:48 +08:00
|
|
|
&& !ts_check_signer_name(ctx->tsa_name, signer)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ret = 1;
|
2015-05-08 11:41:07 +08:00
|
|
|
|
2006-02-13 07:11:56 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_free(signer);
|
|
|
|
X509_ALGOR_free(md_alg);
|
|
|
|
OPENSSL_free(imprint);
|
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_status_info(TS_RESP *response)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-06-11 02:07:40 +08:00
|
|
|
TS_STATUS_INFO *info = response->status_info;
|
2015-01-22 11:40:55 +08:00
|
|
|
long status = ASN1_INTEGER_get(info->status);
|
|
|
|
const char *status_text = NULL;
|
|
|
|
char *embedded_status_text = NULL;
|
|
|
|
char failure_text[TS_STATUS_BUF_SIZE] = "";
|
|
|
|
|
|
|
|
if (status == 0 || status == 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* There was an error, get the description in status_text. */
|
2015-11-02 09:15:23 +08:00
|
|
|
if (0 <= status && status < (long) OSSL_NELEM(ts_status_text))
|
2015-06-04 10:04:48 +08:00
|
|
|
status_text = ts_status_text[status];
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
|
|
|
status_text = "unknown code";
|
|
|
|
|
|
|
|
if (sk_ASN1_UTF8STRING_num(info->text) > 0
|
2015-06-04 10:04:48 +08:00
|
|
|
&& (embedded_status_text = ts_get_status_text(info->text)) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-05-08 11:41:07 +08:00
|
|
|
/* Fill in failure_text with the failure information. */
|
2015-01-22 11:40:55 +08:00
|
|
|
if (info->failure_info) {
|
|
|
|
int i;
|
|
|
|
int first = 1;
|
2015-05-08 11:41:07 +08:00
|
|
|
for (i = 0; i < (int)OSSL_NELEM(ts_failure_info); ++i) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ASN1_BIT_STRING_get_bit(info->failure_info,
|
2015-06-04 10:04:48 +08:00
|
|
|
ts_failure_info[i].code)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!first)
|
2015-11-02 09:15:23 +08:00
|
|
|
strcat(failure_text, ",");
|
2015-01-22 11:40:55 +08:00
|
|
|
else
|
|
|
|
first = 0;
|
2015-06-04 10:04:48 +08:00
|
|
|
strcat(failure_text, ts_failure_info[i].text);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failure_text[0] == '\0')
|
|
|
|
strcpy(failure_text, "unspecified");
|
|
|
|
|
|
|
|
TSerr(TS_F_TS_CHECK_STATUS_INFO, TS_R_NO_TIME_STAMP_TOKEN);
|
|
|
|
ERR_add_error_data(6,
|
|
|
|
"status code: ", status_text,
|
|
|
|
", status text: ", embedded_status_text ?
|
|
|
|
embedded_status_text : "unspecified",
|
|
|
|
", failure codes: ", failure_text);
|
|
|
|
OPENSSL_free(embedded_status_text);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
2016-08-03 04:38:37 +08:00
|
|
|
int length = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
char *result = NULL;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
|
|
|
|
ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
|
2016-08-03 04:38:37 +08:00
|
|
|
if (ASN1_STRING_length(current) > TS_MAX_STATUS_LENGTH - length - 1)
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
length += ASN1_STRING_length(current);
|
|
|
|
length += 1; /* separator character */
|
|
|
|
}
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((result = OPENSSL_malloc(length)) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_TS_GET_STATUS_TEXT, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-06-29 04:51:01 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) {
|
|
|
|
ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
|
|
|
|
length = ASN1_STRING_length(current);
|
|
|
|
if (i > 0)
|
|
|
|
*p++ = '/';
|
2016-08-16 21:06:48 +08:00
|
|
|
strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
|
2015-01-22 11:40:55 +08:00
|
|
|
p += length;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2016-08-01 01:38:09 +08:00
|
|
|
static int ts_check_policy(const ASN1_OBJECT *req_oid,
|
|
|
|
const TS_TST_INFO *tst_info)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2016-08-01 01:38:09 +08:00
|
|
|
const ASN1_OBJECT *resp_oid = tst_info->policy_id;
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (OBJ_cmp(req_oid, resp_oid) != 0) {
|
|
|
|
TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR **md_alg,
|
|
|
|
unsigned char **imprint, unsigned *imprint_len)
|
|
|
|
{
|
2015-06-11 02:07:40 +08:00
|
|
|
TS_MSG_IMPRINT *msg_imprint = tst_info->msg_imprint;
|
|
|
|
X509_ALGOR *md_alg_resp = msg_imprint->hash_algo;
|
2015-01-22 11:40:55 +08:00
|
|
|
const EVP_MD *md;
|
2015-11-27 21:02:12 +08:00
|
|
|
EVP_MD_CTX *md_ctx = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char buffer[4096];
|
|
|
|
int length;
|
|
|
|
|
|
|
|
*md_alg = NULL;
|
|
|
|
*imprint = NULL;
|
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((*md_alg = X509_ALGOR_dup(md_alg_resp)) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((md = EVP_get_digestbyobj((*md_alg)->algorithm)) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_TS_COMPUTE_IMPRINT, TS_R_UNSUPPORTED_MD_ALGORITHM);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
length = EVP_MD_size(md);
|
|
|
|
if (length < 0)
|
|
|
|
goto err;
|
|
|
|
*imprint_len = length;
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((*imprint = OPENSSL_malloc(*imprint_len)) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
md_ctx = EVP_MD_CTX_new();
|
2015-11-27 21:02:12 +08:00
|
|
|
if (md_ctx == NULL) {
|
|
|
|
TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!EVP_DigestInit(md_ctx, md))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) {
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestUpdate(md_ctx, buffer, length))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!EVP_DigestFinal(md_ctx, *imprint, NULL))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(md_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
return 1;
|
2006-02-13 07:11:56 +08:00
|
|
|
err:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(md_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR_free(*md_alg);
|
|
|
|
OPENSSL_free(*imprint);
|
|
|
|
*imprint_len = 0;
|
|
|
|
*imprint = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_imprints(X509_ALGOR *algor_a,
|
2016-08-01 01:38:09 +08:00
|
|
|
const unsigned char *imprint_a, unsigned len_a,
|
2015-01-22 11:40:55 +08:00
|
|
|
TS_TST_INFO *tst_info)
|
|
|
|
{
|
2015-06-11 02:07:40 +08:00
|
|
|
TS_MSG_IMPRINT *b = tst_info->msg_imprint;
|
|
|
|
X509_ALGOR *algor_b = b->hash_algo;
|
2015-01-22 11:40:55 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (algor_a) {
|
|
|
|
if (OBJ_cmp(algor_a->algorithm, algor_b->algorithm))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* The parameter must be NULL in both. */
|
|
|
|
if ((algor_a->parameter
|
|
|
|
&& ASN1_TYPE_get(algor_a->parameter) != V_ASN1_NULL)
|
|
|
|
|| (algor_b->parameter
|
|
|
|
&& ASN1_TYPE_get(algor_b->parameter) != V_ASN1_NULL))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = len_a == (unsigned)ASN1_STRING_length(b->hashed_msg) &&
|
2016-08-16 21:06:48 +08:00
|
|
|
memcmp(imprint_a, ASN1_STRING_get0_data(b->hashed_msg), len_a) == 0;
|
2006-02-13 07:11:56 +08:00
|
|
|
err:
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!ret)
|
|
|
|
TSerr(TS_F_TS_CHECK_IMPRINTS, TS_R_MESSAGE_IMPRINT_MISMATCH);
|
|
|
|
return ret;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-06-11 02:07:40 +08:00
|
|
|
const ASN1_INTEGER *b = tst_info->nonce;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if (!b) {
|
|
|
|
TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_NOT_RETURNED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No error if a nonce is returned without being requested. */
|
|
|
|
if (ASN1_INTEGER_cmp(a, b) != 0) {
|
|
|
|
TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_MISMATCH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the specified TSA name matches either the subject or one of the
|
|
|
|
* subject alternative names of the TSA certificate.
|
|
|
|
*/
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
STACK_OF(GENERAL_NAME) *gen_names = NULL;
|
|
|
|
int idx = -1;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
if (tsa_name->type == GEN_DIRNAME
|
2015-09-03 05:01:18 +08:00
|
|
|
&& X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
|
2015-05-07 01:43:59 +08:00
|
|
|
while (gen_names != NULL) {
|
2015-06-04 10:04:48 +08:00
|
|
|
found = ts_find_name(gen_names, tsa_name) >= 0;
|
2015-05-07 01:43:59 +08:00
|
|
|
if (found)
|
|
|
|
break;
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Get the next subject alternative name, although there should be no
|
|
|
|
* more than one.
|
|
|
|
*/
|
|
|
|
GENERAL_NAMES_free(gen_names);
|
2015-05-07 01:43:59 +08:00
|
|
|
gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2015-05-02 02:37:16 +08:00
|
|
|
GENERAL_NAMES_free(gen_names);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
2006-02-13 07:11:56 +08:00
|
|
|
|
|
|
|
/* Returns 1 if name is in gen_names, 0 otherwise. */
|
2015-06-04 10:04:48 +08:00
|
|
|
static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i, found;
|
|
|
|
for (i = 0, found = 0; !found && i < sk_GENERAL_NAME_num(gen_names); ++i) {
|
|
|
|
GENERAL_NAME *current = sk_GENERAL_NAME_value(gen_names, i);
|
|
|
|
found = GENERAL_NAME_cmp(current, name) == 0;
|
|
|
|
}
|
|
|
|
return found ? i - 1 : -1;
|
|
|
|
}
|