endecode_test.c 57 KB

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