blob: 918eae5e33c4cc4d85ae1fdb821be4e447f27f0a [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009void mbedtls_pem_write_buffer(char *start, char *end, data_t *buf,
10 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
Gilles Peskine449bd832023-01-11 14:50:10 +010017 ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, NULL, 0, &olen);
18 TEST_ASSERT(ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL);
Paul Bakker16300582014-04-11 13:28:43 +020019
Gilles Peskine449bd832023-01-11 14:50:10 +010020 check_buf = (unsigned char *) mbedtls_calloc(1, olen);
21 TEST_ASSERT(check_buf != NULL);
Paul Bakker16300582014-04-11 13:28:43 +020022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, check_buf, olen, &olen2);
Paul Bakker16300582014-04-11 13:28:43 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 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:
Gilles Peskine449bd832023-01-11 14:50:10 +010031 mbedtls_free(check_buf);
Paul Bakker16300582014-04-11 13:28:43 +020032}
33/* END_CASE */
Andres AG9c94b692016-10-24 14:31:54 +010034
Przemek Stekielbc0509a2022-08-10 15:10:15 +020035/* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010036void mbedtls_pem_read_buffer(char *header, char *footer, char *data,
37 char *pwd, int res, data_t *out)
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;
Gilles Peskine449bd832023-01-11 14:50:10 +010042 size_t pwd_len = strlen(pwd);
Glenn Strauss72bd4e42022-02-04 10:32:17 -050043 const unsigned char *buf;
Andres AG9c94b692016-10-24 14:31:54 +010044
Manuel Pégourié-Gonnard1c2008f2023-03-16 10:20:29 +010045 MD_PSA_INIT();
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_pem_init(&ctx);
Andres AG9c94b692016-10-24 14:31:54 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 ret = mbedtls_pem_read_buffer(&ctx, header, footer, (unsigned char *) data,
50 (unsigned char *) pwd, pwd_len, &use_len);
51 TEST_ASSERT(ret == res);
52 if (ret != 0) {
Glenn Strauss72bd4e42022-02-04 10:32:17 -050053 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010054 }
Glenn Strauss72bd4e42022-02-04 10:32:17 -050055
Glenn Strauss72bd4e42022-02-04 10:32:17 -050056 use_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 buf = mbedtls_pem_get_buffer(&ctx, &use_len);
58 TEST_EQUAL(use_len, out->len);
59 TEST_ASSERT(memcmp(out->x, buf, out->len) == 0);
Andres AG9c94b692016-10-24 14:31:54 +010060
61exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_pem_free(&ctx);
Manuel Pégourié-Gonnard1c2008f2023-03-16 10:20:29 +010063 MD_PSA_DONE();
Andres AG9c94b692016-10-24 14:31:54 +010064}
65/* END_CASE */