encode_key2any.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*
  2. * Copyright 2020-2024 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. * Low level APIs are deprecated for public use, but still ok for internal use.
  11. */
  12. #include "internal/deprecated.h"
  13. #include <openssl/core.h>
  14. #include <openssl/core_dispatch.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/params.h>
  18. #include <openssl/asn1.h>
  19. #include <openssl/err.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/pkcs12.h> /* PKCS8_encrypt() */
  23. #include <openssl/dh.h>
  24. #include <openssl/dsa.h>
  25. #include <openssl/ec.h>
  26. #include <openssl/proverr.h>
  27. #include "internal/passphrase.h"
  28. #include "internal/cryptlib.h"
  29. #include "crypto/ecx.h"
  30. #include "crypto/rsa.h"
  31. #include "prov/implementations.h"
  32. #include "prov/bio.h"
  33. #include "prov/provider_ctx.h"
  34. #include "prov/der_rsa.h"
  35. #include "endecoder_local.h"
  36. #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
  37. # define OPENSSL_NO_KEYPARAMS
  38. #endif
  39. struct key2any_ctx_st {
  40. PROV_CTX *provctx;
  41. /* Set to 0 if parameters should not be saved (dsa only) */
  42. int save_parameters;
  43. /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
  44. int cipher_intent;
  45. EVP_CIPHER *cipher;
  46. struct ossl_passphrase_data_st pwdata;
  47. };
  48. typedef int check_key_type_fn(const void *key, int nid);
  49. typedef int key_to_paramstring_fn(const void *key, int nid, int save,
  50. void **str, int *strtype);
  51. typedef int key_to_der_fn(BIO *out, const void *key,
  52. int key_nid, const char *pemname,
  53. key_to_paramstring_fn *p2s, i2d_of_void *k2d,
  54. struct key2any_ctx_st *ctx);
  55. typedef int write_bio_of_void_fn(BIO *bp, const void *x);
  56. /* Free the blob allocated during key_to_paramstring_fn */
  57. static void free_asn1_data(int type, void *data)
  58. {
  59. switch (type) {
  60. case V_ASN1_OBJECT:
  61. ASN1_OBJECT_free(data);
  62. break;
  63. case V_ASN1_SEQUENCE:
  64. ASN1_STRING_free(data);
  65. break;
  66. }
  67. }
  68. static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid,
  69. void *params, int params_type,
  70. i2d_of_void *k2d)
  71. {
  72. /* der, derlen store the key DER output and its length */
  73. unsigned char *der = NULL;
  74. int derlen;
  75. /* The final PKCS#8 info */
  76. PKCS8_PRIV_KEY_INFO *p8info = NULL;
  77. if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
  78. || (derlen = k2d(key, &der)) <= 0
  79. || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0,
  80. params_type, params, der, derlen)) {
  81. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  82. PKCS8_PRIV_KEY_INFO_free(p8info);
  83. OPENSSL_free(der);
  84. p8info = NULL;
  85. }
  86. return p8info;
  87. }
  88. static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info,
  89. struct key2any_ctx_st *ctx)
  90. {
  91. X509_SIG *p8 = NULL;
  92. char kstr[PEM_BUFSIZE];
  93. size_t klen = 0;
  94. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  95. if (ctx->cipher == NULL)
  96. return NULL;
  97. if (!ossl_pw_get_passphrase(kstr, sizeof(kstr), &klen, NULL, 1,
  98. &ctx->pwdata)) {
  99. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
  100. return NULL;
  101. }
  102. /* First argument == -1 means "standard" */
  103. p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info, libctx, NULL);
  104. OPENSSL_cleanse(kstr, klen);
  105. return p8;
  106. }
  107. static X509_SIG *key_to_encp8(const void *key, int key_nid,
  108. void *params, int params_type,
  109. i2d_of_void *k2d, struct key2any_ctx_st *ctx)
  110. {
  111. PKCS8_PRIV_KEY_INFO *p8info =
  112. key_to_p8info(key, key_nid, params, params_type, k2d);
  113. X509_SIG *p8 = NULL;
  114. if (p8info == NULL) {
  115. free_asn1_data(params_type, params);
  116. } else {
  117. p8 = p8info_to_encp8(p8info, ctx);
  118. PKCS8_PRIV_KEY_INFO_free(p8info);
  119. }
  120. return p8;
  121. }
  122. static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid,
  123. void *params, int params_type,
  124. i2d_of_void k2d)
  125. {
  126. /* der, derlen store the key DER output and its length */
  127. unsigned char *der = NULL;
  128. int derlen;
  129. /* The final X509_PUBKEY */
  130. X509_PUBKEY *xpk = NULL;
  131. if ((xpk = X509_PUBKEY_new()) == NULL
  132. || (derlen = k2d(key, &der)) <= 0
  133. || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid),
  134. params_type, params, der, derlen)) {
  135. ERR_raise(ERR_LIB_PROV, ERR_R_X509_LIB);
  136. X509_PUBKEY_free(xpk);
  137. OPENSSL_free(der);
  138. xpk = NULL;
  139. }
  140. return xpk;
  141. }
  142. /*
  143. * key_to_epki_* produce encoded output with the private key data in a
  144. * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require
  145. * that there's an intent to encrypt, anything else is an error.
  146. *
  147. * key_to_pki_* primarily produce encoded output with the private key data
  148. * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if
  149. * there is an intent to encrypt the data, the corresponding key_to_epki_*
  150. * function is used instead.
  151. *
  152. * key_to_spki_* produce encoded output with the public key data in an
  153. * X.509 SubjectPublicKeyInfo.
  154. *
  155. * Key parameters don't have any defined envelopment of this kind, but are
  156. * included in some manner in the output from the functions described above,
  157. * either in the AlgorithmIdentifier's parameter field, or as part of the
  158. * key data itself.
  159. */
  160. static int key_to_epki_der_priv_bio(BIO *out, const void *key,
  161. int key_nid,
  162. ossl_unused const char *pemname,
  163. key_to_paramstring_fn *p2s,
  164. i2d_of_void *k2d,
  165. struct key2any_ctx_st *ctx)
  166. {
  167. int ret = 0;
  168. void *str = NULL;
  169. int strtype = V_ASN1_UNDEF;
  170. X509_SIG *p8;
  171. if (!ctx->cipher_intent)
  172. return 0;
  173. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  174. &str, &strtype))
  175. return 0;
  176. p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
  177. if (p8 != NULL)
  178. ret = i2d_PKCS8_bio(out, p8);
  179. X509_SIG_free(p8);
  180. return ret;
  181. }
  182. static int key_to_epki_pem_priv_bio(BIO *out, const void *key,
  183. int key_nid,
  184. ossl_unused const char *pemname,
  185. key_to_paramstring_fn *p2s,
  186. i2d_of_void *k2d,
  187. struct key2any_ctx_st *ctx)
  188. {
  189. int ret = 0;
  190. void *str = NULL;
  191. int strtype = V_ASN1_UNDEF;
  192. X509_SIG *p8;
  193. if (!ctx->cipher_intent)
  194. return 0;
  195. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  196. &str, &strtype))
  197. return 0;
  198. p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
  199. if (p8 != NULL)
  200. ret = PEM_write_bio_PKCS8(out, p8);
  201. X509_SIG_free(p8);
  202. return ret;
  203. }
  204. static int key_to_pki_der_priv_bio(BIO *out, const void *key,
  205. int key_nid,
  206. ossl_unused const char *pemname,
  207. key_to_paramstring_fn *p2s,
  208. i2d_of_void *k2d,
  209. struct key2any_ctx_st *ctx)
  210. {
  211. int ret = 0;
  212. void *str = NULL;
  213. int strtype = V_ASN1_UNDEF;
  214. PKCS8_PRIV_KEY_INFO *p8info;
  215. if (ctx->cipher_intent)
  216. return key_to_epki_der_priv_bio(out, key, key_nid, pemname,
  217. p2s, k2d, ctx);
  218. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  219. &str, &strtype))
  220. return 0;
  221. p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
  222. if (p8info != NULL)
  223. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
  224. else
  225. free_asn1_data(strtype, str);
  226. PKCS8_PRIV_KEY_INFO_free(p8info);
  227. return ret;
  228. }
  229. static int key_to_pki_pem_priv_bio(BIO *out, const void *key,
  230. int key_nid,
  231. ossl_unused const char *pemname,
  232. key_to_paramstring_fn *p2s,
  233. i2d_of_void *k2d,
  234. struct key2any_ctx_st *ctx)
  235. {
  236. int ret = 0;
  237. void *str = NULL;
  238. int strtype = V_ASN1_UNDEF;
  239. PKCS8_PRIV_KEY_INFO *p8info;
  240. if (ctx->cipher_intent)
  241. return key_to_epki_pem_priv_bio(out, key, key_nid, pemname,
  242. p2s, k2d, ctx);
  243. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  244. &str, &strtype))
  245. return 0;
  246. p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
  247. if (p8info != NULL)
  248. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
  249. else
  250. free_asn1_data(strtype, str);
  251. PKCS8_PRIV_KEY_INFO_free(p8info);
  252. return ret;
  253. }
  254. static int key_to_spki_der_pub_bio(BIO *out, const void *key,
  255. int key_nid,
  256. ossl_unused const char *pemname,
  257. key_to_paramstring_fn *p2s,
  258. i2d_of_void *k2d,
  259. struct key2any_ctx_st *ctx)
  260. {
  261. int ret = 0;
  262. void *str = NULL;
  263. int strtype = V_ASN1_UNDEF;
  264. X509_PUBKEY *xpk = NULL;
  265. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  266. &str, &strtype))
  267. return 0;
  268. xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
  269. if (xpk != NULL)
  270. ret = i2d_X509_PUBKEY_bio(out, xpk);
  271. /* Also frees |str| */
  272. X509_PUBKEY_free(xpk);
  273. return ret;
  274. }
  275. static int key_to_spki_pem_pub_bio(BIO *out, const void *key,
  276. int key_nid,
  277. ossl_unused const char *pemname,
  278. key_to_paramstring_fn *p2s,
  279. i2d_of_void *k2d,
  280. struct key2any_ctx_st *ctx)
  281. {
  282. int ret = 0;
  283. void *str = NULL;
  284. int strtype = V_ASN1_UNDEF;
  285. X509_PUBKEY *xpk = NULL;
  286. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  287. &str, &strtype))
  288. return 0;
  289. xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
  290. if (xpk != NULL)
  291. ret = PEM_write_bio_X509_PUBKEY(out, xpk);
  292. else
  293. free_asn1_data(strtype, str);
  294. /* Also frees |str| */
  295. X509_PUBKEY_free(xpk);
  296. return ret;
  297. }
  298. /*
  299. * key_to_type_specific_* produce encoded output with type specific key data,
  300. * no envelopment; the same kind of output as the type specific i2d_ and
  301. * PEM_write_ functions, which is often a simple SEQUENCE of INTEGER.
  302. *
  303. * OpenSSL tries to discourage production of new keys in this form, because
  304. * of the ambiguity when trying to recognise them, but can't deny that PKCS#1
  305. * et al still are live standards.
  306. *
  307. * Note that these functions completely ignore p2s, and rather rely entirely
  308. * on k2d to do the complete work.
  309. */
  310. static int key_to_type_specific_der_bio(BIO *out, const void *key,
  311. int key_nid,
  312. ossl_unused const char *pemname,
  313. key_to_paramstring_fn *p2s,
  314. i2d_of_void *k2d,
  315. struct key2any_ctx_st *ctx)
  316. {
  317. unsigned char *der = NULL;
  318. int derlen;
  319. int ret;
  320. if ((derlen = k2d(key, &der)) <= 0) {
  321. ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB);
  322. return 0;
  323. }
  324. ret = BIO_write(out, der, derlen);
  325. OPENSSL_free(der);
  326. return ret > 0;
  327. }
  328. #define key_to_type_specific_der_priv_bio key_to_type_specific_der_bio
  329. #define key_to_type_specific_der_pub_bio key_to_type_specific_der_bio
  330. #define key_to_type_specific_der_param_bio key_to_type_specific_der_bio
  331. static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key,
  332. int key_nid, const char *pemname,
  333. key_to_paramstring_fn *p2s,
  334. i2d_of_void *k2d,
  335. struct key2any_ctx_st *ctx,
  336. pem_password_cb *cb, void *cbarg)
  337. {
  338. return
  339. PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher,
  340. NULL, 0, cb, cbarg) > 0;
  341. }
  342. static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key,
  343. int key_nid, const char *pemname,
  344. key_to_paramstring_fn *p2s,
  345. i2d_of_void *k2d,
  346. struct key2any_ctx_st *ctx)
  347. {
  348. return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
  349. p2s, k2d, ctx,
  350. ossl_pw_pem_password, &ctx->pwdata);
  351. }
  352. static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key,
  353. int key_nid, const char *pemname,
  354. key_to_paramstring_fn *p2s,
  355. i2d_of_void *k2d,
  356. struct key2any_ctx_st *ctx)
  357. {
  358. return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
  359. p2s, k2d, ctx, NULL, NULL);
  360. }
  361. #ifndef OPENSSL_NO_KEYPARAMS
  362. static int key_to_type_specific_pem_param_bio(BIO *out, const void *key,
  363. int key_nid, const char *pemname,
  364. key_to_paramstring_fn *p2s,
  365. i2d_of_void *k2d,
  366. struct key2any_ctx_st *ctx)
  367. {
  368. return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
  369. p2s, k2d, ctx, NULL, NULL);
  370. }
  371. #endif
  372. /* ---------------------------------------------------------------------- */
  373. #ifndef OPENSSL_NO_DH
  374. static int prepare_dh_params(const void *dh, int nid, int save,
  375. void **pstr, int *pstrtype)
  376. {
  377. ASN1_STRING *params = ASN1_STRING_new();
  378. if (params == NULL) {
  379. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  380. return 0;
  381. }
  382. if (nid == EVP_PKEY_DHX)
  383. params->length = i2d_DHxparams(dh, &params->data);
  384. else
  385. params->length = i2d_DHparams(dh, &params->data);
  386. if (params->length <= 0) {
  387. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  388. ASN1_STRING_free(params);
  389. return 0;
  390. }
  391. params->type = V_ASN1_SEQUENCE;
  392. *pstr = params;
  393. *pstrtype = V_ASN1_SEQUENCE;
  394. return 1;
  395. }
  396. static int dh_spki_pub_to_der(const void *dh, unsigned char **pder)
  397. {
  398. const BIGNUM *bn = NULL;
  399. ASN1_INTEGER *pub_key = NULL;
  400. int ret;
  401. if ((bn = DH_get0_pub_key(dh)) == NULL) {
  402. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  403. return 0;
  404. }
  405. if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  406. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  407. return 0;
  408. }
  409. ret = i2d_ASN1_INTEGER(pub_key, pder);
  410. ASN1_STRING_clear_free(pub_key);
  411. return ret;
  412. }
  413. static int dh_pki_priv_to_der(const void *dh, unsigned char **pder)
  414. {
  415. const BIGNUM *bn = NULL;
  416. ASN1_INTEGER *priv_key = NULL;
  417. int ret;
  418. if ((bn = DH_get0_priv_key(dh)) == NULL) {
  419. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  420. return 0;
  421. }
  422. if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  423. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  424. return 0;
  425. }
  426. ret = i2d_ASN1_INTEGER(priv_key, pder);
  427. ASN1_STRING_clear_free(priv_key);
  428. return ret;
  429. }
  430. # define dh_epki_priv_to_der dh_pki_priv_to_der
  431. static int dh_type_specific_params_to_der(const void *dh, unsigned char **pder)
  432. {
  433. if (DH_test_flags(dh, DH_FLAG_TYPE_DHX))
  434. return i2d_DHxparams(dh, pder);
  435. return i2d_DHparams(dh, pder);
  436. }
  437. /*
  438. * DH doesn't have i2d_DHPrivateKey or i2d_DHPublicKey, so we can't make
  439. * corresponding functions here.
  440. */
  441. # define dh_type_specific_priv_to_der NULL
  442. # define dh_type_specific_pub_to_der NULL
  443. static int dh_check_key_type(const void *dh, int expected_type)
  444. {
  445. int type =
  446. DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
  447. return type == expected_type;
  448. }
  449. # define dh_evp_type EVP_PKEY_DH
  450. # define dhx_evp_type EVP_PKEY_DHX
  451. # define dh_input_type "DH"
  452. # define dhx_input_type "DHX"
  453. # define dh_pem_type "DH"
  454. # define dhx_pem_type "X9.42 DH"
  455. #endif
  456. /* ---------------------------------------------------------------------- */
  457. #ifndef OPENSSL_NO_DSA
  458. static int encode_dsa_params(const void *dsa, int nid,
  459. void **pstr, int *pstrtype)
  460. {
  461. ASN1_STRING *params = ASN1_STRING_new();
  462. if (params == NULL) {
  463. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  464. return 0;
  465. }
  466. params->length = i2d_DSAparams(dsa, &params->data);
  467. if (params->length <= 0) {
  468. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  469. ASN1_STRING_free(params);
  470. return 0;
  471. }
  472. *pstrtype = V_ASN1_SEQUENCE;
  473. *pstr = params;
  474. return 1;
  475. }
  476. static int prepare_dsa_params(const void *dsa, int nid, int save,
  477. void **pstr, int *pstrtype)
  478. {
  479. const BIGNUM *p = DSA_get0_p(dsa);
  480. const BIGNUM *q = DSA_get0_q(dsa);
  481. const BIGNUM *g = DSA_get0_g(dsa);
  482. if (save && p != NULL && q != NULL && g != NULL)
  483. return encode_dsa_params(dsa, nid, pstr, pstrtype);
  484. *pstr = NULL;
  485. *pstrtype = V_ASN1_UNDEF;
  486. return 1;
  487. }
  488. static int dsa_spki_pub_to_der(const void *dsa, unsigned char **pder)
  489. {
  490. const BIGNUM *bn = NULL;
  491. ASN1_INTEGER *pub_key = NULL;
  492. int ret;
  493. if ((bn = DSA_get0_pub_key(dsa)) == NULL) {
  494. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  495. return 0;
  496. }
  497. if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  498. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  499. return 0;
  500. }
  501. ret = i2d_ASN1_INTEGER(pub_key, pder);
  502. ASN1_STRING_clear_free(pub_key);
  503. return ret;
  504. }
  505. static int dsa_pki_priv_to_der(const void *dsa, unsigned char **pder)
  506. {
  507. const BIGNUM *bn = NULL;
  508. ASN1_INTEGER *priv_key = NULL;
  509. int ret;
  510. if ((bn = DSA_get0_priv_key(dsa)) == NULL) {
  511. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
  512. return 0;
  513. }
  514. if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
  515. ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
  516. return 0;
  517. }
  518. ret = i2d_ASN1_INTEGER(priv_key, pder);
  519. ASN1_STRING_clear_free(priv_key);
  520. return ret;
  521. }
  522. # define dsa_epki_priv_to_der dsa_pki_priv_to_der
  523. # define dsa_type_specific_priv_to_der (i2d_of_void *)i2d_DSAPrivateKey
  524. # define dsa_type_specific_pub_to_der (i2d_of_void *)i2d_DSAPublicKey
  525. # define dsa_type_specific_params_to_der (i2d_of_void *)i2d_DSAparams
  526. # define dsa_check_key_type NULL
  527. # define dsa_evp_type EVP_PKEY_DSA
  528. # define dsa_input_type "DSA"
  529. # define dsa_pem_type "DSA"
  530. #endif
  531. /* ---------------------------------------------------------------------- */
  532. #ifndef OPENSSL_NO_EC
  533. static int prepare_ec_explicit_params(const void *eckey,
  534. void **pstr, int *pstrtype)
  535. {
  536. ASN1_STRING *params = ASN1_STRING_new();
  537. if (params == NULL) {
  538. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  539. return 0;
  540. }
  541. params->length = i2d_ECParameters(eckey, &params->data);
  542. if (params->length <= 0) {
  543. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  544. ASN1_STRING_free(params);
  545. return 0;
  546. }
  547. *pstrtype = V_ASN1_SEQUENCE;
  548. *pstr = params;
  549. return 1;
  550. }
  551. /*
  552. * This implements EcpkParameters, where the CHOICE is based on whether there
  553. * is a curve name (curve nid) to be found or not. See RFC 3279 for details.
  554. */
  555. static int prepare_ec_params(const void *eckey, int nid, int save,
  556. void **pstr, int *pstrtype)
  557. {
  558. int curve_nid;
  559. const EC_GROUP *group = EC_KEY_get0_group(eckey);
  560. ASN1_OBJECT *params = NULL;
  561. if (group == NULL)
  562. return 0;
  563. curve_nid = EC_GROUP_get_curve_name(group);
  564. if (curve_nid != NID_undef) {
  565. params = OBJ_nid2obj(curve_nid);
  566. if (params == NULL)
  567. return 0;
  568. }
  569. if (curve_nid != NID_undef
  570. && (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE)) {
  571. /* The CHOICE came to namedCurve */
  572. if (OBJ_length(params) == 0) {
  573. /* Some curves might not have an associated OID */
  574. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_OID);
  575. ASN1_OBJECT_free(params);
  576. return 0;
  577. }
  578. *pstr = params;
  579. *pstrtype = V_ASN1_OBJECT;
  580. return 1;
  581. } else {
  582. /* The CHOICE came to ecParameters */
  583. return prepare_ec_explicit_params(eckey, pstr, pstrtype);
  584. }
  585. }
  586. static int ec_spki_pub_to_der(const void *eckey, unsigned char **pder)
  587. {
  588. if (EC_KEY_get0_public_key(eckey) == NULL) {
  589. ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
  590. return 0;
  591. }
  592. return i2o_ECPublicKey(eckey, pder);
  593. }
  594. static int ec_pki_priv_to_der(const void *veckey, unsigned char **pder)
  595. {
  596. EC_KEY *eckey = (EC_KEY *)veckey;
  597. unsigned int old_flags;
  598. int ret = 0;
  599. /*
  600. * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
  601. * as the pkeyalg->parameter field. (For a named curve this is an OID)
  602. * The pkey field is an octet string that holds the encoded
  603. * ECPrivateKey SEQUENCE with the optional parameters field omitted.
  604. * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
  605. */
  606. old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */
  607. EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS);
  608. ret = i2d_ECPrivateKey(eckey, pder);
  609. EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */
  610. return ret; /* return the length of the der encoded data */
  611. }
  612. # define ec_epki_priv_to_der ec_pki_priv_to_der
  613. # define ec_type_specific_params_to_der (i2d_of_void *)i2d_ECParameters
  614. /* No ec_type_specific_pub_to_der, there simply is no such thing */
  615. # define ec_type_specific_priv_to_der (i2d_of_void *)i2d_ECPrivateKey
  616. # define ec_check_key_type NULL
  617. # define ec_evp_type EVP_PKEY_EC
  618. # define ec_input_type "EC"
  619. # define ec_pem_type "EC"
  620. # ifndef OPENSSL_NO_SM2
  621. /*
  622. * Albeit SM2 is a slightly different algorithm than ECDSA, the key type
  623. * encoding (in all places where an AlgorithmIdentifier is produced, such
  624. * as PrivateKeyInfo and SubjectPublicKeyInfo) is the same as for ECC keys
  625. * according to the example in GM/T 0015-2012, appendix D.2.
  626. * This leaves the distinction of SM2 keys to the EC group (which is found
  627. * in AlgorithmIdentified.params).
  628. */
  629. # define sm2_evp_type ec_evp_type
  630. # define sm2_input_type "SM2"
  631. # define sm2_pem_type "SM2"
  632. # endif
  633. #endif
  634. /* ---------------------------------------------------------------------- */
  635. #ifndef OPENSSL_NO_ECX
  636. # define prepare_ecx_params NULL
  637. static int ecx_spki_pub_to_der(const void *vecxkey, unsigned char **pder)
  638. {
  639. const ECX_KEY *ecxkey = vecxkey;
  640. unsigned char *keyblob;
  641. if (ecxkey == NULL) {
  642. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  643. return 0;
  644. }
  645. keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
  646. if (keyblob == NULL)
  647. return 0;
  648. *pder = keyblob;
  649. return ecxkey->keylen;
  650. }
  651. static int ecx_pki_priv_to_der(const void *vecxkey, unsigned char **pder)
  652. {
  653. const ECX_KEY *ecxkey = vecxkey;
  654. ASN1_OCTET_STRING oct;
  655. int keybloblen;
  656. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  657. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  658. return 0;
  659. }
  660. oct.data = ecxkey->privkey;
  661. oct.length = ecxkey->keylen;
  662. oct.flags = 0;
  663. keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
  664. if (keybloblen < 0) {
  665. ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB);
  666. return 0;
  667. }
  668. return keybloblen;
  669. }
  670. # define ecx_epki_priv_to_der ecx_pki_priv_to_der
  671. /*
  672. * ED25519, ED448, X25519 and X448 only has PKCS#8 / SubjectPublicKeyInfo
  673. * representation, so we don't define ecx_type_specific_[priv,pub,params]_to_der.
  674. */
  675. # define ecx_check_key_type NULL
  676. # define ed25519_evp_type EVP_PKEY_ED25519
  677. # define ed448_evp_type EVP_PKEY_ED448
  678. # define x25519_evp_type EVP_PKEY_X25519
  679. # define x448_evp_type EVP_PKEY_X448
  680. # define ed25519_input_type "ED25519"
  681. # define ed448_input_type "ED448"
  682. # define x25519_input_type "X25519"
  683. # define x448_input_type "X448"
  684. # define ed25519_pem_type "ED25519"
  685. # define ed448_pem_type "ED448"
  686. # define x25519_pem_type "X25519"
  687. # define x448_pem_type "X448"
  688. #endif
  689. /* ---------------------------------------------------------------------- */
  690. /*
  691. * Helper functions to prepare RSA-PSS params for encoding. We would
  692. * have simply written the whole AlgorithmIdentifier, but existing libcrypto
  693. * functionality doesn't allow that.
  694. */
  695. static int prepare_rsa_params(const void *rsa, int nid, int save,
  696. void **pstr, int *pstrtype)
  697. {
  698. const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30((RSA *)rsa);
  699. *pstr = NULL;
  700. switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
  701. case RSA_FLAG_TYPE_RSA:
  702. /* If plain RSA, the parameters shall be NULL */
  703. *pstrtype = V_ASN1_NULL;
  704. return 1;
  705. case RSA_FLAG_TYPE_RSASSAPSS:
  706. if (ossl_rsa_pss_params_30_is_unrestricted(pss)) {
  707. *pstrtype = V_ASN1_UNDEF;
  708. return 1;
  709. } else {
  710. ASN1_STRING *astr = NULL;
  711. WPACKET pkt;
  712. unsigned char *str = NULL;
  713. size_t str_sz = 0;
  714. int i;
  715. for (i = 0; i < 2; i++) {
  716. switch (i) {
  717. case 0:
  718. if (!WPACKET_init_null_der(&pkt))
  719. goto err;
  720. break;
  721. case 1:
  722. if ((str = OPENSSL_malloc(str_sz)) == NULL
  723. || !WPACKET_init_der(&pkt, str, str_sz)) {
  724. WPACKET_cleanup(&pkt);
  725. goto err;
  726. }
  727. break;
  728. }
  729. if (!ossl_DER_w_RSASSA_PSS_params(&pkt, -1, pss)
  730. || !WPACKET_finish(&pkt)
  731. || !WPACKET_get_total_written(&pkt, &str_sz)) {
  732. WPACKET_cleanup(&pkt);
  733. goto err;
  734. }
  735. WPACKET_cleanup(&pkt);
  736. /*
  737. * If no PSS parameters are going to be written, there's no
  738. * point going for another iteration.
  739. * This saves us from getting |str| allocated just to have it
  740. * immediately de-allocated.
  741. */
  742. if (str_sz == 0)
  743. break;
  744. }
  745. if ((astr = ASN1_STRING_new()) == NULL)
  746. goto err;
  747. *pstrtype = V_ASN1_SEQUENCE;
  748. ASN1_STRING_set0(astr, str, (int)str_sz);
  749. *pstr = astr;
  750. return 1;
  751. err:
  752. OPENSSL_free(str);
  753. return 0;
  754. }
  755. }
  756. /* Currently unsupported RSA key type */
  757. return 0;
  758. }
  759. /*
  760. * RSA is extremely simple, as PKCS#1 is used for the PKCS#8 |privateKey|
  761. * field as well as the SubjectPublicKeyInfo |subjectPublicKey| field.
  762. */
  763. #define rsa_pki_priv_to_der rsa_type_specific_priv_to_der
  764. #define rsa_epki_priv_to_der rsa_type_specific_priv_to_der
  765. #define rsa_spki_pub_to_der rsa_type_specific_pub_to_der
  766. #define rsa_type_specific_priv_to_der (i2d_of_void *)i2d_RSAPrivateKey
  767. #define rsa_type_specific_pub_to_der (i2d_of_void *)i2d_RSAPublicKey
  768. #define rsa_type_specific_params_to_der NULL
  769. static int rsa_check_key_type(const void *rsa, int expected_type)
  770. {
  771. switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
  772. case RSA_FLAG_TYPE_RSA:
  773. return expected_type == EVP_PKEY_RSA;
  774. case RSA_FLAG_TYPE_RSASSAPSS:
  775. return expected_type == EVP_PKEY_RSA_PSS;
  776. }
  777. /* Currently unsupported RSA key type */
  778. return EVP_PKEY_NONE;
  779. }
  780. #define rsa_evp_type EVP_PKEY_RSA
  781. #define rsapss_evp_type EVP_PKEY_RSA_PSS
  782. #define rsa_input_type "RSA"
  783. #define rsapss_input_type "RSA-PSS"
  784. #define rsa_pem_type "RSA"
  785. #define rsapss_pem_type "RSA-PSS"
  786. /* ---------------------------------------------------------------------- */
  787. static OSSL_FUNC_decoder_newctx_fn key2any_newctx;
  788. static OSSL_FUNC_decoder_freectx_fn key2any_freectx;
  789. static void *key2any_newctx(void *provctx)
  790. {
  791. struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  792. if (ctx != NULL) {
  793. ctx->provctx = provctx;
  794. ctx->save_parameters = 1;
  795. }
  796. return ctx;
  797. }
  798. static void key2any_freectx(void *vctx)
  799. {
  800. struct key2any_ctx_st *ctx = vctx;
  801. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  802. EVP_CIPHER_free(ctx->cipher);
  803. OPENSSL_free(ctx);
  804. }
  805. static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx)
  806. {
  807. static const OSSL_PARAM settables[] = {
  808. OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0),
  809. OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0),
  810. OSSL_PARAM_END,
  811. };
  812. return settables;
  813. }
  814. static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  815. {
  816. struct key2any_ctx_st *ctx = vctx;
  817. OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
  818. const OSSL_PARAM *cipherp =
  819. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER);
  820. const OSSL_PARAM *propsp =
  821. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES);
  822. const OSSL_PARAM *save_paramsp =
  823. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS);
  824. if (cipherp != NULL) {
  825. const char *ciphername = NULL;
  826. const char *props = NULL;
  827. if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername))
  828. return 0;
  829. if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props))
  830. return 0;
  831. EVP_CIPHER_free(ctx->cipher);
  832. ctx->cipher = NULL;
  833. ctx->cipher_intent = ciphername != NULL;
  834. if (ciphername != NULL
  835. && ((ctx->cipher =
  836. EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL))
  837. return 0;
  838. }
  839. if (save_paramsp != NULL) {
  840. if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters))
  841. return 0;
  842. }
  843. return 1;
  844. }
  845. static int key2any_check_selection(int selection, int selection_mask)
  846. {
  847. /*
  848. * The selections are kinda sorta "levels", i.e. each selection given
  849. * here is assumed to include those following.
  850. */
  851. int checks[] = {
  852. OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  853. OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  854. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  855. };
  856. size_t i;
  857. /* The decoder implementations made here support guessing */
  858. if (selection == 0)
  859. return 1;
  860. for (i = 0; i < OSSL_NELEM(checks); i++) {
  861. int check1 = (selection & checks[i]) != 0;
  862. int check2 = (selection_mask & checks[i]) != 0;
  863. /*
  864. * If the caller asked for the currently checked bit(s), return
  865. * whether the decoder description says it's supported.
  866. */
  867. if (check1)
  868. return check2;
  869. }
  870. /* This should be dead code, but just to be safe... */
  871. return 0;
  872. }
  873. static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout,
  874. const void *key, int type, const char *pemname,
  875. check_key_type_fn *checker,
  876. key_to_der_fn *writer,
  877. OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg,
  878. key_to_paramstring_fn *key2paramstring,
  879. i2d_of_void *key2der)
  880. {
  881. int ret = 0;
  882. if (key == NULL) {
  883. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  884. } else if (writer != NULL
  885. && (checker == NULL || checker(key, type))) {
  886. BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
  887. if (out != NULL
  888. && (pwcb == NULL
  889. || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pwcb, pwcbarg)))
  890. ret =
  891. writer(out, key, type, pemname, key2paramstring, key2der, ctx);
  892. BIO_free(out);
  893. } else {
  894. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  895. }
  896. return ret;
  897. }
  898. #define DO_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY
  899. #define DO_PRIVATE_KEY(impl, type, kind, output) \
  900. if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \
  901. return key2any_encode(ctx, cout, key, impl##_evp_type, \
  902. impl##_pem_type " PRIVATE KEY", \
  903. type##_check_key_type, \
  904. key_to_##kind##_##output##_priv_bio, \
  905. cb, cbarg, prepare_##type##_params, \
  906. type##_##kind##_priv_to_der);
  907. #define DO_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  908. #define DO_PUBLIC_KEY(impl, type, kind, output) \
  909. if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \
  910. return key2any_encode(ctx, cout, key, impl##_evp_type, \
  911. impl##_pem_type " PUBLIC KEY", \
  912. type##_check_key_type, \
  913. key_to_##kind##_##output##_pub_bio, \
  914. cb, cbarg, prepare_##type##_params, \
  915. type##_##kind##_pub_to_der);
  916. #define DO_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  917. #define DO_PARAMETERS(impl, type, kind, output) \
  918. if ((selection & DO_PARAMETERS_selection_mask) != 0) \
  919. return key2any_encode(ctx, cout, key, impl##_evp_type, \
  920. impl##_pem_type " PARAMETERS", \
  921. type##_check_key_type, \
  922. key_to_##kind##_##output##_param_bio, \
  923. NULL, NULL, NULL, \
  924. type##_##kind##_params_to_der);
  925. /*-
  926. * Implement the kinds of output structure that can be produced. They are
  927. * referred to by name, and for each name, the following macros are defined
  928. * (braces not included):
  929. *
  930. * DO_{kind}_selection_mask
  931. *
  932. * A mask of selection bits that must not be zero. This is used as a
  933. * selection criterion for each implementation.
  934. * This mask must never be zero.
  935. *
  936. * DO_{kind}
  937. *
  938. * The performing macro. It must use the DO_ macros defined above,
  939. * always in this order:
  940. *
  941. * - DO_PRIVATE_KEY
  942. * - DO_PUBLIC_KEY
  943. * - DO_PARAMETERS
  944. *
  945. * Any of those may be omitted, but the relative order must still be
  946. * the same.
  947. */
  948. /*
  949. * PKCS#8 defines two structures for private keys only:
  950. * - PrivateKeyInfo (raw unencrypted form)
  951. * - EncryptedPrivateKeyInfo (encrypted wrapping)
  952. *
  953. * To allow a certain amount of flexibility, we allow the routines
  954. * for PrivateKeyInfo to also produce EncryptedPrivateKeyInfo if a
  955. * passphrase callback has been passed to them.
  956. */
  957. #define DO_PrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask
  958. #define DO_PrivateKeyInfo(impl, type, output) \
  959. DO_PRIVATE_KEY(impl, type, pki, output)
  960. #define DO_EncryptedPrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask
  961. #define DO_EncryptedPrivateKeyInfo(impl, type, output) \
  962. DO_PRIVATE_KEY(impl, type, epki, output)
  963. /* SubjectPublicKeyInfo is a structure for public keys only */
  964. #define DO_SubjectPublicKeyInfo_selection_mask DO_PUBLIC_KEY_selection_mask
  965. #define DO_SubjectPublicKeyInfo(impl, type, output) \
  966. DO_PUBLIC_KEY(impl, type, spki, output)
  967. /*
  968. * "type-specific" is a uniform name for key type specific output for private
  969. * and public keys as well as key parameters. This is used internally in
  970. * libcrypto so it doesn't have to have special knowledge about select key
  971. * types, but also when no better name has been found. If there are more
  972. * expressive DO_ names above, those are preferred.
  973. *
  974. * Three forms exist:
  975. *
  976. * - type_specific_keypair Only supports private and public key
  977. * - type_specific_params Only supports parameters
  978. * - type_specific Supports all parts of an EVP_PKEY
  979. * - type_specific_no_pub Supports all parts of an EVP_PKEY
  980. * except public key
  981. */
  982. #define DO_type_specific_params_selection_mask DO_PARAMETERS_selection_mask
  983. #define DO_type_specific_params(impl, type, output) \
  984. DO_PARAMETERS(impl, type, type_specific, output)
  985. #define DO_type_specific_keypair_selection_mask \
  986. ( DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask )
  987. #define DO_type_specific_keypair(impl, type, output) \
  988. DO_PRIVATE_KEY(impl, type, type_specific, output) \
  989. DO_PUBLIC_KEY(impl, type, type_specific, output)
  990. #define DO_type_specific_selection_mask \
  991. ( DO_type_specific_keypair_selection_mask \
  992. | DO_type_specific_params_selection_mask )
  993. #define DO_type_specific(impl, type, output) \
  994. DO_type_specific_keypair(impl, type, output) \
  995. DO_type_specific_params(impl, type, output)
  996. #define DO_type_specific_no_pub_selection_mask \
  997. ( DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask)
  998. #define DO_type_specific_no_pub(impl, type, output) \
  999. DO_PRIVATE_KEY(impl, type, type_specific, output) \
  1000. DO_type_specific_params(impl, type, output)
  1001. /*
  1002. * Type specific aliases for the cases where we need to refer to them by
  1003. * type name.
  1004. * This only covers key types that are represented with i2d_{TYPE}PrivateKey,
  1005. * i2d_{TYPE}PublicKey and i2d_{TYPE}params / i2d_{TYPE}Parameters.
  1006. */
  1007. #define DO_RSA_selection_mask DO_type_specific_keypair_selection_mask
  1008. #define DO_RSA(impl, type, output) DO_type_specific_keypair(impl, type, output)
  1009. #define DO_DH_selection_mask DO_type_specific_params_selection_mask
  1010. #define DO_DH(impl, type, output) DO_type_specific_params(impl, type, output)
  1011. #define DO_DHX_selection_mask DO_type_specific_params_selection_mask
  1012. #define DO_DHX(impl, type, output) DO_type_specific_params(impl, type, output)
  1013. #define DO_DSA_selection_mask DO_type_specific_selection_mask
  1014. #define DO_DSA(impl, type, output) DO_type_specific(impl, type, output)
  1015. #define DO_EC_selection_mask DO_type_specific_no_pub_selection_mask
  1016. #define DO_EC(impl, type, output) DO_type_specific_no_pub(impl, type, output)
  1017. #define DO_SM2_selection_mask DO_type_specific_no_pub_selection_mask
  1018. #define DO_SM2(impl, type, output) DO_type_specific_no_pub(impl, type, output)
  1019. /* PKCS#1 defines a structure for RSA private and public keys */
  1020. #define DO_PKCS1_selection_mask DO_RSA_selection_mask
  1021. #define DO_PKCS1(impl, type, output) DO_RSA(impl, type, output)
  1022. /* PKCS#3 defines a structure for DH parameters */
  1023. #define DO_PKCS3_selection_mask DO_DH_selection_mask
  1024. #define DO_PKCS3(impl, type, output) DO_DH(impl, type, output)
  1025. /* X9.42 defines a structure for DHx parameters */
  1026. #define DO_X9_42_selection_mask DO_DHX_selection_mask
  1027. #define DO_X9_42(impl, type, output) DO_DHX(impl, type, output)
  1028. /* X9.62 defines a structure for EC keys and parameters */
  1029. #define DO_X9_62_selection_mask DO_EC_selection_mask
  1030. #define DO_X9_62(impl, type, output) DO_EC(impl, type, output)
  1031. /*
  1032. * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables.
  1033. * It takes the following arguments:
  1034. *
  1035. * impl This is the key type name that's being implemented.
  1036. * type This is the type name for the set of functions that implement
  1037. * the key type. For example, ed25519, ed448, x25519 and x448
  1038. * are all implemented with the exact same set of functions.
  1039. * evp_type The corresponding EVP_PKEY_xxx type macro for each key.
  1040. * Necessary because we currently use EVP_PKEY with legacy
  1041. * native keys internally. This will need to be refactored
  1042. * when that legacy support goes away.
  1043. * kind What kind of support to implement. These translate into
  1044. * the DO_##kind macros above.
  1045. * output The output type to implement. may be der or pem.
  1046. *
  1047. * The resulting OSSL_DISPATCH array gets the following name (expressed in
  1048. * C preprocessor terms) from those arguments:
  1049. *
  1050. * ossl_##impl##_to_##kind##_##output##_encoder_functions
  1051. */
  1052. #define MAKE_ENCODER(impl, type, evp_type, kind, output) \
  1053. static OSSL_FUNC_encoder_import_object_fn \
  1054. impl##_to_##kind##_##output##_import_object; \
  1055. static OSSL_FUNC_encoder_free_object_fn \
  1056. impl##_to_##kind##_##output##_free_object; \
  1057. static OSSL_FUNC_encoder_encode_fn \
  1058. impl##_to_##kind##_##output##_encode; \
  1059. \
  1060. static void * \
  1061. impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \
  1062. const OSSL_PARAM params[]) \
  1063. { \
  1064. struct key2any_ctx_st *ctx = vctx; \
  1065. \
  1066. return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
  1067. ctx->provctx, selection, params); \
  1068. } \
  1069. static void impl##_to_##kind##_##output##_free_object(void *key) \
  1070. { \
  1071. ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
  1072. } \
  1073. static int impl##_to_##kind##_##output##_does_selection(void *ctx, \
  1074. int selection) \
  1075. { \
  1076. return key2any_check_selection(selection, \
  1077. DO_##kind##_selection_mask); \
  1078. } \
  1079. static int \
  1080. impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \
  1081. const void *key, \
  1082. const OSSL_PARAM key_abstract[], \
  1083. int selection, \
  1084. OSSL_PASSPHRASE_CALLBACK *cb, \
  1085. void *cbarg) \
  1086. { \
  1087. /* We don't deal with abstract objects */ \
  1088. if (key_abstract != NULL) { \
  1089. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  1090. return 0; \
  1091. } \
  1092. DO_##kind(impl, type, output) \
  1093. \
  1094. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
  1095. return 0; \
  1096. } \
  1097. const OSSL_DISPATCH \
  1098. ossl_##impl##_to_##kind##_##output##_encoder_functions[] = { \
  1099. { OSSL_FUNC_ENCODER_NEWCTX, \
  1100. (void (*)(void))key2any_newctx }, \
  1101. { OSSL_FUNC_ENCODER_FREECTX, \
  1102. (void (*)(void))key2any_freectx }, \
  1103. { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
  1104. (void (*)(void))key2any_settable_ctx_params }, \
  1105. { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
  1106. (void (*)(void))key2any_set_ctx_params }, \
  1107. { OSSL_FUNC_ENCODER_DOES_SELECTION, \
  1108. (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \
  1109. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  1110. (void (*)(void))impl##_to_##kind##_##output##_import_object }, \
  1111. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  1112. (void (*)(void))impl##_to_##kind##_##output##_free_object }, \
  1113. { OSSL_FUNC_ENCODER_ENCODE, \
  1114. (void (*)(void))impl##_to_##kind##_##output##_encode }, \
  1115. OSSL_DISPATCH_END \
  1116. }
  1117. /*
  1118. * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey,
  1119. * i2d_{TYPE}params, as they exist.
  1120. */
  1121. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, der);
  1122. #ifndef OPENSSL_NO_DH
  1123. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, der);
  1124. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, der);
  1125. #endif
  1126. #ifndef OPENSSL_NO_DSA
  1127. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, der);
  1128. #endif
  1129. #ifndef OPENSSL_NO_EC
  1130. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, der);
  1131. # ifndef OPENSSL_NO_SM2
  1132. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, der);
  1133. # endif
  1134. #endif
  1135. /*
  1136. * Replacements for PEM_write_bio_{TYPE}PrivateKey,
  1137. * PEM_write_bio_{TYPE}PublicKey, PEM_write_bio_{TYPE}params, as they exist.
  1138. */
  1139. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, pem);
  1140. #ifndef OPENSSL_NO_DH
  1141. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, pem);
  1142. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, pem);
  1143. #endif
  1144. #ifndef OPENSSL_NO_DSA
  1145. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, pem);
  1146. #endif
  1147. #ifndef OPENSSL_NO_EC
  1148. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, pem);
  1149. # ifndef OPENSSL_NO_SM2
  1150. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, pem);
  1151. # endif
  1152. #endif
  1153. /*
  1154. * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the
  1155. * implementations specified above, but are more specific.
  1156. * The SubjectPublicKeyInfo implementations also replace the
  1157. * PEM_write_bio_{TYPE}_PUBKEY functions.
  1158. * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(),
  1159. * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters().
  1160. */
  1161. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, EncryptedPrivateKeyInfo, der);
  1162. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, EncryptedPrivateKeyInfo, pem);
  1163. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PrivateKeyInfo, der);
  1164. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PrivateKeyInfo, pem);
  1165. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, der);
  1166. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, pem);
  1167. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, EncryptedPrivateKeyInfo, der);
  1168. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, EncryptedPrivateKeyInfo, pem);
  1169. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PrivateKeyInfo, der);
  1170. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PrivateKeyInfo, pem);
  1171. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, der);
  1172. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, pem);
  1173. #ifndef OPENSSL_NO_DH
  1174. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, EncryptedPrivateKeyInfo, der);
  1175. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, EncryptedPrivateKeyInfo, pem);
  1176. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PrivateKeyInfo, der);
  1177. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PrivateKeyInfo, pem);
  1178. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, der);
  1179. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, pem);
  1180. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, EncryptedPrivateKeyInfo, der);
  1181. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, EncryptedPrivateKeyInfo, pem);
  1182. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PrivateKeyInfo, der);
  1183. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PrivateKeyInfo, pem);
  1184. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, der);
  1185. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, pem);
  1186. #endif
  1187. #ifndef OPENSSL_NO_DSA
  1188. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, EncryptedPrivateKeyInfo, der);
  1189. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, EncryptedPrivateKeyInfo, pem);
  1190. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PrivateKeyInfo, der);
  1191. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PrivateKeyInfo, pem);
  1192. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, der);
  1193. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, pem);
  1194. #endif
  1195. #ifndef OPENSSL_NO_EC
  1196. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, der);
  1197. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, pem);
  1198. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, der);
  1199. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, pem);
  1200. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der);
  1201. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem);
  1202. # ifndef OPENSSL_NO_SM2
  1203. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, der);
  1204. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, pem);
  1205. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, der);
  1206. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, pem);
  1207. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der);
  1208. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem);
  1209. # endif
  1210. # ifndef OPENSSL_NO_ECX
  1211. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, EncryptedPrivateKeyInfo, der);
  1212. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, EncryptedPrivateKeyInfo, pem);
  1213. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PrivateKeyInfo, der);
  1214. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PrivateKeyInfo, pem);
  1215. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, der);
  1216. MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, pem);
  1217. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, EncryptedPrivateKeyInfo, der);
  1218. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, EncryptedPrivateKeyInfo, pem);
  1219. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, der);
  1220. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, pem);
  1221. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
  1222. MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
  1223. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, EncryptedPrivateKeyInfo, der);
  1224. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, EncryptedPrivateKeyInfo, pem);
  1225. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PrivateKeyInfo, der);
  1226. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PrivateKeyInfo, pem);
  1227. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, der);
  1228. MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, pem);
  1229. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, EncryptedPrivateKeyInfo, der);
  1230. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, EncryptedPrivateKeyInfo, pem);
  1231. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, der);
  1232. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PrivateKeyInfo, pem);
  1233. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
  1234. MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
  1235. # endif
  1236. #endif
  1237. /*
  1238. * Support for key type specific output formats. Not all key types have
  1239. * this, we only aim to duplicate what is available in 1.1.1 as
  1240. * i2d_TYPEPrivateKey(), i2d_TYPEPublicKey() and i2d_TYPEparams().
  1241. * For example, there are no publicly available i2d_ function for
  1242. * ED25519, ED448, X25519 or X448, and they therefore only have PKCS#8
  1243. * and SubjectPublicKeyInfo implementations as implemented above.
  1244. */
  1245. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, der);
  1246. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, pem);
  1247. #ifndef OPENSSL_NO_DH
  1248. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, der);
  1249. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, pem);
  1250. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, der);
  1251. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, pem);
  1252. #endif
  1253. #ifndef OPENSSL_NO_DSA
  1254. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, der);
  1255. MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, pem);
  1256. #endif
  1257. #ifndef OPENSSL_NO_EC
  1258. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, der);
  1259. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, pem);
  1260. # ifndef OPENSSL_NO_SM2
  1261. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, der);
  1262. MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, pem);
  1263. # endif
  1264. #endif
  1265. /* Convenience structure names */
  1266. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, der);
  1267. MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, pem);
  1268. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, der);
  1269. MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, pem);
  1270. #ifndef OPENSSL_NO_DH
  1271. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, der); /* parameters only */
  1272. MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, pem); /* parameters only */
  1273. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, der); /* parameters only */
  1274. MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, pem); /* parameters only */
  1275. #endif
  1276. #ifndef OPENSSL_NO_EC
  1277. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, der);
  1278. MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, pem);
  1279. #endif