/src/openssl/ssl/statem/statem_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the OpenSSL license (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <limits.h> |
12 | | #include <string.h> |
13 | | #include <stdio.h> |
14 | | #include "../ssl_locl.h" |
15 | | #include "statem_locl.h" |
16 | | #include "internal/cryptlib.h" |
17 | | #include <openssl/buffer.h> |
18 | | #include <openssl/objects.h> |
19 | | #include <openssl/evp.h> |
20 | | #include <openssl/x509.h> |
21 | | |
22 | | /* |
23 | | * Map error codes to TLS/SSL alart types. |
24 | | */ |
25 | | typedef struct x509err2alert_st { |
26 | | int x509err; |
27 | | int alert; |
28 | | } X509ERR2ALERT; |
29 | | |
30 | | /* Fixed value used in the ServerHello random field to identify an HRR */ |
31 | | const unsigned char hrrrandom[] = { |
32 | | 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, |
33 | | 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, |
34 | | 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c |
35 | | }; |
36 | | |
37 | | /* |
38 | | * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or |
39 | | * SSL3_RT_CHANGE_CIPHER_SPEC) |
40 | | */ |
41 | | int ssl3_do_write(SSL *s, int type) |
42 | 0 | { |
43 | 0 | int ret; |
44 | 0 | size_t written = 0; |
45 | 0 |
|
46 | 0 | ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off], |
47 | 0 | s->init_num, &written); |
48 | 0 | if (ret < 0) |
49 | 0 | return -1; |
50 | 0 | if (type == SSL3_RT_HANDSHAKE) |
51 | 0 | /* |
52 | 0 | * should not be done for 'Hello Request's, but in that case we'll |
53 | 0 | * ignore the result anyway |
54 | 0 | * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added |
55 | 0 | */ |
56 | 0 | if (!SSL_IS_TLS13(s) || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET |
57 | 0 | && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE |
58 | 0 | && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE)) |
59 | 0 | if (!ssl3_finish_mac(s, |
60 | 0 | (unsigned char *)&s->init_buf->data[s->init_off], |
61 | 0 | written)) |
62 | 0 | return -1; |
63 | 0 | if (written == s->init_num) { |
64 | 0 | if (s->msg_callback) |
65 | 0 | s->msg_callback(1, s->version, type, s->init_buf->data, |
66 | 0 | (size_t)(s->init_off + s->init_num), s, |
67 | 0 | s->msg_callback_arg); |
68 | 0 | return 1; |
69 | 0 | } |
70 | 0 | s->init_off += written; |
71 | 0 | s->init_num -= written; |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype) |
76 | 0 | { |
77 | 0 | size_t msglen; |
78 | 0 |
|
79 | 0 | if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt)) |
80 | 0 | || !WPACKET_get_length(pkt, &msglen) |
81 | 0 | || msglen > INT_MAX) |
82 | 0 | return 0; |
83 | 0 | s->init_num = (int)msglen; |
84 | 0 | s->init_off = 0; |
85 | 0 |
|
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | int tls_setup_handshake(SSL *s) |
90 | 0 | { |
91 | 0 | if (!ssl3_init_finished_mac(s)) { |
92 | 0 | /* SSLfatal() already called */ |
93 | 0 | return 0; |
94 | 0 | } |
95 | 0 | |
96 | 0 | /* Reset any extension flags */ |
97 | 0 | memset(s->ext.extflags, 0, sizeof(s->ext.extflags)); |
98 | 0 |
|
99 | 0 | if (s->server) { |
100 | 0 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s); |
101 | 0 | int i, ver_min, ver_max, ok = 0; |
102 | 0 |
|
103 | 0 | /* |
104 | 0 | * Sanity check that the maximum version we accept has ciphers |
105 | 0 | * enabled. For clients we do this check during construction of the |
106 | 0 | * ClientHello. |
107 | 0 | */ |
108 | 0 | if (ssl_get_min_max_version(s, &ver_min, &ver_max, NULL) != 0) { |
109 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_SETUP_HANDSHAKE, |
110 | 0 | ERR_R_INTERNAL_ERROR); |
111 | 0 | return 0; |
112 | 0 | } |
113 | 0 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
114 | 0 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i); |
115 | 0 |
|
116 | 0 | if (SSL_IS_DTLS(s)) { |
117 | 0 | if (DTLS_VERSION_GE(ver_max, c->min_dtls) && |
118 | 0 | DTLS_VERSION_LE(ver_max, c->max_dtls)) |
119 | 0 | ok = 1; |
120 | 0 | } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) { |
121 | 0 | ok = 1; |
122 | 0 | } |
123 | 0 | if (ok) |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | if (!ok) { |
127 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_SETUP_HANDSHAKE, |
128 | 0 | SSL_R_NO_CIPHERS_AVAILABLE); |
129 | 0 | ERR_add_error_data(1, "No ciphers enabled for max supported " |
130 | 0 | "SSL/TLS version"); |
131 | 0 | return 0; |
132 | 0 | } |
133 | 0 | if (SSL_IS_FIRST_HANDSHAKE(s)) { |
134 | 0 | /* N.B. s->session_ctx == s->ctx here */ |
135 | 0 | tsan_counter(&s->session_ctx->stats.sess_accept); |
136 | 0 | } else { |
137 | 0 | /* N.B. s->ctx may not equal s->session_ctx */ |
138 | 0 | tsan_counter(&s->ctx->stats.sess_accept_renegotiate); |
139 | 0 |
|
140 | 0 | s->s3->tmp.cert_request = 0; |
141 | 0 | } |
142 | 0 | } else { |
143 | 0 | if (SSL_IS_FIRST_HANDSHAKE(s)) |
144 | 0 | tsan_counter(&s->session_ctx->stats.sess_connect); |
145 | 0 | else |
146 | 0 | tsan_counter(&s->session_ctx->stats.sess_connect_renegotiate); |
147 | 0 |
|
148 | 0 | /* mark client_random uninitialized */ |
149 | 0 | memset(s->s3->client_random, 0, sizeof(s->s3->client_random)); |
150 | 0 | s->hit = 0; |
151 | 0 |
|
152 | 0 | s->s3->tmp.cert_req = 0; |
153 | 0 |
|
154 | 0 | if (SSL_IS_DTLS(s)) |
155 | 0 | s->statem.use_timer = 1; |
156 | 0 | } |
157 | 0 |
|
158 | 0 | return 1; |
159 | 0 | } |
160 | | |
161 | | /* |
162 | | * Size of the to-be-signed TLS13 data, without the hash size itself: |
163 | | * 64 bytes of value 32, 33 context bytes, 1 byte separator |
164 | | */ |
165 | 0 | #define TLS13_TBS_START_SIZE 64 |
166 | 0 | #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1) |
167 | | |
168 | | static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs, |
169 | | void **hdata, size_t *hdatalen) |
170 | 0 | { |
171 | 0 | static const char *servercontext = "TLS 1.3, server CertificateVerify"; |
172 | 0 | static const char *clientcontext = "TLS 1.3, client CertificateVerify"; |
173 | 0 |
|
174 | 0 | if (SSL_IS_TLS13(s)) { |
175 | 0 | size_t hashlen; |
176 | 0 |
|
177 | 0 | /* Set the first 64 bytes of to-be-signed data to octet 32 */ |
178 | 0 | memset(tls13tbs, 32, TLS13_TBS_START_SIZE); |
179 | 0 | /* This copies the 33 bytes of context plus the 0 separator byte */ |
180 | 0 | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY |
181 | 0 | || s->statem.hand_state == TLS_ST_SW_CERT_VRFY) |
182 | 0 | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext); |
183 | 0 | else |
184 | 0 | strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext); |
185 | 0 |
|
186 | 0 | /* |
187 | 0 | * If we're currently reading then we need to use the saved handshake |
188 | 0 | * hash value. We can't use the current handshake hash state because |
189 | 0 | * that includes the CertVerify itself. |
190 | 0 | */ |
191 | 0 | if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY |
192 | 0 | || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) { |
193 | 0 | memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash, |
194 | 0 | s->cert_verify_hash_len); |
195 | 0 | hashlen = s->cert_verify_hash_len; |
196 | 0 | } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE, |
197 | 0 | EVP_MAX_MD_SIZE, &hashlen)) { |
198 | 0 | /* SSLfatal() already called */ |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | |
202 | 0 | *hdata = tls13tbs; |
203 | 0 | *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen; |
204 | 0 | } else { |
205 | 0 | size_t retlen; |
206 | 0 |
|
207 | 0 | retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata); |
208 | 0 | if (retlen <= 0) { |
209 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_GET_CERT_VERIFY_TBS_DATA, |
210 | 0 | ERR_R_INTERNAL_ERROR); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | *hdatalen = retlen; |
214 | 0 | } |
215 | 0 |
|
216 | 0 | return 1; |
217 | 0 | } |
218 | | |
219 | | int tls_construct_cert_verify(SSL *s, WPACKET *pkt) |
220 | 0 | { |
221 | 0 | EVP_PKEY *pkey = NULL; |
222 | 0 | const EVP_MD *md = NULL; |
223 | 0 | EVP_MD_CTX *mctx = NULL; |
224 | 0 | EVP_PKEY_CTX *pctx = NULL; |
225 | 0 | size_t hdatalen = 0, siglen = 0; |
226 | 0 | void *hdata; |
227 | 0 | unsigned char *sig = NULL; |
228 | 0 | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
229 | 0 | const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg; |
230 | 0 |
|
231 | 0 | if (lu == NULL || s->s3->tmp.cert == NULL) { |
232 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
233 | 0 | ERR_R_INTERNAL_ERROR); |
234 | 0 | goto err; |
235 | 0 | } |
236 | 0 | pkey = s->s3->tmp.cert->privatekey; |
237 | 0 |
|
238 | 0 | if (pkey == NULL || !tls1_lookup_md(lu, &md)) { |
239 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
240 | 0 | ERR_R_INTERNAL_ERROR); |
241 | 0 | goto err; |
242 | 0 | } |
243 | 0 |
|
244 | 0 | mctx = EVP_MD_CTX_new(); |
245 | 0 | if (mctx == NULL) { |
246 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
247 | 0 | ERR_R_MALLOC_FAILURE); |
248 | 0 | goto err; |
249 | 0 | } |
250 | 0 |
|
251 | 0 | /* Get the data to be signed */ |
252 | 0 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
253 | 0 | /* SSLfatal() already called */ |
254 | 0 | goto err; |
255 | 0 | } |
256 | 0 | |
257 | 0 | if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { |
258 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
259 | 0 | ERR_R_INTERNAL_ERROR); |
260 | 0 | goto err; |
261 | 0 | } |
262 | 0 | siglen = EVP_PKEY_size(pkey); |
263 | 0 | sig = OPENSSL_malloc(siglen); |
264 | 0 | if (sig == NULL) { |
265 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
266 | 0 | ERR_R_MALLOC_FAILURE); |
267 | 0 | goto err; |
268 | 0 | } |
269 | 0 |
|
270 | 0 | if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) { |
271 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
272 | 0 | ERR_R_EVP_LIB); |
273 | 0 | goto err; |
274 | 0 | } |
275 | 0 |
|
276 | 0 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
277 | 0 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
278 | 0 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
279 | 0 | RSA_PSS_SALTLEN_DIGEST) <= 0) { |
280 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
281 | 0 | ERR_R_EVP_LIB); |
282 | 0 | goto err; |
283 | 0 | } |
284 | 0 | } |
285 | 0 | if (s->version == SSL3_VERSION) { |
286 | 0 | if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0 |
287 | 0 | || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
288 | 0 | (int)s->session->master_key_length, |
289 | 0 | s->session->master_key) |
290 | 0 | || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) { |
291 | 0 |
|
292 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
293 | 0 | ERR_R_EVP_LIB); |
294 | 0 | goto err; |
295 | 0 | } |
296 | 0 | } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) { |
297 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
298 | 0 | ERR_R_EVP_LIB); |
299 | 0 | goto err; |
300 | 0 | } |
301 | 0 |
|
302 | 0 | #ifndef OPENSSL_NO_GOST |
303 | 0 | { |
304 | 0 | int pktype = lu->sig; |
305 | 0 |
|
306 | 0 | if (pktype == NID_id_GostR3410_2001 |
307 | 0 | || pktype == NID_id_GostR3410_2012_256 |
308 | 0 | || pktype == NID_id_GostR3410_2012_512) |
309 | 0 | BUF_reverse(sig, NULL, siglen); |
310 | 0 | } |
311 | 0 | #endif |
312 | 0 |
|
313 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) { |
314 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY, |
315 | 0 | ERR_R_INTERNAL_ERROR); |
316 | 0 | goto err; |
317 | 0 | } |
318 | 0 |
|
319 | 0 | /* Digest cached records and discard handshake buffer */ |
320 | 0 | if (!ssl3_digest_cached_records(s, 0)) { |
321 | 0 | /* SSLfatal() already called */ |
322 | 0 | goto err; |
323 | 0 | } |
324 | 0 | |
325 | 0 | OPENSSL_free(sig); |
326 | 0 | EVP_MD_CTX_free(mctx); |
327 | 0 | return 1; |
328 | 0 | err: |
329 | 0 | OPENSSL_free(sig); |
330 | 0 | EVP_MD_CTX_free(mctx); |
331 | 0 | return 0; |
332 | 0 | } |
333 | | |
334 | | MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt) |
335 | 0 | { |
336 | 0 | EVP_PKEY *pkey = NULL; |
337 | 0 | const unsigned char *data; |
338 | 0 | #ifndef OPENSSL_NO_GOST |
339 | 0 | unsigned char *gost_data = NULL; |
340 | 0 | #endif |
341 | 0 | MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
342 | 0 | int j; |
343 | 0 | unsigned int len; |
344 | 0 | X509 *peer; |
345 | 0 | const EVP_MD *md = NULL; |
346 | 0 | size_t hdatalen = 0; |
347 | 0 | void *hdata; |
348 | 0 | unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE]; |
349 | 0 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
350 | 0 | EVP_PKEY_CTX *pctx = NULL; |
351 | 0 |
|
352 | 0 | if (mctx == NULL) { |
353 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
354 | 0 | ERR_R_MALLOC_FAILURE); |
355 | 0 | goto err; |
356 | 0 | } |
357 | 0 |
|
358 | 0 | peer = s->session->peer; |
359 | 0 | pkey = X509_get0_pubkey(peer); |
360 | 0 | if (pkey == NULL) { |
361 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
362 | 0 | ERR_R_INTERNAL_ERROR); |
363 | 0 | goto err; |
364 | 0 | } |
365 | 0 |
|
366 | 0 | if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) { |
367 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY, |
368 | 0 | SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); |
369 | 0 | goto err; |
370 | 0 | } |
371 | 0 |
|
372 | 0 | if (SSL_USE_SIGALGS(s)) { |
373 | 0 | unsigned int sigalg; |
374 | 0 |
|
375 | 0 | if (!PACKET_get_net_2(pkt, &sigalg)) { |
376 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
377 | 0 | SSL_R_BAD_PACKET); |
378 | 0 | goto err; |
379 | 0 | } |
380 | 0 | if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) { |
381 | 0 | /* SSLfatal() already called */ |
382 | 0 | goto err; |
383 | 0 | } |
384 | 0 | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
385 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
386 | 0 | ERR_R_INTERNAL_ERROR); |
387 | 0 | goto err; |
388 | 0 | } |
389 | 0 |
|
390 | 0 | if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) { |
391 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
392 | 0 | ERR_R_INTERNAL_ERROR); |
393 | 0 | goto err; |
394 | 0 | } |
395 | 0 |
|
396 | | #ifdef SSL_DEBUG |
397 | | if (SSL_USE_SIGALGS(s)) |
398 | | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
399 | | #endif |
400 | | |
401 | 0 | /* Check for broken implementations of GOST ciphersuites */ |
402 | 0 | /* |
403 | 0 | * If key is GOST and len is exactly 64 or 128, it is signature without |
404 | 0 | * length field (CryptoPro implementations at least till TLS 1.2) |
405 | 0 | */ |
406 | 0 | #ifndef OPENSSL_NO_GOST |
407 | 0 | if (!SSL_USE_SIGALGS(s) |
408 | 0 | && ((PACKET_remaining(pkt) == 64 |
409 | 0 | && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001 |
410 | 0 | || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256)) |
411 | 0 | || (PACKET_remaining(pkt) == 128 |
412 | 0 | && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) { |
413 | 0 | len = PACKET_remaining(pkt); |
414 | 0 | } else |
415 | 0 | #endif |
416 | 0 | if (!PACKET_get_net_2(pkt, &len)) { |
417 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
418 | 0 | SSL_R_LENGTH_MISMATCH); |
419 | 0 | goto err; |
420 | 0 | } |
421 | 0 |
|
422 | 0 | j = EVP_PKEY_size(pkey); |
423 | 0 | if (((int)len > j) || ((int)PACKET_remaining(pkt) > j) |
424 | 0 | || (PACKET_remaining(pkt) == 0)) { |
425 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
426 | 0 | SSL_R_WRONG_SIGNATURE_SIZE); |
427 | 0 | goto err; |
428 | 0 | } |
429 | 0 | if (!PACKET_get_bytes(pkt, &data, len)) { |
430 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
431 | 0 | SSL_R_LENGTH_MISMATCH); |
432 | 0 | goto err; |
433 | 0 | } |
434 | 0 |
|
435 | 0 | if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) { |
436 | 0 | /* SSLfatal() already called */ |
437 | 0 | goto err; |
438 | 0 | } |
439 | 0 | |
440 | | #ifdef SSL_DEBUG |
441 | | fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md)); |
442 | | #endif |
443 | 0 | if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) { |
444 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
445 | 0 | ERR_R_EVP_LIB); |
446 | 0 | goto err; |
447 | 0 | } |
448 | 0 | #ifndef OPENSSL_NO_GOST |
449 | 0 | { |
450 | 0 | int pktype = EVP_PKEY_id(pkey); |
451 | 0 | if (pktype == NID_id_GostR3410_2001 |
452 | 0 | || pktype == NID_id_GostR3410_2012_256 |
453 | 0 | || pktype == NID_id_GostR3410_2012_512) { |
454 | 0 | if ((gost_data = OPENSSL_malloc(len)) == NULL) { |
455 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
456 | 0 | SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); |
457 | 0 | goto err; |
458 | 0 | } |
459 | 0 | BUF_reverse(gost_data, data, len); |
460 | 0 | data = gost_data; |
461 | 0 | } |
462 | 0 | } |
463 | 0 | #endif |
464 | 0 |
|
465 | 0 | if (SSL_USE_PSS(s)) { |
466 | 0 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
467 | 0 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
468 | 0 | RSA_PSS_SALTLEN_DIGEST) <= 0) { |
469 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
470 | 0 | ERR_R_EVP_LIB); |
471 | 0 | goto err; |
472 | 0 | } |
473 | 0 | } |
474 | 0 | if (s->version == SSL3_VERSION) { |
475 | 0 | if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0 |
476 | 0 | || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
477 | 0 | (int)s->session->master_key_length, |
478 | 0 | s->session->master_key)) { |
479 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
480 | 0 | ERR_R_EVP_LIB); |
481 | 0 | goto err; |
482 | 0 | } |
483 | 0 | if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) { |
484 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
485 | 0 | SSL_R_BAD_SIGNATURE); |
486 | 0 | goto err; |
487 | 0 | } |
488 | 0 | } else { |
489 | 0 | j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen); |
490 | 0 | if (j <= 0) { |
491 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY, |
492 | 0 | SSL_R_BAD_SIGNATURE); |
493 | 0 | goto err; |
494 | 0 | } |
495 | 0 | } |
496 | 0 |
|
497 | 0 | ret = MSG_PROCESS_CONTINUE_READING; |
498 | 0 | err: |
499 | 0 | BIO_free(s->s3->handshake_buffer); |
500 | 0 | s->s3->handshake_buffer = NULL; |
501 | 0 | EVP_MD_CTX_free(mctx); |
502 | 0 | #ifndef OPENSSL_NO_GOST |
503 | 0 | OPENSSL_free(gost_data); |
504 | 0 | #endif |
505 | 0 | return ret; |
506 | 0 | } |
507 | | |
508 | | int tls_construct_finished(SSL *s, WPACKET *pkt) |
509 | 0 | { |
510 | 0 | size_t finish_md_len; |
511 | 0 | const char *sender; |
512 | 0 | size_t slen; |
513 | 0 |
|
514 | 0 | /* This is a real handshake so make sure we clean it up at the end */ |
515 | 0 | if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED) |
516 | 0 | s->statem.cleanuphand = 1; |
517 | 0 |
|
518 | 0 | /* |
519 | 0 | * We only change the keys if we didn't already do this when we sent the |
520 | 0 | * client certificate |
521 | 0 | */ |
522 | 0 | if (SSL_IS_TLS13(s) |
523 | 0 | && !s->server |
524 | 0 | && s->s3->tmp.cert_req == 0 |
525 | 0 | && (!s->method->ssl3_enc->change_cipher_state(s, |
526 | 0 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {; |
527 | 0 | /* SSLfatal() already called */ |
528 | 0 | return 0; |
529 | 0 | } |
530 | 0 | |
531 | 0 | if (s->server) { |
532 | 0 | sender = s->method->ssl3_enc->server_finished_label; |
533 | 0 | slen = s->method->ssl3_enc->server_finished_label_len; |
534 | 0 | } else { |
535 | 0 | sender = s->method->ssl3_enc->client_finished_label; |
536 | 0 | slen = s->method->ssl3_enc->client_finished_label_len; |
537 | 0 | } |
538 | 0 |
|
539 | 0 | finish_md_len = s->method->ssl3_enc->final_finish_mac(s, |
540 | 0 | sender, slen, |
541 | 0 | s->s3->tmp.finish_md); |
542 | 0 | if (finish_md_len == 0) { |
543 | 0 | /* SSLfatal() already called */ |
544 | 0 | return 0; |
545 | 0 | } |
546 | 0 | |
547 | 0 | s->s3->tmp.finish_md_len = finish_md_len; |
548 | 0 |
|
549 | 0 | if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) { |
550 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED, |
551 | 0 | ERR_R_INTERNAL_ERROR); |
552 | 0 | return 0; |
553 | 0 | } |
554 | 0 |
|
555 | 0 | /* |
556 | 0 | * Log the master secret, if logging is enabled. We don't log it for |
557 | 0 | * TLSv1.3: there's a different key schedule for that. |
558 | 0 | */ |
559 | 0 | if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL, |
560 | 0 | s->session->master_key, |
561 | 0 | s->session->master_key_length)) { |
562 | 0 | /* SSLfatal() already called */ |
563 | 0 | return 0; |
564 | 0 | } |
565 | 0 | |
566 | 0 | /* |
567 | 0 | * Copy the finished so we can use it for renegotiation checks |
568 | 0 | */ |
569 | 0 | if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) { |
570 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED, |
571 | 0 | ERR_R_INTERNAL_ERROR); |
572 | 0 | return 0; |
573 | 0 | } |
574 | 0 | if (!s->server) { |
575 | 0 | memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, |
576 | 0 | finish_md_len); |
577 | 0 | s->s3->previous_client_finished_len = finish_md_len; |
578 | 0 | } else { |
579 | 0 | memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, |
580 | 0 | finish_md_len); |
581 | 0 | s->s3->previous_server_finished_len = finish_md_len; |
582 | 0 | } |
583 | 0 |
|
584 | 0 | return 1; |
585 | 0 | } |
586 | | |
587 | | int tls_construct_key_update(SSL *s, WPACKET *pkt) |
588 | 0 | { |
589 | 0 | if (!WPACKET_put_bytes_u8(pkt, s->key_update)) { |
590 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_KEY_UPDATE, |
591 | 0 | ERR_R_INTERNAL_ERROR); |
592 | 0 | return 0; |
593 | 0 | } |
594 | 0 |
|
595 | 0 | s->key_update = SSL_KEY_UPDATE_NONE; |
596 | 0 | return 1; |
597 | 0 | } |
598 | | |
599 | | MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt) |
600 | 0 | { |
601 | 0 | unsigned int updatetype; |
602 | 0 |
|
603 | 0 | s->key_update_count++; |
604 | 0 | if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) { |
605 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE, |
606 | 0 | SSL_R_TOO_MANY_KEY_UPDATES); |
607 | 0 | return MSG_PROCESS_ERROR; |
608 | 0 | } |
609 | 0 |
|
610 | 0 | /* |
611 | 0 | * A KeyUpdate message signals a key change so the end of the message must |
612 | 0 | * be on a record boundary. |
613 | 0 | */ |
614 | 0 | if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
615 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_UPDATE, |
616 | 0 | SSL_R_NOT_ON_RECORD_BOUNDARY); |
617 | 0 | return MSG_PROCESS_ERROR; |
618 | 0 | } |
619 | 0 |
|
620 | 0 | if (!PACKET_get_1(pkt, &updatetype) |
621 | 0 | || PACKET_remaining(pkt) != 0) { |
622 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_UPDATE, |
623 | 0 | SSL_R_BAD_KEY_UPDATE); |
624 | 0 | return MSG_PROCESS_ERROR; |
625 | 0 | } |
626 | 0 |
|
627 | 0 | /* |
628 | 0 | * There are only two defined key update types. Fail if we get a value we |
629 | 0 | * didn't recognise. |
630 | 0 | */ |
631 | 0 | if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED |
632 | 0 | && updatetype != SSL_KEY_UPDATE_REQUESTED) { |
633 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE, |
634 | 0 | SSL_R_BAD_KEY_UPDATE); |
635 | 0 | return MSG_PROCESS_ERROR; |
636 | 0 | } |
637 | 0 |
|
638 | 0 | /* |
639 | 0 | * If we get a request for us to update our sending keys too then, we need |
640 | 0 | * to additionally send a KeyUpdate message. However that message should |
641 | 0 | * not also request an update (otherwise we get into an infinite loop). |
642 | 0 | */ |
643 | 0 | if (updatetype == SSL_KEY_UPDATE_REQUESTED) |
644 | 0 | s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED; |
645 | 0 |
|
646 | 0 | if (!tls13_update_key(s, 0)) { |
647 | 0 | /* SSLfatal() already called */ |
648 | 0 | return MSG_PROCESS_ERROR; |
649 | 0 | } |
650 | 0 | |
651 | 0 | return MSG_PROCESS_FINISHED_READING; |
652 | 0 | } |
653 | | |
654 | | /* |
655 | | * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen |
656 | | * to far. |
657 | | */ |
658 | | int ssl3_take_mac(SSL *s) |
659 | 0 | { |
660 | 0 | const char *sender; |
661 | 0 | size_t slen; |
662 | 0 |
|
663 | 0 | if (!s->server) { |
664 | 0 | sender = s->method->ssl3_enc->server_finished_label; |
665 | 0 | slen = s->method->ssl3_enc->server_finished_label_len; |
666 | 0 | } else { |
667 | 0 | sender = s->method->ssl3_enc->client_finished_label; |
668 | 0 | slen = s->method->ssl3_enc->client_finished_label_len; |
669 | 0 | } |
670 | 0 |
|
671 | 0 | s->s3->tmp.peer_finish_md_len = |
672 | 0 | s->method->ssl3_enc->final_finish_mac(s, sender, slen, |
673 | 0 | s->s3->tmp.peer_finish_md); |
674 | 0 |
|
675 | 0 | if (s->s3->tmp.peer_finish_md_len == 0) { |
676 | 0 | /* SSLfatal() already called */ |
677 | 0 | return 0; |
678 | 0 | } |
679 | 0 | |
680 | 0 | return 1; |
681 | 0 | } |
682 | | |
683 | | MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt) |
684 | 0 | { |
685 | 0 | size_t remain; |
686 | 0 |
|
687 | 0 | remain = PACKET_remaining(pkt); |
688 | 0 | /* |
689 | 0 | * 'Change Cipher Spec' is just a single byte, which should already have |
690 | 0 | * been consumed by ssl_get_message() so there should be no bytes left, |
691 | 0 | * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes |
692 | 0 | */ |
693 | 0 | if (SSL_IS_DTLS(s)) { |
694 | 0 | if ((s->version == DTLS1_BAD_VER |
695 | 0 | && remain != DTLS1_CCS_HEADER_LENGTH + 1) |
696 | 0 | || (s->version != DTLS1_BAD_VER |
697 | 0 | && remain != DTLS1_CCS_HEADER_LENGTH - 1)) { |
698 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
699 | 0 | SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, |
700 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
701 | 0 | return MSG_PROCESS_ERROR; |
702 | 0 | } |
703 | 0 | } else { |
704 | 0 | if (remain != 0) { |
705 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
706 | 0 | SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, |
707 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
708 | 0 | return MSG_PROCESS_ERROR; |
709 | 0 | } |
710 | 0 | } |
711 | 0 |
|
712 | 0 | /* Check we have a cipher to change to */ |
713 | 0 | if (s->s3->tmp.new_cipher == NULL) { |
714 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
715 | 0 | SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY); |
716 | 0 | return MSG_PROCESS_ERROR; |
717 | 0 | } |
718 | 0 |
|
719 | 0 | s->s3->change_cipher_spec = 1; |
720 | 0 | if (!ssl3_do_change_cipher_spec(s)) { |
721 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, |
722 | 0 | ERR_R_INTERNAL_ERROR); |
723 | 0 | return MSG_PROCESS_ERROR; |
724 | 0 | } |
725 | 0 |
|
726 | 0 | if (SSL_IS_DTLS(s)) { |
727 | 0 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); |
728 | 0 |
|
729 | 0 | if (s->version == DTLS1_BAD_VER) |
730 | 0 | s->d1->handshake_read_seq++; |
731 | 0 |
|
732 | | #ifndef OPENSSL_NO_SCTP |
733 | | /* |
734 | | * Remember that a CCS has been received, so that an old key of |
735 | | * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no |
736 | | * SCTP is used |
737 | | */ |
738 | | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL); |
739 | | #endif |
740 | | } |
741 | 0 |
|
742 | 0 | return MSG_PROCESS_CONTINUE_READING; |
743 | 0 | } |
744 | | |
745 | | MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt) |
746 | 0 | { |
747 | 0 | size_t md_len; |
748 | 0 |
|
749 | 0 |
|
750 | 0 | /* This is a real handshake so make sure we clean it up at the end */ |
751 | 0 | if (s->server) { |
752 | 0 | /* |
753 | 0 | * To get this far we must have read encrypted data from the client. We |
754 | 0 | * no longer tolerate unencrypted alerts. This value is ignored if less |
755 | 0 | * than TLSv1.3 |
756 | 0 | */ |
757 | 0 | s->statem.enc_read_state = ENC_READ_STATE_VALID; |
758 | 0 | if (s->post_handshake_auth != SSL_PHA_REQUESTED) |
759 | 0 | s->statem.cleanuphand = 1; |
760 | 0 | if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) { |
761 | 0 | /* SSLfatal() already called */ |
762 | 0 | return MSG_PROCESS_ERROR; |
763 | 0 | } |
764 | 0 | } |
765 | 0 | |
766 | 0 | /* |
767 | 0 | * In TLSv1.3 a Finished message signals a key change so the end of the |
768 | 0 | * message must be on a record boundary. |
769 | 0 | */ |
770 | 0 | if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
771 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED, |
772 | 0 | SSL_R_NOT_ON_RECORD_BOUNDARY); |
773 | 0 | return MSG_PROCESS_ERROR; |
774 | 0 | } |
775 | 0 |
|
776 | 0 | /* If this occurs, we have missed a message */ |
777 | 0 | if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) { |
778 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED, |
779 | 0 | SSL_R_GOT_A_FIN_BEFORE_A_CCS); |
780 | 0 | return MSG_PROCESS_ERROR; |
781 | 0 | } |
782 | 0 | s->s3->change_cipher_spec = 0; |
783 | 0 |
|
784 | 0 | md_len = s->s3->tmp.peer_finish_md_len; |
785 | 0 |
|
786 | 0 | if (md_len != PACKET_remaining(pkt)) { |
787 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_FINISHED, |
788 | 0 | SSL_R_BAD_DIGEST_LENGTH); |
789 | 0 | return MSG_PROCESS_ERROR; |
790 | 0 | } |
791 | 0 |
|
792 | 0 | if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, |
793 | 0 | md_len) != 0) { |
794 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_FINISHED, |
795 | 0 | SSL_R_DIGEST_CHECK_FAILED); |
796 | 0 | return MSG_PROCESS_ERROR; |
797 | 0 | } |
798 | 0 |
|
799 | 0 | /* |
800 | 0 | * Copy the finished so we can use it for renegotiation checks |
801 | 0 | */ |
802 | 0 | if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) { |
803 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_FINISHED, |
804 | 0 | ERR_R_INTERNAL_ERROR); |
805 | 0 | return MSG_PROCESS_ERROR; |
806 | 0 | } |
807 | 0 | if (s->server) { |
808 | 0 | memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, |
809 | 0 | md_len); |
810 | 0 | s->s3->previous_client_finished_len = md_len; |
811 | 0 | } else { |
812 | 0 | memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, |
813 | 0 | md_len); |
814 | 0 | s->s3->previous_server_finished_len = md_len; |
815 | 0 | } |
816 | 0 |
|
817 | 0 | /* |
818 | 0 | * In TLS1.3 we also have to change cipher state and do any final processing |
819 | 0 | * of the initial server flight (if we are a client) |
820 | 0 | */ |
821 | 0 | if (SSL_IS_TLS13(s)) { |
822 | 0 | if (s->server) { |
823 | 0 | if (s->post_handshake_auth != SSL_PHA_REQUESTED && |
824 | 0 | !s->method->ssl3_enc->change_cipher_state(s, |
825 | 0 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
826 | 0 | /* SSLfatal() already called */ |
827 | 0 | return MSG_PROCESS_ERROR; |
828 | 0 | } |
829 | 0 | } else { |
830 | 0 | if (!s->method->ssl3_enc->generate_master_secret(s, |
831 | 0 | s->master_secret, s->handshake_secret, 0, |
832 | 0 | &s->session->master_key_length)) { |
833 | 0 | /* SSLfatal() already called */ |
834 | 0 | return MSG_PROCESS_ERROR; |
835 | 0 | } |
836 | 0 | if (!s->method->ssl3_enc->change_cipher_state(s, |
837 | 0 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
838 | 0 | /* SSLfatal() already called */ |
839 | 0 | return MSG_PROCESS_ERROR; |
840 | 0 | } |
841 | 0 | if (!tls_process_initial_server_flight(s)) { |
842 | 0 | /* SSLfatal() already called */ |
843 | 0 | return MSG_PROCESS_ERROR; |
844 | 0 | } |
845 | 0 | } |
846 | 0 | } |
847 | 0 | |
848 | 0 | return MSG_PROCESS_FINISHED_READING; |
849 | 0 | } |
850 | | |
851 | | int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt) |
852 | 0 | { |
853 | 0 | if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) { |
854 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
855 | 0 | SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); |
856 | 0 | return 0; |
857 | 0 | } |
858 | 0 |
|
859 | 0 | return 1; |
860 | 0 | } |
861 | | |
862 | | /* Add a certificate to the WPACKET */ |
863 | | static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain) |
864 | 0 | { |
865 | 0 | int len; |
866 | 0 | unsigned char *outbytes; |
867 | 0 |
|
868 | 0 | len = i2d_X509(x, NULL); |
869 | 0 | if (len < 0) { |
870 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET, |
871 | 0 | ERR_R_BUF_LIB); |
872 | 0 | return 0; |
873 | 0 | } |
874 | 0 | if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes) |
875 | 0 | || i2d_X509(x, &outbytes) != len) { |
876 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET, |
877 | 0 | ERR_R_INTERNAL_ERROR); |
878 | 0 | return 0; |
879 | 0 | } |
880 | 0 |
|
881 | 0 | if (SSL_IS_TLS13(s) |
882 | 0 | && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x, |
883 | 0 | chain)) { |
884 | 0 | /* SSLfatal() already called */ |
885 | 0 | return 0; |
886 | 0 | } |
887 | 0 | |
888 | 0 | return 1; |
889 | 0 | } |
890 | | |
891 | | /* Add certificate chain to provided WPACKET */ |
892 | | static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk) |
893 | 0 | { |
894 | 0 | int i, chain_count; |
895 | 0 | X509 *x; |
896 | 0 | STACK_OF(X509) *extra_certs; |
897 | 0 | STACK_OF(X509) *chain = NULL; |
898 | 0 | X509_STORE *chain_store; |
899 | 0 |
|
900 | 0 | if (cpk == NULL || cpk->x509 == NULL) |
901 | 0 | return 1; |
902 | 0 | |
903 | 0 | x = cpk->x509; |
904 | 0 |
|
905 | 0 | /* |
906 | 0 | * If we have a certificate specific chain use it, else use parent ctx. |
907 | 0 | */ |
908 | 0 | if (cpk->chain != NULL) |
909 | 0 | extra_certs = cpk->chain; |
910 | 0 | else |
911 | 0 | extra_certs = s->ctx->extra_certs; |
912 | 0 |
|
913 | 0 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) |
914 | 0 | chain_store = NULL; |
915 | 0 | else if (s->cert->chain_store) |
916 | 0 | chain_store = s->cert->chain_store; |
917 | 0 | else |
918 | 0 | chain_store = s->ctx->cert_store; |
919 | 0 |
|
920 | 0 | if (chain_store != NULL) { |
921 | 0 | X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new(); |
922 | 0 |
|
923 | 0 | if (xs_ctx == NULL) { |
924 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, |
925 | 0 | ERR_R_MALLOC_FAILURE); |
926 | 0 | return 0; |
927 | 0 | } |
928 | 0 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) { |
929 | 0 | X509_STORE_CTX_free(xs_ctx); |
930 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, |
931 | 0 | ERR_R_X509_LIB); |
932 | 0 | return 0; |
933 | 0 | } |
934 | 0 | /* |
935 | 0 | * It is valid for the chain not to be complete (because normally we |
936 | 0 | * don't include the root cert in the chain). Therefore we deliberately |
937 | 0 | * ignore the error return from this call. We're not actually verifying |
938 | 0 | * the cert - we're just building as much of the chain as we can |
939 | 0 | */ |
940 | 0 | (void)X509_verify_cert(xs_ctx); |
941 | 0 | /* Don't leave errors in the queue */ |
942 | 0 | ERR_clear_error(); |
943 | 0 | chain = X509_STORE_CTX_get0_chain(xs_ctx); |
944 | 0 | i = ssl_security_cert_chain(s, chain, NULL, 0); |
945 | 0 | if (i != 1) { |
946 | | #if 0 |
947 | | /* Dummy error calls so mkerr generates them */ |
948 | | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); |
949 | | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); |
950 | | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); |
951 | | #endif |
952 | | X509_STORE_CTX_free(xs_ctx); |
953 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i); |
954 | 0 | return 0; |
955 | 0 | } |
956 | 0 | chain_count = sk_X509_num(chain); |
957 | 0 | for (i = 0; i < chain_count; i++) { |
958 | 0 | x = sk_X509_value(chain, i); |
959 | 0 |
|
960 | 0 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) { |
961 | 0 | /* SSLfatal() already called */ |
962 | 0 | X509_STORE_CTX_free(xs_ctx); |
963 | 0 | return 0; |
964 | 0 | } |
965 | 0 | } |
966 | 0 | X509_STORE_CTX_free(xs_ctx); |
967 | 0 | } else { |
968 | 0 | i = ssl_security_cert_chain(s, extra_certs, x, 0); |
969 | 0 | if (i != 1) { |
970 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i); |
971 | 0 | return 0; |
972 | 0 | } |
973 | 0 | if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) { |
974 | 0 | /* SSLfatal() already called */ |
975 | 0 | return 0; |
976 | 0 | } |
977 | 0 | for (i = 0; i < sk_X509_num(extra_certs); i++) { |
978 | 0 | x = sk_X509_value(extra_certs, i); |
979 | 0 | if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) { |
980 | 0 | /* SSLfatal() already called */ |
981 | 0 | return 0; |
982 | 0 | } |
983 | 0 | } |
984 | 0 | } |
985 | 0 | return 1; |
986 | 0 | } |
987 | | |
988 | | unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk) |
989 | 0 | { |
990 | 0 | if (!WPACKET_start_sub_packet_u24(pkt)) { |
991 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN, |
992 | 0 | ERR_R_INTERNAL_ERROR); |
993 | 0 | return 0; |
994 | 0 | } |
995 | 0 |
|
996 | 0 | if (!ssl_add_cert_chain(s, pkt, cpk)) |
997 | 0 | return 0; |
998 | 0 | |
999 | 0 | if (!WPACKET_close(pkt)) { |
1000 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN, |
1001 | 0 | ERR_R_INTERNAL_ERROR); |
1002 | 0 | return 0; |
1003 | 0 | } |
1004 | 0 |
|
1005 | 0 | return 1; |
1006 | 0 | } |
1007 | | |
1008 | | /* |
1009 | | * Tidy up after the end of a handshake. In the case of SCTP this may result |
1010 | | * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is |
1011 | | * freed up as well. |
1012 | | */ |
1013 | | WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs, int stop) |
1014 | 0 | { |
1015 | 0 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
1016 | 0 |
|
1017 | 0 | if (clearbufs) { |
1018 | 0 | if (!SSL_IS_DTLS(s)) { |
1019 | 0 | /* |
1020 | 0 | * We don't do this in DTLS because we may still need the init_buf |
1021 | 0 | * in case there are any unexpected retransmits |
1022 | 0 | */ |
1023 | 0 | BUF_MEM_free(s->init_buf); |
1024 | 0 | s->init_buf = NULL; |
1025 | 0 | } |
1026 | 0 | if (!ssl_free_wbio_buffer(s)) { |
1027 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_FINISH_HANDSHAKE, |
1028 | 0 | ERR_R_INTERNAL_ERROR); |
1029 | 0 | return WORK_ERROR; |
1030 | 0 | } |
1031 | 0 | s->init_num = 0; |
1032 | 0 | } |
1033 | 0 |
|
1034 | 0 | if (SSL_IS_TLS13(s) && !s->server |
1035 | 0 | && s->post_handshake_auth == SSL_PHA_REQUESTED) |
1036 | 0 | s->post_handshake_auth = SSL_PHA_EXT_SENT; |
1037 | 0 |
|
1038 | 0 | /* |
1039 | 0 | * Only set if there was a Finished message and this isn't after a TLSv1.3 |
1040 | 0 | * post handshake exchange |
1041 | 0 | */ |
1042 | 0 | if (s->statem.cleanuphand) { |
1043 | 0 | /* skipped if we just sent a HelloRequest */ |
1044 | 0 | s->renegotiate = 0; |
1045 | 0 | s->new_session = 0; |
1046 | 0 | s->statem.cleanuphand = 0; |
1047 | 0 | s->ext.ticket_expected = 0; |
1048 | 0 |
|
1049 | 0 | ssl3_cleanup_key_block(s); |
1050 | 0 |
|
1051 | 0 | if (s->server) { |
1052 | 0 | /* |
1053 | 0 | * In TLSv1.3 we update the cache as part of constructing the |
1054 | 0 | * NewSessionTicket |
1055 | 0 | */ |
1056 | 0 | if (!SSL_IS_TLS13(s)) |
1057 | 0 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
1058 | 0 |
|
1059 | 0 | /* N.B. s->ctx may not equal s->session_ctx */ |
1060 | 0 | tsan_counter(&s->ctx->stats.sess_accept_good); |
1061 | 0 | s->handshake_func = ossl_statem_accept; |
1062 | 0 |
|
1063 | 0 | if (SSL_IS_DTLS(s) && !s->hit) { |
1064 | 0 | /* |
1065 | 0 | * We are finishing after the client. We start the timer going |
1066 | 0 | * in case there are any retransmits of our final flight |
1067 | 0 | * required. |
1068 | 0 | */ |
1069 | 0 | dtls1_start_timer(s); |
1070 | 0 | } |
1071 | 0 | } else { |
1072 | 0 | if (SSL_IS_TLS13(s)) { |
1073 | 0 | /* |
1074 | 0 | * We encourage applications to only use TLSv1.3 tickets once, |
1075 | 0 | * so we remove this one from the cache. |
1076 | 0 | */ |
1077 | 0 | if ((s->session_ctx->session_cache_mode |
1078 | 0 | & SSL_SESS_CACHE_CLIENT) != 0) |
1079 | 0 | SSL_CTX_remove_session(s->session_ctx, s->session); |
1080 | 0 | } else { |
1081 | 0 | /* |
1082 | 0 | * In TLSv1.3 we update the cache as part of processing the |
1083 | 0 | * NewSessionTicket |
1084 | 0 | */ |
1085 | 0 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
1086 | 0 | } |
1087 | 0 | if (s->hit) |
1088 | 0 | tsan_counter(&s->session_ctx->stats.sess_hit); |
1089 | 0 |
|
1090 | 0 | s->handshake_func = ossl_statem_connect; |
1091 | 0 | tsan_counter(&s->session_ctx->stats.sess_connect_good); |
1092 | 0 |
|
1093 | 0 | if (SSL_IS_DTLS(s) && s->hit) { |
1094 | 0 | /* |
1095 | 0 | * We are finishing after the server. We start the timer going |
1096 | 0 | * in case there are any retransmits of our final flight |
1097 | 0 | * required. |
1098 | 0 | */ |
1099 | 0 | dtls1_start_timer(s); |
1100 | 0 | } |
1101 | 0 | } |
1102 | 0 |
|
1103 | 0 | if (SSL_IS_DTLS(s)) { |
1104 | 0 | /* done with handshaking */ |
1105 | 0 | s->d1->handshake_read_seq = 0; |
1106 | 0 | s->d1->handshake_write_seq = 0; |
1107 | 0 | s->d1->next_handshake_write_seq = 0; |
1108 | 0 | dtls1_clear_received_buffer(s); |
1109 | 0 | } |
1110 | 0 | } |
1111 | 0 |
|
1112 | 0 | if (s->info_callback != NULL) |
1113 | 0 | cb = s->info_callback; |
1114 | 0 | else if (s->ctx->info_callback != NULL) |
1115 | 0 | cb = s->ctx->info_callback; |
1116 | 0 |
|
1117 | 0 | /* The callback may expect us to not be in init at handshake done */ |
1118 | 0 | ossl_statem_set_in_init(s, 0); |
1119 | 0 |
|
1120 | 0 | if (cb != NULL) |
1121 | 0 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
1122 | 0 |
|
1123 | 0 | if (!stop) { |
1124 | 0 | /* If we've got more work to do we go back into init */ |
1125 | 0 | ossl_statem_set_in_init(s, 1); |
1126 | 0 | return WORK_FINISHED_CONTINUE; |
1127 | 0 | } |
1128 | 0 | |
1129 | 0 | return WORK_FINISHED_STOP; |
1130 | 0 | } |
1131 | | |
1132 | | int tls_get_message_header(SSL *s, int *mt) |
1133 | 0 | { |
1134 | 0 | /* s->init_num < SSL3_HM_HEADER_LENGTH */ |
1135 | 0 | int skip_message, i, recvd_type; |
1136 | 0 | unsigned char *p; |
1137 | 0 | size_t l, readbytes; |
1138 | 0 |
|
1139 | 0 | p = (unsigned char *)s->init_buf->data; |
1140 | 0 |
|
1141 | 0 | do { |
1142 | 0 | while (s->init_num < SSL3_HM_HEADER_LENGTH) { |
1143 | 0 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, |
1144 | 0 | &p[s->init_num], |
1145 | 0 | SSL3_HM_HEADER_LENGTH - s->init_num, |
1146 | 0 | 0, &readbytes); |
1147 | 0 | if (i <= 0) { |
1148 | 0 | s->rwstate = SSL_READING; |
1149 | 0 | return 0; |
1150 | 0 | } |
1151 | 0 | if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
1152 | 0 | /* |
1153 | 0 | * A ChangeCipherSpec must be a single byte and may not occur |
1154 | 0 | * in the middle of a handshake message. |
1155 | 0 | */ |
1156 | 0 | if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) { |
1157 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1158 | 0 | SSL_F_TLS_GET_MESSAGE_HEADER, |
1159 | 0 | SSL_R_BAD_CHANGE_CIPHER_SPEC); |
1160 | 0 | return 0; |
1161 | 0 | } |
1162 | 0 | if (s->statem.hand_state == TLS_ST_BEFORE |
1163 | 0 | && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) { |
1164 | 0 | /* |
1165 | 0 | * We are stateless and we received a CCS. Probably this is |
1166 | 0 | * from a client between the first and second ClientHellos. |
1167 | 0 | * We should ignore this, but return an error because we do |
1168 | 0 | * not return success until we see the second ClientHello |
1169 | 0 | * with a valid cookie. |
1170 | 0 | */ |
1171 | 0 | return 0; |
1172 | 0 | } |
1173 | 0 | s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
1174 | 0 | s->init_num = readbytes - 1; |
1175 | 0 | s->init_msg = s->init_buf->data; |
1176 | 0 | s->s3->tmp.message_size = readbytes; |
1177 | 0 | return 1; |
1178 | 0 | } else if (recvd_type != SSL3_RT_HANDSHAKE) { |
1179 | 0 | SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
1180 | 0 | SSL_F_TLS_GET_MESSAGE_HEADER, |
1181 | 0 | SSL_R_CCS_RECEIVED_EARLY); |
1182 | 0 | return 0; |
1183 | 0 | } |
1184 | 0 | s->init_num += readbytes; |
1185 | 0 | } |
1186 | 0 |
|
1187 | 0 | skip_message = 0; |
1188 | 0 | if (!s->server) |
1189 | 0 | if (s->statem.hand_state != TLS_ST_OK |
1190 | 0 | && p[0] == SSL3_MT_HELLO_REQUEST) |
1191 | 0 | /* |
1192 | 0 | * The server may always send 'Hello Request' messages -- |
1193 | 0 | * we are doing a handshake anyway now, so ignore them if |
1194 | 0 | * their format is correct. Does not count for 'Finished' |
1195 | 0 | * MAC. |
1196 | 0 | */ |
1197 | 0 | if (p[1] == 0 && p[2] == 0 && p[3] == 0) { |
1198 | 0 | s->init_num = 0; |
1199 | 0 | skip_message = 1; |
1200 | 0 |
|
1201 | 0 | if (s->msg_callback) |
1202 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
1203 | 0 | p, SSL3_HM_HEADER_LENGTH, s, |
1204 | 0 | s->msg_callback_arg); |
1205 | 0 | } |
1206 | 0 | } while (skip_message); |
1207 | 0 | /* s->init_num == SSL3_HM_HEADER_LENGTH */ |
1208 | 0 |
|
1209 | 0 | *mt = *p; |
1210 | 0 | s->s3->tmp.message_type = *(p++); |
1211 | 0 |
|
1212 | 0 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
1213 | 0 | /* |
1214 | 0 | * Only happens with SSLv3+ in an SSLv2 backward compatible |
1215 | 0 | * ClientHello |
1216 | 0 | * |
1217 | 0 | * Total message size is the remaining record bytes to read |
1218 | 0 | * plus the SSL3_HM_HEADER_LENGTH bytes that we already read |
1219 | 0 | */ |
1220 | 0 | l = RECORD_LAYER_get_rrec_length(&s->rlayer) |
1221 | 0 | + SSL3_HM_HEADER_LENGTH; |
1222 | 0 | s->s3->tmp.message_size = l; |
1223 | 0 |
|
1224 | 0 | s->init_msg = s->init_buf->data; |
1225 | 0 | s->init_num = SSL3_HM_HEADER_LENGTH; |
1226 | 0 | } else { |
1227 | 0 | n2l3(p, l); |
1228 | 0 | /* BUF_MEM_grow takes an 'int' parameter */ |
1229 | 0 | if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) { |
1230 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER, |
1231 | 0 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
1232 | 0 | return 0; |
1233 | 0 | } |
1234 | 0 | s->s3->tmp.message_size = l; |
1235 | 0 |
|
1236 | 0 | s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH; |
1237 | 0 | s->init_num = 0; |
1238 | 0 | } |
1239 | 0 |
|
1240 | 0 | return 1; |
1241 | 0 | } |
1242 | | |
1243 | | int tls_get_message_body(SSL *s, size_t *len) |
1244 | 0 | { |
1245 | 0 | size_t n, readbytes; |
1246 | 0 | unsigned char *p; |
1247 | 0 | int i; |
1248 | 0 |
|
1249 | 0 | if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) { |
1250 | 0 | /* We've already read everything in */ |
1251 | 0 | *len = (unsigned long)s->init_num; |
1252 | 0 | return 1; |
1253 | 0 | } |
1254 | 0 | |
1255 | 0 | p = s->init_msg; |
1256 | 0 | n = s->s3->tmp.message_size - s->init_num; |
1257 | 0 | while (n > 0) { |
1258 | 0 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL, |
1259 | 0 | &p[s->init_num], n, 0, &readbytes); |
1260 | 0 | if (i <= 0) { |
1261 | 0 | s->rwstate = SSL_READING; |
1262 | 0 | *len = 0; |
1263 | 0 | return 0; |
1264 | 0 | } |
1265 | 0 | s->init_num += readbytes; |
1266 | 0 | n -= readbytes; |
1267 | 0 | } |
1268 | 0 |
|
1269 | 0 | /* |
1270 | 0 | * If receiving Finished, record MAC of prior handshake messages for |
1271 | 0 | * Finished verification. |
1272 | 0 | */ |
1273 | 0 | if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) { |
1274 | 0 | /* SSLfatal() already called */ |
1275 | 0 | *len = 0; |
1276 | 0 | return 0; |
1277 | 0 | } |
1278 | 0 | |
1279 | 0 | /* Feed this message into MAC computation. */ |
1280 | 0 | if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) { |
1281 | 0 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1282 | 0 | s->init_num)) { |
1283 | 0 | /* SSLfatal() already called */ |
1284 | 0 | *len = 0; |
1285 | 0 | return 0; |
1286 | 0 | } |
1287 | 0 | if (s->msg_callback) |
1288 | 0 | s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data, |
1289 | 0 | (size_t)s->init_num, s, s->msg_callback_arg); |
1290 | 0 | } else { |
1291 | 0 | /* |
1292 | 0 | * We defer feeding in the HRR until later. We'll do it as part of |
1293 | 0 | * processing the message |
1294 | 0 | * The TLsv1.3 handshake transcript stops at the ClientFinished |
1295 | 0 | * message. |
1296 | 0 | */ |
1297 | 0 | #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2) |
1298 | 0 | /* KeyUpdate and NewSessionTicket do not need to be added */ |
1299 | 0 | if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET |
1300 | 0 | && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) { |
1301 | 0 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO |
1302 | 0 | || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE |
1303 | 0 | || memcmp(hrrrandom, |
1304 | 0 | s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET, |
1305 | 0 | SSL3_RANDOM_SIZE) != 0) { |
1306 | 0 | if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
1307 | 0 | s->init_num + SSL3_HM_HEADER_LENGTH)) { |
1308 | 0 | /* SSLfatal() already called */ |
1309 | 0 | *len = 0; |
1310 | 0 | return 0; |
1311 | 0 | } |
1312 | 0 | } |
1313 | 0 | } |
1314 | 0 | if (s->msg_callback) |
1315 | 0 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, |
1316 | 0 | (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s, |
1317 | 0 | s->msg_callback_arg); |
1318 | 0 | } |
1319 | 0 |
|
1320 | 0 | *len = s->init_num; |
1321 | 0 | return 1; |
1322 | 0 | } |
1323 | | |
1324 | | static const X509ERR2ALERT x509table[] = { |
1325 | | {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE}, |
1326 | | {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE}, |
1327 | | {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE}, |
1328 | | {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA}, |
1329 | | {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED}, |
1330 | | {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE}, |
1331 | | {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE}, |
1332 | | {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED}, |
1333 | | {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR}, |
1334 | | {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE}, |
1335 | | {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED}, |
1336 | | {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE}, |
1337 | | {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR}, |
1338 | | {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE}, |
1339 | | {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA}, |
1340 | | {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE}, |
1341 | | {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE}, |
1342 | | {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE}, |
1343 | | {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE}, |
1344 | | {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE}, |
1345 | | {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE}, |
1346 | | {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE}, |
1347 | | {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA}, |
1348 | | {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR}, |
1349 | | {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE}, |
1350 | | {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE}, |
1351 | | {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR}, |
1352 | | {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA}, |
1353 | | {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA}, |
1354 | | {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR}, |
1355 | | {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE}, |
1356 | | {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE}, |
1357 | | {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE}, |
1358 | | {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA}, |
1359 | | {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA}, |
1360 | | {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA}, |
1361 | | {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA}, |
1362 | | {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA}, |
1363 | | {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR}, |
1364 | | |
1365 | | /* Last entry; return this if we don't find the value above. */ |
1366 | | {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN} |
1367 | | }; |
1368 | | |
1369 | | int ssl_x509err2alert(int x509err) |
1370 | 0 | { |
1371 | 0 | const X509ERR2ALERT *tp; |
1372 | 0 |
|
1373 | 0 | for (tp = x509table; tp->x509err != X509_V_OK; ++tp) |
1374 | 0 | if (tp->x509err == x509err) |
1375 | 0 | break; |
1376 | 0 | return tp->alert; |
1377 | 0 | } |
1378 | | |
1379 | | int ssl_allow_compression(SSL *s) |
1380 | 0 | { |
1381 | 0 | if (s->options & SSL_OP_NO_COMPRESSION) |
1382 | 0 | return 0; |
1383 | 0 | return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL); |
1384 | 0 | } |
1385 | | |
1386 | | static int version_cmp(const SSL *s, int a, int b) |
1387 | 0 | { |
1388 | 0 | int dtls = SSL_IS_DTLS(s); |
1389 | 0 |
|
1390 | 0 | if (a == b) |
1391 | 0 | return 0; |
1392 | 0 | if (!dtls) |
1393 | 0 | return a < b ? -1 : 1; |
1394 | 0 | return DTLS_VERSION_LT(a, b) ? -1 : 1; |
1395 | 0 | } |
1396 | | |
1397 | | typedef struct { |
1398 | | int version; |
1399 | | const SSL_METHOD *(*cmeth) (void); |
1400 | | const SSL_METHOD *(*smeth) (void); |
1401 | | } version_info; |
1402 | | |
1403 | | #if TLS_MAX_VERSION != TLS1_3_VERSION |
1404 | | # error Code needs update for TLS_method() support beyond TLS1_3_VERSION. |
1405 | | #endif |
1406 | | |
1407 | | /* Must be in order high to low */ |
1408 | | static const version_info tls_version_table[] = { |
1409 | | #ifndef OPENSSL_NO_TLS1_3 |
1410 | | {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method}, |
1411 | | #else |
1412 | | {TLS1_3_VERSION, NULL, NULL}, |
1413 | | #endif |
1414 | | #ifndef OPENSSL_NO_TLS1_2 |
1415 | | {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method}, |
1416 | | #else |
1417 | | {TLS1_2_VERSION, NULL, NULL}, |
1418 | | #endif |
1419 | | #ifndef OPENSSL_NO_TLS1_1 |
1420 | | {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method}, |
1421 | | #else |
1422 | | {TLS1_1_VERSION, NULL, NULL}, |
1423 | | #endif |
1424 | | #ifndef OPENSSL_NO_TLS1 |
1425 | | {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method}, |
1426 | | #else |
1427 | | {TLS1_VERSION, NULL, NULL}, |
1428 | | #endif |
1429 | | #ifndef OPENSSL_NO_SSL3 |
1430 | | {SSL3_VERSION, sslv3_client_method, sslv3_server_method}, |
1431 | | #else |
1432 | | {SSL3_VERSION, NULL, NULL}, |
1433 | | #endif |
1434 | | {0, NULL, NULL}, |
1435 | | }; |
1436 | | |
1437 | | #if DTLS_MAX_VERSION != DTLS1_2_VERSION |
1438 | | # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION. |
1439 | | #endif |
1440 | | |
1441 | | /* Must be in order high to low */ |
1442 | | static const version_info dtls_version_table[] = { |
1443 | | #ifndef OPENSSL_NO_DTLS1_2 |
1444 | | {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method}, |
1445 | | #else |
1446 | | {DTLS1_2_VERSION, NULL, NULL}, |
1447 | | #endif |
1448 | | #ifndef OPENSSL_NO_DTLS1 |
1449 | | {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method}, |
1450 | | {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL}, |
1451 | | #else |
1452 | | {DTLS1_VERSION, NULL, NULL}, |
1453 | | {DTLS1_BAD_VER, NULL, NULL}, |
1454 | | #endif |
1455 | | {0, NULL, NULL}, |
1456 | | }; |
1457 | | |
1458 | | /* |
1459 | | * ssl_method_error - Check whether an SSL_METHOD is enabled. |
1460 | | * |
1461 | | * @s: The SSL handle for the candidate method |
1462 | | * @method: the intended method. |
1463 | | * |
1464 | | * Returns 0 on success, or an SSL error reason on failure. |
1465 | | */ |
1466 | | static int ssl_method_error(const SSL *s, const SSL_METHOD *method) |
1467 | 0 | { |
1468 | 0 | int version = method->version; |
1469 | 0 |
|
1470 | 0 | if ((s->min_proto_version != 0 && |
1471 | 0 | version_cmp(s, version, s->min_proto_version) < 0) || |
1472 | 0 | ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0) |
1473 | 0 | return SSL_R_VERSION_TOO_LOW; |
1474 | 0 | |
1475 | 0 | if (s->max_proto_version != 0 && |
1476 | 0 | version_cmp(s, version, s->max_proto_version) > 0) |
1477 | 0 | return SSL_R_VERSION_TOO_HIGH; |
1478 | 0 | |
1479 | 0 | if ((s->options & method->mask) != 0) |
1480 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1481 | 0 | if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s)) |
1482 | 0 | return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE; |
1483 | 0 | |
1484 | 0 | return 0; |
1485 | 0 | } |
1486 | | |
1487 | | /* |
1488 | | * Only called by servers. Returns 1 if the server has a TLSv1.3 capable |
1489 | | * certificate type, or has PSK configured. Otherwise returns 0. |
1490 | | */ |
1491 | | static int is_tls13_capable(const SSL *s) |
1492 | 0 | { |
1493 | 0 | int i; |
1494 | 0 |
|
1495 | 0 | #ifndef OPENSSL_NO_PSK |
1496 | 0 | if (s->psk_server_callback != NULL) |
1497 | 0 | return 1; |
1498 | 0 | #endif |
1499 | 0 | |
1500 | 0 | if (s->psk_find_session_cb != NULL) |
1501 | 0 | return 1; |
1502 | 0 | |
1503 | 0 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
1504 | 0 | /* Skip over certs disallowed for TLSv1.3 */ |
1505 | 0 | switch (i) { |
1506 | 0 | case SSL_PKEY_DSA_SIGN: |
1507 | 0 | case SSL_PKEY_GOST01: |
1508 | 0 | case SSL_PKEY_GOST12_256: |
1509 | 0 | case SSL_PKEY_GOST12_512: |
1510 | 0 | continue; |
1511 | 0 | default: |
1512 | 0 | break; |
1513 | 0 | } |
1514 | 0 | if (ssl_has_cert(s, i)) |
1515 | 0 | return 1; |
1516 | 0 | } |
1517 | 0 |
|
1518 | 0 | return 0; |
1519 | 0 | } |
1520 | | |
1521 | | /* |
1522 | | * ssl_version_supported - Check that the specified `version` is supported by |
1523 | | * `SSL *` instance |
1524 | | * |
1525 | | * @s: The SSL handle for the candidate method |
1526 | | * @version: Protocol version to test against |
1527 | | * |
1528 | | * Returns 1 when supported, otherwise 0 |
1529 | | */ |
1530 | | int ssl_version_supported(const SSL *s, int version, const SSL_METHOD **meth) |
1531 | 0 | { |
1532 | 0 | const version_info *vent; |
1533 | 0 | const version_info *table; |
1534 | 0 |
|
1535 | 0 | switch (s->method->version) { |
1536 | 0 | default: |
1537 | 0 | /* Version should match method version for non-ANY method */ |
1538 | 0 | return version_cmp(s, version, s->version) == 0; |
1539 | 0 | case TLS_ANY_VERSION: |
1540 | 0 | table = tls_version_table; |
1541 | 0 | break; |
1542 | 0 | case DTLS_ANY_VERSION: |
1543 | 0 | table = dtls_version_table; |
1544 | 0 | break; |
1545 | 0 | } |
1546 | 0 |
|
1547 | 0 | for (vent = table; |
1548 | 0 | vent->version != 0 && version_cmp(s, version, vent->version) <= 0; |
1549 | 0 | ++vent) { |
1550 | 0 | if (vent->cmeth != NULL |
1551 | 0 | && version_cmp(s, version, vent->version) == 0 |
1552 | 0 | && ssl_method_error(s, vent->cmeth()) == 0 |
1553 | 0 | && (!s->server |
1554 | 0 | || version != TLS1_3_VERSION |
1555 | 0 | || is_tls13_capable(s))) { |
1556 | 0 | if (meth != NULL) |
1557 | 0 | *meth = vent->cmeth(); |
1558 | 0 | return 1; |
1559 | 0 | } |
1560 | 0 | } |
1561 | 0 | return 0; |
1562 | 0 | } |
1563 | | |
1564 | | /* |
1565 | | * ssl_check_version_downgrade - In response to RFC7507 SCSV version |
1566 | | * fallback indication from a client check whether we're using the highest |
1567 | | * supported protocol version. |
1568 | | * |
1569 | | * @s server SSL handle. |
1570 | | * |
1571 | | * Returns 1 when using the highest enabled version, 0 otherwise. |
1572 | | */ |
1573 | | int ssl_check_version_downgrade(SSL *s) |
1574 | 0 | { |
1575 | 0 | const version_info *vent; |
1576 | 0 | const version_info *table; |
1577 | 0 |
|
1578 | 0 | /* |
1579 | 0 | * Check that the current protocol is the highest enabled version |
1580 | 0 | * (according to s->ctx->method, as version negotiation may have changed |
1581 | 0 | * s->method). |
1582 | 0 | */ |
1583 | 0 | if (s->version == s->ctx->method->version) |
1584 | 0 | return 1; |
1585 | 0 | |
1586 | 0 | /* |
1587 | 0 | * Apparently we're using a version-flexible SSL_METHOD (not at its |
1588 | 0 | * highest protocol version). |
1589 | 0 | */ |
1590 | 0 | if (s->ctx->method->version == TLS_method()->version) |
1591 | 0 | table = tls_version_table; |
1592 | 0 | else if (s->ctx->method->version == DTLS_method()->version) |
1593 | 0 | table = dtls_version_table; |
1594 | 0 | else { |
1595 | 0 | /* Unexpected state; fail closed. */ |
1596 | 0 | return 0; |
1597 | 0 | } |
1598 | 0 | |
1599 | 0 | for (vent = table; vent->version != 0; ++vent) { |
1600 | 0 | if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0) |
1601 | 0 | return s->version == vent->version; |
1602 | 0 | } |
1603 | 0 | return 0; |
1604 | 0 | } |
1605 | | |
1606 | | /* |
1607 | | * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS |
1608 | | * protocols, provided the initial (D)TLS method is version-flexible. This |
1609 | | * function sanity-checks the proposed value and makes sure the method is |
1610 | | * version-flexible, then sets the limit if all is well. |
1611 | | * |
1612 | | * @method_version: The version of the current SSL_METHOD. |
1613 | | * @version: the intended limit. |
1614 | | * @bound: pointer to limit to be updated. |
1615 | | * |
1616 | | * Returns 1 on success, 0 on failure. |
1617 | | */ |
1618 | | int ssl_set_version_bound(int method_version, int version, int *bound) |
1619 | 0 | { |
1620 | 0 | if (version == 0) { |
1621 | 0 | *bound = version; |
1622 | 0 | return 1; |
1623 | 0 | } |
1624 | 0 | |
1625 | 0 | /*- |
1626 | 0 | * Restrict TLS methods to TLS protocol versions. |
1627 | 0 | * Restrict DTLS methods to DTLS protocol versions. |
1628 | 0 | * Note, DTLS version numbers are decreasing, use comparison macros. |
1629 | 0 | * |
1630 | 0 | * Note that for both lower-bounds we use explicit versions, not |
1631 | 0 | * (D)TLS_MIN_VERSION. This is because we don't want to break user |
1632 | 0 | * configurations. If the MIN (supported) version ever rises, the user's |
1633 | 0 | * "floor" remains valid even if no longer available. We don't expect the |
1634 | 0 | * MAX ceiling to ever get lower, so making that variable makes sense. |
1635 | 0 | */ |
1636 | 0 | switch (method_version) { |
1637 | 0 | default: |
1638 | 0 | /* |
1639 | 0 | * XXX For fixed version methods, should we always fail and not set any |
1640 | 0 | * bounds, always succeed and not set any bounds, or set the bounds and |
1641 | 0 | * arrange to fail later if they are not met? At present fixed-version |
1642 | 0 | * methods are not subject to controls that disable individual protocol |
1643 | 0 | * versions. |
1644 | 0 | */ |
1645 | 0 | return 0; |
1646 | 0 |
|
1647 | 0 | case TLS_ANY_VERSION: |
1648 | 0 | if (version < SSL3_VERSION || version > TLS_MAX_VERSION) |
1649 | 0 | return 0; |
1650 | 0 | break; |
1651 | 0 |
|
1652 | 0 | case DTLS_ANY_VERSION: |
1653 | 0 | if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) || |
1654 | 0 | DTLS_VERSION_LT(version, DTLS1_BAD_VER)) |
1655 | 0 | return 0; |
1656 | 0 | break; |
1657 | 0 | } |
1658 | 0 | |
1659 | 0 | *bound = version; |
1660 | 0 | return 1; |
1661 | 0 | } |
1662 | | |
1663 | | static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd) |
1664 | 0 | { |
1665 | 0 | if (vers == TLS1_2_VERSION |
1666 | 0 | && ssl_version_supported(s, TLS1_3_VERSION, NULL)) { |
1667 | 0 | *dgrd = DOWNGRADE_TO_1_2; |
1668 | 0 | } else if (!SSL_IS_DTLS(s) |
1669 | 0 | && vers < TLS1_2_VERSION |
1670 | 0 | /* |
1671 | 0 | * We need to ensure that a server that disables TLSv1.2 |
1672 | 0 | * (creating a hole between TLSv1.3 and TLSv1.1) can still |
1673 | 0 | * complete handshakes with clients that support TLSv1.2 and |
1674 | 0 | * below. Therefore we do not enable the sentinel if TLSv1.3 is |
1675 | 0 | * enabled and TLSv1.2 is not. |
1676 | 0 | */ |
1677 | 0 | && ssl_version_supported(s, TLS1_2_VERSION, NULL)) { |
1678 | 0 | *dgrd = DOWNGRADE_TO_1_1; |
1679 | 0 | } else { |
1680 | 0 | *dgrd = DOWNGRADE_NONE; |
1681 | 0 | } |
1682 | 0 | } |
1683 | | |
1684 | | /* |
1685 | | * ssl_choose_server_version - Choose server (D)TLS version. Called when the |
1686 | | * client HELLO is received to select the final server protocol version and |
1687 | | * the version specific method. |
1688 | | * |
1689 | | * @s: server SSL handle. |
1690 | | * |
1691 | | * Returns 0 on success or an SSL error reason number on failure. |
1692 | | */ |
1693 | | int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd) |
1694 | 0 | { |
1695 | 0 | /*- |
1696 | 0 | * With version-flexible methods we have an initial state with: |
1697 | 0 | * |
1698 | 0 | * s->method->version == (D)TLS_ANY_VERSION, |
1699 | 0 | * s->version == (D)TLS_MAX_VERSION. |
1700 | 0 | * |
1701 | 0 | * So we detect version-flexible methods via the method version, not the |
1702 | 0 | * handle version. |
1703 | 0 | */ |
1704 | 0 | int server_version = s->method->version; |
1705 | 0 | int client_version = hello->legacy_version; |
1706 | 0 | const version_info *vent; |
1707 | 0 | const version_info *table; |
1708 | 0 | int disabled = 0; |
1709 | 0 | RAW_EXTENSION *suppversions; |
1710 | 0 |
|
1711 | 0 | s->client_version = client_version; |
1712 | 0 |
|
1713 | 0 | switch (server_version) { |
1714 | 0 | default: |
1715 | 0 | if (!SSL_IS_TLS13(s)) { |
1716 | 0 | if (version_cmp(s, client_version, s->version) < 0) |
1717 | 0 | return SSL_R_WRONG_SSL_VERSION; |
1718 | 0 | *dgrd = DOWNGRADE_NONE; |
1719 | 0 | /* |
1720 | 0 | * If this SSL handle is not from a version flexible method we don't |
1721 | 0 | * (and never did) check min/max FIPS or Suite B constraints. Hope |
1722 | 0 | * that's OK. It is up to the caller to not choose fixed protocol |
1723 | 0 | * versions they don't want. If not, then easy to fix, just return |
1724 | 0 | * ssl_method_error(s, s->method) |
1725 | 0 | */ |
1726 | 0 | return 0; |
1727 | 0 | } |
1728 | 0 | /* |
1729 | 0 | * Fall through if we are TLSv1.3 already (this means we must be after |
1730 | 0 | * a HelloRetryRequest |
1731 | 0 | */ |
1732 | 0 | /* fall thru */ |
1733 | 0 | case TLS_ANY_VERSION: |
1734 | 0 | table = tls_version_table; |
1735 | 0 | break; |
1736 | 0 | case DTLS_ANY_VERSION: |
1737 | 0 | table = dtls_version_table; |
1738 | 0 | break; |
1739 | 0 | } |
1740 | 0 |
|
1741 | 0 | suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions]; |
1742 | 0 |
|
1743 | 0 | /* If we did an HRR then supported versions is mandatory */ |
1744 | 0 | if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE) |
1745 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1746 | 0 | |
1747 | 0 | if (suppversions->present && !SSL_IS_DTLS(s)) { |
1748 | 0 | unsigned int candidate_vers = 0; |
1749 | 0 | unsigned int best_vers = 0; |
1750 | 0 | const SSL_METHOD *best_method = NULL; |
1751 | 0 | PACKET versionslist; |
1752 | 0 |
|
1753 | 0 | suppversions->parsed = 1; |
1754 | 0 |
|
1755 | 0 | if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) { |
1756 | 0 | /* Trailing or invalid data? */ |
1757 | 0 | return SSL_R_LENGTH_MISMATCH; |
1758 | 0 | } |
1759 | 0 |
|
1760 | 0 | /* |
1761 | 0 | * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION. |
1762 | 0 | * The spec only requires servers to check that it isn't SSLv3: |
1763 | 0 | * "Any endpoint receiving a Hello message with |
1764 | 0 | * ClientHello.legacy_version or ServerHello.legacy_version set to |
1765 | 0 | * 0x0300 MUST abort the handshake with a "protocol_version" alert." |
1766 | 0 | * We are slightly stricter and require that it isn't SSLv3 or lower. |
1767 | 0 | * We tolerate TLSv1 and TLSv1.1. |
1768 | 0 | */ |
1769 | 0 | if (client_version <= SSL3_VERSION) |
1770 | 0 | return SSL_R_BAD_LEGACY_VERSION; |
1771 | 0 | |
1772 | 0 | while (PACKET_get_net_2(&versionslist, &candidate_vers)) { |
1773 | 0 | if (version_cmp(s, candidate_vers, best_vers) <= 0) |
1774 | 0 | continue; |
1775 | 0 | if (ssl_version_supported(s, candidate_vers, &best_method)) |
1776 | 0 | best_vers = candidate_vers; |
1777 | 0 | } |
1778 | 0 | if (PACKET_remaining(&versionslist) != 0) { |
1779 | 0 | /* Trailing data? */ |
1780 | 0 | return SSL_R_LENGTH_MISMATCH; |
1781 | 0 | } |
1782 | 0 |
|
1783 | 0 | if (best_vers > 0) { |
1784 | 0 | if (s->hello_retry_request != SSL_HRR_NONE) { |
1785 | 0 | /* |
1786 | 0 | * This is after a HelloRetryRequest so we better check that we |
1787 | 0 | * negotiated TLSv1.3 |
1788 | 0 | */ |
1789 | 0 | if (best_vers != TLS1_3_VERSION) |
1790 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1791 | 0 | return 0; |
1792 | 0 | } |
1793 | 0 | check_for_downgrade(s, best_vers, dgrd); |
1794 | 0 | s->version = best_vers; |
1795 | 0 | s->method = best_method; |
1796 | 0 | return 0; |
1797 | 0 | } |
1798 | 0 | return SSL_R_UNSUPPORTED_PROTOCOL; |
1799 | 0 | } |
1800 | 0 |
|
1801 | 0 | /* |
1802 | 0 | * If the supported versions extension isn't present, then the highest |
1803 | 0 | * version we can negotiate is TLSv1.2 |
1804 | 0 | */ |
1805 | 0 | if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0) |
1806 | 0 | client_version = TLS1_2_VERSION; |
1807 | 0 |
|
1808 | 0 | /* |
1809 | 0 | * No supported versions extension, so we just use the version supplied in |
1810 | 0 | * the ClientHello. |
1811 | 0 | */ |
1812 | 0 | for (vent = table; vent->version != 0; ++vent) { |
1813 | 0 | const SSL_METHOD *method; |
1814 | 0 |
|
1815 | 0 | if (vent->smeth == NULL || |
1816 | 0 | version_cmp(s, client_version, vent->version) < 0) |
1817 | 0 | continue; |
1818 | 0 | method = vent->smeth(); |
1819 | 0 | if (ssl_method_error(s, method) == 0) { |
1820 | 0 | check_for_downgrade(s, vent->version, dgrd); |
1821 | 0 | s->version = vent->version; |
1822 | 0 | s->method = method; |
1823 | 0 | return 0; |
1824 | 0 | } |
1825 | 0 | disabled = 1; |
1826 | 0 | } |
1827 | 0 | return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW; |
1828 | 0 | } |
1829 | | |
1830 | | /* |
1831 | | * ssl_choose_client_version - Choose client (D)TLS version. Called when the |
1832 | | * server HELLO is received to select the final client protocol version and |
1833 | | * the version specific method. |
1834 | | * |
1835 | | * @s: client SSL handle. |
1836 | | * @version: The proposed version from the server's HELLO. |
1837 | | * @extensions: The extensions received |
1838 | | * |
1839 | | * Returns 1 on success or 0 on error. |
1840 | | */ |
1841 | | int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions) |
1842 | 0 | { |
1843 | 0 | const version_info *vent; |
1844 | 0 | const version_info *table; |
1845 | 0 | int ret, ver_min, ver_max, real_max, origv; |
1846 | 0 |
|
1847 | 0 | origv = s->version; |
1848 | 0 | s->version = version; |
1849 | 0 |
|
1850 | 0 | /* This will overwrite s->version if the extension is present */ |
1851 | 0 | if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions, |
1852 | 0 | SSL_EXT_TLS1_2_SERVER_HELLO |
1853 | 0 | | SSL_EXT_TLS1_3_SERVER_HELLO, extensions, |
1854 | 0 | NULL, 0)) { |
1855 | 0 | s->version = origv; |
1856 | 0 | return 0; |
1857 | 0 | } |
1858 | 0 | |
1859 | 0 | if (s->hello_retry_request != SSL_HRR_NONE |
1860 | 0 | && s->version != TLS1_3_VERSION) { |
1861 | 0 | s->version = origv; |
1862 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION, |
1863 | 0 | SSL_R_WRONG_SSL_VERSION); |
1864 | 0 | return 0; |
1865 | 0 | } |
1866 | 0 |
|
1867 | 0 | switch (s->method->version) { |
1868 | 0 | default: |
1869 | 0 | if (s->version != s->method->version) { |
1870 | 0 | s->version = origv; |
1871 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
1872 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, |
1873 | 0 | SSL_R_WRONG_SSL_VERSION); |
1874 | 0 | return 0; |
1875 | 0 | } |
1876 | 0 | /* |
1877 | 0 | * If this SSL handle is not from a version flexible method we don't |
1878 | 0 | * (and never did) check min/max, FIPS or Suite B constraints. Hope |
1879 | 0 | * that's OK. It is up to the caller to not choose fixed protocol |
1880 | 0 | * versions they don't want. If not, then easy to fix, just return |
1881 | 0 | * ssl_method_error(s, s->method) |
1882 | 0 | */ |
1883 | 0 | return 1; |
1884 | 0 | case TLS_ANY_VERSION: |
1885 | 0 | table = tls_version_table; |
1886 | 0 | break; |
1887 | 0 | case DTLS_ANY_VERSION: |
1888 | 0 | table = dtls_version_table; |
1889 | 0 | break; |
1890 | 0 | } |
1891 | 0 |
|
1892 | 0 | ret = ssl_get_min_max_version(s, &ver_min, &ver_max, &real_max); |
1893 | 0 | if (ret != 0) { |
1894 | 0 | s->version = origv; |
1895 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
1896 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, ret); |
1897 | 0 | return 0; |
1898 | 0 | } |
1899 | 0 | if (SSL_IS_DTLS(s) ? DTLS_VERSION_LT(s->version, ver_min) |
1900 | 0 | : s->version < ver_min) { |
1901 | 0 | s->version = origv; |
1902 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
1903 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, SSL_R_UNSUPPORTED_PROTOCOL); |
1904 | 0 | return 0; |
1905 | 0 | } else if (SSL_IS_DTLS(s) ? DTLS_VERSION_GT(s->version, ver_max) |
1906 | 0 | : s->version > ver_max) { |
1907 | 0 | s->version = origv; |
1908 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
1909 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, SSL_R_UNSUPPORTED_PROTOCOL); |
1910 | 0 | return 0; |
1911 | 0 | } |
1912 | 0 |
|
1913 | 0 | if ((s->mode & SSL_MODE_SEND_FALLBACK_SCSV) == 0) |
1914 | 0 | real_max = ver_max; |
1915 | 0 |
|
1916 | 0 | /* Check for downgrades */ |
1917 | 0 | if (s->version == TLS1_2_VERSION && real_max > s->version) { |
1918 | 0 | if (memcmp(tls12downgrade, |
1919 | 0 | s->s3->server_random + SSL3_RANDOM_SIZE |
1920 | 0 | - sizeof(tls12downgrade), |
1921 | 0 | sizeof(tls12downgrade)) == 0) { |
1922 | 0 | s->version = origv; |
1923 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1924 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, |
1925 | 0 | SSL_R_INAPPROPRIATE_FALLBACK); |
1926 | 0 | return 0; |
1927 | 0 | } |
1928 | 0 | } else if (!SSL_IS_DTLS(s) |
1929 | 0 | && s->version < TLS1_2_VERSION |
1930 | 0 | && real_max > s->version) { |
1931 | 0 | if (memcmp(tls11downgrade, |
1932 | 0 | s->s3->server_random + SSL3_RANDOM_SIZE |
1933 | 0 | - sizeof(tls11downgrade), |
1934 | 0 | sizeof(tls11downgrade)) == 0) { |
1935 | 0 | s->version = origv; |
1936 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1937 | 0 | SSL_F_SSL_CHOOSE_CLIENT_VERSION, |
1938 | 0 | SSL_R_INAPPROPRIATE_FALLBACK); |
1939 | 0 | return 0; |
1940 | 0 | } |
1941 | 0 | } |
1942 | 0 |
|
1943 | 0 | for (vent = table; vent->version != 0; ++vent) { |
1944 | 0 | if (vent->cmeth == NULL || s->version != vent->version) |
1945 | 0 | continue; |
1946 | 0 | |
1947 | 0 | s->method = vent->cmeth(); |
1948 | 0 | return 1; |
1949 | 0 | } |
1950 | 0 |
|
1951 | 0 | s->version = origv; |
1952 | 0 | SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION, |
1953 | 0 | SSL_R_UNSUPPORTED_PROTOCOL); |
1954 | 0 | return 0; |
1955 | 0 | } |
1956 | | |
1957 | | /* |
1958 | | * ssl_get_min_max_version - get minimum and maximum protocol version |
1959 | | * @s: The SSL connection |
1960 | | * @min_version: The minimum supported version |
1961 | | * @max_version: The maximum supported version |
1962 | | * @real_max: The highest version below the lowest compile time version hole |
1963 | | * where that hole lies above at least one run-time enabled |
1964 | | * protocol. |
1965 | | * |
1966 | | * Work out what version we should be using for the initial ClientHello if the |
1967 | | * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx |
1968 | | * options, the MinProtocol and MaxProtocol configuration commands, any Suite B |
1969 | | * constraints and any floor imposed by the security level here, |
1970 | | * so we don't advertise the wrong protocol version to only reject the outcome later. |
1971 | | * |
1972 | | * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled, |
1973 | | * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol |
1974 | | * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1. |
1975 | | * |
1976 | | * Returns 0 on success or an SSL error reason number on failure. On failure |
1977 | | * min_version and max_version will also be set to 0. |
1978 | | */ |
1979 | | int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version, |
1980 | | int *real_max) |
1981 | 0 | { |
1982 | 0 | int version, tmp_real_max; |
1983 | 0 | int hole; |
1984 | 0 | const SSL_METHOD *single = NULL; |
1985 | 0 | const SSL_METHOD *method; |
1986 | 0 | const version_info *table; |
1987 | 0 | const version_info *vent; |
1988 | 0 |
|
1989 | 0 | switch (s->method->version) { |
1990 | 0 | default: |
1991 | 0 | /* |
1992 | 0 | * If this SSL handle is not from a version flexible method we don't |
1993 | 0 | * (and never did) check min/max FIPS or Suite B constraints. Hope |
1994 | 0 | * that's OK. It is up to the caller to not choose fixed protocol |
1995 | 0 | * versions they don't want. If not, then easy to fix, just return |
1996 | 0 | * ssl_method_error(s, s->method) |
1997 | 0 | */ |
1998 | 0 | *min_version = *max_version = s->version; |
1999 | 0 | /* |
2000 | 0 | * Providing a real_max only makes sense where we're using a version |
2001 | 0 | * flexible method. |
2002 | 0 | */ |
2003 | 0 | if (!ossl_assert(real_max == NULL)) |
2004 | 0 | return ERR_R_INTERNAL_ERROR; |
2005 | 0 | return 0; |
2006 | 0 | case TLS_ANY_VERSION: |
2007 | 0 | table = tls_version_table; |
2008 | 0 | break; |
2009 | 0 | case DTLS_ANY_VERSION: |
2010 | 0 | table = dtls_version_table; |
2011 | 0 | break; |
2012 | 0 | } |
2013 | 0 |
|
2014 | 0 | /* |
2015 | 0 | * SSL_OP_NO_X disables all protocols above X *if* there are some protocols |
2016 | 0 | * below X enabled. This is required in order to maintain the "version |
2017 | 0 | * capability" vector contiguous. Any versions with a NULL client method |
2018 | 0 | * (protocol version client is disabled at compile-time) is also a "hole". |
2019 | 0 | * |
2020 | 0 | * Our initial state is hole == 1, version == 0. That is, versions above |
2021 | 0 | * the first version in the method table are disabled (a "hole" above |
2022 | 0 | * the valid protocol entries) and we don't have a selected version yet. |
2023 | 0 | * |
2024 | 0 | * Whenever "hole == 1", and we hit an enabled method, its version becomes |
2025 | 0 | * the selected version, and the method becomes a candidate "single" |
2026 | 0 | * method. We're no longer in a hole, so "hole" becomes 0. |
2027 | 0 | * |
2028 | 0 | * If "hole == 0" and we hit an enabled method, then "single" is cleared, |
2029 | 0 | * as we support a contiguous range of at least two methods. If we hit |
2030 | 0 | * a disabled method, then hole becomes true again, but nothing else |
2031 | 0 | * changes yet, because all the remaining methods may be disabled too. |
2032 | 0 | * If we again hit an enabled method after the new hole, it becomes |
2033 | 0 | * selected, as we start from scratch. |
2034 | 0 | */ |
2035 | 0 | *min_version = version = 0; |
2036 | 0 | hole = 1; |
2037 | 0 | if (real_max != NULL) |
2038 | 0 | *real_max = 0; |
2039 | 0 | tmp_real_max = 0; |
2040 | 0 | for (vent = table; vent->version != 0; ++vent) { |
2041 | 0 | /* |
2042 | 0 | * A table entry with a NULL client method is still a hole in the |
2043 | 0 | * "version capability" vector. |
2044 | 0 | */ |
2045 | 0 | if (vent->cmeth == NULL) { |
2046 | 0 | hole = 1; |
2047 | 0 | tmp_real_max = 0; |
2048 | 0 | continue; |
2049 | 0 | } |
2050 | 0 | method = vent->cmeth(); |
2051 | 0 |
|
2052 | 0 | if (hole == 1 && tmp_real_max == 0) |
2053 | 0 | tmp_real_max = vent->version; |
2054 | 0 |
|
2055 | 0 | if (ssl_method_error(s, method) != 0) { |
2056 | 0 | hole = 1; |
2057 | 0 | } else if (!hole) { |
2058 | 0 | single = NULL; |
2059 | 0 | *min_version = method->version; |
2060 | 0 | } else { |
2061 | 0 | if (real_max != NULL && tmp_real_max != 0) |
2062 | 0 | *real_max = tmp_real_max; |
2063 | 0 | version = (single = method)->version; |
2064 | 0 | *min_version = version; |
2065 | 0 | hole = 0; |
2066 | 0 | } |
2067 | 0 | } |
2068 | 0 |
|
2069 | 0 | *max_version = version; |
2070 | 0 |
|
2071 | 0 | /* Fail if everything is disabled */ |
2072 | 0 | if (version == 0) |
2073 | 0 | return SSL_R_NO_PROTOCOLS_AVAILABLE; |
2074 | 0 | |
2075 | 0 | return 0; |
2076 | 0 | } |
2077 | | |
2078 | | /* |
2079 | | * ssl_set_client_hello_version - Work out what version we should be using for |
2080 | | * the initial ClientHello.legacy_version field. |
2081 | | * |
2082 | | * @s: client SSL handle. |
2083 | | * |
2084 | | * Returns 0 on success or an SSL error reason number on failure. |
2085 | | */ |
2086 | | int ssl_set_client_hello_version(SSL *s) |
2087 | 0 | { |
2088 | 0 | int ver_min, ver_max, ret; |
2089 | 0 |
|
2090 | 0 | /* |
2091 | 0 | * In a renegotiation we always send the same client_version that we sent |
2092 | 0 | * last time, regardless of which version we eventually negotiated. |
2093 | 0 | */ |
2094 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
2095 | 0 | return 0; |
2096 | 0 | |
2097 | 0 | ret = ssl_get_min_max_version(s, &ver_min, &ver_max, NULL); |
2098 | 0 |
|
2099 | 0 | if (ret != 0) |
2100 | 0 | return ret; |
2101 | 0 | |
2102 | 0 | s->version = ver_max; |
2103 | 0 |
|
2104 | 0 | /* TLS1.3 always uses TLS1.2 in the legacy_version field */ |
2105 | 0 | if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION) |
2106 | 0 | ver_max = TLS1_2_VERSION; |
2107 | 0 |
|
2108 | 0 | s->client_version = ver_max; |
2109 | 0 | return 0; |
2110 | 0 | } |
2111 | | |
2112 | | /* |
2113 | | * Checks a list of |groups| to determine if the |group_id| is in it. If it is |
2114 | | * and |checkallow| is 1 then additionally check if the group is allowed to be |
2115 | | * used. Returns 1 if the group is in the list (and allowed if |checkallow| is |
2116 | | * 1) or 0 otherwise. |
2117 | | */ |
2118 | | #ifndef OPENSSL_NO_EC |
2119 | | int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups, |
2120 | | size_t num_groups, int checkallow) |
2121 | 0 | { |
2122 | 0 | size_t i; |
2123 | 0 |
|
2124 | 0 | if (groups == NULL || num_groups == 0) |
2125 | 0 | return 0; |
2126 | 0 | |
2127 | 0 | for (i = 0; i < num_groups; i++) { |
2128 | 0 | uint16_t group = groups[i]; |
2129 | 0 |
|
2130 | 0 | if (group_id == group |
2131 | 0 | && (!checkallow |
2132 | 0 | || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) { |
2133 | 0 | return 1; |
2134 | 0 | } |
2135 | 0 | } |
2136 | 0 |
|
2137 | 0 | return 0; |
2138 | 0 | } |
2139 | | #endif |
2140 | | |
2141 | | /* Replace ClientHello1 in the transcript hash with a synthetic message */ |
2142 | | int create_synthetic_message_hash(SSL *s, const unsigned char *hashval, |
2143 | | size_t hashlen, const unsigned char *hrr, |
2144 | | size_t hrrlen) |
2145 | 0 | { |
2146 | 0 | unsigned char hashvaltmp[EVP_MAX_MD_SIZE]; |
2147 | 0 | unsigned char msghdr[SSL3_HM_HEADER_LENGTH]; |
2148 | 0 |
|
2149 | 0 | memset(msghdr, 0, sizeof(msghdr)); |
2150 | 0 |
|
2151 | 0 | if (hashval == NULL) { |
2152 | 0 | hashval = hashvaltmp; |
2153 | 0 | hashlen = 0; |
2154 | 0 | /* Get the hash of the initial ClientHello */ |
2155 | 0 | if (!ssl3_digest_cached_records(s, 0) |
2156 | 0 | || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp), |
2157 | 0 | &hashlen)) { |
2158 | 0 | /* SSLfatal() already called */ |
2159 | 0 | return 0; |
2160 | 0 | } |
2161 | 0 | } |
2162 | 0 | |
2163 | 0 | /* Reinitialise the transcript hash */ |
2164 | 0 | if (!ssl3_init_finished_mac(s)) { |
2165 | 0 | /* SSLfatal() already called */ |
2166 | 0 | return 0; |
2167 | 0 | } |
2168 | 0 | |
2169 | 0 | /* Inject the synthetic message_hash message */ |
2170 | 0 | msghdr[0] = SSL3_MT_MESSAGE_HASH; |
2171 | 0 | msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen; |
2172 | 0 | if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH) |
2173 | 0 | || !ssl3_finish_mac(s, hashval, hashlen)) { |
2174 | 0 | /* SSLfatal() already called */ |
2175 | 0 | return 0; |
2176 | 0 | } |
2177 | 0 | |
2178 | 0 | /* |
2179 | 0 | * Now re-inject the HRR and current message if appropriate (we just deleted |
2180 | 0 | * it when we reinitialised the transcript hash above). Only necessary after |
2181 | 0 | * receiving a ClientHello2 with a cookie. |
2182 | 0 | */ |
2183 | 0 | if (hrr != NULL |
2184 | 0 | && (!ssl3_finish_mac(s, hrr, hrrlen) |
2185 | 0 | || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
2186 | 0 | s->s3->tmp.message_size |
2187 | 0 | + SSL3_HM_HEADER_LENGTH))) { |
2188 | 0 | /* SSLfatal() already called */ |
2189 | 0 | return 0; |
2190 | 0 | } |
2191 | 0 | |
2192 | 0 | return 1; |
2193 | 0 | } |
2194 | | |
2195 | | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
2196 | 0 | { |
2197 | 0 | return X509_NAME_cmp(*a, *b); |
2198 | 0 | } |
2199 | | |
2200 | | int parse_ca_names(SSL *s, PACKET *pkt) |
2201 | 0 | { |
2202 | 0 | STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
2203 | 0 | X509_NAME *xn = NULL; |
2204 | 0 | PACKET cadns; |
2205 | 0 |
|
2206 | 0 | if (ca_sk == NULL) { |
2207 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES, |
2208 | 0 | ERR_R_MALLOC_FAILURE); |
2209 | 0 | goto err; |
2210 | 0 | } |
2211 | 0 | /* get the CA RDNs */ |
2212 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &cadns)) { |
2213 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES, |
2214 | 0 | SSL_R_LENGTH_MISMATCH); |
2215 | 0 | goto err; |
2216 | 0 | } |
2217 | 0 |
|
2218 | 0 | while (PACKET_remaining(&cadns)) { |
2219 | 0 | const unsigned char *namestart, *namebytes; |
2220 | 0 | unsigned int name_len; |
2221 | 0 |
|
2222 | 0 | if (!PACKET_get_net_2(&cadns, &name_len) |
2223 | 0 | || !PACKET_get_bytes(&cadns, &namebytes, name_len)) { |
2224 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES, |
2225 | 0 | SSL_R_LENGTH_MISMATCH); |
2226 | 0 | goto err; |
2227 | 0 | } |
2228 | 0 |
|
2229 | 0 | namestart = namebytes; |
2230 | 0 | if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) { |
2231 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES, |
2232 | 0 | ERR_R_ASN1_LIB); |
2233 | 0 | goto err; |
2234 | 0 | } |
2235 | 0 | if (namebytes != (namestart + name_len)) { |
2236 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES, |
2237 | 0 | SSL_R_CA_DN_LENGTH_MISMATCH); |
2238 | 0 | goto err; |
2239 | 0 | } |
2240 | 0 |
|
2241 | 0 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
2242 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES, |
2243 | 0 | ERR_R_MALLOC_FAILURE); |
2244 | 0 | goto err; |
2245 | 0 | } |
2246 | 0 | xn = NULL; |
2247 | 0 | } |
2248 | 0 |
|
2249 | 0 | sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free); |
2250 | 0 | s->s3->tmp.peer_ca_names = ca_sk; |
2251 | 0 |
|
2252 | 0 | return 1; |
2253 | 0 | |
2254 | 0 | err: |
2255 | 0 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
2256 | 0 | X509_NAME_free(xn); |
2257 | 0 | return 0; |
2258 | 0 | } |
2259 | | |
2260 | | int construct_ca_names(SSL *s, WPACKET *pkt) |
2261 | 0 | { |
2262 | 0 | const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s); |
2263 | 0 |
|
2264 | 0 | /* Start sub-packet for client CA list */ |
2265 | 0 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
2266 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES, |
2267 | 0 | ERR_R_INTERNAL_ERROR); |
2268 | 0 | return 0; |
2269 | 0 | } |
2270 | 0 |
|
2271 | 0 | if (ca_sk != NULL) { |
2272 | 0 | int i; |
2273 | 0 |
|
2274 | 0 | for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) { |
2275 | 0 | unsigned char *namebytes; |
2276 | 0 | X509_NAME *name = sk_X509_NAME_value(ca_sk, i); |
2277 | 0 | int namelen; |
2278 | 0 |
|
2279 | 0 | if (name == NULL |
2280 | 0 | || (namelen = i2d_X509_NAME(name, NULL)) < 0 |
2281 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, namelen, |
2282 | 0 | &namebytes) |
2283 | 0 | || i2d_X509_NAME(name, &namebytes) != namelen) { |
2284 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES, |
2285 | 0 | ERR_R_INTERNAL_ERROR); |
2286 | 0 | return 0; |
2287 | 0 | } |
2288 | 0 | } |
2289 | 0 | } |
2290 | 0 |
|
2291 | 0 | if (!WPACKET_close(pkt)) { |
2292 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES, |
2293 | 0 | ERR_R_INTERNAL_ERROR); |
2294 | 0 | return 0; |
2295 | 0 | } |
2296 | 0 |
|
2297 | 0 | return 1; |
2298 | 0 | } |
2299 | | |
2300 | | /* Create a buffer containing data to be signed for server key exchange */ |
2301 | | size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs, |
2302 | | const void *param, size_t paramlen) |
2303 | 0 | { |
2304 | 0 | size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen; |
2305 | 0 | unsigned char *tbs = OPENSSL_malloc(tbslen); |
2306 | 0 |
|
2307 | 0 | if (tbs == NULL) { |
2308 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS, |
2309 | 0 | ERR_R_MALLOC_FAILURE); |
2310 | 0 | return 0; |
2311 | 0 | } |
2312 | 0 | memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE); |
2313 | 0 | memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE); |
2314 | 0 |
|
2315 | 0 | memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen); |
2316 | 0 |
|
2317 | 0 | *ptbs = tbs; |
2318 | 0 | return tbslen; |
2319 | 0 | } |
2320 | | |
2321 | | /* |
2322 | | * Saves the current handshake digest for Post-Handshake Auth, |
2323 | | * Done after ClientFinished is processed, done exactly once |
2324 | | */ |
2325 | | int tls13_save_handshake_digest_for_pha(SSL *s) |
2326 | 0 | { |
2327 | 0 | if (s->pha_dgst == NULL) { |
2328 | 0 | if (!ssl3_digest_cached_records(s, 1)) |
2329 | 0 | /* SSLfatal() already called */ |
2330 | 0 | return 0; |
2331 | 0 | |
2332 | 0 | s->pha_dgst = EVP_MD_CTX_new(); |
2333 | 0 | if (s->pha_dgst == NULL) { |
2334 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2335 | 0 | SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA, |
2336 | 0 | ERR_R_INTERNAL_ERROR); |
2337 | 0 | return 0; |
2338 | 0 | } |
2339 | 0 | if (!EVP_MD_CTX_copy_ex(s->pha_dgst, |
2340 | 0 | s->s3->handshake_dgst)) { |
2341 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2342 | 0 | SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA, |
2343 | 0 | ERR_R_INTERNAL_ERROR); |
2344 | 0 | return 0; |
2345 | 0 | } |
2346 | 0 | } |
2347 | 0 | return 1; |
2348 | 0 | } |
2349 | | |
2350 | | /* |
2351 | | * Restores the Post-Handshake Auth handshake digest |
2352 | | * Done just before sending/processing the Cert Request |
2353 | | */ |
2354 | | int tls13_restore_handshake_digest_for_pha(SSL *s) |
2355 | 0 | { |
2356 | 0 | if (s->pha_dgst == NULL) { |
2357 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2358 | 0 | SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA, |
2359 | 0 | ERR_R_INTERNAL_ERROR); |
2360 | 0 | return 0; |
2361 | 0 | } |
2362 | 0 | if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst, |
2363 | 0 | s->pha_dgst)) { |
2364 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
2365 | 0 | SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA, |
2366 | 0 | ERR_R_INTERNAL_ERROR); |
2367 | 0 | return 0; |
2368 | 0 | } |
2369 | 0 | return 1; |
2370 | 0 | } |