diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-17 16:53:36 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-17 16:53:36 +0100 |
commit | e2cb3b990f9c832d69691f2a869df8c9ac73d46a (patch) | |
tree | 1c4a312a9a21544a5a73976bfe1ad5eb8f95ccf1 /networking/tls.c | |
parent | 9a6897a48a3bd23dacfe8f23016d2f8083c2308b (diff) | |
download | busybox-w32-e2cb3b990f9c832d69691f2a869df8c9ac73d46a.tar.gz busybox-w32-e2cb3b990f9c832d69691f2a869df8c9ac73d46a.tar.bz2 busybox-w32-e2cb3b990f9c832d69691f2a869df8c9ac73d46a.zip |
tls: make our send_client_finished() pass server check
sha256 hash should be calculated over incoming handshake packets too!
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls.c')
-rw-r--r-- | networking/tls.c | 420 |
1 files changed, 180 insertions, 240 deletions
diff --git a/networking/tls.c b/networking/tls.c index 562874aa6..60afd30b4 100644 --- a/networking/tls.c +++ b/networking/tls.c | |||
@@ -25,7 +25,9 @@ | |||
25 | 25 | ||
26 | #include "tls.h" | 26 | #include "tls.h" |
27 | 27 | ||
28 | #if 1 | 28 | #define TLS_DEBUG 2 |
29 | |||
30 | #if TLS_DEBUG | ||
29 | # define dbg(...) fprintf(stderr, __VA_ARGS__) | 31 | # define dbg(...) fprintf(stderr, __VA_ARGS__) |
30 | #else | 32 | #else |
31 | # define dbg(...) ((void)0) | 33 | # define dbg(...) ((void)0) |
@@ -201,12 +203,87 @@ typedef struct tls_state { | |||
201 | uint8_t inbuf[18*1024]; | 203 | uint8_t inbuf[18*1024]; |
202 | } tls_state_t; | 204 | } tls_state_t; |
203 | 205 | ||
206 | |||
207 | static unsigned get24be(const uint8_t *p) | ||
208 | { | ||
209 | return 0x100*(0x100*p[0] + p[1]) + p[2]; | ||
210 | } | ||
211 | |||
212 | #if TLS_DEBUG | ||
213 | static void dump_hex(const char *fmt, const void *vp, int len) | ||
214 | { | ||
215 | char hexbuf[32 * 1024 + 4]; | ||
216 | const uint8_t *p = vp; | ||
217 | |||
218 | bin2hex(hexbuf, (void*)p, len)[0] = '\0'; | ||
219 | dbg(fmt, hexbuf); | ||
220 | } | ||
221 | |||
222 | static void dump_tls_record(const void *vp, int len) | ||
223 | { | ||
224 | const uint8_t *p = vp; | ||
225 | |||
226 | while (len > 0) { | ||
227 | unsigned xhdr_len; | ||
228 | if (len < 5) { | ||
229 | dump_hex("< |%s|\n", p, len); | ||
230 | return; | ||
231 | } | ||
232 | xhdr_len = 0x100*p[3] + p[4]; | ||
233 | dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len); | ||
234 | p += 5; | ||
235 | len -= 5; | ||
236 | if (len >= 4 && p[-5] == RECORD_TYPE_HANDSHAKE) { | ||
237 | unsigned len24 = get24be(p + 1); | ||
238 | dbg(" type:%u len24:%u", p[0], len24); | ||
239 | } | ||
240 | if (xhdr_len > len) | ||
241 | xhdr_len = len; | ||
242 | dump_hex(" |%s|\n", p, xhdr_len); | ||
243 | p += xhdr_len; | ||
244 | len -= xhdr_len; | ||
245 | } | ||
246 | } | ||
247 | #endif | ||
248 | |||
204 | void tls_get_random(void *buf, unsigned len) | 249 | void tls_get_random(void *buf, unsigned len) |
205 | { | 250 | { |
206 | if (len != open_read_close("/dev/urandom", buf, len)) | 251 | if (len != open_read_close("/dev/urandom", buf, len)) |
207 | xfunc_die(); | 252 | xfunc_die(); |
208 | } | 253 | } |
209 | 254 | ||
255 | //TODO rename this to sha256_hash, and sha256_hash -> sha256_update | ||
256 | static void hash_sha256(uint8_t out[SHA256_OUTSIZE], const void *data, unsigned size) | ||
257 | { | ||
258 | sha256_ctx_t ctx; | ||
259 | sha256_begin(&ctx); | ||
260 | sha256_hash(&ctx, data, size); | ||
261 | sha256_end(&ctx, out); | ||
262 | } | ||
263 | |||
264 | #if TLS_DEBUG >= 2 | ||
265 | /* Nondestructively see the current hash value */ | ||
266 | static void sha256_peek(sha256_ctx_t *ctx, void *buffer) | ||
267 | { | ||
268 | sha256_ctx_t ctx_copy = *ctx; | ||
269 | sha256_end(&ctx_copy, buffer); | ||
270 | } | ||
271 | |||
272 | static void sha256_hash_dbg(const char *fmt, sha256_ctx_t *ctx, const void *buffer, size_t len) | ||
273 | { | ||
274 | uint8_t h[SHA256_OUTSIZE]; | ||
275 | |||
276 | sha256_hash(ctx, buffer, len); | ||
277 | dump_hex(fmt, buffer, len); | ||
278 | dbg(" (%u) ", (int)len); | ||
279 | sha256_peek(ctx, h); | ||
280 | dump_hex("%s\n", h, SHA256_OUTSIZE); | ||
281 | } | ||
282 | #else | ||
283 | # define sha256_hash_dbg(fmt, ctx, buffer, len) \ | ||
284 | sha256_hash(ctx, buffer, len) | ||
285 | #endif /* not TLS_DEBUG >= 2 */ | ||
286 | |||
210 | static | 287 | static |
211 | tls_state_t *new_tls_state(void) | 288 | tls_state_t *new_tls_state(void) |
212 | { | 289 | { |
@@ -284,53 +361,22 @@ static void xwrite_and_hash(tls_state_t *tls, /*const*/ void *buf, unsigned size | |||
284 | } | 361 | } |
285 | 362 | ||
286 | xwrite(tls->fd, buf, size); | 363 | xwrite(tls->fd, buf, size); |
364 | dbg("wrote %u bytes\n", size); | ||
287 | if (tls->encrypt_on_write) { | 365 | if (tls->encrypt_on_write) { |
288 | xwrite(tls->fd, mac_hash, sizeof(mac_hash)); | 366 | xwrite(tls->fd, mac_hash, sizeof(mac_hash)); |
367 | dbg("wrote %u bytes of hash\n", (int)sizeof(mac_hash)); | ||
289 | xhdr->len16_lo -= SHA256_OUTSIZE; | 368 | xhdr->len16_lo -= SHA256_OUTSIZE; |
290 | } | 369 | } |
291 | 370 | ||
292 | /* Handshake hash does not include record headers */ | 371 | /* Handshake hash does not include record headers */ |
293 | if (size > 5 && xhdr->type == RECORD_TYPE_HANDSHAKE) | 372 | if (size > 5 && xhdr->type == RECORD_TYPE_HANDSHAKE) { |
294 | sha256_hash(&tls->handshake_sha256_ctx, (uint8_t*)buf + 5, size - 5); | 373 | sha256_hash_dbg(">> sha256:%s", &tls->handshake_sha256_ctx, (uint8_t*)buf + 5, size - 5); |
295 | } | ||
296 | |||
297 | static unsigned get24be(const uint8_t *p) | ||
298 | { | ||
299 | return 0x100*(0x100*p[0] + p[1]) + p[2]; | ||
300 | } | ||
301 | |||
302 | static void dump(const void *vp, int len) | ||
303 | { | ||
304 | char hexbuf[32 * 1024 + 4]; | ||
305 | const uint8_t *p = vp; | ||
306 | |||
307 | while (len > 0) { | ||
308 | unsigned xhdr_len; | ||
309 | if (len < 5) { | ||
310 | bin2hex(hexbuf, (void*)p, len)[0] = '\0'; | ||
311 | dbg("< |%s|\n", hexbuf); | ||
312 | return; | ||
313 | } | ||
314 | xhdr_len = 0x100*p[3] + p[4]; | ||
315 | dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len); | ||
316 | p += 5; | ||
317 | len -= 5; | ||
318 | if (len >= 4 && p[-5] == RECORD_TYPE_HANDSHAKE) { | ||
319 | unsigned len24 = get24be(p + 1); | ||
320 | dbg(" type:%u len24:%u", p[0], len24); | ||
321 | } | ||
322 | if (xhdr_len > len) | ||
323 | xhdr_len = len; | ||
324 | bin2hex(hexbuf, (void*)p, xhdr_len)[0] = '\0'; | ||
325 | dbg(" |%s|\n", hexbuf); | ||
326 | p += xhdr_len; | ||
327 | len -= xhdr_len; | ||
328 | } | 374 | } |
329 | } | 375 | } |
330 | 376 | ||
331 | static void tls_error_die(tls_state_t *tls) | 377 | static void tls_error_die(tls_state_t *tls) |
332 | { | 378 | { |
333 | dump(tls->inbuf, tls->insize + tls->tail); | 379 | dump_tls_record(tls->inbuf, tls->insize + tls->tail); |
334 | xfunc_die(); | 380 | xfunc_die(); |
335 | } | 381 | } |
336 | 382 | ||
@@ -369,6 +415,14 @@ static int xread_tls_block(tls_state_t *tls) | |||
369 | tls->tail = total - target; | 415 | tls->tail = total - target; |
370 | tls->insize = target; | 416 | tls->insize = target; |
371 | target -= sizeof(*xhdr); | 417 | target -= sizeof(*xhdr); |
418 | |||
419 | /* RFC 5246 is not saying it explicitly, but sha256 hash | ||
420 | * in our FINISHED packet must include hashes of incoming packets too! | ||
421 | */ | ||
422 | if (tls->inbuf[0] == RECORD_TYPE_HANDSHAKE) { | ||
423 | sha256_hash_dbg("<< sha256:%s", &tls->handshake_sha256_ctx, tls->inbuf + 5, target); | ||
424 | } | ||
425 | |||
372 | dbg("got block len:%u\n", target); | 426 | dbg("got block len:%u\n", target); |
373 | return target; | 427 | return target; |
374 | } | 428 | } |
@@ -591,7 +645,7 @@ static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) | |||
591 | xfunc_die(); | 645 | xfunc_die(); |
592 | der++; | 646 | der++; |
593 | der = enter_der_item(der, &end); /* enter SEQ */ | 647 | der = enter_der_item(der, &end); /* enter SEQ */ |
594 | //memset(tls->server_rsa_pub_key, 0, sizeof(tls->server_rsa_pub_key)); | 648 | /* memset(tls->server_rsa_pub_key, 0, sizeof(tls->server_rsa_pub_key)); - already is */ |
595 | der_binary_to_pstm(&tls->server_rsa_pub_key.N, der, end); /* modulus */ | 649 | der_binary_to_pstm(&tls->server_rsa_pub_key.N, der, end); /* modulus */ |
596 | der = skip_der_item(der, end); | 650 | der = skip_der_item(der, end); |
597 | der_binary_to_pstm(&tls->server_rsa_pub_key.e, der, end); /* exponent */ | 651 | der_binary_to_pstm(&tls->server_rsa_pub_key.e, der, end); /* exponent */ |
@@ -599,14 +653,6 @@ static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) | |||
599 | dbg("server_rsa_pub_key.size:%d\n", tls->server_rsa_pub_key.size); | 653 | dbg("server_rsa_pub_key.size:%d\n", tls->server_rsa_pub_key.size); |
600 | } | 654 | } |
601 | 655 | ||
602 | static void hash_sha256(uint8_t out[SHA256_OUTSIZE], const void *data, unsigned size) | ||
603 | { | ||
604 | sha256_ctx_t ctx; | ||
605 | sha256_begin(&ctx); | ||
606 | sha256_hash(&ctx, data, size); | ||
607 | sha256_end(&ctx, out); | ||
608 | } | ||
609 | |||
610 | // RFC 2104: HMAC(key, text) based on a hash H (say, sha256) is: | 656 | // RFC 2104: HMAC(key, text) based on a hash H (say, sha256) is: |
611 | // ipad = [0x36 x INSIZE] | 657 | // ipad = [0x36 x INSIZE] |
612 | // opad = [0x5c x INSIZE] | 658 | // opad = [0x5c x INSIZE] |
@@ -722,6 +768,7 @@ static void tls_prf_hmac_sha256( | |||
722 | 768 | ||
723 | /* A(1) = HMAC_hash(secret, seed) */ | 769 | /* A(1) = HMAC_hash(secret, seed) */ |
724 | hmac_sha256(a, SECRET, SEED, NULL); | 770 | hmac_sha256(a, SECRET, SEED, NULL); |
771 | //TODO: convert hmac_sha256 to precomputed | ||
725 | 772 | ||
726 | for(;;) { | 773 | for(;;) { |
727 | /* HMAC_hash(secret, A(1) + seed) */ | 774 | /* HMAC_hash(secret, A(1) + seed) */ |
@@ -785,24 +832,10 @@ static void send_client_hello(tls_state_t *tls) | |||
785 | hello.comprtypes_len = 1; | 832 | hello.comprtypes_len = 1; |
786 | //hello.comprtypes[0] = 0; | 833 | //hello.comprtypes[0] = 0; |
787 | 834 | ||
835 | //dbg (make it repeatable): memset(hello.rand32, 0x11, sizeof(hello.rand32)); | ||
836 | dbg(">> HANDSHAKE_CLIENT_HELLO\n"); | ||
788 | xwrite_and_hash(tls, &hello, sizeof(hello)); | 837 | xwrite_and_hash(tls, &hello, sizeof(hello)); |
789 | memcpy(tls->client_and_server_rand32, hello.rand32, sizeof(hello.rand32)); | 838 | memcpy(tls->client_and_server_rand32, hello.rand32, sizeof(hello.rand32)); |
790 | |||
791 | #if 0 /* dump */ | ||
792 | for (;;) { | ||
793 | uint8_t buf[16*1024]; | ||
794 | sleep(2); | ||
795 | len = recv(tls->fd, buf, sizeof(buf), 0); //MSG_DONTWAIT); | ||
796 | if (len < 0) { | ||
797 | if (errno == EAGAIN) | ||
798 | continue; | ||
799 | bb_perror_msg_and_die("recv"); | ||
800 | } | ||
801 | if (len == 0) | ||
802 | break; | ||
803 | dump(buf, len); | ||
804 | } | ||
805 | #endif | ||
806 | } | 839 | } |
807 | 840 | ||
808 | static void get_server_hello(tls_state_t *tls) | 841 | static void get_server_hello(tls_state_t *tls) |
@@ -905,23 +938,17 @@ static void send_client_key_exchange(tls_state_t *tls) | |||
905 | //openssl: | 938 | //openssl: |
906 | //write to 0xe9a090 [0xf9ac20] (395 bytes => 395 (0x18B)) | 939 | //write to 0xe9a090 [0xf9ac20] (395 bytes => 395 (0x18B)) |
907 | //0000 - 16 03 03 01 86 10 00 01 -82 01 80 xx xx xx xx xx | 940 | //0000 - 16 03 03 01 86 10 00 01 -82 01 80 xx xx xx xx xx |
908 | uint8_t key[384]; // size?? | 941 | uint8_t key[4 * 1024]; // size?? |
909 | }; | 942 | }; |
910 | struct client_key_exchange record; | 943 | struct client_key_exchange record; |
911 | uint8_t rsa_premaster[SSL_HS_RSA_PREMASTER_SIZE]; | 944 | uint8_t rsa_premaster[SSL_HS_RSA_PREMASTER_SIZE]; |
945 | int len; | ||
912 | 946 | ||
913 | memset(&record, 0, sizeof(record)); | 947 | memset(&record, 0, sizeof(record)); |
914 | record.xhdr.type = RECORD_TYPE_HANDSHAKE; | 948 | record.xhdr.type = RECORD_TYPE_HANDSHAKE; |
915 | record.xhdr.proto_maj = TLS_MAJ; | 949 | record.xhdr.proto_maj = TLS_MAJ; |
916 | record.xhdr.proto_min = TLS_MIN; | 950 | record.xhdr.proto_min = TLS_MIN; |
917 | record.xhdr.len16_hi = (sizeof(record) - sizeof(record.xhdr)) >> 8; | ||
918 | record.xhdr.len16_lo = (sizeof(record) - sizeof(record.xhdr)) & 0xff; | ||
919 | record.type = HANDSHAKE_CLIENT_KEY_EXCHANGE; | 951 | record.type = HANDSHAKE_CLIENT_KEY_EXCHANGE; |
920 | //record.len24_hi = 0; | ||
921 | record.len24_mid = (sizeof(record) - sizeof(record.xhdr) - 4) >> 8; | ||
922 | record.len24_lo = (sizeof(record) - sizeof(record.xhdr) - 4) & 0xff; | ||
923 | record.keylen16_hi = (sizeof(record) - sizeof(record.xhdr) - 6) >> 8; | ||
924 | record.keylen16_lo = (sizeof(record) - sizeof(record.xhdr) - 6) & 0xff; | ||
925 | 952 | ||
926 | tls_get_random(rsa_premaster, sizeof(rsa_premaster)); | 953 | tls_get_random(rsa_premaster, sizeof(rsa_premaster)); |
927 | // RFC 5246 | 954 | // RFC 5246 |
@@ -930,78 +957,92 @@ static void send_client_key_exchange(tls_state_t *tls) | |||
930 | // version negotiated for the connection." | 957 | // version negotiated for the connection." |
931 | rsa_premaster[0] = TLS_MAJ; | 958 | rsa_premaster[0] = TLS_MAJ; |
932 | rsa_premaster[1] = TLS_MIN; | 959 | rsa_premaster[1] = TLS_MIN; |
933 | psRsaEncryptPub(/*pool:*/ NULL, | 960 | len = psRsaEncryptPub(/*pool:*/ NULL, |
934 | /* psRsaKey_t* */ &tls->server_rsa_pub_key, | 961 | /* psRsaKey_t* */ &tls->server_rsa_pub_key, |
935 | rsa_premaster, /*inlen:*/ sizeof(rsa_premaster), | 962 | rsa_premaster, /*inlen:*/ sizeof(rsa_premaster), |
936 | record.key, sizeof(record.key), | 963 | record.key, sizeof(record.key), |
937 | data_param_ignored | 964 | data_param_ignored |
938 | ); | 965 | ); |
966 | record.keylen16_hi = len >> 8; | ||
967 | record.keylen16_lo = len & 0xff; | ||
968 | len += 2; | ||
969 | //record.len24_hi = 0; | ||
970 | record.len24_mid = len >> 8; | ||
971 | record.len24_lo = len & 0xff; | ||
972 | len += 4; | ||
973 | record.xhdr.len16_hi = len >> 8; | ||
974 | record.xhdr.len16_lo = len & 0xff; | ||
939 | 975 | ||
940 | xwrite_and_hash(tls, &record, sizeof(record)); | 976 | dbg(">> HANDSHAKE_CLIENT_KEY_EXCHANGE\n"); |
977 | xwrite_and_hash(tls, &record, sizeof(record.xhdr) + len); | ||
941 | 978 | ||
942 | // RFC 5246 | 979 | // RFC 5246 |
943 | // For all key exchange methods, the same algorithm is used to convert | 980 | // For all key exchange methods, the same algorithm is used to convert |
944 | // the pre_master_secret into the master_secret. The pre_master_secret | 981 | // the pre_master_secret into the master_secret. The pre_master_secret |
945 | // should be deleted from memory once the master_secret has been | 982 | // should be deleted from memory once the master_secret has been |
946 | // computed. | 983 | // computed. |
947 | // master_secret = PRF(pre_master_secret, "master secret", | 984 | // master_secret = PRF(pre_master_secret, "master secret", |
948 | // ClientHello.random + ServerHello.random) | 985 | // ClientHello.random + ServerHello.random) |
949 | // [0..47]; | 986 | // [0..47]; |
950 | // The master secret is always exactly 48 bytes in length. The length | 987 | // The master secret is always exactly 48 bytes in length. The length |
951 | // of the premaster secret will vary depending on key exchange method. | 988 | // of the premaster secret will vary depending on key exchange method. |
952 | tls_prf_hmac_sha256( | 989 | tls_prf_hmac_sha256( |
953 | tls->master_secret, sizeof(tls->master_secret), | 990 | tls->master_secret, sizeof(tls->master_secret), |
954 | rsa_premaster, sizeof(rsa_premaster), | 991 | rsa_premaster, sizeof(rsa_premaster), |
955 | "master secret", | 992 | "master secret", |
956 | tls->client_and_server_rand32, sizeof(tls->client_and_server_rand32) | 993 | tls->client_and_server_rand32, sizeof(tls->client_and_server_rand32) |
957 | ); | 994 | ); |
995 | dump_hex("master secret:%s\n", tls->master_secret, sizeof(tls->master_secret)); | ||
958 | 996 | ||
959 | // RFC 5246 | 997 | // RFC 5246 |
960 | // 6.3. Key Calculation | 998 | // 6.3. Key Calculation |
961 | // | 999 | // |
962 | // The Record Protocol requires an algorithm to generate keys required | 1000 | // The Record Protocol requires an algorithm to generate keys required |
963 | // by the current connection state (see Appendix A.6) from the security | 1001 | // by the current connection state (see Appendix A.6) from the security |
964 | // parameters provided by the handshake protocol. | 1002 | // parameters provided by the handshake protocol. |
965 | // | 1003 | // |
966 | // The master secret is expanded into a sequence of secure bytes, which | 1004 | // The master secret is expanded into a sequence of secure bytes, which |
967 | // is then split to a client write MAC key, a server write MAC key, a | 1005 | // is then split to a client write MAC key, a server write MAC key, a |
968 | // client write encryption key, and a server write encryption key. Each | 1006 | // client write encryption key, and a server write encryption key. Each |
969 | // of these is generated from the byte sequence in that order. Unused | 1007 | // of these is generated from the byte sequence in that order. Unused |
970 | // values are empty. Some AEAD ciphers may additionally require a | 1008 | // values are empty. Some AEAD ciphers may additionally require a |
971 | // client write IV and a server write IV (see Section 6.2.3.3). | 1009 | // client write IV and a server write IV (see Section 6.2.3.3). |
972 | // | 1010 | // |
973 | // When keys and MAC keys are generated, the master secret is used as an | 1011 | // When keys and MAC keys are generated, the master secret is used as an |
974 | // entropy source. | 1012 | // entropy source. |
975 | // | 1013 | // |
976 | // To generate the key material, compute | 1014 | // To generate the key material, compute |
977 | // | 1015 | // |
978 | // key_block = PRF(SecurityParameters.master_secret, | 1016 | // key_block = PRF(SecurityParameters.master_secret, |
979 | // "key expansion", | 1017 | // "key expansion", |
980 | // SecurityParameters.server_random + | 1018 | // SecurityParameters.server_random + |
981 | // SecurityParameters.client_random); | 1019 | // SecurityParameters.client_random); |
982 | // | 1020 | // |
983 | // until enough output has been generated. Then, the key_block is | 1021 | // until enough output has been generated. Then, the key_block is |
984 | // partitioned as follows: | 1022 | // partitioned as follows: |
985 | // | 1023 | // |
986 | // client_write_MAC_key[SecurityParameters.mac_key_length] | 1024 | // client_write_MAC_key[SecurityParameters.mac_key_length] |
987 | // server_write_MAC_key[SecurityParameters.mac_key_length] | 1025 | // server_write_MAC_key[SecurityParameters.mac_key_length] |
988 | // client_write_key[SecurityParameters.enc_key_length] | 1026 | // client_write_key[SecurityParameters.enc_key_length] |
989 | // server_write_key[SecurityParameters.enc_key_length] | 1027 | // server_write_key[SecurityParameters.enc_key_length] |
990 | // client_write_IV[SecurityParameters.fixed_iv_length] | 1028 | // client_write_IV[SecurityParameters.fixed_iv_length] |
991 | // server_write_IV[SecurityParameters.fixed_iv_length] | 1029 | // server_write_IV[SecurityParameters.fixed_iv_length] |
992 | { | 1030 | { |
993 | uint8_t tmp64[64]; | 1031 | uint8_t tmp64[64]; |
994 | /* make server_rand32 + client_rand32 */ | 1032 | /* make server_rand32 + client_rand32 */ |
995 | memcpy(&tmp64[0] , &tls->client_and_server_rand32[32], 32); | 1033 | memcpy(&tmp64[0] , &tls->client_and_server_rand32[32], 32); |
996 | memcpy(&tmp64[32], &tls->client_and_server_rand32[0] , 32); | 1034 | memcpy(&tmp64[32], &tls->client_and_server_rand32[0] , 32); |
997 | 1035 | ||
998 | tls_prf_hmac_sha256( | 1036 | tls_prf_hmac_sha256( |
999 | tls->client_write_MAC_key, sizeof(tls->client_write_MAC_key), | 1037 | tls->client_write_MAC_key, sizeof(tls->client_write_MAC_key), |
1000 | tls->master_secret, sizeof(tls->master_secret), | 1038 | tls->master_secret, sizeof(tls->master_secret), |
1001 | "key expansion", | 1039 | "key expansion", |
1002 | tmp64, 64 | 1040 | tmp64, 64 |
1003 | ); | 1041 | ); |
1004 | } | 1042 | dump_hex("client_write_MAC_key:%s\n", |
1043 | tls->client_write_MAC_key, sizeof(tls->client_write_MAC_key) | ||
1044 | ); | ||
1045 | } | ||
1005 | } | 1046 | } |
1006 | 1047 | ||
1007 | static void send_change_cipher_spec(tls_state_t *tls) | 1048 | static void send_change_cipher_spec(tls_state_t *tls) |
@@ -1011,14 +1052,13 @@ static void send_change_cipher_spec(tls_state_t *tls) | |||
1011 | 01 | 1052 | 01 |
1012 | }; | 1053 | }; |
1013 | /* Not "xwrite_and_hash": this is not a handshake message */ | 1054 | /* Not "xwrite_and_hash": this is not a handshake message */ |
1055 | dbg(">> CHANGE_CIPHER_SPEC\n"); | ||
1014 | xwrite(tls->fd, rec, sizeof(rec)); | 1056 | xwrite(tls->fd, rec, sizeof(rec)); |
1015 | 1057 | ||
1016 | tls->write_seq64_be = 0; | 1058 | tls->write_seq64_be = 0; |
1017 | tls->encrypt_on_write = 1; | 1059 | tls->encrypt_on_write = 1; |
1018 | } | 1060 | } |
1019 | 1061 | ||
1020 | static void send_client_finished(tls_state_t *tls) | ||
1021 | { | ||
1022 | // 7.4.9. Finished | 1062 | // 7.4.9. Finished |
1023 | // A Finished message is always sent immediately after a change | 1063 | // A Finished message is always sent immediately after a change |
1024 | // cipher spec message to verify that the key exchange and | 1064 | // cipher spec message to verify that the key exchange and |
@@ -1056,6 +1096,8 @@ static void send_client_finished(tls_state_t *tls) | |||
1056 | // suite. Any cipher suite which does not explicitly specify | 1096 | // suite. Any cipher suite which does not explicitly specify |
1057 | // verify_data_length has a verify_data_length equal to 12. This | 1097 | // verify_data_length has a verify_data_length equal to 12. This |
1058 | // includes all existing cipher suites. | 1098 | // includes all existing cipher suites. |
1099 | static void send_client_finished(tls_state_t *tls) | ||
1100 | { | ||
1059 | struct client_finished { | 1101 | struct client_finished { |
1060 | struct record_hdr xhdr; | 1102 | struct record_hdr xhdr; |
1061 | uint8_t type; | 1103 | uint8_t type; |
@@ -1085,41 +1127,16 @@ static void send_client_finished(tls_state_t *tls) | |||
1085 | "client finished", | 1127 | "client finished", |
1086 | handshake_hash, sizeof(handshake_hash) | 1128 | handshake_hash, sizeof(handshake_hash) |
1087 | ); | 1129 | ); |
1130 | dump_hex("from secret: %s\n", tls->master_secret, sizeof(tls->master_secret)); | ||
1131 | dump_hex("from labelSeed: %s", "client finished", sizeof("client finished")-1); | ||
1132 | dump_hex("%s\n", handshake_hash, sizeof(handshake_hash)); | ||
1133 | dump_hex("=> digest: %s\n", record.prf_result, sizeof(record.prf_result)); | ||
1088 | 1134 | ||
1089 | //(1) TODO: well, this should be encrypted on send, really. | 1135 | //(1) TODO: well, this should be encrypted on send, really. |
1090 | //(2) do we really need to also hash it? | 1136 | //(2) do we really need to also hash it? |
1091 | 1137 | ||
1138 | dbg(">> HANDSHAKE_FINISHED\n"); | ||
1092 | xwrite_and_hash(tls, &record, sizeof(record)); | 1139 | xwrite_and_hash(tls, &record, sizeof(record)); |
1093 | |||
1094 | //s_client does this: | ||
1095 | // | ||
1096 | //write to 0x1d750b0 [0x1e74620] (6 bytes => 6 (0x6)) | ||
1097 | //0000 - 14 03 03 00 01 01 ...... >> CHANGE_CIPHER_SPEC | ||
1098 | // | ||
1099 | //write to 0x1d750b0 [0x1e74620] (53 bytes => 53 (0x35)) | ||
1100 | //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] | ||
1101 | //0010 - c0 d1 d4 0b a3|73 ec a8-fa b5 cb 12 b6 4c 2a b1 .....s.......L*. | ||
1102 | //0020 - fb 42 7f 73 0d 06 1c 87-56 f0 db df e6 6a 25 aa .B.s....V....j%. | ||
1103 | //0030 - fc 42 38 cb 0b] .B8.. | ||
1104 | |||
1105 | //we do this (as seen by s_server): | ||
1106 | // | ||
1107 | //read from 0x26b7650 [0x26ac383] (5 bytes => 5 (0x5)) | ||
1108 | //0000 - 14 03 03 00 01 ..... | ||
1109 | //read from 0x26b7650 [0x26ac388] (1 bytes => 1 (0x1)) | ||
1110 | //0000 - 01 . << CHANGE_CIPHER_SPEC | ||
1111 | //read from 0x26b7650 [0x26ac383] (5 bytes => 5 (0x5)) | ||
1112 | //0000 - 16 03 03 0030 ....0 | ||
1113 | //read from 0x26b7650 [0x26ac388] (48 bytes => 48 (0x30)) | ||
1114 | //0000 - 14 00000c [d1 6f 08 c3-ef ec 26 7c 17 76 ac 1c| .....o....&|.v.. << FINISHED (0x14) [PRF 12 bytes|SHA256_OUTSIZE 32 bytes] | ||
1115 | //0010 - 0b 27 ae 1c 59 f8 cb af-de 25 25 7e 50 51 da dd .'..Y....%%~PQ.. | ||
1116 | //0020 - c2 9e d6 27 96 0b 72 05-63 54 89 00 0e d7 b1 10] ...'..r.cT...... | ||
1117 | // | ||
1118 | //write to 0x26b7650 [0x26b4d90] (7 bytes => 7 (0x7)) | ||
1119 | //0000 - 15 03 03 00 02 02 33 ......3 | ||
1120 | //ERROR | ||
1121 | //140684206577528:error:1408C095:SSL routines:ssl3_get_finished:digest check failed:s3_both.c:273: | ||
1122 | //140684206577528:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:s3_pkt.c:656: | ||
1123 | } | 1140 | } |
1124 | 1141 | ||
1125 | static void get_change_cipher_spec(tls_state_t *tls) | 1142 | static void get_change_cipher_spec(tls_state_t *tls) |
@@ -1196,6 +1213,7 @@ static void tls_handshake(tls_state_t *tls) | |||
1196 | send_change_cipher_spec(tls); | 1213 | send_change_cipher_spec(tls); |
1197 | //we now should be able to send encrypted... as soon as we grok AES. | 1214 | //we now should be able to send encrypted... as soon as we grok AES. |
1198 | send_client_finished(tls); | 1215 | send_client_finished(tls); |
1216 | |||
1199 | get_change_cipher_spec(tls); | 1217 | get_change_cipher_spec(tls); |
1200 | get_server_finished(tls); | 1218 | get_server_finished(tls); |
1201 | //we now should receive encrypted, and application data can be sent/received | 1219 | //we now should receive encrypted, and application data can be sent/received |
@@ -1271,64 +1289,7 @@ read from 0x1d750b0 [0x1e6ac08] (1035 bytes => 1035 (0x40B)) | |||
1271 | 0020 - 2e 2b 30 0d 06 09 2a 86-48 86 f7 0d 01 01 0b 05 .+0...*.H....... | 1289 | 0020 - 2e 2b 30 0d 06 09 2a 86-48 86 f7 0d 01 01 0b 05 .+0...*.H....... |
1272 | 0030 - 00 30 14 31 12 30 10 06-03 55 04 03 0c 09 6c 6f .0.1.0...U....lo | 1290 | 0030 - 00 30 14 31 12 30 10 06-03 55 04 03 0c 09 6c 6f .0.1.0...U....lo |
1273 | 0040 - 63 61 6c 68 6f 73 74 30-20 17 0d 31 37 30 31 31 calhost0 ..17011 | 1291 | 0040 - 63 61 6c 68 6f 73 74 30-20 17 0d 31 37 30 31 31 calhost0 ..17011 |
1274 | 0050 - 36 30 33 30 39 34 36 5a-18 0f 32 32 39 30 31 30 6030946Z..229010 | 1292 | ..."......."......."......."......."......."......."......."......."..... |
1275 | 0060 - 33 31 30 33 30 39 34 36-5a 30 14 31 12 30 10 06 31030946Z0.1.0.. | ||
1276 | 0070 - 03 55 04 03 0c 09 6c 6f-63 61 6c 68 6f 73 74 30 .U....localhost0 | ||
1277 | 0080 - 82 01 a2 30 0d 06 09 2a-86 48 86 f7 0d 01 01 01 ...0...*.H...... | ||
1278 | 0090 - 05 00 03 82 01 8f 00 30-82 01 8a 02 82 01 81 00 .......0........ | ||
1279 | 00a0 - f5 a8 66 54 af fd 33 9e-fb 1d 2d d7 33 ff b9 99 ..fT..3...-.3... | ||
1280 | 00b0 - 21 16 8a 65 75 1c 39 9d-0a c3 fc 8c 6d 63 de 7b !..eu.9.....mc.{ | ||
1281 | 00c0 - d8 1e d8 20 a1 a9 d5 ab-2a 8e d1 4f b8 9a f4 f7 ... ....*..O.... | ||
1282 | 00d0 - 91 0d db 70 a8 59 d3 0d-8f 6f bb 51 a4 5c f1 c1 ...p.Y...o.Q.\.. | ||
1283 | 00e0 - ce 1e b5 58 3a 46 1b 2a-1f 7c 75 87 b4 9f ed d3 ...X:F.*.|u..... | ||
1284 | 00f0 - 3d 9a cf a4 5b 96 5b 25-cf f5 33 1c c8 7a 8a 37 =...[.[%..3..z.7 | ||
1285 | 0100 - 69 2f 63 8c f9 a4 a5 40-06 c9 63 cf fd 35 5d d3 i/c....@..c..5]. | ||
1286 | 0110 - 8e e3 17 2f 8a ab 5e fd-6c 7e 5b 03 bb b6 3c e3 .../..^.l~[...<. | ||
1287 | 0120 - 32 01 99 72 45 a7 a3 c3-11 d8 6c dc 50 a6 75 01 2..rE.....l.P.u. | ||
1288 | 0130 - a2 81 85 56 5e be eb 8c-ad 44 05 cf 47 e0 3e 8b ...V^....D..G.>. | ||
1289 | 0140 - cd 6e 08 11 ee 5e 94 39-5d 8c a2 e3 b0 03 7f c2 .n...^.9]....... | ||
1290 | 0150 - f8 99 35 0b d1 c2 3c 37-49 a8 bb 70 8e e2 a6 c8 ..5...<7I..p.... | ||
1291 | 0160 - 5b 7d f5 d1 dc 6e ef fb-83 f7 4c ef 8c 85 e3 b9 [}...n....L..... | ||
1292 | 0170 - fd 51 18 70 7e a5 03 b7-6d e5 c6 c2 d1 4d e2 16 .Q.p~...m....M.. | ||
1293 | 0180 - 42 c8 21 25 2a 27 af c4-4a c2 f0 b6 2f 72 46 33 B.!%*'..J.../rF3 | ||
1294 | 0190 - b1 89 d8 95 7a 9d 52 db-75 ae f4 a0 97 32 8c 5f ....z.R.u....2._ | ||
1295 | 01a0 - 97 e7 5d 12 15 3f 85 8f-d1 9c ca a0 fd 59 cd c5 ..]..?.......Y.. | ||
1296 | 01b0 - 25 70 d3 97 62 04 af cb-19 1b 6e 24 c9 82 b8 25 %p..b.....n$...% | ||
1297 | 01c0 - c3 5c bd d7 ee 08 17 6f-ea 99 24 2f c7 05 a8 40 .\.....o..$/...@ | ||
1298 | 01d0 - 24 d5 74 7b 5b 65 c5 ec-97 63 de d1 46 d2 c4 09 $.t{[e...c..F... | ||
1299 | 01e0 - c2 22 23 99 ce 13 03 6f-b4 08 7f 3e 00 c6 fe 91 ."#....o...>.... | ||
1300 | 01f0 - bc c8 ba b2 32 c2 42 9d-89 de 04 d0 36 27 9e 8c ....2.B.....6'.. | ||
1301 | 0200 - ad 79 3e d6 84 38 db cd-57 ad 68 05 85 0b 13 59 .y>..8..W.h....Y | ||
1302 | 0210 - 11 5a 2b a4 d0 5f c3 ee-1a 02 69 d4 c5 bf 19 b5 .Z+.._....i..... | ||
1303 | 0220 - 02 03 01 00 01 a3 50 30-4e 30 1d 06 03 55 1d 0e ......P0N0...U.. | ||
1304 | 0230 - 04 16 04 14 08 87 29 ce-04 09 7f 11 33 80 ba 30 ......).....3..0 | ||
1305 | 0240 - b2 77 84 b4 73 6e 82 94-30 1f 06 03 55 1d 23 04 .w..sn..0...U.#. | ||
1306 | 0250 - 18 30 16 80 14 08 87 29-ce 04 09 7f 11 33 80 ba .0.....).....3.. | ||
1307 | 0260 - 30 b2 77 84 b4 73 6e 82-94 30 0c 06 03 55 1d 13 0.w..sn..0...U.. | ||
1308 | 0270 - 04 05 30 03 01 01 ff 30-0d 06 09 2a 86 48 86 f7 ..0....0...*.H.. | ||
1309 | 0280 - 0d 01 01 0b 05 00 03 82-01 81 00 22 c4 a8 6f 0a ..........."..o. | ||
1310 | 0290 - 40 a0 c4 92 ae 83 86 9c-42 f8 0a a8 c7 72 d2 5d @.......B....r.] | ||
1311 | 02a0 - de 78 f2 5a 38 e0 e6 0b-3c df af dd 6c 44 a9 0a .x.Z8...<...lD.. | ||
1312 | 02b0 - 81 f3 0e 49 e5 07 da 40-0f 85 73 db d6 86 87 6a ...I...@..s....j | ||
1313 | 02c0 - b9 29 75 6a a9 dd fc 16-81 dc eb 25 3a c7 dc 3f .)uj.......%:..? | ||
1314 | 02d0 - c8 a3 2d bb 49 3e 08 72-e9 03 e6 2f ee 1e 17 9a ..-.I>.r.../.... | ||
1315 | 02e0 - 9c f6 fd 3c 73 cf e4 2c-5d f1 a8 1c 93 f7 1d f7 ...<s..,]....... | ||
1316 | 02f0 - ae b2 96 bf ba bd d0 5d-3f 4e d3 54 5e 81 47 72 .......]?N.T^.Gr | ||
1317 | 0300 - a1 35 39 db fa a7 54 96-f4 1f e5 5e c8 80 f8 1f .59...T....^.... | ||
1318 | 0310 - 73 e5 09 14 ae e5 4a c2-f3 72 5c d8 52 6e d8 d2 s.....J..r\.Rn.. | ||
1319 | 0320 - e9 de 48 14 56 fc 32 ce-69 a3 68 a1 e9 f5 78 e8 ..H.V.2.i.h...x. | ||
1320 | 0330 - ad 84 a8 c3 45 1a 3a 7d-54 db e3 ce 84 9e b1 d7 ....E.:}T....... | ||
1321 | 0340 - 60 8e b2 e7 15 84 8e a5-0f c4 53 e4 b9 71 e7 46 `.........S..q.F | ||
1322 | 0350 - 93 f7 af 00 bd 3d 36 9a-4e 8f eb e1 18 06 54 49 .....=6.N.....TI | ||
1323 | 0360 - 4a 79 9f 3d db 9d d6 e7-57 b7 75 dd 26 ac b7 86 Jy.=....W.u.&... | ||
1324 | 0370 - 19 f0 f9 b6 5e 35 36 a4-5a a7 b0 33 88 9e 7f c9 ....^56.Z..3.... | ||
1325 | 0380 - f3 3e cc 02 ce 49 3c 0e-25 42 27 bf 7b 38 db d8 .>...I<.%B'.{8.. | ||
1326 | 0390 - 3a 87 6f db 79 7a 54 c1-a3 6a 54 01 17 92 04 24 :.o.yzT..jT....$ | ||
1327 | 03a0 - 6c 66 1d 47 45 4e 3a af-fe 45 1d 2c 23 81 f6 cc lf.GEN:..E.,#... | ||
1328 | 03b0 - e1 0d 89 81 90 b6 3b 7d-49 8d 0f d5 d8 f6 d8 0c ......;}I....... | ||
1329 | 03c0 - 7e 47 1e fb b7 a0 7a 66-7a 1e f2 ba 4e 28 ed 36 ~G....zfz...N(.6 | ||
1330 | 03d0 - 6b b6 2b e5 9c 57 0e ba-df 44 1b 4d 5e a2 9e 3f k.+..W...D.M^..? | ||
1331 | 03e0 - 29 18 b6 7c 26 ea 6a d5-0d f1 e9 42 fa 08 0d 44 )..|&.j....B...D | ||
1332 | 03f0 - 11 8a cd c5 a3 0a 22 43-d5 13 f9 a5 8a 06 f9 00 ......"C........ | 1293 | 03f0 - 11 8a cd c5 a3 0a 22 43-d5 13 f9 a5 8a 06 f9 00 ......"C........ |
1333 | 0400 - 3c f7 86 4e e8 a5 d8 5b-92 37 f5 <..N...[.7. | 1294 | 0400 - 3c f7 86 4e e8 a5 d8 5b-92 37 f5 <..N...[.7. |
1334 | depth=0 CN = localhost | 1295 | depth=0 CN = localhost |
@@ -1413,28 +1374,7 @@ Certificate chain | |||
1413 | --- | 1374 | --- |
1414 | Server certificate | 1375 | Server certificate |
1415 | -----BEGIN CERTIFICATE----- | 1376 | -----BEGIN CERTIFICATE----- |
1416 | MIID/TCCAmWgAwIBAgIJANnZjbiUrS4rMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV | 1377 | ..."......."......."......."......."......."......."......."......."..... |
1417 | BAMMCWxvY2FsaG9zdDAgFw0xNzAxMTYwMzA5NDZaGA8yMjkwMTAzMTAzMDk0Nlow | ||
1418 | FDESMBAGA1UEAwwJbG9jYWxob3N0MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB | ||
1419 | igKCAYEA9ahmVK/9M577HS3XM/+5mSEWimV1HDmdCsP8jG1j3nvYHtggoanVqyqO | ||
1420 | 0U+4mvT3kQ3bcKhZ0w2Pb7tRpFzxwc4etVg6RhsqH3x1h7Sf7dM9ms+kW5ZbJc/1 | ||
1421 | MxzIeoo3aS9jjPmkpUAGyWPP/TVd047jFy+Kq179bH5bA7u2POMyAZlyRaejwxHY | ||
1422 | bNxQpnUBooGFVl6+64ytRAXPR+A+i81uCBHuXpQ5XYyi47ADf8L4mTUL0cI8N0mo | ||
1423 | u3CO4qbIW3310dxu7/uD90zvjIXjuf1RGHB+pQO3beXGwtFN4hZCyCElKievxErC | ||
1424 | 8LYvckYzsYnYlXqdUtt1rvSglzKMX5fnXRIVP4WP0ZzKoP1ZzcUlcNOXYgSvyxkb | ||
1425 | biTJgrglw1y91+4IF2/qmSQvxwWoQCTVdHtbZcXsl2Pe0UbSxAnCIiOZzhMDb7QI | ||
1426 | fz4Axv6RvMi6sjLCQp2J3gTQNieejK15PtaEONvNV61oBYULE1kRWiuk0F/D7hoC | ||
1427 | adTFvxm1AgMBAAGjUDBOMB0GA1UdDgQWBBQIhynOBAl/ETOAujCyd4S0c26ClDAf | ||
1428 | BgNVHSMEGDAWgBQIhynOBAl/ETOAujCyd4S0c26ClDAMBgNVHRMEBTADAQH/MA0G | ||
1429 | CSqGSIb3DQEBCwUAA4IBgQAixKhvCkCgxJKug4acQvgKqMdy0l3eePJaOODmCzzf | ||
1430 | r91sRKkKgfMOSeUH2kAPhXPb1oaHarkpdWqp3fwWgdzrJTrH3D/Ioy27ST4IcukD | ||
1431 | 5i/uHheanPb9PHPP5Cxd8agck/cd966ylr+6vdBdP07TVF6BR3KhNTnb+qdUlvQf | ||
1432 | 5V7IgPgfc+UJFK7lSsLzclzYUm7Y0uneSBRW/DLOaaNooen1eOithKjDRRo6fVTb | ||
1433 | 486EnrHXYI6y5xWEjqUPxFPkuXHnRpP3rwC9PTaaTo/r4RgGVElKeZ89253W51e3 | ||
1434 | dd0mrLeGGfD5tl41NqRap7AziJ5/yfM+zALOSTwOJUInv3s429g6h2/beXpUwaNq | ||
1435 | VAEXkgQkbGYdR0VOOq/+RR0sI4H2zOENiYGQtjt9SY0P1dj22Ax+Rx77t6B6Znoe | ||
1436 | 8rpOKO02a7Yr5ZxXDrrfRBtNXqKePykYtnwm6mrVDfHpQvoIDUQRis3FowoiQ9UT | ||
1437 | +aWKBvkAPPeGTuil2FuSN/U= | ||
1438 | -----END CERTIFICATE----- | 1378 | -----END CERTIFICATE----- |
1439 | subject=/CN=localhost | 1379 | subject=/CN=localhost |
1440 | issuer=/CN=localhost | 1380 | issuer=/CN=localhost |