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