2017-01-14 17:19:32 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Denys Vlasenko
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2017-01-14 17:19:32 +05:30
|
|
|
*/
|
|
|
|
//config:config TLS
|
2017-01-23 05:38:16 +05:30
|
|
|
//config: bool #No description makes it a hidden option
|
2017-01-14 17:19:32 +05:30
|
|
|
//config: default n
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls.o
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_pstm.o
|
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_pstm_montgomery_reduce.o
|
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_pstm_mul_comba.o
|
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_pstm_sqr_comba.o
|
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_rsa.o
|
2017-01-18 21:50:27 +05:30
|
|
|
//kbuild:lib-$(CONFIG_TLS) += tls_aes.o
|
2017-01-14 17:19:32 +05:30
|
|
|
////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
#include "tls.h"
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
//Tested against kernel.org:
|
|
|
|
//TLS 1.2
|
|
|
|
#define TLS_MAJ 3
|
|
|
|
#define TLS_MIN 3
|
|
|
|
//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE *** matrixssl uses this on my box
|
|
|
|
//#define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
|
|
|
|
//#define CIPHER_ID TLS_DH_anon_WITH_AES_256_CBC_SHA // SSL_ALERT_HANDSHAKE_FAILURE
|
|
|
|
//^^^^^^^^^^^^^^^^^^^^^^^ (tested b/c this one doesn't req server certs... no luck, server refuses it)
|
|
|
|
//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 // SSL_ALERT_HANDSHAKE_FAILURE
|
|
|
|
//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
|
|
|
|
//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 // ok, recvs SERVER_KEY_EXCHANGE
|
|
|
|
//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
|
|
|
|
//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
|
|
|
|
//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
|
|
|
|
//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
|
|
|
|
//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
|
|
|
|
//#define CIPHER_ID TLS_RSA_WITH_AES_256_GCM_SHA384 // ok, no SERVER_KEY_EXCHANGE
|
|
|
|
//#define CIPHER_ID TLS_RSA_WITH_AES_128_GCM_SHA256 // ok, no SERVER_KEY_EXCHANGE *** select this?
|
|
|
|
|
|
|
|
// works against "openssl s_server -cipher NULL"
|
|
|
|
// and against wolfssl-3.9.10-stable/examples/server/server.c:
|
|
|
|
//#define CIPHER_ID TLS_RSA_WITH_NULL_SHA256 // for testing (does everything except encrypting)
|
|
|
|
|
|
|
|
// works against wolfssl-3.9.10-stable/examples/server/server.c
|
|
|
|
// works for kernel.org
|
|
|
|
// does not work for cdn.kernel.org (e.g. downloading an actual tarball, not a web page)
|
|
|
|
// getting alert 40 "handshake failure" at once
|
|
|
|
// with GNU Wget 1.18, they agree on TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xC02F) cipher
|
|
|
|
// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES256-SHA256
|
|
|
|
// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES256-GCM-SHA384
|
|
|
|
// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-SHA256
|
|
|
|
// ok: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-GCM-SHA256
|
|
|
|
// ok: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher AES128-SHA
|
|
|
|
// (TLS_RSA_WITH_AES_128_CBC_SHA - in TLS 1.2 it's mandated to be always supported)
|
|
|
|
#define CIPHER_ID1 TLS_RSA_WITH_AES_256_CBC_SHA256 // no SERVER_KEY_EXCHANGE from peer
|
2017-01-24 22:38:07 +05:30
|
|
|
// Works with "wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.5.tar.xz"
|
|
|
|
#define CIPHER_ID2 TLS_RSA_WITH_AES_128_CBC_SHA
|
2017-01-24 20:30:54 +05:30
|
|
|
|
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
#define TLS_DEBUG 0
|
|
|
|
#define TLS_DEBUG_HASH 0
|
|
|
|
#define TLS_DEBUG_DER 0
|
|
|
|
#define TLS_DEBUG_FIXED_SECRETS 0
|
2017-01-23 20:42:17 +05:30
|
|
|
#if 0
|
|
|
|
# define dump_raw_out(...) dump_hex(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dump_raw_out(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
# define dump_raw_in(...) dump_hex(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dump_raw_in(...) ((void)0)
|
|
|
|
#endif
|
2017-01-17 21:23:36 +05:30
|
|
|
|
|
|
|
#if TLS_DEBUG
|
2017-01-14 17:19:32 +05:30
|
|
|
# define dbg(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dbg(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2017-01-18 11:15:50 +05:30
|
|
|
#if TLS_DEBUG_DER
|
|
|
|
# define dbg_der(...) fprintf(stderr, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define dbg_der(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
#define RECORD_TYPE_CHANGE_CIPHER_SPEC 20
|
|
|
|
#define RECORD_TYPE_ALERT 21
|
|
|
|
#define RECORD_TYPE_HANDSHAKE 22
|
|
|
|
#define RECORD_TYPE_APPLICATION_DATA 23
|
|
|
|
|
|
|
|
#define HANDSHAKE_HELLO_REQUEST 0
|
|
|
|
#define HANDSHAKE_CLIENT_HELLO 1
|
|
|
|
#define HANDSHAKE_SERVER_HELLO 2
|
|
|
|
#define HANDSHAKE_HELLO_VERIFY_REQUEST 3
|
|
|
|
#define HANDSHAKE_NEW_SESSION_TICKET 4
|
|
|
|
#define HANDSHAKE_CERTIFICATE 11
|
|
|
|
#define HANDSHAKE_SERVER_KEY_EXCHANGE 12
|
|
|
|
#define HANDSHAKE_CERTIFICATE_REQUEST 13
|
|
|
|
#define HANDSHAKE_SERVER_HELLO_DONE 14
|
|
|
|
#define HANDSHAKE_CERTIFICATE_VERIFY 15
|
|
|
|
#define HANDSHAKE_CLIENT_KEY_EXCHANGE 16
|
|
|
|
#define HANDSHAKE_FINISHED 20
|
|
|
|
|
2017-01-14 17:19:32 +05:30
|
|
|
#define SSL_NULL_WITH_NULL_NULL 0x0000
|
|
|
|
#define SSL_RSA_WITH_NULL_MD5 0x0001
|
|
|
|
#define SSL_RSA_WITH_NULL_SHA 0x0002
|
|
|
|
#define SSL_RSA_WITH_RC4_128_MD5 0x0004
|
|
|
|
#define SSL_RSA_WITH_RC4_128_SHA 0x0005
|
|
|
|
#define SSL_RSA_WITH_3DES_EDE_CBC_SHA 0x000A /* 10 */
|
|
|
|
#define TLS_RSA_WITH_AES_128_CBC_SHA 0x002F /* 47 */
|
|
|
|
#define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035 /* 53 */
|
2017-01-17 03:56:33 +05:30
|
|
|
#define TLS_RSA_WITH_NULL_SHA256 0x003B /* 59 */
|
|
|
|
|
2017-01-14 17:19:32 +05:30
|
|
|
#define TLS_EMPTY_RENEGOTIATION_INFO_SCSV 0x00FF
|
|
|
|
|
|
|
|
#define TLS_RSA_WITH_IDEA_CBC_SHA 0x0007 /* 7 */
|
|
|
|
#define SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016 /* 22 */
|
|
|
|
#define SSL_DH_anon_WITH_RC4_128_MD5 0x0018 /* 24 */
|
|
|
|
#define SSL_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B /* 27 */
|
|
|
|
#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033 /* 51 */
|
|
|
|
#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039 /* 57 */
|
|
|
|
#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067 /* 103 */
|
|
|
|
#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B /* 107 */
|
|
|
|
#define TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034 /* 52 */
|
|
|
|
#define TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A /* 58 */
|
|
|
|
#define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C /* 60 */
|
|
|
|
#define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D /* 61 */
|
|
|
|
#define TLS_RSA_WITH_SEED_CBC_SHA 0x0096 /* 150 */
|
|
|
|
#define TLS_PSK_WITH_AES_128_CBC_SHA 0x008C /* 140 */
|
|
|
|
#define TLS_PSK_WITH_AES_128_CBC_SHA256 0x00AE /* 174 */
|
|
|
|
#define TLS_PSK_WITH_AES_256_CBC_SHA384 0x00AF /* 175 */
|
|
|
|
#define TLS_PSK_WITH_AES_256_CBC_SHA 0x008D /* 141 */
|
|
|
|
#define TLS_DHE_PSK_WITH_AES_128_CBC_SHA 0x0090 /* 144 */
|
|
|
|
#define TLS_DHE_PSK_WITH_AES_256_CBC_SHA 0x0091 /* 145 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004 /* 49156 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005 /* 49157 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009 /* 49161 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A /* 49162 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012 /* 49170 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013 /* 49171 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014 /* 49172 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E /* 49166 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F /* 49167 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023 /* 49187 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024 /* 49188 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025 /* 49189 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026 /* 49190 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027 /* 49191 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028 /* 49192 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029 /* 49193 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A /* 49194 */
|
|
|
|
|
2017-01-23 21:07:04 +05:30
|
|
|
/* RFC 5288 "AES Galois Counter Mode (GCM) Cipher Suites for TLS" */
|
2017-01-14 17:19:32 +05:30
|
|
|
#define TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C /* 156 */
|
|
|
|
#define TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D /* 157 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B /* 49195 */
|
|
|
|
#define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C /* 49196 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D /* 49197 */
|
|
|
|
#define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E /* 49198 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F /* 49199 */
|
|
|
|
#define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030 /* 49200 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031 /* 49201 */
|
|
|
|
#define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032 /* 49202 */
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
/* Might go to libbb.h */
|
|
|
|
#define TLS_MAX_CRYPTBLOCK_SIZE 16
|
|
|
|
#define TLS_MAX_OUTBUF (1 << 14)
|
2017-01-19 01:07:24 +05:30
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
enum {
|
|
|
|
SHA_INSIZE = 64,
|
|
|
|
SHA1_OUTSIZE = 20,
|
|
|
|
SHA256_OUTSIZE = 32,
|
2017-01-23 20:42:17 +05:30
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
AES_BLOCKSIZE = 16,
|
|
|
|
AES128_KEYSIZE = 16,
|
|
|
|
AES256_KEYSIZE = 32,
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
RSA_PREMASTER_SIZE = 48,
|
|
|
|
|
2017-01-20 18:42:10 +05:30
|
|
|
RECHDR_LEN = 5,
|
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
/* 8 = 3+5. 3 extra bytes result in record data being 32-bit aligned */
|
2017-01-20 07:45:09 +05:30
|
|
|
OUTBUF_PFX = 8 + AES_BLOCKSIZE, /* header + IV */
|
2017-01-24 20:30:54 +05:30
|
|
|
OUTBUF_SFX = TLS_MAX_MAC_SIZE + TLS_MAX_CRYPTBLOCK_SIZE, /* MAC + padding */
|
2017-01-21 00:57:06 +05:30
|
|
|
|
|
|
|
// RFC 5246
|
|
|
|
// | 6.2.1. Fragmentation
|
|
|
|
// | The record layer fragments information blocks into TLSPlaintext
|
|
|
|
// | records carrying data in chunks of 2^14 bytes or less. Client
|
|
|
|
// | message boundaries are not preserved in the record layer (i.e.,
|
|
|
|
// | multiple client messages of the same ContentType MAY be coalesced
|
|
|
|
// | into a single TLSPlaintext record, or a single message MAY be
|
|
|
|
// | fragmented across several records)
|
|
|
|
// |...
|
|
|
|
// | length
|
|
|
|
// | The length (in bytes) of the following TLSPlaintext.fragment.
|
|
|
|
// | The length MUST NOT exceed 2^14.
|
|
|
|
// |...
|
|
|
|
// | 6.2.2. Record Compression and Decompression
|
|
|
|
// |...
|
|
|
|
// | Compression must be lossless and may not increase the content length
|
|
|
|
// | by more than 1024 bytes. If the decompression function encounters a
|
|
|
|
// | TLSCompressed.fragment that would decompress to a length in excess of
|
|
|
|
// | 2^14 bytes, it MUST report a fatal decompression failure error.
|
|
|
|
// |...
|
|
|
|
// | length
|
|
|
|
// | The length (in bytes) of the following TLSCompressed.fragment.
|
|
|
|
// | The length MUST NOT exceed 2^14 + 1024.
|
|
|
|
// |...
|
|
|
|
// | 6.2.3. Record Payload Protection
|
|
|
|
// | The encryption and MAC functions translate a TLSCompressed
|
|
|
|
// | structure into a TLSCiphertext. The decryption functions reverse
|
|
|
|
// | the process. The MAC of the record also includes a sequence
|
|
|
|
// | number so that missing, extra, or repeated messages are
|
|
|
|
// | detectable.
|
|
|
|
// |...
|
|
|
|
// | length
|
|
|
|
// | The length (in bytes) of the following TLSCiphertext.fragment.
|
|
|
|
// | The length MUST NOT exceed 2^14 + 2048.
|
2017-01-24 20:30:54 +05:30
|
|
|
MAX_INBUF = RECHDR_LEN + (1 << 14) + 2048,
|
2017-01-17 03:56:33 +05:30
|
|
|
};
|
|
|
|
|
2017-01-14 18:27:16 +05:30
|
|
|
struct record_hdr {
|
2017-01-14 17:19:32 +05:30
|
|
|
uint8_t type;
|
|
|
|
uint8_t proto_maj, proto_min;
|
|
|
|
uint8_t len16_hi, len16_lo;
|
|
|
|
};
|
|
|
|
|
2017-01-23 05:38:16 +05:30
|
|
|
struct tls_handshake_data {
|
2017-01-24 20:30:54 +05:30
|
|
|
/* In bbox, md5/sha1/sha256 ctx's are the same structure */
|
|
|
|
md5sha_ctx_t handshake_hash_ctx;
|
|
|
|
|
2017-01-23 21:07:04 +05:30
|
|
|
uint8_t client_and_server_rand32[2 * 32];
|
|
|
|
uint8_t master_secret[48];
|
2017-01-21 03:18:41 +05:30
|
|
|
//TODO: store just the DER key here, parse/use/delete it when sending client key
|
|
|
|
//this way it will stay key type agnostic here.
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
psRsaKey_t server_rsa_pub_key;
|
2017-01-24 20:30:54 +05:30
|
|
|
|
|
|
|
unsigned saved_client_hello_size;
|
|
|
|
uint8_t saved_client_hello[1];
|
2017-01-23 05:38:16 +05:30
|
|
|
};
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-17 21:23:36 +05:30
|
|
|
|
|
|
|
static unsigned get24be(const uint8_t *p)
|
|
|
|
{
|
|
|
|
return 0x100*(0x100*p[0] + p[1]) + p[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TLS_DEBUG
|
|
|
|
static void dump_hex(const char *fmt, const void *vp, int len)
|
|
|
|
{
|
|
|
|
char hexbuf[32 * 1024 + 4];
|
|
|
|
const uint8_t *p = vp;
|
|
|
|
|
|
|
|
bin2hex(hexbuf, (void*)p, len)[0] = '\0';
|
|
|
|
dbg(fmt, hexbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_tls_record(const void *vp, int len)
|
|
|
|
{
|
|
|
|
const uint8_t *p = vp;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
unsigned xhdr_len;
|
2017-01-20 18:42:10 +05:30
|
|
|
if (len < RECHDR_LEN) {
|
2017-01-17 21:23:36 +05:30
|
|
|
dump_hex("< |%s|\n", p, len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
xhdr_len = 0x100*p[3] + p[4];
|
|
|
|
dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len);
|
2017-01-20 18:42:10 +05:30
|
|
|
p += RECHDR_LEN;
|
|
|
|
len -= RECHDR_LEN;
|
|
|
|
if (len >= 4 && p[-RECHDR_LEN] == RECORD_TYPE_HANDSHAKE) {
|
2017-01-17 21:23:36 +05:30
|
|
|
unsigned len24 = get24be(p + 1);
|
|
|
|
dbg(" type:%u len24:%u", p[0], len24);
|
|
|
|
}
|
|
|
|
if (xhdr_len > len)
|
|
|
|
xhdr_len = len;
|
|
|
|
dump_hex(" |%s|\n", p, xhdr_len);
|
|
|
|
p += xhdr_len;
|
|
|
|
len -= xhdr_len;
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 23:41:14 +05:30
|
|
|
#else
|
|
|
|
# define dump_hex(...) ((void)0)
|
|
|
|
# define dump_tls_record(...) ((void)0)
|
2017-01-17 21:23:36 +05:30
|
|
|
#endif
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
void tls_get_random(void *buf, unsigned len)
|
|
|
|
{
|
|
|
|
if (len != open_read_close("/dev/urandom", buf, len))
|
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
/* Nondestructively see the current hash value */
|
|
|
|
static unsigned sha_peek(md5sha_ctx_t *ctx, void *buffer)
|
2017-01-17 21:23:36 +05:30
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
md5sha_ctx_t ctx_copy = *ctx; /* struct copy */
|
|
|
|
return sha_end(&ctx_copy, buffer);
|
2017-01-17 21:23:36 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
static ALWAYS_INLINE unsigned get_handshake_hash(tls_state_t *tls, void *buffer)
|
2017-01-17 21:23:36 +05:30
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
return sha_peek(&tls->hsd->handshake_hash_ctx, buffer);
|
2017-01-17 21:23:36 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
#if !TLS_DEBUG_HASH
|
|
|
|
# define hash_handshake(tls, fmt, buffer, len) \
|
|
|
|
hash_handshake(tls, buffer, len)
|
|
|
|
#endif
|
|
|
|
static void hash_handshake(tls_state_t *tls, const char *fmt, const void *buffer, unsigned len)
|
2017-01-17 21:23:36 +05:30
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
md5sha_hash(&tls->hsd->handshake_hash_ctx, buffer, len);
|
|
|
|
#if TLS_DEBUG_HASH
|
|
|
|
{
|
|
|
|
uint8_t h[TLS_MAX_MAC_SIZE];
|
|
|
|
dump_hex(fmt, buffer, len);
|
|
|
|
dbg(" (%u bytes) ", (int)len);
|
|
|
|
len = sha_peek(&tls->hsd->handshake_hash_ctx, h);
|
|
|
|
if (len == SHA1_OUTSIZE)
|
|
|
|
dump_hex("sha1:%s\n", h, len);
|
|
|
|
else
|
|
|
|
if (len == SHA256_OUTSIZE)
|
|
|
|
dump_hex("sha256:%s\n", h, len);
|
|
|
|
else
|
|
|
|
dump_hex("sha???:%s\n", h, len);
|
|
|
|
}
|
2017-01-18 11:15:50 +05:30
|
|
|
#endif
|
2017-01-24 20:30:54 +05:30
|
|
|
}
|
2017-01-17 21:23:36 +05:30
|
|
|
|
2017-01-17 21:34:24 +05:30
|
|
|
// RFC 2104
|
|
|
|
// HMAC(key, text) based on a hash H (say, sha256) is:
|
|
|
|
// ipad = [0x36 x INSIZE]
|
|
|
|
// opad = [0x5c x INSIZE]
|
|
|
|
// HMAC(key, text) = H((key XOR opad) + H((key XOR ipad) + text))
|
|
|
|
//
|
|
|
|
// H(key XOR opad) and H(key XOR ipad) can be precomputed
|
|
|
|
// if we often need HMAC hmac with the same key.
|
|
|
|
//
|
|
|
|
// text is often given in disjoint pieces.
|
2017-01-24 22:38:07 +05:30
|
|
|
typedef struct hmac_precomputed {
|
|
|
|
md5sha_ctx_t hashed_key_xor_ipad;
|
|
|
|
md5sha_ctx_t hashed_key_xor_opad;
|
|
|
|
} hmac_precomputed_t;
|
|
|
|
|
|
|
|
static unsigned hmac_sha_precomputed_v(
|
|
|
|
hmac_precomputed_t *pre,
|
|
|
|
uint8_t *out,
|
2017-01-17 21:34:24 +05:30
|
|
|
va_list va)
|
|
|
|
{
|
|
|
|
uint8_t *text;
|
2017-01-24 20:30:54 +05:30
|
|
|
unsigned len;
|
2017-01-17 21:34:24 +05:30
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
/* pre->hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
|
|
|
|
/* pre->hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
/* calculate out = H((key XOR ipad) + text) */
|
|
|
|
while ((text = va_arg(va, uint8_t*)) != NULL) {
|
|
|
|
unsigned text_size = va_arg(va, unsigned);
|
2017-01-24 22:38:07 +05:30
|
|
|
md5sha_hash(&pre->hashed_key_xor_ipad, text, text_size);
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
2017-01-24 22:38:07 +05:30
|
|
|
len = sha_end(&pre->hashed_key_xor_ipad, out);
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
/* out = H((key XOR opad) + out) */
|
2017-01-24 22:38:07 +05:30
|
|
|
md5sha_hash(&pre->hashed_key_xor_opad, out, len);
|
|
|
|
return sha_end(&pre->hashed_key_xor_opad, out);
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
static void hmac_sha256_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size)
|
2017-01-17 21:34:24 +05:30
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
uint8_t key_xor_ipad[SHA_INSIZE];
|
|
|
|
uint8_t key_xor_opad[SHA_INSIZE];
|
2017-01-17 21:34:24 +05:30
|
|
|
uint8_t tempkey[SHA256_OUTSIZE];
|
2017-01-24 20:30:54 +05:30
|
|
|
unsigned i;
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
// "The authentication key can be of any length up to INSIZE, the
|
|
|
|
// block length of the hash function. Applications that use keys longer
|
|
|
|
// than INSIZE bytes will first hash the key using H and then use the
|
|
|
|
// resultant OUTSIZE byte string as the actual key to HMAC."
|
2017-01-24 20:30:54 +05:30
|
|
|
if (key_size > SHA_INSIZE) {
|
|
|
|
md5sha_ctx_t ctx;
|
2017-01-24 22:38:07 +05:30
|
|
|
sha256_begin(&ctx);
|
2017-01-24 20:30:54 +05:30
|
|
|
md5sha_hash(&ctx, key, key_size);
|
|
|
|
key_size = sha_end(&ctx, tempkey);
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < key_size; i++) {
|
|
|
|
key_xor_ipad[i] = key[i] ^ 0x36;
|
|
|
|
key_xor_opad[i] = key[i] ^ 0x5c;
|
|
|
|
}
|
2017-01-24 20:30:54 +05:30
|
|
|
for (; i < SHA_INSIZE; i++) {
|
2017-01-17 21:34:24 +05:30
|
|
|
key_xor_ipad[i] = 0x36;
|
|
|
|
key_xor_opad[i] = 0x5c;
|
|
|
|
}
|
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
sha256_begin(&pre->hashed_key_xor_ipad);
|
|
|
|
sha256_begin(&pre->hashed_key_xor_opad);
|
|
|
|
md5sha_hash(&pre->hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
|
|
|
|
md5sha_hash(&pre->hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
|
|
|
|
}
|
|
|
|
// TODO: ^^^ vvv merge?
|
|
|
|
static void hmac_sha1_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size)
|
|
|
|
{
|
|
|
|
uint8_t key_xor_ipad[SHA_INSIZE];
|
|
|
|
uint8_t key_xor_opad[SHA_INSIZE];
|
|
|
|
uint8_t tempkey[SHA1_OUTSIZE];
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
// "The authentication key can be of any length up to INSIZE, the
|
|
|
|
// block length of the hash function. Applications that use keys longer
|
|
|
|
// than INSIZE bytes will first hash the key using H and then use the
|
|
|
|
// resultant OUTSIZE byte string as the actual key to HMAC."
|
|
|
|
if (key_size > SHA_INSIZE) {
|
|
|
|
md5sha_ctx_t ctx;
|
|
|
|
sha1_begin(&ctx);
|
|
|
|
md5sha_hash(&ctx, key, key_size);
|
|
|
|
key_size = sha_end(&ctx, tempkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < key_size; i++) {
|
|
|
|
key_xor_ipad[i] = key[i] ^ 0x36;
|
|
|
|
key_xor_opad[i] = key[i] ^ 0x5c;
|
|
|
|
}
|
|
|
|
for (; i < SHA_INSIZE; i++) {
|
|
|
|
key_xor_ipad[i] = 0x36;
|
|
|
|
key_xor_opad[i] = 0x5c;
|
2017-01-24 20:30:54 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
sha1_begin(&pre->hashed_key_xor_ipad);
|
|
|
|
sha1_begin(&pre->hashed_key_xor_opad);
|
|
|
|
md5sha_hash(&pre->hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
|
|
|
|
md5sha_hash(&pre->hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_size, ...)
|
|
|
|
{
|
|
|
|
hmac_precomputed_t pre;
|
|
|
|
va_list va;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
va_start(va, key_size);
|
|
|
|
|
|
|
|
if (tls->MAC_size == SHA256_OUTSIZE)
|
|
|
|
hmac_sha256_begin(&pre, key, key_size);
|
|
|
|
else
|
|
|
|
hmac_sha1_begin(&pre, key, key_size);
|
|
|
|
|
|
|
|
len = hmac_sha_precomputed_v(&pre, out, va);
|
|
|
|
|
2017-01-17 21:34:24 +05:30
|
|
|
va_end(va);
|
2017-01-24 22:38:07 +05:30
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned hmac_sha256(/*tls_state_t *tls,*/ uint8_t *out, uint8_t *key, unsigned key_size, ...)
|
|
|
|
{
|
|
|
|
hmac_precomputed_t pre;
|
|
|
|
va_list va;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
va_start(va, key_size);
|
|
|
|
|
|
|
|
hmac_sha256_begin(&pre, key, key_size);
|
|
|
|
len = hmac_sha_precomputed_v(&pre, out, va);
|
|
|
|
|
|
|
|
va_end(va);
|
|
|
|
return len;
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 5246:
|
|
|
|
// 5. HMAC and the Pseudorandom Function
|
|
|
|
//...
|
|
|
|
// In this section, we define one PRF, based on HMAC. This PRF with the
|
|
|
|
// SHA-256 hash function is used for all cipher suites defined in this
|
|
|
|
// document and in TLS documents published prior to this document when
|
|
|
|
// TLS 1.2 is negotiated.
|
2017-01-24 22:38:07 +05:30
|
|
|
// ^^^^^^^^^^^^^ IMPORTANT!
|
|
|
|
// PRF uses sha256 regardless of cipher (at least for all ciphers
|
|
|
|
// defined by RFC5246). It's not sha1 for AES_128_CBC_SHA!
|
2017-01-17 21:34:24 +05:30
|
|
|
//...
|
|
|
|
// P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
|
|
|
|
// HMAC_hash(secret, A(2) + seed) +
|
|
|
|
// HMAC_hash(secret, A(3) + seed) + ...
|
|
|
|
// where + indicates concatenation.
|
|
|
|
// A() is defined as:
|
|
|
|
// A(0) = seed
|
|
|
|
// A(1) = HMAC_hash(secret, A(0)) = HMAC_hash(secret, seed)
|
|
|
|
// A(i) = HMAC_hash(secret, A(i-1))
|
|
|
|
// P_hash can be iterated as many times as necessary to produce the
|
|
|
|
// required quantity of data. For example, if P_SHA256 is being used to
|
|
|
|
// create 80 bytes of data, it will have to be iterated three times
|
|
|
|
// (through A(3)), creating 96 bytes of output data; the last 16 bytes
|
|
|
|
// of the final iteration will then be discarded, leaving 80 bytes of
|
|
|
|
// output data.
|
|
|
|
//
|
|
|
|
// TLS's PRF is created by applying P_hash to the secret as:
|
|
|
|
//
|
|
|
|
// PRF(secret, label, seed) = P_<hash>(secret, label + seed)
|
|
|
|
//
|
|
|
|
// The label is an ASCII string.
|
2017-01-24 22:38:07 +05:30
|
|
|
static void prf_hmac_sha256(/*tls_state_t *tls,*/
|
2017-01-17 21:34:24 +05:30
|
|
|
uint8_t *outbuf, unsigned outbuf_size,
|
|
|
|
uint8_t *secret, unsigned secret_size,
|
|
|
|
const char *label,
|
|
|
|
uint8_t *seed, unsigned seed_size)
|
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
uint8_t a[TLS_MAX_MAC_SIZE];
|
2017-01-17 21:34:24 +05:30
|
|
|
uint8_t *out_p = outbuf;
|
|
|
|
unsigned label_size = strlen(label);
|
2017-01-24 22:38:07 +05:30
|
|
|
unsigned MAC_size = SHA256_OUTSIZE;///tls->MAC_size;
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
/* In P_hash() calculation, "seed" is "label + seed": */
|
|
|
|
#define SEED label, label_size, seed, seed_size
|
|
|
|
#define SECRET secret, secret_size
|
2017-01-24 20:30:54 +05:30
|
|
|
#define A a, MAC_size
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
/* A(1) = HMAC_hash(secret, seed) */
|
2017-01-24 22:38:07 +05:30
|
|
|
hmac_sha256(/*tls,*/ a, SECRET, SEED, NULL);
|
2017-01-24 20:30:54 +05:30
|
|
|
//TODO: convert hmac to precomputed
|
2017-01-17 21:34:24 +05:30
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/* HMAC_hash(secret, A(1) + seed) */
|
2017-01-24 20:30:54 +05:30
|
|
|
if (outbuf_size <= MAC_size) {
|
2017-01-17 21:34:24 +05:30
|
|
|
/* Last, possibly incomplete, block */
|
|
|
|
/* (use a[] as temp buffer) */
|
2017-01-24 22:38:07 +05:30
|
|
|
hmac_sha256(/*tls,*/ a, SECRET, A, SEED, NULL);
|
2017-01-17 21:34:24 +05:30
|
|
|
memcpy(out_p, a, outbuf_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Not last block. Store directly to result buffer */
|
2017-01-24 22:38:07 +05:30
|
|
|
hmac_sha256(/*tls,*/ out_p, SECRET, A, SEED, NULL);
|
2017-01-24 20:30:54 +05:30
|
|
|
out_p += MAC_size;
|
|
|
|
outbuf_size -= MAC_size;
|
2017-01-17 21:34:24 +05:30
|
|
|
/* A(2) = HMAC_hash(secret, A(1)) */
|
2017-01-24 22:38:07 +05:30
|
|
|
hmac_sha256(/*tls,*/ a, SECRET, A, NULL);
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
|
|
|
#undef A
|
|
|
|
#undef SECRET
|
|
|
|
#undef SEED
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:42:17 +05:30
|
|
|
static void bad_record_die(tls_state_t *tls, const char *expected, int len)
|
|
|
|
{
|
2017-01-24 21:36:10 +05:30
|
|
|
bb_error_msg("got bad TLS record (len:%d) while expecting %s", len, expected);
|
2017-01-23 20:42:17 +05:30
|
|
|
if (len > 0) {
|
|
|
|
uint8_t *p = tls->inbuf;
|
2017-01-24 21:36:10 +05:30
|
|
|
while (len > 0) {
|
2017-01-23 20:42:17 +05:30
|
|
|
fprintf(stderr, " %02x", *p++);
|
2017-01-24 21:36:10 +05:30
|
|
|
len--;
|
|
|
|
}
|
2017-01-23 20:42:17 +05:30
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:36:10 +05:30
|
|
|
static void tls_error_die(tls_state_t *tls, int line)
|
2017-01-16 08:55:01 +05:30
|
|
|
{
|
2017-01-21 00:57:06 +05:30
|
|
|
dump_tls_record(tls->inbuf, tls->ofs_to_buffered + tls->buffered_size);
|
2017-01-24 21:36:10 +05:30
|
|
|
bb_error_msg_and_die("tls error at line %d cipher:%04x", line, tls->cipher_id);
|
2017-01-20 23:41:14 +05:30
|
|
|
}
|
2017-01-24 21:36:10 +05:30
|
|
|
#define tls_error_die(tls) tls_error_die(tls, __LINE__)
|
2017-01-20 23:41:14 +05:30
|
|
|
|
2017-01-21 00:57:06 +05:30
|
|
|
#if 0 //UNUSED
|
|
|
|
static void tls_free_inbuf(tls_state_t *tls)
|
|
|
|
{
|
|
|
|
if (tls->buffered_size == 0) {
|
|
|
|
free(tls->inbuf);
|
|
|
|
tls->inbuf_size = 0;
|
|
|
|
tls->inbuf = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
static void tls_free_outbuf(tls_state_t *tls)
|
|
|
|
{
|
|
|
|
free(tls->outbuf);
|
|
|
|
tls->outbuf_size = 0;
|
|
|
|
tls->outbuf = NULL;
|
2017-01-17 21:34:24 +05:30
|
|
|
}
|
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
static void *tls_get_outbuf(tls_state_t *tls, int len)
|
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
if (len > TLS_MAX_OUTBUF)
|
2017-01-20 07:45:09 +05:30
|
|
|
xfunc_die();
|
2017-01-24 20:30:54 +05:30
|
|
|
len += OUTBUF_PFX + OUTBUF_SFX;
|
|
|
|
if (tls->outbuf_size < len) {
|
|
|
|
tls->outbuf_size = len;
|
|
|
|
tls->outbuf = xrealloc(tls->outbuf, len);
|
2017-01-20 07:45:09 +05:30
|
|
|
}
|
|
|
|
return tls->outbuf + OUTBUF_PFX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xwrite_encrypted(tls_state_t *tls, unsigned size, unsigned type)
|
2017-01-17 21:34:24 +05:30
|
|
|
{
|
2017-01-20 07:45:09 +05:30
|
|
|
uint8_t *buf = tls->outbuf + OUTBUF_PFX;
|
|
|
|
struct record_hdr *xhdr;
|
2017-01-20 22:29:25 +05:30
|
|
|
uint8_t padding_length;
|
2017-01-17 03:56:33 +05:30
|
|
|
|
2017-01-20 18:42:10 +05:30
|
|
|
xhdr = (void*)(buf - RECHDR_LEN);
|
2017-01-24 20:30:54 +05:30
|
|
|
if (tls->cipher_id != TLS_RSA_WITH_NULL_SHA256)
|
2017-01-20 18:42:10 +05:30
|
|
|
xhdr = (void*)(buf - RECHDR_LEN - AES_BLOCKSIZE); /* place for IV */
|
2017-01-20 07:45:09 +05:30
|
|
|
|
|
|
|
xhdr->type = type;
|
|
|
|
xhdr->proto_maj = TLS_MAJ;
|
|
|
|
xhdr->proto_min = TLS_MIN;
|
2017-01-21 01:49:38 +05:30
|
|
|
/* fake unencrypted record len for MAC calculation */
|
2017-01-20 07:45:09 +05:30
|
|
|
xhdr->len16_hi = size >> 8;
|
|
|
|
xhdr->len16_lo = size & 0xff;
|
2017-01-18 11:15:50 +05:30
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
/* Calculate MAC signature */
|
2017-01-24 20:30:54 +05:30
|
|
|
hmac(tls, buf + size, /* result */
|
|
|
|
tls->client_write_MAC_key, tls->MAC_size,
|
|
|
|
&tls->write_seq64_be, sizeof(tls->write_seq64_be),
|
|
|
|
xhdr, RECHDR_LEN,
|
|
|
|
buf, size,
|
|
|
|
NULL
|
|
|
|
);
|
2017-01-18 11:15:50 +05:30
|
|
|
tls->write_seq64_be = SWAP_BE64(1 + SWAP_BE64(tls->write_seq64_be));
|
2017-01-17 03:56:33 +05:30
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
size += tls->MAC_size;
|
2017-01-20 07:45:09 +05:30
|
|
|
|
2017-01-20 22:29:25 +05:30
|
|
|
// RFC 5246
|
|
|
|
// 6.2.3.1. Null or Standard Stream Cipher
|
|
|
|
//
|
|
|
|
// Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6)
|
|
|
|
// convert TLSCompressed.fragment structures to and from stream
|
|
|
|
// TLSCiphertext.fragment structures.
|
|
|
|
//
|
|
|
|
// stream-ciphered struct {
|
|
|
|
// opaque content[TLSCompressed.length];
|
|
|
|
// opaque MAC[SecurityParameters.mac_length];
|
|
|
|
// } GenericStreamCipher;
|
|
|
|
//
|
|
|
|
// The MAC is generated as:
|
|
|
|
// MAC(MAC_write_key, seq_num +
|
|
|
|
// TLSCompressed.type +
|
|
|
|
// TLSCompressed.version +
|
|
|
|
// TLSCompressed.length +
|
|
|
|
// TLSCompressed.fragment);
|
|
|
|
// where "+" denotes concatenation.
|
|
|
|
// seq_num
|
|
|
|
// The sequence number for this record.
|
|
|
|
// MAC
|
|
|
|
// The MAC algorithm specified by SecurityParameters.mac_algorithm.
|
|
|
|
//
|
|
|
|
// Note that the MAC is computed before encryption. The stream cipher
|
|
|
|
// encrypts the entire block, including the MAC.
|
|
|
|
//...
|
|
|
|
// Appendix C. Cipher Suite Definitions
|
|
|
|
//...
|
|
|
|
// MAC Algorithm mac_length mac_key_length
|
|
|
|
// -------- ----------- ---------- --------------
|
|
|
|
// SHA HMAC-SHA1 20 20
|
|
|
|
// SHA256 HMAC-SHA256 32 32
|
2017-01-24 20:30:54 +05:30
|
|
|
if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256) {
|
2017-01-19 01:07:24 +05:30
|
|
|
/* No encryption, only signing */
|
2017-01-20 07:45:09 +05:30
|
|
|
xhdr->len16_hi = size >> 8;
|
|
|
|
xhdr->len16_lo = size & 0xff;
|
2017-01-20 22:29:25 +05:30
|
|
|
dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
|
2017-01-23 05:38:16 +05:30
|
|
|
xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
|
2017-01-20 07:45:09 +05:30
|
|
|
dbg("wrote %u bytes (NULL crypt, SHA256 hash)\n", size);
|
2017-01-19 01:07:24 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6.2.3.2. CBC Block Cipher
|
|
|
|
// For block ciphers (such as 3DES or AES), the encryption and MAC
|
|
|
|
// functions convert TLSCompressed.fragment structures to and from block
|
|
|
|
// TLSCiphertext.fragment structures.
|
|
|
|
// struct {
|
|
|
|
// opaque IV[SecurityParameters.record_iv_length];
|
|
|
|
// block-ciphered struct {
|
|
|
|
// opaque content[TLSCompressed.length];
|
|
|
|
// opaque MAC[SecurityParameters.mac_length];
|
|
|
|
// uint8 padding[GenericBlockCipher.padding_length];
|
|
|
|
// uint8 padding_length;
|
|
|
|
// };
|
|
|
|
// } GenericBlockCipher;
|
|
|
|
//...
|
|
|
|
// IV
|
|
|
|
// The Initialization Vector (IV) SHOULD be chosen at random, and
|
|
|
|
// MUST be unpredictable. Note that in versions of TLS prior to 1.1,
|
|
|
|
// there was no IV field (...). For block ciphers, the IV length is
|
|
|
|
// of length SecurityParameters.record_iv_length, which is equal to the
|
|
|
|
// SecurityParameters.block_size.
|
|
|
|
// padding
|
|
|
|
// Padding that is added to force the length of the plaintext to be
|
|
|
|
// an integral multiple of the block cipher's block length.
|
|
|
|
// padding_length
|
|
|
|
// The padding length MUST be such that the total size of the
|
|
|
|
// GenericBlockCipher structure is a multiple of the cipher's block
|
|
|
|
// length. Legal values range from zero to 255, inclusive.
|
|
|
|
//...
|
|
|
|
// Appendix C. Cipher Suite Definitions
|
|
|
|
//...
|
|
|
|
// Key IV Block
|
|
|
|
// Cipher Type Material Size Size
|
|
|
|
// ------------ ------ -------- ---- -----
|
|
|
|
// AES_128_CBC Block 16 16 16
|
|
|
|
// AES_256_CBC Block 32 16 16
|
|
|
|
|
2017-01-21 01:49:38 +05:30
|
|
|
/* Fill IV and padding in outbuf */
|
2017-01-20 07:45:09 +05:30
|
|
|
tls_get_random(buf - AES_BLOCKSIZE, AES_BLOCKSIZE); /* IV */
|
2017-01-24 20:30:54 +05:30
|
|
|
dbg("before crypt: 5 hdr + %u data + %u hash bytes\n", size, tls->MAC_size);
|
2017-01-19 01:07:24 +05:30
|
|
|
// RFC is talking nonsense:
|
2017-01-23 21:07:04 +05:30
|
|
|
// "Padding that is added to force the length of the plaintext to be
|
|
|
|
// an integral multiple of the block cipher's block length."
|
2017-01-19 01:07:24 +05:30
|
|
|
// WRONG. _padding+padding_length_, not just _padding_,
|
|
|
|
// pads the data.
|
|
|
|
// IOW: padding_length is the last byte of padding[] array,
|
|
|
|
// contrary to what RFC depicts.
|
|
|
|
//
|
|
|
|
// What actually happens is that there is always padding.
|
|
|
|
// If you need one byte to reach BLOCKSIZE, this byte is 0x00.
|
|
|
|
// If you need two bytes, they are both 0x01.
|
|
|
|
// If you need three, they are 0x02,0x02,0x02. And so on.
|
|
|
|
// If you need no bytes to reach BLOCKSIZE, you have to pad a full
|
|
|
|
// BLOCKSIZE with bytes of value (BLOCKSIZE-1).
|
|
|
|
// It's ok to have more than minimum padding, but we do minimum.
|
|
|
|
padding_length = (~size) & (AES_BLOCKSIZE - 1);
|
|
|
|
do {
|
2017-01-21 01:49:38 +05:30
|
|
|
buf[size++] = padding_length; /* padding */
|
2017-01-19 01:07:24 +05:30
|
|
|
} while ((size & (AES_BLOCKSIZE - 1)) != 0);
|
|
|
|
|
|
|
|
/* Encrypt content+MAC+padding in place */
|
2017-02-04 20:53:49 +05:30
|
|
|
aes_cbc_encrypt(
|
|
|
|
tls->client_write_key, tls->key_size, /* selects 128/256 */
|
|
|
|
buf - AES_BLOCKSIZE, /* IV */
|
|
|
|
buf, size, /* plaintext */
|
|
|
|
buf /* ciphertext */
|
|
|
|
);
|
2017-01-17 21:34:24 +05:30
|
|
|
|
2017-01-19 01:07:24 +05:30
|
|
|
/* Write out */
|
|
|
|
dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n",
|
|
|
|
AES_BLOCKSIZE, size, padding_length);
|
|
|
|
size += AES_BLOCKSIZE; /* + IV */
|
|
|
|
xhdr->len16_hi = size >> 8;
|
|
|
|
xhdr->len16_lo = size & 0xff;
|
2017-01-20 22:29:25 +05:30
|
|
|
dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
|
2017-01-23 05:38:16 +05:30
|
|
|
xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
|
2017-01-20 18:42:10 +05:30
|
|
|
dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
static void xwrite_handshake_record(tls_state_t *tls, unsigned size)
|
2017-01-20 07:45:09 +05:30
|
|
|
{
|
2017-01-24 20:30:54 +05:30
|
|
|
//if (!tls->encrypt_on_write) {
|
2017-01-20 07:45:09 +05:30
|
|
|
uint8_t *buf = tls->outbuf + OUTBUF_PFX;
|
2017-01-20 18:42:10 +05:30
|
|
|
struct record_hdr *xhdr = (void*)(buf - RECHDR_LEN);
|
2017-01-20 07:45:09 +05:30
|
|
|
|
|
|
|
xhdr->type = RECORD_TYPE_HANDSHAKE;
|
|
|
|
xhdr->proto_maj = TLS_MAJ;
|
|
|
|
xhdr->proto_min = TLS_MIN;
|
|
|
|
xhdr->len16_hi = size >> 8;
|
|
|
|
xhdr->len16_lo = size & 0xff;
|
2017-01-20 22:29:25 +05:30
|
|
|
dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
|
2017-01-23 05:38:16 +05:30
|
|
|
xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
|
2017-01-20 18:42:10 +05:30
|
|
|
dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
|
2017-01-24 20:30:54 +05:30
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
//xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xwrite_and_update_handshake_hash(tls_state_t *tls, unsigned size)
|
|
|
|
{
|
|
|
|
if (!tls->encrypt_on_write) {
|
|
|
|
uint8_t *buf;
|
|
|
|
|
|
|
|
xwrite_handshake_record(tls, size);
|
2017-01-20 07:45:09 +05:30
|
|
|
/* Handshake hash does not include record headers */
|
2017-01-24 20:30:54 +05:30
|
|
|
buf = tls->outbuf + OUTBUF_PFX;
|
|
|
|
hash_handshake(tls, ">> hash:%s", buf, size);
|
2017-01-20 07:45:09 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
|
|
|
|
}
|
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
static int tls_has_buffered_record(tls_state_t *tls)
|
|
|
|
{
|
2017-01-21 00:57:06 +05:30
|
|
|
int buffered = tls->buffered_size;
|
2017-01-20 23:41:14 +05:30
|
|
|
struct record_hdr *xhdr;
|
|
|
|
int rec_size;
|
|
|
|
|
|
|
|
if (buffered < RECHDR_LEN)
|
|
|
|
return 0;
|
2017-01-21 00:57:06 +05:30
|
|
|
xhdr = (void*)(tls->inbuf + tls->ofs_to_buffered);
|
2017-01-20 23:41:14 +05:30
|
|
|
rec_size = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
|
|
|
|
if (buffered < rec_size)
|
|
|
|
return 0;
|
|
|
|
return rec_size;
|
|
|
|
}
|
|
|
|
|
2017-01-23 20:42:17 +05:30
|
|
|
static const char *alert_text(int code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
case 20: return "bad MAC";
|
|
|
|
case 50: return "decode error";
|
|
|
|
case 51: return "decrypt error";
|
|
|
|
case 40: return "handshake failure";
|
|
|
|
case 112: return "unrecognized name";
|
|
|
|
}
|
|
|
|
return itoa(code);
|
|
|
|
}
|
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
static int tls_xread_record(tls_state_t *tls)
|
2017-01-14 17:19:32 +05:30
|
|
|
{
|
2017-01-14 18:27:16 +05:30
|
|
|
struct record_hdr *xhdr;
|
2017-01-19 04:50:45 +05:30
|
|
|
int sz;
|
2017-01-14 17:19:32 +05:30
|
|
|
int total;
|
|
|
|
int target;
|
|
|
|
|
2017-01-20 18:42:10 +05:30
|
|
|
again:
|
2017-01-21 00:57:06 +05:30
|
|
|
dbg("ofs_to_buffered:%u buffered_size:%u\n", tls->ofs_to_buffered, tls->buffered_size);
|
|
|
|
total = tls->buffered_size;
|
2017-01-20 22:29:25 +05:30
|
|
|
if (total != 0) {
|
2017-01-21 00:57:06 +05:30
|
|
|
memmove(tls->inbuf, tls->inbuf + tls->ofs_to_buffered, total);
|
|
|
|
//dbg("<< remaining at %d [%d] ", tls->ofs_to_buffered, total);
|
2017-01-20 22:29:25 +05:30
|
|
|
//dump_raw_in("<< %s\n", tls->inbuf, total);
|
|
|
|
}
|
|
|
|
errno = 0;
|
2017-01-21 00:57:06 +05:30
|
|
|
target = MAX_INBUF;
|
2017-01-14 17:19:32 +05:30
|
|
|
for (;;) {
|
2017-01-21 00:57:06 +05:30
|
|
|
int rem;
|
|
|
|
|
|
|
|
if (total >= RECHDR_LEN && target == MAX_INBUF) {
|
2017-01-14 18:27:16 +05:30
|
|
|
xhdr = (void*)tls->inbuf;
|
2017-01-20 18:42:10 +05:30
|
|
|
target = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
|
2017-01-21 00:57:06 +05:30
|
|
|
if (target > MAX_INBUF) {
|
2017-01-14 18:27:16 +05:30
|
|
|
/* malformed input (too long): yell and die */
|
2017-01-21 00:57:06 +05:30
|
|
|
tls->buffered_size = 0;
|
|
|
|
tls->ofs_to_buffered = total;
|
2017-01-14 18:27:16 +05:30
|
|
|
tls_error_die(tls);
|
|
|
|
}
|
2017-01-20 22:29:25 +05:30
|
|
|
/* can also check type/proto_maj/proto_min here */
|
|
|
|
dbg("xhdr type:%d ver:%d.%d len:%d\n",
|
|
|
|
xhdr->type, xhdr->proto_maj, xhdr->proto_min,
|
|
|
|
0x100 * xhdr->len16_hi + xhdr->len16_lo
|
|
|
|
);
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
/* if total >= target, we have a full packet (and possibly more)... */
|
2017-01-14 18:27:16 +05:30
|
|
|
if (total - target >= 0)
|
2017-01-14 17:19:32 +05:30
|
|
|
break;
|
2017-01-21 00:57:06 +05:30
|
|
|
/* input buffer is grown only as needed */
|
|
|
|
rem = tls->inbuf_size - total;
|
|
|
|
if (rem == 0) {
|
|
|
|
tls->inbuf_size += MAX_INBUF / 8;
|
|
|
|
if (tls->inbuf_size > MAX_INBUF)
|
|
|
|
tls->inbuf_size = MAX_INBUF;
|
|
|
|
dbg("inbuf_size:%d\n", tls->inbuf_size);
|
|
|
|
rem = tls->inbuf_size - total;
|
|
|
|
tls->inbuf = xrealloc(tls->inbuf, tls->inbuf_size);
|
|
|
|
}
|
2017-01-23 05:38:16 +05:30
|
|
|
sz = safe_read(tls->ifd, tls->inbuf + total, rem);
|
2017-01-20 18:42:10 +05:30
|
|
|
if (sz <= 0) {
|
|
|
|
if (sz == 0 && total == 0) {
|
|
|
|
/* "Abrupt" EOF, no TLS shutdown (seen from kernel.org) */
|
|
|
|
dbg("EOF (without TLS shutdown) from peer\n");
|
2017-01-21 00:57:06 +05:30
|
|
|
tls->buffered_size = 0;
|
2017-01-20 18:42:10 +05:30
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
bb_perror_msg_and_die("short read, have only %d", total);
|
|
|
|
}
|
2017-01-20 22:29:25 +05:30
|
|
|
dump_raw_in("<< %s\n", tls->inbuf + total, sz);
|
2017-01-19 04:50:45 +05:30
|
|
|
total += sz;
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
2017-01-21 00:57:06 +05:30
|
|
|
tls->buffered_size = total - target;
|
|
|
|
tls->ofs_to_buffered = target;
|
|
|
|
//dbg("<< stashing at %d [%d] ", tls->ofs_to_buffered, tls->buffered_size);
|
|
|
|
//dump_hex("<< %s\n", tls->inbuf + tls->ofs_to_buffered, tls->buffered_size);
|
2017-01-20 18:42:10 +05:30
|
|
|
|
|
|
|
sz = target - RECHDR_LEN;
|
2017-01-19 04:50:45 +05:30
|
|
|
|
|
|
|
/* Needs to be decrypted? */
|
2017-01-24 20:30:54 +05:30
|
|
|
if (tls->min_encrypted_len_on_read > tls->MAC_size) {
|
2017-01-20 18:42:10 +05:30
|
|
|
uint8_t *p = tls->inbuf + RECHDR_LEN;
|
2017-01-19 04:50:45 +05:30
|
|
|
int padding_len;
|
|
|
|
|
|
|
|
if (sz & (AES_BLOCKSIZE-1)
|
2017-01-24 22:38:07 +05:30
|
|
|
|| sz < (int)tls->min_encrypted_len_on_read
|
2017-01-19 04:50:45 +05:30
|
|
|
) {
|
2017-01-24 22:38:07 +05:30
|
|
|
bb_error_msg_and_die("bad encrypted len:%u < %u",
|
|
|
|
sz, tls->min_encrypted_len_on_read);
|
2017-01-19 04:50:45 +05:30
|
|
|
}
|
2017-01-21 01:49:38 +05:30
|
|
|
/* Decrypt content+MAC+padding, moving it over IV in the process */
|
|
|
|
sz -= AES_BLOCKSIZE; /* we will overwrite IV now */
|
2017-02-04 20:53:49 +05:30
|
|
|
aes_cbc_decrypt(
|
|
|
|
tls->server_write_key, tls->key_size, /* selects 128/256 */
|
|
|
|
p, /* IV */
|
|
|
|
p + AES_BLOCKSIZE, sz, /* ciphertext */
|
|
|
|
p /* plaintext */
|
2017-01-19 04:50:45 +05:30
|
|
|
);
|
|
|
|
padding_len = p[sz - 1];
|
2017-01-21 01:49:38 +05:30
|
|
|
dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[0], padding_len);
|
2017-01-19 04:50:45 +05:30
|
|
|
padding_len++;
|
2017-01-24 20:30:54 +05:30
|
|
|
sz -= tls->MAC_size + padding_len; /* drop MAC and padding */
|
2017-01-21 01:53:10 +05:30
|
|
|
//if (sz < 0)
|
|
|
|
// bb_error_msg_and_die("bad padding size:%u", padding_len);
|
2017-01-20 07:45:09 +05:30
|
|
|
} else {
|
|
|
|
/* if nonzero, then it's TLS_RSA_WITH_NULL_SHA256: drop MAC */
|
|
|
|
/* else: no encryption yet on input, subtract zero = NOP */
|
|
|
|
sz -= tls->min_encrypted_len_on_read;
|
2017-01-19 04:50:45 +05:30
|
|
|
}
|
2017-01-21 01:53:10 +05:30
|
|
|
if (sz < 0)
|
|
|
|
bb_error_msg_and_die("encrypted data too short");
|
2017-01-17 21:23:36 +05:30
|
|
|
|
2017-01-20 18:42:10 +05:30
|
|
|
//dump_hex("<< %s\n", tls->inbuf, RECHDR_LEN + sz);
|
|
|
|
|
|
|
|
xhdr = (void*)tls->inbuf;
|
|
|
|
if (xhdr->type == RECORD_TYPE_ALERT && sz >= 2) {
|
|
|
|
uint8_t *p = tls->inbuf + RECHDR_LEN;
|
|
|
|
dbg("ALERT size:%d level:%d description:%d\n", sz, p[0], p[1]);
|
2017-01-23 20:42:17 +05:30
|
|
|
if (p[0] == 2) { /* fatal */
|
|
|
|
bb_error_msg_and_die("TLS %s from peer (alert code %d): %s",
|
|
|
|
"error",
|
|
|
|
p[1], alert_text(p[1])
|
|
|
|
);
|
|
|
|
}
|
2017-01-21 01:49:38 +05:30
|
|
|
if (p[0] == 1) { /* warning */
|
|
|
|
if (p[1] == 0) { /* "close_notify" warning: it's EOF */
|
2017-01-20 18:42:10 +05:30
|
|
|
dbg("EOF (TLS encoded) from peer\n");
|
|
|
|
sz = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-01-23 20:42:17 +05:30
|
|
|
//This possibly needs to be cached and shown only if
|
|
|
|
//a fatal alert follows
|
|
|
|
// bb_error_msg("TLS %s from peer (alert code %d): %s",
|
|
|
|
// "warning",
|
|
|
|
// p[1], alert_text(p[1])
|
|
|
|
// );
|
2017-01-20 18:42:10 +05:30
|
|
|
/* discard it, get next record */
|
|
|
|
goto again;
|
|
|
|
}
|
2017-01-23 20:42:17 +05:30
|
|
|
/* p[0] not 1 or 2: not defined in protocol */
|
2017-01-20 18:42:10 +05:30
|
|
|
sz = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:23:36 +05:30
|
|
|
/* RFC 5246 is not saying it explicitly, but sha256 hash
|
2017-01-17 21:34:24 +05:30
|
|
|
* in our FINISHED record must include data of incoming packets too!
|
2017-01-17 21:23:36 +05:30
|
|
|
*/
|
2017-01-24 20:30:54 +05:30
|
|
|
if (tls->inbuf[0] == RECORD_TYPE_HANDSHAKE
|
|
|
|
&& tls->MAC_size != 0 /* do we know which hash to use? (server_hello() does not!) */
|
|
|
|
) {
|
|
|
|
hash_handshake(tls, "<< hash:%s", tls->inbuf + RECHDR_LEN, sz);
|
2017-01-17 21:23:36 +05:30
|
|
|
}
|
2017-01-20 18:42:10 +05:30
|
|
|
end:
|
2017-01-19 04:50:45 +05:30
|
|
|
dbg("got block len:%u\n", sz);
|
|
|
|
return sz;
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-17 21:34:24 +05:30
|
|
|
/*
|
|
|
|
* DER parsing routines
|
|
|
|
*/
|
2017-01-14 17:19:32 +05:30
|
|
|
static unsigned get_der_len(uint8_t **bodyp, uint8_t *der, uint8_t *end)
|
|
|
|
{
|
2017-01-15 03:08:25 +05:30
|
|
|
unsigned len, len1;
|
2017-01-14 17:19:32 +05:30
|
|
|
|
|
|
|
if (end - der < 2)
|
|
|
|
xfunc_die();
|
|
|
|
// if ((der[0] & 0x1f) == 0x1f) /* not single-byte item code? */
|
|
|
|
// xfunc_die();
|
|
|
|
|
|
|
|
len = der[1]; /* maybe it's short len */
|
|
|
|
if (len >= 0x80) {
|
2017-01-17 03:56:33 +05:30
|
|
|
/* no, it's long */
|
2017-01-15 03:08:25 +05:30
|
|
|
|
2017-01-17 03:56:33 +05:30
|
|
|
if (len == 0x80 || end - der < (int)(len - 0x7e)) {
|
2017-01-14 17:19:32 +05:30
|
|
|
/* 0x80 is "0 bytes of len", invalid DER: must use short len if can */
|
2017-01-17 03:56:33 +05:30
|
|
|
/* need 3 or 4 bytes for 81, 82 */
|
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
|
|
|
|
len1 = der[2]; /* if (len == 0x81) it's "ii 81 xx", fetch xx */
|
|
|
|
if (len > 0x82) {
|
2017-01-14 17:19:32 +05:30
|
|
|
/* >0x82 is "3+ bytes of len", should not happen realistically */
|
|
|
|
xfunc_die();
|
|
|
|
}
|
2017-01-17 03:56:33 +05:30
|
|
|
if (len == 0x82) { /* it's "ii 82 xx yy" */
|
|
|
|
len1 = 0x100*len1 + der[3];
|
|
|
|
der += 1; /* skip [yy] */
|
|
|
|
}
|
2017-01-15 03:08:25 +05:30
|
|
|
der += 1; /* skip [xx] */
|
|
|
|
len = len1;
|
2017-01-14 18:27:16 +05:30
|
|
|
// if (len < 0x80)
|
|
|
|
// xfunc_die(); /* invalid DER: must use short len if can */
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
2017-01-15 03:08:25 +05:30
|
|
|
der += 2; /* skip [code]+[1byte] */
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-15 03:08:25 +05:30
|
|
|
if (end - der < (int)len)
|
2017-01-14 17:19:32 +05:30
|
|
|
xfunc_die();
|
|
|
|
*bodyp = der;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *enter_der_item(uint8_t *der, uint8_t **endp)
|
|
|
|
{
|
|
|
|
uint8_t *new_der;
|
|
|
|
unsigned len = get_der_len(&new_der, der, *endp);
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg_der("entered der @%p:0x%02x len:%u inner_byte @%p:0x%02x\n", der, der[0], len, new_der, new_der[0]);
|
2017-01-14 17:19:32 +05:30
|
|
|
/* Move "end" position to cover only this item */
|
|
|
|
*endp = new_der + len;
|
|
|
|
return new_der;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *skip_der_item(uint8_t *der, uint8_t *end)
|
|
|
|
{
|
|
|
|
uint8_t *new_der;
|
|
|
|
unsigned len = get_der_len(&new_der, der, end);
|
|
|
|
/* Skip body */
|
|
|
|
new_der += len;
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg_der("skipped der 0x%02x, next byte 0x%02x\n", der[0], new_der[0]);
|
2017-01-14 17:19:32 +05:30
|
|
|
return new_der;
|
|
|
|
}
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end)
|
|
|
|
{
|
2017-01-15 04:48:22 +05:30
|
|
|
uint8_t *bin_ptr;
|
|
|
|
unsigned len = get_der_len(&bin_ptr, der, end);
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]);
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len);
|
|
|
|
pstm_read_unsigned_bin(pstm_n, bin_ptr, len);
|
|
|
|
//return bin + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len)
|
2017-01-14 17:19:32 +05:30
|
|
|
{
|
2017-01-14 18:27:16 +05:30
|
|
|
/* Certificate is a DER-encoded data structure. Each DER element has a length,
|
|
|
|
* which makes it easy to skip over large compound elements of any complexity
|
|
|
|
* without parsing them. Example: partial decode of kernel.org certificate:
|
2017-01-14 17:19:32 +05:30
|
|
|
* SEQ 0x05ac/1452 bytes (Certificate): 308205ac
|
|
|
|
* SEQ 0x0494/1172 bytes (tbsCertificate): 30820494
|
|
|
|
* [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0] 3 bytes: a003
|
|
|
|
* INTEGER (version): 0201 02
|
|
|
|
* INTEGER 0x11 bytes (serialNumber): 0211 00 9f85bf664b0cddafca508679501b2be4
|
|
|
|
* //^^^^^^note: matrixSSL also allows [ASN_CONTEXT_SPECIFIC | ASN_PRIMITIVE | 2] = 0x82 type
|
|
|
|
* SEQ 0x0d bytes (signatureAlgo): 300d
|
|
|
|
* OID 9 bytes: 0609 2a864886f70d01010b (OID_SHA256_RSA_SIG 42.134.72.134.247.13.1.1.11)
|
|
|
|
* NULL: 0500
|
|
|
|
* SEQ 0x5f bytes (issuer): 305f
|
|
|
|
* SET 11 bytes: 310b
|
|
|
|
* SEQ 9 bytes: 3009
|
|
|
|
* OID 3 bytes: 0603 550406
|
|
|
|
* Printable string "FR": 1302 4652
|
|
|
|
* SET 14 bytes: 310e
|
|
|
|
* SEQ 12 bytes: 300c
|
|
|
|
* OID 3 bytes: 0603 550408
|
|
|
|
* Printable string "Paris": 1305 5061726973
|
|
|
|
* SET 14 bytes: 310e
|
|
|
|
* SEQ 12 bytes: 300c
|
|
|
|
* OID 3 bytes: 0603 550407
|
|
|
|
* Printable string "Paris": 1305 5061726973
|
|
|
|
* SET 14 bytes: 310e
|
|
|
|
* SEQ 12 bytes: 300c
|
|
|
|
* OID 3 bytes: 0603 55040a
|
|
|
|
* Printable string "Gandi": 1305 47616e6469
|
|
|
|
* SET 32 bytes: 3120
|
|
|
|
* SEQ 30 bytes: 301e
|
|
|
|
* OID 3 bytes: 0603 550403
|
2017-01-14 18:27:16 +05:30
|
|
|
* Printable string "Gandi Standard SSL CA 2": 1317 47616e6469205374616e646172642053534c2043412032
|
2017-01-14 17:19:32 +05:30
|
|
|
* SEQ 30 bytes (validity): 301e
|
|
|
|
* TIME "161011000000Z": 170d 3136313031313030303030305a
|
|
|
|
* TIME "191011235959Z": 170d 3139313031313233353935395a
|
|
|
|
* SEQ 0x5b/91 bytes (subject): 305b //I did not decode this
|
|
|
|
* 3121301f060355040b1318446f6d61696e20436f
|
|
|
|
* 6e74726f6c2056616c6964617465643121301f06
|
|
|
|
* 0355040b1318506f73697469766553534c204d75
|
|
|
|
* 6c74692d446f6d61696e31133011060355040313
|
|
|
|
* 0a6b65726e656c2e6f7267
|
|
|
|
* SEQ 0x01a2/418 bytes (subjectPublicKeyInfo): 308201a2
|
|
|
|
* SEQ 13 bytes (algorithm): 300d
|
|
|
|
* OID 9 bytes: 0609 2a864886f70d010101 (OID_RSA_KEY_ALG 42.134.72.134.247.13.1.1.1)
|
|
|
|
* NULL: 0500
|
|
|
|
* BITSTRING 0x018f/399 bytes (publicKey): 0382018f
|
|
|
|
* ????: 00
|
|
|
|
* //after the zero byte, it appears key itself uses DER encoding:
|
|
|
|
* SEQ 0x018a/394 bytes: 3082018a
|
|
|
|
* INTEGER 0x0181/385 bytes (modulus): 02820181
|
|
|
|
* 00b1ab2fc727a3bef76780c9349bf3
|
|
|
|
* ...24 more blocks of 15 bytes each...
|
|
|
|
* 90e895291c6bc8693b65
|
|
|
|
* INTEGER 3 bytes (exponent): 0203 010001
|
|
|
|
* [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0x3] 0x01e5 bytes (X509v3 extensions): a38201e5
|
|
|
|
* SEQ 0x01e1 bytes: 308201e1
|
|
|
|
* ...
|
|
|
|
* Certificate is a sequence of three elements:
|
|
|
|
* tbsCertificate (SEQ)
|
|
|
|
* signatureAlgorithm (AlgorithmIdentifier)
|
|
|
|
* signatureValue (BIT STRING)
|
|
|
|
*
|
|
|
|
* In turn, tbsCertificate is a sequence of:
|
|
|
|
* version
|
|
|
|
* serialNumber
|
|
|
|
* signatureAlgo (AlgorithmIdentifier)
|
|
|
|
* issuer (Name, has complex structure)
|
|
|
|
* validity (Validity, SEQ of two Times)
|
|
|
|
* subject (Name)
|
|
|
|
* subjectPublicKeyInfo (SEQ)
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* subjectPublicKeyInfo is a sequence of:
|
|
|
|
* algorithm (AlgorithmIdentifier)
|
|
|
|
* publicKey (BIT STRING)
|
|
|
|
*
|
2017-01-14 18:27:16 +05:30
|
|
|
* We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey
|
2017-01-14 17:19:32 +05:30
|
|
|
*/
|
|
|
|
uint8_t *end = der + len;
|
|
|
|
|
|
|
|
/* enter "Certificate" item: [der, end) will be only Cert */
|
|
|
|
der = enter_der_item(der, &end);
|
|
|
|
|
|
|
|
/* enter "tbsCertificate" item: [der, end) will be only tbsCert */
|
|
|
|
der = enter_der_item(der, &end);
|
|
|
|
|
|
|
|
/* skip up to subjectPublicKeyInfo */
|
|
|
|
der = skip_der_item(der, end); /* version */
|
|
|
|
der = skip_der_item(der, end); /* serialNumber */
|
|
|
|
der = skip_der_item(der, end); /* signatureAlgo */
|
|
|
|
der = skip_der_item(der, end); /* issuer */
|
|
|
|
der = skip_der_item(der, end); /* validity */
|
|
|
|
der = skip_der_item(der, end); /* subject */
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
/* enter subjectPublicKeyInfo */
|
2017-01-14 17:19:32 +05:30
|
|
|
der = enter_der_item(der, &end);
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
{ /* check subjectPublicKeyInfo.algorithm */
|
|
|
|
static const uint8_t expected[] = {
|
|
|
|
0x30,0x0d, // SEQ 13 bytes
|
|
|
|
0x06,0x09, 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, // OID RSA_KEY_ALG 42.134.72.134.247.13.1.1.1
|
|
|
|
//0x05,0x00, // NULL
|
|
|
|
};
|
|
|
|
if (memcmp(der, expected, sizeof(expected)) != 0)
|
|
|
|
bb_error_msg_and_die("not RSA key");
|
|
|
|
}
|
|
|
|
/* skip subjectPublicKeyInfo.algorithm */
|
2017-01-14 17:19:32 +05:30
|
|
|
der = skip_der_item(der, end);
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
/* enter subjectPublicKeyInfo.publicKey */
|
2017-01-14 17:19:32 +05:30
|
|
|
// die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */
|
|
|
|
der = enter_der_item(der, &end);
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
/* parse RSA key: */
|
|
|
|
//based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note
|
|
|
|
dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]);
|
|
|
|
if (end - der < 14) xfunc_die();
|
|
|
|
/* example format:
|
|
|
|
* ignore bits: 00
|
|
|
|
* SEQ 0x018a/394 bytes: 3082018a
|
|
|
|
* INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX
|
|
|
|
* INTEGER 3 bytes (exponent): 0203 010001
|
|
|
|
*/
|
|
|
|
if (*der != 0) /* "ignore bits", should be 0 */
|
|
|
|
xfunc_die();
|
|
|
|
der++;
|
|
|
|
der = enter_der_item(der, &end); /* enter SEQ */
|
2017-01-23 05:38:16 +05:30
|
|
|
/* memset(tls->hsd->server_rsa_pub_key, 0, sizeof(tls->hsd->server_rsa_pub_key)); - already is */
|
|
|
|
der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.N, der, end); /* modulus */
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
der = skip_der_item(der, end);
|
2017-01-23 05:38:16 +05:30
|
|
|
der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.e, der, end); /* exponent */
|
|
|
|
tls->hsd->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->hsd->server_rsa_pub_key.N);
|
|
|
|
dbg("server_rsa_pub_key.size:%d\n", tls->hsd->server_rsa_pub_key.size);
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-17 21:34:24 +05:30
|
|
|
/*
|
|
|
|
* TLS Handshake routines
|
|
|
|
*/
|
2017-01-21 03:18:41 +05:30
|
|
|
static int tls_xread_handshake_block(tls_state_t *tls, int min_len)
|
2017-01-16 08:55:01 +05:30
|
|
|
{
|
2017-01-17 21:34:24 +05:30
|
|
|
struct record_hdr *xhdr;
|
2017-01-20 23:41:14 +05:30
|
|
|
int len = tls_xread_record(tls);
|
2017-01-16 08:55:01 +05:30
|
|
|
|
2017-01-17 21:34:24 +05:30
|
|
|
xhdr = (void*)tls->inbuf;
|
|
|
|
if (len < min_len
|
|
|
|
|| xhdr->type != RECORD_TYPE_HANDSHAKE
|
|
|
|
|| xhdr->proto_maj != TLS_MAJ
|
|
|
|
|| xhdr->proto_min != TLS_MIN
|
|
|
|
) {
|
2017-01-23 20:42:17 +05:30
|
|
|
bad_record_die(tls, "handshake record", len);
|
2017-01-16 08:55:01 +05:30
|
|
|
}
|
2017-01-17 21:34:24 +05:30
|
|
|
dbg("got HANDSHAKE\n");
|
|
|
|
return len;
|
2017-01-16 08:55:01 +05:30
|
|
|
}
|
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
static ALWAYS_INLINE void fill_handshake_record_hdr(void *buf, unsigned type, unsigned len)
|
2017-01-17 22:47:27 +05:30
|
|
|
{
|
|
|
|
struct handshake_hdr {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
2017-01-20 07:45:09 +05:30
|
|
|
} *h = buf;
|
2017-01-17 22:47:27 +05:30
|
|
|
|
|
|
|
len -= 4;
|
2017-01-20 07:45:09 +05:30
|
|
|
h->type = type;
|
2017-01-17 22:47:27 +05:30
|
|
|
h->len24_hi = len >> 16;
|
|
|
|
h->len24_mid = len >> 8;
|
|
|
|
h->len24_lo = len & 0xff;
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni)
|
2017-01-15 18:46:51 +05:30
|
|
|
{
|
|
|
|
struct client_hello {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
|
|
|
uint8_t proto_maj, proto_min;
|
|
|
|
uint8_t rand32[32];
|
|
|
|
uint8_t session_id_len;
|
|
|
|
/* uint8_t session_id[]; */
|
|
|
|
uint8_t cipherid_len16_hi, cipherid_len16_lo;
|
2017-01-24 20:30:54 +05:30
|
|
|
uint8_t cipherid[2 * (2 + !!CIPHER_ID2)]; /* actually variable */
|
2017-01-15 18:46:51 +05:30
|
|
|
uint8_t comprtypes_len;
|
|
|
|
uint8_t comprtypes[1]; /* actually variable */
|
2017-01-21 03:18:41 +05:30
|
|
|
/* Extensions (SNI shown):
|
|
|
|
* hi,lo // len of all extensions
|
2017-01-23 20:42:17 +05:30
|
|
|
* 00,00 // extension_type: "Server Name"
|
|
|
|
* 00,0e // list len (there can be more than one SNI)
|
|
|
|
* 00,0c // len of 1st Server Name Indication
|
|
|
|
* 00 // name type: host_name
|
|
|
|
* 00,09 // name len
|
2017-01-21 03:18:41 +05:30
|
|
|
* "localhost" // name
|
|
|
|
*/
|
2017-01-23 20:42:17 +05:30
|
|
|
// GNU Wget 1.18 to cdn.kernel.org sends these extensions:
|
|
|
|
// 0055
|
|
|
|
// 0005 0005 0100000000 - status_request
|
|
|
|
// 0000 0013 0011 00 000e 63646e 2e 6b65726e656c 2e 6f7267 - server_name
|
|
|
|
// ff01 0001 00 - renegotiation_info
|
|
|
|
// 0023 0000 - session_ticket
|
|
|
|
// 000a 0008 0006001700180019 - supported_groups
|
|
|
|
// 000b 0002 0100 - ec_point_formats
|
|
|
|
// 000d 0016 00140401040305010503060106030301030302010203 - signature_algorithms
|
2017-01-15 18:46:51 +05:30
|
|
|
};
|
2017-01-21 03:18:41 +05:30
|
|
|
struct client_hello *record;
|
|
|
|
int len;
|
|
|
|
int sni_len = sni ? strnlen(sni, 127) : 0;
|
|
|
|
|
|
|
|
len = sizeof(*record);
|
|
|
|
if (sni_len)
|
|
|
|
len += 11 + strlen(sni);
|
|
|
|
record = tls_get_outbuf(tls, len);
|
|
|
|
memset(record, 0, len);
|
2017-01-23 20:42:17 +05:30
|
|
|
|
2017-01-21 03:18:41 +05:30
|
|
|
fill_handshake_record_hdr(record, HANDSHAKE_CLIENT_HELLO, len);
|
2017-01-20 07:45:09 +05:30
|
|
|
record->proto_maj = TLS_MAJ; /* the "requested" version of the protocol, */
|
|
|
|
record->proto_min = TLS_MIN; /* can be higher than one in record headers */
|
|
|
|
tls_get_random(record->rand32, sizeof(record->rand32));
|
2017-01-20 18:42:10 +05:30
|
|
|
if (TLS_DEBUG_FIXED_SECRETS)
|
|
|
|
memset(record->rand32, 0x11, sizeof(record->rand32));
|
2017-01-21 03:18:41 +05:30
|
|
|
/* record->session_id_len = 0; - already is */
|
2017-01-23 20:42:17 +05:30
|
|
|
|
2017-01-21 03:18:41 +05:30
|
|
|
/* record->cipherid_len16_hi = 0; */
|
2017-01-24 20:30:54 +05:30
|
|
|
record->cipherid_len16_lo = sizeof(record->cipherid);
|
2017-01-23 20:42:17 +05:30
|
|
|
/* RFC 5746 Renegotiation Indication Extension - some servers will refuse to work with us otherwise */
|
2017-01-24 20:30:54 +05:30
|
|
|
/*record->cipherid[0] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV >> 8; - zero */
|
|
|
|
record->cipherid[1] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV & 0xff;
|
|
|
|
if ((CIPHER_ID1 >> 8) != 0) record->cipherid[2] = CIPHER_ID1 >> 8;
|
|
|
|
/*************************/ record->cipherid[3] = CIPHER_ID1 & 0xff;
|
|
|
|
#if CIPHER_ID2
|
|
|
|
if ((CIPHER_ID2 >> 8) != 0) record->cipherid[4] = CIPHER_ID2 >> 8;
|
|
|
|
/*************************/ record->cipherid[5] = CIPHER_ID2 & 0xff;
|
|
|
|
#endif
|
2017-01-23 20:42:17 +05:30
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
record->comprtypes_len = 1;
|
2017-01-21 03:18:41 +05:30
|
|
|
/* record->comprtypes[0] = 0; */
|
|
|
|
|
|
|
|
if (sni_len) {
|
|
|
|
uint8_t *p = (void*)(record + 1);
|
|
|
|
//p[0] = 0; //
|
|
|
|
p[1] = sni_len + 9; //ext_len
|
|
|
|
//p[2] = 0; //
|
|
|
|
//p[3] = 0; //extension_type
|
|
|
|
//p[4] = 0; //
|
|
|
|
p[5] = sni_len + 5; //list len
|
|
|
|
//p[6] = 0; //
|
|
|
|
p[7] = sni_len + 3; //len of 1st SNI
|
|
|
|
//p[8] = 0; //name type
|
|
|
|
//p[9] = 0; //
|
|
|
|
p[10] = sni_len; //name len
|
|
|
|
memcpy(&p[11], sni, sni_len);
|
|
|
|
}
|
2017-01-20 18:57:58 +05:30
|
|
|
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg(">> CLIENT_HELLO\n");
|
2017-01-24 20:30:54 +05:30
|
|
|
/* Can hash it only when we know which MAC hash to use */
|
|
|
|
/*xwrite_and_update_handshake_hash(tls, len); - WRONG! */
|
|
|
|
xwrite_handshake_record(tls, len);
|
|
|
|
|
|
|
|
tls->hsd = xzalloc(sizeof(*tls->hsd) + len);
|
|
|
|
tls->hsd->saved_client_hello_size = len;
|
|
|
|
memcpy(tls->hsd->saved_client_hello, record, len);
|
|
|
|
memcpy(tls->hsd->client_and_server_rand32, record->rand32, sizeof(record->rand32));
|
2017-01-15 18:46:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void get_server_hello(tls_state_t *tls)
|
|
|
|
{
|
|
|
|
struct server_hello {
|
|
|
|
struct record_hdr xhdr;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
|
|
|
uint8_t proto_maj, proto_min;
|
|
|
|
uint8_t rand32[32]; /* first 4 bytes are unix time in BE format */
|
|
|
|
uint8_t session_id_len;
|
|
|
|
uint8_t session_id[32];
|
|
|
|
uint8_t cipherid_hi, cipherid_lo;
|
|
|
|
uint8_t comprtype;
|
|
|
|
/* extensions may follow, but only those which client offered in its Hello */
|
|
|
|
};
|
2017-01-21 03:18:41 +05:30
|
|
|
|
2017-01-15 18:46:51 +05:30
|
|
|
struct server_hello *hp;
|
2017-01-17 03:56:33 +05:30
|
|
|
uint8_t *cipherid;
|
2017-01-24 20:30:54 +05:30
|
|
|
unsigned cipher;
|
|
|
|
int len, len24;
|
2017-01-15 18:46:51 +05:30
|
|
|
|
2017-02-03 22:49:59 +05:30
|
|
|
len = tls_xread_handshake_block(tls, 74 - 32);
|
2017-01-15 18:46:51 +05:30
|
|
|
|
|
|
|
hp = (void*)tls->inbuf;
|
|
|
|
// 74 bytes:
|
|
|
|
// 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|
|
|
|
|
//SvHl len=70 maj.min unixtime^^^ 28randbytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^ slen sid32bytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cipSel comprSel
|
|
|
|
if (hp->type != HANDSHAKE_SERVER_HELLO
|
|
|
|
|| hp->len24_hi != 0
|
|
|
|
|| hp->len24_mid != 0
|
2017-01-17 03:56:33 +05:30
|
|
|
/* hp->len24_lo checked later */
|
2017-01-15 18:46:51 +05:30
|
|
|
|| hp->proto_maj != TLS_MAJ
|
|
|
|
|| hp->proto_min != TLS_MIN
|
|
|
|
) {
|
2017-01-23 20:42:17 +05:30
|
|
|
bad_record_die(tls, "'server hello'", len);
|
2017-01-15 18:46:51 +05:30
|
|
|
}
|
2017-01-17 03:56:33 +05:30
|
|
|
|
|
|
|
cipherid = &hp->cipherid_hi;
|
2017-01-24 20:30:54 +05:30
|
|
|
len24 = hp->len24_lo;
|
2017-01-17 03:56:33 +05:30
|
|
|
if (hp->session_id_len != 32) {
|
|
|
|
if (hp->session_id_len != 0)
|
2017-02-03 22:49:59 +05:30
|
|
|
bad_record_die(tls, "'server hello'", len);
|
2017-01-17 03:56:33 +05:30
|
|
|
|
|
|
|
// session_id_len == 0: no session id
|
|
|
|
// "The server
|
|
|
|
// may return an empty session_id to indicate that the session will
|
|
|
|
// not be cached and therefore cannot be resumed."
|
|
|
|
cipherid -= 32;
|
2017-01-24 20:30:54 +05:30
|
|
|
len24 += 32; /* what len would be if session id would be present */
|
2017-01-17 03:56:33 +05:30
|
|
|
}
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
if (len24 < 70
|
|
|
|
// || cipherid[0] != (CIPHER_ID >> 8)
|
|
|
|
// || cipherid[1] != (CIPHER_ID & 0xff)
|
|
|
|
// || cipherid[2] != 0 /* comprtype */
|
2017-01-17 03:56:33 +05:30
|
|
|
) {
|
2017-02-03 22:49:59 +05:30
|
|
|
bad_record_die(tls, "'server hello'", len);
|
2017-01-17 03:56:33 +05:30
|
|
|
}
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< SERVER_HELLO\n");
|
2017-01-24 20:30:54 +05:30
|
|
|
|
2017-01-23 05:38:16 +05:30
|
|
|
memcpy(tls->hsd->client_and_server_rand32 + 32, hp->rand32, sizeof(hp->rand32));
|
2017-01-24 20:30:54 +05:30
|
|
|
|
|
|
|
tls->cipher_id = cipher = 0x100 * cipherid[0] + cipherid[1];
|
|
|
|
dbg("server chose cipher %04x\n", cipher);
|
|
|
|
|
|
|
|
if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA) {
|
|
|
|
tls->key_size = AES128_KEYSIZE;
|
|
|
|
tls->MAC_size = SHA1_OUTSIZE;
|
|
|
|
}
|
|
|
|
else { /* TLS_RSA_WITH_AES_256_CBC_SHA256 */
|
|
|
|
tls->key_size = AES256_KEYSIZE;
|
|
|
|
tls->MAC_size = SHA256_OUTSIZE;
|
|
|
|
}
|
2017-01-24 22:38:07 +05:30
|
|
|
/* Handshake hash eventually destined to FINISHED record
|
|
|
|
* is sha256 regardless of cipher
|
|
|
|
* (at least for all ciphers defined by RFC5246).
|
|
|
|
* It's not sha1 for AES_128_CBC_SHA - only MAC is sha1, not this hash.
|
|
|
|
*/
|
|
|
|
sha256_begin(&tls->hsd->handshake_hash_ctx);
|
2017-01-24 20:30:54 +05:30
|
|
|
hash_handshake(tls, ">> client hello hash:%s",
|
|
|
|
tls->hsd->saved_client_hello, tls->hsd->saved_client_hello_size
|
|
|
|
);
|
|
|
|
hash_handshake(tls, "<< server hello hash:%s",
|
|
|
|
tls->inbuf + RECHDR_LEN, len
|
|
|
|
);
|
2017-01-15 18:46:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void get_server_cert(tls_state_t *tls)
|
2017-01-14 17:19:32 +05:30
|
|
|
{
|
2017-01-14 18:27:16 +05:30
|
|
|
struct record_hdr *xhdr;
|
2017-01-14 17:19:32 +05:30
|
|
|
uint8_t *certbuf;
|
|
|
|
int len, len1;
|
|
|
|
|
2017-01-21 03:18:41 +05:30
|
|
|
len = tls_xread_handshake_block(tls, 10);
|
2017-01-15 06:47:03 +05:30
|
|
|
|
2017-01-14 17:19:32 +05:30
|
|
|
xhdr = (void*)tls->inbuf;
|
|
|
|
certbuf = (void*)(xhdr + 1);
|
|
|
|
if (certbuf[0] != HANDSHAKE_CERTIFICATE)
|
|
|
|
tls_error_die(tls);
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< CERTIFICATE\n");
|
2017-01-14 18:27:16 +05:30
|
|
|
// 4392 bytes:
|
|
|
|
// 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...
|
|
|
|
//Cert len=4388 ChainLen CertLen^ DER encoded X509 starts here. openssl x509 -in FILE -inform DER -noout -text
|
2017-01-14 17:19:32 +05:30
|
|
|
len1 = get24be(certbuf + 1);
|
|
|
|
if (len1 > len - 4) tls_error_die(tls);
|
|
|
|
len = len1;
|
|
|
|
len1 = get24be(certbuf + 4);
|
|
|
|
if (len1 > len - 3) tls_error_die(tls);
|
|
|
|
len = len1;
|
|
|
|
len1 = get24be(certbuf + 7);
|
|
|
|
if (len1 > len - 3) tls_error_die(tls);
|
|
|
|
len = len1;
|
|
|
|
|
|
|
|
if (len)
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
find_key_in_der_cert(tls, certbuf + 10, len);
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:36:10 +05:30
|
|
|
static void send_empty_client_cert(tls_state_t *tls)
|
|
|
|
{
|
|
|
|
struct client_empty_cert {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
|
|
|
uint8_t cert_chain_len24_hi, cert_chain_len24_mid, cert_chain_len24_lo;
|
|
|
|
};
|
|
|
|
struct client_empty_cert *record;
|
|
|
|
|
|
|
|
record = tls_get_outbuf(tls, sizeof(*record));
|
|
|
|
//FIXME: can just memcpy a ready-made one.
|
|
|
|
fill_handshake_record_hdr(record, HANDSHAKE_CERTIFICATE, sizeof(*record));
|
|
|
|
record->cert_chain_len24_hi = 0;
|
|
|
|
record->cert_chain_len24_mid = 0;
|
|
|
|
record->cert_chain_len24_lo = 0;
|
|
|
|
|
|
|
|
dbg(">> CERTIFICATE\n");
|
|
|
|
xwrite_and_update_handshake_hash(tls, sizeof(*record));
|
|
|
|
}
|
|
|
|
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
static void send_client_key_exchange(tls_state_t *tls)
|
|
|
|
{
|
|
|
|
struct client_key_exchange {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
2017-01-17 22:47:27 +05:30
|
|
|
/* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */
|
|
|
|
uint8_t keylen16_hi, keylen16_lo;
|
2017-01-17 21:23:36 +05:30
|
|
|
uint8_t key[4 * 1024]; // size??
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
};
|
2017-01-20 07:45:09 +05:30
|
|
|
//FIXME: better size estimate
|
|
|
|
struct client_key_exchange *record = tls_get_outbuf(tls, sizeof(*record));
|
2017-01-20 23:41:14 +05:30
|
|
|
uint8_t rsa_premaster[RSA_PREMASTER_SIZE];
|
2017-01-17 21:23:36 +05:30
|
|
|
int len;
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
|
2017-01-15 18:46:51 +05:30
|
|
|
tls_get_random(rsa_premaster, sizeof(rsa_premaster));
|
2017-01-20 18:42:10 +05:30
|
|
|
if (TLS_DEBUG_FIXED_SECRETS)
|
|
|
|
memset(rsa_premaster, 0x44, sizeof(rsa_premaster));
|
2017-01-17 22:47:27 +05:30
|
|
|
// RFC 5246
|
|
|
|
// "Note: The version number in the PreMasterSecret is the version
|
|
|
|
// offered by the client in the ClientHello.client_version, not the
|
|
|
|
// version negotiated for the connection."
|
2017-01-15 18:46:51 +05:30
|
|
|
rsa_premaster[0] = TLS_MAJ;
|
|
|
|
rsa_premaster[1] = TLS_MIN;
|
2017-01-24 22:38:07 +05:30
|
|
|
dump_hex("premaster:%s\n", rsa_premaster, sizeof(rsa_premaster));
|
2017-01-17 21:23:36 +05:30
|
|
|
len = psRsaEncryptPub(/*pool:*/ NULL,
|
2017-01-23 05:38:16 +05:30
|
|
|
/* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key,
|
2017-01-15 18:46:51 +05:30
|
|
|
rsa_premaster, /*inlen:*/ sizeof(rsa_premaster),
|
2017-01-20 07:45:09 +05:30
|
|
|
record->key, sizeof(record->key),
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
data_param_ignored
|
|
|
|
);
|
2017-01-20 07:45:09 +05:30
|
|
|
record->keylen16_hi = len >> 8;
|
|
|
|
record->keylen16_lo = len & 0xff;
|
2017-01-17 21:23:36 +05:30
|
|
|
len += 2;
|
2017-01-20 07:45:09 +05:30
|
|
|
record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE;
|
|
|
|
record->len24_hi = 0;
|
|
|
|
record->len24_mid = len >> 8;
|
|
|
|
record->len24_lo = len & 0xff;
|
2017-01-17 21:23:36 +05:30
|
|
|
len += 4;
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg(">> CLIENT_KEY_EXCHANGE\n");
|
2017-01-20 07:45:09 +05:30
|
|
|
xwrite_and_update_handshake_hash(tls, len);
|
2017-01-16 08:55:01 +05:30
|
|
|
|
2017-01-17 21:23:36 +05:30
|
|
|
// RFC 5246
|
|
|
|
// For all key exchange methods, the same algorithm is used to convert
|
|
|
|
// the pre_master_secret into the master_secret. The pre_master_secret
|
|
|
|
// should be deleted from memory once the master_secret has been
|
|
|
|
// computed.
|
|
|
|
// master_secret = PRF(pre_master_secret, "master secret",
|
|
|
|
// ClientHello.random + ServerHello.random)
|
|
|
|
// [0..47];
|
|
|
|
// The master secret is always exactly 48 bytes in length. The length
|
|
|
|
// of the premaster secret will vary depending on key exchange method.
|
2017-01-24 22:38:07 +05:30
|
|
|
prf_hmac_sha256(/*tls,*/
|
2017-01-23 05:38:16 +05:30
|
|
|
tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
|
2017-01-16 08:55:01 +05:30
|
|
|
rsa_premaster, sizeof(rsa_premaster),
|
|
|
|
"master secret",
|
2017-01-23 05:38:16 +05:30
|
|
|
tls->hsd->client_and_server_rand32, sizeof(tls->hsd->client_and_server_rand32)
|
2017-01-16 08:55:01 +05:30
|
|
|
);
|
2017-01-23 05:38:16 +05:30
|
|
|
dump_hex("master secret:%s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
|
2017-01-17 03:56:33 +05:30
|
|
|
|
2017-01-17 21:23:36 +05:30
|
|
|
// RFC 5246
|
|
|
|
// 6.3. Key Calculation
|
|
|
|
//
|
|
|
|
// The Record Protocol requires an algorithm to generate keys required
|
|
|
|
// by the current connection state (see Appendix A.6) from the security
|
|
|
|
// parameters provided by the handshake protocol.
|
|
|
|
//
|
|
|
|
// The master secret is expanded into a sequence of secure bytes, which
|
|
|
|
// is then split to a client write MAC key, a server write MAC key, a
|
|
|
|
// client write encryption key, and a server write encryption key. Each
|
|
|
|
// of these is generated from the byte sequence in that order. Unused
|
|
|
|
// values are empty. Some AEAD ciphers may additionally require a
|
|
|
|
// client write IV and a server write IV (see Section 6.2.3.3).
|
|
|
|
//
|
|
|
|
// When keys and MAC keys are generated, the master secret is used as an
|
|
|
|
// entropy source.
|
|
|
|
//
|
|
|
|
// To generate the key material, compute
|
|
|
|
//
|
|
|
|
// key_block = PRF(SecurityParameters.master_secret,
|
|
|
|
// "key expansion",
|
|
|
|
// SecurityParameters.server_random +
|
|
|
|
// SecurityParameters.client_random);
|
|
|
|
//
|
|
|
|
// until enough output has been generated. Then, the key_block is
|
|
|
|
// partitioned as follows:
|
|
|
|
//
|
|
|
|
// client_write_MAC_key[SecurityParameters.mac_key_length]
|
|
|
|
// server_write_MAC_key[SecurityParameters.mac_key_length]
|
|
|
|
// client_write_key[SecurityParameters.enc_key_length]
|
|
|
|
// server_write_key[SecurityParameters.enc_key_length]
|
|
|
|
// client_write_IV[SecurityParameters.fixed_iv_length]
|
|
|
|
// server_write_IV[SecurityParameters.fixed_iv_length]
|
|
|
|
{
|
|
|
|
uint8_t tmp64[64];
|
2017-01-19 01:07:24 +05:30
|
|
|
|
|
|
|
/* make "server_rand32 + client_rand32" */
|
2017-01-23 05:38:16 +05:30
|
|
|
memcpy(&tmp64[0] , &tls->hsd->client_and_server_rand32[32], 32);
|
|
|
|
memcpy(&tmp64[32], &tls->hsd->client_and_server_rand32[0] , 32);
|
2017-01-17 21:23:36 +05:30
|
|
|
|
2017-01-24 22:38:07 +05:30
|
|
|
prf_hmac_sha256(/*tls,*/
|
2017-01-24 20:30:54 +05:30
|
|
|
tls->client_write_MAC_key, 2 * (tls->MAC_size + tls->key_size),
|
2017-01-19 01:07:24 +05:30
|
|
|
// also fills:
|
2017-01-24 20:30:54 +05:30
|
|
|
// server_write_MAC_key[]
|
|
|
|
// client_write_key[]
|
|
|
|
// server_write_key[]
|
2017-01-23 05:38:16 +05:30
|
|
|
tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
|
2017-01-17 21:23:36 +05:30
|
|
|
"key expansion",
|
|
|
|
tmp64, 64
|
|
|
|
);
|
2017-01-24 20:30:54 +05:30
|
|
|
tls->client_write_key = tls->client_write_MAC_key + (2 * tls->MAC_size);
|
|
|
|
tls->server_write_key = tls->client_write_key + tls->key_size;
|
2017-01-17 21:23:36 +05:30
|
|
|
dump_hex("client_write_MAC_key:%s\n",
|
2017-01-24 20:30:54 +05:30
|
|
|
tls->client_write_MAC_key, tls->MAC_size
|
2017-01-17 21:23:36 +05:30
|
|
|
);
|
2017-01-19 01:07:24 +05:30
|
|
|
dump_hex("client_write_key:%s\n",
|
2017-01-24 20:30:54 +05:30
|
|
|
tls->client_write_key, tls->key_size
|
2017-01-19 01:07:24 +05:30
|
|
|
);
|
2017-01-17 21:23:36 +05:30
|
|
|
}
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-17 21:54:11 +05:30
|
|
|
static const uint8_t rec_CHANGE_CIPHER_SPEC[] = {
|
|
|
|
RECORD_TYPE_CHANGE_CIPHER_SPEC, TLS_MAJ, TLS_MIN, 00, 01,
|
|
|
|
01
|
|
|
|
};
|
|
|
|
|
2017-01-15 06:47:03 +05:30
|
|
|
static void send_change_cipher_spec(tls_state_t *tls)
|
|
|
|
{
|
2017-01-17 21:23:36 +05:30
|
|
|
dbg(">> CHANGE_CIPHER_SPEC\n");
|
2017-01-23 05:38:16 +05:30
|
|
|
xwrite(tls->ofd, rec_CHANGE_CIPHER_SPEC, sizeof(rec_CHANGE_CIPHER_SPEC));
|
2017-01-15 06:47:03 +05:30
|
|
|
}
|
|
|
|
|
2017-01-15 18:46:51 +05:30
|
|
|
// 7.4.9. Finished
|
|
|
|
// A Finished message is always sent immediately after a change
|
|
|
|
// cipher spec message to verify that the key exchange and
|
|
|
|
// authentication processes were successful. It is essential that a
|
|
|
|
// change cipher spec message be received between the other handshake
|
|
|
|
// messages and the Finished message.
|
|
|
|
//...
|
|
|
|
// The Finished message is the first one protected with the just
|
|
|
|
// negotiated algorithms, keys, and secrets. Recipients of Finished
|
|
|
|
// messages MUST verify that the contents are correct. Once a side
|
|
|
|
// has sent its Finished message and received and validated the
|
|
|
|
// Finished message from its peer, it may begin to send and receive
|
|
|
|
// application data over the connection.
|
|
|
|
//...
|
|
|
|
// struct {
|
|
|
|
// opaque verify_data[verify_data_length];
|
|
|
|
// } Finished;
|
|
|
|
//
|
|
|
|
// verify_data
|
|
|
|
// PRF(master_secret, finished_label, Hash(handshake_messages))
|
|
|
|
// [0..verify_data_length-1];
|
|
|
|
//
|
|
|
|
// finished_label
|
|
|
|
// For Finished messages sent by the client, the string
|
|
|
|
// "client finished". For Finished messages sent by the server,
|
|
|
|
// the string "server finished".
|
|
|
|
//
|
|
|
|
// Hash denotes a Hash of the handshake messages. For the PRF
|
|
|
|
// defined in Section 5, the Hash MUST be the Hash used as the basis
|
|
|
|
// for the PRF. Any cipher suite which defines a different PRF MUST
|
|
|
|
// also define the Hash to use in the Finished computation.
|
|
|
|
//
|
|
|
|
// In previous versions of TLS, the verify_data was always 12 octets
|
|
|
|
// long. In the current version of TLS, it depends on the cipher
|
|
|
|
// suite. Any cipher suite which does not explicitly specify
|
|
|
|
// verify_data_length has a verify_data_length equal to 12. This
|
|
|
|
// includes all existing cipher suites.
|
2017-01-17 21:23:36 +05:30
|
|
|
static void send_client_finished(tls_state_t *tls)
|
|
|
|
{
|
2017-01-18 11:15:50 +05:30
|
|
|
struct finished {
|
2017-01-16 08:55:01 +05:30
|
|
|
uint8_t type;
|
|
|
|
uint8_t len24_hi, len24_mid, len24_lo;
|
|
|
|
uint8_t prf_result[12];
|
|
|
|
};
|
2017-01-20 07:45:09 +05:30
|
|
|
struct finished *record = tls_get_outbuf(tls, sizeof(*record));
|
2017-01-24 20:30:54 +05:30
|
|
|
uint8_t handshake_hash[TLS_MAX_MAC_SIZE];
|
|
|
|
unsigned len;
|
2017-01-16 08:55:01 +05:30
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record));
|
2017-01-17 22:47:27 +05:30
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
len = get_handshake_hash(tls, handshake_hash);
|
2017-01-24 22:38:07 +05:30
|
|
|
prf_hmac_sha256(/*tls,*/
|
2017-01-24 20:30:54 +05:30
|
|
|
record->prf_result, sizeof(record->prf_result),
|
|
|
|
tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
|
|
|
|
"client finished",
|
|
|
|
handshake_hash, len
|
2017-01-16 08:55:01 +05:30
|
|
|
);
|
2017-01-23 05:38:16 +05:30
|
|
|
dump_hex("from secret: %s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
|
2017-01-17 21:23:36 +05:30
|
|
|
dump_hex("from labelSeed: %s", "client finished", sizeof("client finished")-1);
|
2017-01-18 11:15:50 +05:30
|
|
|
dump_hex("%s\n", handshake_hash, sizeof(handshake_hash));
|
2017-01-20 07:45:09 +05:30
|
|
|
dump_hex("=> digest: %s\n", record->prf_result, sizeof(record->prf_result));
|
2017-01-17 03:56:33 +05:30
|
|
|
|
2017-01-18 11:15:50 +05:30
|
|
|
dbg(">> FINISHED\n");
|
2017-01-20 07:45:09 +05:30
|
|
|
xwrite_encrypted(tls, sizeof(*record), RECORD_TYPE_HANDSHAKE);
|
2017-01-15 18:46:51 +05:30
|
|
|
}
|
|
|
|
|
2017-01-23 05:38:16 +05:30
|
|
|
void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni)
|
2017-01-14 17:19:32 +05:30
|
|
|
{
|
|
|
|
// Client RFC 5246 Server
|
|
|
|
// (*) - optional messages, not always sent
|
|
|
|
//
|
|
|
|
// ClientHello ------->
|
|
|
|
// ServerHello
|
|
|
|
// Certificate*
|
|
|
|
// ServerKeyExchange*
|
|
|
|
// CertificateRequest*
|
|
|
|
// <------- ServerHelloDone
|
|
|
|
// Certificate*
|
|
|
|
// ClientKeyExchange
|
|
|
|
// CertificateVerify*
|
|
|
|
// [ChangeCipherSpec]
|
|
|
|
// Finished ------->
|
|
|
|
// [ChangeCipherSpec]
|
|
|
|
// <------- Finished
|
|
|
|
// Application Data <------> Application Data
|
|
|
|
int len;
|
|
|
|
|
2017-01-24 20:30:54 +05:30
|
|
|
send_client_hello_and_alloc_hsd(tls, sni);
|
2017-01-15 18:46:51 +05:30
|
|
|
get_server_hello(tls);
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
// RFC 5246
|
2017-01-14 17:19:32 +05:30
|
|
|
// The server MUST send a Certificate message whenever the agreed-
|
|
|
|
// upon key exchange method uses certificates for authentication
|
|
|
|
// (this includes all key exchange methods defined in this document
|
|
|
|
// except DH_anon). This message will always immediately follow the
|
|
|
|
// ServerHello message.
|
|
|
|
//
|
|
|
|
// IOW: in practice, Certificate *always* follows.
|
|
|
|
// (for example, kernel.org does not even accept DH_anon cipher id)
|
2017-01-15 18:46:51 +05:30
|
|
|
get_server_cert(tls);
|
2017-01-14 17:19:32 +05:30
|
|
|
|
2017-01-21 03:18:41 +05:30
|
|
|
len = tls_xread_handshake_block(tls, 4);
|
2017-01-20 18:42:10 +05:30
|
|
|
if (tls->inbuf[RECHDR_LEN] == HANDSHAKE_SERVER_KEY_EXCHANGE) {
|
2017-01-14 18:27:16 +05:30
|
|
|
// 459 bytes:
|
|
|
|
// 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...
|
|
|
|
//SvKey len=455^
|
tls: format and send CLIENT_KEY_EXCHANGE
$ ./busybox tls kernel.org
insize:0 tail:0
got block len:74
got HANDSHAKE
got SERVER_HELLO
insize:79 tail:4265
got block len:4392
got HANDSHAKE
got CERTIFICATE
entered der @0x8b217a7:0x30 len:1452 inner_byte @0x8b217ab:0x30
entered der @0x8b217ab:0x30 len:1172 inner_byte @0x8b217af:0xa0
skipped der 0xa0, next byte 0x02
skipped der 0x02, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
skipped der 0x30, next byte 0x30
entered der @0x8b218b4:0x30 len:418 inner_byte @0x8b218b8:0x30
skipped der 0x30, next byte 0x03
entered der @0x8b218c7:0x03 len:399 inner_byte @0x8b218cb:0x00
key bytes:399, first:0x00
entered der @0x8b218cc:0x30 len:394 inner_byte @0x8b218d0:0x02
binary bytes:385, first:0x00
skipped der 0x02, next byte 0x02
binary bytes:3, first:0x01
server_rsa_pub_key.size:384
insize:4397 tail:9
got block len:4
got SERVER_HELLO_DONE
insize:9 tail:0
^C
Next step: send CHANGE_CIPHER_SPEC... and actually implement it.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-01-15 04:42:42 +05:30
|
|
|
// with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes:
|
|
|
|
// 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...
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len);
|
|
|
|
//probably need to save it
|
2017-01-24 21:36:10 +05:30
|
|
|
len = tls_xread_handshake_block(tls, 4);
|
2017-01-15 06:47:03 +05:30
|
|
|
}
|
2017-01-17 22:47:27 +05:30
|
|
|
|
2017-01-24 21:36:10 +05:30
|
|
|
if (tls->inbuf[RECHDR_LEN] == HANDSHAKE_CERTIFICATE_REQUEST) {
|
|
|
|
dbg("<< CERTIFICATE_REQUEST\n");
|
|
|
|
// RFC 5246: "If no suitable certificate is available,
|
|
|
|
// the client MUST send a certificate message containing no
|
|
|
|
// certificates. That is, the certificate_list structure has a
|
|
|
|
// length of zero. ...
|
|
|
|
// Client certificates are sent using the Certificate structure
|
|
|
|
// defined in Section 7.4.2."
|
|
|
|
// (i.e. the same format as server certs)
|
|
|
|
send_empty_client_cert(tls);
|
|
|
|
len = tls_xread_handshake_block(tls, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tls->inbuf[RECHDR_LEN] != HANDSHAKE_SERVER_HELLO_DONE) {
|
|
|
|
bad_record_die(tls, "'server hello done'", len);
|
|
|
|
}
|
2017-01-17 21:54:11 +05:30
|
|
|
// 0e 000000 (len:0)
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< SERVER_HELLO_DONE\n");
|
2017-01-17 21:54:11 +05:30
|
|
|
|
|
|
|
send_client_key_exchange(tls);
|
|
|
|
|
|
|
|
send_change_cipher_spec(tls);
|
2017-01-19 04:50:45 +05:30
|
|
|
/* from now on we should send encrypted */
|
|
|
|
/* tls->write_seq64_be = 0; - already is */
|
|
|
|
tls->encrypt_on_write = 1;
|
2017-01-17 21:54:11 +05:30
|
|
|
|
|
|
|
send_client_finished(tls);
|
|
|
|
|
|
|
|
/* Get CHANGE_CIPHER_SPEC */
|
2017-01-20 23:41:14 +05:30
|
|
|
len = tls_xread_record(tls);
|
2017-01-17 21:54:11 +05:30
|
|
|
if (len != 1 || memcmp(tls->inbuf, rec_CHANGE_CIPHER_SPEC, 6) != 0)
|
2017-01-24 21:36:10 +05:30
|
|
|
bad_record_die(tls, "switch to encrypted traffic", len);
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< CHANGE_CIPHER_SPEC\n");
|
2017-01-24 20:30:54 +05:30
|
|
|
if (tls->cipher_id == TLS_RSA_WITH_NULL_SHA256)
|
|
|
|
tls->min_encrypted_len_on_read = tls->MAC_size;
|
2017-01-24 22:38:07 +05:30
|
|
|
else {
|
|
|
|
unsigned mac_blocks = (unsigned)(tls->MAC_size + AES_BLOCKSIZE-1) / AES_BLOCKSIZE;
|
|
|
|
/* all incoming packets now should be encrypted and have
|
|
|
|
* at least IV + (MAC padded to blocksize):
|
|
|
|
*/
|
|
|
|
tls->min_encrypted_len_on_read = AES_BLOCKSIZE + (mac_blocks * AES_BLOCKSIZE);
|
|
|
|
dbg("min_encrypted_len_on_read: %u", tls->min_encrypted_len_on_read);
|
|
|
|
}
|
2017-01-17 21:54:11 +05:30
|
|
|
|
|
|
|
/* Get (encrypted) FINISHED from the server */
|
2017-01-20 23:41:14 +05:30
|
|
|
len = tls_xread_record(tls);
|
2017-01-20 18:42:10 +05:30
|
|
|
if (len < 4 || tls->inbuf[RECHDR_LEN] != HANDSHAKE_FINISHED)
|
2017-01-17 21:54:11 +05:30
|
|
|
tls_error_die(tls);
|
2017-01-17 22:47:27 +05:30
|
|
|
dbg("<< FINISHED\n");
|
2017-01-17 21:54:11 +05:30
|
|
|
/* application data can be sent/received */
|
2017-01-23 05:38:16 +05:30
|
|
|
|
|
|
|
/* free handshake data */
|
|
|
|
// if (PARANOIA)
|
2017-01-24 20:30:54 +05:30
|
|
|
// memset(tls->hsd, 0, tls->hsd->hsd_size);
|
2017-01-23 05:38:16 +05:30
|
|
|
free(tls->hsd);
|
|
|
|
tls->hsd = NULL;
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|
|
|
|
|
2017-01-20 07:45:09 +05:30
|
|
|
static void tls_xwrite(tls_state_t *tls, int len)
|
|
|
|
{
|
|
|
|
dbg(">> DATA\n");
|
|
|
|
xwrite_encrypted(tls, len, RECORD_TYPE_APPLICATION_DATA);
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:55:01 +05:30
|
|
|
// To run a test server using openssl:
|
2017-01-19 04:50:45 +05:30
|
|
|
// openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
|
2017-01-16 08:55:01 +05:30
|
|
|
// openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1
|
2017-01-19 04:50:45 +05:30
|
|
|
//
|
|
|
|
// Unencryped SHA256 example:
|
2017-01-16 08:55:01 +05:30
|
|
|
// openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
|
2017-01-19 04:50:45 +05:30
|
|
|
// openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL
|
|
|
|
// openssl s_client -connect 127.0.0.1:4433 -debug -tls1_2 -no_tls1 -no_tls1_1 -cipher NULL-SHA256
|
2017-01-16 08:55:01 +05:30
|
|
|
|
2017-01-23 05:38:16 +05:30
|
|
|
void FAST_FUNC tls_run_copy_loop(tls_state_t *tls)
|
2017-01-14 17:19:32 +05:30
|
|
|
{
|
2017-01-20 23:41:14 +05:30
|
|
|
fd_set readfds;
|
|
|
|
int inbuf_size;
|
|
|
|
const int INBUF_STEP = 4 * 1024;
|
|
|
|
|
2017-01-23 05:38:16 +05:30
|
|
|
//TODO: convert to poll
|
|
|
|
/* Select loop copying stdin to ofd, and ifd to stdout */
|
2017-01-20 07:45:09 +05:30
|
|
|
FD_ZERO(&readfds);
|
2017-01-23 05:38:16 +05:30
|
|
|
FD_SET(tls->ifd, &readfds);
|
2017-01-20 07:45:09 +05:30
|
|
|
FD_SET(STDIN_FILENO, &readfds);
|
|
|
|
|
2017-01-20 23:41:14 +05:30
|
|
|
inbuf_size = INBUF_STEP;
|
2017-01-20 07:45:09 +05:30
|
|
|
for (;;) {
|
2017-01-20 23:41:14 +05:30
|
|
|
fd_set testfds;
|
2017-01-20 07:45:09 +05:30
|
|
|
int nread;
|
|
|
|
|
|
|
|
testfds = readfds;
|
2017-01-23 05:38:16 +05:30
|
|
|
if (select(tls->ifd + 1, &testfds, NULL, NULL, NULL) < 0)
|
2017-01-20 07:45:09 +05:30
|
|
|
bb_perror_msg_and_die("select");
|
|
|
|
|
|
|
|
if (FD_ISSET(STDIN_FILENO, &testfds)) {
|
2017-01-20 18:42:10 +05:30
|
|
|
void *buf;
|
|
|
|
|
|
|
|
dbg("STDIN HAS DATA\n");
|
2017-01-20 23:41:14 +05:30
|
|
|
buf = tls_get_outbuf(tls, inbuf_size);
|
|
|
|
nread = safe_read(STDIN_FILENO, buf, inbuf_size);
|
2017-01-20 07:45:09 +05:30
|
|
|
if (nread < 1) {
|
2017-01-20 23:41:14 +05:30
|
|
|
/* We'd want to do this: */
|
2017-01-20 07:45:09 +05:30
|
|
|
/* Close outgoing half-connection so they get EOF,
|
2017-01-20 23:41:14 +05:30
|
|
|
* but leave incoming alone so we can see response
|
|
|
|
*/
|
2017-01-23 05:38:16 +05:30
|
|
|
//shutdown(tls->ofd, SHUT_WR);
|
2017-01-20 23:41:14 +05:30
|
|
|
/* But TLS has no way to encode this,
|
|
|
|
* doubt it's ok to do it "raw"
|
|
|
|
*/
|
2017-01-20 07:45:09 +05:30
|
|
|
FD_CLR(STDIN_FILENO, &readfds);
|
2017-01-21 00:57:06 +05:30
|
|
|
tls_free_outbuf(tls); /* mem usage optimization */
|
2017-01-20 23:41:14 +05:30
|
|
|
} else {
|
|
|
|
if (nread == inbuf_size) {
|
|
|
|
/* TLS has per record overhead, if input comes fast,
|
|
|
|
* read, encrypt and send bigger chunks
|
|
|
|
*/
|
|
|
|
inbuf_size += INBUF_STEP;
|
2017-01-24 20:30:54 +05:30
|
|
|
if (inbuf_size > TLS_MAX_OUTBUF)
|
|
|
|
inbuf_size = TLS_MAX_OUTBUF;
|
2017-01-20 23:41:14 +05:30
|
|
|
}
|
|
|
|
tls_xwrite(tls, nread);
|
2017-01-20 07:45:09 +05:30
|
|
|
}
|
|
|
|
}
|
2017-01-23 05:38:16 +05:30
|
|
|
if (FD_ISSET(tls->ifd, &testfds)) {
|
2017-01-20 18:42:10 +05:30
|
|
|
dbg("NETWORK HAS DATA\n");
|
2017-01-20 23:41:14 +05:30
|
|
|
read_record:
|
|
|
|
nread = tls_xread_record(tls);
|
|
|
|
if (nread < 1) {
|
|
|
|
/* TLS protocol has no real concept of one-sided shutdowns:
|
|
|
|
* if we get "TLS EOF" from the peer, writes will fail too
|
|
|
|
*/
|
2017-01-23 05:38:16 +05:30
|
|
|
//FD_CLR(tls->ifd, &readfds);
|
2017-01-20 23:41:14 +05:30
|
|
|
//close(STDOUT_FILENO);
|
2017-01-21 00:57:06 +05:30
|
|
|
//tls_free_inbuf(tls); /* mem usage optimization */
|
2017-01-20 23:41:14 +05:30
|
|
|
//continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tls->inbuf[0] != RECORD_TYPE_APPLICATION_DATA)
|
|
|
|
bb_error_msg_and_die("unexpected record type %d", tls->inbuf[0]);
|
2017-01-20 18:42:10 +05:30
|
|
|
xwrite(STDOUT_FILENO, tls->inbuf + RECHDR_LEN, nread);
|
2017-01-20 23:41:14 +05:30
|
|
|
/* We may already have a complete next record buffered,
|
|
|
|
* can process it without network reads (and possible blocking)
|
|
|
|
*/
|
|
|
|
if (tls_has_buffered_record(tls))
|
|
|
|
goto read_record;
|
2017-01-20 07:45:09 +05:30
|
|
|
}
|
|
|
|
}
|
2017-01-14 17:19:32 +05:30
|
|
|
}
|