Ver Fonte

update:mbedtls的luatos修改补上

alienwalker há 3 anos atrás
pai
commit
d8a62abf7d

+ 34 - 0
components/mbedtls/include/mbedtls/debug.h

@@ -38,6 +38,39 @@
 
 #define MBEDTLS_DEBUG_STRIP_PARENS( ... )   __VA_ARGS__
 
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+#define MBEDTLS_SSL_DEBUG_MSG( level, args )                    \
+    mbedtls_debug_print_msg( ssl, level, __FUNCTION__, __LINE__,    \
+                             MBEDTLS_DEBUG_STRIP_PARENS args )
+
+#define MBEDTLS_SSL_DEBUG_RET( level, text, ret )                \
+    mbedtls_debug_print_ret( ssl, level, __FUNCTION__, __LINE__, text, ret )
+
+#define MBEDTLS_SSL_DEBUG_BUF( level, text, buf, len )           \
+    mbedtls_debug_print_buf( ssl, level, __FUNCTION__, __LINE__, text, buf, len )
+
+#if defined(MBEDTLS_BIGNUM_C)
+#define MBEDTLS_SSL_DEBUG_MPI( level, text, X )                  \
+    mbedtls_debug_print_mpi( ssl, level, __FUNCTION__, __LINE__, text, X )
+#endif
+
+#if defined(MBEDTLS_ECP_C)
+#define MBEDTLS_SSL_DEBUG_ECP( level, text, X )                  \
+    mbedtls_debug_print_ecp( ssl, level, __FUNCTION__, __LINE__, text, X )
+#endif
+
+#if defined(MBEDTLS_X509_CRT_PARSE_C)
+#define MBEDTLS_SSL_DEBUG_CRT( level, text, crt )                \
+    mbedtls_debug_print_crt( ssl, level, __FUNCTION__, __LINE__, text, crt )
+#endif
+
+#if defined(MBEDTLS_ECDH_C)
+#define MBEDTLS_SSL_DEBUG_ECDH( level, ecdh, attr )               \
+    mbedtls_debug_printf_ecdh( ssl, level, __FUNCTION__, __LINE__, ecdh, attr )
+#endif
+
+#else
+
 #define MBEDTLS_SSL_DEBUG_MSG( level, args )                    \
     mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__,    \
                              MBEDTLS_DEBUG_STRIP_PARENS args )
@@ -68,6 +101,7 @@
     mbedtls_debug_printf_ecdh( ssl, level, __FILE__, __LINE__, ecdh, attr )
 #endif
 
+#endif //(defined __LUATOS__) || (defined __USER_CODE__)
 #else /* MBEDTLS_DEBUG_C */
 
 #define MBEDTLS_SSL_DEBUG_MSG( level, args )            do { } while( 0 )

+ 32 - 3
components/mbedtls/library/ctr_drbg.c

@@ -29,7 +29,9 @@
 #include "mbedtls/ctr_drbg.h"
 #include "mbedtls/platform_util.h"
 #include "mbedtls/error.h"
-
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+#include "mbedtls/platform.h"
+#endif
 #include <string.h>
 
 #if defined(MBEDTLS_FS_IO)
