endecode_test.c 58 KB

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