diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-13 02:16:24 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-11-13 02:17:54 +0100 |
commit | bddb6545a982696bde417a9ae621f9e2d3c22b3d (patch) | |
tree | bacdebc4431ff1c438b41f9ae7341ef06b78ad94 /networking/tls.c | |
parent | 8767c12774d9392565f20d3ea4e28bb1b6075a31 (diff) | |
download | busybox-w32-bddb6545a982696bde417a9ae621f9e2d3c22b3d.tar.gz busybox-w32-bddb6545a982696bde417a9ae621f9e2d3c22b3d.tar.bz2 busybox-w32-bddb6545a982696bde417a9ae621f9e2d3c22b3d.zip |
tls: add support for ECDHE-ECDSA-AES-128-CBC-SHA and x25519 curve
function old new delta
curve25519 - 835 +835
tls_handshake 1619 1935 +316
xc_diffadd - 230 +230
fe_mul__distinct - 149 +149
lm_sub - 103 +103
lm_add - 82 +82
fe_mul_c - 74 +74
fe_select - 45 +45
static.f25519_one - 32 +32
static.basepoint9 - 32 +32
static.OID_ECDSA_KEY_ALG - 21 +21
static.OID_RSA_KEY_ALG - 13 +13
static.supported_groups - 8 +8
static.empty_client_cert - 7 +7
der_binary_to_pstm 40 42 +2
static.expected 13 - -13
------------------------------------------------------------------------------
(add/remove: 14/1 grow/shrink: 2/0 up/down: 1949/-13) Total: 1936 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/tls.c')
-rw-r--r-- | networking/tls.c | 334 |
1 files changed, 255 insertions, 79 deletions
diff --git a/networking/tls.c b/networking/tls.c index 0f637a3d7..694fbf34d 100644 --- a/networking/tls.c +++ b/networking/tls.c | |||
@@ -12,8 +12,9 @@ | |||
12 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_montgomery_reduce.o | 12 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_montgomery_reduce.o |
13 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_mul_comba.o | 13 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_mul_comba.o |
14 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_sqr_comba.o | 14 | //kbuild:lib-$(CONFIG_TLS) += tls_pstm_sqr_comba.o |
15 | //kbuild:lib-$(CONFIG_TLS) += tls_rsa.o | ||
16 | //kbuild:lib-$(CONFIG_TLS) += tls_aes.o | 15 | //kbuild:lib-$(CONFIG_TLS) += tls_aes.o |
16 | //kbuild:lib-$(CONFIG_TLS) += tls_rsa.o | ||
17 | //kbuild:lib-$(CONFIG_TLS) += tls_fe.o | ||
17 | ////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o | 18 | ////kbuild:lib-$(CONFIG_TLS) += tls_aes_gcm.o |
18 | 19 | ||
19 | #include "tls.h" | 20 | #include "tls.h" |
@@ -57,6 +58,7 @@ | |||
57 | #define CIPHER_ID2 TLS_RSA_WITH_AES_128_CBC_SHA | 58 | #define CIPHER_ID2 TLS_RSA_WITH_AES_128_CBC_SHA |
58 | 59 | ||
59 | // bug #11456: host is.gd accepts only ECDHE-ECDSA-foo (the simplest which works: ECDHE-ECDSA-AES128-SHA 0xC009) | 60 | // bug #11456: host is.gd accepts only ECDHE-ECDSA-foo (the simplest which works: ECDHE-ECDSA-AES128-SHA 0xC009) |
61 | #define CIPHER_ID3 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA | ||
60 | 62 | ||
61 | 63 | ||
62 | #define TLS_DEBUG 0 | 64 | #define TLS_DEBUG 0 |
@@ -260,15 +262,22 @@ struct record_hdr { | |||
260 | uint8_t len16_hi, len16_lo; | 262 | uint8_t len16_hi, len16_lo; |
261 | }; | 263 | }; |
262 | 264 | ||
265 | enum { | ||
266 | KEY_ALG_RSA, | ||
267 | KEY_ALG_ECDSA, | ||
268 | }; | ||
263 | struct tls_handshake_data { | 269 | struct tls_handshake_data { |
264 | /* In bbox, md5/sha1/sha256 ctx's are the same structure */ | 270 | /* In bbox, md5/sha1/sha256 ctx's are the same structure */ |
265 | md5sha_ctx_t handshake_hash_ctx; | 271 | md5sha_ctx_t handshake_hash_ctx; |
266 | 272 | ||
267 | uint8_t client_and_server_rand32[2 * 32]; | 273 | uint8_t client_and_server_rand32[2 * 32]; |
268 | uint8_t master_secret[48]; | 274 | uint8_t master_secret[48]; |
275 | |||
276 | smallint key_alg; | ||
269 | //TODO: store just the DER key here, parse/use/delete it when sending client key | 277 | //TODO: store just the DER key here, parse/use/delete it when sending client key |
270 | //this way it will stay key type agnostic here. | 278 | //this way it will stay key type agnostic here. |
271 | psRsaKey_t server_rsa_pub_key; | 279 | psRsaKey_t server_rsa_pub_key; |
280 | uint8_t ecc_pub_key32[32]; | ||
272 | 281 | ||
273 | unsigned saved_client_hello_size; | 282 | unsigned saved_client_hello_size; |
274 | uint8_t saved_client_hello[1]; | 283 | uint8_t saved_client_hello[1]; |
@@ -1022,15 +1031,25 @@ static uint8_t *skip_der_item(uint8_t *der, uint8_t *end) | |||
1022 | return new_der; | 1031 | return new_der; |
1023 | } | 1032 | } |
1024 | 1033 | ||
1034 | // | ||
1035 | static void binary_to_pstm(pstm_int *pstm_n, uint8_t *bin_ptr, unsigned len) | ||
1036 | { | ||
1037 | pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len); | ||
1038 | pstm_read_unsigned_bin(pstm_n, bin_ptr, len); | ||
1039 | //return bin_ptr + len; | ||
1040 | } | ||
1041 | // | ||
1042 | |||
1025 | static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end) | 1043 | static void der_binary_to_pstm(pstm_int *pstm_n, uint8_t *der, uint8_t *end) |
1026 | { | 1044 | { |
1027 | uint8_t *bin_ptr; | 1045 | uint8_t *bin_ptr; |
1028 | unsigned len = get_der_len(&bin_ptr, der, end); | 1046 | unsigned len = get_der_len(&bin_ptr, der, end); |
1029 | 1047 | ||
1030 | dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]); | 1048 | dbg_der("binary bytes:%u, first:0x%02x\n", len, bin_ptr[0]); |
1031 | pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len); | 1049 | binary_to_pstm(pstm_n, bin_ptr, len); |
1032 | pstm_read_unsigned_bin(pstm_n, bin_ptr, len); | 1050 | //pstm_init_for_read_unsigned_bin(/*pool:*/ NULL, pstm_n, len); |
1033 | //return bin + len; | 1051 | //pstm_read_unsigned_bin(pstm_n, bin_ptr, len); |
1052 | ////return bin_ptr + len; | ||
1034 | } | 1053 | } |
1035 | 1054 | ||
1036 | static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) | 1055 | static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) |
@@ -1113,6 +1132,18 @@ static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) | |||
1113 | * publicKey (BIT STRING) | 1132 | * publicKey (BIT STRING) |
1114 | * | 1133 | * |
1115 | * We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey | 1134 | * We need Certificate.tbsCertificate.subjectPublicKeyInfo.publicKey |
1135 | * | ||
1136 | * Example of an ECDSA key: | ||
1137 | * SEQ 0x59 bytes (subjectPublicKeyInfo): 3059 | ||
1138 | * SEQ 0x13 bytes (algorithm): 3013 | ||
1139 | * OID 7 bytes: 0607 2a8648ce3d0201 (OID_ECDSA_KEY_ALG 42.134.72.206.61.2.1) | ||
1140 | * OID 8 bytes: 0608 2a8648ce3d030107 (OID_EC_prime256v1 42.134.72.206.61.3.1.7) | ||
1141 | * BITSTRING 0x42 bytes (publicKey): 0342 | ||
1142 | * 0004 53af f65e 50cc 7959 7e29 0171 c75c | ||
1143 | * 7335 e07d f45b 9750 b797 3a38 aebb 2ac6 | ||
1144 | * 8329 2748 e77e 41cb d482 2ce6 05ec a058 | ||
1145 | * f3ab d561 2f4c d845 9ad3 7252 e3de bd3b | ||
1146 | * 9012 | ||
1116 | */ | 1147 | */ |
1117 | uint8_t *end = der + len; | 1148 | uint8_t *end = der + len; |
1118 | 1149 | ||
@@ -1147,40 +1178,61 @@ static void find_key_in_der_cert(tls_state_t *tls, uint8_t *der, int len) | |||
1147 | /* enter subjectPublicKeyInfo */ | 1178 | /* enter subjectPublicKeyInfo */ |
1148 | der = enter_der_item(der, &end); | 1179 | der = enter_der_item(der, &end); |
1149 | { /* check subjectPublicKeyInfo.algorithm */ | 1180 | { /* check subjectPublicKeyInfo.algorithm */ |
1150 | static const uint8_t expected[] = { | 1181 | static const uint8_t OID_RSA_KEY_ALG[] = { |
1151 | 0x30,0x0d, // SEQ 13 bytes | 1182 | 0x30,0x0d, // SEQ 13 bytes |
1152 | 0x06,0x09, 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, // OID RSA_KEY_ALG 42.134.72.134.247.13.1.1.1 | 1183 | 0x06,0x09, 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, // OID RSA_KEY_ALG 42.134.72.134.247.13.1.1.1 |
1153 | //0x05,0x00, // NULL | 1184 | //0x05,0x00, // NULL |
1154 | }; | 1185 | }; |
1155 | if (memcmp(der, expected, sizeof(expected)) != 0) | 1186 | static const uint8_t OID_ECDSA_KEY_ALG[] = { |
1156 | bb_error_msg_and_die("not RSA key"); | 1187 | 0x30,0x13, // SEQ 0x13 bytes |
1188 | 0x06,0x07, 0x2a,0x86,0x48,0xce,0x3d,0x02,0x01, //OID_ECDSA_KEY_ALG 42.134.72.206.61.2.1 | ||
1189 | 0x06,0x08, 0x2a,0x86,0x48,0xce,0x3d,0x03,0x01,0x07, //OID_EC_prime256v1 42.134.72.206.61.3.1.7 | ||
1190 | //rfc3279: | ||
1191 | //42.134.72.206.61.3 is ellipticCurve | ||
1192 | //42.134.72.206.61.3.0 is c-TwoCurve | ||
1193 | //42.134.72.206.61.3.1 is primeCurve | ||
1194 | //42.134.72.206.61.3.1.7 is prime256v1 | ||
1195 | }; | ||
1196 | if (memcmp(der, OID_RSA_KEY_ALG, sizeof(OID_RSA_KEY_ALG)) == 0) { | ||
1197 | dbg("RSA key\n"); | ||
1198 | tls->hsd->key_alg = KEY_ALG_RSA; | ||
1199 | } else | ||
1200 | if (memcmp(der, OID_ECDSA_KEY_ALG, sizeof(OID_ECDSA_KEY_ALG)) == 0) { | ||
1201 | dbg("ECDSA key\n"); | ||
1202 | tls->hsd->key_alg = KEY_ALG_ECDSA; | ||
1203 | } else | ||
1204 | bb_error_msg_and_die("not RSA or ECDSA key"); | ||
1157 | } | 1205 | } |
1158 | /* skip subjectPublicKeyInfo.algorithm */ | ||
1159 | der = skip_der_item(der, end); | ||
1160 | /* enter subjectPublicKeyInfo.publicKey */ | ||
1161 | // die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */ | ||
1162 | der = enter_der_item(der, &end); | ||
1163 | 1206 | ||
1164 | /* parse RSA key: */ | 1207 | if (tls->hsd->key_alg == KEY_ALG_RSA) { |
1165 | //based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note | 1208 | /* parse RSA key: */ |
1166 | dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]); | 1209 | //based on getAsnRsaPubKey(), pkcs1ParsePrivBin() is also of note |
1167 | if (end - der < 14) xfunc_die(); | 1210 | /* skip subjectPublicKeyInfo.algorithm */ |
1168 | /* example format: | 1211 | der = skip_der_item(der, end); |
1169 | * ignore bits: 00 | 1212 | /* enter subjectPublicKeyInfo.publicKey */ |
1170 | * SEQ 0x018a/394 bytes: 3082018a | 1213 | // die_if_not_this_der_type(der, end, 0x03); /* must be BITSTRING */ |
1171 | * INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX | 1214 | der = enter_der_item(der, &end); |
1172 | * INTEGER 3 bytes (exponent): 0203 010001 | 1215 | |
1173 | */ | 1216 | dbg("key bytes:%u, first:0x%02x\n", (int)(end - der), der[0]); |
1174 | if (*der != 0) /* "ignore bits", should be 0 */ | 1217 | if (end - der < 14) |
1175 | xfunc_die(); | 1218 | xfunc_die(); |
1176 | der++; | 1219 | /* example format: |
1177 | der = enter_der_item(der, &end); /* enter SEQ */ | 1220 | * ignore bits: 00 |
1178 | /* memset(tls->hsd->server_rsa_pub_key, 0, sizeof(tls->hsd->server_rsa_pub_key)); - already is */ | 1221 | * SEQ 0x018a/394 bytes: 3082018a |
1179 | der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.N, der, end); /* modulus */ | 1222 | * INTEGER 0x0181/385 bytes (modulus): 02820181 XX...XXX |
1180 | der = skip_der_item(der, end); | 1223 | * INTEGER 3 bytes (exponent): 0203 010001 |
1181 | der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.e, der, end); /* exponent */ | 1224 | */ |
1182 | tls->hsd->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->hsd->server_rsa_pub_key.N); | 1225 | if (*der != 0) /* "ignore bits", should be 0 */ |
1183 | dbg("server_rsa_pub_key.size:%d\n", tls->hsd->server_rsa_pub_key.size); | 1226 | xfunc_die(); |
1227 | der++; | ||
1228 | der = enter_der_item(der, &end); /* enter SEQ */ | ||
1229 | /* memset(tls->hsd->server_rsa_pub_key, 0, sizeof(tls->hsd->server_rsa_pub_key)); - already is */ | ||
1230 | der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.N, der, end); /* modulus */ | ||
1231 | der = skip_der_item(der, end); | ||
1232 | der_binary_to_pstm(&tls->hsd->server_rsa_pub_key.e, der, end); /* exponent */ | ||
1233 | tls->hsd->server_rsa_pub_key.size = pstm_unsigned_bin_size(&tls->hsd->server_rsa_pub_key.N); | ||
1234 | dbg("server_rsa_pub_key.size:%d\n", tls->hsd->server_rsa_pub_key.size); | ||
1235 | } | ||
1184 | } | 1236 | } |
1185 | 1237 | ||
1186 | /* | 1238 | /* |
@@ -1217,6 +1269,22 @@ static ALWAYS_INLINE void fill_handshake_record_hdr(void *buf, unsigned type, un | |||
1217 | 1269 | ||
1218 | static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | 1270 | static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) |
1219 | { | 1271 | { |
1272 | static const uint8_t supported_groups[] = { | ||
1273 | 0x00,0x0a, //extension_type: "supported_groups" | ||
1274 | 0x00,0x04, //ext len | ||
1275 | 0x00,0x02, //list len | ||
1276 | 0x00,0x1d, //curve_x25519 (rfc7748) | ||
1277 | //0x00,0x17, //curve_secp256r1 | ||
1278 | //0x00,0x18, //curve_secp384r1 | ||
1279 | //0x00,0x19, //curve_secp521r1 | ||
1280 | }; | ||
1281 | //static const uint8_t signature_algorithms[] = { | ||
1282 | // 000d | ||
1283 | // 0020 | ||
1284 | // 001e | ||
1285 | // 0601 0602 0603 0501 0502 0503 0401 0402 0403 0301 0302 0303 0201 0202 0203 | ||
1286 | //}; | ||
1287 | |||
1220 | struct client_hello { | 1288 | struct client_hello { |
1221 | uint8_t type; | 1289 | uint8_t type; |
1222 | uint8_t len24_hi, len24_mid, len24_lo; | 1290 | uint8_t len24_hi, len24_mid, len24_lo; |
@@ -1225,7 +1293,7 @@ static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | |||
1225 | uint8_t session_id_len; | 1293 | uint8_t session_id_len; |
1226 | /* uint8_t session_id[]; */ | 1294 | /* uint8_t session_id[]; */ |
1227 | uint8_t cipherid_len16_hi, cipherid_len16_lo; | 1295 | uint8_t cipherid_len16_hi, cipherid_len16_lo; |
1228 | uint8_t cipherid[2 * (2 + !!CIPHER_ID2)]; /* actually variable */ | 1296 | uint8_t cipherid[2 * (2 + !!CIPHER_ID2 + !!CIPHER_ID3)]; /* actually variable */ |
1229 | uint8_t comprtypes_len; | 1297 | uint8_t comprtypes_len; |
1230 | uint8_t comprtypes[1]; /* actually variable */ | 1298 | uint8_t comprtypes[1]; /* actually variable */ |
1231 | /* Extensions (SNI shown): | 1299 | /* Extensions (SNI shown): |
@@ -1250,12 +1318,19 @@ static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | |||
1250 | // 0017 0000 - extended master secret | 1318 | // 0017 0000 - extended master secret |
1251 | }; | 1319 | }; |
1252 | struct client_hello *record; | 1320 | struct client_hello *record; |
1321 | uint8_t *ptr; | ||
1253 | int len; | 1322 | int len; |
1254 | int sni_len = sni ? strnlen(sni, 127 - 9) : 0; | 1323 | int ext_len; |
1324 | int sni_len = sni ? strnlen(sni, 127 - 5) : 0; | ||
1255 | 1325 | ||
1256 | len = sizeof(*record); | 1326 | ext_len = 0; |
1327 | /* is.gd responds with "handshake failure" to our hello if there's no supported_groups element */ | ||
1328 | ext_len += sizeof(supported_groups); | ||
1257 | if (sni_len) | 1329 | if (sni_len) |
1258 | len += 11 + sni_len; | 1330 | ext_len += 9 + sni_len; |
1331 | |||
1332 | /* +2 is for "len of all extensions" 2-byte field */ | ||
1333 | len = sizeof(*record) + 2 + ext_len; | ||
1259 | record = tls_get_outbuf(tls, len); | 1334 | record = tls_get_outbuf(tls, len); |
1260 | memset(record, 0, len); | 1335 | memset(record, 0, len); |
1261 | 1336 | ||
@@ -1278,25 +1353,30 @@ static void send_client_hello_and_alloc_hsd(tls_state_t *tls, const char *sni) | |||
1278 | if ((CIPHER_ID2 >> 8) != 0) record->cipherid[4] = CIPHER_ID2 >> 8; | 1353 | if ((CIPHER_ID2 >> 8) != 0) record->cipherid[4] = CIPHER_ID2 >> 8; |
1279 | /*************************/ record->cipherid[5] = CIPHER_ID2 & 0xff; | 1354 | /*************************/ record->cipherid[5] = CIPHER_ID2 & 0xff; |
1280 | #endif | 1355 | #endif |
1356 | #if CIPHER_ID3 | ||
1357 | if ((CIPHER_ID3 >> 8) != 0) record->cipherid[6] = CIPHER_ID3 >> 8; | ||
1358 | /*************************/ record->cipherid[7] = CIPHER_ID3 & 0xff; | ||
1359 | #endif | ||
1281 | 1360 | ||
1282 | record->comprtypes_len = 1; | 1361 | record->comprtypes_len = 1; |
1283 | /* record->comprtypes[0] = 0; */ | 1362 | /* record->comprtypes[0] = 0; */ |
1284 | 1363 | ||
1364 | ptr = (void*)(record + 1); | ||
1365 | *ptr++ = ext_len >> 8; | ||
1366 | *ptr++ = ext_len; | ||
1285 | if (sni_len) { | 1367 | if (sni_len) { |
1286 | uint8_t *p = (void*)(record + 1); | 1368 | //ptr[0] = 0; // |
1287 | //p[0] = 0; // | 1369 | //ptr[1] = 0; //extension_type |
1288 | p[1] = sni_len + 9; //ext_len | 1370 | //ptr[2] = 0; // |
1289 | //p[2] = 0; // | 1371 | ptr[3] = sni_len + 5; //list len |
1290 | //p[3] = 0; //extension_type | 1372 | //ptr[4] = 0; // |
1291 | //p[4] = 0; // | 1373 | ptr[5] = sni_len + 3; //len of 1st SNI |
1292 | p[5] = sni_len + 5; //list len | 1374 | //ptr[6] = 0; //name type |
1293 | //p[6] = 0; // | 1375 | //ptr[7] = 0; // |
1294 | p[7] = sni_len + 3; //len of 1st SNI | 1376 | ptr[8] = sni_len; //name len |
1295 | //p[8] = 0; //name type | 1377 | ptr = mempcpy(&ptr[9], sni, sni_len); |
1296 | //p[9] = 0; // | ||
1297 | p[10] = sni_len; //name len | ||
1298 | memcpy(&p[11], sni, sni_len); | ||
1299 | } | 1378 | } |
1379 | mempcpy(ptr, supported_groups, sizeof(supported_groups)); | ||
1300 | 1380 | ||
1301 | dbg(">> CLIENT_HELLO\n"); | 1381 | dbg(">> CLIENT_HELLO\n"); |
1302 | /* Can hash it only when we know which MAC hash to use */ | 1382 | /* Can hash it only when we know which MAC hash to use */ |
@@ -1373,7 +1453,9 @@ static void get_server_hello(tls_state_t *tls) | |||
1373 | tls->cipher_id = cipher = 0x100 * cipherid[0] + cipherid[1]; | 1453 | tls->cipher_id = cipher = 0x100 * cipherid[0] + cipherid[1]; |
1374 | dbg("server chose cipher %04x\n", cipher); | 1454 | dbg("server chose cipher %04x\n", cipher); |
1375 | 1455 | ||
1376 | if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA) { | 1456 | if (cipher == TLS_RSA_WITH_AES_128_CBC_SHA |
1457 | || cipher == TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA | ||
1458 | ) { | ||
1377 | tls->key_size = AES128_KEYSIZE; | 1459 | tls->key_size = AES128_KEYSIZE; |
1378 | tls->MAC_size = SHA1_OUTSIZE; | 1460 | tls->MAC_size = SHA1_OUTSIZE; |
1379 | } | 1461 | } |
@@ -1425,6 +1507,55 @@ static void get_server_cert(tls_state_t *tls) | |||
1425 | find_key_in_der_cert(tls, certbuf + 10, len); | 1507 | find_key_in_der_cert(tls, certbuf + 10, len); |
1426 | } | 1508 | } |
1427 | 1509 | ||
1510 | /* On input, len is known to be >= 4. | ||
1511 | * The record is known to be SERVER_KEY_EXCHANGE. | ||
1512 | */ | ||
1513 | static void process_server_key(tls_state_t *tls, int len) | ||
1514 | { | ||
1515 | struct record_hdr *xhdr; | ||
1516 | uint8_t *keybuf; | ||
1517 | int len1; | ||
1518 | uint32_t t32; | ||
1519 | |||
1520 | xhdr = (void*)tls->inbuf; | ||
1521 | keybuf = (void*)(xhdr + 1); | ||
1522 | //seen from is.gd: it selects curve_x25519: | ||
1523 | // 0c 00006e //SERVER_KEY_EXCHANGE | ||
1524 | // 03 //curve_type: named curve | ||
1525 | // 001d //curve_x25519 | ||
1526 | //server-chosen EC point, and then signed_params | ||
1527 | // (rfc8422: "A hash of the params, with the signature | ||
1528 | // appropriate to that hash applied. The private key corresponding | ||
1529 | // to the certified public key in the server's Certificate message is | ||
1530 | // used for signing.") | ||
1531 | //follow. Format unclear/guessed: | ||
1532 | // 20 //eccPubKeyLen | ||
1533 | // 25511923d73b70dd2f60e66ba2f3fda31a9c25170963c7a3a972e481dbb2835d //eccPubKey (32bytes) | ||
1534 | // 0203 //hashSigAlg: 2:SHA1 (4:SHA256 5:SHA384 6:SHA512), 3:ECDSA (1:RSA) | ||
1535 | // 0046 //len (16bit) | ||
1536 | // 30 44 //SEQ, len | ||
1537 | // 02 20 //INTEGER, len | ||
1538 | // 2e18e7c2a9badd0a70cd3059a6ab114539b9f5163568911147386cd77ed7c412 //32bytes | ||
1539 | //this item ^^^^^ is sometimes 33 bytes (with all container sizes also +1) | ||
1540 | // 02 20 //INTEGER, len | ||
1541 | // 64523d6216cb94c43c9b20e377d8c52c55be6703fd6730a155930c705eaf3af6 //32bytes | ||
1542 | //same about this item ^^^^^ | ||
1543 | /* Get and verify length */ | ||
1544 | len1 = get24be(keybuf + 1); | ||
1545 | if (len1 > len - 4) tls_error_die(tls); | ||
1546 | len = len1; | ||
1547 | if (len < (1+2+1+32)) tls_error_die(tls); | ||
1548 | keybuf += 4; | ||
1549 | |||
1550 | /* So far we only support curve_x25519 */ | ||
1551 | move_from_unaligned32(t32, keybuf); | ||
1552 | if (t32 != htonl(0x03001d20)) | ||
1553 | tls_error_die(tls); | ||
1554 | |||
1555 | memcpy(tls->hsd->ecc_pub_key32, keybuf + 4, 32); | ||
1556 | dbg("got eccPubKey\n"); | ||
1557 | } | ||
1558 | |||
1428 | static void send_empty_client_cert(tls_state_t *tls) | 1559 | static void send_empty_client_cert(tls_state_t *tls) |
1429 | { | 1560 | { |
1430 | struct client_empty_cert { | 1561 | struct client_empty_cert { |
@@ -1433,13 +1564,18 @@ static void send_empty_client_cert(tls_state_t *tls) | |||
1433 | uint8_t cert_chain_len24_hi, cert_chain_len24_mid, cert_chain_len24_lo; | 1564 | uint8_t cert_chain_len24_hi, cert_chain_len24_mid, cert_chain_len24_lo; |
1434 | }; | 1565 | }; |
1435 | struct client_empty_cert *record; | 1566 | struct client_empty_cert *record; |
1567 | static const uint8_t empty_client_cert[] = { | ||
1568 | HANDSHAKE_CERTIFICATE, | ||
1569 | 0, 0, 3, //len24 | ||
1570 | 0, 0, 0, //cert_chain_len24 | ||
1571 | }; | ||
1436 | 1572 | ||
1437 | record = tls_get_outbuf(tls, sizeof(*record)); | 1573 | record = tls_get_outbuf(tls, sizeof(*record)); |
1438 | //FIXME: can just memcpy a ready-made one. | 1574 | //fill_handshake_record_hdr(record, HANDSHAKE_CERTIFICATE, sizeof(*record)); |
1439 | fill_handshake_record_hdr(record, HANDSHAKE_CERTIFICATE, sizeof(*record)); | 1575 | //record->cert_chain_len24_hi = 0; |
1440 | record->cert_chain_len24_hi = 0; | 1576 | //record->cert_chain_len24_mid = 0; |
1441 | record->cert_chain_len24_mid = 0; | 1577 | //record->cert_chain_len24_lo = 0; |
1442 | record->cert_chain_len24_lo = 0; | 1578 | memcpy(record, empty_client_cert, sizeof(empty_client_cert)); |
1443 | 1579 | ||
1444 | dbg(">> CERTIFICATE\n"); | 1580 | dbg(">> CERTIFICATE\n"); |
1445 | xwrite_and_update_handshake_hash(tls, sizeof(*record)); | 1581 | xwrite_and_update_handshake_hash(tls, sizeof(*record)); |
@@ -1450,34 +1586,63 @@ static void send_client_key_exchange(tls_state_t *tls) | |||
1450 | struct client_key_exchange { | 1586 | struct client_key_exchange { |
1451 | uint8_t type; | 1587 | uint8_t type; |
1452 | uint8_t len24_hi, len24_mid, len24_lo; | 1588 | uint8_t len24_hi, len24_mid, len24_lo; |
1453 | /* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */ | 1589 | uint8_t key[2 + 4 * 1024]; // size?? |
1454 | uint8_t keylen16_hi, keylen16_lo; | ||
1455 | uint8_t key[4 * 1024]; // size?? | ||
1456 | }; | 1590 | }; |
1457 | //FIXME: better size estimate | 1591 | //FIXME: better size estimate |
1458 | struct client_key_exchange *record = tls_get_outbuf(tls, sizeof(*record)); | 1592 | struct client_key_exchange *record = tls_get_outbuf(tls, sizeof(*record)); |
1459 | uint8_t rsa_premaster[RSA_PREMASTER_SIZE]; | 1593 | uint8_t rsa_premaster[RSA_PREMASTER_SIZE]; |
1594 | uint8_t x25519_premaster[CURVE25519_KEYSIZE]; | ||
1595 | uint8_t *premaster; | ||
1596 | int premaster_size; | ||
1460 | int len; | 1597 | int len; |
1461 | 1598 | ||
1462 | tls_get_random(rsa_premaster, sizeof(rsa_premaster)); | 1599 | if (tls->hsd->key_alg == KEY_ALG_RSA) { |
1463 | if (TLS_DEBUG_FIXED_SECRETS) | 1600 | tls_get_random(rsa_premaster, sizeof(rsa_premaster)); |
1464 | memset(rsa_premaster, 0x44, sizeof(rsa_premaster)); | 1601 | if (TLS_DEBUG_FIXED_SECRETS) |
1465 | // RFC 5246 | 1602 | memset(rsa_premaster, 0x44, sizeof(rsa_premaster)); |
1466 | // "Note: The version number in the PreMasterSecret is the version | 1603 | // RFC 5246 |
1467 | // offered by the client in the ClientHello.client_version, not the | 1604 | // "Note: The version number in the PreMasterSecret is the version |
1468 | // version negotiated for the connection." | 1605 | // offered by the client in the ClientHello.client_version, not the |
1469 | rsa_premaster[0] = TLS_MAJ; | 1606 | // version negotiated for the connection." |
1470 | rsa_premaster[1] = TLS_MIN; | 1607 | rsa_premaster[0] = TLS_MAJ; |
1471 | dump_hex("premaster:%s\n", rsa_premaster, sizeof(rsa_premaster)); | 1608 | rsa_premaster[1] = TLS_MIN; |
1472 | len = psRsaEncryptPub(/*pool:*/ NULL, | 1609 | dump_hex("premaster:%s\n", rsa_premaster, sizeof(rsa_premaster)); |
1473 | /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key, | 1610 | len = psRsaEncryptPub(/*pool:*/ NULL, |
1474 | rsa_premaster, /*inlen:*/ sizeof(rsa_premaster), | 1611 | /* psRsaKey_t* */ &tls->hsd->server_rsa_pub_key, |
1475 | record->key, sizeof(record->key), | 1612 | rsa_premaster, /*inlen:*/ sizeof(rsa_premaster), |
1476 | data_param_ignored | 1613 | record->key + 2, sizeof(record->key) - 2, |
1477 | ); | 1614 | data_param_ignored |
1478 | record->keylen16_hi = len >> 8; | 1615 | ); |
1479 | record->keylen16_lo = len & 0xff; | 1616 | /* keylen16 exists for RSA (in TLS, not in SSL), but not for some other key types */ |
1480 | len += 2; | 1617 | record->key[0] = len >> 8; |
1618 | record->key[1] = len & 0xff; | ||
1619 | len += 2; | ||
1620 | premaster = rsa_premaster; | ||
1621 | premaster_size = sizeof(rsa_premaster); | ||
1622 | } else { | ||
1623 | /* KEY_ALG_ECDSA */ | ||
1624 | static const uint8_t basepoint9[CURVE25519_KEYSIZE] = {9}; | ||
1625 | uint8_t privkey[CURVE25519_KEYSIZE]; //[32] | ||
1626 | |||
1627 | /* Generate random private key, see RFC 7748 */ | ||
1628 | tls_get_random(privkey, sizeof(privkey)); | ||
1629 | privkey[0] &= 0xf8; | ||
1630 | privkey[CURVE25519_KEYSIZE-1] = ((privkey[CURVE25519_KEYSIZE-1] & 0x7f) | 0x40); | ||
1631 | |||
1632 | /* Compute public key */ | ||
1633 | curve25519(record->key + 1, privkey, basepoint9); | ||
1634 | |||
1635 | /* Compute premaster using peer's public key */ | ||
1636 | dbg("computing x25519_premaster\n"); | ||
1637 | curve25519(x25519_premaster, privkey, tls->hsd->ecc_pub_key32); | ||
1638 | |||
1639 | len = CURVE25519_KEYSIZE; | ||
1640 | record->key[0] = len; | ||
1641 | len++; | ||
1642 | premaster = x25519_premaster; | ||
1643 | premaster_size = sizeof(x25519_premaster); | ||
1644 | } | ||
1645 | |||
1481 | record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE; | 1646 | record->type = HANDSHAKE_CLIENT_KEY_EXCHANGE; |
1482 | record->len24_hi = 0; | 1647 | record->len24_hi = 0; |
1483 | record->len24_mid = len >> 8; | 1648 | record->len24_mid = len >> 8; |
@@ -1499,7 +1664,7 @@ static void send_client_key_exchange(tls_state_t *tls) | |||
1499 | // of the premaster secret will vary depending on key exchange method. | 1664 | // of the premaster secret will vary depending on key exchange method. |
1500 | prf_hmac_sha256(/*tls,*/ | 1665 | prf_hmac_sha256(/*tls,*/ |
1501 | tls->hsd->master_secret, sizeof(tls->hsd->master_secret), | 1666 | tls->hsd->master_secret, sizeof(tls->hsd->master_secret), |
1502 | rsa_premaster, sizeof(rsa_premaster), | 1667 | premaster, premaster_size, |
1503 | "master secret", | 1668 | "master secret", |
1504 | tls->hsd->client_and_server_rand32, sizeof(tls->hsd->client_and_server_rand32) | 1669 | tls->hsd->client_and_server_rand32, sizeof(tls->hsd->client_and_server_rand32) |
1505 | ); | 1670 | ); |
@@ -1686,8 +1851,19 @@ void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni) | |||
1686 | //SvKey len=455^ | 1851 | //SvKey len=455^ |
1687 | // with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes: | 1852 | // with TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: 461 bytes: |
1688 | // 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... | 1853 | // 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... |
1854 | // | ||
1855 | // RFC 8422 5.4. Server Key Exchange | ||
1856 | // This message is sent when using the ECDHE_ECDSA, ECDHE_RSA, and | ||
1857 | // ECDH_anon key exchange algorithms. | ||
1858 | // This message is used to convey the server's ephemeral ECDH public key | ||
1859 | // (and the corresponding elliptic curve domain parameters) to the | ||
1860 | // client. | ||
1689 | dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len); | 1861 | dbg("<< SERVER_KEY_EXCHANGE len:%u\n", len); |
1690 | //probably need to save it | 1862 | dump_raw_in("<< %s\n", tls->inbuf, RECHDR_LEN + len); |
1863 | if (tls->hsd->key_alg == KEY_ALG_ECDSA) | ||
1864 | process_server_key(tls, len); | ||
1865 | |||
1866 | // read next handshake block | ||
1691 | len = tls_xread_handshake_block(tls, 4); | 1867 | len = tls_xread_handshake_block(tls, 4); |
1692 | } | 1868 | } |
1693 | 1869 | ||