summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorbeck <>2019-11-15 15:14:02 +0000
committerbeck <>2019-11-15 15:14:02 +0000
commit6ed1a438e76e4b38c933c65009239e991044c6b7 (patch)
tree97a70efefebf8e9452c3aefebe8f5746f6603d6b /src/lib
parent5a623efc3d1b522e895f2480f2031408c7edd803 (diff)
downloadopenbsd-6ed1a438e76e4b38c933c65009239e991044c6b7.tar.gz
openbsd-6ed1a438e76e4b38c933c65009239e991044c6b7.tar.bz2
openbsd-6ed1a438e76e4b38c933c65009239e991044c6b7.zip
Deduplicate some extension processing code.
ok tb@ inoguchi@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libssl/ssl_tlsext.c105
1 files changed, 47 insertions, 58 deletions
diff --git a/src/lib/libssl/ssl_tlsext.c b/src/lib/libssl/ssl_tlsext.c
index 91b74b5d3f..f91b790f99 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.49 2019/05/29 17:28:37 jsing Exp $ */ 1/* $OpenBSD: ssl_tlsext.c,v 1.50 2019/11/15 15:14:02 beck 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>
@@ -1223,17 +1223,11 @@ tlsext_keyshare_client_needs(SSL *s)
1223 TLS1_3_VERSION); 1223 TLS1_3_VERSION);
1224} 1224}
1225 1225
1226int 1226static int
1227tlsext_keyshare_client_build(SSL *s, CBB *cbb) 1227tlsext_keyshare_x25519_internal(SSL *s, CBB *cbb)
1228{ 1228{
1229 uint8_t *public_key = NULL, *private_key = NULL; 1229 uint8_t *public_key = NULL, *private_key = NULL;
1230 CBB client_shares, key_exchange; 1230 CBB key_exchange;
1231
1232 /* Generate and provide key shares. */
1233 if (!CBB_add_u16_length_prefixed(cbb, &client_shares))
1234 return 0;
1235
1236 /* XXX - other groups. */
1237 1231
1238 /* Generate X25519 key pair. */ 1232 /* Generate X25519 key pair. */
1239 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) 1233 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
@@ -1243,16 +1237,13 @@ tlsext_keyshare_client_build(SSL *s, CBB *cbb)
1243 X25519_keypair(public_key, private_key); 1237 X25519_keypair(public_key, private_key);
1244 1238
1245 /* Add the group and serialize the public key. */ 1239 /* Add the group and serialize the public key. */
1246 if (!CBB_add_u16(&client_shares, tls1_ec_nid2curve_id(NID_X25519))) 1240 if (!CBB_add_u16(cbb, tls1_ec_nid2curve_id(NID_X25519)))
1247 goto err; 1241 goto err;
1248 if (!CBB_add_u16_length_prefixed(&client_shares, &key_exchange)) 1242 if (!CBB_add_u16_length_prefixed(cbb, &key_exchange))
1249 goto err; 1243 goto err;
1250 if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH)) 1244 if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH))
1251 goto err; 1245 goto err;
1252 1246
1253 if (!CBB_flush(cbb))
1254 goto err;
1255
1256 S3I(s)->hs_tls13.x25519_public = public_key; 1247 S3I(s)->hs_tls13.x25519_public = public_key;
1257 S3I(s)->hs_tls13.x25519_private = private_key; 1248 S3I(s)->hs_tls13.x25519_private = private_key;
1258 1249
@@ -1266,6 +1257,32 @@ tlsext_keyshare_client_build(SSL *s, CBB *cbb)
1266} 1257}
1267 1258
1268int 1259int
1260tlsext_keyshare_client_build(SSL *s, CBB *cbb)
1261{
1262 CBB client_shares;
1263
1264 /* Generate and provide key shares. */
1265 if (!CBB_add_u16_length_prefixed(cbb, &client_shares))
1266 return 0;
1267
1268 /* XXX - other groups. */
1269
1270 if (!tlsext_keyshare_x25519_internal(s, &client_shares))
1271 return 0;
1272
1273 if (!CBB_flush(cbb))
1274 goto err;
1275
1276 return 1;
1277
1278 err:
1279 freezero(S3I(s)->hs_tls13.x25519_public, X25519_KEY_LENGTH);
1280 freezero(S3I(s)->hs_tls13.x25519_private, X25519_KEY_LENGTH);
1281
1282 return 0;
1283}
1284
1285int
1269tlsext_keyshare_server_parse(SSL *s, CBS *cbs, int *alert) 1286tlsext_keyshare_server_parse(SSL *s, CBS *cbs, int *alert)
1270{ 1287{
1271 CBS client_shares; 1288 CBS client_shares;
@@ -1324,41 +1341,21 @@ tlsext_keyshare_server_needs(SSL *s)
1324int 1341int
1325tlsext_keyshare_server_build(SSL *s, CBB *cbb) 1342tlsext_keyshare_server_build(SSL *s, CBB *cbb)
1326{ 1343{
1327 uint8_t *public_key = NULL, *private_key = NULL;
1328 CBB key_exchange;
1329
1330 /* XXX deduplicate with client code */
1331
1332 /* X25519 */ 1344 /* X25519 */
1333 if (S3I(s)->hs_tls13.x25519_peer_public == NULL) 1345 if (S3I(s)->hs_tls13.x25519_peer_public == NULL)
1334 return 0; 1346 return 0;
1335 1347
1336 /* Generate X25519 key pair. */ 1348 if (!tlsext_keyshare_x25519_internal(s, cbb))
1337 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) 1349 return 0;
1338 goto err;
1339 if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL)
1340 goto err;
1341 X25519_keypair(public_key, private_key);
1342
1343 /* Add the group and serialize the public key. */
1344 if (!CBB_add_u16(cbb, tls1_ec_nid2curve_id(NID_X25519)))
1345 goto err;
1346 if (!CBB_add_u16_length_prefixed(cbb, &key_exchange))
1347 goto err;
1348 if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH))
1349 goto err;
1350 1350
1351 if (!CBB_flush(cbb)) 1351 if (!CBB_flush(cbb))
1352 goto err; 1352 goto err;
1353 1353
1354 S3I(s)->hs_tls13.x25519_public = public_key;
1355 S3I(s)->hs_tls13.x25519_private = private_key;
1356
1357 return 1; 1354 return 1;
1358 1355
1359 err: 1356 err:
1360 freezero(public_key, X25519_KEY_LENGTH); 1357 freezero(S3I(s)->hs_tls13.x25519_public, X25519_KEY_LENGTH);
1361 freezero(private_key, X25519_KEY_LENGTH); 1358 freezero(S3I(s)->hs_tls13.x25519_private, X25519_KEY_LENGTH);
1362 1359
1363 return 0; 1360 return 0;
1364} 1361}
@@ -1534,8 +1531,8 @@ tlsext_cookie_client_needs(SSL *s)
1534 S3I(s)->hs_tls13.cookie != NULL); 1531 S3I(s)->hs_tls13.cookie != NULL);
1535} 1532}
1536 1533
1537int 1534static int
1538tlsext_cookie_client_build(SSL *s, CBB *cbb) 1535tlsext_cookie_build_internal(SSL *s, CBB *cbb)
1539{ 1536{
1540 CBB cookie; 1537 CBB cookie;
1541 1538
@@ -1553,6 +1550,12 @@ tlsext_cookie_client_build(SSL *s, CBB *cbb)
1553} 1550}
1554 1551
1555int 1552int
1553tlsext_cookie_client_build(SSL *s, CBB *cbb)
1554{
1555 return tlsext_cookie_build_internal(s, cbb);
1556}
1557
1558int
1556tlsext_cookie_server_parse(SSL *s, CBS *cbs, int *alert) 1559tlsext_cookie_server_parse(SSL *s, CBS *cbs, int *alert)
1557{ 1560{
1558 CBS cookie; 1561 CBS cookie;
@@ -1599,25 +1602,11 @@ tlsext_cookie_server_needs(SSL *s)
1599} 1602}
1600 1603
1601int 1604int
1602tlsext_cookie_server_build(SSL *s, CBB *cbb) 1605tlsext_cookie_server_build(SSL *s, CBB *cbb) {
1603{ 1606 return tlsext_cookie_build_internal(s, cbb);
1604 CBB cookie;
1605
1606 /* XXX deduplicate with client code */
1607
1608 if (!CBB_add_u16_length_prefixed(cbb, &cookie))
1609 return 0;
1610
1611 if (!CBB_add_bytes(&cookie, S3I(s)->hs_tls13.cookie,
1612 S3I(s)->hs_tls13.cookie_len))
1613 return 0;
1614
1615 if (!CBB_flush(cbb))
1616 return 0;
1617
1618 return 1;
1619} 1607}
1620 1608
1609
1621int 1610int
1622tlsext_cookie_client_parse(SSL *s, CBS *cbs, int *alert) 1611tlsext_cookie_client_parse(SSL *s, CBS *cbs, int *alert)
1623{ 1612{