endecode_test.c 56 KB

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