dh_ameth.c 25 KB

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