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