1149985Srwatson/* 2149985Srwatson * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. 3149985Srwatson * 4149985Srwatson * Licensed under the Apache License 2.0 (the "License"). You may not use 5149985Srwatson * this file except in compliance with the License. You can obtain a copy 6149985Srwatson * in the file LICENSE in the source distribution or at 7149985Srwatson * https://www.openssl.org/source/license.html 8149985Srwatson */ 9149985Srwatson 10149985Srwatson#include <string.h> 11149985Srwatson#include <limits.h> 12149985Srwatson#include <openssl/store.h> 13149985Srwatson#include <openssl/ui.h> 14149985Srwatson#include "testutil.h" 15149985Srwatson 16149985Srwatson#ifndef PATH_MAX 17149985Srwatson# if defined(_WIN32) && defined(_MAX_PATH) 18149985Srwatson# define PATH_MAX _MAX_PATH 19149985Srwatson# else 20149985Srwatson# define PATH_MAX 4096 21149985Srwatson# endif 22149985Srwatson#endif 23149985Srwatson 24149985Srwatsontypedef enum OPTION_choice { 25149985Srwatson OPT_ERR = -1, 26149985Srwatson OPT_EOF = 0, 27149985Srwatson OPT_INPUTDIR, 28149985Srwatson OPT_INFILE, 29149985Srwatson OPT_SM2FILE, 30149985Srwatson OPT_DATADIR, 31149985Srwatson OPT_TEST_ENUM 32149985Srwatson} OPTION_CHOICE; 33149985Srwatson 34149985Srwatsonstatic const char *inputdir = NULL; 35149985Srwatsonstatic const char *infile = NULL; 36149985Srwatsonstatic const char *sm2file = NULL; 37149985Srwatsonstatic const char *datadir = NULL; 38149985Srwatson 39149985Srwatsonstatic int test_store_open(void) 40149985Srwatson{ 41149985Srwatson int ret = 0; 42149985Srwatson OSSL_STORE_CTX *sctx = NULL; 43149985Srwatson OSSL_STORE_SEARCH *search = NULL; 44149985Srwatson UI_METHOD *ui_method = NULL; 45149985Srwatson char *input = test_mk_file_path(inputdir, infile); 46149985Srwatson 47149985Srwatson ret = TEST_ptr(input) 48149985Srwatson && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing")) 49149985Srwatson && TEST_ptr(ui_method= UI_create_method("DummyUI")) 50149985Srwatson && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method, 51149985Srwatson NULL, NULL, NULL, NULL)) 52149985Srwatson && TEST_false(OSSL_STORE_find(sctx, NULL)) 53149985Srwatson && TEST_true(OSSL_STORE_find(sctx, search)); 54149985Srwatson UI_destroy_method(ui_method); 55149985Srwatson OSSL_STORE_SEARCH_free(search); 56149985Srwatson OSSL_STORE_close(sctx); 57149985Srwatson OPENSSL_free(input); 58149985Srwatson return ret; 59149985Srwatson} 60149985Srwatson 61149985Srwatsonstatic int test_store_search_by_key_fingerprint_fail(void) 62149985Srwatson{ 63149985Srwatson int ret; 64149985Srwatson OSSL_STORE_SEARCH *search = NULL; 65149985Srwatson 66149985Srwatson ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint( 67149985Srwatson EVP_sha256(), NULL, 0)); 68149985Srwatson OSSL_STORE_SEARCH_free(search); 69149985Srwatson return ret; 70149985Srwatson} 71149985Srwatson 72149985Srwatsonstatic int get_params(const char *uri, const char *type) 73149985Srwatson{ 74149985Srwatson EVP_PKEY *pkey = NULL; 75149985Srwatson OSSL_STORE_CTX *ctx = NULL; 76149985Srwatson OSSL_STORE_INFO *info; 77149985Srwatson int ret = 0; 78149985Srwatson 79149985Srwatson ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL); 80149985Srwatson if (!TEST_ptr(ctx)) 81149985Srwatson goto err; 82149985Srwatson 83149985Srwatson while (!OSSL_STORE_eof(ctx) 84149985Srwatson && (info = OSSL_STORE_load(ctx)) != NULL 85149985Srwatson && pkey == NULL) { 86149985Srwatson if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { 87149985Srwatson pkey = OSSL_STORE_INFO_get1_PARAMS(info); 88149985Srwatson } 89149985Srwatson OSSL_STORE_INFO_free(info); 90281450Sngie info = NULL; 91149985Srwatson } 92149985Srwatson 93149985Srwatson if (pkey != NULL) 94149985Srwatson ret = EVP_PKEY_is_a(pkey, type); 95149985Srwatson EVP_PKEY_free(pkey); 96149985Srwatson 97149985Srwatson err: 98149985Srwatson OSSL_STORE_close(ctx); 99149985Srwatson return ret; 100149985Srwatson} 101149985Srwatson 102149985Srwatsonstatic int test_store_get_params(int idx) 103149985Srwatson{ 104149985Srwatson const char *type; 105149985Srwatson const char *urifmt; 106149985Srwatson char uri[PATH_MAX]; 107149985Srwatson 108149985Srwatson switch(idx) { 109149985Srwatson#ifndef OPENSSL_NO_DH 110149985Srwatson case 0: 111149985Srwatson type = "DH"; 112149985Srwatson break; 113149985Srwatson case 1: 114149985Srwatson type = "DHX"; 115149985Srwatson break; 116149985Srwatson#else 117149985Srwatson case 0: 118149985Srwatson case 1: 119149985Srwatson return 1; 120149985Srwatson#endif 121149985Srwatson case 2: 122149985Srwatson#ifndef OPENSSL_NO_DSA 123149985Srwatson type = "DSA"; 124149985Srwatson break; 125149985Srwatson#else 126149985Srwatson return 1; 127149985Srwatson#endif 128149985Srwatson default: 129149985Srwatson TEST_error("Invalid test index"); 130149985Srwatson return 0; 131149985Srwatson } 132149985Srwatson 133149985Srwatson urifmt = "%s/%s-params.pem"; 134149985Srwatson#ifdef __VMS 135149985Srwatson { 136149985Srwatson char datadir_end = datadir[strlen(datadir) - 1]; 137149985Srwatson 138149985Srwatson if (datadir_end == ':' || datadir_end == ']' || datadir_end == '>') 139149985Srwatson urifmt = "%s%s-params.pem"; 140149985Srwatson } 141149985Srwatson#endif 142149985Srwatson if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type))) 143149985Srwatson return 0; 144149985Srwatson 145149985Srwatson TEST_info("Testing uri: %s", uri); 146149985Srwatson if (!TEST_true(get_params(uri, type))) 147149985Srwatson return 0; 148149985Srwatson 149149985Srwatson return 1; 150149985Srwatson} 151149985Srwatson 152149985Srwatson/* 153149985Srwatson * This test verifies that calling OSSL_STORE_ATTACH does not set an 154149985Srwatson * "unregistered scheme" error when called. 155149985Srwatson */ 156149985Srwatsonstatic int test_store_attach_unregistered_scheme(void) 157149985Srwatson{ 158149985Srwatson int ret; 159149985Srwatson OSSL_STORE_CTX *store_ctx = NULL; 160149985Srwatson OSSL_PROVIDER *provider = NULL; 161149985Srwatson OSSL_LIB_CTX *libctx = NULL; 162149985Srwatson BIO *bio = NULL; 163149985Srwatson char *input = test_mk_file_path(inputdir, sm2file); 164149985Srwatson 165149985Srwatson ret = TEST_ptr(input) 166149985Srwatson && TEST_ptr(libctx = OSSL_LIB_CTX_new()) 167149985Srwatson && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default")) 168149985Srwatson && TEST_ptr(bio = BIO_new_file(input, "r")) 169149985Srwatson && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL, 170149985Srwatson NULL, NULL, NULL, NULL, NULL)) 171149985Srwatson && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE) 172149985Srwatson && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()), 173149985Srwatson OSSL_STORE_R_UNREGISTERED_SCHEME); 174149985Srwatson 175149985Srwatson BIO_free(bio); 176149985Srwatson OSSL_STORE_close(store_ctx); 177149985Srwatson OSSL_PROVIDER_unload(provider); 178149985Srwatson OSSL_LIB_CTX_free(libctx); 179149985Srwatson OPENSSL_free(input); 180149985Srwatson return ret; 181149985Srwatson} 182149985Srwatson 183149985Srwatsonconst OPTIONS *test_get_options(void) 184149985Srwatson{ 185149985Srwatson static const OPTIONS test_options[] = { 186149985Srwatson OPT_TEST_OPTIONS_DEFAULT_USAGE, 187149985Srwatson { "dir", OPT_INPUTDIR, '/' }, 188149985Srwatson { "in", OPT_INFILE, '<' }, 189149985Srwatson { "sm2", OPT_SM2FILE, '<' }, 190149985Srwatson { "data", OPT_DATADIR, 's' }, 191149985Srwatson { NULL } 192149985Srwatson }; 193149985Srwatson return test_options; 194149985Srwatson} 195149985Srwatson 196149985Srwatsonint setup_tests(void) 197149985Srwatson{ 198149985Srwatson OPTION_CHOICE o; 199149985Srwatson 200149985Srwatson while ((o = opt_next()) != OPT_EOF) { 201149985Srwatson switch (o) { 202149985Srwatson case OPT_INPUTDIR: 203149985Srwatson inputdir = opt_arg(); 204149985Srwatson break; 205149985Srwatson case OPT_INFILE: 206149985Srwatson infile = opt_arg(); 207149985Srwatson break; 208149985Srwatson case OPT_SM2FILE: 209149985Srwatson sm2file = opt_arg(); 210149985Srwatson break; 211149985Srwatson case OPT_DATADIR: 212149985Srwatson datadir = opt_arg(); 213149985Srwatson break; 214149985Srwatson case OPT_TEST_CASES: 215149985Srwatson break; 216149985Srwatson default: 217149985Srwatson case OPT_ERR: 218149985Srwatson return 0; 219149985Srwatson } 220149985Srwatson } 221149985Srwatson 222149985Srwatson if (datadir == NULL) { 223149985Srwatson TEST_error("No data directory specified"); 224149985Srwatson return 0; 225149985Srwatson } 226149985Srwatson if (inputdir == NULL) { 227149985Srwatson TEST_error("No input directory specified"); 228149985Srwatson return 0; 229149985Srwatson } 230149985Srwatson 231149985Srwatson if (infile != NULL) 232149985Srwatson ADD_TEST(test_store_open); 233149985Srwatson ADD_TEST(test_store_search_by_key_fingerprint_fail); 234149985Srwatson ADD_ALL_TESTS(test_store_get_params, 3); 235149985Srwatson if (sm2file != NULL) 236149985Srwatson ADD_TEST(test_store_attach_unregistered_scheme); 237149985Srwatson return 1; 238149985Srwatson} 239149985Srwatson