dh_ameth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/x509.h>
  17. #include <openssl/asn1.h>
  18. #include "dh_local.h"
  19. #include <openssl/bn.h>
  20. #include "crypto/asn1.h"
  21. #include "crypto/dh.h"
  22. #include "crypto/evp.h"
  23. #include <openssl/cms.h>
  24. #include <openssl/core_names.h>
  25. #include <openssl/param_build.h>
  26. #include "internal/ffc.h"
  27. /*
  28. * i2d/d2i like DH parameter functions which use the appropriate routine for
  29. * PKCS#3 DH or X9.42 DH.
  30. */
  31. static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp,
  32. long length)
  33. {
  34. if (pkey->ameth == &dhx_asn1_meth)
  35. return d2i_DHxparams(NULL, pp, length);
  36. return d2i_DHparams(NULL, pp, length);
  37. }
  38. static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp)
  39. {
  40. if (pkey->ameth == &dhx_asn1_meth)
  41. return i2d_DHxparams(a, pp);
  42. return i2d_DHparams(a, pp);
  43. }
  44. static void int_dh_free(EVP_PKEY *pkey)
  45. {
  46. DH_free(pkey->pkey.dh);
  47. }
  48. static int dh_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
  49. {
  50. const unsigned char *p, *pm;
  51. int pklen, pmlen;
  52. int ptype;
  53. const void *pval;
  54. const ASN1_STRING *pstr;
  55. X509_ALGOR *palg;
  56. ASN1_INTEGER *public_key = NULL;
  57. DH *dh = NULL;
  58. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  59. return 0;
  60. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  61. if (ptype != V_ASN1_SEQUENCE) {
  62. DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
  63. goto err;
  64. }
  65. pstr = pval;
  66. pm = pstr->data;
  67. pmlen = pstr->length;
  68. if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL) {
  69. DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
  70. goto err;
  71. }
  72. if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
  73. DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
  74. goto err;
  75. }
  76. /* We have parameters now set public key */
  77. if ((dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
  78. DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR);
  79. goto err;
  80. }
  81. ASN1_INTEGER_free(public_key);
  82. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
  83. return 1;
  84. err:
  85. ASN1_INTEGER_free(public_key);
  86. DH_free(dh);
  87. return 0;
  88. }
  89. static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  90. {
  91. DH *dh;
  92. int ptype;
  93. unsigned char *penc = NULL;
  94. int penclen;
  95. ASN1_STRING *str;
  96. ASN1_INTEGER *pub_key = NULL;
  97. dh = pkey->pkey.dh;
  98. str = ASN1_STRING_new();
  99. if (str == NULL) {
  100. DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  101. goto err;
  102. }
  103. str->length = i2d_dhp(pkey, dh, &str->data);
  104. if (str->length <= 0) {
  105. DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  106. goto err;
  107. }
  108. ptype = V_ASN1_SEQUENCE;
  109. pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
  110. if (pub_key == NULL)
  111. goto err;
  112. penclen = i2d_ASN1_INTEGER(pub_key, &penc);
  113. ASN1_INTEGER_free(pub_key);
  114. if (penclen <= 0) {
  115. DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  116. goto err;
  117. }
  118. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  119. ptype, str, penc, penclen))
  120. return 1;
  121. err:
  122. OPENSSL_free(penc);
  123. ASN1_STRING_free(str);
  124. return 0;
  125. }
  126. /*
  127. * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in that
  128. * the AlgorithmIdentifier contains the parameters, the private key is
  129. * explicitly included and the pubkey must be recalculated.
  130. */
  131. static int dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  132. {
  133. const unsigned char *p, *pm;
  134. int pklen, pmlen;
  135. int ptype;
  136. const void *pval;
  137. const ASN1_STRING *pstr;
  138. const X509_ALGOR *palg;
  139. ASN1_INTEGER *privkey = NULL;
  140. DH *dh = NULL;
  141. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  142. return 0;
  143. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  144. if (ptype != V_ASN1_SEQUENCE)
  145. goto decerr;
  146. if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
  147. goto decerr;
  148. pstr = pval;
  149. pm = pstr->data;
  150. pmlen = pstr->length;
  151. if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL)
  152. goto decerr;
  153. /* We have parameters now set private key */
  154. if ((dh->priv_key = BN_secure_new()) == NULL
  155. || !ASN1_INTEGER_to_BN(privkey, dh->priv_key)) {
  156. DHerr(DH_F_DH_PRIV_DECODE, DH_R_BN_ERROR);
  157. goto dherr;
  158. }
  159. /* Calculate public key, increments dirty_cnt */
  160. if (!DH_generate_key(dh))
  161. goto dherr;
  162. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
  163. ASN1_STRING_clear_free(privkey);
  164. return 1;
  165. decerr:
  166. DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
  167. dherr:
  168. DH_free(dh);
  169. ASN1_STRING_clear_free(privkey);
  170. return 0;
  171. }
  172. static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  173. {
  174. ASN1_STRING *params = NULL;
  175. ASN1_INTEGER *prkey = NULL;
  176. unsigned char *dp = NULL;
  177. int dplen;
  178. params = ASN1_STRING_new();
  179. if (params == NULL) {
  180. DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  181. goto err;
  182. }
  183. params->length = i2d_dhp(pkey, pkey->pkey.dh, &params->data);
  184. if (params->length <= 0) {
  185. DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  186. goto err;
  187. }
  188. params->type = V_ASN1_SEQUENCE;
  189. /* Get private key into integer */
  190. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
  191. if (prkey == NULL) {
  192. DHerr(DH_F_DH_PRIV_ENCODE, DH_R_BN_ERROR);
  193. goto err;
  194. }
  195. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  196. ASN1_STRING_clear_free(prkey);
  197. prkey = NULL;
  198. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  199. V_ASN1_SEQUENCE, params, dp, dplen))
  200. goto err;
  201. return 1;
  202. err:
  203. OPENSSL_free(dp);
  204. ASN1_STRING_free(params);
  205. ASN1_STRING_clear_free(prkey);
  206. return 0;
  207. }
  208. static int dh_param_decode(EVP_PKEY *pkey,
  209. const unsigned char **pder, int derlen)
  210. {
  211. DH *dh;
  212. if ((dh = d2i_dhp(pkey, pder, derlen)) == NULL) {
  213. DHerr(DH_F_DH_PARAM_DECODE, ERR_R_DH_LIB);
  214. return 0;
  215. }
  216. dh->dirty_cnt++;
  217. EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh);
  218. return 1;
  219. }
  220. static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  221. {
  222. return i2d_dhp(pkey, pkey->pkey.dh, pder);
  223. }
  224. static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype)
  225. {
  226. int reason = ERR_R_BUF_LIB;
  227. const char *ktype = NULL;
  228. BIGNUM *priv_key, *pub_key;
  229. if (ptype == 2)
  230. priv_key = x->priv_key;
  231. else
  232. priv_key = NULL;
  233. if (ptype > 0)
  234. pub_key = x->pub_key;
  235. else
  236. pub_key = NULL;
  237. if (x->params.p == NULL || (ptype == 2 && priv_key == NULL)
  238. || (ptype > 0 && pub_key == NULL)) {
  239. reason = ERR_R_PASSED_NULL_PARAMETER;
  240. goto err;
  241. }
  242. if (ptype == 2)
  243. ktype = "DH Private-Key";
  244. else if (ptype == 1)
  245. ktype = "DH Public-Key";
  246. else
  247. ktype = "DH Parameters";
  248. if (!BIO_indent(bp, indent, 128)
  249. || BIO_printf(bp, "%s: (%d bit)\n", ktype, DH_bits(x)) <= 0)
  250. goto err;
  251. indent += 4;
  252. if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent))
  253. goto err;
  254. if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent))
  255. goto err;
  256. if (!ffc_params_print(bp, &x->params, indent))
  257. goto err;
  258. if (x->length != 0) {
  259. if (!BIO_indent(bp, indent, 128)
  260. || BIO_printf(bp, "recommended-private-length: %d bits\n",
  261. (int)x->length) <= 0)
  262. goto err;
  263. }
  264. return 1;
  265. err:
  266. DHerr(DH_F_DO_DH_PRINT, reason);
  267. return 0;
  268. }
  269. static int int_dh_size(const EVP_PKEY *pkey)
  270. {
  271. return DH_size(pkey->pkey.dh);
  272. }
  273. static int dh_bits(const EVP_PKEY *pkey)
  274. {
  275. return DH_bits(pkey->pkey.dh);
  276. }
  277. static int dh_security_bits(const EVP_PKEY *pkey)
  278. {
  279. return DH_security_bits(pkey->pkey.dh);
  280. }
  281. static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  282. {
  283. return ffc_params_cmp(&a->pkey.dh->params, &a->pkey.dh->params,
  284. a->ameth != &dhx_asn1_meth);
  285. }
  286. static int int_dh_param_copy(DH *to, const DH *from, int is_x942)
  287. {
  288. if (is_x942 == -1)
  289. is_x942 = (from->params.q != NULL);
  290. if (!ffc_params_copy(&to->params, &from->params))
  291. return 0;
  292. if (!is_x942)
  293. to->length = from->length;
  294. to->dirty_cnt++;
  295. return 1;
  296. }
  297. DH *DHparams_dup(const DH *dh)
  298. {
  299. DH *ret;
  300. ret = DH_new();
  301. if (ret == NULL)
  302. return NULL;
  303. if (!int_dh_param_copy(ret, dh, -1)) {
  304. DH_free(ret);
  305. return NULL;
  306. }
  307. return ret;
  308. }
  309. static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  310. {
  311. if (to->pkey.dh == NULL) {
  312. to->pkey.dh = DH_new();
  313. if (to->pkey.dh == NULL)
  314. return 0;
  315. }
  316. return int_dh_param_copy(to->pkey.dh, from->pkey.dh,
  317. from->ameth == &dhx_asn1_meth);
  318. }
  319. static int dh_missing_parameters(const EVP_PKEY *a)
  320. {
  321. return a->pkey.dh == NULL
  322. || a->pkey.dh->params.p == NULL
  323. || a->pkey.dh->params.g == NULL;
  324. }
  325. static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  326. {
  327. if (dh_cmp_parameters(a, b) == 0)
  328. return 0;
  329. if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0)
  330. return 0;
  331. else
  332. return 1;
  333. }
  334. static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  335. ASN1_PCTX *ctx)
  336. {
  337. return do_dh_print(bp, pkey->pkey.dh, indent, 0);
  338. }
  339. static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  340. ASN1_PCTX *ctx)
  341. {
  342. return do_dh_print(bp, pkey->pkey.dh, indent, 1);
  343. }
  344. static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  345. ASN1_PCTX *ctx)
  346. {
  347. return do_dh_print(bp, pkey->pkey.dh, indent, 2);
  348. }
  349. int DHparams_print(BIO *bp, const DH *x)
  350. {
  351. return do_dh_print(bp, x, 4, 0);
  352. }
  353. #ifndef OPENSSL_NO_CMS
  354. static int dh_cms_decrypt(CMS_RecipientInfo *ri);
  355. static int dh_cms_encrypt(CMS_RecipientInfo *ri);
  356. #endif
  357. static int dh_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  358. {
  359. switch (op) {
  360. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
  361. return dh_buf2key(EVP_PKEY_get0_DH(pkey), arg2, arg1);
  362. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  363. return dh_key2buf(EVP_PKEY_get0_DH(pkey), arg2, 0, 1);
  364. default:
  365. return -2;
  366. }
  367. }
  368. static int dhx_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  369. {
  370. switch (op) {
  371. #ifndef OPENSSL_NO_CMS
  372. case ASN1_PKEY_CTRL_CMS_ENVELOPE:
  373. if (arg1 == 1)
  374. return dh_cms_decrypt(arg2);
  375. else if (arg1 == 0)
  376. return dh_cms_encrypt(arg2);
  377. return -2;
  378. case ASN1_PKEY_CTRL_CMS_RI_TYPE:
  379. *(int *)arg2 = CMS_RECIPINFO_AGREE;
  380. return 1;
  381. #endif
  382. default:
  383. return -2;
  384. }
  385. }
  386. static int dh_pkey_public_check(const EVP_PKEY *pkey)
  387. {
  388. DH *dh = pkey->pkey.dh;
  389. if (dh->pub_key == NULL) {
  390. DHerr(DH_F_DH_PKEY_PUBLIC_CHECK, DH_R_MISSING_PUBKEY);
  391. return 0;
  392. }
  393. return DH_check_pub_key_ex(dh, dh->pub_key);
  394. }
  395. static int dh_pkey_param_check(const EVP_PKEY *pkey)
  396. {
  397. DH *dh = pkey->pkey.dh;
  398. return DH_check_ex(dh);
  399. }
  400. static size_t dh_pkey_dirty_cnt(const EVP_PKEY *pkey)
  401. {
  402. return pkey->pkey.dh->dirty_cnt;
  403. }
  404. static int dh_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
  405. EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
  406. const char *propq)
  407. {
  408. DH *dh = from->pkey.dh;
  409. OSSL_PARAM_BLD *tmpl;
  410. const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh);
  411. const BIGNUM *pub_key = DH_get0_pub_key(dh);
  412. const BIGNUM *priv_key = DH_get0_priv_key(dh);
  413. OSSL_PARAM *params = NULL;
  414. int selection = 0;
  415. int rv = 0;
  416. /*
  417. * If the DH method is foreign, then we can't be sure of anything, and
  418. * can therefore not export or pretend to export.
  419. */
  420. if (dh_get_method(dh) != DH_OpenSSL())
  421. return 0;
  422. if (p == NULL || g == NULL)
  423. return 0;
  424. tmpl = OSSL_PARAM_BLD_new();
  425. if (tmpl == NULL)
  426. return 0;
  427. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
  428. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
  429. goto err;
  430. if (q != NULL) {
  431. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q))
  432. goto err;
  433. }
  434. selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  435. if (pub_key != NULL) {
  436. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
  437. goto err;
  438. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  439. }
  440. if (priv_key != NULL) {
  441. if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
  442. priv_key))
  443. goto err;
  444. selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  445. }
  446. if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  447. goto err;
  448. /* We export, the provider imports */
  449. rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
  450. OSSL_PARAM_BLD_free_params(params);
  451. err:
  452. OSSL_PARAM_BLD_free(tmpl);
  453. return rv;
  454. }
  455. static int dh_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
  456. {
  457. EVP_PKEY_CTX *pctx = vpctx;
  458. EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  459. DH *dh = dh_new_with_libctx(pctx->libctx);
  460. if (dh == NULL) {
  461. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  462. return 0;
  463. }
  464. if (!dh_ffc_params_fromdata(dh, params)
  465. || !dh_key_fromdata(dh, params)
  466. || !EVP_PKEY_assign_DH(pkey, dh)) {
  467. DH_free(dh);
  468. return 0;
  469. }
  470. return 1;
  471. }
  472. const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
  473. EVP_PKEY_DH,
  474. EVP_PKEY_DH,
  475. 0,
  476. "DH",
  477. "OpenSSL PKCS#3 DH method",
  478. dh_pub_decode,
  479. dh_pub_encode,
  480. dh_pub_cmp,
  481. dh_public_print,
  482. dh_priv_decode,
  483. dh_priv_encode,
  484. dh_private_print,
  485. int_dh_size,
  486. dh_bits,
  487. dh_security_bits,
  488. dh_param_decode,
  489. dh_param_encode,
  490. dh_missing_parameters,
  491. dh_copy_parameters,
  492. dh_cmp_parameters,
  493. dh_param_print,
  494. 0,
  495. int_dh_free,
  496. dh_pkey_ctrl,
  497. 0, 0, 0, 0, 0,
  498. 0,
  499. dh_pkey_public_check,
  500. dh_pkey_param_check,
  501. 0, 0, 0, 0,
  502. dh_pkey_dirty_cnt,
  503. dh_pkey_export_to,
  504. dh_pkey_import_from,
  505. };
  506. const EVP_PKEY_ASN1_METHOD dhx_asn1_meth = {
  507. EVP_PKEY_DHX,
  508. EVP_PKEY_DHX,
  509. 0,
  510. "X9.42 DH",
  511. "OpenSSL X9.42 DH method",
  512. dh_pub_decode,
  513. dh_pub_encode,
  514. dh_pub_cmp,
  515. dh_public_print,
  516. dh_priv_decode,
  517. dh_priv_encode,
  518. dh_private_print,
  519. int_dh_size,
  520. dh_bits,
  521. dh_security_bits,
  522. dh_param_decode,
  523. dh_param_encode,
  524. dh_missing_parameters,
  525. dh_copy_parameters,
  526. dh_cmp_parameters,
  527. dh_param_print,
  528. 0,
  529. int_dh_free,
  530. dhx_pkey_ctrl,
  531. 0, 0, 0, 0, 0,
  532. 0,
  533. dh_pkey_public_check,
  534. dh_pkey_param_check
  535. };
  536. #ifndef OPENSSL_NO_CMS
  537. static int dh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
  538. X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
  539. {
  540. const ASN1_OBJECT *aoid;
  541. int atype;
  542. const void *aval;
  543. ASN1_INTEGER *public_key = NULL;
  544. int rv = 0;
  545. EVP_PKEY *pkpeer = NULL, *pk = NULL;
  546. DH *dhpeer = NULL;
  547. const unsigned char *p;
  548. int plen;
  549. X509_ALGOR_get0(&aoid, &atype, &aval, alg);
  550. if (OBJ_obj2nid(aoid) != NID_dhpublicnumber)
  551. goto err;
  552. /* Only absent parameters allowed in RFC XXXX */
  553. if (atype != V_ASN1_UNDEF && atype == V_ASN1_NULL)
  554. goto err;
  555. pk = EVP_PKEY_CTX_get0_pkey(pctx);
  556. if (pk == NULL)
  557. goto err;
  558. if (pk->type != EVP_PKEY_DHX)
  559. goto err;
  560. /* Get parameters from parent key */
  561. dhpeer = DHparams_dup(pk->pkey.dh);
  562. /* We have parameters now set public key */
  563. plen = ASN1_STRING_length(pubkey);
  564. p = ASN1_STRING_get0_data(pubkey);
  565. if (p == NULL || plen == 0)
  566. goto err;
  567. if ((public_key = d2i_ASN1_INTEGER(NULL, &p, plen)) == NULL) {
  568. DHerr(DH_F_DH_CMS_SET_PEERKEY, DH_R_DECODE_ERROR);
  569. goto err;
  570. }
  571. /* We have parameters now set public key */
  572. if ((dhpeer->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
  573. DHerr(DH_F_DH_CMS_SET_PEERKEY, DH_R_BN_DECODE_ERROR);
  574. goto err;
  575. }
  576. pkpeer = EVP_PKEY_new();
  577. if (pkpeer == NULL)
  578. goto err;
  579. EVP_PKEY_assign(pkpeer, pk->ameth->pkey_id, dhpeer);
  580. dhpeer = NULL;
  581. if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
  582. rv = 1;
  583. err:
  584. ASN1_INTEGER_free(public_key);
  585. EVP_PKEY_free(pkpeer);
  586. DH_free(dhpeer);
  587. return rv;
  588. }
  589. static int dh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
  590. {
  591. int rv = 0;
  592. X509_ALGOR *alg, *kekalg = NULL;
  593. ASN1_OCTET_STRING *ukm;
  594. const unsigned char *p;
  595. unsigned char *dukm = NULL;
  596. size_t dukmlen = 0;
  597. int keylen, plen;
  598. const EVP_CIPHER *kekcipher;
  599. EVP_CIPHER_CTX *kekctx;
  600. if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
  601. goto err;
  602. /*
  603. * For DH we only have one OID permissible. If ever any more get defined
  604. * we will need something cleverer.
  605. */
  606. if (OBJ_obj2nid(alg->algorithm) != NID_id_smime_alg_ESDH) {
  607. DHerr(DH_F_DH_CMS_SET_SHARED_INFO, DH_R_KDF_PARAMETER_ERROR);
  608. goto err;
  609. }
  610. if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, EVP_PKEY_DH_KDF_X9_42) <= 0)
  611. goto err;
  612. if (EVP_PKEY_CTX_set_dh_kdf_md(pctx, EVP_sha1()) <= 0)
  613. goto err;
  614. if (alg->parameter->type != V_ASN1_SEQUENCE)
  615. goto err;
  616. p = alg->parameter->value.sequence->data;
  617. plen = alg->parameter->value.sequence->length;
  618. kekalg = d2i_X509_ALGOR(NULL, &p, plen);
  619. if (!kekalg)
  620. goto err;
  621. kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  622. if (!kekctx)
  623. goto err;
  624. kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
  625. if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
  626. goto err;
  627. if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
  628. goto err;
  629. if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
  630. goto err;
  631. keylen = EVP_CIPHER_CTX_key_length(kekctx);
  632. if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0)
  633. goto err;
  634. /* Use OBJ_nid2obj to ensure we use built in OID that isn't freed */
  635. if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx,
  636. OBJ_nid2obj(EVP_CIPHER_type(kekcipher)))
  637. <= 0)
  638. goto err;
  639. if (ukm) {
  640. dukmlen = ASN1_STRING_length(ukm);
  641. dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen);
  642. if (!dukm)
  643. goto err;
  644. }
  645. if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0)
  646. goto err;
  647. dukm = NULL;
  648. rv = 1;
  649. err:
  650. X509_ALGOR_free(kekalg);
  651. OPENSSL_free(dukm);
  652. return rv;
  653. }
  654. static int dh_cms_decrypt(CMS_RecipientInfo *ri)
  655. {
  656. EVP_PKEY_CTX *pctx;
  657. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  658. if (pctx == NULL)
  659. return 0;
  660. /* See if we need to set peer key */
  661. if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
  662. X509_ALGOR *alg;
  663. ASN1_BIT_STRING *pubkey;
  664. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
  665. NULL, NULL, NULL))
  666. return 0;
  667. if (!alg || !pubkey)
  668. return 0;
  669. if (!dh_cms_set_peerkey(pctx, alg, pubkey)) {
  670. DHerr(DH_F_DH_CMS_DECRYPT, DH_R_PEER_KEY_ERROR);
  671. return 0;
  672. }
  673. }
  674. /* Set DH derivation parameters and initialise unwrap context */
  675. if (!dh_cms_set_shared_info(pctx, ri)) {
  676. DHerr(DH_F_DH_CMS_DECRYPT, DH_R_SHARED_INFO_ERROR);
  677. return 0;
  678. }
  679. return 1;
  680. }
  681. static int dh_cms_encrypt(CMS_RecipientInfo *ri)
  682. {
  683. EVP_PKEY_CTX *pctx;
  684. EVP_PKEY *pkey;
  685. EVP_CIPHER_CTX *ctx;
  686. int keylen;
  687. X509_ALGOR *talg, *wrap_alg = NULL;
  688. const ASN1_OBJECT *aoid;
  689. ASN1_BIT_STRING *pubkey;
  690. ASN1_STRING *wrap_str;
  691. ASN1_OCTET_STRING *ukm;
  692. unsigned char *penc = NULL, *dukm = NULL;
  693. int penclen;
  694. size_t dukmlen = 0;
  695. int rv = 0;
  696. int kdf_type, wrap_nid;
  697. const EVP_MD *kdf_md;
  698. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  699. if (pctx == NULL)
  700. return 0;
  701. /* Get ephemeral key */
  702. pkey = EVP_PKEY_CTX_get0_pkey(pctx);
  703. if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
  704. NULL, NULL, NULL))
  705. goto err;
  706. X509_ALGOR_get0(&aoid, NULL, NULL, talg);
  707. /* Is everything uninitialised? */
  708. if (aoid == OBJ_nid2obj(NID_undef)) {
  709. ASN1_INTEGER *pubk = BN_to_ASN1_INTEGER(pkey->pkey.dh->pub_key, NULL);
  710. if (pubk == NULL)
  711. goto err;
  712. /* Set the key */
  713. penclen = i2d_ASN1_INTEGER(pubk, &penc);
  714. ASN1_INTEGER_free(pubk);
  715. if (penclen <= 0)
  716. goto err;
  717. ASN1_STRING_set0(pubkey, penc, penclen);
  718. pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  719. pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  720. penc = NULL;
  721. X509_ALGOR_set0(talg, OBJ_nid2obj(NID_dhpublicnumber),
  722. V_ASN1_UNDEF, NULL);
  723. }
  724. /* See if custom parameters set */
  725. kdf_type = EVP_PKEY_CTX_get_dh_kdf_type(pctx);
  726. if (kdf_type <= 0)
  727. goto err;
  728. if (!EVP_PKEY_CTX_get_dh_kdf_md(pctx, &kdf_md))
  729. goto err;
  730. if (kdf_type == EVP_PKEY_DH_KDF_NONE) {
  731. kdf_type = EVP_PKEY_DH_KDF_X9_42;
  732. if (EVP_PKEY_CTX_set_dh_kdf_type(pctx, kdf_type) <= 0)
  733. goto err;
  734. } else if (kdf_type != EVP_PKEY_DH_KDF_X9_42)
  735. /* Unknown KDF */
  736. goto err;
  737. if (kdf_md == NULL) {
  738. /* Only SHA1 supported */
  739. kdf_md = EVP_sha1();
  740. if (EVP_PKEY_CTX_set_dh_kdf_md(pctx, kdf_md) <= 0)
  741. goto err;
  742. } else if (EVP_MD_type(kdf_md) != NID_sha1)
  743. /* Unsupported digest */
  744. goto err;
  745. if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
  746. goto err;
  747. /* Get wrap NID */
  748. ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  749. wrap_nid = EVP_CIPHER_CTX_type(ctx);
  750. if (EVP_PKEY_CTX_set0_dh_kdf_oid(pctx, OBJ_nid2obj(wrap_nid)) <= 0)
  751. goto err;
  752. keylen = EVP_CIPHER_CTX_key_length(ctx);
  753. /* Package wrap algorithm in an AlgorithmIdentifier */
  754. wrap_alg = X509_ALGOR_new();
  755. if (wrap_alg == NULL)
  756. goto err;
  757. wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
  758. wrap_alg->parameter = ASN1_TYPE_new();
  759. if (wrap_alg->parameter == NULL)
  760. goto err;
  761. if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
  762. goto err;
  763. if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
  764. ASN1_TYPE_free(wrap_alg->parameter);
  765. wrap_alg->parameter = NULL;
  766. }
  767. if (EVP_PKEY_CTX_set_dh_kdf_outlen(pctx, keylen) <= 0)
  768. goto err;
  769. if (ukm) {
  770. dukmlen = ASN1_STRING_length(ukm);
  771. dukm = OPENSSL_memdup(ASN1_STRING_get0_data(ukm), dukmlen);
  772. if (!dukm)
  773. goto err;
  774. }
  775. if (EVP_PKEY_CTX_set0_dh_kdf_ukm(pctx, dukm, dukmlen) <= 0)
  776. goto err;
  777. dukm = NULL;
  778. /*
  779. * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
  780. * of another AlgorithmIdentifier.
  781. */
  782. penc = NULL;
  783. penclen = i2d_X509_ALGOR(wrap_alg, &penc);
  784. if (penc == NULL || penclen == 0)
  785. goto err;
  786. wrap_str = ASN1_STRING_new();
  787. if (wrap_str == NULL)
  788. goto err;
  789. ASN1_STRING_set0(wrap_str, penc, penclen);
  790. penc = NULL;
  791. X509_ALGOR_set0(talg, OBJ_nid2obj(NID_id_smime_alg_ESDH),
  792. V_ASN1_SEQUENCE, wrap_str);
  793. rv = 1;
  794. err:
  795. OPENSSL_free(penc);
  796. X509_ALGOR_free(wrap_alg);
  797. OPENSSL_free(dukm);
  798. return rv;
  799. }
  800. #endif