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