endecoder_legacy_test.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. /*
  10. * This program tests the following known key type specific function against
  11. * the corresponding OSSL_ENCODER implementation:
  12. *
  13. * - i2d_{TYPE}PrivateKey()
  14. * - i2d_{TYPE}PublicKey(),
  15. * - i2d_{TYPE}params(),
  16. * - i2d_{TYPE}_PUBKEY(),
  17. * - PEM_write_bio_{TYPE}PrivateKey()
  18. * - PEM_write_bio_{TYPE}PublicKey()
  19. * - PEM_write_bio_{TYPE}params()
  20. * - PEM_write_bio_{TYPE}_PUBKEY()
  21. *
  22. * as well as the following functions against the corresponding OSSL_DECODER
  23. * implementation.
  24. *
  25. * - d2i_{TYPE}PrivateKey()
  26. * - d2i_{TYPE}PublicKey(),
  27. * - d2i_{TYPE}params(),
  28. * - d2i_{TYPE}_PUBKEY(),
  29. * - PEM_read_bio_{TYPE}PrivateKey()
  30. * - PEM_read_bio_{TYPE}PublicKey()
  31. * - PEM_read_bio_{TYPE}params()
  32. * - PEM_read_bio_{TYPE}_PUBKEY()
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. /*
  37. * We test deprecated functions, so we need to suppress deprecation warnings.
  38. */
  39. #define OPENSSL_SUPPRESS_DEPRECATED
  40. #include <openssl/bio.h>
  41. #include <openssl/evp.h>
  42. #include <openssl/asn1.h>
  43. #include <openssl/pem.h>
  44. #include <openssl/params.h>
  45. #include <openssl/encoder.h>
  46. #include <openssl/decoder.h>
  47. #include <openssl/dh.h>
  48. #include <openssl/dsa.h>
  49. #ifndef OPENSSL_NO_DEPRECATED_3_0
  50. # include <openssl/rsa.h>
  51. #endif
  52. #include "internal/nelem.h"
  53. #include "crypto/evp.h"
  54. #include "testutil.h"
  55. typedef int PEM_write_bio_of_void_protected(BIO *out, void *obj,
  56. const EVP_CIPHER *enc,
  57. unsigned char *kstr, int klen,
  58. pem_password_cb *cb, void *u);
  59. typedef int PEM_write_bio_of_void_unprotected(BIO *out, void *obj);
  60. typedef void *PEM_read_bio_of_void(BIO *out, void **obj,
  61. pem_password_cb *cb, void *u);
  62. typedef int EVP_PKEY_print_fn(BIO *out, const EVP_PKEY *pkey,
  63. int indent, ASN1_PCTX *pctx);
  64. typedef int EVP_PKEY_eq_fn(const EVP_PKEY *a, const EVP_PKEY *b);
  65. static struct test_stanza_st {
  66. const char *keytype;
  67. const char *structure[2];
  68. int evp_type;
  69. i2d_of_void *i2d_PrivateKey;
  70. i2d_of_void *i2d_PublicKey;
  71. i2d_of_void *i2d_params;
  72. i2d_of_void *i2d_PUBKEY;
  73. PEM_write_bio_of_void_protected *pem_write_bio_PrivateKey;
  74. PEM_write_bio_of_void_unprotected *pem_write_bio_PublicKey;
  75. PEM_write_bio_of_void_unprotected *pem_write_bio_params;
  76. PEM_write_bio_of_void_unprotected *pem_write_bio_PUBKEY;
  77. d2i_of_void *d2i_PrivateKey;
  78. d2i_of_void *d2i_PublicKey;
  79. d2i_of_void *d2i_params;
  80. d2i_of_void *d2i_PUBKEY;
  81. PEM_read_bio_of_void *pem_read_bio_PrivateKey;
  82. PEM_read_bio_of_void *pem_read_bio_PublicKey;
  83. PEM_read_bio_of_void *pem_read_bio_params;
  84. PEM_read_bio_of_void *pem_read_bio_PUBKEY;
  85. } test_stanzas[] = {
  86. #ifndef OPENSSL_NO_DH
  87. { "DH", { "DH", "type-specific" }, EVP_PKEY_DH,
  88. NULL, /* No i2d_DHPrivateKey */
  89. NULL, /* No i2d_DHPublicKey */
  90. (i2d_of_void *)i2d_DHparams,
  91. NULL, /* No i2d_DH_PUBKEY */
  92. NULL, /* No PEM_write_bio_DHPrivateKey */
  93. NULL, /* No PEM_write_bio_DHPublicKey */
  94. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHparams,
  95. NULL, /* No PEM_write_bio_DH_PUBKEY */
  96. NULL, /* No d2i_DHPrivateKey */
  97. NULL, /* No d2i_DHPublicKey */
  98. (d2i_of_void *)d2i_DHparams,
  99. NULL, /* No d2i_DH_PUBKEY */
  100. NULL, /* No PEM_read_bio_DHPrivateKey */
  101. NULL, /* No PEM_read_bio_DHPublicKey */
  102. (PEM_read_bio_of_void *)PEM_read_bio_DHparams,
  103. NULL }, /* No PEM_read_bio_DH_PUBKEY */
  104. { "DHX", { "DHX", "type-specific" }, EVP_PKEY_DHX,
  105. NULL, /* No i2d_DHxPrivateKey */
  106. NULL, /* No i2d_DHxPublicKey */
  107. (i2d_of_void *)i2d_DHxparams,
  108. NULL, /* No i2d_DHx_PUBKEY */
  109. NULL, /* No PEM_write_bio_DHxPrivateKey */
  110. NULL, /* No PEM_write_bio_DHxPublicKey */
  111. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHxparams,
  112. NULL, /* No PEM_write_bio_DHx_PUBKEY */
  113. NULL, /* No d2i_DHxPrivateKey */
  114. NULL, /* No d2i_DHxPublicKey */
  115. (d2i_of_void *)d2i_DHxparams,
  116. NULL, /* No d2i_DHx_PUBKEY */
  117. NULL, /* No PEM_read_bio_DHxPrivateKey */
  118. NULL, /* No PEM_read_bio_DHxPublicKey */
  119. NULL, /* No PEM_read_bio_DHxparams */
  120. NULL }, /* No PEM_read_bio_DHx_PUBKEY */
  121. #endif
  122. #ifndef OPENSSL_NO_DSA
  123. { "DSA", { "DSA", "type-specific" }, EVP_PKEY_DSA,
  124. (i2d_of_void *)i2d_DSAPrivateKey,
  125. (i2d_of_void *)i2d_DSAPublicKey,
  126. (i2d_of_void *)i2d_DSAparams,
  127. (i2d_of_void *)i2d_DSA_PUBKEY,
  128. (PEM_write_bio_of_void_protected *)PEM_write_bio_DSAPrivateKey,
  129. NULL, /* No PEM_write_bio_DSAPublicKey */
  130. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSAparams,
  131. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSA_PUBKEY,
  132. (d2i_of_void *)d2i_DSAPrivateKey,
  133. (d2i_of_void *)d2i_DSAPublicKey,
  134. (d2i_of_void *)d2i_DSAparams,
  135. (d2i_of_void *)d2i_DSA_PUBKEY,
  136. (PEM_read_bio_of_void *)PEM_read_bio_DSAPrivateKey,
  137. NULL, /* No PEM_write_bio_DSAPublicKey */
  138. (PEM_read_bio_of_void *)PEM_read_bio_DSAparams,
  139. (PEM_read_bio_of_void *)PEM_read_bio_DSA_PUBKEY },
  140. #endif
  141. #ifndef OPENSSL_NO_EC
  142. { "EC", { "EC", "type-specific" }, EVP_PKEY_EC,
  143. (i2d_of_void *)i2d_ECPrivateKey,
  144. NULL, /* No i2d_ECPublicKey */
  145. (i2d_of_void *)i2d_ECParameters,
  146. (i2d_of_void *)i2d_EC_PUBKEY,
  147. (PEM_write_bio_of_void_protected *)PEM_write_bio_ECPrivateKey,
  148. NULL, /* No PEM_write_bio_ECPublicKey */
  149. NULL, /* No PEM_write_bio_ECParameters */
  150. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_EC_PUBKEY,
  151. (d2i_of_void *)d2i_ECPrivateKey,
  152. NULL, /* No d2i_ECPublicKey */
  153. (d2i_of_void *)d2i_ECParameters,
  154. (d2i_of_void *)d2i_EC_PUBKEY,
  155. (PEM_read_bio_of_void *)PEM_read_bio_ECPrivateKey,
  156. NULL, /* No PEM_read_bio_ECPublicKey */
  157. NULL, /* No PEM_read_bio_ECParameters */
  158. (PEM_read_bio_of_void *)PEM_read_bio_EC_PUBKEY, },
  159. #endif
  160. { "RSA", { "RSA", "type-specific" }, EVP_PKEY_RSA,
  161. (i2d_of_void *)i2d_RSAPrivateKey,
  162. (i2d_of_void *)i2d_RSAPublicKey,
  163. NULL, /* No i2d_RSAparams */
  164. (i2d_of_void *)i2d_RSA_PUBKEY,
  165. (PEM_write_bio_of_void_protected *)PEM_write_bio_RSAPrivateKey,
  166. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSAPublicKey,
  167. NULL, /* No PEM_write_bio_RSAparams */
  168. (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSA_PUBKEY,
  169. (d2i_of_void *)d2i_RSAPrivateKey,
  170. (d2i_of_void *)d2i_RSAPublicKey,
  171. NULL, /* No d2i_RSAparams */
  172. (d2i_of_void *)d2i_RSA_PUBKEY,
  173. (PEM_read_bio_of_void *)PEM_read_bio_RSAPrivateKey,
  174. (PEM_read_bio_of_void *)PEM_read_bio_RSAPublicKey,
  175. NULL, /* No PEM_read_bio_RSAparams */
  176. (PEM_read_bio_of_void *)PEM_read_bio_RSA_PUBKEY }
  177. };
  178. /*
  179. * Keys that we're going to test with. We initialize this with the intended
  180. * key types, and generate the keys themselves on program setup.
  181. * They must all be downgradable with EVP_PKEY_get0()
  182. */
  183. #ifndef OPENSSL_NO_DH
  184. static const OSSL_PARAM DH_params[] = { OSSL_PARAM_END };
  185. static const OSSL_PARAM DHX_params[] = { OSSL_PARAM_END };
  186. #endif
  187. #ifndef OPENSSL_NO_DSA
  188. static size_t qbits = 160; /* PVK only tolerates 160 Q bits */
  189. static size_t pbits = 1024; /* With 160 Q bits, we MUST use 1024 P bits */
  190. static const OSSL_PARAM DSA_params[] = {
  191. OSSL_PARAM_size_t("pbits", &pbits),
  192. OSSL_PARAM_size_t("qbits", &qbits),
  193. OSSL_PARAM_END
  194. };
  195. #endif
  196. #ifndef OPENSSL_NO_EC
  197. static char groupname[] = "prime256v1";
  198. static const OSSL_PARAM EC_params[] = {
  199. OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1),
  200. OSSL_PARAM_END
  201. };
  202. #endif
  203. static struct key_st {
  204. const char *keytype;
  205. int evp_type;
  206. /* non-NULL if a template EVP_PKEY must be generated first */
  207. const OSSL_PARAM *template_params;
  208. EVP_PKEY *key;
  209. } keys[] = {
  210. #ifndef OPENSSL_NO_DH
  211. { "DH", EVP_PKEY_DH, DH_params, NULL },
  212. { "DHX", EVP_PKEY_DHX, DHX_params, NULL },
  213. #endif
  214. #ifndef OPENSSL_NO_DSA
  215. { "DSA", EVP_PKEY_DSA, DSA_params, NULL },
  216. #endif
  217. #ifndef OPENSSL_NO_EC
  218. { "EC", EVP_PKEY_EC, EC_params, NULL },
  219. #endif
  220. #ifndef OPENSSL_NO_DEPRECATED_3_0
  221. { "RSA", EVP_PKEY_RSA, NULL, NULL },
  222. #endif
  223. };
  224. static EVP_PKEY *make_key(const char *type,
  225. const OSSL_PARAM *gen_template_params)
  226. {
  227. EVP_PKEY *template = NULL;
  228. EVP_PKEY *pkey = NULL;
  229. EVP_PKEY_CTX *ctx = NULL;
  230. OSSL_PARAM *gen_template_params_noconst =
  231. (OSSL_PARAM *)gen_template_params;
  232. if (gen_template_params != NULL
  233. && ((ctx = EVP_PKEY_CTX_new_from_name(NULL, type, NULL)) == NULL
  234. || EVP_PKEY_paramgen_init(ctx) <= 0
  235. || (gen_template_params[0].key != NULL
  236. && EVP_PKEY_CTX_set_params(ctx, gen_template_params_noconst) <= 0)
  237. || EVP_PKEY_gen(ctx, &template) <= 0))
  238. goto end;
  239. EVP_PKEY_CTX_free(ctx);
  240. /*
  241. * No real need to check the errors other than for the cascade
  242. * effect. |pkey| will simply remain NULL if something goes wrong.
  243. */
  244. ctx =
  245. template != NULL
  246. ? EVP_PKEY_CTX_new(template, NULL)
  247. : EVP_PKEY_CTX_new_from_name(NULL, type, NULL);
  248. (void)(ctx != NULL
  249. && EVP_PKEY_keygen_init(ctx) > 0
  250. && EVP_PKEY_keygen(ctx, &pkey) > 0);
  251. end:
  252. EVP_PKEY_free(template);
  253. EVP_PKEY_CTX_free(ctx);
  254. return pkey;
  255. }
  256. static struct key_st *lookup_key(const char *type)
  257. {
  258. size_t i;
  259. for (i = 0; i < OSSL_NELEM(keys); i++) {
  260. if (strcmp(keys[i].keytype, type) == 0)
  261. return &keys[i];
  262. }
  263. return NULL;
  264. }
  265. static int test_membio_str_eq(BIO *bio_provided, BIO *bio_legacy)
  266. {
  267. char *str_provided = NULL, *str_legacy = NULL;
  268. long len_provided = BIO_get_mem_data(bio_provided, &str_provided);
  269. long len_legacy = BIO_get_mem_data(bio_legacy, &str_legacy);
  270. return TEST_strn2_eq(str_provided, len_provided,
  271. str_legacy, len_legacy);
  272. }
  273. static int test_protected_PEM(const char *keytype, int evp_type,
  274. void *legacy_key,
  275. PEM_write_bio_of_void_protected *pem_write_bio,
  276. PEM_read_bio_of_void *pem_read_bio,
  277. EVP_PKEY_eq_fn *evp_pkey_eq,
  278. EVP_PKEY_print_fn *evp_pkey_print,
  279. EVP_PKEY *provided_pkey, int selection,
  280. const char *structure)
  281. {
  282. int ok = 0;
  283. BIO *membio_legacy = NULL;
  284. BIO *membio_provided = NULL;
  285. OSSL_ENCODER_CTX *ectx = NULL;
  286. OSSL_DECODER_CTX *dctx = NULL;
  287. void *decoded_legacy_key = NULL;
  288. EVP_PKEY *decoded_legacy_pkey = NULL;
  289. EVP_PKEY *decoded_provided_pkey = NULL;
  290. /* Set up the BIOs, so we have them */
  291. if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
  292. || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
  293. goto end;
  294. if (!TEST_ptr(ectx =
  295. OSSL_ENCODER_CTX_new_by_EVP_PKEY(provided_pkey, selection,
  296. "PEM", structure,
  297. NULL))
  298. || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
  299. || !TEST_true(pem_write_bio(membio_legacy, legacy_key,
  300. NULL, NULL, 0, NULL, NULL))
  301. || !test_membio_str_eq(membio_provided, membio_legacy))
  302. goto end;
  303. if (pem_read_bio != NULL) {
  304. /* Now try decoding the results and compare the resulting keys */
  305. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  306. || !TEST_ptr(dctx =
  307. OSSL_DECODER_CTX_new_by_EVP_PKEY(&decoded_provided_pkey,
  308. "PEM", structure,
  309. keytype, selection,
  310. NULL, NULL))
  311. || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
  312. || !TEST_ptr(decoded_legacy_key =
  313. pem_read_bio(membio_legacy, NULL, NULL, NULL))
  314. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  315. decoded_legacy_key)))
  316. goto end;
  317. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  318. decoded_legacy_pkey), 0)) {
  319. TEST_info("decoded_provided_pkey:");
  320. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  321. TEST_info("decoded_legacy_pkey:");
  322. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  323. }
  324. }
  325. ok = 1;
  326. end:
  327. EVP_PKEY_free(decoded_legacy_pkey);
  328. EVP_PKEY_free(decoded_provided_pkey);
  329. OSSL_ENCODER_CTX_free(ectx);
  330. OSSL_DECODER_CTX_free(dctx);
  331. BIO_free(membio_provided);
  332. BIO_free(membio_legacy);
  333. return ok;
  334. }
  335. static int test_unprotected_PEM(const char *keytype, int evp_type,
  336. void *legacy_key,
  337. PEM_write_bio_of_void_unprotected *pem_write_bio,
  338. PEM_read_bio_of_void *pem_read_bio,
  339. EVP_PKEY_eq_fn *evp_pkey_eq,
  340. EVP_PKEY_print_fn *evp_pkey_print,
  341. EVP_PKEY *provided_pkey, int selection,
  342. const char *structure)
  343. {
  344. int ok = 0;
  345. BIO *membio_legacy = NULL;
  346. BIO *membio_provided = NULL;
  347. OSSL_ENCODER_CTX *ectx = NULL;
  348. OSSL_DECODER_CTX *dctx = NULL;
  349. void *decoded_legacy_key = NULL;
  350. EVP_PKEY *decoded_legacy_pkey = NULL;
  351. EVP_PKEY *decoded_provided_pkey = NULL;
  352. /* Set up the BIOs, so we have them */
  353. if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
  354. || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
  355. goto end;
  356. if (!TEST_ptr(ectx =
  357. OSSL_ENCODER_CTX_new_by_EVP_PKEY(provided_pkey, selection,
  358. "PEM", structure,
  359. NULL))
  360. || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
  361. || !TEST_true(pem_write_bio(membio_legacy, legacy_key))
  362. || !test_membio_str_eq(membio_provided, membio_legacy))
  363. goto end;
  364. if (pem_read_bio != NULL) {
  365. /* Now try decoding the results and compare the resulting keys */
  366. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  367. || !TEST_ptr(dctx =
  368. OSSL_DECODER_CTX_new_by_EVP_PKEY(&decoded_provided_pkey,
  369. "PEM", structure,
  370. keytype, selection,
  371. NULL, NULL))
  372. || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
  373. || !TEST_ptr(decoded_legacy_key =
  374. pem_read_bio(membio_legacy, NULL, NULL, NULL))
  375. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  376. decoded_legacy_key)))
  377. goto end;
  378. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  379. decoded_legacy_pkey), 0)) {
  380. TEST_info("decoded_provided_pkey:");
  381. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  382. TEST_info("decoded_legacy_pkey:");
  383. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  384. }
  385. }
  386. ok = 1;
  387. end:
  388. EVP_PKEY_free(decoded_legacy_pkey);
  389. EVP_PKEY_free(decoded_provided_pkey);
  390. OSSL_ENCODER_CTX_free(ectx);
  391. OSSL_DECODER_CTX_free(dctx);
  392. BIO_free(membio_provided);
  393. BIO_free(membio_legacy);
  394. return ok;
  395. }
  396. static int test_DER(const char *keytype, int evp_type,
  397. void *legacy_key, i2d_of_void *i2d, d2i_of_void *d2i,
  398. EVP_PKEY_eq_fn *evp_pkey_eq,
  399. EVP_PKEY_print_fn *evp_pkey_print,
  400. EVP_PKEY *provided_pkey, int selection,
  401. const char *structure)
  402. {
  403. int ok = 0;
  404. unsigned char *der_legacy = NULL;
  405. const unsigned char *pder_legacy = NULL;
  406. size_t der_legacy_len = 0;
  407. unsigned char *der_provided = NULL;
  408. const unsigned char *pder_provided = NULL;
  409. size_t der_provided_len = 0;
  410. size_t tmp_size;
  411. OSSL_ENCODER_CTX *ectx = NULL;
  412. OSSL_DECODER_CTX *dctx = NULL;
  413. void *decoded_legacy_key = NULL;
  414. EVP_PKEY *decoded_legacy_pkey = NULL;
  415. EVP_PKEY *decoded_provided_pkey = NULL;
  416. if (!TEST_ptr(ectx =
  417. OSSL_ENCODER_CTX_new_by_EVP_PKEY(provided_pkey, selection,
  418. "DER", structure,
  419. NULL))
  420. || !TEST_true(OSSL_ENCODER_to_data(ectx,
  421. &der_provided, &der_provided_len))
  422. || !TEST_size_t_gt(der_legacy_len = i2d(legacy_key, &der_legacy), 0)
  423. || !TEST_mem_eq(der_provided, der_provided_len,
  424. der_legacy, der_legacy_len))
  425. goto end;
  426. if (d2i != NULL) {
  427. /* Now try decoding the results and compare the resulting keys */
  428. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  429. || !TEST_ptr(dctx =
  430. OSSL_DECODER_CTX_new_by_EVP_PKEY(&decoded_provided_pkey,
  431. "DER", structure,
  432. keytype, selection,
  433. NULL, NULL))
  434. || !TEST_true((pder_provided = der_provided,
  435. tmp_size = der_provided_len,
  436. OSSL_DECODER_from_data(dctx, &pder_provided,
  437. &tmp_size)))
  438. || !TEST_ptr((pder_legacy = der_legacy,
  439. decoded_legacy_key = d2i(NULL, &pder_legacy,
  440. (long)der_legacy_len)))
  441. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  442. decoded_legacy_key)))
  443. goto end;
  444. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  445. decoded_legacy_pkey), 0)) {
  446. TEST_info("decoded_provided_pkey:");
  447. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  448. TEST_info("decoded_legacy_pkey:");
  449. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  450. }
  451. }
  452. ok = 1;
  453. end:
  454. EVP_PKEY_free(decoded_legacy_pkey);
  455. EVP_PKEY_free(decoded_provided_pkey);
  456. OSSL_ENCODER_CTX_free(ectx);
  457. OSSL_DECODER_CTX_free(dctx);
  458. OPENSSL_free(der_provided);
  459. OPENSSL_free(der_legacy);
  460. return ok;
  461. }
  462. static int test_key(int idx)
  463. {
  464. struct test_stanza_st *test_stanza = NULL;
  465. struct key_st *key = NULL;
  466. int ok = 0;
  467. size_t i;
  468. EVP_PKEY *pkey = NULL, *downgraded_pkey = NULL;
  469. void *legacy_obj = NULL;
  470. /* Get the test data */
  471. if (!TEST_ptr(test_stanza = &test_stanzas[idx])
  472. || !TEST_ptr(key = lookup_key(test_stanza->keytype)))
  473. goto end;
  474. /* Set up the keys */
  475. if (!TEST_ptr(pkey = key->key)
  476. || !TEST_true(evp_pkey_copy_downgraded(&downgraded_pkey, pkey))
  477. || !TEST_ptr(downgraded_pkey)
  478. || !TEST_int_eq(EVP_PKEY_id(downgraded_pkey), key->evp_type)
  479. || !TEST_ptr(legacy_obj = EVP_PKEY_get0(downgraded_pkey)))
  480. goto end;
  481. ok = 1;
  482. /* Test PrivateKey to PEM */
  483. if (test_stanza->pem_write_bio_PrivateKey != NULL) {
  484. int selection = OSSL_KEYMGMT_SELECT_ALL;
  485. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  486. const char *structure = test_stanza->structure[i];
  487. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PrivateKey for %s, %s",
  488. test_stanza->keytype, structure);
  489. if (!test_protected_PEM(key->keytype, key->evp_type, legacy_obj,
  490. test_stanza->pem_write_bio_PrivateKey,
  491. test_stanza->pem_read_bio_PrivateKey,
  492. EVP_PKEY_eq, EVP_PKEY_print_private,
  493. pkey, selection, structure))
  494. ok = 0;
  495. }
  496. }
  497. /* Test PublicKey to PEM */
  498. if (test_stanza->pem_write_bio_PublicKey != NULL) {
  499. int selection =
  500. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  501. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  502. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  503. const char *structure = test_stanza->structure[i];
  504. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PublicKey for %s, %s",
  505. test_stanza->keytype, structure);
  506. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  507. test_stanza->pem_write_bio_PublicKey,
  508. test_stanza->pem_read_bio_PublicKey,
  509. EVP_PKEY_eq, EVP_PKEY_print_public,
  510. pkey, selection, structure))
  511. ok = 0;
  512. }
  513. }
  514. /* Test params to PEM */
  515. if (test_stanza->pem_write_bio_params != NULL) {
  516. int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  517. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  518. const char *structure = test_stanza->structure[i];
  519. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}params for %s, %s",
  520. test_stanza->keytype, structure);
  521. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  522. test_stanza->pem_write_bio_params,
  523. test_stanza->pem_read_bio_params,
  524. EVP_PKEY_parameters_eq,
  525. EVP_PKEY_print_params,
  526. pkey, selection, structure))
  527. ok = 0;
  528. }
  529. }
  530. /* Test PUBKEY to PEM */
  531. if (test_stanza->pem_write_bio_PUBKEY != NULL) {
  532. int selection =
  533. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  534. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  535. const char *structure = "SubjectPublicKeyInfo";
  536. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}_PUBKEY for %s, %s",
  537. test_stanza->keytype, structure);
  538. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  539. test_stanza->pem_write_bio_PUBKEY,
  540. test_stanza->pem_read_bio_PUBKEY,
  541. EVP_PKEY_eq, EVP_PKEY_print_public,
  542. pkey, selection, structure))
  543. ok = 0;
  544. }
  545. /* Test PrivateKey to DER */
  546. if (test_stanza->i2d_PrivateKey != NULL) {
  547. int selection = OSSL_KEYMGMT_SELECT_ALL;
  548. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  549. const char *structure = test_stanza->structure[i];
  550. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PrivateKey for %s, %s",
  551. test_stanza->keytype, structure);
  552. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  553. test_stanza->i2d_PrivateKey,
  554. test_stanza->d2i_PrivateKey,
  555. EVP_PKEY_eq, EVP_PKEY_print_private,
  556. pkey, selection, structure))
  557. ok = 0;
  558. }
  559. }
  560. /* Test PublicKey to DER */
  561. if (test_stanza->i2d_PublicKey != NULL) {
  562. int selection =
  563. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  564. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  565. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  566. const char *structure = test_stanza->structure[i];
  567. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PublicKey for %s, %s",
  568. test_stanza->keytype, structure);
  569. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  570. test_stanza->i2d_PublicKey,
  571. test_stanza->d2i_PublicKey,
  572. EVP_PKEY_eq, EVP_PKEY_print_public,
  573. pkey, selection, structure))
  574. ok = 0;
  575. }
  576. }
  577. /* Test params to DER */
  578. if (test_stanza->i2d_params != NULL) {
  579. int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  580. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  581. const char *structure = test_stanza->structure[i];
  582. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}params for %s, %s",
  583. test_stanza->keytype, structure);
  584. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  585. test_stanza->i2d_params, test_stanza->d2i_params,
  586. EVP_PKEY_parameters_eq, EVP_PKEY_print_params,
  587. pkey, selection, structure))
  588. ok = 0;
  589. }
  590. }
  591. /* Test PUBKEY to DER */
  592. if (test_stanza->i2d_PUBKEY != NULL) {
  593. int selection =
  594. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  595. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  596. const char *structure = "SubjectPublicKeyInfo";
  597. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}_PUBKEY for %s, %s",
  598. test_stanza->keytype, structure);
  599. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  600. test_stanza->i2d_PUBKEY, test_stanza->d2i_PUBKEY,
  601. EVP_PKEY_eq, EVP_PKEY_print_public,
  602. pkey, selection, structure))
  603. ok = 0;
  604. }
  605. end:
  606. EVP_PKEY_free(downgraded_pkey);
  607. return ok;
  608. }
  609. int setup_tests(void)
  610. {
  611. size_t i;
  612. TEST_info("Generating keys...");
  613. for (i = 0; i < OSSL_NELEM(keys); i++) {
  614. if (!TEST_ptr(keys[i].key =
  615. make_key(keys[i].keytype, keys[i].template_params)))
  616. return 0;
  617. }
  618. TEST_info("Generating key... done");
  619. ADD_ALL_TESTS(test_key, OSSL_NELEM(test_stanzas));
  620. return 1;
  621. }
  622. void cleanup_tests(void)
  623. {
  624. size_t i;
  625. for (i = 0; i < OSSL_NELEM(keys); i++)
  626. EVP_PKEY_free(keys[i].key);
  627. }