Ver código fonte

[tls] Use ANS X9.82 Approved RBG as source of random data for TLS

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anos atrás
pai
commit
75090f2abf
1 arquivos alterados com 35 adições e 9 exclusões
  1. 35
    9
      src/net/tls.c

+ 35
- 9
src/net/tls.c Ver arquivo

40
 #include <ipxe/open.h>
40
 #include <ipxe/open.h>
41
 #include <ipxe/asn1.h>
41
 #include <ipxe/asn1.h>
42
 #include <ipxe/x509.h>
42
 #include <ipxe/x509.h>
43
+#include <ipxe/rbg.h>
43
 #include <ipxe/tls.h>
44
 #include <ipxe/tls.h>
44
 
45
 
45
 static int tls_send_plaintext ( struct tls_session *tls, unsigned int type,
46
 static int tls_send_plaintext ( struct tls_session *tls, unsigned int type,
121
 /**
122
 /**
122
  * Generate random data
123
  * Generate random data
123
  *
124
  *
125
+ * @v tls		TLS session
124
  * @v data		Buffer to fill
126
  * @v data		Buffer to fill
125
  * @v len		Length of buffer
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
 
1796
 
1783
 int add_tls ( struct interface *xfer, struct interface **next ) {
1797
 int add_tls ( struct interface *xfer, struct interface **next ) {
1784
 	struct tls_session *tls;
1798
 	struct tls_session *tls;
1799
+	int rc;
1785
 
1800
 
1786
 	/* Allocate and initialise TLS structure */
1801
 	/* Allocate and initialise TLS structure */
1787
 	tls = malloc ( sizeof ( *tls ) );
1802
 	tls = malloc ( sizeof ( *tls ) );
1788
-	if ( ! tls )
1789
-		return -ENOMEM;
1803
+	if ( ! tls ) {
1804
+		rc = -ENOMEM;
1805
+		goto err_alloc;
1806
+	}
1790
 	memset ( tls, 0, sizeof ( *tls ) );
1807
 	memset ( tls, 0, sizeof ( *tls ) );
1791
 	ref_init ( &tls->refcnt, free_tls );
1808
 	ref_init ( &tls->refcnt, free_tls );
1792
 	intf_init ( &tls->plainstream, &tls_plainstream_desc, &tls->refcnt );
1809
 	intf_init ( &tls->plainstream, &tls_plainstream_desc, &tls->refcnt );
1796
 	tls_clear_cipher ( tls, &tls->rx_cipherspec );
1813
 	tls_clear_cipher ( tls, &tls->rx_cipherspec );
1797
 	tls_clear_cipher ( tls, &tls->rx_cipherspec_pending );
1814
 	tls_clear_cipher ( tls, &tls->rx_cipherspec_pending );
1798
 	tls->client_random.gmt_unix_time = 0;
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
 	tls->pre_master_secret.version = htons ( TLS_VERSION_TLS_1_0 );
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
 	digest_init ( &md5_algorithm, tls->handshake_md5_ctx );
1825
 	digest_init ( &md5_algorithm, tls->handshake_md5_ctx );
1805
 	digest_init ( &sha1_algorithm, tls->handshake_sha1_ctx );
1826
 	digest_init ( &sha1_algorithm, tls->handshake_sha1_ctx );
1806
 	process_init_stopped ( &tls->process, &tls_process_desc, &tls->refcnt );
1827
 	process_init_stopped ( &tls->process, &tls_process_desc, &tls->refcnt );
1811
 	*next = &tls->cipherstream;
1832
 	*next = &tls->cipherstream;
1812
 	ref_put ( &tls->refcnt );
1833
 	ref_put ( &tls->refcnt );
1813
 	return 0;
1834
 	return 0;
1835
+
1836
+ err_random:
1837
+	ref_put ( &tls->refcnt );
1838
+ err_alloc:
1839
+	return rc;
1814
 }
1840
 }

Carregando…
Cancelar
Salvar