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_SECP256R1 23
131 #define TLS_NAMED_CURVE_SECP384R1 24
132 #define TLS_NAMED_CURVE_X25519 29
133 
134 /* TLS signature algorithms extension */
135 #define TLS_SIGNATURE_ALGORITHMS 13
136 
137 /* TLS session ticket extension */
138 #define TLS_SESSION_TICKET 35
139 
140 /* TLS renegotiation information extension */
141 #define TLS_RENEGOTIATION_INFO 0xff01
142 
143 /** TLS authentication header */
145  /** Sequence number */
147  /** TLS header */
149 } __attribute__ (( packed ));
150 
151 /** TLS verification data */
153  /** Client verification data */
155  /** Server verification data */
157 } __attribute__ (( packed ));
158 
159 /** TLS RX state machine state */
163 };
164 
165 /** TLS TX pending flags */
172  TLS_TX_FINISHED = 0x0020,
173 };
174 
175 /** A TLS key exchange algorithm */
177  /** Algorithm name */
178  const char *name;
179  /**
180  * Transmit Client Key Exchange record
181  *
182  * @v tls TLS connection
183  * @ret rc Return status code
184  */
185  int ( * exchange ) ( struct tls_connection *tls );
186 };
187 
188 /** A TLS cipher suite */
190  /** Key exchange algorithm */
192  /** Public-key encryption algorithm */
194  /** Bulk encryption cipher algorithm */
196  /** MAC digest algorithm */
198  /** Handshake digest algorithm (for TLSv1.2 and above) */
200  /** Numeric code (in network-endian order) */
202  /** Key length */
204  /** Fixed initialisation vector length */
206  /** Record initialisation vector length */
208  /** MAC length */
210 };
211 
212 /** TLS cipher suite table */
213 #define TLS_CIPHER_SUITES \
214  __table ( struct tls_cipher_suite, "tls_cipher_suites" )
215 
216 /** Declare a TLS cipher suite */
217 #define __tls_cipher_suite( pref ) \
218  __table_entry ( TLS_CIPHER_SUITES, pref )
219 
220 /** TLS named curved type */
221 #define TLS_NAMED_CURVE_TYPE 3
222 
223 /** TLS uncompressed curve point format */
224 #define TLS_POINT_FORMAT_UNCOMPRESSED 4
225 
226 /** A TLS named curve */
228  /** Elliptic curve */
230  /** Numeric code (in network-endian order) */
232  /** Curve point format byte (if any) */
234  /** Pre-master secret length */
236 };
237 
238 /** TLS named curve table */
239 #define TLS_NAMED_CURVES \
240  __table ( struct tls_named_curve, "tls_named_curves" )
241 
242 /** Declare a TLS named curve */
243 #define __tls_named_curve( pref ) \
244  __table_entry ( TLS_NAMED_CURVES, pref )
245 
246 /** A TLS cipher specification */
248  /** Cipher suite */
250  /** Dynamically-allocated storage */
251  void *dynamic;
252  /** Bulk encryption cipher context */
253  void *cipher_ctx;
254  /** MAC secret */
255  void *mac_secret;
256  /** Fixed initialisation vector */
257  void *fixed_iv;
258 };
259 
260 /** A TLS cipher specification pair */
262  /** Current cipher specification */
264  /** Next cipher specification */
266 };
267 
268 /** A TLS signature and hash algorithm identifier */
270  /** Hash algorithm */
272  /** Signature algorithm */
274 } __attribute__ (( packed ));
275 
276 /** A TLS signature algorithm */
278  /** Digest algorithm */
280  /** Public-key algorithm */
282  /** Numeric code */
284 };
285 
286 /** TLS signature hash algorithm table
287  *
288  * Note that the default (TLSv1.1 and earlier) algorithm using
289  * MD5+SHA1 is never explicitly specified.
290  */
291 #define TLS_SIG_HASH_ALGORITHMS \
292  __table ( struct tls_signature_hash_algorithm, \
293  "tls_sig_hash_algorithms" )
294 
295 /** Declare a TLS signature hash algorithm */
296 #define __tls_sig_hash_algorithm \
297  __table_entry ( TLS_SIG_HASH_ALGORITHMS, 01 )
298 
299 /** TLS client random data */
301  /** GMT Unix time */
303  /** Random data */
305 } __attribute__ (( packed ));
306 
307 /** An MD5+SHA1 context */
309  /** MD5 context */
311  /** SHA-1 context */
313 } __attribute__ (( packed ));
314 
315 /** MD5+SHA1 context size */
316 #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
317 
318 /** An MD5+SHA1 digest */
320  /** MD5 digest */
322  /** SHA-1 digest */
324 } __attribute__ (( packed ));
325 
326 /** MD5+SHA1 digest size */
327 #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
328 
329 /** A TLS session */
330 struct tls_session {
331  /** Reference counter */
332  struct refcnt refcnt;
333  /** List of sessions */
334  struct list_head list;
335 
336  /** Server name */
337  const char *name;
338  /** Root of trust */
339  struct x509_root *root;
340  /** Private key */
341  struct private_key *key;
342 
343  /** Session ID */
344  uint8_t id[32];
345  /** Length of session ID */
346  size_t id_len;
347  /** Session ticket */
348  void *ticket;
349  /** Length of session ticket */
350  size_t ticket_len;
351  /** Master secret */
353 
354  /** List of connections */
355  struct list_head conn;
356 };
357 
358 /** TLS transmit state */
359 struct tls_tx {
360  /** Cipher specifications */
362  /** Sequence number */
364  /** Pending transmissions */
365  unsigned int pending;
366  /** Transmit process */
367  struct process process;
368 };
369 
370 /** TLS receive state */
371 struct tls_rx {
372  /** Cipher specifications */
374  /** Sequence number */
376  /** State machine current state */
378  /** Current received record header */
380  /** Current received record header (static I/O buffer) */
381  struct io_buffer iobuf;
382  /** List of received data buffers */
383  struct list_head data;
384  /** Received handshake fragment */
386 };
387 
388 /** TLS client state */
389 struct tls_client {
390  /** Random bytes */
392  /** Private key (if used) */
393  struct private_key *key;
394  /** Certificate chain (if used) */
395  struct x509_chain *chain;
396  /** Security negotiation pending operation */
398 };
399 
400 /** TLS server state */
401 struct tls_server {
402  /** Random bytes */
404  /** Server Key Exchange record (if any) */
405  void *exchange;
406  /** Server Key Exchange record length */
407  size_t exchange_len;
408  /** Root of trust */
409  struct x509_root *root;
410  /** Certificate chain */
411  struct x509_chain *chain;
412  /** Public key (within server certificate) */
413  struct asn1_cursor key;
414  /** Certificate validator */
416  /** Certificate validation pending operation */
418  /** Security negotiation pending operation */
420 };
421 
422 /** A TLS connection */
424  /** Reference counter */
425  struct refcnt refcnt;
426 
427  /** Session */
429  /** List of connections within the same session */
430  struct list_head list;
431  /** Session ID */
433  /** Length of session ID */
435  /** New session ticket */
437  /** Length of new session ticket */
439 
440  /** Plaintext stream */
442  /** Ciphertext stream */
444 
445  /** Protocol version */
447  /** Master secret */
449  /** Digest algorithm used for handshake verification */
451  /** Digest algorithm context used for handshake verification */
453  /** Secure renegotiation flag */
455  /** Verification data */
457 
458  /** Transmit state */
459  struct tls_tx tx;
460  /** Receive state */
461  struct tls_rx rx;
462  /** Client state */
464  /** Server state */
466 };
467 
468 /** RX I/O buffer size
469  *
470  * The maximum fragment length extension is optional, and many common
471  * implementations (including OpenSSL) do not support it. We must
472  * therefore be prepared to receive records of up to 16kB in length.
473  * The chance of an allocation of this size failing is non-negligible,
474  * so we must split received data into smaller allocations.
475  */
476 #define TLS_RX_BUFSIZE 4096
477 
478 /** Minimum RX I/O buffer size
479  *
480  * To simplify manipulations, we ensure that no RX I/O buffer is
481  * smaller than this size. This allows us to assume that the MAC and
482  * padding are entirely contained within the final I/O buffer.
483  */
484 #define TLS_RX_MIN_BUFSIZE 512
485 
486 /** RX I/O buffer alignment */
487 #define TLS_RX_ALIGN 16
488 
492 
493 extern int add_tls ( struct interface *xfer, const char *name,
494  struct x509_root *root, struct private_key *key );
495 
496 #endif /* _IPXE_TLS_H */
struct tls_verify_data verify
Verification data.
Definition: tls.h:456
A process.
Definition: process.h:17
#define __attribute__(x)
Definition: compiler.h:10
struct tls_header header
Current received record header.
Definition: tls.h:379
struct digest_algorithm * digest
Digest algorithm.
Definition: tls.h:279
uint8_t random[32]
Random bytes.
Definition: tls.h:403
uint64_t seq
Sequence number.
Definition: tls.h:146
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
An MD5+SHA1 context.
Definition: tls.h:308
uint8_t sha1[SHA1_DIGEST_SIZE]
SHA-1 digest.
Definition: tls.h:323
struct asn1_cursor key
Public key (within server certificate)
Definition: tls.h:413
A TLS cipher specification pair.
Definition: tls.h:261
struct tls_session * session
Session.
Definition: tls.h:428
uint8_t master_secret[48]
Master secret.
Definition: tls.h:352
struct list_head data
List of received data buffers.
Definition: tls.h:383
struct tls_key_exchange_algorithm * exchange
Key exchange algorithm.
Definition: tls.h:191
uint8_t md5[MD5_DIGEST_SIZE]
MD5 digest.
Definition: tls.h:321
uint8_t record_iv_len
Record initialisation vector length.
Definition: tls.h:207
struct io_buffer * handshake
Received handshake fragment.
Definition: tls.h:385
struct stp_switch root
Root switch.
Definition: stp.h:26
struct pending_operation negotiation
Security negotiation pending operation.
Definition: tls.h:397
TLS server state.
Definition: tls.h:401
struct tls_cipherspec_pair cipherspec
Cipher specifications.
Definition: tls.h:373
uint8_t type
Content type.
Definition: tls.h:33
I/O buffers.
TLS client state.
Definition: tls.h:389
struct tls_key_exchange_algorithm tls_dhe_exchange_algorithm
Ephemeral Diffie-Hellman key exchange algorithm.
Definition: tls.c:1654
void * exchange
Server Key Exchange record (if any)
Definition: tls.h:405
size_t new_session_ticket_len
Length of new session ticket.
Definition: tls.h:438
uint8_t session_id[32]
Session ID.
Definition: tls.h:432
struct tls_key_exchange_algorithm tls_pubkey_exchange_algorithm
Public key exchange algorithm.
Definition: tls.c:1439
uint16_t length
Length of payload.
Definition: tls.h:40
unsigned long long uint64_t
Definition: stdint.h:13
Cryptographic API.
TLS authentication header.
Definition: tls.h:144
A TLS cipher specification.
Definition: tls.h:247
const char * name
Algorithm name.
Definition: tls.h:178
struct tls_key_exchange_algorithm tls_ecdhe_exchange_algorithm
Ephemeral Elliptic Curve Diffie-Hellman key exchange algorithm.
Definition: tls.c:1784
struct pubkey_algorithm * pubkey
Public-key encryption algorithm.
Definition: tls.h:193
TLS receive state.
Definition: tls.h:371
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.
struct private_key * key
Private key (if used)
Definition: tls.h:393
struct tls_server server
Server state.
Definition: tls.h:465
An X.509 certificate chain.
Definition: x509.h:200
uint8_t * handshake_ctx
Digest algorithm context used for handshake verification.
Definition: tls.h:452
int(* exchange)(struct tls_connection *tls)
Transmit Client Key Exchange record.
Definition: tls.h:185
TLS transmit state.
Definition: tls.h:359
size_t id_len
Length of session ID.
Definition: tls.h:346
An object interface.
Definition: interface.h:124
void * new_session_ticket
New session ticket.
Definition: tls.h:436
struct tls_client client
Client state.
Definition: tls.h:463
void * cipher_ctx
Bulk encryption cipher context.
Definition: tls.h:253
tls_tx_pending
TLS TX pending flags.
Definition: tls.h:166
Object interfaces.
struct tls_cipher_suite * suite
Cipher suite.
Definition: tls.h:249
TLS verification data.
Definition: tls.h:152
struct digest_algorithm * digest
MAC digest algorithm.
Definition: tls.h:197
struct list_head list
List of connections within the same session.
Definition: tls.h:430
uint32_t gmt_unix_time
GMT Unix time.
Definition: tls.h:302
uint8_t fixed_iv_len
Fixed initialisation vector length.
Definition: tls.h:205
A TLS cipher suite.
Definition: tls.h:189
A TLS signature algorithm.
Definition: tls.h:277
size_t ticket_len
Length of session ticket.
Definition: tls.h:350
uint8_t master_secret[48]
Master secret.
Definition: tls.h:448
struct list_head list
List of sessions.
Definition: tls.h:334
struct tls_cipherspec_pair cipherspec
Cipher specifications.
Definition: tls.h:361
uint8_t pre_master_secret_len
Pre-master secret length.
Definition: tls.h:235
struct tls_header header
TLS header.
Definition: tls.h:148
uint64_t seq
Sequence number.
Definition: tls.h:363
struct interface cipherstream
Ciphertext stream.
Definition: tls.h:443
struct pending_operation negotiation
Security negotiation pending operation.
Definition: tls.h:419
uint8_t hash
Hash algorithm.
Definition: tls.h:271
void * ticket
Session ticket.
Definition: tls.h:348
struct elliptic_curve * curve
Elliptic curve.
Definition: tls.h:229
struct x509_chain * chain
Certificate chain.
Definition: tls.h:411
#define MD5_CTX_SIZE
MD5 context size.
Definition: md5.h:66
uint8_t mac_len
MAC length.
Definition: tls.h:209
Processes.
struct pending_operation validation
Certificate validation pending operation.
Definition: tls.h:417
unsigned char uint8_t
Definition: stdint.h:10
uint8_t signature
Signature algorithm.
Definition: tls.h:273
X.509 certificates.
unsigned int pending
Pending transmissions.
Definition: tls.h:365
struct tls_cipherspec pending
Next cipher specification.
Definition: tls.h:265
unsigned int uint32_t
Definition: stdint.h:12
struct tls_rx rx
Receive state.
Definition: tls.h:461
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:3812
struct tls_signature_hash_id code
Numeric code.
Definition: tls.h:283
enum tls_rx_state state
State machine current state.
Definition: tls.h:377
uint8_t client[12]
Client verification data.
Definition: tls.h:154
An MD5+SHA1 digest.
Definition: tls.h:319
An X.509 root certificate list.
Definition: x509.h:374
A TLS header.
Definition: tls.h:28
struct tls_tx tx
Transmit state.
Definition: tls.h:459
Pending operations.
struct digest_algorithm * handshake_digest
Digest algorithm used for handshake verification.
Definition: tls.h:450
uint8_t random[28]
Random data.
Definition: tls.h:304
uint64_t seq
Sequence number.
Definition: tls.h:375
uint16_t code
Numeric code (in network-endian order)
Definition: tls.h:231
An elliptic curve.
Definition: crypto.h:184
A TLS session.
Definition: tls.h:330
#define SHA1_DIGEST_SIZE
Definition: Tpm20.h:25
SHA-1 algorithm.
uint8_t sha1[SHA1_CTX_SIZE]
SHA-1 context.
Definition: tls.h:312
struct io_buffer iobuf
Current received record header (static I/O buffer)
Definition: tls.h:381
struct digest_algorithm * handshake
Handshake digest algorithm (for TLSv1.2 and above)
Definition: tls.h:199
tls_rx_state
TLS RX state machine state.
Definition: tls.h:160
#define SHA1_CTX_SIZE
SHA-1 context size.
Definition: sha1.h:66
struct x509_chain * chain
Certificate chain (if used)
Definition: tls.h:395
uint16_t version
Protocol version.
Definition: tls.h:38
A message digest algorithm.
Definition: crypto.h:18
Reference counting.
uint16_t version
Protocol version.
Definition: tls.h:446
struct tls_cipherspec active
Current cipher specification.
Definition: tls.h:263
A cipher algorithm.
Definition: crypto.h:50
A private key.
Definition: privkey.h:16
uint8_t server[12]
Server verification data.
Definition: tls.h:156
Linker tables.
A TLS key exchange algorithm.
Definition: tls.h:176
struct x509_root * root
Root of trust.
Definition: tls.h:339
A TLS connection.
Definition: tls.h:423
struct private_key * key
Private key.
Definition: tls.h:341
#define MD5_DIGEST_SIZE
MD5 digest size.
Definition: md5.h:72
struct tls_client_random random
Random bytes.
Definition: tls.h:391
A pending operation.
Definition: pending.h:13
uint8_t md5[MD5_CTX_SIZE]
MD5 context.
Definition: tls.h:310
struct cipher_algorithm * cipher
Bulk encryption cipher algorithm.
Definition: tls.h:195
struct list_head conn
List of connections.
Definition: tls.h:355
int secure_renegotiation
Secure renegotiation flag.
Definition: tls.h:454
A TLS named curve.
Definition: tls.h:227
struct pubkey_algorithm * pubkey
Public-key algorithm.
Definition: tls.h:281
void * dynamic
Dynamically-allocated storage.
Definition: tls.h:251
const char * name
Server name.
Definition: tls.h:337
A TLS signature and hash algorithm identifier.
Definition: tls.h:269
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
size_t exchange_len
Server Key Exchange record length.
Definition: tls.h:407
size_t session_id_len
Length of session ID.
Definition: tls.h:434
struct interface plainstream
Plaintext stream.
Definition: tls.h:441
MD5 algorithm.
TLS client random data.
Definition: tls.h:300
An ASN.1 object cursor.
Definition: asn1.h:20
A public key algorithm.
Definition: crypto.h:121
uint8_t format
Curve point format byte (if any)
Definition: tls.h:233
void * fixed_iv
Fixed initialisation vector.
Definition: tls.h:257
union @383 key
Sense key.
Definition: scsi.h:18
uint16_t code
Numeric code (in network-endian order)
Definition: tls.h:201
void * mac_secret
MAC secret.
Definition: tls.h:255
A persistent I/O buffer.
Definition: iobuf.h:33
struct x509_root * root
Root of trust.
Definition: tls.h:409
uint8_t key_len
Key length.
Definition: tls.h:203