2013-06-24 19:17:19 +02:00
|
|
|
/*
|
|
|
|
* PKCS#12 Personal Information Exchange Syntax
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-24 19:17:19 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-06-24 19:17:19 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-06-24 19:17:19 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-06-24 19:17:19 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The PKCS #12 Personal Information Exchange Syntax Standard v1.1
|
|
|
|
*
|
|
|
|
* http://www.rsa.com/rsalabs/pkcs/files/h11301-wp-pkcs-12v1-1-personal-information-exchange-syntax.pdf
|
|
|
|
* ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C)
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs12.h"
|
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#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"
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DES_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/des.h"
|
2013-06-24 19:17:19 +02:00
|
|
|
#endif
|
|
|
|
|
2023-06-07 13:07:21 +02:00
|
|
|
#include "psa_util_internal.h"
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2018-10-12 11:46:32 +02:00
|
|
|
#if defined(MBEDTLS_ASN1_PARSE_C)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
|
|
|
|
mbedtls_asn1_buf *salt, int *iterations)
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-12 15:05:10 +02:00
|
|
|
unsigned char **p = ¶ms->p;
|
2013-06-29 12:16:17 +02:00
|
|
|
const unsigned char *end = params->p + params->len;
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pkcs-12PbeParams ::= SEQUENCE {
|
|
|
|
* salt OCTET STRING,
|
|
|
|
* iterations INTEGER
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-09-12 15:05:10 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-09-12 15:05:10 +02:00
|
|
|
salt->p = *p;
|
|
|
|
*p += salt->len;
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-09-12 15:05:10 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_int(p, end, iterations)) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret);
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-09-12 15:05:10 +02:00
|
|
|
if (*p != end) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 18:34:48 +02:00
|
|
|
#define PKCS12_MAX_PWDLEN 128
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *iv, size_t ivlen)
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2016-04-13 12:48:25 +02:00
|
|
|
int ret, iterations = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf salt;
|
2013-06-24 19:17:19 +02:00
|
|
|
size_t i;
|
2015-09-28 18:34:48 +02:00
|
|
|
unsigned char unipwd[PKCS12_MAX_PWDLEN * 2 + 2];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pwdlen > PKCS12_MAX_PWDLEN) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&salt, 0, sizeof(mbedtls_asn1_buf));
|
|
|
|
memset(&unipwd, 0, sizeof(unipwd));
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = pkcs12_parse_pbe_params(pbe_params, &salt,
|
|
|
|
&iterations)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < pwdlen; i++) {
|
2013-06-24 19:17:19 +02:00
|
|
|
unipwd[i * 2 + 1] = pwd[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pkcs12_derivation(key, keylen, unipwd, pwdlen * 2 + 2,
|
|
|
|
salt.p, salt.len, md_type,
|
|
|
|
MBEDTLS_PKCS12_DERIVE_KEY, iterations)) != 0) {
|
|
|
|
return ret;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iv == NULL || ivlen == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pkcs12_derivation(iv, ivlen, unipwd, pwdlen * 2 + 2,
|
|
|
|
salt.p, salt.len, md_type,
|
|
|
|
MBEDTLS_PKCS12_DERIVE_IV, iterations)) != 0) {
|
|
|
|
return ret;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2015-09-28 18:34:48 +02:00
|
|
|
#undef PKCS12_MAX_PWDLEN
|
|
|
|
|
2023-09-06 16:48:08 +02:00
|
|
|
#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
|
|
|
int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
|
|
|
|
mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
const unsigned char *data, size_t len,
|
|
|
|
unsigned char *output, size_t output_size,
|
|
|
|
size_t *output_len);
|
|
|
|
#endif
|
|
|
|
|
2023-09-12 15:42:49 +02:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
|
|
|
|
mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
const unsigned char *data, size_t len,
|
|
|
|
unsigned char *output)
|
2023-09-06 16:48:08 +02:00
|
|
|
{
|
|
|
|
size_t output_len = 0;
|
|
|
|
|
|
|
|
/* We assume caller of the function is providing a big enough output buffer
|
|
|
|
* so we pass output_size as SIZE_MAX to pass checks, However, no guarantees
|
|
|
|
* for the output size actually being correct.
|
|
|
|
*/
|
|
|
|
return mbedtls_pkcs12_pbe_ext(pbe_params, mode, cipher_type, md_type,
|
|
|
|
pwd, pwdlen, data, len, output, SIZE_MAX,
|
|
|
|
&output_len);
|
|
|
|
}
|
2023-09-12 15:42:49 +02:00
|
|
|
#endif
|
2023-09-06 16:48:08 +02:00
|
|
|
|
|
|
|
int mbedtls_pkcs12_pbe_ext(mbedtls_asn1_buf *pbe_params, int mode,
|
|
|
|
mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
const unsigned char *data, size_t len,
|
|
|
|
unsigned char *output, size_t output_size,
|
|
|
|
size_t *output_len)
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2013-06-24 19:33:27 +02:00
|
|
|
int ret, keylen = 0;
|
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t cipher_ctx;
|
2023-10-05 06:24:06 +02:00
|
|
|
size_t iv_len = 0;
|
2023-09-06 16:48:08 +02:00
|
|
|
size_t finish_olen = 0;
|
|
|
|
unsigned int padlen = 0;
|
2013-06-24 19:33:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pwd == NULL && pwdlen != 0) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-11-11 20:00:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type(cipher_type);
|
|
|
|
if (cipher_info == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2013-06-24 19:33:27 +02:00
|
|
|
|
2023-06-24 17:53:13 +02:00
|
|
|
keylen = (int) mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
|
2013-06-24 19:33:27 +02:00
|
|
|
|
2023-09-06 16:48:08 +02:00
|
|
|
if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
|
|
|
|
if (output_size < len) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == MBEDTLS_PKCS12_PBE_ENCRYPT) {
|
|
|
|
padlen = cipher_info->block_size - (len % cipher_info->block_size);
|
|
|
|
if (output_size < (len + padlen)) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:24:06 +02:00
|
|
|
iv_len = mbedtls_cipher_info_get_iv_size(cipher_info);
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = pkcs12_pbe_derive_key_iv(pbe_params, md_type, pwd, pwdlen,
|
|
|
|
key, keylen,
|
2023-10-05 06:24:06 +02:00
|
|
|
iv, iv_len)) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_init(&cipher_ctx);
|
2014-07-01 14:53:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
|
2013-07-03 14:44:40 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-10-05 06:24:06 +02:00
|
|
|
if ((ret = mbedtls_cipher_setkey(&cipher_ctx, key, 8 * keylen,
|
|
|
|
(mbedtls_operation_t) mode)) != 0) {
|
2013-07-03 14:44:40 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
Improve & test legacy mbedtls_pkcs12_pbe
* Prevent pkcs12_pbe encryption when PKCS7 padding has been
disabled since this not part of the specs.
* Allow decryption when PKCS7 padding is disabled for legacy
reasons, However, invalid padding is not checked.
* Document new behaviour, known limitations and possible
security concerns.
* Add tests to check these scenarios. Test data has been
generated by the below code using OpenSSL as a reference:
#include <openssl/pkcs12.h>
#include <openssl/evp.h>
#include <openssl/des.h>
#include <openssl/asn1.h>
#include "crypto/asn1.h"
#include <string.h>
int main()
{
char pass[] = "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB";
unsigned char salt[] = "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC";
unsigned char plaintext[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
unsigned char *ciphertext = NULL;
int iter = 10;
X509_ALGOR *alg = X509_ALGOR_new();
int ciphertext_len = 0;
int alg_nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
alg->parameter = ASN1_TYPE_new();
struct asn1_object_st * aobj;
PKCS5_pbe_set0_algor(alg, alg_nid, iter,
salt, sizeof(salt)-1);
aobj = alg->algorithm;
printf("\"30%.2X", 2 + aobj->length + alg->parameter->value.asn1_string->length);
printf("06%.2X", aobj->length);
for (int i = 0; i < aobj->length; i++) {
printf("%.2X", aobj->data[i]);
}
for (int i = 0; i < alg->parameter->value.asn1_string->length; i++) {
printf("%.2X", alg->parameter->value.asn1_string->data[i]);
}
printf("\":\"");
for (int i = 0; i < sizeof(pass)-1; i++) {
printf("%.2X", pass[i] & 0xFF);
}
printf("\":\"");
for (int i = 0; i < sizeof(plaintext)-1; i++) {
printf("%.2X", plaintext[i]);
}
printf("\":");
printf("0");
printf(":\"");
unsigned char * res = PKCS12_pbe_crypt(alg, pass, sizeof(pass)-1, plaintext, sizeof(plaintext)-1, &ciphertext, &ciphertext_len, 1);
if (res == NULL)
printf("Encryption failed!\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%.2X", res[i]);
}
printf("\"\n");
return 0;
}
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
#
2023-09-04 16:11:22 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
|
|
/* PKCS12 uses CBC with PKCS7 padding */
|
|
|
|
|
2023-10-13 11:39:53 +02:00
|
|
|
mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_PKCS7;
|
Improve & test legacy mbedtls_pkcs12_pbe
* Prevent pkcs12_pbe encryption when PKCS7 padding has been
disabled since this not part of the specs.
* Allow decryption when PKCS7 padding is disabled for legacy
reasons, However, invalid padding is not checked.
* Document new behaviour, known limitations and possible
security concerns.
* Add tests to check these scenarios. Test data has been
generated by the below code using OpenSSL as a reference:
#include <openssl/pkcs12.h>
#include <openssl/evp.h>
#include <openssl/des.h>
#include <openssl/asn1.h>
#include "crypto/asn1.h"
#include <string.h>
int main()
{
char pass[] = "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB";
unsigned char salt[] = "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC";
unsigned char plaintext[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
unsigned char *ciphertext = NULL;
int iter = 10;
X509_ALGOR *alg = X509_ALGOR_new();
int ciphertext_len = 0;
int alg_nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
alg->parameter = ASN1_TYPE_new();
struct asn1_object_st * aobj;
PKCS5_pbe_set0_algor(alg, alg_nid, iter,
salt, sizeof(salt)-1);
aobj = alg->algorithm;
printf("\"30%.2X", 2 + aobj->length + alg->parameter->value.asn1_string->length);
printf("06%.2X", aobj->length);
for (int i = 0; i < aobj->length; i++) {
printf("%.2X", aobj->data[i]);
}
for (int i = 0; i < alg->parameter->value.asn1_string->length; i++) {
printf("%.2X", alg->parameter->value.asn1_string->data[i]);
}
printf("\":\"");
for (int i = 0; i < sizeof(pass)-1; i++) {
printf("%.2X", pass[i] & 0xFF);
}
printf("\":\"");
for (int i = 0; i < sizeof(plaintext)-1; i++) {
printf("%.2X", plaintext[i]);
}
printf("\":");
printf("0");
printf(":\"");
unsigned char * res = PKCS12_pbe_crypt(alg, pass, sizeof(pass)-1, plaintext, sizeof(plaintext)-1, &ciphertext, &ciphertext_len, 1);
if (res == NULL)
printf("Encryption failed!\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%.2X", res[i]);
}
printf("\"\n");
return 0;
}
Signed-off-by: Waleed Elmelegy <waleed.elmelegy@arm.com>
#
2023-09-04 16:11:22 +02:00
|
|
|
#if !defined(MBEDTLS_CIPHER_PADDING_PKCS7)
|
|
|
|
/* For historical reasons, when decrypting, this function works when
|
|
|
|
* decrypting even when support for PKCS7 padding is disabled. In this
|
|
|
|
* case, it ignores the padding, and so will never report a
|
|
|
|
* password mismatch.
|
|
|
|
*/
|
|
|
|
if (mode == MBEDTLS_PKCS12_PBE_DECRYPT) {
|
|
|
|
padding = MBEDTLS_PADDING_NONE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, padding)) != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
|
|
|
|
2023-10-05 06:24:06 +02:00
|
|
|
ret = mbedtls_cipher_crypt(&cipher_ctx, iv, iv_len, data, len, output, &finish_olen);
|
|
|
|
if (ret == MBEDTLS_ERR_CIPHER_INVALID_PADDING) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-09-06 16:48:08 +02:00
|
|
|
*output_len += finish_olen;
|
|
|
|
|
2013-07-03 14:44:40 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(key, sizeof(key));
|
|
|
|
mbedtls_platform_zeroize(iv, sizeof(iv));
|
|
|
|
mbedtls_cipher_free(&cipher_ctx);
|
2013-07-03 14:44:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-06-24 19:23:12 +02:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2018-10-12 11:46:32 +02:00
|
|
|
#endif /* MBEDTLS_ASN1_PARSE_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
|
|
|
|
const unsigned char *filler, size_t fill_len)
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = data;
|
|
|
|
size_t use_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (filler != NULL && fill_len != 0) {
|
|
|
|
while (data_len > 0) {
|
|
|
|
use_len = (data_len > fill_len) ? fill_len : data_len;
|
|
|
|
memcpy(p, filler, use_len);
|
2021-11-11 20:00:38 +01:00
|
|
|
p += use_len;
|
|
|
|
data_len -= use_len;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-12-02 18:51:34 +01:00
|
|
|
/* If either of the above are not true then clearly there is nothing
|
|
|
|
* that this function can do. The function should *not* be called
|
|
|
|
* under either of those circumstances, as you could end up with an
|
|
|
|
* incorrect output but for safety's sake, leaving the check in as
|
|
|
|
* otherwise we could end up with memory corruption.*/
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int calculate_hashes(mbedtls_md_type_t md_type, int iterations,
|
|
|
|
unsigned char *diversifier, unsigned char *salt_block,
|
|
|
|
unsigned char *pwd_block, unsigned char *hash_output, int use_salt,
|
|
|
|
int use_password, size_t hlen, size_t v)
|
2022-08-24 16:47:10 +02:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
size_t i;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_type(md_type);
|
|
|
|
if (md_info == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&md_ctx);
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
// Calculate hash( diversifier || salt_block || pwd_block )
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_update(&md_ctx, diversifier, v)) != 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_salt != 0) {
|
|
|
|
if ((ret = mbedtls_md_update(&md_ctx, salt_block, v)) != 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_password != 0) {
|
|
|
|
if ((ret = mbedtls_md_update(&md_ctx, pwd_block, v)) != 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&md_ctx, hash_output)) != 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
|
|
|
|
// Perform remaining ( iterations - 1 ) recursive hash calculations
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 1; i < (size_t) iterations; i++) {
|
|
|
|
if ((ret = mbedtls_md(md_info, hash_output, hlen, hash_output))
|
|
|
|
!= 0) {
|
2022-08-24 16:47:10 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-24 16:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&md_ctx);
|
2022-08-24 16:47:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen,
|
|
|
|
const unsigned char *salt, size_t saltlen,
|
|
|
|
mbedtls_md_type_t md_type, int id, int iterations)
|
2013-06-24 19:17:19 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-06-24 19:17:19 +02:00
|
|
|
unsigned int j;
|
|
|
|
|
|
|
|
unsigned char diversifier[128];
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char salt_block[128], pwd_block[128], hash_block[128] = { 0 };
|
2023-03-28 11:20:23 +02:00
|
|
|
unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
|
2013-06-24 19:17:19 +02:00
|
|
|
unsigned char *p;
|
|
|
|
unsigned char c;
|
2021-11-18 15:02:21 +01:00
|
|
|
int use_password = 0;
|
|
|
|
int use_salt = 0;
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
size_t hlen, use_len, v, i;
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
// This version only allows max of 64 bytes of password or salt
|
2023-01-11 14:50:10 +01:00
|
|
|
if (datalen > 128 || pwdlen > 64 || saltlen > 64) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pwd == NULL && pwdlen != 0) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-11-11 20:00:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (salt == NULL && saltlen != 0) {
|
|
|
|
return MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-11-18 15:02:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
use_password = (pwd && pwdlen != 0);
|
|
|
|
use_salt = (salt && saltlen != 0);
|
2021-11-18 15:02:21 +01:00
|
|
|
|
2023-03-28 11:14:24 +02:00
|
|
|
hlen = mbedtls_md_get_size_from_type(md_type);
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hlen <= 32) {
|
2013-06-24 19:17:19 +02:00
|
|
|
v = 64;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2013-06-24 19:17:19 +02:00
|
|
|
v = 128;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(diversifier, (unsigned char) id, v);
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_salt != 0) {
|
|
|
|
pkcs12_fill_buffer(salt_block, v, salt, saltlen);
|
2021-11-18 15:02:21 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_password != 0) {
|
|
|
|
pkcs12_fill_buffer(pwd_block, v, pwd, pwdlen);
|
2021-11-18 15:02:21 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
p = data;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (datalen > 0) {
|
|
|
|
if (calculate_hashes(md_type, iterations, diversifier, salt_block,
|
|
|
|
pwd_block, hash_output, use_salt, use_password, hlen,
|
|
|
|
v) != 0) {
|
2013-07-03 14:44:40 +02:00
|
|
|
goto exit;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
use_len = (datalen > hlen) ? hlen : datalen;
|
|
|
|
memcpy(p, hash_output, use_len);
|
2013-06-24 19:17:19 +02:00
|
|
|
datalen -= use_len;
|
|
|
|
p += use_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (datalen == 0) {
|
2013-06-24 19:17:19 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
// Concatenating copies of hash_output into hash_block (B)
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs12_fill_buffer(hash_block, v, hash_output, hlen);
|
2013-06-24 19:17:19 +02:00
|
|
|
|
|
|
|
// B += 1
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = v; i > 0; i--) {
|
|
|
|
if (++hash_block[i - 1] != 0) {
|
2013-06-24 19:17:19 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_salt != 0) {
|
2021-11-18 15:02:21 +01:00
|
|
|
// salt_block += B
|
|
|
|
c = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = v; i > 0; i--) {
|
2021-11-18 15:02:21 +01:00
|
|
|
j = salt_block[i - 1] + hash_block[i - 1] + c;
|
2023-01-11 14:50:10 +01:00
|
|
|
c = MBEDTLS_BYTE_1(j);
|
|
|
|
salt_block[i - 1] = MBEDTLS_BYTE_0(j);
|
2021-11-18 15:02:21 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (use_password != 0) {
|
2021-11-18 15:02:21 +01:00
|
|
|
// pwd_block += B
|
|
|
|
c = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = v; i > 0; i--) {
|
2021-11-18 15:02:21 +01:00
|
|
|
j = pwd_block[i - 1] + hash_block[i - 1] + c;
|
2023-01-11 14:50:10 +01:00
|
|
|
c = MBEDTLS_BYTE_1(j);
|
|
|
|
pwd_block[i - 1] = MBEDTLS_BYTE_0(j);
|
2021-11-18 15:02:21 +01:00
|
|
|
}
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 14:44:40 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(salt_block, sizeof(salt_block));
|
|
|
|
mbedtls_platform_zeroize(pwd_block, sizeof(pwd_block));
|
|
|
|
mbedtls_platform_zeroize(hash_block, sizeof(hash_block));
|
|
|
|
mbedtls_platform_zeroize(hash_output, sizeof(hash_output));
|
2014-06-18 13:59:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-06-24 19:17:19 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS12_C */
|