Skip to content

Commit 74cbfd3

Browse files
kadukjasnell
authored andcommitted
deps: cherry-pick akamai/openssl/commit/a6282c566d88db11300c82abc3c84a4e2e9ea568
Original Commit Message: Some cleanup for the main QUIC changes Try to reduce unneeded whitespace changes and wrap new code to 80 columns. Reword documentation to attempt to improve clarity. Add some more sanity checks and clarifying comments to the code. Update referenced I-D versions. PR-URL: #34033 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 8a9763a commit 74cbfd3

18 files changed

+166
-111
lines changed

Diff for: deps/openssl/openssl/doc/man3/SSL_CTX_set_quic_method.pod

+24-19
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,25 @@ SSL_quic_max_handshake_flight_len() returns the maximum number of bytes
6363
that may be received at the given encryption level. This function should be
6464
used to limit buffering in the QUIC implementation.
6565

66-
See https://tools.ietf.org/html/draft-ietf-quic-transport-16#section-4.4.
66+
See https://tools.ietf.org/html/draft-ietf-quic-transport-27#section-4.
6767

6868
SSL_quic_read_level() returns the current read encryption level.
6969

7070
SSL_quic_write_level() returns the current write encryption level.
7171

72-
SSL_provide_quic_data() provides data from QUIC at a particular encryption
73-
level B<level>. It is an error to call this function outside of the handshake
74-
or with an encryption level other than the current read level. It returns one
75-
on success and zero on error.
72+
SSL_provide_quic_data() is used to provide data from QUIC CRYPTO frames to the
73+
state machine, at a particular encryption level B<level>. It is an error to
74+
call this function outside of the handshake or with an encryption level other
75+
than the current read level. The application must buffer and consolidate any
76+
frames with less than four bytes of content. It returns one on success and
77+
zero on error.
7678

7779
SSL_process_quic_post_handshake() processes any data that QUIC has provided
7880
after the handshake has completed. This includes NewSessionTicket messages
7981
sent by the server.
8082

81-
SSL_is_quic() indicates whether a connection uses QUIC.
83+
SSL_is_quic() indicates whether a connection uses QUIC. A given B<SSL>
84+
or B<SSL_CTX> can only be used with QUIC or TLS, but not both.
8285

8386
=head1 NOTES
8487

@@ -89,11 +92,11 @@ functions allow a QUIC implementation to serve as the underlying transport as
8992
described in draft-ietf-quic-tls.
9093

9194
When configured for QUIC, SSL_do_handshake() will drive the handshake as
92-
before, but it will not use the configured B<BIO>. It will call functions on
93-
B<SSL_QUIC_METHOD> to configure secrets and send data. If data is needed from
94-
the peer, it will return B<SSL_ERROR_WANT_READ>. When received, the caller
95-
should call SSL_provide_quic_data() and then SSL_do_handshake() to continue
96-
the handshake. After the handshake is complete, the caller should call
95+
before, but it will not use the configured B<BIO>. It will call functions from
96+
the configured B<SSL_QUIC_METHOD> to configure secrets and send data. If data
97+
is needed from the peer, it will return B<SSL_ERROR_WANT_READ>. When received,
98+
the caller should call SSL_provide_quic_data() and then SSL_do_handshake() to
99+
continue the handshake. After the handshake is complete, the caller should call
97100
SSL_provide_quic_data() for any post-handshake data, followed by
98101
SSL_process_quic_post_handshake() to process it. It is an error to call
99102
SSL_read()/SSL_read_ex() and SSL_write()/SSL_write_ex() in QUIC.
@@ -105,19 +108,20 @@ pass the active write level to add_handshake_data() when writing data. Callers
105108
can use SSL_quic_write_level() to query the active write level when
106109
generating their own errors.
107110

108-
See https://tools.ietf.org/html/draft-ietf-quic-tls-15#section-4.1 for more
111+
See https://tools.ietf.org/html/draft-ietf-quic-tls-27#section-4.1 for more
109112
details.
110113

111114
To avoid DoS attacks, the QUIC implementation must limit the amount of data
112115
being queued up. The implementation can call
113116
SSL_quic_max_handshake_flight_len() to get the maximum buffer length at each
114117
encryption level.
115118

