Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 71397ae

Browse files
committed
deps: Add interface required to implement QUIC draft-17
Ported from tatsuhiro-t/openssl@920a331 PR-URL: #6 Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent 026e997 commit 71397ae

File tree

7 files changed

+229
-3
lines changed

7 files changed

+229
-3
lines changed

deps/openssl/openssl/include/openssl/ssl.h

+19
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ typedef int (*SSL_verify_cb)(int preverify_ok, X509_STORE_CTX *x509_ctx);
507507
*/
508508
# define SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG 0x00000400U
509509

510+
/*
511+
* Support QUIC Hack
512+
*/
513+
# define SSL_MODE_QUIC_HACK 0x00000800U
514+
510515
/* Cert related flags */
511516
/*
512517
* Many implementations ignore some aspects of the TLS standards such as
@@ -634,6 +639,20 @@ void SSL_set_msg_callback(SSL *ssl,
634639
# define SSL_CTX_set_msg_callback_arg(ctx, arg) SSL_CTX_ctrl((ctx), SSL_CTRL_SET_MSG_CALLBACK_ARG, 0, (arg))
635640
# define SSL_set_msg_callback_arg(ssl, arg) SSL_ctrl((ssl), SSL_CTRL_SET_MSG_CALLBACK_ARG, 0, (arg))
636641

642+
typedef enum {
643+
SSL_KEY_CLIENT_EARLY_TRAFFIC,
644+
SSL_KEY_CLIENT_HANDSHAKE_TRAFFIC,
645+
SSL_KEY_CLIENT_APPLICATION_TRAFFIC,
646+
SSL_KEY_SERVER_HANDSHAKE_TRAFFIC,
647+
SSL_KEY_SERVER_APPLICATION_TRAFFIC
648+
} OSSL_KEY_TYPE;
649+
650+
void SSL_set_key_callback(SSL *ssl,
651+
int (*cb)(SSL *ssl, int name,
652+
const unsigned char *secret,
653+
size_t secretlen, void *arg),
654+
void *arg);
655+
637656
# define SSL_get_extms_support(s) \
638657
SSL_ctrl((s),SSL_CTRL_GET_EXTMS_SUPPORT,0,NULL)
639658

deps/openssl/openssl/ssl/record/rec_layer_s3.c

+136
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <limits.h>
1212
#include <errno.h>
13+
#include <assert.h>
1314
#include "../ssl_locl.h"
1415
#include <openssl/evp.h>
1516
#include <openssl/buffer.h>
@@ -347,6 +348,22 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, size_t len,
347348
int i;
348349
size_t tmpwrit;
349350

351+
if (s->mode & SSL_MODE_QUIC_HACK) {
352+
/* If we have an alert to send, lets send it */
353+
if (s->s3->alert_dispatch) {
354+
i = s->method->ssl_dispatch_alert(s);
355+
if (i <= 0) {
356+
/* SSLfatal() already called if appropriate */
357+
return i;
358+
}
359+
}
360+
361+
s->rwstate = SSL_WRITING;
362+
*written = len;
363+
364+
return 1;
365+
}
366+
350367
s->rwstate = SSL_NOTHING;
351368
tot = s->rlayer.wnum;
352369
/*
@@ -667,6 +684,10 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
667684
size_t totlen = 0, len, wpinited = 0;
668685
size_t j;
669686

687+
if (s->mode & SSL_MODE_QUIC_HACK) {
688+
assert(0);
689+
}
690+
670691
for (j = 0; j < numpipes; j++)
671692
totlen += pipelens[j];
672693
/*
@@ -1131,6 +1152,10 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
11311152
size_t currbuf = 0;
11321153
size_t tmpwrit = 0;
11331154

1155+
if (s->mode & SSL_MODE_QUIC_HACK) {
1156+
assert(0);
1157+
}
1158+
11341159
if ((s->rlayer.wpend_tot > len)
11351160
|| (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
11361161
&& (s->rlayer.wpend_buf != buf))
@@ -1234,6 +1259,117 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
12341259
}
12351260
}
12361261

1262+
if (s->mode & SSL_MODE_QUIC_HACK) {
1263+
/* In QUIC, we only expect handshake protocol. Alerts are
1264+
notified by decicated API function. */
1265+
if (!ossl_statem_get_in_handshake(s)) {
1266+
/* We found handshake data, so we're going back into init */
1267+
ossl_statem_set_in_init(s, 1);
1268+
1269+
i = s->handshake_func(s);
1270+
/* SSLfatal() already called if appropriate */
1271+
if (i < 0)
1272+
return i;
1273+
if (i == 0) {
1274+
return -1;
1275+
}
1276+
*readbytes = 0;
1277+
return 1;
1278+
}
1279+
1280+
if (s->rlayer.packet_length == 0) {
1281+
if (rbuf->left < 4) {
1282+
if (rbuf->len - rbuf->offset < 4 - rbuf->left) {
1283+
memmove(rbuf->buf, rbuf->buf + rbuf->offset - rbuf->left,
1284+
rbuf->left);
1285+
rbuf->offset = rbuf->left;
1286+
}
1287+
s->rwstate = SSL_READING;
1288+
/* TODO(size_t): Convert this function */
1289+
ret = BIO_read(s->rbio, rbuf->buf + rbuf->offset,
1290+
rbuf->len - rbuf->offset);
1291+
if (ret < 0) {
1292+
return -1;
1293+
}
1294+
/* TODO Check this is really ok */
1295+
if (ret == 0) {
1296+
*readbytes = 0;
1297+
return 1;
1298+
}
1299+
1300+
rbuf->left += ret;
1301+
rbuf->offset += ret;
1302+
1303+
if (rbuf->left < 4) {
1304+
*readbytes = 0;
1305+
return 1;
1306+
}
1307+
rbuf->offset -= rbuf->left;
1308+
}
1309+
1310+
switch (rbuf->buf[rbuf->offset]) {
1311+
case SSL3_MT_CLIENT_HELLO:
1312+
case SSL3_MT_SERVER_HELLO:
1313+
case SSL3_MT_NEWSESSION_TICKET:
1314+
case SSL3_MT_END_OF_EARLY_DATA:
1315+
case SSL3_MT_ENCRYPTED_EXTENSIONS:
1316+
case SSL3_MT_CERTIFICATE:
1317+
case SSL3_MT_CERTIFICATE_REQUEST:
1318+
case SSL3_MT_CERTIFICATE_VERIFY:
1319+
case SSL3_MT_FINISHED:
1320+
case SSL3_MT_KEY_UPDATE:
1321+
case SSL3_MT_MESSAGE_HASH:
1322+
break;
1323+
default:
1324+
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
1325+
ERR_R_INTERNAL_ERROR);
1326+
return -1;
1327+
}
1328+
1329+
s->rlayer.packet_length = (rbuf->buf[rbuf->offset + 1] << 16)
1330+
+ (rbuf->buf[rbuf->offset + 2] << 8)
1331+
+ rbuf->buf[rbuf->offset + 3] + 4;
1332+
}
1333+
1334+
if (s->rlayer.packet_length) {
1335+
size_t n;
1336+
1337+
n = len < s->rlayer.packet_length ? len : s->rlayer.packet_length;
1338+
if (rbuf->left == 0) {
1339+
s->rwstate = SSL_READING;
1340+
ret = BIO_read(s->rbio, buf, n);
1341+
if (ret >= 0) {
1342+
s->rlayer.packet_length -= ret;
1343+
*readbytes = ret;
1344+
if (recvd_type) {
1345+
*recvd_type = SSL3_RT_HANDSHAKE;
1346+
}
1347+
return 1;
1348+
}
1349+
return -1;
1350+
}
1351+
1352+
n = n < rbuf->left ? n : rbuf->left;
1353+
1354+
memcpy(buf, rbuf->buf + rbuf->offset, n);
1355+
rbuf->offset += n;
1356+
rbuf->left -= n;
1357+
s->rlayer.packet_length -= n;
1358+
if (rbuf->left == 0) {
1359+
rbuf->offset = 0;
1360+
}
1361+
*readbytes = n;
1362+
if (recvd_type) {
1363+
*recvd_type = SSL3_RT_HANDSHAKE;
1364+
}
1365+
return 1;
1366+
}
1367+
1368+
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
1369+
ERR_R_INTERNAL_ERROR);
1370+
return -1;
1371+
}
1372+
12371373
if ((type && (type != SSL3_RT_APPLICATION_DATA)
12381374
&& (type != SSL3_RT_HANDSHAKE)) || (peek
12391375
&& (type !=

deps/openssl/openssl/ssl/s3_msg.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@ int ssl3_dispatch_alert(SSL *s)
7474
size_t written;
7575

7676
s->s3->alert_dispatch = 0;
77-
alertlen = 2;
78-
i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0,
79-
&written);
77+
78+
if (!(s->mode & SSL_MODE_QUIC_HACK)) {
79+
alertlen = 2;
80+
i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1,
81+
0, &written);
82+
} else {
83+
s->rwstate = SSL_WRITING;
84+
i = 1;
85+
}
86+
8087
if (i <= 0) {
8188
s->s3->alert_dispatch = 1;
8289
} else {

deps/openssl/openssl/ssl/ssl_lib.c

+10
Original file line numberDiff line numberDiff line change
@@ -4327,6 +4327,16 @@ void SSL_set_msg_callback(SSL *ssl,
43274327
SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
43284328
}
43294329

4330+
void SSL_set_key_callback(SSL *ssl,
4331+
int (*cb)(SSL *ssl, int name,
4332+
const unsigned char *secret,
4333+
size_t secretlen, void *arg),
4334+
void *arg)
4335+
{
4336+
ssl->key_callback = cb;
4337+
ssl->key_callback_arg = arg;
4338+
}
4339+
43304340
void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
43314341
int (*cb) (SSL *ssl,
43324342
int

deps/openssl/openssl/ssl/ssl_locl.h

+3
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,9 @@ struct ssl_st {
11251125
void (*msg_callback) (int write_p, int version, int content_type,
11261126
const void *buf, size_t len, SSL *ssl, void *arg);
11271127
void *msg_callback_arg;
1128+
int (*key_callback)(SSL *ssl, int name, const unsigned char *secret,
1129+
size_t secretlen, void *arg);
1130+
void *key_callback_arg;
11281131
int hit; /* reusing a previous session */
11291132
X509_VERIFY_PARAM *param;
11301133
/* Per connection DANE state */

deps/openssl/openssl/ssl/tls13_enc.c

+50
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,56 @@ int tls13_change_cipher_state(SSL *s, int which)
671671
goto err;
672672
}
673673

674+
if (s->key_callback) {
675+
int type;
676+
if (label == client_early_traffic) {
677+
type = SSL_KEY_CLIENT_EARLY_TRAFFIC;
678+
} else if (label == client_handshake_traffic) {
679+
type = SSL_KEY_CLIENT_HANDSHAKE_TRAFFIC;
680+
} else if (label == client_application_traffic) {
681+
type = SSL_KEY_CLIENT_APPLICATION_TRAFFIC;
682+
} else if (label == server_handshake_traffic) {
683+
type = SSL_KEY_SERVER_HANDSHAKE_TRAFFIC;
684+
} else if (label == server_application_traffic) {
685+
type = SSL_KEY_SERVER_APPLICATION_TRAFFIC;
686+
} else {
687+
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_CHANGE_CIPHER_STATE,
688+
ERR_R_INTERNAL_ERROR);
689+
goto err;
690+
}
691+
if (!s->key_callback(s, type, secret, hashlen, s->key_callback_arg)) {
692+
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_CHANGE_CIPHER_STATE,
693+
ERR_R_INTERNAL_ERROR);
694+
goto err;
695+
}
696+
697+
if (s->server) {
698+
switch (type) {
699+
case SSL_KEY_CLIENT_HANDSHAKE_TRAFFIC:
700+
case SSL_KEY_CLIENT_APPLICATION_TRAFFIC:
701+
if (s->rlayer.rbuf.left) {
702+
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
703+
SSL_F_TLS13_CHANGE_CIPHER_STATE,
704+
ERR_R_INTERNAL_ERROR);
705+
goto err;
706+
}
707+
break;
708+
}
709+
} else {
710+
switch (type) {
711+
case SSL_KEY_SERVER_HANDSHAKE_TRAFFIC:
712+
case SSL_KEY_SERVER_APPLICATION_TRAFFIC:
713+
if (s->rlayer.rbuf.left) {
714+
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
715+
SSL_F_TLS13_CHANGE_CIPHER_STATE,
716+
ERR_R_INTERNAL_ERROR);
717+
goto err;
718+
}
719+
break;
720+
}
721+
}
722+
}
723+
674724
if (label == server_application_traffic) {
675725
memcpy(s->server_app_traffic_secret, secret, hashlen);
676726
/* Now we create the exporter master secret */

deps/openssl/openssl/util/libssl.num

+1
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,4 @@ SSL_CTX_get_recv_max_early_data 498 1_1_1 EXIST::FUNCTION:
498498
SSL_CTX_set_recv_max_early_data 499 1_1_1 EXIST::FUNCTION:
499499
SSL_CTX_set_post_handshake_auth 500 1_1_1 EXIST::FUNCTION:
500500
SSL_get_signature_type_nid 501 1_1_1a EXIST::FUNCTION:
501+
SSL_set_key_callback 502 3_0_0 EXIST::FUNCTION:

0 commit comments

Comments
 (0)