endecoder_legacy_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  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, const 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, const 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_generate(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_long_ge(len_provided, 0)
  271. && TEST_strn2_eq(str_provided, len_provided,
  272. str_legacy, len_legacy);
  273. }
  274. static int test_protected_PEM(const char *keytype, int evp_type,
  275. const void *legacy_key,
  276. PEM_write_bio_of_void_protected *pem_write_bio,
  277. PEM_read_bio_of_void *pem_read_bio,
  278. EVP_PKEY_eq_fn *evp_pkey_eq,
  279. EVP_PKEY_print_fn *evp_pkey_print,
  280. EVP_PKEY *provided_pkey, int selection,
  281. const char *structure)
  282. {
  283. int ok = 0;
  284. BIO *membio_legacy = NULL;
  285. BIO *membio_provided = NULL;
  286. OSSL_ENCODER_CTX *ectx = NULL;
  287. OSSL_DECODER_CTX *dctx = NULL;
  288. void *decoded_legacy_key = NULL;
  289. EVP_PKEY *decoded_legacy_pkey = NULL;
  290. EVP_PKEY *decoded_provided_pkey = NULL;
  291. /* Set up the BIOs, so we have them */
  292. if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
  293. || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
  294. goto end;
  295. if (!TEST_ptr(ectx =
  296. OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
  297. "PEM", structure,
  298. NULL))
  299. || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
  300. || !TEST_true(pem_write_bio(membio_legacy, legacy_key,
  301. NULL, NULL, 0, NULL, NULL))
  302. || !test_membio_str_eq(membio_provided, membio_legacy))
  303. goto end;
  304. if (pem_read_bio != NULL) {
  305. /* Now try decoding the results and compare the resulting keys */
  306. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  307. || !TEST_ptr(dctx =
  308. OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
  309. "PEM", structure,
  310. keytype, selection,
  311. NULL, NULL))
  312. || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
  313. || !TEST_ptr(decoded_legacy_key =
  314. pem_read_bio(membio_legacy, NULL, NULL, NULL))
  315. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  316. decoded_legacy_key)))
  317. goto end;
  318. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  319. decoded_legacy_pkey), 0)) {
  320. TEST_info("decoded_provided_pkey:");
  321. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  322. TEST_info("decoded_legacy_pkey:");
  323. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  324. }
  325. }
  326. ok = 1;
  327. end:
  328. EVP_PKEY_free(decoded_legacy_pkey);
  329. EVP_PKEY_free(decoded_provided_pkey);
  330. OSSL_ENCODER_CTX_free(ectx);
  331. OSSL_DECODER_CTX_free(dctx);
  332. BIO_free(membio_provided);
  333. BIO_free(membio_legacy);
  334. return ok;
  335. }
  336. static int test_unprotected_PEM(const char *keytype, int evp_type,
  337. const void *legacy_key,
  338. PEM_write_bio_of_void_unprotected *pem_write_bio,
  339. PEM_read_bio_of_void *pem_read_bio,
  340. EVP_PKEY_eq_fn *evp_pkey_eq,
  341. EVP_PKEY_print_fn *evp_pkey_print,
  342. EVP_PKEY *provided_pkey, int selection,
  343. const char *structure)
  344. {
  345. int ok = 0;
  346. BIO *membio_legacy = NULL;
  347. BIO *membio_provided = NULL;
  348. OSSL_ENCODER_CTX *ectx = NULL;
  349. OSSL_DECODER_CTX *dctx = NULL;
  350. void *decoded_legacy_key = NULL;
  351. EVP_PKEY *decoded_legacy_pkey = NULL;
  352. EVP_PKEY *decoded_provided_pkey = NULL;
  353. /* Set up the BIOs, so we have them */
  354. if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
  355. || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
  356. goto end;
  357. if (!TEST_ptr(ectx =
  358. OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
  359. "PEM", structure,
  360. NULL))
  361. || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
  362. || !TEST_true(pem_write_bio(membio_legacy, legacy_key))
  363. || !test_membio_str_eq(membio_provided, membio_legacy))
  364. goto end;
  365. if (pem_read_bio != NULL) {
  366. /* Now try decoding the results and compare the resulting keys */
  367. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  368. || !TEST_ptr(dctx =
  369. OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
  370. "PEM", structure,
  371. keytype, selection,
  372. NULL, NULL))
  373. || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
  374. || !TEST_ptr(decoded_legacy_key =
  375. pem_read_bio(membio_legacy, NULL, NULL, NULL))
  376. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  377. decoded_legacy_key)))
  378. goto end;
  379. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  380. decoded_legacy_pkey), 0)) {
  381. TEST_info("decoded_provided_pkey:");
  382. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  383. TEST_info("decoded_legacy_pkey:");
  384. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  385. }
  386. }
  387. ok = 1;
  388. end:
  389. EVP_PKEY_free(decoded_legacy_pkey);
  390. EVP_PKEY_free(decoded_provided_pkey);
  391. OSSL_ENCODER_CTX_free(ectx);
  392. OSSL_DECODER_CTX_free(dctx);
  393. BIO_free(membio_provided);
  394. BIO_free(membio_legacy);
  395. return ok;
  396. }
  397. static int test_DER(const char *keytype, int evp_type,
  398. const void *legacy_key, i2d_of_void *i2d, d2i_of_void *d2i,
  399. EVP_PKEY_eq_fn *evp_pkey_eq,
  400. EVP_PKEY_print_fn *evp_pkey_print,
  401. EVP_PKEY *provided_pkey, int selection,
  402. const char *structure)
  403. {
  404. int ok = 0;
  405. unsigned char *der_legacy = NULL;
  406. const unsigned char *pder_legacy = NULL;
  407. size_t der_legacy_len = 0;
  408. unsigned char *der_provided = NULL;
  409. const unsigned char *pder_provided = NULL;
  410. size_t der_provided_len = 0;
  411. size_t tmp_size;
  412. OSSL_ENCODER_CTX *ectx = NULL;
  413. OSSL_DECODER_CTX *dctx = NULL;
  414. void *decoded_legacy_key = NULL;
  415. EVP_PKEY *decoded_legacy_pkey = NULL;
  416. EVP_PKEY *decoded_provided_pkey = NULL;
  417. if (!TEST_ptr(ectx =
  418. OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
  419. "DER", structure,
  420. NULL))
  421. || !TEST_true(OSSL_ENCODER_to_data(ectx,
  422. &der_provided, &der_provided_len))
  423. || !TEST_size_t_gt(der_legacy_len = i2d(legacy_key, &der_legacy), 0)
  424. || !TEST_mem_eq(der_provided, der_provided_len,
  425. der_legacy, der_legacy_len))
  426. goto end;
  427. if (d2i != NULL) {
  428. /* Now try decoding the results and compare the resulting keys */
  429. if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
  430. || !TEST_ptr(dctx =
  431. OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
  432. "DER", structure,
  433. keytype, selection,
  434. NULL, NULL))
  435. || !TEST_true((pder_provided = der_provided,
  436. tmp_size = der_provided_len,
  437. OSSL_DECODER_from_data(dctx, &pder_provided,
  438. &tmp_size)))
  439. || !TEST_ptr((pder_legacy = der_legacy,
  440. decoded_legacy_key = d2i(NULL, &pder_legacy,
  441. (long)der_legacy_len)))
  442. || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
  443. decoded_legacy_key)))
  444. goto end;
  445. if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
  446. decoded_legacy_pkey), 0)) {
  447. TEST_info("decoded_provided_pkey:");
  448. evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
  449. TEST_info("decoded_legacy_pkey:");
  450. evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
  451. }
  452. }
  453. ok = 1;
  454. end:
  455. EVP_PKEY_free(decoded_legacy_pkey);
  456. EVP_PKEY_free(decoded_provided_pkey);
  457. OSSL_ENCODER_CTX_free(ectx);
  458. OSSL_DECODER_CTX_free(dctx);
  459. OPENSSL_free(der_provided);
  460. OPENSSL_free(der_legacy);
  461. return ok;
  462. }
  463. static int test_key(int idx)
  464. {
  465. struct test_stanza_st *test_stanza = NULL;
  466. struct key_st *key = NULL;
  467. int ok = 0;
  468. size_t i;
  469. EVP_PKEY *pkey = NULL, *downgraded_pkey = NULL;
  470. const void *legacy_obj = NULL;
  471. /* Get the test data */
  472. if (!TEST_ptr(test_stanza = &test_stanzas[idx])
  473. || !TEST_ptr(key = lookup_key(test_stanza->keytype)))
  474. goto end;
  475. /* Set up the keys */
  476. if (!TEST_ptr(pkey = key->key)
  477. || !TEST_true(evp_pkey_copy_downgraded(&downgraded_pkey, pkey))
  478. || !TEST_ptr(downgraded_pkey)
  479. || !TEST_int_eq(EVP_PKEY_get_id(downgraded_pkey), key->evp_type)
  480. || !TEST_ptr(legacy_obj = EVP_PKEY_get0(downgraded_pkey)))
  481. goto end;
  482. ok = 1;
  483. /* Test PrivateKey to PEM */
  484. if (test_stanza->pem_write_bio_PrivateKey != NULL) {
  485. int selection = OSSL_KEYMGMT_SELECT_ALL;
  486. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  487. const char *structure = test_stanza->structure[i];
  488. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PrivateKey for %s, %s",
  489. test_stanza->keytype, structure);
  490. if (!test_protected_PEM(key->keytype, key->evp_type, legacy_obj,
  491. test_stanza->pem_write_bio_PrivateKey,
  492. test_stanza->pem_read_bio_PrivateKey,
  493. EVP_PKEY_eq, EVP_PKEY_print_private,
  494. pkey, selection, structure))
  495. ok = 0;
  496. }
  497. }
  498. /* Test PublicKey to PEM */
  499. if (test_stanza->pem_write_bio_PublicKey != NULL) {
  500. int selection =
  501. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  502. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  503. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  504. const char *structure = test_stanza->structure[i];
  505. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PublicKey for %s, %s",
  506. test_stanza->keytype, structure);
  507. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  508. test_stanza->pem_write_bio_PublicKey,
  509. test_stanza->pem_read_bio_PublicKey,
  510. EVP_PKEY_eq, EVP_PKEY_print_public,
  511. pkey, selection, structure))
  512. ok = 0;
  513. }
  514. }
  515. /* Test params to PEM */
  516. if (test_stanza->pem_write_bio_params != NULL) {
  517. int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  518. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  519. const char *structure = test_stanza->structure[i];
  520. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}params for %s, %s",
  521. test_stanza->keytype, structure);
  522. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  523. test_stanza->pem_write_bio_params,
  524. test_stanza->pem_read_bio_params,
  525. EVP_PKEY_parameters_eq,
  526. EVP_PKEY_print_params,
  527. pkey, selection, structure))
  528. ok = 0;
  529. }
  530. }
  531. /* Test PUBKEY to PEM */
  532. if (test_stanza->pem_write_bio_PUBKEY != NULL) {
  533. int selection =
  534. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  535. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  536. const char *structure = "SubjectPublicKeyInfo";
  537. TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}_PUBKEY for %s, %s",
  538. test_stanza->keytype, structure);
  539. if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
  540. test_stanza->pem_write_bio_PUBKEY,
  541. test_stanza->pem_read_bio_PUBKEY,
  542. EVP_PKEY_eq, EVP_PKEY_print_public,
  543. pkey, selection, structure))
  544. ok = 0;
  545. }
  546. /* Test PrivateKey to DER */
  547. if (test_stanza->i2d_PrivateKey != NULL) {
  548. int selection = OSSL_KEYMGMT_SELECT_ALL;
  549. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  550. const char *structure = test_stanza->structure[i];
  551. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PrivateKey for %s, %s",
  552. test_stanza->keytype, structure);
  553. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  554. test_stanza->i2d_PrivateKey,
  555. test_stanza->d2i_PrivateKey,
  556. EVP_PKEY_eq, EVP_PKEY_print_private,
  557. pkey, selection, structure))
  558. ok = 0;
  559. }
  560. }
  561. /* Test PublicKey to DER */
  562. if (test_stanza->i2d_PublicKey != NULL) {
  563. int selection =
  564. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  565. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  566. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  567. const char *structure = test_stanza->structure[i];
  568. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PublicKey for %s, %s",
  569. test_stanza->keytype, structure);
  570. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  571. test_stanza->i2d_PublicKey,
  572. test_stanza->d2i_PublicKey,
  573. EVP_PKEY_eq, EVP_PKEY_print_public,
  574. pkey, selection, structure))
  575. ok = 0;
  576. }
  577. }
  578. /* Test params to DER */
  579. if (test_stanza->i2d_params != NULL) {
  580. int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  581. for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
  582. const char *structure = test_stanza->structure[i];
  583. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}params for %s, %s",
  584. test_stanza->keytype, structure);
  585. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  586. test_stanza->i2d_params, test_stanza->d2i_params,
  587. EVP_PKEY_parameters_eq, EVP_PKEY_print_params,
  588. pkey, selection, structure))
  589. ok = 0;
  590. }
  591. }
  592. /* Test PUBKEY to DER */
  593. if (test_stanza->i2d_PUBKEY != NULL) {
  594. int selection =
  595. OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  596. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
  597. const char *structure = "SubjectPublicKeyInfo";
  598. TEST_info("Test OSSL_ENCODER against i2d_{TYPE}_PUBKEY for %s, %s",
  599. test_stanza->keytype, structure);
  600. if (!test_DER(key->keytype, key->evp_type, legacy_obj,
  601. test_stanza->i2d_PUBKEY, test_stanza->d2i_PUBKEY,
  602. EVP_PKEY_eq, EVP_PKEY_print_public,
  603. pkey, selection, structure))
  604. ok = 0;
  605. }
  606. end:
  607. EVP_PKEY_free(downgraded_pkey);
  608. return ok;
  609. }
  610. #define USAGE "rsa-key.pem dh-key.pem\n"
  611. OPT_TEST_DECLARE_USAGE(USAGE)
  612. int setup_tests(void)
  613. {
  614. size_t i;
  615. if (!test_skip_common_options()) {
  616. TEST_error("Error parsing test options\n");
  617. return 0;
  618. }
  619. if (test_get_argument_count() != 2) {
  620. TEST_error("usage: endecoder_legacy_test %s", USAGE);
  621. return 0;
  622. }
  623. TEST_info("Generating keys...");
  624. for (i = 0; i < OSSL_NELEM(keys); i++) {
  625. #ifndef OPENSSL_NO_DH
  626. if (strcmp(keys[i].keytype, "DH") == 0) {
  627. if (!TEST_ptr(keys[i].key =
  628. load_pkey_pem(test_get_argument(1), NULL)))
  629. return 0;
  630. continue;
  631. }
  632. #endif
  633. #ifndef OPENSSL_NO_DEPRECATED_3_0
  634. if (strcmp(keys[i].keytype, "RSA") == 0) {
  635. if (!TEST_ptr(keys[i].key =
  636. load_pkey_pem(test_get_argument(0), NULL)))
  637. return 0;
  638. continue;
  639. }
  640. #endif
  641. TEST_info("Generating %s key...", keys[i].keytype);
  642. if (!TEST_ptr(keys[i].key =
  643. make_key(keys[i].keytype, keys[i].template_params)))
  644. return 0;
  645. }
  646. TEST_info("Generating keys done");
  647. ADD_ALL_TESTS(test_key, OSSL_NELEM(test_stanzas));
  648. return 1;
  649. }
  650. void cleanup_tests(void)
  651. {
  652. size_t i;
  653. for (i = 0; i < OSSL_NELEM(keys); i++)
  654. EVP_PKEY_free(keys[i].key);
  655. }