2014-04-11 13:28:43 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/base64.h"
|
|
|
|
#include "mbedtls/pem.h"
|
2017-03-01 12:53:29 +01:00
|
|
|
#include "mbedtls/des.h"
|
|
|
|
#include "mbedtls/aes.h"
|
2014-04-11 13:28:43 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
2016-10-24 15:31:54 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pem_write_buffer(char *start, char *end, data_t *buf,
|
|
|
|
char *result_str)
|
2014-04-11 13:28:43 +02:00
|
|
|
{
|
2014-07-10 15:26:12 +02:00
|
|
|
unsigned char *check_buf = NULL;
|
2014-04-11 13:28:43 +02:00
|
|
|
int ret;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t olen = 0, olen2 = 0;
|
2014-04-11 13:28:43 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, NULL, 0, &olen);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
|
2014-04-11 13:28:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
check_buf = (unsigned char *) mbedtls_calloc(1, olen);
|
|
|
|
TEST_ASSERT(check_buf != NULL);
|
2014-04-11 13:28:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, check_buf, olen, &olen2);
|
2014-04-11 13:28:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(olen2 <= olen);
|
|
|
|
TEST_ASSERT(olen > strlen((char *) result_str));
|
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(strncmp((char *) check_buf, (char *) result_str, olen) == 0);
|
2014-07-10 15:26:12 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(check_buf);
|
2014-04-11 13:28:43 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2016-10-24 15:31:54 +02:00
|
|
|
|
2023-05-03 14:49:28 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
|
|
|
void mbedtls_pem_write_buffer_lengths()
|
|
|
|
{
|
2023-05-03 17:39:06 +02:00
|
|
|
unsigned char data[256] = { 0 };
|
2023-05-03 14:49:28 +02:00
|
|
|
unsigned char buf[1024];
|
|
|
|
size_t olen_needed, olen;
|
|
|
|
int ret;
|
|
|
|
for (size_t l = 0; l <= sizeof(data); l++) {
|
|
|
|
ret = mbedtls_pem_write_buffer("\n", "\n", data, l, NULL, 0, &olen_needed);
|
2023-05-03 17:39:36 +02:00
|
|
|
TEST_EQUAL(ret, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
|
2023-05-03 14:49:28 +02:00
|
|
|
|
|
|
|
/* Test that a bigger buffer still only requires `olen_needed` */
|
|
|
|
ret = mbedtls_pem_write_buffer("\n", "\n", data, l, buf, sizeof(buf), &olen);
|
2023-05-03 17:39:36 +02:00
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
TEST_EQUAL(olen_needed, olen);
|
2023-05-03 14:49:28 +02:00
|
|
|
|
|
|
|
/* Test that a buffer of exactly `olen_needed` works */
|
|
|
|
memset(buf, 1, sizeof(buf));
|
|
|
|
ret = mbedtls_pem_write_buffer("\n", "\n", data, l, buf, olen_needed, &olen);
|
2023-05-03 17:39:36 +02:00
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
TEST_EQUAL(olen_needed, olen);
|
2023-05-03 14:49:28 +02:00
|
|
|
/* Test the function didn't overflow the given buffer */
|
|
|
|
for (size_t i = olen_needed; i < sizeof(buf); i++) {
|
2023-05-03 17:39:36 +02:00
|
|
|
TEST_EQUAL(buf[i], 1);
|
2023-05-03 14:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-08-10 15:10:15 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pem_read_buffer(char *header, char *footer, char *data,
|
|
|
|
char *pwd, int res, data_t *out)
|
2016-10-24 15:31:54 +02:00
|
|
|
{
|
|
|
|
mbedtls_pem_context ctx;
|
2017-03-01 12:53:29 +01:00
|
|
|
int ret;
|
2016-10-24 15:31:54 +02:00
|
|
|
size_t use_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t pwd_len = strlen(pwd);
|
2022-02-04 16:32:17 +01:00
|
|
|
const unsigned char *buf;
|
2016-10-24 15:31:54 +02:00
|
|
|
|
2023-03-16 10:20:29 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pem_init(&ctx);
|
2016-10-24 15:31:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pem_read_buffer(&ctx, header, footer, (unsigned char *) data,
|
|
|
|
(unsigned char *) pwd, pwd_len, &use_len);
|
|
|
|
TEST_ASSERT(ret == res);
|
|
|
|
if (ret != 0) {
|
2022-02-04 16:32:17 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-04 16:32:17 +01:00
|
|
|
|
|
|
|
use_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
buf = mbedtls_pem_get_buffer(&ctx, &use_len);
|
|
|
|
TEST_EQUAL(use_len, out->len);
|
|
|
|
TEST_ASSERT(memcmp(out->x, buf, out->len) == 0);
|
2016-10-24 15:31:54 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pem_free(&ctx);
|
2023-03-16 10:20:29 +01:00
|
|
|
MD_PSA_DONE();
|
2016-10-24 15:31:54 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|