check in v3.8.0 source

This commit is contained in:
2023-08-31 00:49:24 -07:00
parent 3ef498f9e6
commit 316795abde
1218 changed files with 562506 additions and 0 deletions

1015
crypto/ts/ts_asn1.c Normal file

File diff suppressed because it is too large Load Diff

530
crypto/ts/ts_conf.c Normal file
View File

@@ -0,0 +1,530 @@
/* $OpenBSD: ts_conf.c,v 1.11 2018/04/14 07:18:37 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <string.h>
#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ts.h>
#ifndef OPENSSL_NO_ENGINE
#include <openssl/engine.h>
#endif
/* Macro definitions for the configuration file. */
#define BASE_SECTION "tsa"
#define ENV_DEFAULT_TSA "default_tsa"
#define ENV_SERIAL "serial"
#define ENV_CRYPTO_DEVICE "crypto_device"
#define ENV_SIGNER_CERT "signer_cert"
#define ENV_CERTS "certs"
#define ENV_SIGNER_KEY "signer_key"
#define ENV_DEFAULT_POLICY "default_policy"
#define ENV_OTHER_POLICIES "other_policies"
#define ENV_DIGESTS "digests"
#define ENV_ACCURACY "accuracy"
#define ENV_ORDERING "ordering"
#define ENV_TSA_NAME "tsa_name"
#define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
#define ENV_VALUE_SECS "secs"
#define ENV_VALUE_MILLISECS "millisecs"
#define ENV_VALUE_MICROSECS "microsecs"
#define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
#define ENV_VALUE_YES "yes"
#define ENV_VALUE_NO "no"
/* Function definitions for certificate and key loading. */
X509 *
TS_CONF_load_cert(const char *file)
{
BIO *cert = NULL;
X509 *x = NULL;
if ((cert = BIO_new_file(file, "r")) == NULL)
goto end;
x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
end:
if (x == NULL)
fprintf(stderr, "unable to load certificate: %s\n", file);
BIO_free(cert);
return x;
}
STACK_OF(X509) *
TS_CONF_load_certs(const char *file)
{
BIO *certs = NULL;
STACK_OF(X509) *othercerts = NULL;
STACK_OF(X509_INFO) *allcerts = NULL;
int i;
if (!(certs = BIO_new_file(file, "r")))
goto end;
if (!(othercerts = sk_X509_new_null()))
goto end;
allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
if (xi->x509) {
if (sk_X509_push(othercerts, xi->x509) == 0) {
sk_X509_pop_free(othercerts, X509_free);
othercerts = NULL;
goto end;
}
xi->x509 = NULL;
}
}
end:
if (othercerts == NULL)
fprintf(stderr, "unable to load certificates: %s\n", file);
sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
BIO_free(certs);
return othercerts;
}
EVP_PKEY *
TS_CONF_load_key(const char *file, const char *pass)
{
BIO *key = NULL;
EVP_PKEY *pkey = NULL;
if (!(key = BIO_new_file(file, "r")))
goto end;
pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *) pass);
end:
if (pkey == NULL)
fprintf(stderr, "unable to load private key: %s\n", file);
BIO_free(key);
return pkey;
}
/* Function definitions for handling configuration options. */
static void
TS_CONF_lookup_fail(const char *name, const char *tag)
{
fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
}
static void
TS_CONF_invalid(const char *name, const char *tag)
{
fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
}
const char *
TS_CONF_get_tsa_section(CONF *conf, const char *section)
{
if (!section) {
section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
if (!section)
TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
}
return section;
}
int
TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
TS_RESP_CTX *ctx)
{
int ret = 0;
char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
if (!serial) {
TS_CONF_lookup_fail(section, ENV_SERIAL);
goto err;
}
TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
ret = 1;
err:
return ret;
}
#ifndef OPENSSL_NO_ENGINE
int
TS_CONF_set_crypto_device(CONF *conf, const char *section, const char *device)
{
int ret = 0;
if (!device)
device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
if (device && !TS_CONF_set_default_engine(device)) {
TS_CONF_invalid(section, ENV_CRYPTO_DEVICE);
goto err;
}
ret = 1;
err:
return ret;
}
int
TS_CONF_set_default_engine(const char *name)
{
ENGINE *e = NULL;
int ret = 0;
/* Leave the default if builtin specified. */
if (strcmp(name, "builtin") == 0)
return 1;
if (!(e = ENGINE_by_id(name)))
goto err;
/* All the operations are going to be carried out by the engine. */
if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
goto err;
ret = 1;
err:
if (!ret) {
TSerror(TS_R_COULD_NOT_SET_ENGINE);
ERR_asprintf_error_data("engine:%s", name);
}
ENGINE_free(e);
return ret;
}
#endif
int
TS_CONF_set_signer_cert(CONF *conf, const char *section, const char *cert,
TS_RESP_CTX *ctx)
{
int ret = 0;
X509 *cert_obj = NULL;
if (!cert)
cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
if (!cert) {
TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
goto err;
}
if (!(cert_obj = TS_CONF_load_cert(cert)))
goto err;
if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
goto err;
ret = 1;
err:
X509_free(cert_obj);
return ret;
}
int
TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
TS_RESP_CTX *ctx)
{
int ret = 0;
STACK_OF(X509) *certs_obj = NULL;
if (!certs)
certs = NCONF_get_string(conf, section, ENV_CERTS);
/* Certificate chain is optional. */
if (!certs)
goto end;
if (!(certs_obj = TS_CONF_load_certs(certs)))
goto err;
if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
goto err;
end:
ret = 1;
err:
sk_X509_pop_free(certs_obj, X509_free);
return ret;
}
int
TS_CONF_set_signer_key(CONF *conf, const char *section, const char *key,
const char *pass, TS_RESP_CTX *ctx)
{
int ret = 0;
EVP_PKEY *key_obj = NULL;
if (!key)
key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
if (!key) {
TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
goto err;
}
if (!(key_obj = TS_CONF_load_key(key, pass)))
goto err;
if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
goto err;
ret = 1;
err:
EVP_PKEY_free(key_obj);
return ret;
}
int
TS_CONF_set_def_policy(CONF *conf, const char *section, const char *policy,
TS_RESP_CTX *ctx)
{
int ret = 0;
ASN1_OBJECT *policy_obj = NULL;
if (!policy)
policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
if (!policy) {
TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
goto err;
}
if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
goto err;
}
if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
goto err;
ret = 1;
err:
ASN1_OBJECT_free(policy_obj);
return ret;
}
int
TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
int ret = 0;
int i;
STACK_OF(CONF_VALUE) *list = NULL;
char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
/* If no other policy is specified, that's fine. */
if (policies && !(list = X509V3_parse_list(policies))) {
TS_CONF_invalid(section, ENV_OTHER_POLICIES);
goto err;
}
for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
const char *extval = val->value ? val->value : val->name;
ASN1_OBJECT *objtmp;
if (!(objtmp = OBJ_txt2obj(extval, 0))) {
TS_CONF_invalid(section, ENV_OTHER_POLICIES);
goto err;
}
if (!TS_RESP_CTX_add_policy(ctx, objtmp))
goto err;
ASN1_OBJECT_free(objtmp);
}
ret = 1;
err:
sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
return ret;
}
int
TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
int ret = 0;
int i;
STACK_OF(CONF_VALUE) *list = NULL;
char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
if (!digests) {
TS_CONF_lookup_fail(section, ENV_DIGESTS);
goto err;
}
if (!(list = X509V3_parse_list(digests))) {
TS_CONF_invalid(section, ENV_DIGESTS);
goto err;
}
if (sk_CONF_VALUE_num(list) == 0) {
TS_CONF_invalid(section, ENV_DIGESTS);
goto err;
}
for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
const char *extval = val->value ? val->value : val->name;
const EVP_MD *md;
if (!(md = EVP_get_digestbyname(extval))) {
TS_CONF_invalid(section, ENV_DIGESTS);
goto err;
}
if (!TS_RESP_CTX_add_md(ctx, md))
goto err;
}
ret = 1;
err:
sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
return ret;
}
int
TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
int ret = 0;
int i;
int secs = 0, millis = 0, micros = 0;
STACK_OF(CONF_VALUE) *list = NULL;
char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
if (accuracy && !(list = X509V3_parse_list(accuracy))) {
TS_CONF_invalid(section, ENV_ACCURACY);
goto err;
}
for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
if (val->value)
secs = atoi(val->value);
} else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
if (val->value)
millis = atoi(val->value);
} else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
if (val->value)
micros = atoi(val->value);
} else {
TS_CONF_invalid(section, ENV_ACCURACY);
goto err;
}
}
if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
goto err;
ret = 1;
err:
sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
return ret;
}
int
TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
TS_RESP_CTX *ctx)
{
int ret = 0;
long digits = 0;
/* If not specified, set the default value to 0, i.e. sec precision */
if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
&digits))
digits = 0;
if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
goto err;
}
if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
goto err;
return 1;
err:
return ret;
}
static int
TS_CONF_add_flag(CONF *conf, const char *section, const char *field, int flag,
TS_RESP_CTX *ctx)
{
/* Default is false. */
const char *value = NCONF_get_string(conf, section, field);
if (value) {
if (strcmp(value, ENV_VALUE_YES) == 0)
TS_RESP_CTX_add_flags(ctx, flag);
else if (strcmp(value, ENV_VALUE_NO) != 0) {
TS_CONF_invalid(section, field);
return 0;
}
}
return 1;
}
int
TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
}
int
TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
}
int
TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, TS_RESP_CTX *ctx)
{
return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
TS_ESS_CERT_ID_CHAIN, ctx);
}

