dh_ameth.c 24 KB

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