iPXE
tls.h
Go to the documentation of this file.
1 #ifndef _IPXE_TLS_H
2 #define _IPXE_TLS_H
3 
4 /**
5  * @file
6  *
7  * Transport Layer Security Protocol
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/refcnt.h>
14 #include <ipxe/interface.h>
15 #include <ipxe/process.h>
16 #include <ipxe/crypto.h>
17 #include <ipxe/md5.h>
18 #include <ipxe/sha1.h>
19 #include <ipxe/x509.h>
20 #include <ipxe/privkey.h>
21 #include <ipxe/pending.h>
22 #include <ipxe/iobuf.h>
23 #include <ipxe/tables.h>
24 
25 struct tls_connection;
26 
27 /** A TLS header */
28 struct tls_header {
29  /** Content type
30  *
31  * This is a TLS_TYPE_XXX constant
32  */
34  /** Protocol version
35  *
36  * This is a TLS_VERSION_XXX constant
37  */
39  /** Length of payload */
41 } __attribute__ (( packed ));
42 
43 /** TLS version 1.1 */
44 #define TLS_VERSION_TLS_1_1 0x0302
45 
46 /** TLS version 1.2 */
47 #define TLS_VERSION_TLS_1_2 0x0303
48 
49 /** Maximum supported TLS version */
50 #define TLS_VERSION_MAX TLS_VERSION_TLS_1_2
51 
52 /** Change cipher content type */
53 #define TLS_TYPE_CHANGE_CIPHER 20
54 
55 /** Change cipher spec magic byte */
56 #define TLS_CHANGE_CIPHER_SPEC 1
57 
58 /** Alert content type */
59 #define TLS_TYPE_ALERT 21
60 
61 /** Handshake content type */
62 #define TLS_TYPE_HANDSHAKE 22
63 
64 /** Application data content type */
65 #define TLS_TYPE_DATA 23
66 
67 /* Handshake message types */
68 #define TLS_HELLO_REQUEST 0
69 #define TLS_CLIENT_HELLO 1
70 #define TLS_SERVER_HELLO 2
71 #define TLS_NEW_SESSION_TICKET 4
72 #define TLS_CERTIFICATE 11
73 #define TLS_SERVER_KEY_EXCHANGE 12
74 #define TLS_CERTIFICATE_REQUEST 13
75 #define TLS_SERVER_HELLO_DONE 14
76 #define TLS_CERTIFICATE_VERIFY 15
77 #define TLS_CLIENT_KEY_EXCHANGE 16
78 #define TLS_FINISHED 20
79 
80 /* TLS alert levels */
81 #define TLS_ALERT_WARNING 1
82 #define TLS_ALERT_FATAL 2
83 
84 /* TLS cipher specifications */
85 #define TLS_RSA_WITH_NULL_MD5 0x0001
86 #define TLS_RSA_WITH_NULL_SHA 0x0002
87 #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002f
88 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033
89 #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035
90 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039
91 #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003c
92 #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003d
93 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067
94 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006b
95 #define TLS_RSA_WITH_AES_128_GCM_SHA256 0x009c
96 #define TLS_RSA_WITH_AES_256_GCM_SHA384 0x009d
97 #define TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009e
98 #define TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009f
99 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xc013
100 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xc014
101 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xc027
102 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xc028
103 #define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xc02f
104 #define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xc030
105 
106 /* TLS hash algorithm identifiers */
107 #define TLS_MD5_ALGORITHM 1
108 #define TLS_SHA1_ALGORITHM 2
109 #define TLS_SHA224_ALGORITHM 3
110 #define TLS_SHA256_ALGORITHM 4
111 #define TLS_SHA384_ALGORITHM 5
112 #define TLS_SHA512_ALGORITHM 6
113 
114 /* TLS signature algorithm identifiers */
115 #define TLS_RSA_ALGORITHM 1
116 
117 /* TLS server name extension */
118 #define TLS_SERVER_NAME 0
119 #define TLS_SERVER_NAME_HOST_NAME 0
120 
121 /* TLS maximum fragment length extension */
122 #define TLS_MAX_FRAGMENT_LENGTH 1
123 #define TLS_MAX_FRAGMENT_LENGTH_512 1
124 #define TLS_MAX_FRAGMENT_LENGTH_1024 2
125 #define TLS_MAX_FRAGMENT_LENGTH_2048 3
126 #define TLS_MAX_FRAGMENT_LENGTH_4096 4
127 
128 /* TLS named curve extension */
129 #define TLS_NAMED_CURVE 10
130 #define TLS_NAMED_CURVE_X25519 29
131 
132 /* TLS signature algorithms extension */
133 #define TLS_SIGNATURE_ALGORITHMS 13
134 
135 /* TLS session ticket extension */
136 #define TLS_SESSION_TICKET 35
137 
138 /* TLS renegotiation information extension */
139 #define TLS_RENEGOTIATION_INFO 0xff01
140 
141 /** TLS authentication header */
143  /** Sequence number */
145  /** TLS header */
147 } __attribute__ (( packed ));
148 
149 /** TLS verification data */
151  /** Client verification data */
153  /** Server verification data */
155 } __attribute__ (( packed ));
156 
157 /** TLS RX state machine state */
161 };
162 
163 /** TLS TX pending flags */
170  TLS_TX_FINISHED = 0x0020,
171 };
172 
173 /** A TLS key exchange algorithm */
175  /** Algorithm name */
176  const char *name;
177  /**
178  * Transmit Client Key Exchange record
179  *
180  * @v tls TLS connection
181  * @ret rc Return status code
182  */
183  int ( * exchange ) ( struct tls_connection *tls );
184 };
185 
186 /** A TLS cipher suite */
188  /** Key exchange algorithm */
190  /** Public-key encryption algorithm */
192  /** Bulk encryption cipher algorithm */
194  /** MAC digest algorithm */
196  /** Handshake digest algorithm (for TLSv1.2 and above) */
198  /** Numeric code (in network-endian order) */
200  /** Key length */
202  /** Fixed initialisation vector length */
204  /** Record initialisation vector length */
206  /** MAC length */
208 };
209 
210 /** TLS cipher suite table */
211 #define TLS_CIPHER_SUITES \
212  __table ( struct tls_cipher_suite, "tls_cipher_suites" )
213 
214 /** Declare a TLS cipher suite */
215 #define __tls_cipher_suite( pref ) \
216  __table_entry ( TLS_CIPHER_SUITES, pref )
217 
218 /** TLS named curved type */
219 #define TLS_NAMED_CURVE_TYPE 3
220 
221 /** A TLS named curve */
223  /** Elliptic curve */
225  /** Numeric code (in network-endian order) */
227 };
228 
229 /** TLS named curve table */
230 #define TLS_NAMED_CURVES \
231  __table ( struct tls_named_curve, "tls_named_curves" )
232 
233 /** Declare a TLS named curve */
234 #define __tls_named_curve( pref ) \
235  __table_entry ( TLS_NAMED_CURVES, pref )
236 
237 /** A TLS cipher specification */
239  /** Cipher suite */
241  /** Dynamically-allocated storage */
242  void *dynamic;
243  /** Public key encryption context */
244  void *pubkey_ctx;
245  /** Bulk encryption cipher context */
246  void *cipher_ctx;
247  /** MAC secret */
248  void *mac_secret;
249  /** Fixed initialisation vector */
250  void *fixed_iv;
251 };
252 
253 /** A TLS signature and hash algorithm identifier */
255  /** Hash algorithm */
257  /** Signature algorithm */
259 } __attribute__ (( packed ));
260 
261 /** A TLS signature algorithm */
263  /** Digest algorithm */
265  /** Public-key algorithm */
267  /** Numeric code */
269 };
270 
271 /** TLS signature hash algorithm table
272  *
273  * Note that the default (TLSv1.1 and earlier) algorithm using
274  * MD5+SHA1 is never explicitly specified.
275  */
276 #define TLS_SIG_HASH_ALGORITHMS \
277  __table ( struct tls_signature_hash_algorithm, \
278  "tls_sig_hash_algorithms" )
279 
280 /** Declare a TLS signature hash algorithm */
281 #define __tls_sig_hash_algorithm \
282  __table_entry ( TLS_SIG_HASH_ALGORITHMS, 01 )
283 
284 /** TLS client random data */
286  /** GMT Unix time */
288  /** Random data */
290 } __attribute__ (( packed ));
291 
292 /** An MD5+SHA1 context */
294  /** MD5 context */
296  /** SHA-1 context */
298 } __attribute__ (( packed ));
299 
300 /** MD5+SHA1 context size */
301 #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
302 
303 /** An MD5+SHA1 digest */
305  /** MD5 digest */
307  /** SHA-1 digest */
309 } __attribute__ (( packed ));
310 
311 /** MD5+SHA1 digest size */
312 #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
313 
314 /** A TLS session */
315 struct tls_session {
316  /** Reference counter */
317  struct refcnt refcnt;
318  /** List of sessions */
319  struct list_head list;
320 
321  /** Server name */
322  const char *name;
323  /** Root of trust */
324  struct x509_root *root;
325  /** Private key */
326  struct private_key *key;
327 
328  /** Session ID */
329  uint8_t id[32];
330  /** Length of session ID */
331  size_t id_len;
332  /** Session ticket */
333  void *ticket;
334  /** Length of session ticket */
335  size_t ticket_len;
336  /** Master secret */
338 
339  /** List of connections */
340  struct list_head conn;
341 };
342 
343 /** A TLS connection */
345  /** Reference counter */
346  struct refcnt refcnt;
347 
348  /** Session */
350  /** List of connections within the same session */
351  struct list_head list;
352  /** Session ID */
354  /** Length of session ID */
356  /** New session ticket */
358  /** Length of new session ticket */
360 
361  /** Plaintext stream */
363  /** Ciphertext stream */
365 
366  /** Protocol version */
368  /** Current TX cipher specification */
370  /** Next TX cipher specification */
372  /** Current RX cipher specification */
374  /** Next RX cipher specification */
376  /** Master secret */
378  /** Server random bytes */
380  /** Client random bytes */
382  /** Server Key Exchange record (if any) */
383  void *server_key;
384  /** Server Key Exchange record length */
386  /** Digest algorithm used for handshake verification */
388  /** Digest algorithm context used for handshake verification */
390  /** Private key */
391  struct private_key *key;
392  /** Client certificate chain (if used) */
393  struct x509_chain *certs;
394  /** Secure renegotiation flag */
396  /** Verification data */
398 
399  /** Root of trust */
400  struct x509_root *root;
401  /** Server certificate chain */
402  struct x509_chain *chain;
403  /** Certificate validator */
405 
406  /** Client security negotiation pending operation */
408  /** Server security negotiation pending operation */
410  /** Certificate validation pending operation */
412 
413  /** TX sequence number */
415  /** TX pending transmissions */
416  unsigned int tx_pending;
417  /** TX process */
418  struct process process;
419 
420  /** RX sequence number */
422  /** RX state */
424  /** Current received record header */
426  /** Current received record header (static I/O buffer) */
428  /** List of received data buffers */
430  /** Received handshake fragment */
432 };
433 
434 /** RX I/O buffer size
435  *
436  * The maximum fragment length extension is optional, and many common
437  * implementations (including OpenSSL) do not support it. We must
438  * therefore be prepared to receive records of up to 16kB in length.
439  * The chance of an allocation of this size failing is non-negligible,
440  * so we must split received data into smaller allocations.
441  */
442 #define TLS_RX_BUFSIZE 4096
443 
444 /** Minimum RX I/O buffer size
445  *
446  * To simplify manipulations, we ensure that no RX I/O buffer is
447  * smaller than this size. This allows us to assume that the MAC and
448  * padding are entirely contained within the final I/O buffer.
449  */
450 #define TLS_RX_MIN_BUFSIZE 512
451 
452 /** RX I/O buffer alignment */
453 #define TLS_RX_ALIGN 16
454 
458 
459 extern int add_tls ( struct interface *xfer, const char *name,
460  struct x509_root *root, struct private_key *key );
461 
462 #endif /* _IPXE_TLS_H */
struct tls_verify_data verify
Verification data.
Definition: tls.h:397
A process.
Definition: process.h:17
#define __attribute__(x)
Definition: compiler.h:10
struct digest_algorithm * digest
Digest algorithm.
Definition: tls.h:264
uint64_t seq
Sequence number.
Definition: tls.h:144
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
struct x509_chain * chain
Server certificate chain.
Definition: tls.h:402
An MD5+SHA1 context.
Definition: tls.h:293
uint8_t sha1[SHA1_DIGEST_SIZE]
SHA-1 digest.
Definition: tls.h:308
struct pending_operation client_negotiation
Client security negotiation pending operation.
Definition: tls.h:407
struct tls_session * session
Session.
Definition: tls.h:349
uint8_t master_secret[48]
Master secret.
Definition: tls.h:337
struct tls_key_exchange_algorithm * exchange
Key exchange algorithm.
Definition: tls.h:189
uint8_t md5[MD5_DIGEST_SIZE]
MD5 digest.
Definition: tls.h:306
struct io_buffer rx_header_iobuf
Current received record header (static I/O buffer)
Definition: tls.h:427
uint8_t record_iv_len
Record initialisation vector length.
Definition: tls.h:205
struct stp_switch root
Root switch.
Definition: stp.h:26
uint64_t rx_seq
RX sequence number.
Definition: tls.h:421
uint8_t type
Content type.
Definition: tls.h:33
I/O buffers.
Definition: b44.h:369
struct tls_key_exchange_algorithm tls_dhe_exchange_algorithm
Ephemeral Diffie-Hellman key exchange algorithm.
Definition: tls.c:1661
size_t new_session_ticket_len
Length of new session ticket.
Definition: tls.h:359
uint8_t server_random[32]
Server random bytes.
Definition: tls.h:379
uint8_t session_id[32]
Session ID.
Definition: tls.h:353
struct tls_key_exchange_algorithm tls_pubkey_exchange_algorithm
Public key exchange algorithm.
Definition: tls.c:1447
uint16_t length
Length of payload.
Definition: tls.h:40
struct x509_root * root
Root of trust.
Definition: tls.h:400
unsigned long long uint64_t
Definition: stdint.h:13
Cryptographic API.
TLS authentication header.
Definition: tls.h:142
A TLS cipher specification.
Definition: tls.h:238
const char * name
Algorithm name.
Definition: tls.h:176
struct tls_key_exchange_algorithm tls_ecdhe_exchange_algorithm
Ephemeral Elliptic Curve Diffie-Hellman key exchange algorithm.
Definition: tls.c:1774
struct pubkey_algorithm * pubkey
Public-key encryption algorithm.
Definition: tls.h:191
A doubly-linked list entry (or list head)
Definition: list.h:18
A reference counter.
Definition: refcnt.h:26
A certificate validator.
Definition: validator.c:64
Private key.
enum tls_rx_state rx_state
RX state.
Definition: tls.h:423
An X.509 certificate chain.
Definition: x509.h:199
uint8_t * handshake_ctx
Digest algorithm context used for handshake verification.
Definition: tls.h:389
struct tls_cipherspec tx_cipherspec
Current TX cipher specification.
Definition: tls.h:369
int(* exchange)(struct tls_connection *tls)
Transmit Client Key Exchange record.
Definition: tls.h:183
size_t id_len
Length of session ID.
Definition: tls.h:331
An object interface.
Definition: interface.h:124
void * new_session_ticket
New session ticket.
Definition: tls.h:357
void * cipher_ctx
Bulk encryption cipher context.
Definition: tls.h:246
tls_tx_pending
TLS TX pending flags.
Definition: tls.h:164
Object interfaces.
struct tls_cipher_suite * suite
Cipher suite.
Definition: tls.h:240
struct x509_chain * certs
Client certificate chain (if used)
Definition: tls.h:393
TLS verification data.
Definition: tls.h:150
struct digest_algorithm * digest
MAC digest algorithm.
Definition: tls.h:195
struct pending_operation validation
Certificate validation pending operation.
Definition: tls.h:411
struct list_head list
List of connections within the same session.
Definition: tls.h:351
uint32_t gmt_unix_time
GMT Unix time.
Definition: tls.h:287
uint8_t fixed_iv_len
Fixed initialisation vector length.
Definition: tls.h:203
A TLS cipher suite.
Definition: tls.h:187
A TLS signature algorithm.
Definition: tls.h:262
size_t ticket_len
Length of session ticket.
Definition: tls.h:335
uint8_t master_secret[48]
Master secret.
Definition: tls.h:377
struct list_head list
List of sessions.
Definition: tls.h:319
struct tls_header header
TLS header.
Definition: tls.h:146
struct tls_client_random client_random
Client random bytes.
Definition: tls.h:381
struct interface cipherstream
Ciphertext stream.
Definition: tls.h:364
void * server_key
Server Key Exchange record (if any)
Definition: tls.h:383
uint8_t hash
Hash algorithm.
Definition: tls.h:256
void * ticket
Session ticket.
Definition: tls.h:333
struct elliptic_curve * curve
Elliptic curve.
Definition: tls.h:224
#define MD5_CTX_SIZE
MD5 context size.
Definition: md5.h:66
uint8_t mac_len
MAC length.
Definition: tls.h:207
void * pubkey_ctx
Public key encryption context.
Definition: tls.h:244
Processes.
unsigned char uint8_t
Definition: stdint.h:10
uint8_t signature
Signature algorithm.
Definition: tls.h:258
struct private_key * key
Private key.
Definition: tls.h:391
X.509 certificates.
unsigned int uint32_t
Definition: stdint.h:12
struct tls_cipherspec rx_cipherspec
Current RX cipher specification.
Definition: tls.h:373
int add_tls(struct interface *xfer, const char *name, struct x509_root *root, struct private_key *key)
Add TLS on an interface.
Definition: tls.c:3816
struct tls_signature_hash_id code
Numeric code.
Definition: tls.h:268
struct tls_cipherspec tx_cipherspec_pending
Next TX cipher specification.
Definition: tls.h:371
uint8_t client[12]
Client verification data.
Definition: tls.h:152
struct pending_operation server_negotiation
Server security negotiation pending operation.
Definition: tls.h:409
An MD5+SHA1 digest.
Definition: tls.h:304
An X.509 root certificate list.
Definition: x509.h:366
A TLS header.
Definition: tls.h:28
Pending operations.
struct list_head rx_data
List of received data buffers.
Definition: tls.h:429
unsigned int tx_pending
TX pending transmissions.
Definition: tls.h:416
struct digest_algorithm * handshake_digest
Digest algorithm used for handshake verification.
Definition: tls.h:387
uint8_t random[28]
Random data.
Definition: tls.h:289
uint16_t code
Numeric code (in network-endian order)
Definition: tls.h:226
An elliptic curve.
Definition: crypto.h:199
A TLS session.
Definition: tls.h:315
#define SHA1_DIGEST_SIZE
Definition: Tpm20.h:25
SHA-1 algorithm.
uint8_t sha1[SHA1_CTX_SIZE]
SHA-1 context.
Definition: tls.h:297
struct digest_algorithm * handshake
Handshake digest algorithm (for TLSv1.2 and above)
Definition: tls.h:197
struct tls_cipherspec rx_cipherspec_pending
Next RX cipher specification.
Definition: tls.h:375
tls_rx_state
TLS RX state machine state.
Definition: tls.h:158
#define SHA1_CTX_SIZE
SHA-1 context size.
Definition: sha1.h:66
uint16_t version
Protocol version.
Definition: tls.h:38
A message digest algorithm.
Definition: crypto.h:17
Reference counting.
uint16_t version
Protocol version.
Definition: tls.h:367
A cipher algorithm.
Definition: crypto.h:49
A private key.
Definition: privkey.h:16
uint8_t server[12]
Server verification data.
Definition: tls.h:154
struct io_buffer * rx_handshake
Received handshake fragment.
Definition: tls.h:431
Linker tables.
A TLS key exchange algorithm.
Definition: tls.h:174
struct x509_root * root
Root of trust.
Definition: tls.h:324
A TLS connection.
Definition: tls.h:344
struct private_key * key
Private key.
Definition: tls.h:326
#define MD5_DIGEST_SIZE
MD5 digest size.
Definition: md5.h:72
A pending operation.
Definition: pending.h:13
uint8_t md5[MD5_CTX_SIZE]
MD5 context.
Definition: tls.h:295
struct cipher_algorithm * cipher
Bulk encryption cipher algorithm.
Definition: tls.h:193
struct list_head conn
List of connections.
Definition: tls.h:340
int secure_renegotiation
Secure renegotiation flag.
Definition: tls.h:395
A TLS named curve.
Definition: tls.h:222
struct pubkey_algorithm * pubkey
Public-key algorithm.
Definition: tls.h:266
void * dynamic
Dynamically-allocated storage.
Definition: tls.h:242
const char * name
Server name.
Definition: tls.h:322
A TLS signature and hash algorithm identifier.
Definition: tls.h:254
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
size_t session_id_len
Length of session ID.
Definition: tls.h:355
struct interface plainstream
Plaintext stream.
Definition: tls.h:362
MD5 algorithm.
TLS client random data.
Definition: tls.h:285
A public key algorithm.
Definition: crypto.h:120
void * fixed_iv
Fixed initialisation vector.
Definition: tls.h:250
union @382 key
Sense key.
Definition: crypto.h:284
uint16_t code
Numeric code (in network-endian order)
Definition: tls.h:199
void * mac_secret
MAC secret.
Definition: tls.h:248
uint64_t tx_seq
TX sequence number.
Definition: tls.h:414
size_t server_key_len
Server Key Exchange record length.
Definition: tls.h:385
A persistent I/O buffer.
Definition: iobuf.h:33
uint8_t key_len
Key length.
Definition: tls.h:201