dh_pmeth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DH & DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/asn1t.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/evp.h>
  19. #include "dh_local.h"
  20. #include <openssl/bn.h>
  21. #include <openssl/dsa.h>
  22. #include <openssl/objects.h>
  23. #include "crypto/evp.h"
  24. /* DH pkey context structure */
  25. typedef struct {
  26. /* Parameter gen parameters */
  27. int prime_len;
  28. int generator;
  29. int paramgen_type;
  30. int subprime_len;
  31. int pad;
  32. /* message digest used for parameter generation */
  33. const EVP_MD *md;
  34. int rfc5114_param;
  35. int param_nid;
  36. /* Keygen callback info */
  37. int gentmp[2];
  38. /* KDF (if any) to use for DH */
  39. char kdf_type;
  40. /* OID to use for KDF */
  41. ASN1_OBJECT *kdf_oid;
  42. /* Message digest to use for key derivation */
  43. const EVP_MD *kdf_md;
  44. /* User key material */
  45. unsigned char *kdf_ukm;
  46. size_t kdf_ukmlen;
  47. /* KDF output length */
  48. size_t kdf_outlen;
  49. } DH_PKEY_CTX;
  50. static int pkey_dh_init(EVP_PKEY_CTX *ctx)
  51. {
  52. DH_PKEY_CTX *dctx;
  53. if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
  54. ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
  55. return 0;
  56. }
  57. dctx->prime_len = 2048;
  58. dctx->subprime_len = -1;
  59. dctx->generator = 2;
  60. dctx->kdf_type = EVP_PKEY_DH_KDF_NONE;
  61. ctx->data = dctx;
  62. ctx->keygen_info = dctx->gentmp;
  63. ctx->keygen_info_count = 2;
  64. return 1;
  65. }
  66. static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
  67. {
  68. DH_PKEY_CTX *dctx = ctx->data;
  69. if (dctx != NULL) {
  70. OPENSSL_free(dctx->kdf_ukm);
  71. ASN1_OBJECT_free(dctx->kdf_oid);
  72. OPENSSL_free(dctx);
  73. }
  74. }
  75. static int pkey_dh_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  76. {
  77. DH_PKEY_CTX *dctx, *sctx;
  78. if (!pkey_dh_init(dst))
  79. return 0;
  80. sctx = src->data;
  81. dctx = dst->data;
  82. dctx->prime_len = sctx->prime_len;
  83. dctx->subprime_len = sctx->subprime_len;
  84. dctx->generator = sctx->generator;
  85. dctx->paramgen_type = sctx->paramgen_type;
  86. dctx->pad = sctx->pad;
  87. dctx->md = sctx->md;
  88. dctx->rfc5114_param = sctx->rfc5114_param;
  89. dctx->param_nid = sctx->param_nid;
  90. dctx->kdf_type = sctx->kdf_type;
  91. dctx->kdf_oid = OBJ_dup(sctx->kdf_oid);
  92. if (dctx->kdf_oid == NULL)
  93. return 0;
  94. dctx->kdf_md = sctx->kdf_md;
  95. if (sctx->kdf_ukm != NULL) {
  96. dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
  97. if (dctx->kdf_ukm == NULL)
  98. return 0;
  99. dctx->kdf_ukmlen = sctx->kdf_ukmlen;
  100. }
  101. dctx->kdf_outlen = sctx->kdf_outlen;
  102. return 1;
  103. }
  104. static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  105. {
  106. DH_PKEY_CTX *dctx = ctx->data;
  107. switch (type) {
  108. case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
  109. if (p1 < 256)
  110. return -2;
  111. dctx->prime_len = p1;
  112. return 1;
  113. case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
  114. if (dctx->paramgen_type == DH_PARAMGEN_TYPE_GENERATOR)
  115. return -2;
  116. dctx->subprime_len = p1;
  117. return 1;
  118. case EVP_PKEY_CTRL_DH_PAD:
  119. dctx->pad = p1;
  120. return 1;
  121. case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
  122. if (dctx->paramgen_type != DH_PARAMGEN_TYPE_GENERATOR)
  123. return -2;
  124. dctx->generator = p1;
  125. return 1;
  126. case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
  127. #ifdef OPENSSL_NO_DSA
  128. if (p1 != DH_PARAMGEN_TYPE_GENERATOR)
  129. return -2;
  130. #else
  131. if (p1 < 0 || p1 > 2)
  132. return -2;
  133. #endif
  134. dctx->paramgen_type = p1;
  135. return 1;
  136. case EVP_PKEY_CTRL_DH_RFC5114:
  137. if (p1 < 1 || p1 > 3 || dctx->param_nid != NID_undef)
  138. return -2;
  139. dctx->rfc5114_param = p1;
  140. return 1;
  141. case EVP_PKEY_CTRL_DH_NID:
  142. if (p1 <= 0 || dctx->rfc5114_param != 0)
  143. return -2;
  144. dctx->param_nid = p1;
  145. return 1;
  146. case EVP_PKEY_CTRL_PEER_KEY:
  147. /* Default behaviour is OK */
  148. return 1;
  149. case EVP_PKEY_CTRL_DH_KDF_TYPE:
  150. if (p1 == -2)
  151. return dctx->kdf_type;
  152. if (p1 != EVP_PKEY_DH_KDF_NONE && p1 != EVP_PKEY_DH_KDF_X9_42)
  153. return -2;
  154. dctx->kdf_type = p1;
  155. return 1;
  156. case EVP_PKEY_CTRL_DH_KDF_MD:
  157. dctx->kdf_md = p2;
  158. return 1;
  159. case EVP_PKEY_CTRL_GET_DH_KDF_MD:
  160. *(const EVP_MD **)p2 = dctx->kdf_md;
  161. return 1;
  162. case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
  163. if (p1 <= 0)
  164. return -2;
  165. dctx->kdf_outlen = (size_t)p1;
  166. return 1;
  167. case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
  168. *(int *)p2 = dctx->kdf_outlen;
  169. return 1;
  170. case EVP_PKEY_CTRL_DH_KDF_UKM:
  171. OPENSSL_free(dctx->kdf_ukm);
  172. dctx->kdf_ukm = p2;
  173. if (p2)
  174. dctx->kdf_ukmlen = p1;
  175. else
  176. dctx->kdf_ukmlen = 0;
  177. return 1;
  178. case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
  179. *(unsigned char **)p2 = dctx->kdf_ukm;
  180. return dctx->kdf_ukmlen;
  181. case EVP_PKEY_CTRL_DH_KDF_OID:
  182. ASN1_OBJECT_free(dctx->kdf_oid);
  183. dctx->kdf_oid = p2;
  184. return 1;
  185. case EVP_PKEY_CTRL_GET_DH_KDF_OID:
  186. *(ASN1_OBJECT **)p2 = dctx->kdf_oid;
  187. return 1;
  188. default:
  189. return -2;
  190. }
  191. }
  192. static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
  193. const char *type, const char *value)
  194. {
  195. if (strcmp(type, "dh_paramgen_prime_len") == 0) {
  196. int len;
  197. len = atoi(value);
  198. return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
  199. }
  200. if (strcmp(type, "dh_rfc5114") == 0) {
  201. DH_PKEY_CTX *dctx = ctx->data;
  202. int len;
  203. len = atoi(value);
  204. if (len < 0 || len > 3)
  205. return -2;
  206. dctx->rfc5114_param = len;
  207. return 1;
  208. }
  209. if (strcmp(type, "dh_param") == 0) {
  210. DH_PKEY_CTX *dctx = ctx->data;
  211. int nid = OBJ_sn2nid(value);
  212. if (nid == NID_undef) {
  213. ERR_raise(ERR_LIB_DH, DH_R_INVALID_PARAMETER_NAME);
  214. return -2;
  215. }
  216. dctx->param_nid = nid;
  217. return 1;
  218. }
  219. if (strcmp(type, "dh_paramgen_generator") == 0) {
  220. int len;
  221. len = atoi(value);
  222. return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
  223. }
  224. if (strcmp(type, "dh_paramgen_subprime_len") == 0) {
  225. int len;
  226. len = atoi(value);
  227. return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
  228. }
  229. if (strcmp(type, "dh_paramgen_type") == 0) {
  230. int typ;
  231. typ = atoi(value);
  232. return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
  233. }
  234. if (strcmp(type, "dh_pad") == 0) {
  235. int pad;
  236. pad = atoi(value);
  237. return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
  238. }
  239. return -2;
  240. }
  241. static DH *ffc_params_generate(OSSL_LIB_CTX *libctx, DH_PKEY_CTX *dctx,
  242. BN_GENCB *pcb)
  243. {
  244. DH *ret;
  245. int rv = 0;
  246. int res;
  247. int prime_len = dctx->prime_len;
  248. int subprime_len = dctx->subprime_len;
  249. if (dctx->paramgen_type > DH_PARAMGEN_TYPE_FIPS_186_4)
  250. return NULL;
  251. ret = DH_new();
  252. if (ret == NULL)
  253. return NULL;
  254. if (subprime_len == -1) {
  255. if (prime_len >= 2048)
  256. subprime_len = 256;
  257. else
  258. subprime_len = 160;
  259. }
  260. if (dctx->md != NULL)
  261. ossl_ffc_set_digest(&ret->params, EVP_MD_name(dctx->md), NULL);
  262. # ifndef FIPS_MODULE
  263. if (dctx->paramgen_type == DH_PARAMGEN_TYPE_FIPS_186_2)
  264. rv = ossl_ffc_params_FIPS186_2_generate(libctx, &ret->params,
  265. FFC_PARAM_TYPE_DH,
  266. prime_len, subprime_len, &res,
  267. pcb);
  268. else
  269. # endif
  270. /* For FIPS we always use the DH_PARAMGEN_TYPE_FIPS_186_4 generator */
  271. if (dctx->paramgen_type >= DH_PARAMGEN_TYPE_FIPS_186_2)
  272. rv = ossl_ffc_params_FIPS186_4_generate(libctx, &ret->params,
  273. FFC_PARAM_TYPE_DH,
  274. prime_len, subprime_len, &res,
  275. pcb);
  276. if (rv <= 0) {
  277. DH_free(ret);
  278. return NULL;
  279. }
  280. return ret;
  281. }
  282. static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx,
  283. EVP_PKEY *pkey)
  284. {
  285. DH *dh = NULL;
  286. DH_PKEY_CTX *dctx = ctx->data;
  287. BN_GENCB *pcb = NULL;
  288. int ret;
  289. /*
  290. * Look for a safe prime group for key establishment. Which uses
  291. * either RFC_3526 (modp_XXXX) or RFC_7919 (ffdheXXXX).
  292. */
  293. if (dctx->param_nid != NID_undef) {
  294. if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
  295. return 0;
  296. EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
  297. return 1;
  298. }
  299. #ifndef FIPS_MODULE
  300. if (dctx->rfc5114_param) {
  301. switch (dctx->rfc5114_param) {
  302. case 1:
  303. dh = DH_get_1024_160();
  304. break;
  305. case 2:
  306. dh = DH_get_2048_224();
  307. break;
  308. case 3:
  309. dh = DH_get_2048_256();
  310. break;
  311. default:
  312. return -2;
  313. }
  314. EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
  315. return 1;
  316. }
  317. #endif /* FIPS_MODULE */
  318. if (ctx->pkey_gencb != NULL) {
  319. pcb = BN_GENCB_new();
  320. if (pcb == NULL)
  321. return 0;
  322. evp_pkey_set_cb_translate(pcb, ctx);
  323. }
  324. # ifdef FIPS_MODULE
  325. dctx->paramgen_type = DH_PARAMGEN_TYPE_FIPS_186_4;
  326. # endif /* FIPS_MODULE */
  327. if (dctx->paramgen_type >= DH_PARAMGEN_TYPE_FIPS_186_2) {
  328. dh = ffc_params_generate(NULL, dctx, pcb);
  329. BN_GENCB_free(pcb);
  330. if (dh == NULL)
  331. return 0;
  332. EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
  333. return 1;
  334. }
  335. dh = DH_new();
  336. if (dh == NULL) {
  337. BN_GENCB_free(pcb);
  338. return 0;
  339. }
  340. ret = DH_generate_parameters_ex(dh,
  341. dctx->prime_len, dctx->generator, pcb);
  342. BN_GENCB_free(pcb);
  343. if (ret)
  344. EVP_PKEY_assign_DH(pkey, dh);
  345. else
  346. DH_free(dh);
  347. return ret;
  348. }
  349. static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  350. {
  351. DH_PKEY_CTX *dctx = ctx->data;
  352. DH *dh = NULL;
  353. if (ctx->pkey == NULL && dctx->param_nid == NID_undef) {
  354. ERR_raise(ERR_LIB_DH, DH_R_NO_PARAMETERS_SET);
  355. return 0;
  356. }
  357. if (dctx->param_nid != NID_undef)
  358. dh = DH_new_by_nid(dctx->param_nid);
  359. else
  360. dh = DH_new();
  361. if (dh == NULL)
  362. return 0;
  363. EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
  364. /* Note: if error return, pkey is freed by parent routine */
  365. if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  366. return 0;
  367. return DH_generate_key(pkey->pkey.dh);
  368. }
  369. static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  370. size_t *keylen)
  371. {
  372. int ret;
  373. DH *dh;
  374. DH_PKEY_CTX *dctx = ctx->data;
  375. BIGNUM *dhpub;
  376. if (!ctx->pkey || !ctx->peerkey) {
  377. ERR_raise(ERR_LIB_DH, DH_R_KEYS_NOT_SET);
  378. return 0;
  379. }
  380. dh = ctx->pkey->pkey.dh;
  381. dhpub = ctx->peerkey->pkey.dh->pub_key;
  382. if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE) {
  383. if (key == NULL) {
  384. *keylen = DH_size(dh);
  385. return 1;
  386. }
  387. if (dctx->pad)
  388. ret = DH_compute_key_padded(key, dhpub, dh);
  389. else
  390. ret = DH_compute_key(key, dhpub, dh);
  391. if (ret < 0)
  392. return ret;
  393. *keylen = ret;
  394. return 1;
  395. }
  396. else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42) {
  397. unsigned char *Z = NULL;
  398. size_t Zlen = 0;
  399. if (!dctx->kdf_outlen || !dctx->kdf_oid)
  400. return 0;
  401. if (key == NULL) {
  402. *keylen = dctx->kdf_outlen;
  403. return 1;
  404. }
  405. if (*keylen != dctx->kdf_outlen)
  406. return 0;
  407. ret = 0;
  408. Zlen = DH_size(dh);
  409. Z = OPENSSL_malloc(Zlen);
  410. if (Z == NULL) {
  411. goto err;
  412. }
  413. if (DH_compute_key_padded(Z, dhpub, dh) <= 0)
  414. goto err;
  415. if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
  416. dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
  417. goto err;
  418. *keylen = dctx->kdf_outlen;
  419. ret = 1;
  420. err:
  421. OPENSSL_clear_free(Z, Zlen);
  422. return ret;
  423. }
  424. return 0;
  425. }
  426. static const EVP_PKEY_METHOD dh_pkey_meth = {
  427. EVP_PKEY_DH,
  428. 0,
  429. pkey_dh_init,
  430. pkey_dh_copy,
  431. pkey_dh_cleanup,
  432. 0,
  433. pkey_dh_paramgen,
  434. 0,
  435. pkey_dh_keygen,
  436. 0,
  437. 0,
  438. 0,
  439. 0,
  440. 0, 0,
  441. 0, 0, 0, 0,
  442. 0, 0,
  443. 0, 0,
  444. 0,
  445. pkey_dh_derive,
  446. pkey_dh_ctrl,
  447. pkey_dh_ctrl_str
  448. };
  449. const EVP_PKEY_METHOD *dh_pkey_method(void)
  450. {
  451. return &dh_pkey_meth;
  452. }
  453. static const EVP_PKEY_METHOD dhx_pkey_meth = {
  454. EVP_PKEY_DHX,
  455. 0,
  456. pkey_dh_init,
  457. pkey_dh_copy,
  458. pkey_dh_cleanup,
  459. 0,
  460. pkey_dh_paramgen,
  461. 0,
  462. pkey_dh_keygen,
  463. 0,
  464. 0,
  465. 0,
  466. 0,
  467. 0, 0,
  468. 0, 0, 0, 0,
  469. 0, 0,
  470. 0, 0,
  471. 0,
  472. pkey_dh_derive,
  473. pkey_dh_ctrl,
  474. pkey_dh_ctrl_str
  475. };
  476. const EVP_PKEY_METHOD *dhx_pkey_method(void)
  477. {
  478. return &dhx_pkey_meth;
  479. }