blob: 5566d791159af3e7a55f3c8d3c5c9d07f63015d0 [file] [log] [blame]
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001/*
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002 * Copyright (C) 2017 Denys Vlasenko
Denys Vlasenko11d00962017-01-15 00:12:42 +01003 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01005 */
6//config:config TLS
Denys Vlasenko9a647c32017-01-23 01:08:16 +01007//config: bool #No description makes it a hidden option
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01008//config: default n
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01009//Note:
10//Config.src also defines FEATURE_TLS_SHA1 option
Denys Vlasenkoceff6b02017-01-14 12:49:32 +010011
Denys Vlasenkoceff6b02017-01-14 12:49:32 +010012//kbuild:lib-$(CONFIG_TLS) += tls.o
Denys Vlasenko11d00962017-01-15 00:12:42 +010013//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 Vlasenkob7e9ae62017-01-18 17:20:27 +010017//kbuild:lib-$(CONFIG_TLS) += tls_aes.o
Denys Vlasenko83e5c622018-11-23 17:21:38 +010018//kbuild:lib-$(CONFIG_TLS) += tls_aesgcm.o
Denys Vlasenkobddb6542018-11-13 02:16:24 +010019//kbuild:lib-$(CONFIG_TLS) += tls_rsa.o
20//kbuild:lib-$(CONFIG_TLS) += tls_fe.o
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020021//kbuild:lib-$(CONFIG_TLS) += tls_sp_c32.o
Denys Vlasenkoceff6b02017-01-14 12:49:32 +010022
Denys Vlasenko11d00962017-01-15 00:12:42 +010023#include "tls.h"
Denys Vlasenkoceff6b02017-01-14 12:49:32 +010024
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +010025// works against "openssl s_server -cipher NULL"
26// and against wolfssl-3.9.10-stable/examples/server/server.c:
27#define ALLOW_RSA_NULL_SHA256 0 // for testing (does everything except encrypting)
28
Denys Vlasenko83e5c622018-11-23 17:21:38 +010029//Tested against kernel.org:
Denys Vlasenko49ecee02017-01-24 16:00:54 +010030//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA // ok, recvs SERVER_KEY_EXCHANGE *** matrixssl uses this on my box
31//#define CIPHER_ID TLS_RSA_WITH_AES_256_CBC_SHA256 // ok, no SERVER_KEY_EXCHANGE
32//#define CIPHER_ID TLS_DH_anon_WITH_AES_256_CBC_SHA // SSL_ALERT_HANDSHAKE_FAILURE
33//^^^^^^^^^^^^^^^^^^^^^^^ (tested b/c this one doesn't req server certs... no luck, server refuses it)
34//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 // SSL_ALERT_HANDSHAKE_FAILURE
35//#define CIPHER_ID TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
36//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 // ok, recvs SERVER_KEY_EXCHANGE
37//#define CIPHER_ID TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
38//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
39//#define CIPHER_ID TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
40//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
41//#define CIPHER_ID TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 // SSL_ALERT_HANDSHAKE_FAILURE
42//#define CIPHER_ID TLS_RSA_WITH_AES_256_GCM_SHA384 // ok, no SERVER_KEY_EXCHANGE
Denys Vlasenko83e5c622018-11-23 17:21:38 +010043//#define CIPHER_ID TLS_RSA_WITH_AES_128_GCM_SHA256 // ok, no SERVER_KEY_EXCHANGE
Denys Vlasenko49ecee02017-01-24 16:00:54 +010044
Denys Vlasenko49ecee02017-01-24 16:00:54 +010045// works against wolfssl-3.9.10-stable/examples/server/server.c
46// works for kernel.org
47// does not work for cdn.kernel.org (e.g. downloading an actual tarball, not a web page)
48// getting alert 40 "handshake failure" at once
49// with GNU Wget 1.18, they agree on TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xC02F) cipher
Denys Vlasenko2eb04292018-11-26 16:39:19 +010050// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -cipher AES256-SHA256
51// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -cipher AES256-GCM-SHA384
52// fail: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -cipher AES128-SHA256
53// ok: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -cipher AES128-GCM-SHA256
54// ok: openssl s_client -connect cdn.kernel.org:443 -debug -tls1_2 -cipher AES128-SHA
Denys Vlasenko49ecee02017-01-24 16:00:54 +010055// (TLS_RSA_WITH_AES_128_CBC_SHA - in TLS 1.2 it's mandated to be always supported)
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +010056//#define CIPHER_ID1 TLS_RSA_WITH_AES_256_CBC_SHA256 //0x003D
Denys Vlasenko89193f92017-01-24 18:08:07 +010057// Works with "wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.5.tar.xz"
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +010058//#define CIPHER_ID2 TLS_RSA_WITH_AES_128_CBC_SHA //0x002F
Denys Vlasenko49ecee02017-01-24 16:00:54 +010059
Denys Vlasenkod2923b32018-11-24 21:26:20 +010060// bug #11456:
Denys Vlasenko83e5c622018-11-23 17:21:38 +010061// ftp.openbsd.org only supports ECDHE-RSA-AESnnn-GCM-SHAnnn or ECDHE-RSA-CHACHA20-POLY1305
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +010062//#define CIPHER_ID3 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 //0xC02F
Denys Vlasenkoab3c5e42018-11-25 00:53:19 +010063// host is.gd accepts only ECDHE-ECDSA-foo (the simplest which works: ECDHE-ECDSA-AES128-SHA 0xC009)
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +010064//#define CIPHER_ID4 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA //0xC009
Denys Vlasenko83e5c622018-11-23 17:21:38 +010065
Denys Vlasenko49ecee02017-01-24 16:00:54 +010066
Denys Vlasenko89193f92017-01-24 18:08:07 +010067#define TLS_DEBUG 0
68#define TLS_DEBUG_HASH 0
69#define TLS_DEBUG_DER 0
70#define TLS_DEBUG_FIXED_SECRETS 0
Denys Vlasenkob5bf1912017-01-23 16:12:17 +010071#if 0
72# define dump_raw_out(...) dump_hex(__VA_ARGS__)
73#else
74# define dump_raw_out(...) ((void)0)
75#endif
76#if 0
77# define dump_raw_in(...) dump_hex(__VA_ARGS__)
78#else
79# define dump_raw_in(...) ((void)0)
80#endif
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +010081
82#if TLS_DEBUG
Denys Vlasenkoceff6b02017-01-14 12:49:32 +010083# define dbg(...) fprintf(stderr, __VA_ARGS__)
84#else
85# define dbg(...) ((void)0)
86#endif
87
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +010088#if TLS_DEBUG_DER
89# define dbg_der(...) fprintf(stderr, __VA_ARGS__)
90#else
91# define dbg_der(...) ((void)0)
92#endif
93
Denys Vlasenkoa33b0082018-11-25 14:28:32 +010094
95//TLS 1.2
96#define TLS_MAJ 3
97#define TLS_MIN 3
98
Denys Vlasenko98066662018-02-06 13:33:00 +010099#define RECORD_TYPE_CHANGE_CIPHER_SPEC 20 /* 0x14 */
100#define RECORD_TYPE_ALERT 21 /* 0x15 */
101#define RECORD_TYPE_HANDSHAKE 22 /* 0x16 */
102#define RECORD_TYPE_APPLICATION_DATA 23 /* 0x17 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100103
Denys Vlasenko98066662018-02-06 13:33:00 +0100104#define HANDSHAKE_HELLO_REQUEST 0 /* 0x00 */
105#define HANDSHAKE_CLIENT_HELLO 1 /* 0x01 */
106#define HANDSHAKE_SERVER_HELLO 2 /* 0x02 */
107#define HANDSHAKE_HELLO_VERIFY_REQUEST 3 /* 0x03 */
108#define HANDSHAKE_NEW_SESSION_TICKET 4 /* 0x04 */
109#define HANDSHAKE_CERTIFICATE 11 /* 0x0b */
110#define HANDSHAKE_SERVER_KEY_EXCHANGE 12 /* 0x0c */
111#define HANDSHAKE_CERTIFICATE_REQUEST 13 /* 0x0d */
112#define HANDSHAKE_SERVER_HELLO_DONE 14 /* 0x0e */
113#define HANDSHAKE_CERTIFICATE_VERIFY 15 /* 0x0f */
114#define HANDSHAKE_CLIENT_KEY_EXCHANGE 16 /* 0x10 */
115#define HANDSHAKE_FINISHED 20 /* 0x14 */
Denys Vlasenko11d00962017-01-15 00:12:42 +0100116
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100117#define TLS_EMPTY_RENEGOTIATION_INFO_SCSV 0x00FF /* not a real cipher id... */
118
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100119#define SSL_NULL_WITH_NULL_NULL 0x0000
120#define SSL_RSA_WITH_NULL_MD5 0x0001
121#define SSL_RSA_WITH_NULL_SHA 0x0002
122#define SSL_RSA_WITH_RC4_128_MD5 0x0004
123#define SSL_RSA_WITH_RC4_128_SHA 0x0005
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100124#define TLS_RSA_WITH_IDEA_CBC_SHA 0x0007 /* 7 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100125#define SSL_RSA_WITH_3DES_EDE_CBC_SHA 0x000A /* 10 */
126
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100127#define SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA 0x0016 /* 22 */
128#define SSL_DH_anon_WITH_RC4_128_MD5 0x0018 /* 24 */
129#define SSL_DH_anon_WITH_3DES_EDE_CBC_SHA 0x001B /* 27 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100130#define TLS_RSA_WITH_AES_128_CBC_SHA 0x002F /*SSLv3 Kx=RSA Au=RSA Enc=AES(128) Mac=SHA1 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100131#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA 0x0033 /* 51 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100132#define TLS_DH_anon_WITH_AES_128_CBC_SHA 0x0034 /* 52 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100133#define TLS_RSA_WITH_AES_256_CBC_SHA 0x0035 /* 53 */
134#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA 0x0039 /* 57 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100135#define TLS_DH_anon_WITH_AES_256_CBC_SHA 0x003A /* 58 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100136#define TLS_RSA_WITH_NULL_SHA256 0x003B /* 59 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100137#define TLS_RSA_WITH_AES_128_CBC_SHA256 0x003C /* 60 */
138#define TLS_RSA_WITH_AES_256_CBC_SHA256 0x003D /* 61 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100139#define TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 0x0067 /* 103 */
140#define TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 0x006B /* 107 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100141#define TLS_PSK_WITH_AES_128_CBC_SHA 0x008C /* 140 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100142#define TLS_PSK_WITH_AES_256_CBC_SHA 0x008D /* 141 */
143#define TLS_DHE_PSK_WITH_AES_128_CBC_SHA 0x0090 /* 144 */
144#define TLS_DHE_PSK_WITH_AES_256_CBC_SHA 0x0091 /* 145 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100145#define TLS_RSA_WITH_SEED_CBC_SHA 0x0096 /* 150 */
Denys Vlasenko330d7f52018-11-25 17:27:48 +0100146#define TLS_RSA_WITH_AES_128_GCM_SHA256 0x009C /*TLSv1.2 Kx=RSA Au=RSA Enc=AESGCM(128) Mac=AEAD */
147#define TLS_RSA_WITH_AES_256_GCM_SHA384 0x009D /*TLSv1.2 Kx=RSA Au=RSA Enc=AESGCM(256) Mac=AEAD */
148#define TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 0x009E /*TLSv1.2 Kx=DH Au=RSA Enc=AESGCM(128) Mac=AEAD */
149#define TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 0x009F /*TLSv1.2 Kx=DH Au=RSA Enc=AESGCM(256) Mac=AEAD */
Denys Vlasenko8a46c742018-11-26 17:33:17 +0100150#define TLS_DH_anon_WITH_AES_128_GCM_SHA256 0x00A6 /* RFC 5288 */
151#define TLS_DH_anon_WITH_AES_256_GCM_SHA384 0x00A7 /* RFC 5288 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100152#define TLS_PSK_WITH_AES_128_CBC_SHA256 0x00AE /* 174 */
153#define TLS_PSK_WITH_AES_256_CBC_SHA384 0x00AF /* 175 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100154#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA 0xC004 /* 49156 */
155#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA 0xC005 /* 49157 */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100156#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA 0xC009 /*TLSv1 Kx=ECDH Au=ECDSA Enc=AES(128) Mac=SHA1 */
157#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA 0xC00A /*TLSv1 Kx=ECDH Au=ECDSA Enc=AES(256) Mac=SHA1 */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100158#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA 0xC00E /* 49166 */
159#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA 0xC00F /* 49167 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100160#define TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA 0xC012 /* 49170 */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100161#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA 0xC013 /*TLSv1 Kx=ECDH Au=RSA Enc=AES(128) Mac=SHA1 */
162#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA 0xC014 /*TLSv1 Kx=ECDH Au=RSA Enc=AES(256) Mac=SHA1 */
Denys Vlasenko8a46c742018-11-26 17:33:17 +0100163#define TLS_ECDH_anon_WITH_AES_128_CBC_SHA 0xC018 /* RFC 4492 */
164#define TLS_ECDH_anon_WITH_AES_256_CBC_SHA 0xC019 /* RFC 4492 */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100165#define TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 0xC023 /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AES(128) Mac=SHA256 */
166#define TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 0xC024 /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AES(256) Mac=SHA384 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100167#define TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 0xC025 /* 49189 */
168#define TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 0xC026 /* 49190 */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100169#define TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 0xC027 /*TLSv1.2 Kx=ECDH Au=RSA Enc=AES(128) Mac=SHA256 */
170#define TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 0xC028 /*TLSv1.2 Kx=ECDH Au=RSA Enc=AES(256) Mac=SHA384 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100171#define TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 0xC029 /* 49193 */
172#define TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 0xC02A /* 49194 */
Denys Vlasenko7a18b952017-01-23 16:37:04 +0100173/* RFC 5288 "AES Galois Counter Mode (GCM) Cipher Suites for TLS" */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100174#define TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 0xC02B /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESGCM(128) Mac=AEAD */
175#define TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 0xC02C /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESGCM(256) Mac=AEAD */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100176#define TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 0xC02D /* 49197 */
177#define TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 0xC02E /* 49198 */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100178#define TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 0xC02F /*TLSv1.2 Kx=ECDH Au=RSA Enc=AESGCM(128) Mac=AEAD */
179#define TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 0xC030 /*TLSv1.2 Kx=ECDH Au=RSA Enc=AESGCM(256) Mac=AEAD */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100180#define TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 0xC031 /* 49201 */
181#define TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 0xC032 /* 49202 */
Denys Vlasenkodffc8ff2018-11-27 10:35:10 +0100182#define TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA 0xC035
183#define TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA 0xC036
184#define TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 0xC037
185#define TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 0xC038
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100186
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100187/* From http://wiki.mozilla.org/Security/Server_Side_TLS */
188/* and 'openssl ciphers -V -stdname' */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100189#define TLS_RSA_WITH_AES_128_CCM 0xC09C /*TLSv1.2 Kx=RSA Au=RSA Enc=AESCCM(128) Mac=AEAD */
190#define TLS_RSA_WITH_AES_256_CCM 0xC09D /*TLSv1.2 Kx=RSA Au=RSA Enc=AESCCM(256) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100191#define TLS_DHE_RSA_WITH_AES_128_CCM 0xC09E /*TLSv1.2 Kx=DH Au=RSA Enc=AESCCM(128) Mac=AEAD */
Denys Vlasenko5df3b122018-11-04 21:25:41 +0100192#define TLS_DHE_RSA_WITH_AES_256_CCM 0xC09F /*TLSv1.2 Kx=DH Au=RSA Enc=AESCCM(256) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100193#define TLS_RSA_WITH_AES_128_CCM_8 0xC0A0 /*TLSv1.2 Kx=RSA Au=RSA Enc=AESCCM8(128) Mac=AEAD */
194#define TLS_RSA_WITH_AES_256_CCM_8 0xC0A1 /*TLSv1.2 Kx=RSA Au=RSA Enc=AESCCM8(256) Mac=AEAD */
195#define TLS_DHE_RSA_WITH_AES_128_CCM_8 0xC0A2 /*TLSv1.2 Kx=DH Au=RSA Enc=AESCCM8(128) Mac=AEAD */
196#define TLS_DHE_RSA_WITH_AES_256_CCM_8 0xC0A3 /*TLSv1.2 Kx=DH Au=RSA Enc=AESCCM8(256) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100197#define TLS_ECDHE_ECDSA_WITH_AES_128_CCM 0xC0AC /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESCCM(128) Mac=AEAD */
198#define TLS_ECDHE_ECDSA_WITH_AES_256_CCM 0xC0AD /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESCCM(256) Mac=AEAD */
199#define TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESCCM8(128) Mac=AEAD */
200#define TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 0xC0AF /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=AESCCM8(256) Mac=AEAD */
Denys Vlasenkoa33b0082018-11-25 14:28:32 +0100201#define TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA8 /*TLSv1.2 Kx=ECDH Au=RSA Enc=CHACHA20/POLY1305(256) Mac=AEAD */
202#define TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 0xCCA9 /*TLSv1.2 Kx=ECDH Au=ECDSA Enc=CHACHA20/POLY1305(256) Mac=AEAD */
203#define TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 0xCCAA /*TLSv1.2 Kx=DH Au=RSA Enc=CHACHA20/POLY1305(256) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100204
Denys Vlasenkob29d0452018-11-04 21:18:29 +0100205#define TLS_AES_128_GCM_SHA256 0x1301 /*TLSv1.3 Kx=any Au=any Enc=AESGCM(128) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100206#define TLS_AES_256_GCM_SHA384 0x1302 /*TLSv1.3 Kx=any Au=any Enc=AESGCM(256) Mac=AEAD */
207#define TLS_CHACHA20_POLY1305_SHA256 0x1303 /*TLSv1.3 Kx=any Au=any Enc=CHACHA20/POLY1305(256) Mac=AEAD */
Denys Vlasenko9b0ce4d2018-11-04 20:53:54 +0100208#define TLS_AES_128_CCM_SHA256 0x1304 /*TLSv1.3 Kx=any Au=any Enc=AESCCM(128) Mac=AEAD */
209
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100210/* Might go to libbb.h */
211#define TLS_MAX_CRYPTBLOCK_SIZE 16
212#define TLS_MAX_OUTBUF (1 << 14)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100213
Denys Vlasenko9a6897a2017-01-16 23:26:33 +0100214enum {
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100215 SHA_INSIZE = 64,
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100216
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100217 AES128_KEYSIZE = 16,
218 AES256_KEYSIZE = 32,
219
Denys Vlasenko38972a82017-01-20 19:11:14 +0100220 RSA_PREMASTER_SIZE = 48,
221
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +0100222 RECHDR_LEN = 5,
223
Denys Vlasenko38972a82017-01-20 19:11:14 +0100224 /* 8 = 3+5. 3 extra bytes result in record data being 32-bit aligned */
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100225 OUTBUF_PFX = 8 + AES_BLOCK_SIZE, /* header + IV */
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100226 OUTBUF_SFX = TLS_MAX_MAC_SIZE + TLS_MAX_CRYPTBLOCK_SIZE, /* MAC + padding */
Denys Vlasenko39161392017-01-20 20:27:06 +0100227
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100228 // RFC 5246:
Denys Vlasenko39161392017-01-20 20:27:06 +0100229 // | 6.2.1. Fragmentation
230 // | The record layer fragments information blocks into TLSPlaintext
231 // | records carrying data in chunks of 2^14 bytes or less. Client
232 // | message boundaries are not preserved in the record layer (i.e.,
233 // | multiple client messages of the same ContentType MAY be coalesced
234 // | into a single TLSPlaintext record, or a single message MAY be
235 // | fragmented across several records)
236 // |...
237 // | length
238 // | The length (in bytes) of the following TLSPlaintext.fragment.
239 // | The length MUST NOT exceed 2^14.
240 // |...
241 // | 6.2.2. Record Compression and Decompression
242 // |...
243 // | Compression must be lossless and may not increase the content length
244 // | by more than 1024 bytes. If the decompression function encounters a
245 // | TLSCompressed.fragment that would decompress to a length in excess of
246 // | 2^14 bytes, it MUST report a fatal decompression failure error.
247 // |...
248 // | length
249 // | The length (in bytes) of the following TLSCompressed.fragment.
250 // | The length MUST NOT exceed 2^14 + 1024.
251 // |...
252 // | 6.2.3. Record Payload Protection
253 // | The encryption and MAC functions translate a TLSCompressed
254 // | structure into a TLSCiphertext. The decryption functions reverse
255 // | the process. The MAC of the record also includes a sequence
256 // | number so that missing, extra, or repeated messages are
257 // | detectable.
258 // |...
259 // | length
260 // | The length (in bytes) of the following TLSCiphertext.fragment.
261 // | The length MUST NOT exceed 2^14 + 2048.
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100262 MAX_INBUF = RECHDR_LEN + (1 << 14) + 2048,
Denys Vlasenkoa33b0082018-11-25 14:28:32 +0100263
264 /* Bits for tls->flags */
265 NEED_EC_KEY = 1 << 0,
266 GOT_CERT_RSA_KEY_ALG = 1 << 1,
267 GOT_CERT_ECDSA_KEY_ALG = 1 << 2, // so far unused
268 GOT_EC_KEY = 1 << 3,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200269 GOT_EC_CURVE_X25519 = 1 << 4, // else P256
270 ENCRYPTION_AESGCM = 1 << 5, // else AES-SHA (or NULL-SHA if ALLOW_RSA_NULL_SHA256=1)
271 ENCRYPT_ON_WRITE = 1 << 6,
Denys Vlasenko9a6897a2017-01-16 23:26:33 +0100272};
273
Denys Vlasenkob1003f72017-01-14 13:57:16 +0100274struct record_hdr {
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100275 uint8_t type;
276 uint8_t proto_maj, proto_min;
277 uint8_t len16_hi, len16_lo;
278};
279
Denys Vlasenko9a647c32017-01-23 01:08:16 +0100280struct tls_handshake_data {
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100281 /* In bbox, md5/sha1/sha256 ctx's are the same structure */
282 md5sha_ctx_t handshake_hash_ctx;
283
Denys Vlasenko7a18b952017-01-23 16:37:04 +0100284 uint8_t client_and_server_rand32[2 * 32];
285 uint8_t master_secret[48];
Denys Vlasenkobddb6542018-11-13 02:16:24 +0100286
Denys Vlasenkodd2577f2017-01-20 22:48:41 +0100287//TODO: store just the DER key here, parse/use/delete it when sending client key
288//this way it will stay key type agnostic here.
Denys Vlasenko11d00962017-01-15 00:12:42 +0100289 psRsaKey_t server_rsa_pub_key;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200290
291 /* peer's elliptic curve key data */
292 /* for x25519, it contains one point in first 32 bytes */
293 /* for P256, it contains x,y point pair, each 32 bytes long */
294 uint8_t ecc_pub_key32[2 * 32];
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100295
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100296/* HANDSHAKE HASH: */
297 //unsigned saved_client_hello_size;
298 //uint8_t saved_client_hello[1];
Denys Vlasenko9a647c32017-01-23 01:08:16 +0100299};
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100300
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100301
302static unsigned get24be(const uint8_t *p)
303{
304 return 0x100*(0x100*p[0] + p[1]) + p[2];
305}
306
307#if TLS_DEBUG
Denys Vlasenkoeb53d012018-11-25 14:45:55 +0100308/* Nondestructively see the current hash value */
Denys Vlasenko838b88c2018-11-25 18:52:47 +0100309# if TLS_DEBUG_HASH
Denys Vlasenkoeb53d012018-11-25 14:45:55 +0100310static unsigned sha_peek(md5sha_ctx_t *ctx, void *buffer)
311{
312 md5sha_ctx_t ctx_copy = *ctx; /* struct copy */
313 return sha_end(&ctx_copy, buffer);
314}
Denys Vlasenko838b88c2018-11-25 18:52:47 +0100315# endif
Denys Vlasenkoeb53d012018-11-25 14:45:55 +0100316
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100317static void dump_hex(const char *fmt, const void *vp, int len)
318{
319 char hexbuf[32 * 1024 + 4];
320 const uint8_t *p = vp;
321
322 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
323 dbg(fmt, hexbuf);
324}
325
326static void dump_tls_record(const void *vp, int len)
327{
328 const uint8_t *p = vp;
329
330 while (len > 0) {
331 unsigned xhdr_len;
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +0100332 if (len < RECHDR_LEN) {
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100333 dump_hex("< |%s|\n", p, len);
334 return;
335 }
336 xhdr_len = 0x100*p[3] + p[4];
337 dbg("< hdr_type:%u ver:%u.%u len:%u", p[0], p[1], p[2], xhdr_len);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +0100338 p += RECHDR_LEN;
339 len -= RECHDR_LEN;
340 if (len >= 4 && p[-RECHDR_LEN] == RECORD_TYPE_HANDSHAKE) {
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100341 unsigned len24 = get24be(p + 1);
342 dbg(" type:%u len24:%u", p[0], len24);
343 }
344 if (xhdr_len > len)
345 xhdr_len = len;
346 dump_hex(" |%s|\n", p, xhdr_len);
347 p += xhdr_len;
348 len -= xhdr_len;
349 }
350}
Denys Vlasenko38972a82017-01-20 19:11:14 +0100351#else
352# define dump_hex(...) ((void)0)
353# define dump_tls_record(...) ((void)0)
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100354#endif
355
Denys Vlasenko624066f2018-11-23 19:24:57 +0100356void FAST_FUNC tls_get_random(void *buf, unsigned len)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100357{
358 if (len != open_read_close("/dev/urandom", buf, len))
359 xfunc_die();
360}
361
Denys Vlasenko941440c2018-11-24 13:51:46 +0100362static void xorbuf3(void *dst, const void *src1, const void *src2, unsigned count)
363{
364 uint8_t *d = dst;
365 const uint8_t *s1 = src1;
366 const uint8_t* s2 = src2;
367 while (count--)
368 *d++ = *s1++ ^ *s2++;
369}
370
371void FAST_FUNC xorbuf(void *dst, const void *src, unsigned count)
372{
373 xorbuf3(dst, dst, src, count);
374}
375
Denys Vlasenko03569bc2018-11-24 14:08:29 +0100376void FAST_FUNC xorbuf_aligned_AES_BLOCK_SIZE(void *dst, const void *src)
377{
378 unsigned long *d = dst;
379 const unsigned long *s = src;
380 d[0] ^= s[0];
381#if ULONG_MAX <= 0xffffffffffffffff
382 d[1] ^= s[1];
383 #if ULONG_MAX == 0xffffffff
384 d[2] ^= s[2];
385 d[3] ^= s[3];
386 #endif
387#endif
388}
389
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100390#if !TLS_DEBUG_HASH
391# define hash_handshake(tls, fmt, buffer, len) \
392 hash_handshake(tls, buffer, len)
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +0100393#endif
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100394static void hash_handshake(tls_state_t *tls, const char *fmt, const void *buffer, unsigned len)
395{
396 md5sha_hash(&tls->hsd->handshake_hash_ctx, buffer, len);
397#if TLS_DEBUG_HASH
398 {
399 uint8_t h[TLS_MAX_MAC_SIZE];
400 dump_hex(fmt, buffer, len);
401 dbg(" (%u bytes) ", (int)len);
402 len = sha_peek(&tls->hsd->handshake_hash_ctx, h);
Denys Vlasenko71fa5b02018-12-10 16:14:58 +0100403 if (ENABLE_FEATURE_TLS_SHA1 && len == SHA1_OUTSIZE)
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100404 dump_hex("sha1:%s\n", h, len);
405 else
406 if (len == SHA256_OUTSIZE)
407 dump_hex("sha256:%s\n", h, len);
408 else
409 dump_hex("sha???:%s\n", h, len);
410 }
411#endif
412}
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +0100413
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +0100414#if !ENABLE_FEATURE_TLS_SHA1
415# define TLS_MAC_SIZE(tls) SHA256_OUTSIZE
416#else
417# define TLS_MAC_SIZE(tls) (tls)->MAC_size
418#endif
419
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100420// RFC 2104:
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100421// HMAC(key, text) based on a hash H (say, sha256) is:
422// ipad = [0x36 x INSIZE]
423// opad = [0x5c x INSIZE]
424// HMAC(key, text) = H((key XOR opad) + H((key XOR ipad) + text))
425//
426// H(key XOR opad) and H(key XOR ipad) can be precomputed
427// if we often need HMAC hmac with the same key.
428//
429// text is often given in disjoint pieces.
Denys Vlasenko89193f92017-01-24 18:08:07 +0100430typedef struct hmac_precomputed {
431 md5sha_ctx_t hashed_key_xor_ipad;
432 md5sha_ctx_t hashed_key_xor_opad;
433} hmac_precomputed_t;
434
Denys Vlasenko636c3b62017-04-03 17:43:44 +0200435typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;
Denys Vlasenko71fa5b02018-12-10 16:14:58 +0100436#if !ENABLE_FEATURE_TLS_SHA1
437#define hmac_begin(pre,key,key_size,begin) \
438 hmac_begin(pre,key,key_size)
439#define begin sha256_begin
440#endif
Denys Vlasenko636c3b62017-04-03 17:43:44 +0200441static void hmac_begin(hmac_precomputed_t *pre, uint8_t *key, unsigned key_size, md5sha_begin_func *begin)
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100442{
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100443 uint8_t key_xor_ipad[SHA_INSIZE];
444 uint8_t key_xor_opad[SHA_INSIZE];
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100445// uint8_t tempkey[SHA1_OUTSIZE < SHA256_OUTSIZE ? SHA256_OUTSIZE : SHA1_OUTSIZE];
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100446 unsigned i;
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100447
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100448 // "The authentication key can be of any length up to INSIZE, the
449 // block length of the hash function. Applications that use keys longer
450 // than INSIZE bytes will first hash the key using H and then use the
451 // resultant OUTSIZE byte string as the actual key to HMAC."
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100452 if (key_size > SHA_INSIZE) {
James Byrne69374872019-07-02 11:35:03 +0200453 bb_simple_error_msg_and_die("HMAC key>64"); //does not happen (yet?)
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100454// md5sha_ctx_t ctx;
455// begin(&ctx);
456// md5sha_hash(&ctx, key, key_size);
457// key_size = sha_end(&ctx, tempkey);
458// //key = tempkey; - right? RIGHT? why does it work without this?
459// // because SHA_INSIZE is 64, but hmac() is always called with
460// // key_size = tls->MAC_size = SHA1/256_OUTSIZE (20 or 32),
461// // and prf_hmac_sha256() -> hmac_sha256() key sizes are:
462// // - RSA_PREMASTER_SIZE is 48
463// // - CURVE25519_KEYSIZE is 32
464// // - master_secret[] is 48
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100465 }
466
467 for (i = 0; i < key_size; i++) {
468 key_xor_ipad[i] = key[i] ^ 0x36;
469 key_xor_opad[i] = key[i] ^ 0x5c;
470 }
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100471 for (; i < SHA_INSIZE; i++) {
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100472 key_xor_ipad[i] = 0x36;
473 key_xor_opad[i] = 0x5c;
474 }
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100475
Denys Vlasenko636c3b62017-04-03 17:43:44 +0200476 begin(&pre->hashed_key_xor_ipad);
477 begin(&pre->hashed_key_xor_opad);
Denys Vlasenko89193f92017-01-24 18:08:07 +0100478 md5sha_hash(&pre->hashed_key_xor_ipad, key_xor_ipad, SHA_INSIZE);
479 md5sha_hash(&pre->hashed_key_xor_opad, key_xor_opad, SHA_INSIZE);
480}
Denys Vlasenko71fa5b02018-12-10 16:14:58 +0100481#undef begin
Denys Vlasenko89193f92017-01-24 18:08:07 +0100482
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100483static unsigned hmac_sha_precomputed_v(
484 hmac_precomputed_t *pre,
485 uint8_t *out,
486 va_list va)
487{
488 uint8_t *text;
489 unsigned len;
490
491 /* pre->hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
492 /* pre->hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
493
494 /* calculate out = H((key XOR ipad) + text) */
495 while ((text = va_arg(va, uint8_t*)) != NULL) {
496 unsigned text_size = va_arg(va, unsigned);
497 md5sha_hash(&pre->hashed_key_xor_ipad, text, text_size);
498 }
499 len = sha_end(&pre->hashed_key_xor_ipad, out);
500
501 /* out = H((key XOR opad) + out) */
502 md5sha_hash(&pre->hashed_key_xor_opad, out, len);
503 return sha_end(&pre->hashed_key_xor_opad, out);
504}
505
506static unsigned hmac_sha_precomputed(hmac_precomputed_t *pre_init, uint8_t *out, ...)
507{
508 hmac_precomputed_t pre;
509 va_list va;
510 unsigned len;
511
512 va_start(va, out);
513 pre = *pre_init; /* struct copy */
514 len = hmac_sha_precomputed_v(&pre, out, va);
515 va_end(va);
516 return len;
517}
518
Denys Vlasenko71fa5b02018-12-10 16:14:58 +0100519#if !ENABLE_FEATURE_TLS_SHA1
520#define hmac(tls,out,key,key_size,...) \
521 hmac(out,key,key_size, __VA_ARGS__)
522#endif
Denys Vlasenko89193f92017-01-24 18:08:07 +0100523static unsigned hmac(tls_state_t *tls, uint8_t *out, uint8_t *key, unsigned key_size, ...)
524{
525 hmac_precomputed_t pre;
526 va_list va;
527 unsigned len;
528
529 va_start(va, key_size);
530
Denys Vlasenko636c3b62017-04-03 17:43:44 +0200531 hmac_begin(&pre, key, key_size,
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +0100532 (ENABLE_FEATURE_TLS_SHA1 && tls->MAC_size == SHA1_OUTSIZE)
533 ? sha1_begin
534 : sha256_begin
Denys Vlasenko636c3b62017-04-03 17:43:44 +0200535 );
Denys Vlasenko89193f92017-01-24 18:08:07 +0100536 len = hmac_sha_precomputed_v(&pre, out, va);
537
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100538 va_end(va);
Denys Vlasenko89193f92017-01-24 18:08:07 +0100539 return len;
540}
541
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100542// RFC 5246:
543// 5. HMAC and the Pseudorandom Function
544//...
545// In this section, we define one PRF, based on HMAC. This PRF with the
546// SHA-256 hash function is used for all cipher suites defined in this
547// document and in TLS documents published prior to this document when
548// TLS 1.2 is negotiated.
Denys Vlasenko89193f92017-01-24 18:08:07 +0100549// ^^^^^^^^^^^^^ IMPORTANT!
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100550// PRF uses sha256 regardless of cipher for all ciphers
551// defined by RFC 5246. It's not sha1 for AES_128_CBC_SHA!
552// However, for _SHA384 ciphers, it's sha384. See RFC 5288,5289.
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100553//...
554// P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
555// HMAC_hash(secret, A(2) + seed) +
556// HMAC_hash(secret, A(3) + seed) + ...
557// where + indicates concatenation.
558// A() is defined as:
559// A(0) = seed
560// A(1) = HMAC_hash(secret, A(0)) = HMAC_hash(secret, seed)
561// A(i) = HMAC_hash(secret, A(i-1))
562// P_hash can be iterated as many times as necessary to produce the
563// required quantity of data. For example, if P_SHA256 is being used to
564// create 80 bytes of data, it will have to be iterated three times
565// (through A(3)), creating 96 bytes of output data; the last 16 bytes
566// of the final iteration will then be discarded, leaving 80 bytes of
567// output data.
568//
569// TLS's PRF is created by applying P_hash to the secret as:
570//
571// PRF(secret, label, seed) = P_<hash>(secret, label + seed)
572//
573// The label is an ASCII string.
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100574//
575// RFC 5288:
576// For cipher suites ending with _SHA256, the PRF is the TLS PRF
577// with SHA-256 as the hash function.
578// For cipher suites ending with _SHA384, the PRF is the TLS PRF
579// with SHA-384 as the hash function.
Denys Vlasenko89193f92017-01-24 18:08:07 +0100580static void prf_hmac_sha256(/*tls_state_t *tls,*/
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100581 uint8_t *outbuf, unsigned outbuf_size,
582 uint8_t *secret, unsigned secret_size,
583 const char *label,
584 uint8_t *seed, unsigned seed_size)
585{
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100586 hmac_precomputed_t pre;
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100587 uint8_t a[TLS_MAX_MAC_SIZE];
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100588 uint8_t *out_p = outbuf;
589 unsigned label_size = strlen(label);
Denys Vlasenko229d3c42017-04-03 21:53:29 +0200590 unsigned MAC_size = SHA256_OUTSIZE;
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100591
592 /* In P_hash() calculation, "seed" is "label + seed": */
593#define SEED label, label_size, seed, seed_size
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100594#define A a, MAC_size
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100595
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100596 hmac_begin(&pre, secret, secret_size, sha256_begin);
597
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100598 /* A(1) = HMAC_hash(secret, seed) */
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100599 hmac_sha_precomputed(&pre, a, SEED, NULL);
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100600
Denys Vlasenko229d3c42017-04-03 21:53:29 +0200601 for (;;) {
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100602 /* HMAC_hash(secret, A(1) + seed) */
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100603 if (outbuf_size <= MAC_size) {
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100604 /* Last, possibly incomplete, block */
605 /* (use a[] as temp buffer) */
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100606 hmac_sha_precomputed(&pre, a, A, SEED, NULL);
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100607 memcpy(out_p, a, outbuf_size);
608 return;
609 }
610 /* Not last block. Store directly to result buffer */
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100611 hmac_sha_precomputed(&pre, out_p, A, SEED, NULL);
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100612 out_p += MAC_size;
613 outbuf_size -= MAC_size;
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100614 /* A(2) = HMAC_hash(secret, A(1)) */
Denys Vlasenkod9f6c3b2018-11-26 15:55:41 +0100615 hmac_sha_precomputed(&pre, a, A, NULL);
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100616 }
617#undef A
618#undef SECRET
619#undef SEED
620}
621
Denys Vlasenkob5bf1912017-01-23 16:12:17 +0100622static void bad_record_die(tls_state_t *tls, const char *expected, int len)
623{
Denys Vlasenko1500b3a2017-01-24 17:06:10 +0100624 bb_error_msg("got bad TLS record (len:%d) while expecting %s", len, expected);
Denys Vlasenkob5bf1912017-01-23 16:12:17 +0100625 if (len > 0) {
626 uint8_t *p = tls->inbuf;
Denys Vlasenko98066662018-02-06 13:33:00 +0100627 if (len > 99)
628 len = 99; /* don't flood, a few lines should be enough */
629 do {
Denys Vlasenkob5bf1912017-01-23 16:12:17 +0100630 fprintf(stderr, " %02x", *p++);
Denys Vlasenko1500b3a2017-01-24 17:06:10 +0100631 len--;
Denys Vlasenko98066662018-02-06 13:33:00 +0100632 } while (len != 0);
Denys Vlasenkob5bf1912017-01-23 16:12:17 +0100633 fputc('\n', stderr);
634 }
635 xfunc_die();
636}
637
Denys Vlasenko1500b3a2017-01-24 17:06:10 +0100638static void tls_error_die(tls_state_t *tls, int line)
Denys Vlasenko936e83e2017-01-16 04:25:01 +0100639{
Denys Vlasenko39161392017-01-20 20:27:06 +0100640 dump_tls_record(tls->inbuf, tls->ofs_to_buffered + tls->buffered_size);
Denys Vlasenko1500b3a2017-01-24 17:06:10 +0100641 bb_error_msg_and_die("tls error at line %d cipher:%04x", line, tls->cipher_id);
Denys Vlasenko38972a82017-01-20 19:11:14 +0100642}
Denys Vlasenko1500b3a2017-01-24 17:06:10 +0100643#define tls_error_die(tls) tls_error_die(tls, __LINE__)
Denys Vlasenko38972a82017-01-20 19:11:14 +0100644
Denys Vlasenko39161392017-01-20 20:27:06 +0100645#if 0 //UNUSED
646static void tls_free_inbuf(tls_state_t *tls)
647{
648 if (tls->buffered_size == 0) {
649 free(tls->inbuf);
650 tls->inbuf_size = 0;
651 tls->inbuf = NULL;
652 }
653}
654#endif
655
Denys Vlasenko38972a82017-01-20 19:11:14 +0100656static void tls_free_outbuf(tls_state_t *tls)
657{
658 free(tls->outbuf);
659 tls->outbuf_size = 0;
660 tls->outbuf = NULL;
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100661}
662
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100663static void *tls_get_outbuf(tls_state_t *tls, int len)
664{
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100665 if (len > TLS_MAX_OUTBUF)
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100666 xfunc_die();
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100667 len += OUTBUF_PFX + OUTBUF_SFX;
668 if (tls->outbuf_size < len) {
669 tls->outbuf_size = len;
670 tls->outbuf = xrealloc(tls->outbuf, len);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100671 }
672 return tls->outbuf + OUTBUF_PFX;
673}
674
Denys Vlasenkod5a04052018-11-13 11:58:53 +0100675static void *tls_get_zeroed_outbuf(tls_state_t *tls, int len)
676{
677 void *record = tls_get_outbuf(tls, len);
678 memset(record, 0, len);
679 return record;
680}
681
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100682static void xwrite_encrypted_and_hmac_signed(tls_state_t *tls, unsigned size, unsigned type)
Denys Vlasenkofe0588d2017-01-17 17:04:24 +0100683{
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100684 uint8_t *buf = tls->outbuf + OUTBUF_PFX;
685 struct record_hdr *xhdr;
Denys Vlasenkoe7863f32017-01-20 17:59:25 +0100686 uint8_t padding_length;
Denys Vlasenko9a6897a2017-01-16 23:26:33 +0100687
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +0100688 xhdr = (void*)(buf - RECHDR_LEN);
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +0100689 if (!ALLOW_RSA_NULL_SHA256 /* if "no encryption" can't be selected */
Denys Vlasenko5d561ef2017-04-04 01:41:15 +0200690 || tls->cipher_id != TLS_RSA_WITH_NULL_SHA256 /* or if it wasn't selected */
691 ) {
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100692 xhdr = (void*)(buf - RECHDR_LEN - AES_BLOCK_SIZE); /* place for IV */
Denys Vlasenko5d561ef2017-04-04 01:41:15 +0200693 }
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +0100694
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100695 xhdr->type = type;
696 xhdr->proto_maj = TLS_MAJ;
697 xhdr->proto_min = TLS_MIN;
Denys Vlasenko54b927d2017-01-20 21:19:38 +0100698 /* fake unencrypted record len for MAC calculation */
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100699 xhdr->len16_hi = size >> 8;
700 xhdr->len16_lo = size & 0xff;
701
702 /* Calculate MAC signature */
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100703 hmac(tls, buf + size, /* result */
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +0100704 tls->client_write_MAC_key, TLS_MAC_SIZE(tls),
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100705 &tls->write_seq64_be, sizeof(tls->write_seq64_be),
706 xhdr, RECHDR_LEN,
707 buf, size,
708 NULL
709 );
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +0100710 tls->write_seq64_be = SWAP_BE64(1 + SWAP_BE64(tls->write_seq64_be));
Denys Vlasenko9a6897a2017-01-16 23:26:33 +0100711
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +0100712 size += TLS_MAC_SIZE(tls);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100713
Denys Vlasenkod4681c72018-11-26 10:33:23 +0100714 // RFC 5246:
Denys Vlasenkoe7863f32017-01-20 17:59:25 +0100715 // 6.2.3.1. Null or Standard Stream Cipher
716 //
717 // Stream ciphers (including BulkCipherAlgorithm.null; see Appendix A.6)
718 // convert TLSCompressed.fragment structures to and from stream
719 // TLSCiphertext.fragment structures.
720 //
721 // stream-ciphered struct {
722 // opaque content[TLSCompressed.length];
723 // opaque MAC[SecurityParameters.mac_length];
724 // } GenericStreamCipher;
725 //
726 // The MAC is generated as:
727 // MAC(MAC_write_key, seq_num +
728 // TLSCompressed.type +
729 // TLSCompressed.version +
730 // TLSCompressed.length +
731 // TLSCompressed.fragment);
732 // where "+" denotes concatenation.
733 // seq_num
734 // The sequence number for this record.
735 // MAC
736 // The MAC algorithm specified by SecurityParameters.mac_algorithm.
737 //
738 // Note that the MAC is computed before encryption. The stream cipher
739 // encrypts the entire block, including the MAC.
740 //...
741 // Appendix C. Cipher Suite Definitions
742 //...
743 // MAC Algorithm mac_length mac_key_length
744 // -------- ----------- ---------- --------------
745 // SHA HMAC-SHA1 20 20
746 // SHA256 HMAC-SHA256 32 32
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +0100747 if (ALLOW_RSA_NULL_SHA256
Denys Vlasenko5d561ef2017-04-04 01:41:15 +0200748 && tls->cipher_id == TLS_RSA_WITH_NULL_SHA256
749 ) {
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100750 /* No encryption, only signing */
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100751 xhdr->len16_hi = size >> 8;
752 xhdr->len16_lo = size & 0xff;
Denys Vlasenkoe7863f32017-01-20 17:59:25 +0100753 dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
Denys Vlasenko9a647c32017-01-23 01:08:16 +0100754 xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100755 dbg("wrote %u bytes (NULL crypt, SHA256 hash)\n", size);
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100756 return;
757 }
758
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100759 // 6.2.3.2. CBC Block Cipher
760 // For block ciphers (such as 3DES or AES), the encryption and MAC
761 // functions convert TLSCompressed.fragment structures to and from block
762 // TLSCiphertext.fragment structures.
763 // struct {
764 // opaque IV[SecurityParameters.record_iv_length];
765 // block-ciphered struct {
766 // opaque content[TLSCompressed.length];
767 // opaque MAC[SecurityParameters.mac_length];
768 // uint8 padding[GenericBlockCipher.padding_length];
769 // uint8 padding_length;
770 // };
771 // } GenericBlockCipher;
772 //...
773 // IV
774 // The Initialization Vector (IV) SHOULD be chosen at random, and
775 // MUST be unpredictable. Note that in versions of TLS prior to 1.1,
776 // there was no IV field (...). For block ciphers, the IV length is
777 // of length SecurityParameters.record_iv_length, which is equal to the
778 // SecurityParameters.block_size.
779 // padding
780 // Padding that is added to force the length of the plaintext to be
781 // an integral multiple of the block cipher's block length.
782 // padding_length
783 // The padding length MUST be such that the total size of the
784 // GenericBlockCipher structure is a multiple of the cipher's block
785 // length. Legal values range from zero to 255, inclusive.
786 //...
787 // Appendix C. Cipher Suite Definitions
788 //...
789 // Key IV Block
790 // Cipher Type Material Size Size
791 // ------------ ------ -------- ---- -----
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100792 // AES_128_CBC Block 16 16 16
793 // AES_256_CBC Block 32 16 16
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100794
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100795 tls_get_random(buf - AES_BLOCK_SIZE, AES_BLOCK_SIZE); /* IV */
Denys Vlasenko98066662018-02-06 13:33:00 +0100796 dbg("before crypt: 5 hdr + %u data + %u hash bytes\n",
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +0100797 size - TLS_MAC_SIZE(tls), TLS_MAC_SIZE(tls));
Denys Vlasenko98066662018-02-06 13:33:00 +0100798
799 /* Fill IV and padding in outbuf */
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100800 // RFC is talking nonsense:
Denys Vlasenko7a18b952017-01-23 16:37:04 +0100801 // "Padding that is added to force the length of the plaintext to be
802 // an integral multiple of the block cipher's block length."
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100803 // WRONG. _padding+padding_length_, not just _padding_,
804 // pads the data.
805 // IOW: padding_length is the last byte of padding[] array,
806 // contrary to what RFC depicts.
807 //
808 // What actually happens is that there is always padding.
809 // If you need one byte to reach BLOCKSIZE, this byte is 0x00.
810 // If you need two bytes, they are both 0x01.
811 // If you need three, they are 0x02,0x02,0x02. And so on.
812 // If you need no bytes to reach BLOCKSIZE, you have to pad a full
813 // BLOCKSIZE with bytes of value (BLOCKSIZE-1).
814 // It's ok to have more than minimum padding, but we do minimum.
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100815 padding_length = (~size) & (AES_BLOCK_SIZE - 1);
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100816 do {
Denys Vlasenko54b927d2017-01-20 21:19:38 +0100817 buf[size++] = padding_length; /* padding */
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100818 } while ((size & (AES_BLOCK_SIZE - 1)) != 0);
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100819
820 /* Encrypt content+MAC+padding in place */
Denys Vlasenkoc31b54f2017-02-04 16:23:49 +0100821 aes_cbc_encrypt(
Denys Vlasenkod2923b32018-11-24 21:26:20 +0100822 &tls->aes_encrypt, /* selects 128/256 */
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100823 buf - AES_BLOCK_SIZE, /* IV */
Denys Vlasenkoc31b54f2017-02-04 16:23:49 +0100824 buf, size, /* plaintext */
825 buf /* ciphertext */
826 );
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100827
828 /* Write out */
829 dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n",
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100830 AES_BLOCK_SIZE, size, padding_length);
831 size += AES_BLOCK_SIZE; /* + IV */
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +0100832 xhdr->len16_hi = size >> 8;
833 xhdr->len16_lo = size & 0xff;
Denys Vlasenkoe7863f32017-01-20 17:59:25 +0100834 dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
Denys Vlasenko9a647c32017-01-23 01:08:16 +0100835 xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +0100836 dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
Denys Vlasenkoceff6b02017-01-14 12:49:32 +0100837}
838
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100839/* Example how GCM encryption combines nonce, aad, input and generates
840 * "header | exp_nonce | encrypted output | tag":
841 * nonce:0d 6a 26 31 00 00 00 00 00 00 00 01 (implicit 4 bytes (derived from master secret), then explicit 8 bytes)
842 * aad: 00 00 00 00 00 00 00 01 17 03 03 00 1c
843 * 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)
844 * 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
845 * tag: c2 86 ce 4a 50 4a d0 aa 50 b3 76 5c 49 2a 3f 33
846 * 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
847 * .............................................^^ buf points here
848 */
849static void xwrite_encrypted_aesgcm(tls_state_t *tls, unsigned size, unsigned type)
850{
Denys Vlasenkoecc90902018-11-23 18:31:26 +0100851#define COUNTER(v) (*(uint32_t*)(v + 12))
852
Denys Vlasenko03569bc2018-11-24 14:08:29 +0100853 uint8_t aad[13 + 3] ALIGNED_long; /* +3 creates [16] buffer, simplifying GHASH() */
854 uint8_t nonce[12 + 4] ALIGNED_long; /* +4 creates space for AES block counter */
855 uint8_t scratch[AES_BLOCK_SIZE] ALIGNED_long; //[16]
856 uint8_t authtag[AES_BLOCK_SIZE] ALIGNED_long; //[16]
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100857 uint8_t *buf;
858 struct record_hdr *xhdr;
859 unsigned remaining;
860 unsigned cnt;
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100861 uint64_t t64;
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100862
863 buf = tls->outbuf + OUTBUF_PFX; /* see above for the byte it points to */
864 dump_hex("xwrite_encrypted_aesgcm plaintext:%s\n", buf, size);
865
866 xhdr = (void*)(buf - 8 - RECHDR_LEN);
867 xhdr->type = type; /* do it here so that "type" param no longer used */
868
869 aad[8] = type;
870 aad[9] = TLS_MAJ;
871 aad[10] = TLS_MIN;
872 aad[11] = size >> 8;
Denys Vlasenkoecc90902018-11-23 18:31:26 +0100873 /* set aad[12], and clear aad[13..15] */
874 COUNTER(aad) = SWAP_LE32(size & 0xff);
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100875
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100876 memcpy(nonce, tls->client_write_IV, 4);
877 t64 = tls->write_seq64_be;
878 move_to_unaligned64(nonce + 4, t64);
879 move_to_unaligned64(aad, t64);
880 move_to_unaligned64(buf - 8, t64);
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100881 /* seq64 is not used later in this func, can increment here */
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100882 tls->write_seq64_be = SWAP_BE64(1 + SWAP_BE64(t64));
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100883
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100884 cnt = 1;
885 remaining = size;
886 while (remaining != 0) {
887 unsigned n;
888
889 cnt++;
890 COUNTER(nonce) = htonl(cnt); /* yes, first cnt here is 2 (!) */
891 aes_encrypt_one_block(&tls->aes_encrypt, nonce, scratch);
892 n = remaining > AES_BLOCK_SIZE ? AES_BLOCK_SIZE : remaining;
893 xorbuf(buf, scratch, n);
894 buf += n;
895 remaining -= n;
896 }
897
Denys Vlasenkoecc90902018-11-23 18:31:26 +0100898 aesgcm_GHASH(tls->H, aad, /*sizeof(aad),*/ tls->outbuf + OUTBUF_PFX, size, authtag /*, sizeof(authtag)*/);
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100899 COUNTER(nonce) = htonl(1);
900 aes_encrypt_one_block(&tls->aes_encrypt, nonce, scratch);
Denys Vlasenko03569bc2018-11-24 14:08:29 +0100901 xorbuf_aligned_AES_BLOCK_SIZE(authtag, scratch);
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100902
903 memcpy(buf, authtag, sizeof(authtag));
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100904
905 /* Write out */
906 xhdr = (void*)(tls->outbuf + OUTBUF_PFX - 8 - RECHDR_LEN);
907 size += 8 + sizeof(authtag);
908 /*xhdr->type = type; - already is */
909 xhdr->proto_maj = TLS_MAJ;
910 xhdr->proto_min = TLS_MIN;
911 xhdr->len16_hi = size >> 8;
912 xhdr->len16_lo = size & 0xff;
913 size += RECHDR_LEN;
914 dump_raw_out(">> %s\n", xhdr, size);
915 xwrite(tls->ofd, xhdr, size);
916 dbg("wrote %u bytes\n", size);
Denys Vlasenkobe5ca422018-11-25 14:03:59 +0100917#undef COUNTER
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100918}
919
920static void xwrite_encrypted(tls_state_t *tls, unsigned size, unsigned type)
921{
922 if (!(tls->flags & ENCRYPTION_AESGCM)) {
923 xwrite_encrypted_and_hmac_signed(tls, size, type);
924 return;
925 }
926 xwrite_encrypted_aesgcm(tls, size, type);
927}
928
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100929static void xwrite_handshake_record(tls_state_t *tls, unsigned size)
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100930{
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100931 uint8_t *buf = tls->outbuf + OUTBUF_PFX;
932 struct record_hdr *xhdr = (void*)(buf - RECHDR_LEN);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100933
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100934 xhdr->type = RECORD_TYPE_HANDSHAKE;
935 xhdr->proto_maj = TLS_MAJ;
936 xhdr->proto_min = TLS_MIN;
937 xhdr->len16_hi = size >> 8;
938 xhdr->len16_lo = size & 0xff;
939 dump_raw_out(">> %s\n", xhdr, RECHDR_LEN + size);
940 xwrite(tls->ofd, xhdr, RECHDR_LEN + size);
941 dbg("wrote %u bytes\n", (int)RECHDR_LEN + size);
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100942}
943
944static void xwrite_and_update_handshake_hash(tls_state_t *tls, unsigned size)
945{
Denys Vlasenkoeb53d012018-11-25 14:45:55 +0100946 if (!(tls->flags & ENCRYPT_ON_WRITE)) {
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100947 uint8_t *buf;
948
949 xwrite_handshake_record(tls, size);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100950 /* Handshake hash does not include record headers */
Denys Vlasenko49ecee02017-01-24 16:00:54 +0100951 buf = tls->outbuf + OUTBUF_PFX;
952 hash_handshake(tls, ">> hash:%s", buf, size);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +0100953 return;
954 }
955 xwrite_encrypted(tls, size, RECORD_TYPE_HANDSHAKE);
956}
957
Denys Vlasenko38972a82017-01-20 19:11:14 +0100958static int tls_has_buffered_record(tls_state_t *tls)
959{
Denys Vlasenko39161392017-01-20 20:27:06 +0100960 int buffered = tls->buffered_size;
Denys Vlasenko38972a82017-01-20 19:11:14 +0100961 struct record_hdr *xhdr;
962 int rec_size;
963
964 if (buffered < RECHDR_LEN)
965 return 0;
Denys Vlasenko39161392017-01-20 20:27:06 +0100966 xhdr = (void*)(tls->inbuf + tls->ofs_to_buffered);
Denys Vlasenko38972a82017-01-20 19:11:14 +0100967 rec_size = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
968 if (buffered < rec_size)
969 return 0;
970 return rec_size;
971}
972
Denys Vlasenkob5bf1912017-01-23 16:12:17 +0100973static const char *alert_text(int code)
974{
975 switch (code) {
976 case 20: return "bad MAC";
977 case 50: return "decode error";
978 case 51: return "decrypt error";
979 case 40: return "handshake failure";
980 case 112: return "unrecognized name";
981 }
982 return itoa(code);
983}
984
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100985static void tls_aesgcm_decrypt(tls_state_t *tls, uint8_t *buf, int size)
986{
Denys Vlasenkoecc90902018-11-23 18:31:26 +0100987#define COUNTER(v) (*(uint32_t*)(v + 12))
988
Denys Vlasenko03569bc2018-11-24 14:08:29 +0100989 //uint8_t aad[13 + 3] ALIGNED_long; /* +3 creates [16] buffer, simplifying GHASH() */
990 uint8_t nonce[12 + 4] ALIGNED_long; /* +4 creates space for AES block counter */
991 uint8_t scratch[AES_BLOCK_SIZE] ALIGNED_long; //[16]
992 //uint8_t authtag[AES_BLOCK_SIZE] ALIGNED_long; //[16]
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100993 unsigned remaining;
994 unsigned cnt;
995
Denys Vlasenko219c9d42018-11-23 18:48:20 +0100996 //memcpy(aad, buf, 8);
Denys Vlasenko83e5c622018-11-23 17:21:38 +0100997 //aad[8] = type;
998 //aad[9] = TLS_MAJ;
999 //aad[10] = TLS_MIN;
1000 //aad[11] = size >> 8;
Denys Vlasenkoecc90902018-11-23 18:31:26 +01001001 ///* set aad[12], and clear aad[13..15] */
1002 //COUNTER(aad) = SWAP_LE32(size & 0xff);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001003
1004 memcpy(nonce, tls->server_write_IV, 4);
1005 memcpy(nonce + 4, buf, 8);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001006
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001007 cnt = 1;
1008 remaining = size;
1009 while (remaining != 0) {
1010 unsigned n;
1011
1012 cnt++;
1013 COUNTER(nonce) = htonl(cnt); /* yes, first cnt here is 2 (!) */
1014 aes_encrypt_one_block(&tls->aes_decrypt, nonce, scratch);
1015 n = remaining > AES_BLOCK_SIZE ? AES_BLOCK_SIZE : remaining;
Denys Vlasenko941440c2018-11-24 13:51:46 +01001016 xorbuf3(buf, scratch, buf + 8, n);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001017 buf += n;
1018 remaining -= n;
1019 }
1020
Denys Vlasenko941440c2018-11-24 13:51:46 +01001021 //aesgcm_GHASH(tls->H, aad, tls->inbuf + RECHDR_LEN, size, authtag);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001022 //COUNTER(nonce) = htonl(1);
1023 //aes_encrypt_one_block(&tls->aes_encrypt, nonce, scratch);
Denys Vlasenko03569bc2018-11-24 14:08:29 +01001024 //xorbuf_aligned_AES_BLOCK_SIZE(authtag, scratch);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001025
1026 //memcmp(buf, authtag, sizeof(authtag)) || DIE("HASH DOES NOT MATCH!");
1027#undef COUNTER
1028}
1029
Denys Vlasenko98066662018-02-06 13:33:00 +01001030static int tls_xread_record(tls_state_t *tls, const char *expected)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001031{
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001032 struct record_hdr *xhdr;
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001033 int sz;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001034 int total;
1035 int target;
1036
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001037 again:
Denys Vlasenko39161392017-01-20 20:27:06 +01001038 dbg("ofs_to_buffered:%u buffered_size:%u\n", tls->ofs_to_buffered, tls->buffered_size);
1039 total = tls->buffered_size;
Denys Vlasenkoe7863f32017-01-20 17:59:25 +01001040 if (total != 0) {
Denys Vlasenko39161392017-01-20 20:27:06 +01001041 memmove(tls->inbuf, tls->inbuf + tls->ofs_to_buffered, total);
1042 //dbg("<< remaining at %d [%d] ", tls->ofs_to_buffered, total);
Denys Vlasenkoe7863f32017-01-20 17:59:25 +01001043 //dump_raw_in("<< %s\n", tls->inbuf, total);
1044 }
1045 errno = 0;
Denys Vlasenko39161392017-01-20 20:27:06 +01001046 target = MAX_INBUF;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001047 for (;;) {
Denys Vlasenko39161392017-01-20 20:27:06 +01001048 int rem;
1049
1050 if (total >= RECHDR_LEN && target == MAX_INBUF) {
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001051 xhdr = (void*)tls->inbuf;
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001052 target = RECHDR_LEN + (0x100 * xhdr->len16_hi + xhdr->len16_lo);
Denys Vlasenko98066662018-02-06 13:33:00 +01001053
1054 if (target > MAX_INBUF /* malformed input (too long) */
1055 || xhdr->proto_maj != TLS_MAJ
1056 || xhdr->proto_min != TLS_MIN
1057 ) {
1058 sz = total < target ? total : target;
Denys Vlasenko98066662018-02-06 13:33:00 +01001059 bad_record_die(tls, expected, sz);
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001060 }
Denys Vlasenkoe7863f32017-01-20 17:59:25 +01001061 dbg("xhdr type:%d ver:%d.%d len:%d\n",
1062 xhdr->type, xhdr->proto_maj, xhdr->proto_min,
1063 0x100 * xhdr->len16_hi + xhdr->len16_lo
1064 );
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001065 }
1066 /* if total >= target, we have a full packet (and possibly more)... */
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001067 if (total - target >= 0)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001068 break;
Denys Vlasenko39161392017-01-20 20:27:06 +01001069 /* input buffer is grown only as needed */
1070 rem = tls->inbuf_size - total;
1071 if (rem == 0) {
1072 tls->inbuf_size += MAX_INBUF / 8;
1073 if (tls->inbuf_size > MAX_INBUF)
1074 tls->inbuf_size = MAX_INBUF;
1075 dbg("inbuf_size:%d\n", tls->inbuf_size);
1076 rem = tls->inbuf_size - total;
1077 tls->inbuf = xrealloc(tls->inbuf, tls->inbuf_size);
1078 }
Denys Vlasenko9a647c32017-01-23 01:08:16 +01001079 sz = safe_read(tls->ifd, tls->inbuf + total, rem);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001080 if (sz <= 0) {
1081 if (sz == 0 && total == 0) {
1082 /* "Abrupt" EOF, no TLS shutdown (seen from kernel.org) */
1083 dbg("EOF (without TLS shutdown) from peer\n");
Denys Vlasenko39161392017-01-20 20:27:06 +01001084 tls->buffered_size = 0;
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001085 goto end;
1086 }
1087 bb_perror_msg_and_die("short read, have only %d", total);
1088 }
Denys Vlasenkoe7863f32017-01-20 17:59:25 +01001089 dump_raw_in("<< %s\n", tls->inbuf + total, sz);
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001090 total += sz;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001091 }
Denys Vlasenko39161392017-01-20 20:27:06 +01001092 tls->buffered_size = total - target;
1093 tls->ofs_to_buffered = target;
1094 //dbg("<< stashing at %d [%d] ", tls->ofs_to_buffered, tls->buffered_size);
1095 //dump_hex("<< %s\n", tls->inbuf + tls->ofs_to_buffered, tls->buffered_size);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001096
1097 sz = target - RECHDR_LEN;
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001098
1099 /* Needs to be decrypted? */
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001100 if (tls->min_encrypted_len_on_read != 0) {
1101 if (sz < (int)tls->min_encrypted_len_on_read)
1102 bb_error_msg_and_die("bad encrypted len:%u", sz);
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001103
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001104 if (tls->flags & ENCRYPTION_AESGCM) {
1105 /* AESGCM */
1106 uint8_t *p = tls->inbuf + RECHDR_LEN;
1107
1108 sz -= 8 + AES_BLOCK_SIZE; /* we will overwrite nonce, drop hash */
1109 tls_aesgcm_decrypt(tls, p, sz);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001110 dbg("encrypted size:%u\n", sz);
1111 } else
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +01001112 if (tls->min_encrypted_len_on_read > TLS_MAC_SIZE(tls)) {
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001113 /* AES+SHA */
1114 uint8_t *p = tls->inbuf + RECHDR_LEN;
1115 int padding_len;
1116
1117 if (sz & (AES_BLOCK_SIZE-1))
1118 bb_error_msg_and_die("bad encrypted len:%u", sz);
1119
1120 /* Decrypt content+MAC+padding, moving it over IV in the process */
1121 sz -= AES_BLOCK_SIZE; /* we will overwrite IV now */
1122 aes_cbc_decrypt(
Denys Vlasenko5e4236d2018-11-23 18:02:44 +01001123 &tls->aes_decrypt, /* selects 128/256 */
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001124 p, /* IV */
1125 p + AES_BLOCK_SIZE, sz, /* ciphertext */
1126 p /* plaintext */
1127 );
1128 padding_len = p[sz - 1];
1129 dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[0], padding_len);
1130 padding_len++;
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +01001131 sz -= TLS_MAC_SIZE(tls) + padding_len; /* drop MAC and padding */
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001132 } else {
1133 /* if nonzero, then it's TLS_RSA_WITH_NULL_SHA256: drop MAC */
1134 /* else: no encryption yet on input, subtract zero = NOP */
1135 sz -= tls->min_encrypted_len_on_read;
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001136 }
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001137 }
Denys Vlasenko0af52652017-01-20 21:23:10 +01001138 if (sz < 0)
James Byrne69374872019-07-02 11:35:03 +02001139 bb_simple_error_msg_and_die("encrypted data too short");
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01001140
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001141 //dump_hex("<< %s\n", tls->inbuf, RECHDR_LEN + sz);
1142
1143 xhdr = (void*)tls->inbuf;
1144 if (xhdr->type == RECORD_TYPE_ALERT && sz >= 2) {
1145 uint8_t *p = tls->inbuf + RECHDR_LEN;
1146 dbg("ALERT size:%d level:%d description:%d\n", sz, p[0], p[1]);
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001147 if (p[0] == 2) { /* fatal */
1148 bb_error_msg_and_die("TLS %s from peer (alert code %d): %s",
1149 "error",
1150 p[1], alert_text(p[1])
1151 );
1152 }
Denys Vlasenko54b927d2017-01-20 21:19:38 +01001153 if (p[0] == 1) { /* warning */
1154 if (p[1] == 0) { /* "close_notify" warning: it's EOF */
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001155 dbg("EOF (TLS encoded) from peer\n");
1156 sz = 0;
1157 goto end;
1158 }
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001159//This possibly needs to be cached and shown only if
1160//a fatal alert follows
1161// bb_error_msg("TLS %s from peer (alert code %d): %s",
1162// "warning",
1163// p[1], alert_text(p[1])
1164// );
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001165 /* discard it, get next record */
1166 goto again;
1167 }
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001168 /* p[0] not 1 or 2: not defined in protocol */
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001169 sz = 0;
1170 goto end;
1171 }
1172
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01001173 /* RFC 5246 is not saying it explicitly, but sha256 hash
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001174 * in our FINISHED record must include data of incoming packets too!
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01001175 */
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001176 if (tls->inbuf[0] == RECORD_TYPE_HANDSHAKE
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001177/* HANDSHAKE HASH: */
1178 // && do_we_know_which_hash_to_use /* server_hello() might not know it in the future! */
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001179 ) {
1180 hash_handshake(tls, "<< hash:%s", tls->inbuf + RECHDR_LEN, sz);
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01001181 }
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001182 end:
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01001183 dbg("got block len:%u\n", sz);
1184 return sz;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001185}
1186
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001187static void binary_to_pstm(pstm_int *pstm_n, uint8_t *bin_ptr, unsigned len)
1188{
1189 pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len);
1190 pstm_read_unsigned_bin(pstm_n, bin_ptr, len);
1191 //return bin_ptr + len;
1192}
1193
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001194/*
1195 * DER parsing routines
1196 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001197static unsigned get_der_len(uint8_t **bodyp, uint8_t *der, uint8_t *end)
1198{
Denys Vlasenko2a17d1f2017-01-14 22:38:25 +01001199 unsigned len, len1;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001200
1201 if (end - der < 2)
1202 xfunc_die();
1203// if ((der[0] & 0x1f) == 0x1f) /* not single-byte item code? */
1204// xfunc_die();
1205
1206 len = der[1]; /* maybe it's short len */
1207 if (len >= 0x80) {
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001208 /* no, it's long */
Denys Vlasenko2a17d1f2017-01-14 22:38:25 +01001209
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001210 if (len == 0x80 || end - der < (int)(len - 0x7e)) {
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001211 /* 0x80 is "0 bytes of len", invalid DER: must use short len if can */
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001212 /* need 3 or 4 bytes for 81, 82 */
1213 xfunc_die();
1214 }
1215
1216 len1 = der[2]; /* if (len == 0x81) it's "ii 81 xx", fetch xx */
1217 if (len > 0x82) {
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001218 /* >0x82 is "3+ bytes of len", should not happen realistically */
1219 xfunc_die();
1220 }
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001221 if (len == 0x82) { /* it's "ii 82 xx yy" */
1222 len1 = 0x100*len1 + der[3];
1223 der += 1; /* skip [yy] */
1224 }
Denys Vlasenko2a17d1f2017-01-14 22:38:25 +01001225 der += 1; /* skip [xx] */
1226 len = len1;
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001227// if (len < 0x80)
1228// xfunc_die(); /* invalid DER: must use short len if can */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001229 }
Denys Vlasenko2a17d1f2017-01-14 22:38:25 +01001230 der += 2; /* skip [code]+[1byte] */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001231
Denys Vlasenko2a17d1f2017-01-14 22:38:25 +01001232 if (end - der < (int)len)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001233 xfunc_die();
1234 *bodyp = der;
1235
1236 return len;
1237}
1238
1239static uint8_t *enter_der_item(uint8_t *der, uint8_t **endp)
1240{
1241 uint8_t *new_der;
1242 unsigned len = get_der_len(&new_der, der, *endp);
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01001243 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 Vlasenkoceff6b02017-01-14 12:49:32 +01001244 /* Move "end" position to cover only this item */
1245 *endp = new_der + len;
1246 return new_der;
1247}
1248
1249static uint8_t *skip_der_item(uint8_t *der, uint8_t *end)
1250{
1251 uint8_t *new_der;
1252 unsigned len = get_der_len(&new_der, der, end);
1253 /* Skip body */
1254 new_der += len;
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01001255 dbg_der("skipped der 0x%02x, next byte 0x%02x\n", der[0], new_der[0]);
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001256 return new_der;
1257}
1258
Denys Vlasenko11d00962017-01-15 00:12:42 +01001259static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end)
1260{
Denys Vlasenkof78ad092017-01-15 00:18:22 +01001261 uint8_t *bin_ptr;
1262 unsigned len = get_der_len(&bin_ptr, der, end);
Denys Vlasenko11d00962017-01-15 00:12:42 +01001263
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01001264 dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]);
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001265 binary_to_pstm(pstm_n, bin_ptr, len);
Denys Vlasenko11d00962017-01-15 00:12:42 +01001266}
1267
1268static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001269{
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001270/* Certificate is a DER-encoded data structure. Each DER element has a length,
1271 * which makes it easy to skip over large compound elements of any complexity
1272 * without parsing them. Example: partial decode of kernel.org certificate:
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001273 * SEQ 0x05ac/1452 bytes (Certificate): 308205ac
1274 * SEQ 0x0494/1172 bytes (tbsCertificate): 30820494
1275 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0] 3 bytes: a003
1276 * INTEGER (version): 0201 02
1277 * INTEGER 0x11 bytes (serialNumber): 0211 00 9f85bf664b0cddafca508679501b2be4
1278 * //^^^^^^note: matrixSSL also allows [ASN_CONTEXT_SPECIFIC | ASN_PRIMITIVE | 2] = 0x82 type
1279 * SEQ 0x0d bytes (signatureAlgo): 300d
1280 * OID 9 bytes: 0609 2a864886f70d01010b (OID_SHA256_RSA_SIG 42.134.72.134.247.13.1.1.11)
1281 * NULL: 0500
1282 * SEQ 0x5f bytes (issuer): 305f
1283 * SET 11 bytes: 310b
1284 * SEQ 9 bytes: 3009
1285 * OID 3 bytes: 0603 550406
1286 * Printable string "FR": 1302 4652
1287 * SET 14 bytes: 310e
1288 * SEQ 12 bytes: 300c
1289 * OID 3 bytes: 0603 550408
1290 * Printable string "Paris": 1305 5061726973
1291 * SET 14 bytes: 310e
1292 * SEQ 12 bytes: 300c
1293 * OID 3 bytes: 0603 550407
1294 * Printable string "Paris": 1305 5061726973
1295 * SET 14 bytes: 310e
1296 * SEQ 12 bytes: 300c
1297 * OID 3 bytes: 0603 55040a
1298 * Printable string "Gandi": 1305 47616e6469
1299 * SET 32 bytes: 3120
1300 * SEQ 30 bytes: 301e
1301 * OID 3 bytes: 0603 550403
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001302 * Printable string "Gandi Standard SSL CA 2": 1317 47616e6469205374616e646172642053534c2043412032
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001303 * SEQ 30 bytes (validity): 301e
1304 * TIME "161011000000Z": 170d 3136313031313030303030305a
1305 * TIME "191011235959Z": 170d 3139313031313233353935395a
1306 * SEQ 0x5b/91 bytes (subject): 305b //I did not decode this
1307 * 3121301f060355040b1318446f6d61696e20436f
1308 * 6e74726f6c2056616c6964617465643121301f06
1309 * 0355040b1318506f73697469766553534c204d75
1310 * 6c74692d446f6d61696e31133011060355040313
1311 * 0a6b65726e656c2e6f7267
1312 * SEQ 0x01a2/418 bytes (subjectPublicKeyInfo): 308201a2
1313 * SEQ 13 bytes (algorithm): 300d
1314 * OID 9 bytes: 0609 2a864886f70d010101 (OID_RSA_KEY_ALG 42.134.72.134.247.13.1.1.1)
1315 * NULL: 0500
1316 * BITSTRING 0x018f/399 bytes (publicKey): 0382018f
1317 * ????: 00
1318 * //after the zero byte, it appears key itself uses DER encoding:
1319 * SEQ 0x018a/394 bytes: 3082018a
1320 * INTEGER 0x0181/385 bytes (modulus): 02820181
1321 * 00b1ab2fc727a3bef76780c9349bf3
1322 * ...24 more blocks of 15 bytes each...
1323 * 90e895291c6bc8693b65
1324 * INTEGER 3 bytes (exponent): 0203 010001
1325 * [ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0x3] 0x01e5 bytes (X509v3 extensions): a38201e5
1326 * SEQ 0x01e1 bytes: 308201e1
1327 * ...
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001328 * Certificate is a sequence of three elements:
1329 * tbsCertificate (SEQ)
1330 * signatureAlgorithm (AlgorithmIdentifier)
1331 * signatureValue (BIT STRING)
1332 *
1333 * In turn, tbsCertificate is a sequence of:
1334 * version
1335 * serialNumber
1336 * signatureAlgo (AlgorithmIdentifier)
1337 * issuer (Name, has complex structure)
1338 * validity (Validity, SEQ of two Times)
1339 * subject (Name)
1340 * subjectPublicKeyInfo (SEQ)
1341 * ...
1342 *
1343 * subjectPublicKeyInfo is a sequence of:
1344 * algorithm (AlgorithmIdentifier)
1345 * publicKey (BIT STRING)
1346 *
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001347 * We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001348 *
1349 * Example of an ECDSA key:
1350 * SEQ 0x59 bytes (subjectPublicKeyInfo): 3059
1351 * SEQ 0x13 bytes (algorithm): 3013
1352 * OID 7 bytes: 0607 2a8648ce3d0201 (OID_ECDSA_KEY_ALG 42.134.72.206.61.2.1)
1353 * OID 8 bytes: 0608 2a8648ce3d030107 (OID_EC_prime256v1 42.134.72.206.61.3.1.7)
1354 * BITSTRING 0x42 bytes (publicKey): 0342
1355 * 0004 53af f65e 50cc 7959 7e29 0171 c75c
1356 * 7335 e07d f45b 9750 b797 3a38 aebb 2ac6
1357 * 8329 2748 e77e 41cb d482 2ce6 05ec a058
1358 * f3ab d561 2f4c d845 9ad3 7252 e3de bd3b
1359 * 9012
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001360 */
1361 uint8_t *end = der + len;
1362
1363 /* enter "Certificate" item: [der, end) will be only Cert */
1364 der = enter_der_item(der, &end);
1365
1366 /* enter "tbsCertificate" item: [der, end) will be only tbsCert */
1367 der = enter_der_item(der, &end);
1368
Ivan Abrea5cb4f902018-06-24 20:04:57 +02001369 /*
1370 * Skip version field only if it is present. For a v1 certificate, the
1371 * version field won't be present since v1 is the default value for the
1372 * version field and fields with default values should be omitted (see
1373 * RFC 5280 sections 4.1 and 4.1.2.1). If the version field is present
1374 * it will have a tag class of 2 (context-specific), bit 6 as 1
1375 * (constructed), and a tag number of 0 (see ITU-T X.690 sections 8.1.2
1376 * and 8.14).
1377 */
Denys Vlasenko084bac42018-11-05 00:18:18 +01001378 /* bits 7-6: 10 */
1379 /* bit 5: 1 */
1380 /* bits 4-0: 00000 */
1381 if (der[0] == 0xa0)
Ivan Abrea5cb4f902018-06-24 20:04:57 +02001382 der = skip_der_item(der, end); /* version */
Ivan Abrea5cb4f902018-06-24 20:04:57 +02001383
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001384 /* skip up to subjectPublicKeyInfo */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001385 der = skip_der_item(der, end); /* serialNumber */
1386 der = skip_der_item(der, end); /* signatureAlgo */
1387 der = skip_der_item(der, end); /* issuer */
1388 der = skip_der_item(der, end); /* validity */
1389 der = skip_der_item(der, end); /* subject */
1390
Denys Vlasenko11d00962017-01-15 00:12:42 +01001391 /* enter subjectPublicKeyInfo */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001392 der = enter_der_item(der, &end);
Denys Vlasenko11d00962017-01-15 00:12:42 +01001393 { /* check subjectPublicKeyInfo.algorithm */
Denys Vlasenko84fc6452019-05-21 17:29:24 +02001394 static const uint8_t OID_RSA_KEY_ALG[] ALIGN1 = {
Denys Vlasenko11d00962017-01-15 00:12:42 +01001395 0x30,0x0d, // SEQ 13 bytes
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001396 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 Vlasenko11d00962017-01-15 00:12:42 +01001397 //0x05,0x00, // NULL
1398 };
Denys Vlasenko84fc6452019-05-21 17:29:24 +02001399 static const uint8_t OID_ECDSA_KEY_ALG[] ALIGN1 = {
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001400 0x30,0x13, // SEQ 0x13 bytes
1401 0x06,0x07, 0x2a,0x86,0x48,0xce,0x3d,0x02,0x01, //OID_ECDSA_KEY_ALG 42.134.72.206.61.2.1
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001402 //allow any curve code for now...
1403 // 0x06,0x08, 0x2a,0x86,0x48,0xce,0x3d,0x03,0x01,0x07, //OID_EC_prime256v1 42.134.72.206.61.3.1.7
1404 //RFC 3279:
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001405 //42.134.72.206.61.3 is ellipticCurve
1406 //42.134.72.206.61.3.0 is c-TwoCurve
1407 //42.134.72.206.61.3.1 is primeCurve
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001408 //42.134.72.206.61.3.1.7 is curve_secp256r1
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001409 };
1410 if (memcmp(der, OID_RSA_KEY_ALG, sizeof(OID_RSA_KEY_ALG)) == 0) {
1411 dbg("RSA key\n");
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001412 tls->flags |= GOT_CERT_RSA_KEY_ALG;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001413 } else
1414 if (memcmp(der, OID_ECDSA_KEY_ALG, sizeof(OID_ECDSA_KEY_ALG)) == 0) {
1415 dbg("ECDSA key\n");
Denys Vlasenkoa33b0082018-11-25 14:28:32 +01001416 //UNUSED: tls->flags |= GOT_CERT_ECDSA_KEY_ALG;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001417 } else
James Byrne69374872019-07-02 11:35:03 +02001418 bb_simple_error_msg_and_die("not RSA or ECDSA cert");
Denys Vlasenko11d00962017-01-15 00:12:42 +01001419 }
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001420
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001421 if (tls->flags & GOT_CERT_RSA_KEY_ALG) {
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001422 /* parse RSA key: */
1423 //based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note
1424 /* skip subjectPublicKeyInfo.algorithm */
1425 der = skip_der_item(der, end);
1426 /* enter subjectPublicKeyInfo.publicKey */
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001427 //die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001428 der = enter_der_item(der, &end);
1429
1430 dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]);
1431 if (end - der < 14)
1432 xfunc_die();
1433 /* example format:
1434 * ignore bits: 00
1435 * SEQ 0x018a/394 bytes: 3082018a
1436 * INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX
1437 * INTEGER 3 bytes (exponent): 0203 010001
1438 */
1439 if (*der != 0) /* "ignore bits", should be 0 */
1440 xfunc_die();
1441 der++;
1442 der = enter_der_item(der, &end); /* enter SEQ */
1443 /* memset(tls->hsd->server_rsa_pub_key, 0, sizeof(tls->hsd->server_rsa_pub_key)); - already is */
1444 der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.N, der, end); /* modulus */
1445 der = skip_der_item(der, end);
1446 der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.e, der, end); /* exponent */
1447 tls->hsd->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->hsd->server_rsa_pub_key.N);
1448 dbg("server_rsa_pub_key.size:%d\n", tls->hsd->server_rsa_pub_key.size);
1449 }
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001450 /* else: ECDSA key. It is not used for generating encryption keys,
1451 * it is used only to sign the EC public key (which comes in ServerKey message).
1452 * Since we do not verify cert validity, verifying signature on EC public key
1453 * wouldn't add any security. Thus, we do nothing here.
1454 */
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001455}
1456
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001457/*
1458 * TLS Handshake routines
1459 */
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001460static int tls_xread_handshake_block(tls_state_t *tls, int min_len)
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001461{
1462 struct record_hdr *xhdr;
Denys Vlasenko98066662018-02-06 13:33:00 +01001463 int len = tls_xread_record(tls, "handshake record");
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001464
1465 xhdr = (void*)tls->inbuf;
1466 if (len < min_len
1467 || xhdr->type != RECORD_TYPE_HANDSHAKE
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001468 ) {
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001469 bad_record_die(tls, "handshake record", len);
Denys Vlasenkofe0588d2017-01-17 17:04:24 +01001470 }
1471 dbg("got HANDSHAKE\n");
1472 return len;
1473}
1474
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01001475static ALWAYS_INLINE void fill_handshake_record_hdr(void *buf, unsigned type, unsigned len)
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001476{
1477 struct handshake_hdr {
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001478 uint8_t type;
1479 uint8_t len24_hi, len24_mid, len24_lo;
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01001480 } *h = buf;
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001481
1482 len -= 4;
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01001483 h->type = type;
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001484 h->len24_hi = len >> 16;
1485 h->len24_mid = len >> 8;
1486 h->len24_lo = len & 0xff;
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001487}
1488
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001489static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni)
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001490{
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001491#define NUM_CIPHERS (7 + 6 * ENABLE_FEATURE_TLS_SHA1 + ALLOW_RSA_NULL_SHA256)
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001492 static const uint8_t ciphers[] = {
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001493 0x00,2 + NUM_CIPHERS*2, //len16_be
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001494 0x00,0xFF, //not a cipher - TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1495 /* ^^^^^^ RFC 5746 Renegotiation Indication Extension - some servers will refuse to work with us otherwise */
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001496#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001497 0xC0,0x09, // 1 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA - ok: wget https://is.gd/
1498 0xC0,0x0A, // 2 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA - ok: wget https://is.gd/
1499 0xC0,0x13, // 3 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-SHA
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001500 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 Vlasenko3a4d5a72018-12-10 19:19:38 +01001501 // 0xC0,0x18, // TLS_ECDH_anon_WITH_AES_128_CBC_SHA
1502 // 0xC0,0x19, // TLS_ECDH_anon_WITH_AES_256_CBC_SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001503#endif
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001504 0xC0,0x23, // 5 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 - ok: wget https://is.gd/
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001505 // 0xC0,0x24, // TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001506 0xC0,0x27, // 6 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001507 // 0xC0,0x28, // TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001508 0xC0,0x2B, // 7 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 - ok: wget https://is.gd/
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001509 // 0xC0,0x2C, // TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 - wget https://is.gd/: "TLS error from peer (alert code 20): bad MAC"
Denys Vlasenkod4681c72018-11-26 10:33:23 +01001510//TODO: GCM_SHA384 ciphers can be supported, only need sha384-based PRF?
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001511 0xC0,0x2F, // 8 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-GCM-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001512 // 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"
1513 //possibly these too:
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001514#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001515 // 0xC0,0x35, // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA
1516 // 0xC0,0x36, // TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001517#endif
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001518 // 0xC0,0x37, // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
1519 // 0xC0,0x38, // TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001520#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001521 0x00,0x2F, // 9 TLS_RSA_WITH_AES_128_CBC_SHA - ok: openssl s_server ... -cipher AES128-SHA
1522 0x00,0x35, //10 TLS_RSA_WITH_AES_256_CBC_SHA - ok: openssl s_server ... -cipher AES256-SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001523#endif
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001524 0x00,0x3C, //11 TLS_RSA_WITH_AES_128_CBC_SHA256 - ok: openssl s_server ... -cipher AES128-SHA256
1525 0x00,0x3D, //12 TLS_RSA_WITH_AES_256_CBC_SHA256 - ok: openssl s_server ... -cipher AES256-SHA256
1526 0x00,0x9C, //13 TLS_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher AES128-GCM-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001527 // 0x00,0x9D, // TLS_RSA_WITH_AES_256_GCM_SHA384 - openssl s_server ... -cipher AES256-GCM-SHA384: "decryption failed or bad record mac"
1528#if ALLOW_RSA_NULL_SHA256
1529 0x00,0x3B, // TLS_RSA_WITH_NULL_SHA256
1530#endif
1531 0x01,0x00, //not a cipher - comprtypes_len, comprtype
1532 };
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001533 static const uint8_t supported_groups[] = {
1534 0x00,0x0a, //extension_type: "supported_groups"
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001535 0x00,0x06, //ext len
1536 0x00,0x04, //list len
Denys Vlasenko6b69ab62021-04-26 13:46:36 +02001537 0x00,0x17, //curve_secp256r1 (aka P256)
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001538 //0x00,0x18, //curve_secp384r1
1539 //0x00,0x19, //curve_secp521r1
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001540 0x00,0x1d, //curve_x25519 (RFC 7748)
1541 //0x00,0x1e, //curve_x448 (RFC 7748)
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001542 };
1543 //static const uint8_t signature_algorithms[] = {
1544 // 000d
1545 // 0020
1546 // 001e
1547 // 0601 0602 0603 0501 0502 0503 0401 0402 0403 0301 0302 0303 0201 0202 0203
1548 //};
1549
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001550 struct client_hello {
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001551 uint8_t type;
1552 uint8_t len24_hi, len24_mid, len24_lo;
1553 uint8_t proto_maj, proto_min;
1554 uint8_t rand32[32];
1555 uint8_t session_id_len;
1556 /* uint8_t session_id[]; */
1557 uint8_t cipherid_len16_hi, cipherid_len16_lo;
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001558 uint8_t cipherid[2 + NUM_CIPHERS*2]; /* actually variable */
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001559 uint8_t comprtypes_len;
1560 uint8_t comprtypes[1]; /* actually variable */
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001561 /* Extensions (SNI shown):
1562 * hi,lo // len of all extensions
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001563 * 00,00 // extension_type: "Server Name"
1564 * 00,0e // list len (there can be more than one SNI)
1565 * 00,0c // len of 1st Server Name Indication
1566 * 00 // name type: host_name
1567 * 00,09 // name len
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001568 * "localhost" // name
1569 */
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001570// GNU Wget 1.18 to cdn.kernel.org sends these extensions:
1571// 0055
1572// 0005 0005 0100000000 - status_request
1573// 0000 0013 0011 00 000e 63646e 2e 6b65726e656c 2e 6f7267 - server_name
1574// ff01 0001 00 - renegotiation_info
1575// 0023 0000 - session_ticket
1576// 000a 0008 0006001700180019 - supported_groups
1577// 000b 0002 0100 - ec_point_formats
Denys Vlasenko98066662018-02-06 13:33:00 +01001578// 000d 0016 0014 0401 0403 0501 0503 0601 0603 0301 0303 0201 0203 - signature_algorithms
1579// wolfssl library sends this option, RFC 7627 (closes a security weakness, some servers may require it. TODO?):
1580// 0017 0000 - extended master secret
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001581 };
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001582 struct client_hello *record;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001583 uint8_t *ptr;
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001584 int len;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001585 int ext_len;
1586 int sni_len = sni ? strnlen(sni, 127 - 5) : 0;
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001587
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001588 ext_len = 0;
1589 /* is.gd responds with "handshake failure" to our hello if there's no supported_groups element */
1590 ext_len += sizeof(supported_groups);
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001591 if (sni_len)
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001592 ext_len += 9 + sni_len;
1593
1594 /* +2 is for "len of all extensions" 2-byte field */
1595 len = sizeof(*record) + 2 + ext_len;
Denys Vlasenkod5a04052018-11-13 11:58:53 +01001596 record = tls_get_zeroed_outbuf(tls, len);
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001597
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001598 fill_handshake_record_hdr(record, HANDSHAKE_CLIENT_HELLO, len);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01001599 record->proto_maj = TLS_MAJ; /* the "requested" version of the protocol, */
1600 record->proto_min = TLS_MIN; /* can be higher than one in record headers */
1601 tls_get_random(record->rand32, sizeof(record->rand32));
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01001602 if (TLS_DEBUG_FIXED_SECRETS)
1603 memset(record->rand32, 0x11, sizeof(record->rand32));
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001604 /* record->session_id_len = 0; - already is */
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001605
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001606 BUILD_BUG_ON(sizeof(ciphers) != 2 + 2 + NUM_CIPHERS*2 + 2);
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001607 memcpy(&record->cipherid_len16_hi, ciphers, sizeof(ciphers));
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001608
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001609 ptr = (void*)(record + 1);
1610 *ptr++ = ext_len >> 8;
1611 *ptr++ = ext_len;
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001612 if (sni_len) {
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001613 //ptr[0] = 0; //
1614 //ptr[1] = 0; //extension_type
1615 //ptr[2] = 0; //
1616 ptr[3] = sni_len + 5; //list len
1617 //ptr[4] = 0; //
1618 ptr[5] = sni_len + 3; //len of 1st SNI
1619 //ptr[6] = 0; //name type
1620 //ptr[7] = 0; //
1621 ptr[8] = sni_len; //name len
1622 ptr = mempcpy(&ptr[9], sni, sni_len);
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001623 }
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001624 memcpy(ptr, supported_groups, sizeof(supported_groups));
Denys Vlasenko19e695e2017-01-20 14:27:58 +01001625
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001626 tls->hsd = xzalloc(sizeof(*tls->hsd));
1627 /* HANDSHAKE HASH: ^^^ + len if need to save saved_client_hello */
1628 memcpy(tls->hsd->client_and_server_rand32, record->rand32, sizeof(record->rand32));
1629/* HANDSHAKE HASH:
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001630 tls->hsd->saved_client_hello_size = len;
1631 memcpy(tls->hsd->saved_client_hello, record, len);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001632 */
1633 dbg(">> CLIENT_HELLO\n");
1634 /* Can hash immediately only if we know which MAC hash to use.
1635 * So far we do know: it's sha256:
1636 */
1637 sha256_begin(&tls->hsd->handshake_hash_ctx);
1638 xwrite_and_update_handshake_hash(tls, len);
1639 /* if this would become infeasible: save tls->hsd->saved_client_hello,
1640 * use "xwrite_handshake_record(tls, len)" here,
1641 * and hash saved_client_hello later.
1642 */
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001643}
1644
1645static void get_server_hello(tls_state_t *tls)
1646{
1647 struct server_hello {
1648 struct record_hdr xhdr;
1649 uint8_t type;
1650 uint8_t len24_hi, len24_mid, len24_lo;
1651 uint8_t proto_maj, proto_min;
1652 uint8_t rand32[32]; /* first 4 bytes are unix time in BE format */
1653 uint8_t session_id_len;
1654 uint8_t session_id[32];
1655 uint8_t cipherid_hi, cipherid_lo;
1656 uint8_t comprtype;
1657 /* extensions may follow, but only those which client offered in its Hello */
1658 };
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001659
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001660 struct server_hello *hp;
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001661 uint8_t *cipherid;
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001662 uint8_t cipherid1;
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001663 int len, len24;
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001664
Denys Vlasenko5b05d9d2017-02-03 18:19:59 +01001665 len = tls_xread_handshake_block(tls, 74 - 32);
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001666
1667 hp = (void*)tls->inbuf;
1668 // 74 bytes:
1669 // 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|
1670 //SvHl len=70 maj.min unixtime^^^ 28randbytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^_^^^ slen sid32bytes^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cipSel comprSel
1671 if (hp->type != HANDSHAKE_SERVER_HELLO
1672 || hp->len24_hi != 0
1673 || hp->len24_mid != 0
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001674 /* hp->len24_lo checked later */
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001675 || hp->proto_maj != TLS_MAJ
1676 || hp->proto_min != TLS_MIN
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001677 ) {
Denys Vlasenkob5bf1912017-01-23 16:12:17 +01001678 bad_record_die(tls, "'server hello'", len);
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001679 }
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001680
1681 cipherid = &hp->cipherid_hi;
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001682 len24 = hp->len24_lo;
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001683 if (hp->session_id_len != 32) {
1684 if (hp->session_id_len != 0)
Denys Vlasenko5b05d9d2017-02-03 18:19:59 +01001685 bad_record_die(tls, "'server hello'", len);
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001686
1687 // session_id_len == 0: no session id
1688 // "The server
1689 // may return an empty session_id to indicate that the session will
1690 // not be cached and therefore cannot be resumed."
1691 cipherid -= 32;
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001692 len24 += 32; /* what len would be if session id would be present */
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01001693 }
1694
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001695 if (len24 < 70)
Denys Vlasenko5b05d9d2017-02-03 18:19:59 +01001696 bad_record_die(tls, "'server hello'", len);
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001697 dbg("<< SERVER_HELLO\n");
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001698
Denys Vlasenko9a647c32017-01-23 01:08:16 +01001699 memcpy(tls->hsd->client_and_server_rand32 + 32, hp->rand32, sizeof(hp->rand32));
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001700
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001701 /* Set up encryption params based on selected cipher */
1702#if 0
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001703#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001704 0xC0,0x09, // 1 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA - ok: wget https://is.gd/
1705 0xC0,0x0A, // 2 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA - ok: wget https://is.gd/
1706 0xC0,0x13, // 3 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-SHA
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001707 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 Vlasenko3a4d5a72018-12-10 19:19:38 +01001708 // 0xC0,0x18, // TLS_ECDH_anon_WITH_AES_128_CBC_SHA
1709 // 0xC0,0x19, // TLS_ECDH_anon_WITH_AES_256_CBC_SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001710#endif
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001711 0xC0,0x23, // 5 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 - ok: wget https://is.gd/
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001712 // 0xC0,0x24, // TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001713 0xC0,0x27, // 6 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001714 // 0xC0,0x28, // TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001715 0xC0,0x2B, // 7 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 - ok: wget https://is.gd/
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001716 // 0xC0,0x2C, // TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 - wget https://is.gd/: "TLS error from peer (alert code 20): bad MAC"
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001717//TODO: GCM_SHA384 ciphers can be supported, only need sha384-based PRF?
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001718 0xC0,0x2F, // 8 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher ECDHE-RSA-AES128-GCM-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001719 // 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"
1720 //possibly these too:
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001721#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001722 // 0xC0,0x35, // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA
1723 // 0xC0,0x36, // TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001724#endif
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001725 // 0xC0,0x37, // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
1726 // 0xC0,0x38, // TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384 - can't do SHA384 yet
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001727#if ENABLE_FEATURE_TLS_SHA1
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001728 0x00,0x2F, // 9 TLS_RSA_WITH_AES_128_CBC_SHA - ok: openssl s_server ... -cipher AES128-SHA
1729 0x00,0x35, //10 TLS_RSA_WITH_AES_256_CBC_SHA - ok: openssl s_server ... -cipher AES256-SHA
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001730#endif
Denys Vlasenko2eb04292018-11-26 16:39:19 +01001731 0x00,0x3C, //11 TLS_RSA_WITH_AES_128_CBC_SHA256 - ok: openssl s_server ... -cipher AES128-SHA256
1732 0x00,0x3D, //12 TLS_RSA_WITH_AES_256_CBC_SHA256 - ok: openssl s_server ... -cipher AES256-SHA256
1733 0x00,0x9C, //13 TLS_RSA_WITH_AES_128_GCM_SHA256 - ok: openssl s_server ... -cipher AES128-GCM-SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001734 // 0x00,0x9D, // TLS_RSA_WITH_AES_256_GCM_SHA384 - openssl s_server ... -cipher AES256-GCM-SHA384: "decryption failed or bad record mac"
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001735#if ALLOW_RSA_NULL_SHA256
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001736 0x00,0x3B, // TLS_RSA_WITH_NULL_SHA256
1737#endif
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001738#endif
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001739 cipherid1 = cipherid[1];
Denys Vlasenko60f78402018-11-26 16:30:22 +01001740 tls->cipher_id = 0x100 * cipherid[0] + cipherid1;
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001741 tls->key_size = AES256_KEYSIZE;
1742 tls->MAC_size = SHA256_OUTSIZE;
1743 /*tls->IV_size = 0; - already is */
1744 if (cipherid[0] == 0xC0) {
1745 /* All C0xx are ECDHE */
1746 tls->flags |= NEED_EC_KEY;
1747 if (cipherid1 & 1) {
1748 /* Odd numbered C0xx use AES128 (even ones use AES256) */
1749 tls->key_size = AES128_KEYSIZE;
1750 }
Denys Vlasenko3a4d5a72018-12-10 19:19:38 +01001751 if (ENABLE_FEATURE_TLS_SHA1 && cipherid1 <= 0x19) {
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001752 tls->MAC_size = SHA1_OUTSIZE;
1753 } else
1754 if (cipherid1 >= 0x2B && cipherid1 <= 0x30) {
1755 /* C02B,2C,2F,30 are AES-GCM */
1756 tls->flags |= ENCRYPTION_AESGCM;
1757 tls->MAC_size = 0;
1758 tls->IV_size = 4;
1759 }
1760 } else {
1761 /* All 00xx are RSA */
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001762 if ((ENABLE_FEATURE_TLS_SHA1 && cipherid1 == 0x2F)
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001763 || cipherid1 == 0x3C
1764 || cipherid1 == 0x9C
1765 ) {
1766 tls->key_size = AES128_KEYSIZE;
1767 }
Denys Vlasenko71fa5b02018-12-10 16:14:58 +01001768 if (ENABLE_FEATURE_TLS_SHA1 && cipherid1 <= 0x35) {
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001769 tls->MAC_size = SHA1_OUTSIZE;
1770 } else
Denys Vlasenko60f78402018-11-26 16:30:22 +01001771 if (cipherid1 == 0x9C /*|| cipherid1 == 0x9D*/) {
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001772 /* 009C,9D are AES-GCM */
1773 tls->flags |= ENCRYPTION_AESGCM;
1774 tls->MAC_size = 0;
1775 tls->IV_size = 4;
1776 }
1777 }
Denys Vlasenko60f78402018-11-26 16:30:22 +01001778 dbg("server chose cipher %04x\n", tls->cipher_id);
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01001779 dbg("key_size:%u MAC_size:%u IV_size:%u\n", tls->key_size, tls->MAC_size, tls->IV_size);
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001780
Denys Vlasenko89193f92017-01-24 18:08:07 +01001781 /* Handshake hash eventually destined to FINISHED record
1782 * is sha256 regardless of cipher
1783 * (at least for all ciphers defined by RFC5246).
1784 * It's not sha1 for AES_128_CBC_SHA - only MAC is sha1, not this hash.
1785 */
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001786/* HANDSHAKE HASH:
Denys Vlasenko89193f92017-01-24 18:08:07 +01001787 sha256_begin(&tls->hsd->handshake_hash_ctx);
Denys Vlasenko49ecee02017-01-24 16:00:54 +01001788 hash_handshake(tls, ">> client hello hash:%s",
1789 tls->hsd->saved_client_hello, tls->hsd->saved_client_hello_size
1790 );
1791 hash_handshake(tls, "<< server hello hash:%s",
1792 tls->inbuf + RECHDR_LEN, len
1793 );
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001794 */
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001795}
1796
1797static void get_server_cert(tls_state_t *tls)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001798{
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001799 struct record_hdr *xhdr;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001800 uint8_t *certbuf;
1801 int len, len1;
1802
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01001803 len = tls_xread_handshake_block(tls, 10);
Denys Vlasenkoc5540d62017-01-15 02:17:03 +01001804
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001805 xhdr = (void*)tls->inbuf;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001806 certbuf = (void*)(xhdr + 1);
1807 if (certbuf[0] != HANDSHAKE_CERTIFICATE)
Denys Vlasenko98066662018-02-06 13:33:00 +01001808 bad_record_die(tls, "certificate", len);
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01001809 dbg("<< CERTIFICATE\n");
Denys Vlasenkob1003f72017-01-14 13:57:16 +01001810 // 4392 bytes:
1811 // 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...
1812 //Cert len=4388 ChainLen CertLen^ DER encoded X509 starts here. openssl x509 -in FILE -inform DER -noout -text
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001813 len1 = get24be(certbuf + 1);
1814 if (len1 > len - 4) tls_error_die(tls);
1815 len = len1;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001816 len1 = get24be(certbuf + 4);
1817 if (len1 > len - 3) tls_error_die(tls);
1818 len = len1;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01001819 len1 = get24be(certbuf + 7);
1820 if (len1 > len - 3) tls_error_die(tls);
1821 len = len1;
1822
1823 if (len)
Denys Vlasenko11d00962017-01-15 00:12:42 +01001824 find_key_in_der_cert(tls, certbuf + 10, len);
1825}
1826
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001827/* On input, len is known to be >= 4.
1828 * The record is known to be SERVER_KEY_EXCHANGE.
1829 */
1830static void process_server_key(tls_state_t *tls, int len)
1831{
1832 struct record_hdr *xhdr;
1833 uint8_t *keybuf;
1834 int len1;
1835 uint32_t t32;
1836
1837 xhdr = (void*)tls->inbuf;
1838 keybuf = (void*)(xhdr + 1);
1839//seen from is.gd: it selects curve_x25519:
Denys Vlasenko4e46b982018-11-18 19:50:24 +01001840// 0c 00006e //SERVER_KEY_EXCHANGE, len
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001841// 03 //curve_type: named curve
1842// 001d //curve_x25519
1843//server-chosen EC point, and then signed_params
Denys Vlasenko4e46b982018-11-18 19:50:24 +01001844// (RFC 8422: "A hash of the params, with the signature
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001845// appropriate to that hash applied. The private key corresponding
1846// to the certified public key in the server's Certificate message is
1847// used for signing.")
1848//follow. Format unclear/guessed:
1849// 20 //eccPubKeyLen
1850// 25511923d73b70dd2f60e66ba2f3fda31a9c25170963c7a3a972e481dbb2835d //eccPubKey (32bytes)
1851// 0203 //hashSigAlg: 2:SHA1 (4:SHA256 5:SHA384 6:SHA512), 3:ECDSA (1:RSA)
1852// 0046 //len (16bit)
1853// 30 44 //SEQ, len
1854// 02 20 //INTEGER, len
1855// 2e18e7c2a9badd0a70cd3059a6ab114539b9f5163568911147386cd77ed7c412 //32bytes
1856//this item ^^^^^ is sometimes 33 bytes (with all container sizes also +1)
1857// 02 20 //INTEGER, len
1858// 64523d6216cb94c43c9b20e377d8c52c55be6703fd6730a155930c705eaf3af6 //32bytes
1859//same about this item ^^^^^
Denys Vlasenko4e46b982018-11-18 19:50:24 +01001860
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001861//seen from ftp.openbsd.org
Denys Vlasenko4e46b982018-11-18 19:50:24 +01001862//(which only accepts ECDHE-RSA-AESnnn-GCM-SHAnnn and ECDHE-RSA-CHACHA20-POLY1305 ciphers):
1863// 0c 000228 //SERVER_KEY_EXCHANGE, len
1864// 03 //curve_type: named curve
1865// 001d //curve_x25519
1866// 20 //eccPubKeyLen
1867// eef7a15c43b71a4c7eaa48a39369399cc4332e569ec90a83274cc92596705c1a //eccPubKey
1868// 0401 //hashSigAlg: 4:SHA256, 1:RSA
1869// 0200 //len
1870// //0x200 bytes follow
1871
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001872 /* Get and verify length */
1873 len1 = get24be(keybuf + 1);
1874 if (len1 > len - 4) tls_error_die(tls);
1875 len = len1;
1876 if (len < (1+2+1+32)) tls_error_die(tls);
1877 keybuf += 4;
1878
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001879#if BB_BIG_ENDIAN
1880# define _0x03001741 0x03001741
1881# define _0x03001d20 0x03001d20
1882#else
1883# define _0x03001741 0x41170003
1884# define _0x03001d20 0x201d0003
1885#endif
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001886 move_from_unaligned32(t32, keybuf);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001887 keybuf += 4;
1888 switch (t32) {
1889 case _0x03001d20: //curve_x25519
1890 tls->flags |= GOT_EC_CURVE_X25519;
1891 memcpy(tls->hsd->ecc_pub_key32, keybuf, 32);
1892 break;
Denys Vlasenko6b69ab62021-04-26 13:46:36 +02001893 case _0x03001741: //curve_secp256r1 (aka P256)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001894 /* P256 point can be transmitted odd- or even-compressed
1895 * (first byte is 3 or 2) or uncompressed (4).
1896 */
1897 if (*keybuf++ != 4)
1898 bb_simple_error_msg_and_die("compressed EC points not supported");
1899 memcpy(tls->hsd->ecc_pub_key32, keybuf, 2 * 32);
1900 break;
1901 default:
1902 bb_error_msg_and_die("elliptic curve is not x25519 or P256: 0x%08x", t32);
1903 }
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001904
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001905 tls->flags |= GOT_EC_KEY;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001906 dbg("got eccPubKey\n");
1907}
1908
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01001909static void send_empty_client_cert(tls_state_t *tls)
1910{
1911 struct client_empty_cert {
1912 uint8_t type;
1913 uint8_t len24_hi, len24_mid, len24_lo;
1914 uint8_t cert_chain_len24_hi, cert_chain_len24_mid, cert_chain_len24_lo;
1915 };
1916 struct client_empty_cert *record;
1917
Denys Vlasenkod5a04052018-11-13 11:58:53 +01001918 record = tls_get_zeroed_outbuf(tls, sizeof(*record));
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001919 //fill_handshake_record_hdr(record, HANDSHAKE_CERTIFICATE, sizeof(*record));
1920 //record->cert_chain_len24_hi = 0;
1921 //record->cert_chain_len24_mid = 0;
1922 //record->cert_chain_len24_lo = 0;
Denys Vlasenkode7b5bb2018-11-13 11:44:32 +01001923 // same as above:
Denys Vlasenkod5a04052018-11-13 11:58:53 +01001924 record->type = HANDSHAKE_CERTIFICATE;
1925 record->len24_lo = 3;
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01001926
1927 dbg(">> CERTIFICATE\n");
1928 xwrite_and_update_handshake_hash(tls, sizeof(*record));
1929}
1930
Denys Vlasenko11d00962017-01-15 00:12:42 +01001931static void send_client_key_exchange(tls_state_t *tls)
1932{
Denys Vlasenko11d00962017-01-15 00:12:42 +01001933 struct client_key_exchange {
Denys Vlasenko11d00962017-01-15 00:12:42 +01001934 uint8_t type;
1935 uint8_t len24_hi, len24_mid, len24_lo;
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001936 uint8_t key[2 + 4 * 1024]; // size??
Denys Vlasenko11d00962017-01-15 00:12:42 +01001937 };
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01001938//FIXME: better size estimate
Denys Vlasenkod5a04052018-11-13 11:58:53 +01001939 struct client_key_exchange *record = tls_get_zeroed_outbuf(tls, sizeof(*record));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001940 uint8_t premaster[RSA_PREMASTER_SIZE > EC_CURVE_KEYSIZE ? RSA_PREMASTER_SIZE : EC_CURVE_KEYSIZE];
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001941 int premaster_size;
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01001942 int len;
Denys Vlasenko11d00962017-01-15 00:12:42 +01001943
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001944 if (!(tls->flags & NEED_EC_KEY)) {
1945 /* RSA */
1946 if (!(tls->flags & GOT_CERT_RSA_KEY_ALG))
James Byrne69374872019-07-02 11:35:03 +02001947 bb_simple_error_msg("server cert is not RSA");
Denys Vlasenko83e5c622018-11-23 17:21:38 +01001948
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001949 tls_get_random(premaster, RSA_PREMASTER_SIZE);
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001950 if (TLS_DEBUG_FIXED_SECRETS)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001951 memset(premaster, 0x44, RSA_PREMASTER_SIZE);
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001952 // RFC 5246
1953 // "Note: The version number in the PreMasterSecret is the version
1954 // offered by the client in the ClientHello.client_version, not the
1955 // version negotiated for the connection."
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001956 premaster[0] = TLS_MAJ;
1957 premaster[1] = TLS_MIN;
1958 dump_hex("premaster:%s\n", premaster, sizeof(premaster));
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001959 len = psRsaEncryptPub(/*pool:*/ NULL,
1960 /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001961 premaster, /*inlen:*/ RSA_PREMASTER_SIZE,
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001962 record->key + 2, sizeof(record->key) - 2,
1963 data_param_ignored
1964 );
1965 /* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */
1966 record->key[0] = len >> 8;
1967 record->key[1] = len & 0xff;
1968 len += 2;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001969 premaster_size = RSA_PREMASTER_SIZE;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001970 } else {
Denys Vlasenko6b69ab62021-04-26 13:46:36 +02001971 /* ECDHE */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001972 if (!(tls->flags & GOT_EC_KEY))
1973 bb_simple_error_msg_and_die("server did not provide EC key");
1974
Denys Vlasenko6b69ab62021-04-26 13:46:36 +02001975 if (tls->flags & GOT_EC_CURVE_X25519) {
1976 /* ECDHE, curve x25519 */
1977 dbg("computing x25519_premaster\n");
1978 curve_x25519_compute_pubkey_and_premaster(
1979 record->key + 1, premaster,
1980 /*point:*/ tls->hsd->ecc_pub_key32
1981 );
1982 len = CURVE25519_KEYSIZE;
1983 //record->key[0] = len;
1984 //len++;
1985 //premaster_size = CURVE25519_KEYSIZE;
1986 } else {
1987 /* ECDHE, curve P256 */
1988 dbg("computing P256_premaster\n");
1989 curve_P256_compute_pubkey_and_premaster(
1990 record->key + 2, premaster,
1991 /*point:*/ tls->hsd->ecc_pub_key32
1992 );
1993 record->key[1] = 4; /* "uncompressed point" */
1994 len = 1 + P256_KEYSIZE * 2;
1995 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001996 record->key[0] = len;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001997 len++;
Denys Vlasenko6b69ab62021-04-26 13:46:36 +02001998 premaster_size = P256_KEYSIZE; // = CURVE25519_KEYSIZE = 32
Denys Vlasenkobddb6542018-11-13 02:16:24 +01001999 }
2000
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002001 record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE;
Denys Vlasenkod5a04052018-11-13 11:58:53 +01002002 /* record->len24_hi = 0; - already is */
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002003 record->len24_mid = len >> 8;
2004 record->len24_lo = len & 0xff;
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002005 len += 4;
Denys Vlasenko11d00962017-01-15 00:12:42 +01002006
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01002007 dbg(">> CLIENT_KEY_EXCHANGE\n");
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002008 xwrite_and_update_handshake_hash(tls, len);
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002009
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002010 // RFC 5246
2011 // For all key exchange methods, the same algorithm is used to convert
2012 // the pre_master_secret into the master_secret. The pre_master_secret
2013 // should be deleted from memory once the master_secret has been
2014 // computed.
2015 // master_secret = PRF(pre_master_secret, "master secret",
2016 // ClientHello.random + ServerHello.random)
2017 // [0..47];
2018 // The master secret is always exactly 48 bytes in length. The length
2019 // of the premaster secret will vary depending on key exchange method.
Denys Vlasenko89193f92017-01-24 18:08:07 +01002020 prf_hmac_sha256(/*tls,*/
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002021 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
Denys Vlasenkobddb6542018-11-13 02:16:24 +01002022 premaster, premaster_size,
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002023 "master secret",
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002024 tls->hsd->client_and_server_rand32, sizeof(tls->hsd->client_and_server_rand32)
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002025 );
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002026 dump_hex("master secret:%s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01002027
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002028 // RFC 5246
2029 // 6.3. Key Calculation
2030 //
2031 // The Record Protocol requires an algorithm to generate keys required
2032 // by the current connection state (see Appendix A.6) from the security
2033 // parameters provided by the handshake protocol.
2034 //
2035 // The master secret is expanded into a sequence of secure bytes, which
2036 // is then split to a client write MAC key, a server write MAC key, a
2037 // client write encryption key, and a server write encryption key. Each
2038 // of these is generated from the byte sequence in that order. Unused
2039 // values are empty. Some AEAD ciphers may additionally require a
2040 // client write IV and a server write IV (see Section 6.2.3.3).
2041 //
2042 // When keys and MAC keys are generated, the master secret is used as an
2043 // entropy source.
2044 //
2045 // To generate the key material, compute
2046 //
2047 // key_block = PRF(SecurityParameters.master_secret,
2048 // "key expansion",
2049 // SecurityParameters.server_random +
2050 // SecurityParameters.client_random);
2051 //
2052 // until enough output has been generated. Then, the key_block is
2053 // partitioned as follows:
2054 //
2055 // client_write_MAC_key[SecurityParameters.mac_key_length]
2056 // server_write_MAC_key[SecurityParameters.mac_key_length]
2057 // client_write_key[SecurityParameters.enc_key_length]
2058 // server_write_key[SecurityParameters.enc_key_length]
2059 // client_write_IV[SecurityParameters.fixed_iv_length]
2060 // server_write_IV[SecurityParameters.fixed_iv_length]
2061 {
2062 uint8_t tmp64[64];
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +01002063
2064 /* make "server_rand32 + client_rand32" */
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002065 memcpy(&tmp64[0] , &tls->hsd->client_and_server_rand32[32], 32);
2066 memcpy(&tmp64[32], &tls->hsd->client_and_server_rand32[0] , 32);
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01002067
Denys Vlasenko89193f92017-01-24 18:08:07 +01002068 prf_hmac_sha256(/*tls,*/
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002069 tls->client_write_MAC_key, 2 * (tls->MAC_size + tls->key_size + tls->IV_size),
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +01002070 // also fills:
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002071 // server_write_MAC_key[]
2072 // client_write_key[]
2073 // server_write_key[]
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002074 // client_write_IV[]
2075 // server_write_IV[]
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002076 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002077 "key expansion",
2078 tmp64, 64
2079 );
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002080 tls->client_write_key = tls->client_write_MAC_key + (2 * tls->MAC_size);
2081 tls->server_write_key = tls->client_write_key + tls->key_size;
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002082 tls->client_write_IV = tls->server_write_key + tls->key_size;
2083 tls->server_write_IV = tls->client_write_IV + tls->IV_size;
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002084 dump_hex("client_write_MAC_key:%s\n",
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002085 tls->client_write_MAC_key, tls->MAC_size
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002086 );
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +01002087 dump_hex("client_write_key:%s\n",
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002088 tls->client_write_key, tls->key_size
Denys Vlasenkob5dfc3d2017-01-18 20:37:24 +01002089 );
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002090 dump_hex("client_write_IV:%s\n",
2091 tls->client_write_IV, tls->IV_size
2092 );
Denys Vlasenko5e4236d2018-11-23 18:02:44 +01002093
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002094 aes_setkey(&tls->aes_decrypt, tls->server_write_key, tls->key_size);
Denys Vlasenko5e4236d2018-11-23 18:02:44 +01002095 aes_setkey(&tls->aes_encrypt, tls->client_write_key, tls->key_size);
2096 {
2097 uint8_t iv[AES_BLOCK_SIZE];
2098 memset(iv, 0, AES_BLOCK_SIZE);
2099 aes_encrypt_one_block(&tls->aes_encrypt, iv, tls->H);
2100 }
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002101 }
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002102}
2103
Denys Vlasenko84fc6452019-05-21 17:29:24 +02002104static const uint8_t rec_CHANGE_CIPHER_SPEC[] ALIGN1 = {
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002105 RECORD_TYPE_CHANGE_CIPHER_SPEC, TLS_MAJ, TLS_MIN, 00, 01,
2106 01
2107};
2108
Denys Vlasenkoc5540d62017-01-15 02:17:03 +01002109static void send_change_cipher_spec(tls_state_t *tls)
2110{
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002111 dbg(">> CHANGE_CIPHER_SPEC\n");
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002112 xwrite(tls->ofd, rec_CHANGE_CIPHER_SPEC, sizeof(rec_CHANGE_CIPHER_SPEC));
Denys Vlasenkoc5540d62017-01-15 02:17:03 +01002113}
2114
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01002115// 7.4.9. Finished
2116// A Finished message is always sent immediately after a change
2117// cipher spec message to verify that the key exchange and
2118// authentication processes were successful. It is essential that a
2119// change cipher spec message be received between the other handshake
2120// messages and the Finished message.
2121//...
2122// The Finished message is the first one protected with the just
2123// negotiated algorithms, keys, and secrets. Recipients of Finished
2124// messages MUST verify that the contents are correct. Once a side
2125// has sent its Finished message and received and validated the
2126// Finished message from its peer, it may begin to send and receive
2127// application data over the connection.
2128//...
2129// struct {
2130// opaque verify_data[verify_data_length];
2131// } Finished;
2132//
2133// verify_data
2134// PRF(master_secret, finished_label, Hash(handshake_messages))
2135// [0..verify_data_length-1];
2136//
2137// finished_label
2138// For Finished messages sent by the client, the string
2139// "client finished". For Finished messages sent by the server,
2140// the string "server finished".
2141//
2142// Hash denotes a Hash of the handshake messages. For the PRF
2143// defined in Section 5, the Hash MUST be the Hash used as the basis
2144// for the PRF. Any cipher suite which defines a different PRF MUST
2145// also define the Hash to use in the Finished computation.
2146//
2147// In previous versions of TLS, the verify_data was always 12 octets
2148// long. In the current version of TLS, it depends on the cipher
2149// suite. Any cipher suite which does not explicitly specify
2150// verify_data_length has a verify_data_length equal to 12. This
2151// includes all existing cipher suites.
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002152static void send_client_finished(tls_state_t *tls)
2153{
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01002154 struct finished {
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002155 uint8_t type;
2156 uint8_t len24_hi, len24_mid, len24_lo;
2157 uint8_t prf_result[12];
2158 };
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002159 struct finished *record = tls_get_outbuf(tls, sizeof(*record));
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002160 uint8_t handshake_hash[TLS_MAX_MAC_SIZE];
2161 unsigned len;
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002162
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002163 fill_handshake_record_hdr(record, HANDSHAKE_FINISHED, sizeof(*record));
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002164
Denys Vlasenkoeb53d012018-11-25 14:45:55 +01002165 len = sha_end(&tls->hsd->handshake_hash_ctx, handshake_hash);
2166
Denys Vlasenko89193f92017-01-24 18:08:07 +01002167 prf_hmac_sha256(/*tls,*/
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002168 record->prf_result, sizeof(record->prf_result),
2169 tls->hsd->master_secret, sizeof(tls->hsd->master_secret),
2170 "client finished",
2171 handshake_hash, len
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002172 );
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002173 dump_hex("from secret: %s\n", tls->hsd->master_secret, sizeof(tls->hsd->master_secret));
Denys Vlasenkoe2cb3b92017-01-17 16:53:36 +01002174 dump_hex("from labelSeed: %s", "client finished", sizeof("client finished")-1);
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01002175 dump_hex("%s\n", handshake_hash, sizeof(handshake_hash));
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002176 dump_hex("=> digest: %s\n", record->prf_result, sizeof(record->prf_result));
Denys Vlasenko9a6897a2017-01-16 23:26:33 +01002177
Denys Vlasenkoc8ba23b2017-01-18 06:45:50 +01002178 dbg(">> FINISHED\n");
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002179 xwrite_encrypted(tls, sizeof(*record), RECORD_TYPE_HANDSHAKE);
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01002180}
2181
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002182void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002183{
2184 // Client RFC 5246 Server
2185 // (*) - optional messages, not always sent
2186 //
2187 // ClientHello ------->
2188 // ServerHello
2189 // Certificate*
2190 // ServerKeyExchange*
2191 // CertificateRequest*
2192 // <------- ServerHelloDone
2193 // Certificate*
2194 // ClientKeyExchange
2195 // CertificateVerify*
2196 // [ChangeCipherSpec]
2197 // Finished ------->
2198 // [ChangeCipherSpec]
2199 // <------- Finished
2200 // Application Data <------> Application Data
2201 int len;
Denys Vlasenko98066662018-02-06 13:33:00 +01002202 int got_cert_req;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002203
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002204 send_client_hello_and_alloc_hsd(tls, sni);
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01002205 get_server_hello(tls);
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002206
Denys Vlasenko38972a82017-01-20 19:11:14 +01002207 // RFC 5246
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002208 // The server MUST send a Certificate message whenever the agreed-
2209 // upon key exchange method uses certificates for authentication
2210 // (this includes all key exchange methods defined in this document
2211 // except DH_anon). This message will always immediately follow the
2212 // ServerHello message.
2213 //
2214 // IOW: in practice, Certificate *always* follows.
2215 // (for example, kernel.org does not even accept DH_anon cipher id)
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01002216 get_server_cert(tls);
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002217
Denys Vlasenkodd2577f2017-01-20 22:48:41 +01002218 len = tls_xread_handshake_block(tls, 4);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01002219 if (tls->inbuf[RECHDR_LEN] == HANDSHAKE_SERVER_KEY_EXCHANGE) {
Denys Vlasenkob1003f72017-01-14 13:57:16 +01002220 // 459 bytes:
2221 // 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...
2222 //SvKey len=455^
Denys Vlasenko11d00962017-01-15 00:12:42 +01002223 // with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes:
2224 // 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 Vlasenkobddb6542018-11-13 02:16:24 +01002225 //
2226 // RFC 8422 5.4. Server Key Exchange
2227 // This message is sent when using the ECDHE_ECDSA, ECDHE_RSA, and
2228 // ECDH_anon key exchange algorithms.
2229 // This message is used to convey the server's ephemeral ECDH public key
2230 // (and the corresponding elliptic curve domain parameters) to the
2231 // client.
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002232 dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len);
Denys Vlasenkobddb6542018-11-13 02:16:24 +01002233 dump_raw_in("<< %s\n", tls->inbuf, RECHDR_LEN + len);
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002234 if (tls->flags & NEED_EC_KEY)
Denys Vlasenkobddb6542018-11-13 02:16:24 +01002235 process_server_key(tls, len);
2236
2237 // read next handshake block
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01002238 len = tls_xread_handshake_block(tls, 4);
Denys Vlasenkoc5540d62017-01-15 02:17:03 +01002239 }
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002240
Denys Vlasenko98066662018-02-06 13:33:00 +01002241 got_cert_req = (tls->inbuf[RECHDR_LEN] == HANDSHAKE_CERTIFICATE_REQUEST);
2242 if (got_cert_req) {
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01002243 dbg("<< CERTIFICATE_REQUEST\n");
2244 // RFC 5246: "If no suitable certificate is available,
2245 // the client MUST send a certificate message containing no
2246 // certificates. That is, the certificate_list structure has a
2247 // length of zero. ...
2248 // Client certificates are sent using the Certificate structure
2249 // defined in Section 7.4.2."
2250 // (i.e. the same format as server certs)
Denys Vlasenko98066662018-02-06 13:33:00 +01002251
2252 /*send_empty_client_cert(tls); - WRONG (breaks handshake hash calc) */
2253 /* need to hash _all_ server replies first, up to ServerHelloDone */
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01002254 len = tls_xread_handshake_block(tls, 4);
2255 }
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002256
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01002257 if (tls->inbuf[RECHDR_LEN] != HANDSHAKE_SERVER_HELLO_DONE) {
2258 bad_record_die(tls, "'server hello done'", len);
2259 }
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002260 // 0e 000000 (len:0)
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002261 dbg("<< SERVER_HELLO_DONE\n");
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002262
Denys Vlasenko98066662018-02-06 13:33:00 +01002263 if (got_cert_req)
2264 send_empty_client_cert(tls);
2265
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002266 send_client_key_exchange(tls);
2267
2268 send_change_cipher_spec(tls);
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01002269 /* from now on we should send encrypted */
2270 /* tls->write_seq64_be = 0; - already is */
Denys Vlasenkoeb53d012018-11-25 14:45:55 +01002271 tls->flags |= ENCRYPT_ON_WRITE;
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002272
2273 send_client_finished(tls);
2274
2275 /* Get CHANGE_CIPHER_SPEC */
Denys Vlasenko98066662018-02-06 13:33:00 +01002276 len = tls_xread_record(tls, "switch to encrypted traffic");
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002277 if (len != 1 || memcmp(tls->inbuf, rec_CHANGE_CIPHER_SPEC, 6) != 0)
Denys Vlasenko1500b3a2017-01-24 17:06:10 +01002278 bad_record_die(tls, "switch to encrypted traffic", len);
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002279 dbg("<< CHANGE_CIPHER_SPEC\n");
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002280
Denys Vlasenkoca7cdd42018-11-26 00:17:10 +01002281 if (ALLOW_RSA_NULL_SHA256
Denys Vlasenko5d561ef2017-04-04 01:41:15 +02002282 && tls->cipher_id == TLS_RSA_WITH_NULL_SHA256
2283 ) {
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002284 tls->min_encrypted_len_on_read = tls->MAC_size;
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002285 } else
2286 if (!(tls->flags & ENCRYPTION_AESGCM)) {
Denys Vlasenko63bfe0e2018-12-10 16:43:53 +01002287 unsigned mac_blocks = (unsigned)(TLS_MAC_SIZE(tls) + AES_BLOCK_SIZE-1) / AES_BLOCK_SIZE;
Denys Vlasenko89193f92017-01-24 18:08:07 +01002288 /* all incoming packets now should be encrypted and have
2289 * at least IV + (MAC padded to blocksize):
2290 */
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002291 tls->min_encrypted_len_on_read = AES_BLOCK_SIZE + (mac_blocks * AES_BLOCK_SIZE);
2292 } else {
2293 tls->min_encrypted_len_on_read = 8 + AES_BLOCK_SIZE;
Denys Vlasenko89193f92017-01-24 18:08:07 +01002294 }
Denys Vlasenko83e5c622018-11-23 17:21:38 +01002295 dbg("min_encrypted_len_on_read: %u\n", tls->min_encrypted_len_on_read);
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002296
2297 /* Get (encrypted) FINISHED from the server */
Denys Vlasenko98066662018-02-06 13:33:00 +01002298 len = tls_xread_record(tls, "'server finished'");
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01002299 if (len < 4 || tls->inbuf[RECHDR_LEN] != HANDSHAKE_FINISHED)
Denys Vlasenko98066662018-02-06 13:33:00 +01002300 bad_record_die(tls, "'server finished'", len);
Denys Vlasenko5d1662e2017-01-17 18:17:27 +01002301 dbg("<< FINISHED\n");
Denys Vlasenkoe69d78c2017-01-17 17:24:11 +01002302 /* application data can be sent/received */
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002303
2304 /* free handshake data */
Denys Vlasenkoa6192f32018-11-25 16:17:26 +01002305 psRsaKey_clear(&tls->hsd->server_rsa_pub_key);
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002306// if (PARANOIA)
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002307// memset(tls->hsd, 0, tls->hsd->hsd_size);
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002308 free(tls->hsd);
2309 tls->hsd = NULL;
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002310}
2311
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002312static void tls_xwrite(tls_state_t *tls, int len)
2313{
2314 dbg(">> DATA\n");
2315 xwrite_encrypted(tls, len, RECORD_TYPE_APPLICATION_DATA);
2316}
2317
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002318// To run a test server using openssl:
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002319// openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
Denys Vlasenko2eb04292018-11-26 16:39:19 +01002320// openssl s_server -key key.pem -cert server.pem -debug -tls1_2
Denys Vlasenkocccf8e72017-01-19 00:20:45 +01002321//
2322// Unencryped SHA256 example:
2323// openssl req -x509 -newkey rsa:$((4096/4*3)) -keyout key.pem -out server.pem -nodes -days 99999 -subj '/CN=localhost'
Denys Vlasenko2eb04292018-11-26 16:39:19 +01002324// openssl s_server -key key.pem -cert server.pem -debug -tls1_2 -cipher NULL
2325// openssl s_client -connect 127.0.0.1:4433 -debug -tls1_2 -cipher NULL-SHA256
Denys Vlasenko936e83e2017-01-16 04:25:01 +01002326
Denys Vlasenko403f2992018-02-06 15:15:08 +01002327void FAST_FUNC tls_run_copy_loop(tls_state_t *tls, unsigned flags)
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002328{
Denys Vlasenko38972a82017-01-20 19:11:14 +01002329 int inbuf_size;
2330 const int INBUF_STEP = 4 * 1024;
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002331 struct pollfd pfds[2];
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002332
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002333 pfds[0].fd = STDIN_FILENO;
2334 pfds[0].events = POLLIN;
2335 pfds[1].fd = tls->ifd;
2336 pfds[1].events = POLLIN;
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002337
Denys Vlasenko38972a82017-01-20 19:11:14 +01002338 inbuf_size = INBUF_STEP;
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002339 for (;;) {
2340 int nread;
2341
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002342 if (safe_poll(pfds, 2, -1) < 0)
James Byrne69374872019-07-02 11:35:03 +02002343 bb_simple_perror_msg_and_die("poll");
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002344
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002345 if (pfds[0].revents) {
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01002346 void *buf;
2347
2348 dbg("STDIN HAS DATA\n");
Denys Vlasenko38972a82017-01-20 19:11:14 +01002349 buf = tls_get_outbuf(tls, inbuf_size);
2350 nread = safe_read(STDIN_FILENO, buf, inbuf_size);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002351 if (nread < 1) {
Denys Vlasenko38972a82017-01-20 19:11:14 +01002352 /* We'd want to do this: */
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002353 /* Close outgoing half-connection so they get EOF,
Denys Vlasenko38972a82017-01-20 19:11:14 +01002354 * but leave incoming alone so we can see response
2355 */
Denys Vlasenko9a647c32017-01-23 01:08:16 +01002356 //shutdown(tls->ofd, SHUT_WR);
Denys Vlasenko38972a82017-01-20 19:11:14 +01002357 /* But TLS has no way to encode this,
2358 * doubt it's ok to do it "raw"
2359 */
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002360 pfds[0].fd = -1;
Denys Vlasenko39161392017-01-20 20:27:06 +01002361 tls_free_outbuf(tls); /* mem usage optimization */
Denys Vlasenko403f2992018-02-06 15:15:08 +01002362 if (flags & TLSLOOP_EXIT_ON_LOCAL_EOF)
2363 break;
Denys Vlasenko38972a82017-01-20 19:11:14 +01002364 } else {
2365 if (nread == inbuf_size) {
2366 /* TLS has per record overhead, if input comes fast,
2367 * read, encrypt and send bigger chunks
2368 */
2369 inbuf_size += INBUF_STEP;
Denys Vlasenko49ecee02017-01-24 16:00:54 +01002370 if (inbuf_size > TLS_MAX_OUTBUF)
2371 inbuf_size = TLS_MAX_OUTBUF;
Denys Vlasenko38972a82017-01-20 19:11:14 +01002372 }
2373 tls_xwrite(tls, nread);
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002374 }
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002375 }
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002376 if (pfds[1].revents) {
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01002377 dbg("NETWORK HAS DATA\n");
Denys Vlasenko38972a82017-01-20 19:11:14 +01002378 read_record:
Denys Vlasenko98066662018-02-06 13:33:00 +01002379 nread = tls_xread_record(tls, "encrypted data");
Denys Vlasenko38972a82017-01-20 19:11:14 +01002380 if (nread < 1) {
2381 /* TLS protocol has no real concept of one-sided shutdowns:
2382 * if we get "TLS EOF" from the peer, writes will fail too
2383 */
Denys Vlasenko0ec4d082017-02-16 16:27:39 +01002384 //pfds[1].fd = -1;
Denys Vlasenko38972a82017-01-20 19:11:14 +01002385 //close(STDOUT_FILENO);
Denys Vlasenko39161392017-01-20 20:27:06 +01002386 //tls_free_inbuf(tls); /* mem usage optimization */
Denys Vlasenko38972a82017-01-20 19:11:14 +01002387 //continue;
2388 break;
2389 }
2390 if (tls->inbuf[0] != RECORD_TYPE_APPLICATION_DATA)
Denys Vlasenko98066662018-02-06 13:33:00 +01002391 bad_record_die(tls, "encrypted data", nread);
Denys Vlasenkoa0aae9f2017-01-20 14:12:10 +01002392 xwrite(STDOUT_FILENO, tls->inbuf + RECHDR_LEN, nread);
Denys Vlasenko38972a82017-01-20 19:11:14 +01002393 /* We may already have a complete next record buffered,
2394 * can process it without network reads (and possible blocking)
2395 */
2396 if (tls_has_buffered_record(tls))
2397 goto read_record;
Denys Vlasenkoabbf17a2017-01-20 03:15:09 +01002398 }
2399 }
Denys Vlasenkoceff6b02017-01-14 12:49:32 +01002400}