116-
draft-ietf-quic-tls defines a new TLS extension quic_transport_parameters
119+
draft-ietf-quic-tls defines a new TLS extension "quic_transport_parameters"
117120
used by QUIC for each endpoint to unilaterally declare its supported
118-
transport parameters. draft-ietf-quic-transport (section 7.4) defines the
119-
contents of that extension (a TransportParameters struct) and describes how
120-
to handle it and its semantic meaning.
121+
transport parameters. The contents of the extension are specified in
122+
https://tools.ietf.org/html/draft-ietf-quic-transport-27#section-18 (as
123+
a sequence of tag/length/value parameters) along with the interpretation of the
124+
various parameters and the rules for their processing.
121125

122126
OpenSSL handles this extension as an opaque byte string. The caller is
123127
responsible for serializing and parsing it.
@@ -205,10 +209,11 @@ SSL_process_quic_post_handshake()
205209
return 1 on success, and 0 on error.
206210

207211
SSL_quic_read_level() and SSL_quic_write_level() return the current
208-
encryption level as B<OSSL_ENCRYPTION_LEVEL> (B<enum ssl_encryption_level_t>).
212+
encryption level as an B<OSSL_ENCRYPTION_LEVEL>
213+
(B<enum ssl_encryption_level_t>).
209214

210-
SSL_quic_max_handshake_flight_len() returns the maximum length of a flight
211-
for a given encryption level.
215+
SSL_quic_max_handshake_flight_len() returns the maximum length in bytes of a
216+
flight for a given encryption level.
212217

213218
SSL_is_quic() returns 1 if QUIC is being used, 0 if not.
214219

Diff for: deps/openssl/openssl/include/openssl/ssl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2473,10 +2473,10 @@ __owur int SSL_process_quic_post_handshake(SSL *ssl);
24732473

24742474
__owur int SSL_is_quic(SSL *ssl);
24752475

2476-
# endif
2477-
24782476
int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *c);
24792477

2478+
# endif
2479+
24802480
# ifdef __cplusplus
24812481
}
24822482
# endif

Diff for: deps/openssl/openssl/include/openssl/sslerr.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#ifndef HEADER_SSLERR_H
1212
# define HEADER_SSLERR_H
1313

14-
# include <openssl/symhacks.h>
14+
# ifndef HEADER_SYMHACKS_H
15+
# include <openssl/symhacks.h>
16+
# endif
1517

1618
# ifdef __cplusplus
1719
extern "C"

Diff for: deps/openssl/openssl/include/openssl/tls1.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ extern "C" {
148148
/* Temporary extension type */
149149
# define TLSEXT_TYPE_renegotiate 0xff01
150150

151-
/* ExtensionType value from draft-ietf-quic-tls-13 */
151+
/* ExtensionType value from draft-ietf-quic-tls-27 */
152152
# define TLSEXT_TYPE_quic_transport_parameters 0xffa5
153153

154154
# ifndef OPENSSL_NO_NEXTPROTONEG

Diff for: deps/openssl/openssl/ssl/build.info

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ SOURCE[../libssl]=\
1212
ssl_asn1.c ssl_txt.c ssl_init.c ssl_conf.c ssl_mcnf.c \
1313
bio_ssl.c ssl_err.c tls_srp.c t1_trce.c ssl_utst.c \
1414
record/ssl3_buffer.c record/ssl3_record.c record/dtls1_bitmap.c \
15-
statem/statem.c record/ssl3_record_tls13.c \
16-
ssl_quic.c statem/statem_quic.c
15+
statem/statem.c record/ssl3_record_tls13.c
16+
17+
IF[{- !$disabled{quic} -}]
18+
SOURCE[../libssl]=ssl_quic.c statem/statem_quic.c
19+
ENDIF

Diff for: deps/openssl/openssl/ssl/ssl_ciph.c

+2
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ int ssl_cert_is_disabled(size_t idx)
21632163
return 0;
21642164
}
21652165