123
crypto/ts/ts_err.c Normal file
View File

@@ -0,0 +1,123 @@
/* $OpenBSD: ts_err.c,v 1.6 2022/07/12 14:42:50 kn Exp $ */
/* ====================================================================
* Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/opensslconf.h>
#include <openssl/err.h>
#include <openssl/ts.h>
#ifndef OPENSSL_NO_ERR
#define ERR_FUNC(func) ERR_PACK(ERR_LIB_TS,func,0)
#define ERR_REASON(reason) ERR_PACK(ERR_LIB_TS,0,reason)
static ERR_STRING_DATA TS_str_functs[] = {
{ERR_FUNC(0xfff), "CRYPTO_internal"},
{0, NULL}
};
static ERR_STRING_DATA TS_str_reasons[]= {
{ERR_REASON(TS_R_BAD_PKCS7_TYPE) , "bad pkcs7 type"},
{ERR_REASON(TS_R_BAD_TYPE) , "bad type"},
{ERR_REASON(TS_R_CERTIFICATE_VERIFY_ERROR), "certificate verify error"},
{ERR_REASON(TS_R_COULD_NOT_SET_ENGINE) , "could not set engine"},
{ERR_REASON(TS_R_COULD_NOT_SET_TIME) , "could not set time"},
{ERR_REASON(TS_R_D2I_TS_RESP_INT_FAILED) , "d2i ts resp int failed"},
{ERR_REASON(TS_R_DETACHED_CONTENT) , "detached content"},
{ERR_REASON(TS_R_ESS_ADD_SIGNING_CERT_ERROR), "ess add signing cert error"},
{ERR_REASON(TS_R_ESS_SIGNING_CERTIFICATE_ERROR), "ess signing certificate error"},
{ERR_REASON(TS_R_INVALID_NULL_POINTER) , "invalid null pointer"},
{ERR_REASON(TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE), "invalid signer certificate purpose"},
{ERR_REASON(TS_R_MESSAGE_IMPRINT_MISMATCH), "message imprint mismatch"},
{ERR_REASON(TS_R_NONCE_MISMATCH) , "nonce mismatch"},
{ERR_REASON(TS_R_NONCE_NOT_RETURNED) , "nonce not returned"},
{ERR_REASON(TS_R_NO_CONTENT) , "no content"},
{ERR_REASON(TS_R_NO_TIME_STAMP_TOKEN) , "no time stamp token"},
{ERR_REASON(TS_R_PKCS7_ADD_SIGNATURE_ERROR), "pkcs7 add signature error"},
{ERR_REASON(TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR), "pkcs7 add signed attr error"},
{ERR_REASON(TS_R_PKCS7_TO_TS_TST_INFO_FAILED), "pkcs7 to ts tst info failed"},
{ERR_REASON(TS_R_POLICY_MISMATCH) , "policy mismatch"},
{ERR_REASON(TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE), "private key does not match certificate"},
{ERR_REASON(TS_R_RESPONSE_SETUP_ERROR) , "response setup error"},
{ERR_REASON(TS_R_SIGNATURE_FAILURE) , "signature failure"},
{ERR_REASON(TS_R_THERE_MUST_BE_ONE_SIGNER), "there must be one signer"},
{ERR_REASON(TS_R_TIME_SYSCALL_ERROR) , "time syscall error"},
{ERR_REASON(TS_R_TOKEN_NOT_PRESENT) , "token not present"},
{ERR_REASON(TS_R_TOKEN_PRESENT) , "token present"},
{ERR_REASON(TS_R_TSA_NAME_MISMATCH) , "tsa name mismatch"},
{ERR_REASON(TS_R_TSA_UNTRUSTED) , "tsa untrusted"},
{ERR_REASON(TS_R_TST_INFO_SETUP_ERROR) , "tst info setup error"},
{ERR_REASON(TS_R_TS_DATASIGN) , "ts datasign"},
{ERR_REASON(TS_R_UNACCEPTABLE_POLICY) , "unacceptable policy"},
{ERR_REASON(TS_R_UNSUPPORTED_MD_ALGORITHM), "unsupported md algorithm"},
{ERR_REASON(TS_R_UNSUPPORTED_VERSION) , "unsupported version"},
{ERR_REASON(TS_R_WRONG_CONTENT_TYPE) , "wrong content type"},
{0, NULL}
};
#endif
void
ERR_load_TS_strings(void)
{
#ifndef OPENSSL_NO_ERR
if (ERR_func_error_string(TS_str_functs[0].error) == NULL) {
ERR_load_strings(0, TS_str_functs);
ERR_load_strings(0, TS_str_reasons);
}
#endif
}

153
crypto/ts/ts_lib.c Normal file
View File

@@ -0,0 +1,153 @@
/* $OpenBSD: ts_lib.c,v 1.13 2022/11/26 16:08:54 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/objects.h>
#include <openssl/ts.h>
#include <openssl/x509v3.h>
#include "bn_local.h"
#include "x509_local.h"
/* Local function declarations. */
/* Function definitions. */
int
TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
{
BIGNUM num_bn;
int result = 0;
char *hex;
BN_init(&num_bn);
ASN1_INTEGER_to_BN(num, &num_bn);
if ((hex = BN_bn2hex(&num_bn))) {
result = BIO_write(bio, "0x", 2) > 0;
result = result && BIO_write(bio, hex, strlen(hex)) > 0;
free(hex);
}
BN_free(&num_bn);
return result;
}
int
TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
{
char obj_txt[128];
int len = OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
if (len >= sizeof(obj_txt))
len = sizeof(obj_txt) - 1;
BIO_write(bio, obj_txt, len);
BIO_write(bio, "\n", 1);
return 1;
}
int
TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
{
int i, critical, n;
X509_EXTENSION *ex;
ASN1_OBJECT *obj;
BIO_printf(bio, "Extensions:\n");
n = X509v3_get_ext_count(extensions);
for (i = 0; i < n; i++) {
ex = X509v3_get_ext(extensions, i);
obj = X509_EXTENSION_get_object(ex);
i2a_ASN1_OBJECT(bio, obj);
critical = X509_EXTENSION_get_critical(ex);
BIO_printf(bio, ": %s\n", critical ? "critical" : "");
if (!X509V3_EXT_print(bio, ex, 0, 4)) {
BIO_printf(bio, "%4s", "");
ASN1_STRING_print(bio, ex->value);
}
BIO_write(bio, "\n", 1);
}
return 1;
}
int
TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
{
int i = OBJ_obj2nid(alg->algorithm);
return BIO_printf(bio, "Hash Algorithm: %s\n",
(i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
}
int
TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
{
ASN1_OCTET_STRING *msg;
TS_X509_ALGOR_print_bio(bio, TS_MSG_IMPRINT_get_algo(a));
BIO_printf(bio, "Message data:\n");
msg = TS_MSG_IMPRINT_get_msg(a);
BIO_dump_indent(bio, (const char *)ASN1_STRING_data(msg),
ASN1_STRING_length(msg), 4);
return 1;
}

316
crypto/ts/ts_local.h Normal file
View File

@@ -0,0 +1,316 @@
/* $OpenBSD: ts_local.h,v 1.3 2022/11/26 17:23:18 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@opentsa.org) for the OpenSSL
* project 2002, 2003, 2004.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#ifndef HEADER_TS_LOCAL_H
#define HEADER_TS_LOCAL_H
__BEGIN_HIDDEN_DECLS
/*
* MessageImprint ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier,
* hashedMessage OCTET STRING }
*/
struct TS_msg_imprint_st {
X509_ALGOR *hash_algo;
ASN1_OCTET_STRING *hashed_msg;
};
/*
* TimeStampReq ::= SEQUENCE {
* version INTEGER { v1(1) },
* messageImprint MessageImprint,
* --a hash algorithm OID and the hash value of the data to be
* --time-stamped
* reqPolicy TSAPolicyId OPTIONAL,
* nonce INTEGER OPTIONAL,
* certReq BOOLEAN DEFAULT FALSE,
* extensions [0] IMPLICIT Extensions OPTIONAL }
*/
struct TS_req_st {
ASN1_INTEGER *version;
TS_MSG_IMPRINT *msg_imprint;
ASN1_OBJECT *policy_id; /* OPTIONAL */
ASN1_INTEGER *nonce; /* OPTIONAL */
ASN1_BOOLEAN cert_req; /* DEFAULT FALSE */
STACK_OF(X509_EXTENSION) *extensions; /* [0] OPTIONAL */
};
/*
* Accuracy ::= SEQUENCE {
* seconds INTEGER OPTIONAL,
* millis [0] INTEGER (1..999) OPTIONAL,
* micros [1] INTEGER (1..999) OPTIONAL }
*/
struct TS_accuracy_st {
ASN1_INTEGER *seconds;
ASN1_INTEGER *millis;
ASN1_INTEGER *micros;
};
/*
* TSTInfo ::= SEQUENCE {
* version INTEGER { v1(1) },
* policy TSAPolicyId,
* messageImprint MessageImprint,
* -- MUST have the same value as the similar field in
* -- TimeStampReq
* serialNumber INTEGER,
* -- Time-Stamping users MUST be ready to accommodate integers
* -- up to 160 bits.
* genTime GeneralizedTime,
* accuracy Accuracy OPTIONAL,
* ordering BOOLEAN DEFAULT FALSE,
* nonce INTEGER OPTIONAL,
* -- MUST be present if the similar field was present
* -- in TimeStampReq. In that case it MUST have the same value.
* tsa [0] GeneralName OPTIONAL,
* extensions [1] IMPLICIT Extensions OPTIONAL }
*/
struct TS_tst_info_st {
ASN1_INTEGER *version;
ASN1_OBJECT *policy_id;
TS_MSG_IMPRINT *msg_imprint;
ASN1_INTEGER *serial;
ASN1_GENERALIZEDTIME *time;
TS_ACCURACY *accuracy;
ASN1_BOOLEAN ordering;
ASN1_INTEGER *nonce;
GENERAL_NAME *tsa;
STACK_OF(X509_EXTENSION) *extensions;
};
/*
* PKIStatusInfo ::= SEQUENCE {
* status PKIStatus,
* statusString PKIFreeText OPTIONAL,
* failInfo PKIFailureInfo OPTIONAL }
*
* From RFC 1510 - section 3.1.1:
* PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
* -- text encoded as UTF-8 String (note: each UTF8String SHOULD
* -- include an RFC 1766 language tag to indicate the language
* -- of the contained text)
*/
struct TS_status_info_st {
ASN1_INTEGER *status;
STACK_OF(ASN1_UTF8STRING) *text;
ASN1_BIT_STRING *failure_info;
};
/*
* TimeStampResp ::= SEQUENCE {
* status PKIStatusInfo,
* timeStampToken TimeStampToken OPTIONAL }
*/
struct TS_resp_st {
TS_STATUS_INFO *status_info;
PKCS7 *token;
TS_TST_INFO *tst_info;
};
/* The structure below would belong to the ESS component. */
/*
* IssuerSerial ::= SEQUENCE {
* issuer GeneralNames,
* serialNumber CertificateSerialNumber
* }
*/
struct ESS_issuer_serial {
STACK_OF(GENERAL_NAME) *issuer;
ASN1_INTEGER *serial;
};
/*
* ESSCertID ::= SEQUENCE {
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL
* }
*/
struct ESS_cert_id {
ASN1_OCTET_STRING *hash; /* Always SHA-1 digest. */
ESS_ISSUER_SERIAL *issuer_serial;
};
/*
* SigningCertificate ::= SEQUENCE {
* certs SEQUENCE OF ESSCertID,
* policies SEQUENCE OF PolicyInformation OPTIONAL
* }
*/
struct ESS_signing_cert {
STACK_OF(ESS_CERT_ID) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};
/*
* ESSCertIDv2 ::= SEQUENCE {
* hashAlgorithm AlgorithmIdentifier
* DEFAULT {algorithm id-sha256},
* certHash Hash,
* issuerSerial IssuerSerial OPTIONAL }
*/
struct ESS_cert_id_v2 {
X509_ALGOR *hash_alg; /* Default SHA-256. */
ASN1_OCTET_STRING *hash;
ESS_ISSUER_SERIAL *issuer_serial;
};
/*
* SigningCertificateV2 ::= SEQUENCE {
* certs SEQUENCE OF ESSCertIDv2,
* policies SEQUENCE OF PolicyInformation OPTIONAL }
*/
struct ESS_signing_cert_v2 {
STACK_OF(ESS_CERT_ID_V2) *cert_ids;
STACK_OF(POLICYINFO) *policy_info;
};
struct TS_resp_ctx {
X509 *signer_cert;
EVP_PKEY *signer_key;
STACK_OF(X509) *certs; /* Certs to include in signed data. */
STACK_OF(ASN1_OBJECT) *policies; /* Acceptable policies. */
ASN1_OBJECT *default_policy; /* It may appear in policies, too. */
STACK_OF(EVP_MD) *mds; /* Acceptable message digests. */
ASN1_INTEGER *seconds; /* accuracy, 0 means not specified. */
ASN1_INTEGER *millis; /* accuracy, 0 means not specified. */
ASN1_INTEGER *micros; /* accuracy, 0 means not specified. */
unsigned clock_precision_digits; /* fraction of seconds in
time stamp token. */
unsigned flags; /* Optional info, see values above. */
/* Callback functions. */
TS_serial_cb serial_cb;
void *serial_cb_data; /* User data for serial_cb. */
TS_time_cb time_cb;
void *time_cb_data; /* User data for time_cb. */
TS_extension_cb extension_cb;
void *extension_cb_data; /* User data for extension_cb. */
/* These members are used only while creating the response. */
TS_REQ *request;
TS_RESP *response;
TS_TST_INFO *tst_info;
};
/* Context structure for the generic verify method. */
struct TS_verify_ctx {
/* Set this to the union of TS_VFY_... flags you want to carry out. */
unsigned flags;
/* Must be set only with TS_VFY_SIGNATURE. certs is optional. */
X509_STORE *store;
STACK_OF(X509) *certs;
/* Must be set only with TS_VFY_POLICY. */
ASN1_OBJECT *policy;
/* Must be set only with TS_VFY_IMPRINT. If md_alg is NULL,
the algorithm from the response is used. */
X509_ALGOR *md_alg;
unsigned char *imprint;
unsigned imprint_len;
/* Must be set only with TS_VFY_DATA. */
BIO *data;
/* Must be set only with TS_VFY_TSA_NAME. */
ASN1_INTEGER *nonce;
/* Must be set only with TS_VFY_TSA_NAME. */
GENERAL_NAME *tsa_name;
};
/*
* Public OpenSSL API that we do not currently want to expose.
*/
ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new(void);
void ESS_CERT_ID_V2_free(ESS_CERT_ID_V2 *a);
int i2d_ESS_CERT_ID_V2(const ESS_CERT_ID_V2 *a, unsigned char **pp);
ESS_CERT_ID_V2 *d2i_ESS_CERT_ID_V2(ESS_CERT_ID_V2 **a, const unsigned char **pp,
long length);
ESS_CERT_ID_V2 *ESS_CERT_ID_V2_dup(ESS_CERT_ID_V2 *a);
ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_new(void);
void ESS_SIGNING_CERT_V2_free(ESS_SIGNING_CERT_V2 *a);
int i2d_ESS_SIGNING_CERT_V2(const ESS_SIGNING_CERT_V2 *a,
unsigned char **pp);
ESS_SIGNING_CERT_V2 *d2i_ESS_SIGNING_CERT_V2(ESS_SIGNING_CERT_V2 **a,
const unsigned char **pp, long length);
ESS_SIGNING_CERT_V2 *ESS_SIGNING_CERT_V2_dup(ESS_SIGNING_CERT_V2 *a);
__END_HIDDEN_DECLS
#endif /* !HEADER_TS_LOCAL_H */

104
crypto/ts/ts_req_print.c Normal file
View File

@@ -0,0 +1,104 @@
/* $OpenBSD: ts_req_print.c,v 1.4 2014/07/11 08:44:49 jsing Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/bn.h>
#include <openssl/objects.h>
#include <openssl/ts.h>
#include <openssl/x509v3.h>
/* Function definitions. */
int
TS_REQ_print_bio(BIO *bio, TS_REQ *a)
{
int v;
ASN1_OBJECT *policy_id;
const ASN1_INTEGER *nonce;
if (a == NULL)
return 0;
v = TS_REQ_get_version(a);
BIO_printf(bio, "Version: %d\n", v);
TS_MSG_IMPRINT_print_bio(bio, TS_REQ_get_msg_imprint(a));
BIO_printf(bio, "Policy OID: ");
policy_id = TS_REQ_get_policy_id(a);
if (policy_id == NULL)
BIO_printf(bio, "unspecified\n");
else
TS_OBJ_print_bio(bio, policy_id);
BIO_printf(bio, "Nonce: ");
nonce = TS_REQ_get_nonce(a);
if (nonce == NULL)
BIO_printf(bio, "unspecified");
else
TS_ASN1_INTEGER_print_bio(bio, nonce);
BIO_write(bio, "\n", 1);
BIO_printf(bio, "Certificate required: %s\n",
TS_REQ_get_cert_req(a) ? "yes" : "no");
TS_ext_print_bio(bio, TS_REQ_get_exts(a));
return 1;
}

257
crypto/ts/ts_req_utils.c Normal file
View File

@@ -0,0 +1,257 @@
/* $OpenBSD: ts_req_utils.c,v 1.7 2022/07/24 08:16:47 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/ts.h>
#include <openssl/x509v3.h>
#include "ts_local.h"
int
TS_REQ_set_version(TS_REQ *a, long version)
{
return ASN1_INTEGER_set(a->version, version);
}
long
TS_REQ_get_version(const TS_REQ *a)
{
return ASN1_INTEGER_get(a->version);
}
int
TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint)
{
TS_MSG_IMPRINT *new_msg_imprint;
if (a->msg_imprint == msg_imprint)
return 1;
new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
if (new_msg_imprint == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
TS_MSG_IMPRINT_free(a->msg_imprint);
a->msg_imprint = new_msg_imprint;
return 1;
}
TS_MSG_IMPRINT *
TS_REQ_get_msg_imprint(TS_REQ *a)
{
return a->msg_imprint;
}
int
TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg)
{
X509_ALGOR *new_alg;
if (a->hash_algo == alg)
return 1;
new_alg = X509_ALGOR_dup(alg);
if (new_alg == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
X509_ALGOR_free(a->hash_algo);
a->hash_algo = new_alg;
return 1;
}
X509_ALGOR *
TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a)
{
return a->hash_algo;
}
int
TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len)
{
return ASN1_OCTET_STRING_set(a->hashed_msg, d, len);
}
ASN1_OCTET_STRING *
TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a)
{
return a->hashed_msg;
}
int
TS_REQ_set_policy_id(TS_REQ *a, const ASN1_OBJECT *policy)
{
ASN1_OBJECT *new_policy;
if (a->policy_id == policy)
return 1;
new_policy = OBJ_dup(policy);
if (new_policy == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_OBJECT_free(a->policy_id);
a->policy_id = new_policy;
return 1;
}
ASN1_OBJECT *
TS_REQ_get_policy_id(TS_REQ *a)
{
return a->policy_id;
}
int
TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce)
{
ASN1_INTEGER *new_nonce;
if (a->nonce == nonce)
return 1;
new_nonce = ASN1_INTEGER_dup(nonce);
if (new_nonce == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_INTEGER_free(a->nonce);
a->nonce = new_nonce;
return 1;
}
const ASN1_INTEGER *
TS_REQ_get_nonce(const TS_REQ *a)
{
return a->nonce;
}
int
TS_REQ_set_cert_req(TS_REQ *a, int cert_req)
{
a->cert_req = cert_req ? 0xFF : 0x00;
return 1;
}
int
TS_REQ_get_cert_req(const TS_REQ *a)
{
return a->cert_req ? 1 : 0;
}
STACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a)
{
return a->extensions;
}
void
TS_REQ_ext_free(TS_REQ *a)
{
if (!a)
return;
sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
a->extensions = NULL;
}
int
TS_REQ_get_ext_count(TS_REQ *a)
{
return X509v3_get_ext_count(a->extensions);
}
int
TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos)
{
return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
}
int
TS_REQ_get_ext_by_OBJ(TS_REQ *a, const ASN1_OBJECT *obj, int lastpos)
{
return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
}
int
TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos)
{
return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
}
X509_EXTENSION *
TS_REQ_get_ext(TS_REQ *a, int loc)
{
return X509v3_get_ext(a->extensions, loc);
}
X509_EXTENSION *
TS_REQ_delete_ext(TS_REQ *a, int loc)
{
return X509v3_delete_ext(a->extensions, loc);
}
int
TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc)
{
return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
}
void *
TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx)
{
return X509V3_get_d2i(a->extensions, nid, crit, idx);
}

303
crypto/ts/ts_rsp_print.c Normal file
View File

@@ -0,0 +1,303 @@
/* $OpenBSD: ts_rsp_print.c,v 1.6 2022/07/24 08:16:47 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/bn.h>
#include <openssl/objects.h>
#include <openssl/ts.h>
#include <openssl/x509v3.h>
#include "ts_local.h"
struct status_map_st {
int bit;
const char *text;
};
/* Local function declarations. */
static int TS_status_map_print(BIO *bio, struct status_map_st *a,
ASN1_BIT_STRING *v);
static int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
/* Function definitions. */
int
TS_RESP_print_bio(BIO *bio, TS_RESP *a)
{
TS_TST_INFO *tst_info;
BIO_printf(bio, "Status info:\n");
TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
BIO_printf(bio, "\nTST info:\n");
tst_info = TS_RESP_get_tst_info(a);
if (tst_info != NULL)
TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
else
BIO_printf(bio, "Not included.\n");
return 1;
}
int
TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
{
static const char *status_map[] = {
"Granted.",
"Granted with modifications.",
"Rejected.",
"Waiting.",
"Revocation warning.",
"Revoked."
};
static struct status_map_st failure_map[] = {
{
TS_INFO_BAD_ALG,
"unrecognized or unsupported algorithm identifier"
},
{
TS_INFO_BAD_REQUEST,
"transaction not permitted or supported"
},
{
TS_INFO_BAD_DATA_FORMAT,
"the data submitted has the wrong format"
},
{
TS_INFO_TIME_NOT_AVAILABLE,
"the TSA's time source is not available"
},
{
TS_INFO_UNACCEPTED_POLICY,
"the requested TSA policy is not supported by the TSA"
},
{
TS_INFO_UNACCEPTED_EXTENSION,
"the requested extension is not supported by the TSA"
},
{
TS_INFO_ADD_INFO_NOT_AVAILABLE,
"the additional information requested could not be understood "
"or is not available"
},
{
TS_INFO_SYSTEM_FAILURE,
"the request cannot be handled due to system failure"
},
{ -1, NULL }
};
long status;
int i, lines = 0;
/* Printing status code. */
BIO_printf(bio, "Status: ");
status = ASN1_INTEGER_get(a->status);
if (0 <= status &&
status < (long)(sizeof(status_map) / sizeof(status_map[0])))
BIO_printf(bio, "%s\n", status_map[status]);
else
BIO_printf(bio, "out of bounds\n");
/* Printing status description. */
BIO_printf(bio, "Status description: ");
for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
if (i > 0)
BIO_puts(bio, "\t");
ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i),
0);
BIO_puts(bio, "\n");
}
if (i == 0)
BIO_printf(bio, "unspecified\n");
/* Printing failure information. */
BIO_printf(bio, "Failure info: ");
if (a->failure_info != NULL)
lines = TS_status_map_print(bio, failure_map, a->failure_info);
if (lines == 0)
BIO_printf(bio, "unspecified");
BIO_printf(bio, "\n");
return 1;
}
static int
TS_status_map_print(BIO *bio, struct status_map_st *a, ASN1_BIT_STRING *v)
{
int lines = 0;
for (; a->bit >= 0; ++a) {
if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
if (++lines > 1)
BIO_printf(bio, ", ");
BIO_printf(bio, "%s", a->text);
}
}
return lines;
}
int
TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
{
int v;
ASN1_OBJECT *policy_id;
const ASN1_INTEGER *serial;
const ASN1_GENERALIZEDTIME *gtime;
TS_ACCURACY *accuracy;
const ASN1_INTEGER *nonce;
GENERAL_NAME *tsa_name;
if (a == NULL)
return 0;
/* Print version. */
v = TS_TST_INFO_get_version(a);
BIO_printf(bio, "Version: %d\n", v);
/* Print policy id. */
BIO_printf(bio, "Policy OID: ");
policy_id = TS_TST_INFO_get_policy_id(a);
TS_OBJ_print_bio(bio, policy_id);
/* Print message imprint. */
TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
/* Print serial number. */
BIO_printf(bio, "Serial number: ");
serial = TS_TST_INFO_get_serial(a);
if (serial == NULL)
BIO_printf(bio, "unspecified");
else
TS_ASN1_INTEGER_print_bio(bio, serial);
BIO_write(bio, "\n", 1);
/* Print time stamp. */
BIO_printf(bio, "Time stamp: ");
gtime = TS_TST_INFO_get_time(a);
ASN1_GENERALIZEDTIME_print(bio, gtime);
BIO_write(bio, "\n", 1);
/* Print accuracy. */
BIO_printf(bio, "Accuracy: ");
accuracy = TS_TST_INFO_get_accuracy(a);
if (accuracy == NULL)
BIO_printf(bio, "unspecified");
else
TS_ACCURACY_print_bio(bio, accuracy);
BIO_write(bio, "\n", 1);
/* Print ordering. */
BIO_printf(bio, "Ordering: %s\n",
TS_TST_INFO_get_ordering(a) ? "yes" : "no");
/* Print nonce. */
BIO_printf(bio, "Nonce: ");
nonce = TS_TST_INFO_get_nonce(a);
if (nonce == NULL)
BIO_printf(bio, "unspecified");
else
TS_ASN1_INTEGER_print_bio(bio, nonce);
BIO_write(bio, "\n", 1);
/* Print TSA name. */
BIO_printf(bio, "TSA: ");
tsa_name = TS_TST_INFO_get_tsa(a);
if (tsa_name == NULL)
BIO_printf(bio, "unspecified");
else {
STACK_OF(CONF_VALUE) *nval;
if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
X509V3_EXT_val_prn(bio, nval, 0, 0);
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
}
BIO_write(bio, "\n", 1);
/* Print extensions. */
TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
return 1;
}
static int
TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
{
const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
if (seconds != NULL)
TS_ASN1_INTEGER_print_bio(bio, seconds);
else
BIO_printf(bio, "unspecified");
BIO_printf(bio, " seconds, ");
if (millis != NULL)
TS_ASN1_INTEGER_print_bio(bio, millis);
else
BIO_printf(bio, "unspecified");
BIO_printf(bio, " millis, ");
if (micros != NULL)
TS_ASN1_INTEGER_print_bio(bio, micros);
else
BIO_printf(bio, "unspecified");
BIO_printf(bio, " micros");
return 1;
}

