Test parameter validation for pk, pkparse and pkwrite

This commit is contained in:
Gilles Peskine 2018-12-19 00:55:07 +01:00
parent e97dc60b42
commit 78438e4109
2 changed files with 319 additions and 0 deletions

View File

@ -1,3 +1,6 @@
PK invalid parameters
invalid_parameters:
PK utils: RSA
depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME
pk_utils:MBEDTLS_PK_RSA:512:64:"RSA"

View File

@ -69,6 +69,322 @@ size_t mbedtls_rsa_key_len_func( void *ctx )
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void valid_parameters( )
{
mbedtls_pk_context pk;
size_t len;
mbedtls_pk_init( &pk );
TEST_VALID_PARAM( mbedtls_pk_setup( &pk, NULL ) ==
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
TEST_VALID_PARAM( mbedtls_pk_get_bitlen( NULL ) == 0 );
TEST_VALID_PARAM( mbedtls_pk_get_len( NULL ) == 0 );
TEST_VALID_PARAM( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 );
TEST_VALID_PARAM( mbedtls_pk_encrypt( &pk,
NULL, 0,
NULL, &len, 0,
rnd_std_rand, NULL ) ==
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
TEST_VALID_PARAM( mbedtls_pk_decrypt( &pk,
NULL, 0,
NULL, &len, 0,
rnd_std_rand, NULL ) ==
MBEDTLS_ERR_PK_BAD_INPUT_DATA );
#if defined(MBEDTLS_PK_PARSE_C)
TEST_VALID_PARAM( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1 ) ==
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
TEST_VALID_PARAM( mbedtls_pk_parse_public_key( &pk, NULL, 0 ) ==
MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
#endif /* MBEDTLS_PK_PARSE_C */
#if defined(MBEDTLS_PK_WRITE_C)
TEST_VALID_PARAM( mbedtls_pk_write_key_der( &pk, NULL, 0 ) ==
MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
TEST_VALID_PARAM( mbedtls_pk_write_pubkey_der( &pk, NULL, 0 ) ==
MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
#endif /* MBEDTLS_PK_WRITE_C */
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void invalid_parameters( )
{
size_t len;
unsigned char *null_buf = NULL;
unsigned char buf[1];
unsigned char *p = buf;
char str[1] = {0};
mbedtls_pk_context pk;
void *options = buf;
mbedtls_pk_init( &pk );
TEST_INVALID_PARAM( mbedtls_pk_init( NULL ) );
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
TEST_INVALID_PARAM( mbedtls_pk_restart_init( NULL ) );
TEST_INVALID_PARAM( mbedtls_pk_restart_free( NULL ) );
#endif
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_setup( NULL, NULL ) );
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_setup_rsa_alt( NULL, buf,
NULL, NULL, NULL ) );
#endif
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_restartable( NULL,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
buf, sizeof( buf ),
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_restartable( &pk,
MBEDTLS_MD_NONE,
NULL, sizeof( buf ),
buf, sizeof( buf ),
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_restartable( &pk,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
NULL, sizeof( buf ),
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify( NULL,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify( &pk,
MBEDTLS_MD_NONE,
NULL, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify( &pk,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
NULL, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
NULL,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
&pk,
MBEDTLS_MD_NONE,
NULL, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options,
&pk,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
NULL, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign_restartable( NULL,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
buf, &len,
rnd_std_rand, NULL,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign_restartable( &pk,
MBEDTLS_MD_NONE,
NULL, sizeof( buf ),
buf, &len,
rnd_std_rand, NULL,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign_restartable( &pk,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
NULL, &len,
rnd_std_rand, NULL,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign( NULL,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
buf, &len,
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign( &pk,
MBEDTLS_MD_NONE,
NULL, sizeof( buf ),
buf, &len,
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_sign( &pk,
MBEDTLS_MD_NONE,
buf, sizeof( buf ),
NULL, &len,
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_decrypt( NULL,
buf, sizeof( buf ),
buf, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_decrypt( &pk,
NULL, sizeof( buf ),
buf, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_decrypt( &pk,
buf, sizeof( buf ),
NULL, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_decrypt( &pk,
buf, sizeof( buf ),
buf, NULL, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_encrypt( NULL,
buf, sizeof( buf ),
buf, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_encrypt( &pk,
NULL, sizeof( buf ),
buf, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_encrypt( &pk,
buf, sizeof( buf ),
NULL, &len, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_encrypt( &pk,
buf, sizeof( buf ),
buf, NULL, sizeof( buf ),
rnd_std_rand, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_check_pair( NULL, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_check_pair( &pk, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_debug( NULL, NULL ) );
#if defined(MBEDTLS_PK_PARSE_C)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_load_file( NULL, &p, &len ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_load_file( str, NULL, &len ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_load_file( str, &p, NULL ) );
#if defined(MBEDTLS_FS_IO)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_keyfile( NULL, str, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_keyfile( &pk, NULL, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_public_keyfile( NULL, str ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_public_keyfile( &pk, NULL ) );
#endif
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_subpubkey( NULL, buf, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_subpubkey( &null_buf, buf, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_subpubkey( &p, NULL, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_subpubkey( &p, buf, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_key( NULL,
buf, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_key( &pk,
NULL, sizeof( buf ),
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_key( &pk,
buf, sizeof( buf ),
NULL, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_public_key( NULL,
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_parse_public_key( &pk,
NULL, sizeof( buf ) ) );
#endif /* MBEDTLS_PK_PARSE_C */
#if defined(MBEDTLS_PK_WRITE_C)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey( NULL, p, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey( &null_buf, p, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey( &p, NULL, &pk ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey( &p, p, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey_der( NULL,
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey_der( &pk,
NULL, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_key_der( NULL,
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_key_der( &pk,
NULL, sizeof( buf ) ) );
#if defined(MBEDTLS_PEM_WRITE_C)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey_pem( NULL,
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_pubkey_pem( &pk,
NULL, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_key_pem( NULL,
buf, sizeof( buf ) ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA,
mbedtls_pk_write_key_pem( &pk,
NULL, sizeof( buf ) ) );
#endif /* MBEDTLS_PEM_WRITE_C */
#endif /* MBEDTLS_PK_WRITE_C */
}
/* END_CASE */
/* BEGIN_CASE */
void pk_utils( int type, int size, int len, char * name )
{