7 * Transport Layer Security Protocol
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13 #include <ipxe/refcnt.h>
14 #include <ipxe/interface.h>
15 #include <ipxe/process.h>
16 #include <ipxe/crypto.h>
18 #include <ipxe/sha1.h>
19 #include <ipxe/sha256.h>
20 #include <ipxe/x509.h>
21 #include <ipxe/pending.h>
22 #include <ipxe/iobuf.h>
23 #include <ipxe/tables.h>
29 * This is a TLS_TYPE_XXX constant
34 * This is a TLS_VERSION_XXX constant
37 /** Length of payload */
39 } __attribute__ (( packed ));
41 /** TLS version 1.0 */
42 #define TLS_VERSION_TLS_1_0 0x0301
44 /** TLS version 1.1 */
45 #define TLS_VERSION_TLS_1_1 0x0302
47 /** TLS version 1.2 */
48 #define TLS_VERSION_TLS_1_2 0x0303
50 /** Change cipher content type */
51 #define TLS_TYPE_CHANGE_CIPHER 20
53 /** Alert content type */
54 #define TLS_TYPE_ALERT 21
56 /** Handshake content type */
57 #define TLS_TYPE_HANDSHAKE 22
59 /** Application data content type */
60 #define TLS_TYPE_DATA 23
62 /* Handshake message types */
63 #define TLS_HELLO_REQUEST 0
64 #define TLS_CLIENT_HELLO 1
65 #define TLS_SERVER_HELLO 2
66 #define TLS_CERTIFICATE 11
67 #define TLS_SERVER_KEY_EXCHANGE 12
68 #define TLS_CERTIFICATE_REQUEST 13
69 #define TLS_SERVER_HELLO_DONE 14
70 #define TLS_CERTIFICATE_VERIFY 15
71 #define TLS_CLIENT_KEY_EXCHANGE 16
72 #define TLS_FINISHED 20
74 /* TLS alert levels */
75 #define TLS_ALERT_WARNING 1
76 #define TLS_ALERT_FATAL 2
78 /* TLS cipher specifications */
79 #define TLS_RSA_WITH_NULL_MD5 0x0001
80 #define TLS_RSA_WITH_NULL_SHA 0x0002
81 #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002f
82 #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
83 #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003c
84 #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003d
86 /* TLS hash algorithm identifiers */
87 #define TLS_MD5_ALGORITHM 1
88 #define TLS_SHA1_ALGORITHM 2
89 #define TLS_SHA224_ALGORITHM 3
90 #define TLS_SHA256_ALGORITHM 4
91 #define TLS_SHA384_ALGORITHM 5
92 #define TLS_SHA512_ALGORITHM 6
94 /* TLS signature algorithm identifiers */
95 #define TLS_RSA_ALGORITHM 1
97 /* TLS server name extension */
98 #define TLS_SERVER_NAME 0
99 #define TLS_SERVER_NAME_HOST_NAME 0
101 /* TLS maximum fragment length extension */
102 #define TLS_MAX_FRAGMENT_LENGTH 1
103 #define TLS_MAX_FRAGMENT_LENGTH_512 1
104 #define TLS_MAX_FRAGMENT_LENGTH_1024 2
105 #define TLS_MAX_FRAGMENT_LENGTH_2048 3
106 #define TLS_MAX_FRAGMENT_LENGTH_4096 4
108 /* TLS signature algorithms extension */
109 #define TLS_SIGNATURE_ALGORITHMS 13
111 /** TLS RX state machine state */
117 /** TLS TX pending flags */
118 enum tls_tx_pending {
119 TLS_TX_CLIENT_HELLO = 0x0001,
120 TLS_TX_CERTIFICATE = 0x0002,
121 TLS_TX_CLIENT_KEY_EXCHANGE = 0x0004,
122 TLS_TX_CERTIFICATE_VERIFY = 0x0008,
123 TLS_TX_CHANGE_CIPHER = 0x0010,
124 TLS_TX_FINISHED = 0x0020,
127 /** A TLS cipher suite */
128 struct tls_cipher_suite {
129 /** Public-key encryption algorithm */
130 struct pubkey_algorithm *pubkey;
131 /** Bulk encryption cipher algorithm */
132 struct cipher_algorithm *cipher;
133 /** MAC digest algorithm */
134 struct digest_algorithm *digest;
137 /** Numeric code (in network-endian order) */
141 /** TLS cipher suite table */
142 #define TLS_CIPHER_SUITES \
143 __table ( struct tls_cipher_suite, "tls_cipher_suites" )
145 /** Declare a TLS cipher suite */
146 #define __tls_cipher_suite( pref ) \
147 __table_entry ( TLS_CIPHER_SUITES, pref )
149 /** A TLS cipher specification */
150 struct tls_cipherspec {
152 struct tls_cipher_suite *suite;
153 /** Dynamically-allocated storage */
155 /** Public key encryption context */
157 /** Bulk encryption cipher context */
159 /** Next bulk encryption cipher context (TX only) */
160 void *cipher_next_ctx;
165 /** A TLS signature and hash algorithm identifier */
166 struct tls_signature_hash_id {
167 /** Hash algorithm */
169 /** Signature algorithm */
171 } __attribute__ (( packed ));
173 /** A TLS signature algorithm */
174 struct tls_signature_hash_algorithm {
175 /** Digest algorithm */
176 struct digest_algorithm *digest;
177 /** Public-key algorithm */
178 struct pubkey_algorithm *pubkey;
180 struct tls_signature_hash_id code;
183 /** TLS signature hash algorithm table
185 * Note that the default (TLSv1.1 and earlier) algorithm using
186 * MD5+SHA1 is never explicitly specified.
188 #define TLS_SIG_HASH_ALGORITHMS \
189 __table ( struct tls_signature_hash_algorithm, \
190 "tls_sig_hash_algorithms" )
192 /** Declare a TLS signature hash algorithm */
193 #define __tls_sig_hash_algorithm \
194 __table_entry ( TLS_SIG_HASH_ALGORITHMS, 01 )
196 /** TLS pre-master secret */
197 struct tls_pre_master_secret {
202 } __attribute__ (( packed ));
204 /** TLS client random data */
205 struct tls_client_random {
207 uint32_t gmt_unix_time;
210 } __attribute__ (( packed ));
212 /** An MD5+SHA1 context */
213 struct md5_sha1_context {
215 uint8_t md5[MD5_CTX_SIZE];
217 uint8_t sha1[SHA1_CTX_SIZE];
218 } __attribute__ (( packed ));
220 /** MD5+SHA1 context size */
221 #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
223 /** An MD5+SHA1 digest */
224 struct md5_sha1_digest {
226 uint8_t md5[MD5_DIGEST_SIZE];
228 uint8_t sha1[SHA1_DIGEST_SIZE];
229 } __attribute__ (( packed ));
231 /** MD5+SHA1 digest size */
232 #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
236 /** Reference counter */
237 struct refcnt refcnt;
241 /** Plaintext stream */
242 struct interface plainstream;
243 /** Ciphertext stream */
244 struct interface cipherstream;
246 /** Protocol version */
248 /** Current TX cipher specification */
249 struct tls_cipherspec tx_cipherspec;
250 /** Next TX cipher specification */
251 struct tls_cipherspec tx_cipherspec_pending;
252 /** Current RX cipher specification */
253 struct tls_cipherspec rx_cipherspec;
254 /** Next RX cipher specification */
255 struct tls_cipherspec rx_cipherspec_pending;
256 /** Premaster secret */
257 struct tls_pre_master_secret pre_master_secret;
259 uint8_t master_secret[48];
260 /** Server random bytes */
261 uint8_t server_random[32];
262 /** Client random bytes */
263 struct tls_client_random client_random;
264 /** MD5+SHA1 context for handshake verification */
265 uint8_t handshake_md5_sha1_ctx[MD5_SHA1_CTX_SIZE];
266 /** SHA256 context for handshake verification */
267 uint8_t handshake_sha256_ctx[SHA256_CTX_SIZE];
268 /** Digest algorithm used for handshake verification */
269 struct digest_algorithm *handshake_digest;
270 /** Digest algorithm context used for handshake verification */
271 uint8_t *handshake_ctx;
272 /** Client certificate (if used) */
273 struct x509_certificate *cert;
275 /** Server certificate chain */
276 struct x509_chain *chain;
277 /** Certificate validator */
278 struct interface validator;
280 /** Client security negotiation pending operation */
281 struct pending_operation client_negotiation;
282 /** Server security negotiation pending operation */
283 struct pending_operation server_negotiation;
285 /** TX sequence number */
287 /** TX pending transmissions */
288 unsigned int tx_pending;
290 struct process process;
292 /** RX sequence number */
295 enum tls_rx_state rx_state;
296 /** Current received record header */
297 struct tls_header rx_header;
298 /** Current received record header (static I/O buffer) */
299 struct io_buffer rx_header_iobuf;
300 /** List of received data buffers */
301 struct list_head rx_data;
304 /** RX I/O buffer size
306 * The maximum fragment length extension is optional, and many common
307 * implementations (including OpenSSL) do not support it. We must
308 * therefore be prepared to receive records of up to 16kB in length.
309 * The chance of an allocation of this size failing is non-negligible,
310 * so we must split received data into smaller allocations.
312 #define TLS_RX_BUFSIZE 4096
314 /** Minimum RX I/O buffer size
316 * To simplify manipulations, we ensure that no RX I/O buffer is
317 * smaller than this size. This allows us to assume that the MAC and
318 * padding are entirely contained within the final I/O buffer.
320 #define TLS_RX_MIN_BUFSIZE 512
322 /** RX I/O buffer alignment */
323 #define TLS_RX_ALIGN 16
325 extern int add_tls ( struct interface *xfer, const char *name,
326 struct interface **next );
328 #endif /* _IPXE_TLS_H */