2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/pem.h>
11 #include <openssl/core_names.h>
12 #include <openssl/self_test.h>
15 typedef enum OPTION_choice {
25 struct self_test_arg {
29 static OSSL_LIB_CTX *libctx = NULL;
30 static char *pairwise_name = NULL;
31 static char *dsaparam_file = NULL;
32 static struct self_test_arg self_test_args = { 0 };
34 const OPTIONS *test_get_options(void)
36 static const OPTIONS test_options[] = {
37 OPT_TEST_OPTIONS_DEFAULT_USAGE,
38 { "config", OPT_CONFIG_FILE, '<',
39 "The configuration file to use for the libctx" },
40 { "pairwise", OPT_PAIRWISETEST, 's',
41 "Test keygen pairwise test failures" },
42 { "dsaparam", OPT_DSAPARAM, 's', "DSA param file" },
48 static int self_test_on_pairwise_fail(const OSSL_PARAM params[], void *arg)
50 struct self_test_arg *args = arg;
51 const OSSL_PARAM *p = NULL;
52 const char *type = NULL, *phase = NULL;
54 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
55 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
57 phase = (const char *)p->data;
58 if (strcmp(phase, OSSL_SELF_TEST_PHASE_CORRUPT) == 0) {
59 p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
60 if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
62 type = (const char *)p->data;
63 if (strcmp(type, args->type) == 0)
69 static int setup_selftest_pairwise_failure(const char *type)
72 OSSL_PROVIDER *prov = NULL;
74 if (!TEST_ptr(prov = OSSL_PROVIDER_load(libctx, "fips")))
77 /* Setup a callback that corrupts the pairwise self tests and causes failures */
78 self_test_args.type = type;
79 OSSL_SELF_TEST_set_callback(libctx, self_test_on_pairwise_fail, &self_test_args);
83 OSSL_PROVIDER_unload(prov);
87 static int test_keygen_pairwise_failure(void)
90 EVP_PKEY_CTX *ctx = NULL;
91 EVP_PKEY *pParams = NULL;
92 EVP_PKEY *pkey = NULL;
93 const char *type = OSSL_SELF_TEST_TYPE_PCT;
96 if (strcmp(pairwise_name, "rsa") == 0) {
97 if (!TEST_true(setup_selftest_pairwise_failure(type)))
99 if (!TEST_ptr_null(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", 2048)))
101 } else if (strncmp(pairwise_name, "ec", 2) == 0) {
102 if (strcmp(pairwise_name, "eckat") == 0)
103 type = OSSL_SELF_TEST_TYPE_PCT_KAT;
104 if (!TEST_true(setup_selftest_pairwise_failure(type)))
106 if (!TEST_ptr_null(pkey = EVP_PKEY_Q_keygen(libctx, NULL, "EC", "P-256")))
108 } else if (strncmp(pairwise_name, "dsa", 3) == 0) {
109 if (strcmp(pairwise_name, "dsakat") == 0)
110 type = OSSL_SELF_TEST_TYPE_PCT_KAT;
111 if (!TEST_true(setup_selftest_pairwise_failure(type)))
113 if (!TEST_ptr(bio = BIO_new_file(dsaparam_file, "r")))
115 if (!TEST_ptr(pParams = PEM_read_bio_Parameters_ex(bio, NULL, libctx, NULL)))
117 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pParams, NULL)))
119 if (!TEST_int_eq(EVP_PKEY_keygen_init(ctx), 1))
121 if (!TEST_int_le(EVP_PKEY_keygen(ctx, &pkey), 0))
123 if (!TEST_ptr_null(pkey))
129 EVP_PKEY_CTX_free(ctx);
131 EVP_PKEY_free(pParams);
135 int setup_tests(void)
138 char *config_file = NULL;
140 while ((o = opt_next()) != OPT_EOF) {
142 case OPT_CONFIG_FILE:
143 config_file = opt_arg();
145 case OPT_PAIRWISETEST:
146 pairwise_name = opt_arg();
149 dsaparam_file = opt_arg();
159 libctx = OSSL_LIB_CTX_new();
162 if (!OSSL_LIB_CTX_load_config(libctx, config_file)) {
163 opt_printf_stderr("Failed to load config\n");
166 ADD_TEST(test_keygen_pairwise_failure);
170 void cleanup_tests(void)
172 OSSL_LIB_CTX_free(libctx);