dh_ameth.c 22 KB

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