2166+
#ifndef OPENSSL_NO_QUIC
21662167
int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *c)
21672168
{
21682169
switch (c->algorithm2 & (0xFF << TLS1_PRF_DGST_SHIFT)) {
@@ -2194,3 +2195,4 @@ int SSL_CIPHER_get_prf_nid(const SSL_CIPHER *c)
21942195
}
21952196
return NID_undef;
21962197
}
2198+
#endif

Diff for: deps/openssl/openssl/ssl/ssl_lib.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3982,7 +3982,7 @@ EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
39823982

39833983
const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
39843984
{
3985-
if (s->session != NULL)
3985+
if ((s->session != NULL) && (s->session->cipher != NULL))
39863986
return s->session->cipher;
39873987
return NULL;
39883988
}

Diff for: deps/openssl/openssl/ssl/ssl_local.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ struct quic_data_st {
10861086
OSSL_ENCRYPTION_LEVEL level;
10871087
size_t offset;
10881088
size_t length;
1089+
/* char data[]; should be here but C90 VLAs not allowed here */
10891090
};
10901091
typedef struct quic_data_st QUIC_DATA;
10911092
int quic_set_encryption_secrets(SSL *ssl, OSSL_ENCRYPTION_LEVEL level);
@@ -1561,8 +1562,6 @@ typedef struct tls_group_info_st {
15611562
# define TLS_CURVE_CHAR2 0x1
15621563
# define TLS_CURVE_CUSTOM 0x2
15631564

1564-
typedef struct cert_pkey_st CERT_PKEY;
1565-
15661565
/*
15671566
* Structure containing table entry of certificate info corresponding to
15681567
* CERT_PKEY entries

Diff for: deps/openssl/openssl/ssl/ssl_quic.c

+21-24
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "internal/cryptlib.h"
1212
#include "internal/refcount.h"
1313

14-
#ifdef OPENSSL_NO_QUIC
15-
NON_EMPTY_TRANSLATION_UNIT
16-
#else
17-
1814
int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
1915
size_t params_len)
2016
{
@@ -109,10 +105,10 @@ int SSL_provide_quic_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL level,
109105
return 0;
110106
}
111107

112-
/* Split the QUIC messages up, if necessary */
108+
/* Split on handshake message boundaries, if necessary */
113109
while (len > 0) {
114110
QUIC_DATA *qd;
115-
const uint8_t *p = data + 1;
111+
const uint8_t *p;
116112

117113
/* Check for an incomplete block */
118114
qd = ssl->quic_input_data_tail;
@@ -130,6 +126,12 @@ int SSL_provide_quic_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL level,
130126
}
131127
}
132128

129+
if (len < SSL3_HM_HEADER_LENGTH) {
130+
SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, SSL_R_BAD_LENGTH);
131+
return 0;
132+
}
133+
/* TLS Handshake message header has 1-byte type and 3-byte length */
134+
p = data + 1;
133135
n2l3(p, l);
134136
l += SSL3_HM_HEADER_LENGTH;
135137

@@ -163,31 +165,17 @@ int SSL_provide_quic_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL level,
163165

164166
int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
165167
{
166-
switch (ctx->method->version) {
167-
case DTLS1_VERSION:
168-
case DTLS1_2_VERSION:
169-
case DTLS_ANY_VERSION:
170-
case DTLS1_BAD_VER:
168+
if (ctx->method->version != TLS_ANY_VERSION)
171169
return 0;
172-
default:
173-
break;
174-
}
175170
ctx->quic_method = quic_method;
176171
ctx->options &= ~SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
177172
return 1;
178173
}
179174

