blob: 5ce0bf5ff0f9794921ccf20e9fe029c645424185 [file] [log] [blame]
Hanno Beckerd687ef02019-05-29 13:05:55 +01001/* BEGIN_HEADER */
2#include "mbedtls/bignum.h"
3#include "mbedtls/x509.h"
4#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
Hanno Beckerd687ef02019-05-29 13:05:55 +01007#include "mbedtls/pem.h"
8#include "mbedtls/oid.h"
9#include "mbedtls/base64.h"
10#include "string.h"
11
12/* Profile for backward compatibility. Allows SHA-1, unlike the default
13 profile. */
14const mbedtls_x509_crt_profile compat_profile =
15{
16 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
17 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
18 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
19 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
20 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
21 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
22 0xFFFFFFF, /* Any PK alg */
23 0xFFFFFFF, /* Any curve */
24 1024,
25};
26
27typedef struct
28{
29 mbedtls_x509_crt *crt;
30 mbedtls_x509_crt *ca;
31 uint32_t expected_flags;
32 unsigned id;
33 int expected_result;
34 int iter_total;
35 int result;
36} x509_verify_thread_ctx;
37
38void* x509_verify_thread_worker( void *p )
39{
40 unsigned iter_cnt;
41 x509_verify_thread_ctx *ctx = (x509_verify_thread_ctx *) p;
42
43 for( iter_cnt=0; iter_cnt < (unsigned) ctx->iter_total; iter_cnt++ )
44 {
45 uint32_t flags;
46 int res;
47
48 res = mbedtls_x509_crt_verify_with_profile( ctx->crt, ctx->ca,
49 NULL, &compat_profile,
50 NULL, &flags, NULL, NULL );
51 if( res != ctx->expected_result ||
52 flags != ctx->expected_flags )
53 {
54 ctx->result = 1;
55 pthread_exit( NULL );
56 }
57 }
58
59 ctx->result = 0;
60 pthread_exit( NULL );
61 return( NULL );
62}
63/* END_HEADER */
64
65/* BEGIN_DEPENDENCIES
66 * depends_on:MBEDTLS_THREADING_PTHREAD:MBEDTLS_X509_CRT_PARSE_C
67 * END_DEPENDENCIES
68 */
69
70/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
71void x509_verify_thread( char *crt_file, char *ca_file,
72 int result, int flags_result,
73 int thread_total,
74 int iterations_per_thread )
75{
76 x509_verify_thread_ctx *thread_ctx;
77 pthread_t *threads;
78 int cur_thread;
79
80 mbedtls_x509_crt crt;
81 mbedtls_x509_crt ca;
82
83#if defined(MBEDTLS_USE_PSA_CRYPTO)
84 TEST_ASSERT( psa_crypto_init() == 0 );
85#endif
86
87 mbedtls_x509_crt_init( &crt );
88 mbedtls_x509_crt_init( &ca );
89 threads = mbedtls_calloc( thread_total, sizeof( pthread_t ) );
90 thread_ctx = mbedtls_calloc( thread_total, sizeof( x509_verify_thread_ctx ) );
91
92 TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 );
93 TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 );
94 TEST_ASSERT( threads != NULL );
95
96 /* Start all verify threads */
97 for( cur_thread = 0; cur_thread < thread_total; cur_thread++ )
98 {
99 thread_ctx[ cur_thread ].id = (unsigned) cur_thread;
100 thread_ctx[ cur_thread ].ca = &ca;
101 thread_ctx[ cur_thread ].crt = &crt;
102 thread_ctx[ cur_thread ].expected_result = result;
103 thread_ctx[ cur_thread ].expected_flags = flags_result;
104 thread_ctx[ cur_thread ].iter_total = iterations_per_thread;
105 TEST_ASSERT( pthread_create( &threads[ cur_thread ], NULL,
106 &x509_verify_thread_worker,
107 &thread_ctx[ cur_thread ] ) == 0 );
108 }
109
110 /* Wait for all threads to complete */
111 for( cur_thread = 0; cur_thread < thread_total; cur_thread++ )
112 TEST_ASSERT( pthread_join( threads[ cur_thread ], NULL ) == 0 );
113
114 /* Check their results */
115 for( cur_thread = 0; cur_thread < thread_total; cur_thread++ )
116 TEST_ASSERT( thread_ctx[ cur_thread ].result == 0 );
117
118exit:
119 mbedtls_free( threads );
120 mbedtls_free( thread_ctx );
121 mbedtls_x509_crt_free( &crt );
122 mbedtls_x509_crt_free( &ca );
123}
124/* END_CASE */