dh_pmeth.c 13 KB

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