mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Externals: Update mbedtls to 2.28.0
This commit is contained in:
119
Externals/mbedtls/library/pkcs12.c
vendored
119
Externals/mbedtls/library/pkcs12.c
vendored
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* PKCS#12 Personal Information Exchange Syntax
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* Copyright The Mbed TLS Contributors
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
@ -15,8 +15,6 @@
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
/*
|
||||
* The PKCS #12 Personal Information Exchange Syntax Standard v1.1
|
||||
@ -25,11 +23,7 @@
|
||||
* ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1-1.asn
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_PKCS12_C)
|
||||
|
||||
@ -37,6 +31,7 @@
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@ -53,7 +48,7 @@
|
||||
static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations )
|
||||
{
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char **p = ¶ms->p;
|
||||
const unsigned char *end = params->p + params->len;
|
||||
|
||||
@ -65,21 +60,21 @@ static int pkcs12_parse_pbe_params( mbedtls_asn1_buf *params,
|
||||
*
|
||||
*/
|
||||
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
||||
return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
||||
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
|
||||
|
||||
if( ( ret = mbedtls_asn1_get_tag( p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
|
||||
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
|
||||
|
||||
salt->p = *p;
|
||||
*p += salt->len;
|
||||
|
||||
if( ( ret = mbedtls_asn1_get_int( p, end, iterations ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT + ret );
|
||||
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT, ret ) );
|
||||
|
||||
if( *p != end )
|
||||
return( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT +
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
||||
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@ -145,7 +140,7 @@ int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode,
|
||||
((void) output);
|
||||
return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
#else
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char key[16];
|
||||
mbedtls_arc4_context ctx;
|
||||
((void) mode);
|
||||
@ -184,6 +179,9 @@ int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode,
|
||||
mbedtls_cipher_context_t cipher_ctx;
|
||||
size_t olen = 0;
|
||||
|
||||
if( pwd == NULL && pwdlen != 0 )
|
||||
return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
|
||||
|
||||
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
|
||||
if( cipher_info == NULL )
|
||||
return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
@ -236,12 +234,23 @@ static void pkcs12_fill_buffer( unsigned char *data, size_t data_len,
|
||||
unsigned char *p = data;
|
||||
size_t use_len;
|
||||
|
||||
while( data_len > 0 )
|
||||
if( filler != NULL && fill_len != 0 )
|
||||
{
|
||||
use_len = ( data_len > fill_len ) ? fill_len : data_len;
|
||||
memcpy( p, filler, use_len );
|
||||
p += use_len;
|
||||
data_len -= use_len;
|
||||
while( data_len > 0 )
|
||||
{
|
||||
use_len = ( data_len > fill_len ) ? fill_len : data_len;
|
||||
memcpy( p, filler, use_len );
|
||||
p += use_len;
|
||||
data_len -= use_len;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 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.*/
|
||||
}
|
||||
}
|
||||
|
||||
@ -250,7 +259,7 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
const unsigned char *salt, size_t saltlen,
|
||||
mbedtls_md_type_t md_type, int id, int iterations )
|
||||
{
|
||||
int ret;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned int j;
|
||||
|
||||
unsigned char diversifier[128];
|
||||
@ -258,6 +267,8 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
unsigned char hash_output[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char *p;
|
||||
unsigned char c;
|
||||
int use_password = 0;
|
||||
int use_salt = 0;
|
||||
|
||||
size_t hlen, use_len, v, i;
|
||||
|
||||
@ -268,6 +279,15 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
if( datalen > 128 || pwdlen > 64 || saltlen > 64 )
|
||||
return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
|
||||
|
||||
if( pwd == NULL && pwdlen != 0 )
|
||||
return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
|
||||
|
||||
if( salt == NULL && saltlen != 0 )
|
||||
return( MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA );
|
||||
|
||||
use_password = ( pwd && pwdlen != 0 );
|
||||
use_salt = ( salt && saltlen != 0 );
|
||||
|
||||
md_info = mbedtls_md_info_from_type( md_type );
|
||||
if( md_info == NULL )
|
||||
return( MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE );
|
||||
@ -285,8 +305,15 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
|
||||
memset( diversifier, (unsigned char) id, v );
|
||||
|
||||
pkcs12_fill_buffer( salt_block, v, salt, saltlen );
|
||||
pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
|
||||
if( use_salt != 0 )
|
||||
{
|
||||
pkcs12_fill_buffer( salt_block, v, salt, saltlen );
|
||||
}
|
||||
|
||||
if( use_password != 0 )
|
||||
{
|
||||
pkcs12_fill_buffer( pwd_block, v, pwd, pwdlen );
|
||||
}
|
||||
|
||||
p = data;
|
||||
while( datalen > 0 )
|
||||
@ -298,11 +325,17 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
if( ( ret = mbedtls_md_update( &md_ctx, diversifier, v ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v ) ) != 0 )
|
||||
goto exit;
|
||||
if( use_salt != 0 )
|
||||
{
|
||||
if( ( ret = mbedtls_md_update( &md_ctx, salt_block, v )) != 0 )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v ) ) != 0 )
|
||||
goto exit;
|
||||
if( use_password != 0)
|
||||
{
|
||||
if( ( ret = mbedtls_md_update( &md_ctx, pwd_block, v )) != 0 )
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_md_finish( &md_ctx, hash_output ) ) != 0 )
|
||||
goto exit;
|
||||
@ -330,22 +363,28 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen,
|
||||
if( ++hash_block[i - 1] != 0 )
|
||||
break;
|
||||
|
||||
// salt_block += B
|
||||
c = 0;
|
||||
for( i = v; i > 0; i-- )
|
||||
if( use_salt != 0 )
|
||||
{
|
||||
j = salt_block[i - 1] + hash_block[i - 1] + c;
|
||||
c = (unsigned char) (j >> 8);
|
||||
salt_block[i - 1] = j & 0xFF;
|
||||
// salt_block += B
|
||||
c = 0;
|
||||
for( i = v; i > 0; i-- )
|
||||
{
|
||||
j = salt_block[i - 1] + hash_block[i - 1] + c;
|
||||
c = MBEDTLS_BYTE_1( j );
|
||||
salt_block[i - 1] = MBEDTLS_BYTE_0( j );
|
||||
}
|
||||
}
|
||||
|
||||
// pwd_block += B
|
||||
c = 0;
|
||||
for( i = v; i > 0; i-- )
|
||||
if( use_password != 0 )
|
||||
{
|
||||
j = pwd_block[i - 1] + hash_block[i - 1] + c;
|
||||
c = (unsigned char) (j >> 8);
|
||||
pwd_block[i - 1] = j & 0xFF;
|
||||
// pwd_block += B
|
||||
c = 0;
|
||||
for( i = v; i > 0; i-- )
|
||||
{
|
||||
j = pwd_block[i - 1] + hash_block[i - 1] + c;
|
||||
c = MBEDTLS_BYTE_1( j );
|
||||
pwd_block[i - 1] = MBEDTLS_BYTE_0( j );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user