]>
Commit | Line | Data |
---|---|---|
6dd083fd | 1 | /* |
a28d06f3 | 2 | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
6dd083fd | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6dd083fd MC |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
ab83e314 | 10 | #include <openssl/ocsp.h> |
706457b7 | 11 | #include "../ssl_local.h" |
67dc995e | 12 | #include "internal/cryptlib.h" |
706457b7 | 13 | #include "statem_local.h" |
6dd083fd | 14 | |
b186a592 MC |
15 | EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, |
16 | unsigned int context, X509 *x, | |
f63a17d6 | 17 | size_t chainidx) |
ab83e314 MC |
18 | { |
19 | /* Add RI if renegotiating */ | |
20 | if (!s->renegotiate) | |
b186a592 | 21 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
22 | |
23 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) | |
24 | || !WPACKET_start_sub_packet_u16(pkt) | |
555cbb32 TS |
25 | || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished, |
26 | s->s3.previous_client_finished_len) | |
ab83e314 | 27 | || !WPACKET_close(pkt)) { |
c48ffbcc | 28 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 29 | return EXT_RETURN_FAIL; |
ab83e314 MC |
30 | } |
31 | ||
b186a592 | 32 | return EXT_RETURN_SENT; |
ab83e314 MC |
33 | } |
34 | ||
b186a592 MC |
35 | EXT_RETURN tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, |
36 | unsigned int context, X509 *x, | |
f63a17d6 | 37 | size_t chainidx) |
ab83e314 | 38 | { |
aff8c126 | 39 | if (s->ext.hostname == NULL) |
b186a592 | 40 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
41 | |
42 | /* Add TLS extension servername to the Client Hello message */ | |
43 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) | |
44 | /* Sub-packet for server_name extension */ | |
45 | || !WPACKET_start_sub_packet_u16(pkt) | |
46 | /* Sub-packet for servername list (always 1 hostname)*/ | |
47 | || !WPACKET_start_sub_packet_u16(pkt) | |
48 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) | |
aff8c126 RS |
49 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname, |
50 | strlen(s->ext.hostname)) | |
ab83e314 MC |
51 | || !WPACKET_close(pkt) |
52 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 53 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 54 | return EXT_RETURN_FAIL; |
ab83e314 MC |
55 | } |
56 | ||
b186a592 | 57 | return EXT_RETURN_SENT; |
ab83e314 MC |
58 | } |
59 | ||
cf72c757 F |
60 | /* Push a Max Fragment Len extension into ClientHello */ |
61 | EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL *s, WPACKET *pkt, | |
62 | unsigned int context, X509 *x, | |
f63a17d6 | 63 | size_t chainidx) |
cf72c757 F |
64 | { |
65 | if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED) | |
66 | return EXT_RETURN_NOT_SENT; | |
67 | ||
68 | /* Add Max Fragment Length extension if client enabled it. */ | |
69 | /*- | |
70 | * 4 bytes for this extension type and extension length | |
71 | * 1 byte for the Max Fragment Length code value. | |
72 | */ | |
73 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length) | |
74 | /* Sub-packet for Max Fragment Length extension (1 byte) */ | |
75 | || !WPACKET_start_sub_packet_u16(pkt) | |
76 | || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode) | |
77 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 78 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
cf72c757 F |
79 | return EXT_RETURN_FAIL; |
80 | } | |
81 | ||
82 | return EXT_RETURN_SENT; | |
83 | } | |
84 | ||
ab83e314 | 85 | #ifndef OPENSSL_NO_SRP |
b186a592 | 86 | EXT_RETURN tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 87 | X509 *x, size_t chainidx) |
ab83e314 MC |
88 | { |
89 | /* Add SRP username if there is one */ | |
90 | if (s->srp_ctx.login == NULL) | |
b186a592 | 91 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
92 | |
93 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) | |
94 | /* Sub-packet for SRP extension */ | |
95 | || !WPACKET_start_sub_packet_u16(pkt) | |
96 | || !WPACKET_start_sub_packet_u8(pkt) | |
97 | /* login must not be zero...internal error if so */ | |
98 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) | |
99 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, | |
100 | strlen(s->srp_ctx.login)) | |
101 | || !WPACKET_close(pkt) | |
102 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 103 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 104 | return EXT_RETURN_FAIL; |
ab83e314 MC |
105 | } |
106 | ||
b186a592 | 107 | return EXT_RETURN_SENT; |
ab83e314 MC |
108 | } |
109 | #endif | |
110 | ||
9d2d857f | 111 | static int use_ecc(SSL *s, int min_version, int max_version) |
ab83e314 | 112 | { |
589b6227 | 113 | int i, end, ret = 0; |
ab83e314 MC |
114 | unsigned long alg_k, alg_a; |
115 | STACK_OF(SSL_CIPHER) *cipher_stack = NULL; | |
dbc6268f MC |
116 | const uint16_t *pgroups = NULL; |
117 | size_t num_groups, j; | |
ab83e314 MC |
118 | |
119 | /* See if we support any ECC ciphersuites */ | |
120 | if (s->version == SSL3_VERSION) | |
121 | return 0; | |
122 | ||
589b6227 | 123 | cipher_stack = SSL_get1_supported_ciphers(s); |
1266eefd MC |
124 | end = sk_SSL_CIPHER_num(cipher_stack); |
125 | for (i = 0; i < end; i++) { | |
ab83e314 MC |
126 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
127 | ||
128 | alg_k = c->algorithm_mkey; | |
129 | alg_a = c->algorithm_auth; | |
130 | if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) | |
1266eefd | 131 | || (alg_a & SSL_aECDSA) |
589b6227 MC |
132 | || c->min_tls >= TLS1_3_VERSION) { |
133 | ret = 1; | |
134 | break; | |
135 | } | |
ab83e314 | 136 | } |
589b6227 | 137 | sk_SSL_CIPHER_free(cipher_stack); |
dbc6268f MC |
138 | if (!ret) |
139 | return 0; | |
140 | ||
141 | /* Check we have at least one EC supported group */ | |
142 | tls1_get_supported_groups(s, &pgroups, &num_groups); | |
143 | for (j = 0; j < num_groups; j++) { | |
144 | uint16_t ctmp = pgroups[j]; | |
145 | ||
8b1db5d3 | 146 | if (tls_valid_group(s, ctmp, min_version, max_version, 1, NULL) |
dbc6268f MC |
147 | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
148 | return 1; | |
149 | } | |
150 | ||
151 | return 0; | |
ab83e314 MC |
152 | } |
153 | ||
b186a592 MC |
154 | EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, |
155 | unsigned int context, X509 *x, | |
f63a17d6 | 156 | size_t chainidx) |
ab83e314 MC |
157 | { |
158 | const unsigned char *pformats; | |
159 | size_t num_formats; | |
dbc6268f | 160 | int reason, min_version, max_version; |
ab83e314 | 161 | |
dbc6268f MC |
162 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
163 | if (reason != 0) { | |
c48ffbcc | 164 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
dbc6268f MC |
165 | return EXT_RETURN_FAIL; |
166 | } | |
9d2d857f | 167 | if (!use_ecc(s, min_version, max_version)) |
b186a592 | 168 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
169 | |
170 | /* Add TLS extension ECPointFormats to the ClientHello message */ | |
ab83e314 MC |
171 | tls1_get_formatlist(s, &pformats, &num_formats); |
172 | ||
173 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) | |
174 | /* Sub-packet for formats extension */ | |
175 | || !WPACKET_start_sub_packet_u16(pkt) | |
176 | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) | |
177 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 178 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 179 | return EXT_RETURN_FAIL; |
ab83e314 MC |
180 | } |
181 | ||
b186a592 | 182 | return EXT_RETURN_SENT; |
ab83e314 MC |
183 | } |
184 | ||
b186a592 MC |
185 | EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt, |
186 | unsigned int context, X509 *x, | |
f63a17d6 | 187 | size_t chainidx) |
ab83e314 | 188 | { |
f48d826e | 189 | const uint16_t *pgroups = NULL; |
8b1db5d3 | 190 | size_t num_groups = 0, i, tls13added = 0, added = 0; |
9aaecbfc | 191 | int min_version, max_version, reason; |
ab83e314 | 192 | |
9aaecbfc | 193 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
194 | if (reason != 0) { | |
c48ffbcc | 195 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
9aaecbfc | 196 | return EXT_RETURN_FAIL; |
197 | } | |
198 | ||
8b1db5d3 MC |
199 | /* |
200 | * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore | |
201 | * if we don't have EC support then we don't send this extension. | |
202 | */ | |
203 | if (!use_ecc(s, min_version, max_version) | |
204 | && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)) | |
dbc6268f | 205 | return EXT_RETURN_NOT_SENT; |
dbc6268f | 206 | |
ab83e314 MC |
207 | /* |
208 | * Add TLS extension supported_groups to the ClientHello message | |
209 | */ | |
f48d826e | 210 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
ab83e314 MC |
211 | |
212 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) | |
213 | /* Sub-packet for supported_groups extension */ | |
214 | || !WPACKET_start_sub_packet_u16(pkt) | |
9aaecbfc | 215 | || !WPACKET_start_sub_packet_u16(pkt) |
216 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) { | |
c48ffbcc | 217 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 218 | return EXT_RETURN_FAIL; |
ab83e314 | 219 | } |
9aaecbfc | 220 | /* Copy group ID if supported */ |
f48d826e DSH |
221 | for (i = 0; i < num_groups; i++) { |
222 | uint16_t ctmp = pgroups[i]; | |
8b1db5d3 | 223 | int okfortls13; |
9e84a42d | 224 | |
8b1db5d3 | 225 | if (tls_valid_group(s, ctmp, min_version, max_version, 0, &okfortls13) |
dbc6268f | 226 | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) { |
9e84a42d | 227 | if (!WPACKET_put_bytes_u16(pkt, ctmp)) { |
c48ffbcc | 228 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
9aaecbfc | 229 | return EXT_RETURN_FAIL; |
230 | } | |
8b1db5d3 MC |
231 | if (okfortls13 && max_version == TLS1_3_VERSION) |
232 | tls13added++; | |
233 | added++; | |
ab83e314 MC |
234 | } |
235 | } | |
236 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
9afc6c54 | 237 | if (added == 0) |
8b1db5d3 MC |
238 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, |
239 | "No groups enabled for max supported SSL/TLS version"); | |
240 | else | |
241 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
b186a592 | 242 | return EXT_RETURN_FAIL; |
9afc6c54 MC |
243 | } |
244 | ||
245 | if (tls13added == 0 && max_version == TLS1_3_VERSION) { | |
246 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, | |
247 | "No groups enabled for max supported SSL/TLS version"); | |
248 | return EXT_RETURN_FAIL; | |
ab83e314 MC |
249 | } |
250 | ||
b186a592 | 251 | return EXT_RETURN_SENT; |
ab83e314 | 252 | } |
ab83e314 | 253 | |
b186a592 MC |
254 | EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt, |
255 | unsigned int context, X509 *x, | |
f63a17d6 | 256 | size_t chainidx) |
ab83e314 MC |
257 | { |
258 | size_t ticklen; | |
259 | ||
260 | if (!tls_use_ticket(s)) | |
b186a592 | 261 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
262 | |
263 | if (!s->new_session && s->session != NULL | |
08191294 MC |
264 | && s->session->ext.tick != NULL |
265 | && s->session->ssl_version != TLS1_3_VERSION) { | |
aff8c126 RS |
266 | ticklen = s->session->ext.ticklen; |
267 | } else if (s->session && s->ext.session_ticket != NULL | |
268 | && s->ext.session_ticket->data != NULL) { | |
269 | ticklen = s->ext.session_ticket->length; | |
270 | s->session->ext.tick = OPENSSL_malloc(ticklen); | |
271 | if (s->session->ext.tick == NULL) { | |
c48ffbcc | 272 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 273 | return EXT_RETURN_FAIL; |
ab83e314 | 274 | } |
aff8c126 RS |
275 | memcpy(s->session->ext.tick, |
276 | s->ext.session_ticket->data, ticklen); | |
277 | s->session->ext.ticklen = ticklen; | |
ab83e314 MC |
278 | } else { |
279 | ticklen = 0; | |
280 | } | |
281 | ||
aff8c126 RS |
282 | if (ticklen == 0 && s->ext.session_ticket != NULL && |
283 | s->ext.session_ticket->data == NULL) | |
b186a592 | 284 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
285 | |
286 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) | |
aff8c126 | 287 | || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) { |
c48ffbcc | 288 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 289 | return EXT_RETURN_FAIL; |
ab83e314 MC |
290 | } |
291 | ||
b186a592 | 292 | return EXT_RETURN_SENT; |
ab83e314 MC |
293 | } |
294 | ||
b186a592 MC |
295 | EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, |
296 | unsigned int context, X509 *x, | |
f63a17d6 | 297 | size_t chainidx) |
ab83e314 MC |
298 | { |
299 | size_t salglen; | |
98c792d1 | 300 | const uint16_t *salg; |
ab83e314 MC |
301 | |
302 | if (!SSL_CLIENT_USE_SIGALGS(s)) | |
b186a592 | 303 | return EXT_RETURN_NOT_SENT; |
ab83e314 | 304 | |
a9669ddc | 305 | salglen = tls12_get_psigalgs(s, 1, &salg); |
ab83e314 MC |
306 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) |
307 | /* Sub-packet for sig-algs extension */ | |
308 | || !WPACKET_start_sub_packet_u16(pkt) | |
309 | /* Sub-packet for the actual list */ | |
310 | || !WPACKET_start_sub_packet_u16(pkt) | |
311 | || !tls12_copy_sigalgs(s, pkt, salg, salglen) | |
312 | || !WPACKET_close(pkt) | |
313 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 314 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 315 | return EXT_RETURN_FAIL; |
ab83e314 MC |
316 | } |
317 | ||
b186a592 | 318 | return EXT_RETURN_SENT; |
ab83e314 MC |
319 | } |
320 | ||
321 | #ifndef OPENSSL_NO_OCSP | |
b186a592 MC |
322 | EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt, |
323 | unsigned int context, X509 *x, | |
f63a17d6 | 324 | size_t chainidx) |
ab83e314 MC |
325 | { |
326 | int i; | |
327 | ||
e96e0f8e MC |
328 | /* This extension isn't defined for client Certificates */ |
329 | if (x != NULL) | |
b186a592 | 330 | return EXT_RETURN_NOT_SENT; |
e96e0f8e | 331 | |
aff8c126 | 332 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) |
b186a592 | 333 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
334 | |
335 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) | |
336 | /* Sub-packet for status request extension */ | |
337 | || !WPACKET_start_sub_packet_u16(pkt) | |
338 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) | |
339 | /* Sub-packet for the ids */ | |
340 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
c48ffbcc | 341 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 342 | return EXT_RETURN_FAIL; |
ab83e314 | 343 | } |
aff8c126 | 344 | for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) { |
ab83e314 | 345 | unsigned char *idbytes; |
aff8c126 | 346 | OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i); |
1266eefd | 347 | int idlen = i2d_OCSP_RESPID(id, NULL); |
ab83e314 | 348 | |
ab83e314 MC |
349 | if (idlen <= 0 |
350 | /* Sub-packet for an individual id */ | |
351 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) | |
352 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { | |
c48ffbcc | 353 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 354 | return EXT_RETURN_FAIL; |
ab83e314 MC |
355 | } |
356 | } | |
357 | if (!WPACKET_close(pkt) | |
358 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
c48ffbcc | 359 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 360 | return EXT_RETURN_FAIL; |
ab83e314 | 361 | } |
aff8c126 | 362 | if (s->ext.ocsp.exts) { |
ab83e314 | 363 | unsigned char *extbytes; |
aff8c126 | 364 | int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL); |
ab83e314 MC |
365 | |
366 | if (extlen < 0) { | |
c48ffbcc | 367 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 368 | return EXT_RETURN_FAIL; |
ab83e314 MC |
369 | } |
370 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) | |
aff8c126 | 371 | || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes) |
ab83e314 | 372 | != extlen) { |
c48ffbcc | 373 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 374 | return EXT_RETURN_FAIL; |
ab83e314 MC |
375 | } |
376 | } | |
377 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
c48ffbcc | 378 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 379 | return EXT_RETURN_FAIL; |
ab83e314 MC |
380 | } |
381 | ||
b186a592 | 382 | return EXT_RETURN_SENT; |
ab83e314 MC |
383 | } |
384 | #endif | |
385 | ||
386 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
b186a592 | 387 | EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 388 | X509 *x, size_t chainidx) |
ab83e314 | 389 | { |
c7f47786 | 390 | if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s)) |
b186a592 | 391 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
392 | |
393 | /* | |
394 | * The client advertises an empty extension to indicate its support | |
395 | * for Next Protocol Negotiation | |
396 | */ | |
397 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) | |
398 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
c48ffbcc | 399 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 400 | return EXT_RETURN_FAIL; |
ab83e314 MC |
401 | } |
402 | ||
b186a592 | 403 | return EXT_RETURN_SENT; |
ab83e314 MC |
404 | } |
405 | #endif | |
406 | ||
b186a592 | 407 | EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 408 | X509 *x, size_t chainidx) |
ab83e314 | 409 | { |
555cbb32 | 410 | s->s3.alpn_sent = 0; |
ab83e314 | 411 | |
c7f47786 | 412 | if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s)) |
b186a592 | 413 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
414 | |
415 | if (!WPACKET_put_bytes_u16(pkt, | |
416 | TLSEXT_TYPE_application_layer_protocol_negotiation) | |
417 | /* Sub-packet ALPN extension */ | |
418 | || !WPACKET_start_sub_packet_u16(pkt) | |
aff8c126 | 419 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len) |
ab83e314 | 420 | || !WPACKET_close(pkt)) { |
c48ffbcc | 421 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 422 | return EXT_RETURN_FAIL; |
ab83e314 | 423 | } |
555cbb32 | 424 | s->s3.alpn_sent = 1; |
ab83e314 | 425 | |
b186a592 | 426 | return EXT_RETURN_SENT; |
ab83e314 MC |
427 | } |
428 | ||
429 | ||
430 | #ifndef OPENSSL_NO_SRTP | |
b186a592 MC |
431 | EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, |
432 | unsigned int context, X509 *x, | |
f63a17d6 | 433 | size_t chainidx) |
ab83e314 MC |
434 | { |
435 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s); | |
1266eefd | 436 | int i, end; |
ab83e314 MC |
437 | |
438 | if (clnt == NULL) | |
b186a592 | 439 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
440 | |
441 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) | |
442 | /* Sub-packet for SRTP extension */ | |
443 | || !WPACKET_start_sub_packet_u16(pkt) | |
444 | /* Sub-packet for the protection profile list */ | |
445 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
c48ffbcc | 446 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 447 | return EXT_RETURN_FAIL; |
ab83e314 | 448 | } |
1266eefd MC |
449 | |
450 | end = sk_SRTP_PROTECTION_PROFILE_num(clnt); | |
451 | for (i = 0; i < end; i++) { | |
452 | const SRTP_PROTECTION_PROFILE *prof = | |
453 | sk_SRTP_PROTECTION_PROFILE_value(clnt, i); | |
454 | ||
ab83e314 | 455 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { |
c48ffbcc | 456 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 457 | return EXT_RETURN_FAIL; |
ab83e314 MC |
458 | } |
459 | } | |
460 | if (!WPACKET_close(pkt) | |
461 | /* Add an empty use_mki value */ | |
462 | || !WPACKET_put_bytes_u8(pkt, 0) | |
463 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 464 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 465 | return EXT_RETURN_FAIL; |
ab83e314 MC |
466 | } |
467 | ||
b186a592 | 468 | return EXT_RETURN_SENT; |
ab83e314 MC |
469 | } |
470 | #endif | |
471 | ||
b186a592 | 472 | EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 473 | X509 *x, size_t chainidx) |
ab83e314 MC |
474 | { |
475 | if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) | |
b186a592 | 476 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
477 | |
478 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) | |
479 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
c48ffbcc | 480 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 481 | return EXT_RETURN_FAIL; |
ab83e314 MC |
482 | } |
483 | ||
b186a592 | 484 | return EXT_RETURN_SENT; |
ab83e314 MC |
485 | } |
486 | ||
487 | #ifndef OPENSSL_NO_CT | |
b186a592 | 488 | EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 489 | X509 *x, size_t chainidx) |
ab83e314 MC |
490 | { |
491 | if (s->ct_validation_callback == NULL) | |
b186a592 | 492 | return EXT_RETURN_NOT_SENT; |
ab83e314 | 493 | |
e96e0f8e MC |
494 | /* Not defined for client Certificates */ |
495 | if (x != NULL) | |
b186a592 | 496 | return EXT_RETURN_NOT_SENT; |
e96e0f8e | 497 | |
ab83e314 MC |
498 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) |
499 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
c48ffbcc | 500 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 501 | return EXT_RETURN_FAIL; |
ab83e314 MC |
502 | } |
503 | ||
b186a592 | 504 | return EXT_RETURN_SENT; |
ab83e314 MC |
505 | } |
506 | #endif | |
507 | ||
b186a592 | 508 | EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 509 | X509 *x, size_t chainidx) |
ab83e314 | 510 | { |
088dfa13 TS |
511 | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
512 | return EXT_RETURN_NOT_SENT; | |
513 | ||
ab83e314 MC |
514 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
515 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
c48ffbcc | 516 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 517 | return EXT_RETURN_FAIL; |
ab83e314 MC |
518 | } |
519 | ||
b186a592 | 520 | return EXT_RETURN_SENT; |
ab83e314 MC |
521 | } |
522 | ||
b186a592 MC |
523 | EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt, |
524 | unsigned int context, X509 *x, | |
f63a17d6 | 525 | size_t chainidx) |
ab83e314 MC |
526 | { |
527 | int currv, min_version, max_version, reason; | |
528 | ||
b5b993b2 | 529 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
88050dd1 | 530 | if (reason != 0) { |
c48ffbcc | 531 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
88050dd1 MC |
532 | return EXT_RETURN_FAIL; |
533 | } | |
534 | ||
535 | /* | |
536 | * Don't include this if we can't negotiate TLSv1.3. We can do a straight | |
537 | * comparison here because we will never be called in DTLS. | |
538 | */ | |
539 | if (max_version < TLS1_3_VERSION) | |
540 | return EXT_RETURN_NOT_SENT; | |
541 | ||
ab83e314 MC |
542 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
543 | || !WPACKET_start_sub_packet_u16(pkt) | |
544 | || !WPACKET_start_sub_packet_u8(pkt)) { | |
c48ffbcc | 545 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 546 | return EXT_RETURN_FAIL; |
ab83e314 MC |
547 | } |
548 | ||
ab83e314 | 549 | for (currv = max_version; currv >= min_version; currv--) { |
35e742ec | 550 | if (!WPACKET_put_bytes_u16(pkt, currv)) { |
c48ffbcc | 551 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 552 | return EXT_RETURN_FAIL; |
ab83e314 MC |
553 | } |
554 | } | |
555 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
c48ffbcc | 556 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 557 | return EXT_RETURN_FAIL; |
ab83e314 MC |
558 | } |
559 | ||
b186a592 | 560 | return EXT_RETURN_SENT; |
ab83e314 MC |
561 | } |
562 | ||
b2f7e8c0 | 563 | /* |
e3c0d76b | 564 | * Construct a psk_kex_modes extension. |
b2f7e8c0 | 565 | */ |
b186a592 MC |
566 | EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, |
567 | unsigned int context, X509 *x, | |
f63a17d6 | 568 | size_t chainidx) |
b2f7e8c0 MC |
569 | { |
570 | #ifndef OPENSSL_NO_TLS1_3 | |
e3c0d76b MC |
571 | int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX; |
572 | ||
b2f7e8c0 MC |
573 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes) |
574 | || !WPACKET_start_sub_packet_u16(pkt) | |
575 | || !WPACKET_start_sub_packet_u8(pkt) | |
576 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE) | |
e3c0d76b | 577 | || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)) |
b2f7e8c0 MC |
578 | || !WPACKET_close(pkt) |
579 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 580 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 581 | return EXT_RETURN_FAIL; |
b2f7e8c0 | 582 | } |
b3ad72ce | 583 | |
e3c0d76b MC |
584 | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE; |
585 | if (nodhe) | |
586 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; | |
b2f7e8c0 MC |
587 | #endif |
588 | ||
b186a592 | 589 | return EXT_RETURN_SENT; |
b2f7e8c0 MC |
590 | } |
591 | ||
3847d426 MC |
592 | #ifndef OPENSSL_NO_TLS1_3 |
593 | static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id) | |
594 | { | |
7b1ec1cf MC |
595 | unsigned char *encoded_point = NULL; |
596 | EVP_PKEY *key_share_key = NULL; | |
3847d426 MC |
597 | size_t encodedlen; |
598 | ||
555cbb32 | 599 | if (s->s3.tmp.pkey != NULL) { |
fc7129dc | 600 | if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) { |
c48ffbcc | 601 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
d8028b20 | 602 | return 0; |
7b1ec1cf MC |
603 | } |
604 | /* | |
605 | * Could happen if we got an HRR that wasn't requesting a new key_share | |
606 | */ | |
555cbb32 | 607 | key_share_key = s->s3.tmp.pkey; |
7b1ec1cf | 608 | } else { |
f63a17d6 | 609 | key_share_key = ssl_generate_pkey_group(s, curve_id); |
7b1ec1cf | 610 | if (key_share_key == NULL) { |
f63a17d6 | 611 | /* SSLfatal() already called */ |
d8028b20 | 612 | return 0; |
7b1ec1cf | 613 | } |
3847d426 MC |
614 | } |
615 | ||
616 | /* Encode the public key. */ | |
5ac8fb58 MC |
617 | encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key, |
618 | &encoded_point); | |
3847d426 | 619 | if (encodedlen == 0) { |
c48ffbcc | 620 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
7b1ec1cf | 621 | goto err; |
3847d426 MC |
622 | } |
623 | ||
624 | /* Create KeyShareEntry */ | |
625 | if (!WPACKET_put_bytes_u16(pkt, curve_id) | |
2248dbeb | 626 | || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) { |
c48ffbcc | 627 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
7b1ec1cf | 628 | goto err; |
3847d426 MC |
629 | } |
630 | ||
631 | /* | |
632 | * TODO(TLS1.3): When changing to send more than one key_share we're | |
633 | * going to need to be able to save more than one EVP_PKEY. For now | |
634 | * we reuse the existing tmp.pkey | |
635 | */ | |
555cbb32 TS |
636 | s->s3.tmp.pkey = key_share_key; |
637 | s->s3.group_id = curve_id; | |
2248dbeb | 638 | OPENSSL_free(encoded_point); |
3847d426 | 639 | |
d8028b20 | 640 | return 1; |
7b1ec1cf | 641 | err: |
555cbb32 | 642 | if (s->s3.tmp.pkey == NULL) |
7b1ec1cf MC |
643 | EVP_PKEY_free(key_share_key); |
644 | OPENSSL_free(encoded_point); | |
d8028b20 | 645 | return 0; |
3847d426 MC |
646 | } |
647 | #endif | |
648 | ||
b186a592 MC |
649 | EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, |
650 | unsigned int context, X509 *x, | |
f63a17d6 | 651 | size_t chainidx) |
ab83e314 | 652 | { |
3cf96e88 | 653 | #ifndef OPENSSL_NO_TLS1_3 |
f48d826e DSH |
654 | size_t i, num_groups = 0; |
655 | const uint16_t *pgroups = NULL; | |
9e84a42d | 656 | uint16_t curve_id = 0; |
ab83e314 MC |
657 | |
658 | /* key_share extension */ | |
659 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) | |
660 | /* Extension data sub-packet */ | |
661 | || !WPACKET_start_sub_packet_u16(pkt) | |
662 | /* KeyShare list sub-packet */ | |
663 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
c48ffbcc | 664 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 665 | return EXT_RETURN_FAIL; |
ab83e314 MC |
666 | } |
667 | ||
f48d826e | 668 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
ab83e314 MC |
669 | |
670 | /* | |
671 | * TODO(TLS1.3): Make the number of key_shares sent configurable. For | |
672 | * now, just send one | |
673 | */ | |
555cbb32 TS |
674 | if (s->s3.group_id != 0) { |
675 | curve_id = s->s3.group_id; | |
3847d426 | 676 | } else { |
f48d826e | 677 | for (i = 0; i < num_groups; i++) { |
ab83e314 | 678 | |
dbc6268f | 679 | if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED)) |
3847d426 | 680 | continue; |
ab83e314 | 681 | |
f48d826e | 682 | curve_id = pgroups[i]; |
3847d426 | 683 | break; |
ab83e314 | 684 | } |
3847d426 | 685 | } |
ab83e314 | 686 | |
3847d426 | 687 | if (curve_id == 0) { |
c48ffbcc | 688 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
b186a592 | 689 | return EXT_RETURN_FAIL; |
ab83e314 MC |
690 | } |
691 | ||
f63a17d6 MC |
692 | if (!add_key_share(s, pkt, curve_id)) { |
693 | /* SSLfatal() already called */ | |
b186a592 | 694 | return EXT_RETURN_FAIL; |
f63a17d6 | 695 | } |
3847d426 | 696 | |
ab83e314 | 697 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
c48ffbcc | 698 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 699 | return EXT_RETURN_FAIL; |
ab83e314 | 700 | } |
b186a592 | 701 | return EXT_RETURN_SENT; |
aa2ed504 TS |
702 | #else |
703 | return EXT_RETURN_NOT_SENT; | |
704 | #endif | |
ab83e314 MC |
705 | } |
706 | ||
b186a592 | 707 | EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 708 | X509 *x, size_t chainidx) |
cfef5027 | 709 | { |
b186a592 | 710 | EXT_RETURN ret = EXT_RETURN_FAIL; |
cfef5027 MC |
711 | |
712 | /* Should only be set if we've had an HRR */ | |
713 | if (s->ext.tls13_cookie_len == 0) | |
b186a592 | 714 | return EXT_RETURN_NOT_SENT; |
cfef5027 MC |
715 | |
716 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie) | |
717 | /* Extension data sub-packet */ | |
718 | || !WPACKET_start_sub_packet_u16(pkt) | |
719 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie, | |
720 | s->ext.tls13_cookie_len) | |
721 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 722 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
cfef5027 MC |
723 | goto end; |
724 | } | |
725 | ||
b186a592 | 726 | ret = EXT_RETURN_SENT; |
cfef5027 MC |
727 | end: |
728 | OPENSSL_free(s->ext.tls13_cookie); | |
febb0afa | 729 | s->ext.tls13_cookie = NULL; |
cfef5027 MC |
730 | s->ext.tls13_cookie_len = 0; |
731 | ||
732 | return ret; | |
733 | } | |
734 | ||
b186a592 MC |
735 | EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, |
736 | unsigned int context, X509 *x, | |
f63a17d6 | 737 | size_t chainidx) |
38df5a45 | 738 | { |
696de86f PW |
739 | #ifndef OPENSSL_NO_PSK |
740 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
741 | #endif /* OPENSSL_NO_PSK */ | |
ccb76685 | 742 | const unsigned char *id = NULL; |
fff202e5 | 743 | size_t idlen = 0; |
add8d0e9 | 744 | SSL_SESSION *psksess = NULL; |
ffc5bbaa | 745 | SSL_SESSION *edsess = NULL; |
add8d0e9 MC |
746 | const EVP_MD *handmd = NULL; |
747 | ||
fc7129dc | 748 | if (s->hello_retry_request == SSL_HRR_PENDING) |
add8d0e9 MC |
749 | handmd = ssl_handshake_md(s); |
750 | ||
751 | if (s->psk_use_session_cb != NULL | |
ffc5bbaa MC |
752 | && (!s->psk_use_session_cb(s, handmd, &id, &idlen, &psksess) |
753 | || (psksess != NULL | |
754 | && psksess->ssl_version != TLS1_3_VERSION))) { | |
755 | SSL_SESSION_free(psksess); | |
c48ffbcc | 756 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
add8d0e9 MC |
757 | return EXT_RETURN_FAIL; |
758 | } | |
759 | ||
c2b290c3 | 760 | #ifndef OPENSSL_NO_PSK |
f3d40db1 MC |
761 | if (psksess == NULL && s->psk_client_callback != NULL) { |
762 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
763 | size_t psklen = 0; | |
764 | ||
765 | memset(identity, 0, sizeof(identity)); | |
766 | psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1, | |
767 | psk, sizeof(psk)); | |
768 | ||
769 | if (psklen > PSK_MAX_PSK_LEN) { | |
c48ffbcc | 770 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); |
f3d40db1 MC |
771 | return EXT_RETURN_FAIL; |
772 | } else if (psklen > 0) { | |
773 | const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; | |
774 | const SSL_CIPHER *cipher; | |
775 | ||
776 | idlen = strlen(identity); | |
777 | if (idlen > PSK_MAX_IDENTITY_LEN) { | |
c48ffbcc | 778 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3d40db1 MC |
779 | return EXT_RETURN_FAIL; |
780 | } | |
781 | id = (unsigned char *)identity; | |
782 | ||
783 | /* | |
784 | * We found a PSK using an old style callback. We don't know | |
785 | * the digest so we default to SHA256 as per the TLSv1.3 spec | |
786 | */ | |
787 | cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id); | |
788 | if (cipher == NULL) { | |
c48ffbcc | 789 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3d40db1 MC |
790 | return EXT_RETURN_FAIL; |
791 | } | |
792 | ||
793 | psksess = SSL_SESSION_new(); | |
794 | if (psksess == NULL | |
795 | || !SSL_SESSION_set1_master_key(psksess, psk, psklen) | |
796 | || !SSL_SESSION_set_cipher(psksess, cipher) | |
797 | || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) { | |
c48ffbcc | 798 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f3d40db1 MC |
799 | OPENSSL_cleanse(psk, psklen); |
800 | return EXT_RETURN_FAIL; | |
801 | } | |
802 | OPENSSL_cleanse(psk, psklen); | |
803 | } | |
804 | } | |
c2b290c3 | 805 | #endif /* OPENSSL_NO_PSK */ |
f3d40db1 | 806 | |
add8d0e9 MC |
807 | SSL_SESSION_free(s->psksession); |
808 | s->psksession = psksess; | |
809 | if (psksess != NULL) { | |
810 | OPENSSL_free(s->psksession_id); | |
811 | s->psksession_id = OPENSSL_memdup(id, idlen); | |
812 | if (s->psksession_id == NULL) { | |
39a14059 | 813 | s->psksession_id_len = 0; |
c48ffbcc | 814 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
add8d0e9 MC |
815 | return EXT_RETURN_FAIL; |
816 | } | |
817 | s->psksession_id_len = idlen; | |
818 | } | |
819 | ||
38df5a45 | 820 | if (s->early_data_state != SSL_EARLY_DATA_CONNECTING |
add8d0e9 MC |
821 | || (s->session->ext.max_early_data == 0 |
822 | && (psksess == NULL || psksess->ext.max_early_data == 0))) { | |
38df5a45 | 823 | s->max_early_data = 0; |
b186a592 | 824 | return EXT_RETURN_NOT_SENT; |
38df5a45 | 825 | } |
ffc5bbaa MC |
826 | edsess = s->session->ext.max_early_data != 0 ? s->session : psksess; |
827 | s->max_early_data = edsess->ext.max_early_data; | |
828 | ||
bfab12bb MC |
829 | if (edsess->ext.hostname != NULL) { |
830 | if (s->ext.hostname == NULL | |
831 | || (s->ext.hostname != NULL | |
832 | && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) { | |
f63a17d6 | 833 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
f63a17d6 | 834 | SSL_R_INCONSISTENT_EARLY_DATA_SNI); |
bfab12bb MC |
835 | return EXT_RETURN_FAIL; |
836 | } | |
ffc5bbaa MC |
837 | } |
838 | ||
839 | if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) { | |
c48ffbcc | 840 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
ffc5bbaa MC |
841 | return EXT_RETURN_FAIL; |
842 | } | |
843 | ||
844 | /* | |
845 | * Verify that we are offering an ALPN protocol consistent with the early | |
846 | * data. | |
847 | */ | |
848 | if (edsess->ext.alpn_selected != NULL) { | |
849 | PACKET prots, alpnpkt; | |
850 | int found = 0; | |
851 | ||
852 | if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) { | |
c48ffbcc | 853 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
ffc5bbaa MC |
854 | return EXT_RETURN_FAIL; |
855 | } | |
856 | while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) { | |
857 | if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected, | |
858 | edsess->ext.alpn_selected_len)) { | |
859 | found = 1; | |
860 | break; | |
861 | } | |
862 | } | |
863 | if (!found) { | |
f63a17d6 | 864 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
f63a17d6 | 865 | SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
ffc5bbaa MC |
866 | return EXT_RETURN_FAIL; |
867 | } | |
868 | } | |
38df5a45 MC |
869 | |
870 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) | |
871 | || !WPACKET_start_sub_packet_u16(pkt) | |
872 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 873 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 874 | return EXT_RETURN_FAIL; |
38df5a45 MC |
875 | } |
876 | ||
877 | /* | |
878 | * We set this to rejected here. Later, if the server acknowledges the | |
879 | * extension, we set it to accepted. | |
880 | */ | |
881 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; | |
4be3a7c7 | 882 | s->ext.early_data_ok = 1; |
38df5a45 | 883 | |
b186a592 | 884 | return EXT_RETURN_SENT; |
38df5a45 MC |
885 | } |
886 | ||
1266eefd MC |
887 | #define F5_WORKAROUND_MIN_MSG_LEN 0xff |
888 | #define F5_WORKAROUND_MAX_MSG_LEN 0x200 | |
889 | ||
d702ad12 MC |
890 | /* |
891 | * PSK pre binder overhead = | |
892 | * 2 bytes for TLSEXT_TYPE_psk | |
893 | * 2 bytes for extension length | |
894 | * 2 bytes for identities list length | |
895 | * 2 bytes for identity length | |
896 | * 4 bytes for obfuscated_ticket_age | |
897 | * 2 bytes for binder list length | |
898 | * 1 byte for binder length | |
899 | * The above excludes the number of bytes for the identity itself and the | |
900 | * subsequent binder bytes | |
901 | */ | |
902 | #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1) | |
903 | ||
b186a592 MC |
904 | EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt, |
905 | unsigned int context, X509 *x, | |
f63a17d6 | 906 | size_t chainidx) |
ab83e314 MC |
907 | { |
908 | unsigned char *padbytes; | |
909 | size_t hlen; | |
910 | ||
911 | if ((s->options & SSL_OP_TLSEXT_PADDING) == 0) | |
b186a592 | 912 | return EXT_RETURN_NOT_SENT; |
ab83e314 MC |
913 | |
914 | /* | |
d702ad12 MC |
915 | * Add padding to workaround bugs in F5 terminators. See RFC7685. |
916 | * This code calculates the length of all extensions added so far but | |
917 | * excludes the PSK extension (because that MUST be written last). Therefore | |
918 | * this extension MUST always appear second to last. | |
ab83e314 MC |
919 | */ |
920 | if (!WPACKET_get_total_written(pkt, &hlen)) { | |
c48ffbcc | 921 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b186a592 | 922 | return EXT_RETURN_FAIL; |
ab83e314 MC |
923 | } |
924 | ||
d702ad12 MC |
925 | /* |
926 | * If we're going to send a PSK then that will be written out after this | |
927 | * extension, so we need to calculate how long it is going to be. | |
928 | */ | |
929 | if (s->session->ssl_version == TLS1_3_VERSION | |
930 | && s->session->ext.ticklen != 0 | |
931 | && s->session->cipher != NULL) { | |
c8f6c28a | 932 | const EVP_MD *md = ssl_md(s->ctx, s->session->cipher->algorithm2); |
d702ad12 MC |
933 | |
934 | if (md != NULL) { | |
935 | /* | |
936 | * Add the fixed PSK overhead, the identity length and the binder | |
937 | * length. | |
938 | */ | |
939 | hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen | |
ed576acd | 940 | + EVP_MD_get_size(md); |
d702ad12 MC |
941 | } |
942 | } | |
943 | ||
1266eefd | 944 | if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) { |
1ee4b98e | 945 | /* Calculate the amount of padding we need to add */ |
1266eefd MC |
946 | hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen; |
947 | ||
948 | /* | |
949 | * Take off the size of extension header itself (2 bytes for type and | |
10ed1b72 TS |
950 | * 2 bytes for length bytes), but ensure that the extension is at least |
951 | * 1 byte long so as not to have an empty extension last (WebSphere 7.x, | |
952 | * 8.x are intolerant of that condition) | |
1266eefd | 953 | */ |
3d85c7f4 | 954 | if (hlen > 4) |
ab83e314 MC |
955 | hlen -= 4; |
956 | else | |
10ed1b72 | 957 | hlen = 1; |
ab83e314 MC |
958 | |
959 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding) | |
960 | || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { | |
c48ffbcc | 961 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
eb5fd03b | 962 | return EXT_RETURN_FAIL; |
ab83e314 MC |
963 | } |
964 | memset(padbytes, 0, hlen); | |
965 | } | |
966 | ||
b186a592 | 967 | return EXT_RETURN_SENT; |
ab83e314 MC |
968 | } |
969 | ||
ec15acb6 MC |
970 | /* |
971 | * Construct the pre_shared_key extension | |
972 | */ | |
b186a592 | 973 | EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, |
f63a17d6 | 974 | X509 *x, size_t chainidx) |
ec15acb6 MC |
975 | { |
976 | #ifndef OPENSSL_NO_TLS1_3 | |
15b1688a | 977 | uint32_t now, agesec, agems = 0; |
add8d0e9 | 978 | size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen; |
9368f865 | 979 | unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL; |
15b1688a | 980 | const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL; |
9368f865 | 981 | int dores = 0; |
ec15acb6 | 982 | |
c96ce52c | 983 | s->ext.tick_identity = 0; |
ec15acb6 | 984 | |
d702ad12 MC |
985 | /* |
986 | * Note: At this stage of the code we only support adding a single | |
987 | * resumption PSK. If we add support for multiple PSKs then the length | |
988 | * calculations in the padding extension will need to be adjusted. | |
989 | */ | |
990 | ||
ec15acb6 | 991 | /* |
08191294 MC |
992 | * If this is an incompatible or new session then we have nothing to resume |
993 | * so don't add this extension. | |
ec15acb6 | 994 | */ |
08191294 | 995 | if (s->session->ssl_version != TLS1_3_VERSION |
add8d0e9 | 996 | || (s->session->ext.ticklen == 0 && s->psksession == NULL)) |
b186a592 | 997 | return EXT_RETURN_NOT_SENT; |
ec15acb6 | 998 | |
fc7129dc | 999 | if (s->hello_retry_request == SSL_HRR_PENDING) |
9368f865 MC |
1000 | handmd = ssl_handshake_md(s); |
1001 | ||
9368f865 | 1002 | if (s->session->ext.ticklen != 0) { |
72257204 | 1003 | /* Get the digest associated with the ciphersuite in the session */ |
9368f865 | 1004 | if (s->session->cipher == NULL) { |
c48ffbcc | 1005 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
635c8f77 | 1006 | return EXT_RETURN_FAIL; |
9368f865 | 1007 | } |
c8f6c28a | 1008 | mdres = ssl_md(s->ctx, s->session->cipher->algorithm2); |
9368f865 | 1009 | if (mdres == NULL) { |
72257204 MC |
1010 | /* |
1011 | * Don't recognize this cipher so we can't use the session. | |
1012 | * Ignore it | |
1013 | */ | |
9368f865 MC |
1014 | goto dopsksess; |
1015 | } | |
1016 | ||
fc7129dc | 1017 | if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) { |
9368f865 | 1018 | /* |
72257204 MC |
1019 | * Selected ciphersuite hash does not match the hash for the session |
1020 | * so we can't use it. | |
9368f865 MC |
1021 | */ |
1022 | goto dopsksess; | |
1023 | } | |
1f5b44e9 | 1024 | |
cf3e221b | 1025 | /* |
9368f865 | 1026 | * Technically the C standard just says time() returns a time_t and says |
72257204 MC |
1027 | * nothing about the encoding of that type. In practice most |
1028 | * implementations follow POSIX which holds it as an integral type in | |
1029 | * seconds since epoch. We've already made the assumption that we can do | |
1030 | * this in multiple places in the code, so portability shouldn't be an | |
1031 | * issue. | |
cf3e221b | 1032 | */ |
9368f865 MC |
1033 | now = (uint32_t)time(NULL); |
1034 | agesec = now - (uint32_t)s->session->time; | |
7e70213f MC |
1035 | /* |
1036 | * We calculate the age in seconds but the server may work in ms. Due to | |
1037 | * rounding errors we could overestimate the age by up to 1s. It is | |
1038 | * better to underestimate it. Otherwise, if the RTT is very short, when | |
1039 | * the server calculates the age reported by the client it could be | |
1040 | * bigger than the age calculated on the server - which should never | |
1041 | * happen. | |
1042 | */ | |
1043 | if (agesec > 0) | |
1044 | agesec--; | |
cf3e221b | 1045 | |
9368f865 MC |
1046 | if (s->session->ext.tick_lifetime_hint < agesec) { |
1047 | /* Ticket is too old. Ignore it. */ | |
1048 | goto dopsksess; | |
1049 | } | |
ec15acb6 | 1050 | |
9368f865 MC |
1051 | /* |
1052 | * Calculate age in ms. We're just doing it to nearest second. Should be | |
1053 | * good enough. | |
1054 | */ | |
1055 | agems = agesec * (uint32_t)1000; | |
fc24f0bf | 1056 | |
9368f865 MC |
1057 | if (agesec != 0 && agems / (uint32_t)1000 != agesec) { |
1058 | /* | |
72257204 MC |
1059 | * Overflow. Shouldn't happen unless this is a *really* old session. |
1060 | * If so we just ignore it. | |
9368f865 MC |
1061 | */ |
1062 | goto dopsksess; | |
1063 | } | |
ec15acb6 | 1064 | |
ec15acb6 | 1065 | /* |
72257204 MC |
1066 | * Obfuscate the age. Overflow here is fine, this addition is supposed |
1067 | * to be mod 2^32. | |
ec15acb6 | 1068 | */ |
9368f865 MC |
1069 | agems += s->session->ext.tick_age_add; |
1070 | ||
ed576acd | 1071 | reshashsize = EVP_MD_get_size(mdres); |
c96ce52c | 1072 | s->ext.tick_identity++; |
9368f865 | 1073 | dores = 1; |
ec15acb6 MC |
1074 | } |
1075 | ||
9368f865 | 1076 | dopsksess: |
add8d0e9 | 1077 | if (!dores && s->psksession == NULL) |
9368f865 | 1078 | return EXT_RETURN_NOT_SENT; |
ec15acb6 | 1079 | |
add8d0e9 | 1080 | if (s->psksession != NULL) { |
c8f6c28a | 1081 | mdpsk = ssl_md(s->ctx, s->psksession->cipher->algorithm2); |
9368f865 MC |
1082 | if (mdpsk == NULL) { |
1083 | /* | |
1084 | * Don't recognize this cipher so we can't use the session. | |
1085 | * If this happens it's an application bug. | |
1086 | */ | |
c48ffbcc | 1087 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
635c8f77 | 1088 | return EXT_RETURN_FAIL; |
9368f865 MC |
1089 | } |
1090 | ||
fc7129dc | 1091 | if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) { |
9368f865 MC |
1092 | /* |
1093 | * Selected ciphersuite hash does not match the hash for the PSK | |
1094 | * session. This is an application bug. | |
1095 | */ | |
c48ffbcc | 1096 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
635c8f77 | 1097 | return EXT_RETURN_FAIL; |
9368f865 MC |
1098 | } |
1099 | ||
ed576acd | 1100 | pskhashsize = EVP_MD_get_size(mdpsk); |
9368f865 | 1101 | } |
ec15acb6 MC |
1102 | |
1103 | /* Create the extension, but skip over the binder for now */ | |
1104 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) | |
1105 | || !WPACKET_start_sub_packet_u16(pkt) | |
9368f865 | 1106 | || !WPACKET_start_sub_packet_u16(pkt)) { |
c48ffbcc | 1107 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
635c8f77 | 1108 | return EXT_RETURN_FAIL; |
9368f865 MC |
1109 | } |
1110 | ||
1111 | if (dores) { | |
1112 | if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, | |
1113 | s->session->ext.ticklen) | |
1114 | || !WPACKET_put_bytes_u32(pkt, agems)) { | |
c48ffbcc | 1115 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
635c8f77 | 1116 | return EXT_RETURN_FAIL; |
9368f865 MC |
1117 | } |
1118 | } | |
1119 | ||
add8d0e9 MC |
1120 | if (s->psksession != NULL) { |
1121 | if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id, | |
1122 | s->psksession_id_len) | |
9368f865 | 1123 | || !WPACKET_put_bytes_u32(pkt, 0)) { |
c48ffbcc | 1124 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
635c8f77 | 1125 | return EXT_RETURN_FAIL; |
9368f865 | 1126 | } |
c96ce52c | 1127 | s->ext.tick_identity++; |
9368f865 MC |
1128 | } |
1129 | ||
1130 | if (!WPACKET_close(pkt) | |
ec15acb6 MC |
1131 | || !WPACKET_get_total_written(pkt, &binderoffset) |
1132 | || !WPACKET_start_sub_packet_u16(pkt) | |
9368f865 MC |
1133 | || (dores |
1134 | && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder)) | |
add8d0e9 | 1135 | || (s->psksession != NULL |
9368f865 | 1136 | && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder)) |
ec15acb6 MC |
1137 | || !WPACKET_close(pkt) |
1138 | || !WPACKET_close(pkt) | |
1139 | || !WPACKET_get_total_written(pkt, &msglen) | |
1140 | /* | |
1141 | * We need to fill in all the sub-packet lengths now so we can | |
1142 | * calculate the HMAC of the message up to the binders | |
1143 | */ | |
1144 | || !WPACKET_fill_lengths(pkt)) { | |
c48ffbcc | 1145 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
635c8f77 | 1146 | return EXT_RETURN_FAIL; |
ec15acb6 MC |
1147 | } |
1148 | ||
1149 | msgstart = WPACKET_get_curr(pkt) - msglen; | |
1150 | ||
72257204 MC |
1151 | if (dores |
1152 | && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL, | |
1153 | resbinder, s->session, 1, 0) != 1) { | |
635c8f77 MC |
1154 | /* SSLfatal() already called */ |
1155 | return EXT_RETURN_FAIL; | |
ec15acb6 MC |
1156 | } |
1157 | ||
add8d0e9 | 1158 | if (s->psksession != NULL |
72257204 | 1159 | && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL, |
add8d0e9 | 1160 | pskbinder, s->psksession, 1, 1) != 1) { |
635c8f77 MC |
1161 | /* SSLfatal() already called */ |
1162 | return EXT_RETURN_FAIL; | |
9368f865 MC |
1163 | } |
1164 | ||
635c8f77 | 1165 | return EXT_RETURN_SENT; |
ec15acb6 | 1166 | #else |
89bc9cf6 | 1167 | return EXT_RETURN_NOT_SENT; |
ec15acb6 MC |
1168 | #endif |
1169 | } | |
1170 | ||
9d75dce3 | 1171 | EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt, |
a7e6a3d8 P |
1172 | ossl_unused unsigned int context, |
1173 | ossl_unused X509 *x, | |
1174 | ossl_unused size_t chainidx) | |
9d75dce3 TS |
1175 | { |
1176 | #ifndef OPENSSL_NO_TLS1_3 | |
32097b33 MC |
1177 | if (!s->pha_enabled) |
1178 | return EXT_RETURN_NOT_SENT; | |
9d75dce3 TS |
1179 | |
1180 | /* construct extension - 0 length, no contents */ | |
1181 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth) | |
1182 | || !WPACKET_start_sub_packet_u16(pkt) | |
1183 | || !WPACKET_close(pkt)) { | |
c48ffbcc | 1184 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
9d75dce3 TS |
1185 | return EXT_RETURN_FAIL; |
1186 | } | |
1187 | ||
1188 | s->post_handshake_auth = SSL_PHA_EXT_SENT; | |
1189 | ||
1190 | return EXT_RETURN_SENT; | |
1191 | #else | |
1192 | return EXT_RETURN_NOT_SENT; | |
1193 | #endif | |
1194 | } | |
1195 | ||
1196 | ||
6dd083fd MC |
1197 | /* |
1198 | * Parse the server's renegotiation binding and abort if it's not right | |
1199 | */ | |
61138358 | 1200 | int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1201 | X509 *x, size_t chainidx) |
6dd083fd | 1202 | { |
555cbb32 TS |
1203 | size_t expected_len = s->s3.previous_client_finished_len |
1204 | + s->s3.previous_server_finished_len; | |
6dd083fd MC |
1205 | size_t ilen; |
1206 | const unsigned char *data; | |
1207 | ||
1208 | /* Check for logic errors */ | |
b77f3ed1 | 1209 | if (!ossl_assert(expected_len == 0 |
555cbb32 | 1210 | || s->s3.previous_client_finished_len != 0) |
b77f3ed1 | 1211 | || !ossl_assert(expected_len == 0 |
555cbb32 | 1212 | || s->s3.previous_server_finished_len != 0)) { |
c48ffbcc | 1213 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
b77f3ed1 MC |
1214 | return 0; |
1215 | } | |
6dd083fd MC |
1216 | |
1217 | /* Parse the length byte */ | |
1218 | if (!PACKET_get_1_len(pkt, &ilen)) { | |
c48ffbcc | 1219 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
6dd083fd MC |
1220 | return 0; |
1221 | } | |
1222 | ||
1223 | /* Consistency check */ | |
1224 | if (PACKET_remaining(pkt) != ilen) { | |
c48ffbcc | 1225 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
6dd083fd MC |
1226 | return 0; |
1227 | } | |
1228 | ||
1229 | /* Check that the extension matches */ | |
1230 | if (ilen != expected_len) { | |
c48ffbcc | 1231 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
6dd083fd MC |
1232 | return 0; |
1233 | } | |
1234 | ||
555cbb32 TS |
1235 | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len) |
1236 | || memcmp(data, s->s3.previous_client_finished, | |
1237 | s->s3.previous_client_finished_len) != 0) { | |
c48ffbcc | 1238 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
6dd083fd MC |
1239 | return 0; |
1240 | } | |
1241 | ||
555cbb32 TS |
1242 | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len) |
1243 | || memcmp(data, s->s3.previous_server_finished, | |
1244 | s->s3.previous_server_finished_len) != 0) { | |
c48ffbcc | 1245 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
6dd083fd MC |
1246 | return 0; |
1247 | } | |
555cbb32 | 1248 | s->s3.send_connection_binding = 1; |
6dd083fd MC |
1249 | |
1250 | return 1; | |
1251 | } | |
1252 | ||
cf72c757 F |
1253 | /* Parse the server's max fragment len extension packet */ |
1254 | int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context, | |
f63a17d6 | 1255 | X509 *x, size_t chainidx) |
cf72c757 F |
1256 | { |
1257 | unsigned int value; | |
1258 | ||
1259 | if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { | |
c48ffbcc | 1260 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
cf72c757 F |
1261 | return 0; |
1262 | } | |
1263 | ||
1264 | /* |value| should contains a valid max-fragment-length code. */ | |
1265 | if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { | |
f63a17d6 | 1266 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1267 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
cf72c757 F |
1268 | return 0; |
1269 | } | |
1270 | ||
1271 | /* Must be the same value as client-configured one who was sent to server */ | |
1272 | /*- | |
1273 | * RFC 6066: if a client receives a maximum fragment length negotiation | |
1274 | * response that differs from the length it requested, ... | |
1275 | * It must abort with SSL_AD_ILLEGAL_PARAMETER alert | |
1276 | */ | |
1277 | if (value != s->ext.max_fragment_len_mode) { | |
f63a17d6 | 1278 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1279 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
cf72c757 F |
1280 | return 0; |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * Maximum Fragment Length Negotiation succeeded. | |
1285 | * The negotiated Maximum Fragment Length is binding now. | |
1286 | */ | |
1287 | s->session->ext.max_fragment_len_mode = value; | |
1288 | ||
1289 | return 1; | |
1290 | } | |
1291 | ||
61138358 | 1292 | int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1293 | X509 *x, size_t chainidx) |
6dd083fd | 1294 | { |
fb34a0f4 | 1295 | if (s->ext.hostname == NULL) { |
c48ffbcc | 1296 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
fb34a0f4 MC |
1297 | return 0; |
1298 | } | |
1299 | ||
1300 | if (PACKET_remaining(pkt) > 0) { | |
c48ffbcc | 1301 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1302 | return 0; |
1303 | } | |
1304 | ||
1305 | if (!s->hit) { | |
aff8c126 | 1306 | if (s->session->ext.hostname != NULL) { |
c48ffbcc | 1307 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1308 | return 0; |
1309 | } | |
aff8c126 RS |
1310 | s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname); |
1311 | if (s->session->ext.hostname == NULL) { | |
c48ffbcc | 1312 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1313 | return 0; |
1314 | } | |
1315 | } | |
1316 | ||
1317 | return 1; | |
1318 | } | |
1319 | ||
61138358 | 1320 | int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1321 | X509 *x, size_t chainidx) |
6dd083fd | 1322 | { |
848a950b | 1323 | size_t ecpointformats_len; |
6dd083fd MC |
1324 | PACKET ecptformatlist; |
1325 | ||
1326 | if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) { | |
c48ffbcc | 1327 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1328 | return 0; |
1329 | } | |
1330 | if (!s->hit) { | |
aff8c126 | 1331 | ecpointformats_len = PACKET_remaining(&ecptformatlist); |
848a950b | 1332 | if (ecpointformats_len == 0) { |
c48ffbcc | 1333 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
848a950b MC |
1334 | return 0; |
1335 | } | |
6dd083fd | 1336 | |
cd0fb43c MC |
1337 | s->ext.peer_ecpointformats_len = 0; |
1338 | OPENSSL_free(s->ext.peer_ecpointformats); | |
1339 | s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len); | |
1340 | if (s->ext.peer_ecpointformats == NULL) { | |
39a14059 | 1341 | s->ext.peer_ecpointformats_len = 0; |
c48ffbcc | 1342 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1343 | return 0; |
1344 | } | |
1345 | ||
cd0fb43c | 1346 | s->ext.peer_ecpointformats_len = ecpointformats_len; |
6dd083fd MC |
1347 | |
1348 | if (!PACKET_copy_bytes(&ecptformatlist, | |
cd0fb43c | 1349 | s->ext.peer_ecpointformats, |
aff8c126 | 1350 | ecpointformats_len)) { |
c48ffbcc | 1351 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1352 | return 0; |
1353 | } | |
1354 | } | |
1355 | ||
1356 | return 1; | |
1357 | } | |
6dd083fd | 1358 | |
61138358 | 1359 | int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1360 | X509 *x, size_t chainidx) |
6dd083fd | 1361 | { |
aff8c126 RS |
1362 | if (s->ext.session_ticket_cb != NULL && |
1363 | !s->ext.session_ticket_cb(s, PACKET_data(pkt), | |
1364 | PACKET_remaining(pkt), | |
1365 | s->ext.session_ticket_cb_arg)) { | |
c48ffbcc | 1366 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1367 | return 0; |
1368 | } | |
1266eefd | 1369 | |
fb34a0f4 | 1370 | if (!tls_use_ticket(s)) { |
c48ffbcc | 1371 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1372 | return 0; |
1373 | } | |
fb34a0f4 | 1374 | if (PACKET_remaining(pkt) > 0) { |
c48ffbcc | 1375 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
fb34a0f4 MC |
1376 | return 0; |
1377 | } | |
1266eefd | 1378 | |
aff8c126 | 1379 | s->ext.ticket_expected = 1; |
6dd083fd MC |
1380 | |
1381 | return 1; | |
1382 | } | |
1383 | ||
ab83e314 | 1384 | #ifndef OPENSSL_NO_OCSP |
61138358 | 1385 | int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1386 | X509 *x, size_t chainidx) |
6dd083fd | 1387 | { |
5de683d2 MC |
1388 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1389 | /* We ignore this if the server sends a CertificateRequest */ | |
1390 | /* TODO(TLS1.3): Add support for this */ | |
1391 | return 1; | |
1392 | } | |
1393 | ||
6dd083fd | 1394 | /* |
f63e4288 MC |
1395 | * MUST only be sent if we've requested a status |
1396 | * request message. In TLS <= 1.2 it must also be empty. | |
6dd083fd | 1397 | */ |
fb34a0f4 | 1398 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { |
c48ffbcc | 1399 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1400 | return 0; |
1401 | } | |
fb34a0f4 | 1402 | if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) { |
c48ffbcc | 1403 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
fb34a0f4 MC |
1404 | return 0; |
1405 | } | |
f63e4288 MC |
1406 | |
1407 | if (SSL_IS_TLS13(s)) { | |
1408 | /* We only know how to handle this if it's for the first Certificate in | |
1ee4b98e | 1409 | * the chain. We ignore any other responses. |
f63e4288 | 1410 | */ |
8521ced6 | 1411 | if (chainidx != 0) |
f63e4288 | 1412 | return 1; |
f63a17d6 MC |
1413 | |
1414 | /* SSLfatal() already called */ | |
1415 | return tls_process_cert_status_body(s, pkt); | |
f63e4288 MC |
1416 | } |
1417 | ||
6dd083fd | 1418 | /* Set flag to expect CertificateStatus message */ |
aff8c126 | 1419 | s->ext.status_expected = 1; |
6dd083fd MC |
1420 | |
1421 | return 1; | |
1422 | } | |
ab83e314 | 1423 | #endif |
6dd083fd MC |
1424 | |
1425 | ||
1426 | #ifndef OPENSSL_NO_CT | |
61138358 | 1427 | int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1428 | size_t chainidx) |
6dd083fd | 1429 | { |
5de683d2 MC |
1430 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1431 | /* We ignore this if the server sends it in a CertificateRequest */ | |
1432 | /* TODO(TLS1.3): Add support for this */ | |
1433 | return 1; | |
1434 | } | |
1435 | ||
6dd083fd MC |
1436 | /* |
1437 | * Only take it if we asked for it - i.e if there is no CT validation | |
1438 | * callback set, then a custom extension MAY be processing it, so we | |
1439 | * need to let control continue to flow to that. | |
1440 | */ | |
1441 | if (s->ct_validation_callback != NULL) { | |
1442 | size_t size = PACKET_remaining(pkt); | |
1443 | ||
1444 | /* Simply copy it off for later processing */ | |
aff8c126 RS |
1445 | OPENSSL_free(s->ext.scts); |
1446 | s->ext.scts = NULL; | |
1266eefd | 1447 | |
3a63c0ed | 1448 | s->ext.scts_len = (uint16_t)size; |
6dd083fd | 1449 | if (size > 0) { |
aff8c126 | 1450 | s->ext.scts = OPENSSL_malloc(size); |
39a14059 MC |
1451 | if (s->ext.scts == NULL) { |
1452 | s->ext.scts_len = 0; | |
1453 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); | |
1454 | return 0; | |
1455 | } | |
1456 | if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) { | |
c48ffbcc | 1457 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1458 | return 0; |
1459 | } | |
1460 | } | |
1461 | } else { | |
b186a592 MC |
1462 | ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0 |
1463 | ? ENDPOINT_CLIENT : ENDPOINT_BOTH; | |
1464 | ||
1465 | /* | |
1466 | * If we didn't ask for it then there must be a custom extension, | |
1467 | * otherwise this is unsolicited. | |
1468 | */ | |
1469 | if (custom_ext_find(&s->cert->custext, role, | |
1470 | TLSEXT_TYPE_signed_certificate_timestamp, | |
1471 | NULL) == NULL) { | |
c48ffbcc | 1472 | SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
b186a592 MC |
1473 | return 0; |
1474 | } | |
1475 | ||
f63a17d6 | 1476 | if (!custom_ext_parse(s, context, |
43ae5eed MC |
1477 | TLSEXT_TYPE_signed_certificate_timestamp, |
1478 | PACKET_data(pkt), PACKET_remaining(pkt), | |
f63a17d6 MC |
1479 | x, chainidx)) { |
1480 | /* SSLfatal already called */ | |
6dd083fd | 1481 | return 0; |
f63a17d6 | 1482 | } |
6dd083fd MC |
1483 | } |
1484 | ||
1485 | return 1; | |
1486 | } | |
1487 | #endif | |
1488 | ||
1489 | ||
1490 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
1491 | /* | |
1492 | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No | |
1493 | * elements of zero length are allowed and the set of elements must exactly | |
1494 | * fill the length of the block. Returns 1 on success or 0 on failure. | |
1495 | */ | |
f63a17d6 | 1496 | static int ssl_next_proto_validate(SSL *s, PACKET *pkt) |
6dd083fd MC |
1497 | { |
1498 | PACKET tmp_protocol; | |
1499 | ||
1500 | while (PACKET_remaining(pkt)) { | |
1501 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) | |
f63a17d6 | 1502 | || PACKET_remaining(&tmp_protocol) == 0) { |
c48ffbcc | 1503 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
6dd083fd | 1504 | return 0; |
f63a17d6 | 1505 | } |
6dd083fd MC |
1506 | } |
1507 | ||
1508 | return 1; | |
1509 | } | |
1510 | ||
61138358 | 1511 | int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1512 | size_t chainidx) |
6dd083fd MC |
1513 | { |
1514 | unsigned char *selected; | |
1515 | unsigned char selected_len; | |
1516 | PACKET tmppkt; | |
1517 | ||
1266eefd | 1518 | /* Check if we are in a renegotiation. If so ignore this extension */ |
c7f47786 | 1519 | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
6dd083fd MC |
1520 | return 1; |
1521 | ||
1522 | /* We must have requested it. */ | |
aff8c126 | 1523 | if (s->ctx->ext.npn_select_cb == NULL) { |
c48ffbcc | 1524 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1525 | return 0; |
1526 | } | |
1266eefd | 1527 | |
6dd083fd MC |
1528 | /* The data must be valid */ |
1529 | tmppkt = *pkt; | |
f63a17d6 MC |
1530 | if (!ssl_next_proto_validate(s, &tmppkt)) { |
1531 | /* SSLfatal() already called */ | |
6dd083fd MC |
1532 | return 0; |
1533 | } | |
aff8c126 RS |
1534 | if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len, |
1535 | PACKET_data(pkt), | |
1536 | PACKET_remaining(pkt), | |
1537 | s->ctx->ext.npn_select_cb_arg) != | |
6dd083fd | 1538 | SSL_TLSEXT_ERR_OK) { |
c48ffbcc | 1539 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1540 | return 0; |
1541 | } | |
1266eefd | 1542 | |
6dd083fd MC |
1543 | /* |
1544 | * Could be non-NULL if server has sent multiple NPN extensions in | |
1545 | * a single Serverhello | |
1546 | */ | |
aff8c126 RS |
1547 | OPENSSL_free(s->ext.npn); |
1548 | s->ext.npn = OPENSSL_malloc(selected_len); | |
1549 | if (s->ext.npn == NULL) { | |
39a14059 | 1550 | s->ext.npn_len = 0; |
c48ffbcc | 1551 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1552 | return 0; |
1553 | } | |
1554 | ||
aff8c126 RS |
1555 | memcpy(s->ext.npn, selected, selected_len); |
1556 | s->ext.npn_len = selected_len; | |
555cbb32 | 1557 | s->s3.npn_seen = 1; |
6dd083fd MC |
1558 | |
1559 | return 1; | |
1560 | } | |
1561 | #endif | |
1562 | ||
61138358 | 1563 | int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1564 | size_t chainidx) |
6dd083fd MC |
1565 | { |
1566 | size_t len; | |
1567 | ||
1568 | /* We must have requested it. */ | |
555cbb32 | 1569 | if (!s->s3.alpn_sent) { |
c48ffbcc | 1570 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1571 | return 0; |
1572 | } | |
1573 | /*- | |
1574 | * The extension data consists of: | |
1575 | * uint16 list_length | |
1576 | * uint8 proto_length; | |
1577 | * uint8 proto[proto_length]; | |
1578 | */ | |
1579 | if (!PACKET_get_net_2_len(pkt, &len) | |
1580 | || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len) | |
1581 | || PACKET_remaining(pkt) != len) { | |
c48ffbcc | 1582 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1583 | return 0; |
1584 | } | |
555cbb32 TS |
1585 | OPENSSL_free(s->s3.alpn_selected); |
1586 | s->s3.alpn_selected = OPENSSL_malloc(len); | |
1587 | if (s->s3.alpn_selected == NULL) { | |
39a14059 | 1588 | s->s3.alpn_selected_len = 0; |
c48ffbcc | 1589 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1590 | return 0; |
1591 | } | |
555cbb32 | 1592 | if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) { |
c48ffbcc | 1593 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
6dd083fd MC |
1594 | return 0; |
1595 | } | |
555cbb32 | 1596 | s->s3.alpn_selected_len = len; |
6dd083fd | 1597 | |
0ef28021 MC |
1598 | if (s->session->ext.alpn_selected == NULL |
1599 | || s->session->ext.alpn_selected_len != len | |
555cbb32 | 1600 | || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len) |
0ef28021 | 1601 | != 0) { |
4be3a7c7 MC |
1602 | /* ALPN not consistent with the old session so cannot use early_data */ |
1603 | s->ext.early_data_ok = 0; | |
1604 | } | |
1605 | if (!s->hit) { | |
9d5db9c9 MC |
1606 | /* |
1607 | * This is a new session and so alpn_selected should have been | |
1608 | * initialised to NULL. We should update it with the selected ALPN. | |
1609 | */ | |
1610 | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { | |
c48ffbcc | 1611 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
9d5db9c9 MC |
1612 | return 0; |
1613 | } | |
4be3a7c7 | 1614 | s->session->ext.alpn_selected = |
555cbb32 | 1615 | OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); |
4be3a7c7 | 1616 | if (s->session->ext.alpn_selected == NULL) { |
39a14059 | 1617 | s->session->ext.alpn_selected_len = 0; |
c48ffbcc | 1618 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4be3a7c7 MC |
1619 | return 0; |
1620 | } | |
555cbb32 | 1621 | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; |
ae8d7d99 MC |
1622 | } |
1623 | ||
6dd083fd MC |
1624 | return 1; |
1625 | } | |
1626 | ||
1627 | #ifndef OPENSSL_NO_SRTP | |
61138358 | 1628 | int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1629 | size_t chainidx) |
6dd083fd MC |
1630 | { |
1631 | unsigned int id, ct, mki; | |
1632 | int i; | |
1633 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt; | |
1634 | SRTP_PROTECTION_PROFILE *prof; | |
1635 | ||
1266eefd MC |
1636 | if (!PACKET_get_net_2(pkt, &ct) || ct != 2 |
1637 | || !PACKET_get_net_2(pkt, &id) | |
1638 | || !PACKET_get_1(pkt, &mki) | |
1639 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1640 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
f63a17d6 | 1641 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
6dd083fd MC |
1642 | return 0; |
1643 | } | |
1644 | ||
1645 | if (mki != 0) { | |
1646 | /* Must be no MKI, since we never offer one */ | |
c48ffbcc | 1647 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE); |
6dd083fd MC |
1648 | return 0; |
1649 | } | |
1650 | ||
6dd083fd | 1651 | /* Throw an error if the server gave us an unsolicited extension */ |
1266eefd | 1652 | clnt = SSL_get_srtp_profiles(s); |
6dd083fd | 1653 | if (clnt == NULL) { |
c48ffbcc | 1654 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES); |
6dd083fd MC |
1655 | return 0; |
1656 | } | |
1657 | ||
1658 | /* | |
1659 | * Check to see if the server gave us something we support (and | |
1660 | * presumably offered) | |
1661 | */ | |
1662 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) { | |
1663 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); | |
1664 | ||
1665 | if (prof->id == id) { | |
1666 | s->srtp_profile = prof; | |
6dd083fd MC |
1667 | return 1; |
1668 | } | |
1669 | } | |
1670 | ||
c48ffbcc | 1671 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
f63a17d6 | 1672 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
6dd083fd MC |
1673 | return 0; |
1674 | } | |
1675 | #endif | |
1676 | ||
61138358 | 1677 | int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1678 | size_t chainidx) |
6dd083fd MC |
1679 | { |
1680 | /* Ignore if inappropriate ciphersuite */ | |
1681 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) | |
555cbb32 TS |
1682 | && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD |
1683 | && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4) | |
28a31a0a | 1684 | s->ext.use_etm = 1; |
6dd083fd MC |
1685 | |
1686 | return 1; | |
1687 | } | |
1688 | ||
61138358 | 1689 | int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1690 | size_t chainidx) |
6dd083fd | 1691 | { |
088dfa13 TS |
1692 | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
1693 | return 1; | |
555cbb32 | 1694 | s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
6dd083fd MC |
1695 | if (!s->hit) |
1696 | s->session->flags |= SSL_SESS_FLAG_EXTMS; | |
1697 | ||
1698 | return 1; | |
1699 | } | |
1700 | ||
88050dd1 MC |
1701 | int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context, |
1702 | X509 *x, size_t chainidx) | |
1703 | { | |
1704 | unsigned int version; | |
1705 | ||
1706 | if (!PACKET_get_net_2(pkt, &version) | |
1707 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1708 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
88050dd1 MC |
1709 | return 0; |
1710 | } | |
1711 | ||
27e462f1 MC |
1712 | /* |
1713 | * The only protocol version we support which is valid in this extension in | |
1714 | * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else. | |
1715 | */ | |
1716 | if (version != TLS1_3_VERSION) { | |
1717 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, | |
27e462f1 MC |
1718 | SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
1719 | return 0; | |
1720 | } | |
1721 | ||
426dfc9f | 1722 | /* We ignore this extension for HRRs except to sanity check it */ |
27e462f1 | 1723 | if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) |
426dfc9f | 1724 | return 1; |
426dfc9f | 1725 | |
88050dd1 MC |
1726 | /* We just set it here. We validate it in ssl_choose_client_version */ |
1727 | s->version = version; | |
1728 | ||
1729 | return 1; | |
1730 | } | |
1731 | ||
61138358 | 1732 | int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1733 | size_t chainidx) |
6dd083fd | 1734 | { |
3cf96e88 | 1735 | #ifndef OPENSSL_NO_TLS1_3 |
6dd083fd MC |
1736 | unsigned int group_id; |
1737 | PACKET encoded_pt; | |
555cbb32 | 1738 | EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL; |
a011b586 | 1739 | const TLS_GROUP_INFO *ginf = NULL; |
6dd083fd MC |
1740 | |
1741 | /* Sanity check */ | |
555cbb32 | 1742 | if (ckey == NULL || s->s3.peer_tmp != NULL) { |
c48ffbcc | 1743 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
6dd083fd MC |
1744 | return 0; |
1745 | } | |
1746 | ||
1747 | if (!PACKET_get_net_2(pkt, &group_id)) { | |
c48ffbcc | 1748 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
6dd083fd MC |
1749 | return 0; |
1750 | } | |
1751 | ||
fe874d27 | 1752 | if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) { |
f48d826e DSH |
1753 | const uint16_t *pgroups = NULL; |
1754 | size_t i, num_groups; | |
3847d426 MC |
1755 | |
1756 | if (PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1757 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
3847d426 MC |
1758 | return 0; |
1759 | } | |
1760 | ||
1761 | /* | |
1762 | * It is an error if the HelloRetryRequest wants a key_share that we | |
1763 | * already sent in the first ClientHello | |
1764 | */ | |
555cbb32 | 1765 | if (group_id == s->s3.group_id) { |
c48ffbcc | 1766 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
3847d426 MC |
1767 | return 0; |
1768 | } | |
1769 | ||
1770 | /* Validate the selected group is one we support */ | |
f48d826e DSH |
1771 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
1772 | for (i = 0; i < num_groups; i++) { | |
1773 | if (group_id == pgroups[i]) | |
3847d426 MC |
1774 | break; |
1775 | } | |
f48d826e | 1776 | if (i >= num_groups |
dbc6268f | 1777 | || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) { |
c48ffbcc | 1778 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
3847d426 MC |
1779 | return 0; |
1780 | } | |
1781 | ||
555cbb32 TS |
1782 | s->s3.group_id = group_id; |
1783 | EVP_PKEY_free(s->s3.tmp.pkey); | |
1784 | s->s3.tmp.pkey = NULL; | |
3847d426 MC |
1785 | return 1; |
1786 | } | |
1787 | ||
555cbb32 | 1788 | if (group_id != s->s3.group_id) { |
6dd083fd MC |
1789 | /* |
1790 | * This isn't for the group that we sent in the original | |
1791 | * key_share! | |
1792 | */ | |
c48ffbcc | 1793 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
6dd083fd MC |
1794 | return 0; |
1795 | } | |
aa6bd216 BK |
1796 | /* Retain this group in the SSL_SESSION */ |
1797 | if (!s->hit) { | |
1798 | s->session->kex_group = group_id; | |
1799 | } else if (group_id != s->session->kex_group) { | |
1800 | /* | |
1801 | * If this is a resumption but changed what group was used, we need | |
1802 | * to record the new group in the session, but the session is not | |
1803 | * a new session and could be in use by other threads. So, make | |
1804 | * a copy of the session to record the new information so that it's | |
1805 | * useful for any sessions resumed from tickets issued on this | |
1806 | * connection. | |
1807 | */ | |
1808 | SSL_SESSION *new_sess; | |
1809 | ||
1810 | if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) { | |
1811 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); | |
1812 | return 0; | |
1813 | } | |
1814 | SSL_SESSION_free(s->session); | |
1815 | s->session = new_sess; | |
1816 | s->session->kex_group = group_id; | |
1817 | } | |
6dd083fd | 1818 | |
a011b586 | 1819 | if ((ginf = tls1_group_id_lookup(s->ctx, group_id)) == NULL) { |
c48ffbcc | 1820 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
a011b586 NT |
1821 | return 0; |
1822 | } | |
1823 | ||
6dd083fd MC |
1824 | if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt) |
1825 | || PACKET_remaining(&encoded_pt) == 0) { | |
c48ffbcc | 1826 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
6dd083fd MC |
1827 | return 0; |
1828 | } | |
1829 | ||
a011b586 NT |
1830 | if (!ginf->is_kem) { |
1831 | /* Regular KEX */ | |
1832 | skey = EVP_PKEY_new(); | |
1833 | if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) { | |
c48ffbcc | 1834 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
a011b586 NT |
1835 | return 0; |
1836 | } | |
afce590b | 1837 | |
5ac8fb58 MC |
1838 | if (EVP_PKEY_set1_encoded_public_key(skey, PACKET_data(&encoded_pt), |
1839 | PACKET_remaining(&encoded_pt)) <= 0) { | |
c48ffbcc | 1840 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT); |
a011b586 NT |
1841 | EVP_PKEY_free(skey); |
1842 | return 0; | |
1843 | } | |
6dd083fd | 1844 | |
a011b586 NT |
1845 | if (ssl_derive(s, ckey, skey, 1) == 0) { |
1846 | /* SSLfatal() already called */ | |
1847 | EVP_PKEY_free(skey); | |
1848 | return 0; | |
1849 | } | |
1850 | s->s3.peer_tmp = skey; | |
1851 | } else { | |
1852 | /* KEM Mode */ | |
1853 | const unsigned char *ct = PACKET_data(&encoded_pt); | |
1854 | size_t ctlen = PACKET_remaining(&encoded_pt); | |
1855 | ||
1856 | if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) { | |
1857 | /* SSLfatal() already called */ | |
1858 | return 0; | |
1859 | } | |
6dd083fd | 1860 | } |
aa6bd216 | 1861 | s->s3.did_kex = 1; |
3cf96e88 | 1862 | #endif |
6dd083fd MC |
1863 | |
1864 | return 1; | |
1865 | } | |
4ff65f77 | 1866 | |
cfef5027 | 1867 | int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1868 | size_t chainidx) |
cfef5027 MC |
1869 | { |
1870 | PACKET cookie; | |
1871 | ||
1872 | if (!PACKET_as_length_prefixed_2(pkt, &cookie) | |
1873 | || !PACKET_memdup(&cookie, &s->ext.tls13_cookie, | |
1874 | &s->ext.tls13_cookie_len)) { | |
c48ffbcc | 1875 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
cfef5027 MC |
1876 | return 0; |
1877 | } | |
1878 | ||
1879 | return 1; | |
1880 | } | |
1881 | ||
38df5a45 | 1882 | int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context, |
f63a17d6 | 1883 | X509 *x, size_t chainidx) |
38df5a45 | 1884 | { |
fe874d27 | 1885 | if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { |
6594189f MC |
1886 | unsigned long max_early_data; |
1887 | ||
1888 | if (!PACKET_get_net_4(pkt, &max_early_data) | |
1889 | || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1890 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA); |
6594189f MC |
1891 | return 0; |
1892 | } | |
1893 | ||
1894 | s->session->ext.max_early_data = max_early_data; | |
1895 | ||
1896 | return 1; | |
1897 | } | |
1898 | ||
38df5a45 | 1899 | if (PACKET_remaining(pkt) != 0) { |
c48ffbcc | 1900 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
38df5a45 MC |
1901 | return 0; |
1902 | } | |
1903 | ||
4be3a7c7 | 1904 | if (!s->ext.early_data_ok |
c96ce52c | 1905 | || !s->hit) { |
38df5a45 MC |
1906 | /* |
1907 | * If we get here then we didn't send early data, or we didn't resume | |
4be3a7c7 MC |
1908 | * using the first identity, or the SNI/ALPN is not consistent so the |
1909 | * server should not be accepting it. | |
38df5a45 | 1910 | */ |
c48ffbcc | 1911 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
38df5a45 MC |
1912 | return 0; |
1913 | } | |
1914 | ||
1915 | s->ext.early_data = SSL_EARLY_DATA_ACCEPTED; | |
1916 | ||
1917 | return 1; | |
1918 | } | |
1919 | ||
61138358 | 1920 | int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x, |
f63a17d6 | 1921 | size_t chainidx) |
4ff65f77 MC |
1922 | { |
1923 | #ifndef OPENSSL_NO_TLS1_3 | |
1924 | unsigned int identity; | |
1925 | ||
1926 | if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) { | |
c48ffbcc | 1927 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
4ff65f77 MC |
1928 | return 0; |
1929 | } | |
1930 | ||
c96ce52c | 1931 | if (identity >= (unsigned int)s->ext.tick_identity) { |
c48ffbcc | 1932 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY); |
c96ce52c MC |
1933 | return 0; |
1934 | } | |
1935 | ||
1936 | /* | |
1937 | * Session resumption tickets are always sent before PSK tickets. If the | |
1938 | * ticket index is 0 then it must be for a session resumption ticket if we | |
1939 | * sent two tickets, or if we didn't send a PSK ticket. | |
1940 | */ | |
1941 | if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) { | |
9368f865 MC |
1942 | s->hit = 1; |
1943 | SSL_SESSION_free(s->psksession); | |
1944 | s->psksession = NULL; | |
1945 | return 1; | |
1946 | } | |
1947 | ||
c96ce52c MC |
1948 | if (s->psksession == NULL) { |
1949 | /* Should never happen */ | |
c48ffbcc | 1950 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
4ff65f77 MC |
1951 | return 0; |
1952 | } | |
1953 | ||
add8d0e9 MC |
1954 | /* |
1955 | * If we used the external PSK for sending early_data then s->early_secret | |
1956 | * is already set up, so don't overwrite it. Otherwise we copy the | |
1957 | * early_secret across that we generated earlier. | |
1958 | */ | |
1959 | if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY | |
1960 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) | |
1961 | || s->session->ext.max_early_data > 0 | |
1962 | || s->psksession->ext.max_early_data == 0) | |
1963 | memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE); | |
1964 | ||
9368f865 MC |
1965 | SSL_SESSION_free(s->session); |
1966 | s->session = s->psksession; | |
1967 | s->psksession = NULL; | |
4ff65f77 | 1968 | s->hit = 1; |
c96ce52c MC |
1969 | /* Early data is only allowed if we used the first ticket */ |
1970 | if (identity != 0) | |
1971 | s->ext.early_data_ok = 0; | |
4ff65f77 MC |
1972 | #endif |
1973 | ||
1974 | return 1; | |
1975 | } |