1035
crypto/ts/ts_rsp_sign.c Normal file

File diff suppressed because it is too large Load Diff

460
crypto/ts/ts_rsp_utils.c Normal file
View File

@@ -0,0 +1,460 @@
/* $OpenBSD: ts_rsp_utils.c,v 1.9 2022/07/24 19:25:36 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/pkcs7.h>
#include <openssl/ts.h>
#include "ts_local.h"
/* Function definitions. */
int
TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
{
TS_STATUS_INFO *new_status_info;
if (a->status_info == status_info)
return 1;
new_status_info = TS_STATUS_INFO_dup(status_info);
if (new_status_info == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
TS_STATUS_INFO_free(a->status_info);
a->status_info = new_status_info;
return 1;
}
TS_STATUS_INFO *
TS_RESP_get_status_info(TS_RESP *a)
{
return a->status_info;
}
const ASN1_UTF8STRING *
TS_STATUS_INFO_get0_failure_info(const TS_STATUS_INFO *si)
{
return si->failure_info;
}
const STACK_OF(ASN1_UTF8STRING) *
TS_STATUS_INFO_get0_text(const TS_STATUS_INFO *si)
{
return si->text;
}
const ASN1_INTEGER *
TS_STATUS_INFO_get0_status(const TS_STATUS_INFO *si)
{
return si->status;
}
int
TS_STATUS_INFO_set_status(TS_STATUS_INFO *si, int i)
{
return ASN1_INTEGER_set(si->status, i);
}
/* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
void
TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
{
/* Set new PKCS7 and TST_INFO objects. */
PKCS7_free(a->token);
a->token = p7;
TS_TST_INFO_free(a->tst_info);
a->tst_info = tst_info;
}
PKCS7 *
TS_RESP_get_token(TS_RESP *a)
{
return a->token;
}
TS_TST_INFO *
TS_RESP_get_tst_info(TS_RESP *a)
{
return a->tst_info;
}
int
TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
{
return ASN1_INTEGER_set(a->version, version);
}
long
TS_TST_INFO_get_version(const TS_TST_INFO *a)
{
return ASN1_INTEGER_get(a->version);
}
int
TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
{
ASN1_OBJECT *new_policy;
if (a->policy_id == policy)
return 1;
new_policy = OBJ_dup(policy);
if (new_policy == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_OBJECT_free(a->policy_id);
a->policy_id = new_policy;
return 1;
}
ASN1_OBJECT *
TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
{
return a->policy_id;
}
int
TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
{
TS_MSG_IMPRINT *new_msg_imprint;
if (a->msg_imprint == msg_imprint)
return 1;
new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
if (new_msg_imprint == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
TS_MSG_IMPRINT_free(a->msg_imprint);
a->msg_imprint = new_msg_imprint;
return 1;
}
TS_MSG_IMPRINT *
TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
{
return a->msg_imprint;
}
int
TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
{
ASN1_INTEGER *new_serial;
if (a->serial == serial)
return 1;
new_serial = ASN1_INTEGER_dup(serial);
if (new_serial == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_INTEGER_free(a->serial);
a->serial = new_serial;
return 1;
}
const ASN1_INTEGER *
TS_TST_INFO_get_serial(const TS_TST_INFO *a)
{
return a->serial;
}
int
TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
{
ASN1_GENERALIZEDTIME *new_time;
if (a->time == gtime)
return 1;
new_time = ASN1_STRING_dup(gtime);
if (new_time == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_GENERALIZEDTIME_free(a->time);
a->time = new_time;
return 1;
}
const ASN1_GENERALIZEDTIME *
TS_TST_INFO_get_time(const TS_TST_INFO *a)
{
return a->time;
}
int
TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
{
TS_ACCURACY *new_accuracy;
if (a->accuracy == accuracy)
return 1;
new_accuracy = TS_ACCURACY_dup(accuracy);
if (new_accuracy == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
TS_ACCURACY_free(a->accuracy);
a->accuracy = new_accuracy;
return 1;
}
TS_ACCURACY *
TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
{
return a->accuracy;
}
int
TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
{
ASN1_INTEGER *new_seconds;
if (a->seconds == seconds)
return 1;
new_seconds = ASN1_INTEGER_dup(seconds);
if (new_seconds == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_INTEGER_free(a->seconds);
a->seconds = new_seconds;
return 1;
}
const ASN1_INTEGER *
TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
{
return a->seconds;
}
int
TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
{
ASN1_INTEGER *new_millis = NULL;
if (a->millis == millis)
return 1;
if (millis != NULL) {
new_millis = ASN1_INTEGER_dup(millis);
if (new_millis == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
}
ASN1_INTEGER_free(a->millis);
a->millis = new_millis;
return 1;
}
const ASN1_INTEGER *
TS_ACCURACY_get_millis(const TS_ACCURACY *a)
{
return a->millis;
}
int
TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
{
ASN1_INTEGER *new_micros = NULL;
if (a->micros == micros)
return 1;
if (micros != NULL) {
new_micros = ASN1_INTEGER_dup(micros);
if (new_micros == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
}
ASN1_INTEGER_free(a->micros);
a->micros = new_micros;
return 1;
}
const ASN1_INTEGER *
TS_ACCURACY_get_micros(const TS_ACCURACY *a)
{
return a->micros;
}
int
TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
{
a->ordering = ordering ? 0xFF : 0x00;
return 1;
}
int
TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
{
return a->ordering ? 1 : 0;
}
int
TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
{
ASN1_INTEGER *new_nonce;
if (a->nonce == nonce)
return 1;
new_nonce = ASN1_INTEGER_dup(nonce);
if (new_nonce == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
ASN1_INTEGER_free(a->nonce);
a->nonce = new_nonce;
return 1;
}
const ASN1_INTEGER *
TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
{
return a->nonce;
}
int
TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
{
GENERAL_NAME *new_tsa;
if (a->tsa == tsa)
return 1;
new_tsa = GENERAL_NAME_dup(tsa);
if (new_tsa == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
return 0;
}
GENERAL_NAME_free(a->tsa);
a->tsa = new_tsa;
return 1;
}
GENERAL_NAME *
TS_TST_INFO_get_tsa(TS_TST_INFO *a)
{
return a->tsa;
}
STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
{
return a->extensions;
}
void
TS_TST_INFO_ext_free(TS_TST_INFO *a)
{
if (!a)
return;
sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
a->extensions = NULL;
}
int
TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
{
return X509v3_get_ext_count(a->extensions);
}
int
TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
{
return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
}
int
TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, const ASN1_OBJECT *obj, int lastpos)
{
return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
}
int
TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
{
return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
}
X509_EXTENSION *
TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
{
return X509v3_get_ext(a->extensions, loc);
}
X509_EXTENSION *
TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
{
return X509v3_delete_ext(a->extensions, loc);
}
int
TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
{
return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
}
void *
TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
{
return X509V3_get_d2i(a->extensions, nid, crit, idx);
}

844
crypto/ts/ts_rsp_verify.c Normal file
View File

@@ -0,0 +1,844 @@
/* $OpenBSD: ts_rsp_verify.c,v 1.29 2022/11/26 16:08:54 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2002.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <string.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/pkcs7.h>
#include <openssl/ts.h>
#include "evp_local.h"
#include "ts_local.h"
#include "x509_local.h"
/* Private function declarations. */
static int TS_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
X509 *signer, STACK_OF(X509) **chain);
static int TS_check_signing_certs(PKCS7_SIGNER_INFO *si, STACK_OF(X509) *chain);
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);
static ESS_SIGNING_CERT_V2 *ESS_get_signing_cert_v2(PKCS7_SIGNER_INFO *si);
static int TS_find_cert_v2(STACK_OF(ESS_CERT_ID_V2) *cert_ids, X509 *cert);
static int TS_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert);
static int int_TS_RESP_verify_token(TS_VERIFY_CTX *ctx,
PKCS7 *token, TS_TST_INFO *tst_info);
static int TS_check_status_info(TS_RESP *response);
static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text);
static int TS_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info);
static int TS_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
X509_ALGOR **md_alg,
unsigned char **imprint, unsigned *imprint_len);
static int TS_check_imprints(X509_ALGOR *algor_a,
unsigned char *imprint_a, unsigned len_a,
TS_TST_INFO *tst_info);
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, GENERAL_NAME *name);
/*
* Local mapping between response codes and descriptions.
* Don't forget to change TS_STATUS_BUF_SIZE when modifying
* the elements of this array.
*/
static const char *TS_status_text[] = {
"granted",
"grantedWithMods",
"rejection",
"waiting",
"revocationWarning",
"revocationNotification"
};
#define TS_STATUS_TEXT_SIZE (sizeof(TS_status_text)/sizeof(*TS_status_text))
/*
* This must be greater or equal to the sum of the strings in TS_status_text
* plus the number of its elements.
*/
#define TS_STATUS_BUF_SIZE 256
static struct {
int code;
const char *text;
} 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" }
};
#define TS_FAILURE_INFO_SIZE (sizeof(TS_failure_info) / \
sizeof(*TS_failure_info))
/* Functions for verifying a signed TS_TST_INFO structure. */
/*
* This function carries out the following tasks:
* - 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.
*/
int
TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
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) {
TSerror(TS_R_INVALID_NULL_POINTER);
goto err;
}
/* Check for the correct content type */
if (!PKCS7_type_is_signed(token)) {
TSerror(TS_R_WRONG_CONTENT_TYPE);
goto err;
}
/* Check if there is one and only one signer. */
sinfos = PKCS7_get_signer_info(token);
if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) {
TSerror(TS_R_THERE_MUST_BE_ONE_SIGNER);
goto err;
}
si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0);
/* Check for no content: no data to verify signature. */
if (PKCS7_get_detached(token)) {
TSerror(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);
/* Now verify the certificate. */
if (!TS_verify_cert(store, certs, signer, &chain))
goto err;
/* Check if the signer certificate is consistent with the
ESS extension. */
if (!TS_check_signing_certs(si, chain))
goto err;
/* Creating the message digest. */
p7bio = PKCS7_dataInit(token, NULL);
/* We now have to 'read' from p7bio to calculate digests etc. */
while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0)
;
/* Verifying the signature. */
j = PKCS7_signatureVerify(p7bio, token, si, signer);
if (j <= 0) {
TSerror(TS_R_SIGNATURE_FAILURE);
goto err;
}
/* Return the signer certificate if needed. */
if (signer_out) {
*signer_out = signer;
CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
}
ret = 1;
err:
BIO_free_all(p7bio);
sk_X509_pop_free(chain, X509_free);
sk_X509_free(signers);
return ret;
}
/*
* The certificate chain is returned in chain. Caller is responsible for
* freeing the vector.
*/
static int
TS_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted, X509 *signer,
STACK_OF(X509) **chain)
{
X509_STORE_CTX cert_ctx;
int i;
int ret = 0;
/* chain is an out argument. */
*chain = NULL;
if (X509_STORE_CTX_init(&cert_ctx, store, signer, untrusted) == 0) {
TSerror(ERR_R_X509_LIB);
goto err;
}
if (X509_STORE_CTX_set_purpose(&cert_ctx,
X509_PURPOSE_TIMESTAMP_SIGN) == 0)
goto err;
i = X509_verify_cert(&cert_ctx);
if (i <= 0) {
int j = X509_STORE_CTX_get_error(&cert_ctx);
TSerror(TS_R_CERTIFICATE_VERIFY_ERROR);
ERR_asprintf_error_data("Verify error:%s",
X509_verify_cert_error_string(j));
goto err;
} else {
/* Get a copy of the certificate chain. */
*chain = X509_STORE_CTX_get1_chain(&cert_ctx);
ret = 1;
}
err:
X509_STORE_CTX_cleanup(&cert_ctx);
return ret;
}
static int
TS_check_signing_certs(PKCS7_SIGNER_INFO *si, STACK_OF(X509) *chain)
{
ESS_SIGNING_CERT *ss = NULL;
STACK_OF(ESS_CERT_ID) *cert_ids;
ESS_SIGNING_CERT_V2 *ssv2 = NULL;
STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2;
X509 *cert;
int i = 0;
int ret = 0;
if ((ss = ESS_get_signing_cert(si)) != NULL) {
cert_ids = ss->cert_ids;
/* The signer certificate must be the first in cert_ids. */
cert = sk_X509_value(chain, 0);
if (TS_find_cert(cert_ids, cert) != 0)
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) {
/* All the certificates of the chain must be in cert_ids. */
for (i = 1; i < sk_X509_num(chain); i++) {
cert = sk_X509_value(chain, i);
if (TS_find_cert(cert_ids, cert) < 0)
goto err;
}
}
}
if ((ssv2 = ESS_get_signing_cert_v2(si)) != NULL) {
cert_ids_v2 = ssv2->cert_ids;
/* The signer certificate must be the first in cert_ids_v2. */
cert = sk_X509_value(chain, 0);
if (TS_find_cert_v2(cert_ids_v2, cert) != 0)
goto err;
/*
* Check the other certificates of the chain if there are more
* than one certificate ids in cert_ids_v2.
*/
if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
/* All the certificates of the chain must be in cert_ids_v2. */
for (i = 1; i < sk_X509_num(chain); i++) {
cert = sk_X509_value(chain, i);
if (TS_find_cert_v2(cert_ids_v2, cert) < 0)
goto err;
}
}
}
ret = 1;
err:
if (!ret)
TSerror(TS_R_ESS_SIGNING_CERTIFICATE_ERROR);
ESS_SIGNING_CERT_free(ss);
ESS_SIGNING_CERT_V2_free(ssv2);
return ret;
}
static ESS_SIGNING_CERT *
ESS_get_signing_cert(PKCS7_SIGNER_INFO *si)
{
ASN1_TYPE *attr;
const unsigned char *p;
attr = PKCS7_get_signed_attribute(si,
NID_id_smime_aa_signingCertificate);
if (!attr)
return NULL;
if (attr->type != V_ASN1_SEQUENCE)
return NULL;
p = attr->value.sequence->data;
return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
}
static ESS_SIGNING_CERT_V2 *
ESS_get_signing_cert_v2(PKCS7_SIGNER_INFO *si)
{
ASN1_TYPE *attr;
const unsigned char *p;
attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificateV2);
if (attr == NULL)
return NULL;
p = attr->value.sequence->data;
return d2i_ESS_SIGNING_CERT_V2(NULL, &p, attr->value.sequence->length);
}
/* Returns < 0 if certificate is not found, certificate index otherwise. */
static int
TS_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
{
int i;
unsigned char cert_hash[TS_HASH_LEN];
if (!cert_ids || !cert)
return -1;
if (!X509_digest(cert, TS_HASH_EVP, cert_hash, NULL))
return -1;
/* Recompute SHA1 hash of certificate if necessary (side effect). */
if (X509_check_purpose(cert, -1, 0) == -1)
return -1;
/* 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);
/* Check the SHA-1 hash first. */
if (cid->hash->length == TS_HASH_LEN && !memcmp(cid->hash->data,
cert_hash, TS_HASH_LEN)) {
/* Check the issuer/serial as well if specified. */
ESS_ISSUER_SERIAL *is = cid->issuer_serial;
if (is == NULL || TS_issuer_serial_cmp(is, cert) == 0)
return i;
}
}
return -1;
}
/* Returns < 0 if certificate is not found, certificate index otherwise. */
static int
TS_find_cert_v2(STACK_OF(ESS_CERT_ID_V2) *cert_ids, X509 *cert)
{
int i;
unsigned char cert_digest[EVP_MAX_MD_SIZE];
unsigned int len;
/* Look for cert in the cert_ids vector. */
for (i = 0; i < sk_ESS_CERT_ID_V2_num(cert_ids); ++i) {
ESS_CERT_ID_V2 *cid = sk_ESS_CERT_ID_V2_value(cert_ids, i);
const EVP_MD *md = EVP_sha256();
if (cid->hash_alg != NULL)
md = EVP_get_digestbyobj(cid->hash_alg->algorithm);
if (md == NULL)
return -1;
if (!X509_digest(cert, md, cert_digest, &len))
return -1;
if ((unsigned int)cid->hash->length != len)
return -1;
if (memcmp(cid->hash->data, cert_digest, cid->hash->length) == 0) {
ESS_ISSUER_SERIAL *is = cid->issuer_serial;
if (is == NULL || TS_issuer_serial_cmp(is, cert) == 0)
return i;
}
}
return -1;
}
static int
TS_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert)
{
GENERAL_NAME *issuer;
if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
return -1;
/* Check the issuer first. It must be a directory name. */
issuer = sk_GENERAL_NAME_value(is->issuer, 0);
if (issuer->type != GEN_DIRNAME ||
X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)))
return -1;
/* Check the serial number, too. */
if (ASN1_INTEGER_cmp(is->serial, X509_get_serialNumber(cert)))
return -1;
return 0;
}
/*
* Verifies whether 'response' contains a valid response with regards
* to the settings of the context:
* - Gives an error message if the TS_TST_INFO is not present.
* - Calls _TS_RESP_verify_token to verify the token content.
*/
int
TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response)
{
PKCS7 *token = TS_RESP_get_token(response);
TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
int ret = 0;
/* Check if we have a successful TS_TST_INFO object in place. */
if (!TS_check_status_info(response))
goto err;
/* Check the contents of the time stamp token. */
if (!int_TS_RESP_verify_token(ctx, token, tst_info))
goto err;
ret = 1;
err:
return ret;
}
/*
* Tries to extract a TS_TST_INFO structure from the PKCS7 token and
* calls the internal int_TS_RESP_verify_token function for verifying it.
*/
int
TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token)
{
TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token);
int ret = 0;
if (tst_info) {
ret = int_TS_RESP_verify_token(ctx, token, tst_info);
TS_TST_INFO_free(tst_info);
}
return ret;
}
/*
* Verifies whether the 'token' contains a valid time stamp token
* with regards to the settings of the context. Only those checks are
* carried out that are specified in the context:
* - 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.
*/
static int
int_TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token,
TS_TST_INFO *tst_info)
{
X509 *signer = NULL;
GENERAL_NAME *tsa_name = TS_TST_INFO_get_tsa(tst_info);
X509_ALGOR *md_alg = NULL;
unsigned char *imprint = NULL;
unsigned imprint_len = 0;
int ret = 0;
/* Verify the signature. */
if ((ctx->flags & TS_VFY_SIGNATURE) &&
!TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
goto err;
/* Check version number of response. */
if ((ctx->flags & TS_VFY_VERSION) &&
TS_TST_INFO_get_version(tst_info) != 1) {
TSerror(TS_R_UNSUPPORTED_VERSION);
goto err;
}
/* Check policies. */
if ((ctx->flags & TS_VFY_POLICY) &&
!TS_check_policy(ctx->policy, tst_info))
goto err;
/* Check message imprints. */
if ((ctx->flags & TS_VFY_IMPRINT) &&
!TS_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
tst_info))
goto err;
/* Compute and check message imprints. */
if ((ctx->flags & TS_VFY_DATA) &&
(!TS_compute_imprint(ctx->data, tst_info,
&md_alg, &imprint, &imprint_len) ||
!TS_check_imprints(md_alg, imprint, imprint_len, tst_info)))
goto err;
/* Check nonces. */
if ((ctx->flags & TS_VFY_NONCE) &&
!TS_check_nonces(ctx->nonce, tst_info))
goto err;
/* Check whether TSA name and signer certificate match. */
if ((ctx->flags & TS_VFY_SIGNER) &&
tsa_name && !TS_check_signer_name(tsa_name, signer)) {
TSerror(TS_R_TSA_NAME_MISMATCH);
goto err;
}
/* Check whether the TSA is the expected one. */
if ((ctx->flags & TS_VFY_TSA_NAME) &&
!TS_check_signer_name(ctx->tsa_name, signer)) {
TSerror(TS_R_TSA_UNTRUSTED);
goto err;
}
ret = 1;
err:
X509_free(signer);
X509_ALGOR_free(md_alg);
free(imprint);
return ret;
}
static int
TS_check_status_info(TS_RESP *response)
{
TS_STATUS_INFO *info = TS_RESP_get_status_info(response);
long status = ASN1_INTEGER_get(info->status);
const char *status_text = NULL;
char *embedded_status_text = NULL;
char failure_text[TS_STATUS_BUF_SIZE] = "";
/* Check if everything went fine. */
if (status == 0 || status == 1)
return 1;
/* There was an error, get the description in status_text. */
if (0 <= status && status < (long)TS_STATUS_TEXT_SIZE)
status_text = TS_status_text[status];
else
status_text = "unknown code";
/* Set the embedded_status_text to the returned description. */
if (sk_ASN1_UTF8STRING_num(info->text) > 0 &&
!(embedded_status_text = TS_get_status_text(info->text)))
return 0;
/* Filling in failure_text with the failure information. */
if (info->failure_info) {
int i;
int first = 1;
for (i = 0; i < (int)TS_FAILURE_INFO_SIZE; ++i) {
if (ASN1_BIT_STRING_get_bit(info->failure_info,
TS_failure_info[i].code)) {
if (!first)
strlcat(failure_text, ",",
TS_STATUS_BUF_SIZE);
else
first = 0;
strlcat(failure_text, TS_failure_info[i].text,
TS_STATUS_BUF_SIZE);
}
}
}
if (failure_text[0] == '\0')
strlcpy(failure_text, "unspecified", TS_STATUS_BUF_SIZE);
/* Making up the error string. */
TSerror(TS_R_NO_TIME_STAMP_TOKEN);
ERR_asprintf_error_data
("status code: %s, status text: %s, failure codes: %s",
status_text,
embedded_status_text ? embedded_status_text : "unspecified",
failure_text);
free(embedded_status_text);
return 0;
}
static char *
TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
{
int i;
unsigned int length = 0;
char *result = NULL;
/* Determine length first. */
for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
length += ASN1_STRING_length(current);
length += 1; /* separator character */
}
/* Allocate memory (closing '\0' included). */
if (!(result = malloc(length))) {
TSerror(ERR_R_MALLOC_FAILURE);
return NULL;
}
/* Concatenate the descriptions. */
result[0] = '\0';
for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
if (i > 0)
strlcat(result, "/", length);
strlcat(result, (const char *)ASN1_STRING_data(current), length);
}
return result;
}
static int
TS_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info)
{
ASN1_OBJECT *resp_oid = TS_TST_INFO_get_policy_id(tst_info);
if (OBJ_cmp(req_oid, resp_oid) != 0) {
TSerror(TS_R_POLICY_MISMATCH);
return 0;
}
return 1;
}
static int
TS_compute_imprint(BIO *data, TS_TST_INFO *tst_info, X509_ALGOR **out_md_alg,
unsigned char **out_imprint, unsigned int *out_imprint_len)
{
TS_MSG_IMPRINT *msg_imprint;
X509_ALGOR *md_alg_resp;
X509_ALGOR *md_alg = NULL;
unsigned char *imprint = NULL;
unsigned int imprint_len = 0;
const EVP_MD *md;
EVP_MD_CTX md_ctx;
unsigned char buffer[4096];
int length;
*out_md_alg = NULL;
*out_imprint = NULL;
*out_imprint_len = 0;
/* Retrieve the MD algorithm of the response. */
msg_imprint = TS_TST_INFO_get_msg_imprint(tst_info);
md_alg_resp = TS_MSG_IMPRINT_get_algo(msg_imprint);
if ((md_alg = X509_ALGOR_dup(md_alg_resp)) == NULL)
goto err;
/* Getting the MD object. */
if ((md = EVP_get_digestbyobj((md_alg)->algorithm)) == NULL) {
TSerror(TS_R_UNSUPPORTED_MD_ALGORITHM);
goto err;
}
/* Compute message digest. */
if ((length = EVP_MD_size(md)) < 0)
goto err;
imprint_len = length;
if ((imprint = malloc(imprint_len)) == NULL) {
TSerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EVP_DigestInit(&md_ctx, md))
goto err;
while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) {
if (!EVP_DigestUpdate(&md_ctx, buffer, length))
goto err;
}
if (!EVP_DigestFinal(&md_ctx, imprint, NULL))
goto err;
*out_md_alg = md_alg;
md_alg = NULL;
*out_imprint = imprint;
imprint = NULL;
*out_imprint_len = imprint_len;
return 1;
err:
X509_ALGOR_free(md_alg);
free(imprint);
return 0;
}
static int
TS_check_imprints(X509_ALGOR *algor_a, unsigned char *imprint_a, unsigned len_a,
TS_TST_INFO *tst_info)
{
TS_MSG_IMPRINT *b = TS_TST_INFO_get_msg_imprint(tst_info);
X509_ALGOR *algor_b = TS_MSG_IMPRINT_get_algo(b);
int ret = 0;
/* algor_a is optional. */
if (algor_a) {
/* Compare algorithm OIDs. */
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;
}
/* Compare octet strings. */
ret = len_a == (unsigned) ASN1_STRING_length(b->hashed_msg) &&
memcmp(imprint_a, ASN1_STRING_data(b->hashed_msg), len_a) == 0;
err:
if (!ret)
TSerror(TS_R_MESSAGE_IMPRINT_MISMATCH);
return ret;
}
static int
TS_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info)
{
const ASN1_INTEGER *b = TS_TST_INFO_get_nonce(tst_info);
/* Error if nonce is missing. */
if (!b) {
TSerror(TS_R_NONCE_NOT_RETURNED);
return 0;
}
/* No error if a nonce is returned without being requested. */
if (ASN1_INTEGER_cmp(a, b) != 0) {
TSerror(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. */
static int
TS_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)
{
STACK_OF(GENERAL_NAME) *gen_names = NULL;
int idx = -1;
int found = 0;
if (signer == NULL)
return 0;
/* Check the subject name first. */
if (tsa_name->type == GEN_DIRNAME &&
X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0)
return 1;
/* Check all the alternative names. */
gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name,
NULL, &idx);
while (gen_names != NULL &&
!(found = (TS_find_name(gen_names, tsa_name) >= 0))) {
/* Get the next subject alternative name,
although there should be no more than one. */
GENERAL_NAMES_free(gen_names);
gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name,
NULL, &idx);
}
if (gen_names)
GENERAL_NAMES_free(gen_names);
return found;
}
/* Returns 1 if name is in gen_names, 0 otherwise. */
static int
TS_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name)
{
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;
}

