dhm.c 21 KB

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