@@ -59,6 +59,10 @@ static int nested_parse( unsigned char **const p,
59
59
*p = start;
60
60
ret = mbedtls_asn1_get_mpi( p, end, &mpi );
61
61
mbedtls_mpi_free( &mpi );
62
+ #else
63
+ *p = start + 1;
64
+ ret = mbedtls_asn1_get_len( p, end, &len );
65
+ *p += len;
62
66
#endif
63
67
/* If we're sure that the number fits in an int, also
64
68
* call mbedtls_asn1_get_int(). */
@@ -246,6 +250,41 @@ void get_boolean( const data_t *input,
246
250
}
247
251
/* END_CASE */
248
252
253
+ /* BEGIN_CASE */
254
+ void empty_integer( const data_t *input )
255
+ {
256
+ unsigned char *p;
257
+ #if defined(MBEDTLS_BIGNUM_C)
258
+ mbedtls_mpi actual_mpi;
259
+ #endif
260
+ int val;
261
+
262
+ #if defined(MBEDTLS_BIGNUM_C)
263
+ mbedtls_mpi_init( & actual_mpi );
264
+ #endif
265
+
266
+ /* An INTEGER with no content is not valid. */
267
+ p = input->x;
268
+ TEST_EQUAL( mbedtls_asn1_get_int( &p, input->x + input->len, &val ),
269
+ MBEDTLS_ERR_ASN1_INVALID_LENGTH );
270
+
271
+ #if defined(MBEDTLS_BIGNUM_C)
272
+ /* INTEGERs are sometimes abused as bitstrings, so the library accepts
273
+ * an INTEGER with empty content and gives it the value 0. */
274
+ p = input->x;
275
+ TEST_EQUAL( mbedtls_asn1_get_mpi( &p, input->x + input->len, &actual_mpi ),
276
+ 0 );
277
+ TEST_EQUAL( mbedtls_mpi_cmp_int( &actual_mpi, 0 ), 0 );
278
+ #endif
279
+
280
+ exit:
281
+ #if defined(MBEDTLS_BIGNUM_C)
282
+ mbedtls_mpi_free( &actual_mpi );
283
+ #endif
284
+ /*empty cleanup in some configurations*/ ;
285
+ }
286
+ /* END_CASE */
287
+
249
288
/* BEGIN_CASE */
250
289
void get_integer( const data_t *input,
251
290
const char *expected_hex, int expected_result )
@@ -254,16 +293,18 @@ void get_integer( const data_t *input,
254
293
#if defined(MBEDTLS_BIGNUM_C)
255
294
mbedtls_mpi expected_mpi;
256
295
mbedtls_mpi actual_mpi;
296
+ mbedtls_mpi complement;
297
+ int expected_result_for_mpi = expected_result;
257
298
#endif
258
299
long expected_value;
259
300
int expected_result_for_int = expected_result;
260
- int expected_result_for_mpi = expected_result;
261
301
int val;
262
302
int ret;
263
303
264
304
#if defined(MBEDTLS_BIGNUM_C)
265
305
mbedtls_mpi_init( &expected_mpi );
266
306
mbedtls_mpi_init( &actual_mpi );
307
+ mbedtls_mpi_init( &complement );
267
308
#endif
268
309
269
310
errno = 0;
@@ -275,6 +316,16 @@ void get_integer( const data_t *input,
275
316
#endif
276
317
) )
277
318
{
319
+ /* The library returns the dubious error code INVALID_LENGTH
320
+ * for integers that are out of range. */
321
+ expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
322
+ }
323
+ if( expected_result == 0 && expected_value < 0 )
324
+ {
325
+ /* The library does not support negative INTEGERs and
326
+ * returns the dubious error code INVALID_LENGTH.
327
+ * Test that we preserve the historical behavior. If we
328
+ * decide to change the behavior, we'll also change this test. */
278
329
expected_result_for_int = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
279
330
}
280
331
@@ -300,7 +351,34 @@ void get_integer( const data_t *input,
300
351
TEST_EQUAL( ret, expected_result_for_mpi );
301
352
if( ret == 0 )
302
353
{
303
- TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi , &expected_mpi ) == 0 );
354
+ if( expected_value >= 0 )
355
+ {
356
+ TEST_ASSERT( mbedtls_mpi_cmp_mpi( &actual_mpi,
357
+ &expected_mpi ) == 0 );
358
+ }
359
+ else
360
+ {
361
+ /* The library ignores the sign bit in ASN.1 INTEGERs
362
+ * (which makes sense insofar as INTEGERs are sometimes
363
+ * abused as bit strings), so the result of parsing them
364
+ * is a positive integer such that expected_mpi +
365
+ * actual_mpi = 2^n where n is the length of the content
366
+ * of the INTEGER. (Leading ff octets don't matter for the
367
+ * expected value, but they matter for the actual value.)
368
+ * Test that we don't change from this behavior. If we
369
+ * decide to fix the library to change the behavior on
370
+ * negative INTEGERs, we'll fix this test code. */
371
+ unsigned char *q = input->x + 1;
372
+ size_t len;
373
+ TEST_ASSERT( mbedtls_asn1_get_len( &q, input->x + input->len,
374
+ &len ) == 0 );
375
+ TEST_ASSERT( mbedtls_mpi_lset( &complement, 1 ) == 0 );
376
+ TEST_ASSERT( mbedtls_mpi_shift_l( &complement, len * 8 ) == 0 );
377
+ TEST_ASSERT( mbedtls_mpi_add_mpi( &complement, &complement,
378
+ &expected_mpi ) == 0 );
379
+ TEST_ASSERT( mbedtls_mpi_cmp_mpi( &complement,
380
+ &actual_mpi ) == 0 );
381
+ }
304
382
TEST_ASSERT( p == input->x + input->len );
305
383
}
306
384
#endif
@@ -309,7 +387,9 @@ exit:
309
387
#if defined(MBEDTLS_BIGNUM_C)
310
388
mbedtls_mpi_free( &expected_mpi );
311
389
mbedtls_mpi_free( &actual_mpi );
390
+ mbedtls_mpi_free( &complement );
312
391
#endif
392
+ /*empty cleanup in some configurations*/ ;
313
393
}
314
394
/* END_CASE */
315
395
0 commit comments