@@ -125,8 +127,12 @@ void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
 static int block_cipher_df( unsigned char *output,
                             const unsigned char *data, size_t data_len )
 {
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+	unsigned char *buf = NULL;
+#else
     unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT +
                       MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
+#endif
     unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
     unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
     unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
@@ -139,9 +145,12 @@ static int block_cipher_df( unsigned char *output,
 
     if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
         return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
-
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    buf = mbedtls_calloc(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16, 1);
+#endif
     memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT +
             MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
+
     mbedtls_aes_init( &aes_ctx );
 
     /*
@@ -227,7 +236,11 @@ exit:
     /*
     * tidy up the stack
     */
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    mbedtls_free(buf);
+#else
     mbedtls_platform_zeroize( buf, sizeof( buf ) );
+#endif
     mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
     mbedtls_platform_zeroize( key, sizeof( key ) );
     mbedtls_platform_zeroize( chain, sizeof( chain ) );
@@ -363,7 +376,11 @@ static int mbedtls_ctr_drbg_reseed_internal( mbedtls_ctr_drbg_context *ctx,
                                              size_t len,
                                              size_t nonce_len )
 {
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+	unsigned char *seed = NULL;
+#else
     unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
+#endif
     size_t seedlen = 0;
     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
 
@@ -373,12 +390,17 @@ static int mbedtls_ctr_drbg_reseed_internal( mbedtls_ctr_drbg_context *ctx,
         return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
     if( len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len - nonce_len )
         return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
-
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    seed = mbedtls_calloc(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, 1);
+#endif
     memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
 
     /* Gather entropy_len bytes of entropy to seed state. */
     if( 0 != ctx->f_entropy( ctx->p_entropy, seed, ctx->entropy_len ) )
     {
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    	mbedtls_free(seed);
+#endif
         return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
     }
     seedlen += ctx->entropy_len;
@@ -388,6 +410,9 @@ static int mbedtls_ctr_drbg_reseed_internal( mbedtls_ctr_drbg_context *ctx,
     {
         if( 0 != ctx->f_entropy( ctx->p_entropy, seed + seedlen, nonce_len ) )
         {
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+        	mbedtls_free(seed);
+#endif
             return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
         }
         seedlen += nonce_len;
@@ -410,7 +435,11 @@ static int mbedtls_ctr_drbg_reseed_internal( mbedtls_ctr_drbg_context *ctx,
     ctx->reseed_counter = 1;
 
 exit:
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    mbedtls_free(seed);
+#else
     mbedtls_platform_zeroize( seed, sizeof( seed ) );
+#endif
     return( ret );
 }
 

+ 23 - 6
components/mbedtls/library/debug.c

@@ -44,7 +44,11 @@
 #define inline __inline
 #endif
 
+#ifdef __SMALL_RAM___
+#define DEBUG_BUF_SIZE      96
+#else
 #define DEBUG_BUF_SIZE      512
+#endif
 
 static int debug_threshold = 0;
 
@@ -147,7 +151,9 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
     {
         return;
     }
-
+#ifdef __SMALL_RAM___
+    return;
+#endif
     mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
               text, (unsigned int) len );
 
@@ -205,7 +211,9 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
     {
         return;
     }
-
+#ifdef __SMALL_RAM___
+    return;
+#endif
     mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
     mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
 
@@ -231,7 +239,9 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
     {
         return;
     }
-
+#ifdef __SMALL_RAM___
+    return;
+#endif
     bitlen = mbedtls_mpi_bitlen( X );
 
     mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
@@ -280,7 +290,9 @@ static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
     size_t i;
     mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
     char name[16];
-
+#ifdef __SMALL_RAM___
+    return;
+#endif
     memset( items, 0, sizeof( items ) );
 
     if( mbedtls_pk_debug( pk, items ) != 0 )
@@ -351,11 +363,16 @@ void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
     {
         return;
     }
-
+#ifdef __SMALL_RAM___
+    return;
+#endif
     while( crt != NULL )
     {
+#ifdef __SMALL_RAM___
+        char buf[4];
+#else
         char buf[1024];
-
+#endif
         mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
         debug_send_line( ssl, level, file, line, str );
 

+ 5 - 2
components/mbedtls/library/ssl_cli.c

@@ -1187,10 +1187,13 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
                                       ssl->conf->min_minor_ver,
                                       ssl->conf->max_minor_ver ) != 0 )
             continue;
-
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+        MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x (%s)",
+                                            (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
+#else
         MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
                                     (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
-
+#endif
 #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
     defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
         uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );

+ 7 - 1
components/mbedtls/library/ssl_msg.c

@@ -5670,7 +5670,13 @@ int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
 
     n = ( len < ssl->in_msglen )
         ? len : ssl->in_msglen;
-
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+    if (!buf)
+    {
+    	MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
+    	return 0;
+    }
+#endif
     memcpy( buf, ssl->in_offt, n );
     ssl->in_msglen -= n;
 

+ 4 - 0
components/mbedtls/library/x509_crt.c

@@ -105,7 +105,11 @@ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
     MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
     0xFFFFFFF, /* Any PK alg    */
     0xFFFFFFF, /* Any curve     */
+#if (defined __LUATOS__) || (defined __USER_CODE__)
+	1024,
+#else
     2048,
+#endif
 };
 
 /*