1a8e1175bSopenharmony_ci/* BEGIN_HEADER */ 2a8e1175bSopenharmony_ci#include "mbedtls/base64.h" 3a8e1175bSopenharmony_ci#include "mbedtls/pem.h" 4a8e1175bSopenharmony_ci#include "mbedtls/des.h" 5a8e1175bSopenharmony_ci#include "mbedtls/aes.h" 6a8e1175bSopenharmony_ci/* END_HEADER */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ 9a8e1175bSopenharmony_civoid mbedtls_pem_write_buffer(char *start, char *end, data_t *buf, 10a8e1175bSopenharmony_ci char *result_str) 11a8e1175bSopenharmony_ci{ 12a8e1175bSopenharmony_ci unsigned char *check_buf = NULL; 13a8e1175bSopenharmony_ci int ret; 14a8e1175bSopenharmony_ci size_t olen = 0, olen2 = 0; 15a8e1175bSopenharmony_ci 16a8e1175bSopenharmony_ci 17a8e1175bSopenharmony_ci ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, NULL, 0, &olen); 18a8e1175bSopenharmony_ci TEST_ASSERT(ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL); 19a8e1175bSopenharmony_ci 20a8e1175bSopenharmony_ci check_buf = (unsigned char *) mbedtls_calloc(1, olen); 21a8e1175bSopenharmony_ci TEST_ASSERT(check_buf != NULL); 22a8e1175bSopenharmony_ci 23a8e1175bSopenharmony_ci ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, check_buf, olen, &olen2); 24a8e1175bSopenharmony_ci 25a8e1175bSopenharmony_ci TEST_ASSERT(olen2 <= olen); 26a8e1175bSopenharmony_ci TEST_ASSERT(olen > strlen((char *) result_str)); 27a8e1175bSopenharmony_ci TEST_ASSERT(ret == 0); 28a8e1175bSopenharmony_ci TEST_ASSERT(strncmp((char *) check_buf, (char *) result_str, olen) == 0); 29a8e1175bSopenharmony_ci 30a8e1175bSopenharmony_ciexit: 31a8e1175bSopenharmony_ci mbedtls_free(check_buf); 32a8e1175bSopenharmony_ci} 33a8e1175bSopenharmony_ci/* END_CASE */ 34a8e1175bSopenharmony_ci 35a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ 36a8e1175bSopenharmony_civoid mbedtls_pem_write_buffer_lengths() 37a8e1175bSopenharmony_ci{ 38a8e1175bSopenharmony_ci unsigned char data[256] = { 0 }; 39a8e1175bSopenharmony_ci unsigned char buf[1024]; 40a8e1175bSopenharmony_ci size_t olen_needed, olen; 41a8e1175bSopenharmony_ci int ret; 42a8e1175bSopenharmony_ci for (size_t l = 0; l <= sizeof(data); l++) { 43a8e1175bSopenharmony_ci ret = mbedtls_pem_write_buffer("\n", "\n", data, l, NULL, 0, &olen_needed); 44a8e1175bSopenharmony_ci TEST_EQUAL(ret, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL); 45a8e1175bSopenharmony_ci 46a8e1175bSopenharmony_ci /* Test that a bigger buffer still only requires `olen_needed` */ 47a8e1175bSopenharmony_ci ret = mbedtls_pem_write_buffer("\n", "\n", data, l, buf, sizeof(buf), &olen); 48a8e1175bSopenharmony_ci TEST_EQUAL(ret, 0); 49a8e1175bSopenharmony_ci TEST_EQUAL(olen_needed, olen); 50a8e1175bSopenharmony_ci 51a8e1175bSopenharmony_ci /* Test that a buffer of exactly `olen_needed` works */ 52a8e1175bSopenharmony_ci memset(buf, 1, sizeof(buf)); 53a8e1175bSopenharmony_ci ret = mbedtls_pem_write_buffer("\n", "\n", data, l, buf, olen_needed, &olen); 54a8e1175bSopenharmony_ci TEST_EQUAL(ret, 0); 55a8e1175bSopenharmony_ci TEST_EQUAL(olen_needed, olen); 56a8e1175bSopenharmony_ci /* Test the function didn't overflow the given buffer */ 57a8e1175bSopenharmony_ci for (size_t i = olen_needed; i < sizeof(buf); i++) { 58a8e1175bSopenharmony_ci TEST_EQUAL(buf[i], 1); 59a8e1175bSopenharmony_ci } 60a8e1175bSopenharmony_ci } 61a8e1175bSopenharmony_ci} 62a8e1175bSopenharmony_ci/* END_CASE */ 63a8e1175bSopenharmony_ci 64a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C */ 65a8e1175bSopenharmony_civoid mbedtls_pem_read_buffer(char *header, char *footer, char *data, 66a8e1175bSopenharmony_ci char *pwd, int res, data_t *out) 67a8e1175bSopenharmony_ci{ 68a8e1175bSopenharmony_ci mbedtls_pem_context ctx; 69a8e1175bSopenharmony_ci int ret; 70a8e1175bSopenharmony_ci size_t use_len = 0; 71a8e1175bSopenharmony_ci size_t pwd_len = strlen(pwd); 72a8e1175bSopenharmony_ci const unsigned char *buf; 73a8e1175bSopenharmony_ci 74a8e1175bSopenharmony_ci MD_PSA_INIT(); 75a8e1175bSopenharmony_ci 76a8e1175bSopenharmony_ci mbedtls_pem_init(&ctx); 77a8e1175bSopenharmony_ci 78a8e1175bSopenharmony_ci ret = mbedtls_pem_read_buffer(&ctx, header, footer, (unsigned char *) data, 79a8e1175bSopenharmony_ci (unsigned char *) pwd, pwd_len, &use_len); 80a8e1175bSopenharmony_ci TEST_ASSERT(ret == res); 81a8e1175bSopenharmony_ci if (ret != 0) { 82a8e1175bSopenharmony_ci goto exit; 83a8e1175bSopenharmony_ci } 84a8e1175bSopenharmony_ci 85a8e1175bSopenharmony_ci use_len = 0; 86a8e1175bSopenharmony_ci buf = mbedtls_pem_get_buffer(&ctx, &use_len); 87a8e1175bSopenharmony_ci TEST_EQUAL(use_len, out->len); 88a8e1175bSopenharmony_ci TEST_ASSERT(memcmp(out->x, buf, out->len) == 0); 89a8e1175bSopenharmony_ci 90a8e1175bSopenharmony_ciexit: 91a8e1175bSopenharmony_ci mbedtls_pem_free(&ctx); 92a8e1175bSopenharmony_ci MD_PSA_DONE(); 93a8e1175bSopenharmony_ci} 94a8e1175bSopenharmony_ci/* END_CASE */ 95