endecoder_legacy_test.c 28 KB

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