aboutsummaryrefslogtreecommitdiff
path: root/networking/tls.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-24 18:08:07 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-24 18:08:07 +0100
commit89193f985bf50af702e0f98a3c86573277c03287 (patch)
treee04c0a8f56e868bb599fa92de4c015d95da9ddbc /networking/tls.c
parent1500b3a50d587adeca7be96b36394f23f2d80a82 (diff)
downloadbusybox-w32-89193f985bf50af702e0f98a3c86573277c03287.tar.gz
busybox-w32-89193f985bf50af702e0f98a3c86573277c03287.tar.bz2
busybox-w32-89193f985bf50af702e0f98a3c86573277c03287.zip
tls: can download kernels now :)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls.c')
-rw-r--r--networking/tls.c172
1 files changed, 120 insertions, 52 deletions
diff --git a/networking/tls.c b/networking/tls.c
index dc94a8b9b..335324568 100644
--- a/networking/tls.c
+++ b/networking/tls.c
@@ -53,15 +53,14 @@
53// ok: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-SHA 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) 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 55#define CIPHER_ID1 TLS_RSA_WITH_AES_256_CBC_SHA256 // no SERVER_KEY_EXCHANGE from peer
56// Does not work yet: 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 57#define CIPHER_ID2 TLS_RSA_WITH_AES_128_CBC_SHA
58#define CIPHER_ID2 0
59 58
60 59
61#define TLS_DEBUG 1 60#define TLS_DEBUG 0
62#define TLS_DEBUG_HASH 1 61#define TLS_DEBUG_HASH 0
63#define TLS_DEBUG_DER 1 62#define TLS_DEBUG_DER 0
64#define TLS_DEBUG_FIXED_SECRETS 1 63#define TLS_DEBUG_FIXED_SECRETS 0
65#if 0 64#if 0
66# define dump_raw_out(...) dump_hex(__VA_ARGS__) 65# define dump_raw_out(...) dump_hex(__VA_ARGS__)
67#else 66#else
@@ -340,51 +339,48 @@ static void hash_handshake(tls_state_t *tls, const char *fmt, const void *buffer
340// if we often need HMAC hmac with the same key. 339// if we often need HMAC hmac with the same key.
341// 340//
342// text is often given in disjoint pieces. 341// text is often given in disjoint pieces.
343static unsigned hmac_sha_precomputed_v(uint8_t *out, 342typedef struct hmac_precomputed {
344 md5sha_ctx_t *hashed_key_xor_ipad, 343 md5sha_ctx_t hashed_key_xor_ipad;
345 md5sha_ctx_t *hashed_key_xor_opad, 344 md5sha_ctx_t hashed_key_xor_opad;
345} hmac_precomputed_t;
346
347static unsigned hmac_sha_precomputed_v(
348 hmac_precomputed_t *pre,
349 uint8_t *out,
346 va_list va) 350 va_list va)
347{ 351{
348 uint8_t *text; 352 uint8_t *text;
349 unsigned len; 353 unsigned len;
350 354
351 /* hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */ 355 /* pre->hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
352 /* hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */ 356 /* pre->hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
353 357
354 /* calculate out = H((key XOR ipad) + text) */ 358 /* calculate out = H((key XOR ipad) + text) */
355 while ((text = va_arg(va, uint8_t*)) != NULL) { 359 while ((text = va_arg(va, uint8_t*)) != NULL) {
356 unsigned text_size = va_arg(va, unsigned); 360 unsigned text_size = va_arg(va, unsigned);
357 md5sha_hash(hashed_key_xor_ipad, text, text_size); 361 md5sha_hash(&pre->hashed_key_xor_ipad, text, text_size);
358 } 362 }
359 len = sha_end(hashed_key_xor_ipad, out); 363 len = sha_end(&pre->hashed_key_xor_ipad, out);
360 364
361 /* out = H((key XOR opad) + out) */ 365 /* out = H((key XOR opad) + out) */
362 md5sha_hash(hashed_key_xor_opad, out, len); 366 md5sha_hash(&pre->hashed_key_xor_opad, out, len);
363 return sha_end(hashed_key_xor_opad, out); 367 return sha_end(&pre->hashed_key_xor_opad, out);
364} 368}
365 369
366static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_size, ...) 370static void hmac_sha256_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size)
367{ 371{
368 md5sha_ctx_t hashed_key_xor_ipad;
369 md5sha_ctx_t hashed_key_xor_opad;
370 uint8_t key_xor_ipad[SHA_INSIZE]; 372 uint8_t key_xor_ipad[SHA_INSIZE];
371 uint8_t key_xor_opad[SHA_INSIZE]; 373 uint8_t key_xor_opad[SHA_INSIZE];
372 uint8_t tempkey[SHA256_OUTSIZE]; 374 uint8_t tempkey[SHA256_OUTSIZE];
373 va_list va;
374 unsigned i; 375 unsigned i;
375 376
376 va_start(va, key_size);
377
378 // "The authentication key can be of any length up to INSIZE, the 377 // "The authentication key can be of any length up to INSIZE, the
379 // block length of the hash function. Applications that use keys longer 378 // block length of the hash function. Applications that use keys longer
380 // than INSIZE bytes will first hash the key using H and then use the 379 // than INSIZE bytes will first hash the key using H and then use the
381 // resultant OUTSIZE byte string as the actual key to HMAC." 380 // resultant OUTSIZE byte string as the actual key to HMAC."
382 if (key_size > SHA_INSIZE) { 381 if (key_size > SHA_INSIZE) {
383 md5sha_ctx_t ctx; 382 md5sha_ctx_t ctx;
384 if (tls->MAC_size == SHA256_OUTSIZE) 383 sha256_begin(&ctx);
385 sha256_begin(&ctx);
386 else
387 sha1_begin(&ctx);
388 md5sha_hash(&ctx, key, key_size); 384 md5sha_hash(&ctx, key, key_size);
389 key_size = sha_end(&ctx, tempkey); 385 key_size = sha_end(&ctx, tempkey);
390 } 386 }
@@ -398,19 +394,77 @@ static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_
398 key_xor_opad[i] = 0x5c; 394 key_xor_opad[i] = 0x5c;
399 } 395 }
400 396
401 if (tls->MAC_size == SHA256_OUTSIZE) { 397 sha256_begin(&pre->hashed_key_xor_ipad);
402 sha256_begin(&hashed_key_xor_ipad); 398 sha256_begin(&pre->hashed_key_xor_opad);
403 sha256_begin(&hashed_key_xor_opad); 399 md5sha_hash(&pre->hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
404 } else { 400 md5sha_hash(&pre->hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
405 sha1_begin(&hashed_key_xor_ipad); 401}
406 sha1_begin(&hashed_key_xor_opad); 402// TODO: ^^^ vvv merge?
403static void hmac_sha1_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size)
404{
405 uint8_t key_xor_ipad[SHA_INSIZE];
406 uint8_t key_xor_opad[SHA_INSIZE];
407 uint8_t tempkey[SHA1_OUTSIZE];
408 unsigned i;
409
410 // "The authentication key can be of any length up to INSIZE, the
411 // block length of the hash function. Applications that use keys longer
412 // than INSIZE bytes will first hash the key using H and then use the
413 // resultant OUTSIZE byte string as the actual key to HMAC."
414 if (key_size > SHA_INSIZE) {
415 md5sha_ctx_t ctx;
416 sha1_begin(&ctx);
417 md5sha_hash(&ctx, key, key_size);
418 key_size = sha_end(&ctx, tempkey);
419 }
420
421 for (i = 0; i < key_size; i++) {
422 key_xor_ipad[i] = key[i] ^ 0x36;
423 key_xor_opad[i] = key[i] ^ 0x5c;
424 }
425 for (; i < SHA_INSIZE; i++) {
426 key_xor_ipad[i] = 0x36;
427 key_xor_opad[i] = 0x5c;
407 } 428 }
408 md5sha_hash(&hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
409 md5sha_hash(&hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
410 429
411 i = hmac_sha_precomputed_v(out, &hashed_key_xor_ipad, &hashed_key_xor_opad, va); 430 sha1_begin(&pre->hashed_key_xor_ipad);
431 sha1_begin(&pre->hashed_key_xor_opad);
432 md5sha_hash(&pre->hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
433 md5sha_hash(&pre->hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
434}
435
436static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_size, ...)
437{
438 hmac_precomputed_t pre;
439 va_list va;
440 unsigned len;
441
442 va_start(va, key_size);
443
444 if (tls->MAC_size == SHA256_OUTSIZE)
445 hmac_sha256_begin(&pre, key, key_size);
446 else
447 hmac_sha1_begin(&pre, key, key_size);
448
449 len = hmac_sha_precomputed_v(&pre, out, va);
450
412 va_end(va); 451 va_end(va);
413 return i; 452 return len;
453}
454
455static unsigned hmac_sha256(/*tls_state_t *tls,*/ uint8_t *out, uint8_t *key, unsigned key_size, ...)
456{
457 hmac_precomputed_t pre;
458 va_list va;
459 unsigned len;
460
461 va_start(va, key_size);
462
463 hmac_sha256_begin(&pre, key, key_size);
464 len = hmac_sha_precomputed_v(&pre, out, va);
465
466 va_end(va);
467 return len;
414} 468}
415 469
416// RFC 5246: 470// RFC 5246:
@@ -420,6 +474,9 @@ static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_
420// SHA-256 hash function is used for all cipher suites defined in this 474// SHA-256 hash function is used for all cipher suites defined in this
421// document and in TLS documents published prior to this document when 475// document and in TLS documents published prior to this document when
422// TLS 1.2 is negotiated. 476// TLS 1.2 is negotiated.
477// ^^^^^^^^^^^^^ IMPORTANT!
478// PRF uses sha256 regardless of cipher (at least for all ciphers
479// defined by RFC5246). It's not sha1 for AES_128_CBC_SHA!
423//... 480//...
424// P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + 481// P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
425// HMAC_hash(secret, A(2) + seed) + 482// HMAC_hash(secret, A(2) + seed) +
@@ -441,7 +498,7 @@ static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_
441// PRF(secret, label, seed) = P_<hash>(secret, label + seed) 498// PRF(secret, label, seed) = P_<hash>(secret, label + seed)
442// 499//
443// The label is an ASCII string. 500// The label is an ASCII string.
444static void prf_hmac(tls_state_t *tls, 501static void prf_hmac_sha256(/*tls_state_t *tls,*/
445 uint8_t *outbuf, unsigned outbuf_size, 502 uint8_t *outbuf, unsigned outbuf_size,
446 uint8_t *secret, unsigned secret_size, 503 uint8_t *secret, unsigned secret_size,
447 const char *label, 504 const char *label,
@@ -450,7 +507,7 @@ static void prf_hmac(tls_state_t *tls,
450 uint8_t a[TLS_MAX_MAC_SIZE]; 507 uint8_t a[TLS_MAX_MAC_SIZE];
451 uint8_t *out_p = outbuf; 508 uint8_t *out_p = outbuf;
452 unsigned label_size = strlen(label); 509 unsigned label_size = strlen(label);
453 unsigned MAC_size = tls->MAC_size; 510 unsigned MAC_size = SHA256_OUTSIZE;///tls->MAC_size;
454 511
455 /* In P_hash() calculation, "seed" is "label + seed": */ 512 /* In P_hash() calculation, "seed" is "label + seed": */
456#define SEED label, label_size, seed, seed_size 513#define SEED label, label_size, seed, seed_size
@@ -458,7 +515,7 @@ static void prf_hmac(tls_state_t *tls,
458#define A a, MAC_size 515#define A a, MAC_size
459 516
460 /* A(1) = HMAC_hash(secret, seed) */ 517 /* A(1) = HMAC_hash(secret, seed) */
461 hmac(tls, a, SECRET, SEED, NULL); 518 hmac_sha256(/*tls,*/ a, SECRET, SEED, NULL);
462//TODO: convert hmac to precomputed 519//TODO: convert hmac to precomputed
463 520
464 for(;;) { 521 for(;;) {
@@ -466,16 +523,16 @@ static void prf_hmac(tls_state_t *tls,
466 if (outbuf_size <= MAC_size) { 523 if (outbuf_size <= MAC_size) {
467 /* Last, possibly incomplete, block */ 524 /* Last, possibly incomplete, block */
468 /* (use a[] as temp buffer) */ 525 /* (use a[] as temp buffer) */
469 hmac(tls, a, SECRET, A, SEED, NULL); 526 hmac_sha256(/*tls,*/ a, SECRET, A, SEED, NULL);
470 memcpy(out_p, a, outbuf_size); 527 memcpy(out_p, a, outbuf_size);
471 return; 528 return;
472 } 529 }
473 /* Not last block. Store directly to result buffer */ 530 /* Not last block. Store directly to result buffer */
474 hmac(tls, out_p, SECRET, A, SEED, NULL); 531 hmac_sha256(/*tls,*/ out_p, SECRET, A, SEED, NULL);
475 out_p += MAC_size; 532 out_p += MAC_size;
476 outbuf_size -= MAC_size; 533 outbuf_size -= MAC_size;
477 /* A(2) = HMAC_hash(secret, A(1)) */ 534 /* A(2) = HMAC_hash(secret, A(1)) */
478 hmac(tls, a, SECRET, A, NULL); 535 hmac_sha256(/*tls,*/ a, SECRET, A, NULL);
479 } 536 }
480#undef A 537#undef A
481#undef SECRET 538#undef SECRET
@@ -823,9 +880,10 @@ static int tls_xread_record(tls_state_t *tls)
823 int padding_len; 880 int padding_len;
824 881
825 if (sz & (AES_BLOCKSIZE-1) 882 if (sz & (AES_BLOCKSIZE-1)
826 || sz < tls->min_encrypted_len_on_read 883 || sz < (int)tls->min_encrypted_len_on_read
827 ) { 884 ) {
828 bb_error_msg_and_die("bad encrypted len:%u", sz); 885 bb_error_msg_and_die("bad encrypted len:%u < %u",
886 sz, tls->min_encrypted_len_on_read);
829 } 887 }
830 /* Decrypt content+MAC+padding, moving it over IV in the process */ 888 /* Decrypt content+MAC+padding, moving it over IV in the process */
831 psAesInit(&ctx, p, /* IV */ 889 psAesInit(&ctx, p, /* IV */
@@ -1301,13 +1359,17 @@ static void get_server_hello(tls_state_t *tls)
1301 if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA) { 1359 if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA) {
1302 tls->key_size = AES128_KEYSIZE; 1360 tls->key_size = AES128_KEYSIZE;
1303 tls->MAC_size = SHA1_OUTSIZE; 1361 tls->MAC_size = SHA1_OUTSIZE;
1304 sha1_begin(&tls->hsd->handshake_hash_ctx);
1305 } 1362 }
1306 else { /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 1363 else { /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
1307 tls->key_size = AES256_KEYSIZE; 1364 tls->key_size = AES256_KEYSIZE;
1308 tls->MAC_size = SHA256_OUTSIZE; 1365 tls->MAC_size = SHA256_OUTSIZE;
1309 sha256_begin(&tls->hsd->handshake_hash_ctx);
1310 } 1366 }
1367 /* Handshake hash eventually destined to FINISHED record
1368 * is sha256 regardless of cipher
1369 * (at least for all ciphers defined by RFC5246).
1370 * It's not sha1 for AES_128_CBC_SHA - only MAC is sha1, not this hash.
1371 */
1372 sha256_begin(&tls->hsd->handshake_hash_ctx);
1311 hash_handshake(tls, ">> client hello hash:%s", 1373 hash_handshake(tls, ">> client hello hash:%s",
1312 tls->hsd->saved_client_hello, tls->hsd->saved_client_hello_size 1374 tls->hsd->saved_client_hello, tls->hsd->saved_client_hello_size
1313 ); 1375 );
@@ -1389,6 +1451,7 @@ static void send_client_key_exchange(tls_state_t *tls)
1389 // version negotiated for the connection." 1451 // version negotiated for the connection."
1390 rsa_premaster[0] = TLS_MAJ; 1452 rsa_premaster[0] = TLS_MAJ;
1391 rsa_premaster[1] = TLS_MIN; 1453 rsa_premaster[1] = TLS_MIN;
1454 dump_hex("premaster:%s\n", rsa_premaster, sizeof(rsa_premaster));
1392 len = psRsaEncryptPub(/*pool:*/ NULL, 1455 len = psRsaEncryptPub(/*pool:*/ NULL,
1393 /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key, 1456 /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key,
1394 rsa_premaster, /*inlen:*/ sizeof(rsa_premaster), 1457 rsa_premaster, /*inlen:*/ sizeof(rsa_premaster),
@@ -1417,7 +1480,7 @@ static void send_client_key_exchange(tls_state_t *tls)
1417 // [0..47]; 1480 // [0..47];
1418 // The master secret is always exactly 48 bytes in length. The length 1481 // The master secret is always exactly 48 bytes in length. The length
1419 // of the premaster secret will vary depending on key exchange method. 1482 // of the premaster secret will vary depending on key exchange method.
1420 prf_hmac(tls, 1483 prf_hmac_sha256(/*tls,*/
1421 tls->hsd->master_secret, sizeof(tls->hsd->master_secret), 1484 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
1422 rsa_premaster, sizeof(rsa_premaster), 1485 rsa_premaster, sizeof(rsa_premaster),
1423 "master secret", 1486 "master secret",
@@ -1465,7 +1528,7 @@ static void send_client_key_exchange(tls_state_t *tls)
1465 memcpy(&tmp64[0] , &tls->hsd->client_and_server_rand32[32], 32); 1528 memcpy(&tmp64[0] , &tls->hsd->client_and_server_rand32[32], 32);
1466 memcpy(&tmp64[32], &tls->hsd->client_and_server_rand32[0] , 32); 1529 memcpy(&tmp64[32], &tls->hsd->client_and_server_rand32[0] , 32);
1467 1530
1468 prf_hmac(tls, 1531 prf_hmac_sha256(/*tls,*/
1469 tls->client_write_MAC_key, 2 * (tls->MAC_size + tls->key_size), 1532 tls->client_write_MAC_key, 2 * (tls->MAC_size + tls->key_size),
1470 // also fills: 1533 // also fills:
1471 // server_write_MAC_key[] 1534 // server_write_MAC_key[]
@@ -1548,7 +1611,7 @@ static void send_client_finished(tls_state_t *tls)
1548 fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record)); 1611 fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record));
1549 1612
1550 len = get_handshake_hash(tls, handshake_hash); 1613 len = get_handshake_hash(tls, handshake_hash);
1551 prf_hmac(tls, 1614 prf_hmac_sha256(/*tls,*/
1552 record->prf_result, sizeof(record->prf_result), 1615 record->prf_result, sizeof(record->prf_result),
1553 tls->hsd->master_secret, sizeof(tls->hsd->master_secret), 1616 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
1554 "client finished", 1617 "client finished",
@@ -1645,9 +1708,14 @@ void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni)
1645 dbg("<< CHANGE_CIPHER_SPEC\n"); 1708 dbg("<< CHANGE_CIPHER_SPEC\n");
1646 if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256) 1709 if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256)
1647 tls->min_encrypted_len_on_read = tls->MAC_size; 1710 tls->min_encrypted_len_on_read = tls->MAC_size;
1648 else 1711 else {
1649 /* all incoming packets now should be encrypted and have IV + MAC + padding */ 1712 unsigned mac_blocks = (unsigned)(tls->MAC_size + AES_BLOCKSIZE-1) / AES_BLOCKSIZE;
1650 tls->min_encrypted_len_on_read = AES_BLOCKSIZE + tls->MAC_size + AES_BLOCKSIZE; 1713 /* all incoming packets now should be encrypted and have
1714 * at least IV + (MAC padded to blocksize):
1715 */
1716 tls->min_encrypted_len_on_read = AES_BLOCKSIZE + (mac_blocks * AES_BLOCKSIZE);
1717 dbg("min_encrypted_len_on_read: %u", tls->min_encrypted_len_on_read);
1718 }
1651 1719
1652 /* Get (encrypted) FINISHED from the server */ 1720 /* Get (encrypted) FINISHED from the server */
1653 len = tls_xread_record(tls); 1721 len = tls_xread_record(tls);