blob: 947f1fb25d794e7d9bd38fcc31ae5f151c52a8ef [file] [log] [blame]
Paul Bakker16300582014-04-11 13:28:43 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/base64.h"
3#include "mbedtls/pem.h"
Andres AGa3b9adb2017-03-01 11:53:29 +00004#include "mbedtls/des.h"
5#include "mbedtls/aes.h"
Paul Bakker16300582014-04-11 13:28:43 +02006/* END_HEADER */
7
Andres AG9c94b692016-10-24 14:31:54 +01008/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
Azim Khan5fcca462018-06-29 11:05:32 +01009void mbedtls_pem_write_buffer( char * start, char * end, data_t * buf,
Azim Khand30ca132017-06-09 04:32:58 +010010 char * result_str )
Paul Bakker16300582014-04-11 13:28:43 +020011{
Paul Bakkerbd51b262014-07-10 15:26:12 +020012 unsigned char *check_buf = NULL;
Paul Bakker16300582014-04-11 13:28:43 +020013 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +010014 size_t olen = 0, olen2 = 0;
Paul Bakker16300582014-04-11 13:28:43 +020015
Paul Bakker16300582014-04-11 13:28:43 +020016
Azim Khand30ca132017-06-09 04:32:58 +010017 ret = mbedtls_pem_write_buffer( start, end, buf->x, buf->len, NULL, 0, &olen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 TEST_ASSERT( ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
Paul Bakker16300582014-04-11 13:28:43 +020019
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020020 check_buf = (unsigned char *) mbedtls_calloc( 1, olen );
Paul Bakker16300582014-04-11 13:28:43 +020021 TEST_ASSERT( check_buf != NULL );
22
Azim Khand30ca132017-06-09 04:32:58 +010023 ret = mbedtls_pem_write_buffer( start, end, buf->x, buf->len, check_buf, olen, &olen2 );
Paul Bakker16300582014-04-11 13:28:43 +020024
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 Bakkerbd51b262014-07-10 15:26:12 +020029
30exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031 mbedtls_free( check_buf );
Paul Bakker16300582014-04-11 13:28:43 +020032}
33/* END_CASE */
Andres AG9c94b692016-10-24 14:31:54 +010034
35/* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_AES_C:MBEDTLS_DES_C:MBEDTLS_MD5_C:MBEDTLS_CIPHER_MODE_CBC */
Andres AGa3b9adb2017-03-01 11:53:29 +000036void mbedtls_pem_read_buffer( char *header, char *footer, char *data,
37 char *pwd, int res )
Andres AG9c94b692016-10-24 14:31:54 +010038{
39 mbedtls_pem_context ctx;
Andres AGa3b9adb2017-03-01 11:53:29 +000040 int ret;
Andres AG9c94b692016-10-24 14:31:54 +010041 size_t use_len = 0;
Andres AGa3b9adb2017-03-01 11:53:29 +000042 size_t pwd_len = strlen( pwd );
Andres AG9c94b692016-10-24 14:31:54 +010043
44 mbedtls_pem_init( &ctx );
45
Andres AGa3b9adb2017-03-01 11:53:29 +000046 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 AG9c94b692016-10-24 14:31:54 +010049
50exit:
51 mbedtls_pem_free( &ctx );
52}
53/* END_CASE */