/*
* Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
* Copyright Nokia 2007-2019
* Copyright Siemens AG 2015-2019
*
* Licensed under the Apache License 2.0 (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
*/
#include "helpers/cmp_testlib.h"
#include <openssl/x509_vfy.h>
typedef struct test_fixture {
const char *test_case_name;
OSSL_CMP_CTX *ctx;
} OSSL_CMP_CTX_TEST_FIXTURE;
static void tear_down(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
{
if (fixture != NULL)
OSSL_CMP_CTX_free(fixture->ctx);
OPENSSL_free(fixture);
}
static OSSL_CMP_CTX_TEST_FIXTURE *set_up(const char *const test_case_name)
{
OSSL_CMP_CTX_TEST_FIXTURE *fixture;
if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
return NULL;
if (!TEST_ptr(fixture->ctx = OSSL_CMP_CTX_new(NULL, NULL))) {
tear_down(fixture);
return NULL;
}
fixture->test_case_name = test_case_name;
return fixture;
}
static STACK_OF(X509) *sk_X509_new_1(void)
{
STACK_OF(X509) *sk = sk_X509_new_null();
X509 *x = X509_new();
if (x == NULL || !sk_X509_push(sk, x)) {
sk_X509_free(sk);
X509_free(x);
sk = NULL;
}
return sk;
}
static void sk_X509_pop_X509_free(STACK_OF(X509) *sk)
{
OSSL_STACK_OF_X509_free(sk);
}
static int execute_CTX_reinit_test(OSSL_CMP_CTX_TEST_FIXTURE *fixture)
{
OSSL_CMP_CTX *ctx = fixture->ctx;
ASN1_OCTET_STRING *bytes = NULL;
STACK_OF(X509) *certs = NULL;
X509 *cert = X509_new();
int res = 0;
/* set non-default values in all relevant fields */
ctx->status = 1;
ctx->failInfoCode = 1;
if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
|| !ossl_cmp_ctx_set0_newCert(ctx, X509_new())
|| !TEST_ptr(certs = sk_X509_new_1())
|| !ossl_cmp_ctx_set1_newChain(ctx, certs)
|| !ossl_cmp_ctx_set1_caPubs(ctx, certs)
|| !ossl_cmp_ctx_set1_extraCertsIn(ctx, certs)
|| !ossl_cmp_ctx_set1_validatedSrvCert(ctx, cert)
|| !TEST_ptr(bytes = ASN1_OCTET_STRING_new())
|| !OSSL_CMP_CTX_set1_transactionID(ctx, bytes)
|| !OSSL_CMP_CTX_set1_senderNonce(ctx, bytes)
|| !ossl_cmp_ctx_set1_recipNonce(ctx, bytes))
goto err;
if (!TEST_true(OSSL_CMP_CTX_reinit(ctx)))
goto err;
/* check whether values have been reset to default in all relevant fields */
if (!TEST_true(ctx->status == -1
&& ctx->failInfoCode == -1
&& ctx->statusString == NULL
&& ctx->newCert == NULL
&& ctx->newChain == NULL
&& ctx->caPubs == NULL
&& ctx->extraCertsIn == NULL
&& ctx->validatedSrvCert == NULL
&& ctx->transactionID == NULL
&& ctx->senderNonce == NULL
&& ctx->recipNonce == NULL))
goto err;
/* this does not check that all remaining fields are untouched */
res = 1;
err:
X509_free(cert);
sk_X509_pop_X509_free(certs);
ASN1_OCTET_STRING_free(bytes);
return res;
}
static int test_CTX_libctx_propq(void)
{
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
const char *propq = "?provider=legacy";
OSSL_CMP_CTX *cmpctx = OSSL_CMP_CTX_new(libctx, propq);
int res = TEST_ptr(libctx)
&& TEST_ptr(cmpctx)
&& TEST_ptr_eq(libctx, OSSL_CMP_CTX_get0_libctx(cmpctx))
&& TEST_str_eq(propq, OSSL_CMP_CTX_get0_propq(cmpctx));
OSSL_CMP_CTX_free(cmpctx);
OSSL_LIB_CTX_free(libctx);
return res;
}
static int test_CTX_reinit(void)
{
SETUP_TEST_FIXTURE(OSSL_CMP_CTX_TEST_FIXTURE, set_up);
EXECUTE_TEST(execute_CTX_reinit_test, tear_down);
return result;
}
#if !defined(OPENSSL_NO_ERR) && !defined(OPENSSL_NO_AUTOERRINIT)
static int msg_total_size = 0;
static int msg_total_size_log_cb(const char *func, const char *file, int line,
OSSL_CMP_severity level, const char *msg)
{
msg_total_size += strlen(msg);