]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
8c74b5e5 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
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 | |
8c74b5e5 | 8 | */ |
846e33c7 | 9 | |
ea262260 BM |
10 | /* ==================================================================== |
11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
12 | * | |
0f113f3e | 13 | * Portions of the attached software ("Contribution") are developed by |
ea262260 BM |
14 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
15 | * | |
16 | * The Contribution is licensed pursuant to the OpenSSL open source | |
17 | * license provided above. | |
18 | * | |
ea262260 BM |
19 | * ECC cipher suite support in OpenSSL originally written by |
20 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | |
21 | * | |
22 | */ | |
ddac1974 NL |
23 | /* ==================================================================== |
24 | * Copyright 2005 Nokia. All rights reserved. | |
25 | * | |
26 | * The portions of the attached software ("Contribution") is developed by | |
27 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
28 | * license. | |
29 | * | |
30 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
31 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
32 | * support (see RFC 4279) to OpenSSL. | |
33 | * | |
34 | * No patent licenses or other rights except those expressly stated in | |
35 | * the OpenSSL open source license shall be deemed granted or received | |
36 | * expressly, by implication, estoppel, or otherwise. | |
37 | * | |
38 | * No assurances are provided by Nokia that the Contribution does not | |
39 | * infringe the patent or other intellectual property rights of any third | |
40 | * party or that the license provides you with all the necessary rights | |
41 | * to make use of the Contribution. | |
42 | * | |
43 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
44 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
45 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
46 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
47 | * OTHERWISE. | |
48 | */ | |
d02b48c6 RE |
49 | |
50 | #include <stdio.h> | |
fc24f0bf | 51 | #include <time.h> |
8ba708e5 | 52 | #include "../ssl_locl.h" |
61ae935a | 53 | #include "statem_locl.h" |
ec577822 BM |
54 | #include <openssl/buffer.h> |
55 | #include <openssl/rand.h> | |
56 | #include <openssl/objects.h> | |
57 | #include <openssl/evp.h> | |
dbad1690 | 58 | #include <openssl/md5.h> |
3c27208f | 59 | #include <openssl/dh.h> |
d095b68d | 60 | #include <openssl/bn.h> |
3c27208f | 61 | #include <openssl/engine.h> |
f9b3bff6 | 62 | |
3847d426 | 63 | static MSG_PROCESS_RETURN tls_process_hello_retry_request(SSL *s, PACKET *pkt); |
e46f2334 MC |
64 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt); |
65 | ||
7ab09630 | 66 | static ossl_inline int cert_req_allowed(SSL *s); |
a455d0f6 | 67 | static int key_exchange_expected(SSL *s); |
0f113f3e | 68 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); |
d45ba43d | 69 | static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, |
ae2f7b37 | 70 | WPACKET *pkt); |
ea262260 | 71 | |
61ae935a MC |
72 | /* |
73 | * Is a CertificateRequest message allowed at the moment or not? | |
74 | * | |
75 | * Return values are: | |
76 | * 1: Yes | |
77 | * 0: No | |
78 | */ | |
7ab09630 | 79 | static ossl_inline int cert_req_allowed(SSL *s) |
61ae935a MC |
80 | { |
81 | /* TLS does not like anon-DH with client cert */ | |
b7fa1f98 | 82 | if ((s->version > SSL3_VERSION |
a230b26e EK |
83 | && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
84 | || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) | |
61ae935a MC |
85 | return 0; |
86 | ||
87 | return 1; | |
88 | } | |
89 | ||
90 | /* | |
a455d0f6 | 91 | * Should we expect the ServerKeyExchange message or not? |
61ae935a MC |
92 | * |
93 | * Return values are: | |
94 | * 1: Yes | |
95 | * 0: No | |
96 | */ | |
a455d0f6 | 97 | static int key_exchange_expected(SSL *s) |
61ae935a MC |
98 | { |
99 | long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
100 | ||
101 | /* | |
102 | * Can't skip server key exchange if this is an ephemeral | |
a455d0f6 | 103 | * ciphersuite or for SRP |
61ae935a | 104 | */ |
a455d0f6 MC |
105 | if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
106 | | SSL_kSRP)) { | |
107 | return 1; | |
61ae935a MC |
108 | } |
109 | ||
a455d0f6 | 110 | return 0; |
61ae935a MC |
111 | } |
112 | ||
0f1e51ea MC |
113 | /* |
114 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed | |
115 | * handshake state transitions when a TLS1.3 client is reading messages from the | |
116 | * server. The message type that the server has sent is provided in |mt|. The | |
117 | * current state is in |s->statem.hand_state|. | |
118 | * | |
94ed2c67 MC |
119 | * Return values are 1 for success (transition allowed) and 0 on error |
120 | * (transition not allowed) | |
0f1e51ea MC |
121 | */ |
122 | static int ossl_statem_client13_read_transition(SSL *s, int mt) | |
123 | { | |
124 | OSSL_STATEM *st = &s->statem; | |
125 | ||
126 | /* | |
127 | * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't | |
128 | * yet negotiated TLSv1.3 at that point so that is handled by | |
129 | * ossl_statem_client_read_transition() | |
130 | */ | |
131 | ||
132 | switch (st->hand_state) { | |
133 | default: | |
134 | break; | |
135 | ||
3847d426 MC |
136 | case TLS_ST_CW_CLNT_HELLO: |
137 | /* | |
138 | * This must a ClientHello following a HelloRetryRequest, so the only | |
139 | * thing we can get now is a ServerHello. | |
140 | */ | |
141 | if (mt == SSL3_MT_SERVER_HELLO) { | |
142 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
143 | return 1; | |
144 | } | |
145 | break; | |
146 | ||
0f1e51ea | 147 | case TLS_ST_CR_SRVR_HELLO: |
e46f2334 MC |
148 | if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) { |
149 | st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS; | |
150 | return 1; | |
151 | } | |
152 | break; | |
153 | ||
154 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
0f1e51ea | 155 | if (s->hit) { |
92760c21 MC |
156 | if (mt == SSL3_MT_FINISHED) { |
157 | st->hand_state = TLS_ST_CR_FINISHED; | |
0f1e51ea MC |
158 | return 1; |
159 | } | |
160 | } else { | |
92760c21 MC |
161 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
162 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
163 | return 1; | |
f5ca0b04 MC |
164 | } |
165 | if (mt == SSL3_MT_CERTIFICATE) { | |
0f1e51ea MC |
166 | st->hand_state = TLS_ST_CR_CERT; |
167 | return 1; | |
168 | } | |
169 | } | |
170 | break; | |
171 | ||
92760c21 MC |
172 | case TLS_ST_CR_CERT_REQ: |
173 | if (mt == SSL3_MT_CERTIFICATE) { | |
174 | st->hand_state = TLS_ST_CR_CERT; | |
175 | return 1; | |
176 | } | |
177 | break; | |
178 | ||
0f1e51ea | 179 | case TLS_ST_CR_CERT: |
2c5dfdc3 MC |
180 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
181 | st->hand_state = TLS_ST_CR_CERT_VRFY; | |
182 | return 1; | |
183 | } | |
184 | break; | |
185 | ||
186 | case TLS_ST_CR_CERT_VRFY: | |
0f1e51ea MC |
187 | if (mt == SSL3_MT_FINISHED) { |
188 | st->hand_state = TLS_ST_CR_FINISHED; | |
189 | return 1; | |
190 | } | |
191 | break; | |
cc2455bf MC |
192 | |
193 | case TLS_ST_OK: | |
194 | if (mt == SSL3_MT_NEWSESSION_TICKET) { | |
195 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
196 | return 1; | |
197 | } | |
e1c3de44 MC |
198 | if (mt == SSL3_MT_KEY_UPDATE) { |
199 | st->hand_state = TLS_ST_CR_KEY_UPDATE; | |
200 | return 1; | |
201 | } | |
cc2455bf | 202 | break; |
0f1e51ea MC |
203 | } |
204 | ||
0f1e51ea | 205 | /* No valid transition found */ |
0f1e51ea MC |
206 | return 0; |
207 | } | |
208 | ||
61ae935a | 209 | /* |
8481f583 MC |
210 | * ossl_statem_client_read_transition() encapsulates the logic for the allowed |
211 | * handshake state transitions when the client is reading messages from the | |
212 | * server. The message type that the server has sent is provided in |mt|. The | |
213 | * current state is in |s->statem.hand_state|. | |
61ae935a | 214 | * |
94ed2c67 MC |
215 | * Return values are 1 for success (transition allowed) and 0 on error |
216 | * (transition not allowed) | |
61ae935a | 217 | */ |
8481f583 | 218 | int ossl_statem_client_read_transition(SSL *s, int mt) |
61ae935a | 219 | { |
d6f1a6e9 | 220 | OSSL_STATEM *st = &s->statem; |
a455d0f6 | 221 | int ske_expected; |
61ae935a | 222 | |
0f1e51ea | 223 | /* |
3847d426 MC |
224 | * Note that after writing the first ClientHello we don't know what version |
225 | * we are going to negotiate yet, so we don't take this branch until later. | |
0f1e51ea | 226 | */ |
f5ca0b04 | 227 | if (SSL_IS_TLS13(s)) { |
5abeaf35 MC |
228 | if (!ossl_statem_client13_read_transition(s, mt)) |
229 | goto err; | |
230 | return 1; | |
231 | } | |
0f1e51ea | 232 | |
a230b26e | 233 | switch (st->hand_state) { |
f3b3d7f0 RS |
234 | default: |
235 | break; | |
236 | ||
61ae935a MC |
237 | case TLS_ST_CW_CLNT_HELLO: |
238 | if (mt == SSL3_MT_SERVER_HELLO) { | |
239 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
240 | return 1; | |
241 | } | |
242 | ||
243 | if (SSL_IS_DTLS(s)) { | |
244 | if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
245 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
246 | return 1; | |
247 | } | |
3847d426 MC |
248 | } else { |
249 | if (mt == SSL3_MT_HELLO_RETRY_REQUEST) { | |
250 | st->hand_state = TLS_ST_CR_HELLO_RETRY_REQUEST; | |
251 | return 1; | |
252 | } | |
61ae935a MC |
253 | } |
254 | break; | |
255 | ||
d7f8783f | 256 | case TLS_ST_EARLY_DATA: |
4004ce5f MC |
257 | /* |
258 | * We've not actually selected TLSv1.3 yet, but we have sent early | |
259 | * data. The only thing allowed now is a ServerHello or a | |
260 | * HelloRetryRequest. | |
261 | */ | |
262 | if (mt == SSL3_MT_SERVER_HELLO) { | |
263 | st->hand_state = TLS_ST_CR_SRVR_HELLO; | |
264 | return 1; | |
265 | } | |
266 | if (mt == SSL3_MT_HELLO_RETRY_REQUEST) { | |
267 | st->hand_state = TLS_ST_CR_HELLO_RETRY_REQUEST; | |
268 | return 1; | |
269 | } | |
270 | break; | |
271 | ||
61ae935a MC |
272 | case TLS_ST_CR_SRVR_HELLO: |
273 | if (s->hit) { | |
aff8c126 | 274 | if (s->ext.ticket_expected) { |
61ae935a MC |
275 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
276 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
277 | return 1; | |
278 | } | |
279 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
280 | st->hand_state = TLS_ST_CR_CHANGE; | |
281 | return 1; | |
282 | } | |
283 | } else { | |
284 | if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
285 | st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; | |
286 | return 1; | |
ad3819c2 | 287 | } else if (s->version >= TLS1_VERSION |
aff8c126 RS |
288 | && s->ext.session_secret_cb != NULL |
289 | && s->session->ext.tick != NULL | |
a230b26e | 290 | && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
ad3819c2 MC |
291 | /* |
292 | * Normally, we can tell if the server is resuming the session | |
293 | * from the session ID. EAP-FAST (RFC 4851), however, relies on | |
294 | * the next server message after the ServerHello to determine if | |
295 | * the server is resuming. | |
296 | */ | |
297 | s->hit = 1; | |
298 | st->hand_state = TLS_ST_CR_CHANGE; | |
299 | return 1; | |
61ae935a | 300 | } else if (!(s->s3->tmp.new_cipher->algorithm_auth |
a230b26e | 301 | & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
61ae935a MC |
302 | if (mt == SSL3_MT_CERTIFICATE) { |
303 | st->hand_state = TLS_ST_CR_CERT; | |
304 | return 1; | |
305 | } | |
306 | } else { | |
a455d0f6 | 307 | ske_expected = key_exchange_expected(s); |
a455d0f6 MC |
308 | /* SKE is optional for some PSK ciphersuites */ |
309 | if (ske_expected | |
a230b26e EK |
310 | || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
311 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
a455d0f6 MC |
312 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
313 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
314 | return 1; | |
315 | } | |
316 | } else if (mt == SSL3_MT_CERTIFICATE_REQUEST | |
a230b26e EK |
317 | && cert_req_allowed(s)) { |
318 | st->hand_state = TLS_ST_CR_CERT_REQ; | |
319 | return 1; | |
a455d0f6 | 320 | } else if (mt == SSL3_MT_SERVER_DONE) { |
a230b26e EK |
321 | st->hand_state = TLS_ST_CR_SRVR_DONE; |
322 | return 1; | |
61ae935a MC |
323 | } |
324 | } | |
325 | } | |
326 | break; | |
327 | ||
328 | case TLS_ST_CR_CERT: | |
bb1aaab4 MC |
329 | /* |
330 | * The CertificateStatus message is optional even if | |
aff8c126 | 331 | * |ext.status_expected| is set |
bb1aaab4 | 332 | */ |
aff8c126 | 333 | if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) { |
bb1aaab4 MC |
334 | st->hand_state = TLS_ST_CR_CERT_STATUS; |
335 | return 1; | |
a455d0f6 MC |
336 | } |
337 | /* Fall through */ | |
338 | ||
339 | case TLS_ST_CR_CERT_STATUS: | |
340 | ske_expected = key_exchange_expected(s); | |
a455d0f6 | 341 | /* SKE is optional for some PSK ciphersuites */ |
a230b26e EK |
342 | if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK) |
343 | && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { | |
61ae935a MC |
344 | if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
345 | st->hand_state = TLS_ST_CR_KEY_EXCH; | |
346 | return 1; | |
61ae935a | 347 | } |
672f3337 | 348 | goto err; |
61ae935a | 349 | } |
a455d0f6 | 350 | /* Fall through */ |
61ae935a | 351 | |
a455d0f6 MC |
352 | case TLS_ST_CR_KEY_EXCH: |
353 | if (mt == SSL3_MT_CERTIFICATE_REQUEST) { | |
354 | if (cert_req_allowed(s)) { | |
61ae935a MC |
355 | st->hand_state = TLS_ST_CR_CERT_REQ; |
356 | return 1; | |
61ae935a | 357 | } |
672f3337 | 358 | goto err; |
61ae935a | 359 | } |
a455d0f6 | 360 | /* Fall through */ |
61ae935a MC |
361 | |
362 | case TLS_ST_CR_CERT_REQ: | |
363 | if (mt == SSL3_MT_SERVER_DONE) { | |
364 | st->hand_state = TLS_ST_CR_SRVR_DONE; | |
365 | return 1; | |
366 | } | |
367 | break; | |
368 | ||
369 | case TLS_ST_CW_FINISHED: | |
aff8c126 | 370 | if (s->ext.ticket_expected) { |
c45d6b2b DB |
371 | if (mt == SSL3_MT_NEWSESSION_TICKET) { |
372 | st->hand_state = TLS_ST_CR_SESSION_TICKET; | |
373 | return 1; | |
374 | } | |
61ae935a MC |
375 | } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
376 | st->hand_state = TLS_ST_CR_CHANGE; | |
377 | return 1; | |
378 | } | |
379 | break; | |
380 | ||
381 | case TLS_ST_CR_SESSION_TICKET: | |
382 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
383 | st->hand_state = TLS_ST_CR_CHANGE; | |
384 | return 1; | |
385 | } | |
386 | break; | |
387 | ||
388 | case TLS_ST_CR_CHANGE: | |
389 | if (mt == SSL3_MT_FINISHED) { | |
390 | st->hand_state = TLS_ST_CR_FINISHED; | |
391 | return 1; | |
392 | } | |
393 | break; | |
c7f47786 MC |
394 | |
395 | case TLS_ST_OK: | |
4004ce5f | 396 | if (mt == SSL3_MT_HELLO_REQUEST) { |
c7f47786 MC |
397 | st->hand_state = TLS_ST_CR_HELLO_REQ; |
398 | return 1; | |
399 | } | |
400 | break; | |
61ae935a MC |
401 | } |
402 | ||
672f3337 | 403 | err: |
61ae935a | 404 | /* No valid transition found */ |
672f3337 | 405 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE); |
340a2828 | 406 | SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE); |
61ae935a MC |
407 | return 0; |
408 | } | |
409 | ||
410 | /* | |
0f1e51ea MC |
411 | * ossl_statem_client13_write_transition() works out what handshake state to |
412 | * move to next when the TLSv1.3 client is writing messages to be sent to the | |
413 | * server. | |
0f1e51ea MC |
414 | */ |
415 | static WRITE_TRAN ossl_statem_client13_write_transition(SSL *s) | |
416 | { | |
417 | OSSL_STATEM *st = &s->statem; | |
418 | ||
419 | /* | |
3847d426 MC |
420 | * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated |
421 | * TLSv1.3 yet at that point. They are handled by | |
422 | * ossl_statem_client_write_transition(). | |
0f1e51ea MC |
423 | */ |
424 | switch (st->hand_state) { | |
425 | default: | |
426 | /* Shouldn't happen */ | |
427 | return WRITE_TRAN_ERROR; | |
428 | ||
3847d426 MC |
429 | case TLS_ST_CW_CLNT_HELLO: |
430 | /* We only hit this in the case of HelloRetryRequest */ | |
431 | return WRITE_TRAN_FINISHED; | |
432 | ||
433 | case TLS_ST_CR_HELLO_RETRY_REQUEST: | |
434 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
435 | return WRITE_TRAN_CONTINUE; | |
436 | ||
92760c21 | 437 | case TLS_ST_CR_FINISHED: |
ef6c191b MC |
438 | if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY |
439 | || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING) | |
d7f8783f | 440 | st->hand_state = TLS_ST_PENDING_EARLY_DATA_END; |
564547e4 MC |
441 | else |
442 | st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT | |
443 | : TLS_ST_CW_FINISHED; | |
444 | return WRITE_TRAN_CONTINUE; | |
445 | ||
d7f8783f | 446 | case TLS_ST_PENDING_EARLY_DATA_END: |
ef6c191b MC |
447 | if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) { |
448 | st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA; | |
449 | return WRITE_TRAN_CONTINUE; | |
450 | } | |
451 | /* Fall through */ | |
452 | ||
453 | case TLS_ST_CW_END_OF_EARLY_DATA: | |
94ed2c67 | 454 | st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT |
92760c21 | 455 | : TLS_ST_CW_FINISHED; |
0f1e51ea MC |
456 | return WRITE_TRAN_CONTINUE; |
457 | ||
458 | case TLS_ST_CW_CERT: | |
459 | /* If a non-empty Certificate we also send CertificateVerify */ | |
94ed2c67 | 460 | st->hand_state = (s->s3->tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY |
92760c21 | 461 | : TLS_ST_CW_FINISHED; |
0f1e51ea MC |
462 | return WRITE_TRAN_CONTINUE; |
463 | ||
464 | case TLS_ST_CW_CERT_VRFY: | |
0f1e51ea MC |
465 | st->hand_state = TLS_ST_CW_FINISHED; |
466 | return WRITE_TRAN_CONTINUE; | |
467 | ||
e1c3de44 | 468 | case TLS_ST_CR_KEY_UPDATE: |
5bf47933 MC |
469 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
470 | st->hand_state = TLS_ST_CW_KEY_UPDATE; | |
471 | return WRITE_TRAN_CONTINUE; | |
472 | } | |
473 | /* Fall through */ | |
474 | ||
9412b3ad | 475 | case TLS_ST_CW_KEY_UPDATE: |
cc2455bf | 476 | case TLS_ST_CR_SESSION_TICKET: |
0f1e51ea | 477 | case TLS_ST_CW_FINISHED: |
94ed2c67 | 478 | st->hand_state = TLS_ST_OK; |
94ed2c67 | 479 | return WRITE_TRAN_CONTINUE; |
cc2455bf MC |
480 | |
481 | case TLS_ST_OK: | |
9412b3ad MC |
482 | if (s->key_update != SSL_KEY_UPDATE_NONE) { |
483 | st->hand_state = TLS_ST_CW_KEY_UPDATE; | |
484 | return WRITE_TRAN_CONTINUE; | |
485 | } | |
486 | ||
487 | /* Try to read from the server instead */ | |
cc2455bf | 488 | return WRITE_TRAN_FINISHED; |
0f1e51ea MC |
489 | } |
490 | } | |
491 | ||
492 | /* | |
493 | * ossl_statem_client_write_transition() works out what handshake state to | |
494 | * move to next when the client is writing messages to be sent to the server. | |
61ae935a | 495 | */ |
8481f583 | 496 | WRITE_TRAN ossl_statem_client_write_transition(SSL *s) |
61ae935a | 497 | { |
d6f1a6e9 | 498 | OSSL_STATEM *st = &s->statem; |
61ae935a | 499 | |
0f1e51ea MC |
500 | /* |
501 | * Note that immediately before/after a ClientHello we don't know what | |
502 | * version we are going to negotiate yet, so we don't take this branch until | |
503 | * later | |
504 | */ | |
f5ca0b04 | 505 | if (SSL_IS_TLS13(s)) |
0f1e51ea MC |
506 | return ossl_statem_client13_write_transition(s); |
507 | ||
a230b26e | 508 | switch (st->hand_state) { |
f3b3d7f0 RS |
509 | default: |
510 | /* Shouldn't happen */ | |
511 | return WRITE_TRAN_ERROR; | |
512 | ||
a230b26e | 513 | case TLS_ST_OK: |
c7f47786 MC |
514 | if (!s->renegotiate) { |
515 | /* | |
516 | * We haven't requested a renegotiation ourselves so we must have | |
517 | * received a message from the server. Better read it. | |
518 | */ | |
519 | return WRITE_TRAN_FINISHED; | |
520 | } | |
a230b26e EK |
521 | /* Renegotiation - fall through */ |
522 | case TLS_ST_BEFORE: | |
523 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
524 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 525 | |
a230b26e | 526 | case TLS_ST_CW_CLNT_HELLO: |
49e7fe12 MC |
527 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { |
528 | /* | |
529 | * We are assuming this is a TLSv1.3 connection, although we haven't | |
530 | * actually selected a version yet. | |
531 | */ | |
d7f8783f | 532 | st->hand_state = TLS_ST_EARLY_DATA; |
49e7fe12 MC |
533 | return WRITE_TRAN_CONTINUE; |
534 | } | |
a230b26e EK |
535 | /* |
536 | * No transition at the end of writing because we don't know what | |
537 | * we will be sent | |
538 | */ | |
539 | return WRITE_TRAN_FINISHED; | |
61ae935a | 540 | |
d7f8783f | 541 | case TLS_ST_EARLY_DATA: |
4004ce5f MC |
542 | return WRITE_TRAN_FINISHED; |
543 | ||
a230b26e EK |
544 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
545 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
546 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 547 | |
a230b26e EK |
548 | case TLS_ST_CR_SRVR_DONE: |
549 | if (s->s3->tmp.cert_req) | |
550 | st->hand_state = TLS_ST_CW_CERT; | |
551 | else | |
61ae935a | 552 | st->hand_state = TLS_ST_CW_KEY_EXCH; |
a230b26e | 553 | return WRITE_TRAN_CONTINUE; |
61ae935a | 554 | |
a230b26e EK |
555 | case TLS_ST_CW_CERT: |
556 | st->hand_state = TLS_ST_CW_KEY_EXCH; | |
557 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 558 | |
a230b26e EK |
559 | case TLS_ST_CW_KEY_EXCH: |
560 | /* | |
561 | * For TLS, cert_req is set to 2, so a cert chain of nothing is | |
562 | * sent, but no verify packet is sent | |
563 | */ | |
564 | /* | |
565 | * XXX: For now, we do not support client authentication in ECDH | |
566 | * cipher suites with ECDH (rather than ECDSA) certificates. We | |
567 | * need to skip the certificate verify message when client's | |
568 | * ECDH public key is sent inside the client certificate. | |
569 | */ | |
570 | if (s->s3->tmp.cert_req == 1) { | |
571 | st->hand_state = TLS_ST_CW_CERT_VRFY; | |
572 | } else { | |
61ae935a | 573 | st->hand_state = TLS_ST_CW_CHANGE; |
a230b26e EK |
574 | } |
575 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | |
576 | st->hand_state = TLS_ST_CW_CHANGE; | |
577 | } | |
578 | return WRITE_TRAN_CONTINUE; | |
61ae935a | 579 | |
a230b26e EK |
580 | case TLS_ST_CW_CERT_VRFY: |
581 | st->hand_state = TLS_ST_CW_CHANGE; | |
582 | return WRITE_TRAN_CONTINUE; | |
583 | ||
584 | case TLS_ST_CW_CHANGE: | |
61ae935a | 585 | #if defined(OPENSSL_NO_NEXTPROTONEG) |
4004ce5f MC |
586 | st-> |
587 | hand_state = TLS_ST_CW_FINISHED; | |
61ae935a | 588 | #else |
aff8c126 | 589 | if (!SSL_IS_DTLS(s) && s->s3->npn_seen) |
a230b26e EK |
590 | st->hand_state = TLS_ST_CW_NEXT_PROTO; |
591 | else | |
592 | st->hand_state = TLS_ST_CW_FINISHED; | |
61ae935a | 593 | #endif |
a230b26e | 594 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
595 | |
596 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
a230b26e EK |
597 | case TLS_ST_CW_NEXT_PROTO: |
598 | st->hand_state = TLS_ST_CW_FINISHED; | |
599 | return WRITE_TRAN_CONTINUE; | |
61ae935a MC |
600 | #endif |
601 | ||
a230b26e EK |
602 | case TLS_ST_CW_FINISHED: |
603 | if (s->hit) { | |
604 | st->hand_state = TLS_ST_OK; | |
a230b26e EK |
605 | return WRITE_TRAN_CONTINUE; |
606 | } else { | |
607 | return WRITE_TRAN_FINISHED; | |
608 | } | |
61ae935a | 609 | |
a230b26e EK |
610 | case TLS_ST_CR_FINISHED: |
611 | if (s->hit) { | |
612 | st->hand_state = TLS_ST_CW_CHANGE; | |
613 | return WRITE_TRAN_CONTINUE; | |
614 | } else { | |
615 | st->hand_state = TLS_ST_OK; | |
a230b26e EK |
616 | return WRITE_TRAN_CONTINUE; |
617 | } | |
c7f47786 MC |
618 | |
619 | case TLS_ST_CR_HELLO_REQ: | |
620 | /* | |
621 | * If we can renegotiate now then do so, otherwise wait for a more | |
622 | * convenient time. | |
623 | */ | |
624 | if (ssl3_renegotiate_check(s, 1)) { | |
625 | if (!tls_setup_handshake(s)) { | |
626 | ossl_statem_set_error(s); | |
627 | return WRITE_TRAN_ERROR; | |
628 | } | |
629 | st->hand_state = TLS_ST_CW_CLNT_HELLO; | |
630 | return WRITE_TRAN_CONTINUE; | |
631 | } | |
632 | st->hand_state = TLS_ST_OK; | |
c7f47786 | 633 | return WRITE_TRAN_CONTINUE; |
61ae935a MC |
634 | } |
635 | } | |
636 | ||
637 | /* | |
638 | * Perform any pre work that needs to be done prior to sending a message from | |
639 | * the client to the server. | |
640 | */ | |
8481f583 | 641 | WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst) |
61ae935a | 642 | { |
d6f1a6e9 | 643 | OSSL_STATEM *st = &s->statem; |
61ae935a | 644 | |
a230b26e | 645 | switch (st->hand_state) { |
f3b3d7f0 RS |
646 | default: |
647 | /* No pre work to be done */ | |
648 | break; | |
649 | ||
61ae935a MC |
650 | case TLS_ST_CW_CLNT_HELLO: |
651 | s->shutdown = 0; | |
652 | if (SSL_IS_DTLS(s)) { | |
653 | /* every DTLS ClientHello resets Finished MAC */ | |
2c4a056f MC |
654 | if (!ssl3_init_finished_mac(s)) { |
655 | ossl_statem_set_error(s); | |
656 | return WORK_ERROR; | |
657 | } | |
61ae935a MC |
658 | } |
659 | break; | |
660 | ||
61ae935a MC |
661 | case TLS_ST_CW_CHANGE: |
662 | if (SSL_IS_DTLS(s)) { | |
663 | if (s->hit) { | |
664 | /* | |
665 | * We're into the last flight so we don't retransmit these | |
666 | * messages unless we need to. | |
667 | */ | |
668 | st->use_timer = 0; | |
669 | } | |
670 | #ifndef OPENSSL_NO_SCTP | |
671 | if (BIO_dgram_is_sctp(SSL_get_wbio(s))) | |
672 | return dtls_wait_for_dry(s); | |
673 | #endif | |
674 | } | |
f3b3d7f0 | 675 | break; |
61ae935a | 676 | |
d7f8783f | 677 | case TLS_ST_PENDING_EARLY_DATA_END: |
ef6c191b MC |
678 | /* |
679 | * If we've been called by SSL_do_handshake()/SSL_write(), or we did not | |
680 | * attempt to write early data before calling SSL_read() then we press | |
681 | * on with the handshake. Otherwise we pause here. | |
682 | */ | |
683 | if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING | |
684 | || s->early_data_state == SSL_EARLY_DATA_NONE) | |
685 | return WORK_FINISHED_CONTINUE; | |
686 | /* Fall through */ | |
687 | ||
688 | case TLS_ST_EARLY_DATA: | |
61ae935a | 689 | case TLS_ST_OK: |
30f05b19 | 690 | return tls_finish_handshake(s, wst, 1); |
61ae935a MC |
691 | } |
692 | ||
693 | return WORK_FINISHED_CONTINUE; | |
694 | } | |
695 | ||
696 | /* | |
697 | * Perform any work that needs to be done after sending a message from the | |
698 | * client to the server. | |
699 | */ | |
8481f583 | 700 | WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst) |
61ae935a | 701 | { |
d6f1a6e9 | 702 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
703 | |
704 | s->init_num = 0; | |
705 | ||
a230b26e | 706 | switch (st->hand_state) { |
f3b3d7f0 RS |
707 | default: |
708 | /* No post work to be done */ | |
709 | break; | |
710 | ||
61ae935a | 711 | case TLS_ST_CW_CLNT_HELLO: |
46417569 | 712 | if (wst == WORK_MORE_A && statem_flush(s) != 1) |
61ae935a | 713 | return WORK_MORE_A; |
46417569 | 714 | |
61ae935a MC |
715 | if (SSL_IS_DTLS(s)) { |
716 | /* Treat the next message as the first packet */ | |
717 | s->first_packet = 1; | |
718 | } | |
6cb42265 MC |
719 | |
720 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING | |
721 | && s->max_early_data > 0) { | |
722 | /* | |
723 | * We haven't selected TLSv1.3 yet so we don't call the change | |
724 | * cipher state function associated with the SSL_METHOD. Instead | |
725 | * we call tls13_change_cipher_state() directly. | |
726 | */ | |
727 | if (!tls13_change_cipher_state(s, | |
728 | SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
1ea4d09a | 729 | return WORK_ERROR; |
6cb42265 | 730 | } |
61ae935a MC |
731 | break; |
732 | ||
ef6c191b MC |
733 | case TLS_ST_CW_END_OF_EARLY_DATA: |
734 | /* | |
735 | * We set the enc_write_ctx back to NULL because we may end up writing | |
736 | * in cleartext again if we get a HelloRetryRequest from the server. | |
737 | */ | |
738 | EVP_CIPHER_CTX_free(s->enc_write_ctx); | |
739 | s->enc_write_ctx = NULL; | |
740 | break; | |
741 | ||
61ae935a MC |
742 | case TLS_ST_CW_KEY_EXCH: |
743 | if (tls_client_key_exchange_post_work(s) == 0) | |
744 | return WORK_ERROR; | |
745 | break; | |
746 | ||
747 | case TLS_ST_CW_CHANGE: | |
748 | s->session->cipher = s->s3->tmp.new_cipher; | |
749 | #ifdef OPENSSL_NO_COMP | |
750 | s->session->compress_meth = 0; | |
751 | #else | |
752 | if (s->s3->tmp.new_compression == NULL) | |
753 | s->session->compress_meth = 0; | |
754 | else | |
755 | s->session->compress_meth = s->s3->tmp.new_compression->id; | |
756 | #endif | |
757 | if (!s->method->ssl3_enc->setup_key_block(s)) | |
758 | return WORK_ERROR; | |
759 | ||
760 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
761 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
762 | return WORK_ERROR; | |
763 | ||
764 | if (SSL_IS_DTLS(s)) { | |
765 | #ifndef OPENSSL_NO_SCTP | |
766 | if (s->hit) { | |
767 | /* | |
768 | * Change to new shared key of SCTP-Auth, will be ignored if | |
769 | * no SCTP used. | |
770 | */ | |
771 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
772 | 0, NULL); | |
773 | } | |
774 | #endif | |
775 | ||
776 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
777 | } | |
778 | break; | |
779 | ||
780 | case TLS_ST_CW_FINISHED: | |
781 | #ifndef OPENSSL_NO_SCTP | |
782 | if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) { | |
783 | /* | |
784 | * Change to new shared key of SCTP-Auth, will be ignored if | |
785 | * no SCTP used. | |
786 | */ | |
787 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
788 | 0, NULL); | |
789 | } | |
790 | #endif | |
791 | if (statem_flush(s) != 1) | |
792 | return WORK_MORE_B; | |
92760c21 MC |
793 | |
794 | if (SSL_IS_TLS13(s)) { | |
795 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
796 | SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
797 | return WORK_ERROR; | |
798 | } | |
61ae935a | 799 | break; |
9412b3ad MC |
800 | |
801 | case TLS_ST_CW_KEY_UPDATE: | |
802 | if (statem_flush(s) != 1) | |
803 | return WORK_MORE_A; | |
57389a32 MC |
804 | if (!tls13_update_key(s, 1)) |
805 | return WORK_ERROR; | |
9412b3ad | 806 | break; |
61ae935a MC |
807 | } |
808 | ||
809 | return WORK_FINISHED_CONTINUE; | |
810 | } | |
811 | ||
812 | /* | |
6392fb8e MC |
813 | * Get the message construction function and message type for sending from the |
814 | * client | |
61ae935a MC |
815 | * |
816 | * Valid return values are: | |
817 | * 1: Success | |
818 | * 0: Error | |
819 | */ | |
6392fb8e | 820 | int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt, |
a15c953f | 821 | confunc_f *confunc, int *mt) |
61ae935a | 822 | { |
d6f1a6e9 | 823 | OSSL_STATEM *st = &s->statem; |
61ae935a | 824 | |
4a01c59f MC |
825 | switch (st->hand_state) { |
826 | default: | |
827 | /* Shouldn't happen */ | |
828 | return 0; | |
829 | ||
830 | case TLS_ST_CW_CHANGE: | |
5923ad4b | 831 | if (SSL_IS_DTLS(s)) |
6392fb8e | 832 | *confunc = dtls_construct_change_cipher_spec; |
4a01c59f | 833 | else |
6392fb8e MC |
834 | *confunc = tls_construct_change_cipher_spec; |
835 | *mt = SSL3_MT_CHANGE_CIPHER_SPEC; | |
4a01c59f MC |
836 | break; |
837 | ||
838 | case TLS_ST_CW_CLNT_HELLO: | |
6392fb8e MC |
839 | *confunc = tls_construct_client_hello; |
840 | *mt = SSL3_MT_CLIENT_HELLO; | |
4a01c59f MC |
841 | break; |
842 | ||
ef6c191b MC |
843 | case TLS_ST_CW_END_OF_EARLY_DATA: |
844 | *confunc = tls_construct_end_of_early_data; | |
845 | *mt = SSL3_MT_END_OF_EARLY_DATA; | |
846 | break; | |
847 | ||
848 | case TLS_ST_PENDING_EARLY_DATA_END: | |
849 | *confunc = NULL; | |
850 | *mt = SSL3_MT_DUMMY; | |
851 | break; | |
852 | ||
4a01c59f | 853 | case TLS_ST_CW_CERT: |
6392fb8e MC |
854 | *confunc = tls_construct_client_certificate; |
855 | *mt = SSL3_MT_CERTIFICATE; | |
4a01c59f MC |
856 | break; |
857 | ||
858 | case TLS_ST_CW_KEY_EXCH: | |
6392fb8e MC |
859 | *confunc = tls_construct_client_key_exchange; |
860 | *mt = SSL3_MT_CLIENT_KEY_EXCHANGE; | |
4a01c59f MC |
861 | break; |
862 | ||
863 | case TLS_ST_CW_CERT_VRFY: | |
d8bc1399 | 864 | *confunc = tls_construct_cert_verify; |
6392fb8e | 865 | *mt = SSL3_MT_CERTIFICATE_VERIFY; |
4a01c59f | 866 | break; |
61ae935a MC |
867 | |
868 | #if !defined(OPENSSL_NO_NEXTPROTONEG) | |
4a01c59f | 869 | case TLS_ST_CW_NEXT_PROTO: |
6392fb8e MC |
870 | *confunc = tls_construct_next_proto; |
871 | *mt = SSL3_MT_NEXT_PROTO; | |
4a01c59f | 872 | break; |
61ae935a | 873 | #endif |
4a01c59f | 874 | case TLS_ST_CW_FINISHED: |
6392fb8e MC |
875 | *confunc = tls_construct_finished; |
876 | *mt = SSL3_MT_FINISHED; | |
4a01c59f | 877 | break; |
9412b3ad MC |
878 | |
879 | case TLS_ST_CW_KEY_UPDATE: | |
880 | *confunc = tls_construct_key_update; | |
881 | *mt = SSL3_MT_KEY_UPDATE; | |
882 | break; | |
4a01c59f | 883 | } |
5923ad4b | 884 | |
5923ad4b | 885 | return 1; |
61ae935a MC |
886 | } |
887 | ||
888 | /* | |
889 | * Returns the maximum allowed length for the current message that we are | |
890 | * reading. Excludes the message header. | |
891 | */ | |
eda75751 | 892 | size_t ossl_statem_client_max_message_size(SSL *s) |
61ae935a | 893 | { |
d6f1a6e9 | 894 | OSSL_STATEM *st = &s->statem; |
61ae935a | 895 | |
a230b26e | 896 | switch (st->hand_state) { |
f3b3d7f0 RS |
897 | default: |
898 | /* Shouldn't happen */ | |
899 | return 0; | |
900 | ||
a230b26e EK |
901 | case TLS_ST_CR_SRVR_HELLO: |
902 | return SERVER_HELLO_MAX_LENGTH; | |
61ae935a | 903 | |
a230b26e EK |
904 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
905 | return HELLO_VERIFY_REQUEST_MAX_LENGTH; | |
61ae935a | 906 | |
3847d426 MC |
907 | case TLS_ST_CR_HELLO_RETRY_REQUEST: |
908 | return HELLO_RETRY_REQUEST_MAX_LENGTH; | |
909 | ||
a230b26e EK |
910 | case TLS_ST_CR_CERT: |
911 | return s->max_cert_list; | |
61ae935a | 912 | |
2c5dfdc3 MC |
913 | case TLS_ST_CR_CERT_VRFY: |
914 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
915 | ||
a230b26e EK |
916 | case TLS_ST_CR_CERT_STATUS: |
917 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
61ae935a | 918 | |
a230b26e EK |
919 | case TLS_ST_CR_KEY_EXCH: |
920 | return SERVER_KEY_EXCH_MAX_LENGTH; | |
61ae935a | 921 | |
a230b26e EK |
922 | case TLS_ST_CR_CERT_REQ: |
923 | /* | |
924 | * Set to s->max_cert_list for compatibility with previous releases. In | |
925 | * practice these messages can get quite long if servers are configured | |
926 | * to provide a long list of acceptable CAs | |
927 | */ | |
928 | return s->max_cert_list; | |
61ae935a | 929 | |
a230b26e EK |
930 | case TLS_ST_CR_SRVR_DONE: |
931 | return SERVER_HELLO_DONE_MAX_LENGTH; | |
61ae935a | 932 | |
a230b26e EK |
933 | case TLS_ST_CR_CHANGE: |
934 | if (s->version == DTLS1_BAD_VER) | |
935 | return 3; | |
936 | return CCS_MAX_LENGTH; | |
61ae935a | 937 | |
a230b26e EK |
938 | case TLS_ST_CR_SESSION_TICKET: |
939 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
61ae935a | 940 | |
a230b26e EK |
941 | case TLS_ST_CR_FINISHED: |
942 | return FINISHED_MAX_LENGTH; | |
e46f2334 MC |
943 | |
944 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: | |
945 | return ENCRYPTED_EXTENSIONS_MAX_LENGTH; | |
e1c3de44 MC |
946 | |
947 | case TLS_ST_CR_KEY_UPDATE: | |
948 | return KEY_UPDATE_MAX_LENGTH; | |
61ae935a | 949 | } |
61ae935a MC |
950 | } |
951 | ||
952 | /* | |
953 | * Process a message that the client has been received from the server. | |
954 | */ | |
8481f583 | 955 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt) |
61ae935a | 956 | { |
d6f1a6e9 | 957 | OSSL_STATEM *st = &s->statem; |
61ae935a | 958 | |
a230b26e | 959 | switch (st->hand_state) { |
f3b3d7f0 RS |
960 | default: |
961 | /* Shouldn't happen */ | |
962 | return MSG_PROCESS_ERROR; | |
963 | ||
a230b26e EK |
964 | case TLS_ST_CR_SRVR_HELLO: |
965 | return tls_process_server_hello(s, pkt); | |
61ae935a | 966 | |
a230b26e EK |
967 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
968 | return dtls_process_hello_verify(s, pkt); | |
61ae935a | 969 | |
3847d426 MC |
970 | case TLS_ST_CR_HELLO_RETRY_REQUEST: |
971 | return tls_process_hello_retry_request(s, pkt); | |
972 | ||
a230b26e EK |
973 | case TLS_ST_CR_CERT: |
974 | return tls_process_server_certificate(s, pkt); | |
61ae935a | 975 | |
2c5dfdc3 MC |
976 | case TLS_ST_CR_CERT_VRFY: |
977 | return tls_process_cert_verify(s, pkt); | |
978 | ||
a230b26e EK |
979 | case TLS_ST_CR_CERT_STATUS: |
980 | return tls_process_cert_status(s, pkt); | |
61ae935a | 981 | |
a230b26e EK |
982 | case TLS_ST_CR_KEY_EXCH: |
983 | return tls_process_key_exchange(s, pkt); | |
61ae935a | 984 | |
a230b26e EK |
985 | case TLS_ST_CR_CERT_REQ: |
986 | return tls_process_certificate_request(s, pkt); | |
61ae935a | 987 | |
a230b26e EK |
988 | case TLS_ST_CR_SRVR_DONE: |
989 | return tls_process_server_done(s, pkt); | |
61ae935a | 990 | |
a230b26e EK |
991 | case TLS_ST_CR_CHANGE: |
992 | return tls_process_change_cipher_spec(s, pkt); | |
61ae935a | 993 | |
a230b26e EK |
994 | case TLS_ST_CR_SESSION_TICKET: |
995 | return tls_process_new_session_ticket(s, pkt); | |
61ae935a | 996 | |
a230b26e EK |
997 | case TLS_ST_CR_FINISHED: |
998 | return tls_process_finished(s, pkt); | |
e46f2334 | 999 | |
c7f47786 MC |
1000 | case TLS_ST_CR_HELLO_REQ: |
1001 | return tls_process_hello_req(s, pkt); | |
1002 | ||
e46f2334 MC |
1003 | case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
1004 | return tls_process_encrypted_extensions(s, pkt); | |
e1c3de44 MC |
1005 | |
1006 | case TLS_ST_CR_KEY_UPDATE: | |
1007 | return tls_process_key_update(s, pkt); | |
61ae935a | 1008 | } |
61ae935a MC |
1009 | } |
1010 | ||
1011 | /* | |
1012 | * Perform any further processing required following the receipt of a message | |
1013 | * from the server | |
1014 | */ | |
8481f583 | 1015 | WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst) |
61ae935a | 1016 | { |
d6f1a6e9 | 1017 | OSSL_STATEM *st = &s->statem; |
61ae935a | 1018 | |
a230b26e | 1019 | switch (st->hand_state) { |
f3b3d7f0 RS |
1020 | default: |
1021 | /* Shouldn't happen */ | |
1022 | return WORK_ERROR; | |
1023 | ||
05c4f1d5 MC |
1024 | case TLS_ST_CR_CERT_REQ: |
1025 | return tls_prepare_client_certificate(s, wst); | |
1026 | ||
61ae935a MC |
1027 | #ifndef OPENSSL_NO_SCTP |
1028 | case TLS_ST_CR_SRVR_DONE: | |
1029 | /* We only get here if we are using SCTP and we are renegotiating */ | |
1030 | if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
1031 | s->s3->in_read_app_data = 2; | |
1032 | s->rwstate = SSL_READING; | |
1033 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
1034 | BIO_set_retry_read(SSL_get_rbio(s)); | |
fe3a3291 | 1035 | ossl_statem_set_sctp_read_sock(s, 1); |
61ae935a MC |
1036 | return WORK_MORE_A; |
1037 | } | |
fe3a3291 | 1038 | ossl_statem_set_sctp_read_sock(s, 0); |
61ae935a MC |
1039 | return WORK_FINISHED_STOP; |
1040 | #endif | |
61ae935a | 1041 | } |
61ae935a MC |
1042 | } |
1043 | ||
7cea05dc | 1044 | int tls_construct_client_hello(SSL *s, WPACKET *pkt) |
0f113f3e | 1045 | { |
2c7b4dbc | 1046 | unsigned char *p; |
ec60ccc1 MC |
1047 | size_t sess_id_len; |
1048 | int i, protverr; | |
2c7b4dbc | 1049 | int al = SSL_AD_HANDSHAKE_FAILURE; |
09b6c2ef | 1050 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
1051 | SSL_COMP *comp; |
1052 | #endif | |
b9908bf9 | 1053 | SSL_SESSION *sess = s->session; |
0f113f3e | 1054 | |
7cea05dc | 1055 | if (!WPACKET_set_max_size(pkt, SSL3_RT_MAX_PLAIN_LENGTH)) { |
2c7b4dbc MC |
1056 | /* Should not happen */ |
1057 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
7cea05dc | 1058 | return 0; |
2c7b4dbc | 1059 | } |
0f113f3e | 1060 | |
b9908bf9 | 1061 | /* Work out what SSL/TLS/DTLS version to use */ |
4fa52141 VD |
1062 | protverr = ssl_set_client_hello_version(s); |
1063 | if (protverr != 0) { | |
1064 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr); | |
7cea05dc | 1065 | return 0; |
4fa52141 | 1066 | } |
0f113f3e | 1067 | |
a230b26e | 1068 | if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) || |
0f113f3e | 1069 | /* |
b9908bf9 MC |
1070 | * In the case of EAP-FAST, we can have a pre-shared |
1071 | * "ticket" without a session ID. | |
0f113f3e | 1072 | */ |
aff8c126 | 1073 | (!sess->session_id_length && !sess->ext.tick) || |
b9908bf9 MC |
1074 | (sess->not_resumable)) { |
1075 | if (!ssl_get_new_session(s, 0)) | |
7cea05dc | 1076 | return 0; |
b9908bf9 MC |
1077 | } |
1078 | /* else use the pre-loaded session */ | |
0f113f3e | 1079 | |
b9908bf9 | 1080 | p = s->s3->client_random; |
0f113f3e | 1081 | |
b9908bf9 MC |
1082 | /* |
1083 | * for DTLS if client_random is initialized, reuse it, we are | |
1084 | * required to use same upon reply to HelloVerify | |
1085 | */ | |
1086 | if (SSL_IS_DTLS(s)) { | |
1087 | size_t idx; | |
1088 | i = 1; | |
1089 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { | |
1090 | if (p[idx]) { | |
1091 | i = 0; | |
1092 | break; | |
0f113f3e | 1093 | } |
0f113f3e | 1094 | } |
b9908bf9 MC |
1095 | } else |
1096 | i = 1; | |
0f113f3e | 1097 | |
a230b26e | 1098 | if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0) |
7cea05dc | 1099 | return 0; |
b9908bf9 | 1100 | |
b9908bf9 MC |
1101 | /*- |
1102 | * version indicates the negotiated version: for example from | |
1103 | * an SSLv2/v3 compatible client hello). The client_version | |
1104 | * field is the maximum version we permit and it is also | |
1105 | * used in RSA encrypted premaster secrets. Some servers can | |
1106 | * choke if we initially report a higher version then | |
1107 | * renegotiate to a lower one in the premaster secret. This | |
1108 | * didn't happen with TLS 1.0 as most servers supported it | |
1109 | * but it can with TLS 1.1 or later if the server only supports | |
1110 | * 1.0. | |
1111 | * | |
1112 | * Possible scenario with previous logic: | |
1113 | * 1. Client hello indicates TLS 1.2 | |
1114 | * 2. Server hello says TLS 1.0 | |
1115 | * 3. RSA encrypted premaster secret uses 1.2. | |
8483a003 | 1116 | * 4. Handshake proceeds using TLS 1.0. |
b9908bf9 MC |
1117 | * 5. Server sends hello request to renegotiate. |
1118 | * 6. Client hello indicates TLS v1.0 as we now | |
1119 | * know that is maximum server supports. | |
1120 | * 7. Server chokes on RSA encrypted premaster secret | |
1121 | * containing version 1.0. | |
1122 | * | |
1123 | * For interoperability it should be OK to always use the | |
1124 | * maximum version we support in client hello and then rely | |
1125 | * on the checking of version to ensure the servers isn't | |
1126 | * being inconsistent: for example initially negotiating with | |
1127 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
1128 | * client_version in client hello and not resetting it to | |
1129 | * the negotiated version. | |
cd998837 MC |
1130 | * |
1131 | * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the | |
16bce0e0 | 1132 | * supported_versions extension for the real supported versions. |
b9908bf9 | 1133 | */ |
7acb8b64 | 1134 | if (!WPACKET_put_bytes_u16(pkt, s->client_version) |
7cea05dc | 1135 | || !WPACKET_memcpy(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)) { |
2c7b4dbc | 1136 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1137 | return 0; |
2c7b4dbc | 1138 | } |
b9908bf9 MC |
1139 | |
1140 | /* Session ID */ | |
f05bcf0f | 1141 | if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) |
ec60ccc1 | 1142 | sess_id_len = 0; |
b9908bf9 | 1143 | else |
ec60ccc1 MC |
1144 | sess_id_len = s->session->session_id_length; |
1145 | if (sess_id_len > sizeof(s->session->session_id) | |
7cea05dc | 1146 | || !WPACKET_start_sub_packet_u8(pkt) |
ec60ccc1 MC |
1147 | || (sess_id_len != 0 && !WPACKET_memcpy(pkt, s->session->session_id, |
1148 | sess_id_len)) | |
7cea05dc | 1149 | || !WPACKET_close(pkt)) { |
2c7b4dbc | 1150 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1151 | return 0; |
b9908bf9 | 1152 | } |
0f113f3e | 1153 | |
b9908bf9 MC |
1154 | /* cookie stuff for DTLS */ |
1155 | if (SSL_IS_DTLS(s)) { | |
2c7b4dbc | 1156 | if (s->d1->cookie_len > sizeof(s->d1->cookie) |
7cea05dc | 1157 | || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie, |
b2b3024e | 1158 | s->d1->cookie_len)) { |
b9908bf9 | 1159 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1160 | return 0; |
0f113f3e | 1161 | } |
b9908bf9 MC |
1162 | } |
1163 | ||
1164 | /* Ciphers supported */ | |
7cea05dc | 1165 | if (!WPACKET_start_sub_packet_u16(pkt)) { |
2c7b4dbc | 1166 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1167 | return 0; |
2c7b4dbc MC |
1168 | } |
1169 | /* ssl_cipher_list_to_bytes() raises SSLerr if appropriate */ | |
7cea05dc MC |
1170 | if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), pkt)) |
1171 | return 0; | |
1172 | if (!WPACKET_close(pkt)) { | |
2c7b4dbc | 1173 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1174 | return 0; |
b9908bf9 | 1175 | } |
0f113f3e | 1176 | |
b9908bf9 | 1177 | /* COMPRESSION */ |
7cea05dc | 1178 | if (!WPACKET_start_sub_packet_u8(pkt)) { |
2c7b4dbc | 1179 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1180 | return 0; |
2c7b4dbc MC |
1181 | } |
1182 | #ifndef OPENSSL_NO_COMP | |
c19602b5 MC |
1183 | if (ssl_allow_compression(s) |
1184 | && s->ctx->comp_methods | |
1185 | && (SSL_IS_DTLS(s) || s->s3->tmp.max_ver < TLS1_3_VERSION)) { | |
2c7b4dbc MC |
1186 | int compnum = sk_SSL_COMP_num(s->ctx->comp_methods); |
1187 | for (i = 0; i < compnum; i++) { | |
1188 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); | |
7cea05dc | 1189 | if (!WPACKET_put_bytes_u8(pkt, comp->id)) { |
2c7b4dbc | 1190 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1191 | return 0; |
2c7b4dbc MC |
1192 | } |
1193 | } | |
b9908bf9 | 1194 | } |
09b6c2ef | 1195 | #endif |
2c7b4dbc | 1196 | /* Add the NULL method */ |
7cea05dc | 1197 | if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { |
2c7b4dbc | 1198 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
7cea05dc | 1199 | return 0; |
2c7b4dbc | 1200 | } |
761772d7 | 1201 | |
b9908bf9 | 1202 | /* TLS extensions */ |
30aeba43 | 1203 | if (!tls_construct_extensions(s, pkt, EXT_CLIENT_HELLO, NULL, 0, &al)) { |
b9908bf9 MC |
1204 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
1205 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
7cea05dc | 1206 | return 0; |
b9908bf9 | 1207 | } |
0f113f3e | 1208 | |
b9908bf9 | 1209 | return 1; |
0f113f3e | 1210 | } |
d02b48c6 | 1211 | |
be3583fa | 1212 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt) |
8ba708e5 MC |
1213 | { |
1214 | int al; | |
cb150cbc | 1215 | size_t cookie_len; |
8ba708e5 MC |
1216 | PACKET cookiepkt; |
1217 | ||
1218 | if (!PACKET_forward(pkt, 2) | |
a230b26e | 1219 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { |
8ba708e5 MC |
1220 | al = SSL_AD_DECODE_ERROR; |
1221 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1222 | goto f_err; | |
1223 | } | |
1224 | ||
1225 | cookie_len = PACKET_remaining(&cookiepkt); | |
1226 | if (cookie_len > sizeof(s->d1->cookie)) { | |
1227 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1228 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG); | |
1229 | goto f_err; | |
1230 | } | |
1231 | ||
1232 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { | |
1233 | al = SSL_AD_DECODE_ERROR; | |
1234 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
1235 | goto f_err; | |
1236 | } | |
1237 | s->d1->cookie_len = cookie_len; | |
1238 | ||
1239 | return MSG_PROCESS_FINISHED_READING; | |
1240 | f_err: | |
1241 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1242 | ossl_statem_set_error(s); |
8ba708e5 MC |
1243 | return MSG_PROCESS_ERROR; |
1244 | } | |
1245 | ||
be3583fa | 1246 | MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1247 | { |
1248 | STACK_OF(SSL_CIPHER) *sk; | |
1249 | const SSL_CIPHER *c; | |
332eb390 | 1250 | PACKET session_id, extpkt; |
b9908bf9 | 1251 | size_t session_id_len; |
b6981744 | 1252 | const unsigned char *cipherchars; |
b9908bf9 MC |
1253 | int i, al = SSL_AD_INTERNAL_ERROR; |
1254 | unsigned int compression; | |
4fa52141 | 1255 | unsigned int sversion; |
3434f40b | 1256 | unsigned int context; |
4fa52141 | 1257 | int protverr; |
332eb390 | 1258 | RAW_EXTENSION *extensions = NULL; |
b9908bf9 MC |
1259 | #ifndef OPENSSL_NO_COMP |
1260 | SSL_COMP *comp; | |
1261 | #endif | |
1262 | ||
4fa52141 VD |
1263 | if (!PACKET_get_net_2(pkt, &sversion)) { |
1264 | al = SSL_AD_DECODE_ERROR; | |
1265 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1266 | goto f_err; | |
1267 | } | |
50932c4a | 1268 | |
4ff65f77 | 1269 | /* We do this immediately so we know what format the ServerHello is in */ |
4fa52141 VD |
1270 | protverr = ssl_choose_client_version(s, sversion); |
1271 | if (protverr != 0) { | |
1272 | al = SSL_AD_PROTOCOL_VERSION; | |
1273 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr); | |
1274 | goto f_err; | |
0f113f3e | 1275 | } |
0f113f3e | 1276 | |
524420d8 MC |
1277 | /* |
1278 | * In TLSv1.3 a ServerHello message signals a key change so the end of the | |
1279 | * message must be on a record boundary. | |
1280 | */ | |
1281 | if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) { | |
1282 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1283 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_NOT_ON_RECORD_BOUNDARY); | |
1284 | goto f_err; | |
1285 | } | |
1286 | ||
0f113f3e MC |
1287 | /* load the server hello data */ |
1288 | /* load the server random */ | |
73999b62 | 1289 | if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
50932c4a | 1290 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1291 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1292 | goto f_err; |
1293 | } | |
0f113f3e | 1294 | |
fc5ce51d | 1295 | /* Get the session-id. */ |
71728dd8 MC |
1296 | if (!SSL_IS_TLS13(s)) { |
1297 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { | |
1298 | al = SSL_AD_DECODE_ERROR; | |
1299 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1300 | goto f_err; | |
1301 | } | |
1302 | session_id_len = PACKET_remaining(&session_id); | |
1303 | if (session_id_len > sizeof s->session->session_id | |
1304 | || session_id_len > SSL3_SESSION_ID_SIZE) { | |
1305 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1306 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, | |
1307 | SSL_R_SSL3_SESSION_ID_TOO_LONG); | |
1308 | goto f_err; | |
1309 | } | |
1310 | } else { | |
625b0d51 | 1311 | PACKET_null_init(&session_id); |
71728dd8 | 1312 | session_id_len = 0; |
0f113f3e | 1313 | } |
e481f9b9 | 1314 | |
73999b62 | 1315 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
f0659bdb | 1316 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
1317 | al = SSL_AD_DECODE_ERROR; |
1318 | goto f_err; | |
1319 | } | |
1320 | ||
4ff65f77 MC |
1321 | if (!SSL_IS_TLS13(s)) { |
1322 | if (!PACKET_get_1(pkt, &compression)) { | |
1323 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
1324 | al = SSL_AD_DECODE_ERROR; | |
1325 | goto f_err; | |
1326 | } | |
1327 | } else { | |
1328 | compression = 0; | |
1329 | } | |
1330 | ||
1331 | /* TLS extensions */ | |
1332 | if (PACKET_remaining(pkt) == 0) { | |
1333 | PACKET_null_init(&extpkt); | |
1334 | } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) { | |
1335 | al = SSL_AD_DECODE_ERROR; | |
1336 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_LENGTH); | |
1337 | goto f_err; | |
1338 | } | |
1339 | ||
1340 | context = SSL_IS_TLS13(s) ? EXT_TLS1_3_SERVER_HELLO | |
1341 | : EXT_TLS1_2_SERVER_HELLO; | |
fc5ece2e | 1342 | if (!tls_collect_extensions(s, &extpkt, context, &extensions, &al, NULL)) |
4ff65f77 MC |
1343 | goto f_err; |
1344 | ||
1345 | s->hit = 0; | |
1346 | ||
1347 | if (SSL_IS_TLS13(s)) { | |
1348 | /* This will set s->hit if we are resuming */ | |
1349 | if (!tls_parse_extension(s, TLSEXT_IDX_psk, | |
1350 | EXT_TLS1_3_SERVER_HELLO, | |
1351 | extensions, NULL, 0, &al)) | |
1352 | goto f_err; | |
1353 | } else { | |
8c1a5343 | 1354 | /* |
4ff65f77 MC |
1355 | * Check if we can resume the session based on external pre-shared |
1356 | * secret. EAP-FAST (RFC 4851) supports two types of session resumption. | |
1357 | * Resumption based on server-side state works with session IDs. | |
1358 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
1359 | * works by overriding the SessionTicket extension at the application | |
1360 | * layer, and does not send a session ID. (We do not know whether | |
1361 | * EAP-FAST servers would honour the session ID.) Therefore, the session | |
1362 | * ID alone is not a reliable indicator of session resumption, so we | |
1363 | * first check if we can resume, and later peek at the next handshake | |
1364 | * message to see if the server wants to resume. | |
8c1a5343 | 1365 | */ |
4ff65f77 MC |
1366 | if (s->version >= TLS1_VERSION |
1367 | && s->ext.session_secret_cb != NULL && s->session->ext.tick) { | |
1368 | const SSL_CIPHER *pref_cipher = NULL; | |
1369 | /* | |
1370 | * s->session->master_key_length is a size_t, but this is an int for | |
1371 | * backwards compat reasons | |
1372 | */ | |
1373 | int master_key_length; | |
1374 | master_key_length = sizeof(s->session->master_key); | |
1375 | if (s->ext.session_secret_cb(s, s->session->master_key, | |
1376 | &master_key_length, | |
1377 | NULL, &pref_cipher, | |
1378 | s->ext.session_secret_cb_arg) | |
1379 | && master_key_length > 0) { | |
1380 | s->session->master_key_length = master_key_length; | |
1381 | s->session->cipher = pref_cipher ? | |
60d685d1 | 1382 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0); |
4ff65f77 MC |
1383 | } else { |
1384 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
1385 | al = SSL_AD_INTERNAL_ERROR; | |
1386 | goto f_err; | |
1387 | } | |
0f113f3e | 1388 | } |
4ff65f77 MC |
1389 | |
1390 | if (session_id_len != 0 | |
1391 | && session_id_len == s->session->session_id_length | |
1392 | && memcmp(PACKET_data(&session_id), s->session->session_id, | |
1393 | session_id_len) == 0) | |
1394 | s->hit = 1; | |
50932c4a MC |
1395 | } |
1396 | ||
4ff65f77 | 1397 | if (s->hit) { |
0f113f3e | 1398 | if (s->sid_ctx_length != s->session->sid_ctx_length |
4ff65f77 | 1399 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
0f113f3e MC |
1400 | /* actually a client application bug */ |
1401 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1402 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1403 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
1404 | goto f_err; | |
1405 | } | |
6e3d0153 | 1406 | } else { |
0f113f3e | 1407 | /* |
6e3d0153 | 1408 | * If we were trying for session-id reuse but the server |
4ff65f77 | 1409 | * didn't resume, make a new SSL_SESSION. |
6e3d0153 EK |
1410 | * In the case of EAP-FAST and PAC, we do not send a session ID, |
1411 | * so the PAC-based session secret is always preserved. It'll be | |
1412 | * overwritten if the server refuses resumption. | |
0f113f3e | 1413 | */ |
4ff65f77 MC |
1414 | if (s->session->session_id_length > 0 |
1415 | || (SSL_IS_TLS13(s) | |
1416 | && s->session->ext.tick_identity | |
1417 | != TLSEXT_PSK_BAD_IDENTITY)) { | |
4f6eaa59 | 1418 | s->ctx->stats.sess_miss++; |
0f113f3e MC |
1419 | if (!ssl_get_new_session(s, 0)) { |
1420 | goto f_err; | |
1421 | } | |
1422 | } | |
50932c4a | 1423 | |
ccae4a15 | 1424 | s->session->ssl_version = s->version; |
fc5ce51d EK |
1425 | s->session->session_id_length = session_id_len; |
1426 | /* session_id_len could be 0 */ | |
a19fc66a KR |
1427 | if (session_id_len > 0) |
1428 | memcpy(s->session->session_id, PACKET_data(&session_id), | |
1429 | session_id_len); | |
0f113f3e | 1430 | } |
fc5ce51d | 1431 | |
ccae4a15 FI |
1432 | /* Session version and negotiated protocol version should match */ |
1433 | if (s->version != s->session->ssl_version) { | |
1434 | al = SSL_AD_PROTOCOL_VERSION; | |
1435 | ||
1436 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, | |
1437 | SSL_R_SSL_SESSION_VERSION_MISMATCH); | |
1438 | goto f_err; | |
1439 | } | |
1440 | ||
60d685d1 | 1441 | c = ssl_get_cipher_by_char(s, cipherchars, 0); |
0f113f3e MC |
1442 | if (c == NULL) { |
1443 | /* unknown cipher */ | |
1444 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1445 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); |
0f113f3e MC |
1446 | goto f_err; |
1447 | } | |
0f113f3e | 1448 | /* |
3eb2aff4 KR |
1449 | * Now that we know the version, update the check to see if it's an allowed |
1450 | * version. | |
1451 | */ | |
1452 | s->s3->tmp.min_ver = s->version; | |
1453 | s->s3->tmp.max_ver = s->version; | |
1454 | /* | |
1455 | * If it is a disabled cipher we either didn't send it in client hello, | |
1456 | * or it's not allowed for the selected protocol. So we return an error. | |
0f113f3e MC |
1457 | */ |
1458 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { | |
1459 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1460 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1461 | goto f_err; |
1462 | } | |
0f113f3e MC |
1463 | |
1464 | sk = ssl_get_ciphers_by_id(s); | |
1465 | i = sk_SSL_CIPHER_find(sk, c); | |
1466 | if (i < 0) { | |
1467 | /* we did not say we would use this cipher */ | |
1468 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1469 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1470 | goto f_err; |
1471 | } | |
1472 | ||
1473 | /* | |
1474 | * Depending on the session caching (internal/external), the cipher | |
1475 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1476 | * set and use it for comparison. | |
1477 | */ | |
1478 | if (s->session->cipher) | |
1479 | s->session->cipher_id = s->session->cipher->id; | |
1480 | if (s->hit && (s->session->cipher_id != c->id)) { | |
9e9858d1 | 1481 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1482 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
9e9858d1 RS |
1483 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
1484 | goto f_err; | |
0f113f3e MC |
1485 | } |
1486 | s->s3->tmp.new_cipher = c; | |
71728dd8 | 1487 | |
09b6c2ef | 1488 | #ifdef OPENSSL_NO_COMP |
fc5ce51d | 1489 | if (compression != 0) { |
0f113f3e | 1490 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1491 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1492 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1493 | goto f_err; | |
1494 | } | |
1495 | /* | |
1496 | * If compression is disabled we'd better not try to resume a session | |
1497 | * using compression. | |
1498 | */ | |
1499 | if (s->session->compress_meth != 0) { | |
b9908bf9 | 1500 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
0f113f3e MC |
1501 | goto f_err; |
1502 | } | |
09b6c2ef | 1503 | #else |
fc5ce51d | 1504 | if (s->hit && compression != s->session->compress_meth) { |
0f113f3e | 1505 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1506 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1507 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
1508 | goto f_err; | |
1509 | } | |
fc5ce51d | 1510 | if (compression == 0) |
0f113f3e MC |
1511 | comp = NULL; |
1512 | else if (!ssl_allow_compression(s)) { | |
1513 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1514 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
0f113f3e | 1515 | goto f_err; |
fc5ce51d EK |
1516 | } else { |
1517 | comp = ssl3_comp_find(s->ctx->comp_methods, compression); | |
1518 | } | |
0f113f3e | 1519 | |
fc5ce51d | 1520 | if (compression != 0 && comp == NULL) { |
0f113f3e | 1521 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1522 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1523 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1524 | goto f_err; | |
1525 | } else { | |
1526 | s->s3->tmp.new_compression = comp; | |
1527 | } | |
09b6c2ef | 1528 | #endif |
761772d7 | 1529 | |
4ff65f77 | 1530 | if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, &al)) |
332eb390 MC |
1531 | goto f_err; |
1532 | ||
8723588e MC |
1533 | #ifndef OPENSSL_NO_SCTP |
1534 | if (SSL_IS_DTLS(s) && s->hit) { | |
1535 | unsigned char sctpauthkey[64]; | |
1536 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
1537 | ||
1538 | /* | |
1539 | * Add new shared key for SCTP-Auth, will be ignored if | |
1540 | * no SCTP used. | |
1541 | */ | |
141eb8c6 MC |
1542 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
1543 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
8723588e MC |
1544 | |
1545 | if (SSL_export_keying_material(s, sctpauthkey, | |
a230b26e EK |
1546 | sizeof(sctpauthkey), |
1547 | labelbuffer, | |
1548 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
c0aa6b81 | 1549 | goto f_err; |
8723588e MC |
1550 | |
1551 | BIO_ctrl(SSL_get_wbio(s), | |
1552 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
1553 | sizeof(sctpauthkey), sctpauthkey); | |
1554 | } | |
1555 | #endif | |
1556 | ||
92760c21 MC |
1557 | /* |
1558 | * In TLSv1.3 we have some post-processing to change cipher state, otherwise | |
1559 | * we're done with this message | |
1560 | */ | |
1561 | if (SSL_IS_TLS13(s) | |
1562 | && (!s->method->ssl3_enc->setup_key_block(s) | |
92760c21 MC |
1563 | || !s->method->ssl3_enc->change_cipher_state(s, |
1564 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ))) { | |
1565 | al = SSL_AD_INTERNAL_ERROR; | |
1566 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_CANNOT_CHANGE_CIPHER); | |
1567 | goto f_err; | |
1568 | } | |
1569 | ||
1b0286a3 | 1570 | OPENSSL_free(extensions); |
b9908bf9 | 1571 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1572 | f_err: |
1573 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1574 | ossl_statem_set_error(s); |
1b0286a3 | 1575 | OPENSSL_free(extensions); |
b9908bf9 | 1576 | return MSG_PROCESS_ERROR; |
0f113f3e | 1577 | } |
d02b48c6 | 1578 | |
3847d426 MC |
1579 | static MSG_PROCESS_RETURN tls_process_hello_retry_request(SSL *s, PACKET *pkt) |
1580 | { | |
1581 | unsigned int sversion; | |
2248dbeb | 1582 | int errorcode; |
3847d426 MC |
1583 | RAW_EXTENSION *extensions = NULL; |
1584 | int al; | |
1585 | PACKET extpkt; | |
1586 | ||
1587 | if (!PACKET_get_net_2(pkt, &sversion)) { | |
1588 | al = SSL_AD_DECODE_ERROR; | |
1589 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, SSL_R_LENGTH_MISMATCH); | |
1590 | goto f_err; | |
1591 | } | |
1592 | ||
1593 | s->hello_retry_request = 1; | |
1594 | ||
1595 | /* This will fail if it doesn't choose TLSv1.3+ */ | |
2248dbeb MC |
1596 | errorcode = ssl_choose_client_version(s, sversion); |
1597 | if (errorcode != 0) { | |
3847d426 | 1598 | al = SSL_AD_PROTOCOL_VERSION; |
2248dbeb | 1599 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, errorcode); |
3847d426 MC |
1600 | goto f_err; |
1601 | } | |
1602 | ||
1603 | if (!PACKET_as_length_prefixed_2(pkt, &extpkt)) { | |
1604 | al = SSL_AD_DECODE_ERROR; | |
1605 | SSLerr(SSL_F_TLS_PROCESS_HELLO_RETRY_REQUEST, SSL_R_BAD_LENGTH); | |
1606 | goto f_err; | |
1607 | } | |
1608 | ||
1609 | if (!tls_collect_extensions(s, &extpkt, EXT_TLS1_3_HELLO_RETRY_REQUEST, | |
fc5ece2e | 1610 | &extensions, &al, NULL) |
3847d426 MC |
1611 | || !tls_parse_all_extensions(s, EXT_TLS1_3_HELLO_RETRY_REQUEST, |
1612 | extensions, NULL, 0, &al)) | |
1613 | goto f_err; | |
1614 | ||
1615 | OPENSSL_free(extensions); | |
1616 | ||
1617 | return MSG_PROCESS_FINISHED_READING; | |
1618 | f_err: | |
1619 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1620 | ossl_statem_set_error(s); | |
1621 | OPENSSL_free(extensions); | |
1622 | return MSG_PROCESS_ERROR; | |
1623 | } | |
1624 | ||
be3583fa | 1625 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1626 | { |
1627 | int al, i, ret = MSG_PROCESS_ERROR, exp_idx; | |
1628 | unsigned long cert_list_len, cert_len; | |
1629 | X509 *x = NULL; | |
b6981744 | 1630 | const unsigned char *certstart, *certbytes; |
b9908bf9 MC |
1631 | STACK_OF(X509) *sk = NULL; |
1632 | EVP_PKEY *pkey = NULL; | |
d805a57b | 1633 | size_t chainidx; |
e96e0f8e | 1634 | unsigned int context = 0; |
0f113f3e MC |
1635 | |
1636 | if ((sk = sk_X509_new_null()) == NULL) { | |
b9908bf9 | 1637 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1638 | goto err; |
0f113f3e MC |
1639 | } |
1640 | ||
e96e0f8e MC |
1641 | if ((SSL_IS_TLS13(s) && !PACKET_get_1(pkt, &context)) |
1642 | || context != 0 | |
1643 | || !PACKET_get_net_3(pkt, &cert_list_len) | |
1644 | || PACKET_remaining(pkt) != cert_list_len) { | |
0f113f3e | 1645 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1646 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1647 | goto f_err; |
1648 | } | |
d805a57b | 1649 | for (chainidx = 0; PACKET_remaining(pkt); chainidx++) { |
73999b62 | 1650 | if (!PACKET_get_net_3(pkt, &cert_len) |
a230b26e | 1651 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { |
0f113f3e | 1652 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1653 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1654 | SSL_R_CERT_LENGTH_MISMATCH); |
1655 | goto f_err; | |
1656 | } | |
1657 | ||
df758a85 MC |
1658 | certstart = certbytes; |
1659 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len); | |
0f113f3e MC |
1660 | if (x == NULL) { |
1661 | al = SSL_AD_BAD_CERTIFICATE; | |
b9908bf9 | 1662 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
0f113f3e MC |
1663 | goto f_err; |
1664 | } | |
df758a85 | 1665 | if (certbytes != (certstart + cert_len)) { |
0f113f3e | 1666 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1667 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1668 | SSL_R_CERT_LENGTH_MISMATCH); |
1669 | goto f_err; | |
1670 | } | |
e96e0f8e MC |
1671 | |
1672 | if (SSL_IS_TLS13(s)) { | |
1673 | RAW_EXTENSION *rawexts = NULL; | |
1674 | PACKET extensions; | |
1675 | ||
1676 | if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { | |
1677 | al = SSL_AD_DECODE_ERROR; | |
1678 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_BAD_LENGTH); | |
1679 | goto f_err; | |
1680 | } | |
1681 | if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_CERTIFICATE, | |
fc5ece2e | 1682 | &rawexts, &al, NULL) |
e96e0f8e | 1683 | || !tls_parse_all_extensions(s, EXT_TLS1_3_CERTIFICATE, |
5ee289ea MC |
1684 | rawexts, x, chainidx, &al)) { |
1685 | OPENSSL_free(rawexts); | |
e96e0f8e | 1686 | goto f_err; |
5ee289ea MC |
1687 | } |
1688 | OPENSSL_free(rawexts); | |
e96e0f8e MC |
1689 | } |
1690 | ||
0f113f3e | 1691 | if (!sk_X509_push(sk, x)) { |
b9908bf9 | 1692 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1693 | goto err; |
0f113f3e MC |
1694 | } |
1695 | x = NULL; | |
0f113f3e MC |
1696 | } |
1697 | ||
1698 | i = ssl_verify_cert_chain(s, sk); | |
c8e2f98c MC |
1699 | /* |
1700 | * The documented interface is that SSL_VERIFY_PEER should be set in order | |
1701 | * for client side verification of the server certificate to take place. | |
1702 | * However, historically the code has only checked that *any* flag is set | |
1703 | * to cause server verification to take place. Use of the other flags makes | |
1704 | * no sense in client mode. An attempt to clean up the semantics was | |
1705 | * reverted because at least one application *only* set | |
1706 | * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused | |
1707 | * server verification to take place, after the clean up it silently did | |
1708 | * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags | |
1709 | * sent to them because they are void functions. Therefore, we now use the | |
1710 | * (less clean) historic behaviour of performing validation if any flag is | |
1711 | * set. The *documented* interface remains the same. | |
1712 | */ | |
1713 | if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) { | |
0f113f3e | 1714 | al = ssl_verify_alarm_type(s->verify_result); |
b9908bf9 | 1715 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1716 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
1717 | goto f_err; | |
1718 | } | |
1719 | ERR_clear_error(); /* but we keep s->verify_result */ | |
1720 | if (i > 1) { | |
b9908bf9 | 1721 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i); |
0f113f3e MC |
1722 | al = SSL_AD_HANDSHAKE_FAILURE; |
1723 | goto f_err; | |
1724 | } | |
1725 | ||
c34b0f99 | 1726 | s->session->peer_chain = sk; |
0f113f3e MC |
1727 | /* |
1728 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
d4d78943 | 1729 | * which we don't include in statem_srvr.c |
0f113f3e MC |
1730 | */ |
1731 | x = sk_X509_value(sk, 0); | |
1732 | sk = NULL; | |
1733 | /* | |
1734 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end | |
1735 | */ | |
1736 | ||
8382fd3a | 1737 | pkey = X509_get0_pubkey(x); |
0f113f3e | 1738 | |
55a9a16f | 1739 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
0f113f3e MC |
1740 | x = NULL; |
1741 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1742 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1743 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
1744 | goto f_err; | |
1745 | } | |
1746 | ||
1747 | i = ssl_cert_type(x, pkey); | |
55a9a16f | 1748 | if (i < 0) { |
0f113f3e MC |
1749 | x = NULL; |
1750 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1751 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1752 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
1753 | goto f_err; | |
1754 | } | |
05b8486e DSH |
1755 | /* |
1756 | * Check certificate type is consistent with ciphersuite. For TLS 1.3 | |
1757 | * skip check since TLS 1.3 ciphersuites can be used with any certificate | |
1758 | * type. | |
1759 | */ | |
1760 | if (!SSL_IS_TLS13(s)) { | |
1761 | exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
1762 | if (exp_idx >= 0 && i != exp_idx | |
1763 | && (exp_idx != SSL_PKEY_GOST_EC || | |
1764 | (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256 | |
1765 | && i != SSL_PKEY_GOST01))) { | |
1766 | x = NULL; | |
1767 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1768 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, | |
1769 | SSL_R_WRONG_CERTIFICATE_TYPE); | |
1770 | goto f_err; | |
1771 | } | |
0f113f3e | 1772 | } |
a273c6ee | 1773 | s->session->peer_type = i; |
55a9a16f MC |
1774 | |
1775 | X509_free(s->session->peer); | |
05f0fb9f | 1776 | X509_up_ref(x); |
55a9a16f | 1777 | s->session->peer = x; |
0f113f3e | 1778 | s->session->verify_result = s->verify_result; |
0f113f3e | 1779 | x = NULL; |
2c5dfdc3 MC |
1780 | |
1781 | /* Save the current hash state for when we receive the CertificateVerify */ | |
1782 | if (SSL_IS_TLS13(s) | |
1783 | && !ssl_handshake_hash(s, s->cert_verify_hash, | |
1784 | sizeof(s->cert_verify_hash), | |
1785 | &s->cert_verify_hash_len)) { | |
1786 | al = SSL_AD_INTERNAL_ERROR; | |
1787 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
1788 | goto f_err; | |
1789 | } | |
1790 | ||
b9908bf9 | 1791 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 RS |
1792 | goto done; |
1793 | ||
0f113f3e | 1794 | f_err: |
66696478 | 1795 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
cc273a93 | 1796 | err: |
fe3a3291 | 1797 | ossl_statem_set_error(s); |
66696478 | 1798 | done: |
0f113f3e MC |
1799 | X509_free(x); |
1800 | sk_X509_pop_free(sk, X509_free); | |
b9908bf9 | 1801 | return ret; |
0f113f3e | 1802 | } |
d02b48c6 | 1803 | |
7dc1c647 | 1804 | static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al) |
02a74590 MC |
1805 | { |
1806 | #ifndef OPENSSL_NO_PSK | |
7dc1c647 | 1807 | PACKET psk_identity_hint; |
02a74590 | 1808 | |
7dc1c647 MC |
1809 | /* PSK ciphersuites are preceded by an identity hint */ |
1810 | ||
1811 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { | |
1812 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1813 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH); |
7dc1c647 MC |
1814 | return 0; |
1815 | } | |
1816 | ||
1817 | /* | |
1818 | * Store PSK identity hint for later use, hint is used in | |
1819 | * tls_construct_client_key_exchange. Assume that the maximum length of | |
1820 | * a PSK identity hint can be as long as the maximum length of a PSK | |
1821 | * identity. | |
1822 | */ | |
1823 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { | |
1824 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
4fa88861 | 1825 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG); |
7dc1c647 MC |
1826 | return 0; |
1827 | } | |
02a74590 | 1828 | |
7dc1c647 MC |
1829 | if (PACKET_remaining(&psk_identity_hint) == 0) { |
1830 | OPENSSL_free(s->session->psk_identity_hint); | |
1831 | s->session->psk_identity_hint = NULL; | |
1832 | } else if (!PACKET_strndup(&psk_identity_hint, | |
a230b26e | 1833 | &s->session->psk_identity_hint)) { |
7dc1c647 MC |
1834 | *al = SSL_AD_INTERNAL_ERROR; |
1835 | return 0; | |
1836 | } | |
1837 | ||
1838 | return 1; | |
1839 | #else | |
4fa88861 | 1840 | SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
7dc1c647 MC |
1841 | *al = SSL_AD_INTERNAL_ERROR; |
1842 | return 0; | |
02a74590 MC |
1843 | #endif |
1844 | } | |
1845 | ||
25c6c10c MC |
1846 | static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
1847 | { | |
1848 | #ifndef OPENSSL_NO_SRP | |
1849 | PACKET prime, generator, salt, server_pub; | |
1850 | ||
1851 | if (!PACKET_get_length_prefixed_2(pkt, &prime) | |
1852 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1853 | || !PACKET_get_length_prefixed_1(pkt, &salt) | |
1854 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { | |
1855 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1856 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH); |
25c6c10c MC |
1857 | return 0; |
1858 | } | |
1859 | ||
348240c6 | 1860 | /* TODO(size_t): Convert BN_bin2bn() calls */ |
25c6c10c MC |
1861 | if ((s->srp_ctx.N = |
1862 | BN_bin2bn(PACKET_data(&prime), | |
348240c6 | 1863 | (int)PACKET_remaining(&prime), NULL)) == NULL |
25c6c10c MC |
1864 | || (s->srp_ctx.g = |
1865 | BN_bin2bn(PACKET_data(&generator), | |
348240c6 | 1866 | (int)PACKET_remaining(&generator), NULL)) == NULL |
25c6c10c MC |
1867 | || (s->srp_ctx.s = |
1868 | BN_bin2bn(PACKET_data(&salt), | |
348240c6 | 1869 | (int)PACKET_remaining(&salt), NULL)) == NULL |
25c6c10c MC |
1870 | || (s->srp_ctx.B = |
1871 | BN_bin2bn(PACKET_data(&server_pub), | |
348240c6 | 1872 | (int)PACKET_remaining(&server_pub), NULL)) == NULL) { |
25c6c10c | 1873 | *al = SSL_AD_INTERNAL_ERROR; |
4fa88861 | 1874 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB); |
25c6c10c MC |
1875 | return 0; |
1876 | } | |
1877 | ||
1878 | if (!srp_verify_server_param(s, al)) { | |
1879 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1880 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS); |
25c6c10c MC |
1881 | return 0; |
1882 | } | |
1883 | ||
1884 | /* We must check if there is a certificate */ | |
a230b26e | 1885 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
25c6c10c MC |
1886 | *pkey = X509_get0_pubkey(s->session->peer); |
1887 | ||
1888 | return 1; | |
1889 | #else | |
4fa88861 | 1890 | SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR); |
25c6c10c MC |
1891 | *al = SSL_AD_INTERNAL_ERROR; |
1892 | return 0; | |
1893 | #endif | |
1894 | } | |
1895 | ||
e01a610d MC |
1896 | static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
1897 | { | |
1898 | #ifndef OPENSSL_NO_DH | |
1899 | PACKET prime, generator, pub_key; | |
1900 | EVP_PKEY *peer_tmp = NULL; | |
1901 | ||
1902 | DH *dh = NULL; | |
1903 | BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; | |
1904 | ||
26505153 RL |
1905 | int check_bits = 0; |
1906 | ||
e01a610d MC |
1907 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
1908 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1909 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { | |
1910 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 1911 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH); |
e01a610d MC |
1912 | return 0; |
1913 | } | |
1914 | ||
1915 | peer_tmp = EVP_PKEY_new(); | |
1916 | dh = DH_new(); | |
1917 | ||
1918 | if (peer_tmp == NULL || dh == NULL) { | |
1919 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1920 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE); |
e01a610d MC |
1921 | goto err; |
1922 | } | |
1923 | ||
348240c6 MC |
1924 | /* TODO(size_t): Convert these calls */ |
1925 | p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL); | |
1926 | g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator), | |
1927 | NULL); | |
1928 | bnpub_key = BN_bin2bn(PACKET_data(&pub_key), | |
1929 | (int)PACKET_remaining(&pub_key), NULL); | |
e01a610d MC |
1930 | if (p == NULL || g == NULL || bnpub_key == NULL) { |
1931 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1932 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1933 | goto err; |
1934 | } | |
1935 | ||
26505153 RL |
1936 | /* test non-zero pupkey */ |
1937 | if (BN_is_zero(bnpub_key)) { | |
e01a610d | 1938 | *al = SSL_AD_DECODE_ERROR; |
4fa88861 | 1939 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE); |
e01a610d MC |
1940 | goto err; |
1941 | } | |
1942 | ||
1943 | if (!DH_set0_pqg(dh, p, NULL, g)) { | |
1944 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1945 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1946 | goto err; |
1947 | } | |
1948 | p = g = NULL; | |
1949 | ||
26505153 RL |
1950 | if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) { |
1951 | *al = SSL_AD_DECODE_ERROR; | |
1952 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE); | |
1953 | goto err; | |
1954 | } | |
1955 | ||
e01a610d MC |
1956 | if (!DH_set0_key(dh, bnpub_key, NULL)) { |
1957 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1958 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB); |
e01a610d MC |
1959 | goto err; |
1960 | } | |
1961 | bnpub_key = NULL; | |
1962 | ||
1963 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { | |
1964 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
4fa88861 | 1965 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL); |
e01a610d MC |
1966 | goto err; |
1967 | } | |
1968 | ||
1969 | if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) { | |
1970 | *al = SSL_AD_INTERNAL_ERROR; | |
4fa88861 | 1971 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB); |
e01a610d MC |
1972 | goto err; |
1973 | } | |
1974 | ||
1975 | s->s3->peer_tmp = peer_tmp; | |
1976 | ||
1977 | /* | |
1978 | * FIXME: This makes assumptions about which ciphersuites come with | |
1979 | * public keys. We should have a less ad-hoc way of doing this | |
1980 | */ | |
a230b26e | 1981 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
e01a610d MC |
1982 | *pkey = X509_get0_pubkey(s->session->peer); |
1983 | /* else anonymous DH, so no certificate or pkey. */ | |
1984 | ||
1985 | return 1; | |
1986 | ||
1987 | err: | |
1988 | BN_free(p); | |
1989 | BN_free(g); | |
1990 | BN_free(bnpub_key); | |
1991 | DH_free(dh); | |
1992 | EVP_PKEY_free(peer_tmp); | |
1993 | ||
1994 | return 0; | |
1995 | #else | |
4fa88861 | 1996 | SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR); |
e01a610d MC |
1997 | *al = SSL_AD_INTERNAL_ERROR; |
1998 | return 0; | |
1999 | #endif | |
2000 | } | |
2001 | ||
ff74aeb1 MC |
2002 | static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al) |
2003 | { | |
2004 | #ifndef OPENSSL_NO_EC | |
2005 | PACKET encoded_pt; | |
2006 | const unsigned char *ecparams; | |
2007 | int curve_nid; | |
ec24630a | 2008 | unsigned int curve_flags; |
ff74aeb1 MC |
2009 | EVP_PKEY_CTX *pctx = NULL; |
2010 | ||
2011 | /* | |
2012 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
2013 | * public key. For now we only support named (not generic) curves and | |
2014 | * ECParameters in this case is just three bytes. | |
2015 | */ | |
2016 | if (!PACKET_get_bytes(pkt, &ecparams, 3)) { | |
2017 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 2018 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT); |
ff74aeb1 MC |
2019 | return 0; |
2020 | } | |
2021 | /* | |
2022 | * Check curve is one of our preferences, if not server has sent an | |
2023 | * invalid curve. ECParameters is 3 bytes. | |
2024 | */ | |
2025 | if (!tls1_check_curve(s, ecparams, 3)) { | |
2026 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 2027 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE); |
ff74aeb1 MC |
2028 | return 0; |
2029 | } | |
2030 | ||
ec24630a DSH |
2031 | curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags); |
2032 | ||
a230b26e | 2033 | if (curve_nid == 0) { |
ff74aeb1 | 2034 | *al = SSL_AD_INTERNAL_ERROR; |
4fa88861 | 2035 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, |
ff74aeb1 MC |
2036 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
2037 | return 0; | |
2038 | } | |
2039 | ||
ec24630a DSH |
2040 | if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) { |
2041 | EVP_PKEY *key = EVP_PKEY_new(); | |
2042 | ||
2043 | if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) { | |
2044 | *al = SSL_AD_INTERNAL_ERROR; | |
2045 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); | |
2046 | EVP_PKEY_free(key); | |
2047 | return 0; | |
2048 | } | |
2049 | s->s3->peer_tmp = key; | |
2050 | } else { | |
2051 | /* Set up EVP_PKEY with named curve as parameters */ | |
2052 | pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); | |
2053 | if (pctx == NULL | |
2054 | || EVP_PKEY_paramgen_init(pctx) <= 0 | |
2055 | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0 | |
2056 | || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { | |
2057 | *al = SSL_AD_INTERNAL_ERROR; | |
2058 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB); | |
2059 | EVP_PKEY_CTX_free(pctx); | |
2060 | return 0; | |
2061 | } | |
ff74aeb1 | 2062 | EVP_PKEY_CTX_free(pctx); |
ec24630a | 2063 | pctx = NULL; |
ff74aeb1 | 2064 | } |
ff74aeb1 MC |
2065 | |
2066 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { | |
2067 | *al = SSL_AD_DECODE_ERROR; | |
4fa88861 | 2068 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH); |
ff74aeb1 MC |
2069 | return 0; |
2070 | } | |
2071 | ||
ec24630a DSH |
2072 | if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp, |
2073 | PACKET_data(&encoded_pt), | |
2074 | PACKET_remaining(&encoded_pt))) { | |
ff74aeb1 | 2075 | *al = SSL_AD_DECODE_ERROR; |
4fa88861 | 2076 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT); |
ff74aeb1 MC |
2077 | return 0; |
2078 | } | |
2079 | ||
2080 | /* | |
2081 | * The ECC/TLS specification does not mention the use of DSA to sign | |
2082 | * ECParameters in the server key exchange message. We do support RSA | |
2083 | * and ECDSA. | |
2084 | */ | |
2085 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA) | |
2086 | *pkey = X509_get0_pubkey(s->session->peer); | |
2087 | else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA) | |
2088 | *pkey = X509_get0_pubkey(s->session->peer); | |
2089 | /* else anonymous ECDH, so no certificate or pkey. */ | |
2090 | ||
2091 | return 1; | |
2092 | #else | |
4fa88861 | 2093 | SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR); |
ff74aeb1 MC |
2094 | *al = SSL_AD_INTERNAL_ERROR; |
2095 | return 0; | |
2096 | #endif | |
2097 | } | |
2098 | ||
be3583fa | 2099 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt) |
b9908bf9 | 2100 | { |
5554facb | 2101 | int al = -1; |
e1e588ac | 2102 | long alg_k; |
b9908bf9 | 2103 | EVP_PKEY *pkey = NULL; |
fe3066ee MC |
2104 | EVP_MD_CTX *md_ctx = NULL; |
2105 | EVP_PKEY_CTX *pctx = NULL; | |
73999b62 | 2106 | PACKET save_param_start, signature; |
b9908bf9 | 2107 | |
b9908bf9 MC |
2108 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
2109 | ||
73999b62 | 2110 | save_param_start = *pkt; |
8d92c1f8 | 2111 | |
3260adf1 | 2112 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
61dd9f7a DSH |
2113 | EVP_PKEY_free(s->s3->peer_tmp); |
2114 | s->s3->peer_tmp = NULL; | |
3260adf1 | 2115 | #endif |
d02b48c6 | 2116 | |
7689082b | 2117 | if (alg_k & SSL_PSK) { |
7dc1c647 MC |
2118 | if (!tls_process_ske_psk_preamble(s, pkt, &al)) |
2119 | goto err; | |
7689082b DSH |
2120 | } |
2121 | ||
2122 | /* Nothing else to do for plain PSK or RSAPSK */ | |
2123 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { | |
25c6c10c MC |
2124 | } else if (alg_k & SSL_kSRP) { |
2125 | if (!tls_process_ske_srp(s, pkt, &pkey, &al)) | |
0f113f3e | 2126 | goto err; |
e01a610d MC |
2127 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
2128 | if (!tls_process_ske_dhe(s, pkt, &pkey, &al)) | |
2129 | goto err; | |
ff74aeb1 MC |
2130 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
2131 | if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al)) | |
2132 | goto err; | |
0f113f3e MC |
2133 | } else if (alg_k) { |
2134 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 2135 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
e1e588ac | 2136 | goto err; |
0f113f3e | 2137 | } |
0f113f3e | 2138 | |
0f113f3e MC |
2139 | /* if it was signed, check the signature */ |
2140 | if (pkey != NULL) { | |
32942870 | 2141 | PACKET params; |
be8dba2c MC |
2142 | int maxsig; |
2143 | const EVP_MD *md = NULL; | |
e1e588ac | 2144 | |
32942870 EK |
2145 | /* |
2146 | * |pkt| now points to the beginning of the signature, so the difference | |
2147 | * equals the length of the parameters. | |
2148 | */ | |
2149 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, | |
2150 | PACKET_remaining(&save_param_start) - | |
73999b62 | 2151 | PACKET_remaining(pkt))) { |
32942870 | 2152 | al = SSL_AD_INTERNAL_ERROR; |
f0659bdb | 2153 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac | 2154 | goto err; |
32942870 EK |
2155 | } |
2156 | ||
0f113f3e | 2157 | if (SSL_USE_SIGALGS(s)) { |
703bcee0 | 2158 | unsigned int sigalg; |
0f113f3e | 2159 | int rv; |
703bcee0 MC |
2160 | |
2161 | if (!PACKET_get_net_2(pkt, &sigalg)) { | |
e1e588ac | 2162 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 2163 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
e1e588ac | 2164 | goto err; |
0f113f3e | 2165 | } |
5554facb | 2166 | rv = tls12_check_peer_sigalg(s, sigalg, pkey); |
e1e588ac MC |
2167 | if (rv == -1) { |
2168 | al = SSL_AD_INTERNAL_ERROR; | |
2169 | goto err; | |
2170 | } else if (rv == 0) { | |
2171 | al = SSL_AD_DECODE_ERROR; | |
0f113f3e | 2172 | goto err; |
0f113f3e | 2173 | } |
a2f9200f | 2174 | #ifdef SSL_DEBUG |
0f113f3e MC |
2175 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
2176 | #endif | |
f365a3e2 DSH |
2177 | } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
2178 | al = SSL_AD_INTERNAL_ERROR; | |
2179 | goto err; | |
32942870 | 2180 | } |
0f113f3e | 2181 | |
f365a3e2 DSH |
2182 | md = ssl_md(s->s3->tmp.peer_sigalg->hash_idx); |
2183 | ||
73999b62 MC |
2184 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
2185 | || PACKET_remaining(pkt) != 0) { | |
e1e588ac | 2186 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 2187 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
e1e588ac | 2188 | goto err; |
0f113f3e | 2189 | } |
be8dba2c MC |
2190 | maxsig = EVP_PKEY_size(pkey); |
2191 | if (maxsig < 0) { | |
e1e588ac | 2192 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 2193 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac | 2194 | goto err; |
8098fc56 | 2195 | } |
0f113f3e MC |
2196 | |
2197 | /* | |
8098fc56 | 2198 | * Check signature length |
0f113f3e | 2199 | */ |
be8dba2c | 2200 | if (PACKET_remaining(&signature) > (size_t)maxsig) { |
0f113f3e | 2201 | /* wrong packet length */ |
e1e588ac | 2202 | al = SSL_AD_DECODE_ERROR; |
a230b26e EK |
2203 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
2204 | SSL_R_WRONG_SIGNATURE_LENGTH); | |
e1e588ac MC |
2205 | goto err; |
2206 | } | |
2207 | ||
2208 | md_ctx = EVP_MD_CTX_new(); | |
2209 | if (md_ctx == NULL) { | |
2210 | al = SSL_AD_INTERNAL_ERROR; | |
2211 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
2212 | goto err; | |
0f113f3e | 2213 | } |
e1e588ac | 2214 | |
fe3066ee MC |
2215 | if (EVP_DigestVerifyInit(md_ctx, &pctx, md, NULL, pkey) <= 0) { |
2216 | al = SSL_AD_INTERNAL_ERROR; | |
2217 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
2218 | goto err; | |
2219 | } | |
5554facb | 2220 | if (SSL_USE_PSS(s)) { |
fe3066ee | 2221 | if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
91410d40 | 2222 | || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
968ae5b3 | 2223 | RSA_PSS_SALTLEN_DIGEST) <= 0) { |
fe3066ee MC |
2224 | al = SSL_AD_INTERNAL_ERROR; |
2225 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
2226 | goto err; | |
2227 | } | |
2228 | } | |
2229 | if (EVP_DigestVerifyUpdate(md_ctx, &(s->s3->client_random[0]), | |
2230 | SSL3_RANDOM_SIZE) <= 0 | |
2231 | || EVP_DigestVerifyUpdate(md_ctx, &(s->s3->server_random[0]), | |
2232 | SSL3_RANDOM_SIZE) <= 0 | |
2233 | || EVP_DigestVerifyUpdate(md_ctx, PACKET_data(¶ms), | |
2234 | PACKET_remaining(¶ms)) <= 0) { | |
192e4bbb DSH |
2235 | al = SSL_AD_INTERNAL_ERROR; |
2236 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
e1e588ac | 2237 | goto err; |
192e4bbb | 2238 | } |
fe3066ee MC |
2239 | if (EVP_DigestVerifyFinal(md_ctx, PACKET_data(&signature), |
2240 | PACKET_remaining(&signature)) <= 0) { | |
192e4bbb DSH |
2241 | /* bad signature */ |
2242 | al = SSL_AD_DECRYPT_ERROR; | |
2243 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); | |
e1e588ac | 2244 | goto err; |
0f113f3e | 2245 | } |
e1e588ac | 2246 | EVP_MD_CTX_free(md_ctx); |
fe3066ee | 2247 | md_ctx = NULL; |
0f113f3e | 2248 | } else { |
7689082b | 2249 | /* aNULL, aSRP or PSK do not need public keys */ |
e1e588ac | 2250 | if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
a230b26e | 2251 | && !(alg_k & SSL_PSK)) { |
0f113f3e | 2252 | /* Might be wrong key type, check it */ |
e1e588ac | 2253 | if (ssl3_check_cert_and_algorithm(s)) { |
0f113f3e | 2254 | /* Otherwise this shouldn't happen */ |
e1e588ac | 2255 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 2256 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
e1e588ac MC |
2257 | } else { |
2258 | al = SSL_AD_DECODE_ERROR; | |
2259 | } | |
0f113f3e MC |
2260 | goto err; |
2261 | } | |
2262 | /* still data left over */ | |
73999b62 | 2263 | if (PACKET_remaining(pkt) != 0) { |
e1e588ac | 2264 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2265 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
e1e588ac | 2266 | goto err; |
0f113f3e MC |
2267 | } |
2268 | } | |
e1e588ac | 2269 | |
b9908bf9 | 2270 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2271 | err: |
7dc1c647 MC |
2272 | if (al != -1) |
2273 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 2274 | ossl_statem_set_error(s); |
fe3066ee | 2275 | EVP_MD_CTX_free(md_ctx); |
b9908bf9 | 2276 | return MSG_PROCESS_ERROR; |
0f113f3e | 2277 | } |
d02b48c6 | 2278 | |
be3583fa | 2279 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt) |
b9908bf9 MC |
2280 | { |
2281 | int ret = MSG_PROCESS_ERROR; | |
03f44b97 | 2282 | unsigned int i, name_len; |
b9908bf9 | 2283 | X509_NAME *xn = NULL; |
b6981744 | 2284 | const unsigned char *namestart, *namebytes; |
b9908bf9 | 2285 | STACK_OF(X509_NAME) *ca_sk = NULL; |
03f44b97 | 2286 | PACKET cadns; |
0f113f3e MC |
2287 | |
2288 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | |
b9908bf9 | 2289 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2290 | goto err; |
2291 | } | |
2292 | ||
03f44b97 DSH |
2293 | if (SSL_IS_TLS13(s)) { |
2294 | PACKET reqctx; | |
2295 | ||
2296 | /* Free and zero certificate types: it is not present in TLS 1.3 */ | |
2297 | OPENSSL_free(s->s3->tmp.ctype); | |
2298 | s->s3->tmp.ctype = NULL; | |
2299 | s->s3->tmp.ctype_len = 0; | |
2300 | /* TODO(TLS1.3) need to process request context, for now ignore */ | |
2301 | if (!PACKET_get_length_prefixed_1(pkt, &reqctx)) { | |
2302 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, | |
2303 | SSL_R_LENGTH_MISMATCH); | |
2304 | goto err; | |
2305 | } | |
2306 | } else { | |
2307 | PACKET ctypes; | |
75c13e78 | 2308 | |
03f44b97 DSH |
2309 | /* get the certificate types */ |
2310 | if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) { | |
2311 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2312 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, | |
2313 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e | 2314 | goto err; |
03f44b97 DSH |
2315 | } |
2316 | ||
2317 | if (!PACKET_memdup(&ctypes, &s->s3->tmp.ctype, &s->s3->tmp.ctype_len)) { | |
2318 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR); | |
2319 | goto err; | |
2320 | } | |
0f113f3e | 2321 | } |
ac112332 | 2322 | |
0f113f3e | 2323 | if (SSL_USE_SIGALGS(s)) { |
703bcee0 MC |
2324 | PACKET sigalgs; |
2325 | ||
2326 | if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) { | |
0f113f3e | 2327 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2328 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2329 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2330 | goto err; |
2331 | } | |
ac112332 | 2332 | |
9195ddcd DSH |
2333 | /* Clear certificate validity flags */ |
2334 | for (i = 0; i < SSL_PKEY_NUM; i++) | |
6383d316 | 2335 | s->s3->tmp.valid_flags[i] = 0; |
703bcee0 | 2336 | if (!tls1_save_sigalgs(s, &sigalgs)) { |
0f113f3e | 2337 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2338 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2339 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
2340 | goto err; | |
2341 | } | |
2342 | if (!tls1_process_sigalgs(s)) { | |
2343 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 2344 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2345 | goto err; |
2346 | } | |
0f113f3e MC |
2347 | } |
2348 | ||
2349 | /* get the CA RDNs */ | |
03f44b97 | 2350 | if (!PACKET_get_length_prefixed_2(pkt, &cadns)) { |
0f113f3e | 2351 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2352 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2353 | goto err; |
2354 | } | |
2355 | ||
03f44b97 DSH |
2356 | while (PACKET_remaining(&cadns)) { |
2357 | if (!PACKET_get_net_2(&cadns, &name_len) | |
2358 | || !PACKET_get_bytes(&cadns, &namebytes, name_len)) { | |
0f113f3e | 2359 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
2360 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
2361 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
2362 | goto err; |
2363 | } | |
2364 | ||
ac112332 | 2365 | namestart = namebytes; |
0f113f3e | 2366 | |
ac112332 MC |
2367 | if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes, |
2368 | name_len)) == NULL) { | |
3c33c6f6 | 2369 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2370 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); |
3c33c6f6 | 2371 | goto err; |
0f113f3e MC |
2372 | } |
2373 | ||
ac112332 | 2374 | if (namebytes != (namestart + name_len)) { |
0f113f3e | 2375 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 2376 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
2377 | SSL_R_CA_DN_LENGTH_MISMATCH); |
2378 | goto err; | |
2379 | } | |
2380 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
b9908bf9 | 2381 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2382 | goto err; |
2383 | } | |
6afef8b1 | 2384 | xn = NULL; |
0f113f3e | 2385 | } |
03f44b97 DSH |
2386 | /* TODO(TLS1.3) need to parse and process extensions, for now ignore */ |
2387 | if (SSL_IS_TLS13(s)) { | |
2388 | PACKET reqexts; | |
2389 | ||
2390 | if (!PACKET_get_length_prefixed_2(pkt, &reqexts)) { | |
2391 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2392 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, | |
2393 | SSL_R_EXT_LENGTH_MISMATCH); | |
2394 | goto err; | |
2395 | } | |
2396 | } | |
2397 | ||
2398 | if (PACKET_remaining(pkt) != 0) { | |
2399 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2400 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); | |
2401 | goto err; | |
2402 | } | |
0f113f3e | 2403 | |
0f113f3e MC |
2404 | /* we should setup a certificate to return.... */ |
2405 | s->s3->tmp.cert_req = 1; | |
222561fe | 2406 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
0f113f3e MC |
2407 | s->s3->tmp.ca_names = ca_sk; |
2408 | ca_sk = NULL; | |
2409 | ||
05c4f1d5 | 2410 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
cc273a93 | 2411 | goto done; |
0f113f3e | 2412 | err: |
fe3a3291 | 2413 | ossl_statem_set_error(s); |
cc273a93 | 2414 | done: |
6afef8b1 | 2415 | X509_NAME_free(xn); |
222561fe | 2416 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
b9908bf9 | 2417 | return ret; |
0f113f3e MC |
2418 | } |
2419 | ||
2420 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
dfeab068 | 2421 | { |
0f113f3e | 2422 | return (X509_NAME_cmp(*a, *b)); |
dfeab068 | 2423 | } |
dfeab068 | 2424 | |
be3583fa | 2425 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt) |
b9908bf9 | 2426 | { |
6df55cac | 2427 | int al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2428 | unsigned int ticklen; |
9ac6244b | 2429 | unsigned long ticket_lifetime_hint, age_add = 0; |
ec60ccc1 | 2430 | unsigned int sess_len; |
de1df7e9 | 2431 | RAW_EXTENSION *exts = NULL; |
b9908bf9 | 2432 | |
73999b62 | 2433 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
fc24f0bf | 2434 | || (SSL_IS_TLS13(s) && !PACKET_get_net_4(pkt, &age_add)) |
a230b26e | 2435 | || !PACKET_get_net_2(pkt, &ticklen) |
de1df7e9 | 2436 | || (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) != ticklen) |
1f5b44e9 MC |
2437 | || (SSL_IS_TLS13(s) |
2438 | && (ticklen == 0 || PACKET_remaining(pkt) < ticklen))) { | |
f0659bdb | 2439 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
e711da71 EK |
2440 | goto f_err; |
2441 | } | |
2442 | ||
de1df7e9 MC |
2443 | /* |
2444 | * Server is allowed to change its mind (in <=TLSv1.2) and send an empty | |
2445 | * ticket. We already checked this TLSv1.3 case above, so it should never | |
2446 | * be 0 here in that instance | |
2447 | */ | |
e711da71 | 2448 | if (ticklen == 0) |
c9de4a20 | 2449 | return MSG_PROCESS_CONTINUE_READING; |
e711da71 | 2450 | |
de1df7e9 | 2451 | /* TODO(TLS1.3): Is this a suitable test for TLS1.3? */ |
98ece4ee MC |
2452 | if (s->session->session_id_length > 0) { |
2453 | int i = s->session_ctx->session_cache_mode; | |
2454 | SSL_SESSION *new_sess; | |
2455 | /* | |
2456 | * We reused an existing session, so we need to replace it with a new | |
2457 | * one | |
2458 | */ | |
2459 | if (i & SSL_SESS_CACHE_CLIENT) { | |
2460 | /* | |
e4612d02 | 2461 | * Remove the old session from the cache. We carry on if this fails |
98ece4ee | 2462 | */ |
e4612d02 | 2463 | SSL_CTX_remove_session(s->session_ctx, s->session); |
98ece4ee MC |
2464 | } |
2465 | ||
2466 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { | |
2467 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 2468 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
98ece4ee MC |
2469 | goto f_err; |
2470 | } | |
2471 | ||
2472 | SSL_SESSION_free(s->session); | |
2473 | s->session = new_sess; | |
2474 | } | |
2475 | ||
fc24f0bf MC |
2476 | /* |
2477 | * Technically the cast to long here is not guaranteed by the C standard - | |
2478 | * but we use it elsewhere, so this should be ok. | |
2479 | */ | |
2480 | s->session->time = (long)time(NULL); | |
2481 | ||
aff8c126 RS |
2482 | OPENSSL_free(s->session->ext.tick); |
2483 | s->session->ext.tick = NULL; | |
2484 | s->session->ext.ticklen = 0; | |
e711da71 | 2485 | |
aff8c126 RS |
2486 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
2487 | if (s->session->ext.tick == NULL) { | |
b9908bf9 | 2488 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2489 | goto err; |
2490 | } | |
aff8c126 | 2491 | if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) { |
561e12bb | 2492 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 2493 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
561e12bb MC |
2494 | goto f_err; |
2495 | } | |
e711da71 | 2496 | |
aff8c126 | 2497 | s->session->ext.tick_lifetime_hint = ticket_lifetime_hint; |
fc24f0bf | 2498 | s->session->ext.tick_age_add = age_add; |
aff8c126 | 2499 | s->session->ext.ticklen = ticklen; |
de1df7e9 MC |
2500 | |
2501 | if (SSL_IS_TLS13(s)) { | |
2502 | PACKET extpkt; | |
2503 | ||
2504 | if (!PACKET_as_length_prefixed_2(pkt, &extpkt) | |
2505 | || !tls_collect_extensions(s, &extpkt, | |
2506 | EXT_TLS1_3_NEW_SESSION_TICKET, | |
fc5ece2e | 2507 | &exts, &al, NULL) |
de1df7e9 MC |
2508 | || !tls_parse_all_extensions(s, EXT_TLS1_3_NEW_SESSION_TICKET, |
2509 | exts, NULL, 0, &al)) { | |
2510 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_BAD_EXTENSION); | |
2511 | goto f_err; | |
2512 | } | |
2513 | } | |
2514 | ||
0f113f3e MC |
2515 | /* |
2516 | * There are two ways to detect a resumed ticket session. One is to set | |
2517 | * an appropriate session ID and then the server must return a match in | |
2518 | * ServerHello. This allows the normal client session ID matching to work | |
2519 | * and we know much earlier that the ticket has been accepted. The | |
2520 | * other way is to set zero length session ID when the ticket is | |
2521 | * presented and rely on the handshake to determine session resumption. | |
2522 | * We choose the former approach because this fits in with assumptions | |
2523 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is | |
2524 | * SHA256 is disabled) hash of the ticket. | |
2525 | */ | |
ec60ccc1 MC |
2526 | /* |
2527 | * TODO(size_t): we use sess_len here because EVP_Digest expects an int | |
2528 | * but s->session->session_id_length is a size_t | |
2529 | */ | |
aff8c126 | 2530 | if (!EVP_Digest(s->session->ext.tick, ticklen, |
ec60ccc1 | 2531 | s->session->session_id, &sess_len, |
d166ed8c DSH |
2532 | EVP_sha256(), NULL)) { |
2533 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB); | |
2534 | goto err; | |
2535 | } | |
ec60ccc1 | 2536 | s->session->session_id_length = sess_len; |
de1df7e9 MC |
2537 | |
2538 | /* This is a standalone message in TLSv1.3, so there is no more to read */ | |
2539 | if (SSL_IS_TLS13(s)) { | |
33d93417 | 2540 | OPENSSL_free(exts); |
de1df7e9 MC |
2541 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
2542 | return MSG_PROCESS_FINISHED_READING; | |
2543 | } | |
2544 | ||
b9908bf9 | 2545 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
2546 | f_err: |
2547 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2548 | err: | |
fe3a3291 | 2549 | ossl_statem_set_error(s); |
33d93417 | 2550 | OPENSSL_free(exts); |
b9908bf9 | 2551 | return MSG_PROCESS_ERROR; |
0f113f3e | 2552 | } |
67c8e7f4 | 2553 | |
f63e4288 MC |
2554 | /* |
2555 | * In TLSv1.3 this is called from the extensions code, otherwise it is used to | |
2556 | * parse a separate message. Returns 1 on success or 0 on failure. On failure | |
2557 | * |*al| is populated with a suitable alert code. | |
2558 | */ | |
2559 | int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al) | |
b9908bf9 | 2560 | { |
8b0e934a | 2561 | size_t resplen; |
b9908bf9 | 2562 | unsigned int type; |
b9908bf9 | 2563 | |
73999b62 | 2564 | if (!PACKET_get_1(pkt, &type) |
a230b26e | 2565 | || type != TLSEXT_STATUSTYPE_ocsp) { |
f63e4288 MC |
2566 | *al = SSL_AD_DECODE_ERROR; |
2567 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, | |
2568 | SSL_R_UNSUPPORTED_STATUS_TYPE); | |
2569 | return 0; | |
0f113f3e | 2570 | } |
56a26ce3 MC |
2571 | if (!PACKET_get_net_3_len(pkt, &resplen) |
2572 | || PACKET_remaining(pkt) != resplen) { | |
f63e4288 MC |
2573 | *al = SSL_AD_DECODE_ERROR; |
2574 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); | |
2575 | return 0; | |
0f113f3e | 2576 | } |
8cbfcc70 RS |
2577 | s->ext.ocsp.resp = OPENSSL_malloc(resplen); |
2578 | if (s->ext.ocsp.resp == NULL) { | |
f63e4288 MC |
2579 | *al = SSL_AD_INTERNAL_ERROR; |
2580 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, ERR_R_MALLOC_FAILURE); | |
2581 | return 0; | |
0f113f3e | 2582 | } |
8cbfcc70 | 2583 | if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) { |
f63e4288 MC |
2584 | *al = SSL_AD_DECODE_ERROR; |
2585 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS_BODY, SSL_R_LENGTH_MISMATCH); | |
2586 | return 0; | |
ac63710a | 2587 | } |
8cbfcc70 | 2588 | s->ext.ocsp.resp_len = resplen; |
f63e4288 MC |
2589 | |
2590 | return 1; | |
2591 | } | |
2faa1b48 | 2592 | |
f63e4288 MC |
2593 | |
2594 | MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt) | |
2595 | { | |
2596 | int al; | |
2597 | ||
2598 | if (!tls_process_cert_status_body(s, pkt, &al)) { | |
2599 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2600 | ossl_statem_set_error(s); | |
2601 | return MSG_PROCESS_ERROR; | |
2602 | } | |
2603 | ||
b9908bf9 | 2604 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e | 2605 | } |
d02b48c6 | 2606 | |
7776a36c MC |
2607 | /* |
2608 | * Perform miscellaneous checks and processing after we have received the | |
2609 | * server's initial flight. In TLS1.3 this is after the Server Finished message. | |
6530c490 MC |
2610 | * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0 |
2611 | * on failure. | |
7776a36c MC |
2612 | */ |
2613 | int tls_process_initial_server_flight(SSL *s, int *al) | |
b9908bf9 | 2614 | { |
a455d0f6 MC |
2615 | /* |
2616 | * at this point we check that we have the required stuff from | |
2617 | * the server | |
2618 | */ | |
2619 | if (!ssl3_check_cert_and_algorithm(s)) { | |
7776a36c MC |
2620 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2621 | return 0; | |
a455d0f6 MC |
2622 | } |
2623 | ||
bb1aaab4 | 2624 | /* |
aff8c126 RS |
2625 | * Call the ocsp status callback if needed. The |ext.ocsp.resp| and |
2626 | * |ext.ocsp.resp_len| values will be set if we actually received a status | |
bb1aaab4 MC |
2627 | * message, or NULL and -1 otherwise |
2628 | */ | |
aff8c126 RS |
2629 | if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing |
2630 | && s->ctx->ext.status_cb != NULL) { | |
2631 | int ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg); | |
2632 | ||
bb1aaab4 | 2633 | if (ret == 0) { |
7776a36c MC |
2634 | *al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
2635 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, | |
bb1aaab4 | 2636 | SSL_R_INVALID_STATUS_RESPONSE); |
7776a36c | 2637 | return 0; |
bb1aaab4 MC |
2638 | } |
2639 | if (ret < 0) { | |
7776a36c MC |
2640 | *al = SSL_AD_INTERNAL_ERROR; |
2641 | SSLerr(SSL_F_TLS_PROCESS_INITIAL_SERVER_FLIGHT, | |
2642 | ERR_R_MALLOC_FAILURE); | |
2643 | return 0; | |
bb1aaab4 MC |
2644 | } |
2645 | } | |
ed29e82a RP |
2646 | #ifndef OPENSSL_NO_CT |
2647 | if (s->ct_validation_callback != NULL) { | |
43341433 VD |
2648 | /* Note we validate the SCTs whether or not we abort on error */ |
2649 | if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { | |
7776a36c MC |
2650 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2651 | return 0; | |
ed29e82a RP |
2652 | } |
2653 | } | |
2654 | #endif | |
2655 | ||
7776a36c MC |
2656 | return 1; |
2657 | } | |
2658 | ||
2659 | MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt) | |
2660 | { | |
2661 | int al = SSL_AD_INTERNAL_ERROR; | |
2662 | ||
2663 | if (PACKET_remaining(pkt) > 0) { | |
2664 | /* should contain no data */ | |
2665 | al = SSL_AD_DECODE_ERROR; | |
2666 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH); | |
2667 | goto err; | |
2668 | } | |
2669 | #ifndef OPENSSL_NO_SRP | |
2670 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2671 | if (SRP_Calc_A_param(s) <= 0) { | |
2672 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC); | |
2673 | goto err; | |
2674 | } | |
2675 | } | |
2676 | #endif | |
2677 | ||
2678 | /* | |
2679 | * Error queue messages are generated directly by this function | |
2680 | */ | |
2681 | if (!tls_process_initial_server_flight(s, &al)) | |
2682 | goto err; | |
2683 | ||
473483d4 MC |
2684 | #ifndef OPENSSL_NO_SCTP |
2685 | /* Only applies to renegotiation */ | |
2686 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
a230b26e | 2687 | && s->renegotiate != 0) |
473483d4 MC |
2688 | return MSG_PROCESS_CONTINUE_PROCESSING; |
2689 | else | |
2690 | #endif | |
2691 | return MSG_PROCESS_FINISHED_READING; | |
7776a36c MC |
2692 | |
2693 | err: | |
2694 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2695 | ossl_statem_set_error(s); | |
2696 | return MSG_PROCESS_ERROR; | |
0f113f3e | 2697 | } |
176f31dd | 2698 | |
f1ec23c0 | 2699 | static int tls_construct_cke_psk_preamble(SSL *s, WPACKET *pkt, int *al) |
0f113f3e | 2700 | { |
7689082b | 2701 | #ifndef OPENSSL_NO_PSK |
13c0ec4a MC |
2702 | int ret = 0; |
2703 | /* | |
2704 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2705 | * \0-terminated identity. The last byte is for us for simulating | |
2706 | * strnlen. | |
2707 | */ | |
2708 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
2709 | size_t identitylen = 0; | |
2710 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
2711 | unsigned char *tmppsk = NULL; | |
2712 | char *tmpidentity = NULL; | |
2713 | size_t psklen = 0; | |
2714 | ||
2715 | if (s->psk_client_callback == NULL) { | |
05ec6a25 | 2716 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB); |
13c0ec4a MC |
2717 | *al = SSL_AD_INTERNAL_ERROR; |
2718 | goto err; | |
2719 | } | |
d02b48c6 | 2720 | |
13c0ec4a | 2721 | memset(identity, 0, sizeof(identity)); |
d02b48c6 | 2722 | |
13c0ec4a MC |
2723 | psklen = s->psk_client_callback(s, s->session->psk_identity_hint, |
2724 | identity, sizeof(identity) - 1, | |
2725 | psk, sizeof(psk)); | |
7689082b | 2726 | |
13c0ec4a | 2727 | if (psklen > PSK_MAX_PSK_LEN) { |
05ec6a25 | 2728 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2729 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2730 | goto err; | |
2731 | } else if (psklen == 0) { | |
05ec6a25 | 2732 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, |
13c0ec4a MC |
2733 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
2734 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2735 | goto err; | |
2736 | } | |
7689082b | 2737 | |
13c0ec4a MC |
2738 | identitylen = strlen(identity); |
2739 | if (identitylen > PSK_MAX_IDENTITY_LEN) { | |
05ec6a25 | 2740 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2741 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2742 | goto err; | |
2743 | } | |
7689082b | 2744 | |
13c0ec4a MC |
2745 | tmppsk = OPENSSL_memdup(psk, psklen); |
2746 | tmpidentity = OPENSSL_strdup(identity); | |
2747 | if (tmppsk == NULL || tmpidentity == NULL) { | |
05ec6a25 | 2748 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE); |
13c0ec4a MC |
2749 | *al = SSL_AD_INTERNAL_ERROR; |
2750 | goto err; | |
2751 | } | |
7689082b | 2752 | |
13c0ec4a MC |
2753 | OPENSSL_free(s->s3->tmp.psk); |
2754 | s->s3->tmp.psk = tmppsk; | |
2755 | s->s3->tmp.psklen = psklen; | |
2756 | tmppsk = NULL; | |
2757 | OPENSSL_free(s->session->psk_identity); | |
2758 | s->session->psk_identity = tmpidentity; | |
2759 | tmpidentity = NULL; | |
f1ec23c0 | 2760 | |
b2b3024e | 2761 | if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) { |
f1ec23c0 MC |
2762 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
2763 | *al = SSL_AD_INTERNAL_ERROR; | |
2764 | goto err; | |
2765 | } | |
7689082b | 2766 | |
13c0ec4a | 2767 | ret = 1; |
0bce0b02 | 2768 | |
13c0ec4a MC |
2769 | err: |
2770 | OPENSSL_cleanse(psk, psklen); | |
2771 | OPENSSL_cleanse(identity, sizeof(identity)); | |
2772 | OPENSSL_clear_free(tmppsk, psklen); | |
2773 | OPENSSL_clear_free(tmpidentity, identitylen); | |
d02b48c6 | 2774 | |
13c0ec4a MC |
2775 | return ret; |
2776 | #else | |
05ec6a25 | 2777 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2778 | *al = SSL_AD_INTERNAL_ERROR; |
2779 | return 0; | |
b9908bf9 | 2780 | #endif |
13c0ec4a | 2781 | } |
b9908bf9 | 2782 | |
f1ec23c0 | 2783 | static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) |
13c0ec4a | 2784 | { |
bc36ee62 | 2785 | #ifndef OPENSSL_NO_RSA |
f1ec23c0 | 2786 | unsigned char *encdata = NULL; |
13c0ec4a MC |
2787 | EVP_PKEY *pkey = NULL; |
2788 | EVP_PKEY_CTX *pctx = NULL; | |
2789 | size_t enclen; | |
2790 | unsigned char *pms = NULL; | |
2791 | size_t pmslen = 0; | |
b9908bf9 | 2792 | |
13c0ec4a MC |
2793 | if (s->session->peer == NULL) { |
2794 | /* | |
2795 | * We should always have a server certificate with SSL_kRSA. | |
2796 | */ | |
05ec6a25 | 2797 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2798 | return 0; |
2799 | } | |
0f113f3e | 2800 | |
13c0ec4a MC |
2801 | pkey = X509_get0_pubkey(s->session->peer); |
2802 | if (EVP_PKEY_get0_RSA(pkey) == NULL) { | |
05ec6a25 | 2803 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2804 | return 0; |
2805 | } | |
0f113f3e | 2806 | |
13c0ec4a MC |
2807 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
2808 | pms = OPENSSL_malloc(pmslen); | |
2809 | if (pms == NULL) { | |
05ec6a25 | 2810 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); |
13c0ec4a MC |
2811 | *al = SSL_AD_INTERNAL_ERROR; |
2812 | return 0; | |
2813 | } | |
0bce0b02 | 2814 | |
13c0ec4a MC |
2815 | pms[0] = s->client_version >> 8; |
2816 | pms[1] = s->client_version & 0xff; | |
348240c6 MC |
2817 | /* TODO(size_t): Convert this function */ |
2818 | if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) { | |
13c0ec4a MC |
2819 | goto err; |
2820 | } | |
0f113f3e | 2821 | |
13c0ec4a | 2822 | /* Fix buf for TLS and beyond */ |
f1ec23c0 MC |
2823 | if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { |
2824 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); | |
2825 | goto err; | |
2826 | } | |
13c0ec4a MC |
2827 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
2828 | if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 | |
2829 | || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { | |
05ec6a25 | 2830 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); |
13c0ec4a MC |
2831 | goto err; |
2832 | } | |
f1ec23c0 MC |
2833 | if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) |
2834 | || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { | |
05ec6a25 | 2835 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); |
13c0ec4a MC |
2836 | goto err; |
2837 | } | |
13c0ec4a MC |
2838 | EVP_PKEY_CTX_free(pctx); |
2839 | pctx = NULL; | |
0f113f3e | 2840 | |
13c0ec4a | 2841 | /* Fix buf for TLS and beyond */ |
f1ec23c0 MC |
2842 | if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { |
2843 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); | |
2844 | goto err; | |
b9908bf9 | 2845 | } |
13c0ec4a MC |
2846 | |
2847 | s->s3->tmp.pms = pms; | |
2848 | s->s3->tmp.pmslen = pmslen; | |
2849 | ||
2faa1b48 CB |
2850 | /* Log the premaster secret, if logging is enabled. */ |
2851 | if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) | |
2852 | goto err; | |
2853 | ||
13c0ec4a MC |
2854 | return 1; |
2855 | err: | |
2856 | OPENSSL_clear_free(pms, pmslen); | |
2857 | EVP_PKEY_CTX_free(pctx); | |
2858 | ||
2859 | return 0; | |
2860 | #else | |
05ec6a25 | 2861 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); |
13c0ec4a MC |
2862 | *al = SSL_AD_INTERNAL_ERROR; |
2863 | return 0; | |
f9b3bff6 | 2864 | #endif |
13c0ec4a MC |
2865 | } |
2866 | ||
f1ec23c0 | 2867 | static int tls_construct_cke_dhe(SSL *s, WPACKET *pkt, int *al) |
a8c1c704 MC |
2868 | { |
2869 | #ifndef OPENSSL_NO_DH | |
2870 | DH *dh_clnt = NULL; | |
2871 | const BIGNUM *pub_key; | |
2872 | EVP_PKEY *ckey = NULL, *skey = NULL; | |
f1ec23c0 | 2873 | unsigned char *keybytes = NULL; |
a8c1c704 MC |
2874 | |
2875 | skey = s->s3->peer_tmp; | |
f1ec23c0 MC |
2876 | if (skey == NULL) |
2877 | goto err; | |
2878 | ||
0a699a07 | 2879 | ckey = ssl_generate_pkey(skey); |
b599ce3b MC |
2880 | if (ckey == NULL) |
2881 | goto err; | |
2882 | ||
a8c1c704 MC |
2883 | dh_clnt = EVP_PKEY_get0_DH(ckey); |
2884 | ||
0f1e51ea | 2885 | if (dh_clnt == NULL || ssl_derive(s, ckey, skey, 0) == 0) |
f1ec23c0 | 2886 | goto err; |
a8c1c704 MC |
2887 | |
2888 | /* send off the data */ | |
2889 | DH_get0_key(dh_clnt, &pub_key, NULL); | |
b2b3024e | 2890 | if (!WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(pub_key), &keybytes)) |
f1ec23c0 MC |
2891 | goto err; |
2892 | ||
2893 | BN_bn2bin(pub_key, keybytes); | |
a8c1c704 MC |
2894 | EVP_PKEY_free(ckey); |
2895 | ||
2896 | return 1; | |
f1ec23c0 MC |
2897 | err: |
2898 | EVP_PKEY_free(ckey); | |
2899 | #endif | |
05ec6a25 | 2900 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR); |
a8c1c704 MC |
2901 | *al = SSL_AD_INTERNAL_ERROR; |
2902 | return 0; | |
a8c1c704 MC |
2903 | } |
2904 | ||
f1ec23c0 | 2905 | static int tls_construct_cke_ecdhe(SSL *s, WPACKET *pkt, int *al) |
67ad5aab MC |
2906 | { |
2907 | #ifndef OPENSSL_NO_EC | |
2908 | unsigned char *encodedPoint = NULL; | |
348240c6 | 2909 | size_t encoded_pt_len = 0; |
67ad5aab | 2910 | EVP_PKEY *ckey = NULL, *skey = NULL; |
f1ec23c0 | 2911 | int ret = 0; |
67ad5aab MC |
2912 | |
2913 | skey = s->s3->peer_tmp; | |
ec24630a | 2914 | if (skey == NULL) { |
05ec6a25 | 2915 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
67ad5aab MC |
2916 | return 0; |
2917 | } | |
2918 | ||
0a699a07 | 2919 | ckey = ssl_generate_pkey(skey); |
b599ce3b MC |
2920 | if (ckey == NULL) { |
2921 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_MALLOC_FAILURE); | |
2922 | goto err; | |
2923 | } | |
67ad5aab | 2924 | |
0f1e51ea | 2925 | if (ssl_derive(s, ckey, skey, 0) == 0) { |
05ec6a25 | 2926 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB); |
67ad5aab MC |
2927 | goto err; |
2928 | } | |
2929 | ||
2930 | /* Generate encoding of client key */ | |
ec24630a | 2931 | encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint); |
67ad5aab MC |
2932 | |
2933 | if (encoded_pt_len == 0) { | |
05ec6a25 | 2934 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB); |
67ad5aab MC |
2935 | goto err; |
2936 | } | |
2937 | ||
b2b3024e | 2938 | if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) { |
f1ec23c0 MC |
2939 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
2940 | goto err; | |
2941 | } | |
67ad5aab | 2942 | |
f1ec23c0 | 2943 | ret = 1; |
67ad5aab | 2944 | err: |
f1ec23c0 | 2945 | OPENSSL_free(encodedPoint); |
67ad5aab | 2946 | EVP_PKEY_free(ckey); |
f1ec23c0 | 2947 | return ret; |
67ad5aab | 2948 | #else |
05ec6a25 | 2949 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR); |
67ad5aab MC |
2950 | *al = SSL_AD_INTERNAL_ERROR; |
2951 | return 0; | |
2952 | #endif | |
2953 | } | |
2954 | ||
f1ec23c0 | 2955 | static int tls_construct_cke_gost(SSL *s, WPACKET *pkt, int *al) |
e00e0b3d MC |
2956 | { |
2957 | #ifndef OPENSSL_NO_GOST | |
2958 | /* GOST key exchange message creation */ | |
2959 | EVP_PKEY_CTX *pkey_ctx = NULL; | |
2960 | X509 *peer_cert; | |
2961 | size_t msglen; | |
2962 | unsigned int md_len; | |
2963 | unsigned char shared_ukm[32], tmp[256]; | |
2964 | EVP_MD_CTX *ukm_hash = NULL; | |
2965 | int dgst_nid = NID_id_GostR3411_94; | |
2966 | unsigned char *pms = NULL; | |
2967 | size_t pmslen = 0; | |
2968 | ||
2969 | if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) | |
2970 | dgst_nid = NID_id_GostR3411_2012_256; | |
2971 | ||
2972 | /* | |
2973 | * Get server sertificate PKEY and create ctx from it | |
2974 | */ | |
2975 | peer_cert = s->session->peer; | |
2976 | if (!peer_cert) { | |
2977 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
05ec6a25 | 2978 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, |
e00e0b3d MC |
2979 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
2980 | return 0; | |
2981 | } | |
2982 | ||
2983 | pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL); | |
2984 | if (pkey_ctx == NULL) { | |
2985 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 2986 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
e00e0b3d MC |
2987 | return 0; |
2988 | } | |
2989 | /* | |
2990 | * If we have send a certificate, and certificate key | |
2991 | * parameters match those of server certificate, use | |
2992 | * certificate key for key exchange | |
2993 | */ | |
2994 | ||
2995 | /* Otherwise, generate ephemeral key pair */ | |
2996 | pmslen = 32; | |
2997 | pms = OPENSSL_malloc(pmslen); | |
2998 | if (pms == NULL) { | |
2999 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 3000 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE); |
2f3930bc | 3001 | goto err; |
e00e0b3d MC |
3002 | } |
3003 | ||
3004 | if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 | |
348240c6 MC |
3005 | /* Generate session key |
3006 | * TODO(size_t): Convert this function | |
3007 | */ | |
3008 | || RAND_bytes(pms, (int)pmslen) <= 0) { | |
e00e0b3d | 3009 | *al = SSL_AD_INTERNAL_ERROR; |
05ec6a25 | 3010 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3011 | goto err; |
3012 | }; | |
e00e0b3d MC |
3013 | /* |
3014 | * Compute shared IV and store it in algorithm-specific context | |
3015 | * data | |
3016 | */ | |
3017 | ukm_hash = EVP_MD_CTX_new(); | |
3018 | if (ukm_hash == NULL | |
a230b26e EK |
3019 | || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0 |
3020 | || EVP_DigestUpdate(ukm_hash, s->s3->client_random, | |
3021 | SSL3_RANDOM_SIZE) <= 0 | |
3022 | || EVP_DigestUpdate(ukm_hash, s->s3->server_random, | |
3023 | SSL3_RANDOM_SIZE) <= 0 | |
3024 | || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { | |
e00e0b3d | 3025 | *al = SSL_AD_INTERNAL_ERROR; |
05ec6a25 | 3026 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3027 | goto err; |
3028 | } | |
3029 | EVP_MD_CTX_free(ukm_hash); | |
3030 | ukm_hash = NULL; | |
3031 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | |
3032 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { | |
3033 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 3034 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
3035 | goto err; |
3036 | } | |
3037 | /* Make GOST keytransport blob message */ | |
3038 | /* | |
3039 | * Encapsulate it into sequence | |
3040 | */ | |
e00e0b3d MC |
3041 | msglen = 255; |
3042 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { | |
3043 | *al = SSL_AD_INTERNAL_ERROR; | |
05ec6a25 | 3044 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG); |
e00e0b3d MC |
3045 | goto err; |
3046 | } | |
f1ec23c0 | 3047 | |
08029dfa MC |
3048 | if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) |
3049 | || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81)) | |
b2b3024e | 3050 | || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) { |
f1ec23c0 MC |
3051 | *al = SSL_AD_INTERNAL_ERROR; |
3052 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); | |
3053 | goto err; | |
e00e0b3d | 3054 | } |
f1ec23c0 | 3055 | |
e00e0b3d MC |
3056 | EVP_PKEY_CTX_free(pkey_ctx); |
3057 | s->s3->tmp.pms = pms; | |
3058 | s->s3->tmp.pmslen = pmslen; | |
3059 | ||
3060 | return 1; | |
3061 | err: | |
3062 | EVP_PKEY_CTX_free(pkey_ctx); | |
3063 | OPENSSL_clear_free(pms, pmslen); | |
3064 | EVP_MD_CTX_free(ukm_hash); | |
3065 | return 0; | |
3066 | #else | |
05ec6a25 | 3067 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR); |
e00e0b3d MC |
3068 | *al = SSL_AD_INTERNAL_ERROR; |
3069 | return 0; | |
3070 | #endif | |
3071 | } | |
3072 | ||
f1ec23c0 | 3073 | static int tls_construct_cke_srp(SSL *s, WPACKET *pkt, int *al) |
840a2bf8 | 3074 | { |
8b9546c7 | 3075 | #ifndef OPENSSL_NO_SRP |
f1ec23c0 MC |
3076 | unsigned char *abytes = NULL; |
3077 | ||
3078 | if (s->srp_ctx.A == NULL | |
b2b3024e MC |
3079 | || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A), |
3080 | &abytes)) { | |
05ec6a25 | 3081 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
3082 | return 0; |
3083 | } | |
f1ec23c0 MC |
3084 | BN_bn2bin(s->srp_ctx.A, abytes); |
3085 | ||
840a2bf8 MC |
3086 | OPENSSL_free(s->session->srp_username); |
3087 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); | |
3088 | if (s->session->srp_username == NULL) { | |
05ec6a25 | 3089 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE); |
840a2bf8 MC |
3090 | return 0; |
3091 | } | |
3092 | ||
3093 | return 1; | |
3094 | #else | |
05ec6a25 | 3095 | SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR); |
840a2bf8 MC |
3096 | *al = SSL_AD_INTERNAL_ERROR; |
3097 | return 0; | |
3098 | #endif | |
3099 | } | |
3100 | ||
7cea05dc | 3101 | int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt) |
13c0ec4a | 3102 | { |
13c0ec4a MC |
3103 | unsigned long alg_k; |
3104 | int al = -1; | |
3105 | ||
f1ec23c0 | 3106 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
13c0ec4a | 3107 | |
13c0ec4a | 3108 | if ((alg_k & SSL_PSK) |
7cea05dc | 3109 | && !tls_construct_cke_psk_preamble(s, pkt, &al)) |
13c0ec4a MC |
3110 | goto err; |
3111 | ||
f1ec23c0 | 3112 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
7cea05dc | 3113 | if (!tls_construct_cke_rsa(s, pkt, &al)) |
13c0ec4a | 3114 | goto err; |
a8c1c704 | 3115 | } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
7cea05dc | 3116 | if (!tls_construct_cke_dhe(s, pkt, &al)) |
b9908bf9 | 3117 | goto err; |
67ad5aab | 3118 | } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
7cea05dc | 3119 | if (!tls_construct_cke_ecdhe(s, pkt, &al)) |
ce0c1f2b | 3120 | goto err; |
e00e0b3d | 3121 | } else if (alg_k & SSL_kGOST) { |
7cea05dc | 3122 | if (!tls_construct_cke_gost(s, pkt, &al)) |
a71edf3b | 3123 | goto err; |
840a2bf8 | 3124 | } else if (alg_k & SSL_kSRP) { |
7cea05dc | 3125 | if (!tls_construct_cke_srp(s, pkt, &al)) |
69f68237 | 3126 | goto err; |
4a424545 | 3127 | } else if (!(alg_k & SSL_kPSK)) { |
b9908bf9 MC |
3128 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
3129 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
3130 | goto err; | |
3131 | } | |
3132 | ||
b9908bf9 | 3133 | return 1; |
0f113f3e | 3134 | err: |
13c0ec4a MC |
3135 | if (al != -1) |
3136 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
0bce0b02 | 3137 | OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen); |
76106e60 | 3138 | s->s3->tmp.pms = NULL; |
7689082b DSH |
3139 | #ifndef OPENSSL_NO_PSK |
3140 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); | |
3141 | s->s3->tmp.psk = NULL; | |
0f113f3e | 3142 | #endif |
b9908bf9 MC |
3143 | return 0; |
3144 | } | |
3145 | ||
3146 | int tls_client_key_exchange_post_work(SSL *s) | |
3147 | { | |
3148 | unsigned char *pms = NULL; | |
3149 | size_t pmslen = 0; | |
3150 | ||
6f137370 MC |
3151 | pms = s->s3->tmp.pms; |
3152 | pmslen = s->s3->tmp.pmslen; | |
3153 | ||
b9908bf9 MC |
3154 | #ifndef OPENSSL_NO_SRP |
3155 | /* Check for SRP */ | |
3156 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
3157 | if (!srp_generate_client_master_secret(s)) { | |
3158 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, | |
3159 | ERR_R_INTERNAL_ERROR); | |
3160 | goto err; | |
3161 | } | |
3162 | return 1; | |
3163 | } | |
3164 | #endif | |
b9908bf9 MC |
3165 | |
3166 | if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { | |
3167 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3168 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE); | |
3169 | goto err; | |
3170 | } | |
3171 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { | |
3172 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3173 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR); | |
6f137370 MC |
3174 | /* ssl_generate_master_secret frees the pms even on error */ |
3175 | pms = NULL; | |
3176 | pmslen = 0; | |
b9908bf9 MC |
3177 | goto err; |
3178 | } | |
6f137370 MC |
3179 | pms = NULL; |
3180 | pmslen = 0; | |
473483d4 MC |
3181 | |
3182 | #ifndef OPENSSL_NO_SCTP | |
3183 | if (SSL_IS_DTLS(s)) { | |
3184 | unsigned char sctpauthkey[64]; | |
3185 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
3186 | ||
3187 | /* | |
3188 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
3189 | * used. | |
3190 | */ | |
141eb8c6 MC |
3191 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
3192 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
473483d4 MC |
3193 | |
3194 | if (SSL_export_keying_material(s, sctpauthkey, | |
a230b26e EK |
3195 | sizeof(sctpauthkey), labelbuffer, |
3196 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
473483d4 MC |
3197 | goto err; |
3198 | ||
3199 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
3200 | sizeof(sctpauthkey), sctpauthkey); | |
3201 | } | |
3202 | #endif | |
3203 | ||
b9908bf9 MC |
3204 | return 1; |
3205 | err: | |
3206 | OPENSSL_clear_free(pms, pmslen); | |
3207 | s->s3->tmp.pms = NULL; | |
3208 | return 0; | |
0f113f3e | 3209 | } |
d02b48c6 | 3210 | |
0f113f3e MC |
3211 | /* |
3212 | * Check a certificate can be used for client authentication. Currently check | |
3213 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
3214 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 DSH |
3215 | */ |
3216 | static int ssl3_check_client_certificate(SSL *s) | |
0f113f3e | 3217 | { |
0f113f3e | 3218 | /* If no suitable signature algorithm can't use certificate */ |
ad4dd362 | 3219 | if (!tls_choose_sigalg(s, NULL) || s->s3->tmp.sigalg == NULL) |
0f113f3e MC |
3220 | return 0; |
3221 | /* | |
3222 | * If strict mode check suitability of chain before using it. This also | |
3223 | * adjusts suite B digest if necessary. | |
3224 | */ | |
3225 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
3226 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
3227 | return 0; | |
0f113f3e MC |
3228 | return 1; |
3229 | } | |
0d609395 | 3230 | |
be3583fa | 3231 | WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst) |
0f113f3e MC |
3232 | { |
3233 | X509 *x509 = NULL; | |
3234 | EVP_PKEY *pkey = NULL; | |
3235 | int i; | |
3236 | ||
b9908bf9 | 3237 | if (wst == WORK_MORE_A) { |
0f113f3e MC |
3238 | /* Let cert callback update client certificates if required */ |
3239 | if (s->cert->cert_cb) { | |
3240 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
3241 | if (i < 0) { | |
3242 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3243 | return WORK_MORE_A; |
0f113f3e MC |
3244 | } |
3245 | if (i == 0) { | |
3246 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 3247 | ossl_statem_set_error(s); |
0f113f3e MC |
3248 | return 0; |
3249 | } | |
3250 | s->rwstate = SSL_NOTHING; | |
3251 | } | |
3252 | if (ssl3_check_client_certificate(s)) | |
b9908bf9 MC |
3253 | return WORK_FINISHED_CONTINUE; |
3254 | ||
3255 | /* Fall through to WORK_MORE_B */ | |
3256 | wst = WORK_MORE_B; | |
0f113f3e MC |
3257 | } |
3258 | ||
3259 | /* We need to get a client cert */ | |
b9908bf9 | 3260 | if (wst == WORK_MORE_B) { |
0f113f3e MC |
3261 | /* |
3262 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
3263 | * return(-1); We then get retied later | |
3264 | */ | |
0f113f3e MC |
3265 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
3266 | if (i < 0) { | |
3267 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 3268 | return WORK_MORE_B; |
0f113f3e MC |
3269 | } |
3270 | s->rwstate = SSL_NOTHING; | |
3271 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
0f113f3e MC |
3272 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
3273 | i = 0; | |
3274 | } else if (i == 1) { | |
3275 | i = 0; | |
b9908bf9 | 3276 | SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE, |
0f113f3e MC |
3277 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
3278 | } | |
3279 | ||
222561fe | 3280 | X509_free(x509); |
25aaa98a | 3281 | EVP_PKEY_free(pkey); |
0f113f3e MC |
3282 | if (i && !ssl3_check_client_certificate(s)) |
3283 | i = 0; | |
3284 | if (i == 0) { | |
3285 | if (s->version == SSL3_VERSION) { | |
3286 | s->s3->tmp.cert_req = 0; | |
3287 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); | |
b9908bf9 | 3288 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3289 | } else { |
3290 | s->s3->tmp.cert_req = 2; | |
124037fd | 3291 | if (!ssl3_digest_cached_records(s, 0)) { |
dab18ab5 | 3292 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
fe3a3291 | 3293 | ossl_statem_set_error(s); |
dab18ab5 DSH |
3294 | return 0; |
3295 | } | |
0f113f3e MC |
3296 | } |
3297 | } | |
3298 | ||
b9908bf9 | 3299 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
3300 | } |
3301 | ||
b9908bf9 MC |
3302 | /* Shouldn't ever get here */ |
3303 | return WORK_ERROR; | |
3304 | } | |
3305 | ||
7cea05dc | 3306 | int tls_construct_client_certificate(SSL *s, WPACKET *pkt) |
b9908bf9 | 3307 | { |
0baed5e9 | 3308 | int al = SSL_AD_INTERNAL_ERROR; |
e96e0f8e MC |
3309 | |
3310 | /* | |
3311 | * TODO(TLS1.3): For now we must put an empty context. Needs to be filled in | |
3312 | * later | |
3313 | */ | |
3314 | if ((SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) | |
3315 | || !ssl3_output_cert_chain(s, pkt, | |
b90506e9 | 3316 | (s->s3->tmp.cert_req == 2) ? NULL |
e96e0f8e MC |
3317 | : s->cert->key, |
3318 | &al)) { | |
b9908bf9 | 3319 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
f7e393be MC |
3320 | goto err; |
3321 | } | |
3322 | ||
3323 | if (SSL_IS_TLS13(s) | |
3324 | && SSL_IS_FIRST_HANDSHAKE(s) | |
3325 | && (!s->method->ssl3_enc->change_cipher_state(s, | |
3326 | SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { | |
3327 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, | |
3328 | SSL_R_CANNOT_CHANGE_CIPHER); | |
3329 | goto err; | |
0f113f3e | 3330 | } |
b9908bf9 MC |
3331 | |
3332 | return 1; | |
f7e393be MC |
3333 | err: |
3334 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
3335 | return 0; | |
0f113f3e MC |
3336 | } |
3337 | ||
3338 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
d02b48c6 | 3339 | |
36d16f8e | 3340 | int ssl3_check_cert_and_algorithm(SSL *s) |
0f113f3e | 3341 | { |
60f43e9e RL |
3342 | int i; |
3343 | #ifndef OPENSSL_NO_EC | |
3344 | int idx; | |
3345 | #endif | |
0f113f3e MC |
3346 | long alg_k, alg_a; |
3347 | EVP_PKEY *pkey = NULL; | |
26c79d56 | 3348 | int al = SSL_AD_HANDSHAKE_FAILURE; |
d02b48c6 | 3349 | |
0f113f3e MC |
3350 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
3351 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 3352 | |
0f113f3e | 3353 | /* we don't have a certificate */ |
55a9a16f | 3354 | if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK)) |
0f113f3e | 3355 | return (1); |
d02b48c6 | 3356 | |
0f113f3e | 3357 | /* This is the passed certificate */ |
d02b48c6 | 3358 | |
10bf4fc2 | 3359 | #ifndef OPENSSL_NO_EC |
60f43e9e | 3360 | idx = s->session->peer_type; |
0f113f3e | 3361 | if (idx == SSL_PKEY_ECC) { |
a273c6ee | 3362 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) { |
0f113f3e MC |
3363 | /* check failed */ |
3364 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); | |
3365 | goto f_err; | |
3366 | } else { | |
3367 | return 1; | |
3368 | } | |
3369 | } else if (alg_a & SSL_aECDSA) { | |
3370 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3371 | SSL_R_MISSING_ECDSA_SIGNING_CERT); | |
3372 | goto f_err; | |
0f113f3e MC |
3373 | } |
3374 | #endif | |
8382fd3a | 3375 | pkey = X509_get0_pubkey(s->session->peer); |
a273c6ee | 3376 | i = X509_certificate_type(s->session->peer, pkey); |
0f113f3e MC |
3377 | |
3378 | /* Check that we have a certificate if we require one */ | |
3379 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { | |
3380 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3381 | SSL_R_MISSING_RSA_SIGNING_CERT); | |
3382 | goto f_err; | |
3383 | } | |
bc36ee62 | 3384 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
3385 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
3386 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3387 | SSL_R_MISSING_DSA_SIGNING_CERT); | |
3388 | goto f_err; | |
3389 | } | |
d02b48c6 | 3390 | #endif |
bc36ee62 | 3391 | #ifndef OPENSSL_NO_RSA |
361a1191 KR |
3392 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && |
3393 | !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { | |
3394 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3395 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
3396 | goto f_err; | |
0f113f3e | 3397 | } |
79df9d62 | 3398 | #endif |
bc36ee62 | 3399 | #ifndef OPENSSL_NO_DH |
fb79abe3 | 3400 | if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) { |
26c79d56 KR |
3401 | al = SSL_AD_INTERNAL_ERROR; |
3402 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
0f113f3e | 3403 | goto f_err; |
0f113f3e | 3404 | } |
d02b48c6 RE |
3405 | #endif |
3406 | ||
0f113f3e MC |
3407 | return (1); |
3408 | f_err: | |
26c79d56 | 3409 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
0f113f3e MC |
3410 | return (0); |
3411 | } | |
3412 | ||
e481f9b9 | 3413 | #ifndef OPENSSL_NO_NEXTPROTONEG |
7cea05dc | 3414 | int tls_construct_next_proto(SSL *s, WPACKET *pkt) |
b9908bf9 | 3415 | { |
15e6be6c MC |
3416 | size_t len, padding_len; |
3417 | unsigned char *padding = NULL; | |
15e6be6c | 3418 | |
aff8c126 | 3419 | len = s->ext.npn_len; |
b9908bf9 | 3420 | padding_len = 32 - ((len + 2) % 32); |
15e6be6c | 3421 | |
aff8c126 | 3422 | if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len) |
7cea05dc | 3423 | || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) { |
15e6be6c MC |
3424 | SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR); |
3425 | goto err; | |
3426 | } | |
3427 | ||
3428 | memset(padding, 0, padding_len); | |
3429 | ||
b9908bf9 | 3430 | return 1; |
15e6be6c | 3431 | err: |
15e6be6c MC |
3432 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
3433 | return 0; | |
b9908bf9 | 3434 | } |
6434abbf | 3435 | #endif |
368888bc | 3436 | |
c7f47786 MC |
3437 | MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt) |
3438 | { | |
3439 | if (PACKET_remaining(pkt) > 0) { | |
3440 | /* should contain no data */ | |
3441 | SSLerr(SSL_F_TLS_PROCESS_HELLO_REQ, SSL_R_LENGTH_MISMATCH); | |
3442 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
3443 | ossl_statem_set_error(s); | |
3444 | return MSG_PROCESS_ERROR; | |
3445 | } | |
3446 | ||
3447 | /* | |
1f04f23e MC |
3448 | * This is a historical discrepancy (not in the RFC) maintained for |
3449 | * compatibility reasons. If a TLS client receives a HelloRequest it will | |
3450 | * attempt an abbreviated handshake. However if a DTLS client receives a | |
3451 | * HelloRequest it will do a full handshake. Either behaviour is reasonable | |
3452 | * but doing one for TLS and another for DTLS is odd. | |
c7f47786 MC |
3453 | */ |
3454 | if (SSL_IS_DTLS(s)) | |
3455 | SSL_renegotiate(s); | |
3456 | else | |
3457 | SSL_renegotiate_abbreviated(s); | |
3458 | ||
3459 | return MSG_PROCESS_FINISHED_READING; | |
3460 | } | |
3461 | ||
e46f2334 MC |
3462 | static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt) |
3463 | { | |
3464 | int al = SSL_AD_INTERNAL_ERROR; | |
3465 | PACKET extensions; | |
3434f40b | 3466 | RAW_EXTENSION *rawexts = NULL; |
e46f2334 | 3467 | |
e46f2334 MC |
3468 | if (!PACKET_as_length_prefixed_2(pkt, &extensions)) { |
3469 | al = SSL_AD_DECODE_ERROR; | |
3470 | SSLerr(SSL_F_TLS_PROCESS_ENCRYPTED_EXTENSIONS, SSL_R_LENGTH_MISMATCH); | |
3471 | goto err; | |
3472 | } | |
3473 | ||
e96e0f8e | 3474 | if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
fc5ece2e | 3475 | &rawexts, &al, NULL) |
e96e0f8e | 3476 | || !tls_parse_all_extensions(s, EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
f97d4c37 | 3477 | rawexts, NULL, 0, &al)) |
3434f40b MC |
3478 | goto err; |
3479 | ||
1b0286a3 | 3480 | OPENSSL_free(rawexts); |
e46f2334 MC |
3481 | return MSG_PROCESS_CONTINUE_READING; |
3482 | ||
3483 | err: | |
3484 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
3485 | ossl_statem_set_error(s); | |
1b0286a3 | 3486 | OPENSSL_free(rawexts); |
e46f2334 MC |
3487 | return MSG_PROCESS_ERROR; |
3488 | } | |
3489 | ||
368888bc | 3490 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
0f113f3e MC |
3491 | { |
3492 | int i = 0; | |
368888bc | 3493 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
3494 | if (s->ctx->client_cert_engine) { |
3495 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | |
3496 | SSL_get_client_CA_list(s), | |
3497 | px509, ppkey, NULL, NULL, NULL); | |
3498 | if (i != 0) | |
3499 | return i; | |
3500 | } | |
3501 | #endif | |
3502 | if (s->ctx->client_cert_cb) | |
3503 | i = s->ctx->client_cert_cb(s, px509, ppkey); | |
3504 | return i; | |
3505 | } | |
d45ba43d | 3506 | |
ae2f7b37 | 3507 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, WPACKET *pkt) |
d45ba43d | 3508 | { |
2c7b4dbc MC |
3509 | int i; |
3510 | size_t totlen = 0, len, maxlen; | |
d45ba43d MC |
3511 | int empty_reneg_info_scsv = !s->renegotiate; |
3512 | /* Set disabled masks for this session */ | |
3513 | ssl_set_client_disabled(s); | |
3514 | ||
3515 | if (sk == NULL) | |
3516 | return (0); | |
d45ba43d | 3517 | |
2c7b4dbc MC |
3518 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
3519 | # if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6 | |
3520 | # error Max cipher length too short | |
3521 | # endif | |
3522 | /* | |
3523 | * Some servers hang if client hello > 256 bytes as hack workaround | |
3524 | * chop number of supported ciphers to keep it well below this if we | |
3525 | * use TLS v1.2 | |
3526 | */ | |
3527 | if (TLS1_get_version(s) >= TLS1_2_VERSION) | |
3528 | maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | |
3529 | else | |
3530 | #endif | |
3531 | /* Maximum length that can be stored in 2 bytes. Length must be even */ | |
3532 | maxlen = 0xfffe; | |
3533 | ||
3534 | if (empty_reneg_info_scsv) | |
3535 | maxlen -= 2; | |
3536 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) | |
3537 | maxlen -= 2; | |
3538 | ||
3539 | for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) { | |
3540 | const SSL_CIPHER *c; | |
3541 | ||
d45ba43d MC |
3542 | c = sk_SSL_CIPHER_value(sk, i); |
3543 | /* Skip disabled ciphers */ | |
3544 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) | |
3545 | continue; | |
2c7b4dbc MC |
3546 | |
3547 | if (!s->method->put_cipher_by_char(c, pkt, &len)) { | |
3548 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3549 | return 0; | |
3550 | } | |
3551 | ||
3552 | totlen += len; | |
d45ba43d | 3553 | } |
2c7b4dbc MC |
3554 | |
3555 | if (totlen == 0) { | |
3556 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, SSL_R_NO_CIPHERS_AVAILABLE); | |
3557 | return 0; | |
3558 | } | |
3559 | ||
3560 | if (totlen != 0) { | |
d45ba43d MC |
3561 | if (empty_reneg_info_scsv) { |
3562 | static SSL_CIPHER scsv = { | |
3563 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3564 | }; | |
2c7b4dbc MC |
3565 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
3566 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3567 | return 0; | |
3568 | } | |
d45ba43d MC |
3569 | } |
3570 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
3571 | static SSL_CIPHER scsv = { | |
3572 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
3573 | }; | |
2c7b4dbc MC |
3574 | if (!s->method->put_cipher_by_char(&scsv, pkt, &len)) { |
3575 | SSLerr(SSL_F_SSL_CIPHER_LIST_TO_BYTES, ERR_R_INTERNAL_ERROR); | |
3576 | return 0; | |
3577 | } | |
d45ba43d MC |
3578 | } |
3579 | } | |
3580 | ||
2c7b4dbc | 3581 | return 1; |
d45ba43d | 3582 | } |
ef6c191b MC |
3583 | |
3584 | int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt) | |
3585 | { | |
3586 | if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY | |
3587 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) { | |
3588 | SSLerr(SSL_F_TLS_CONSTRUCT_END_OF_EARLY_DATA, | |
3589 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
3590 | return 0; | |
3591 | } | |
3592 | ||
3593 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; | |
3594 | return 1; | |
3595 | } |