summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libssl/s3_lib.c14
-rw-r--r--src/lib/libssl/ssl_locl.h11
-rw-r--r--src/lib/libssl/ssl_srvr.c242
-rw-r--r--src/lib/libssl/tls13_server.c6
-rw-r--r--src/lib/libssl/tls_internal.h6
-rw-r--r--src/lib/libssl/tls_key_share.c69
6 files changed, 115 insertions, 233 deletions
diff --git a/src/lib/libssl/s3_lib.c b/src/lib/libssl/s3_lib.c
index 54261c575a..899432e947 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.221 2022/01/06 18:23:56 jsing Exp $ */ 1/* $OpenBSD: s3_lib.c,v 1.222 2022/01/07 15:46:30 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 *
@@ -1565,10 +1565,6 @@ ssl3_free(SSL *s)
1565 ssl3_release_write_buffer(s); 1565 ssl3_release_write_buffer(s);
1566 freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len); 1566 freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len);
1567 1567
1568 DH_free(S3I(s)->tmp.dh);
1569 EC_KEY_free(S3I(s)->tmp.ecdh);
1570 freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH);
1571
1572 tls_key_share_free(S3I(s)->hs.key_share); 1568 tls_key_share_free(S3I(s)->hs.key_share);
1573 1569
1574 tls13_secrets_destroy(S3I(s)->hs.tls13.secrets); 1570 tls13_secrets_destroy(S3I(s)->hs.tls13.secrets);
@@ -1601,14 +1597,6 @@ ssl3_clear(SSL *s)
1601 sk_X509_pop_free(s->internal->verified_chain, X509_free); 1597 sk_X509_pop_free(s->internal->verified_chain, X509_free);
1602 s->internal->verified_chain = NULL; 1598 s->internal->verified_chain = NULL;
1603 1599
1604 DH_free(S3I(s)->tmp.dh);
1605 S3I(s)->tmp.dh = NULL;
1606 EC_KEY_free(S3I(s)->tmp.ecdh);
1607 S3I(s)->tmp.ecdh = NULL;
1608 S3I(s)->tmp.ecdh_nid = NID_undef;
1609 freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH);
1610 S3I(s)->tmp.x25519 = NULL;
1611
1612 freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len); 1600 freezero(S3I(s)->hs.sigalgs, S3I(s)->hs.sigalgs_len);
1613 S3I(s)->hs.sigalgs = NULL; 1601 S3I(s)->hs.sigalgs = NULL;
1614 S3I(s)->hs.sigalgs_len = 0; 1602 S3I(s)->hs.sigalgs_len = 0;
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index 83b40d2dd3..cc7b342247 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.374 2022/01/06 18:23:56 jsing Exp $ */ 1/* $OpenBSD: ssl_locl.h,v 1.375 2022/01/07 15:46:30 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 *
@@ -1168,15 +1168,6 @@ typedef struct ssl3_state_internal_st {
1168 1168
1169 SSL_HANDSHAKE hs; 1169 SSL_HANDSHAKE hs;
1170 1170
1171 struct {
1172 DH *dh;
1173
1174 EC_KEY *ecdh; /* holds short lived ECDH key */
1175 int ecdh_nid;
1176
1177 uint8_t *x25519;
1178 } tmp;
1179
1180 /* Connection binding to prevent renegotiation attacks */ 1171 /* Connection binding to prevent renegotiation attacks */
1181 unsigned char previous_client_finished[EVP_MAX_MD_SIZE]; 1172 unsigned char previous_client_finished[EVP_MAX_MD_SIZE];
1182 unsigned char previous_client_finished_len; 1173 unsigned char previous_client_finished_len;
diff --git a/src/lib/libssl/ssl_srvr.c b/src/lib/libssl/ssl_srvr.c
index 0496985351..b66a2c108d 100644
--- a/src/lib/libssl/ssl_srvr.c
+++ b/src/lib/libssl/ssl_srvr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_srvr.c,v 1.130 2022/01/04 12:53:31 jsing Exp $ */ 1/* $OpenBSD: ssl_srvr.c,v 1.131 2022/01/07 15:46:30 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 *
@@ -1309,23 +1309,23 @@ ssl3_send_server_done(SSL *s)
1309static int 1309static int
1310ssl3_send_server_kex_dhe(SSL *s, CBB *cbb) 1310ssl3_send_server_kex_dhe(SSL *s, CBB *cbb)
1311{ 1311{
1312 DH *dh = NULL; 1312 int nid = NID_dhKeyAgreement;
1313 int al;
1314 1313
1315 if ((dh = DH_new()) == NULL) 1314 tls_key_share_free(S3I(s)->hs.key_share);
1315 if ((S3I(s)->hs.key_share = tls_key_share_new_nid(nid)) == NULL)
1316 goto err; 1316 goto err;
1317 1317
1318 if (s->cert->dh_tmp_auto != 0) { 1318 if (s->cert->dh_tmp_auto != 0) {
1319 size_t key_bits; 1319 size_t key_bits;
1320 1320
1321 if ((key_bits = ssl_dhe_params_auto_key_bits(s)) == 0) { 1321 if ((key_bits = ssl_dhe_params_auto_key_bits(s)) == 0) {
1322 al = SSL_AD_INTERNAL_ERROR;
1323 SSLerror(s, ERR_R_INTERNAL_ERROR); 1322 SSLerror(s, ERR_R_INTERNAL_ERROR);
1324 goto fatal_err; 1323 ssl3_send_alert(s, SSL3_AL_FATAL,
1325 } 1324 SSL_AD_INTERNAL_ERROR);
1326
1327 if (!ssl_kex_generate_dhe_params_auto(dh, key_bits))
1328 goto err; 1325 goto err;
1326 }
1327 tls_key_share_set_key_bits(S3I(s)->hs.key_share,
1328 key_bits);
1329 } else { 1329 } else {
1330 DH *dh_params = s->cert->dh_tmp; 1330 DH *dh_params = s->cert->dh_tmp;
1331 1331
@@ -1334,157 +1334,69 @@ ssl3_send_server_kex_dhe(SSL *s, CBB *cbb)
1334 SSL_C_PKEYLENGTH(S3I(s)->hs.cipher)); 1334 SSL_C_PKEYLENGTH(S3I(s)->hs.cipher));
1335 1335
1336 if (dh_params == NULL) { 1336 if (dh_params == NULL) {
1337 al = SSL_AD_HANDSHAKE_FAILURE;
1338 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1337 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY);
1339 goto fatal_err; 1338 ssl3_send_alert(s, SSL3_AL_FATAL,
1339 SSL_AD_HANDSHAKE_FAILURE);
1340 goto err;
1340 } 1341 }
1341 1342
1342 if (!ssl_kex_generate_dhe(dh, dh_params)) 1343 if (!tls_key_share_set_dh_params(S3I(s)->hs.key_share, dh_params))
1343 goto err; 1344 goto err;
1344 } 1345 }
1345 1346
1346 if (!ssl_kex_params_dhe(dh, cbb)) 1347 if (!tls_key_share_generate(S3I(s)->hs.key_share))
1347 goto err;
1348 if (!ssl_kex_public_dhe(dh, cbb))
1349 goto err; 1348 goto err;
1350 1349
1351 if (S3I(s)->tmp.dh != NULL) { 1350 if (!tls_key_share_params(S3I(s)->hs.key_share, cbb))
1352 SSLerror(s, ERR_R_INTERNAL_ERROR); 1351 goto err;
1352 if (!tls_key_share_public(S3I(s)->hs.key_share, cbb))
1353 goto err; 1353 goto err;
1354 }
1355 S3I(s)->tmp.dh = dh;
1356 1354
1357 return 1; 1355 return 1;
1358 1356
1359 fatal_err:
1360 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1361 err: 1357 err:
1362 DH_free(dh);
1363
1364 return 0; 1358 return 0;
1365} 1359}
1366 1360
1367static int 1361static int
1368ssl3_send_server_kex_ecdhe_ecp(SSL *s, int nid, CBB *cbb) 1362ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb)
1369{ 1363{
1370 uint16_t curve_id; 1364 CBB public;
1371 EC_KEY *ecdh; 1365 int nid;
1372 CBB ecpoint;
1373 int al;
1374 1366
1375 /* 1367 if ((nid = tls1_get_shared_curve(s)) == NID_undef) {
1376 * Only named curves are supported in ECDH ephemeral key exchanges.
1377 * For supported named curves, curve_id is non-zero.
1378 */
1379 if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) {
1380 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1368 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1369 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1381 goto err; 1370 goto err;
1382 } 1371 }
1383 1372
1384 if (S3I(s)->tmp.ecdh != NULL) { 1373 tls_key_share_free(S3I(s)->hs.key_share);
1385 SSLerror(s, ERR_R_INTERNAL_ERROR); 1374 if ((S3I(s)->hs.key_share = tls_key_share_new_nid(nid)) == NULL)
1386 goto err; 1375 goto err;
1387 }
1388 1376
1389 if ((S3I(s)->tmp.ecdh = EC_KEY_new()) == NULL) { 1377 if (!tls_key_share_generate(S3I(s)->hs.key_share))
1390 al = SSL_AD_HANDSHAKE_FAILURE;
1391 SSLerror(s, SSL_R_MISSING_TMP_ECDH_KEY);
1392 goto fatal_err;
1393 }
1394 S3I(s)->tmp.ecdh_nid = nid;
1395 ecdh = S3I(s)->tmp.ecdh;
1396
1397 if (!ssl_kex_generate_ecdhe_ecp(ecdh, nid))
1398 goto err; 1378 goto err;
1399 1379
1400 /* 1380 /*
1401 * Encode the public key. 1381 * ECC key exchange - see RFC 8422, section 5.4.
1402 *
1403 * Only named curves are supported in ECDH ephemeral key exchanges.
1404 * In this case the ServerKeyExchange message has:
1405 * [1 byte CurveType], [2 byte CurveName]
1406 * [1 byte length of encoded point], followed by
1407 * the actual encoded point itself.
1408 */ 1382 */
1409 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) 1383 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE))
1410 goto err; 1384 goto err;
1411 if (!CBB_add_u16(cbb, curve_id)) 1385 if (!CBB_add_u16(cbb, tls_key_share_group(S3I(s)->hs.key_share)))
1412 goto err; 1386 goto err;
1413 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) 1387 if (!CBB_add_u8_length_prefixed(cbb, &public))
1414 goto err; 1388 goto err;
1415 if (!ssl_kex_public_ecdhe_ecp(ecdh, &ecpoint)) 1389 if (!tls_key_share_public(S3I(s)->hs.key_share, &public))
1416 goto err; 1390 goto err;
1417 if (!CBB_flush(cbb)) 1391 if (!CBB_flush(cbb))
1418 goto err; 1392 goto err;
1419 1393
1420 return 1; 1394 return 1;
1421 1395
1422 fatal_err:
1423 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1424 err: 1396 err:
1425 return 0; 1397 return 0;
1426} 1398}
1427 1399
1428static int
1429ssl3_send_server_kex_ecdhe_ecx(SSL *s, int nid, CBB *cbb)
1430{
1431 uint8_t *public_key = NULL, *private_key = NULL;
1432 uint16_t curve_id;
1433 CBB ecpoint;
1434 int ret = 0;
1435
1436 /* Generate an X25519 key pair. */
1437 if (S3I(s)->tmp.x25519 != NULL) {
1438 SSLerror(s, ERR_R_INTERNAL_ERROR);
1439 goto err;
1440 }
1441 if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL)
1442 goto err;
1443 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
1444 goto err;
1445 X25519_keypair(public_key, private_key);
1446
1447 /* Serialize public key. */
1448 if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) {
1449 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1450 goto err;
1451 }
1452
1453 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE))
1454 goto err;
1455 if (!CBB_add_u16(cbb, curve_id))
1456 goto err;
1457 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint))
1458 goto err;
1459 if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH))
1460 goto err;
1461 if (!CBB_flush(cbb))
1462 goto err;
1463
1464 S3I(s)->tmp.x25519 = private_key;
1465 private_key = NULL;
1466 ret = 1;
1467
1468 err:
1469 free(public_key);
1470 freezero(private_key, X25519_KEY_LENGTH);
1471
1472 return ret;
1473}
1474
1475static int
1476ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb)
1477{
1478 int nid;
1479
1480 nid = tls1_get_shared_curve(s);
1481
1482 if (nid == NID_X25519)
1483 return ssl3_send_server_kex_ecdhe_ecx(s, nid, cbb);
1484
1485 return ssl3_send_server_kex_ecdhe_ecp(s, nid, cbb);
1486}
1487
1488int 1400int
1489ssl3_send_server_key_exchange(SSL *s) 1401ssl3_send_server_key_exchange(SSL *s)
1490{ 1402{
@@ -1791,148 +1703,74 @@ ssl3_get_client_kex_rsa(SSL *s, CBS *cbs)
1791static int 1703static int
1792ssl3_get_client_kex_dhe(SSL *s, CBS *cbs) 1704ssl3_get_client_kex_dhe(SSL *s, CBS *cbs)
1793{ 1705{
1794 DH *dh_clnt = NULL;
1795 DH *dh_srvr;
1796 int invalid_key;
1797 uint8_t *key = NULL; 1706 uint8_t *key = NULL;
1798 size_t key_len = 0; 1707 size_t key_len = 0;
1708 int invalid_key;
1799 int ret = 0; 1709 int ret = 0;
1800 1710
1801 if ((dh_srvr = S3I(s)->tmp.dh) == NULL) { 1711 if (S3I(s)->hs.key_share == NULL) {
1802 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1712 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1803 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1713 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY);
1804 goto err; 1714 goto err;
1805 } 1715 }
1806 1716
1807 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) 1717 if (!tls_key_share_peer_public(S3I(s)->hs.key_share, cbs,
1808 goto err; 1718 &invalid_key))
1809
1810 if (!ssl_kex_peer_public_dhe(dh_clnt, cbs, &invalid_key)) {
1811 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1812 SSLerror(s, SSL_R_BAD_PACKET_LENGTH);
1813 goto err; 1719 goto err;
1814 }
1815 if (invalid_key) { 1720 if (invalid_key) {
1816 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1721 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1817 SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH); 1722 SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1818 goto err; 1723 goto err;
1819 } 1724 }
1820 1725
1821 if (!ssl_kex_derive_dhe(dh_srvr, dh_clnt, &key, &key_len)) 1726 if (!tls_key_share_derive(S3I(s)->hs.key_share, &key, &key_len))
1822 goto err; 1727 goto err;
1823 1728
1824 if (!tls12_derive_master_secret(s, key, key_len)) 1729 if (!tls12_derive_master_secret(s, key, key_len))
1825 goto err; 1730 goto err;
1826 1731
1827 DH_free(S3I(s)->tmp.dh);
1828 S3I(s)->tmp.dh = NULL;
1829
1830 ret = 1; 1732 ret = 1;
1831 1733
1832 err: 1734 err:
1833 freezero(key, key_len); 1735 freezero(key, key_len);
1834 DH_free(dh_clnt);
1835 1736
1836 return ret; 1737 return ret;
1837} 1738}
1838 1739
1839static int 1740static int
1840ssl3_get_client_kex_ecdhe_ecp(SSL *s, CBS *cbs) 1741ssl3_get_client_kex_ecdhe(SSL *s, CBS *cbs)
1841{ 1742{
1842 uint8_t *key = NULL; 1743 uint8_t *key = NULL;
1843 size_t key_len = 0; 1744 size_t key_len = 0;
1844 EC_KEY *ecdh_peer = NULL;
1845 EC_KEY *ecdh;
1846 CBS public; 1745 CBS public;
1847 int ret = 0; 1746 int ret = 0;
1848 1747
1849 /* 1748 if (S3I(s)->hs.key_share == NULL) {
1850 * Use the ephemeral values we saved when generating the 1749 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1851 * ServerKeyExchange message. 1750 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY);
1852 */
1853 if ((ecdh = S3I(s)->tmp.ecdh) == NULL) {
1854 SSLerror(s, ERR_R_INTERNAL_ERROR);
1855 goto err; 1751 goto err;
1856 } 1752 }
1857 1753
1858 /*
1859 * Get client's public key from encoded point in the ClientKeyExchange
1860 * message.
1861 */
1862 if (!CBS_get_u8_length_prefixed(cbs, &public)) 1754 if (!CBS_get_u8_length_prefixed(cbs, &public))
1863 goto err; 1755 goto err;
1864 if (CBS_len(cbs) != 0) 1756 if (!tls_key_share_peer_public(S3I(s)->hs.key_share, &public, NULL))
1865 goto err; 1757 goto err;
1866 1758
1867 if ((ecdh_peer = EC_KEY_new()) == NULL) 1759 if (!tls_key_share_derive(S3I(s)->hs.key_share, &key, &key_len))
1868 goto err; 1760 goto err;
1869 1761
1870 if (!ssl_kex_peer_public_ecdhe_ecp(ecdh_peer, S3I(s)->tmp.ecdh_nid,
1871 &public))
1872 goto err;
1873
1874 /* Derive the shared secret and compute master secret. */
1875 if (!ssl_kex_derive_ecdhe_ecp(ecdh, ecdh_peer, &key, &key_len))
1876 goto err;
1877 if (!tls12_derive_master_secret(s, key, key_len)) 1762 if (!tls12_derive_master_secret(s, key, key_len))
1878 goto err; 1763 goto err;
1879 1764
1880 EC_KEY_free(S3I(s)->tmp.ecdh);
1881 S3I(s)->tmp.ecdh = NULL;
1882 S3I(s)->tmp.ecdh_nid = NID_undef;
1883
1884 ret = 1; 1765 ret = 1;
1885 1766
1886 err: 1767 err:
1887 freezero(key, key_len); 1768 freezero(key, key_len);
1888 EC_KEY_free(ecdh_peer);
1889 1769
1890 return ret; 1770 return ret;
1891} 1771}
1892 1772
1893static int 1773static int
1894ssl3_get_client_kex_ecdhe_ecx(SSL *s, CBS *cbs)
1895{
1896 uint8_t *shared_key = NULL;
1897 CBS ecpoint;
1898 int ret = 0;
1899
1900 if (!CBS_get_u8_length_prefixed(cbs, &ecpoint))
1901 goto err;
1902 if (CBS_len(cbs) != 0)
1903 goto err;
1904 if (CBS_len(&ecpoint) != X25519_KEY_LENGTH)
1905 goto err;
1906
1907 if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL)
1908 goto err;
1909 if (!X25519(shared_key, S3I(s)->tmp.x25519, CBS_data(&ecpoint)))
1910 goto err;
1911
1912 freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH);
1913 S3I(s)->tmp.x25519 = NULL;
1914
1915 if (!tls12_derive_master_secret(s, shared_key, X25519_KEY_LENGTH))
1916 goto err;
1917
1918 ret = 1;
1919
1920 err:
1921 freezero(shared_key, X25519_KEY_LENGTH);
1922
1923 return ret;
1924}
1925
1926static int
1927ssl3_get_client_kex_ecdhe(SSL *s, CBS *cbs)
1928{
1929 if (S3I(s)->tmp.x25519 != NULL)
1930 return ssl3_get_client_kex_ecdhe_ecx(s, cbs);
1931
1932 return ssl3_get_client_kex_ecdhe_ecp(s, cbs);
1933}
1934
1935static int
1936ssl3_get_client_kex_gost(SSL *s, CBS *cbs) 1774ssl3_get_client_kex_gost(SSL *s, CBS *cbs)
1937{ 1775{
1938 EVP_PKEY_CTX *pkey_ctx; 1776 EVP_PKEY_CTX *pkey_ctx;
diff --git a/src/lib/libssl/tls13_server.c b/src/lib/libssl/tls13_server.c
index 739ef06609..c32ae22779 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.90 2022/01/05 17:10:02 jsing Exp $ */ 1/* $OpenBSD: tls13_server.c,v 1.91 2022/01/07 15:46:30 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>
@@ -360,8 +360,8 @@ tls13_server_engage_record_protection(struct tls13_ctx *ctx)
360 SSL *s = ctx->ssl; 360 SSL *s = ctx->ssl;
361 int ret = 0; 361 int ret = 0;
362 362
363 if (!tls_key_share_derive(ctx->hs->key_share, 363 if (!tls_key_share_derive(ctx->hs->key_share, &shared_key,
364 &shared_key, &shared_key_len)) 364 &shared_key_len))
365 goto err; 365 goto err;
366 366
367 s->session->cipher = ctx->hs->cipher; 367 s->session->cipher = ctx->hs->cipher;
diff --git a/src/lib/libssl/tls_internal.h b/src/lib/libssl/tls_internal.h
index 7e2beadeac..f7f939215a 100644
--- a/src/lib/libssl/tls_internal.h
+++ b/src/lib/libssl/tls_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_internal.h,v 1.3 2022/01/06 18:23:56 jsing Exp $ */ 1/* $OpenBSD: tls_internal.h,v 1.4 2022/01/07 15:46:30 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018, 2019, 2021 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2018, 2019, 2021 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -18,6 +18,7 @@
18#ifndef HEADER_TLS_INTERNAL_H 18#ifndef HEADER_TLS_INTERNAL_H
19#define HEADER_TLS_INTERNAL_H 19#define HEADER_TLS_INTERNAL_H
20 20
21#include <openssl/dh.h>
21#include <openssl/evp.h> 22#include <openssl/evp.h>
22 23
23#include "bytestring.h" 24#include "bytestring.h"
@@ -64,8 +65,11 @@ void tls_key_share_free(struct tls_key_share *ks);
64 65
65uint16_t tls_key_share_group(struct tls_key_share *ks); 66uint16_t tls_key_share_group(struct tls_key_share *ks);
66int tls_key_share_nid(struct tls_key_share *ks); 67int tls_key_share_nid(struct tls_key_share *ks);
68void tls_key_share_set_key_bits(struct tls_key_share *ks, size_t key_bits);
69int tls_key_share_set_dh_params(struct tls_key_share *ks, DH *dh_params);
67int tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey); 70int tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey);
68int tls_key_share_generate(struct tls_key_share *ks); 71int tls_key_share_generate(struct tls_key_share *ks);
72int tls_key_share_params(struct tls_key_share *ks, CBB *cbb);
69int tls_key_share_public(struct tls_key_share *ks, CBB *cbb); 73int tls_key_share_public(struct tls_key_share *ks, CBB *cbb);
70int tls_key_share_peer_params(struct tls_key_share *ks, CBS *cbs, 74int tls_key_share_peer_params(struct tls_key_share *ks, CBS *cbs,
71 int *invalid_params); 75 int *invalid_params);
diff --git a/src/lib/libssl/tls_key_share.c b/src/lib/libssl/tls_key_share.c
index 6e390f4a24..eb30a0ea69 100644
--- a/src/lib/libssl/tls_key_share.c
+++ b/src/lib/libssl/tls_key_share.c
@@ -1,6 +1,6 @@
1/* $OpenBSD: tls_key_share.c,v 1.2 2022/01/06 18:23:56 jsing Exp $ */ 1/* $OpenBSD: tls_key_share.c,v 1.3 2022/01/07 15:46:30 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020, 2021 Joel Sing <jsing@openbsd.org>
4 * 4 *
5 * Permission to use, copy, modify, and distribute this software for any 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 6 * purpose with or without fee is hereby granted, provided that the above
@@ -18,7 +18,9 @@
18#include <stdlib.h> 18#include <stdlib.h>
19 19
20#include <openssl/curve25519.h> 20#include <openssl/curve25519.h>
21#include <openssl/dh.h>
21#include <openssl/ec.h> 22#include <openssl/ec.h>
23#include <openssl/evp.h>
22 24
23#include "bytestring.h" 25#include "bytestring.h"
24#include "ssl_locl.h" 26#include "ssl_locl.h"
@@ -27,6 +29,7 @@
27struct tls_key_share { 29struct tls_key_share {
28 int nid; 30 int nid;
29 uint16_t group_id; 31 uint16_t group_id;
32 size_t key_bits;
30 33
31 DH *dhe; 34 DH *dhe;
32 DH *dhe_peer; 35 DH *dhe_peer;
@@ -108,6 +111,28 @@ tls_key_share_nid(struct tls_key_share *ks)
108 return ks->nid; 111 return ks->nid;
109} 112}
110 113
114void
115tls_key_share_set_key_bits(struct tls_key_share *ks, size_t key_bits)
116{
117 ks->key_bits = key_bits;
118}
119
120int
121tls_key_share_set_dh_params(struct tls_key_share *ks, DH *dh_params)
122{
123 if (ks->nid != NID_dhKeyAgreement)
124 return 0;
125 if (ks->dhe != NULL || ks->dhe_peer != NULL)
126 return 0;
127
128 if ((ks->dhe = DHparams_dup(dh_params)) == NULL)
129 return 0;
130 if ((ks->dhe_peer = DHparams_dup(dh_params)) == NULL)
131 return 0;
132
133 return 1;
134}
135
111int 136int
112tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey) 137tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey)
113{ 138{
@@ -126,10 +151,28 @@ tls_key_share_peer_pkey(struct tls_key_share *ks, EVP_PKEY *pkey)
126static int 151static int
127tls_key_share_generate_dhe(struct tls_key_share *ks) 152tls_key_share_generate_dhe(struct tls_key_share *ks)
128{ 153{
129 if (ks->dhe == NULL) 154 /*
155 * If auto params are not being used then we must already have DH
156 * parameters set.
157 */
158 if (ks->key_bits == 0) {
159 if (ks->dhe == NULL)
160 return 0;
161
162 return ssl_kex_generate_dhe(ks->dhe, ks->dhe);
163 }
164
165 if (ks->dhe != NULL || ks->dhe_peer != NULL)
166 return 0;
167
168 if ((ks->dhe = DH_new()) == NULL)
169 return 0;
170 if (!ssl_kex_generate_dhe_params_auto(ks->dhe, ks->key_bits))
171 return 0;
172 if ((ks->dhe_peer = DHparams_dup(ks->dhe)) == NULL)
130 return 0; 173 return 0;
131 174
132 return ssl_kex_generate_dhe(ks->dhe, ks->dhe); 175 return 1;
133} 176}
134 177
135static int 178static int
@@ -200,6 +243,24 @@ tls_key_share_generate(struct tls_key_share *ks)
200} 243}
201 244
202static int 245static int
246tls_key_share_params_dhe(struct tls_key_share *ks, CBB *cbb)
247{
248 if (ks->dhe == NULL)
249 return 0;
250
251 return ssl_kex_params_dhe(ks->dhe, cbb);
252}
253
254int
255tls_key_share_params(struct tls_key_share *ks, CBB *cbb)
256{
257 if (ks->nid == NID_dhKeyAgreement)
258 return tls_key_share_params_dhe(ks, cbb);
259
260 return 0;
261}
262
263static int
203tls_key_share_public_dhe(struct tls_key_share *ks, CBB *cbb) 264tls_key_share_public_dhe(struct tls_key_share *ks, CBB *cbb)
204{ 265{
205 if (ks->dhe == NULL) 266 if (ks->dhe == NULL)