mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-27 03:24:18 +01:00
Fix data loss in unsigned int cast in PK
This patch introduces some additional checks in the PK module for 64-bit systems only. The problem is that the API functions in the PK abstraction accept a size_t value for the hashlen, while the RSA module accepts an unsigned int for the hashlen. Instead of silently casting size_t to unsigned int, this change checks whether the hashlen overflows an unsigned int and returns an error.
This commit is contained in:
parent
df33a6a805
commit
c71b7eb0e7
@ -1,6 +1,13 @@
|
|||||||
mbed TLS ChangeLog (Sorted per branch, date)
|
mbed TLS ChangeLog (Sorted per branch, date)
|
||||||
|
|
||||||
= mbed TLS x.x.x branch xxxx-xx-xx
|
= mbed TLS x.x.x branch released xxxx-xx-xx
|
||||||
|
|
||||||
|
Security
|
||||||
|
* Add checks to prevent signature forgeries for very large messages while
|
||||||
|
using RSA through the PK module in 64-bit systems. The issue was caused by
|
||||||
|
some data loss when casting a size_t to an unsigned int value in the
|
||||||
|
functions rsa_verify_wrap(), rsa_sign_wrap(), rsa_alt_sign_wrap() and
|
||||||
|
pk_sign(). Found by Jean-Philippe Aumasson.
|
||||||
|
|
||||||
Bugfix
|
Bugfix
|
||||||
* Fix unused variable/function compilation warnings in pem.c and x509_csr.c
|
* Fix unused variable/function compilation warnings in pem.c and x509_csr.c
|
||||||
|
11
library/pk.c
11
library/pk.c
@ -30,6 +30,8 @@
|
|||||||
#include "polarssl/pk.h"
|
#include "polarssl/pk.h"
|
||||||
#include "polarssl/pk_wrap.h"
|
#include "polarssl/pk_wrap.h"
|
||||||
|
|
||||||
|
#include "polarssl/bignum.h"
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
#include "polarssl/rsa.h"
|
#include "polarssl/rsa.h"
|
||||||
#endif
|
#endif
|
||||||
@ -40,6 +42,8 @@
|
|||||||
#include "polarssl/ecdsa.h"
|
#include "polarssl/ecdsa.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void polarssl_zeroize( void *v, size_t n ) {
|
static void polarssl_zeroize( void *v, size_t n ) {
|
||||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||||
@ -208,6 +212,11 @@ int pk_verify_ext( pk_type_t type, const void *options,
|
|||||||
int ret;
|
int ret;
|
||||||
const pk_rsassa_pss_options *pss_opts;
|
const pk_rsassa_pss_options *pss_opts;
|
||||||
|
|
||||||
|
#if defined(POLARSSL_HAVE_INT64)
|
||||||
|
if( md_alg == POLARSSL_MD_NONE && UINT_MAX < hash_len )
|
||||||
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
#endif /* POLARSSL_HAVE_INT64 */
|
||||||
|
|
||||||
if( options == NULL )
|
if( options == NULL )
|
||||||
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
|
||||||
@ -231,7 +240,7 @@ int pk_verify_ext( pk_type_t type, const void *options,
|
|||||||
return( 0 );
|
return( 0 );
|
||||||
#else
|
#else
|
||||||
return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
|
return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE );
|
||||||
#endif
|
#endif /* POLARSSL_RSA_C && POLARSSL_PKCS1_V21 */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* General case: no options */
|
/* General case: no options */
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
/* Even if RSA not activated, for the sake of RSA-alt */
|
/* Even if RSA not activated, for the sake of RSA-alt */
|
||||||
#include "polarssl/rsa.h"
|
#include "polarssl/rsa.h"
|
||||||
|
#include "polarssl/bignum.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -50,6 +51,8 @@
|
|||||||
#define polarssl_free free
|
#define polarssl_free free
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* Implementation that should never be optimized out by the compiler */
|
||||||
static void polarssl_zeroize( void *v, size_t n ) {
|
static void polarssl_zeroize( void *v, size_t n ) {
|
||||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||||
@ -73,6 +76,11 @@ static int rsa_verify_wrap( void *ctx, md_type_t md_alg,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
#if defined(POLARSSL_HAVE_INT64)
|
||||||
|
if( md_alg == POLARSSL_MD_NONE && UINT_MAX < hash_len )
|
||||||
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
#endif /* POLARSSL_HAVE_INT64 */
|
||||||
|
|
||||||
if( sig_len < ((rsa_context *) ctx)->len )
|
if( sig_len < ((rsa_context *) ctx)->len )
|
||||||
return( POLARSSL_ERR_RSA_VERIFY_FAILED );
|
return( POLARSSL_ERR_RSA_VERIFY_FAILED );
|
||||||
|
|
||||||
@ -92,6 +100,11 @@ static int rsa_sign_wrap( void *ctx, md_type_t md_alg,
|
|||||||
unsigned char *sig, size_t *sig_len,
|
unsigned char *sig, size_t *sig_len,
|
||||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
||||||
{
|
{
|
||||||
|
#if defined(POLARSSL_HAVE_INT64)
|
||||||
|
if( md_alg == POLARSSL_MD_NONE && UINT_MAX < hash_len )
|
||||||
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
#endif /* POLARSSL_HAVE_INT64 */
|
||||||
|
|
||||||
*sig_len = ((rsa_context *) ctx)->len;
|
*sig_len = ((rsa_context *) ctx)->len;
|
||||||
|
|
||||||
return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
|
return( rsa_pkcs1_sign( (rsa_context *) ctx, f_rng, p_rng, RSA_PRIVATE,
|
||||||
@ -411,6 +424,11 @@ static int rsa_alt_sign_wrap( void *ctx, md_type_t md_alg,
|
|||||||
{
|
{
|
||||||
rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
|
rsa_alt_context *rsa_alt = (rsa_alt_context *) ctx;
|
||||||
|
|
||||||
|
#if defined(POLARSSL_HAVE_INT64)
|
||||||
|
if( UINT_MAX < hash_len )
|
||||||
|
return( POLARSSL_ERR_PK_BAD_INPUT_DATA );
|
||||||
|
#endif /* POLARSSL_HAVE_INT64 */
|
||||||
|
|
||||||
*sig_len = rsa_alt->key_len_func( rsa_alt->key );
|
*sig_len = rsa_alt->key_len_func( rsa_alt->key );
|
||||||
|
|
||||||
return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
|
return( rsa_alt->sign_func( rsa_alt->key, f_rng, p_rng, RSA_PRIVATE,
|
||||||
|
Loading…
Reference in New Issue
Block a user