2 * Copyright (C) 2017 Denys Vlasenko
4 * Licensed under GPLv2, see file LICENSE in this source tree.
7 //config: bool "tls (debugging)"
10 //applet:IF_TLS(APPLET(tls, BB_DIR_USR_BIN, BB_SUID_DROP))
12 //kbuild:lib-$(CONFIG_TLS) += tls.o
13 //kbuild:lib-$(CONFIG_TLS) += tls_pstm.o
14 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_montgomery_reduce.o
15 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_mul_comba.o
16 //kbuild:lib-$(CONFIG_TLS) += tls_pstm_sqr_comba.o
17 //kbuild:lib-$(CONFIG_TLS) += tls_rsa.o
18 //kbuild:lib-$(CONFIG_TLS) += tls_aes.o
19 ////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o
21 //usage:#define tls_trivial_usage
22 //usage: "HOST[:PORT]"
23 //usage:#define tls_full_usage "\n\n"
26 #include "common_bufsiz.h"
29 #define TLS_DEBUG_HASH 1
30 #define TLS_DEBUG_DER 0
33 # define dbg(...) fprintf(stderr, __VA_ARGS__)
35 # define dbg(...) ((void)0)
39 # define dbg_der(...) fprintf(stderr, __VA_ARGS__)
41 # define dbg_der(...) ((void)0)
44 #define RECORD_TYPE_CHANGE_CIPHER_SPEC 20
45 #define RECORD_TYPE_ALERT 21
46 #define RECORD_TYPE_HANDSHAKE 22
47 #define RECORD_TYPE_APPLICATION_DATA 23
49 #define HANDSHAKE_HELLO_REQUEST 0
50 #define HANDSHAKE_CLIENT_HELLO 1
51 #define HANDSHAKE_SERVER_HELLO 2
52 #define HANDSHAKE_HELLO_VERIFY_REQUEST 3
53 #define HANDSHAKE_NEW_SESSION_TICKET 4
54 #define HANDSHAKE_CERTIFICATE 11
55 #define HANDSHAKE_SERVER_KEY_EXCHANGE 12
56 #define HANDSHAKE_CERTIFICATE_REQUEST 13
57 #define HANDSHAKE_SERVER_HELLO_DONE 14
58 #define HANDSHAKE_CERTIFICATE_VERIFY 15
59 #define HANDSHAKE_CLIENT_KEY_EXCHANGE 16
60 #define HANDSHAKE_FINISHED 20
62 #define SSL_HS_RANDOM_SIZE 32
63 #define SSL_HS_RSA_PREMASTER_SIZE 48
65 #define SSL_NULL_WITH_NULL_NULL 0x0000
66 #define SSL_RSA_WITH_NULL_MD5 0x0001
67 #define SSL_RSA_WITH_NULL_SHA 0x0002
68 #define SSL_RSA_WITH_RC4_128_MD5 0x0004
69 #define SSL_RSA_WITH_RC4_128_SHA 0x0005
70 #define SSL_RSA_WITH_3DES_EDE_CBC_SHA 0x000A /* 10 */
71 #define TLS_RSA_WITH_AES_128_CBC_SHA 0x002F /* 47 */
72 #define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035 /* 53 */
73 #define TLS_RSA_WITH_NULL_SHA256 0x003B /* 59 */
75 #define TLS_EMPTY_RENEGOTIATION_INFO_SCSV 0x00FF
77 #define TLS_RSA_WITH_IDEA_CBC_SHA 0x0007 /* 7 */
78 #define SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016 /* 22 */
79 #define SSL_DH_anon_WITH_RC4_128_MD5 0x0018 /* 24 */
80 #define SSL_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B /* 27 */
81 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033 /* 51 */
82 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039 /* 57 */
83 #define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067 /* 103 */
84 #define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B /* 107 */
85 #define TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034 /* 52 */
86 #define TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A /* 58 */
87 #define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C /* 60 */
88 #define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D /* 61 */
89 #define TLS_RSA_WITH_SEED_CBC_SHA 0x0096 /* 150 */
90 #define TLS_PSK_WITH_AES_128_CBC_SHA 0x008C /* 140 */
91 #define TLS_PSK_WITH_AES_128_CBC_SHA256 0x00AE /* 174 */
92 #define TLS_PSK_WITH_AES_256_CBC_SHA384 0x00AF /* 175 */
93 #define TLS_PSK_WITH_AES_256_CBC_SHA 0x008D /* 141 */
94 #define TLS_DHE_PSK_WITH_AES_128_CBC_SHA 0x0090 /* 144 */
95 #define TLS_DHE_PSK_WITH_AES_256_CBC_SHA 0x0091 /* 145 */
96 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004 /* 49156 */
97 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005 /* 49157 */
98 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009 /* 49161 */
99 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A /* 49162 */
100 #define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012 /* 49170 */
101 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013 /* 49171 */
102 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014 /* 49172 */
103 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E /* 49166 */
104 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F /* 49167 */
105 #define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023 /* 49187 */
106 #define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024 /* 49188 */
107 #define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025 /* 49189 */
108 #define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026 /* 49190 */
109 #define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027 /* 49191 */
110 #define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028 /* 49192 */
111 #define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029 /* 49193 */
112 #define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A /* 49194 */
114 // RFC 5288 "AES Galois Counter Mode (GCM) Cipher Suites for TLS"
115 #define TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C /* 156 */
116 #define TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D /* 157 */
117 #define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B /* 49195 */
118 #define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C /* 49196 */
119 #define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D /* 49197 */
120 #define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E /* 49198 */
121 #define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F /* 49199 */
122 #define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030 /* 49200 */
123 #define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031 /* 49201 */
124 #define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032 /* 49202 */
126 //Tested against kernel.org:
130 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE
134 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE *** matrixssl uses this on my box
135 //#define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
137 //#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 // SSL_ALERT_HANDSHAKE_FAILURE
138 //#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
139 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 // ok, recvs SERVER_KEY_EXCHANGE
140 //#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
141 //#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
142 //#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
143 //#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
144 //#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
145 //#define CIPHER_ID TLS_RSA_WITH_AES_256_GCM_SHA384 // ok, no SERVER_KEY_EXCHANGE
146 //#define CIPHER_ID TLS_RSA_WITH_AES_128_GCM_SHA256 // ok, no SERVER_KEY_EXCHANGE *** select this?
147 //#define CIPHER_ID TLS_DH_anon_WITH_AES_256_CBC_SHA // SSL_ALERT_HANDSHAKE_FAILURE
148 //^^^^^^^^^^^^^^^^^^^^^^^ (tested b/c this one doesn't req server certs... no luck)
149 //test TLS_RSA_WITH_AES_128_CBC_SHA, in TLS 1.2 it's mandated to be always supported
151 // works against "openssl s_server -cipher NULL"
152 // and against wolfssl-3.9.10-stable/examples/server/server.c:
153 //#define CIPHER_ID TLS_RSA_WITH_NULL_SHA256 // for testing (does everything except encrypting)
154 // works against wolfssl-3.9.10-stable/examples/server/server.c
155 // (getting back and decrypt ok first application data message)
156 #define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
166 MAX_TLS_RECORD = (1 << 14),
167 OUTBUF_PFX = 8 + AES_BLOCKSIZE, /* header + IV */
168 OUTBUF_SFX = SHA256_OUTSIZE + AES_BLOCKSIZE, /* MAC + padding */
169 MAX_OTBUF = MAX_TLS_RECORD - OUTBUF_PFX - OUTBUF_SFX,
174 uint8_t proto_maj, proto_min;
175 uint8_t len16_hi, len16_lo;
178 typedef struct tls_state {
181 psRsaKey_t server_rsa_pub_key;
183 sha256_ctx_t handshake_sha256_ctx;
185 uint8_t client_and_server_rand32[2 * 32];
186 uint8_t master_secret[48];
188 uint8_t encrypt_on_write;
189 int min_encrypted_len_on_read;
190 uint8_t client_write_MAC_key[SHA256_OUTSIZE];
191 uint8_t server_write_MAC_key[SHA256_OUTSIZE];
192 uint8_t client_write_key[AES256_KEYSIZE];
193 uint8_t server_write_key[AES256_KEYSIZE];
196 // Each connection state contains a sequence number, which is
197 // maintained separately for read and write states. The sequence
198 // number MUST be set to zero whenever a connection state is made the
199 // active state. Sequence numbers are of type uint64 and may not
201 uint64_t write_seq64_be;
207 // |6.2.1. Fragmentation
208 // | The record layer fragments information blocks into TLSPlaintext
209 // | records carrying data in chunks of 2^14 bytes or less. Client
210 // | message boundaries are not preserved in the record layer (i.e.,
211 // | multiple client messages of the same ContentType MAY be coalesced
212 // | into a single TLSPlaintext record, or a single message MAY be
213 // | fragmented across several records)
216 // | The length (in bytes) of the following TLSPlaintext.fragment.
217 // | The length MUST NOT exceed 2^14.
219 // | 6.2.2. Record Compression and Decompression
221 // | Compression must be lossless and may not increase the content length
222 // | by more than 1024 bytes. If the decompression function encounters a
223 // | TLSCompressed.fragment that would decompress to a length in excess of
224 // | 2^14 bytes, it MUST report a fatal decompression failure error.
227 // | The length (in bytes) of the following TLSCompressed.fragment.
228 // | The length MUST NOT exceed 2^14 + 1024.
230 // Since our buffer also contains 5-byte headers, make it a bit bigger:
235 uint8_t inbuf[20*1024];
239 static unsigned get24be(const uint8_t *p)
241 return 0x100*(0x100*p[0] + p[1]) + p[2];
245 static void dump_hex(const char *fmt, const void *vp, int len)
247 char hexbuf[32 * 1024 + 4];
248 const uint8_t *p = vp;
250 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
254 static void dump_tls_record(const void *vp, int len)
256 const uint8_t *p = vp;
261 dump_hex("< |%s|\n", p, len);
264 xhdr_len = 0x100*p[3] + p[4];
265 dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len);
268 if (len >= 4 && p[-5] == RECORD_TYPE_HANDSHAKE) {
269 unsigned len24 = get24be(p + 1);
270 dbg(" type:%u len24:%u", p[0], len24);
274 dump_hex(" |%s|\n", p, xhdr_len);
281 void tls_get_random(void *buf, unsigned len)
283 if (len != open_read_close("/dev/urandom", buf, len))
287 //TODO rename this to sha256_hash, and sha256_hash -> sha256_update
288 static void hash_sha256(uint8_t out[SHA256_OUTSIZE], const void *data, unsigned size)
292 sha256_hash(&ctx, data, size);
293 sha256_end(&ctx, out);
296 /* Nondestructively see the current hash value */
297 static void sha256_peek(sha256_ctx_t *ctx, void *buffer)
299 sha256_ctx_t ctx_copy = *ctx;
300 sha256_end(&ctx_copy, buffer);
304 static void sha256_hash_dbg(const char *fmt, sha256_ctx_t *ctx, const void *buffer, size_t len)
306 uint8_t h[SHA256_OUTSIZE];
308 sha256_hash(ctx, buffer, len);
309 dump_hex(fmt, buffer, len);
310 dbg(" (%u) ", (int)len);
312 dump_hex("%s\n", h, SHA256_OUTSIZE);
315 # define sha256_hash_dbg(fmt, ctx, buffer, len) \
316 sha256_hash(ctx, buffer, len)
320 // HMAC(key, text) based on a hash H (say, sha256) is:
321 // ipad = [0x36 x INSIZE]
322 // opad = [0x5c x INSIZE]
323 // HMAC(key, text) = H((key XOR opad) + H((key XOR ipad) + text))
325 // H(key XOR opad) and H(key XOR ipad) can be precomputed
326 // if we often need HMAC hmac with the same key.
328 // text is often given in disjoint pieces.
329 static void hmac_sha256_precomputed_v(uint8_t out[SHA256_OUTSIZE],
330 sha256_ctx_t *hashed_key_xor_ipad,
331 sha256_ctx_t *hashed_key_xor_opad,
336 /* hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
337 /* hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
339 /* calculate out = H((key XOR ipad) + text) */
340 while ((text = va_arg(va, uint8_t*)) != NULL) {
341 unsigned text_size = va_arg(va, unsigned);
342 sha256_hash(hashed_key_xor_ipad, text, text_size);
344 sha256_end(hashed_key_xor_ipad, out);
346 /* out = H((key XOR opad) + out) */
347 sha256_hash(hashed_key_xor_opad, out, SHA256_OUTSIZE);
348 sha256_end(hashed_key_xor_opad, out);
351 static void hmac_sha256(uint8_t out[SHA256_OUTSIZE], uint8_t *key, unsigned key_size, ...)
353 sha256_ctx_t hashed_key_xor_ipad;
354 sha256_ctx_t hashed_key_xor_opad;
355 uint8_t key_xor_ipad[SHA256_INSIZE];
356 uint8_t key_xor_opad[SHA256_INSIZE];
357 uint8_t tempkey[SHA256_OUTSIZE];
361 va_start(va, key_size);
363 // "The authentication key can be of any length up to INSIZE, the
364 // block length of the hash function. Applications that use keys longer
365 // than INSIZE bytes will first hash the key using H and then use the
366 // resultant OUTSIZE byte string as the actual key to HMAC."
367 if (key_size > SHA256_INSIZE) {
368 hash_sha256(tempkey, key, key_size);
370 key_size = SHA256_OUTSIZE;
373 for (i = 0; i < key_size; i++) {
374 key_xor_ipad[i] = key[i] ^ 0x36;
375 key_xor_opad[i] = key[i] ^ 0x5c;
377 for (; i < SHA256_INSIZE; i++) {
378 key_xor_ipad[i] = 0x36;
379 key_xor_opad[i] = 0x5c;
381 sha256_begin(&hashed_key_xor_ipad);
382 sha256_hash(&hashed_key_xor_ipad, key_xor_ipad, SHA256_INSIZE);
383 sha256_begin(&hashed_key_xor_opad);
384 sha256_hash(&hashed_key_xor_opad, key_xor_opad, SHA256_INSIZE);
386 hmac_sha256_precomputed_v(out, &hashed_key_xor_ipad, &hashed_key_xor_opad, va);
391 // 5. HMAC and the Pseudorandom Function
393 // In this section, we define one PRF, based on HMAC. This PRF with the
394 // SHA-256 hash function is used for all cipher suites defined in this
395 // document and in TLS documents published prior to this document when
396 // TLS 1.2 is negotiated.
398 // P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
399 // HMAC_hash(secret, A(2) + seed) +
400 // HMAC_hash(secret, A(3) + seed) + ...
401 // where + indicates concatenation.
402 // A() is defined as:
404 // A(1) = HMAC_hash(secret, A(0)) = HMAC_hash(secret, seed)
405 // A(i) = HMAC_hash(secret, A(i-1))
406 // P_hash can be iterated as many times as necessary to produce the
407 // required quantity of data. For example, if P_SHA256 is being used to
408 // create 80 bytes of data, it will have to be iterated three times
409 // (through A(3)), creating 96 bytes of output data; the last 16 bytes
410 // of the final iteration will then be discarded, leaving 80 bytes of
413 // TLS's PRF is created by applying P_hash to the secret as:
415 // PRF(secret, label, seed) = P_<hash>(secret, label + seed)
417 // The label is an ASCII string.
418 static void prf_hmac_sha256(
419 uint8_t *outbuf, unsigned outbuf_size,
420 uint8_t *secret, unsigned secret_size,
422 uint8_t *seed, unsigned seed_size)
424 uint8_t a[SHA256_OUTSIZE];
425 uint8_t *out_p = outbuf;
426 unsigned label_size = strlen(label);
428 /* In P_hash() calculation, "seed" is "label + seed": */
429 #define SEED label, label_size, seed, seed_size
430 #define SECRET secret, secret_size
431 #define A a, (int)(sizeof(a))
433 /* A(1) = HMAC_hash(secret, seed) */
434 hmac_sha256(a, SECRET, SEED, NULL);
435 //TODO: convert hmac_sha256 to precomputed
438 /* HMAC_hash(secret, A(1) + seed) */
439 if (outbuf_size <= SHA256_OUTSIZE) {
440 /* Last, possibly incomplete, block */
441 /* (use a[] as temp buffer) */
442 hmac_sha256(a, SECRET, A, SEED, NULL);
443 memcpy(out_p, a, outbuf_size);
446 /* Not last block. Store directly to result buffer */
447 hmac_sha256(out_p, SECRET, A, SEED, NULL);
448 out_p += SHA256_OUTSIZE;
449 outbuf_size -= SHA256_OUTSIZE;
450 /* A(2) = HMAC_hash(secret, A(1)) */
451 hmac_sha256(a, SECRET, A, NULL);
458 static tls_state_t *new_tls_state(void)
460 tls_state_t *tls = xzalloc(sizeof(*tls));
462 sha256_begin(&tls->handshake_sha256_ctx);
466 static void tls_error_die(tls_state_t *tls)
468 dump_tls_record(tls->inbuf, tls->insize + tls->tail);
472 static void *tls_get_outbuf(tls_state_t *tls, int len)
476 if (tls->outbuf_size < len + OUTBUF_PFX + OUTBUF_SFX) {
477 tls->outbuf_size = len + OUTBUF_PFX + OUTBUF_SFX;
478 tls->outbuf = xrealloc(tls->outbuf, tls->outbuf_size);
480 return tls->outbuf + OUTBUF_PFX;
484 // 6.2.3.1. Null or Standard Stream Cipher
486 // Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6)
487 // convert TLSCompressed.fragment structures to and from stream
488 // TLSCiphertext.fragment structures.
490 // stream-ciphered struct {
491 // opaque content[TLSCompressed.length];
492 // opaque MAC[SecurityParameters.mac_length];
493 // } GenericStreamCipher;
495 // The MAC is generated as:
496 // MAC(MAC_write_key, seq_num +
497 // TLSCompressed.type +
498 // TLSCompressed.version +
499 // TLSCompressed.length +
500 // TLSCompressed.fragment);
501 // where "+" denotes concatenation.
503 // The sequence number for this record.
505 // The MAC algorithm specified by SecurityParameters.mac_algorithm.
507 // Note that the MAC is computed before encryption. The stream cipher
508 // encrypts the entire block, including the MAC.
510 // Appendix C. Cipher Suite Definitions
513 // Cipher Type Material Size Size
514 // ------------ ------ -------- ---- -----
515 // AES_128_CBC Block 16 16 16
516 // AES_256_CBC Block 32 16 16
518 // MAC Algorithm mac_length mac_key_length
519 // -------- ----------- ---------- --------------
520 // SHA HMAC-SHA1 20 20
521 // SHA256 HMAC-SHA256 32 32
522 static void xwrite_encrypted(tls_state_t *tls, unsigned size, unsigned type)
524 uint8_t *buf = tls->outbuf + OUTBUF_PFX;
525 struct record_hdr *xhdr;
527 xhdr = (void*)(buf - sizeof(*xhdr));
528 if (CIPHER_ID != TLS_RSA_WITH_NULL_SHA256)
529 xhdr = (void*)(buf - sizeof(*xhdr) - AES_BLOCKSIZE); /* place for IV */
532 xhdr->proto_maj = TLS_MAJ;
533 xhdr->proto_min = TLS_MIN;
534 /* fake unencrypted record header len for MAC calculation */
535 xhdr->len16_hi = size >> 8;
536 xhdr->len16_lo = size & 0xff;
538 /* Calculate MAC signature */
539 //TODO: convert hmac_sha256 to precomputed
540 hmac_sha256(buf + size,
541 tls->client_write_MAC_key, sizeof(tls->client_write_MAC_key),
542 &tls->write_seq64_be, sizeof(tls->write_seq64_be),
546 tls->write_seq64_be = SWAP_BE64(1 + SWAP_BE64(tls->write_seq64_be));
548 size += SHA256_OUTSIZE;
550 if (CIPHER_ID == TLS_RSA_WITH_NULL_SHA256) {
551 /* No encryption, only signing */
552 xhdr->len16_hi = size >> 8;
553 xhdr->len16_lo = size & 0xff;
554 dump_hex(">> %s\n", xhdr, sizeof(*xhdr) + size);
555 xwrite(tls->fd, xhdr, sizeof(*xhdr) + size);
556 dbg("wrote %u bytes (NULL crypt, SHA256 hash)\n", size);
561 // 6.2.3.2. CBC Block Cipher
562 // For block ciphers (such as 3DES or AES), the encryption and MAC
563 // functions convert TLSCompressed.fragment structures to and from block
564 // TLSCiphertext.fragment structures.
566 // opaque IV[SecurityParameters.record_iv_length];
567 // block-ciphered struct {
568 // opaque content[TLSCompressed.length];
569 // opaque MAC[SecurityParameters.mac_length];
570 // uint8 padding[GenericBlockCipher.padding_length];
571 // uint8 padding_length;
573 // } GenericBlockCipher;
576 // The Initialization Vector (IV) SHOULD be chosen at random, and
577 // MUST be unpredictable. Note that in versions of TLS prior to 1.1,
578 // there was no IV field (...). For block ciphers, the IV length is
579 // of length SecurityParameters.record_iv_length, which is equal to the
580 // SecurityParameters.block_size.
582 // Padding that is added to force the length of the plaintext to be
583 // an integral multiple of the block cipher's block length.
585 // The padding length MUST be such that the total size of the
586 // GenericBlockCipher structure is a multiple of the cipher's block
587 // length. Legal values range from zero to 255, inclusive.
589 // Appendix C. Cipher Suite Definitions
592 // Cipher Type Material Size Size
593 // ------------ ------ -------- ---- -----
594 // AES_128_CBC Block 16 16 16
595 // AES_256_CBC Block 32 16 16
597 psCipherContext_t ctx;
599 uint8_t padding_length;
601 /* Build IV+content+MAC+padding in outbuf */
602 tls_get_random(buf - AES_BLOCKSIZE, AES_BLOCKSIZE); /* IV */
603 dbg("before crypt: 5 hdr + %u data + %u hash bytes\n", size, SHA256_OUTSIZE);
604 // RFC is talking nonsense:
605 // Padding that is added to force the length of the plaintext to be
606 // an integral multiple of the block cipher's block length.
607 // WRONG. _padding+padding_length_, not just _padding_,
609 // IOW: padding_length is the last byte of padding[] array,
610 // contrary to what RFC depicts.
612 // What actually happens is that there is always padding.
613 // If you need one byte to reach BLOCKSIZE, this byte is 0x00.
614 // If you need two bytes, they are both 0x01.
615 // If you need three, they are 0x02,0x02,0x02. And so on.
616 // If you need no bytes to reach BLOCKSIZE, you have to pad a full
617 // BLOCKSIZE with bytes of value (BLOCKSIZE-1).
618 // It's ok to have more than minimum padding, but we do minimum.
620 padding_length = (~size) & (AES_BLOCKSIZE - 1);
622 *p++ = padding_length; /* padding */
624 } while ((size & (AES_BLOCKSIZE - 1)) != 0);
626 /* Encrypt content+MAC+padding in place */
627 psAesInit(&ctx, buf - AES_BLOCKSIZE, /* IV */
628 tls->client_write_key, sizeof(tls->client_write_key)
632 buf, /* ciphertext */
637 dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n",
638 AES_BLOCKSIZE, size, padding_length);
639 size += AES_BLOCKSIZE; /* + IV */
640 xhdr->len16_hi = size >> 8;
641 xhdr->len16_lo = size & 0xff;
642 dump_hex(">> %s\n", xhdr, sizeof(*xhdr) + size);
643 xwrite(tls->fd, xhdr, sizeof(*xhdr) + size);
644 dbg("wrote %u bytes\n", (int)sizeof(*xhdr) + size);
648 static void xwrite_and_update_handshake_hash(tls_state_t *tls, unsigned size)
650 if (!tls->encrypt_on_write) {
651 uint8_t *buf = tls->outbuf + OUTBUF_PFX;
652 struct record_hdr *xhdr = (void*)(buf - sizeof(*xhdr));
654 xhdr->type = RECORD_TYPE_HANDSHAKE;
655 xhdr->proto_maj = TLS_MAJ;
656 xhdr->proto_min = TLS_MIN;
657 xhdr->len16_hi = size >> 8;
658 xhdr->len16_lo = size & 0xff;
659 dump_hex(">> %s\n", xhdr, sizeof(*xhdr) + size);
660 xwrite(tls->fd, xhdr, sizeof(*xhdr) + size);
661 dbg("wrote %u bytes\n", (int)sizeof(*xhdr) + size);
662 /* Handshake hash does not include record headers */
663 sha256_hash_dbg(">> sha256:%s", &tls->handshake_sha256_ctx, buf, size);
666 xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
669 static int xread_tls_block(tls_state_t *tls)
671 struct record_hdr *xhdr;
676 dbg("insize:%u tail:%u\n", tls->insize, tls->tail);
677 memmove(tls->inbuf, tls->inbuf + tls->insize, tls->tail);
680 target = sizeof(tls->inbuf);
682 if (total >= sizeof(*xhdr) && target == sizeof(tls->inbuf)) {
683 xhdr = (void*)tls->inbuf;
684 target = sizeof(*xhdr) + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
685 if (target >= sizeof(tls->inbuf)) {
686 /* malformed input (too long): yell and die */
691 // can also check type/proto_maj/proto_min here
693 /* if total >= target, we have a full packet (and possibly more)... */
694 if (total - target >= 0)
696 sz = safe_read(tls->fd, tls->inbuf + total, sizeof(tls->inbuf) - total);
698 bb_perror_msg_and_die("short read");
701 tls->tail = total - target;
702 tls->insize = target;
703 sz = target - sizeof(*xhdr);
705 /* Needs to be decrypted? */
706 if (tls->min_encrypted_len_on_read > SHA256_OUTSIZE) {
707 psCipherContext_t ctx;
708 uint8_t *p = tls->inbuf + sizeof(*xhdr);
711 if (sz & (AES_BLOCKSIZE-1)
712 || sz < tls->min_encrypted_len_on_read
714 bb_error_msg_and_die("bad encrypted len:%u", sz);
716 /* Decrypt content+MAC+padding in place */
717 psAesInit(&ctx, p, /* IV */
718 tls->server_write_key, sizeof(tls->server_write_key)
721 p + AES_BLOCKSIZE, /* ciphertext */
722 p + AES_BLOCKSIZE, /* plaintext */
725 padding_len = p[sz - 1];
726 dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[AES_BLOCKSIZE], padding_len);
728 sz -= AES_BLOCKSIZE + SHA256_OUTSIZE + padding_len;
730 bb_error_msg_and_die("bad padding size:%u", padding_len);
734 memmove(tls->inbuf + 5, tls->inbuf + 5 + AES_BLOCKSIZE, sz);
737 /* if nonzero, then it's TLS_RSA_WITH_NULL_SHA256: drop MAC */
738 /* else: no encryption yet on input, subtract zero = NOP */
739 sz -= tls->min_encrypted_len_on_read;
742 /* RFC 5246 is not saying it explicitly, but sha256 hash
743 * in our FINISHED record must include data of incoming packets too!
745 if (tls->inbuf[0] == RECORD_TYPE_HANDSHAKE) {
746 sha256_hash_dbg("<< sha256:%s", &tls->handshake_sha256_ctx, tls->inbuf + 5, sz);
749 dbg("got block len:%u\n", sz);
754 * DER parsing routines
756 static unsigned get_der_len(uint8_t **bodyp, uint8_t *der, uint8_t *end)
762 // if ((der[0] & 0x1f) == 0x1f) /* not single-byte item code? */
765 len = der[1]; /* maybe it's short len */
769 if (len == 0x80 || end - der < (int)(len - 0x7e)) {
770 /* 0x80 is "0 bytes of len", invalid DER: must use short len if can */
771 /* need 3 or 4 bytes for 81, 82 */
775 len1 = der[2]; /* if (len == 0x81) it's "ii 81 xx", fetch xx */
777 /* >0x82 is "3+ bytes of len", should not happen realistically */
780 if (len == 0x82) { /* it's "ii 82 xx yy" */
781 len1 = 0x100*len1 + der[3];
782 der += 1; /* skip [yy] */
784 der += 1; /* skip [xx] */
787 // xfunc_die(); /* invalid DER: must use short len if can */
789 der += 2; /* skip [code]+[1byte] */
791 if (end - der < (int)len)
798 static uint8_t *enter_der_item(uint8_t *der, uint8_t **endp)
801 unsigned len = get_der_len(&new_der, der, *endp);
802 dbg_der("entered der @%p:0x%02x len:%u inner_byte @%p:0x%02x\n", der, der[0], len, new_der, new_der[0]);
803 /* Move "end" position to cover only this item */
804 *endp = new_der + len;
808 static uint8_t *skip_der_item(uint8_t *der, uint8_t *end)
811 unsigned len = get_der_len(&new_der, der, end);
814 dbg_der("skipped der 0x%02x, next byte 0x%02x\n", der[0], new_der[0]);
818 static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end)
821 unsigned len = get_der_len(&bin_ptr, der, end);
823 dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]);
824 pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len);
825 pstm_read_unsigned_bin(pstm_n, bin_ptr, len);
829 static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len)
831 /* Certificate is a DER-encoded data structure. Each DER element has a length,
832 * which makes it easy to skip over large compound elements of any complexity
833 * without parsing them. Example: partial decode of kernel.org certificate:
834 * SEQ 0x05ac/1452 bytes (Certificate): 308205ac
835 * SEQ 0x0494/1172 bytes (tbsCertificate): 30820494
836 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0] 3 bytes: a003
837 * INTEGER (version): 0201 02
838 * INTEGER 0x11 bytes (serialNumber): 0211 00 9f85bf664b0cddafca508679501b2be4
839 * //^^^^^^note: matrixSSL also allows [ASN_CONTEXT_SPECIFIC | ASN_PRIMITIVE | 2] = 0x82 type
840 * SEQ 0x0d bytes (signatureAlgo): 300d
841 * OID 9 bytes: 0609 2a864886f70d01010b (OID_SHA256_RSA_SIG 42.134.72.134.247.13.1.1.11)
843 * SEQ 0x5f bytes (issuer): 305f
846 * OID 3 bytes: 0603 550406
847 * Printable string "FR": 1302 4652
850 * OID 3 bytes: 0603 550408
851 * Printable string "Paris": 1305 5061726973
854 * OID 3 bytes: 0603 550407
855 * Printable string "Paris": 1305 5061726973
858 * OID 3 bytes: 0603 55040a
859 * Printable string "Gandi": 1305 47616e6469
862 * OID 3 bytes: 0603 550403
863 * Printable string "Gandi Standard SSL CA 2": 1317 47616e6469205374616e646172642053534c2043412032
864 * SEQ 30 bytes (validity): 301e
865 * TIME "161011000000Z": 170d 3136313031313030303030305a
866 * TIME "191011235959Z": 170d 3139313031313233353935395a
867 * SEQ 0x5b/91 bytes (subject): 305b //I did not decode this
868 * 3121301f060355040b1318446f6d61696e20436f
869 * 6e74726f6c2056616c6964617465643121301f06
870 * 0355040b1318506f73697469766553534c204d75
871 * 6c74692d446f6d61696e31133011060355040313
872 * 0a6b65726e656c2e6f7267
873 * SEQ 0x01a2/418 bytes (subjectPublicKeyInfo): 308201a2
874 * SEQ 13 bytes (algorithm): 300d
875 * OID 9 bytes: 0609 2a864886f70d010101 (OID_RSA_KEY_ALG 42.134.72.134.247.13.1.1.1)
877 * BITSTRING 0x018f/399 bytes (publicKey): 0382018f
879 * //after the zero byte, it appears key itself uses DER encoding:
880 * SEQ 0x018a/394 bytes: 3082018a
881 * INTEGER 0x0181/385 bytes (modulus): 02820181
882 * 00b1ab2fc727a3bef76780c9349bf3
883 * ...24 more blocks of 15 bytes each...
884 * 90e895291c6bc8693b65
885 * INTEGER 3 bytes (exponent): 0203 010001
886 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0x3] 0x01e5 bytes (X509v3 extensions): a38201e5
887 * SEQ 0x01e1 bytes: 308201e1
889 * Certificate is a sequence of three elements:
890 * tbsCertificate (SEQ)
891 * signatureAlgorithm (AlgorithmIdentifier)
892 * signatureValue (BIT STRING)
894 * In turn, tbsCertificate is a sequence of:
897 * signatureAlgo (AlgorithmIdentifier)
898 * issuer (Name, has complex structure)
899 * validity (Validity, SEQ of two Times)
901 * subjectPublicKeyInfo (SEQ)
904 * subjectPublicKeyInfo is a sequence of:
905 * algorithm (AlgorithmIdentifier)
906 * publicKey (BIT STRING)
908 * We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey
910 uint8_t *end = der + len;
912 /* enter "Certificate" item: [der, end) will be only Cert */
913 der = enter_der_item(der, &end);
915 /* enter "tbsCertificate" item: [der, end) will be only tbsCert */
916 der = enter_der_item(der, &end);
918 /* skip up to subjectPublicKeyInfo */
919 der = skip_der_item(der, end); /* version */
920 der = skip_der_item(der, end); /* serialNumber */
921 der = skip_der_item(der, end); /* signatureAlgo */
922 der = skip_der_item(der, end); /* issuer */
923 der = skip_der_item(der, end); /* validity */
924 der = skip_der_item(der, end); /* subject */
926 /* enter subjectPublicKeyInfo */
927 der = enter_der_item(der, &end);
928 { /* check subjectPublicKeyInfo.algorithm */
929 static const uint8_t expected[] = {
930 0x30,0x0d, // SEQ 13 bytes
931 0x06,0x09, 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, // OID RSA_KEY_ALG 42.134.72.134.247.13.1.1.1
934 if (memcmp(der, expected, sizeof(expected)) != 0)
935 bb_error_msg_and_die("not RSA key");
937 /* skip subjectPublicKeyInfo.algorithm */
938 der = skip_der_item(der, end);
939 /* enter subjectPublicKeyInfo.publicKey */
940 // die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */
941 der = enter_der_item(der, &end);
944 //based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note
945 dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]);
946 if (end - der < 14) xfunc_die();
949 * SEQ 0x018a/394 bytes: 3082018a
950 * INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX
951 * INTEGER 3 bytes (exponent): 0203 010001
953 if (*der != 0) /* "ignore bits", should be 0 */
956 der = enter_der_item(der, &end); /* enter SEQ */
957 /* memset(tls->server_rsa_pub_key, 0, sizeof(tls->server_rsa_pub_key)); - already is */
958 der_binary_to_pstm(&tls->server_rsa_pub_key.N, der, end); /* modulus */
959 der = skip_der_item(der, end);
960 der_binary_to_pstm(&tls->server_rsa_pub_key.e, der, end); /* exponent */
961 tls->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->server_rsa_pub_key.N);
962 dbg("server_rsa_pub_key.size:%d\n", tls->server_rsa_pub_key.size);
966 * TLS Handshake routines
968 static int xread_tls_handshake_block(tls_state_t *tls, int min_len)
970 struct record_hdr *xhdr;
971 int len = xread_tls_block(tls);
973 xhdr = (void*)tls->inbuf;
975 || xhdr->type != RECORD_TYPE_HANDSHAKE
976 || xhdr->proto_maj != TLS_MAJ
977 || xhdr->proto_min != TLS_MIN
981 dbg("got HANDSHAKE\n");
985 static ALWAYS_INLINE void fill_handshake_record_hdr(void *buf, unsigned type, unsigned len)
987 struct handshake_hdr {
989 uint8_t len24_hi, len24_mid, len24_lo;
994 h->len24_hi = len >> 16;
995 h->len24_mid = len >> 8;
996 h->len24_lo = len & 0xff;
999 //TODO: implement RFC 5746 (Renegotiation Indication Extension) - some servers will refuse to work with us otherwise
1000 static void send_client_hello(tls_state_t *tls)
1002 struct client_hello {
1004 uint8_t len24_hi, len24_mid, len24_lo;
1005 uint8_t proto_maj, proto_min;
1007 uint8_t session_id_len;
1008 /* uint8_t session_id[]; */
1009 uint8_t cipherid_len16_hi, cipherid_len16_lo;
1010 uint8_t cipherid[2 * 1]; /* actually variable */
1011 uint8_t comprtypes_len;
1012 uint8_t comprtypes[1]; /* actually variable */
1014 struct client_hello *record = tls_get_outbuf(tls, sizeof(*record));
1016 fill_handshake_record_hdr(record, HANDSHAKE_CLIENT_HELLO, sizeof(*record));
1017 record->proto_maj = TLS_MAJ; /* the "requested" version of the protocol, */
1018 record->proto_min = TLS_MIN; /* can be higher than one in record headers */
1019 tls_get_random(record->rand32, sizeof(record->rand32));
1020 memset(record->rand32, 0x11, sizeof(record->rand32));
1021 memcpy(tls->client_and_server_rand32, record->rand32, sizeof(record->rand32));
1022 record->session_id_len = 0;
1023 record->cipherid_len16_hi = 0;
1024 record->cipherid_len16_lo = 2 * 1;
1025 record->cipherid[0] = CIPHER_ID >> 8;
1026 record->cipherid[1] = CIPHER_ID & 0xff;
1027 record->comprtypes_len = 1;
1028 record->comprtypes[0] = 0;
1030 //dbg (make it repeatable): memset(record.rand32, 0x11, sizeof(record.rand32));
1031 dbg(">> CLIENT_HELLO\n");
1032 xwrite_and_update_handshake_hash(tls, sizeof(*record));
1035 static void get_server_hello(tls_state_t *tls)
1037 struct server_hello {
1038 struct record_hdr xhdr;
1040 uint8_t len24_hi, len24_mid, len24_lo;
1041 uint8_t proto_maj, proto_min;
1042 uint8_t rand32[32]; /* first 4 bytes are unix time in BE format */
1043 uint8_t session_id_len;
1044 uint8_t session_id[32];
1045 uint8_t cipherid_hi, cipherid_lo;
1047 /* extensions may follow, but only those which client offered in its Hello */
1049 struct server_hello *hp;
1052 xread_tls_handshake_block(tls, 74);
1054 hp = (void*)tls->inbuf;
1056 // 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|
1057 //SvHl len=70 maj.min unixtime^^^ 28randbytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^ slen sid32bytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cipSel comprSel
1058 if (hp->type != HANDSHAKE_SERVER_HELLO
1059 || hp->len24_hi != 0
1060 || hp->len24_mid != 0
1061 /* hp->len24_lo checked later */
1062 || hp->proto_maj != TLS_MAJ
1063 || hp->proto_min != TLS_MIN
1068 cipherid = &hp->cipherid_hi;
1069 if (hp->session_id_len != 32) {
1070 if (hp->session_id_len != 0)
1073 // session_id_len == 0: no session id
1075 // may return an empty session_id to indicate that the session will
1076 // not be cached and therefore cannot be resumed."
1078 hp->len24_lo += 32; /* what len would be if session id would be present */
1081 if (hp->len24_lo < 70
1082 || cipherid[0] != (CIPHER_ID >> 8)
1083 || cipherid[1] != (CIPHER_ID & 0xff)
1084 || cipherid[2] != 0 /* comprtype */
1089 dbg("<< SERVER_HELLO\n");
1090 memcpy(tls->client_and_server_rand32 + 32, hp->rand32, sizeof(hp->rand32));
1093 static void get_server_cert(tls_state_t *tls)
1095 struct record_hdr *xhdr;
1099 len = xread_tls_handshake_block(tls, 10);
1101 xhdr = (void*)tls->inbuf;
1102 certbuf = (void*)(xhdr + 1);
1103 if (certbuf[0] != HANDSHAKE_CERTIFICATE)
1105 dbg("<< CERTIFICATE\n");
1107 // 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...
1108 //Cert len=4388 ChainLen CertLen^ DER encoded X509 starts here. openssl x509 -in FILE -inform DER -noout -text
1109 len1 = get24be(certbuf + 1);
1110 if (len1 > len - 4) tls_error_die(tls);
1112 len1 = get24be(certbuf + 4);
1113 if (len1 > len - 3) tls_error_die(tls);
1115 len1 = get24be(certbuf + 7);
1116 if (len1 > len - 3) tls_error_die(tls);
1120 find_key_in_der_cert(tls, certbuf + 10, len);
1123 static void send_client_key_exchange(tls_state_t *tls)
1125 struct client_key_exchange {
1127 uint8_t len24_hi, len24_mid, len24_lo;
1128 /* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */
1129 uint8_t keylen16_hi, keylen16_lo;
1130 uint8_t key[4 * 1024]; // size??
1132 //FIXME: better size estimate
1133 struct client_key_exchange *record = tls_get_outbuf(tls, sizeof(*record));
1134 uint8_t rsa_premaster[SSL_HS_RSA_PREMASTER_SIZE];
1137 tls_get_random(rsa_premaster, sizeof(rsa_premaster));
1138 memset(rsa_premaster, 0x44, sizeof(rsa_premaster));
1140 // "Note: The version number in the PreMasterSecret is the version
1141 // offered by the client in the ClientHello.client_version, not the
1142 // version negotiated for the connection."
1143 rsa_premaster[0] = TLS_MAJ;
1144 rsa_premaster[1] = TLS_MIN;
1145 len = psRsaEncryptPub(/*pool:*/ NULL,
1146 /* psRsaKey_t* */ &tls->server_rsa_pub_key,
1147 rsa_premaster, /*inlen:*/ sizeof(rsa_premaster),
1148 record->key, sizeof(record->key),
1151 record->keylen16_hi = len >> 8;
1152 record->keylen16_lo = len & 0xff;
1154 record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE;
1155 record->len24_hi = 0;
1156 record->len24_mid = len >> 8;
1157 record->len24_lo = len & 0xff;
1160 dbg(">> CLIENT_KEY_EXCHANGE\n");
1161 xwrite_and_update_handshake_hash(tls, len);
1164 // For all key exchange methods, the same algorithm is used to convert
1165 // the pre_master_secret into the master_secret. The pre_master_secret
1166 // should be deleted from memory once the master_secret has been
1168 // master_secret = PRF(pre_master_secret, "master secret",
1169 // ClientHello.random + ServerHello.random)
1171 // The master secret is always exactly 48 bytes in length. The length
1172 // of the premaster secret will vary depending on key exchange method.
1174 tls->master_secret, sizeof(tls->master_secret),
1175 rsa_premaster, sizeof(rsa_premaster),
1177 tls->client_and_server_rand32, sizeof(tls->client_and_server_rand32)
1179 dump_hex("master secret:%s\n", tls->master_secret, sizeof(tls->master_secret));
1182 // 6.3. Key Calculation
1184 // The Record Protocol requires an algorithm to generate keys required
1185 // by the current connection state (see Appendix A.6) from the security
1186 // parameters provided by the handshake protocol.
1188 // The master secret is expanded into a sequence of secure bytes, which
1189 // is then split to a client write MAC key, a server write MAC key, a
1190 // client write encryption key, and a server write encryption key. Each
1191 // of these is generated from the byte sequence in that order. Unused
1192 // values are empty. Some AEAD ciphers may additionally require a
1193 // client write IV and a server write IV (see Section 6.2.3.3).
1195 // When keys and MAC keys are generated, the master secret is used as an
1198 // To generate the key material, compute
1200 // key_block = PRF(SecurityParameters.master_secret,
1202 // SecurityParameters.server_random +
1203 // SecurityParameters.client_random);
1205 // until enough output has been generated. Then, the key_block is
1206 // partitioned as follows:
1208 // client_write_MAC_key[SecurityParameters.mac_key_length]
1209 // server_write_MAC_key[SecurityParameters.mac_key_length]
1210 // client_write_key[SecurityParameters.enc_key_length]
1211 // server_write_key[SecurityParameters.enc_key_length]
1212 // client_write_IV[SecurityParameters.fixed_iv_length]
1213 // server_write_IV[SecurityParameters.fixed_iv_length]
1217 /* make "server_rand32 + client_rand32" */
1218 memcpy(&tmp64[0] , &tls->client_and_server_rand32[32], 32);
1219 memcpy(&tmp64[32], &tls->client_and_server_rand32[0] , 32);
1222 tls->client_write_MAC_key, 2 * (SHA256_OUTSIZE + AES256_KEYSIZE),
1224 // server_write_MAC_key[SHA256_OUTSIZE]
1225 // client_write_key[AES256_KEYSIZE]
1226 // server_write_key[AES256_KEYSIZE]
1227 tls->master_secret, sizeof(tls->master_secret),
1231 dump_hex("client_write_MAC_key:%s\n",
1232 tls->client_write_MAC_key, sizeof(tls->client_write_MAC_key)
1234 dump_hex("client_write_key:%s\n",
1235 tls->client_write_key, sizeof(tls->client_write_key)
1240 static const uint8_t rec_CHANGE_CIPHER_SPEC[] = {
1241 RECORD_TYPE_CHANGE_CIPHER_SPEC, TLS_MAJ, TLS_MIN, 00, 01,
1245 static void send_change_cipher_spec(tls_state_t *tls)
1247 dbg(">> CHANGE_CIPHER_SPEC\n");
1248 xwrite(tls->fd, rec_CHANGE_CIPHER_SPEC, sizeof(rec_CHANGE_CIPHER_SPEC));
1252 // A Finished message is always sent immediately after a change
1253 // cipher spec message to verify that the key exchange and
1254 // authentication processes were successful. It is essential that a
1255 // change cipher spec message be received between the other handshake
1256 // messages and the Finished message.
1258 // The Finished message is the first one protected with the just
1259 // negotiated algorithms, keys, and secrets. Recipients of Finished
1260 // messages MUST verify that the contents are correct. Once a side
1261 // has sent its Finished message and received and validated the
1262 // Finished message from its peer, it may begin to send and receive
1263 // application data over the connection.
1266 // opaque verify_data[verify_data_length];
1270 // PRF(master_secret, finished_label, Hash(handshake_messages))
1271 // [0..verify_data_length-1];
1274 // For Finished messages sent by the client, the string
1275 // "client finished". For Finished messages sent by the server,
1276 // the string "server finished".
1278 // Hash denotes a Hash of the handshake messages. For the PRF
1279 // defined in Section 5, the Hash MUST be the Hash used as the basis
1280 // for the PRF. Any cipher suite which defines a different PRF MUST
1281 // also define the Hash to use in the Finished computation.
1283 // In previous versions of TLS, the verify_data was always 12 octets
1284 // long. In the current version of TLS, it depends on the cipher
1285 // suite. Any cipher suite which does not explicitly specify
1286 // verify_data_length has a verify_data_length equal to 12. This
1287 // includes all existing cipher suites.
1288 static void send_client_finished(tls_state_t *tls)
1292 uint8_t len24_hi, len24_mid, len24_lo;
1293 uint8_t prf_result[12];
1295 struct finished *record = tls_get_outbuf(tls, sizeof(*record));
1296 uint8_t handshake_hash[SHA256_OUTSIZE];
1298 fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record));
1300 sha256_peek(&tls->handshake_sha256_ctx, handshake_hash);
1301 prf_hmac_sha256(record->prf_result, sizeof(record->prf_result),
1302 tls->master_secret, sizeof(tls->master_secret),
1304 handshake_hash, sizeof(handshake_hash)
1306 dump_hex("from secret: %s\n", tls->master_secret, sizeof(tls->master_secret));
1307 dump_hex("from labelSeed: %s", "client finished", sizeof("client finished")-1);
1308 dump_hex("%s\n", handshake_hash, sizeof(handshake_hash));
1309 dump_hex("=> digest: %s\n", record->prf_result, sizeof(record->prf_result));
1311 dbg(">> FINISHED\n");
1312 xwrite_encrypted(tls, sizeof(*record), RECORD_TYPE_HANDSHAKE);
1315 static void tls_handshake(tls_state_t *tls)
1317 // Client RFC 5246 Server
1318 // (*) - optional messages, not always sent
1320 // ClientHello ------->
1323 // ServerKeyExchange*
1324 // CertificateRequest*
1325 // <------- ServerHelloDone
1327 // ClientKeyExchange
1328 // CertificateVerify*
1329 // [ChangeCipherSpec]
1330 // Finished ------->
1331 // [ChangeCipherSpec]
1332 // <------- Finished
1333 // Application Data <------> Application Data
1336 send_client_hello(tls);
1337 get_server_hello(tls);
1340 // The server MUST send a Certificate message whenever the agreed-
1341 // upon key exchange method uses certificates for authentication
1342 // (this includes all key exchange methods defined in this document
1343 // except DH_anon). This message will always immediately follow the
1344 // ServerHello message.
1346 // IOW: in practice, Certificate *always* follows.
1347 // (for example, kernel.org does not even accept DH_anon cipher id)
1348 get_server_cert(tls);
1350 len = xread_tls_handshake_block(tls, 4);
1351 if (tls->inbuf[5] == HANDSHAKE_SERVER_KEY_EXCHANGE) {
1353 // 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...
1355 // with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes:
1356 // 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...
1357 dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len);
1358 //probably need to save it
1359 xread_tls_handshake_block(tls, 4);
1362 // if (tls->inbuf[5] == HANDSHAKE_CERTIFICATE_REQUEST) {
1363 // dbg("<< CERTIFICATE_REQUEST\n");
1364 //RFC 5246: (in response to this,) "If no suitable certificate is available,
1365 // the client MUST send a certificate message containing no
1366 // certificates. That is, the certificate_list structure has a
1367 // length of zero. ...
1368 // Client certificates are sent using the Certificate structure
1369 // defined in Section 7.4.2."
1370 // (i.e. the same format as server certs)
1371 // xread_tls_handshake_block(tls, 4);
1374 if (tls->inbuf[5] != HANDSHAKE_SERVER_HELLO_DONE)
1376 // 0e 000000 (len:0)
1377 dbg("<< SERVER_HELLO_DONE\n");
1379 send_client_key_exchange(tls);
1381 send_change_cipher_spec(tls);
1382 /* from now on we should send encrypted */
1383 /* tls->write_seq64_be = 0; - already is */
1384 tls->encrypt_on_write = 1;
1386 send_client_finished(tls);
1388 /* Get CHANGE_CIPHER_SPEC */
1389 len = xread_tls_block(tls);
1390 if (len != 1 || memcmp(tls->inbuf, rec_CHANGE_CIPHER_SPEC, 6) != 0)
1392 dbg("<< CHANGE_CIPHER_SPEC\n");
1393 if (CIPHER_ID == TLS_RSA_WITH_NULL_SHA256)
1394 tls->min_encrypted_len_on_read = SHA256_OUTSIZE;
1396 /* all incoming packets now should be encrypted and have IV + MAC + padding */
1397 tls->min_encrypted_len_on_read = AES_BLOCKSIZE + SHA256_OUTSIZE + AES_BLOCKSIZE;
1399 /* Get (encrypted) FINISHED from the server */
1400 len = xread_tls_block(tls);
1401 if (len < 4 || tls->inbuf[5] != HANDSHAKE_FINISHED)
1403 dbg("<< FINISHED\n");
1404 /* application data can be sent/received */
1407 static void tls_xwrite(tls_state_t *tls, int len)
1410 xwrite_encrypted(tls, len, RECORD_TYPE_APPLICATION_DATA);
1413 // To run a test server using openssl:
1414 // openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
1415 // openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1
1417 // Unencryped SHA256 example:
1418 // openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
1419 // openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL
1420 // openssl s_client -connect 127.0.0.1:4433 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL-SHA256
1422 int tls_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1423 int tls_main(int argc UNUSED_PARAM, char **argv)
1426 fd_set readfds, testfds;
1430 // getopt32(argv, "myopts")
1435 cfd = create_and_connect_stream_or_die(argv[1], 443);
1437 tls = new_tls_state();
1441 /* Select loop copying stdin to cfd, and cfd to stdout */
1443 FD_SET(cfd, &readfds);
1444 FD_SET(STDIN_FILENO, &readfds);
1446 #define iobuf bb_common_bufsiz1
1447 setup_common_bufsiz();
1453 if (select(cfd + 1, &testfds, NULL, NULL, NULL) < 0)
1454 bb_perror_msg_and_die("select");
1456 if (FD_ISSET(STDIN_FILENO, &testfds)) {
1457 void *buf = tls_get_outbuf(tls, COMMON_BUFSIZE);
1458 nread = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE);
1460 //&& errno != EAGAIN
1461 /* Close outgoing half-connection so they get EOF,
1462 * but leave incoming alone so we can see response */
1463 // shutdown(cfd, SHUT_WR);
1464 FD_CLR(STDIN_FILENO, &readfds);
1466 tls_xwrite(tls, nread);
1468 if (FD_ISSET(cfd, &testfds)) {
1469 nread = xread_tls_block(tls);
1471 //if eof, just close stdout, but not exit!
1472 return EXIT_SUCCESS;
1473 xwrite(STDOUT_FILENO, tls->inbuf + 5, nread);
1477 return EXIT_SUCCESS;
1479 /* Unencryped SHA256 example:
1482 write to 0x1d750b0 [0x1e6f153] (99 bytes => 99 (0x63))
1483 0000 - 16 03 01 005e 01 00005a 0303 [4d ef 5c 82 3e ....^...Z..M.\.> >> ClHello
1484 0010 - bf a6 ee f1 1e 04 d1 5c-99 20 86 13 e9 0a cf 58 .......\. .....X
1485 0020 - 75 b1 bd 7a e6 d6 44 f3-d3 a1 52] 00 0004 003b u..z..D...R....; 003b = TLS_RSA_WITH_NULL_SHA256
1486 0030 - 00ff TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1488 002d, 0023 0000, 000d 0020 [00 1e .....-.#..... .. extlen, SessionTicketTLS 0 bytes, SignatureAlgorithms 32 bytes
1489 0040 - 06 01 06 02 06 03 05 01-05 02 05 03 04 01 04 02 ................
1490 0050 - 04 03 03 01 03 02 03 03-02 01 02 02 02 03] 000f ................ Heart Beat 1 byte
1493 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1494 0000 - 16 03 03 00 3a ....:
1495 read from 0x1d750b0 [0x1e6ac08] (58 bytes => 58 (0x3A))
1496 0000 - 02 000036 0303 [f2 61-ae c8 58 e3 51 42 32 93 ...6...a..X.QB2. << SvHello
1497 0010 - c5 62 e4 f5 06 93 81 65-aa f7 df 74 af 7c 98 b4 .b.....e...t.|..
1498 0020 - 3e a7 35 c3 25 69] 00,003b,00.................. >.5.%i..;....... - no session id! "The server
1499 may return an empty session_id to indicate that the session will
1500 not be cached and therefore cannot be resumed."
1501 003b = TLS_RSA_WITH_NULL_SHA256 accepted, 00 - no compr
1502 000e ff01 0001 extlen, 0xff01=RenegotiationInfo 1 byte
1503 0030 - 00, 0023 0000, SessionTicketTLS 0 bytes
1504 000f 0001 01 ..#....... Heart Beat 1 byte
1506 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1507 0000 - 16 03 03 04 0b .....
1508 read from 0x1d750b0 [0x1e6ac08] (1035 bytes => 1035 (0x40B))
1509 0000 - 0b 00 04 07 00 04 04 00-04 01 30 82 03 fd 30 82 ..........0...0. << Cert
1510 0010 - 02 65 a0 03 02 01 02 02-09 00 d9 d9 8d b8 94 ad .e..............
1511 0020 - 2e 2b 30 0d 06 09 2a 86-48 86 f7 0d 01 01 0b 05 .+0...*.H.......
1512 0030 - 00 30 14 31 12 30 10 06-03 55 04 03 0c 09 6c 6f .0.1.0...U....lo
1513 0040 - 63 61 6c 68 6f 73 74 30-20 17 0d 31 37 30 31 31 calhost0 ..17011
1514 ...".......".......".......".......".......".......".......".......".....
1515 03f0 - 11 8a cd c5 a3 0a 22 43-d5 13 f9 a5 8a 06 f9 00 ......"C........
1516 0400 - 3c f7 86 4e e8 a5 d8 5b-92 37 f5 <..N...[.7.
1517 depth=0 CN = localhost
1518 verify error:num=18:self signed certificate
1520 depth=0 CN = localhost
1523 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1524 0000 - 16 03 03 00 04 .....
1526 read from 0x1d750b0 [0x1e6ac08] (4 bytes => 4 (0x4)) << SvDone
1528 0004 - <SPACES/NULS>
1530 write to 0x1d750b0 [0x1e74620] (395 bytes => 395 (0x18B)) >> ClDone
1531 0000 - 16 03 03 01 86 10 00 01-82 01 80 88 f0 87 5d b0 ..............].
1532 0010 - ea df 3b 4d e2 35 f3 99-e6 d4 29 87 36 86 ea 30 ..;M.5....).6..0
1533 0020 - 38 80 c7 37 66 7f 5b e7-23 38 7e 87 24 66 82 81 8..7f.[.#8~.$f..
1534 0030 - e4 ba 6c 2a 0c 92 a8 b9-39 c1 55 16 32 88 14 cd ..l*....9.U.2...
1535 0040 - 95 8c 82 49 a1 c7 f9 9b-e5 8f f6 5e 7e ee 91 b3 ...I.......^~...
1536 0050 - 2c 92 e7 a3 02 f8 9f 56-04 45 39 df a7 d6 1a 16 ,......V.E9.....
1537 0060 - 67 5c a4 f8 87 8a c4 c8-6c 6f c6 f0 9b c9 b4 87 g\......lo......
1538 0070 - 36 43 c1 67 9f b3 aa 11-34 b0 c2 fc 1f d9 e1 ff 6C.g....4.......
1539 0080 - fb e1 89 db 91 58 ec cc-aa 16 19 9a 91 74 e2 46 .....X.......t.F
1540 0090 - 22 a7 a7 f7 9e 3c 97 82-2c e4 21 b3 fa ef ba 3f "....<..,.!....?
1541 00a0 - 57 48 e4 b2 84 b7 c2 81-92 a9 f1 03 68 f4 e6 0c WH..........h...
1542 00b0 - fd 54 87 f5 e9 a0 5d e6-5f 0e bd 80 86 27 ab 0e .T....]._....'..
1543 00c0 - cf 92 4f bd fc 24 b9 54-72 5f 58 df 6b 2b 1d 97 ..O..$.Tr_X.k+..
1544 00d0 - 00 60 fe 95 b0 aa d6 c7-c1 3a f9 2e 7c 92 a9 6d .`.......:..|..m
1545 00e0 - 28 a3 ef 3e c1 e6 2d 2d-e8 db 81 ea 51 02 3f 64 (..>..--....Q.?d
1546 00f0 - a8 66 14 c1 4b 17 1f 55-c6 5b 3b 38 c3 6a 61 a8 .f..K..U.[;8.ja.
1547 0100 - f7 ad 65 7d cb 14 6d b3-0f 76 19 25 8e ed bd 53 ..e}..m..v.%...S
1548 0110 - 35 a9 a1 34 00 9d 07 81-84 51 35 e0 83 83 e3 a6 5..4.....Q5.....
1549 0120 - c7 77 4c 61 e4 78 9c cb-f5 92 4e d6 dd c4 c2 2b .wLa.x....N....+
1550 0130 - 75 9e 72 a6 7f 81 6a 1c-fc 4a 51 91 81 b4 cc 33 u.r...j..JQ....3
1551 0140 - 1c 8b 0a b6 94 8b 16 1b-86 2f 31 5e 31 e1 57 14 ........./1^1.W.
1552 0150 - 2e b5 09 5d cf 6f ea b2-94 e9 5c cc b9 fc 24 a0 ...].o....\...$.
1553 0160 - b7 f1 f4 9d 95 46 4f 08-5c 45 c6 2f 9f 7d 76 09 .....FO.\E./.}v.
1554 0170 - 6a af 50 2c 89 76 82 5f-e8 34 d8 4b 84 b6 34 18 j.P,.v._.4.K..4.
1555 0180 - 85 95 4a 3f 0f 28 88 3a-71 32 90 ..J?.(.:q2.
1557 write to 0x1d750b0 [0x1e74620] (6 bytes => 6 (0x6))
1558 0000 - 14 03 03 00 01 01 ...... >> CHANGE_CIPHER_SPEC
1560 write to 0x1d750b0 [0x1e74620] (53 bytes => 53 (0x35))
1561 0000 - 16 03 03 0030 14 00000c [ed b9 e1 33 36 0b 76 ....0.......36.v >> FINISHED (0x14) [PRF 12 bytes|SHA256_OUTSIZE 32 bytes]
1562 0010 - c0 d1 d4 0b a3|73 ec a8-fa b5 cb 12 b6 4c 2a b1 .....s.......L*.
1563 0020 - fb 42 7f 73 0d 06 1c 87-56 f0 db df e6 6a 25 aa .B.s....V....j%.
1564 0030 - fc 42 38 cb 0b] .B8..
1566 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1567 0000 - 16 03 03 00 aa .....
1568 read from 0x1d750b0 [0x1e6ac08] (170 bytes => 170 (0xAA))
1569 0000 - 04 00 00 a6 00 00 1c 20-00 a0 dd f4 52 01 54 8d ....... ....R.T. << NEW_SESSION_TICKET
1570 0010 - f8 a6 f9 2d 7d 19 20 5b-14 44 d3 2d 7b f2 ca e8 ...-}. [.D.-{...
1571 0020 - 01 4e 94 7b fe 12 59 3a-00 2e 7e cf 74 43 7a f7 .N.{..Y:..~.tCz.
1572 0030 - 9e cc 70 80 70 7c e3 a5-c6 9d 85 2c 36 19 4c 5c ..p.p|.....,6.L\
1573 0040 - ba 3b c3 e5 69 dc f3 a4-47 38 11 c9 7d 1a b0 6e .;..i...G8..}..n
1574 0050 - d8 49 a0 a8 e4 de 70 a8-d0 6b e4 7a b7 65 25 df .I....p..k.z.e%.
1575 0060 - 1b 5f 64 0f 89 69 02 72-fe eb d3 7a af 51 78 0e ._d..i.r...z.Qx.
1576 0070 - de 17 06 a5 f0 47 9d e0-04 d4 b1 1e be 7e ed bd .....G.......~..
1577 0080 - 27 8f 5d e8 ac f6 45 aa-e0 12 93 41 5f a8 4b b9 '.]...E....A_.K.
1578 0090 - bd 43 8f a1 23 51 af 92-77 8f 38 23 3e 2e c2 f0 .C..#Q..w.8#>...
1579 00a0 - a3 74 fa 83 94 ce 19 8a-5b 5b .t......[[
1581 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1582 0000 - 14 03 03 00 01 ..... << CHANGE_CIPHER_SPEC
1583 read from 0x1d750b0 [0x1e6ac08] (1 bytes => 1 (0x1))
1586 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1587 0000 - 16 03 03 00 30 ....0
1588 read from 0x1d750b0 [0x1e6ac08] (48 bytes => 48 (0x30))
1589 0000 - 14 00000c [06 86 0d 5c-92 0b 63 04 cc b4 f0 00 .......\..c..... << FINISHED (0x14) [PRF 12 bytes|SHA256_OUTSIZE 32 bytes]
1590 0010 -|49 d6 dd 56 73 e3 d2 e8-22 d6 bd 61 b2 b3 af f0 I..Vs..."..a....
1591 0020 - f5 00 8a 80 82 04 33 a7-50 8e ae 3b 4c 8c cf 4a] ......3.P..;L..J
1598 -----BEGIN CERTIFICATE-----
1599 ...".......".......".......".......".......".......".......".......".....
1600 -----END CERTIFICATE-----
1601 subject=/CN=localhost
1602 issuer=/CN=localhost
1604 No client certificate CA names sent
1606 SSL handshake has read 1346 bytes and written 553 bytes
1608 New, TLSv1/SSLv3, Cipher is NULL-SHA256
1609 Server public key is 3072 bit
1610 Secure Renegotiation IS supported
1616 Cipher : NULL-SHA256
1617 Session-ID: 5D62B36950F3DEB571707CD1B815E9E275041B9DB70D7F3E25C4A6535B13B616
1619 Master-Key: 4D08108C59417E0A41656636C51BA5B83F4EFFF9F4C860987B47B31250E5D1816D00940DBCCC196C2D99C8462C889DF1
1621 Krb5 Principal: None
1623 PSK identity hint: None
1624 TLS session ticket lifetime hint: 7200 (seconds)
1626 0000 - dd f4 52 01 54 8d f8 a6-f9 2d 7d 19 20 5b 14 44 ..R.T....-}. [.D
1627 0010 - d3 2d 7b f2 ca e8 01 4e-94 7b fe 12 59 3a 00 2e .-{....N.{..Y:..
1628 0020 - 7e cf 74 43 7a f7 9e cc-70 80 70 7c e3 a5 c6 9d ~.tCz...p.p|....
1629 0030 - 85 2c 36 19 4c 5c ba 3b-c3 e5 69 dc f3 a4 47 38 .,6.L\.;..i...G8
1630 0040 - 11 c9 7d 1a b0 6e d8 49-a0 a8 e4 de 70 a8 d0 6b ..}..n.I....p..k
1631 0050 - e4 7a b7 65 25 df 1b 5f-64 0f 89 69 02 72 fe eb .z.e%.._d..i.r..
1632 0060 - d3 7a af 51 78 0e de 17-06 a5 f0 47 9d e0 04 d4 .z.Qx......G....
1633 0070 - b1 1e be 7e ed bd 27 8f-5d e8 ac f6 45 aa e0 12 ...~..'.]...E...
1634 0080 - 93 41 5f a8 4b b9 bd 43-8f a1 23 51 af 92 77 8f .A_.K..C..#Q..w.
1635 0090 - 38 23 3e 2e c2 f0 a3 74-fa 83 94 ce 19 8a 5b 5b 8#>....t......[[
1637 Start Time: 1484574330
1638 Timeout : 7200 (sec)
1639 Verify return code: 18 (self signed certificate)
1641 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1642 0000 - 17 03 03 00 21 ....!
1643 read from 0x1d750b0 [0x1e6ac08] (33 bytes => 33 (0x21))
1644 0000 - 0a 74 5b 50 02 13 75 a4-27 0a 40 b1 53 74 52 14 .t[P..u.'.@.StR.
1645 0010 - e7 1e 6a 6c c1 60 2e 93-7e a5 d9 43 1d 8e f6 08 ..jl.`..~..C....
1648 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1649 0000 - 17 03 03 00 21 ....!
1650 read from 0x1d750b0 [0x1e6ac08] (33 bytes => 33 (0x21))
1651 0000 - 0a 1b ce 44 98 4f 81 c5-28 7a cc 79 62 db d2 86 ...D.O..(z.yb...
1652 0010 - 6a 55 a4 c7 73 49 ef 3e-bd 03 99 76 df 65 2a a1 jU..sI.>...v.e*.
1655 read from 0x1d750b0 [0x1e6ac03] (5 bytes => 5 (0x5))
1656 0000 - 17 03 03 00 21 ....!
1657 read from 0x1d750b0 [0x1e6ac08] (33 bytes => 33 (0x21))
1658 0000 - 0a 67 66 34 ba 68 36 3c-ad 0a c1 f5 c0 5a 50 fe .gf4.h6<.....ZP.
1659 0010 - 68 cd 04 65 e9 de 6e 98-f9 e2 41 1e 0b 9b 84 06 h..e..n...A.....