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