dh_ameth.c 25 KB

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