226
crypto/ts/ts_verify_ctx.c Normal file
View File

@@ -0,0 +1,226 @@
/* $OpenBSD: ts_verify_ctx.c,v 1.13 2023/04/25 17:52:54 tb Exp $ */
/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
* project 2003.
*/
/* ====================================================================
* Copyright (c) 2006 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <string.h>
#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/ts.h>
#include "ts_local.h"
TS_VERIFY_CTX *
TS_VERIFY_CTX_new(void)
{
TS_VERIFY_CTX *ctx = calloc(1, sizeof(TS_VERIFY_CTX));
if (!ctx)
TSerror(ERR_R_MALLOC_FAILURE);
return ctx;
}
void
TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
{
if (!ctx)
return;
TS_VERIFY_CTX_cleanup(ctx);
free(ctx);
}
void
TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
{
if (!ctx)
return;
X509_STORE_free(ctx->store);
sk_X509_pop_free(ctx->certs, X509_free);
ASN1_OBJECT_free(ctx->policy);
X509_ALGOR_free(ctx->md_alg);
free(ctx->imprint);
BIO_free_all(ctx->data);
ASN1_INTEGER_free(ctx->nonce);
GENERAL_NAME_free(ctx->tsa_name);
memset(ctx, 0, sizeof(*ctx));
}
/*
* XXX: The following accessors demonstrate the amount of care and thought that
* went into OpenSSL 1.1 API design and the review thereof: for whatever reason
* these functions return what was passed in. Correct memory management is left
* as an exercise for the reader... Unfortunately, careful consumers like
* openssl-ruby assume this behavior, so we're stuck with this insanity. The
* cherry on top is the TS_VERIFY_CTS_set_certs() [sic!] function that made it
* into the public API.
*
* Outstanding job, R$ and tjh, A+.
*/
int
TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int flags)
{
ctx->flags |= flags;
return ctx->flags;
}
int
TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int flags)
{
ctx->flags = flags;
return ctx->flags;
}
BIO *
TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *bio)
{
ctx->data = bio;
return ctx->data;
}
X509_STORE *
TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *store)
{
ctx->store = store;
return ctx->store;
}
STACK_OF(X509) *
TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx, STACK_OF(X509) *certs)
{
ctx->certs = certs;
return ctx->certs;
}
unsigned char *
TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx, unsigned char *imprint,
long imprint_len)
{
free(ctx->imprint);
ctx->imprint = imprint;
ctx->imprint_len = imprint_len;
return ctx->imprint;
}
TS_VERIFY_CTX *
TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
{
TS_VERIFY_CTX *ret = ctx;
ASN1_OBJECT *policy;
TS_MSG_IMPRINT *imprint;
X509_ALGOR *md_alg;
ASN1_OCTET_STRING *msg;
const ASN1_INTEGER *nonce;
if (ret)
TS_VERIFY_CTX_cleanup(ret);
else if (!(ret = TS_VERIFY_CTX_new()))
return NULL;
/* Setting flags. */
ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
/* Setting policy. */
if ((policy = TS_REQ_get_policy_id(req)) != NULL) {
if (!(ret->policy = OBJ_dup(policy)))
goto err;
} else
ret->flags &= ~TS_VFY_POLICY;
/* Setting md_alg, imprint and imprint_len. */
imprint = TS_REQ_get_msg_imprint(req);
md_alg = TS_MSG_IMPRINT_get_algo(imprint);
if (!(ret->md_alg = X509_ALGOR_dup(md_alg)))
goto err;
msg = TS_MSG_IMPRINT_get_msg(imprint);
ret->imprint_len = ASN1_STRING_length(msg);
if (!(ret->imprint = malloc(ret->imprint_len)))
goto err;
memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
/* Setting nonce. */
if ((nonce = TS_REQ_get_nonce(req)) != NULL) {
if (!(ret->nonce = ASN1_INTEGER_dup(nonce)))
goto err;
} else
ret->flags &= ~TS_VFY_NONCE;
return ret;
err:
if (ctx)
TS_VERIFY_CTX_cleanup(ctx);
else
TS_VERIFY_CTX_free(ret);
return NULL;
}