dhm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Diffie-Hellman-Merkle key exchange
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The following sources were referenced in the design of this implementation
  9. * of the Diffie-Hellman-Merkle algorithm:
  10. *
  11. * [1] Handbook of Applied Cryptography - 1997, Chapter 12
  12. * Menezes, van Oorschot and Vanstone
  13. *
  14. */
  15. #include "common.h"
  16. #if defined(MBEDTLS_DHM_C)
  17. #include "mbedtls/dhm.h"
  18. #include "mbedtls/platform_util.h"
  19. #include "mbedtls/error.h"
  20. #include <string.h>
  21. #if defined(MBEDTLS_PEM_PARSE_C)
  22. #include "mbedtls/pem.h"
  23. #endif
  24. #if defined(MBEDTLS_ASN1_PARSE_C)
  25. #include "mbedtls/asn1.h"
  26. #endif
  27. #include "mbedtls/platform.h"
  28. #if !defined(MBEDTLS_DHM_ALT)
  29. /*
  30. * helper to validate the mbedtls_mpi size and import it
  31. */
  32. static int dhm_read_bignum(mbedtls_mpi *X,
  33. unsigned char **p,
  34. const unsigned char *end)
  35. {
  36. int ret, n;
  37. if (end - *p < 2) {
  38. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  39. }
  40. n = MBEDTLS_GET_UINT16_BE(*p, 0);
  41. (*p) += 2;
  42. if ((size_t) (end - *p) < (size_t) n) {
  43. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  44. }
  45. if ((ret = mbedtls_mpi_read_binary(X, *p, n)) != 0) {
  46. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED, ret);
  47. }
  48. (*p) += n;
  49. return 0;
  50. }
  51. /*
  52. * Verify sanity of parameter with regards to P
  53. *
  54. * Parameter should be: 2 <= public_param <= P - 2
  55. *
  56. * This means that we need to return an error if
  57. * public_param < 2 or public_param > P-2
  58. *
  59. * For more information on the attack, see:
  60. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  61. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  62. */
  63. static int dhm_check_range(const mbedtls_mpi *param, const mbedtls_mpi *P)
  64. {
  65. mbedtls_mpi U;
  66. int ret = 0;
  67. mbedtls_mpi_init(&U);
  68. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&U, P, 2));
  69. if (mbedtls_mpi_cmp_int(param, 2) < 0 ||
  70. mbedtls_mpi_cmp_mpi(param, &U) > 0) {
  71. ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  72. }
  73. cleanup:
  74. mbedtls_mpi_free(&U);
  75. return ret;
  76. }
  77. void mbedtls_dhm_init(mbedtls_dhm_context *ctx)
  78. {
  79. memset(ctx, 0, sizeof(mbedtls_dhm_context));
  80. }
  81. size_t mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx)
  82. {
  83. return mbedtls_mpi_bitlen(&ctx->P);
  84. }
  85. size_t mbedtls_dhm_get_len(const mbedtls_dhm_context *ctx)
  86. {
  87. return mbedtls_mpi_size(&ctx->P);
  88. }
  89. int mbedtls_dhm_get_value(const mbedtls_dhm_context *ctx,
  90. mbedtls_dhm_parameter param,
  91. mbedtls_mpi *dest)
  92. {
  93. const mbedtls_mpi *src = NULL;
  94. switch (param) {
  95. case MBEDTLS_DHM_PARAM_P:
  96. src = &ctx->P;
  97. break;
  98. case MBEDTLS_DHM_PARAM_G:
  99. src = &ctx->G;
  100. break;
  101. case MBEDTLS_DHM_PARAM_X:
  102. src = &ctx->X;
  103. break;
  104. case MBEDTLS_DHM_PARAM_GX:
  105. src = &ctx->GX;
  106. break;
  107. case MBEDTLS_DHM_PARAM_GY:
  108. src = &ctx->GY;
  109. break;
  110. case MBEDTLS_DHM_PARAM_K:
  111. src = &ctx->K;
  112. break;
  113. default:
  114. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  115. }
  116. return mbedtls_mpi_copy(dest, src);
  117. }
  118. /*
  119. * Parse the ServerKeyExchange parameters
  120. */
  121. int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx,
  122. unsigned char **p,
  123. const unsigned char *end)
  124. {
  125. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  126. if ((ret = dhm_read_bignum(&ctx->P, p, end)) != 0 ||
  127. (ret = dhm_read_bignum(&ctx->G, p, end)) != 0 ||
  128. (ret = dhm_read_bignum(&ctx->GY, p, end)) != 0) {
  129. return ret;
  130. }
  131. if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) {
  132. return ret;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * Pick a random R in the range [2, M-2] for blinding or key generation.
  138. */
  139. static int dhm_random_below(mbedtls_mpi *R, const mbedtls_mpi *M,
  140. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  141. {
  142. int ret;
  143. MBEDTLS_MPI_CHK(mbedtls_mpi_random(R, 3, M, f_rng, p_rng));
  144. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(R, R, 1));
  145. cleanup:
  146. return ret;
  147. }
  148. static int dhm_make_common(mbedtls_dhm_context *ctx, int x_size,
  149. int (*f_rng)(void *, unsigned char *, size_t),
  150. void *p_rng)
  151. {
  152. int ret = 0;
  153. if (mbedtls_mpi_cmp_int(&ctx->P, 0) == 0) {
  154. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  155. }
  156. if (x_size < 0) {
  157. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  158. }
  159. if ((unsigned) x_size < mbedtls_mpi_size(&ctx->P)) {
  160. MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->X, x_size, f_rng, p_rng));
  161. } else {
  162. /* Generate X as large as possible ( <= P - 2 ) */
  163. ret = dhm_random_below(&ctx->X, &ctx->P, f_rng, p_rng);
  164. if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
  165. return MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED;
  166. }
  167. if (ret != 0) {
  168. return ret;
  169. }
  170. }
  171. /*
  172. * Calculate GX = G^X mod P
  173. */
  174. MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->GX, &ctx->G, &ctx->X,
  175. &ctx->P, &ctx->RP));
  176. if ((ret = dhm_check_range(&ctx->GX, &ctx->P)) != 0) {
  177. return ret;
  178. }
  179. cleanup:
  180. return ret;
  181. }
  182. /*
  183. * Setup and write the ServerKeyExchange parameters
  184. */
  185. int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size,
  186. unsigned char *output, size_t *olen,
  187. int (*f_rng)(void *, unsigned char *, size_t),
  188. void *p_rng)
  189. {
  190. int ret;
  191. size_t n1, n2, n3;
  192. unsigned char *p;
  193. ret = dhm_make_common(ctx, x_size, f_rng, p_rng);
  194. if (ret != 0) {
  195. goto cleanup;
  196. }
  197. /*
  198. * Export P, G, GX. RFC 5246 §4.4 states that "leading zero octets are
  199. * not required". We omit leading zeros for compactness.
  200. */
  201. #define DHM_MPI_EXPORT(X, n) \
  202. do { \
  203. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary((X), \
  204. p + 2, \
  205. (n))); \
  206. *p++ = MBEDTLS_BYTE_1(n); \
  207. *p++ = MBEDTLS_BYTE_0(n); \
  208. p += (n); \
  209. } while (0)
  210. n1 = mbedtls_mpi_size(&ctx->P);
  211. n2 = mbedtls_mpi_size(&ctx->G);
  212. n3 = mbedtls_mpi_size(&ctx->GX);
  213. p = output;
  214. DHM_MPI_EXPORT(&ctx->P, n1);
  215. DHM_MPI_EXPORT(&ctx->G, n2);
  216. DHM_MPI_EXPORT(&ctx->GX, n3);
  217. *olen = (size_t) (p - output);
  218. cleanup:
  219. if (ret != 0 && ret > -128) {
  220. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED, ret);
  221. }
  222. return ret;
  223. }
  224. /*
  225. * Set prime modulus and generator
  226. */
  227. int mbedtls_dhm_set_group(mbedtls_dhm_context *ctx,
  228. const mbedtls_mpi *P,
  229. const mbedtls_mpi *G)
  230. {
  231. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  232. if ((ret = mbedtls_mpi_copy(&ctx->P, P)) != 0 ||
  233. (ret = mbedtls_mpi_copy(&ctx->G, G)) != 0) {
  234. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_SET_GROUP_FAILED, ret);
  235. }
  236. return 0;
  237. }
  238. /*
  239. * Import the peer's public value G^Y
  240. */
  241. int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx,
  242. const unsigned char *input, size_t ilen)
  243. {
  244. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  245. if (ilen < 1 || ilen > mbedtls_dhm_get_len(ctx)) {
  246. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  247. }
  248. if ((ret = mbedtls_mpi_read_binary(&ctx->GY, input, ilen)) != 0) {
  249. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED, ret);
  250. }
  251. return 0;
  252. }
  253. /*
  254. * Create own private value X and export G^X
  255. */
  256. int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size,
  257. unsigned char *output, size_t olen,
  258. int (*f_rng)(void *, unsigned char *, size_t),
  259. void *p_rng)
  260. {
  261. int ret;
  262. if (olen < 1 || olen > mbedtls_dhm_get_len(ctx)) {
  263. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  264. }
  265. ret = dhm_make_common(ctx, x_size, f_rng, p_rng);
  266. if (ret == MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED) {
  267. return MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED;
  268. }
  269. if (ret != 0) {
  270. goto cleanup;
  271. }
  272. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->GX, output, olen));
  273. cleanup:
  274. if (ret != 0 && ret > -128) {
  275. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED, ret);
  276. }
  277. return ret;
  278. }
  279. /*
  280. * Use the blinding method and optimisation suggested in section 10 of:
  281. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  282. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  283. * Berlin Heidelberg, 1996. p. 104-113.
  284. */
  285. static int dhm_update_blinding(mbedtls_dhm_context *ctx,
  286. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  287. {
  288. int ret;
  289. mbedtls_mpi R;
  290. mbedtls_mpi_init(&R);
  291. /*
  292. * Don't use any blinding the first time a particular X is used,
  293. * but remember it to use blinding next time.
  294. */
  295. if (mbedtls_mpi_cmp_mpi(&ctx->X, &ctx->pX) != 0) {
  296. MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&ctx->pX, &ctx->X));
  297. MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vi, 1));
  298. MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vf, 1));
  299. return 0;
  300. }
  301. /*
  302. * Ok, we need blinding. Can we re-use existing values?
  303. * If yes, just update them by squaring them.
  304. */
  305. if (mbedtls_mpi_cmp_int(&ctx->Vi, 1) != 0) {
  306. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
  307. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->P));
  308. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
  309. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P));
  310. return 0;
  311. }
  312. /*
  313. * We need to generate blinding values from scratch
  314. */
  315. /* Vi = random( 2, P-2 ) */
  316. MBEDTLS_MPI_CHK(dhm_random_below(&ctx->Vi, &ctx->P, f_rng, p_rng));
  317. /* Vf = Vi^-X mod P
  318. * First compute Vi^-1 = R * (R Vi)^-1, (avoiding leaks from inv_mod),
  319. * then elevate to the Xth power. */
  320. MBEDTLS_MPI_CHK(dhm_random_below(&R, &ctx->P, f_rng, p_rng));
  321. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vi, &R));
  322. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P));
  323. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->Vf, &ctx->Vf, &ctx->P));
  324. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &R));
  325. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P));
  326. MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP));
  327. cleanup:
  328. mbedtls_mpi_free(&R);
  329. return ret;
  330. }
  331. /*
  332. * Derive and export the shared secret (G^Y)^X mod P
  333. */
  334. int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx,
  335. unsigned char *output, size_t output_size, size_t *olen,
  336. int (*f_rng)(void *, unsigned char *, size_t),
  337. void *p_rng)
  338. {
  339. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  340. mbedtls_mpi GYb;
  341. if (f_rng == NULL) {
  342. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  343. }
  344. if (output_size < mbedtls_dhm_get_len(ctx)) {
  345. return MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  346. }
  347. if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) {
  348. return ret;
  349. }
  350. mbedtls_mpi_init(&GYb);
  351. /* Blind peer's value */
  352. MBEDTLS_MPI_CHK(dhm_update_blinding(ctx, f_rng, p_rng));
  353. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&GYb, &ctx->GY, &ctx->Vi));
  354. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&GYb, &GYb, &ctx->P));
  355. /* Do modular exponentiation */
  356. MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->K, &GYb, &ctx->X,
  357. &ctx->P, &ctx->RP));
  358. /* Unblind secret value */
  359. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->K, &ctx->K, &ctx->Vf));
  360. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->K, &ctx->K, &ctx->P));
  361. /* Output the secret without any leading zero byte. This is mandatory
  362. * for TLS per RFC 5246 §8.1.2. */
  363. *olen = mbedtls_mpi_size(&ctx->K);
  364. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->K, output, *olen));
  365. cleanup:
  366. mbedtls_mpi_free(&GYb);
  367. if (ret != 0) {
  368. return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED, ret);
  369. }
  370. return 0;
  371. }
  372. /*
  373. * Free the components of a DHM key
  374. */
  375. void mbedtls_dhm_free(mbedtls_dhm_context *ctx)
  376. {
  377. if (ctx == NULL) {
  378. return;
  379. }
  380. mbedtls_mpi_free(&ctx->pX);
  381. mbedtls_mpi_free(&ctx->Vf);
  382. mbedtls_mpi_free(&ctx->Vi);
  383. mbedtls_mpi_free(&ctx->RP);
  384. mbedtls_mpi_free(&ctx->K);
  385. mbedtls_mpi_free(&ctx->GY);
  386. mbedtls_mpi_free(&ctx->GX);
  387. mbedtls_mpi_free(&ctx->X);
  388. mbedtls_mpi_free(&ctx->G);
  389. mbedtls_mpi_free(&ctx->P);
  390. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_dhm_context));
  391. }
  392. #if defined(MBEDTLS_ASN1_PARSE_C)
  393. /*
  394. * Parse DHM parameters
  395. */
  396. int mbedtls_dhm_parse_dhm(mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  397. size_t dhminlen)
  398. {
  399. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  400. size_t len;
  401. unsigned char *p, *end;
  402. #if defined(MBEDTLS_PEM_PARSE_C)
  403. mbedtls_pem_context pem;
  404. #endif /* MBEDTLS_PEM_PARSE_C */
  405. #if defined(MBEDTLS_PEM_PARSE_C)
  406. mbedtls_pem_init(&pem);
  407. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  408. if (dhminlen == 0 || dhmin[dhminlen - 1] != '\0') {
  409. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  410. } else {
  411. ret = mbedtls_pem_read_buffer(&pem,
  412. "-----BEGIN DH PARAMETERS-----",
  413. "-----END DH PARAMETERS-----",
  414. dhmin, NULL, 0, &dhminlen);
  415. }
  416. if (ret == 0) {
  417. /*
  418. * Was PEM encoded
  419. */
  420. dhminlen = pem.buflen;
  421. } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
  422. goto exit;
  423. }
  424. p = (ret == 0) ? pem.buf : (unsigned char *) dhmin;
  425. #else
  426. p = (unsigned char *) dhmin;
  427. #endif /* MBEDTLS_PEM_PARSE_C */
  428. end = p + dhminlen;
  429. /*
  430. * DHParams ::= SEQUENCE {
  431. * prime INTEGER, -- P
  432. * generator INTEGER, -- g
  433. * privateValueLength INTEGER OPTIONAL
  434. * }
  435. */
  436. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  437. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  438. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret);
  439. goto exit;
  440. }
  441. end = p + len;
  442. if ((ret = mbedtls_asn1_get_mpi(&p, end, &dhm->P)) != 0 ||
  443. (ret = mbedtls_asn1_get_mpi(&p, end, &dhm->G)) != 0) {
  444. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret);
  445. goto exit;
  446. }
  447. if (p != end) {
  448. /* This might be the optional privateValueLength.
  449. * If so, we can cleanly discard it */
  450. mbedtls_mpi rec;
  451. mbedtls_mpi_init(&rec);
  452. ret = mbedtls_asn1_get_mpi(&p, end, &rec);
  453. mbedtls_mpi_free(&rec);
  454. if (ret != 0) {
  455. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret);
  456. goto exit;
  457. }
  458. if (p != end) {
  459. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT,
  460. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  461. goto exit;
  462. }
  463. }
  464. ret = 0;
  465. exit:
  466. #if defined(MBEDTLS_PEM_PARSE_C)
  467. mbedtls_pem_free(&pem);
  468. #endif
  469. if (ret != 0) {
  470. mbedtls_dhm_free(dhm);
  471. }
  472. return ret;
  473. }
  474. #if defined(MBEDTLS_FS_IO)
  475. /*
  476. * Load all data from a file into a given buffer.
  477. *
  478. * The file is expected to contain either PEM or DER encoded data.
  479. * A terminating null byte is always appended. It is included in the announced
  480. * length only if the data looks like it is PEM encoded.
  481. */
  482. static int load_file(const char *path, unsigned char **buf, size_t *n)
  483. {
  484. FILE *f;
  485. long size;
  486. if ((f = fopen(path, "rb")) == NULL) {
  487. return MBEDTLS_ERR_DHM_FILE_IO_ERROR;
  488. }
  489. /* The data loaded here is public, so don't bother disabling buffering. */
  490. fseek(f, 0, SEEK_END);
  491. if ((size = ftell(f)) == -1) {
  492. fclose(f);
  493. return MBEDTLS_ERR_DHM_FILE_IO_ERROR;
  494. }
  495. fseek(f, 0, SEEK_SET);
  496. *n = (size_t) size;
  497. if (*n + 1 == 0 ||
  498. (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
  499. fclose(f);
  500. return MBEDTLS_ERR_DHM_ALLOC_FAILED;
  501. }
  502. if (fread(*buf, 1, *n, f) != *n) {
  503. fclose(f);
  504. mbedtls_zeroize_and_free(*buf, *n + 1);
  505. return MBEDTLS_ERR_DHM_FILE_IO_ERROR;
  506. }
  507. fclose(f);
  508. (*buf)[*n] = '\0';
  509. if (strstr((const char *) *buf, "-----BEGIN ") != NULL) {
  510. ++*n;
  511. }
  512. return 0;
  513. }
  514. /*
  515. * Load and parse DHM parameters
  516. */
  517. int mbedtls_dhm_parse_dhmfile(mbedtls_dhm_context *dhm, const char *path)
  518. {
  519. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  520. size_t n;
  521. unsigned char *buf;
  522. if ((ret = load_file(path, &buf, &n)) != 0) {
  523. return ret;
  524. }
  525. ret = mbedtls_dhm_parse_dhm(dhm, buf, n);
  526. mbedtls_zeroize_and_free(buf, n);
  527. return ret;
  528. }
  529. #endif /* MBEDTLS_FS_IO */
  530. #endif /* MBEDTLS_ASN1_PARSE_C */
  531. #endif /* MBEDTLS_DHM_ALT */
  532. #if defined(MBEDTLS_SELF_TEST)
  533. #if defined(MBEDTLS_PEM_PARSE_C)
  534. static const char mbedtls_test_dhm_params[] =
  535. "-----BEGIN DH PARAMETERS-----\r\n"
  536. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  537. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  538. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  539. "-----END DH PARAMETERS-----\r\n";
  540. #else /* MBEDTLS_PEM_PARSE_C */
  541. static const char mbedtls_test_dhm_params[] = {
  542. 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, 0x35, 0xf4, 0x30, 0x44,
  543. 0x3a, 0x09, 0x90, 0x4f, 0x3a, 0x39, 0xa9, 0x79, 0x79, 0x7d, 0x07, 0x0d,
  544. 0xf5, 0x33, 0x78, 0xe7, 0x9c, 0x24, 0x38, 0xbe, 0xf4, 0xe7, 0x61, 0xf3,
  545. 0xc7, 0x14, 0x55, 0x33, 0x28, 0x58, 0x9b, 0x04, 0x1c, 0x80, 0x9b, 0xe1,
  546. 0xd6, 0xc6, 0xb5, 0xf1, 0xfc, 0x9f, 0x47, 0xd3, 0xa2, 0x54, 0x43, 0x18,
  547. 0x82, 0x53, 0xa9, 0x92, 0xa5, 0x68, 0x18, 0xb3, 0x7b, 0xa9, 0xde, 0x5a,
  548. 0x40, 0xd3, 0x62, 0xe5, 0x6e, 0xff, 0x0b, 0xe5, 0x41, 0x74, 0x74, 0xc1,
  549. 0x25, 0xc1, 0x99, 0x27, 0x2c, 0x8f, 0xe4, 0x1d, 0xea, 0x73, 0x3d, 0xf6,
  550. 0xf6, 0x62, 0xc9, 0x2a, 0xe7, 0x65, 0x56, 0xe7, 0x55, 0xd1, 0x0c, 0x64,
  551. 0xe6, 0xa5, 0x09, 0x68, 0xf6, 0x7f, 0xc6, 0xea, 0x73, 0xd0, 0xdc, 0xa8,
  552. 0x56, 0x9b, 0xe2, 0xba, 0x20, 0x4e, 0x23, 0x58, 0x0d, 0x8b, 0xca, 0x2f,
  553. 0x49, 0x75, 0xb3, 0x02, 0x01, 0x02
  554. };
  555. #endif /* MBEDTLS_PEM_PARSE_C */
  556. static const size_t mbedtls_test_dhm_params_len = sizeof(mbedtls_test_dhm_params);
  557. /*
  558. * Checkup routine
  559. */
  560. int mbedtls_dhm_self_test(int verbose)
  561. {
  562. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  563. mbedtls_dhm_context dhm;
  564. mbedtls_dhm_init(&dhm);
  565. if (verbose != 0) {
  566. mbedtls_printf(" DHM parameter load: ");
  567. }
  568. if ((ret = mbedtls_dhm_parse_dhm(&dhm,
  569. (const unsigned char *) mbedtls_test_dhm_params,
  570. mbedtls_test_dhm_params_len)) != 0) {
  571. if (verbose != 0) {
  572. mbedtls_printf("failed\n");
  573. }
  574. ret = 1;
  575. goto exit;
  576. }
  577. if (verbose != 0) {
  578. mbedtls_printf("passed\n\n");
  579. }
  580. exit:
  581. mbedtls_dhm_free(&dhm);
  582. return ret;
  583. }
  584. #endif /* MBEDTLS_SELF_TEST */
  585. #endif /* MBEDTLS_DHM_C */