Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedtls by
tests/suites/test_suite_x509parse.function@2:bbdeda018a3c, 2017-09-29 (annotated)
- Committer:
- Jasper Wallace
- Date:
- Fri Sep 29 19:50:30 2017 +0100
- Revision:
- 2:bbdeda018a3c
- Parent:
- 1:9ebc941037d5
Update to mbedtls 2.6.0, many changes.
Changes to mbedtls sources made:
in include/mbedtls/config.h comment out:
#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C
uncomment:
#define MBEDTLS_NO_PLATFORM_ENTROPY
remove the following directorys:
programs
yotta
visualc
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
| markrad | 0:cdf462088d13 | 2 | #include "mbedtls/x509.h" |
| markrad | 0:cdf462088d13 | 3 | #include "mbedtls/x509_crt.h" |
| markrad | 0:cdf462088d13 | 4 | #include "mbedtls/x509_crl.h" |
| markrad | 0:cdf462088d13 | 5 | #include "mbedtls/x509_csr.h" |
| markrad | 0:cdf462088d13 | 6 | #include "mbedtls/pem.h" |
| markrad | 0:cdf462088d13 | 7 | #include "mbedtls/oid.h" |
| markrad | 0:cdf462088d13 | 8 | #include "mbedtls/base64.h" |
| markrad | 0:cdf462088d13 | 9 | |
| Jasper Wallace |
2:bbdeda018a3c | 10 | #if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19 |
| Jasper Wallace |
2:bbdeda018a3c | 11 | #error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \ |
| Jasper Wallace |
2:bbdeda018a3c | 12 | than the current threshold 19. To test larger values, please \ |
| Jasper Wallace |
2:bbdeda018a3c | 13 | adapt the script tests/data_files/dir-max/long.sh." |
| Jasper Wallace |
2:bbdeda018a3c | 14 | #endif |
| Jasper Wallace |
2:bbdeda018a3c | 15 | |
| Jasper Wallace |
2:bbdeda018a3c | 16 | /* Profile for backward compatibility. Allows SHA-1, unlike the default |
| Jasper Wallace |
2:bbdeda018a3c | 17 | profile. */ |
| markrad | 0:cdf462088d13 | 18 | const mbedtls_x509_crt_profile compat_profile = |
| markrad | 0:cdf462088d13 | 19 | { |
| markrad | 0:cdf462088d13 | 20 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) | |
| markrad | 0:cdf462088d13 | 21 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) | |
| markrad | 0:cdf462088d13 | 22 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) | |
| markrad | 0:cdf462088d13 | 23 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) | |
| markrad | 0:cdf462088d13 | 24 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) | |
| markrad | 0:cdf462088d13 | 25 | MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ), |
| markrad | 0:cdf462088d13 | 26 | 0xFFFFFFF, /* Any PK alg */ |
| markrad | 0:cdf462088d13 | 27 | 0xFFFFFFF, /* Any curve */ |
| markrad | 0:cdf462088d13 | 28 | 1024, |
| markrad | 0:cdf462088d13 | 29 | }; |
| markrad | 0:cdf462088d13 | 30 | |
| markrad | 0:cdf462088d13 | 31 | int verify_none( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags ) |
| markrad | 0:cdf462088d13 | 32 | { |
| markrad | 0:cdf462088d13 | 33 | ((void) data); |
| markrad | 0:cdf462088d13 | 34 | ((void) crt); |
| markrad | 0:cdf462088d13 | 35 | ((void) certificate_depth); |
| markrad | 0:cdf462088d13 | 36 | *flags |= MBEDTLS_X509_BADCERT_OTHER; |
| markrad | 0:cdf462088d13 | 37 | |
| markrad | 0:cdf462088d13 | 38 | return 0; |
| markrad | 0:cdf462088d13 | 39 | } |
| markrad | 0:cdf462088d13 | 40 | |
| markrad | 0:cdf462088d13 | 41 | int verify_all( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags ) |
| markrad | 0:cdf462088d13 | 42 | { |
| markrad | 0:cdf462088d13 | 43 | ((void) data); |
| markrad | 0:cdf462088d13 | 44 | ((void) crt); |
| markrad | 0:cdf462088d13 | 45 | ((void) certificate_depth); |
| markrad | 0:cdf462088d13 | 46 | *flags = 0; |
| markrad | 0:cdf462088d13 | 47 | |
| markrad | 0:cdf462088d13 | 48 | return 0; |
| markrad | 0:cdf462088d13 | 49 | } |
| markrad | 0:cdf462088d13 | 50 | |
| markrad | 0:cdf462088d13 | 51 | /* strsep() not available on Windows */ |
| markrad | 0:cdf462088d13 | 52 | char *mystrsep(char **stringp, const char *delim) |
| markrad | 0:cdf462088d13 | 53 | { |
| markrad | 0:cdf462088d13 | 54 | const char *p; |
| markrad | 0:cdf462088d13 | 55 | char *ret = *stringp; |
| markrad | 0:cdf462088d13 | 56 | |
| markrad | 0:cdf462088d13 | 57 | if( *stringp == NULL ) |
| markrad | 0:cdf462088d13 | 58 | return( NULL ); |
| markrad | 0:cdf462088d13 | 59 | |
| markrad | 0:cdf462088d13 | 60 | for( ; ; (*stringp)++ ) |
| markrad | 0:cdf462088d13 | 61 | { |
| markrad | 0:cdf462088d13 | 62 | if( **stringp == '\0' ) |
| markrad | 0:cdf462088d13 | 63 | { |
| markrad | 0:cdf462088d13 | 64 | *stringp = NULL; |
| markrad | 0:cdf462088d13 | 65 | goto done; |
| markrad | 0:cdf462088d13 | 66 | } |
| markrad | 0:cdf462088d13 | 67 | |
| markrad | 0:cdf462088d13 | 68 | for( p = delim; *p != '\0'; p++ ) |
| markrad | 0:cdf462088d13 | 69 | if( **stringp == *p ) |
| markrad | 0:cdf462088d13 | 70 | { |
| markrad | 0:cdf462088d13 | 71 | **stringp = '\0'; |
| markrad | 0:cdf462088d13 | 72 | (*stringp)++; |
| markrad | 0:cdf462088d13 | 73 | goto done; |
| markrad | 0:cdf462088d13 | 74 | } |
| markrad | 0:cdf462088d13 | 75 | } |
| markrad | 0:cdf462088d13 | 76 | |
| markrad | 0:cdf462088d13 | 77 | done: |
| markrad | 0:cdf462088d13 | 78 | return( ret ); |
| markrad | 0:cdf462088d13 | 79 | } |
| markrad | 0:cdf462088d13 | 80 | |
| markrad | 0:cdf462088d13 | 81 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| markrad | 0:cdf462088d13 | 82 | typedef struct { |
| markrad | 0:cdf462088d13 | 83 | char buf[512]; |
| markrad | 0:cdf462088d13 | 84 | char *p; |
| markrad | 0:cdf462088d13 | 85 | } verify_print_context; |
| markrad | 0:cdf462088d13 | 86 | |
| markrad | 0:cdf462088d13 | 87 | void verify_print_init( verify_print_context *ctx ) |
| markrad | 0:cdf462088d13 | 88 | { |
| markrad | 0:cdf462088d13 | 89 | memset( ctx, 0, sizeof( verify_print_context ) ); |
| markrad | 0:cdf462088d13 | 90 | ctx->p = ctx->buf; |
| markrad | 0:cdf462088d13 | 91 | } |
| markrad | 0:cdf462088d13 | 92 | |
| markrad | 0:cdf462088d13 | 93 | int verify_print( void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags ) |
| markrad | 0:cdf462088d13 | 94 | { |
| markrad | 0:cdf462088d13 | 95 | int ret; |
| markrad | 0:cdf462088d13 | 96 | verify_print_context *ctx = (verify_print_context *) data; |
| markrad | 0:cdf462088d13 | 97 | char *p = ctx->p; |
| markrad | 0:cdf462088d13 | 98 | size_t n = ctx->buf + sizeof( ctx->buf ) - ctx->p; |
| markrad | 0:cdf462088d13 | 99 | ((void) flags); |
| markrad | 0:cdf462088d13 | 100 | |
| markrad | 0:cdf462088d13 | 101 | ret = mbedtls_snprintf( p, n, "depth %d - serial ", certificate_depth ); |
| markrad | 0:cdf462088d13 | 102 | MBEDTLS_X509_SAFE_SNPRINTF; |
| markrad | 0:cdf462088d13 | 103 | |
| markrad | 0:cdf462088d13 | 104 | ret = mbedtls_x509_serial_gets( p, n, &crt->serial ); |
| markrad | 0:cdf462088d13 | 105 | MBEDTLS_X509_SAFE_SNPRINTF; |
| markrad | 0:cdf462088d13 | 106 | |
| markrad | 0:cdf462088d13 | 107 | ret = mbedtls_snprintf( p, n, " - subject " ); |
| markrad | 0:cdf462088d13 | 108 | MBEDTLS_X509_SAFE_SNPRINTF; |
| markrad | 0:cdf462088d13 | 109 | |
| markrad | 0:cdf462088d13 | 110 | ret = mbedtls_x509_dn_gets( p, n, &crt->subject ); |
| markrad | 0:cdf462088d13 | 111 | MBEDTLS_X509_SAFE_SNPRINTF; |
| markrad | 0:cdf462088d13 | 112 | |
| markrad | 0:cdf462088d13 | 113 | ret = mbedtls_snprintf( p, n, "\n" ); |
| markrad | 0:cdf462088d13 | 114 | MBEDTLS_X509_SAFE_SNPRINTF; |
| markrad | 0:cdf462088d13 | 115 | |
| markrad | 0:cdf462088d13 | 116 | ctx->p = p; |
| markrad | 0:cdf462088d13 | 117 | |
| markrad | 0:cdf462088d13 | 118 | return( 0 ); |
| markrad | 0:cdf462088d13 | 119 | } |
| markrad | 0:cdf462088d13 | 120 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 121 | /* END_HEADER */ |
| markrad | 0:cdf462088d13 | 122 | |
| markrad | 0:cdf462088d13 | 123 | /* BEGIN_DEPENDENCIES |
| markrad | 0:cdf462088d13 | 124 | * depends_on:MBEDTLS_BIGNUM_C |
| markrad | 0:cdf462088d13 | 125 | * END_DEPENDENCIES |
| markrad | 0:cdf462088d13 | 126 | */ |
| markrad | 0:cdf462088d13 | 127 | |
| markrad | 0:cdf462088d13 | 128 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 129 | void x509_cert_info( char *crt_file, char *result_str ) |
| markrad | 0:cdf462088d13 | 130 | { |
| markrad | 0:cdf462088d13 | 131 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 132 | char buf[2000]; |
| markrad | 0:cdf462088d13 | 133 | int res; |
| markrad | 0:cdf462088d13 | 134 | |
| markrad | 0:cdf462088d13 | 135 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 136 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 137 | |
| markrad | 0:cdf462088d13 | 138 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 139 | res = mbedtls_x509_crt_info( buf, 2000, "", &crt ); |
| markrad | 0:cdf462088d13 | 140 | |
| markrad | 0:cdf462088d13 | 141 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 142 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 143 | |
| markrad | 0:cdf462088d13 | 144 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 145 | |
| markrad | 0:cdf462088d13 | 146 | exit: |
| markrad | 0:cdf462088d13 | 147 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 148 | } |
| markrad | 0:cdf462088d13 | 149 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 150 | |
| markrad | 0:cdf462088d13 | 151 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */ |
| markrad | 0:cdf462088d13 | 152 | void mbedtls_x509_crl_info( char *crl_file, char *result_str ) |
| markrad | 0:cdf462088d13 | 153 | { |
| markrad | 0:cdf462088d13 | 154 | mbedtls_x509_crl crl; |
| markrad | 0:cdf462088d13 | 155 | char buf[2000]; |
| markrad | 0:cdf462088d13 | 156 | int res; |
| markrad | 0:cdf462088d13 | 157 | |
| markrad | 0:cdf462088d13 | 158 | mbedtls_x509_crl_init( &crl ); |
| markrad | 0:cdf462088d13 | 159 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 160 | |
| markrad | 0:cdf462088d13 | 161 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 162 | res = mbedtls_x509_crl_info( buf, 2000, "", &crl ); |
| markrad | 0:cdf462088d13 | 163 | |
| markrad | 0:cdf462088d13 | 164 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 165 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 166 | |
| markrad | 0:cdf462088d13 | 167 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 168 | |
| markrad | 0:cdf462088d13 | 169 | exit: |
| markrad | 0:cdf462088d13 | 170 | mbedtls_x509_crl_free( &crl ); |
| markrad | 0:cdf462088d13 | 171 | } |
| markrad | 0:cdf462088d13 | 172 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 173 | |
| Jasper Wallace |
1:9ebc941037d5 | 174 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */ |
| Jasper Wallace |
1:9ebc941037d5 | 175 | void mbedtls_x509_crl_parse( char *crl_file, int result ) |
| Jasper Wallace |
1:9ebc941037d5 | 176 | { |
| Jasper Wallace |
1:9ebc941037d5 | 177 | mbedtls_x509_crl crl; |
| Jasper Wallace |
1:9ebc941037d5 | 178 | char buf[2000]; |
| Jasper Wallace |
1:9ebc941037d5 | 179 | |
| Jasper Wallace |
1:9ebc941037d5 | 180 | mbedtls_x509_crl_init( &crl ); |
| Jasper Wallace |
1:9ebc941037d5 | 181 | memset( buf, 0, 2000 ); |
| Jasper Wallace |
1:9ebc941037d5 | 182 | |
| Jasper Wallace |
1:9ebc941037d5 | 183 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == result ); |
| Jasper Wallace |
1:9ebc941037d5 | 184 | |
| Jasper Wallace |
1:9ebc941037d5 | 185 | exit: |
| Jasper Wallace |
1:9ebc941037d5 | 186 | mbedtls_x509_crl_free( &crl ); |
| Jasper Wallace |
1:9ebc941037d5 | 187 | } |
| Jasper Wallace |
1:9ebc941037d5 | 188 | /* END_CASE */ |
| Jasper Wallace |
1:9ebc941037d5 | 189 | |
| markrad | 0:cdf462088d13 | 190 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C */ |
| markrad | 0:cdf462088d13 | 191 | void mbedtls_x509_csr_info( char *csr_file, char *result_str ) |
| markrad | 0:cdf462088d13 | 192 | { |
| markrad | 0:cdf462088d13 | 193 | mbedtls_x509_csr csr; |
| markrad | 0:cdf462088d13 | 194 | char buf[2000]; |
| markrad | 0:cdf462088d13 | 195 | int res; |
| markrad | 0:cdf462088d13 | 196 | |
| markrad | 0:cdf462088d13 | 197 | mbedtls_x509_csr_init( &csr ); |
| markrad | 0:cdf462088d13 | 198 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 199 | |
| markrad | 0:cdf462088d13 | 200 | TEST_ASSERT( mbedtls_x509_csr_parse_file( &csr, csr_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 201 | res = mbedtls_x509_csr_info( buf, 2000, "", &csr ); |
| markrad | 0:cdf462088d13 | 202 | |
| markrad | 0:cdf462088d13 | 203 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 204 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 205 | |
| markrad | 0:cdf462088d13 | 206 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 207 | |
| markrad | 0:cdf462088d13 | 208 | exit: |
| markrad | 0:cdf462088d13 | 209 | mbedtls_x509_csr_free( &csr ); |
| markrad | 0:cdf462088d13 | 210 | } |
| markrad | 0:cdf462088d13 | 211 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 212 | |
| markrad | 0:cdf462088d13 | 213 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 214 | void x509_verify_info( int flags, char *prefix, char *result_str ) |
| markrad | 0:cdf462088d13 | 215 | { |
| markrad | 0:cdf462088d13 | 216 | char buf[2000]; |
| markrad | 0:cdf462088d13 | 217 | int res; |
| markrad | 0:cdf462088d13 | 218 | |
| markrad | 0:cdf462088d13 | 219 | memset( buf, 0, sizeof( buf ) ); |
| markrad | 0:cdf462088d13 | 220 | |
| markrad | 0:cdf462088d13 | 221 | res = mbedtls_x509_crt_verify_info( buf, sizeof( buf ), prefix, flags ); |
| markrad | 0:cdf462088d13 | 222 | |
| markrad | 0:cdf462088d13 | 223 | TEST_ASSERT( res >= 0 ); |
| markrad | 0:cdf462088d13 | 224 | |
| markrad | 0:cdf462088d13 | 225 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 226 | } |
| markrad | 0:cdf462088d13 | 227 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 228 | |
| markrad | 0:cdf462088d13 | 229 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */ |
| markrad | 0:cdf462088d13 | 230 | void x509_verify( char *crt_file, char *ca_file, char *crl_file, |
| markrad | 0:cdf462088d13 | 231 | char *cn_name_str, int result, int flags_result, |
| Jasper Wallace |
2:bbdeda018a3c | 232 | char *profile_str, |
| markrad | 0:cdf462088d13 | 233 | char *verify_callback ) |
| markrad | 0:cdf462088d13 | 234 | { |
| markrad | 0:cdf462088d13 | 235 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 236 | mbedtls_x509_crt ca; |
| markrad | 0:cdf462088d13 | 237 | mbedtls_x509_crl crl; |
| markrad | 0:cdf462088d13 | 238 | uint32_t flags = 0; |
| markrad | 0:cdf462088d13 | 239 | int res; |
| markrad | 0:cdf462088d13 | 240 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL; |
| markrad | 0:cdf462088d13 | 241 | char * cn_name = NULL; |
| Jasper Wallace |
2:bbdeda018a3c | 242 | const mbedtls_x509_crt_profile *profile; |
| markrad | 0:cdf462088d13 | 243 | |
| markrad | 0:cdf462088d13 | 244 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 245 | mbedtls_x509_crt_init( &ca ); |
| markrad | 0:cdf462088d13 | 246 | mbedtls_x509_crl_init( &crl ); |
| markrad | 0:cdf462088d13 | 247 | |
| markrad | 0:cdf462088d13 | 248 | if( strcmp( cn_name_str, "NULL" ) != 0 ) |
| markrad | 0:cdf462088d13 | 249 | cn_name = cn_name_str; |
| markrad | 0:cdf462088d13 | 250 | |
| Jasper Wallace |
2:bbdeda018a3c | 251 | if( strcmp( profile_str, "default" ) == 0 ) |
| Jasper Wallace |
2:bbdeda018a3c | 252 | profile = &mbedtls_x509_crt_profile_default; |
| Jasper Wallace |
2:bbdeda018a3c | 253 | else if( strcmp( profile_str, "compat" ) == 0 ) |
| Jasper Wallace |
2:bbdeda018a3c | 254 | profile = &compat_profile; |
| Jasper Wallace |
2:bbdeda018a3c | 255 | else |
| Jasper Wallace |
2:bbdeda018a3c | 256 | TEST_ASSERT( "Unknown algorithm profile" == 0 ); |
| Jasper Wallace |
2:bbdeda018a3c | 257 | |
| markrad | 0:cdf462088d13 | 258 | if( strcmp( verify_callback, "NULL" ) == 0 ) |
| markrad | 0:cdf462088d13 | 259 | f_vrfy = NULL; |
| markrad | 0:cdf462088d13 | 260 | else if( strcmp( verify_callback, "verify_none" ) == 0 ) |
| markrad | 0:cdf462088d13 | 261 | f_vrfy = verify_none; |
| markrad | 0:cdf462088d13 | 262 | else if( strcmp( verify_callback, "verify_all" ) == 0 ) |
| markrad | 0:cdf462088d13 | 263 | f_vrfy = verify_all; |
| markrad | 0:cdf462088d13 | 264 | else |
| markrad | 0:cdf462088d13 | 265 | TEST_ASSERT( "No known verify callback selected" == 0 ); |
| markrad | 0:cdf462088d13 | 266 | |
| markrad | 0:cdf462088d13 | 267 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 268 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 269 | TEST_ASSERT( mbedtls_x509_crl_parse_file( &crl, crl_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 270 | |
| Jasper Wallace |
2:bbdeda018a3c | 271 | res = mbedtls_x509_crt_verify_with_profile( &crt, &ca, &crl, profile, cn_name, &flags, f_vrfy, NULL ); |
| markrad | 0:cdf462088d13 | 272 | |
| markrad | 0:cdf462088d13 | 273 | TEST_ASSERT( res == ( result ) ); |
| markrad | 0:cdf462088d13 | 274 | TEST_ASSERT( flags == (uint32_t)( flags_result ) ); |
| markrad | 0:cdf462088d13 | 275 | |
| markrad | 0:cdf462088d13 | 276 | exit: |
| markrad | 0:cdf462088d13 | 277 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 278 | mbedtls_x509_crt_free( &ca ); |
| markrad | 0:cdf462088d13 | 279 | mbedtls_x509_crl_free( &crl ); |
| markrad | 0:cdf462088d13 | 280 | } |
| markrad | 0:cdf462088d13 | 281 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 282 | |
| markrad | 0:cdf462088d13 | 283 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 284 | void x509_verify_callback( char *crt_file, char *ca_file, |
| markrad | 0:cdf462088d13 | 285 | int exp_ret, char *exp_vrfy_out ) |
| markrad | 0:cdf462088d13 | 286 | { |
| markrad | 0:cdf462088d13 | 287 | int ret; |
| markrad | 0:cdf462088d13 | 288 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 289 | mbedtls_x509_crt ca; |
| markrad | 0:cdf462088d13 | 290 | uint32_t flags = 0; |
| markrad | 0:cdf462088d13 | 291 | verify_print_context vrfy_ctx; |
| markrad | 0:cdf462088d13 | 292 | |
| markrad | 0:cdf462088d13 | 293 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 294 | mbedtls_x509_crt_init( &ca ); |
| markrad | 0:cdf462088d13 | 295 | verify_print_init( &vrfy_ctx ); |
| markrad | 0:cdf462088d13 | 296 | |
| markrad | 0:cdf462088d13 | 297 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 298 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &ca, ca_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 299 | |
| Jasper Wallace |
2:bbdeda018a3c | 300 | ret = mbedtls_x509_crt_verify_with_profile( &crt, &ca, NULL, |
| Jasper Wallace |
2:bbdeda018a3c | 301 | &compat_profile, |
| Jasper Wallace |
2:bbdeda018a3c | 302 | NULL, &flags, |
| Jasper Wallace |
2:bbdeda018a3c | 303 | verify_print, &vrfy_ctx ); |
| markrad | 0:cdf462088d13 | 304 | |
| markrad | 0:cdf462088d13 | 305 | TEST_ASSERT( ret == exp_ret ); |
| markrad | 0:cdf462088d13 | 306 | TEST_ASSERT( strcmp( vrfy_ctx.buf, exp_vrfy_out ) == 0 ); |
| markrad | 0:cdf462088d13 | 307 | |
| markrad | 0:cdf462088d13 | 308 | exit: |
| markrad | 0:cdf462088d13 | 309 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 310 | mbedtls_x509_crt_free( &ca ); |
| markrad | 0:cdf462088d13 | 311 | } |
| markrad | 0:cdf462088d13 | 312 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 313 | |
| markrad | 0:cdf462088d13 | 314 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 315 | void mbedtls_x509_dn_gets( char *crt_file, char *entity, char *result_str ) |
| markrad | 0:cdf462088d13 | 316 | { |
| markrad | 0:cdf462088d13 | 317 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 318 | char buf[2000]; |
| markrad | 0:cdf462088d13 | 319 | int res = 0; |
| markrad | 0:cdf462088d13 | 320 | |
| markrad | 0:cdf462088d13 | 321 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 322 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 323 | |
| markrad | 0:cdf462088d13 | 324 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 325 | if( strcmp( entity, "subject" ) == 0 ) |
| markrad | 0:cdf462088d13 | 326 | res = mbedtls_x509_dn_gets( buf, 2000, &crt.subject ); |
| markrad | 0:cdf462088d13 | 327 | else if( strcmp( entity, "issuer" ) == 0 ) |
| markrad | 0:cdf462088d13 | 328 | res = mbedtls_x509_dn_gets( buf, 2000, &crt.issuer ); |
| markrad | 0:cdf462088d13 | 329 | else |
| markrad | 0:cdf462088d13 | 330 | TEST_ASSERT( "Unknown entity" == 0 ); |
| markrad | 0:cdf462088d13 | 331 | |
| markrad | 0:cdf462088d13 | 332 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 333 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 334 | |
| markrad | 0:cdf462088d13 | 335 | TEST_ASSERT( strcmp( buf, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 336 | |
| markrad | 0:cdf462088d13 | 337 | exit: |
| markrad | 0:cdf462088d13 | 338 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 339 | } |
| markrad | 0:cdf462088d13 | 340 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 341 | |
| markrad | 0:cdf462088d13 | 342 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 343 | void mbedtls_x509_time_is_past( char *crt_file, char *entity, int result ) |
| markrad | 0:cdf462088d13 | 344 | { |
| markrad | 0:cdf462088d13 | 345 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 346 | |
| markrad | 0:cdf462088d13 | 347 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 348 | |
| markrad | 0:cdf462088d13 | 349 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 350 | |
| markrad | 0:cdf462088d13 | 351 | if( strcmp( entity, "valid_from" ) == 0 ) |
| markrad | 0:cdf462088d13 | 352 | TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_from ) == result ); |
| markrad | 0:cdf462088d13 | 353 | else if( strcmp( entity, "valid_to" ) == 0 ) |
| markrad | 0:cdf462088d13 | 354 | TEST_ASSERT( mbedtls_x509_time_is_past( &crt.valid_to ) == result ); |
| markrad | 0:cdf462088d13 | 355 | else |
| markrad | 0:cdf462088d13 | 356 | TEST_ASSERT( "Unknown entity" == 0 ); |
| markrad | 0:cdf462088d13 | 357 | |
| markrad | 0:cdf462088d13 | 358 | exit: |
| markrad | 0:cdf462088d13 | 359 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 360 | } |
| markrad | 0:cdf462088d13 | 361 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 362 | |
| markrad | 0:cdf462088d13 | 363 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 364 | void mbedtls_x509_time_is_future( char *crt_file, char *entity, int result ) |
| markrad | 0:cdf462088d13 | 365 | { |
| markrad | 0:cdf462088d13 | 366 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 367 | |
| markrad | 0:cdf462088d13 | 368 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 369 | |
| markrad | 0:cdf462088d13 | 370 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 371 | |
| markrad | 0:cdf462088d13 | 372 | if( strcmp( entity, "valid_from" ) == 0 ) |
| markrad | 0:cdf462088d13 | 373 | TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_from ) == result ); |
| markrad | 0:cdf462088d13 | 374 | else if( strcmp( entity, "valid_to" ) == 0 ) |
| markrad | 0:cdf462088d13 | 375 | TEST_ASSERT( mbedtls_x509_time_is_future( &crt.valid_to ) == result ); |
| markrad | 0:cdf462088d13 | 376 | else |
| markrad | 0:cdf462088d13 | 377 | TEST_ASSERT( "Unknown entity" == 0 ); |
| markrad | 0:cdf462088d13 | 378 | |
| markrad | 0:cdf462088d13 | 379 | exit: |
| markrad | 0:cdf462088d13 | 380 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 381 | } |
| markrad | 0:cdf462088d13 | 382 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 383 | |
| markrad | 0:cdf462088d13 | 384 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */ |
| markrad | 0:cdf462088d13 | 385 | void x509parse_crt_file( char *crt_file, int result ) |
| markrad | 0:cdf462088d13 | 386 | { |
| markrad | 0:cdf462088d13 | 387 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 388 | |
| markrad | 0:cdf462088d13 | 389 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 390 | |
| markrad | 0:cdf462088d13 | 391 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == result ); |
| markrad | 0:cdf462088d13 | 392 | |
| markrad | 0:cdf462088d13 | 393 | exit: |
| markrad | 0:cdf462088d13 | 394 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 395 | } |
| markrad | 0:cdf462088d13 | 396 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 397 | |
| markrad | 0:cdf462088d13 | 398 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 399 | void x509parse_crt( char *crt_data, char *result_str, int result ) |
| markrad | 0:cdf462088d13 | 400 | { |
| markrad | 0:cdf462088d13 | 401 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 402 | unsigned char buf[2000]; |
| markrad | 0:cdf462088d13 | 403 | unsigned char output[2000]; |
| markrad | 0:cdf462088d13 | 404 | int data_len, res; |
| markrad | 0:cdf462088d13 | 405 | |
| markrad | 0:cdf462088d13 | 406 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 407 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 408 | memset( output, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 409 | |
| markrad | 0:cdf462088d13 | 410 | data_len = unhexify( buf, crt_data ); |
| markrad | 0:cdf462088d13 | 411 | |
| markrad | 0:cdf462088d13 | 412 | TEST_ASSERT( mbedtls_x509_crt_parse( &crt, buf, data_len ) == ( result ) ); |
| markrad | 0:cdf462088d13 | 413 | if( ( result ) == 0 ) |
| markrad | 0:cdf462088d13 | 414 | { |
| markrad | 0:cdf462088d13 | 415 | res = mbedtls_x509_crt_info( (char *) output, 2000, "", &crt ); |
| markrad | 0:cdf462088d13 | 416 | |
| markrad | 0:cdf462088d13 | 417 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 418 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 419 | |
| markrad | 0:cdf462088d13 | 420 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 421 | } |
| markrad | 0:cdf462088d13 | 422 | |
| markrad | 0:cdf462088d13 | 423 | exit: |
| markrad | 0:cdf462088d13 | 424 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 425 | } |
| markrad | 0:cdf462088d13 | 426 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 427 | |
| markrad | 0:cdf462088d13 | 428 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C */ |
| markrad | 0:cdf462088d13 | 429 | void x509parse_crl( char *crl_data, char *result_str, int result ) |
| markrad | 0:cdf462088d13 | 430 | { |
| markrad | 0:cdf462088d13 | 431 | mbedtls_x509_crl crl; |
| markrad | 0:cdf462088d13 | 432 | unsigned char buf[2000]; |
| markrad | 0:cdf462088d13 | 433 | unsigned char output[2000]; |
| markrad | 0:cdf462088d13 | 434 | int data_len, res; |
| markrad | 0:cdf462088d13 | 435 | |
| markrad | 0:cdf462088d13 | 436 | mbedtls_x509_crl_init( &crl ); |
| markrad | 0:cdf462088d13 | 437 | memset( buf, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 438 | memset( output, 0, 2000 ); |
| markrad | 0:cdf462088d13 | 439 | |
| markrad | 0:cdf462088d13 | 440 | data_len = unhexify( buf, crl_data ); |
| markrad | 0:cdf462088d13 | 441 | |
| markrad | 0:cdf462088d13 | 442 | TEST_ASSERT( mbedtls_x509_crl_parse( &crl, buf, data_len ) == ( result ) ); |
| markrad | 0:cdf462088d13 | 443 | if( ( result ) == 0 ) |
| markrad | 0:cdf462088d13 | 444 | { |
| markrad | 0:cdf462088d13 | 445 | res = mbedtls_x509_crl_info( (char *) output, 2000, "", &crl ); |
| markrad | 0:cdf462088d13 | 446 | |
| markrad | 0:cdf462088d13 | 447 | TEST_ASSERT( res != -1 ); |
| markrad | 0:cdf462088d13 | 448 | TEST_ASSERT( res != -2 ); |
| markrad | 0:cdf462088d13 | 449 | |
| markrad | 0:cdf462088d13 | 450 | TEST_ASSERT( strcmp( (char *) output, result_str ) == 0 ); |
| markrad | 0:cdf462088d13 | 451 | } |
| markrad | 0:cdf462088d13 | 452 | |
| markrad | 0:cdf462088d13 | 453 | exit: |
| markrad | 0:cdf462088d13 | 454 | mbedtls_x509_crl_free( &crl ); |
| markrad | 0:cdf462088d13 | 455 | } |
| markrad | 0:cdf462088d13 | 456 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 457 | |
| markrad | 0:cdf462088d13 | 458 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C */ |
| markrad | 0:cdf462088d13 | 459 | void mbedtls_x509_csr_parse( char *csr_der_hex, char *ref_out, int ref_ret ) |
| markrad | 0:cdf462088d13 | 460 | { |
| markrad | 0:cdf462088d13 | 461 | mbedtls_x509_csr csr; |
| markrad | 0:cdf462088d13 | 462 | unsigned char *csr_der = NULL; |
| markrad | 0:cdf462088d13 | 463 | char my_out[1000]; |
| markrad | 0:cdf462088d13 | 464 | size_t csr_der_len; |
| markrad | 0:cdf462088d13 | 465 | int my_ret; |
| markrad | 0:cdf462088d13 | 466 | |
| markrad | 0:cdf462088d13 | 467 | mbedtls_x509_csr_init( &csr ); |
| markrad | 0:cdf462088d13 | 468 | memset( my_out, 0, sizeof( my_out ) ); |
| markrad | 0:cdf462088d13 | 469 | csr_der = unhexify_alloc( csr_der_hex, &csr_der_len ); |
| markrad | 0:cdf462088d13 | 470 | |
| markrad | 0:cdf462088d13 | 471 | my_ret = mbedtls_x509_csr_parse_der( &csr, csr_der, csr_der_len ); |
| markrad | 0:cdf462088d13 | 472 | TEST_ASSERT( my_ret == ref_ret ); |
| markrad | 0:cdf462088d13 | 473 | |
| markrad | 0:cdf462088d13 | 474 | if( ref_ret == 0 ) |
| markrad | 0:cdf462088d13 | 475 | { |
| markrad | 0:cdf462088d13 | 476 | size_t my_out_len = mbedtls_x509_csr_info( my_out, sizeof( my_out ), "", &csr ); |
| markrad | 0:cdf462088d13 | 477 | TEST_ASSERT( my_out_len == strlen( ref_out ) ); |
| markrad | 0:cdf462088d13 | 478 | TEST_ASSERT( strcmp( my_out, ref_out ) == 0 ); |
| markrad | 0:cdf462088d13 | 479 | } |
| markrad | 0:cdf462088d13 | 480 | |
| markrad | 0:cdf462088d13 | 481 | exit: |
| markrad | 0:cdf462088d13 | 482 | mbedtls_x509_csr_free( &csr ); |
| markrad | 0:cdf462088d13 | 483 | mbedtls_free( csr_der ); |
| markrad | 0:cdf462088d13 | 484 | } |
| markrad | 0:cdf462088d13 | 485 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 486 | |
| markrad | 0:cdf462088d13 | 487 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 488 | void mbedtls_x509_crt_parse_path( char *crt_path, int ret, int nb_crt ) |
| markrad | 0:cdf462088d13 | 489 | { |
| markrad | 0:cdf462088d13 | 490 | mbedtls_x509_crt chain, *cur; |
| markrad | 0:cdf462088d13 | 491 | int i; |
| markrad | 0:cdf462088d13 | 492 | |
| markrad | 0:cdf462088d13 | 493 | mbedtls_x509_crt_init( &chain ); |
| markrad | 0:cdf462088d13 | 494 | |
| markrad | 0:cdf462088d13 | 495 | TEST_ASSERT( mbedtls_x509_crt_parse_path( &chain, crt_path ) == ret ); |
| markrad | 0:cdf462088d13 | 496 | |
| markrad | 0:cdf462088d13 | 497 | /* Check how many certs we got */ |
| markrad | 0:cdf462088d13 | 498 | for( i = 0, cur = &chain; cur != NULL; cur = cur->next ) |
| markrad | 0:cdf462088d13 | 499 | if( cur->raw.p != NULL ) |
| markrad | 0:cdf462088d13 | 500 | i++; |
| markrad | 0:cdf462088d13 | 501 | |
| markrad | 0:cdf462088d13 | 502 | TEST_ASSERT( i == nb_crt ); |
| markrad | 0:cdf462088d13 | 503 | |
| markrad | 0:cdf462088d13 | 504 | exit: |
| markrad | 0:cdf462088d13 | 505 | mbedtls_x509_crt_free( &chain ); |
| markrad | 0:cdf462088d13 | 506 | } |
| markrad | 0:cdf462088d13 | 507 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 508 | |
| markrad | 0:cdf462088d13 | 509 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| Jasper Wallace |
2:bbdeda018a3c | 510 | void mbedtls_x509_crt_verify_max( char *ca_file, char *chain_dir, int nb_int, |
| Jasper Wallace |
2:bbdeda018a3c | 511 | int ret_chk, int flags_chk ) |
| Jasper Wallace |
2:bbdeda018a3c | 512 | { |
| Jasper Wallace |
2:bbdeda018a3c | 513 | char file_buf[128]; |
| Jasper Wallace |
2:bbdeda018a3c | 514 | int ret; |
| Jasper Wallace |
2:bbdeda018a3c | 515 | uint32_t flags; |
| Jasper Wallace |
2:bbdeda018a3c | 516 | mbedtls_x509_crt trusted, chain; |
| Jasper Wallace |
2:bbdeda018a3c | 517 | |
| Jasper Wallace |
2:bbdeda018a3c | 518 | /* |
| Jasper Wallace |
2:bbdeda018a3c | 519 | * We expect chain_dir to contain certificates 00.crt, 01.crt, etc. |
| Jasper Wallace |
2:bbdeda018a3c | 520 | * with NN.crt signed by NN-1.crt |
| Jasper Wallace |
2:bbdeda018a3c | 521 | */ |
| Jasper Wallace |
2:bbdeda018a3c | 522 | |
| Jasper Wallace |
2:bbdeda018a3c | 523 | mbedtls_x509_crt_init( &trusted ); |
| Jasper Wallace |
2:bbdeda018a3c | 524 | mbedtls_x509_crt_init( &chain ); |
| Jasper Wallace |
2:bbdeda018a3c | 525 | |
| Jasper Wallace |
2:bbdeda018a3c | 526 | /* Load trusted root */ |
| Jasper Wallace |
2:bbdeda018a3c | 527 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, ca_file ) == 0 ); |
| Jasper Wallace |
2:bbdeda018a3c | 528 | |
| Jasper Wallace |
2:bbdeda018a3c | 529 | /* Load a chain with nb_int intermediates (from 01 to nb_int), |
| Jasper Wallace |
2:bbdeda018a3c | 530 | * plus one "end-entity" cert (nb_int + 1) */ |
| Jasper Wallace |
2:bbdeda018a3c | 531 | ret = mbedtls_snprintf( file_buf, sizeof file_buf, "%s/c%02d.pem", chain_dir, |
| Jasper Wallace |
2:bbdeda018a3c | 532 | nb_int + 1 ); |
| Jasper Wallace |
2:bbdeda018a3c | 533 | TEST_ASSERT( ret > 0 && (size_t) ret < sizeof file_buf ); |
| Jasper Wallace |
2:bbdeda018a3c | 534 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, file_buf ) == 0 ); |
| Jasper Wallace |
2:bbdeda018a3c | 535 | |
| Jasper Wallace |
2:bbdeda018a3c | 536 | /* Try to verify that chain */ |
| Jasper Wallace |
2:bbdeda018a3c | 537 | ret = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, |
| Jasper Wallace |
2:bbdeda018a3c | 538 | NULL, NULL ); |
| Jasper Wallace |
2:bbdeda018a3c | 539 | TEST_ASSERT( ret == ret_chk ); |
| Jasper Wallace |
2:bbdeda018a3c | 540 | TEST_ASSERT( flags == (uint32_t) flags_chk ); |
| Jasper Wallace |
2:bbdeda018a3c | 541 | |
| Jasper Wallace |
2:bbdeda018a3c | 542 | exit: |
| Jasper Wallace |
2:bbdeda018a3c | 543 | mbedtls_x509_crt_free( &chain ); |
| Jasper Wallace |
2:bbdeda018a3c | 544 | mbedtls_x509_crt_free( &trusted ); |
| Jasper Wallace |
2:bbdeda018a3c | 545 | } |
| Jasper Wallace |
2:bbdeda018a3c | 546 | /* END_CASE */ |
| Jasper Wallace |
2:bbdeda018a3c | 547 | |
| Jasper Wallace |
2:bbdeda018a3c | 548 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */ |
| markrad | 0:cdf462088d13 | 549 | void mbedtls_x509_crt_verify_chain( char *chain_paths, char *trusted_ca, int flags_result ) |
| markrad | 0:cdf462088d13 | 550 | { |
| markrad | 0:cdf462088d13 | 551 | char* act; |
| markrad | 0:cdf462088d13 | 552 | uint32_t flags; |
| markrad | 0:cdf462088d13 | 553 | int result, res; |
| markrad | 0:cdf462088d13 | 554 | mbedtls_x509_crt trusted, chain; |
| markrad | 0:cdf462088d13 | 555 | |
| markrad | 0:cdf462088d13 | 556 | result= flags_result?MBEDTLS_ERR_X509_CERT_VERIFY_FAILED:0; |
| markrad | 0:cdf462088d13 | 557 | |
| markrad | 0:cdf462088d13 | 558 | mbedtls_x509_crt_init( &chain ); |
| markrad | 0:cdf462088d13 | 559 | mbedtls_x509_crt_init( &trusted ); |
| markrad | 0:cdf462088d13 | 560 | |
| markrad | 0:cdf462088d13 | 561 | while( ( act = mystrsep( &chain_paths, " " ) ) != NULL ) |
| markrad | 0:cdf462088d13 | 562 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &chain, act ) == 0 ); |
| markrad | 0:cdf462088d13 | 563 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &trusted, trusted_ca ) == 0 ); |
| markrad | 0:cdf462088d13 | 564 | |
| markrad | 0:cdf462088d13 | 565 | res = mbedtls_x509_crt_verify( &chain, &trusted, NULL, NULL, &flags, NULL, NULL ); |
| markrad | 0:cdf462088d13 | 566 | |
| markrad | 0:cdf462088d13 | 567 | TEST_ASSERT( res == ( result ) ); |
| markrad | 0:cdf462088d13 | 568 | TEST_ASSERT( flags == (uint32_t)( flags_result ) ); |
| markrad | 0:cdf462088d13 | 569 | |
| markrad | 0:cdf462088d13 | 570 | exit: |
| markrad | 0:cdf462088d13 | 571 | mbedtls_x509_crt_free( &trusted ); |
| markrad | 0:cdf462088d13 | 572 | mbedtls_x509_crt_free( &chain ); |
| markrad | 0:cdf462088d13 | 573 | } |
| markrad | 0:cdf462088d13 | 574 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 575 | |
| markrad | 0:cdf462088d13 | 576 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| markrad | 0:cdf462088d13 | 577 | void x509_oid_desc( char *oid_str, char *ref_desc ) |
| markrad | 0:cdf462088d13 | 578 | { |
| markrad | 0:cdf462088d13 | 579 | mbedtls_x509_buf oid; |
| markrad | 0:cdf462088d13 | 580 | const char *desc = NULL; |
| markrad | 0:cdf462088d13 | 581 | unsigned char buf[20]; |
| markrad | 0:cdf462088d13 | 582 | int ret; |
| markrad | 0:cdf462088d13 | 583 | |
| markrad | 0:cdf462088d13 | 584 | memset( buf, 0, sizeof buf ); |
| markrad | 0:cdf462088d13 | 585 | |
| markrad | 0:cdf462088d13 | 586 | oid.tag = MBEDTLS_ASN1_OID; |
| markrad | 0:cdf462088d13 | 587 | oid.len = unhexify( buf, oid_str ); |
| markrad | 0:cdf462088d13 | 588 | oid.p = buf; |
| markrad | 0:cdf462088d13 | 589 | |
| markrad | 0:cdf462088d13 | 590 | ret = mbedtls_oid_get_extended_key_usage( &oid, &desc ); |
| markrad | 0:cdf462088d13 | 591 | |
| markrad | 0:cdf462088d13 | 592 | if( strcmp( ref_desc, "notfound" ) == 0 ) |
| markrad | 0:cdf462088d13 | 593 | { |
| markrad | 0:cdf462088d13 | 594 | TEST_ASSERT( ret != 0 ); |
| markrad | 0:cdf462088d13 | 595 | TEST_ASSERT( desc == NULL ); |
| markrad | 0:cdf462088d13 | 596 | } |
| markrad | 0:cdf462088d13 | 597 | else |
| markrad | 0:cdf462088d13 | 598 | { |
| markrad | 0:cdf462088d13 | 599 | TEST_ASSERT( ret == 0 ); |
| markrad | 0:cdf462088d13 | 600 | TEST_ASSERT( desc != NULL ); |
| markrad | 0:cdf462088d13 | 601 | TEST_ASSERT( strcmp( desc, ref_desc ) == 0 ); |
| markrad | 0:cdf462088d13 | 602 | } |
| markrad | 0:cdf462088d13 | 603 | } |
| markrad | 0:cdf462088d13 | 604 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 605 | |
| markrad | 0:cdf462088d13 | 606 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| markrad | 0:cdf462088d13 | 607 | void x509_oid_numstr( char *oid_str, char *numstr, int blen, int ret ) |
| markrad | 0:cdf462088d13 | 608 | { |
| markrad | 0:cdf462088d13 | 609 | mbedtls_x509_buf oid; |
| markrad | 0:cdf462088d13 | 610 | unsigned char oid_buf[20]; |
| markrad | 0:cdf462088d13 | 611 | char num_buf[100]; |
| markrad | 0:cdf462088d13 | 612 | |
| markrad | 0:cdf462088d13 | 613 | memset( oid_buf, 0x00, sizeof oid_buf ); |
| markrad | 0:cdf462088d13 | 614 | memset( num_buf, 0x2a, sizeof num_buf ); |
| markrad | 0:cdf462088d13 | 615 | |
| markrad | 0:cdf462088d13 | 616 | oid.tag = MBEDTLS_ASN1_OID; |
| markrad | 0:cdf462088d13 | 617 | oid.len = unhexify( oid_buf, oid_str ); |
| markrad | 0:cdf462088d13 | 618 | oid.p = oid_buf; |
| markrad | 0:cdf462088d13 | 619 | |
| markrad | 0:cdf462088d13 | 620 | TEST_ASSERT( (size_t) blen <= sizeof num_buf ); |
| markrad | 0:cdf462088d13 | 621 | |
| markrad | 0:cdf462088d13 | 622 | TEST_ASSERT( mbedtls_oid_get_numeric_string( num_buf, blen, &oid ) == ret ); |
| markrad | 0:cdf462088d13 | 623 | |
| markrad | 0:cdf462088d13 | 624 | if( ret >= 0 ) |
| markrad | 0:cdf462088d13 | 625 | { |
| markrad | 0:cdf462088d13 | 626 | TEST_ASSERT( num_buf[ret] == 0 ); |
| markrad | 0:cdf462088d13 | 627 | TEST_ASSERT( strcmp( num_buf, numstr ) == 0 ); |
| markrad | 0:cdf462088d13 | 628 | } |
| markrad | 0:cdf462088d13 | 629 | } |
| markrad | 0:cdf462088d13 | 630 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 631 | |
| markrad | 0:cdf462088d13 | 632 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_KEY_USAGE */ |
| markrad | 0:cdf462088d13 | 633 | void x509_check_key_usage( char *crt_file, int usage, int ret ) |
| markrad | 0:cdf462088d13 | 634 | { |
| markrad | 0:cdf462088d13 | 635 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 636 | |
| markrad | 0:cdf462088d13 | 637 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 638 | |
| markrad | 0:cdf462088d13 | 639 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 640 | |
| markrad | 0:cdf462088d13 | 641 | TEST_ASSERT( mbedtls_x509_crt_check_key_usage( &crt, usage ) == ret ); |
| markrad | 0:cdf462088d13 | 642 | |
| markrad | 0:cdf462088d13 | 643 | exit: |
| markrad | 0:cdf462088d13 | 644 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 645 | } |
| markrad | 0:cdf462088d13 | 646 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 647 | |
| markrad | 0:cdf462088d13 | 648 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
| markrad | 0:cdf462088d13 | 649 | void x509_check_extended_key_usage( char *crt_file, char *usage_hex, int ret ) |
| markrad | 0:cdf462088d13 | 650 | { |
| markrad | 0:cdf462088d13 | 651 | mbedtls_x509_crt crt; |
| markrad | 0:cdf462088d13 | 652 | char oid[50]; |
| markrad | 0:cdf462088d13 | 653 | size_t len; |
| markrad | 0:cdf462088d13 | 654 | |
| markrad | 0:cdf462088d13 | 655 | mbedtls_x509_crt_init( &crt ); |
| markrad | 0:cdf462088d13 | 656 | |
| markrad | 0:cdf462088d13 | 657 | len = unhexify( (unsigned char *) oid, usage_hex ); |
| markrad | 0:cdf462088d13 | 658 | |
| markrad | 0:cdf462088d13 | 659 | TEST_ASSERT( mbedtls_x509_crt_parse_file( &crt, crt_file ) == 0 ); |
| markrad | 0:cdf462088d13 | 660 | |
| markrad | 0:cdf462088d13 | 661 | TEST_ASSERT( mbedtls_x509_crt_check_extended_key_usage( &crt, oid, len ) == ret ); |
| markrad | 0:cdf462088d13 | 662 | |
| markrad | 0:cdf462088d13 | 663 | exit: |
| markrad | 0:cdf462088d13 | 664 | mbedtls_x509_crt_free( &crt ); |
| markrad | 0:cdf462088d13 | 665 | } |
| markrad | 0:cdf462088d13 | 666 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 667 | |
| markrad | 0:cdf462088d13 | 668 | /* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */ |
| markrad | 0:cdf462088d13 | 669 | void x509_get_time( int tag, char *time_str, int ret, |
| markrad | 0:cdf462088d13 | 670 | int year, int mon, int day, |
| markrad | 0:cdf462088d13 | 671 | int hour, int min, int sec ) |
| markrad | 0:cdf462088d13 | 672 | { |
| markrad | 0:cdf462088d13 | 673 | mbedtls_x509_time time; |
| Jasper Wallace |
1:9ebc941037d5 | 674 | unsigned char buf[21]; |
| markrad | 0:cdf462088d13 | 675 | unsigned char* start = buf; |
| markrad | 0:cdf462088d13 | 676 | unsigned char* end = buf; |
| markrad | 0:cdf462088d13 | 677 | |
| markrad | 0:cdf462088d13 | 678 | memset( &time, 0x00, sizeof( time ) ); |
| markrad | 0:cdf462088d13 | 679 | *end = (unsigned char)tag; end++; |
| Jasper Wallace |
1:9ebc941037d5 | 680 | *end = strlen( time_str ); |
| Jasper Wallace |
1:9ebc941037d5 | 681 | TEST_ASSERT( *end < 20 ); |
| markrad | 0:cdf462088d13 | 682 | end++; |
| markrad | 0:cdf462088d13 | 683 | memcpy( end, time_str, (size_t)*(end - 1) ); |
| markrad | 0:cdf462088d13 | 684 | end += *(end - 1); |
| markrad | 0:cdf462088d13 | 685 | |
| markrad | 0:cdf462088d13 | 686 | TEST_ASSERT( mbedtls_x509_get_time( &start, end, &time ) == ret ); |
| markrad | 0:cdf462088d13 | 687 | if( ret == 0 ) |
| markrad | 0:cdf462088d13 | 688 | { |
| markrad | 0:cdf462088d13 | 689 | TEST_ASSERT( year == time.year ); |
| markrad | 0:cdf462088d13 | 690 | TEST_ASSERT( mon == time.mon ); |
| markrad | 0:cdf462088d13 | 691 | TEST_ASSERT( day == time.day ); |
| markrad | 0:cdf462088d13 | 692 | TEST_ASSERT( hour == time.hour ); |
| markrad | 0:cdf462088d13 | 693 | TEST_ASSERT( min == time.min ); |
| markrad | 0:cdf462088d13 | 694 | TEST_ASSERT( sec == time.sec ); |
| markrad | 0:cdf462088d13 | 695 | } |
| markrad | 0:cdf462088d13 | 696 | } |
| markrad | 0:cdf462088d13 | 697 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 698 | |
| markrad | 0:cdf462088d13 | 699 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */ |
| markrad | 0:cdf462088d13 | 700 | void x509_parse_rsassa_pss_params( char *hex_params, int params_tag, |
| markrad | 0:cdf462088d13 | 701 | int ref_msg_md, int ref_mgf_md, |
| markrad | 0:cdf462088d13 | 702 | int ref_salt_len, int ref_ret ) |
| markrad | 0:cdf462088d13 | 703 | { |
| markrad | 0:cdf462088d13 | 704 | int my_ret; |
| markrad | 0:cdf462088d13 | 705 | mbedtls_x509_buf params; |
| markrad | 0:cdf462088d13 | 706 | mbedtls_md_type_t my_msg_md, my_mgf_md; |
| markrad | 0:cdf462088d13 | 707 | int my_salt_len; |
| markrad | 0:cdf462088d13 | 708 | |
| markrad | 0:cdf462088d13 | 709 | params.p = unhexify_alloc( hex_params, ¶ms.len ); |
| markrad | 0:cdf462088d13 | 710 | params.tag = params_tag; |
| markrad | 0:cdf462088d13 | 711 | |
| markrad | 0:cdf462088d13 | 712 | my_ret = mbedtls_x509_get_rsassa_pss_params( ¶ms, &my_msg_md, &my_mgf_md, |
| markrad | 0:cdf462088d13 | 713 | &my_salt_len ); |
| markrad | 0:cdf462088d13 | 714 | |
| markrad | 0:cdf462088d13 | 715 | TEST_ASSERT( my_ret == ref_ret ); |
| markrad | 0:cdf462088d13 | 716 | |
| markrad | 0:cdf462088d13 | 717 | if( ref_ret == 0 ) |
| markrad | 0:cdf462088d13 | 718 | { |
| markrad | 0:cdf462088d13 | 719 | TEST_ASSERT( my_msg_md == (mbedtls_md_type_t) ref_msg_md ); |
| markrad | 0:cdf462088d13 | 720 | TEST_ASSERT( my_mgf_md == (mbedtls_md_type_t) ref_mgf_md ); |
| markrad | 0:cdf462088d13 | 721 | TEST_ASSERT( my_salt_len == ref_salt_len ); |
| markrad | 0:cdf462088d13 | 722 | } |
| markrad | 0:cdf462088d13 | 723 | |
| markrad | 0:cdf462088d13 | 724 | exit: |
| markrad | 0:cdf462088d13 | 725 | mbedtls_free( params.p ); |
| markrad | 0:cdf462088d13 | 726 | } |
| markrad | 0:cdf462088d13 | 727 | /* END_CASE */ |
| markrad | 0:cdf462088d13 | 728 | |
| markrad | 0:cdf462088d13 | 729 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_SELF_TEST */ |
| markrad | 0:cdf462088d13 | 730 | void x509_selftest() |
| markrad | 0:cdf462088d13 | 731 | { |
| markrad | 0:cdf462088d13 | 732 | TEST_ASSERT( mbedtls_x509_self_test( 1 ) == 0 ); |
| markrad | 0:cdf462088d13 | 733 | } |
| markrad | 0:cdf462088d13 | 734 | /* END_CASE */ |
