mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-10-30 19:20:40 -04:00 
			
		
		
		
	Merge pull request #873 from hanno-arm/ssl_write_client_hello
Bounds checks in ssl_write_client_hello
This commit is contained in:
		
						commit
						a92e3def48
					
				
							
								
								
									
										9
									
								
								ChangeLog.d/uniformize_bounds_checks.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								ChangeLog.d/uniformize_bounds_checks.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | ||||
| Bugfix | ||||
|    * Add additional bounds checks in ssl_write_client_hello() preventing | ||||
|      output buffer overflow if the configuration declared a buffer that was | ||||
|      too small. | ||||
| Changes | ||||
|    * Abort the ClientHello writing function as soon as some extension doesn't | ||||
|      fit into the record buffer. Previously, such extensions were silently | ||||
|      dropped. As a consequence, the TLS handshake now fails when the output | ||||
|      buffer is not large enough to hold the ClientHello. | ||||
| @ -101,7 +101,7 @@ | ||||
|  * ECP       4   10 (Started from top) | ||||
|  * MD        5   5 | ||||
|  * HKDF      5   1 (Started from top) | ||||
|  * SSL       5   1 (Started from 0x5F00) | ||||
|  * SSL       5   2 (Started from 0x5F00) | ||||
|  * CIPHER    6   8 (Started from 0x6080) | ||||
|  * SSL       6   24 (Started from top, plus 0x6000) | ||||
|  * SSL       7   32 | ||||
|  | ||||
| @ -129,6 +129,7 @@ | ||||
| #define MBEDTLS_ERR_SSL_UNEXPECTED_CID                    -0x6000  /**< An encrypted DTLS-frame with an unexpected CID was received. */ | ||||
| #define MBEDTLS_ERR_SSL_VERSION_MISMATCH                  -0x5F00  /**< An operation failed due to an unexpected version or configuration. */ | ||||
| #define MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS                -0x7000  /**< A cryptographic operation is in progress. Try again later. */ | ||||
| #define MBEDTLS_ERR_SSL_BAD_CONFIG                        -0x5E80  /**< Invalid value in SSL config */ | ||||
| 
 | ||||
| /*
 | ||||
|  * Various constants | ||||
| @ -144,6 +145,9 @@ | ||||
| #define MBEDTLS_SSL_TRANSPORT_DATAGRAM          1   /*!< DTLS     */ | ||||
| 
 | ||||
| #define MBEDTLS_SSL_MAX_HOST_NAME_LEN           255 /*!< Maximum host name defined in RFC 1035 */ | ||||
| #define MBEDTLS_SSL_MAX_ALPN_NAME_LEN           255 /*!< Maximum size in bytes of a protocol name in alpn ext., RFC 7301 */ | ||||
| 
 | ||||
| #define MBEDTLS_SSL_MAX_ALPN_LIST_LEN           65535 /*!< Maximum size in bytes of list in alpn ext., RFC 7301          */ | ||||
| 
 | ||||
| /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
 | ||||
|  * NONE must be zero so that memset()ing structure to zero works */ | ||||
|  | ||||
| @ -207,6 +207,12 @@ | ||||
|         : ( MBEDTLS_SSL_IN_CONTENT_LEN )                             \ | ||||
|         ) | ||||
| 
 | ||||
| /* Maximum size in bytes of list in sig-hash algorithm ext., RFC 5246 */ | ||||
| #define MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN  65534 | ||||
| 
 | ||||
| /* Maximum size in bytes of list in supported elliptic curve ext., RFC 4492 */ | ||||
| #define MBEDTLS_SSL_MAX_CURVE_LIST_LEN         65535 | ||||
| 
 | ||||
