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" |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 6 | #include "mbedtls/legacy_or_psa.h" |
Przemek Stekiel | 41b742f | 2022-08-04 12:13:53 +0200 | [diff] [blame] | 7 | |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 8 | /* END_HEADER */ |
| 9 | |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 10 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 11 | void mbedtls_pem_write_buffer(char *start, char *end, data_t *buf, |
| 12 | char *result_str) |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 13 | { |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 14 | unsigned char *check_buf = NULL; |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 15 | int ret; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 16 | size_t olen = 0, olen2 = 0; |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 17 | |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 18 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 19 | ret = mbedtls_pem_write_buffer(start, end, buf->x, buf->len, NULL, 0, &olen); |
| 20 | TEST_ASSERT(ret == MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 21 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 22 | check_buf = (unsigned char *) mbedtls_calloc(1, olen); |
| 23 | TEST_ASSERT(check_buf != NULL); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 24 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | 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] | 26 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | TEST_ASSERT(olen2 <= olen); |
| 28 | TEST_ASSERT(olen > strlen((char *) result_str)); |
| 29 | TEST_ASSERT(ret == 0); |
| 30 | TEST_ASSERT(strncmp((char *) check_buf, (char *) result_str, olen) == 0); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 31 | |
| 32 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | mbedtls_free(check_buf); |
Paul Bakker | 1630058 | 2014-04-11 13:28:43 +0200 | [diff] [blame] | 34 | } |
| 35 | /* END_CASE */ |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 36 | |
Przemek Stekiel | bc0509a | 2022-08-10 15:10:15 +0200 | [diff] [blame] | 37 | /* BEGIN_CASE depends_on:MBEDTLS_PEM_PARSE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | void mbedtls_pem_read_buffer(char *header, char *footer, char *data, |
| 39 | char *pwd, int res, data_t *out) |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 40 | { |
| 41 | mbedtls_pem_context ctx; |
Andres AG | a3b9adb | 2017-03-01 11:53:29 +0000 | [diff] [blame] | 42 | int ret; |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 43 | size_t use_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | size_t pwd_len = strlen(pwd); |
Glenn Strauss | 72bd4e4 | 2022-02-04 10:32:17 -0500 | [diff] [blame] | 45 | const unsigned char *buf; |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | 1c2008f | 2023-03-16 10:20:29 +0100 | [diff] [blame^] | 47 | MD_PSA_INIT(); |
| 48 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | mbedtls_pem_init(&ctx); |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 50 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | ret = mbedtls_pem_read_buffer(&ctx, header, footer, (unsigned char *) data, |
| 52 | (unsigned char *) pwd, pwd_len, &use_len); |
| 53 | TEST_ASSERT(ret == res); |
| 54 | if (ret != 0) { |
Glenn Strauss | 72bd4e4 | 2022-02-04 10:32:17 -0500 | [diff] [blame] | 55 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | } |
Glenn Strauss | 72bd4e4 | 2022-02-04 10:32:17 -0500 | [diff] [blame] | 57 | |
Glenn Strauss | 72bd4e4 | 2022-02-04 10:32:17 -0500 | [diff] [blame] | 58 | use_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | buf = mbedtls_pem_get_buffer(&ctx, &use_len); |
| 60 | TEST_EQUAL(use_len, out->len); |
| 61 | TEST_ASSERT(memcmp(out->x, buf, out->len) == 0); |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 62 | |
| 63 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | mbedtls_pem_free(&ctx); |
Manuel Pégourié-Gonnard | 1c2008f | 2023-03-16 10:20:29 +0100 | [diff] [blame^] | 65 | MD_PSA_DONE(); |
Andres AG | 9c94b69 | 2016-10-24 14:31:54 +0100 | [diff] [blame] | 66 | } |
| 67 | /* END_CASE */ |