2011-02-12 15:30:57 +01:00
|
|
|
/*
|
|
|
|
* Privacy Enhanced Mail (PEM) decoding
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2011-02-12 15:30:57 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
|
2015-02-06 14:57:42 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/base64.h"
|
|
|
|
#include "mbedtls/des.h"
|
|
|
|
#include "mbedtls/aes.h"
|
2023-03-06 23:58:50 +01:00
|
|
|
#include "mbedtls/md.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/cipher.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2024-02-08 17:51:00 +01:00
|
|
|
#include "mbedtls/asn1.h"
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-07-03 13:37:05 +02:00
|
|
|
|
2022-08-04 08:42:06 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#endif
|
|
|
|
|
2023-03-16 10:24:47 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_MD5) && \
|
2022-09-15 11:10:26 +02:00
|
|
|
defined(MBEDTLS_CIPHER_MODE_CBC) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
(defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C))
|
2022-08-11 08:49:21 +02:00
|
|
|
#define PEM_RFC1421
|
2023-03-16 10:24:47 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_MD5 &&
|
2022-09-15 11:10:26 +02:00
|
|
|
MBEDTLS_CIPHER_MODE_CBC &&
|
2022-08-11 08:49:21 +02:00
|
|
|
( MBEDTLS_AES_C || MBEDTLS_DES_C ) */
|
|
|
|
|
2016-12-07 16:05:53 +01:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pem_init(mbedtls_pem_context *ctx)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx, 0, sizeof(mbedtls_pem_context));
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:38:30 +02:00
|
|
|
#if defined(PEM_RFC1421)
|
2011-02-12 15:30:57 +01:00
|
|
|
/*
|
|
|
|
* Read a 16-byte hex string and convert it to binary
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pem_get_iv(const unsigned char *s, unsigned char *iv,
|
|
|
|
size_t iv_len)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t i, j, k;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(iv, 0, iv_len);
|
|
|
|
|
|
|
|
for (i = 0; i < iv_len * 2; i++, s++) {
|
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
j = *s - '0';
|
|
|
|
} else
|
|
|
|
if (*s >= 'A' && *s <= 'F') {
|
|
|
|
j = *s - '7';
|
|
|
|
} else
|
|
|
|
if (*s >= 'a' && *s <= 'f') {
|
|
|
|
j = *s - 'W';
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
k = ((i & 1) != 0) ? j : j << 4;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
iv[i >> 1] = (unsigned char) (iv[i >> 1] | k);
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pem_pbkdf1(unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *iv,
|
|
|
|
const unsigned char *pwd, size_t pwdlen)
|
2022-08-04 10:38:34 +02:00
|
|
|
{
|
2023-03-06 23:58:50 +01:00
|
|
|
mbedtls_md_context_t md5_ctx;
|
|
|
|
const mbedtls_md_info_t *md5_info;
|
2022-08-04 10:38:34 +02:00
|
|
|
unsigned char md5sum[16];
|
|
|
|
size_t use_len;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-03-06 23:58:50 +01:00
|
|
|
mbedtls_md_init(&md5_ctx);
|
|
|
|
|
|
|
|
/* Prepare the context. (setup() errors gracefully on NULL info.) */
|
|
|
|
md5_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
|
|
|
|
if ((ret = mbedtls_md_setup(&md5_ctx, md5_info, 0)) != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-08-04 10:38:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* key[ 0..15] = MD5(pwd || IV)
|
|
|
|
*/
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-04 10:38:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (keylen <= 16) {
|
|
|
|
memcpy(key, md5sum, keylen);
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(key, md5sum, 16);
|
2022-08-04 10:38:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* key[16..23] = MD5(key[ 0..15] || pwd || IV])
|
|
|
|
*/
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_starts(&md5_ctx)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md5_ctx, md5sum, 16)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md5_ctx, pwd, pwdlen)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md5_ctx, iv, 8)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-03-06 23:58:50 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&md5_ctx, md5sum)) != 0) {
|
2022-08-04 10:38:34 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-04 10:38:34 +02:00
|
|
|
|
|
|
|
use_len = 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (keylen < 32) {
|
2022-08-04 10:38:34 +02:00
|
|
|
use_len = keylen - 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-04 10:38:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(key + 16, md5sum, use_len);
|
2022-08-04 10:38:34 +02:00
|
|
|
|
|
|
|
exit:
|
2023-03-06 23:58:50 +01:00
|
|
|
mbedtls_md_free(&md5_ctx);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(md5sum, 16);
|
2022-08-04 10:38:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-08-04 10:38:34 +02:00
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2011-02-12 15:30:57 +01:00
|
|
|
/*
|
|
|
|
* Decrypt with DES-CBC, using PBKDF1 for key derivation
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pem_des_decrypt(unsigned char des_iv[8],
|
|
|
|
unsigned char *buf, size_t buflen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des_context des_ctx;
|
2011-02-12 15:30:57 +01:00
|
|
|
unsigned char des_key[8];
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des_init(&des_ctx);
|
2014-06-18 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = pem_pbkdf1(des_key, 8, des_iv, pwd, pwdlen)) != 0) {
|
2017-06-29 12:16:38 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_des_setkey_dec(&des_ctx, des_key)) != 0) {
|
2017-02-22 17:23:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_des_crypt_cbc(&des_ctx, MBEDTLS_DES_DECRYPT, buflen,
|
|
|
|
des_iv, buf, buf);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2017-02-22 17:23:26 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des_free(&des_ctx);
|
|
|
|
mbedtls_platform_zeroize(des_key, 8);
|
2017-02-22 17:23:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrypt with 3DES-CBC, using PBKDF1 for key derivation
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pem_des3_decrypt(unsigned char des3_iv[8],
|
|
|
|
unsigned char *buf, size_t buflen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_des3_context des3_ctx;
|
2011-02-12 15:30:57 +01:00
|
|
|
unsigned char des3_key[24];
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des3_init(&des3_ctx);
|
2014-06-18 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = pem_pbkdf1(des3_key, 24, des3_iv, pwd, pwdlen)) != 0) {
|
2017-06-29 12:16:38 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_des3_set3key_dec(&des3_ctx, des3_key)) != 0) {
|
2017-02-22 17:23:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_des3_crypt_cbc(&des3_ctx, MBEDTLS_DES_DECRYPT, buflen,
|
|
|
|
des3_iv, buf, buf);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2017-02-22 17:23:26 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_des3_free(&des3_ctx);
|
|
|
|
mbedtls_platform_zeroize(des3_key, 24);
|
2017-02-22 17:23:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2011-02-12 15:30:57 +01:00
|
|
|
/*
|
|
|
|
* Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
|
|
|
|
unsigned char *buf, size_t buflen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_aes_context aes_ctx;
|
2011-02-12 15:30:57 +01:00
|
|
|
unsigned char aes_key[32];
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_init(&aes_ctx);
|
2014-06-18 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = pem_pbkdf1(aes_key, keylen, aes_iv, pwd, pwdlen)) != 0) {
|
2017-06-29 12:16:38 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_aes_setkey_dec(&aes_ctx, aes_key, keylen * 8)) != 0) {
|
2017-02-22 17:23:26 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, buflen,
|
|
|
|
aes_iv, buf, buf);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2017-02-22 17:23:26 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_aes_free(&aes_ctx);
|
|
|
|
mbedtls_platform_zeroize(aes_key, keylen);
|
2017-02-22 17:23:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2024-02-12 11:01:37 +01:00
|
|
|
#if defined(MBEDTLS_DES_C) || defined(MBEDTLS_AES_C)
|
|
|
|
static int pem_check_pkcs_padding(unsigned char *input, size_t input_len, size_t *data_len)
|
|
|
|
{
|
|
|
|
size_t pad_len = input[input_len - 1];
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (pad_len > input_len) {
|
|
|
|
return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_len = input_len - pad_len;
|
|
|
|
|
|
|
|
for (i = *data_len; i < input_len; i++) {
|
|
|
|
if (input[i] != pad_len) {
|
|
|
|
return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DES_C || MBEDTLS_AES_C */
|
|
|
|
|
2022-08-18 12:38:30 +02:00
|
|
|
#endif /* PEM_RFC1421 */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
|
|
|
|
const unsigned char *data, const unsigned char *pwd,
|
|
|
|
size_t pwdlen, size_t *use_len)
|
2011-02-12 15:30:57 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int ret, enc;
|
|
|
|
size_t len;
|
2011-02-12 15:30:57 +01:00
|
|
|
unsigned char *buf;
|
2013-06-24 13:02:41 +02:00
|
|
|
const unsigned char *s1, *s2, *end;
|
2022-08-18 12:38:30 +02:00
|
|
|
#if defined(PEM_RFC1421)
|
2011-02-12 15:30:57 +01:00
|
|
|
unsigned char pem_iv[16];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_cipher_type_t enc_alg = MBEDTLS_CIPHER_NONE;
|
2011-02-12 15:30:57 +01:00
|
|
|
#else
|
|
|
|
((void) pwd);
|
|
|
|
((void) pwdlen);
|
2022-08-18 12:38:30 +02:00
|
|
|
#endif /* PEM_RFC1421 */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
s1 = (unsigned char *) strstr((const char *) data, header);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s1 == NULL) {
|
|
|
|
return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
s2 = (unsigned char *) strstr((const char *) data, footer);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 == NULL || s2 <= s1) {
|
|
|
|
return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
s1 += strlen(header);
|
|
|
|
if (*s1 == ' ') {
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
if (*s1 == '\r') {
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
if (*s1 == '\n') {
|
|
|
|
s1++;
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
}
|
2013-06-24 13:02:41 +02:00
|
|
|
|
|
|
|
end = s2;
|
2023-01-11 14:50:10 +01:00
|
|
|
end += strlen(footer);
|
|
|
|
if (*end == ' ') {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
if (*end == '\r') {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
if (*end == '\n') {
|
|
|
|
end++;
|
|
|
|
}
|
2023-11-04 13:20:09 +01:00
|
|
|
*use_len = (size_t) (end - data);
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
enc = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 >= 22 && memcmp(s1, "Proc-Type: 4,ENCRYPTED", 22) == 0) {
|
2022-08-18 12:38:30 +02:00
|
|
|
#if defined(PEM_RFC1421)
|
2011-02-12 15:30:57 +01:00
|
|
|
enc++;
|
|
|
|
|
|
|
|
s1 += 22;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*s1 == '\r') {
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
if (*s1 == '\n') {
|
|
|
|
s1++;
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_DATA;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 >= 23 && memcmp(s1, "DEK-Info: DES-EDE3-CBC,", 23) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
enc_alg = MBEDTLS_CIPHER_DES_EDE3_CBC;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 23;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (s2 - s1 >= 18 && memcmp(s1, "DEK-Info: DES-CBC,", 18) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
enc_alg = MBEDTLS_CIPHER_DES_CBC;
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 18;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 < 16 || pem_get_iv(s1, pem_iv, 8) != 0) {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 16;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 >= 14 && memcmp(s1, "DEK-Info: AES-", 14) == 0) {
|
|
|
|
if (s2 - s1 < 22) {
|
|
|
|
return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
|
|
|
|
} else if (memcmp(s1, "DEK-Info: AES-128-CBC,", 22) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
enc_alg = MBEDTLS_CIPHER_AES_128_CBC;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (memcmp(s1, "DEK-Info: AES-192-CBC,", 22) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
enc_alg = MBEDTLS_CIPHER_AES_192_CBC;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (memcmp(s1, "DEK-Info: AES-256-CBC,", 22) == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
enc_alg = MBEDTLS_CIPHER_AES_256_CBC;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 22;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s2 - s1 < 32 || pem_get_iv(s1, pem_iv, 16) != 0) {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_ENC_IV;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
|
|
|
s1 += 32;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2013-09-15 20:43:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enc_alg == MBEDTLS_CIPHER_NONE) {
|
|
|
|
return MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*s1 == '\r') {
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
if (*s1 == '\n') {
|
|
|
|
s1++;
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_DATA;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
|
2022-08-18 12:38:30 +02:00
|
|
|
#endif /* PEM_RFC1421 */
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s1 >= s2) {
|
|
|
|
return MBEDTLS_ERR_PEM_INVALID_DATA;
|
|
|
|
}
|
2015-10-05 01:26:36 +02:00
|
|
|
|
2023-11-04 13:20:09 +01:00
|
|
|
ret = mbedtls_base64_decode(NULL, 0, &len, s1, (size_t) (s2 - s1));
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((buf = mbedtls_calloc(1, len)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_PEM_ALLOC_FAILED;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-11-04 13:20:09 +01:00
|
|
|
if ((ret = mbedtls_base64_decode(buf, len, &len, s1, (size_t) (s2 - s1))) != 0) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(buf, len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
2013-09-15 20:43:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enc != 0) {
|
2022-08-18 12:38:30 +02:00
|
|
|
#if defined(PEM_RFC1421)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pwd == NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(buf, len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_PEM_PASSWORD_REQUIRED;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 17:23:26 +01:00
|
|
|
ret = 0;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enc_alg == MBEDTLS_CIPHER_DES_EDE3_CBC) {
|
|
|
|
ret = pem_des3_decrypt(pem_iv, buf, len, pwd, pwdlen);
|
|
|
|
} else if (enc_alg == MBEDTLS_CIPHER_DES_CBC) {
|
|
|
|
ret = pem_des_decrypt(pem_iv, buf, len, pwd, pwdlen);
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_DES_C */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enc_alg == MBEDTLS_CIPHER_AES_128_CBC) {
|
|
|
|
ret = pem_aes_decrypt(pem_iv, 16, buf, len, pwd, pwdlen);
|
|
|
|
} else if (enc_alg == MBEDTLS_CIPHER_AES_192_CBC) {
|
|
|
|
ret = pem_aes_decrypt(pem_iv, 24, buf, len, pwd, pwdlen);
|
|
|
|
} else if (enc_alg == MBEDTLS_CIPHER_AES_256_CBC) {
|
|
|
|
ret = pem_aes_decrypt(pem_iv, 32, buf, len, pwd, pwdlen);
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_AES_C */
|
2011-02-12 15:30:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(buf);
|
|
|
|
return ret;
|
2017-02-22 17:23:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-12 11:01:37 +01:00
|
|
|
/* Check PKCS padding and update data length based on padding info.
|
|
|
|
* This can be used to detect invalid padding data and password
|
|
|
|
* mismatches. */
|
|
|
|
ret = pem_check_pkcs_padding(buf, len, &len);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-03 21:01:35 +02:00
|
|
|
/*
|
2024-02-12 11:01:37 +01:00
|
|
|
* In RFC1421 PEM is used as container for DER (ASN.1) content so we
|
|
|
|
* can use ASN.1 functions to parse the main SEQUENCE tag and to get its
|
|
|
|
* length.
|
2013-07-03 21:01:35 +02:00
|
|
|
*/
|
2024-02-08 17:51:00 +01:00
|
|
|
unsigned char *p = buf;
|
2024-02-12 11:01:37 +01:00
|
|
|
size_t sequence_len;
|
|
|
|
ret = mbedtls_asn1_get_tag(&p, buf + len, &sequence_len,
|
2024-02-08 17:51:00 +01:00
|
|
|
MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(buf);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PEM_INVALID_DATA, ret);
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
2024-02-08 17:51:00 +01:00
|
|
|
/* Add also the sequence block (tag + len) to the total amount of valid data. */
|
2024-02-12 11:01:37 +01:00
|
|
|
sequence_len += (p - buf);
|
|
|
|
|
|
|
|
/* Ensure that the reported SEQUENCE length matches the data len (i.e. no
|
|
|
|
* trailing garbage data). */
|
|
|
|
if (len != sequence_len) {
|
|
|
|
return MBEDTLS_ERR_PEM_BAD_INPUT_DATA;
|
|
|
|
}
|
2011-02-12 15:30:57 +01:00
|
|
|
#else
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(buf, len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE;
|
2022-08-18 12:38:30 +02:00
|
|
|
#endif /* PEM_RFC1421 */
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->buf = buf;
|
|
|
|
ctx->buflen = len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2011-02-12 15:30:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pem_free(mbedtls_pem_context *ctx)
|
2013-09-15 20:43:33 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->buf != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ctx->buf, ctx->buflen);
|
2019-01-31 14:20:20 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ctx->info);
|
2013-09-15 20:43:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pem_context));
|
2013-09-15 20:43:33 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
2013-09-15 20:43:33 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pem_write_buffer(const char *header, const char *footer,
|
|
|
|
const unsigned char *der_data, size_t der_len,
|
|
|
|
unsigned char *buf, size_t buf_len, size_t *olen)
|
2013-09-15 20:03:26 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2017-01-30 15:34:25 +01:00
|
|
|
unsigned char *encode_buf = NULL, *c, *p = buf;
|
2015-06-02 17:30:35 +02:00
|
|
|
size_t len = 0, use_len, add_len = 0;
|
2013-09-15 20:03:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_base64_encode(NULL, 0, &use_len, der_data, der_len);
|
2023-05-03 14:49:28 +02:00
|
|
|
add_len = strlen(header) + strlen(footer) + (((use_len > 2) ? (use_len - 2) : 0) / 64) + 1;
|
2014-04-11 13:28:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_len + add_len > buf_len) {
|
2013-09-15 20:03:26 +02:00
|
|
|
*olen = use_len + add_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL;
|
2013-09-15 20:03:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_len != 0 &&
|
|
|
|
((encode_buf = mbedtls_calloc(1, use_len)) == NULL)) {
|
|
|
|
return MBEDTLS_ERR_PEM_ALLOC_FAILED;
|
|
|
|
}
|
2013-09-15 20:03:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
|
|
|
|
der_len)) != 0) {
|
|
|
|
mbedtls_free(encode_buf);
|
|
|
|
return ret;
|
2013-09-15 20:03:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, header, strlen(header));
|
|
|
|
p += strlen(header);
|
2013-09-15 20:03:26 +02:00
|
|
|
c = encode_buf;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (use_len) {
|
|
|
|
len = (use_len > 64) ? 64 : use_len;
|
|
|
|
memcpy(p, c, len);
|
2013-09-15 20:03:26 +02:00
|
|
|
use_len -= len;
|
|
|
|
p += len;
|
|
|
|
c += len;
|
|
|
|
*p++ = '\n';
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, footer, strlen(footer));
|
|
|
|
p += strlen(footer);
|
2013-09-15 20:03:26 +02:00
|
|
|
|
|
|
|
*p++ = '\0';
|
2023-11-04 13:20:09 +01:00
|
|
|
*olen = (size_t) (p - buf);
|
2013-09-15 20:03:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/* Clean any remaining data previously written to the buffer */
|
|
|
|
memset(buf + *olen, 0, buf_len - *olen);
|
2020-11-19 10:46:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(encode_buf);
|
|
|
|
return 0;
|
2013-09-15 20:03:26 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
|