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