Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/base64.h" |
| 3 | #include "mbedtls/pem.h" |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 4 | #include "mbedtls/des.h" |
| 5 | #include "mbedtls/aes.h" |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 6 | /* END_HEADER */ |
| 7 | |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 8 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 9 | void mbedtls_pem_write_buffer( char * start, char * end, data_t * buf, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 10 | char * result_str ) |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 11 | { |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 12 | unsigned char *check_buf = NULL; |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 13 | int ret; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 14 | size_t olen = 0, olen2 = 0; |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 15 | |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 16 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 17 | ret = mbedtls_pem_write_buffer( start, end, buf->x, buf->len, NULL, 0, &olen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | TEST_ASSERT( ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 20 | check_buf = (unsigned char *) mbedtls_calloc( 1, olen ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 21 | TEST_ASSERT( check_buf != NULL ); |
| 22 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 23 | ret = mbedtls_pem_write_buffer( start, end, buf->x, buf->len, check_buf, olen, &olen2 ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 24 | |
| 25 | TEST_ASSERT( olen2 <= olen ); |
| 26 | TEST_ASSERT( olen > strlen( (char*) result_str ) ); |
| 27 | TEST_ASSERT( ret == 0 ); |
| 28 | TEST_ASSERT( strncmp( (char *) check_buf, (char *) result_str, olen ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 29 | |
| 30 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | mbedtls_free( check_buf ); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 32 | } |
| 33 | /* END_CASE */ |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 34 | |
| 35 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_AES_C:MBEDTLS_DES_C:MBEDTLS_MD5_C:MBEDTLS_CIPHER_MODE_CBC */ |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 36 | void mbedtls_pem_read_buffer( char *header, char *footer, char *data, |
| 37 | char *pwd, int res ) |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 38 | { |
| 39 | mbedtls_pem_context ctx; |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 40 | int ret; |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 41 | size_t use_len = 0; |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 42 | size_t pwd_len = strlen( pwd ); |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 43 | |
| 44 | mbedtls_pem_init( &ctx ); |
| 45 | |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 46 | ret = mbedtls_pem_read_buffer( &ctx, header, footer, (unsigned char *)data, |
| 47 | (unsigned char *)pwd, pwd_len, &use_len ); |
| 48 | TEST_ASSERT( ret == res ); |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 49 | |
| 50 | exit: |
| 51 | mbedtls_pem_free( &ctx ); |
| 52 | } |
| 53 | /* END_CASE */ |