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