180175
int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
181176
{
182-
switch (ssl->method->version) {
183-
case DTLS1_VERSION:
184-
case DTLS1_2_VERSION:
185-
case DTLS_ANY_VERSION:
186-
case DTLS1_BAD_VER:
177+
if (ssl->method->version != TLS_ANY_VERSION)
187178
return 0;
188-
default:
189-
break;
190-
}
191179
ssl->quic_method = quic_method;
192180
ssl->options &= ~SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
193181
return 1;
@@ -225,6 +213,12 @@ int quic_set_encryption_secrets(SSL *ssl, OSSL_ENCRYPTION_LEVEL level)
225213
/* May not have selected cipher, yet */
226214
const SSL_CIPHER *c = NULL;
227215

216+
/*
217+
* It probably doesn't make sense to use an (external) PSK session,
218+
* but in theory some kinds of external session caches could be
219+
* implemented using it, so allow psksession to be used as well as
220+
* the regular session.
221+
*/
228222
if (ssl->session != NULL)
229223
c = SSL_SESSION_get0_cipher(ssl->session);
230224
else if (ssl->psksession != NULL)
@@ -268,6 +262,11 @@ int SSL_process_quic_post_handshake(SSL *ssl)
268262
return 0;
269263
}
270264

265+
/*
266+
* This is always safe (we are sure to be at a record boundary) because
267+
* SSL_read()/SSL_write() are never used for QUIC connections -- the
268+
* application data is handled at the QUIC layer instead.
269+
*/
271270
ossl_statem_set_in_init(ssl, 1);
272271
ret = ssl->handshake_func(ssl);
273272
ossl_statem_set_in_init(ssl, 0);
@@ -281,5 +280,3 @@ int SSL_is_quic(SSL* ssl)
281280
{
282281
return SSL_IS_QUIC(ssl);
283282
}
284-
285-
#endif

Diff for: deps/openssl/openssl/ssl/statem/extensions_clnt.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
19361936
#ifndef OPENSSL_NO_QUIC
19371937
/*
19381938
* QUIC server must send 0xFFFFFFFF or it's a PROTOCOL_VIOLATION
1939-
* per draft-ietf-quic-tls-24 S4.5
1939+
* per draft-ietf-quic-tls-27 S4.5
19401940
*/
19411941
if (s->quic_method != NULL && max_early_data != 0xFFFFFFFF) {
19421942
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
@@ -2045,7 +2045,8 @@ int tls_parse_stoc_quic_transport_params(SSL *s, PACKET *pkt, unsigned int conte
20452045
&s->ext.peer_quic_transport_params,
20462046
&s->ext.peer_quic_transport_params_len)) {
20472047
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2048-
SSL_F_TLS_PARSE_STOC_QUIC_TRANSPORT_PARAMS, ERR_R_INTERNAL_ERROR);
2048+
SSL_F_TLS_PARSE_STOC_QUIC_TRANSPORT_PARAMS,
2049+
ERR_R_INTERNAL_ERROR);
20492050
return 0;
20502051
}
20512052
return 1;

Diff for: deps/openssl/openssl/ssl/statem/extensions_srvr.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,8 @@ int tls_parse_ctos_quic_transport_params(SSL *s, PACKET *pkt, unsigned int conte
13161316
&s->ext.peer_quic_transport_params,
13171317
&s->ext.peer_quic_transport_params_len)) {
13181318
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1319-
SSL_F_TLS_PARSE_CTOS_QUIC_TRANSPORT_PARAMS, ERR_R_INTERNAL_ERROR);
1319+
SSL_F_TLS_PARSE_CTOS_QUIC_TRANSPORT_PARAMS,
1320+
ERR_R_INTERNAL_ERROR);
13201321
return 0;
13211322
}
13221323
return 1;
@@ -1952,7 +1953,7 @@ EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
19521953
return EXT_RETURN_NOT_SENT;
19531954

19541955
#ifndef OPENSSL_NO_QUIC
1955-
/* QUIC server must always send 0xFFFFFFFF, per draft-ietf-quic-tls-24 S4.5 */
1956+
/* QUIC server must always send 0xFFFFFFFF, per draft-ietf-quic-tls-27 S4.5 */
19561957
if (s->quic_method != NULL)
19571958
max_early_data = 0xFFFFFFFF;
19581959
#endif
@@ -2016,7 +2017,8 @@ EXT_RETURN tls_construct_stoc_quic_transport_params(SSL *s, WPACKET *pkt,
20162017
|| !WPACKET_sub_memcpy_u16(pkt, s->ext.quic_transport_params,
20172018
s->ext.quic_transport_params_len)) {
20182019
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2019-
SSL_F_TLS_CONSTRUCT_STOC_QUIC_TRANSPORT_PARAMS, ERR_R_INTERNAL_ERROR);
2020+
SSL_F_TLS_CONSTRUCT_STOC_QUIC_TRANSPORT_PARAMS,
2021+
ERR_R_INTERNAL_ERROR);
20202022
return EXT_RETURN_FAIL;
20212023
}
20222024

