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