| 
				
			 | 
			
			
				
				@@ -40,6 +40,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); 
			 | 
		
		
	
		
			
			| 
				40
			 | 
			
				40
			 | 
			
			
				
				 #include <ipxe/open.h> 
			 | 
		
		
	
		
			
			| 
				41
			 | 
			
				41
			 | 
			
			
				
				 #include <ipxe/asn1.h> 
			 | 
		
		
	
		
			
			| 
				42
			 | 
			
				42
			 | 
			
			
				
				 #include <ipxe/x509.h> 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				43
			 | 
			
			
				
				+#include <ipxe/rbg.h> 
			 | 
		
		
	
		
			
			| 
				43
			 | 
			
				44
			 | 
			
			
				
				 #include <ipxe/tls.h> 
			 | 
		
		
	
		
			
			| 
				44
			 | 
			
				45
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				45
			 | 
			
				46
			 | 
			
			
				
				 static int tls_send_plaintext ( struct tls_session *tls, unsigned int type, 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -121,12 +122,25 @@ static void tls_close ( struct tls_session *tls, int rc ) { 
			 | 
		
		
	
		
			
			| 
				121
			 | 
			
				122
			 | 
			
			
				
				 /** 
			 | 
		
		
	
		
			
			| 
				122
			 | 
			
				123
			 | 
			
			
				
				  * Generate random data 
			 | 
		
		
	
		
			
			| 
				123
			 | 
			
				124
			 | 
			
			
				
				  * 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				125
			 | 
			
			
				
				+ * @v tls		TLS session 
			 | 
		
		
	
		
			
			| 
				124
			 | 
			
				126
			 | 
			
			
				
				  * @v data		Buffer to fill 
			 | 
		
		
	
		
			
			| 
				125
			 | 
			
				127
			 | 
			
			
				
				  * @v len		Length of buffer 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				128
			 | 
			
			
				
				+ * @ret rc		Return status code 
			 | 
		
		
	
		
			
			| 
				126
			 | 
			
				129
			 | 
			
			
				
				  */ 
			 | 
		
		
	
		
			
			| 
				127
			 | 
			
				
			 | 
			
			
				
				-static void tls_generate_random ( void *data, size_t len ) { 
			 | 
		
		
	
		
			
			| 
				128
			 | 
			
				
			 | 
			
			
				
				-	/* FIXME: Some real random data source would be nice... */ 
			 | 
		
		
	
		
			
			| 
				129
			 | 
			
				
			 | 
			
			
				
				-	memset ( data, 0x01, len ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				130
			 | 
			
			
				
				+static int tls_generate_random ( struct tls_session *tls, 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				131
			 | 
			
			
				
				+				 void *data, size_t len ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				132
			 | 
			
			
				
				+	int rc; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				133
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				134
			 | 
			
			
				
				+	/* Generate random bits with no additional input and without 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				135
			 | 
			
			
				
				+	 * prediction resistance 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				136
			 | 
			
			
				
				+	 */ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				137
			 | 
			
			
				
				+	if ( ( rc = rbg_generate ( NULL, 0, 0, data, len ) ) != 0 ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				138
			 | 
			
			
				
				+		DBGC ( tls, "TLS %p could not generate random data: %s\n", 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				139
			 | 
			
			
				
				+		       tls, strerror ( rc ) ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				140
			 | 
			
			
				
				+		return rc; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				141
			 | 
			
			
				
				+	} 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				142
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				143
			 | 
			
			
				
				+	return 0; 
			 | 
		
		
	
		
			
			| 
				130
			 | 
			
				144
			 | 
			
			
				
				 } 
			 | 
		
		
	
		
			
			| 
				131
			 | 
			
				145
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				132
			 | 
			
				146
			 | 
			
			
				
				 /** 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -1782,11 +1796,14 @@ static struct process_descriptor tls_process_desc = 
			 | 
		
		
	
		
			
			| 
				1782
			 | 
			
				1796
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				1783
			 | 
			
				1797
			 | 
			
			
				
				 int add_tls ( struct interface *xfer, struct interface **next ) { 
			 | 
		
		
	
		
			
			| 
				1784
			 | 
			
				1798
			 | 
			
			
				
				 	struct tls_session *tls; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1799
			 | 
			
			
				
				+	int rc; 
			 | 
		
		
	
		
			
			| 
				1785
			 | 
			
				1800
			 | 
			
			
				
				  
			 | 
		
		
	
		
			
			| 
				1786
			 | 
			
				1801
			 | 
			
			
				
				 	/* Allocate and initialise TLS structure */ 
			 | 
		
		
	
		
			
			| 
				1787
			 | 
			
				1802
			 | 
			
			
				
				 	tls = malloc ( sizeof ( *tls ) ); 
			 | 
		
		
	
		
			
			| 
				1788
			 | 
			
				
			 | 
			
			
				
				-	if ( ! tls ) 
			 | 
		
		
	
		
			
			| 
				1789
			 | 
			
				
			 | 
			
			
				
				-		return -ENOMEM; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1803
			 | 
			
			
				
				+	if ( ! tls ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1804
			 | 
			
			
				
				+		rc = -ENOMEM; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1805
			 | 
			
			
				
				+		goto err_alloc; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1806
			 | 
			
			
				
				+	} 
			 | 
		
		
	
		
			
			| 
				1790
			 | 
			
				1807
			 | 
			
			
				
				 	memset ( tls, 0, sizeof ( *tls ) ); 
			 | 
		
		
	
		
			
			| 
				1791
			 | 
			
				1808
			 | 
			
			
				
				 	ref_init ( &tls->refcnt, free_tls ); 
			 | 
		
		
	
		
			
			| 
				1792
			 | 
			
				1809
			 | 
			
			
				
				 	intf_init ( &tls->plainstream, &tls_plainstream_desc, &tls->refcnt ); 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -1796,11 +1813,15 @@ int add_tls ( struct interface *xfer, struct interface **next ) { 
			 | 
		
		
	
		
			
			| 
				1796
			 | 
			
				1813
			 | 
			
			
				
				 	tls_clear_cipher ( tls, &tls->rx_cipherspec ); 
			 | 
		
		
	
		
			
			| 
				1797
			 | 
			
				1814
			 | 
			
			
				
				 	tls_clear_cipher ( tls, &tls->rx_cipherspec_pending ); 
			 | 
		
		
	
		
			
			| 
				1798
			 | 
			
				1815
			 | 
			
			
				
				 	tls->client_random.gmt_unix_time = 0; 
			 | 
		
		
	
		
			
			| 
				1799
			 | 
			
				
			 | 
			
			
				
				-	tls_generate_random ( &tls->client_random.random, 
			 | 
		
		
	
		
			
			| 
				1800
			 | 
			
				
			 | 
			
			
				
				-			      ( sizeof ( tls->client_random.random ) ) ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1816
			 | 
			
			
				
				+	if ( ( rc = tls_generate_random ( tls, &tls->client_random.random, 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1817
			 | 
			
			
				
				+			  ( sizeof ( tls->client_random.random ) ) ) ) != 0 ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1818
			 | 
			
			
				
				+		goto err_random; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1819
			 | 
			
			
				
				+	} 
			 | 
		
		
	
		
			
			| 
				1801
			 | 
			
				1820
			 | 
			
			
				
				 	tls->pre_master_secret.version = htons ( TLS_VERSION_TLS_1_0 ); 
			 | 
		
		
	
		
			
			| 
				1802
			 | 
			
				
			 | 
			
			
				
				-	tls_generate_random ( &tls->pre_master_secret.random, 
			 | 
		
		
	
		
			
			| 
				1803
			 | 
			
				
			 | 
			
			
				
				-			      ( sizeof ( tls->pre_master_secret.random ) ) ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1821
			 | 
			
			
				
				+	if ( ( rc = tls_generate_random ( tls, &tls->pre_master_secret.random, 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1822
			 | 
			
			
				
				+		      ( sizeof ( tls->pre_master_secret.random ) ) ) ) != 0 ) { 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1823
			 | 
			
			
				
				+		goto err_random; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1824
			 | 
			
			
				
				+	} 
			 | 
		
		
	
		
			
			| 
				1804
			 | 
			
				1825
			 | 
			
			
				
				 	digest_init ( &md5_algorithm, tls->handshake_md5_ctx ); 
			 | 
		
		
	
		
			
			| 
				1805
			 | 
			
				1826
			 | 
			
			
				
				 	digest_init ( &sha1_algorithm, tls->handshake_sha1_ctx ); 
			 | 
		
		
	
		
			
			| 
				1806
			 | 
			
				1827
			 | 
			
			
				
				 	process_init_stopped ( &tls->process, &tls_process_desc, &tls->refcnt ); 
			 | 
		
		
	
	
		
			
			| 
				
			 | 
			
			
				
				@@ -1811,4 +1832,9 @@ int add_tls ( struct interface *xfer, struct interface **next ) { 
			 | 
		
		
	
		
			
			| 
				1811
			 | 
			
				1832
			 | 
			
			
				
				 	*next = &tls->cipherstream; 
			 | 
		
		
	
		
			
			| 
				1812
			 | 
			
				1833
			 | 
			
			
				
				 	ref_put ( &tls->refcnt ); 
			 | 
		
		
	
		
			
			| 
				1813
			 | 
			
				1834
			 | 
			
			
				
				 	return 0; 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1835
			 | 
			
			
				
				+ 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1836
			 | 
			
			
				
				+ err_random: 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1837
			 | 
			
			
				
				+	ref_put ( &tls->refcnt ); 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1838
			 | 
			
			
				
				+ err_alloc: 
			 | 
		
		
	
		
			
			| 
				
			 | 
			
				1839
			 | 
			
			
				
				+	return rc; 
			 | 
		
		
	
		
			
			| 
				1814
			 | 
			
				1840
			 | 
			
			
				
				 } 
			 |