1 1.1 christos /* 2 1.1 christos * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #include <string.h> 11 1.1 christos #include <openssl/core_dispatch.h> 12 1.1 christos #include <openssl/evp.h> 13 1.1 christos #include <openssl/pem.h> 14 1.1 christos #include <openssl/rsa.h> 15 1.1 christos #include <openssl/x509.h> 16 1.1 christos #include <openssl/core_names.h> 17 1.1 christos #include <openssl/params.h> 18 1.1 christos #include <openssl/param_build.h> 19 1.1 christos #include <openssl/encoder.h> 20 1.1 christos #include <openssl/decoder.h> 21 1.1 christos 22 1.1.1.2 christos #include "internal/cryptlib.h" /* ossl_assert */ 23 1.1.1.2 christos #include "crypto/pem.h" /* For PVK and "blob" PEM headers */ 24 1.1.1.2 christos #include "crypto/evp.h" /* For evp_pkey_is_provided() */ 25 1.1 christos 26 1.1 christos #include "helpers/predefined_dhparams.h" 27 1.1 christos #include "testutil.h" 28 1.1 christos 29 1.1 christos #ifdef STATIC_LEGACY 30 1.1 christos OSSL_provider_init_fn ossl_legacy_provider_init; 31 1.1 christos #endif 32 1.1 christos 33 1.1 christos /* Extended test macros to allow passing file & line number */ 34 1.1.1.2 christos #define TEST_FL_ptr(a) test_ptr(file, line, #a, a) 35 1.1.1.2 christos #define TEST_FL_mem_eq(a, m, b, n) test_mem_eq(file, line, #a, #b, a, m, b, n) 36 1.1.1.2 christos #define TEST_FL_strn_eq(a, b, n) test_strn_eq(file, line, #a, #b, a, n, b, n) 37 1.1 christos #define TEST_FL_strn2_eq(a, m, b, n) test_strn_eq(file, line, #a, #b, a, m, b, n) 38 1.1.1.2 christos #define TEST_FL_int_eq(a, b) test_int_eq(file, line, #a, #b, a, b) 39 1.1.1.2 christos #define TEST_FL_int_ge(a, b) test_int_ge(file, line, #a, #b, a, b) 40 1.1.1.2 christos #define TEST_FL_int_gt(a, b) test_int_gt(file, line, #a, #b, a, b) 41 1.1.1.2 christos #define TEST_FL_long_gt(a, b) test_long_gt(file, line, #a, #b, a, b) 42 1.1.1.2 christos #define TEST_FL_true(a) test_true(file, line, #a, (a) != 0) 43 1.1 christos 44 1.1 christos #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC) 45 1.1.1.2 christos #define OPENSSL_NO_KEYPARAMS 46 1.1 christos #endif 47 1.1 christos 48 1.1 christos static int default_libctx = 1; 49 1.1 christos static int is_fips = 0; 50 1.1 christos static int is_fips_3_0_0 = 0; 51 1.1 christos static int is_fips_lt_3_5 = 0; 52 1.1 christos 53 1.1 christos static OSSL_LIB_CTX *testctx = NULL; 54 1.1 christos static OSSL_LIB_CTX *keyctx = NULL; 55 1.1 christos static char *testpropq = NULL; 56 1.1 christos 57 1.1 christos static OSSL_PROVIDER *nullprov = NULL; 58 1.1 christos static OSSL_PROVIDER *deflprov = NULL; 59 1.1 christos static OSSL_PROVIDER *keyprov = NULL; 60 1.1 christos 61 1.1 christos #ifndef OPENSSL_NO_EC 62 1.1 christos static BN_CTX *bnctx = NULL; 63 1.1 christos static OSSL_PARAM_BLD *bld_prime_nc = NULL; 64 1.1 christos static OSSL_PARAM_BLD *bld_prime = NULL; 65 1.1 christos static OSSL_PARAM *ec_explicit_prime_params_nc = NULL; 66 1.1 christos static OSSL_PARAM *ec_explicit_prime_params_explicit = NULL; 67 1.1 christos 68 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 69 1.1 christos static OSSL_PARAM_BLD *bld_tri_nc = NULL; 70 1.1 christos static OSSL_PARAM_BLD *bld_tri = NULL; 71 1.1 christos static OSSL_PARAM *ec_explicit_tri_params_nc = NULL; 72 1.1 christos static OSSL_PARAM *ec_explicit_tri_params_explicit = NULL; 73 1.1.1.2 christos #endif 74 1.1 christos #endif 75 1.1 christos 76 1.1 christos #ifndef OPENSSL_NO_KEYPARAMS 77 1.1 christos static EVP_PKEY *make_template(const char *type, OSSL_PARAM *genparams) 78 1.1 christos { 79 1.1 christos EVP_PKEY *pkey = NULL; 80 1.1 christos EVP_PKEY_CTX *ctx = NULL; 81 1.1 christos 82 1.1.1.2 christos #ifndef OPENSSL_NO_DH 83 1.1 christos /* 84 1.1 christos * Use 512-bit DH(X) keys with predetermined parameters for efficiency, 85 1.1 christos * for testing only. Use a minimum key size of 2048 for security purposes. 86 1.1 christos */ 87 1.1 christos if (strcmp(type, "DH") == 0) 88 1.1 christos return get_dh512(keyctx); 89 1.1 christos 90 1.1 christos if (strcmp(type, "X9.42 DH") == 0) 91 1.1 christos return get_dhx512(keyctx); 92 1.1.1.2 christos #endif 93 1.1 christos 94 1.1 christos /* 95 1.1 christos * No real need to check the errors other than for the cascade 96 1.1 christos * effect. |pkey| will simply remain NULL if something goes wrong. 97 1.1 christos */ 98 1.1 christos (void)((ctx = EVP_PKEY_CTX_new_from_name(keyctx, type, testpropq)) != NULL 99 1.1.1.2 christos && EVP_PKEY_paramgen_init(ctx) > 0 100 1.1.1.2 christos && (genparams == NULL 101 1.1.1.2 christos || EVP_PKEY_CTX_set_params(ctx, genparams) > 0) 102 1.1.1.2 christos && EVP_PKEY_generate(ctx, &pkey) > 0); 103 1.1 christos EVP_PKEY_CTX_free(ctx); 104 1.1 christos 105 1.1 christos return pkey; 106 1.1 christos } 107 1.1 christos #endif 108 1.1 christos 109 1.1.1.2 christos #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_ML_DSA) || !defined(OPENSSL_NO_ML_KEM) || !defined(OPENSSL_NO_SLH_DSA) 110 1.1 christos static EVP_PKEY *make_key(const char *type, EVP_PKEY *template, 111 1.1.1.2 christos OSSL_PARAM *genparams) 112 1.1 christos { 113 1.1 christos EVP_PKEY *pkey = NULL; 114 1.1.1.2 christos EVP_PKEY_CTX *ctx = template != NULL 115 1.1 christos ? EVP_PKEY_CTX_new_from_pkey(keyctx, template, testpropq) 116 1.1 christos : EVP_PKEY_CTX_new_from_name(keyctx, type, testpropq); 117 1.1 christos 118 1.1 christos /* 119 1.1 christos * No real need to check the errors other than for the cascade 120 1.1 christos * effect. |pkey| will simply remain NULL if something goes wrong. 121 1.1 christos */ 122 1.1 christos (void)(ctx != NULL 123 1.1.1.2 christos && EVP_PKEY_keygen_init(ctx) > 0 124 1.1.1.2 christos && (genparams == NULL 125 1.1.1.2 christos || EVP_PKEY_CTX_set_params(ctx, genparams) > 0) 126 1.1.1.2 christos && EVP_PKEY_keygen(ctx, &pkey) > 0); 127 1.1 christos EVP_PKEY_CTX_free(ctx); 128 1.1 christos return pkey; 129 1.1 christos } 130 1.1 christos #endif 131 1.1 christos 132 1.1 christos /* Main test driver */ 133 1.1 christos 134 1.1.1.2 christos typedef int(encoder)(const char *file, const int line, 135 1.1.1.2 christos void **encoded, long *encoded_len, 136 1.1.1.2 christos void *object, int selection, 137 1.1.1.2 christos const char *output_type, const char *output_structure, 138 1.1.1.2 christos const char *pass, const char *pcipher); 139 1.1.1.2 christos typedef int(decoder)(const char *file, const int line, 140 1.1.1.2 christos void **object, void *encoded, long encoded_len, 141 1.1.1.2 christos const char *input_type, const char *structure_type, 142 1.1.1.2 christos const char *keytype, int selection, const char *pass); 143 1.1.1.2 christos typedef int(tester)(const char *file, const int line, 144 1.1.1.2 christos const void *data1, size_t data1_len, 145 1.1.1.2 christos const void *data2, size_t data2_len); 146 1.1.1.2 christos typedef int(checker)(const char *file, const int line, 147 1.1.1.2 christos const char *type, const void *data, size_t data_len); 148 1.1.1.2 christos typedef void(dumper)(const char *label, const void *data, size_t data_len); 149 1.1 christos 150 1.1.1.2 christos #define FLAG_DECODE_WITH_TYPE 0x0001 151 1.1.1.2 christos #define FLAG_FAIL_IF_FIPS 0x0002 152 1.1 christos 153 1.1 christos static int test_encode_decode(const char *file, const int line, 154 1.1.1.2 christos const char *type, EVP_PKEY *pkey, 155 1.1.1.2 christos int selection, const char *output_type, 156 1.1.1.2 christos const char *output_structure, 157 1.1.1.2 christos const char *pass, const char *pcipher, 158 1.1.1.2 christos encoder *encode_cb, decoder *decode_cb, 159 1.1.1.2 christos tester *test_cb, checker *check_cb, 160 1.1.1.2 christos dumper *dump_cb, int flags) 161 1.1 christos { 162 1.1 christos void *encoded = NULL; 163 1.1 christos long encoded_len = 0; 164 1.1 christos EVP_PKEY *pkey2 = NULL; 165 1.1 christos EVP_PKEY *pkey3 = NULL; 166 1.1 christos void *encoded2 = NULL; 167 1.1 christos long encoded2_len = 0; 168 1.1 christos int ok = 0; 169 1.1 christos 170 1.1 christos /* 171 1.1 christos * Encode |pkey|, decode the result into |pkey2|, and finish off by 172 1.1 christos * encoding |pkey2| as well. That last encoding is for checking and 173 1.1 christos * dumping purposes. 174 1.1 christos */ 175 1.1 christos if (!TEST_true(encode_cb(file, line, &encoded, &encoded_len, pkey, selection, 176 1.1.1.2 christos output_type, output_structure, pass, pcipher))) 177 1.1 christos goto end; 178 1.1 christos 179 1.1 christos if ((flags & FLAG_FAIL_IF_FIPS) != 0 && is_fips && !is_fips_3_0_0) { 180 1.1 christos if (TEST_false(decode_cb(file, line, (void **)&pkey2, encoded, 181 1.1.1.2 christos encoded_len, output_type, output_structure, 182 1.1.1.2 christos (flags & FLAG_DECODE_WITH_TYPE ? type : NULL), 183 1.1.1.2 christos selection, pass))) 184 1.1 christos ok = 1; 185 1.1 christos goto end; 186 1.1 christos } 187 1.1 christos 188 1.1 christos if (!TEST_true(check_cb(file, line, type, encoded, encoded_len)) 189 1.1 christos || !TEST_true(decode_cb(file, line, (void **)&pkey2, encoded, encoded_len, 190 1.1.1.2 christos output_type, output_structure, 191 1.1.1.2 christos (flags & FLAG_DECODE_WITH_TYPE ? type : NULL), 192 1.1.1.2 christos selection, pass)) 193 1.1 christos || ((output_structure == NULL 194 1.1.1.2 christos || strcmp(output_structure, "type-specific") != 0) 195 1.1 christos && !TEST_true(decode_cb(file, line, (void **)&pkey3, encoded, encoded_len, 196 1.1.1.2 christos output_type, output_structure, 197 1.1.1.2 christos (flags & FLAG_DECODE_WITH_TYPE ? type : NULL), 198 1.1.1.2 christos 0, pass))) 199 1.1 christos || !TEST_true(encode_cb(file, line, &encoded2, &encoded2_len, pkey2, selection, 200 1.1.1.2 christos output_type, output_structure, pass, pcipher))) 201 1.1 christos goto end; 202 1.1 christos 203 1.1 christos if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) { 204 1.1 christos if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey2), 1) 205 1.1 christos || (pkey3 != NULL 206 1.1 christos && !TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey3), 1))) 207 1.1 christos goto end; 208 1.1 christos } else { 209 1.1 christos if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1) 210 1.1 christos || (pkey3 != NULL 211 1.1 christos && !TEST_int_eq(EVP_PKEY_eq(pkey, pkey3), 1))) 212 1.1 christos goto end; 213 1.1 christos } 214 1.1 christos 215 1.1 christos /* 216 1.1 christos * Double check the encoding, but only for unprotected keys, 217 1.1 christos * as protected keys have a random component, which makes the output 218 1.1 christos * differ. 219 1.1 christos */ 220 1.1 christos if ((pass == NULL && pcipher == NULL) 221 1.1 christos && !test_cb(file, line, encoded, encoded_len, encoded2, encoded2_len)) 222 1.1 christos goto end; 223 1.1 christos 224 1.1 christos ok = 1; 225 1.1.1.2 christos end: 226 1.1 christos if (!ok) { 227 1.1 christos if (encoded != NULL && encoded_len != 0) 228 1.1 christos dump_cb("|pkey| encoded", encoded, encoded_len); 229 1.1 christos if (encoded2 != NULL && encoded2_len != 0) 230 1.1 christos dump_cb("|pkey2| encoded", encoded2, encoded2_len); 231 1.1 christos } 232 1.1 christos 233 1.1 christos OPENSSL_free(encoded); 234 1.1 christos OPENSSL_free(encoded2); 235 1.1 christos EVP_PKEY_free(pkey2); 236 1.1 christos EVP_PKEY_free(pkey3); 237 1.1 christos return ok; 238 1.1 christos } 239 1.1 christos 240 1.1 christos /* Encoding and decoding methods */ 241 1.1 christos 242 1.1 christos static int encode_EVP_PKEY_prov(const char *file, const int line, 243 1.1.1.2 christos void **encoded, long *encoded_len, 244 1.1.1.2 christos void *object, int selection, 245 1.1.1.2 christos const char *output_type, 246 1.1.1.2 christos const char *output_structure, 247 1.1.1.2 christos const char *pass, const char *pcipher) 248 1.1 christos { 249 1.1 christos EVP_PKEY *pkey = object; 250 1.1 christos OSSL_ENCODER_CTX *ectx = NULL; 251 1.1 christos BIO *mem_ser = NULL; 252 1.1 christos BUF_MEM *mem_buf = NULL; 253 1.1 christos const unsigned char *upass = (const unsigned char *)pass; 254 1.1 christos int ok = 0; 255 1.1 christos 256 1.1 christos if (!TEST_FL_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, 257 1.1.1.2 christos output_type, 258 1.1.1.2 christos output_structure, 259 1.1.1.2 christos testpropq)) 260 1.1 christos || !TEST_FL_int_gt(OSSL_ENCODER_CTX_get_num_encoders(ectx), 0) 261 1.1 christos || (pass != NULL 262 1.1 christos && !TEST_FL_true(OSSL_ENCODER_CTX_set_passphrase(ectx, upass, 263 1.1.1.2 christos strlen(pass)))) 264 1.1 christos || (pcipher != NULL 265 1.1 christos && !TEST_FL_true(OSSL_ENCODER_CTX_set_cipher(ectx, pcipher, NULL))) 266 1.1 christos || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) 267 1.1 christos || !TEST_FL_true(OSSL_ENCODER_to_bio(ectx, mem_ser)) 268 1.1 christos || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) 269 1.1 christos || !TEST_FL_ptr(*encoded = mem_buf->data) 270 1.1 christos || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) 271 1.1 christos goto end; 272 1.1 christos 273 1.1 christos /* Detach the encoded output */ 274 1.1 christos mem_buf->data = NULL; 275 1.1 christos mem_buf->length = 0; 276 1.1 christos ok = 1; 277 1.1.1.2 christos end: 278 1.1 christos BIO_free(mem_ser); 279 1.1 christos OSSL_ENCODER_CTX_free(ectx); 280 1.1 christos return ok; 281 1.1 christos } 282 1.1 christos 283 1.1 christos static int decode_EVP_PKEY_prov(const char *file, const int line, 284 1.1.1.2 christos void **object, void *encoded, long encoded_len, 285 1.1.1.2 christos const char *input_type, 286 1.1.1.2 christos const char *structure_type, 287 1.1.1.2 christos const char *keytype, int selection, 288 1.1.1.2 christos const char *pass) 289 1.1 christos { 290 1.1 christos EVP_PKEY *pkey = NULL, *testpkey = NULL; 291 1.1 christos OSSL_DECODER_CTX *dctx = NULL; 292 1.1 christos BIO *encoded_bio = NULL; 293 1.1 christos const unsigned char *upass = (const unsigned char *)pass; 294 1.1 christos int ok = 0; 295 1.1 christos int i; 296 1.1 christos const char *badtype; 297 1.1 christos 298 1.1 christos if (strcmp(input_type, "DER") == 0) 299 1.1 christos badtype = "PEM"; 300 1.1 christos else 301 1.1 christos badtype = "DER"; 302 1.1 christos 303 1.1 christos if (!TEST_FL_ptr(encoded_bio = BIO_new_mem_buf(encoded, encoded_len))) 304 1.1 christos goto end; 305 1.1 christos 306 1.1 christos /* 307 1.1 christos * We attempt the decode 3 times. The first time we provide the expected 308 1.1 christos * starting input type. The second time we provide NULL for the starting 309 1.1 christos * type. The third time we provide a bad starting input type. 310 1.1 christos * The bad starting input type should fail. The other two should succeed 311 1.1 christos * and produce the same result. 312 1.1 christos */ 313 1.1 christos for (i = 0; i < 3; i++) { 314 1.1 christos const char *testtype = (i == 0) ? input_type 315 1.1 christos : ((i == 1) ? NULL : badtype); 316 1.1 christos 317 1.1 christos if (!TEST_FL_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&testpkey, 318 1.1.1.2 christos testtype, 319 1.1.1.2 christos structure_type, 320 1.1.1.2 christos keytype, 321 1.1.1.2 christos selection, 322 1.1.1.2 christos testctx, testpropq)) 323 1.1 christos || (pass != NULL 324 1.1 christos && !OSSL_DECODER_CTX_set_passphrase(dctx, upass, strlen(pass))) 325 1.1 christos || !TEST_FL_int_gt(BIO_reset(encoded_bio), 0) 326 1.1.1.2 christos /* We expect to fail when using a bad input type */ 327 1.1 christos || !TEST_FL_int_eq(OSSL_DECODER_from_bio(dctx, encoded_bio), 328 1.1.1.2 christos (i == 2) ? 0 : 1)) 329 1.1 christos goto end; 330 1.1 christos OSSL_DECODER_CTX_free(dctx); 331 1.1 christos dctx = NULL; 332 1.1 christos 333 1.1 christos if (i == 0) { 334 1.1 christos pkey = testpkey; 335 1.1 christos testpkey = NULL; 336 1.1 christos } else if (i == 1) { 337 1.1 christos if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) { 338 1.1 christos if (!TEST_FL_int_eq(EVP_PKEY_parameters_eq(pkey, testpkey), 1)) 339 1.1 christos goto end; 340 1.1 christos } else { 341 1.1 christos if (!TEST_FL_int_eq(EVP_PKEY_eq(pkey, testpkey), 1)) 342 1.1 christos goto end; 343 1.1 christos } 344 1.1 christos } 345 1.1 christos } 346 1.1 christos ok = 1; 347 1.1 christos *object = pkey; 348 1.1 christos pkey = NULL; 349 1.1 christos 350 1.1.1.2 christos end: 351 1.1 christos EVP_PKEY_free(pkey); 352 1.1 christos EVP_PKEY_free(testpkey); 353 1.1 christos BIO_free(encoded_bio); 354 1.1 christos OSSL_DECODER_CTX_free(dctx); 355 1.1 christos return ok; 356 1.1 christos } 357 1.1 christos 358 1.1 christos static int encode_EVP_PKEY_legacy_PEM(const char *file, const int line, 359 1.1.1.2 christos void **encoded, long *encoded_len, 360 1.1.1.2 christos void *object, ossl_unused int selection, 361 1.1.1.2 christos ossl_unused const char *output_type, 362 1.1.1.2 christos ossl_unused const char *output_structure, 363 1.1.1.2 christos const char *pass, const char *pcipher) 364 1.1 christos { 365 1.1 christos EVP_PKEY *pkey = object; 366 1.1 christos EVP_CIPHER *cipher = NULL; 367 1.1 christos BIO *mem_ser = NULL; 368 1.1 christos BUF_MEM *mem_buf = NULL; 369 1.1 christos const unsigned char *upass = (const unsigned char *)pass; 370 1.1 christos size_t passlen = 0; 371 1.1 christos int ok = 0; 372 1.1 christos 373 1.1 christos if (pcipher != NULL && pass != NULL) { 374 1.1 christos passlen = strlen(pass); 375 1.1 christos if (!TEST_FL_ptr(cipher = EVP_CIPHER_fetch(testctx, pcipher, testpropq))) 376 1.1 christos goto end; 377 1.1 christos } 378 1.1 christos if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) 379 1.1 christos || !TEST_FL_true(PEM_write_bio_PrivateKey_traditional(mem_ser, pkey, 380 1.1.1.2 christos cipher, 381 1.1.1.2 christos upass, passlen, 382 1.1.1.2 christos NULL, NULL)) 383 1.1 christos || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) 384 1.1 christos || !TEST_FL_ptr(*encoded = mem_buf->data) 385 1.1 christos || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) 386 1.1 christos goto end; 387 1.1 christos 388 1.1 christos /* Detach the encoded output */ 389 1.1 christos mem_buf->data = NULL; 390 1.1 christos mem_buf->length = 0; 391 1.1 christos ok = 1; 392 1.1.1.2 christos end: 393 1.1 christos BIO_free(mem_ser); 394 1.1 christos EVP_CIPHER_free(cipher); 395 1.1 christos return ok; 396 1.1 christos } 397 1.1 christos 398 1.1 christos static int encode_EVP_PKEY_MSBLOB(const char *file, const int line, 399 1.1.1.2 christos void **encoded, long *encoded_len, 400 1.1.1.2 christos void *object, int selection, 401 1.1.1.2 christos ossl_unused const char *output_type, 402 1.1.1.2 christos ossl_unused const char *output_structure, 403 1.1.1.2 christos ossl_unused const char *pass, 404 1.1.1.2 christos ossl_unused const char *pcipher) 405 1.1 christos { 406 1.1 christos EVP_PKEY *pkey = object; 407 1.1 christos BIO *mem_ser = NULL; 408 1.1 christos BUF_MEM *mem_buf = NULL; 409 1.1 christos int ok = 0; 410 1.1 christos 411 1.1 christos if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem()))) 412 1.1 christos goto end; 413 1.1 christos 414 1.1 christos if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { 415 1.1 christos if (!TEST_FL_int_ge(i2b_PrivateKey_bio(mem_ser, pkey), 0)) 416 1.1 christos goto end; 417 1.1 christos } else { 418 1.1 christos if (!TEST_FL_int_ge(i2b_PublicKey_bio(mem_ser, pkey), 0)) 419 1.1 christos goto end; 420 1.1 christos } 421 1.1 christos 422 1.1 christos if (!TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) 423 1.1 christos || !TEST_FL_ptr(*encoded = mem_buf->data) 424 1.1 christos || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) 425 1.1 christos goto end; 426 1.1 christos 427 1.1 christos /* Detach the encoded output */ 428 1.1 christos mem_buf->data = NULL; 429 1.1 christos mem_buf->length = 0; 430 1.1 christos ok = 1; 431 1.1.1.2 christos end: 432 1.1 christos BIO_free(mem_ser); 433 1.1 christos return ok; 434 1.1 christos } 435 1.1 christos 436 1.1 christos static pem_password_cb pass_pw; 437 1.1 christos static int pass_pw(char *buf, int size, int rwflag, void *userdata) 438 1.1 christos { 439 1.1 christos OPENSSL_strlcpy(buf, userdata, size); 440 1.1 christos return strlen(userdata); 441 1.1 christos } 442 1.1 christos 443 1.1 christos static int encode_EVP_PKEY_PVK(const char *file, const int line, 444 1.1.1.2 christos void **encoded, long *encoded_len, 445 1.1.1.2 christos void *object, int selection, 446 1.1.1.2 christos ossl_unused const char *output_type, 447 1.1.1.2 christos ossl_unused const char *output_structure, 448 1.1.1.2 christos const char *pass, 449 1.1.1.2 christos ossl_unused const char *pcipher) 450 1.1 christos { 451 1.1 christos EVP_PKEY *pkey = object; 452 1.1 christos BIO *mem_ser = NULL; 453 1.1 christos BUF_MEM *mem_buf = NULL; 454 1.1 christos int enc = (pass != NULL); 455 1.1 christos int ok = 0; 456 1.1 christos 457 1.1 christos if (!TEST_FL_true(ossl_assert((selection 458 1.1.1.2 christos & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) 459 1.1.1.2 christos != 0)) 460 1.1 christos || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) 461 1.1 christos || !TEST_FL_int_ge(i2b_PVK_bio_ex(mem_ser, pkey, enc, 462 1.1.1.2 christos pass_pw, (void *)pass, testctx, testpropq), 463 1.1.1.2 christos 0) 464 1.1 christos || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) 465 1.1 christos || !TEST_FL_ptr(*encoded = mem_buf->data) 466 1.1 christos || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) 467 1.1 christos goto end; 468 1.1 christos 469 1.1 christos /* Detach the encoded output */ 470 1.1 christos mem_buf->data = NULL; 471 1.1 christos mem_buf->length = 0; 472 1.1 christos ok = 1; 473 1.1.1.2 christos end: 474 1.1 christos BIO_free(mem_ser); 475 1.1 christos return ok; 476 1.1 christos } 477 1.1 christos 478 1.1 christos static int test_text(const char *file, const int line, 479 1.1.1.2 christos const void *data1, size_t data1_len, 480 1.1.1.2 christos const void *data2, size_t data2_len) 481 1.1 christos { 482 1.1 christos return TEST_FL_strn2_eq(data1, data1_len, data2, data2_len); 483 1.1 christos } 484 1.1 christos 485 1.1 christos static int test_mem(const char *file, const int line, 486 1.1.1.2 christos const void *data1, size_t data1_len, 487 1.1.1.2 christos const void *data2, size_t data2_len) 488 1.1 christos { 489 1.1 christos return TEST_FL_mem_eq(data1, data1_len, data2, data2_len); 490 1.1 christos } 491 1.1 christos 492 1.1 christos /* Test cases and their dumpers / checkers */ 493 1.1 christos 494 1.1 christos static void collect_name(const char *name, void *arg) 495 1.1 christos { 496 1.1 christos char **namelist = arg; 497 1.1 christos char *new_namelist; 498 1.1 christos size_t space; 499 1.1 christos 500 1.1 christos space = strlen(name); 501 1.1 christos if (*namelist != NULL) 502 1.1 christos space += strlen(*namelist) + 2 /* for comma and space */; 503 1.1 christos space++; /* for terminating null byte */ 504 1.1 christos 505 1.1 christos new_namelist = OPENSSL_realloc(*namelist, space); 506 1.1 christos if (new_namelist == NULL) 507 1.1 christos return; 508 1.1 christos if (*namelist != NULL) { 509 1.1 christos strcat(new_namelist, ", "); 510 1.1 christos strcat(new_namelist, name); 511 1.1 christos } else { 512 1.1 christos strcpy(new_namelist, name); 513 1.1 christos } 514 1.1 christos *namelist = new_namelist; 515 1.1 christos } 516 1.1 christos 517 1.1 christos static void dump_der(const char *label, const void *data, size_t data_len) 518 1.1 christos { 519 1.1 christos test_output_memory(label, data, data_len); 520 1.1 christos } 521 1.1 christos 522 1.1 christos static void dump_pem(const char *label, const void *data, size_t data_len) 523 1.1 christos { 524 1.1 christos test_output_string(label, data, data_len - 1); 525 1.1 christos } 526 1.1 christos 527 1.1 christos static int check_unprotected_PKCS8_DER(const char *file, const int line, 528 1.1.1.2 christos const char *type, 529 1.1.1.2 christos const void *data, size_t data_len) 530 1.1 christos { 531 1.1 christos const unsigned char *datap = data; 532 1.1.1.2 christos PKCS8_PRIV_KEY_INFO *p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &datap, data_len); 533 1.1 christos int ok = 0; 534 1.1 christos 535 1.1 christos if (TEST_FL_ptr(p8inf)) { 536 1.1 christos EVP_PKEY *pkey = EVP_PKCS82PKEY_ex(p8inf, testctx, testpropq); 537 1.1 christos char *namelist = NULL; 538 1.1 christos 539 1.1 christos if (TEST_FL_ptr(pkey)) { 540 1.1 christos if (!(ok = TEST_FL_true(EVP_PKEY_is_a(pkey, type)))) { 541 1.1 christos EVP_PKEY_type_names_do_all(pkey, collect_name, &namelist); 542 1.1 christos if (namelist != NULL) 543 1.1 christos TEST_note("%s isn't any of %s", type, namelist); 544 1.1 christos OPENSSL_free(namelist); 545 1.1 christos } 546 1.1 christos ok = ok && TEST_FL_true(evp_pkey_is_provided(pkey)); 547 1.1 christos EVP_PKEY_free(pkey); 548 1.1 christos } 549 1.1 christos } 550 1.1 christos PKCS8_PRIV_KEY_INFO_free(p8inf); 551 1.1 christos return ok; 552 1.1 christos } 553 1.1 christos 554 1.1 christos static int test_unprotected_via_DER(const char *type, EVP_PKEY *key, int fips) 555 1.1 christos { 556 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 557 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 558 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, 559 1.1.1.2 christos "DER", "PrivateKeyInfo", NULL, NULL, 560 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 561 1.1.1.2 christos test_mem, check_unprotected_PKCS8_DER, 562 1.1.1.2 christos dump_der, fips ? 0 : FLAG_FAIL_IF_FIPS); 563 1.1 christos } 564 1.1 christos 565 1.1 christos static int check_unprotected_PKCS8_PEM(const char *file, const int line, 566 1.1.1.2 christos const char *type, 567 1.1.1.2 christos const void *data, size_t data_len) 568 1.1 christos { 569 1.1.1.2 christos static const char expected_pem_header[] = "-----BEGIN " PEM_STRING_PKCS8INF "-----"; 570 1.1 christos 571 1.1 christos return TEST_FL_strn_eq(data, expected_pem_header, 572 1.1.1.2 christos sizeof(expected_pem_header) - 1); 573 1.1 christos } 574 1.1 christos 575 1.1 christos static int test_unprotected_via_PEM(const char *type, EVP_PKEY *key, int fips) 576 1.1 christos { 577 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 578 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 579 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, 580 1.1.1.2 christos "PEM", "PrivateKeyInfo", NULL, NULL, 581 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 582 1.1.1.2 christos test_text, check_unprotected_PKCS8_PEM, 583 1.1.1.2 christos dump_pem, fips ? 0 : FLAG_FAIL_IF_FIPS); 584 1.1 christos } 585 1.1 christos 586 1.1 christos #ifndef OPENSSL_NO_KEYPARAMS 587 1.1 christos static int check_params_DER(const char *file, const int line, 588 1.1.1.2 christos const char *type, const void *data, size_t data_len) 589 1.1 christos { 590 1.1 christos const unsigned char *datap = data; 591 1.1 christos int ok = 0; 592 1.1 christos int itype = NID_undef; 593 1.1 christos EVP_PKEY *pkey = NULL; 594 1.1 christos 595 1.1 christos if (strcmp(type, "DH") == 0) 596 1.1 christos itype = EVP_PKEY_DH; 597 1.1 christos else if (strcmp(type, "X9.42 DH") == 0) 598 1.1 christos itype = EVP_PKEY_DHX; 599 1.1.1.2 christos else if (strcmp(type, "DSA") == 0) 600 1.1 christos itype = EVP_PKEY_DSA; 601 1.1.1.2 christos else if (strcmp(type, "EC") == 0) 602 1.1 christos itype = EVP_PKEY_EC; 603 1.1 christos 604 1.1 christos if (itype != NID_undef) { 605 1.1 christos pkey = d2i_KeyParams(itype, NULL, &datap, data_len); 606 1.1 christos ok = (pkey != NULL); 607 1.1 christos EVP_PKEY_free(pkey); 608 1.1 christos } 609 1.1 christos 610 1.1 christos return ok; 611 1.1 christos } 612 1.1 christos 613 1.1 christos static int check_params_PEM(const char *file, const int line, 614 1.1.1.2 christos const char *type, 615 1.1.1.2 christos const void *data, size_t data_len) 616 1.1 christos { 617 1.1 christos static char expected_pem_header[80]; 618 1.1 christos 619 1.1.1.2 christos return TEST_FL_int_gt(BIO_snprintf(expected_pem_header, 620 1.1.1.2 christos sizeof(expected_pem_header), 621 1.1.1.2 christos "-----BEGIN %s PARAMETERS-----", type), 622 1.1.1.2 christos 0) 623 1.1 christos && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)); 624 1.1 christos } 625 1.1 christos 626 1.1 christos static int test_params_via_DER(const char *type, EVP_PKEY *key) 627 1.1 christos { 628 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 629 1.1.1.2 christos "DER", "type-specific", NULL, NULL, 630 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 631 1.1.1.2 christos test_mem, check_params_DER, 632 1.1.1.2 christos dump_der, FLAG_DECODE_WITH_TYPE); 633 1.1 christos } 634 1.1 christos 635 1.1 christos static int test_params_via_PEM(const char *type, EVP_PKEY *key) 636 1.1 christos { 637 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 638 1.1.1.2 christos "PEM", "type-specific", NULL, NULL, 639 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 640 1.1.1.2 christos test_text, check_params_PEM, 641 1.1.1.2 christos dump_pem, 0); 642 1.1 christos } 643 1.1 christos #endif /* !OPENSSL_NO_KEYPARAMS */ 644 1.1 christos 645 1.1 christos static int check_unprotected_legacy_PEM(const char *file, const int line, 646 1.1.1.2 christos const char *type, 647 1.1.1.2 christos const void *data, size_t data_len) 648 1.1 christos { 649 1.1 christos static char expected_pem_header[80]; 650 1.1 christos 651 1.1.1.2 christos return TEST_FL_int_gt(BIO_snprintf(expected_pem_header, 652 1.1.1.2 christos sizeof(expected_pem_header), 653 1.1.1.2 christos "-----BEGIN %s PRIVATE KEY-----", type), 654 1.1.1.2 christos 0) 655 1.1 christos && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)); 656 1.1 christos } 657 1.1 christos 658 1.1 christos static int test_unprotected_via_legacy_PEM(const char *type, EVP_PKEY *key) 659 1.1 christos { 660 1.1 christos if (!default_libctx || is_fips) 661 1.1 christos return TEST_skip("Test not available if using a non-default library context or FIPS provider"); 662 1.1 christos 663 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 664 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 665 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 666 1.1.1.2 christos "PEM", "type-specific", NULL, NULL, 667 1.1.1.2 christos encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov, 668 1.1.1.2 christos test_text, check_unprotected_legacy_PEM, 669 1.1.1.2 christos dump_pem, 0); 670 1.1 christos } 671 1.1 christos 672 1.1 christos static int check_MSBLOB(const char *file, const int line, 673 1.1.1.2 christos const char *type, const void *data, size_t data_len) 674 1.1 christos { 675 1.1 christos const unsigned char *datap = data; 676 1.1 christos EVP_PKEY *pkey = b2i_PrivateKey(&datap, data_len); 677 1.1 christos int ok = TEST_FL_ptr(pkey); 678 1.1 christos 679 1.1 christos EVP_PKEY_free(pkey); 680 1.1 christos return ok; 681 1.1 christos } 682 1.1 christos 683 1.1 christos static int test_unprotected_via_MSBLOB(const char *type, EVP_PKEY *key) 684 1.1 christos { 685 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 686 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 687 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 688 1.1.1.2 christos "MSBLOB", NULL, NULL, NULL, 689 1.1.1.2 christos encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov, 690 1.1.1.2 christos test_mem, check_MSBLOB, 691 1.1.1.2 christos dump_der, 0); 692 1.1 christos } 693 1.1 christos 694 1.1 christos static int check_PVK(const char *file, const int line, 695 1.1.1.2 christos const char *type, const void *data, size_t data_len) 696 1.1 christos { 697 1.1 christos const unsigned char *in = data; 698 1.1 christos unsigned int saltlen = 0, keylen = 0; 699 1.1 christos int isdss = -1; 700 1.1 christos 701 1.1 christos return ossl_do_PVK_header(&in, data_len, 0, &isdss, &saltlen, &keylen); 702 1.1 christos } 703 1.1 christos 704 1.1 christos static int test_unprotected_via_PVK(const char *type, EVP_PKEY *key) 705 1.1 christos { 706 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 707 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 708 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 709 1.1.1.2 christos "PVK", NULL, NULL, NULL, 710 1.1.1.2 christos encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov, 711 1.1.1.2 christos test_mem, check_PVK, 712 1.1.1.2 christos dump_der, 0); 713 1.1 christos } 714 1.1 christos 715 1.1 christos static const char *pass_cipher = "AES-256-CBC"; 716 1.1 christos static const char *pass = "the holy handgrenade of antioch"; 717 1.1 christos 718 1.1 christos static int check_protected_PKCS8_DER(const char *file, const int line, 719 1.1.1.2 christos const char *type, 720 1.1.1.2 christos const void *data, size_t data_len) 721 1.1 christos { 722 1.1 christos const unsigned char *datap = data; 723 1.1 christos X509_SIG *p8 = d2i_X509_SIG(NULL, &datap, data_len); 724 1.1 christos int ok = TEST_FL_ptr(p8); 725 1.1 christos 726 1.1 christos X509_SIG_free(p8); 727 1.1 christos return ok; 728 1.1 christos } 729 1.1 christos 730 1.1 christos static int test_protected_via_DER(const char *type, EVP_PKEY *key, int fips) 731 1.1 christos { 732 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 733 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 734 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 735 1.1.1.2 christos "DER", "EncryptedPrivateKeyInfo", 736 1.1.1.2 christos pass, pass_cipher, 737 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 738 1.1.1.2 christos test_mem, check_protected_PKCS8_DER, 739 1.1.1.2 christos dump_der, fips ? 0 : FLAG_FAIL_IF_FIPS); 740 1.1 christos } 741 1.1 christos 742 1.1 christos static int check_protected_PKCS8_PEM(const char *file, const int line, 743 1.1.1.2 christos const char *type, 744 1.1.1.2 christos const void *data, size_t data_len) 745 1.1 christos { 746 1.1.1.2 christos static const char expected_pem_header[] = "-----BEGIN " PEM_STRING_PKCS8 "-----"; 747 1.1 christos 748 1.1 christos return TEST_FL_strn_eq(data, expected_pem_header, 749 1.1.1.2 christos sizeof(expected_pem_header) - 1); 750 1.1 christos } 751 1.1 christos 752 1.1 christos static int test_protected_via_PEM(const char *type, EVP_PKEY *key, int fips) 753 1.1 christos { 754 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 755 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 756 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 757 1.1.1.2 christos "PEM", "EncryptedPrivateKeyInfo", 758 1.1.1.2 christos pass, pass_cipher, 759 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 760 1.1.1.2 christos test_text, check_protected_PKCS8_PEM, 761 1.1.1.2 christos dump_pem, fips ? 0 : FLAG_FAIL_IF_FIPS); 762 1.1 christos } 763 1.1 christos 764 1.1 christos static int check_protected_legacy_PEM(const char *file, const int line, 765 1.1.1.2 christos const char *type, 766 1.1.1.2 christos const void *data, size_t data_len) 767 1.1 christos { 768 1.1 christos static char expected_pem_header[80]; 769 1.1 christos 770 1.1.1.2 christos return TEST_FL_int_gt(BIO_snprintf(expected_pem_header, 771 1.1.1.2 christos sizeof(expected_pem_header), 772 1.1.1.2 christos "-----BEGIN %s PRIVATE KEY-----", type), 773 1.1.1.2 christos 0) 774 1.1 christos && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)) 775 1.1 christos && TEST_FL_ptr(strstr(data, "\nDEK-Info: ")); 776 1.1 christos } 777 1.1 christos 778 1.1 christos static int test_protected_via_legacy_PEM(const char *type, EVP_PKEY *key) 779 1.1 christos { 780 1.1 christos if (!default_libctx || is_fips) 781 1.1 christos return TEST_skip("Test not available if using a non-default library context or FIPS provider"); 782 1.1 christos 783 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 784 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 785 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 786 1.1.1.2 christos "PEM", "type-specific", pass, pass_cipher, 787 1.1.1.2 christos encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov, 788 1.1.1.2 christos test_text, check_protected_legacy_PEM, 789 1.1.1.2 christos dump_pem, 0); 790 1.1 christos } 791 1.1 christos 792 1.1 christos #ifndef OPENSSL_NO_RC4 793 1.1 christos static int test_protected_via_PVK(const char *type, EVP_PKEY *key) 794 1.1 christos { 795 1.1 christos int ret = 0; 796 1.1 christos OSSL_PROVIDER *lgcyprov = OSSL_PROVIDER_load(testctx, "legacy"); 797 1.1 christos if (lgcyprov == NULL) 798 1.1 christos return TEST_skip("Legacy provider not available"); 799 1.1 christos 800 1.1 christos ret = test_encode_decode(__FILE__, __LINE__, type, key, 801 1.1.1.2 christos OSSL_KEYMGMT_SELECT_KEYPAIR 802 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 803 1.1.1.2 christos "PVK", NULL, pass, NULL, 804 1.1.1.2 christos encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov, 805 1.1.1.2 christos test_mem, check_PVK, dump_der, 0); 806 1.1 christos OSSL_PROVIDER_unload(lgcyprov); 807 1.1 christos return ret; 808 1.1 christos } 809 1.1 christos #endif 810 1.1 christos 811 1.1 christos static int check_public_DER(const char *file, const int line, 812 1.1.1.2 christos const char *type, const void *data, size_t data_len) 813 1.1 christos { 814 1.1 christos const unsigned char *datap = data; 815 1.1 christos EVP_PKEY *pkey = d2i_PUBKEY_ex(NULL, &datap, data_len, testctx, testpropq); 816 1.1 christos int ok = (TEST_FL_ptr(pkey) && TEST_FL_true(EVP_PKEY_is_a(pkey, type))); 817 1.1 christos 818 1.1 christos EVP_PKEY_free(pkey); 819 1.1 christos return ok; 820 1.1 christos } 821 1.1 christos 822 1.1 christos static int test_public_via_DER(const char *type, EVP_PKEY *key, int fips) 823 1.1 christos { 824 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 825 1.1.1.2 christos OSSL_KEYMGMT_SELECT_PUBLIC_KEY 826 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, 827 1.1.1.2 christos "DER", "SubjectPublicKeyInfo", NULL, NULL, 828 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 829 1.1.1.2 christos test_mem, check_public_DER, dump_der, 830 1.1.1.2 christos fips ? 0 : FLAG_FAIL_IF_FIPS); 831 1.1 christos } 832 1.1 christos 833 1.1 christos static int check_public_PEM(const char *file, const int line, 834 1.1.1.2 christos const char *type, const void *data, size_t data_len) 835 1.1 christos { 836 1.1.1.2 christos static const char expected_pem_header[] = "-----BEGIN " PEM_STRING_PUBLIC "-----"; 837 1.1 christos 838 1.1.1.2 christos return TEST_FL_strn_eq(data, expected_pem_header, 839 1.1.1.2 christos sizeof(expected_pem_header) - 1); 840 1.1 christos } 841 1.1 christos 842 1.1 christos static int test_public_via_PEM(const char *type, EVP_PKEY *key, int fips) 843 1.1 christos { 844 1.1 christos return test_encode_decode(__FILE__, __LINE__, type, key, 845 1.1.1.2 christos OSSL_KEYMGMT_SELECT_PUBLIC_KEY 846 1.1.1.2 christos | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, 847 1.1.1.2 christos "PEM", "SubjectPublicKeyInfo", NULL, NULL, 848 1.1.1.2 christos encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, 849 1.1.1.2 christos test_text, check_public_PEM, dump_pem, 850 1.1.1.2 christos fips ? 0 : FLAG_FAIL_IF_FIPS); 851 1.1 christos } 852 1.1 christos 853 1.1 christos static int check_public_MSBLOB(const char *file, const int line, 854 1.1.1.2 christos const char *type, 855 1.1.1.2 christos const void *data, size_t data_len) 856 1.1 christos { 857 1.1 christos const unsigned char *datap = data; 858 1.1 christos EVP_PKEY *pkey = b2i_PublicKey(&datap, data_len); 859 1.1 christos int ok = TEST_FL_ptr(pkey); 860 1.1 christos 861 1.1 christos EVP_PKEY_free(pkey); 862 1.1 christos return ok; 863 1.1 christos } 864 1.1 christos 865 1.1 christos static int test_public_via_MSBLOB(const char *type, EVP_PKEY *key) 866 1.1 christos { 867 1.1.1.2 christos return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_PUBLIC_KEY | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, 868 1.1.1.2 christos "MSBLOB", NULL, NULL, NULL, 869 1.1.1.2 christos encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov, 870 1.1.1.2 christos test_mem, check_public_MSBLOB, dump_der, 0); 871 1.1 christos } 872 1.1 christos 873 1.1.1.2 christos #define KEYS(KEYTYPE) \ 874 1.1 christos static EVP_PKEY *key_##KEYTYPE = NULL 875 1.1.1.2 christos #define MAKE_KEYS(KEYTYPE, KEYTYPEstr, params) \ 876 1.1.1.2 christos ok = ok \ 877 1.1 christos && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, NULL, params)) 878 1.1.1.2 christos #define FREE_KEYS(KEYTYPE) \ 879 1.1.1.2 christos EVP_PKEY_free(key_##KEYTYPE); 880 1.1 christos 881 1.1 christos #define DOMAIN_KEYS(KEYTYPE) \ 882 1.1 christos static EVP_PKEY *template_##KEYTYPE = NULL; \ 883 1.1 christos static EVP_PKEY *key_##KEYTYPE = NULL 884 1.1.1.2 christos #define MAKE_DOMAIN_KEYS(KEYTYPE, KEYTYPEstr, params) \ 885 1.1.1.2 christos ok = ok \ 886 1.1.1.2 christos && TEST_ptr(template_##KEYTYPE = make_template(KEYTYPEstr, params)) \ 887 1.1.1.2 christos && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, template_##KEYTYPE, NULL)) 888 1.1.1.2 christos #define FREE_DOMAIN_KEYS(KEYTYPE) \ 889 1.1.1.2 christos EVP_PKEY_free(template_##KEYTYPE); \ 890 1.1 christos EVP_PKEY_free(key_##KEYTYPE) 891 1.1 christos 892 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE(KEYTYPE, KEYTYPEstr, fips) \ 893 1.1.1.2 christos static int test_unprotected_##KEYTYPE##_via_DER(void) \ 894 1.1.1.2 christos { \ 895 1.1 christos return test_unprotected_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \ 896 1.1.1.2 christos } \ 897 1.1.1.2 christos static int test_unprotected_##KEYTYPE##_via_PEM(void) \ 898 1.1.1.2 christos { \ 899 1.1 christos return test_unprotected_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \ 900 1.1.1.2 christos } \ 901 1.1.1.2 christos static int test_protected_##KEYTYPE##_via_DER(void) \ 902 1.1.1.2 christos { \ 903 1.1.1.2 christos return test_protected_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \ 904 1.1.1.2 christos } \ 905 1.1.1.2 christos static int test_protected_##KEYTYPE##_via_PEM(void) \ 906 1.1.1.2 christos { \ 907 1.1.1.2 christos return test_protected_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \ 908 1.1.1.2 christos } \ 909 1.1.1.2 christos static int test_public_##KEYTYPE##_via_DER(void) \ 910 1.1.1.2 christos { \ 911 1.1.1.2 christos return test_public_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \ 912 1.1.1.2 christos } \ 913 1.1.1.2 christos static int test_public_##KEYTYPE##_via_PEM(void) \ 914 1.1.1.2 christos { \ 915 1.1.1.2 christos return test_public_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \ 916 1.1.1.2 christos } 917 1.1.1.2 christos 918 1.1.1.2 christos #define ADD_TEST_SUITE(KEYTYPE) \ 919 1.1.1.2 christos ADD_TEST(test_unprotected_##KEYTYPE##_via_DER); \ 920 1.1.1.2 christos ADD_TEST(test_unprotected_##KEYTYPE##_via_PEM); \ 921 1.1.1.2 christos ADD_TEST(test_protected_##KEYTYPE##_via_DER); \ 922 1.1.1.2 christos ADD_TEST(test_protected_##KEYTYPE##_via_PEM); \ 923 1.1.1.2 christos ADD_TEST(test_public_##KEYTYPE##_via_DER); \ 924 1.1 christos ADD_TEST(test_public_##KEYTYPE##_via_PEM) 925 1.1 christos 926 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE_PARAMS(KEYTYPE, KEYTYPEstr) \ 927 1.1.1.2 christos static int test_params_##KEYTYPE##_via_DER(void) \ 928 1.1.1.2 christos { \ 929 1.1.1.2 christos return test_params_via_DER(KEYTYPEstr, key_##KEYTYPE); \ 930 1.1.1.2 christos } \ 931 1.1.1.2 christos static int test_params_##KEYTYPE##_via_PEM(void) \ 932 1.1.1.2 christos { \ 933 1.1.1.2 christos return test_params_via_PEM(KEYTYPEstr, key_##KEYTYPE); \ 934 1.1 christos } 935 1.1 christos 936 1.1.1.2 christos #define ADD_TEST_SUITE_PARAMS(KEYTYPE) \ 937 1.1.1.2 christos ADD_TEST(test_params_##KEYTYPE##_via_DER); \ 938 1.1 christos ADD_TEST(test_params_##KEYTYPE##_via_PEM) 939 1.1 christos 940 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE_LEGACY(KEYTYPE, KEYTYPEstr) \ 941 1.1.1.2 christos static int test_unprotected_##KEYTYPE##_via_legacy_PEM(void) \ 942 1.1.1.2 christos { \ 943 1.1.1.2 christos return test_unprotected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \ 944 1.1.1.2 christos } \ 945 1.1.1.2 christos static int test_protected_##KEYTYPE##_via_legacy_PEM(void) \ 946 1.1.1.2 christos { \ 947 1.1.1.2 christos return test_protected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \ 948 1.1 christos } 949 1.1 christos 950 1.1.1.2 christos #define ADD_TEST_SUITE_LEGACY(KEYTYPE) \ 951 1.1.1.2 christos ADD_TEST(test_unprotected_##KEYTYPE##_via_legacy_PEM); \ 952 1.1 christos ADD_TEST(test_protected_##KEYTYPE##_via_legacy_PEM) 953 1.1 christos 954 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE_MSBLOB(KEYTYPE, KEYTYPEstr) \ 955 1.1.1.2 christos static int test_unprotected_##KEYTYPE##_via_MSBLOB(void) \ 956 1.1.1.2 christos { \ 957 1.1.1.2 christos return test_unprotected_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \ 958 1.1.1.2 christos } \ 959 1.1.1.2 christos static int test_public_##KEYTYPE##_via_MSBLOB(void) \ 960 1.1.1.2 christos { \ 961 1.1.1.2 christos return test_public_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \ 962 1.1 christos } 963 1.1 christos 964 1.1.1.2 christos #define ADD_TEST_SUITE_MSBLOB(KEYTYPE) \ 965 1.1.1.2 christos ADD_TEST(test_unprotected_##KEYTYPE##_via_MSBLOB); \ 966 1.1 christos ADD_TEST(test_public_##KEYTYPE##_via_MSBLOB) 967 1.1 christos 968 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE, KEYTYPEstr) \ 969 1.1.1.2 christos static int test_unprotected_##KEYTYPE##_via_PVK(void) \ 970 1.1.1.2 christos { \ 971 1.1.1.2 christos return test_unprotected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \ 972 1.1 christos } 973 1.1.1.2 christos #define ADD_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE) \ 974 1.1 christos ADD_TEST(test_unprotected_##KEYTYPE##_via_PVK) 975 1.1 christos #ifndef OPENSSL_NO_RC4 976 1.1.1.2 christos #define IMPLEMENT_TEST_SUITE_PROTECTED_PVK(KEYTYPE, KEYTYPEstr) \ 977 1.1.1.2 christos static int test_protected_##KEYTYPE##_via_PVK(void) \ 978 1.1.1.2 christos { \ 979 1.1.1.2 christos return test_protected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \ 980 1.1 christos } 981 1.1.1.2 christos #define ADD_TEST_SUITE_PROTECTED_PVK(KEYTYPE) \ 982 1.1 christos ADD_TEST(test_protected_##KEYTYPE##_via_PVK) 983 1.1 christos #endif 984 1.1 christos 985 1.1 christos #ifndef OPENSSL_NO_DH 986 1.1 christos DOMAIN_KEYS(DH); 987 1.1 christos IMPLEMENT_TEST_SUITE(DH, "DH", 1) 988 1.1 christos IMPLEMENT_TEST_SUITE_PARAMS(DH, "DH") 989 1.1 christos DOMAIN_KEYS(DHX); 990 1.1 christos IMPLEMENT_TEST_SUITE(DHX, "X9.42 DH", 1) 991 1.1 christos IMPLEMENT_TEST_SUITE_PARAMS(DHX, "X9.42 DH") 992 1.1 christos /* 993 1.1 christos * DH has no support for PEM_write_bio_PrivateKey_traditional(), 994 1.1 christos * so no legacy tests. 995 1.1 christos */ 996 1.1 christos #endif 997 1.1 christos #ifndef OPENSSL_NO_DSA 998 1.1 christos DOMAIN_KEYS(DSA); 999 1.1 christos IMPLEMENT_TEST_SUITE(DSA, "DSA", 1) 1000 1.1 christos IMPLEMENT_TEST_SUITE_PARAMS(DSA, "DSA") 1001 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(DSA, "DSA") 1002 1.1 christos IMPLEMENT_TEST_SUITE_MSBLOB(DSA, "DSA") 1003 1.1 christos IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(DSA, "DSA") 1004 1.1.1.2 christos #ifndef OPENSSL_NO_RC4 1005 1.1 christos IMPLEMENT_TEST_SUITE_PROTECTED_PVK(DSA, "DSA") 1006 1.1.1.2 christos #endif 1007 1.1 christos #endif 1008 1.1 christos #ifndef OPENSSL_NO_EC 1009 1.1 christos DOMAIN_KEYS(EC); 1010 1.1 christos IMPLEMENT_TEST_SUITE(EC, "EC", 1) 1011 1.1 christos IMPLEMENT_TEST_SUITE_PARAMS(EC, "EC") 1012 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(EC, "EC") 1013 1.1 christos DOMAIN_KEYS(ECExplicitPrimeNamedCurve); 1014 1.1 christos IMPLEMENT_TEST_SUITE(ECExplicitPrimeNamedCurve, "EC", 1) 1015 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve, "EC") 1016 1.1 christos DOMAIN_KEYS(ECExplicitPrime2G); 1017 1.1 christos IMPLEMENT_TEST_SUITE(ECExplicitPrime2G, "EC", 0) 1018 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrime2G, "EC") 1019 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1020 1.1 christos DOMAIN_KEYS(ECExplicitTriNamedCurve); 1021 1.1 christos IMPLEMENT_TEST_SUITE(ECExplicitTriNamedCurve, "EC", 1) 1022 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve, "EC") 1023 1.1 christos DOMAIN_KEYS(ECExplicitTri2G); 1024 1.1 christos IMPLEMENT_TEST_SUITE(ECExplicitTri2G, "EC", 0) 1025 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTri2G, "EC") 1026 1.1.1.2 christos #endif 1027 1.1.1.2 christos #ifndef OPENSSL_NO_SM2 1028 1.1 christos KEYS(SM2); 1029 1.1 christos IMPLEMENT_TEST_SUITE(SM2, "SM2", 0) 1030 1.1.1.2 christos #endif 1031 1.1 christos #endif 1032 1.1 christos #ifndef OPENSSL_NO_ECX 1033 1.1 christos /* 1034 1.1 christos * ED25519, ED448, X25519 and X448 have no support for 1035 1.1 christos * PEM_write_bio_PrivateKey_traditional(), so no legacy tests. 1036 1.1 christos */ 1037 1.1 christos KEYS(ED25519); 1038 1.1 christos IMPLEMENT_TEST_SUITE(ED25519, "ED25519", 1) 1039 1.1 christos KEYS(ED448); 1040 1.1 christos IMPLEMENT_TEST_SUITE(ED448, "ED448", 1) 1041 1.1 christos KEYS(X25519); 1042 1.1 christos IMPLEMENT_TEST_SUITE(X25519, "X25519", 1) 1043 1.1 christos KEYS(X448); 1044 1.1 christos IMPLEMENT_TEST_SUITE(X448, "X448", 1) 1045 1.1 christos #endif 1046 1.1 christos #ifndef OPENSSL_NO_ML_KEM 1047 1.1 christos /* 1048 1.1 christos * ML-KEM has no support for PEM_write_bio_PrivateKey_traditional(), so no 1049 1.1 christos * legacy tests. 1050 1.1 christos */ 1051 1.1 christos KEYS(ML_KEM_512); 1052 1.1 christos IMPLEMENT_TEST_SUITE(ML_KEM_512, "ML-KEM-512", 1) 1053 1.1 christos KEYS(ML_KEM_768); 1054 1.1 christos IMPLEMENT_TEST_SUITE(ML_KEM_768, "ML-KEM-768", 1) 1055 1.1 christos KEYS(ML_KEM_1024); 1056 1.1 christos IMPLEMENT_TEST_SUITE(ML_KEM_1024, "ML-KEM-1024", 1) 1057 1.1 christos #endif 1058 1.1 christos #ifndef OPENSSL_NO_SLH_DSA 1059 1.1 christos KEYS(SLH_DSA_SHA2_128s); 1060 1.1 christos KEYS(SLH_DSA_SHA2_128f); 1061 1.1 christos KEYS(SLH_DSA_SHA2_192s); 1062 1.1 christos KEYS(SLH_DSA_SHA2_192f); 1063 1.1 christos KEYS(SLH_DSA_SHA2_256s); 1064 1.1 christos KEYS(SLH_DSA_SHA2_256f); 1065 1.1 christos KEYS(SLH_DSA_SHAKE_128s); 1066 1.1 christos KEYS(SLH_DSA_SHAKE_128f); 1067 1.1 christos KEYS(SLH_DSA_SHAKE_192s); 1068 1.1 christos KEYS(SLH_DSA_SHAKE_192f); 1069 1.1 christos KEYS(SLH_DSA_SHAKE_256s); 1070 1.1 christos KEYS(SLH_DSA_SHAKE_256f); 1071 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_128s, "SLH-DSA-SHA2-128s", 1) 1072 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_128f, "SLH-DSA-SHA2-128f", 1) 1073 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_192s, "SLH-DSA-SHA2-192s", 1) 1074 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_192f, "SLH-DSA-SHA2-192f", 1) 1075 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_256s, "SLH-DSA-SHA2-256s", 1) 1076 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHA2_256f, "SLH-DSA-SHA2-256f", 1) 1077 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_128s, "SLH-DSA-SHAKE-128s", 1) 1078 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_128f, "SLH-DSA-SHAKE-128f", 1) 1079 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_192s, "SLH-DSA-SHAKE-192s", 1) 1080 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_192f, "SLH-DSA-SHAKE-192f", 1) 1081 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_256s, "SLH-DSA-SHAKE-256s", 1) 1082 1.1 christos IMPLEMENT_TEST_SUITE(SLH_DSA_SHAKE_256f, "SLH-DSA-SHAKE-256f", 1) 1083 1.1 christos #endif /* OPENSSL_NO_SLH_DSA */ 1084 1.1 christos KEYS(RSA); 1085 1.1 christos IMPLEMENT_TEST_SUITE(RSA, "RSA", 1) 1086 1.1 christos IMPLEMENT_TEST_SUITE_LEGACY(RSA, "RSA") 1087 1.1 christos KEYS(RSA_PSS); 1088 1.1 christos IMPLEMENT_TEST_SUITE(RSA_PSS, "RSA-PSS", 1) 1089 1.1 christos /* 1090 1.1 christos * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(), 1091 1.1 christos * so no legacy tests. 1092 1.1 christos */ 1093 1.1 christos IMPLEMENT_TEST_SUITE_MSBLOB(RSA, "RSA") 1094 1.1 christos IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(RSA, "RSA") 1095 1.1 christos #ifndef OPENSSL_NO_RC4 1096 1.1 christos IMPLEMENT_TEST_SUITE_PROTECTED_PVK(RSA, "RSA") 1097 1.1 christos #endif 1098 1.1 christos 1099 1.1 christos #ifndef OPENSSL_NO_ML_DSA 1100 1.1 christos KEYS(ML_DSA_44); 1101 1.1 christos KEYS(ML_DSA_65); 1102 1.1 christos KEYS(ML_DSA_87); 1103 1.1 christos IMPLEMENT_TEST_SUITE(ML_DSA_44, "ML-DSA-44", 1) 1104 1.1 christos IMPLEMENT_TEST_SUITE(ML_DSA_65, "ML-DSA-65", 1) 1105 1.1 christos IMPLEMENT_TEST_SUITE(ML_DSA_87, "ML-DSA-87", 1) 1106 1.1 christos #endif /* OPENSSL_NO_ML_DSA */ 1107 1.1 christos 1108 1.1 christos #ifndef OPENSSL_NO_EC 1109 1.1 christos /* Explicit parameters that match a named curve */ 1110 1.1 christos static int do_create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld, 1111 1.1.1.2 christos const unsigned char *gen, 1112 1.1.1.2 christos size_t gen_len) 1113 1.1 christos { 1114 1.1 christos BIGNUM *a, *b, *prime, *order; 1115 1.1 christos 1116 1.1 christos /* Curve prime256v1 */ 1117 1.1 christos static const unsigned char prime_data[] = { 1118 1.1 christos 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 1119 1.1 christos 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1120 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 1121 1.1 christos 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 1122 1.1 christos 0xff 1123 1.1 christos }; 1124 1.1 christos static const unsigned char a_data[] = { 1125 1.1 christos 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 1126 1.1 christos 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1127 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 1128 1.1 christos 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 1129 1.1 christos 0xfc 1130 1.1 christos }; 1131 1.1 christos static const unsigned char b_data[] = { 1132 1.1 christos 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 1133 1.1 christos 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 1134 1.1 christos 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6, 1135 1.1 christos 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b 1136 1.1 christos }; 1137 1.1 christos static const unsigned char seed[] = { 1138 1.1 christos 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 1139 1.1 christos 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, 0x26, 0xb7, 1140 1.1 christos 0x81, 0x9f, 0x7e, 0x90 1141 1.1 christos }; 1142 1.1 christos static const unsigned char order_data[] = { 1143 1.1 christos 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 1144 1.1 christos 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 1145 1.1 christos 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 1146 1.1 christos 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51 1147 1.1 christos }; 1148 1.1 christos return TEST_ptr(a = BN_CTX_get(bnctx)) 1149 1.1.1.2 christos && TEST_ptr(b = BN_CTX_get(bnctx)) 1150 1.1.1.2 christos && TEST_ptr(prime = BN_CTX_get(bnctx)) 1151 1.1.1.2 christos && TEST_ptr(order = BN_CTX_get(bnctx)) 1152 1.1.1.2 christos && TEST_ptr(BN_bin2bn(prime_data, sizeof(prime_data), prime)) 1153 1.1.1.2 christos && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a)) 1154 1.1.1.2 christos && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b)) 1155 1.1.1.2 christos && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order)) 1156 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, 1157 1.1.1.2 christos OSSL_PKEY_PARAM_EC_FIELD_TYPE, SN_X9_62_prime_field, 1158 1.1.1.2 christos 0)) 1159 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, prime)) 1160 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) 1161 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)) 1162 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, 1163 1.1.1.2 christos OSSL_PKEY_PARAM_EC_ORDER, order)) 1164 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, 1165 1.1.1.2 christos OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len)) 1166 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, 1167 1.1.1.2 christos OSSL_PKEY_PARAM_EC_SEED, seed, sizeof(seed))) 1168 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, 1169 1.1.1.2 christos BN_value_one())); 1170 1.1 christos } 1171 1.1 christos 1172 1.1 christos static int create_ec_explicit_prime_params_namedcurve(OSSL_PARAM_BLD *bld) 1173 1.1 christos { 1174 1.1 christos static const unsigned char prime256v1_gen[] = { 1175 1.1 christos 0x04, 1176 1.1 christos 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 1177 1.1 christos 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 1178 1.1 christos 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 1179 1.1 christos 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 1180 1.1 christos 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 1181 1.1 christos 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 1182 1.1 christos 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 1183 1.1 christos 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 1184 1.1 christos }; 1185 1.1 christos return do_create_ec_explicit_prime_params(bld, prime256v1_gen, 1186 1.1.1.2 christos sizeof(prime256v1_gen)); 1187 1.1 christos } 1188 1.1 christos 1189 1.1 christos static int create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld) 1190 1.1 christos { 1191 1.1 christos /* 2G */ 1192 1.1 christos static const unsigned char prime256v1_gen2[] = { 1193 1.1 christos 0x04, 1194 1.1 christos 0xe4, 0x97, 0x08, 0xbe, 0x7d, 0xfa, 0xa2, 0x9a, 1195 1.1 christos 0xa3, 0x12, 0x6f, 0xe4, 0xe7, 0xd0, 0x25, 0xe3, 1196 1.1 christos 0x4a, 0xc1, 0x03, 0x15, 0x8c, 0xd9, 0x33, 0xc6, 1197 1.1 christos 0x97, 0x42, 0xf5, 0xdc, 0x97, 0xb9, 0xd7, 0x31, 1198 1.1 christos 0xe9, 0x7d, 0x74, 0x3d, 0x67, 0x6a, 0x3b, 0x21, 1199 1.1 christos 0x08, 0x9c, 0x31, 0x73, 0xf8, 0xc1, 0x27, 0xc9, 1200 1.1 christos 0xd2, 0xa0, 0xa0, 0x83, 0x66, 0xe0, 0xc9, 0xda, 1201 1.1 christos 0xa8, 0xc6, 0x56, 0x2b, 0x94, 0xb1, 0xae, 0x55 1202 1.1 christos }; 1203 1.1 christos return do_create_ec_explicit_prime_params(bld, prime256v1_gen2, 1204 1.1.1.2 christos sizeof(prime256v1_gen2)); 1205 1.1 christos } 1206 1.1 christos 1207 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1208 1.1 christos static int do_create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld, 1209 1.1.1.2 christos const unsigned char *gen, 1210 1.1.1.2 christos size_t gen_len) 1211 1.1 christos { 1212 1.1 christos BIGNUM *a, *b, *poly, *order, *cofactor; 1213 1.1 christos /* sect233k1 characteristic-two-field tpBasis */ 1214 1.1 christos static const unsigned char poly_data[] = { 1215 1.1.1.2 christos 0x02, 1216 1.1.1.2 christos 0x00, 1217 1.1.1.2 christos 0x00, 1218 1.1.1.2 christos 0x00, 1219 1.1.1.2 christos 0x00, 1220 1.1.1.2 christos 0x00, 1221 1.1.1.2 christos 0x00, 1222 1.1.1.2 christos 0x00, 1223 1.1.1.2 christos 0x00, 1224 1.1.1.2 christos 0x00, 1225 1.1.1.2 christos 0x00, 1226 1.1.1.2 christos 0x00, 1227 1.1.1.2 christos 0x00, 1228 1.1.1.2 christos 0x00, 1229 1.1.1.2 christos 0x00, 1230 1.1.1.2 christos 0x00, 1231 1.1.1.2 christos 0x00, 1232 1.1.1.2 christos 0x00, 1233 1.1.1.2 christos 0x00, 1234 1.1.1.2 christos 0x00, 1235 1.1.1.2 christos 0x04, 1236 1.1.1.2 christos 0x00, 1237 1.1.1.2 christos 0x00, 1238 1.1.1.2 christos 0x00, 1239 1.1.1.2 christos 0x00, 1240 1.1.1.2 christos 0x00, 1241 1.1.1.2 christos 0x00, 1242 1.1.1.2 christos 0x00, 1243 1.1.1.2 christos 0x00, 1244 1.1.1.2 christos 0x01, 1245 1.1 christos }; 1246 1.1 christos static const unsigned char a_data[] = { 1247 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1248 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1249 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 1250 1.1 christos }; 1251 1.1 christos static const unsigned char b_data[] = { 1252 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1253 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1254 1.1 christos 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 1255 1.1 christos }; 1256 1.1 christos static const unsigned char order_data[] = { 1257 1.1 christos 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1258 1.1 christos 0x00, 0x00, 0x00, 0x06, 0x9D, 0x5B, 0xB9, 0x15, 0xBC, 0xD4, 0x6E, 0xFB, 1259 1.1 christos 0x1A, 0xD5, 0xF1, 0x73, 0xAB, 0xDF 1260 1.1 christos }; 1261 1.1.1.2 christos static const unsigned char cofactor_data[] = { 1262 1.1 christos 0x4 1263 1.1 christos }; 1264 1.1 christos return TEST_ptr(a = BN_CTX_get(bnctx)) 1265 1.1.1.2 christos && TEST_ptr(b = BN_CTX_get(bnctx)) 1266 1.1.1.2 christos && TEST_ptr(poly = BN_CTX_get(bnctx)) 1267 1.1.1.2 christos && TEST_ptr(order = BN_CTX_get(bnctx)) 1268 1.1.1.2 christos && TEST_ptr(cofactor = BN_CTX_get(bnctx)) 1269 1.1.1.2 christos && TEST_ptr(BN_bin2bn(poly_data, sizeof(poly_data), poly)) 1270 1.1.1.2 christos && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a)) 1271 1.1.1.2 christos && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b)) 1272 1.1.1.2 christos && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order)) 1273 1.1.1.2 christos && TEST_ptr(BN_bin2bn(cofactor_data, sizeof(cofactor_data), cofactor)) 1274 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, 1275 1.1.1.2 christos OSSL_PKEY_PARAM_EC_FIELD_TYPE, 1276 1.1.1.2 christos SN_X9_62_characteristic_two_field, 0)) 1277 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, poly)) 1278 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) 1279 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)) 1280 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, 1281 1.1.1.2 christos OSSL_PKEY_PARAM_EC_ORDER, order)) 1282 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, 1283 1.1.1.2 christos OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len)) 1284 1.1.1.2 christos && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, 1285 1.1.1.2 christos cofactor)); 1286 1.1 christos } 1287 1.1 christos 1288 1.1 christos static int create_ec_explicit_trinomial_params_namedcurve(OSSL_PARAM_BLD *bld) 1289 1.1 christos { 1290 1.1 christos static const unsigned char gen[] = { 1291 1.1 christos 0x04, 1292 1.1 christos 0x01, 0x72, 0x32, 0xBA, 0x85, 0x3A, 0x7E, 0x73, 0x1A, 0xF1, 0x29, 0xF2, 1293 1.1 christos 0x2F, 0xF4, 0x14, 0x95, 0x63, 0xA4, 0x19, 0xC2, 0x6B, 0xF5, 0x0A, 0x4C, 1294 1.1 christos 0x9D, 0x6E, 0xEF, 0xAD, 0x61, 0x26, 1295 1.1 christos 0x01, 0xDB, 0x53, 0x7D, 0xEC, 0xE8, 0x19, 0xB7, 0xF7, 0x0F, 0x55, 0x5A, 1296 1.1 christos 0x67, 0xC4, 0x27, 0xA8, 0xCD, 0x9B, 0xF1, 0x8A, 0xEB, 0x9B, 0x56, 0xE0, 1297 1.1 christos 0xC1, 0x10, 0x56, 0xFA, 0xE6, 0xA3 1298 1.1 christos }; 1299 1.1 christos return do_create_ec_explicit_trinomial_params(bld, gen, sizeof(gen)); 1300 1.1 christos } 1301 1.1 christos 1302 1.1 christos static int create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld) 1303 1.1 christos { 1304 1.1 christos static const unsigned char gen2[] = { 1305 1.1 christos 0x04, 1306 1.1 christos 0x00, 0xd7, 0xba, 0xd0, 0x26, 0x6c, 0x31, 0x6a, 0x78, 0x76, 0x01, 0xd1, 1307 1.1 christos 0x32, 0x4b, 0x8f, 0x30, 0x29, 0x2d, 0x78, 0x30, 0xca, 0x43, 0xaa, 0xf0, 1308 1.1 christos 0xa2, 0x5a, 0xd4, 0x0f, 0xb3, 0xf4, 1309 1.1 christos 0x00, 0x85, 0x4b, 0x1b, 0x8d, 0x50, 0x10, 0xa5, 0x1c, 0x80, 0xf7, 0x86, 1310 1.1 christos 0x40, 0x62, 0x4c, 0x87, 0xd1, 0x26, 0x7a, 0x9c, 0x5c, 0xe9, 0x82, 0x29, 1311 1.1 christos 0xd1, 0x67, 0x70, 0x41, 0xea, 0xcb 1312 1.1 christos }; 1313 1.1 christos return do_create_ec_explicit_trinomial_params(bld, gen2, sizeof(gen2)); 1314 1.1 christos } 1315 1.1.1.2 christos #endif /* OPENSSL_NO_EC2M */ 1316 1.1 christos 1317 1.1 christos /* 1318 1.1 christos * Test that multiple calls to OSSL_ENCODER_to_data() do not cause side effects 1319 1.1 christos */ 1320 1.1 christos static int ec_encode_to_data_multi(void) 1321 1.1 christos { 1322 1.1 christos int ret; 1323 1.1 christos OSSL_ENCODER_CTX *ectx = NULL; 1324 1.1 christos EVP_PKEY *key = NULL; 1325 1.1 christos uint8_t *enc = NULL; 1326 1.1 christos size_t enc_len = 0; 1327 1.1 christos 1328 1.1 christos ret = TEST_ptr(key = EVP_PKEY_Q_keygen(testctx, "", "EC", "P-256")) 1329 1.1 christos && TEST_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(key, EVP_PKEY_KEYPAIR, 1330 1.1.1.2 christos "DER", NULL, NULL)) 1331 1.1 christos && TEST_int_eq(OSSL_ENCODER_to_data(ectx, NULL, &enc_len), 1) 1332 1.1 christos && TEST_int_eq(OSSL_ENCODER_to_data(ectx, &enc, &enc_len), 1); 1333 1.1 christos OPENSSL_free(enc); 1334 1.1 christos EVP_PKEY_free(key); 1335 1.1 christos OSSL_ENCODER_CTX_free(ectx); 1336 1.1 christos return ret; 1337 1.1 christos } 1338 1.1 christos #endif /* OPENSSL_NO_EC */ 1339 1.1 christos 1340 1.1 christos typedef enum OPTION_choice { 1341 1.1 christos OPT_ERR = -1, 1342 1.1 christos OPT_EOF = 0, 1343 1.1 christos OPT_CONTEXT, 1344 1.1 christos OPT_RSA_FILE, 1345 1.1 christos OPT_RSA_PSS_FILE, 1346 1.1 christos OPT_CONFIG_FILE, 1347 1.1 christos OPT_PROVIDER_NAME, 1348 1.1 christos OPT_TEST_ENUM 1349 1.1 christos } OPTION_CHOICE; 1350 1.1 christos 1351 1.1 christos const OPTIONS *test_get_options(void) 1352 1.1 christos { 1353 1.1 christos static const OPTIONS options[] = { 1354 1.1 christos OPT_TEST_OPTIONS_DEFAULT_USAGE, 1355 1.1 christos { "context", OPT_CONTEXT, '-', 1356 1.1.1.2 christos "Explicitly use a non-default library context" }, 1357 1.1 christos { "rsa", OPT_RSA_FILE, '<', 1358 1.1.1.2 christos "PEM format RSA key file to encode/decode" }, 1359 1.1 christos { "pss", OPT_RSA_PSS_FILE, '<', 1360 1.1.1.2 christos "PEM format RSA-PSS key file to encode/decode" }, 1361 1.1 christos { "config", OPT_CONFIG_FILE, '<', 1362 1.1.1.2 christos "The configuration file to use for the library context" }, 1363 1.1 christos { "provider", OPT_PROVIDER_NAME, 's', 1364 1.1.1.2 christos "The provider to load (The default value is 'default')" }, 1365 1.1 christos { NULL } 1366 1.1 christos }; 1367 1.1 christos return options; 1368 1.1 christos } 1369 1.1 christos 1370 1.1 christos int setup_tests(void) 1371 1.1 christos { 1372 1.1 christos const char *rsa_file = NULL; 1373 1.1 christos const char *rsa_pss_file = NULL; 1374 1.1 christos const char *prov_name = "default"; 1375 1.1 christos char *config_file = NULL; 1376 1.1 christos int ok = 1; 1377 1.1 christos 1378 1.1 christos #ifndef OPENSSL_NO_DSA 1379 1.1.1.2 christos static size_t qbits = 160; /* PVK only tolerates 160 Q bits */ 1380 1.1 christos static size_t pbits = 1024; /* With 160 Q bits, we MUST use 1024 P bits */ 1381 1.1 christos OSSL_PARAM DSA_params[] = { 1382 1.1 christos OSSL_PARAM_size_t("pbits", &pbits), 1383 1.1 christos OSSL_PARAM_size_t("qbits", &qbits), 1384 1.1 christos OSSL_PARAM_END 1385 1.1 christos }; 1386 1.1 christos #endif 1387 1.1 christos 1388 1.1 christos #ifndef OPENSSL_NO_EC 1389 1.1 christos static char groupname[] = "prime256v1"; 1390 1.1 christos OSSL_PARAM EC_params[] = { 1391 1.1 christos OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1), 1392 1.1 christos OSSL_PARAM_END 1393 1.1 christos }; 1394 1.1 christos #endif 1395 1.1 christos 1396 1.1 christos OPTION_CHOICE o; 1397 1.1 christos 1398 1.1 christos while ((o = opt_next()) != OPT_EOF) { 1399 1.1 christos switch (o) { 1400 1.1 christos case OPT_CONTEXT: 1401 1.1 christos default_libctx = 0; 1402 1.1 christos break; 1403 1.1 christos case OPT_PROVIDER_NAME: 1404 1.1 christos prov_name = opt_arg(); 1405 1.1 christos break; 1406 1.1 christos case OPT_CONFIG_FILE: 1407 1.1 christos config_file = opt_arg(); 1408 1.1 christos break; 1409 1.1 christos case OPT_RSA_FILE: 1410 1.1 christos rsa_file = opt_arg(); 1411 1.1 christos break; 1412 1.1 christos case OPT_RSA_PSS_FILE: 1413 1.1 christos rsa_pss_file = opt_arg(); 1414 1.1 christos break; 1415 1.1 christos case OPT_TEST_CASES: 1416 1.1 christos break; 1417 1.1 christos default: 1418 1.1 christos return 0; 1419 1.1 christos } 1420 1.1 christos } 1421 1.1 christos 1422 1.1 christos if (strcmp(prov_name, "fips") == 0) 1423 1.1 christos is_fips = 1; 1424 1.1 christos 1425 1.1 christos if (default_libctx) { 1426 1.1 christos if (!test_get_libctx(NULL, NULL, config_file, &deflprov, prov_name)) 1427 1.1 christos return 0; 1428 1.1 christos } else { 1429 1.1 christos if (!test_get_libctx(&testctx, &nullprov, config_file, &deflprov, prov_name)) 1430 1.1 christos return 0; 1431 1.1 christos } 1432 1.1 christos 1433 1.1 christos /* FIPS(3.0.0): provider imports explicit params but they won't work #17998 */ 1434 1.1 christos is_fips_3_0_0 = is_fips && fips_provider_version_eq(testctx, 3, 0, 0); 1435 1.1 christos /* FIPS(3.5.0) is the first to support ML-DSA, ML-KEM and SLH-DSA */ 1436 1.1 christos is_fips_lt_3_5 = is_fips && fips_provider_version_lt(testctx, 3, 5, 0); 1437 1.1 christos 1438 1.1 christos #ifdef STATIC_LEGACY 1439 1.1 christos /* 1440 1.1 christos * This test is always statically linked against libcrypto. We must not 1441 1.1 christos * attempt to load legacy.so that might be dynamically linked against 1442 1.1 christos * libcrypto. Instead we use a built-in version of the legacy provider. 1443 1.1 christos */ 1444 1.1 christos if (!OSSL_PROVIDER_add_builtin(testctx, "legacy", ossl_legacy_provider_init)) 1445 1.1 christos return 0; 1446 1.1 christos #endif 1447 1.1 christos 1448 1.1 christos /* Separate provider/ctx for generating the test data */ 1449 1.1 christos if (!TEST_ptr(keyctx = OSSL_LIB_CTX_new())) 1450 1.1 christos return 0; 1451 1.1 christos if (!TEST_ptr(keyprov = OSSL_PROVIDER_load(keyctx, "default"))) 1452 1.1 christos return 0; 1453 1.1 christos 1454 1.1 christos #ifndef OPENSSL_NO_EC 1455 1.1 christos if (!TEST_ptr(bnctx = BN_CTX_new_ex(testctx)) 1456 1.1 christos || !TEST_ptr(bld_prime_nc = OSSL_PARAM_BLD_new()) 1457 1.1 christos || !TEST_ptr(bld_prime = OSSL_PARAM_BLD_new()) 1458 1.1 christos || !create_ec_explicit_prime_params_namedcurve(bld_prime_nc) 1459 1.1 christos || !create_ec_explicit_prime_params(bld_prime) 1460 1.1 christos || !TEST_ptr(ec_explicit_prime_params_nc = OSSL_PARAM_BLD_to_param(bld_prime_nc)) 1461 1.1 christos || !TEST_ptr(ec_explicit_prime_params_explicit = OSSL_PARAM_BLD_to_param(bld_prime)) 1462 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1463 1.1 christos || !TEST_ptr(bld_tri_nc = OSSL_PARAM_BLD_new()) 1464 1.1 christos || !TEST_ptr(bld_tri = OSSL_PARAM_BLD_new()) 1465 1.1 christos || !create_ec_explicit_trinomial_params_namedcurve(bld_tri_nc) 1466 1.1 christos || !create_ec_explicit_trinomial_params(bld_tri) 1467 1.1 christos || !TEST_ptr(ec_explicit_tri_params_nc = OSSL_PARAM_BLD_to_param(bld_tri_nc)) 1468 1.1 christos || !TEST_ptr(ec_explicit_tri_params_explicit = OSSL_PARAM_BLD_to_param(bld_tri)) 1469 1.1.1.2 christos #endif 1470 1.1.1.2 christos ) 1471 1.1 christos return 0; 1472 1.1 christos #endif 1473 1.1 christos 1474 1.1 christos TEST_info("Generating keys..."); 1475 1.1 christos 1476 1.1 christos #ifndef OPENSSL_NO_DH 1477 1.1 christos TEST_info("Generating DH keys..."); 1478 1.1 christos MAKE_DOMAIN_KEYS(DH, "DH", NULL); 1479 1.1 christos MAKE_DOMAIN_KEYS(DHX, "X9.42 DH", NULL); 1480 1.1 christos #endif 1481 1.1 christos #ifndef OPENSSL_NO_DSA 1482 1.1 christos TEST_info("Generating DSA keys..."); 1483 1.1 christos MAKE_DOMAIN_KEYS(DSA, "DSA", DSA_params); 1484 1.1 christos #endif 1485 1.1 christos #ifndef OPENSSL_NO_EC 1486 1.1 christos TEST_info("Generating EC keys..."); 1487 1.1 christos MAKE_DOMAIN_KEYS(EC, "EC", EC_params); 1488 1.1 christos MAKE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve, "EC", ec_explicit_prime_params_nc); 1489 1.1 christos MAKE_DOMAIN_KEYS(ECExplicitPrime2G, "EC", ec_explicit_prime_params_explicit); 1490 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1491 1.1 christos MAKE_DOMAIN_KEYS(ECExplicitTriNamedCurve, "EC", ec_explicit_tri_params_nc); 1492 1.1 christos MAKE_DOMAIN_KEYS(ECExplicitTri2G, "EC", ec_explicit_tri_params_explicit); 1493 1.1.1.2 christos #endif 1494 1.1.1.2 christos #ifndef OPENSSL_NO_SM2 1495 1.1 christos MAKE_KEYS(SM2, "SM2", NULL); 1496 1.1.1.2 christos #endif 1497 1.1 christos #endif 1498 1.1 christos #ifndef OPENSSL_NO_ECX 1499 1.1 christos MAKE_KEYS(ED25519, "ED25519", NULL); 1500 1.1 christos MAKE_KEYS(ED448, "ED448", NULL); 1501 1.1 christos MAKE_KEYS(X25519, "X25519", NULL); 1502 1.1 christos MAKE_KEYS(X448, "X448", NULL); 1503 1.1 christos #endif 1504 1.1 christos #ifndef OPENSSL_NO_ML_DSA 1505 1.1 christos if (!is_fips_lt_3_5) { 1506 1.1 christos MAKE_KEYS(ML_DSA_44, "ML-DSA-44", NULL); 1507 1.1 christos MAKE_KEYS(ML_DSA_65, "ML-DSA-65", NULL); 1508 1.1 christos MAKE_KEYS(ML_DSA_87, "ML-DSA-87", NULL); 1509 1.1 christos } 1510 1.1 christos #endif /* OPENSSL_NO_ML_DSA */ 1511 1.1 christos #ifndef OPENSSL_NO_ML_KEM 1512 1.1 christos if (!is_fips_lt_3_5) { 1513 1.1 christos MAKE_KEYS(ML_KEM_512, "ML-KEM-512", NULL); 1514 1.1 christos MAKE_KEYS(ML_KEM_768, "ML-KEM-768", NULL); 1515 1.1 christos MAKE_KEYS(ML_KEM_1024, "ML-KEM-1024", NULL); 1516 1.1 christos } 1517 1.1 christos #endif 1518 1.1 christos #ifndef OPENSSL_NO_SLH_DSA 1519 1.1 christos if (!is_fips_lt_3_5) { 1520 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_128s, "SLH-DSA-SHA2-128s", NULL); 1521 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_128f, "SLH-DSA-SHA2-128f", NULL); 1522 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_192s, "SLH-DSA-SHA2-192s", NULL); 1523 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_192f, "SLH-DSA-SHA2-192f", NULL); 1524 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_256s, "SLH-DSA-SHA2-256s", NULL); 1525 1.1 christos MAKE_KEYS(SLH_DSA_SHA2_256f, "SLH-DSA-SHA2-256f", NULL); 1526 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_128s, "SLH-DSA-SHAKE-128s", NULL); 1527 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_128f, "SLH-DSA-SHAKE-128f", NULL); 1528 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_192s, "SLH-DSA-SHAKE-192s", NULL); 1529 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_192f, "SLH-DSA-SHAKE-192f", NULL); 1530 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_256s, "SLH-DSA-SHAKE-256s", NULL); 1531 1.1 christos MAKE_KEYS(SLH_DSA_SHAKE_256f, "SLH-DSA-SHAKE-256f", NULL); 1532 1.1 christos } 1533 1.1 christos #endif /* OPENSSL_NO_SLH_DSA */ 1534 1.1 christos 1535 1.1 christos TEST_info("Loading RSA key..."); 1536 1.1 christos ok = ok && TEST_ptr(key_RSA = load_pkey_pem(rsa_file, keyctx)); 1537 1.1 christos TEST_info("Loading RSA_PSS key..."); 1538 1.1 christos ok = ok && TEST_ptr(key_RSA_PSS = load_pkey_pem(rsa_pss_file, keyctx)); 1539 1.1 christos TEST_info("Generating keys done"); 1540 1.1 christos 1541 1.1 christos if (ok) { 1542 1.1 christos #ifndef OPENSSL_NO_DH 1543 1.1 christos ADD_TEST_SUITE(DH); 1544 1.1 christos ADD_TEST_SUITE_PARAMS(DH); 1545 1.1 christos ADD_TEST_SUITE(DHX); 1546 1.1 christos ADD_TEST_SUITE_PARAMS(DHX); 1547 1.1 christos /* 1548 1.1 christos * DH has no support for PEM_write_bio_PrivateKey_traditional(), 1549 1.1 christos * so no legacy tests. 1550 1.1 christos */ 1551 1.1 christos #endif 1552 1.1 christos #ifndef OPENSSL_NO_DSA 1553 1.1 christos ADD_TEST_SUITE(DSA); 1554 1.1 christos ADD_TEST_SUITE_PARAMS(DSA); 1555 1.1 christos ADD_TEST_SUITE_LEGACY(DSA); 1556 1.1 christos ADD_TEST_SUITE_MSBLOB(DSA); 1557 1.1 christos ADD_TEST_SUITE_UNPROTECTED_PVK(DSA); 1558 1.1.1.2 christos #ifndef OPENSSL_NO_RC4 1559 1.1 christos ADD_TEST_SUITE_PROTECTED_PVK(DSA); 1560 1.1.1.2 christos #endif 1561 1.1 christos #endif 1562 1.1 christos #ifndef OPENSSL_NO_EC 1563 1.1 christos ADD_TEST(ec_encode_to_data_multi); 1564 1.1 christos ADD_TEST_SUITE(EC); 1565 1.1 christos ADD_TEST_SUITE_PARAMS(EC); 1566 1.1 christos ADD_TEST_SUITE_LEGACY(EC); 1567 1.1 christos ADD_TEST_SUITE(ECExplicitPrimeNamedCurve); 1568 1.1 christos ADD_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve); 1569 1.1 christos ADD_TEST_SUITE(ECExplicitPrime2G); 1570 1.1 christos ADD_TEST_SUITE_LEGACY(ECExplicitPrime2G); 1571 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1572 1.1 christos ADD_TEST_SUITE(ECExplicitTriNamedCurve); 1573 1.1 christos ADD_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve); 1574 1.1 christos ADD_TEST_SUITE(ECExplicitTri2G); 1575 1.1 christos ADD_TEST_SUITE_LEGACY(ECExplicitTri2G); 1576 1.1.1.2 christos #endif 1577 1.1.1.2 christos #ifndef OPENSSL_NO_SM2 1578 1.1 christos if (!is_fips_3_0_0) { 1579 1.1 christos /* 3.0.0 FIPS provider imports explicit EC params and then fails. */ 1580 1.1 christos ADD_TEST_SUITE(SM2); 1581 1.1 christos } 1582 1.1.1.2 christos #endif 1583 1.1 christos #endif 1584 1.1 christos #ifndef OPENSSL_NO_ECX 1585 1.1 christos ADD_TEST_SUITE(ED25519); 1586 1.1 christos ADD_TEST_SUITE(ED448); 1587 1.1 christos ADD_TEST_SUITE(X25519); 1588 1.1 christos ADD_TEST_SUITE(X448); 1589 1.1 christos /* 1590 1.1 christos * ED25519, ED448, X25519 and X448 have no support for 1591 1.1 christos * PEM_write_bio_PrivateKey_traditional(), so no legacy tests. 1592 1.1 christos */ 1593 1.1 christos #endif 1594 1.1 christos #ifndef OPENSSL_NO_ML_KEM 1595 1.1 christos if (!is_fips_lt_3_5) { 1596 1.1 christos ADD_TEST_SUITE(ML_KEM_512); 1597 1.1 christos ADD_TEST_SUITE(ML_KEM_768); 1598 1.1 christos ADD_TEST_SUITE(ML_KEM_1024); 1599 1.1 christos } 1600 1.1 christos #endif 1601 1.1 christos ADD_TEST_SUITE(RSA); 1602 1.1 christos ADD_TEST_SUITE_LEGACY(RSA); 1603 1.1 christos ADD_TEST_SUITE(RSA_PSS); 1604 1.1 christos /* 1605 1.1 christos * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(), 1606 1.1 christos * so no legacy tests. 1607 1.1 christos */ 1608 1.1 christos ADD_TEST_SUITE_MSBLOB(RSA); 1609 1.1 christos ADD_TEST_SUITE_UNPROTECTED_PVK(RSA); 1610 1.1.1.2 christos #ifndef OPENSSL_NO_RC4 1611 1.1 christos ADD_TEST_SUITE_PROTECTED_PVK(RSA); 1612 1.1.1.2 christos #endif 1613 1.1 christos 1614 1.1 christos #ifndef OPENSSL_NO_ML_DSA 1615 1.1 christos if (!is_fips_lt_3_5) { 1616 1.1 christos ADD_TEST_SUITE(ML_DSA_44); 1617 1.1 christos ADD_TEST_SUITE(ML_DSA_65); 1618 1.1 christos ADD_TEST_SUITE(ML_DSA_87); 1619 1.1 christos } 1620 1.1 christos #endif /* OPENSSL_NO_ML_DSA */ 1621 1.1 christos 1622 1.1 christos #ifndef OPENSSL_NO_SLH_DSA 1623 1.1 christos if (!is_fips_lt_3_5) { 1624 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_128s); 1625 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_128f); 1626 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_192s); 1627 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_192f); 1628 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_256s); 1629 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHA2_256f); 1630 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_128s); 1631 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_128f); 1632 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_192s); 1633 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_192f); 1634 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_256s); 1635 1.1 christos ADD_TEST_SUITE(SLH_DSA_SHAKE_256f); 1636 1.1 christos } 1637 1.1 christos #endif /* OPENSSL_NO_SLH_DSA */ 1638 1.1 christos } 1639 1.1 christos 1640 1.1 christos return 1; 1641 1.1 christos } 1642 1.1 christos 1643 1.1 christos void cleanup_tests(void) 1644 1.1 christos { 1645 1.1 christos #ifndef OPENSSL_NO_EC 1646 1.1 christos OSSL_PARAM_free(ec_explicit_prime_params_nc); 1647 1.1 christos OSSL_PARAM_free(ec_explicit_prime_params_explicit); 1648 1.1 christos OSSL_PARAM_BLD_free(bld_prime_nc); 1649 1.1 christos OSSL_PARAM_BLD_free(bld_prime); 1650 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1651 1.1 christos OSSL_PARAM_free(ec_explicit_tri_params_nc); 1652 1.1 christos OSSL_PARAM_free(ec_explicit_tri_params_explicit); 1653 1.1 christos OSSL_PARAM_BLD_free(bld_tri_nc); 1654 1.1 christos OSSL_PARAM_BLD_free(bld_tri); 1655 1.1.1.2 christos #endif 1656 1.1 christos BN_CTX_free(bnctx); 1657 1.1 christos #endif /* OPENSSL_NO_EC */ 1658 1.1 christos 1659 1.1 christos #ifndef OPENSSL_NO_DH 1660 1.1 christos FREE_DOMAIN_KEYS(DH); 1661 1.1 christos FREE_DOMAIN_KEYS(DHX); 1662 1.1 christos #endif 1663 1.1 christos #ifndef OPENSSL_NO_DSA 1664 1.1 christos FREE_DOMAIN_KEYS(DSA); 1665 1.1 christos #endif 1666 1.1 christos #ifndef OPENSSL_NO_EC 1667 1.1 christos FREE_DOMAIN_KEYS(EC); 1668 1.1 christos FREE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve); 1669 1.1 christos FREE_DOMAIN_KEYS(ECExplicitPrime2G); 1670 1.1.1.2 christos #ifndef OPENSSL_NO_EC2M 1671 1.1 christos FREE_DOMAIN_KEYS(ECExplicitTriNamedCurve); 1672 1.1 christos FREE_DOMAIN_KEYS(ECExplicitTri2G); 1673 1.1.1.2 christos #endif 1674 1.1.1.2 christos #ifndef OPENSSL_NO_SM2 1675 1.1 christos FREE_KEYS(SM2); 1676 1.1.1.2 christos #endif 1677 1.1 christos #endif 1678 1.1 christos #ifndef OPENSSL_NO_ECX 1679 1.1 christos FREE_KEYS(ED25519); 1680 1.1 christos FREE_KEYS(ED448); 1681 1.1 christos FREE_KEYS(X25519); 1682 1.1 christos FREE_KEYS(X448); 1683 1.1 christos #endif 1684 1.1 christos #ifndef OPENSSL_NO_ML_KEM 1685 1.1 christos if (!is_fips_lt_3_5) { 1686 1.1 christos FREE_KEYS(ML_KEM_512); 1687 1.1 christos FREE_KEYS(ML_KEM_768); 1688 1.1 christos FREE_KEYS(ML_KEM_1024); 1689 1.1 christos } 1690 1.1 christos #endif 1691 1.1 christos FREE_KEYS(RSA); 1692 1.1 christos FREE_KEYS(RSA_PSS); 1693 1.1 christos 1694 1.1 christos #ifndef OPENSSL_NO_ML_DSA 1695 1.1 christos if (!is_fips_lt_3_5) { 1696 1.1 christos FREE_KEYS(ML_DSA_44); 1697 1.1 christos FREE_KEYS(ML_DSA_65); 1698 1.1 christos FREE_KEYS(ML_DSA_87); 1699 1.1 christos } 1700 1.1 christos #endif /* OPENSSL_NO_ML_DSA */ 1701 1.1 christos 1702 1.1 christos #ifndef OPENSSL_NO_SLH_DSA 1703 1.1 christos if (!is_fips_lt_3_5) { 1704 1.1 christos FREE_KEYS(SLH_DSA_SHA2_128s); 1705 1.1 christos FREE_KEYS(SLH_DSA_SHA2_128f); 1706 1.1 christos FREE_KEYS(SLH_DSA_SHA2_192s); 1707 1.1 christos FREE_KEYS(SLH_DSA_SHA2_192f); 1708 1.1 christos FREE_KEYS(SLH_DSA_SHA2_256s); 1709 1.1 christos FREE_KEYS(SLH_DSA_SHA2_256f); 1710 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_128s); 1711 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_128f); 1712 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_192s); 1713 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_192f); 1714 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_256s); 1715 1.1 christos FREE_KEYS(SLH_DSA_SHAKE_256f); 1716 1.1 christos } 1717 1.1 christos #endif /* OPENSSL_NO_SLH_DSA */ 1718 1.1 christos 1719 1.1 christos OSSL_PROVIDER_unload(nullprov); 1720 1.1 christos OSSL_PROVIDER_unload(deflprov); 1721 1.1 christos OSSL_PROVIDER_unload(keyprov); 1722 1.1 christos OSSL_LIB_CTX_free(testctx); 1723 1.1 christos OSSL_LIB_CTX_free(keyctx); 1724 1.1 christos } 1725