endecode_test.c 49 KB

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