| /*
 | ||||
|  * Check that we obey the standard's message size bounds | ||||
|  */ | ||||
| @ -299,6 +305,41 @@ static inline uint32_t mbedtls_ssl_get_input_buflen( const mbedtls_ssl_context * | ||||
| #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0) | ||||
| #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK                 (1 << 1) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief        This function checks if the remaining size in a buffer is | ||||
|  *               greater or equal than a needed space. | ||||
|  * | ||||
|  * \param cur    Pointer to the current position in the buffer. | ||||
|  * \param end    Pointer to one past the end of the buffer. | ||||
|  * \param need   Needed space in bytes. | ||||
|  * | ||||
|  * \return       Zero if the needed space is available in the buffer, non-zero | ||||
|  *               otherwise. | ||||
|  */ | ||||
| static inline int mbedtls_ssl_chk_buf_ptr( const uint8_t *cur, | ||||
|                                            const uint8_t *end, size_t need ) | ||||
| { | ||||
|     return( ( cur > end ) || ( need > (size_t)( end - cur ) ) ); | ||||
| } | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief        This macro checks if the remaining size in a buffer is | ||||
|  *               greater or equal than a needed space. If it is not the case, | ||||
|  *               it returns an SSL_BUFFER_TOO_SMALL error. | ||||
|  * | ||||
|  * \param cur    Pointer to the current position in the buffer. | ||||
|  * \param end    Pointer to one past the end of the buffer. | ||||
|  * \param need   Needed space in bytes. | ||||
|  * | ||||
|  */ | ||||
| #define MBEDTLS_SSL_CHK_BUF_PTR( cur, end, need )                        \ | ||||
|     do {                                                                 \ | ||||
|         if( mbedtls_ssl_chk_buf_ptr( ( cur ), ( end ), ( need ) ) != 0 ) \ | ||||
|         {                                                                \ | ||||
|             return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );                  \ | ||||
|         }                                                                \ | ||||
|     } while( 0 ) | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| @ -526,6 +526,8 @@ const char * mbedtls_high_level_strerr( int error_code ) | ||||
|             return( "SSL - An operation failed due to an unexpected version or configuration" ); | ||||
|         case -(MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS): | ||||
|             return( "SSL - A cryptographic operation is in progress. Try again later" ); | ||||
|         case -(MBEDTLS_ERR_SSL_BAD_CONFIG): | ||||
|             return( "SSL - Invalid value in SSL config" ); | ||||
| #endif /* MBEDTLS_SSL_TLS_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C) | ||||
|  | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @ -134,8 +134,7 @@ static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx, | ||||
| { | ||||
|     unsigned char hmac_out[COOKIE_MD_OUTLEN]; | ||||
| 
 | ||||
|     if( (size_t)( end - *p ) < COOKIE_HMAC_LEN ) | ||||
|         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
|     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN ); | ||||
| 
 | ||||
|     if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 || | ||||
|         mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 || | ||||
| @ -165,8 +164,7 @@ int mbedtls_ssl_cookie_write( void *p_ctx, | ||||
|     if( ctx == NULL || cli_id == NULL ) | ||||
|         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( (size_t)( end - *p ) < COOKIE_LEN ) | ||||
|         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
|     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_HAVE_TIME) | ||||
|     t = (unsigned long) mbedtls_time( NULL ); | ||||
|  | ||||
| @ -35,6 +35,7 @@ | ||||
| #define mbedtls_free      free | ||||
| #endif | ||||
| 
 | ||||
| #include "mbedtls/ssl_internal.h" | ||||
| #include "mbedtls/ssl_ticket.h" | ||||
| #include "mbedtls/error.h" | ||||
| #include "mbedtls/platform_util.h" | ||||
| @ -224,8 +225,7 @@ int mbedtls_ssl_ticket_write( void *p_ticket, | ||||
| 
 | ||||
|     /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
 | ||||
|      * in addition to session itself, that will be checked when writing it. */ | ||||
|     if( end - start < TICKET_MIN_LEN ) | ||||
|         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); | ||||
|     MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) | ||||
|  | ||||
| @ -4665,7 +4665,9 @@ int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **prot | ||||
|         cur_len = strlen( *p ); | ||||
|         tot_len += cur_len; | ||||
| 
 | ||||
|         if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) | ||||
|         if( ( cur_len == 0 ) || | ||||
|             ( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) || | ||||
|             ( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) ) | ||||
|             return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Manuel Pégourié-Gonnard
						Manuel Pégourié-Gonnard