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