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