tls: if got CERTIFICATE_REQUEST, send an empty CERTIFICATE
[oweals/busybox.git] / networking / tls.c
1 /*
2  * Copyright (C) 2017 Denys Vlasenko
3  *
4  * Licensed under GPLv2, see file LICENSE in this source tree.
5  */
6 //config:config TLS
7 //config:       bool #No description makes it a hidden option
8 //config:       default n
9
10 //kbuild:lib-$(CONFIG_TLS) += tls.o
11 //kbuild:lib-$(CONFIG_TLS) += tls_pstm.o
12 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_montgomery_reduce.o
13 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_mul_comba.o
14 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_sqr_comba.o
15 //kbuild:lib-$(CONFIG_TLS) += tls_rsa.o
16 //kbuild:lib-$(CONFIG_TLS) += tls_aes.o
17 ////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o
18
19 #include "tls.h"
20
21 //Tested against kernel.org:
22 //TLS 1.2
23 #define TLS_MAJ 3
24 #define TLS_MIN 3
25 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE *** matrixssl uses this on my box
26 //#define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
27 //#define CIPHER_ID TLS_DH_anon_WITH_AES_256_CBC_SHA // SSL_ALERT_HANDSHAKE_FAILURE
28 //^^^^^^^^^^^^^^^^^^^^^^^ (tested b/c this one doesn't req server certs... no luck, server refuses it)
29 //#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 // SSL_ALERT_HANDSHAKE_FAILURE
30 //#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
31 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 // ok, recvs SERVER_KEY_EXCHANGE
32 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
33 //#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
34 //#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
35 //#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
36 //#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
37 //#define CIPHER_ID TLS_RSA_WITH_AES_256_GCM_SHA384 // ok, no SERVER_KEY_EXCHANGE
38 //#define CIPHER_ID TLS_RSA_WITH_AES_128_GCM_SHA256 // ok, no SERVER_KEY_EXCHANGE *** select this?
39
40 // works against "openssl s_server -cipher NULL"
41 // and against wolfssl-3.9.10-stable/examples/server/server.c:
42 //#define CIPHER_ID TLS_RSA_WITH_NULL_SHA256 // for testing (does everything except encrypting)
43
44 // works against wolfssl-3.9.10-stable/examples/server/server.c
45 // works for kernel.org
46 // does not work for cdn.kernel.org (e.g. downloading an actual tarball, not a web page)
47 //  getting alert 40 "handshake failure" at once
48 //  with GNU Wget 1.18, they agree on TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xC02F) cipher
49 //  fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES256-SHA256
50 //  fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES256-GCM-SHA384
51 //  fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-SHA256
52 //  ok:   openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-GCM-SHA256
53 //  ok:   openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-SHA
54 //        (TLS_RSA_WITH_AES_128_CBC_SHA - in TLS 1.2 it's mandated to be always supported)
55 #define CIPHER_ID1  TLS_RSA_WITH_AES_256_CBC_SHA256 // no SERVER_KEY_EXCHANGE from peer
56 // Does not work yet:
57 //#define CIPHER_ID2  TLS_RSA_WITH_AES_128_CBC_SHA
58 #define CIPHER_ID2  0
59
60
61 #define TLS_DEBUG      1
62 #define TLS_DEBUG_HASH 1
63 #define TLS_DEBUG_DER  1
64 #define TLS_DEBUG_FIXED_SECRETS 1
65 #if 0
66 # define dump_raw_out(...) dump_hex(__VA_ARGS__)
67 #else
68 # define dump_raw_out(...) ((void)0)
69 #endif
70 #if 0
71 # define dump_raw_in(...) dump_hex(__VA_ARGS__)
72 #else
73 # define dump_raw_in(...) ((void)0)
74 #endif
75
76 #if TLS_DEBUG
77 # define dbg(...) fprintf(stderr, __VA_ARGS__)
78 #else
79 # define dbg(...) ((void)0)
80 #endif
81
82 #if TLS_DEBUG_DER
83 # define dbg_der(...) fprintf(stderr, __VA_ARGS__)
84 #else
85 # define dbg_der(...) ((void)0)
86 #endif
87
88 #define RECORD_TYPE_CHANGE_CIPHER_SPEC  20
89 #define RECORD_TYPE_ALERT               21
90 #define RECORD_TYPE_HANDSHAKE           22
91 #define RECORD_TYPE_APPLICATION_DATA    23
92
93 #define HANDSHAKE_HELLO_REQUEST         0
94 #define HANDSHAKE_CLIENT_HELLO          1
95 #define HANDSHAKE_SERVER_HELLO          2
96 #define HANDSHAKE_HELLO_VERIFY_REQUEST  3
97 #define HANDSHAKE_NEW_SESSION_TICKET    4
98 #define HANDSHAKE_CERTIFICATE           11
99 #define HANDSHAKE_SERVER_KEY_EXCHANGE   12
100 #define HANDSHAKE_CERTIFICATE_REQUEST   13
101 #define HANDSHAKE_SERVER_HELLO_DONE     14
102 #define HANDSHAKE_CERTIFICATE_VERIFY    15
103 #define HANDSHAKE_CLIENT_KEY_EXCHANGE   16
104 #define HANDSHAKE_FINISHED              20
105
106 #define SSL_NULL_WITH_NULL_NULL                 0x0000
107 #define SSL_RSA_WITH_NULL_MD5                   0x0001
108 #define SSL_RSA_WITH_NULL_SHA                   0x0002
109 #define SSL_RSA_WITH_RC4_128_MD5                0x0004
110 #define SSL_RSA_WITH_RC4_128_SHA                0x0005
111 #define SSL_RSA_WITH_3DES_EDE_CBC_SHA           0x000A  /* 10 */
112 #define TLS_RSA_WITH_AES_128_CBC_SHA            0x002F  /* 47 */
113 #define TLS_RSA_WITH_AES_256_CBC_SHA            0x0035  /* 53 */
114 #define TLS_RSA_WITH_NULL_SHA256                0x003B  /* 59 */
115
116 #define TLS_EMPTY_RENEGOTIATION_INFO_SCSV       0x00FF
117
118 #define TLS_RSA_WITH_IDEA_CBC_SHA               0x0007  /* 7 */
119 #define SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA       0x0016  /* 22 */
120 #define SSL_DH_anon_WITH_RC4_128_MD5            0x0018  /* 24 */
121 #define SSL_DH_anon_WITH_3DES_EDE_CBC_SHA       0x001B  /* 27 */
122 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA        0x0033  /* 51 */
123 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA        0x0039  /* 57 */
124 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256     0x0067  /* 103 */
125 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256     0x006B  /* 107 */
126 #define TLS_DH_anon_WITH_AES_128_CBC_SHA        0x0034  /* 52 */
127 #define TLS_DH_anon_WITH_AES_256_CBC_SHA        0x003A  /* 58 */
128 #define TLS_RSA_WITH_AES_128_CBC_SHA256         0x003C  /* 60 */
129 #define TLS_RSA_WITH_AES_256_CBC_SHA256         0x003D  /* 61 */
130 #define TLS_RSA_WITH_SEED_CBC_SHA               0x0096  /* 150 */
131 #define TLS_PSK_WITH_AES_128_CBC_SHA            0x008C  /* 140 */
132 #define TLS_PSK_WITH_AES_128_CBC_SHA256         0x00AE  /* 174 */
133 #define TLS_PSK_WITH_AES_256_CBC_SHA384         0x00AF  /* 175 */
134 #define TLS_PSK_WITH_AES_256_CBC_SHA            0x008D  /* 141 */
135 #define TLS_DHE_PSK_WITH_AES_128_CBC_SHA        0x0090  /* 144 */
136 #define TLS_DHE_PSK_WITH_AES_256_CBC_SHA        0x0091  /* 145 */
137 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA     0xC004  /* 49156 */
138 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA     0xC005  /* 49157 */
139 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA    0xC009  /* 49161 */
140 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA    0xC00A  /* 49162 */
141 #define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA     0xC012  /* 49170 */
142 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA      0xC013  /* 49171 */
143 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA      0xC014  /* 49172 */
144 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA       0xC00E  /* 49166 */
145 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA       0xC00F  /* 49167 */
146 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023  /* 49187 */
147 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024  /* 49188 */
148 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256  0xC025  /* 49189 */
149 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384  0xC026  /* 49190 */
150 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256   0xC027  /* 49191 */
151 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384   0xC028  /* 49192 */
152 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256    0xC029  /* 49193 */
153 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384    0xC02A  /* 49194 */
154
155 /* RFC 5288 "AES Galois Counter Mode (GCM) Cipher Suites for TLS" */
156 #define TLS_RSA_WITH_AES_128_GCM_SHA256         0x009C  /* 156 */
157 #define TLS_RSA_WITH_AES_256_GCM_SHA384         0x009D  /* 157 */
158 #define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B  /* 49195 */
159 #define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C  /* 49196 */
160 #define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256  0xC02D  /* 49197 */
161 #define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384  0xC02E  /* 49198 */
162 #define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256   0xC02F  /* 49199 */
163 #define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384   0xC030  /* 49200 */
164 #define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256    0xC031  /* 49201 */
165 #define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384    0xC032  /* 49202 */
166
167 /* Might go to libbb.h */
168 #define TLS_MAX_CRYPTBLOCK_SIZE 16
169 #define TLS_MAX_OUTBUF          (1 << 14)
170
171 enum {
172         SHA_INSIZE     = 64,
173         SHA1_OUTSIZE   = 20,
174         SHA256_OUTSIZE = 32,
175
176         AES_BLOCKSIZE  = 16,
177         AES128_KEYSIZE = 16,
178         AES256_KEYSIZE = 32,
179
180         RSA_PREMASTER_SIZE = 48,
181
182         RECHDR_LEN = 5,
183
184         /* 8 = 3+5. 3 extra bytes result in record data being 32-bit aligned */
185         OUTBUF_PFX = 8 + AES_BLOCKSIZE, /* header + IV */
186         OUTBUF_SFX = TLS_MAX_MAC_SIZE + TLS_MAX_CRYPTBLOCK_SIZE, /* MAC + padding */
187
188         // RFC 5246
189         // | 6.2.1. Fragmentation
190         // |  The record layer fragments information blocks into TLSPlaintext
191         // |  records carrying data in chunks of 2^14 bytes or less.  Client
192         // |  message boundaries are not preserved in the record layer (i.e.,
193         // |  multiple client messages of the same ContentType MAY be coalesced
194         // |  into a single TLSPlaintext record, or a single message MAY be
195         // |  fragmented across several records)
196         // |...
197         // |  length
198         // |    The length (in bytes) of the following TLSPlaintext.fragment.
199         // |    The length MUST NOT exceed 2^14.
200         // |...
201         // | 6.2.2. Record Compression and Decompression
202         // |...
203         // |  Compression must be lossless and may not increase the content length
204         // |  by more than 1024 bytes.  If the decompression function encounters a
205         // |  TLSCompressed.fragment that would decompress to a length in excess of
206         // |  2^14 bytes, it MUST report a fatal decompression failure error.
207         // |...
208         // |  length
209         // |    The length (in bytes) of the following TLSCompressed.fragment.
210         // |    The length MUST NOT exceed 2^14 + 1024.
211         // |...
212         // | 6.2.3.  Record Payload Protection
213         // |  The encryption and MAC functions translate a TLSCompressed
214         // |  structure into a TLSCiphertext.  The decryption functions reverse
215         // |  the process.  The MAC of the record also includes a sequence
216         // |  number so that missing, extra, or repeated messages are
217         // |  detectable.
218         // |...
219         // |  length
220         // |    The length (in bytes) of the following TLSCiphertext.fragment.
221         // |    The length MUST NOT exceed 2^14 + 2048.
222         MAX_INBUF = RECHDR_LEN + (1 << 14) + 2048,
223 };
224
225 struct record_hdr {
226         uint8_t type;
227         uint8_t proto_maj, proto_min;
228         uint8_t len16_hi, len16_lo;
229 };
230
231 struct tls_handshake_data {
232         /* In bbox, md5/sha1/sha256 ctx's are the same structure */
233         md5sha_ctx_t handshake_hash_ctx;
234
235         uint8_t client_and_server_rand32[2 * 32];
236         uint8_t master_secret[48];
237 //TODO: store just the DER key here, parse/use/delete it when sending client key
238 //this way it will stay key type agnostic here.
239         psRsaKey_t server_rsa_pub_key;
240
241         unsigned saved_client_hello_size;
242         uint8_t saved_client_hello[1];
243 };
244
245
246 static unsigned get24be(const uint8_t *p)
247 {
248         return 0x100*(0x100*p[0] + p[1]) + p[2];
249 }
250
251 #if TLS_DEBUG
252 static void dump_hex(const char *fmt, const void *vp, int len)
253 {
254         char hexbuf[32 * 1024 + 4];
255         const uint8_t *p = vp;
256
257         bin2hex(hexbuf, (void*)p, len)[0] = '\0';
258         dbg(fmt, hexbuf);
259 }
260
261 static void dump_tls_record(const void *vp, int len)
262 {
263         const uint8_t *p = vp;
264
265         while (len > 0) {
266                 unsigned xhdr_len;
267                 if (len < RECHDR_LEN) {
268                         dump_hex("< |%s|\n", p, len);
269                         return;
270                 }
271                 xhdr_len = 0x100*p[3] + p[4];
272                 dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len);
273                 p += RECHDR_LEN;
274                 len -= RECHDR_LEN;
275                 if (len >= 4 && p[-RECHDR_LEN] == RECORD_TYPE_HANDSHAKE) {
276                         unsigned len24 = get24be(p + 1);
277                         dbg(" type:%u len24:%u", p[0], len24);
278                 }
279                 if (xhdr_len > len)
280                         xhdr_len = len;
281                 dump_hex(" |%s|\n", p, xhdr_len);
282                 p += xhdr_len;
283                 len -= xhdr_len;
284         }
285 }
286 #else
287 # define dump_hex(...) ((void)0)
288 # define dump_tls_record(...) ((void)0)
289 #endif
290
291 void tls_get_random(void *buf, unsigned len)
292 {
293         if (len != open_read_close("/dev/urandom", buf, len))
294                 xfunc_die();
295 }
296
297 /* Nondestructively see the current hash value */
298 static unsigned sha_peek(md5sha_ctx_t *ctx, void *buffer)
299 {
300         md5sha_ctx_t ctx_copy = *ctx; /* struct copy */
301         return sha_end(&ctx_copy, buffer);
302 }
303
304 static ALWAYS_INLINE unsigned get_handshake_hash(tls_state_t *tls, void *buffer)
305 {
306         return sha_peek(&tls->hsd->handshake_hash_ctx, buffer);
307 }
308
309 #if !TLS_DEBUG_HASH
310 # define hash_handshake(tls, fmt, buffer, len) \
311          hash_handshake(tls, buffer, len)
312 #endif
313 static void hash_handshake(tls_state_t *tls, const char *fmt, const void *buffer, unsigned len)
314 {
315         md5sha_hash(&tls->hsd->handshake_hash_ctx, buffer, len);
316 #if TLS_DEBUG_HASH
317         {
318                 uint8_t h[TLS_MAX_MAC_SIZE];
319                 dump_hex(fmt, buffer, len);
320                 dbg(" (%u bytes) ", (int)len);
321                 len = sha_peek(&tls->hsd->handshake_hash_ctx, h);
322                 if (len == SHA1_OUTSIZE)
323                         dump_hex("sha1:%s\n", h, len);
324                 else
325                 if (len == SHA256_OUTSIZE)
326                         dump_hex("sha256:%s\n", h, len);
327                 else
328                         dump_hex("sha???:%s\n", h, len);
329         }
330 #endif
331 }
332
333 // RFC 2104
334 // HMAC(key, text) based on a hash H (say, sha256) is:
335 // ipad = [0x36 x INSIZE]
336 // opad = [0x5c x INSIZE]
337 // HMAC(key, text) = H((key XOR opad) + H((key XOR ipad) + text))
338 //
339 // H(key XOR opad) and H(key XOR ipad) can be precomputed
340 // if we often need HMAC hmac with the same key.
341 //
342 // text is often given in disjoint pieces.
343 static unsigned hmac_sha_precomputed_v(uint8_t *out,
344                 md5sha_ctx_t *hashed_key_xor_ipad,
345                 md5sha_ctx_t *hashed_key_xor_opad,
346                 va_list va)
347 {
348         uint8_t *text;
349         unsigned len;
350
351         /* hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
352         /* hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
353
354         /* calculate out = H((key XOR ipad) + text) */
355         while ((text = va_arg(va, uint8_t*)) != NULL) {
356                 unsigned text_size = va_arg(va, unsigned);
357                 md5sha_hash(hashed_key_xor_ipad, text, text_size);
358         }
359         len = sha_end(hashed_key_xor_ipad, out);
360
361         /* out = H((key XOR opad) + out) */
362         md5sha_hash(hashed_key_xor_opad, out, len);
363         return sha_end(hashed_key_xor_opad, out);
364 }
365
366 static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_size, ...)
367 {
368         md5sha_ctx_t hashed_key_xor_ipad;
369         md5sha_ctx_t hashed_key_xor_opad;
370         uint8_t key_xor_ipad[SHA_INSIZE];
371         uint8_t key_xor_opad[SHA_INSIZE];
372         uint8_t tempkey[SHA256_OUTSIZE];
373         va_list va;
374         unsigned i;
375
376         va_start(va, key_size);
377
378         // "The authentication key can be of any length up to INSIZE, the
379         // block length of the hash function.  Applications that use keys longer
380         // than INSIZE bytes will first hash the key using H and then use the
381         // resultant OUTSIZE byte string as the actual key to HMAC."
382         if (key_size > SHA_INSIZE) {
383                 md5sha_ctx_t ctx;
384                 if (tls->MAC_size == SHA256_OUTSIZE)
385                         sha256_begin(&ctx);
386                 else
387                         sha1_begin(&ctx);
388                 md5sha_hash(&ctx, key, key_size);
389                 key_size = sha_end(&ctx, tempkey);
390         }
391
392         for (i = 0; i < key_size; i++) {
393                 key_xor_ipad[i] = key[i] ^ 0x36;
394                 key_xor_opad[i] = key[i] ^ 0x5c;
395         }
396         for (; i < SHA_INSIZE; i++) {
397                 key_xor_ipad[i] = 0x36;
398                 key_xor_opad[i] = 0x5c;
399         }
400
401         if (tls->MAC_size == SHA256_OUTSIZE) {
402                 sha256_begin(&hashed_key_xor_ipad);
403                 sha256_begin(&hashed_key_xor_opad);
404         } else {
405                 sha1_begin(&hashed_key_xor_ipad);
406                 sha1_begin(&hashed_key_xor_opad);
407         }
408         md5sha_hash(&hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
409         md5sha_hash(&hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
410
411         i = hmac_sha_precomputed_v(out, &hashed_key_xor_ipad, &hashed_key_xor_opad, va);
412         va_end(va);
413         return i;
414 }
415
416 // RFC 5246:
417 // 5.  HMAC and the Pseudorandom Function
418 //...
419 // In this section, we define one PRF, based on HMAC.  This PRF with the
420 // SHA-256 hash function is used for all cipher suites defined in this
421 // document and in TLS documents published prior to this document when
422 // TLS 1.2 is negotiated.
423 //...
424 //    P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
425 //                           HMAC_hash(secret, A(2) + seed) +
426 //                           HMAC_hash(secret, A(3) + seed) + ...
427 // where + indicates concatenation.
428 // A() is defined as:
429 //    A(0) = seed
430 //    A(1) = HMAC_hash(secret, A(0)) = HMAC_hash(secret, seed)
431 //    A(i) = HMAC_hash(secret, A(i-1))
432 // P_hash can be iterated as many times as necessary to produce the
433 // required quantity of data.  For example, if P_SHA256 is being used to
434 // create 80 bytes of data, it will have to be iterated three times
435 // (through A(3)), creating 96 bytes of output data; the last 16 bytes
436 // of the final iteration will then be discarded, leaving 80 bytes of
437 // output data.
438 //
439 // TLS's PRF is created by applying P_hash to the secret as:
440 //
441 //    PRF(secret, label, seed) = P_<hash>(secret, label + seed)
442 //
443 // The label is an ASCII string.
444 static void prf_hmac(tls_state_t *tls,
445                 uint8_t *outbuf, unsigned outbuf_size,
446                 uint8_t *secret, unsigned secret_size,
447                 const char *label,
448                 uint8_t *seed, unsigned seed_size)
449 {
450         uint8_t a[TLS_MAX_MAC_SIZE];
451         uint8_t *out_p = outbuf;
452         unsigned label_size = strlen(label);
453         unsigned MAC_size = tls->MAC_size;
454
455         /* In P_hash() calculation, "seed" is "label + seed": */
456 #define SEED   label, label_size, seed, seed_size
457 #define SECRET secret, secret_size
458 #define A      a, MAC_size
459
460         /* A(1) = HMAC_hash(secret, seed) */
461         hmac(tls, a, SECRET, SEED, NULL);
462 //TODO: convert hmac to precomputed
463
464         for(;;) {
465                 /* HMAC_hash(secret, A(1) + seed) */
466                 if (outbuf_size <= MAC_size) {
467                         /* Last, possibly incomplete, block */
468                         /* (use a[] as temp buffer) */
469                         hmac(tls, a, SECRET, A, SEED, NULL);
470                         memcpy(out_p, a, outbuf_size);
471                         return;
472                 }
473                 /* Not last block. Store directly to result buffer */
474                 hmac(tls, out_p, SECRET, A, SEED, NULL);
475                 out_p += MAC_size;
476                 outbuf_size -= MAC_size;
477                 /* A(2) = HMAC_hash(secret, A(1)) */
478                 hmac(tls, a, SECRET, A, NULL);
479         }
480 #undef A
481 #undef SECRET
482 #undef SEED
483 }
484
485 static void bad_record_die(tls_state_t *tls, const char *expected, int len)
486 {
487         bb_error_msg("got bad TLS record (len:%d) while expecting %s", len, expected);
488         if (len > 0) {
489                 uint8_t *p = tls->inbuf;
490                 while (len > 0) {
491                         fprintf(stderr, " %02x", *p++);
492                         len--;
493                 }
494                 fputc('\n', stderr);
495         }
496         xfunc_die();
497 }
498
499 static void tls_error_die(tls_state_t *tls, int line)
500 {
501         dump_tls_record(tls->inbuf, tls->ofs_to_buffered + tls->buffered_size);
502         bb_error_msg_and_die("tls error at line %d cipher:%04x", line, tls->cipher_id);
503 }
504 #define tls_error_die(tls) tls_error_die(tls, __LINE__)
505
506 #if 0 //UNUSED
507 static void tls_free_inbuf(tls_state_t *tls)
508 {
509         if (tls->buffered_size == 0) {
510                 free(tls->inbuf);
511                 tls->inbuf_size = 0;
512                 tls->inbuf = NULL;
513         }
514 }
515 #endif
516
517 static void tls_free_outbuf(tls_state_t *tls)
518 {
519         free(tls->outbuf);
520         tls->outbuf_size = 0;
521         tls->outbuf = NULL;
522 }
523
524 static void *tls_get_outbuf(tls_state_t *tls, int len)
525 {
526         if (len > TLS_MAX_OUTBUF)
527                 xfunc_die();
528         len += OUTBUF_PFX + OUTBUF_SFX;
529         if (tls->outbuf_size < len) {
530                 tls->outbuf_size = len;
531                 tls->outbuf = xrealloc(tls->outbuf, len);
532         }
533         return tls->outbuf + OUTBUF_PFX;
534 }
535
536 static void xwrite_encrypted(tls_state_t *tls, unsigned size, unsigned type)
537 {
538         uint8_t *buf = tls->outbuf + OUTBUF_PFX;
539         struct record_hdr *xhdr;
540         uint8_t padding_length;
541
542         xhdr = (void*)(buf - RECHDR_LEN);
543         if (tls->cipher_id != TLS_RSA_WITH_NULL_SHA256)
544                 xhdr = (void*)(buf - RECHDR_LEN - AES_BLOCKSIZE); /* place for IV */
545
546         xhdr->type = type;
547         xhdr->proto_maj = TLS_MAJ;
548         xhdr->proto_min = TLS_MIN;
549         /* fake unencrypted record len for MAC calculation */
550         xhdr->len16_hi = size >> 8;
551         xhdr->len16_lo = size & 0xff;
552
553         /* Calculate MAC signature */
554         hmac(tls, buf + size, /* result */
555                 tls->client_write_MAC_key, tls->MAC_size,
556                 &tls->write_seq64_be, sizeof(tls->write_seq64_be),
557                 xhdr, RECHDR_LEN,
558                 buf, size,
559                 NULL
560         );
561         tls->write_seq64_be = SWAP_BE64(1 + SWAP_BE64(tls->write_seq64_be));
562
563         size += tls->MAC_size;
564
565         // RFC 5246
566         // 6.2.3.1.  Null or Standard Stream Cipher
567         //
568         // Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6)
569         // convert TLSCompressed.fragment structures to and from stream
570         // TLSCiphertext.fragment structures.
571         //
572         //    stream-ciphered struct {
573         //        opaque content[TLSCompressed.length];
574         //        opaque MAC[SecurityParameters.mac_length];
575         //    } GenericStreamCipher;
576         //
577         // The MAC is generated as:
578         //    MAC(MAC_write_key, seq_num +
579         //                          TLSCompressed.type +
580         //                          TLSCompressed.version +
581         //                          TLSCompressed.length +
582         //                          TLSCompressed.fragment);
583         // where "+" denotes concatenation.
584         // seq_num
585         //    The sequence number for this record.
586         // MAC
587         //    The MAC algorithm specified by SecurityParameters.mac_algorithm.
588         //
589         // Note that the MAC is computed before encryption.  The stream cipher
590         // encrypts the entire block, including the MAC.
591         //...
592         // Appendix C.  Cipher Suite Definitions
593         //...
594         // MAC       Algorithm    mac_length  mac_key_length
595         // --------  -----------  ----------  --------------
596         // SHA       HMAC-SHA1       20            20
597         // SHA256    HMAC-SHA256     32            32
598         if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256) {
599                 /* No encryption, only signing */
600                 xhdr->len16_hi = size >> 8;
601                 xhdr->len16_lo = size & 0xff;
602                 dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
603                 xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
604                 dbg("wrote %u bytes (NULL crypt, SHA256 hash)\n", size);
605                 return;
606         }
607
608         // 6.2.3.2.  CBC Block Cipher
609         // For block ciphers (such as 3DES or AES), the encryption and MAC
610         // functions convert TLSCompressed.fragment structures to and from block
611         // TLSCiphertext.fragment structures.
612         //    struct {
613         //        opaque IV[SecurityParameters.record_iv_length];
614         //        block-ciphered struct {
615         //            opaque content[TLSCompressed.length];
616         //            opaque MAC[SecurityParameters.mac_length];
617         //            uint8 padding[GenericBlockCipher.padding_length];
618         //            uint8 padding_length;
619         //        };
620         //    } GenericBlockCipher;
621         //...
622         // IV
623         //    The Initialization Vector (IV) SHOULD be chosen at random, and
624         //    MUST be unpredictable.  Note that in versions of TLS prior to 1.1,
625         //    there was no IV field (...).  For block ciphers, the IV length is
626         //    of length SecurityParameters.record_iv_length, which is equal to the
627         //    SecurityParameters.block_size.
628         // padding
629         //    Padding that is added to force the length of the plaintext to be
630         //    an integral multiple of the block cipher's block length.
631         // padding_length
632         //    The padding length MUST be such that the total size of the
633         //    GenericBlockCipher structure is a multiple of the cipher's block
634         //    length.  Legal values range from zero to 255, inclusive.
635         //...
636         // Appendix C.  Cipher Suite Definitions
637         //...
638         //                         Key      IV   Block
639         // Cipher        Type    Material  Size  Size
640         // ------------  ------  --------  ----  -----
641         // AES_128_CBC   Block      16      16     16
642         // AES_256_CBC   Block      32      16     16
643
644         /* Fill IV and padding in outbuf */
645         tls_get_random(buf - AES_BLOCKSIZE, AES_BLOCKSIZE); /* IV */
646         dbg("before crypt: 5 hdr + %u data + %u hash bytes\n", size, tls->MAC_size);
647         // RFC is talking nonsense:
648         //    "Padding that is added to force the length of the plaintext to be
649         //    an integral multiple of the block cipher's block length."
650         // WRONG. _padding+padding_length_, not just _padding_,
651         // pads the data.
652         // IOW: padding_length is the last byte of padding[] array,
653         // contrary to what RFC depicts.
654         //
655         // What actually happens is that there is always padding.
656         // If you need one byte to reach BLOCKSIZE, this byte is 0x00.
657         // If you need two bytes, they are both 0x01.
658         // If you need three, they are 0x02,0x02,0x02. And so on.
659         // If you need no bytes to reach BLOCKSIZE, you have to pad a full
660         // BLOCKSIZE with bytes of value (BLOCKSIZE-1).
661         // It's ok to have more than minimum padding, but we do minimum.
662         padding_length = (~size) & (AES_BLOCKSIZE - 1);
663         do {
664                 buf[size++] = padding_length; /* padding */
665         } while ((size & (AES_BLOCKSIZE - 1)) != 0);
666
667         /* Encrypt content+MAC+padding in place */
668         {
669                 psCipherContext_t ctx;
670                 psAesInit(&ctx, buf - AES_BLOCKSIZE, /* IV */
671                         tls->client_write_key, tls->key_size /* selects 128/256 */
672                 );
673                 psAesEncrypt(&ctx,
674                         buf, /* plaintext */
675                         buf, /* ciphertext */
676                         size
677                 );
678         }
679
680         /* Write out */
681         dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n",
682                         AES_BLOCKSIZE, size, padding_length);
683         size += AES_BLOCKSIZE;     /* + IV */
684         xhdr->len16_hi = size >> 8;
685         xhdr->len16_lo = size & 0xff;
686         dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
687         xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
688         dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
689 }
690
691 static void xwrite_handshake_record(tls_state_t *tls, unsigned size)
692 {
693         //if (!tls->encrypt_on_write) {
694                 uint8_t *buf = tls->outbuf + OUTBUF_PFX;
695                 struct record_hdr *xhdr = (void*)(buf - RECHDR_LEN);
696
697                 xhdr->type = RECORD_TYPE_HANDSHAKE;
698                 xhdr->proto_maj = TLS_MAJ;
699                 xhdr->proto_min = TLS_MIN;
700                 xhdr->len16_hi = size >> 8;
701                 xhdr->len16_lo = size & 0xff;
702                 dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
703                 xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
704                 dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
705         //      return;
706         //}
707         //xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
708 }
709
710 static void xwrite_and_update_handshake_hash(tls_state_t *tls, unsigned size)
711 {
712         if (!tls->encrypt_on_write) {
713                 uint8_t *buf;
714
715                 xwrite_handshake_record(tls, size);
716                 /* Handshake hash does not include record headers */
717                 buf = tls->outbuf + OUTBUF_PFX;
718                 hash_handshake(tls, ">> hash:%s", buf, size);
719                 return;
720         }
721         xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
722 }
723
724 static int tls_has_buffered_record(tls_state_t *tls)
725 {
726         int buffered = tls->buffered_size;
727         struct record_hdr *xhdr;
728         int rec_size;
729
730         if (buffered < RECHDR_LEN)
731                 return 0;
732         xhdr = (void*)(tls->inbuf + tls->ofs_to_buffered);
733         rec_size = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
734         if (buffered < rec_size)
735                 return 0;
736         return rec_size;
737 }
738
739 static const char *alert_text(int code)
740 {
741         switch (code) {
742         case 20:  return "bad MAC";
743         case 50:  return "decode error";
744         case 51:  return "decrypt error";
745         case 40:  return "handshake failure";
746         case 112: return "unrecognized name";
747         }
748         return itoa(code);
749 }
750
751 static int tls_xread_record(tls_state_t *tls)
752 {
753         struct record_hdr *xhdr;
754         int sz;
755         int total;
756         int target;
757
758  again:
759         dbg("ofs_to_buffered:%u buffered_size:%u\n", tls->ofs_to_buffered, tls->buffered_size);
760         total = tls->buffered_size;
761         if (total != 0) {
762                 memmove(tls->inbuf, tls->inbuf + tls->ofs_to_buffered, total);
763                 //dbg("<< remaining at %d [%d] ", tls->ofs_to_buffered, total);
764                 //dump_raw_in("<< %s\n", tls->inbuf, total);
765         }
766         errno = 0;
767         target = MAX_INBUF;
768         for (;;) {
769                 int rem;
770
771                 if (total >= RECHDR_LEN && target == MAX_INBUF) {
772                         xhdr = (void*)tls->inbuf;
773                         target = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
774                         if (target > MAX_INBUF) {
775                                 /* malformed input (too long): yell and die */
776                                 tls->buffered_size = 0;
777                                 tls->ofs_to_buffered = total;
778                                 tls_error_die(tls);
779                         }
780                         /* can also check type/proto_maj/proto_min here */
781                         dbg("xhdr type:%d ver:%d.%d len:%d\n",
782                                 xhdr->type, xhdr->proto_maj, xhdr->proto_min,
783                                 0x100 * xhdr->len16_hi + xhdr->len16_lo
784                         );
785                 }
786                 /* if total >= target, we have a full packet (and possibly more)... */
787                 if (total - target >= 0)
788                         break;
789                 /* input buffer is grown only as needed */
790                 rem = tls->inbuf_size - total;
791                 if (rem == 0) {
792                         tls->inbuf_size += MAX_INBUF / 8;
793                         if (tls->inbuf_size > MAX_INBUF)
794                                 tls->inbuf_size = MAX_INBUF;
795                         dbg("inbuf_size:%d\n", tls->inbuf_size);
796                         rem = tls->inbuf_size - total;
797                         tls->inbuf = xrealloc(tls->inbuf, tls->inbuf_size);
798                 }
799                 sz = safe_read(tls->ifd, tls->inbuf + total, rem);
800                 if (sz <= 0) {
801                         if (sz == 0 && total == 0) {
802                                 /* "Abrupt" EOF, no TLS shutdown (seen from kernel.org) */
803                                 dbg("EOF (without TLS shutdown) from peer\n");
804                                 tls->buffered_size = 0;
805                                 goto end;
806                         }
807                         bb_perror_msg_and_die("short read, have only %d", total);
808                 }
809                 dump_raw_in("<< %s\n", tls->inbuf + total, sz);
810                 total += sz;
811         }
812         tls->buffered_size = total - target;
813         tls->ofs_to_buffered = target;
814         //dbg("<< stashing at %d [%d] ", tls->ofs_to_buffered, tls->buffered_size);
815         //dump_hex("<< %s\n", tls->inbuf + tls->ofs_to_buffered, tls->buffered_size);
816
817         sz = target - RECHDR_LEN;
818
819         /* Needs to be decrypted? */
820         if (tls->min_encrypted_len_on_read > tls->MAC_size) {
821                 psCipherContext_t ctx;
822                 uint8_t *p = tls->inbuf + RECHDR_LEN;
823                 int padding_len;
824
825                 if (sz & (AES_BLOCKSIZE-1)
826                  || sz < tls->min_encrypted_len_on_read
827                 ) {
828                         bb_error_msg_and_die("bad encrypted len:%u", sz);
829                 }
830                 /* Decrypt content+MAC+padding, moving it over IV in the process */
831                 psAesInit(&ctx, p, /* IV */
832                         tls->server_write_key, tls->key_size /* selects 128/256 */
833                 );
834                 sz -= AES_BLOCKSIZE; /* we will overwrite IV now */
835                 psAesDecrypt(&ctx,
836                         p + AES_BLOCKSIZE, /* ciphertext */
837                         p,                 /* plaintext */
838                         sz
839                 );
840                 padding_len = p[sz - 1];
841                 dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[0], padding_len);
842                 padding_len++;
843                 sz -= tls->MAC_size + padding_len; /* drop MAC and padding */
844                 //if (sz < 0)
845                 //      bb_error_msg_and_die("bad padding size:%u", padding_len);
846         } else {
847                 /* if nonzero, then it's TLS_RSA_WITH_NULL_SHA256: drop MAC */
848                 /* else: no encryption yet on input, subtract zero = NOP */
849                 sz -= tls->min_encrypted_len_on_read;
850         }
851         if (sz < 0)
852                 bb_error_msg_and_die("encrypted data too short");
853
854         //dump_hex("<< %s\n", tls->inbuf, RECHDR_LEN + sz);
855
856         xhdr = (void*)tls->inbuf;
857         if (xhdr->type == RECORD_TYPE_ALERT && sz >= 2) {
858                 uint8_t *p = tls->inbuf + RECHDR_LEN;
859                 dbg("ALERT size:%d level:%d description:%d\n", sz, p[0], p[1]);
860                 if (p[0] == 2) { /* fatal */
861                         bb_error_msg_and_die("TLS %s from peer (alert code %d): %s",
862                                 "error",
863                                 p[1], alert_text(p[1])
864                         );
865                 }
866                 if (p[0] == 1) { /* warning */
867                         if (p[1] == 0) { /* "close_notify" warning: it's EOF */
868                                 dbg("EOF (TLS encoded) from peer\n");
869                                 sz = 0;
870                                 goto end;
871                         }
872 //This possibly needs to be cached and shown only if
873 //a fatal alert follows
874 //                      bb_error_msg("TLS %s from peer (alert code %d): %s",
875 //                              "warning",
876 //                              p[1], alert_text(p[1])
877 //                      );
878                         /* discard it, get next record */
879                         goto again;
880                 }
881                 /* p[0] not 1 or 2: not defined in protocol */
882                 sz = 0;
883                 goto end;
884         }
885
886         /* RFC 5246 is not saying it explicitly, but sha256 hash
887          * in our FINISHED record must include data of incoming packets too!
888          */
889         if (tls->inbuf[0] == RECORD_TYPE_HANDSHAKE
890          && tls->MAC_size != 0 /* do we know which hash to use? (server_hello() does not!) */
891         ) {
892                 hash_handshake(tls, "<< hash:%s", tls->inbuf + RECHDR_LEN, sz);
893         }
894  end:
895         dbg("got block len:%u\n", sz);
896         return sz;
897 }
898
899 /*
900  * DER parsing routines
901  */
902 static unsigned get_der_len(uint8_t **bodyp, uint8_t *der, uint8_t *end)
903 {
904         unsigned len, len1;
905
906         if (end - der < 2)
907                 xfunc_die();
908 //      if ((der[0] & 0x1f) == 0x1f) /* not single-byte item code? */
909 //              xfunc_die();
910
911         len = der[1]; /* maybe it's short len */
912         if (len >= 0x80) {
913                 /* no, it's long */
914
915                 if (len == 0x80 || end - der < (int)(len - 0x7e)) {
916                         /* 0x80 is "0 bytes of len", invalid DER: must use short len if can */
917                         /* need 3 or 4 bytes for 81, 82 */
918                         xfunc_die();
919                 }
920
921                 len1 = der[2]; /* if (len == 0x81) it's "ii 81 xx", fetch xx */
922                 if (len > 0x82) {
923                         /* >0x82 is "3+ bytes of len", should not happen realistically */
924                         xfunc_die();
925                 }
926                 if (len == 0x82) { /* it's "ii 82 xx yy" */
927                         len1 = 0x100*len1 + der[3];
928                         der += 1; /* skip [yy] */
929                 }
930                 der += 1; /* skip [xx] */
931                 len = len1;
932 //              if (len < 0x80)
933 //                      xfunc_die(); /* invalid DER: must use short len if can */
934         }
935         der += 2; /* skip [code]+[1byte] */
936
937         if (end - der < (int)len)
938                 xfunc_die();
939         *bodyp = der;
940
941         return len;
942 }
943
944 static uint8_t *enter_der_item(uint8_t *der, uint8_t **endp)
945 {
946         uint8_t *new_der;
947         unsigned len = get_der_len(&new_der, der, *endp);
948         dbg_der("entered der @%p:0x%02x len:%u inner_byte @%p:0x%02x\n", der, der[0], len, new_der, new_der[0]);
949         /* Move "end" position to cover only this item */
950         *endp = new_der + len;
951         return new_der;
952 }
953
954 static uint8_t *skip_der_item(uint8_t *der, uint8_t *end)
955 {
956         uint8_t *new_der;
957         unsigned len = get_der_len(&new_der, der, end);
958         /* Skip body */
959         new_der += len;
960         dbg_der("skipped der 0x%02x, next byte 0x%02x\n", der[0], new_der[0]);
961         return new_der;
962 }
963
964 static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end)
965 {
966         uint8_t *bin_ptr;
967         unsigned len = get_der_len(&bin_ptr, der, end);
968
969         dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]);
970         pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len);
971         pstm_read_unsigned_bin(pstm_n, bin_ptr, len);
972         //return bin + len;
973 }
974
975 static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len)
976 {
977 /* Certificate is a DER-encoded data structure. Each DER element has a length,
978  * which makes it easy to skip over large compound elements of any complexity
979  * without parsing them. Example: partial decode of kernel.org certificate:
980  *  SEQ 0x05ac/1452 bytes (Certificate): 308205ac
981  *    SEQ 0x0494/1172 bytes (tbsCertificate): 30820494
982  *      [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0] 3 bytes: a003
983  *        INTEGER (version): 0201 02
984  *      INTEGER 0x11 bytes (serialNumber): 0211 00 9f85bf664b0cddafca508679501b2be4
985  *      //^^^^^^note: matrixSSL also allows [ASN_CONTEXT_SPECIFIC | ASN_PRIMITIVE | 2] = 0x82 type
986  *      SEQ 0x0d bytes (signatureAlgo): 300d
987  *        OID 9 bytes: 0609 2a864886f70d01010b (OID_SHA256_RSA_SIG 42.134.72.134.247.13.1.1.11)
988  *        NULL: 0500
989  *      SEQ 0x5f bytes (issuer): 305f
990  *        SET 11 bytes: 310b
991  *          SEQ 9 bytes: 3009
992  *            OID 3 bytes: 0603 550406
993  *            Printable string "FR": 1302 4652
994  *        SET 14 bytes: 310e
995  *          SEQ 12 bytes: 300c
996  *            OID 3 bytes: 0603 550408
997  *            Printable string "Paris": 1305 5061726973
998  *        SET 14 bytes: 310e
999  *          SEQ 12 bytes: 300c
1000  *            OID 3 bytes: 0603 550407
1001  *            Printable string "Paris": 1305 5061726973
1002  *        SET 14 bytes: 310e
1003  *          SEQ 12 bytes: 300c
1004  *            OID 3 bytes: 0603 55040a
1005  *            Printable string "Gandi": 1305 47616e6469
1006  *        SET 32 bytes: 3120
1007  *          SEQ 30 bytes: 301e
1008  *            OID 3 bytes: 0603 550403
1009  *            Printable string "Gandi Standard SSL CA 2": 1317 47616e6469205374616e646172642053534c2043412032
1010  *      SEQ 30 bytes (validity): 301e
1011  *        TIME "161011000000Z": 170d 3136313031313030303030305a
1012  *        TIME "191011235959Z": 170d 3139313031313233353935395a
1013  *      SEQ 0x5b/91 bytes (subject): 305b //I did not decode this
1014  *          3121301f060355040b1318446f6d61696e20436f
1015  *          6e74726f6c2056616c6964617465643121301f06
1016  *          0355040b1318506f73697469766553534c204d75
1017  *          6c74692d446f6d61696e31133011060355040313
1018  *          0a6b65726e656c2e6f7267
1019  *      SEQ 0x01a2/418 bytes (subjectPublicKeyInfo): 308201a2
1020  *        SEQ 13 bytes (algorithm): 300d
1021  *          OID 9 bytes: 0609 2a864886f70d010101 (OID_RSA_KEY_ALG 42.134.72.134.247.13.1.1.1)
1022  *          NULL: 0500
1023  *        BITSTRING 0x018f/399 bytes (publicKey): 0382018f
1024  *          ????: 00
1025  *          //after the zero byte, it appears key itself uses DER encoding:
1026  *          SEQ 0x018a/394 bytes: 3082018a
1027  *            INTEGER 0x0181/385 bytes (modulus): 02820181
1028  *                  00b1ab2fc727a3bef76780c9349bf3
1029  *                  ...24 more blocks of 15 bytes each...
1030  *                  90e895291c6bc8693b65
1031  *            INTEGER 3 bytes (exponent): 0203 010001
1032  *      [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0x3] 0x01e5 bytes (X509v3 extensions): a38201e5
1033  *        SEQ 0x01e1 bytes: 308201e1
1034  *        ...
1035  * Certificate is a sequence of three elements:
1036  *      tbsCertificate (SEQ)
1037  *      signatureAlgorithm (AlgorithmIdentifier)
1038  *      signatureValue (BIT STRING)
1039  *
1040  * In turn, tbsCertificate is a sequence of:
1041  *      version
1042  *      serialNumber
1043  *      signatureAlgo (AlgorithmIdentifier)
1044  *      issuer (Name, has complex structure)
1045  *      validity (Validity, SEQ of two Times)
1046  *      subject (Name)
1047  *      subjectPublicKeyInfo (SEQ)
1048  *      ...
1049  *
1050  * subjectPublicKeyInfo is a sequence of:
1051  *      algorithm (AlgorithmIdentifier)
1052  *      publicKey (BIT STRING)
1053  *
1054  * We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey
1055  */
1056         uint8_t *end = der + len;
1057
1058         /* enter "Certificate" item: [der, end) will be only Cert */
1059         der = enter_der_item(der, &end);
1060
1061         /* enter "tbsCertificate" item: [der, end) will be only tbsCert */
1062         der = enter_der_item(der, &end);
1063
1064         /* skip up to subjectPublicKeyInfo */
1065         der = skip_der_item(der, end); /* version */
1066         der = skip_der_item(der, end); /* serialNumber */
1067         der = skip_der_item(der, end); /* signatureAlgo */
1068         der = skip_der_item(der, end); /* issuer */
1069         der = skip_der_item(der, end); /* validity */
1070         der = skip_der_item(der, end); /* subject */
1071
1072         /* enter subjectPublicKeyInfo */
1073         der = enter_der_item(der, &end);
1074         { /* check subjectPublicKeyInfo.algorithm */
1075                 static const uint8_t expected[] = {
1076                         0x30,0x0d, // SEQ 13 bytes
1077                         0x06,0x09, 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, // OID RSA_KEY_ALG 42.134.72.134.247.13.1.1.1
1078                         //0x05,0x00, // NULL
1079                 };
1080                 if (memcmp(der, expected, sizeof(expected)) != 0)
1081                         bb_error_msg_and_die("not RSA key");
1082         }
1083         /* skip subjectPublicKeyInfo.algorithm */
1084         der = skip_der_item(der, end);
1085         /* enter subjectPublicKeyInfo.publicKey */
1086 //      die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */
1087         der = enter_der_item(der, &end);
1088
1089         /* parse RSA key: */
1090 //based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note
1091         dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]);
1092         if (end - der < 14) xfunc_die();
1093         /* example format:
1094          * ignore bits: 00
1095          * SEQ 0x018a/394 bytes: 3082018a
1096          *   INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX
1097          *   INTEGER 3 bytes (exponent): 0203 010001
1098          */
1099         if (*der != 0) /* "ignore bits", should be 0 */
1100                 xfunc_die();
1101         der++;
1102         der = enter_der_item(der, &end); /* enter SEQ */
1103         /* memset(tls->hsd->server_rsa_pub_key, 0, sizeof(tls->hsd->server_rsa_pub_key)); - already is */
1104         der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.N, der, end); /* modulus */
1105         der = skip_der_item(der, end);
1106         der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.e, der, end); /* exponent */
1107         tls->hsd->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->hsd->server_rsa_pub_key.N);
1108         dbg("server_rsa_pub_key.size:%d\n", tls->hsd->server_rsa_pub_key.size);
1109 }
1110
1111 /*
1112  * TLS Handshake routines
1113  */
1114 static int tls_xread_handshake_block(tls_state_t *tls, int min_len)
1115 {
1116         struct record_hdr *xhdr;
1117         int len = tls_xread_record(tls);
1118
1119         xhdr = (void*)tls->inbuf;
1120         if (len < min_len
1121          || xhdr->type != RECORD_TYPE_HANDSHAKE
1122          || xhdr->proto_maj != TLS_MAJ
1123          || xhdr->proto_min != TLS_MIN
1124         ) {
1125                 bad_record_die(tls, "handshake record", len);
1126         }
1127         dbg("got HANDSHAKE\n");
1128         return len;
1129 }
1130
1131 static ALWAYS_INLINE void fill_handshake_record_hdr(void *buf, unsigned type, unsigned len)
1132 {
1133         struct handshake_hdr {
1134                 uint8_t type;
1135                 uint8_t len24_hi, len24_mid, len24_lo;
1136         } *h = buf;
1137
1138         len -= 4;
1139         h->type = type;
1140         h->len24_hi  = len >> 16;
1141         h->len24_mid = len >> 8;
1142         h->len24_lo  = len & 0xff;
1143 }
1144
1145 static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni)
1146 {
1147         struct client_hello {
1148                 uint8_t type;
1149                 uint8_t len24_hi, len24_mid, len24_lo;
1150                 uint8_t proto_maj, proto_min;
1151                 uint8_t rand32[32];
1152                 uint8_t session_id_len;
1153                 /* uint8_t session_id[]; */
1154                 uint8_t cipherid_len16_hi, cipherid_len16_lo;
1155                 uint8_t cipherid[2 * (2 + !!CIPHER_ID2)]; /* actually variable */
1156                 uint8_t comprtypes_len;
1157                 uint8_t comprtypes[1]; /* actually variable */
1158                 /* Extensions (SNI shown):
1159                  * hi,lo // len of all extensions
1160                  *   00,00 // extension_type: "Server Name"
1161                  *   00,0e // list len (there can be more than one SNI)
1162                  *     00,0c // len of 1st Server Name Indication
1163                  *       00    // name type: host_name
1164                  *       00,09   // name len
1165                  *       "localhost" // name
1166                  */
1167 // GNU Wget 1.18 to cdn.kernel.org sends these extensions:
1168 // 0055
1169 //   0005 0005 0100000000 - status_request
1170 //   0000 0013 0011 00 000e 63646e 2e 6b65726e656c 2e 6f7267 - server_name
1171 //   ff01 0001 00 - renegotiation_info
1172 //   0023 0000 - session_ticket
1173 //   000a 0008 0006001700180019 - supported_groups
1174 //   000b 0002 0100 - ec_point_formats
1175 //   000d 0016 00140401040305010503060106030301030302010203 - signature_algorithms
1176         };
1177         struct client_hello *record;
1178         int len;
1179         int sni_len = sni ? strnlen(sni, 127) : 0;
1180
1181         len = sizeof(*record);
1182         if (sni_len)
1183                 len += 11 + strlen(sni);
1184         record = tls_get_outbuf(tls, len);
1185         memset(record, 0, len);
1186
1187         fill_handshake_record_hdr(record, HANDSHAKE_CLIENT_HELLO, len);
1188         record->proto_maj = TLS_MAJ;    /* the "requested" version of the protocol, */
1189         record->proto_min = TLS_MIN;    /* can be higher than one in record headers */
1190         tls_get_random(record->rand32, sizeof(record->rand32));
1191         if (TLS_DEBUG_FIXED_SECRETS)
1192                 memset(record->rand32, 0x11, sizeof(record->rand32));
1193         /* record->session_id_len = 0; - already is */
1194
1195         /* record->cipherid_len16_hi = 0; */
1196         record->cipherid_len16_lo = sizeof(record->cipherid);
1197         /* RFC 5746 Renegotiation Indication Extension - some servers will refuse to work with us otherwise */
1198         /*record->cipherid[0] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV >> 8; - zero */
1199         record->cipherid[1] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV & 0xff;
1200         if ((CIPHER_ID1 >> 8) != 0) record->cipherid[2] = CIPHER_ID1 >> 8;
1201         /*************************/ record->cipherid[3] = CIPHER_ID1 & 0xff;
1202 #if CIPHER_ID2
1203         if ((CIPHER_ID2 >> 8) != 0) record->cipherid[4] = CIPHER_ID2 >> 8;
1204         /*************************/ record->cipherid[5] = CIPHER_ID2 & 0xff;
1205 #endif
1206
1207         record->comprtypes_len = 1;
1208         /* record->comprtypes[0] = 0; */
1209
1210         if (sni_len) {
1211                 uint8_t *p = (void*)(record + 1);
1212                 //p[0] = 0;         //
1213                 p[1] = sni_len + 9; //ext_len
1214                 //p[2] = 0;             //
1215                 //p[3] = 0;             //extension_type
1216                 //p[4] = 0;         //
1217                 p[5] = sni_len + 5; //list len
1218                 //p[6] = 0;             //
1219                 p[7] = sni_len + 3;     //len of 1st SNI
1220                 //p[8] = 0;         //name type
1221                 //p[9] = 0;             //
1222                 p[10] = sni_len;        //name len
1223                 memcpy(&p[11], sni, sni_len);
1224         }
1225
1226         dbg(">> CLIENT_HELLO\n");
1227         /* Can hash it only when we know which MAC hash to use */
1228         /*xwrite_and_update_handshake_hash(tls, len); - WRONG! */
1229         xwrite_handshake_record(tls, len);
1230
1231         tls->hsd = xzalloc(sizeof(*tls->hsd) + len);
1232         tls->hsd->saved_client_hello_size = len;
1233         memcpy(tls->hsd->saved_client_hello, record, len);
1234         memcpy(tls->hsd->client_and_server_rand32, record->rand32, sizeof(record->rand32));
1235 }
1236
1237 static void get_server_hello(tls_state_t *tls)
1238 {
1239         struct server_hello {
1240                 struct record_hdr xhdr;
1241                 uint8_t type;
1242                 uint8_t len24_hi, len24_mid, len24_lo;
1243                 uint8_t proto_maj, proto_min;
1244                 uint8_t rand32[32]; /* first 4 bytes are unix time in BE format */
1245                 uint8_t session_id_len;
1246                 uint8_t session_id[32];
1247                 uint8_t cipherid_hi, cipherid_lo;
1248                 uint8_t comprtype;
1249                 /* extensions may follow, but only those which client offered in its Hello */
1250         };
1251
1252         struct server_hello *hp;
1253         uint8_t *cipherid;
1254         unsigned cipher;
1255         int len, len24;
1256
1257         len = tls_xread_handshake_block(tls, 74);
1258
1259         hp = (void*)tls->inbuf;
1260         // 74 bytes:
1261         // 02  000046 03|03   58|78|cf|c1 50|a5|49|ee|7e|29|48|71|fe|97|fa|e8|2d|19|87|72|90|84|9d|37|a3|f0|cb|6f|5f|e3|3c|2f |20  |d8|1a|78|96|52|d6|91|01|24|b3|d6|5b|b7|d0|6c|b3|e1|78|4e|3c|95|de|74|a0|ba|eb|a7|3a|ff|bd|a2|bf |00|9c |00|
1262         //SvHl len=70 maj.min unixtime^^^ 28randbytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^ slen sid32bytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cipSel comprSel
1263         if (hp->type != HANDSHAKE_SERVER_HELLO
1264          || hp->len24_hi  != 0
1265          || hp->len24_mid != 0
1266          /* hp->len24_lo checked later */
1267          || hp->proto_maj != TLS_MAJ
1268          || hp->proto_min != TLS_MIN
1269         ) {
1270                 bad_record_die(tls, "'server hello'", len);
1271         }
1272
1273         cipherid = &hp->cipherid_hi;
1274         len24 = hp->len24_lo;
1275         if (hp->session_id_len != 32) {
1276                 if (hp->session_id_len != 0)
1277                         tls_error_die(tls);
1278
1279                 // session_id_len == 0: no session id
1280                 // "The server
1281                 // may return an empty session_id to indicate that the session will
1282                 // not be cached and therefore cannot be resumed."
1283                 cipherid -= 32;
1284                 len24 += 32; /* what len would be if session id would be present */
1285         }
1286
1287         if (len24 < 70
1288 //       || cipherid[0]  != (CIPHER_ID >> 8)
1289 //       || cipherid[1]  != (CIPHER_ID & 0xff)
1290 //       || cipherid[2]  != 0 /* comprtype */
1291         ) {
1292                 tls_error_die(tls);
1293         }
1294         dbg("<< SERVER_HELLO\n");
1295
1296         memcpy(tls->hsd->client_and_server_rand32 + 32, hp->rand32, sizeof(hp->rand32));
1297
1298         tls->cipher_id = cipher = 0x100 * cipherid[0] + cipherid[1];
1299         dbg("server chose cipher %04x\n", cipher);
1300
1301         if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA) {
1302                 tls->key_size = AES128_KEYSIZE;
1303                 tls->MAC_size = SHA1_OUTSIZE;
1304                 sha1_begin(&tls->hsd->handshake_hash_ctx);
1305         }
1306         else { /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
1307                 tls->key_size = AES256_KEYSIZE;
1308                 tls->MAC_size = SHA256_OUTSIZE;
1309                 sha256_begin(&tls->hsd->handshake_hash_ctx);
1310         }
1311         hash_handshake(tls, ">> client hello hash:%s",
1312                 tls->hsd->saved_client_hello, tls->hsd->saved_client_hello_size
1313         );
1314         hash_handshake(tls, "<< server hello hash:%s",
1315                 tls->inbuf + RECHDR_LEN, len
1316         );
1317 }
1318
1319 static void get_server_cert(tls_state_t *tls)
1320 {
1321         struct record_hdr *xhdr;
1322         uint8_t *certbuf;
1323         int len, len1;
1324
1325         len = tls_xread_handshake_block(tls, 10);
1326
1327         xhdr = (void*)tls->inbuf;
1328         certbuf = (void*)(xhdr + 1);
1329         if (certbuf[0] != HANDSHAKE_CERTIFICATE)
1330                 tls_error_die(tls);
1331         dbg("<< CERTIFICATE\n");
1332         // 4392 bytes:
1333         // 0b  00|11|24 00|11|21 00|05|b0 30|82|05|ac|30|82|04|94|a0|03|02|01|02|02|11|00|9f|85|bf|66|4b|0c|dd|af|ca|50|86|79|50|1b|2b|e4|30|0d...
1334         //Cert len=4388 ChainLen CertLen^ DER encoded X509 starts here. openssl x509 -in FILE -inform DER -noout -text
1335         len1 = get24be(certbuf + 1);
1336         if (len1 > len - 4) tls_error_die(tls);
1337         len = len1;
1338         len1 = get24be(certbuf + 4);
1339         if (len1 > len - 3) tls_error_die(tls);
1340         len = len1;
1341         len1 = get24be(certbuf + 7);
1342         if (len1 > len - 3) tls_error_die(tls);
1343         len = len1;
1344
1345         if (len)
1346                 find_key_in_der_cert(tls, certbuf + 10, len);
1347 }
1348
1349 static void send_empty_client_cert(tls_state_t *tls)
1350 {
1351         struct client_empty_cert {
1352                 uint8_t type;
1353                 uint8_t len24_hi, len24_mid, len24_lo;
1354                 uint8_t cert_chain_len24_hi, cert_chain_len24_mid, cert_chain_len24_lo;
1355         };
1356         struct client_empty_cert *record;
1357
1358         record = tls_get_outbuf(tls, sizeof(*record));
1359 //FIXME: can just memcpy a ready-made one.
1360         fill_handshake_record_hdr(record, HANDSHAKE_CERTIFICATE, sizeof(*record));
1361         record->cert_chain_len24_hi = 0;
1362         record->cert_chain_len24_mid = 0;
1363         record->cert_chain_len24_lo = 0;
1364
1365         dbg(">> CERTIFICATE\n");
1366         xwrite_and_update_handshake_hash(tls, sizeof(*record));
1367 }
1368
1369 static void send_client_key_exchange(tls_state_t *tls)
1370 {
1371         struct client_key_exchange {
1372                 uint8_t type;
1373                 uint8_t len24_hi, len24_mid, len24_lo;
1374                 /* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */
1375                 uint8_t keylen16_hi, keylen16_lo;
1376                 uint8_t key[4 * 1024]; // size??
1377         };
1378 //FIXME: better size estimate
1379         struct client_key_exchange *record = tls_get_outbuf(tls, sizeof(*record));
1380         uint8_t rsa_premaster[RSA_PREMASTER_SIZE];
1381         int len;
1382
1383         tls_get_random(rsa_premaster, sizeof(rsa_premaster));
1384         if (TLS_DEBUG_FIXED_SECRETS)
1385                 memset(rsa_premaster, 0x44, sizeof(rsa_premaster));
1386         // RFC 5246
1387         // "Note: The version number in the PreMasterSecret is the version
1388         // offered by the client in the ClientHello.client_version, not the
1389         // version negotiated for the connection."
1390         rsa_premaster[0] = TLS_MAJ;
1391         rsa_premaster[1] = TLS_MIN;
1392         len = psRsaEncryptPub(/*pool:*/ NULL,
1393                 /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key,
1394                 rsa_premaster, /*inlen:*/ sizeof(rsa_premaster),
1395                 record->key, sizeof(record->key),
1396                 data_param_ignored
1397         );
1398         record->keylen16_hi = len >> 8;
1399         record->keylen16_lo = len & 0xff;
1400         len += 2;
1401         record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE;
1402         record->len24_hi  = 0;
1403         record->len24_mid = len >> 8;
1404         record->len24_lo  = len & 0xff;
1405         len += 4;
1406
1407         dbg(">> CLIENT_KEY_EXCHANGE\n");
1408         xwrite_and_update_handshake_hash(tls, len);
1409
1410         // RFC 5246
1411         // For all key exchange methods, the same algorithm is used to convert
1412         // the pre_master_secret into the master_secret.  The pre_master_secret
1413         // should be deleted from memory once the master_secret has been
1414         // computed.
1415         //      master_secret = PRF(pre_master_secret, "master secret",
1416         //                          ClientHello.random + ServerHello.random)
1417         //                          [0..47];
1418         // The master secret is always exactly 48 bytes in length.  The length
1419         // of the premaster secret will vary depending on key exchange method.
1420         prf_hmac(tls,
1421                 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
1422                 rsa_premaster, sizeof(rsa_premaster),
1423                 "master secret",
1424                 tls->hsd->client_and_server_rand32, sizeof(tls->hsd->client_and_server_rand32)
1425         );
1426         dump_hex("master secret:%s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
1427
1428         // RFC 5246
1429         // 6.3.  Key Calculation
1430         //
1431         // The Record Protocol requires an algorithm to generate keys required
1432         // by the current connection state (see Appendix A.6) from the security
1433         // parameters provided by the handshake protocol.
1434         //
1435         // The master secret is expanded into a sequence of secure bytes, which
1436         // is then split to a client write MAC key, a server write MAC key, a
1437         // client write encryption key, and a server write encryption key.  Each
1438         // of these is generated from the byte sequence in that order.  Unused
1439         // values are empty.  Some AEAD ciphers may additionally require a
1440         // client write IV and a server write IV (see Section 6.2.3.3).
1441         //
1442         // When keys and MAC keys are generated, the master secret is used as an
1443         // entropy source.
1444         //
1445         // To generate the key material, compute
1446         //
1447         //    key_block = PRF(SecurityParameters.master_secret,
1448         //                    "key expansion",
1449         //                    SecurityParameters.server_random +
1450         //                    SecurityParameters.client_random);
1451         //
1452         // until enough output has been generated.  Then, the key_block is
1453         // partitioned as follows:
1454         //
1455         //    client_write_MAC_key[SecurityParameters.mac_key_length]
1456         //    server_write_MAC_key[SecurityParameters.mac_key_length]
1457         //    client_write_key[SecurityParameters.enc_key_length]
1458         //    server_write_key[SecurityParameters.enc_key_length]
1459         //    client_write_IV[SecurityParameters.fixed_iv_length]
1460         //    server_write_IV[SecurityParameters.fixed_iv_length]
1461         {
1462                 uint8_t tmp64[64];
1463
1464                 /* make "server_rand32 + client_rand32" */
1465                 memcpy(&tmp64[0] , &tls->hsd->client_and_server_rand32[32], 32);
1466                 memcpy(&tmp64[32], &tls->hsd->client_and_server_rand32[0] , 32);
1467
1468                 prf_hmac(tls,
1469                         tls->client_write_MAC_key, 2 * (tls->MAC_size + tls->key_size),
1470                         // also fills:
1471                         // server_write_MAC_key[]
1472                         // client_write_key[]
1473                         // server_write_key[]
1474                         tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
1475                         "key expansion",
1476                         tmp64, 64
1477                 );
1478                 tls->client_write_key = tls->client_write_MAC_key + (2 * tls->MAC_size);
1479                 tls->server_write_key = tls->client_write_key + tls->key_size;
1480                 dump_hex("client_write_MAC_key:%s\n",
1481                         tls->client_write_MAC_key, tls->MAC_size
1482                 );
1483                 dump_hex("client_write_key:%s\n",
1484                         tls->client_write_key, tls->key_size
1485                 );
1486         }
1487 }
1488
1489 static const uint8_t rec_CHANGE_CIPHER_SPEC[] = {
1490         RECORD_TYPE_CHANGE_CIPHER_SPEC, TLS_MAJ, TLS_MIN, 00, 01,
1491         01
1492 };
1493
1494 static void send_change_cipher_spec(tls_state_t *tls)
1495 {
1496         dbg(">> CHANGE_CIPHER_SPEC\n");
1497         xwrite(tls->ofd, rec_CHANGE_CIPHER_SPEC, sizeof(rec_CHANGE_CIPHER_SPEC));
1498 }
1499
1500 // 7.4.9.  Finished
1501 // A Finished message is always sent immediately after a change
1502 // cipher spec message to verify that the key exchange and
1503 // authentication processes were successful.  It is essential that a
1504 // change cipher spec message be received between the other handshake
1505 // messages and the Finished message.
1506 //...
1507 // The Finished message is the first one protected with the just
1508 // negotiated algorithms, keys, and secrets.  Recipients of Finished
1509 // messages MUST verify that the contents are correct.  Once a side
1510 // has sent its Finished message and received and validated the
1511 // Finished message from its peer, it may begin to send and receive
1512 // application data over the connection.
1513 //...
1514 // struct {
1515 //     opaque verify_data[verify_data_length];
1516 // } Finished;
1517 //
1518 // verify_data
1519 //    PRF(master_secret, finished_label, Hash(handshake_messages))
1520 //       [0..verify_data_length-1];
1521 //
1522 // finished_label
1523 //    For Finished messages sent by the client, the string
1524 //    "client finished".  For Finished messages sent by the server,
1525 //    the string "server finished".
1526 //
1527 // Hash denotes a Hash of the handshake messages.  For the PRF
1528 // defined in Section 5, the Hash MUST be the Hash used as the basis
1529 // for the PRF.  Any cipher suite which defines a different PRF MUST
1530 // also define the Hash to use in the Finished computation.
1531 //
1532 // In previous versions of TLS, the verify_data was always 12 octets
1533 // long.  In the current version of TLS, it depends on the cipher
1534 // suite.  Any cipher suite which does not explicitly specify
1535 // verify_data_length has a verify_data_length equal to 12.  This
1536 // includes all existing cipher suites.
1537 static void send_client_finished(tls_state_t *tls)
1538 {
1539         struct finished {
1540                 uint8_t type;
1541                 uint8_t len24_hi, len24_mid, len24_lo;
1542                 uint8_t prf_result[12];
1543         };
1544         struct finished *record = tls_get_outbuf(tls, sizeof(*record));
1545         uint8_t handshake_hash[TLS_MAX_MAC_SIZE];
1546         unsigned len;
1547
1548         fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record));
1549
1550         len = get_handshake_hash(tls, handshake_hash);
1551         prf_hmac(tls,
1552                 record->prf_result, sizeof(record->prf_result),
1553                 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
1554                 "client finished",
1555                 handshake_hash, len
1556         );
1557         dump_hex("from secret: %s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
1558         dump_hex("from labelSeed: %s", "client finished", sizeof("client finished")-1);
1559         dump_hex("%s\n", handshake_hash, sizeof(handshake_hash));
1560         dump_hex("=> digest: %s\n", record->prf_result, sizeof(record->prf_result));
1561
1562         dbg(">> FINISHED\n");
1563         xwrite_encrypted(tls, sizeof(*record), RECORD_TYPE_HANDSHAKE);
1564 }
1565
1566 void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni)
1567 {
1568         // Client              RFC 5246                Server
1569         // (*) - optional messages, not always sent
1570         //
1571         // ClientHello          ------->
1572         //                                        ServerHello
1573         //                                       Certificate*
1574         //                                 ServerKeyExchange*
1575         //                                CertificateRequest*
1576         //                      <-------      ServerHelloDone
1577         // Certificate*
1578         // ClientKeyExchange
1579         // CertificateVerify*
1580         // [ChangeCipherSpec]
1581         // Finished             ------->
1582         //                                 [ChangeCipherSpec]
1583         //                      <-------             Finished
1584         // Application Data     <------>     Application Data
1585         int len;
1586
1587         send_client_hello_and_alloc_hsd(tls, sni);
1588         get_server_hello(tls);
1589
1590         // RFC 5246
1591         // The server MUST send a Certificate message whenever the agreed-
1592         // upon key exchange method uses certificates for authentication
1593         // (this includes all key exchange methods defined in this document
1594         // except DH_anon).  This message will always immediately follow the
1595         // ServerHello message.
1596         //
1597         // IOW: in practice, Certificate *always* follows.
1598         // (for example, kernel.org does not even accept DH_anon cipher id)
1599         get_server_cert(tls);
1600
1601         len = tls_xread_handshake_block(tls, 4);
1602         if (tls->inbuf[RECHDR_LEN] == HANDSHAKE_SERVER_KEY_EXCHANGE) {
1603                 // 459 bytes:
1604                 // 0c   00|01|c7 03|00|17|41|04|87|94|2e|2f|68|d0|c9|f4|97|a8|2d|ef|ed|67|ea|c6|f3|b3|56|47|5d|27|b6|bd|ee|70|25|30|5e|b0|8e|f6|21|5a...
1605                 //SvKey len=455^
1606                 // with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes:
1607                 // 0c   00|01|c9 03|00|17|41|04|cd|9b|b4|29|1f|f6|b0|c2|84|82|7f|29|6a|47|4e|ec|87|0b|c1|9c|69|e1|f8|c6|d0|53|e9|27|90|a5|c8|02|15|75...
1608                 dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len);
1609 //probably need to save it
1610                 len = tls_xread_handshake_block(tls, 4);
1611         }
1612
1613         if (tls->inbuf[RECHDR_LEN] == HANDSHAKE_CERTIFICATE_REQUEST) {
1614                 dbg("<< CERTIFICATE_REQUEST\n");
1615                 // RFC 5246: "If no suitable certificate is available,
1616                 // the client MUST send a certificate message containing no
1617                 // certificates.  That is, the certificate_list structure has a
1618                 // length of zero. ...
1619                 // Client certificates are sent using the Certificate structure
1620                 // defined in Section 7.4.2."
1621                 // (i.e. the same format as server certs)
1622                 send_empty_client_cert(tls);
1623                 len = tls_xread_handshake_block(tls, 4);
1624         }
1625
1626         if (tls->inbuf[RECHDR_LEN] != HANDSHAKE_SERVER_HELLO_DONE) {
1627                 bad_record_die(tls, "'server hello done'", len);
1628         }
1629         // 0e 000000 (len:0)
1630         dbg("<< SERVER_HELLO_DONE\n");
1631
1632         send_client_key_exchange(tls);
1633
1634         send_change_cipher_spec(tls);
1635         /* from now on we should send encrypted */
1636         /* tls->write_seq64_be = 0; - already is */
1637         tls->encrypt_on_write = 1;
1638
1639         send_client_finished(tls);
1640
1641         /* Get CHANGE_CIPHER_SPEC */
1642         len = tls_xread_record(tls);
1643         if (len != 1 || memcmp(tls->inbuf, rec_CHANGE_CIPHER_SPEC, 6) != 0)
1644                 bad_record_die(tls, "switch to encrypted traffic", len);
1645         dbg("<< CHANGE_CIPHER_SPEC\n");
1646         if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256)
1647                 tls->min_encrypted_len_on_read = tls->MAC_size;
1648         else
1649                 /* all incoming packets now should be encrypted and have IV + MAC + padding */
1650                 tls->min_encrypted_len_on_read = AES_BLOCKSIZE + tls->MAC_size + AES_BLOCKSIZE;
1651
1652         /* Get (encrypted) FINISHED from the server */
1653         len = tls_xread_record(tls);
1654         if (len < 4 || tls->inbuf[RECHDR_LEN] != HANDSHAKE_FINISHED)
1655                 tls_error_die(tls);
1656         dbg("<< FINISHED\n");
1657         /* application data can be sent/received */
1658
1659         /* free handshake data */
1660 //      if (PARANOIA)
1661 //              memset(tls->hsd, 0, tls->hsd->hsd_size);
1662         free(tls->hsd);
1663         tls->hsd = NULL;
1664 }
1665
1666 static void tls_xwrite(tls_state_t *tls, int len)
1667 {
1668         dbg(">> DATA\n");
1669         xwrite_encrypted(tls, len, RECORD_TYPE_APPLICATION_DATA);
1670 }
1671
1672 // To run a test server using openssl:
1673 // openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
1674 // openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1
1675 //
1676 // Unencryped SHA256 example:
1677 // openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
1678 // openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL
1679 // openssl s_client -connect 127.0.0.1:4433 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL-SHA256
1680
1681 void FAST_FUNC tls_run_copy_loop(tls_state_t *tls)
1682 {
1683         fd_set readfds;
1684         int inbuf_size;
1685         const int INBUF_STEP = 4 * 1024;
1686
1687 //TODO: convert to poll
1688         /* Select loop copying stdin to ofd, and ifd to stdout */
1689         FD_ZERO(&readfds);
1690         FD_SET(tls->ifd, &readfds);
1691         FD_SET(STDIN_FILENO, &readfds);
1692
1693         inbuf_size = INBUF_STEP;
1694         for (;;) {
1695                 fd_set testfds;
1696                 int nread;
1697
1698                 testfds = readfds;
1699                 if (select(tls->ifd + 1, &testfds, NULL, NULL, NULL) < 0)
1700                         bb_perror_msg_and_die("select");
1701
1702                 if (FD_ISSET(STDIN_FILENO, &testfds)) {
1703                         void *buf;
1704
1705                         dbg("STDIN HAS DATA\n");
1706                         buf = tls_get_outbuf(tls, inbuf_size);
1707                         nread = safe_read(STDIN_FILENO, buf, inbuf_size);
1708                         if (nread < 1) {
1709                                 /* We'd want to do this: */
1710                                 /* Close outgoing half-connection so they get EOF,
1711                                  * but leave incoming alone so we can see response
1712                                  */
1713                                 //shutdown(tls->ofd, SHUT_WR);
1714                                 /* But TLS has no way to encode this,
1715                                  * doubt it's ok to do it "raw"
1716                                  */
1717                                 FD_CLR(STDIN_FILENO, &readfds);
1718                                 tls_free_outbuf(tls); /* mem usage optimization */
1719                         } else {
1720                                 if (nread == inbuf_size) {
1721                                         /* TLS has per record overhead, if input comes fast,
1722                                          * read, encrypt and send bigger chunks
1723                                          */
1724                                         inbuf_size += INBUF_STEP;
1725                                         if (inbuf_size > TLS_MAX_OUTBUF)
1726                                                 inbuf_size = TLS_MAX_OUTBUF;
1727                                 }
1728                                 tls_xwrite(tls, nread);
1729                         }
1730                 }
1731                 if (FD_ISSET(tls->ifd, &testfds)) {
1732                         dbg("NETWORK HAS DATA\n");
1733  read_record:
1734                         nread = tls_xread_record(tls);
1735                         if (nread < 1) {
1736                                 /* TLS protocol has no real concept of one-sided shutdowns:
1737                                  * if we get "TLS EOF" from the peer, writes will fail too
1738                                  */
1739                                 //FD_CLR(tls->ifd, &readfds);
1740                                 //close(STDOUT_FILENO);
1741                                 //tls_free_inbuf(tls); /* mem usage optimization */
1742                                 //continue;
1743                                 break;
1744                         }
1745                         if (tls->inbuf[0] != RECORD_TYPE_APPLICATION_DATA)
1746                                 bb_error_msg_and_die("unexpected record type %d", tls->inbuf[0]);
1747                         xwrite(STDOUT_FILENO, tls->inbuf + RECHDR_LEN, nread);
1748                         /* We may already have a complete next record buffered,
1749                          * can process it without network reads (and possible blocking)
1750                          */
1751                         if (tls_has_buffered_record(tls))
1752                                 goto read_record;
1753                 }
1754         }
1755 }