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