diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libssl/Makefile | 3 | ||||
| -rw-r--r-- | src/lib/libssl/s3_lib.c | 20 | ||||
| -rw-r--r-- | src/lib/libssl/ssl_locl.h | 8 | ||||
| -rw-r--r-- | src/lib/libssl/ssl_tlsext.c | 109 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_client.c | 21 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_internal.h | 24 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_key_share.c | 224 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_server.c | 20 |
8 files changed, 299 insertions, 130 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile index 489c4fd217..afbd6d148e 100644 --- a/src/lib/libssl/Makefile +++ b/src/lib/libssl/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.61 2020/01/30 16:25:09 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.62 2020/01/30 17:09:23 jsing Exp $ |
| 2 | 2 | ||
| 3 | .include <bsd.own.mk> | 3 | .include <bsd.own.mk> |
| 4 | .ifndef NOMAN | 4 | .ifndef NOMAN |
| @@ -73,6 +73,7 @@ SRCS= \ | |||
| 73 | tls13_handshake.c \ | 73 | tls13_handshake.c \ |
| 74 | tls13_handshake_msg.c \ | 74 | tls13_handshake_msg.c \ |
| 75 | tls13_key_schedule.c \ | 75 | tls13_key_schedule.c \ |
| 76 | tls13_key_share.c \ | ||
| 76 | tls13_lib.c \ | 77 | tls13_lib.c \ |
| 77 | tls13_record.c \ | 78 | tls13_record.c \ |
| 78 | tls13_record_layer.c \ | 79 | tls13_record_layer.c \ |
diff --git a/src/lib/libssl/s3_lib.c b/src/lib/libssl/s3_lib.c index 252242e053..2832ef4a93 100644 --- a/src/lib/libssl/s3_lib.c +++ b/src/lib/libssl/s3_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: s3_lib.c,v 1.189 2020/01/23 10:40:59 jsing Exp $ */ | 1 | /* $OpenBSD: s3_lib.c,v 1.190 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -1564,12 +1564,8 @@ ssl3_free(SSL *s) | |||
| 1564 | DH_free(S3I(s)->tmp.dh); | 1564 | DH_free(S3I(s)->tmp.dh); |
| 1565 | EC_KEY_free(S3I(s)->tmp.ecdh); | 1565 | EC_KEY_free(S3I(s)->tmp.ecdh); |
| 1566 | 1566 | ||
| 1567 | freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH); | 1567 | tls13_key_share_free(S3I(s)->hs_tls13.key_share); |
| 1568 | |||
| 1569 | tls13_secrets_destroy(S3I(s)->hs_tls13.secrets); | 1568 | tls13_secrets_destroy(S3I(s)->hs_tls13.secrets); |
| 1570 | freezero(S3I(s)->hs_tls13.x25519_private, X25519_KEY_LENGTH); | ||
| 1571 | freezero(S3I(s)->hs_tls13.x25519_public, X25519_KEY_LENGTH); | ||
| 1572 | freezero(S3I(s)->hs_tls13.x25519_peer_public, X25519_KEY_LENGTH); | ||
| 1573 | freezero(S3I(s)->hs_tls13.cookie, S3I(s)->hs_tls13.cookie_len); | 1569 | freezero(S3I(s)->hs_tls13.cookie, S3I(s)->hs_tls13.cookie_len); |
| 1574 | 1570 | ||
| 1575 | sk_X509_NAME_pop_free(S3I(s)->tmp.ca_names, X509_NAME_free); | 1571 | sk_X509_NAME_pop_free(S3I(s)->tmp.ca_names, X509_NAME_free); |
| @@ -1599,21 +1595,17 @@ ssl3_clear(SSL *s) | |||
| 1599 | S3I(s)->tmp.dh = NULL; | 1595 | S3I(s)->tmp.dh = NULL; |
| 1600 | EC_KEY_free(S3I(s)->tmp.ecdh); | 1596 | EC_KEY_free(S3I(s)->tmp.ecdh); |
| 1601 | S3I(s)->tmp.ecdh = NULL; | 1597 | S3I(s)->tmp.ecdh = NULL; |
| 1598 | S3I(s)->tmp.ecdh_nid = NID_undef; | ||
| 1599 | |||
| 1602 | freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len); | 1600 | freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len); |
| 1603 | S3I(s)->hs.sigalgs = NULL; | 1601 | S3I(s)->hs.sigalgs = NULL; |
| 1604 | S3I(s)->hs.sigalgs_len = 0; | 1602 | S3I(s)->hs.sigalgs_len = 0; |
| 1605 | 1603 | ||
| 1606 | freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH); | 1604 | tls13_key_share_free(S3I(s)->hs_tls13.key_share); |
| 1607 | S3I(s)->tmp.x25519 = NULL; | 1605 | S3I(s)->hs_tls13.key_share = NULL; |
| 1608 | 1606 | ||
| 1609 | tls13_secrets_destroy(S3I(s)->hs_tls13.secrets); | 1607 | tls13_secrets_destroy(S3I(s)->hs_tls13.secrets); |
| 1610 | S3I(s)->hs_tls13.secrets = NULL; | 1608 | S3I(s)->hs_tls13.secrets = NULL; |
| 1611 | freezero(S3I(s)->hs_tls13.x25519_private, X25519_KEY_LENGTH); | ||
| 1612 | S3I(s)->hs_tls13.x25519_private = NULL; | ||
| 1613 | freezero(S3I(s)->hs_tls13.x25519_public, X25519_KEY_LENGTH); | ||
| 1614 | S3I(s)->hs_tls13.x25519_public = NULL; | ||
| 1615 | freezero(S3I(s)->hs_tls13.x25519_peer_public, X25519_KEY_LENGTH); | ||
| 1616 | S3I(s)->hs_tls13.x25519_peer_public = NULL; | ||
| 1617 | freezero(S3I(s)->hs_tls13.cookie, S3I(s)->hs_tls13.cookie_len); | 1609 | freezero(S3I(s)->hs_tls13.cookie, S3I(s)->hs_tls13.cookie_len); |
| 1618 | S3I(s)->hs_tls13.cookie = NULL; | 1610 | S3I(s)->hs_tls13.cookie = NULL; |
| 1619 | S3I(s)->hs_tls13.cookie_len = 0; | 1611 | S3I(s)->hs_tls13.cookie_len = 0; |
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h index 5ff6f39b45..476381c165 100644 --- a/src/lib/libssl/ssl_locl.h +++ b/src/lib/libssl/ssl_locl.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ssl_locl.h,v 1.258 2020/01/30 16:25:09 jsing Exp $ */ | 1 | /* $OpenBSD: ssl_locl.h,v 1.259 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -458,11 +458,7 @@ typedef struct ssl_handshake_tls13_st { | |||
| 458 | /* Version proposed by peer server. */ | 458 | /* Version proposed by peer server. */ |
| 459 | uint16_t server_version; | 459 | uint16_t server_version; |
| 460 | 460 | ||
| 461 | /* X25519 key share. */ | 461 | struct tls13_key_share *key_share; |
| 462 | uint8_t *x25519_public; | ||
| 463 | uint8_t *x25519_private; | ||
| 464 | uint8_t *x25519_peer_public; | ||
| 465 | |||
| 466 | struct tls13_secrets *secrets; | 462 | struct tls13_secrets *secrets; |
| 467 | 463 | ||
| 468 | uint8_t *cookie; | 464 | uint8_t *cookie; |
diff --git a/src/lib/libssl/ssl_tlsext.c b/src/lib/libssl/ssl_tlsext.c index 5cebd1d630..46f30aa47e 100644 --- a/src/lib/libssl/ssl_tlsext.c +++ b/src/lib/libssl/ssl_tlsext.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: ssl_tlsext.c,v 1.57 2020/01/26 03:29:30 beck Exp $ */ | 1 | /* $OpenBSD: ssl_tlsext.c,v 1.58 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2016, 2017, 2019 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2016, 2017, 2019 Joel Sing <jsing@openbsd.org> |
| 4 | * Copyright (c) 2017 Doug Hogan <doug@openbsd.org> | 4 | * Copyright (c) 2017 Doug Hogan <doug@openbsd.org> |
| @@ -16,6 +16,7 @@ | |||
| 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | */ | 18 | */ |
| 19 | |||
| 19 | #include <openssl/curve25519.h> | 20 | #include <openssl/curve25519.h> |
| 20 | #include <openssl/ocsp.h> | 21 | #include <openssl/ocsp.h> |
| 21 | 22 | ||
| @@ -1255,82 +1256,46 @@ tlsext_keyshare_client_needs(SSL *s) | |||
| 1255 | int | 1256 | int |
| 1256 | tlsext_keyshare_client_build(SSL *s, CBB *cbb) | 1257 | tlsext_keyshare_client_build(SSL *s, CBB *cbb) |
| 1257 | { | 1258 | { |
| 1258 | uint8_t *public_key = NULL, *private_key = NULL; | 1259 | CBB client_shares; |
| 1259 | CBB client_shares, key_exchange; | ||
| 1260 | 1260 | ||
| 1261 | /* Generate and provide key shares. */ | ||
| 1262 | if (!CBB_add_u16_length_prefixed(cbb, &client_shares)) | 1261 | if (!CBB_add_u16_length_prefixed(cbb, &client_shares)) |
| 1263 | return 0; | 1262 | return 0; |
| 1264 | 1263 | ||
| 1265 | /* XXX - other groups. */ | 1264 | if (!tls13_key_share_public(S3I(s)->hs_tls13.key_share, |
| 1266 | 1265 | &client_shares)) | |
| 1267 | /* Generate X25519 key pair. */ | 1266 | return 0; |
| 1268 | if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
| 1269 | goto err; | ||
| 1270 | if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
| 1271 | goto err; | ||
| 1272 | X25519_keypair(public_key, private_key); | ||
| 1273 | |||
| 1274 | /* Add the group and serialize the public key. */ | ||
| 1275 | if (!CBB_add_u16(&client_shares, tls1_ec_nid2curve_id(NID_X25519))) | ||
| 1276 | goto err; | ||
| 1277 | if (!CBB_add_u16_length_prefixed(&client_shares, &key_exchange)) | ||
| 1278 | goto err; | ||
| 1279 | if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH)) | ||
| 1280 | goto err; | ||
| 1281 | 1267 | ||
| 1282 | if (!CBB_flush(cbb)) | 1268 | if (!CBB_flush(cbb)) |
| 1283 | goto err; | 1269 | return 0; |
| 1284 | |||
| 1285 | S3I(s)->hs_tls13.x25519_public = public_key; | ||
| 1286 | S3I(s)->hs_tls13.x25519_private = private_key; | ||
| 1287 | 1270 | ||
| 1288 | return 1; | 1271 | return 1; |
| 1289 | |||
| 1290 | err: | ||
| 1291 | freezero(public_key, X25519_KEY_LENGTH); | ||
| 1292 | freezero(private_key, X25519_KEY_LENGTH); | ||
| 1293 | |||
| 1294 | return 0; | ||
| 1295 | } | 1272 | } |
| 1296 | 1273 | ||
| 1297 | int | 1274 | int |
| 1298 | tlsext_keyshare_server_parse(SSL *s, CBS *cbs, int *alert) | 1275 | tlsext_keyshare_server_parse(SSL *s, CBS *cbs, int *alert) |
| 1299 | { | 1276 | { |
| 1300 | CBS client_shares; | 1277 | CBS client_shares; |
| 1301 | CBS key_exchange; | ||
| 1302 | uint16_t group; | 1278 | uint16_t group; |
| 1303 | size_t out_len; | ||
| 1304 | 1279 | ||
| 1305 | if (!CBS_get_u16_length_prefixed(cbs, &client_shares)) | 1280 | if (!CBS_get_u16_length_prefixed(cbs, &client_shares)) |
| 1306 | goto err; | 1281 | goto err; |
| 1307 | 1282 | ||
| 1308 | if (CBS_len(cbs) != 0) | ||
| 1309 | goto err; | ||
| 1310 | |||
| 1311 | while (CBS_len(&client_shares) > 0) { | 1283 | while (CBS_len(&client_shares) > 0) { |
| 1312 | 1284 | ||
| 1313 | /* Unpack client share. */ | 1285 | /* Unpack client share. */ |
| 1314 | if (!CBS_get_u16(&client_shares, &group)) | 1286 | if (!CBS_get_u16(&client_shares, &group)) |
| 1315 | goto err; | 1287 | goto err; |
| 1316 | 1288 | ||
| 1317 | if (!CBS_get_u16_length_prefixed(&client_shares, &key_exchange)) | ||
| 1318 | goto err; | ||
| 1319 | |||
| 1320 | /* | 1289 | /* |
| 1321 | * Skip this client share if not X25519 | ||
| 1322 | * XXX support other groups later. | 1290 | * XXX support other groups later. |
| 1323 | * XXX enforce group can only appear once. | 1291 | * XXX enforce group can only appear once. |
| 1324 | */ | 1292 | */ |
| 1325 | if (S3I(s)->hs_tls13.x25519_peer_public != NULL || | 1293 | if (S3I(s)->hs_tls13.key_share == NULL || |
| 1326 | group != tls1_ec_nid2curve_id(NID_X25519)) | 1294 | tls13_key_share_group(S3I(s)->hs_tls13.key_share) != group) |
| 1327 | continue; | 1295 | continue; |
| 1328 | 1296 | ||
| 1329 | if (CBS_len(&key_exchange) != X25519_KEY_LENGTH) | 1297 | if (!tls13_key_share_peer_public(S3I(s)->hs_tls13.key_share, |
| 1330 | goto err; | 1298 | group, &client_shares)) |
| 1331 | |||
| 1332 | if (!CBS_stow(&key_exchange, &S3I(s)->hs_tls13.x25519_peer_public, | ||
| 1333 | &out_len)) | ||
| 1334 | goto err; | 1299 | goto err; |
| 1335 | } | 1300 | } |
| 1336 | 1301 | ||
| @@ -1353,68 +1318,28 @@ tlsext_keyshare_server_needs(SSL *s) | |||
| 1353 | int | 1318 | int |
| 1354 | tlsext_keyshare_server_build(SSL *s, CBB *cbb) | 1319 | tlsext_keyshare_server_build(SSL *s, CBB *cbb) |
| 1355 | { | 1320 | { |
| 1356 | uint8_t *public_key = NULL, *private_key = NULL; | 1321 | if (S3I(s)->hs_tls13.key_share == NULL) |
| 1357 | CBB key_exchange; | ||
| 1358 | |||
| 1359 | /* XXX deduplicate with client code */ | ||
| 1360 | |||
| 1361 | /* X25519 */ | ||
| 1362 | if (S3I(s)->hs_tls13.x25519_peer_public == NULL) | ||
| 1363 | return 0; | 1322 | return 0; |
| 1364 | 1323 | ||
| 1365 | /* Generate X25519 key pair. */ | 1324 | if (!tls13_key_share_public(S3I(s)->hs_tls13.key_share, cbb)) |
| 1366 | if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) | 1325 | return 0; |
| 1367 | goto err; | ||
| 1368 | if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
| 1369 | goto err; | ||
| 1370 | X25519_keypair(public_key, private_key); | ||
| 1371 | |||
| 1372 | /* Add the group and serialize the public key. */ | ||
| 1373 | if (!CBB_add_u16(cbb, tls1_ec_nid2curve_id(NID_X25519))) | ||
| 1374 | goto err; | ||
| 1375 | if (!CBB_add_u16_length_prefixed(cbb, &key_exchange)) | ||
| 1376 | goto err; | ||
| 1377 | if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH)) | ||
| 1378 | goto err; | ||
| 1379 | |||
| 1380 | if (!CBB_flush(cbb)) | ||
| 1381 | goto err; | ||
| 1382 | |||
| 1383 | S3I(s)->hs_tls13.x25519_public = public_key; | ||
| 1384 | S3I(s)->hs_tls13.x25519_private = private_key; | ||
| 1385 | 1326 | ||
| 1386 | return 1; | 1327 | return 1; |
| 1387 | |||
| 1388 | err: | ||
| 1389 | freezero(public_key, X25519_KEY_LENGTH); | ||
| 1390 | freezero(private_key, X25519_KEY_LENGTH); | ||
| 1391 | |||
| 1392 | return 0; | ||
| 1393 | } | 1328 | } |
| 1394 | 1329 | ||
| 1395 | int | 1330 | int |
| 1396 | tlsext_keyshare_client_parse(SSL *s, CBS *cbs, int *alert) | 1331 | tlsext_keyshare_client_parse(SSL *s, CBS *cbs, int *alert) |
| 1397 | { | 1332 | { |
| 1398 | CBS key_exchange; | ||
| 1399 | uint16_t group; | 1333 | uint16_t group; |
| 1400 | size_t out_len; | ||
| 1401 | 1334 | ||
| 1402 | /* Unpack server share. */ | 1335 | /* Unpack server share. */ |
| 1403 | if (!CBS_get_u16(cbs, &group)) | 1336 | if (!CBS_get_u16(cbs, &group)) |
| 1404 | goto err; | 1337 | goto err; |
| 1405 | 1338 | ||
| 1406 | /* Handle other groups and verify that they're valid. */ | 1339 | /* XXX - Handle other groups and verify that they're valid. */ |
| 1407 | if (group != tls1_ec_nid2curve_id(NID_X25519)) | ||
| 1408 | goto err; | ||
| 1409 | |||
| 1410 | if (!CBS_get_u16_length_prefixed(cbs, &key_exchange)) | ||
| 1411 | goto err; | ||
| 1412 | |||
| 1413 | if (CBS_len(&key_exchange) != X25519_KEY_LENGTH) | ||
| 1414 | goto err; | ||
| 1415 | 1340 | ||
| 1416 | if (!CBS_stow(&key_exchange, &S3I(s)->hs_tls13.x25519_peer_public, | 1341 | if (!tls13_key_share_peer_public(S3I(s)->hs_tls13.key_share, |
| 1417 | &out_len)) | 1342 | group, cbs)) |
| 1418 | goto err; | 1343 | goto err; |
| 1419 | 1344 | ||
| 1420 | return 1; | 1345 | return 1; |
diff --git a/src/lib/libssl/tls13_client.c b/src/lib/libssl/tls13_client.c index 3c55be6e68..69e75558dc 100644 --- a/src/lib/libssl/tls13_client.c +++ b/src/lib/libssl/tls13_client.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls13_client.c,v 1.38 2020/01/29 17:03:58 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_client.c,v 1.39 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -52,6 +52,11 @@ tls13_client_init(struct tls13_ctx *ctx) | |||
| 52 | if (!tls1_transcript_init(s)) | 52 | if (!tls1_transcript_init(s)) |
| 53 | return 0; | 53 | return 0; |
| 54 | 54 | ||
| 55 | if ((ctx->hs->key_share = tls13_key_share_new(NID_X25519)) == NULL) | ||
| 56 | return 0; | ||
| 57 | if (!tls13_key_share_generate(ctx->hs->key_share)) | ||
| 58 | return 0; | ||
| 59 | |||
| 55 | arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); | 60 | arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); |
| 56 | 61 | ||
| 57 | return 1; | 62 | return 1; |
| @@ -394,6 +399,7 @@ tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs) | |||
| 394 | struct tls13_secret context; | 399 | struct tls13_secret context; |
| 395 | unsigned char buf[EVP_MAX_MD_SIZE]; | 400 | unsigned char buf[EVP_MAX_MD_SIZE]; |
| 396 | uint8_t *shared_key = NULL; | 401 | uint8_t *shared_key = NULL; |
| 402 | size_t shared_key_len = 0; | ||
| 397 | size_t hash_len; | 403 | size_t hash_len; |
| 398 | SSL *s = ctx->ssl; | 404 | SSL *s = ctx->ssl; |
| 399 | int ret = 0; | 405 | int ret = 0; |
| @@ -406,14 +412,12 @@ tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs) | |||
| 406 | return 1; | 412 | return 1; |
| 407 | 413 | ||
| 408 | /* XXX - handle other key share types. */ | 414 | /* XXX - handle other key share types. */ |
| 409 | if (ctx->hs->x25519_peer_public == NULL) { | 415 | if (ctx->hs->key_share == NULL) { |
| 410 | /* XXX - alert. */ | 416 | /* XXX - alert. */ |
| 411 | goto err; | 417 | goto err; |
| 412 | } | 418 | } |
| 413 | if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL) | 419 | if (!tls13_key_share_derive(ctx->hs->key_share, &shared_key, |
| 414 | goto err; | 420 | &shared_key_len)) |
| 415 | if (!X25519(shared_key, ctx->hs->x25519_private, | ||
| 416 | ctx->hs->x25519_peer_public)) | ||
| 417 | goto err; | 421 | goto err; |
| 418 | 422 | ||
| 419 | s->session->cipher = S3I(s)->hs.new_cipher; | 423 | s->session->cipher = S3I(s)->hs.new_cipher; |
| @@ -443,7 +447,7 @@ tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs) | |||
| 443 | 447 | ||
| 444 | /* Handshake secrets. */ | 448 | /* Handshake secrets. */ |
| 445 | if (!tls13_derive_handshake_secrets(ctx->hs->secrets, shared_key, | 449 | if (!tls13_derive_handshake_secrets(ctx->hs->secrets, shared_key, |
| 446 | X25519_KEY_LENGTH, &context)) | 450 | shared_key_len, &context)) |
| 447 | goto err; | 451 | goto err; |
| 448 | 452 | ||
| 449 | tls13_record_layer_set_aead(ctx->rl, ctx->aead); | 453 | tls13_record_layer_set_aead(ctx->rl, ctx->aead); |
| @@ -460,7 +464,8 @@ tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs) | |||
| 460 | ret = 1; | 464 | ret = 1; |
| 461 | 465 | ||
| 462 | err: | 466 | err: |
| 463 | freezero(shared_key, X25519_KEY_LENGTH); | 467 | freezero(shared_key, shared_key_len); |
| 468 | |||
| 464 | return ret; | 469 | return ret; |
| 465 | } | 470 | } |
| 466 | 471 | ||
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h index ec58525c2b..00035ea36e 100644 --- a/src/lib/libssl/tls13_internal.h +++ b/src/lib/libssl/tls13_internal.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls13_internal.h,v 1.57 2020/01/26 02:45:27 beck Exp $ */ | 1 | /* $OpenBSD: tls13_internal.h,v 1.58 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> |
| 4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> |
| @@ -52,6 +52,9 @@ typedef ssize_t (*tls13_write_cb)(const void *_buf, size_t _buflen, | |||
| 52 | void *_cb_arg); | 52 | void *_cb_arg); |
| 53 | typedef void (*tls13_handshake_message_cb)(void *_cb_arg, CBS *_cbs); | 53 | typedef void (*tls13_handshake_message_cb)(void *_cb_arg, CBS *_cbs); |
| 54 | 54 | ||
| 55 | /* | ||
| 56 | * Buffers. | ||
| 57 | */ | ||
| 55 | struct tls13_buffer; | 58 | struct tls13_buffer; |
| 56 | 59 | ||
| 57 | struct tls13_buffer *tls13_buffer_new(size_t init_size); | 60 | struct tls13_buffer *tls13_buffer_new(size_t init_size); |
| @@ -63,6 +66,9 @@ void tls13_buffer_cbs(struct tls13_buffer *buf, CBS *cbs); | |||
| 63 | int tls13_buffer_finish(struct tls13_buffer *buf, uint8_t **out, | 66 | int tls13_buffer_finish(struct tls13_buffer *buf, uint8_t **out, |
| 64 | size_t *out_len); | 67 | size_t *out_len); |
| 65 | 68 | ||
| 69 | /* | ||
| 70 | * Secrets. | ||
| 71 | */ | ||
| 66 | struct tls13_secret { | 72 | struct tls13_secret { |
| 67 | uint8_t *data; | 73 | uint8_t *data; |
| 68 | size_t len; | 74 | size_t len; |
| @@ -113,6 +119,22 @@ int tls13_update_client_traffic_secret(struct tls13_secrets *secrets); | |||
| 113 | int tls13_update_server_traffic_secret(struct tls13_secrets *secrets); | 119 | int tls13_update_server_traffic_secret(struct tls13_secrets *secrets); |
| 114 | 120 | ||
| 115 | /* | 121 | /* |
| 122 | * Key shares. | ||
| 123 | */ | ||
| 124 | struct tls13_key_share; | ||
| 125 | |||
| 126 | struct tls13_key_share *tls13_key_share_new(int nid); | ||
| 127 | void tls13_key_share_free(struct tls13_key_share *ks); | ||
| 128 | |||
| 129 | uint16_t tls13_key_share_group(struct tls13_key_share *ks); | ||
| 130 | int tls13_key_share_generate(struct tls13_key_share *ks); | ||
| 131 | int tls13_key_share_public(struct tls13_key_share *ks, CBB *cbb); | ||
| 132 | int tls13_key_share_peer_public(struct tls13_key_share *ks, uint16_t group, | ||
| 133 | CBS *cbs); | ||
| 134 | int tls13_key_share_derive(struct tls13_key_share *ks, uint8_t **shared_key, | ||
| 135 | size_t *shared_key_len); | ||
| 136 | |||
| 137 | /* | ||
| 116 | * Record Layer. | 138 | * Record Layer. |
| 117 | */ | 139 | */ |
| 118 | struct tls13_record_layer; | 140 | struct tls13_record_layer; |
diff --git a/src/lib/libssl/tls13_key_share.c b/src/lib/libssl/tls13_key_share.c new file mode 100644 index 0000000000..9a83b9f9f7 --- /dev/null +++ b/src/lib/libssl/tls13_key_share.c | |||
| @@ -0,0 +1,224 @@ | |||
| 1 | /* $OpenBSD: tls13_key_share.c,v 1.1 2020/01/30 17:09:23 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <stdlib.h> | ||
| 19 | |||
| 20 | #include <openssl/curve25519.h> | ||
| 21 | |||
| 22 | #include "bytestring.h" | ||
| 23 | #include "ssl_locl.h" | ||
| 24 | #include "tls13_internal.h" | ||
| 25 | |||
| 26 | struct tls13_key_share { | ||
| 27 | int nid; | ||
| 28 | uint16_t group_id; | ||
| 29 | |||
| 30 | uint8_t *x25519_public; | ||
| 31 | uint8_t *x25519_private; | ||
| 32 | uint8_t *x25519_peer_public; | ||
| 33 | }; | ||
| 34 | |||
| 35 | struct tls13_key_share * | ||
| 36 | tls13_key_share_new(int nid) | ||
| 37 | { | ||
| 38 | struct tls13_key_share *ks; | ||
| 39 | |||
| 40 | if ((ks = calloc(1, sizeof(struct tls13_key_share))) == NULL) | ||
| 41 | goto err; | ||
| 42 | |||
| 43 | if ((ks->group_id = tls1_ec_nid2curve_id(nid)) == 0) | ||
| 44 | goto err; | ||
| 45 | |||
| 46 | ks->nid = nid; | ||
| 47 | |||
| 48 | return ks; | ||
| 49 | |||
| 50 | err: | ||
| 51 | tls13_key_share_free(ks); | ||
| 52 | |||
| 53 | return NULL; | ||
| 54 | } | ||
| 55 | |||
| 56 | void | ||
| 57 | tls13_key_share_free(struct tls13_key_share *ks) | ||
| 58 | { | ||
| 59 | if (ks == NULL) | ||
| 60 | return; | ||
| 61 | |||
| 62 | freezero(ks->x25519_public, X25519_KEY_LENGTH); | ||
| 63 | freezero(ks->x25519_private, X25519_KEY_LENGTH); | ||
| 64 | freezero(ks->x25519_peer_public, X25519_KEY_LENGTH); | ||
| 65 | |||
| 66 | freezero(ks, sizeof(*ks)); | ||
| 67 | } | ||
| 68 | |||
| 69 | uint16_t | ||
| 70 | tls13_key_share_group(struct tls13_key_share *ks) | ||
| 71 | { | ||
| 72 | return ks->group_id; | ||
| 73 | } | ||
| 74 | |||
| 75 | static int | ||
| 76 | tls13_key_share_generate_x25519(struct tls13_key_share *ks) | ||
| 77 | { | ||
| 78 | uint8_t *public = NULL, *private = NULL; | ||
| 79 | int ret = 0; | ||
| 80 | |||
| 81 | if (ks->x25519_public != NULL || ks->x25519_private != NULL) | ||
| 82 | goto err; | ||
| 83 | |||
| 84 | if ((public = calloc(1, X25519_KEY_LENGTH)) == NULL) | ||
| 85 | goto err; | ||
| 86 | if ((private = calloc(1, X25519_KEY_LENGTH)) == NULL) | ||
| 87 | goto err; | ||
| 88 | |||
| 89 | X25519_keypair(public, private); | ||
| 90 | |||
| 91 | ks->x25519_public = public; | ||
| 92 | ks->x25519_private = private; | ||
| 93 | public = NULL; | ||
| 94 | private = NULL; | ||
| 95 | |||
| 96 | ret = 1; | ||
| 97 | |||
| 98 | err: | ||
| 99 | freezero(public, X25519_KEY_LENGTH); | ||
| 100 | freezero(private, X25519_KEY_LENGTH); | ||
| 101 | |||
| 102 | return ret; | ||
| 103 | } | ||
| 104 | |||
| 105 | int | ||
| 106 | tls13_key_share_generate(struct tls13_key_share *ks) | ||
| 107 | { | ||
| 108 | if (ks->nid == NID_X25519) | ||
| 109 | return tls13_key_share_generate_x25519(ks); | ||
| 110 | |||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | static int | ||
| 115 | tls13_key_share_public_x25519(struct tls13_key_share *ks, CBB *cbb) | ||
| 116 | { | ||
| 117 | if (ks->x25519_public == NULL) | ||
| 118 | return 0; | ||
| 119 | |||
| 120 | return CBB_add_bytes(cbb, ks->x25519_public, X25519_KEY_LENGTH); | ||
| 121 | } | ||
| 122 | |||
| 123 | int | ||
| 124 | tls13_key_share_public(struct tls13_key_share *ks, CBB *cbb) | ||
| 125 | { | ||
| 126 | CBB key_exchange; | ||
| 127 | |||
| 128 | if (!CBB_add_u16(cbb, ks->group_id)) | ||
| 129 | goto err; | ||
| 130 | if (!CBB_add_u16_length_prefixed(cbb, &key_exchange)) | ||
| 131 | goto err; | ||
| 132 | |||
| 133 | if (ks->nid == NID_X25519) { | ||
| 134 | if (!tls13_key_share_public_x25519(ks, &key_exchange)) | ||
| 135 | goto err; | ||
| 136 | } else { | ||
| 137 | goto err; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (!CBB_flush(cbb)) | ||
| 141 | goto err; | ||
| 142 | |||
| 143 | return 1; | ||
| 144 | |||
| 145 | err: | ||
| 146 | return 0; | ||
| 147 | } | ||
| 148 | |||
| 149 | static int | ||
| 150 | tls13_key_share_peer_public_x25519(struct tls13_key_share *ks, CBS *cbs) | ||
| 151 | { | ||
| 152 | size_t out_len; | ||
| 153 | |||
| 154 | if (CBS_len(cbs) != X25519_KEY_LENGTH) | ||
| 155 | return 0; | ||
| 156 | |||
| 157 | return CBS_stow(cbs, &ks->x25519_peer_public, &out_len); | ||
| 158 | } | ||
| 159 | |||
| 160 | int | ||
| 161 | tls13_key_share_peer_public(struct tls13_key_share *ks, uint16_t group, | ||
| 162 | CBS *cbs) | ||
| 163 | { | ||
| 164 | CBS key_exchange; | ||
| 165 | |||
| 166 | if (ks->group_id != group) | ||
| 167 | return 0; | ||
| 168 | |||
| 169 | if (!CBS_get_u16_length_prefixed(cbs, &key_exchange)) | ||
| 170 | return 0; | ||
| 171 | |||
| 172 | if (ks->nid == NID_X25519) { | ||
| 173 | if (!tls13_key_share_peer_public_x25519(ks, &key_exchange)) | ||
| 174 | return 0; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (CBS_len(cbs) != 0) | ||
| 178 | return 0; | ||
| 179 | |||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | static int | ||
| 184 | tls13_key_share_derive_x25519(struct tls13_key_share *ks, | ||
| 185 | uint8_t **shared_key, size_t *shared_key_len) | ||
| 186 | { | ||
| 187 | uint8_t *sk = NULL; | ||
| 188 | int ret = 0; | ||
| 189 | |||
| 190 | if (ks->x25519_private == NULL || ks->x25519_peer_public == NULL) | ||
| 191 | goto err; | ||
| 192 | |||
| 193 | if ((sk = calloc(1, X25519_KEY_LENGTH)) == NULL) | ||
| 194 | goto err; | ||
| 195 | if (!X25519(sk, ks->x25519_private, ks->x25519_peer_public)) | ||
| 196 | goto err; | ||
| 197 | |||
| 198 | *shared_key = sk; | ||
| 199 | *shared_key_len = X25519_KEY_LENGTH; | ||
| 200 | sk = NULL; | ||
| 201 | |||
| 202 | ret = 1; | ||
| 203 | |||
| 204 | err: | ||
| 205 | freezero(sk, X25519_KEY_LENGTH); | ||
| 206 | |||
| 207 | return ret; | ||
| 208 | } | ||
| 209 | |||
| 210 | int | ||
| 211 | tls13_key_share_derive(struct tls13_key_share *ks, uint8_t **shared_key, | ||
| 212 | size_t *shared_key_len) | ||
| 213 | { | ||
| 214 | if (*shared_key != NULL) | ||
| 215 | return 0; | ||
| 216 | |||
| 217 | *shared_key_len = 0; | ||
| 218 | |||
| 219 | if (ks->nid == NID_X25519) | ||
| 220 | return tls13_key_share_derive_x25519(ks, shared_key, | ||
| 221 | shared_key_len); | ||
| 222 | |||
| 223 | return 0; | ||
| 224 | } | ||
diff --git a/src/lib/libssl/tls13_server.c b/src/lib/libssl/tls13_server.c index a559e03219..1f17fe4ab0 100644 --- a/src/lib/libssl/tls13_server.c +++ b/src/lib/libssl/tls13_server.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls13_server.c,v 1.21 2020/01/29 17:03:58 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_server.c,v 1.22 2020/01/30 17:09:23 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2019, 2020 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2019, 2020 Joel Sing <jsing@openbsd.org> |
| 4 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> | 4 | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> |
| @@ -51,6 +51,11 @@ tls13_server_init(struct tls13_ctx *ctx) | |||
| 51 | if ((s->session = SSL_SESSION_new()) == NULL) | 51 | if ((s->session = SSL_SESSION_new()) == NULL) |
| 52 | return 0; | 52 | return 0; |
| 53 | 53 | ||
| 54 | if ((ctx->hs->key_share = tls13_key_share_new(NID_X25519)) == NULL) | ||
| 55 | return 0; | ||
| 56 | if (!tls13_key_share_generate(ctx->hs->key_share)) | ||
| 57 | return 0; | ||
| 58 | |||
| 54 | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); | 59 | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); |
| 55 | 60 | ||
| 56 | return 1; | 61 | return 1; |
| @@ -552,19 +557,18 @@ tls13_server_hello_sent(struct tls13_ctx *ctx) | |||
| 552 | struct tls13_secret context; | 557 | struct tls13_secret context; |
| 553 | unsigned char buf[EVP_MAX_MD_SIZE]; | 558 | unsigned char buf[EVP_MAX_MD_SIZE]; |
| 554 | uint8_t *shared_key = NULL; | 559 | uint8_t *shared_key = NULL; |
| 560 | size_t shared_key_len = 0; | ||
| 555 | size_t hash_len; | 561 | size_t hash_len; |
| 556 | SSL *s = ctx->ssl; | 562 | SSL *s = ctx->ssl; |
| 557 | int ret = 0; | 563 | int ret = 0; |
| 558 | 564 | ||
| 559 | /* XXX - handle other key share types. */ | 565 | /* XXX - handle other key share types. */ |
| 560 | if (ctx->hs->x25519_peer_public == NULL) { | 566 | if (ctx->hs->key_share == NULL) { |
| 561 | /* XXX - alert. */ | 567 | /* XXX - alert. */ |
| 562 | goto err; | 568 | goto err; |
| 563 | } | 569 | } |
| 564 | if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL) | 570 | if (!tls13_key_share_derive(ctx->hs->key_share, |
| 565 | goto err; | 571 | &shared_key, &shared_key_len)) |
| 566 | if (!X25519(shared_key, ctx->hs->x25519_private, | ||
| 567 | ctx->hs->x25519_peer_public)) | ||
| 568 | goto err; | 572 | goto err; |
| 569 | 573 | ||
| 570 | s->session->cipher = S3I(s)->hs.new_cipher; | 574 | s->session->cipher = S3I(s)->hs.new_cipher; |
| @@ -594,7 +598,7 @@ tls13_server_hello_sent(struct tls13_ctx *ctx) | |||
| 594 | 598 | ||
| 595 | /* Handshake secrets. */ | 599 | /* Handshake secrets. */ |
| 596 | if (!tls13_derive_handshake_secrets(ctx->hs->secrets, shared_key, | 600 | if (!tls13_derive_handshake_secrets(ctx->hs->secrets, shared_key, |
| 597 | X25519_KEY_LENGTH, &context)) | 601 | shared_key_len, &context)) |
| 598 | goto err; | 602 | goto err; |
| 599 | 603 | ||
| 600 | tls13_record_layer_set_aead(ctx->rl, ctx->aead); | 604 | tls13_record_layer_set_aead(ctx->rl, ctx->aead); |
| @@ -614,7 +618,7 @@ tls13_server_hello_sent(struct tls13_ctx *ctx) | |||
| 614 | ret = 1; | 618 | ret = 1; |
| 615 | 619 | ||
| 616 | err: | 620 | err: |
| 617 | freezero(shared_key, X25519_KEY_LENGTH); | 621 | freezero(shared_key, shared_key_len); |
| 618 | return ret; | 622 | return ret; |
| 619 | } | 623 | } |
| 620 | 624 | ||