Diff for: deps/openssl/openssl/ssl/statem/statem.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ static SUB_STATE_RETURN read_state_machine(SSL *s)
577577
ret = dtls_get_message(s, &mt, &len);
578578
#ifndef OPENSSL_NO_QUIC
579579
} else if (SSL_IS_QUIC(s)) {
580+
/* QUIC behaves like DTLS -- all in one go. */
580581
ret = quic_get_message(s, &mt, &len);
581582
#endif
582583
} else {
@@ -907,7 +908,6 @@ int statem_flush(SSL *s)
907908
#ifndef OPENSSL_NO_QUIC
908909
if (SSL_IS_QUIC(s)) {
909910
if (!s->quic_method->flush_flight(s)) {
910-
/* NOTE: BIO_flush() does not generate an error */
911911
SSLerr(SSL_F_STATEM_FLUSH, ERR_R_INTERNAL_ERROR);
912912
return 0;
913913
}

Diff for: deps/openssl/openssl/ssl/statem/statem_lib.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,29 @@ int ssl3_do_write(SSL *s, int type)
4242
{
4343
int ret;
4444
size_t written = 0;
45+
4546
#ifndef OPENSSL_NO_QUIC
46-
if (SSL_IS_QUIC(s) && type == SSL3_RT_HANDSHAKE) {
47-
ret = s->quic_method->add_handshake_data(s, s->quic_write_level,
48-
(const uint8_t*)&s->init_buf->data[s->init_off],
49-
s->init_num);
50-
if (!ret) {
51-
ret = -1;
52-
/* QUIC can't sent anything out sice the above failed */
53-
SSLerr(SSL_F_SSL3_DO_WRITE, SSL_R_INTERNAL_ERROR);
47+
if (SSL_IS_QUIC(s)) {
48+
if (type == SSL3_RT_HANDSHAKE) {
49+
ret = s->quic_method->add_handshake_data(s, s->quic_write_level,
50+
(const uint8_t*)&s->init_buf->data[s->init_off],
51+
s->init_num);
52+
if (!ret) {
53+
ret = -1;
54+
/* QUIC can't sent anything out sice the above failed */
55+
SSLerr(SSL_F_SSL3_DO_WRITE, SSL_R_INTERNAL_ERROR);
56+
} else {
57+
written = s->init_num;
58+
}
5459
} else {
55-
written = s->init_num;
60+
/* QUIC doesn't use ChangeCipherSpec */
61+
ret = -1;
62+
SSLerr(SSL_F_SSL3_DO_WRITE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
5663
}
5764
} else
5865
#endif
5966
ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
6067
s->init_num, &written);
61-
6268
if (ret < 0)
6369
return -1;
6470
if (type == SSL3_RT_HANDSHAKE)
@@ -1171,7 +1177,6 @@ int tls_get_message_header(SSL *s, int *mt)
11711177

11721178
do {
11731179
while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1174-
/* QUIC: either create a special ssl_read_bytes... or if/else this */
11751180
i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
11761181
&p[s->init_num],
11771182
SSL3_HM_HEADER_LENGTH - s->init_num,

Diff for: deps/openssl/openssl/ssl/statem/statem_local.h

+2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
9393
__owur int tls_get_message_header(SSL *s, int *mt);
9494
__owur int tls_get_message_body(SSL *s, size_t *len);
9595
__owur int dtls_get_message(SSL *s, int *mt, size_t *len);
96+
#ifndef OPENSSL_NO_QUIC
9697
__owur int quic_get_message(SSL *s, int *mt, size_t *len);
98+
#endif
9799

98100
/* Message construction and processing functions */
99101
__owur int tls_process_initial_server_flight(SSL *s);

0 commit comments

Comments
 (0)