2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2017-06-09 05:32:58 +02:00
|
|
|
#include "mbedtls/bignum.h"
|
2016-09-23 14:16:02 +02:00
|
|
|
#include "mbedtls/x509.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/x509_crl.h"
|
|
|
|
#include "mbedtls/x509_csr.h"
|
2019-05-02 14:05:58 +02:00
|
|
|
#include "mbedtls/x509_internal.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/base64.h"
|
2017-04-06 12:55:43 +02:00
|
|
|
#include "string.h"
|
2019-09-03 15:50:17 +02:00
|
|
|
/* We need to include x509.c because we are testing x509 internal
|
|
|
|
* functions from x509_internal.h which are static. With this include
|
|
|
|
* we get the tested functions defined. */
|
2019-10-24 14:48:47 +02:00
|
|
|
#if defined(TEST_ENV_MBED_OS)
|
|
|
|
#include "../src/x509.c"
|
|
|
|
#else
|
2019-09-03 15:50:17 +02:00
|
|
|
#include "../library/x509.c"
|
2019-10-24 14:48:47 +02:00
|
|
|
#endif
|
2011-01-13 18:54:59 +01:00
|
|
|
|
2017-07-28 13:15:13 +02:00
|
|
|
#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
|
2017-07-26 14:38:02 +02:00
|
|
|
#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
|
|
|
|
than the current threshold 19. To test larger values, please \
|
|
|
|
adapt the script tests/data_files/dir-max/long.sh."
|
|
|
|
#endif
|
|
|
|
|
2019-06-03 15:27:03 +02:00
|
|
|
/* Test-only profile allowing all digests, PK algorithms, and curves. */
|
|
|
|
const mbedtls_x509_crt_profile profile_all =
|
|
|
|
{
|
|
|
|
0xFFFFFFFF, /* Any MD */
|
|
|
|
0xFFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFFF, /* Any curve */
|
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2017-05-05 18:59:02 +02:00
|
|
|
/* Profile for backward compatibility. Allows SHA-1, unlike the default
|
|
|
|
profile. */
|
2015-10-23 14:08:48 +02:00
|
|
|
const mbedtls_x509_crt_profile compat_profile =
|
|
|
|
{
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
|
|
|
|
0xFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFF, /* Any curve */
|
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2017-05-23 11:29:29 +02:00
|
|
|
const mbedtls_x509_crt_profile profile_rsa3072 =
|
|
|
|
{
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_RSA ),
|
|
|
|
0,
|
|
|
|
3072,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mbedtls_x509_crt_profile profile_sha512 =
|
|
|
|
{
|
|
|
|
MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
|
|
|
|
0xFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFF, /* Any curve */
|
|
|
|
1024,
|
|
|
|
};
|
|
|
|
|
2015-05-11 19:54:43 +02:00
|
|
|
int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2015-04-20 13:19:02 +02:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_OTHER;
|
2013-09-18 13:46:23 +02:00
|
|
|
|
2012-09-28 09:10:55 +02:00
|
|
|
return 0;
|
2011-01-13 18:54:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:54:43 +02:00
|
|
|
int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
|
2011-01-13 18:54:59 +01:00
|
|
|
{
|
2011-01-18 17:31:52 +01:00
|
|
|
((void) data);
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
2012-09-28 09:10:55 +02:00
|
|
|
*flags = 0;
|
2011-01-18 17:31:52 +01:00
|
|
|
|
2011-01-13 18:54:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-23 12:26:58 +02:00
|
|
|
int verify_fatal( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
|
|
|
|
{
|
|
|
|
int *levels = (int *) data;
|
|
|
|
|
|
|
|
((void) crt);
|
|
|
|
((void) certificate_depth);
|
|
|
|
|
|
|
|
/* Simulate a fatal error in the callback */
|
|
|
|
if( *levels & ( 1 << certificate_depth ) )
|
|
|
|
{
|
|
|
|
*flags |= ( 1 << certificate_depth );
|
2017-05-23 12:58:53 +02:00
|
|
|
return( -1 - certificate_depth );
|
2017-05-23 12:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-11-01 22:34:46 +01:00
|
|
|
/* strsep() not available on Windows */
|
|
|
|
char *mystrsep(char **stringp, const char *delim)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *ret = *stringp;
|
|
|
|
|
|
|
|
if( *stringp == NULL )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
for( ; ; (*stringp)++ )
|
|
|
|
{
|
|
|
|
if( **stringp == '\0' )
|
|
|
|
{
|
|
|
|
*stringp = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( p = delim; *p != '\0'; p++ )
|
|
|
|
if( **stringp == *p )
|
|
|
|
{
|
|
|
|
**stringp = '\0';
|
|
|
|
(*stringp)++;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-09-01 11:59:24 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
typedef struct {
|
|
|
|
char buf[512];
|
|
|
|
char *p;
|
|
|
|
} verify_print_context;
|
|
|
|
|
|
|
|
void verify_print_init( verify_print_context *ctx )
|
|
|
|
{
|
|
|
|
memset( ctx, 0, sizeof( verify_print_context ) );
|
|
|
|
ctx->p = ctx->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
verify_print_context *ctx = (verify_print_context *) data;
|
|
|
|
char *p = ctx->p;
|
|
|
|
size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p;
|
2019-05-20 17:26:34 +02:00
|
|
|
mbedtls_x509_crt_frame const *frame;
|
2019-02-27 19:06:47 +01:00
|
|
|
mbedtls_x509_name *subject;
|
2015-09-01 11:59:24 +02:00
|
|
|
((void) flags);
|
|
|
|
|
2019-02-27 19:06:47 +01:00
|
|
|
ret = mbedtls_x509_crt_get_subject( crt, &subject );
|
2019-02-26 17:45:32 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2019-02-27 19:06:47 +01:00
|
|
|
ret = mbedtls_x509_crt_frame_acquire( crt, &frame );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2019-02-26 17:45:32 +01:00
|
|
|
|
2015-09-01 11:59:24 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth );
|
2019-05-13 12:58:47 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2019-02-26 17:45:32 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_buf serial;
|
|
|
|
serial.p = frame->serial.p;
|
|
|
|
serial.len = frame->serial.len;
|
|
|
|
ret = mbedtls_x509_serial_gets( p, n, &serial );
|
2019-05-13 12:58:47 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
|
2019-02-26 17:45:32 +01:00
|
|
|
}
|
2015-09-01 11:59:24 +02:00
|
|
|
|
|
|
|
ret = mbedtls_snprintf( p, n, " - subject " );
|
2019-05-13 12:58:47 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2019-02-27 19:06:47 +01:00
|
|
|
ret = mbedtls_x509_dn_gets( p, n, subject );
|
2019-05-13 12:58:47 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
|
2015-09-01 11:59:24 +02:00
|
|
|
|
2017-08-21 11:00:22 +02:00
|
|
|
ret = mbedtls_snprintf( p, n, " - flags 0x%08x\n", *flags );
|
2019-05-13 12:58:47 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF_WITH_CLEANUP;
|
2015-09-01 11:59:24 +02:00
|
|
|
|
|
|
|
ctx->p = p;
|
|
|
|
|
2019-02-26 17:45:32 +01:00
|
|
|
cleanup:
|
|
|
|
|
2019-02-27 19:06:47 +01:00
|
|
|
mbedtls_x509_name_free( subject );
|
2019-03-05 14:50:56 +01:00
|
|
|
mbedtls_x509_crt_frame_release( crt );
|
2019-02-26 17:45:32 +01:00
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2015-09-01 11:59:24 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509_cert_info( char * crt_file, char * result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
res = mbedtls_x509_crt_info( buf, 2000, "", &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_crl_info( char * crl_file, char * result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl crl;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl_init( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
|
|
|
|
res = mbedtls_x509_crl_info( buf, 2000, "", &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl_free( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2016-12-08 18:10:38 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_crl_parse( char * crl_file, int result )
|
2016-12-08 18:10:38 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_crl crl;
|
|
|
|
char buf[2000];
|
|
|
|
|
|
|
|
mbedtls_x509_crl_init( &crl );
|
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crl_free( &crl );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_csr_info( char * csr_file, char * result_str )
|
2014-01-24 17:34:26 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr csr;
|
2014-01-24 17:34:26 +01:00
|
|
|
char buf[2000];
|
|
|
|
int res;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_init( &csr );
|
2014-01-24 17:34:26 +01:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 );
|
|
|
|
res = mbedtls_x509_csr_info( buf, 2000, "", &csr );
|
2014-01-24 17:34:26 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( &csr );
|
2014-01-24 17:34:26 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509_verify_info( int flags, char * prefix, char * result_str )
|
2015-04-20 11:38:13 +02:00
|
|
|
{
|
|
|
|
char buf[2000];
|
|
|
|
int res;
|
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
|
|
|
|
res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags );
|
|
|
|
|
|
|
|
TEST_ASSERT( res >= 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-07-14 11:05:59 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
|
|
|
|
void x509_verify_restart( char *crt_file, char *ca_file,
|
|
|
|
int result, int flags_result,
|
|
|
|
int max_ops, int min_restart, int max_restart )
|
|
|
|
{
|
|
|
|
int ret, cnt_restart;
|
|
|
|
mbedtls_x509_crt_restart_ctx rs_ctx;
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
|
2017-08-14 18:04:19 +02:00
|
|
|
/*
|
|
|
|
* See comments on ecp_test_vect_restart() for op count precision.
|
|
|
|
*
|
|
|
|
* For reference, with mbed TLS 2.6 and default settings:
|
|
|
|
* - ecdsa_verify() for P-256: ~ 6700
|
|
|
|
* - ecdsa_verify() for P-384: ~ 18800
|
|
|
|
* - x509_verify() for server5 -> test-ca2: ~ 18800
|
|
|
|
* - x509_verify() for server10 -> int-ca3 -> int-ca2: ~ 25500
|
|
|
|
*/
|
|
|
|
|
2017-07-14 11:05:59 +02:00
|
|
|
mbedtls_x509_crt_restart_init( &rs_ctx );
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
|
|
|
mbedtls_x509_crt_init( &ca );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_ecp_set_max_ops( max_ops );
|
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
|
|
|
ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
|
2019-08-19 13:48:09 +02:00
|
|
|
&mbedtls_x509_crt_profile_default,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags,
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
|
|
|
NULL, NULL,
|
|
|
|
#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
|
|
|
&rs_ctx );
|
2017-07-14 11:05:59 +02:00
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
|
|
|
|
|
|
|
|
TEST_ASSERT( ret == result );
|
2019-08-21 16:25:48 +02:00
|
|
|
if( flags != (uint32_t) flags_result )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Expected %#04x, got %#04x\n",
|
|
|
|
(unsigned) flags_result, (unsigned) flags );
|
|
|
|
}
|
2017-07-14 11:05:59 +02:00
|
|
|
TEST_ASSERT( flags == (uint32_t) flags_result );
|
|
|
|
|
|
|
|
TEST_ASSERT( cnt_restart >= min_restart );
|
|
|
|
TEST_ASSERT( cnt_restart <= max_restart );
|
|
|
|
|
|
|
|
/* Do we leak memory when aborting? */
|
|
|
|
ret = mbedtls_x509_crt_verify_restartable( &crt, &ca, NULL,
|
2019-08-19 13:48:09 +02:00
|
|
|
&mbedtls_x509_crt_profile_default,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags,
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
|
|
|
NULL, NULL,
|
|
|
|
#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
|
|
|
&rs_ctx );
|
2017-07-14 11:05:59 +02:00
|
|
|
TEST_ASSERT( ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_restart_free( &rs_ctx );
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
mbedtls_x509_crt_free( &ca );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
|
2013-08-20 11:48:36 +02:00
|
|
|
void x509_verify( char *crt_file, char *ca_file, char *crl_file,
|
|
|
|
char *cn_name_str, int result, int flags_result,
|
2017-05-05 18:59:02 +02:00
|
|
|
char *profile_str,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *verify_callback )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
mbedtls_x509_crl crl;
|
2015-05-11 19:54:43 +02:00
|
|
|
uint32_t flags = 0;
|
2009-07-11 21:15:20 +02:00
|
|
|
int res;
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
2015-05-11 19:54:43 +02:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
|
2019-07-01 18:36:12 +02:00
|
|
|
#endif
|
2017-05-05 18:59:02 +02:00
|
|
|
const mbedtls_x509_crt_profile *profile;
|
2019-08-19 13:48:09 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
char * cn_name = NULL;
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
|
|
|
mbedtls_x509_crt_init( &ca );
|
|
|
|
mbedtls_x509_crl_init( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2019-08-19 13:48:09 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( cn_name_str, "NULL" ) != 0 )
|
|
|
|
cn_name = cn_name_str;
|
2019-08-19 13:48:09 +02:00
|
|
|
#else
|
|
|
|
(void)cn_name_str;
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2017-08-09 10:41:42 +02:00
|
|
|
if( strcmp( profile_str, "" ) == 0 )
|
2017-05-05 18:59:02 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_default;
|
2018-02-06 17:47:17 +01:00
|
|
|
else if( strcmp( profile_str, "next" ) == 0 )
|
|
|
|
profile = &mbedtls_x509_crt_profile_next;
|
|
|
|
else if( strcmp( profile_str, "suite_b" ) == 0 )
|
|
|
|
profile = &mbedtls_x509_crt_profile_suiteb;
|
2017-05-05 18:59:02 +02:00
|
|
|
else if( strcmp( profile_str, "compat" ) == 0 )
|
|
|
|
profile = &compat_profile;
|
2019-06-03 15:27:03 +02:00
|
|
|
else if( strcmp( profile_str, "all" ) == 0 )
|
|
|
|
profile = &profile_all;
|
2017-05-05 18:59:02 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown algorithm profile" == 0 );
|
|
|
|
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( verify_callback, "NULL" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = NULL;
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( verify_callback, "verify_none" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_none;
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( verify_callback, "verify_all" ) == 0 )
|
2013-08-16 13:38:47 +02:00
|
|
|
f_vrfy = verify_all;
|
|
|
|
else
|
|
|
|
TEST_ASSERT( "No known verify callback selected" == 0 );
|
2019-07-01 18:36:12 +02:00
|
|
|
#else
|
|
|
|
if( strcmp( verify_callback, "NULL" ) != 0 )
|
|
|
|
TEST_ASSERT( "Verify callbacks disabled" == 0 );
|
|
|
|
#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
2019-08-19 13:48:09 +02:00
|
|
|
res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
cn_name,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags, f_vrfy, NULL );
|
2019-07-01 18:36:12 +02:00
|
|
|
#else
|
|
|
|
res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
cn_name,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags );
|
|
|
|
#endif
|
2015-10-23 14:08:48 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
TEST_ASSERT( res == ( result ) );
|
2019-08-21 16:25:48 +02:00
|
|
|
if( flags != (uint32_t) flags_result )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Expected %#04x, got %#04x\n",
|
|
|
|
(unsigned) flags_result, (unsigned) flags );
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:54:43 +02:00
|
|
|
TEST_ASSERT( flags == (uint32_t)( flags_result ) );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
mbedtls_x509_crt_free( &ca );
|
|
|
|
mbedtls_x509_crl_free( &crl );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2019-07-01 18:36:12 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
2017-07-05 18:14:38 +02:00
|
|
|
void x509_verify_callback( char *crt_file, char *ca_file, char *name,
|
2015-09-01 11:59:24 +02:00
|
|
|
int exp_ret, char *exp_vrfy_out )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt ca;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
verify_print_context vrfy_ctx;
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
|
|
|
mbedtls_x509_crt_init( &ca );
|
|
|
|
verify_print_init( &vrfy_ctx );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
|
|
|
|
|
2019-08-19 13:48:09 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
2017-07-05 18:14:38 +02:00
|
|
|
if( strcmp( name, "NULL" ) == 0 )
|
|
|
|
name = NULL;
|
2019-08-19 13:48:09 +02:00
|
|
|
#else
|
|
|
|
(void)name;
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
2017-07-05 18:14:38 +02:00
|
|
|
|
2017-05-05 18:59:02 +02:00
|
|
|
ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL,
|
|
|
|
&compat_profile,
|
2019-08-19 13:48:09 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
name,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags, verify_print, &vrfy_ctx );
|
2015-09-01 11:59:24 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( ret == exp_ret );
|
|
|
|
TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
mbedtls_x509_crt_free( &ca );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_dn_gets( char * crt_file, char * entity, char * result_str )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2009-07-11 00:38:58 +02:00
|
|
|
char buf[2000];
|
2013-08-16 13:38:47 +02:00
|
|
|
int res = 0;
|
2019-02-27 10:05:41 +01:00
|
|
|
mbedtls_x509_name *subject = NULL, *issuer = NULL;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
memset( buf, 0, 2000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_get_subject( &crt, &subject ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_get_issuer( &crt, &issuer ) == 0 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( entity, "subject" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
res = mbedtls_x509_dn_gets( buf, 2000, subject );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( entity, "issuer" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
res = mbedtls_x509_dn_gets( buf, 2000, issuer );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown entity" == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( buf, result_str ) == 0 );
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2019-02-27 10:05:41 +01:00
|
|
|
mbedtls_x509_name_free( issuer );
|
|
|
|
mbedtls_x509_name_free( subject );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_time_is_past( char * crt_file, char * entity, int result )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2019-02-27 10:05:41 +01:00
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt_frame frame;
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( entity, "valid_from" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_from ) == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
else if( strcmp( entity, "valid_to" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_time_is_past( &frame.valid_to ) == result );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown entity" == 0 );
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-11 00:38:58 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_time_is_future( char * crt_file, char * entity, int result )
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2019-02-27 10:05:41 +01:00
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt_frame frame;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_get_frame( &crt, &frame ) == 0 );
|
2014-03-10 12:26:11 +01:00
|
|
|
|
|
|
|
if( strcmp( entity, "valid_from" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_from ) == result );
|
2014-03-10 12:26:11 +01:00
|
|
|
else if( strcmp( entity, "valid_to" ) == 0 )
|
2019-02-27 10:05:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_time_is_future( &frame.valid_to ) == result );
|
2014-03-10 12:26:11 +01:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "Unknown entity" == 0 );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509parse_crt_file( char * crt_file, int result )
|
2014-09-26 14:53:04 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-09-26 14:53:04 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2014-09-26 14:53:04 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result );
|
2014-09-26 14:53:04 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2014-09-26 14:53:04 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509parse_crt( data_t * buf, char * result_str, int result )
|
2009-07-19 21:36:15 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2019-06-10 16:08:43 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2009-07-19 21:36:15 +02:00
|
|
|
unsigned char output[2000];
|
2017-05-30 15:23:15 +02:00
|
|
|
int res;
|
2018-12-11 20:55:56 +01:00
|
|
|
#endif
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2019-01-31 10:15:53 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == ( result ) );
|
2018-12-11 20:55:56 +01:00
|
|
|
#if defined(MBEDTLS_X509_INFO)
|
2019-01-31 10:15:53 +01:00
|
|
|
if( ( result ) == 0 )
|
|
|
|
{
|
2018-12-11 20:55:56 +01:00
|
|
|
memset( output, 0, 2000 );
|
2019-01-31 10:15:53 +01:00
|
|
|
res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
|
|
|
|
|
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
|
|
|
TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
|
|
|
|
}
|
2019-06-11 18:16:13 +02:00
|
|
|
#else
|
|
|
|
((void) result_str);
|
2018-12-11 20:55:56 +01:00
|
|
|
#endif
|
2019-01-31 10:15:53 +01:00
|
|
|
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2019-01-31 10:15:53 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_der_nocopy( &crt, buf->x, buf->len ) == ( result ) );
|
2018-12-11 20:55:56 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf->x, buf->len ) == ( result ) );
|
2019-06-10 16:08:43 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ( result ) == 0 )
|
2009-07-19 21:36:15 +02:00
|
|
|
{
|
2018-12-11 20:55:56 +01:00
|
|
|
memset( output, 0, 2000 );
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2009-07-19 21:36:15 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2018-12-11 20:55:56 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
2019-06-10 16:08:43 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2018-12-11 20:55:56 +01:00
|
|
|
memset( output, 0, 2000 );
|
|
|
|
#endif
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2009-07-19 21:36:15 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-19 21:36:15 +02:00
|
|
|
|
2019-06-28 16:52:54 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
void x509_nested_acquire( data_t * buf )
|
|
|
|
{
|
|
|
|
/* This tests exercises the behavior of the library when
|
|
|
|
* facing nested calls to mbedtls_x509_crt_xxx_acquire().
|
|
|
|
* This is allowed if !MBEDTLS_X509_ALWAYS_FLUSH or
|
|
|
|
* MBEDTLS_THREADING_C, but forbidden otherwise. */
|
|
|
|
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_x509_crt_init( &crt );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_der( &crt, buf->x, buf->len ) == 0 );
|
|
|
|
|
|
|
|
/* Nested aquire for CRT frames */
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_x509_crt_frame const *frame1;
|
|
|
|
mbedtls_x509_crt_frame const *frame2;
|
|
|
|
|
|
|
|
/* Perform a (hopefully) innocent acquire-release pair first. */
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_frame_acquire( &crt, &frame1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_frame_release( &crt ) == 0 );
|
|
|
|
|
|
|
|
/* Perform two nested acquire calls. */
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_frame_acquire( &crt, &frame1 ) == 0 );
|
|
|
|
|
|
|
|
ret = mbedtls_x509_crt_frame_acquire( &crt, &frame2 );
|
|
|
|
#if defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
|
|
|
|
!defined(MBEDTLS_THREADING_C)
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_X509_FATAL_ERROR );
|
|
|
|
#else
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_frame_release( &crt ) == 0 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_frame_release( &crt ) == 0 );
|
|
|
|
|
|
|
|
ret = mbedtls_x509_crt_frame_release( &crt );
|
|
|
|
|
|
|
|
/* In contexts which use resource counting, we expect an
|
|
|
|
* error on an attempted release() without prior acquire(). */
|
|
|
|
#if defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
|
|
|
|
!defined(MBEDTLS_THREADING_C)
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
#else
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_X509_FATAL_ERROR );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nested aquire for PK contexts */
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_pk_context *pk1;
|
|
|
|
mbedtls_pk_context *pk2;
|
|
|
|
|
|
|
|
/* Perform a (hopefully) innocent acquire-release pair first. */
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_pk_acquire( &crt, &pk1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_pk_release( &crt ) == 0 );
|
|
|
|
|
|
|
|
/* Perform two nested acquire calls. */
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_pk_acquire( &crt, &pk1 ) == 0 );
|
|
|
|
|
|
|
|
ret = mbedtls_x509_crt_pk_acquire( &crt, &pk2 );
|
|
|
|
#if defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
|
|
|
|
!defined(MBEDTLS_THREADING_C)
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_X509_FATAL_ERROR );
|
|
|
|
#else
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_pk_release( &crt ) == 0 );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_pk_release( &crt ) == 0 );
|
|
|
|
|
|
|
|
ret = mbedtls_x509_crt_pk_release( &crt );
|
|
|
|
|
|
|
|
/* In contexts which use resource counting, we expect an
|
|
|
|
* error on an attempted release() without prior acquire(). */
|
|
|
|
#if defined(MBEDTLS_X509_ALWAYS_FLUSH) && \
|
|
|
|
!defined(MBEDTLS_THREADING_C)
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
#else
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_X509_FATAL_ERROR );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free( &crt );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509parse_crl( data_t * buf, char * result_str, int result )
|
2009-07-20 22:35:41 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl crl;
|
2009-07-20 22:35:41 +02:00
|
|
|
unsigned char output[2000];
|
2017-05-30 15:23:15 +02:00
|
|
|
int res;
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl_init( &crl );
|
2009-07-20 22:35:41 +02:00
|
|
|
memset( output, 0, 2000 );
|
|
|
|
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf->x, buf->len ) == ( result ) );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ( result ) == 0 )
|
2009-07-20 22:35:41 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl );
|
2013-08-20 11:48:36 +02:00
|
|
|
|
2009-07-20 22:35:41 +02:00
|
|
|
TEST_ASSERT( res != -1 );
|
|
|
|
TEST_ASSERT( res != -2 );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 );
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2012-02-11 19:43:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crl_free( &crl );
|
2009-07-20 22:35:41 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-20 22:35:41 +02:00
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2018-06-29 12:05:32 +02:00
|
|
|
void mbedtls_x509_csr_parse( data_t * csr_der, char * ref_out, int ref_ret )
|
2014-06-13 11:13:15 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr csr;
|
2014-06-13 11:13:15 +02:00
|
|
|
char my_out[1000];
|
|
|
|
int my_ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_init( &csr );
|
2014-06-13 11:13:15 +02:00
|
|
|
memset( my_out, 0, sizeof( my_out ) );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der->x, csr_der->len );
|
2014-06-13 11:13:15 +02:00
|
|
|
TEST_ASSERT( my_ret == ref_ret );
|
|
|
|
|
|
|
|
if( ref_ret == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr );
|
2014-06-13 11:13:15 +02:00
|
|
|
TEST_ASSERT( my_out_len == strlen( ref_out ) );
|
|
|
|
TEST_ASSERT( strcmp( my_out, ref_out ) == 0 );
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr_free( &csr );
|
2014-06-13 11:13:15 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_x509_crt_parse_path( char * crt_path, int ret, int nb_crt )
|
2013-11-26 16:43:39 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt chain, *cur;
|
2013-11-26 16:43:39 +01:00
|
|
|
int i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &chain );
|
2013-11-26 16:43:39 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret );
|
2013-11-26 16:43:39 +01:00
|
|
|
|
|
|
|
/* Check how many certs we got */
|
|
|
|
for( i = 0, cur = &chain; cur != NULL; cur = cur->next )
|
|
|
|
if( cur->raw.p != NULL )
|
|
|
|
i++;
|
|
|
|
|
|
|
|
TEST_ASSERT( i == nb_crt );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &chain );
|
2013-11-26 16:43:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-06-05 13:49:44 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int,
|
|
|
|
int ret_chk, int flags_chk )
|
|
|
|
{
|
|
|
|
char file_buf[128];
|
|
|
|
int ret;
|
|
|
|
uint32_t flags;
|
|
|
|
mbedtls_x509_crt trusted, chain;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
|
|
|
|
* with NN.crt signed by NN-1.crt
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init( &trusted );
|
|
|
|
mbedtls_x509_crt_init( &chain );
|
|
|
|
|
|
|
|
/* Load trusted root */
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 );
|
|
|
|
|
|
|
|
/* Load a chain with nb_int intermediates (from 01 to nb_int),
|
|
|
|
* plus one "end-entity" cert (nb_int + 1) */
|
|
|
|
ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir,
|
|
|
|
nb_int + 1 );
|
|
|
|
TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 );
|
|
|
|
|
|
|
|
/* Try to verify that chain */
|
2019-07-01 18:36:12 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
|
|
|
|
ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags,
|
|
|
|
NULL, NULL );
|
|
|
|
#else
|
2019-08-19 13:48:09 +02:00
|
|
|
ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL,
|
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
2019-07-01 18:36:12 +02:00
|
|
|
&flags );
|
|
|
|
#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
|
|
|
|
2017-06-05 13:49:44 +02:00
|
|
|
TEST_ASSERT( ret == ret_chk );
|
|
|
|
TEST_ASSERT( flags == (uint32_t) flags_chk );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free( &chain );
|
|
|
|
mbedtls_x509_crt_free( &trusted );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-07-01 18:36:12 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
|
2017-05-22 12:04:25 +02:00
|
|
|
void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca,
|
|
|
|
int flags_result, int result,
|
2017-05-23 12:26:58 +02:00
|
|
|
char *profile_name, int vrfy_fatal_lvls )
|
2015-10-11 10:25:22 +02:00
|
|
|
{
|
|
|
|
char* act;
|
|
|
|
uint32_t flags;
|
2017-05-22 12:04:25 +02:00
|
|
|
int res;
|
2015-10-30 09:23:19 +01:00
|
|
|
mbedtls_x509_crt trusted, chain;
|
2017-05-22 12:04:25 +02:00
|
|
|
const mbedtls_x509_crt_profile *profile = NULL;
|
2015-10-11 16:17:27 +02:00
|
|
|
|
2015-10-11 10:25:22 +02:00
|
|
|
mbedtls_x509_crt_init( &chain );
|
|
|
|
mbedtls_x509_crt_init( &trusted );
|
|
|
|
|
2015-11-01 22:34:46 +01:00
|
|
|
while( ( act = mystrsep( &chain_paths, " " ) ) != NULL )
|
2015-10-30 09:23:19 +01:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 );
|
2015-10-11 10:25:22 +02:00
|
|
|
|
2017-08-08 11:10:37 +02:00
|
|
|
if( strcmp( profile_name, "" ) == 0 )
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_default;
|
2017-08-08 11:10:37 +02:00
|
|
|
else if( strcmp( profile_name, "next" ) == 0 )
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_next;
|
2017-08-08 11:10:37 +02:00
|
|
|
else if( strcmp( profile_name, "suiteb" ) == 0 )
|
2017-05-22 12:04:25 +02:00
|
|
|
profile = &mbedtls_x509_crt_profile_suiteb;
|
2017-08-08 11:10:37 +02:00
|
|
|
else if( strcmp( profile_name, "rsa3072" ) == 0 )
|
2017-05-23 11:29:29 +02:00
|
|
|
profile = &profile_rsa3072;
|
2017-08-08 11:10:37 +02:00
|
|
|
else if( strcmp( profile_name, "sha512" ) == 0 )
|
2017-05-23 11:29:29 +02:00
|
|
|
profile = &profile_sha512;
|
2017-05-22 12:04:25 +02:00
|
|
|
|
|
|
|
res = mbedtls_x509_crt_verify_with_profile( &chain, &trusted, NULL, profile,
|
2019-08-19 13:48:09 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
|
|
|
|
NULL,
|
|
|
|
#endif /* !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
|
|
|
|
&flags, verify_fatal, &vrfy_fatal_lvls );
|
2015-10-11 16:17:27 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( res == ( result ) );
|
|
|
|
TEST_ASSERT( flags == (uint32_t)( flags_result ) );
|
2015-10-11 10:25:22 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_x509_crt_free( &trusted );
|
|
|
|
mbedtls_x509_crt_free( &chain );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-10 16:08:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_REMOVE_INFO */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509_oid_desc( data_t * buf, char * ref_desc )
|
2014-03-28 16:06:35 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf oid;
|
2015-03-20 19:14:26 +01:00
|
|
|
const char *desc = NULL;
|
|
|
|
int ret;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
oid.tag = MBEDTLS_ASN1_OID;
|
2017-06-09 05:32:58 +02:00
|
|
|
oid.p = buf->x;
|
|
|
|
oid.len = buf->len;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_oid_get_extended_key_usage( &oid, &desc );
|
2014-03-28 16:06:35 +01:00
|
|
|
|
|
|
|
if( strcmp( ref_desc, "notfound" ) == 0 )
|
2015-03-20 19:14:26 +01:00
|
|
|
{
|
|
|
|
TEST_ASSERT( ret != 0 );
|
2014-03-28 16:06:35 +01:00
|
|
|
TEST_ASSERT( desc == NULL );
|
2015-03-20 19:14:26 +01:00
|
|
|
}
|
2014-03-28 16:06:35 +01:00
|
|
|
else
|
|
|
|
{
|
2015-03-20 19:14:26 +01:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2014-03-28 16:06:35 +01:00
|
|
|
TEST_ASSERT( desc != NULL );
|
|
|
|
TEST_ASSERT( strcmp( desc, ref_desc ) == 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509_oid_numstr( data_t * oid_buf, char * numstr, int blen, int ret )
|
2014-03-28 16:06:35 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf oid;
|
2014-03-28 16:06:35 +01:00
|
|
|
char num_buf[100];
|
|
|
|
|
|
|
|
memset( num_buf, 0x2a, sizeof num_buf );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
oid.tag = MBEDTLS_ASN1_OID;
|
2017-06-09 05:32:58 +02:00
|
|
|
oid.p = oid_buf->x;
|
|
|
|
oid.len = oid_buf->len;
|
2014-03-28 16:06:35 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( (size_t) blen <= sizeof num_buf );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret );
|
2014-03-28 16:06:35 +01:00
|
|
|
|
|
|
|
if( ret >= 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( num_buf[ret] == 0 );
|
|
|
|
TEST_ASSERT( strcmp( num_buf, numstr ) == 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509_check_key_usage( char * crt_file, int usage, int ret )
|
2014-04-09 09:50:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret );
|
2014-04-09 09:50:03 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2014-04-09 09:50:03 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509_check_extended_key_usage( char * crt_file, data_t * oid, int ret
|
2017-06-09 05:32:58 +02:00
|
|
|
)
|
2014-04-10 17:53:56 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt crt;
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( &crt );
|
2014-04-10 17:53:56 +02:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, (const char *)oid->x, oid->len ) == ret );
|
2014-04-10 17:53:56 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_free( &crt );
|
2014-04-10 17:53:56 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-09-03 15:50:17 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C:!MBEDTLS_X509_CRT_REMOVE_TIME */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509_get_time( int tag, char * time_str, int ret, int year, int mon,
|
|
|
|
int day, int hour, int min, int sec )
|
2016-09-23 14:16:02 +02:00
|
|
|
{
|
|
|
|
mbedtls_x509_time time;
|
2017-02-08 15:13:02 +01:00
|
|
|
unsigned char buf[21];
|
2016-09-23 14:16:02 +02:00
|
|
|
unsigned char* start = buf;
|
|
|
|
unsigned char* end = buf;
|
|
|
|
|
|
|
|
memset( &time, 0x00, sizeof( time ) );
|
|
|
|
*end = (unsigned char)tag; end++;
|
2017-02-08 15:13:02 +01:00
|
|
|
*end = strlen( time_str );
|
|
|
|
TEST_ASSERT( *end < 20 );
|
2016-09-23 14:16:02 +02:00
|
|
|
end++;
|
|
|
|
memcpy( end, time_str, (size_t)*(end - 1) );
|
|
|
|
end += *(end - 1);
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret );
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( year == time.year );
|
|
|
|
TEST_ASSERT( mon == time.mon );
|
|
|
|
TEST_ASSERT( day == time.day );
|
|
|
|
TEST_ASSERT( hour == time.hour );
|
|
|
|
TEST_ASSERT( min == time.min );
|
|
|
|
TEST_ASSERT( sec == time.sec );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2018-06-29 12:05:32 +02:00
|
|
|
void x509_parse_rsassa_pss_params( data_t * hex_params, int params_tag,
|
2014-06-06 14:48:38 +02:00
|
|
|
int ref_msg_md, int ref_mgf_md,
|
|
|
|
int ref_salt_len, int ref_ret )
|
|
|
|
{
|
|
|
|
int my_ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf params;
|
|
|
|
mbedtls_md_type_t my_msg_md, my_mgf_md;
|
2014-06-06 14:48:38 +02:00
|
|
|
int my_salt_len;
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
params.p = hex_params->x;
|
|
|
|
params.len = hex_params->len;
|
2014-06-06 14:48:38 +02:00
|
|
|
params.tag = params_tag;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
my_ret = mbedtls_x509_get_rsassa_pss_params( ¶ms, &my_msg_md, &my_mgf_md,
|
2014-06-06 14:48:38 +02:00
|
|
|
&my_salt_len );
|
|
|
|
|
|
|
|
TEST_ASSERT( my_ret == ref_ret );
|
|
|
|
|
|
|
|
if( ref_ret == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md );
|
|
|
|
TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md );
|
2014-06-06 14:48:38 +02:00
|
|
|
TEST_ASSERT( my_salt_len == ref_salt_len );
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2017-06-09 05:32:58 +02:00
|
|
|
;;
|
2014-06-06 14:48:38 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +02:00
|
|
|
void x509_selftest( )
|
2009-07-11 00:38:58 +02:00
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 );
|
2009-07-11 00:38:58 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|