dh_pmeth.c 13 KB

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