blob: dc815d9cb26bb399e81b379f1a06eb51a273b56e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * Reference:
27 *
28 * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
29 */
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakkercff68422013-09-15 20:43:33 +020037#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +020038#include "polarssl/pem.h"
39#endif
40
41#if defined(POLARSSL_ASN1_PARSE_C)
42#include "polarssl/asn1.h"
43#endif
44
45#if defined(POLARSSL_MEMORY_C)
46#include "polarssl/memory.h"
47#else
48#include <stdlib.h>
49#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/*
54 * helper to validate the mpi size and import it
55 */
56static int dhm_read_bignum( mpi *X,
57 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000058 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
60 int ret, n;
61
62 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000063 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65 n = ( (*p)[0] << 8 ) | (*p)[1];
66 (*p) += 2;
67
68 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000069 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000072 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 (*p) += n;
75
76 return( 0 );
77}
78
79/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000080 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000081 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000082 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000083 *
84 * For more information on the attack, see:
85 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
86 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000087 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000088static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000089{
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 mpi L, U;
91 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000092
Paul Bakker6c591fa2011-05-05 11:49:20 +000093 mpi_init( &L ); mpi_init( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000094 mpi_lset( &L, 2 );
95 mpi_sub_int( &U, P, 2 );
Paul Bakkerc47840e2011-02-20 16:37:30 +000096
Paul Bakkeraec37cb2012-04-26 18:59:59 +000097 if( mpi_cmp_mpi( param, &L ) >= 0 &&
98 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +000099 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000100 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000101 }
102
Paul Bakker6c591fa2011-05-05 11:49:20 +0000103 mpi_free( &L ); mpi_free( &U );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000104
Paul Bakker345a6fe2011-02-28 21:20:02 +0000105 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000106}
107
108/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 * Parse the ServerKeyExchange parameters
110 */
111int dhm_read_params( dhm_context *ctx,
112 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000115 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200117 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
120 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
121 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
122 return( ret );
123
Paul Bakker345a6fe2011-02-28 21:20:02 +0000124 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
125 return( ret );
126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 ctx->len = mpi_size( &ctx->P );
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 return( 0 );
130}
131
132/*
133 * Setup and write the ServerKeyExchange parameters
134 */
135int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000136 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000140 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000141 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 unsigned char *p;
143
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000144 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
145 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
146
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000148 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000150 do
151 {
152 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000154 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
155 mpi_shift_r( &ctx->X, 1 );
156
157 if( count++ > 10 )
158 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
159 }
160 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakkerff7fe672010-07-18 09:45:05 +0000162 /*
163 * Calculate GX = G^X mod P
164 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
166 &ctx->P , &ctx->RP ) );
167
Paul Bakker345a6fe2011-02-28 21:20:02 +0000168 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000169 return( ret );
170
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 /*
172 * export P, G, GX
173 */
174#define DHM_MPI_EXPORT(X,n) \
175 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
176 *p++ = (unsigned char)( n >> 8 ); \
177 *p++ = (unsigned char)( n ); p += n;
178
179 n1 = mpi_size( &ctx->P );
180 n2 = mpi_size( &ctx->G );
181 n3 = mpi_size( &ctx->GX );
182
183 p = output;
184 DHM_MPI_EXPORT( &ctx->P , n1 );
185 DHM_MPI_EXPORT( &ctx->G , n2 );
186 DHM_MPI_EXPORT( &ctx->GX, n3 );
187
188 *olen = p - output;
189
190 ctx->len = n1;
191
192cleanup:
193
194 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 return( 0 );
198}
199
200/*
201 * Import the peer's public value G^Y
202 */
203int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000204 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
206 int ret;
207
208 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 return( 0 );
215}
216
217/*
218 * Create own private value X and export G^X
219 */
220int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000221 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222 int (*f_rng)(void *, unsigned char *, size_t),
223 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000225 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000228 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000230 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
231 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 /*
234 * generate X and calculate GX = G^X mod P
235 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000236 do
237 {
238 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000240 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
241 mpi_shift_r( &ctx->X, 1 );
242
243 if( count++ > 10 )
244 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
245 }
246 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
249 &ctx->P , &ctx->RP ) );
250
Paul Bakker345a6fe2011-02-28 21:20:02 +0000251 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
252 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000253
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
255
256cleanup:
257
258 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000259 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 return( 0 );
262}
263
264/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200265 * Use the blinding method and optimisation suggested in section 10 of:
266 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
267 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
268 * Berlin Heidelberg, 1996. p. 104-113.
269 */
270static int dhm_update_blinding( dhm_context *ctx,
271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
272{
273 int ret, count;
274
275 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200276 * Don't use any blinding the first time a particular X is used,
277 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200278 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200279 if( mpi_cmp_mpi( &ctx->X, &ctx->_X ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200280 {
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200281 MPI_CHK( mpi_copy( &ctx->_X, &ctx->X ) );
282 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
283 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200284
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200285 return( 0 );
286 }
287
288 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200289 * Ok, we need blinding. Can we re-use existing values?
290 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200291 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200292 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
293 {
294 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
295 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
296
297 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
298 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
299
300 return( 0 );
301 }
302
303 /*
304 * We need to generate blinding values from scratch
305 */
306
307 /* Vi = random( 2, P-1 ) */
308 count = 0;
309 do
310 {
311 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
312
313 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
314 mpi_shift_r( &ctx->Vi, 1 );
315
316 if( count++ > 10 )
317 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
318 }
319 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200320
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200321 /* Vf = Vi^-X mod P */
322 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
323 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
324
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200325cleanup:
326 return( ret );
327}
328
329/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * Derive and export the shared secret (G^Y)^X mod P
331 */
332int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200333 unsigned char *output, size_t *olen,
334 int (*f_rng)(void *, unsigned char *, size_t),
335 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000336{
337 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200338 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200339
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000341 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Paul Bakker345a6fe2011-02-28 21:20:02 +0000343 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000344 return( ret );
345
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200346 mpi_init( &GYb );
347
348 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200349 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200350 {
351 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
352 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
353 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
354 }
355 else
356 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
357
358 /* Do modular exponentiation */
359 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
360 &ctx->P, &ctx->RP ) );
361
362 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200363 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200364 {
365 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
366 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
367 }
368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *olen = mpi_size( &ctx->K );
370
371 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
372
373cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200374 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000377 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379 return( 0 );
380}
381
382/*
383 * Free the components of a DHM key
384 */
385void dhm_free( dhm_context *ctx )
386{
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200387 mpi_free( &ctx->_X); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000388 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
389 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
390 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200391
392 memset( ctx, 0, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393}
394
Paul Bakker40ce79f2013-09-15 17:43:54 +0200395#if defined(POLARSSL_ASN1_PARSE_C)
396/*
397 * Parse DHM parameters
398 */
399int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
400{
401 int ret;
402 size_t len;
403 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200404#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200405 pem_context pem;
406
407 pem_init( &pem );
408 memset( dhm, 0, sizeof( dhm_context ) );
409
410 ret = pem_read_buffer( &pem,
411 "-----BEGIN DH PARAMETERS-----",
412 "-----END DH PARAMETERS-----",
413 dhmin, NULL, 0, &dhminlen );
414
415 if( ret == 0 )
416 {
417 /*
418 * Was PEM encoded
419 */
420 dhminlen = pem.buflen;
421 }
422 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
423 goto exit;
424
425 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
426#else
427 p = (unsigned char *) dhmin;
428#endif
429 end = p + dhminlen;
430
431 /*
432 * DHParams ::= SEQUENCE {
433 * prime INTEGER, -- P
434 * generator INTEGER, -- g
435 * }
436 */
437 if( ( ret = asn1_get_tag( &p, end, &len,
438 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
439 {
440 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
441 goto exit;
442 }
443
444 end = p + len;
445
446 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
447 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
448 {
449 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
450 goto exit;
451 }
452
453 if( p != end )
454 {
455 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
456 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
457 goto exit;
458 }
459
460 ret = 0;
461
462exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200463#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200464 pem_free( &pem );
465#endif
466 if( ret != 0 )
467 dhm_free( dhm );
468
469 return( ret );
470}
471
472#if defined(POLARSSL_FS_IO)
473/*
474 * Load all data from a file into a given buffer.
475 */
476static int load_file( const char *path, unsigned char **buf, size_t *n )
477{
478 FILE *f;
479 long size;
480
481 if( ( f = fopen( path, "rb" ) ) == NULL )
482 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
483
484 fseek( f, 0, SEEK_END );
485 if( ( size = ftell( f ) ) == -1 )
486 {
487 fclose( f );
488 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
489 }
490 fseek( f, 0, SEEK_SET );
491
492 *n = (size_t) size;
493
494 if( *n + 1 == 0 ||
495 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
496 {
497 fclose( f );
498 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
499 }
500
501 if( fread( *buf, 1, *n, f ) != *n )
502 {
503 fclose( f );
504 polarssl_free( *buf );
505 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
506 }
507
508 fclose( f );
509
510 (*buf)[*n] = '\0';
511
512 return( 0 );
513}
514
515/*
516 * Load and parse DHM parameters
517 */
518int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
519{
520 int ret;
521 size_t n;
522 unsigned char *buf;
523
524 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
525 return( ret );
526
527 ret = dhm_parse_dhm( dhm, buf, n );
528
529 memset( buf, 0, n + 1 );
530 polarssl_free( buf );
531
532 return( ret );
533}
534#endif /* POLARSSL_FS_IO */
535#endif /* POLARSSL_ASN1_PARSE_C */
536
Paul Bakker40e46942009-01-03 21:51:57 +0000537#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakker40ce79f2013-09-15 17:43:54 +0200539#include "polarssl/certs.h"
540
Paul Bakker5121ce52009-01-03 21:22:43 +0000541/*
542 * Checkup routine
543 */
544int dhm_self_test( int verbose )
545{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200546#if defined(POLARSSL_CERTS_C)
547 int ret;
548 dhm_context dhm;
549
550 if( verbose != 0 )
551 printf( " DHM parameter load: " );
552
553 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
554 strlen( test_dhm_params ) ) ) != 0 )
555 {
556 if( verbose != 0 )
557 printf( "failed\n" );
558
559 return( ret );
560 }
561
562 if( verbose != 0 )
563 printf( "passed\n\n" );
564
565 dhm_free( &dhm );
566
567 return( 0 );
568#else
569 ((void) verbose);
570 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000572}
573
574#endif
575
576#endif