These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / tls.h
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/sha256.h>
20 #include <ipxe/x509.h>
21 #include <ipxe/pending.h>
22 #include <ipxe/iobuf.h>
23 #include <ipxe/tables.h>
24
25 /** A TLS header */
26 struct tls_header {
27         /** Content type
28          *
29          * This is a TLS_TYPE_XXX constant
30          */
31         uint8_t type;
32         /** Protocol version
33          *
34          * This is a TLS_VERSION_XXX constant
35          */
36         uint16_t version;
37         /** Length of payload */
38         uint16_t length;
39 } __attribute__ (( packed ));
40
41 /** TLS version 1.0 */
42 #define TLS_VERSION_TLS_1_0 0x0301
43
44 /** TLS version 1.1 */
45 #define TLS_VERSION_TLS_1_1 0x0302
46
47 /** TLS version 1.2 */
48 #define TLS_VERSION_TLS_1_2 0x0303
49
50 /** Change cipher content type */
51 #define TLS_TYPE_CHANGE_CIPHER 20
52
53 /** Alert content type */
54 #define TLS_TYPE_ALERT 21
55
56 /** Handshake content type */
57 #define TLS_TYPE_HANDSHAKE 22
58
59 /** Application data content type */
60 #define TLS_TYPE_DATA 23
61
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
73
74 /* TLS alert levels */
75 #define TLS_ALERT_WARNING 1
76 #define TLS_ALERT_FATAL 2
77
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
85
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
93
94 /* TLS signature algorithm identifiers */
95 #define TLS_RSA_ALGORITHM 1
96
97 /* TLS server name extension */
98 #define TLS_SERVER_NAME 0
99 #define TLS_SERVER_NAME_HOST_NAME 0
100
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
107
108 /* TLS signature algorithms extension */
109 #define TLS_SIGNATURE_ALGORITHMS 13
110
111 /** TLS RX state machine state */
112 enum tls_rx_state {
113         TLS_RX_HEADER = 0,
114         TLS_RX_DATA,
115 };
116
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,
125 };
126
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;
135         /** Key length */
136         uint16_t key_len;
137         /** Numeric code (in network-endian order) */
138         uint16_t code;
139 };
140
141 /** TLS cipher suite table */
142 #define TLS_CIPHER_SUITES                                               \
143         __table ( struct tls_cipher_suite, "tls_cipher_suites" )
144
145 /** Declare a TLS cipher suite */
146 #define __tls_cipher_suite( pref )                                      \
147         __table_entry ( TLS_CIPHER_SUITES, pref )
148
149 /** A TLS cipher specification */
150 struct tls_cipherspec {
151         /** Cipher suite */
152         struct tls_cipher_suite *suite;
153         /** Dynamically-allocated storage */
154         void *dynamic;
155         /** Public key encryption context */
156         void *pubkey_ctx;
157         /** Bulk encryption cipher context */
158         void *cipher_ctx;
159         /** Next bulk encryption cipher context (TX only) */
160         void *cipher_next_ctx;
161         /** MAC secret */
162         void *mac_secret;
163 };
164
165 /** A TLS signature and hash algorithm identifier */
166 struct tls_signature_hash_id {
167         /** Hash algorithm */
168         uint8_t hash;
169         /** Signature algorithm */
170         uint8_t signature;
171 } __attribute__ (( packed ));
172
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;
179         /** Numeric code */
180         struct tls_signature_hash_id code;
181 };
182
183 /** TLS signature hash algorithm table
184  *
185  * Note that the default (TLSv1.1 and earlier) algorithm using
186  * MD5+SHA1 is never explicitly specified.
187  */
188 #define TLS_SIG_HASH_ALGORITHMS                                         \
189         __table ( struct tls_signature_hash_algorithm,                  \
190                   "tls_sig_hash_algorithms" )
191
192 /** Declare a TLS signature hash algorithm */
193 #define __tls_sig_hash_algorithm                                        \
194         __table_entry ( TLS_SIG_HASH_ALGORITHMS, 01 )
195
196 /** TLS pre-master secret */
197 struct tls_pre_master_secret {
198         /** TLS version */
199         uint16_t version;
200         /** Random data */
201         uint8_t random[46];
202 } __attribute__ (( packed ));
203
204 /** TLS client random data */
205 struct tls_client_random {
206         /** GMT Unix time */
207         uint32_t gmt_unix_time;
208         /** Random data */
209         uint8_t random[28];
210 } __attribute__ (( packed ));
211
212 /** An MD5+SHA1 context */
213 struct md5_sha1_context {
214         /** MD5 context */
215         uint8_t md5[MD5_CTX_SIZE];
216         /** SHA-1 context */
217         uint8_t sha1[SHA1_CTX_SIZE];
218 } __attribute__ (( packed ));
219
220 /** MD5+SHA1 context size */
221 #define MD5_SHA1_CTX_SIZE sizeof ( struct md5_sha1_context )
222
223 /** An MD5+SHA1 digest */
224 struct md5_sha1_digest {
225         /** MD5 digest */
226         uint8_t md5[MD5_DIGEST_SIZE];
227         /** SHA-1 digest */
228         uint8_t sha1[SHA1_DIGEST_SIZE];
229 } __attribute__ (( packed ));
230
231 /** MD5+SHA1 digest size */
232 #define MD5_SHA1_DIGEST_SIZE sizeof ( struct md5_sha1_digest )
233
234 /** A TLS session */
235 struct tls_session {
236         /** Reference counter */
237         struct refcnt refcnt;
238
239         /** Server name */
240         const char *name;
241         /** Plaintext stream */
242         struct interface plainstream;
243         /** Ciphertext stream */
244         struct interface cipherstream;
245
246         /** Protocol version */
247         uint16_t 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;
258         /** 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;
274
275         /** Server certificate chain */
276         struct x509_chain *chain;
277         /** Certificate validator */
278         struct interface validator;
279
280         /** Client security negotiation pending operation */
281         struct pending_operation client_negotiation;
282         /** Server security negotiation pending operation */
283         struct pending_operation server_negotiation;
284
285         /** TX sequence number */
286         uint64_t tx_seq;
287         /** TX pending transmissions */
288         unsigned int tx_pending;
289         /** TX process */
290         struct process process;
291
292         /** RX sequence number */
293         uint64_t rx_seq;
294         /** RX state */
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;
302 };
303
304 /** RX I/O buffer size
305  *
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.
311  */
312 #define TLS_RX_BUFSIZE 4096
313
314 /** Minimum RX I/O buffer size
315  *
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.
319  */
320 #define TLS_RX_MIN_BUFSIZE 512
321
322 /** RX I/O buffer alignment */
323 #define TLS_RX_ALIGN 16
324
325 extern int add_tls ( struct interface *xfer, const char *name,
326                      struct interface **next );
327
328 #endif /* _IPXE_TLS_H */