]>
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 MC |
270 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE); |
271 | SSLerr(SSL_F_READ_STATE_MACHINE, 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: | |
592 | return CCS_MAX_LENGTH; | |
593 | ||
594 | case TLS_ST_CR_SESSION_TICKET: | |
595 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
596 | ||
597 | case TLS_ST_CR_FINISHED: | |
598 | return FINISHED_MAX_LENGTH; | |
599 | ||
600 | default: | |
601 | /* Shouldn't happen */ | |
602 | break; | |
603 | } | |
604 | ||
605 | return 0; | |
606 | } | |
607 | ||
608 | /* | |
609 | * Process a message that the client has been received from the server. | |
610 | */ | |
8481f583 | 611 | MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt) |
61ae935a | 612 | { |
d6f1a6e9 | 613 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
614 | |
615 | switch(st->hand_state) { | |
616 | case TLS_ST_CR_SRVR_HELLO: | |
617 | return tls_process_server_hello(s, pkt); | |
618 | ||
619 | case DTLS_ST_CR_HELLO_VERIFY_REQUEST: | |
620 | return dtls_process_hello_verify(s, pkt); | |
621 | ||
622 | case TLS_ST_CR_CERT: | |
623 | return tls_process_server_certificate(s, pkt); | |
624 | ||
625 | case TLS_ST_CR_CERT_STATUS: | |
626 | return tls_process_cert_status(s, pkt); | |
627 | ||
628 | case TLS_ST_CR_KEY_EXCH: | |
629 | return tls_process_key_exchange(s, pkt); | |
630 | ||
631 | case TLS_ST_CR_CERT_REQ: | |
632 | return tls_process_certificate_request(s, pkt); | |
633 | ||
634 | case TLS_ST_CR_SRVR_DONE: | |
635 | return tls_process_server_done(s, pkt); | |
636 | ||
637 | case TLS_ST_CR_CHANGE: | |
638 | return tls_process_change_cipher_spec(s, pkt); | |
639 | ||
640 | case TLS_ST_CR_SESSION_TICKET: | |
641 | return tls_process_new_session_ticket(s, pkt); | |
642 | ||
643 | case TLS_ST_CR_FINISHED: | |
644 | return tls_process_finished(s, pkt); | |
645 | ||
646 | default: | |
647 | /* Shouldn't happen */ | |
648 | break; | |
649 | } | |
650 | ||
651 | return MSG_PROCESS_ERROR; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Perform any further processing required following the receipt of a message | |
656 | * from the server | |
657 | */ | |
8481f583 | 658 | WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst) |
61ae935a | 659 | { |
d6f1a6e9 | 660 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
661 | |
662 | switch(st->hand_state) { | |
05c4f1d5 MC |
663 | case TLS_ST_CR_CERT_REQ: |
664 | return tls_prepare_client_certificate(s, wst); | |
665 | ||
61ae935a MC |
666 | #ifndef OPENSSL_NO_SCTP |
667 | case TLS_ST_CR_SRVR_DONE: | |
668 | /* We only get here if we are using SCTP and we are renegotiating */ | |
669 | if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
670 | s->s3->in_read_app_data = 2; | |
671 | s->rwstate = SSL_READING; | |
672 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
673 | BIO_set_retry_read(SSL_get_rbio(s)); | |
fe3a3291 | 674 | ossl_statem_set_sctp_read_sock(s, 1); |
61ae935a MC |
675 | return WORK_MORE_A; |
676 | } | |
fe3a3291 | 677 | ossl_statem_set_sctp_read_sock(s, 0); |
61ae935a MC |
678 | return WORK_FINISHED_STOP; |
679 | #endif | |
680 | ||
61ae935a MC |
681 | default: |
682 | break; | |
683 | } | |
684 | ||
685 | /* Shouldn't happen */ | |
686 | return WORK_ERROR; | |
687 | } | |
688 | ||
b9908bf9 | 689 | int tls_construct_client_hello(SSL *s) |
0f113f3e MC |
690 | { |
691 | unsigned char *buf; | |
692 | unsigned char *p, *d; | |
693 | int i; | |
4fa52141 | 694 | int protverr; |
0f113f3e MC |
695 | unsigned long l; |
696 | int al = 0; | |
09b6c2ef | 697 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
698 | int j; |
699 | SSL_COMP *comp; | |
700 | #endif | |
b9908bf9 | 701 | SSL_SESSION *sess = s->session; |
0f113f3e MC |
702 | |
703 | buf = (unsigned char *)s->init_buf->data; | |
0f113f3e | 704 | |
b9908bf9 | 705 | /* Work out what SSL/TLS/DTLS version to use */ |
4fa52141 VD |
706 | protverr = ssl_set_client_hello_version(s); |
707 | if (protverr != 0) { | |
708 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr); | |
b9908bf9 | 709 | goto err; |
4fa52141 | 710 | } |
0f113f3e | 711 | |
ccae4a15 FI |
712 | if ((sess == NULL) || |
713 | !ssl_version_supported(s, sess->ssl_version) || | |
0f113f3e | 714 | /* |
b9908bf9 MC |
715 | * In the case of EAP-FAST, we can have a pre-shared |
716 | * "ticket" without a session ID. | |
0f113f3e | 717 | */ |
b9908bf9 MC |
718 | (!sess->session_id_length && !sess->tlsext_tick) || |
719 | (sess->not_resumable)) { | |
720 | if (!ssl_get_new_session(s, 0)) | |
e1b568dd | 721 | goto err; |
b9908bf9 MC |
722 | } |
723 | /* else use the pre-loaded session */ | |
0f113f3e | 724 | |
b9908bf9 | 725 | p = s->s3->client_random; |
0f113f3e | 726 | |
b9908bf9 MC |
727 | /* |
728 | * for DTLS if client_random is initialized, reuse it, we are | |
729 | * required to use same upon reply to HelloVerify | |
730 | */ | |
731 | if (SSL_IS_DTLS(s)) { | |
732 | size_t idx; | |
733 | i = 1; | |
734 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { | |
735 | if (p[idx]) { | |
736 | i = 0; | |
737 | break; | |
0f113f3e | 738 | } |
0f113f3e | 739 | } |
b9908bf9 MC |
740 | } else |
741 | i = 1; | |
0f113f3e | 742 | |
b9908bf9 MC |
743 | if (i && ssl_fill_hello_random(s, 0, p, |
744 | sizeof(s->s3->client_random)) <= 0) | |
745 | goto err; | |
746 | ||
747 | /* Do the message type and length last */ | |
748 | d = p = ssl_handshake_start(s); | |
749 | ||
750 | /*- | |
751 | * version indicates the negotiated version: for example from | |
752 | * an SSLv2/v3 compatible client hello). The client_version | |
753 | * field is the maximum version we permit and it is also | |
754 | * used in RSA encrypted premaster secrets. Some servers can | |
755 | * choke if we initially report a higher version then | |
756 | * renegotiate to a lower one in the premaster secret. This | |
757 | * didn't happen with TLS 1.0 as most servers supported it | |
758 | * but it can with TLS 1.1 or later if the server only supports | |
759 | * 1.0. | |
760 | * | |
761 | * Possible scenario with previous logic: | |
762 | * 1. Client hello indicates TLS 1.2 | |
763 | * 2. Server hello says TLS 1.0 | |
764 | * 3. RSA encrypted premaster secret uses 1.2. | |
8483a003 | 765 | * 4. Handshake proceeds using TLS 1.0. |
b9908bf9 MC |
766 | * 5. Server sends hello request to renegotiate. |
767 | * 6. Client hello indicates TLS v1.0 as we now | |
768 | * know that is maximum server supports. | |
769 | * 7. Server chokes on RSA encrypted premaster secret | |
770 | * containing version 1.0. | |
771 | * | |
772 | * For interoperability it should be OK to always use the | |
773 | * maximum version we support in client hello and then rely | |
774 | * on the checking of version to ensure the servers isn't | |
775 | * being inconsistent: for example initially negotiating with | |
776 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
777 | * client_version in client hello and not resetting it to | |
778 | * the negotiated version. | |
779 | */ | |
780 | *(p++) = s->client_version >> 8; | |
781 | *(p++) = s->client_version & 0xff; | |
782 | ||
783 | /* Random stuff */ | |
784 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); | |
785 | p += SSL3_RANDOM_SIZE; | |
786 | ||
787 | /* Session ID */ | |
788 | if (s->new_session) | |
789 | i = 0; | |
790 | else | |
791 | i = s->session->session_id_length; | |
792 | *(p++) = i; | |
793 | if (i != 0) { | |
794 | if (i > (int)sizeof(s->session->session_id)) { | |
795 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
796 | goto err; | |
0f113f3e | 797 | } |
b9908bf9 MC |
798 | memcpy(p, s->session->session_id, i); |
799 | p += i; | |
800 | } | |
0f113f3e | 801 | |
b9908bf9 MC |
802 | /* cookie stuff for DTLS */ |
803 | if (SSL_IS_DTLS(s)) { | |
804 | if (s->d1->cookie_len > sizeof(s->d1->cookie)) { | |
805 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
0f113f3e MC |
806 | goto err; |
807 | } | |
b9908bf9 MC |
808 | *(p++) = s->d1->cookie_len; |
809 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
810 | p += s->d1->cookie_len; | |
811 | } | |
812 | ||
813 | /* Ciphers supported */ | |
814 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2])); | |
815 | if (i == 0) { | |
816 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); | |
817 | goto err; | |
818 | } | |
800e1cd9 | 819 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
b9908bf9 MC |
820 | /* |
821 | * Some servers hang if client hello > 256 bytes as hack workaround | |
822 | * chop number of supported ciphers to keep it well below this if we | |
823 | * use TLS v1.2 | |
824 | */ | |
825 | if (TLS1_get_version(s) >= TLS1_2_VERSION | |
826 | && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) | |
827 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | |
0f113f3e | 828 | #endif |
b9908bf9 MC |
829 | s2n(i, p); |
830 | p += i; | |
0f113f3e | 831 | |
b9908bf9 | 832 | /* COMPRESSION */ |
09b6c2ef | 833 | #ifdef OPENSSL_NO_COMP |
b9908bf9 | 834 | *(p++) = 1; |
09b6c2ef | 835 | #else |
566dda07 | 836 | |
b9908bf9 MC |
837 | if (!ssl_allow_compression(s) || !s->ctx->comp_methods) |
838 | j = 0; | |
839 | else | |
840 | j = sk_SSL_COMP_num(s->ctx->comp_methods); | |
841 | *(p++) = 1 + j; | |
842 | for (i = 0; i < j; i++) { | |
843 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); | |
844 | *(p++) = comp->id; | |
845 | } | |
09b6c2ef | 846 | #endif |
b9908bf9 | 847 | *(p++) = 0; /* Add the NULL method */ |
761772d7 | 848 | |
b9908bf9 MC |
849 | /* TLS extensions */ |
850 | if (ssl_prepare_clienthello_tlsext(s) <= 0) { | |
851 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); | |
852 | goto err; | |
853 | } | |
854 | if ((p = | |
855 | ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, | |
856 | &al)) == NULL) { | |
857 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
858 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
859 | goto err; | |
860 | } | |
0f113f3e | 861 | |
b9908bf9 MC |
862 | l = p - d; |
863 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) { | |
864 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
865 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
866 | goto err; | |
0f113f3e MC |
867 | } |
868 | ||
b9908bf9 | 869 | return 1; |
0f113f3e | 870 | err: |
fe3a3291 | 871 | ossl_statem_set_error(s); |
b9908bf9 | 872 | return 0; |
0f113f3e | 873 | } |
d02b48c6 | 874 | |
be3583fa | 875 | MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt) |
8ba708e5 MC |
876 | { |
877 | int al; | |
878 | unsigned int cookie_len; | |
879 | PACKET cookiepkt; | |
880 | ||
881 | if (!PACKET_forward(pkt, 2) | |
882 | || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { | |
883 | al = SSL_AD_DECODE_ERROR; | |
884 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
885 | goto f_err; | |
886 | } | |
887 | ||
888 | cookie_len = PACKET_remaining(&cookiepkt); | |
889 | if (cookie_len > sizeof(s->d1->cookie)) { | |
890 | al = SSL_AD_ILLEGAL_PARAMETER; | |
891 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG); | |
892 | goto f_err; | |
893 | } | |
894 | ||
895 | if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { | |
896 | al = SSL_AD_DECODE_ERROR; | |
897 | SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH); | |
898 | goto f_err; | |
899 | } | |
900 | s->d1->cookie_len = cookie_len; | |
901 | ||
902 | return MSG_PROCESS_FINISHED_READING; | |
903 | f_err: | |
904 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 905 | ossl_statem_set_error(s); |
8ba708e5 MC |
906 | return MSG_PROCESS_ERROR; |
907 | } | |
908 | ||
be3583fa | 909 | MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) |
b9908bf9 MC |
910 | { |
911 | STACK_OF(SSL_CIPHER) *sk; | |
912 | const SSL_CIPHER *c; | |
73999b62 | 913 | PACKET session_id; |
b9908bf9 | 914 | size_t session_id_len; |
b6981744 | 915 | const unsigned char *cipherchars; |
b9908bf9 MC |
916 | int i, al = SSL_AD_INTERNAL_ERROR; |
917 | unsigned int compression; | |
4fa52141 VD |
918 | unsigned int sversion; |
919 | int protverr; | |
b9908bf9 MC |
920 | #ifndef OPENSSL_NO_COMP |
921 | SSL_COMP *comp; | |
922 | #endif | |
923 | ||
4fa52141 VD |
924 | if (!PACKET_get_net_2(pkt, &sversion)) { |
925 | al = SSL_AD_DECODE_ERROR; | |
926 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); | |
927 | goto f_err; | |
928 | } | |
50932c4a | 929 | |
4fa52141 VD |
930 | protverr = ssl_choose_client_version(s, sversion); |
931 | if (protverr != 0) { | |
932 | al = SSL_AD_PROTOCOL_VERSION; | |
933 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr); | |
934 | goto f_err; | |
0f113f3e | 935 | } |
0f113f3e MC |
936 | |
937 | /* load the server hello data */ | |
938 | /* load the server random */ | |
73999b62 | 939 | if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
50932c4a | 940 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 941 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
942 | goto f_err; |
943 | } | |
0f113f3e MC |
944 | |
945 | s->hit = 0; | |
946 | ||
fc5ce51d | 947 | /* Get the session-id. */ |
73999b62 | 948 | if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { |
fc5ce51d | 949 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 950 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
951 | goto f_err; |
952 | } | |
953 | session_id_len = PACKET_remaining(&session_id); | |
954 | if (session_id_len > sizeof s->session->session_id | |
955 | || session_id_len > SSL3_SESSION_ID_SIZE) { | |
0f113f3e | 956 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 957 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
0f113f3e MC |
958 | goto f_err; |
959 | } | |
e481f9b9 | 960 | |
73999b62 | 961 | if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
f0659bdb | 962 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
fc5ce51d EK |
963 | al = SSL_AD_DECODE_ERROR; |
964 | goto f_err; | |
965 | } | |
966 | ||
0f113f3e | 967 | /* |
6e3d0153 EK |
968 | * Check if we can resume the session based on external pre-shared secret. |
969 | * EAP-FAST (RFC 4851) supports two types of session resumption. | |
970 | * Resumption based on server-side state works with session IDs. | |
971 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
972 | * works by overriding the SessionTicket extension at the application | |
973 | * layer, and does not send a session ID. (We do not know whether EAP-FAST | |
974 | * servers would honour the session ID.) Therefore, the session ID alone | |
975 | * is not a reliable indicator of session resumption, so we first check if | |
976 | * we can resume, and later peek at the next handshake message to see if the | |
977 | * server wants to resume. | |
0f113f3e | 978 | */ |
6e3d0153 EK |
979 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && |
980 | s->session->tlsext_tick) { | |
4a640fb6 | 981 | const SSL_CIPHER *pref_cipher = NULL; |
0f113f3e MC |
982 | s->session->master_key_length = sizeof(s->session->master_key); |
983 | if (s->tls_session_secret_cb(s, s->session->master_key, | |
984 | &s->session->master_key_length, | |
985 | NULL, &pref_cipher, | |
986 | s->tls_session_secret_cb_arg)) { | |
987 | s->session->cipher = pref_cipher ? | |
50932c4a | 988 | pref_cipher : ssl_get_cipher_by_char(s, cipherchars); |
6e3d0153 | 989 | } else { |
b9908bf9 | 990 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR); |
6e3d0153 EK |
991 | al = SSL_AD_INTERNAL_ERROR; |
992 | goto f_err; | |
0f113f3e | 993 | } |
50932c4a MC |
994 | } |
995 | ||
fc5ce51d EK |
996 | if (session_id_len != 0 && session_id_len == s->session->session_id_length |
997 | && memcmp(PACKET_data(&session_id), s->session->session_id, | |
998 | session_id_len) == 0) { | |
0f113f3e MC |
999 | if (s->sid_ctx_length != s->session->sid_ctx_length |
1000 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { | |
1001 | /* actually a client application bug */ | |
1002 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1003 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1004 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
1005 | goto f_err; | |
1006 | } | |
1007 | s->hit = 1; | |
6e3d0153 | 1008 | } else { |
0f113f3e | 1009 | /* |
6e3d0153 EK |
1010 | * If we were trying for session-id reuse but the server |
1011 | * didn't echo the ID, make a new SSL_SESSION. | |
1012 | * In the case of EAP-FAST and PAC, we do not send a session ID, | |
1013 | * so the PAC-based session secret is always preserved. It'll be | |
1014 | * overwritten if the server refuses resumption. | |
0f113f3e MC |
1015 | */ |
1016 | if (s->session->session_id_length > 0) { | |
4f6eaa59 | 1017 | s->ctx->stats.sess_miss++; |
0f113f3e MC |
1018 | if (!ssl_get_new_session(s, 0)) { |
1019 | goto f_err; | |
1020 | } | |
1021 | } | |
50932c4a | 1022 | |
ccae4a15 | 1023 | s->session->ssl_version = s->version; |
fc5ce51d EK |
1024 | s->session->session_id_length = session_id_len; |
1025 | /* session_id_len could be 0 */ | |
1026 | memcpy(s->session->session_id, PACKET_data(&session_id), | |
1027 | session_id_len); | |
0f113f3e | 1028 | } |
fc5ce51d | 1029 | |
ccae4a15 FI |
1030 | /* Session version and negotiated protocol version should match */ |
1031 | if (s->version != s->session->ssl_version) { | |
1032 | al = SSL_AD_PROTOCOL_VERSION; | |
1033 | ||
1034 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, | |
1035 | SSL_R_SSL_SESSION_VERSION_MISMATCH); | |
1036 | goto f_err; | |
1037 | } | |
1038 | ||
50932c4a | 1039 | c = ssl_get_cipher_by_char(s, cipherchars); |
0f113f3e MC |
1040 | if (c == NULL) { |
1041 | /* unknown cipher */ | |
1042 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1043 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); |
0f113f3e MC |
1044 | goto f_err; |
1045 | } | |
0f113f3e | 1046 | /* |
3eb2aff4 KR |
1047 | * Now that we know the version, update the check to see if it's an allowed |
1048 | * version. | |
1049 | */ | |
1050 | s->s3->tmp.min_ver = s->version; | |
1051 | s->s3->tmp.max_ver = s->version; | |
1052 | /* | |
1053 | * If it is a disabled cipher we either didn't send it in client hello, | |
1054 | * or it's not allowed for the selected protocol. So we return an error. | |
0f113f3e MC |
1055 | */ |
1056 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { | |
1057 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1058 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1059 | goto f_err; |
1060 | } | |
0f113f3e MC |
1061 | |
1062 | sk = ssl_get_ciphers_by_id(s); | |
1063 | i = sk_SSL_CIPHER_find(sk, c); | |
1064 | if (i < 0) { | |
1065 | /* we did not say we would use this cipher */ | |
1066 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1067 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
0f113f3e MC |
1068 | goto f_err; |
1069 | } | |
1070 | ||
1071 | /* | |
1072 | * Depending on the session caching (internal/external), the cipher | |
1073 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1074 | * set and use it for comparison. | |
1075 | */ | |
1076 | if (s->session->cipher) | |
1077 | s->session->cipher_id = s->session->cipher->id; | |
1078 | if (s->hit && (s->session->cipher_id != c->id)) { | |
9e9858d1 | 1079 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1080 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
9e9858d1 RS |
1081 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
1082 | goto f_err; | |
0f113f3e MC |
1083 | } |
1084 | s->s3->tmp.new_cipher = c; | |
0f113f3e MC |
1085 | /* lets get the compression algorithm */ |
1086 | /* COMPRESSION */ | |
73999b62 | 1087 | if (!PACKET_get_1(pkt, &compression)) { |
f0659bdb | 1088 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH); |
50932c4a MC |
1089 | al = SSL_AD_DECODE_ERROR; |
1090 | goto f_err; | |
1091 | } | |
09b6c2ef | 1092 | #ifdef OPENSSL_NO_COMP |
fc5ce51d | 1093 | if (compression != 0) { |
0f113f3e | 1094 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1095 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1096 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1097 | goto f_err; | |
1098 | } | |
1099 | /* | |
1100 | * If compression is disabled we'd better not try to resume a session | |
1101 | * using compression. | |
1102 | */ | |
1103 | if (s->session->compress_meth != 0) { | |
b9908bf9 | 1104 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
0f113f3e MC |
1105 | goto f_err; |
1106 | } | |
09b6c2ef | 1107 | #else |
fc5ce51d | 1108 | if (s->hit && compression != s->session->compress_meth) { |
0f113f3e | 1109 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1110 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1111 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
1112 | goto f_err; | |
1113 | } | |
fc5ce51d | 1114 | if (compression == 0) |
0f113f3e MC |
1115 | comp = NULL; |
1116 | else if (!ssl_allow_compression(s)) { | |
1117 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1118 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
0f113f3e | 1119 | goto f_err; |
fc5ce51d EK |
1120 | } else { |
1121 | comp = ssl3_comp_find(s->ctx->comp_methods, compression); | |
1122 | } | |
0f113f3e | 1123 | |
fc5ce51d | 1124 | if (compression != 0 && comp == NULL) { |
0f113f3e | 1125 | al = SSL_AD_ILLEGAL_PARAMETER; |
b9908bf9 | 1126 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, |
0f113f3e MC |
1127 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
1128 | goto f_err; | |
1129 | } else { | |
1130 | s->s3->tmp.new_compression = comp; | |
1131 | } | |
09b6c2ef | 1132 | #endif |
761772d7 | 1133 | |
0f113f3e | 1134 | /* TLS extensions */ |
73999b62 | 1135 | if (!ssl_parse_serverhello_tlsext(s, pkt)) { |
b9908bf9 | 1136 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT); |
0f113f3e MC |
1137 | goto err; |
1138 | } | |
0f113f3e | 1139 | |
73999b62 | 1140 | if (PACKET_remaining(pkt) != 0) { |
0f113f3e MC |
1141 | /* wrong packet length */ |
1142 | al = SSL_AD_DECODE_ERROR; | |
b9908bf9 | 1143 | SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); |
0f113f3e MC |
1144 | goto f_err; |
1145 | } | |
1146 | ||
8723588e MC |
1147 | #ifndef OPENSSL_NO_SCTP |
1148 | if (SSL_IS_DTLS(s) && s->hit) { | |
1149 | unsigned char sctpauthkey[64]; | |
1150 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
1151 | ||
1152 | /* | |
1153 | * Add new shared key for SCTP-Auth, will be ignored if | |
1154 | * no SCTP used. | |
1155 | */ | |
141eb8c6 MC |
1156 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
1157 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
8723588e MC |
1158 | |
1159 | if (SSL_export_keying_material(s, sctpauthkey, | |
1160 | sizeof(sctpauthkey), | |
1161 | labelbuffer, | |
1162 | sizeof(labelbuffer), NULL, 0, | |
1163 | 0) <= 0) | |
1164 | goto err; | |
1165 | ||
1166 | BIO_ctrl(SSL_get_wbio(s), | |
1167 | BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
1168 | sizeof(sctpauthkey), sctpauthkey); | |
1169 | } | |
1170 | #endif | |
1171 | ||
b9908bf9 | 1172 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1173 | f_err: |
1174 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1175 | err: | |
fe3a3291 | 1176 | ossl_statem_set_error(s); |
b9908bf9 | 1177 | return MSG_PROCESS_ERROR; |
0f113f3e | 1178 | } |
d02b48c6 | 1179 | |
be3583fa | 1180 | MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1181 | { |
1182 | int al, i, ret = MSG_PROCESS_ERROR, exp_idx; | |
1183 | unsigned long cert_list_len, cert_len; | |
1184 | X509 *x = NULL; | |
b6981744 | 1185 | const unsigned char *certstart, *certbytes; |
b9908bf9 MC |
1186 | STACK_OF(X509) *sk = NULL; |
1187 | EVP_PKEY *pkey = NULL; | |
0f113f3e MC |
1188 | |
1189 | if ((sk = sk_X509_new_null()) == NULL) { | |
b9908bf9 | 1190 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1191 | goto err; |
0f113f3e MC |
1192 | } |
1193 | ||
73999b62 MC |
1194 | if (!PACKET_get_net_3(pkt, &cert_list_len) |
1195 | || PACKET_remaining(pkt) != cert_list_len) { | |
0f113f3e | 1196 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1197 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1198 | goto f_err; |
1199 | } | |
73999b62 MC |
1200 | while (PACKET_remaining(pkt)) { |
1201 | if (!PACKET_get_net_3(pkt, &cert_len) | |
1202 | || !PACKET_get_bytes(pkt, &certbytes, cert_len)) { | |
0f113f3e | 1203 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1204 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1205 | SSL_R_CERT_LENGTH_MISMATCH); |
1206 | goto f_err; | |
1207 | } | |
1208 | ||
df758a85 MC |
1209 | certstart = certbytes; |
1210 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len); | |
0f113f3e MC |
1211 | if (x == NULL) { |
1212 | al = SSL_AD_BAD_CERTIFICATE; | |
b9908bf9 | 1213 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
0f113f3e MC |
1214 | goto f_err; |
1215 | } | |
df758a85 | 1216 | if (certbytes != (certstart + cert_len)) { |
0f113f3e | 1217 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1218 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1219 | SSL_R_CERT_LENGTH_MISMATCH); |
1220 | goto f_err; | |
1221 | } | |
1222 | if (!sk_X509_push(sk, x)) { | |
b9908bf9 | 1223 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
cc273a93 | 1224 | goto err; |
0f113f3e MC |
1225 | } |
1226 | x = NULL; | |
0f113f3e MC |
1227 | } |
1228 | ||
1229 | i = ssl_verify_cert_chain(s, sk); | |
c636c1c4 | 1230 | if ((s->verify_mode & SSL_VERIFY_PEER) && i <= 0) { |
0f113f3e | 1231 | al = ssl_verify_alarm_type(s->verify_result); |
b9908bf9 | 1232 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1233 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
1234 | goto f_err; | |
1235 | } | |
1236 | ERR_clear_error(); /* but we keep s->verify_result */ | |
1237 | if (i > 1) { | |
b9908bf9 | 1238 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i); |
0f113f3e MC |
1239 | al = SSL_AD_HANDSHAKE_FAILURE; |
1240 | goto f_err; | |
1241 | } | |
1242 | ||
c34b0f99 | 1243 | s->session->peer_chain = sk; |
0f113f3e MC |
1244 | /* |
1245 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
d4d78943 | 1246 | * which we don't include in statem_srvr.c |
0f113f3e MC |
1247 | */ |
1248 | x = sk_X509_value(sk, 0); | |
1249 | sk = NULL; | |
1250 | /* | |
1251 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end | |
1252 | */ | |
1253 | ||
8382fd3a | 1254 | pkey = X509_get0_pubkey(x); |
0f113f3e | 1255 | |
55a9a16f | 1256 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
0f113f3e MC |
1257 | x = NULL; |
1258 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1259 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1260 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
1261 | goto f_err; | |
1262 | } | |
1263 | ||
1264 | i = ssl_cert_type(x, pkey); | |
55a9a16f | 1265 | if (i < 0) { |
0f113f3e MC |
1266 | x = NULL; |
1267 | al = SSL3_AL_FATAL; | |
b9908bf9 | 1268 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
0f113f3e MC |
1269 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
1270 | goto f_err; | |
1271 | } | |
1272 | ||
55a9a16f | 1273 | exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
e44380a9 DB |
1274 | if (exp_idx >= 0 && i != exp_idx |
1275 | && (exp_idx != SSL_PKEY_GOST_EC || | |
1276 | (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256 | |
1277 | && i != SSL_PKEY_GOST01))) { | |
55a9a16f MC |
1278 | x = NULL; |
1279 | al = SSL_AD_ILLEGAL_PARAMETER; | |
b9908bf9 | 1280 | SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, |
55a9a16f MC |
1281 | SSL_R_WRONG_CERTIFICATE_TYPE); |
1282 | goto f_err; | |
0f113f3e | 1283 | } |
a273c6ee | 1284 | s->session->peer_type = i; |
55a9a16f MC |
1285 | |
1286 | X509_free(s->session->peer); | |
05f0fb9f | 1287 | X509_up_ref(x); |
55a9a16f | 1288 | s->session->peer = x; |
0f113f3e MC |
1289 | s->session->verify_result = s->verify_result; |
1290 | ||
1291 | x = NULL; | |
b9908bf9 | 1292 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 RS |
1293 | goto done; |
1294 | ||
0f113f3e | 1295 | f_err: |
66696478 | 1296 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
cc273a93 | 1297 | err: |
fe3a3291 | 1298 | ossl_statem_set_error(s); |
66696478 | 1299 | done: |
0f113f3e MC |
1300 | X509_free(x); |
1301 | sk_X509_pop_free(sk, X509_free); | |
b9908bf9 | 1302 | return ret; |
0f113f3e | 1303 | } |
d02b48c6 | 1304 | |
be3583fa | 1305 | MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt) |
b9908bf9 | 1306 | { |
6e59a892 | 1307 | EVP_MD_CTX *md_ctx; |
192e4bbb | 1308 | int al, j; |
b9908bf9 MC |
1309 | long alg_k, alg_a; |
1310 | EVP_PKEY *pkey = NULL; | |
1311 | const EVP_MD *md = NULL; | |
1312 | #ifndef OPENSSL_NO_RSA | |
1313 | RSA *rsa = NULL; | |
1314 | #endif | |
b9908bf9 | 1315 | #ifndef OPENSSL_NO_EC |
61dd9f7a | 1316 | EVP_PKEY_CTX *pctx = NULL; |
b9908bf9 | 1317 | #endif |
73999b62 | 1318 | PACKET save_param_start, signature; |
b9908bf9 | 1319 | |
bfb0641f | 1320 | md_ctx = EVP_MD_CTX_new(); |
6e59a892 RL |
1321 | if (md_ctx == NULL) { |
1322 | al = SSL_AD_INTERNAL_ERROR; | |
1323 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1324 | goto f_err; | |
1325 | } | |
b9908bf9 MC |
1326 | |
1327 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
1328 | ||
73999b62 | 1329 | save_param_start = *pkt; |
8d92c1f8 | 1330 | |
fb79abe3 | 1331 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
61dd9f7a DSH |
1332 | EVP_PKEY_free(s->s3->peer_tmp); |
1333 | s->s3->peer_tmp = NULL; | |
d02b48c6 | 1334 | #endif |
8d92c1f8 | 1335 | |
0f113f3e | 1336 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
d02b48c6 | 1337 | |
0f113f3e | 1338 | al = SSL_AD_DECODE_ERROR; |
f2be92b9 | 1339 | |
ddac1974 | 1340 | #ifndef OPENSSL_NO_PSK |
7689082b DSH |
1341 | /* PSK ciphersuites are preceded by an identity hint */ |
1342 | if (alg_k & SSL_PSK) { | |
32942870 | 1343 | PACKET psk_identity_hint; |
73999b62 | 1344 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { |
f0659bdb | 1345 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1346 | goto f_err; |
1347 | } | |
0f113f3e MC |
1348 | |
1349 | /* | |
1350 | * Store PSK identity hint for later use, hint is used in | |
1351 | * ssl3_send_client_key_exchange. Assume that the maximum length of | |
1352 | * a PSK identity hint can be as long as the maximum length of a PSK | |
1353 | * identity. | |
1354 | */ | |
32942870 | 1355 | if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { |
0f113f3e | 1356 | al = SSL_AD_HANDSHAKE_FAILURE; |
b9908bf9 | 1357 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); |
0f113f3e MC |
1358 | goto f_err; |
1359 | } | |
8098fc56 | 1360 | |
558ceae7 DSH |
1361 | if (PACKET_remaining(&psk_identity_hint) == 0) { |
1362 | OPENSSL_free(s->session->psk_identity_hint); | |
1363 | s->session->psk_identity_hint = NULL; | |
1364 | } else if (!PACKET_strndup(&psk_identity_hint, | |
32942870 EK |
1365 | &s->session->psk_identity_hint)) { |
1366 | al = SSL_AD_INTERNAL_ERROR; | |
1367 | goto f_err; | |
0f113f3e | 1368 | } |
7689082b DSH |
1369 | } |
1370 | ||
1371 | /* Nothing else to do for plain PSK or RSAPSK */ | |
1372 | if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { | |
0f113f3e MC |
1373 | } else |
1374 | #endif /* !OPENSSL_NO_PSK */ | |
bbf431cb MC |
1375 | /* |
1376 | * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_* | |
1377 | * options | |
1378 | */ | |
1379 | if (0) { | |
1380 | } | |
edc032b5 | 1381 | #ifndef OPENSSL_NO_SRP |
bbf431cb | 1382 | else if (alg_k & SSL_kSRP) { |
32942870 | 1383 | PACKET prime, generator, salt, server_pub; |
73999b62 MC |
1384 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
1385 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1386 | || !PACKET_get_length_prefixed_1(pkt, &salt) | |
1387 | || !PACKET_get_length_prefixed_2(pkt, &server_pub)) { | |
f0659bdb | 1388 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1389 | goto f_err; |
1390 | } | |
0f113f3e | 1391 | |
32942870 EK |
1392 | if ((s->srp_ctx.N = |
1393 | BN_bin2bn(PACKET_data(&prime), | |
1394 | PACKET_remaining(&prime), NULL)) == NULL | |
1395 | || (s->srp_ctx.g = | |
1396 | BN_bin2bn(PACKET_data(&generator), | |
1397 | PACKET_remaining(&generator), NULL)) == NULL | |
1398 | || (s->srp_ctx.s = | |
1399 | BN_bin2bn(PACKET_data(&salt), | |
1400 | PACKET_remaining(&salt), NULL)) == NULL | |
1401 | || (s->srp_ctx.B = | |
1402 | BN_bin2bn(PACKET_data(&server_pub), | |
1403 | PACKET_remaining(&server_pub), NULL)) == NULL) { | |
f0659bdb | 1404 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
0f113f3e MC |
1405 | goto err; |
1406 | } | |
0f113f3e MC |
1407 | |
1408 | if (!srp_verify_server_param(s, &al)) { | |
b9908bf9 | 1409 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); |
0f113f3e MC |
1410 | goto f_err; |
1411 | } | |
0989790b | 1412 | |
edc032b5 | 1413 | /* We must check if there is a certificate */ |
8df53b7a | 1414 | if (alg_a & (SSL_aRSA|SSL_aDSS)) |
8382fd3a | 1415 | pkey = X509_get0_pubkey(s->session->peer); |
0f113f3e | 1416 | } |
361a1191 | 1417 | #endif /* !OPENSSL_NO_SRP */ |
bc36ee62 | 1418 | #ifndef OPENSSL_NO_DH |
7689082b | 1419 | else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
32942870 | 1420 | PACKET prime, generator, pub_key; |
ee85fc1d | 1421 | EVP_PKEY *peer_tmp = NULL; |
0f113f3e | 1422 | |
ee85fc1d MC |
1423 | DH *dh = NULL; |
1424 | BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; | |
fb79abe3 | 1425 | |
73999b62 MC |
1426 | if (!PACKET_get_length_prefixed_2(pkt, &prime) |
1427 | || !PACKET_get_length_prefixed_2(pkt, &generator) | |
1428 | || !PACKET_get_length_prefixed_2(pkt, &pub_key)) { | |
f0659bdb | 1429 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1430 | goto f_err; |
1431 | } | |
0f113f3e | 1432 | |
ee85fc1d | 1433 | peer_tmp = EVP_PKEY_new(); |
fb79abe3 DSH |
1434 | dh = DH_new(); |
1435 | ||
ee85fc1d MC |
1436 | if (peer_tmp == NULL || dh == NULL) { |
1437 | al = SSL_AD_INTERNAL_ERROR; | |
fb79abe3 | 1438 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
ee85fc1d | 1439 | goto dherr; |
0f113f3e | 1440 | } |
0f113f3e | 1441 | |
0aeddcfa MC |
1442 | p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL); |
1443 | g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator), | |
1444 | NULL); | |
1445 | bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key), | |
1446 | NULL); | |
1447 | if (p == NULL || g == NULL || bnpub_key == NULL) { | |
f0659bdb | 1448 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
ee85fc1d | 1449 | goto dherr; |
0f113f3e | 1450 | } |
0f113f3e | 1451 | |
0aeddcfa | 1452 | if (BN_is_zero(p) || BN_is_zero(g) || BN_is_zero(bnpub_key)) { |
f0659bdb | 1453 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE); |
ee85fc1d | 1454 | goto dherr; |
61e72d76 GL |
1455 | } |
1456 | ||
0aeddcfa | 1457 | if (!DH_set0_pqg(dh, p, NULL, g)) { |
ee85fc1d | 1458 | al = SSL_AD_INTERNAL_ERROR; |
0aeddcfa | 1459 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
ee85fc1d | 1460 | goto dherr; |
0aeddcfa | 1461 | } |
e99ab8ff | 1462 | p = g = NULL; |
0aeddcfa MC |
1463 | |
1464 | if (!DH_set0_key(dh, bnpub_key, NULL)) { | |
ee85fc1d | 1465 | al = SSL_AD_INTERNAL_ERROR; |
0aeddcfa | 1466 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB); |
ee85fc1d | 1467 | goto dherr; |
0aeddcfa | 1468 | } |
e99ab8ff | 1469 | bnpub_key = NULL; |
0aeddcfa | 1470 | |
0f113f3e MC |
1471 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { |
1472 | al = SSL_AD_HANDSHAKE_FAILURE; | |
b9908bf9 | 1473 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL); |
ee85fc1d MC |
1474 | goto dherr; |
1475 | } | |
1476 | ||
1477 | if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) { | |
1478 | al = SSL_AD_INTERNAL_ERROR; | |
1479 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
1480 | goto dherr; | |
0f113f3e | 1481 | } |
ee85fc1d MC |
1482 | |
1483 | s->s3->peer_tmp = peer_tmp; | |
1484 | ||
1485 | goto dhend; | |
1486 | dherr: | |
1487 | BN_free(p); | |
1488 | BN_free(g); | |
1489 | BN_free(bnpub_key); | |
1490 | DH_free(dh); | |
1491 | EVP_PKEY_free(peer_tmp); | |
1492 | goto f_err; | |
1493 | dhend: | |
1494 | /* | |
1495 | * FIXME: This makes assumptions about which ciphersuites come with | |
1496 | * public keys. We should have a less ad-hoc way of doing this | |
1497 | */ | |
8df53b7a | 1498 | if (alg_a & (SSL_aRSA|SSL_aDSS)) |
8382fd3a | 1499 | pkey = X509_get0_pubkey(s->session->peer); |
0f113f3e | 1500 | /* else anonymous DH, so no certificate or pkey. */ |
0f113f3e MC |
1501 | } |
1502 | #endif /* !OPENSSL_NO_DH */ | |
ea262260 | 1503 | |
10bf4fc2 | 1504 | #ifndef OPENSSL_NO_EC |
7689082b | 1505 | else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
32942870 | 1506 | PACKET encoded_pt; |
b6981744 | 1507 | const unsigned char *ecparams; |
61dd9f7a | 1508 | int curve_nid; |
0f113f3e | 1509 | |
0f113f3e MC |
1510 | /* |
1511 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
32942870 | 1512 | * public key. For now we only support named (not generic) curves and |
8098fc56 | 1513 | * ECParameters in this case is just three bytes. |
0f113f3e | 1514 | */ |
73999b62 | 1515 | if (!PACKET_get_bytes(pkt, &ecparams, 3)) { |
f0659bdb | 1516 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
0f113f3e MC |
1517 | goto f_err; |
1518 | } | |
1519 | /* | |
1520 | * Check curve is one of our preferences, if not server has sent an | |
1521 | * invalid curve. ECParameters is 3 bytes. | |
1522 | */ | |
32942870 | 1523 | if (!tls1_check_curve(s, ecparams, 3)) { |
f0659bdb | 1524 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE); |
0f113f3e MC |
1525 | goto f_err; |
1526 | } | |
1527 | ||
61dd9f7a DSH |
1528 | curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2)); |
1529 | if (curve_nid == 0) { | |
0f113f3e | 1530 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 1531 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, |
0f113f3e MC |
1532 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
1533 | goto f_err; | |
1534 | } | |
1535 | ||
61dd9f7a DSH |
1536 | /* Set up EVP_PKEY with named curve as parameters */ |
1537 | pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); | |
1538 | if (pctx == NULL | |
1539 | || EVP_PKEY_paramgen_init(pctx) <= 0 | |
1540 | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0 | |
1541 | || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { | |
1542 | al = SSL_AD_INTERNAL_ERROR; | |
1543 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
1544 | goto f_err; | |
0f113f3e | 1545 | } |
61dd9f7a DSH |
1546 | EVP_PKEY_CTX_free(pctx); |
1547 | pctx = NULL; | |
0f113f3e | 1548 | |
73999b62 | 1549 | if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { |
f0659bdb | 1550 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
8098fc56 MC |
1551 | goto f_err; |
1552 | } | |
0f113f3e | 1553 | |
61dd9f7a DSH |
1554 | if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s->s3->peer_tmp), |
1555 | PACKET_data(&encoded_pt), | |
cae41364 | 1556 | PACKET_remaining(&encoded_pt), NULL) == 0) { |
f0659bdb | 1557 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); |
0f113f3e MC |
1558 | goto f_err; |
1559 | } | |
0f113f3e MC |
1560 | |
1561 | /* | |
1562 | * The ECC/TLS specification does not mention the use of DSA to sign | |
1563 | * ECParameters in the server key exchange message. We do support RSA | |
1564 | * and ECDSA. | |
1565 | */ | |
1566 | if (0) ; | |
1567 | # ifndef OPENSSL_NO_RSA | |
1568 | else if (alg_a & SSL_aRSA) | |
8382fd3a | 1569 | pkey = X509_get0_pubkey(s->session->peer); |
0f113f3e | 1570 | # endif |
10bf4fc2 | 1571 | # ifndef OPENSSL_NO_EC |
0f113f3e | 1572 | else if (alg_a & SSL_aECDSA) |
8382fd3a | 1573 | pkey = X509_get0_pubkey(s->session->peer); |
0f113f3e MC |
1574 | # endif |
1575 | /* else anonymous ECDH, so no certificate or pkey. */ | |
0f113f3e MC |
1576 | } else if (alg_k) { |
1577 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
b9908bf9 | 1578 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
0f113f3e MC |
1579 | goto f_err; |
1580 | } | |
10bf4fc2 | 1581 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e | 1582 | |
0f113f3e MC |
1583 | /* if it was signed, check the signature */ |
1584 | if (pkey != NULL) { | |
32942870 EK |
1585 | PACKET params; |
1586 | /* | |
1587 | * |pkt| now points to the beginning of the signature, so the difference | |
1588 | * equals the length of the parameters. | |
1589 | */ | |
1590 | if (!PACKET_get_sub_packet(&save_param_start, ¶ms, | |
1591 | PACKET_remaining(&save_param_start) - | |
73999b62 | 1592 | PACKET_remaining(pkt))) { |
32942870 | 1593 | al = SSL_AD_INTERNAL_ERROR; |
f0659bdb | 1594 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
32942870 EK |
1595 | goto f_err; |
1596 | } | |
1597 | ||
0f113f3e | 1598 | if (SSL_USE_SIGALGS(s)) { |
b6981744 | 1599 | const unsigned char *sigalgs; |
0f113f3e | 1600 | int rv; |
73999b62 | 1601 | if (!PACKET_get_bytes(pkt, &sigalgs, 2)) { |
f0659bdb | 1602 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
0f113f3e MC |
1603 | goto f_err; |
1604 | } | |
32942870 | 1605 | rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey); |
0f113f3e MC |
1606 | if (rv == -1) |
1607 | goto err; | |
1608 | else if (rv == 0) { | |
1609 | goto f_err; | |
1610 | } | |
a2f9200f | 1611 | #ifdef SSL_DEBUG |
0f113f3e MC |
1612 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
1613 | #endif | |
3aeb9348 | 1614 | } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
192e4bbb | 1615 | md = EVP_md5_sha1(); |
32942870 | 1616 | } else { |
0f113f3e | 1617 | md = EVP_sha1(); |
32942870 | 1618 | } |
0f113f3e | 1619 | |
73999b62 MC |
1620 | if (!PACKET_get_length_prefixed_2(pkt, &signature) |
1621 | || PACKET_remaining(pkt) != 0) { | |
f0659bdb | 1622 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1623 | goto f_err; |
1624 | } | |
0f113f3e | 1625 | j = EVP_PKEY_size(pkey); |
8098fc56 | 1626 | if (j < 0) { |
b9908bf9 | 1627 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
8098fc56 MC |
1628 | goto f_err; |
1629 | } | |
0f113f3e MC |
1630 | |
1631 | /* | |
8098fc56 | 1632 | * Check signature length |
0f113f3e | 1633 | */ |
32942870 | 1634 | if (PACKET_remaining(&signature) > (size_t)j) { |
0f113f3e | 1635 | /* wrong packet length */ |
b9908bf9 | 1636 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); |
0f113f3e MC |
1637 | goto f_err; |
1638 | } | |
6e59a892 RL |
1639 | if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0 |
1640 | || EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]), | |
192e4bbb | 1641 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 | 1642 | || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]), |
192e4bbb | 1643 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 | 1644 | || EVP_VerifyUpdate(md_ctx, PACKET_data(¶ms), |
192e4bbb DSH |
1645 | PACKET_remaining(¶ms)) <= 0) { |
1646 | al = SSL_AD_INTERNAL_ERROR; | |
1647 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
1648 | goto f_err; | |
1649 | } | |
6e59a892 | 1650 | if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature), |
192e4bbb DSH |
1651 | PACKET_remaining(&signature), pkey) <= 0) { |
1652 | /* bad signature */ | |
1653 | al = SSL_AD_DECRYPT_ERROR; | |
1654 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); | |
1655 | goto f_err; | |
0f113f3e MC |
1656 | } |
1657 | } else { | |
7689082b DSH |
1658 | /* aNULL, aSRP or PSK do not need public keys */ |
1659 | if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) { | |
0f113f3e MC |
1660 | /* Might be wrong key type, check it */ |
1661 | if (ssl3_check_cert_and_algorithm(s)) | |
1662 | /* Otherwise this shouldn't happen */ | |
b9908bf9 | 1663 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
1664 | goto err; |
1665 | } | |
1666 | /* still data left over */ | |
73999b62 | 1667 | if (PACKET_remaining(pkt) != 0) { |
b9908bf9 | 1668 | SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
0f113f3e MC |
1669 | goto f_err; |
1670 | } | |
1671 | } | |
bfb0641f | 1672 | EVP_MD_CTX_free(md_ctx); |
b9908bf9 | 1673 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1674 | f_err: |
1675 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1676 | err: | |
bc36ee62 | 1677 | #ifndef OPENSSL_NO_RSA |
d6407083 | 1678 | RSA_free(rsa); |
6b521df3 | 1679 | #endif |
10bf4fc2 | 1680 | #ifndef OPENSSL_NO_EC |
61dd9f7a | 1681 | EVP_PKEY_CTX_free(pctx); |
6b521df3 | 1682 | #endif |
bfb0641f | 1683 | EVP_MD_CTX_free(md_ctx); |
fe3a3291 | 1684 | ossl_statem_set_error(s); |
b9908bf9 | 1685 | return MSG_PROCESS_ERROR; |
0f113f3e | 1686 | } |
d02b48c6 | 1687 | |
be3583fa | 1688 | MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1689 | { |
1690 | int ret = MSG_PROCESS_ERROR; | |
1691 | unsigned int list_len, ctype_num, i, name_len; | |
1692 | X509_NAME *xn = NULL; | |
b6981744 EK |
1693 | const unsigned char *data; |
1694 | const unsigned char *namestart, *namebytes; | |
b9908bf9 | 1695 | STACK_OF(X509_NAME) *ca_sk = NULL; |
0f113f3e MC |
1696 | |
1697 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | |
b9908bf9 | 1698 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1699 | goto err; |
1700 | } | |
1701 | ||
1702 | /* get the certificate types */ | |
73999b62 MC |
1703 | if (!PACKET_get_1(pkt, &ctype_num) |
1704 | || !PACKET_get_bytes(pkt, &data, ctype_num)) { | |
ac112332 | 1705 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1706 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
ac112332 MC |
1707 | goto err; |
1708 | } | |
b548a1f1 RS |
1709 | OPENSSL_free(s->cert->ctypes); |
1710 | s->cert->ctypes = NULL; | |
0f113f3e MC |
1711 | if (ctype_num > SSL3_CT_NUMBER) { |
1712 | /* If we exceed static buffer copy all to cert structure */ | |
1713 | s->cert->ctypes = OPENSSL_malloc(ctype_num); | |
1714 | if (s->cert->ctypes == NULL) { | |
b9908bf9 | 1715 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1716 | goto err; |
1717 | } | |
ac112332 | 1718 | memcpy(s->cert->ctypes, data, ctype_num); |
0f113f3e MC |
1719 | s->cert->ctype_num = (size_t)ctype_num; |
1720 | ctype_num = SSL3_CT_NUMBER; | |
1721 | } | |
1722 | for (i = 0; i < ctype_num; i++) | |
ac112332 MC |
1723 | s->s3->tmp.ctype[i] = data[i]; |
1724 | ||
0f113f3e | 1725 | if (SSL_USE_SIGALGS(s)) { |
73999b62 MC |
1726 | if (!PACKET_get_net_2(pkt, &list_len) |
1727 | || !PACKET_get_bytes(pkt, &data, list_len)) { | |
0f113f3e | 1728 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
1729 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
1730 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
1731 | goto err; |
1732 | } | |
ac112332 | 1733 | |
0f113f3e MC |
1734 | /* Clear certificate digests and validity flags */ |
1735 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
d376e57d | 1736 | s->s3->tmp.md[i] = NULL; |
6383d316 | 1737 | s->s3->tmp.valid_flags[i] = 0; |
0f113f3e | 1738 | } |
ac112332 | 1739 | if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) { |
0f113f3e | 1740 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1741 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
1742 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
1743 | goto err; | |
1744 | } | |
1745 | if (!tls1_process_sigalgs(s)) { | |
1746 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 1747 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1748 | goto err; |
1749 | } | |
a0f63828 DSH |
1750 | } else { |
1751 | ssl_set_default_md(s); | |
0f113f3e MC |
1752 | } |
1753 | ||
1754 | /* get the CA RDNs */ | |
73999b62 MC |
1755 | if (!PACKET_get_net_2(pkt, &list_len) |
1756 | || PACKET_remaining(pkt) != list_len) { | |
0f113f3e | 1757 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1758 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1759 | goto err; |
1760 | } | |
1761 | ||
73999b62 MC |
1762 | while (PACKET_remaining(pkt)) { |
1763 | if (!PACKET_get_net_2(pkt, &name_len) | |
1764 | || !PACKET_get_bytes(pkt, &namebytes, name_len)) { | |
0f113f3e | 1765 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 MC |
1766 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
1767 | SSL_R_LENGTH_MISMATCH); | |
0f113f3e MC |
1768 | goto err; |
1769 | } | |
1770 | ||
ac112332 | 1771 | namestart = namebytes; |
0f113f3e | 1772 | |
ac112332 MC |
1773 | if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes, |
1774 | name_len)) == NULL) { | |
3c33c6f6 | 1775 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1776 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); |
3c33c6f6 | 1777 | goto err; |
0f113f3e MC |
1778 | } |
1779 | ||
ac112332 | 1780 | if (namebytes != (namestart + name_len)) { |
0f113f3e | 1781 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
b9908bf9 | 1782 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, |
0f113f3e MC |
1783 | SSL_R_CA_DN_LENGTH_MISMATCH); |
1784 | goto err; | |
1785 | } | |
1786 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
b9908bf9 | 1787 | SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1788 | goto err; |
1789 | } | |
6afef8b1 | 1790 | xn = NULL; |
0f113f3e MC |
1791 | } |
1792 | ||
0f113f3e MC |
1793 | /* we should setup a certificate to return.... */ |
1794 | s->s3->tmp.cert_req = 1; | |
1795 | s->s3->tmp.ctype_num = ctype_num; | |
222561fe | 1796 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
0f113f3e MC |
1797 | s->s3->tmp.ca_names = ca_sk; |
1798 | ca_sk = NULL; | |
1799 | ||
05c4f1d5 | 1800 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
cc273a93 | 1801 | goto done; |
0f113f3e | 1802 | err: |
fe3a3291 | 1803 | ossl_statem_set_error(s); |
cc273a93 | 1804 | done: |
6afef8b1 | 1805 | X509_NAME_free(xn); |
222561fe | 1806 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
b9908bf9 | 1807 | return ret; |
0f113f3e MC |
1808 | } |
1809 | ||
1810 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
dfeab068 | 1811 | { |
0f113f3e | 1812 | return (X509_NAME_cmp(*a, *b)); |
dfeab068 | 1813 | } |
dfeab068 | 1814 | |
be3583fa | 1815 | MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1816 | { |
1817 | int al; | |
1818 | unsigned int ticklen; | |
1819 | unsigned long ticket_lifetime_hint; | |
b9908bf9 | 1820 | |
73999b62 MC |
1821 | if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
1822 | || !PACKET_get_net_2(pkt, &ticklen) | |
1823 | || PACKET_remaining(pkt) != ticklen) { | |
e711da71 | 1824 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1825 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
e711da71 EK |
1826 | goto f_err; |
1827 | } | |
1828 | ||
1829 | /* Server is allowed to change its mind and send an empty ticket. */ | |
1830 | if (ticklen == 0) | |
c9de4a20 | 1831 | return MSG_PROCESS_CONTINUE_READING; |
e711da71 | 1832 | |
98ece4ee MC |
1833 | if (s->session->session_id_length > 0) { |
1834 | int i = s->session_ctx->session_cache_mode; | |
1835 | SSL_SESSION *new_sess; | |
1836 | /* | |
1837 | * We reused an existing session, so we need to replace it with a new | |
1838 | * one | |
1839 | */ | |
1840 | if (i & SSL_SESS_CACHE_CLIENT) { | |
1841 | /* | |
1842 | * Remove the old session from the cache | |
1843 | */ | |
1844 | if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) { | |
1845 | if (s->session_ctx->remove_session_cb != NULL) | |
1846 | s->session_ctx->remove_session_cb(s->session_ctx, | |
1847 | s->session); | |
1848 | } else { | |
1849 | /* We carry on if this fails */ | |
1850 | SSL_CTX_remove_session(s->session_ctx, s->session); | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { | |
1855 | al = SSL_AD_INTERNAL_ERROR; | |
b9908bf9 | 1856 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
98ece4ee MC |
1857 | goto f_err; |
1858 | } | |
1859 | ||
1860 | SSL_SESSION_free(s->session); | |
1861 | s->session = new_sess; | |
1862 | } | |
1863 | ||
b548a1f1 RS |
1864 | OPENSSL_free(s->session->tlsext_tick); |
1865 | s->session->tlsext_ticklen = 0; | |
e711da71 | 1866 | |
0f113f3e | 1867 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
a71edf3b | 1868 | if (s->session->tlsext_tick == NULL) { |
b9908bf9 | 1869 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1870 | goto err; |
1871 | } | |
73999b62 | 1872 | if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) { |
561e12bb | 1873 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1874 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
561e12bb MC |
1875 | goto f_err; |
1876 | } | |
e711da71 EK |
1877 | |
1878 | s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; | |
0f113f3e MC |
1879 | s->session->tlsext_ticklen = ticklen; |
1880 | /* | |
1881 | * There are two ways to detect a resumed ticket session. One is to set | |
1882 | * an appropriate session ID and then the server must return a match in | |
1883 | * ServerHello. This allows the normal client session ID matching to work | |
1884 | * and we know much earlier that the ticket has been accepted. The | |
1885 | * other way is to set zero length session ID when the ticket is | |
1886 | * presented and rely on the handshake to determine session resumption. | |
1887 | * We choose the former approach because this fits in with assumptions | |
1888 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is | |
1889 | * SHA256 is disabled) hash of the ticket. | |
1890 | */ | |
d166ed8c DSH |
1891 | if (!EVP_Digest(s->session->tlsext_tick, ticklen, |
1892 | s->session->session_id, &s->session->session_id_length, | |
1893 | EVP_sha256(), NULL)) { | |
1894 | SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB); | |
1895 | goto err; | |
1896 | } | |
b9908bf9 | 1897 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1898 | f_err: |
1899 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1900 | err: | |
fe3a3291 | 1901 | ossl_statem_set_error(s); |
b9908bf9 | 1902 | return MSG_PROCESS_ERROR; |
0f113f3e | 1903 | } |
67c8e7f4 | 1904 | |
be3583fa | 1905 | MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt) |
b9908bf9 MC |
1906 | { |
1907 | int al; | |
1908 | unsigned long resplen; | |
1909 | unsigned int type; | |
b9908bf9 | 1910 | |
73999b62 | 1911 | if (!PACKET_get_1(pkt, &type) |
ac63710a | 1912 | || type != TLSEXT_STATUSTYPE_ocsp) { |
0f113f3e | 1913 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1914 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); |
0f113f3e MC |
1915 | goto f_err; |
1916 | } | |
73999b62 MC |
1917 | if (!PACKET_get_net_3(pkt, &resplen) |
1918 | || PACKET_remaining(pkt) != resplen) { | |
0f113f3e | 1919 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1920 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
1921 | goto f_err; |
1922 | } | |
ac63710a | 1923 | s->tlsext_ocsp_resp = OPENSSL_malloc(resplen); |
a71edf3b | 1924 | if (s->tlsext_ocsp_resp == NULL) { |
0f113f3e | 1925 | al = SSL_AD_INTERNAL_ERROR; |
b9908bf9 | 1926 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1927 | goto f_err; |
1928 | } | |
73999b62 | 1929 | if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) { |
ac63710a | 1930 | al = SSL_AD_DECODE_ERROR; |
b9908bf9 | 1931 | SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
ac63710a MC |
1932 | goto f_err; |
1933 | } | |
0f113f3e | 1934 | s->tlsext_ocsp_resplen = resplen; |
b9908bf9 | 1935 | return MSG_PROCESS_CONTINUE_READING; |
0f113f3e MC |
1936 | f_err: |
1937 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 1938 | ossl_statem_set_error(s); |
b9908bf9 | 1939 | return MSG_PROCESS_ERROR; |
0f113f3e | 1940 | } |
d02b48c6 | 1941 | |
be3583fa | 1942 | MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt) |
b9908bf9 | 1943 | { |
73999b62 | 1944 | if (PACKET_remaining(pkt) > 0) { |
0f113f3e MC |
1945 | /* should contain no data */ |
1946 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
b9908bf9 | 1947 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH); |
fe3a3291 | 1948 | ossl_statem_set_error(s); |
b9908bf9 | 1949 | return MSG_PROCESS_ERROR; |
0f113f3e | 1950 | } |
b9908bf9 MC |
1951 | |
1952 | #ifndef OPENSSL_NO_SRP | |
1953 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
1954 | if (SRP_Calc_A_param(s) <= 0) { | |
1955 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC); | |
1956 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 1957 | ossl_statem_set_error(s); |
b9908bf9 MC |
1958 | return MSG_PROCESS_ERROR; |
1959 | } | |
1960 | } | |
1961 | #endif | |
1962 | ||
a455d0f6 MC |
1963 | /* |
1964 | * at this point we check that we have the required stuff from | |
1965 | * the server | |
1966 | */ | |
1967 | if (!ssl3_check_cert_and_algorithm(s)) { | |
1968 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
fe3a3291 | 1969 | ossl_statem_set_error(s); |
a455d0f6 MC |
1970 | return MSG_PROCESS_ERROR; |
1971 | } | |
1972 | ||
bb1aaab4 MC |
1973 | /* |
1974 | * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and | |
1975 | * |tlsext_ocsp_resplen| values will be set if we actually received a status | |
1976 | * message, or NULL and -1 otherwise | |
1977 | */ | |
b1931d43 | 1978 | if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) { |
bb1aaab4 MC |
1979 | int ret; |
1980 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | |
1981 | if (ret == 0) { | |
1982 | ssl3_send_alert(s, SSL3_AL_FATAL, | |
1983 | SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE); | |
1984 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, | |
1985 | SSL_R_INVALID_STATUS_RESPONSE); | |
1986 | return MSG_PROCESS_ERROR; | |
1987 | } | |
1988 | if (ret < 0) { | |
1989 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
1990 | SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE); | |
1991 | return MSG_PROCESS_ERROR; | |
1992 | } | |
1993 | } | |
1994 | ||
ed29e82a RP |
1995 | #ifndef OPENSSL_NO_CT |
1996 | if (s->ct_validation_callback != NULL) { | |
43341433 VD |
1997 | /* Note we validate the SCTs whether or not we abort on error */ |
1998 | if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { | |
ed29e82a RP |
1999 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
2000 | return MSG_PROCESS_ERROR; | |
2001 | } | |
2002 | } | |
2003 | #endif | |
2004 | ||
473483d4 MC |
2005 | #ifndef OPENSSL_NO_SCTP |
2006 | /* Only applies to renegotiation */ | |
2007 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
2008 | && s->renegotiate != 0) | |
2009 | return MSG_PROCESS_CONTINUE_PROCESSING; | |
2010 | else | |
2011 | #endif | |
2012 | return MSG_PROCESS_FINISHED_READING; | |
0f113f3e | 2013 | } |
176f31dd | 2014 | |
b9908bf9 | 2015 | int tls_construct_client_key_exchange(SSL *s) |
0f113f3e MC |
2016 | { |
2017 | unsigned char *p; | |
2018 | int n; | |
7689082b DSH |
2019 | #ifndef OPENSSL_NO_PSK |
2020 | size_t pskhdrlen = 0; | |
2021 | #endif | |
0f113f3e | 2022 | unsigned long alg_k; |
0bce0b02 | 2023 | |
7689082b | 2024 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
d02b48c6 | 2025 | |
b9908bf9 | 2026 | p = ssl_handshake_start(s); |
d02b48c6 | 2027 | |
7689082b DSH |
2028 | |
2029 | #ifndef OPENSSL_NO_PSK | |
b9908bf9 MC |
2030 | if (alg_k & SSL_PSK) { |
2031 | int psk_err = 1; | |
2032 | /* | |
2033 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2034 | * \0-terminated identity. The last byte is for us for simulating | |
2035 | * strnlen. | |
2036 | */ | |
2037 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
2038 | size_t identitylen; | |
2039 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
0bce0b02 MC |
2040 | unsigned char *tmppsk; |
2041 | char *tmpidentity; | |
b9908bf9 MC |
2042 | size_t psklen; |
2043 | ||
2044 | if (s->psk_client_callback == NULL) { | |
2045 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2046 | SSL_R_PSK_NO_CLIENT_CB); | |
2047 | goto err; | |
2048 | } | |
7689082b | 2049 | |
b9908bf9 | 2050 | memset(identity, 0, sizeof(identity)); |
7689082b | 2051 | |
b9908bf9 MC |
2052 | psklen = s->psk_client_callback(s, s->session->psk_identity_hint, |
2053 | identity, sizeof(identity) - 1, | |
2054 | psk, sizeof(psk)); | |
7689082b | 2055 | |
b9908bf9 MC |
2056 | if (psklen > PSK_MAX_PSK_LEN) { |
2057 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2058 | ERR_R_INTERNAL_ERROR); | |
2059 | goto psk_err; | |
2060 | } else if (psklen == 0) { | |
2061 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2062 | SSL_R_PSK_IDENTITY_NOT_FOUND); | |
2063 | goto psk_err; | |
2064 | } | |
7689082b | 2065 | |
b9908bf9 MC |
2066 | identitylen = strlen(identity); |
2067 | if (identitylen > PSK_MAX_IDENTITY_LEN) { | |
2068 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2069 | ERR_R_INTERNAL_ERROR); | |
2070 | goto psk_err; | |
2071 | } | |
0bce0b02 MC |
2072 | |
2073 | tmppsk = OPENSSL_memdup(psk, psklen); | |
2074 | tmpidentity = OPENSSL_strdup(identity); | |
2075 | if (tmppsk == NULL || tmpidentity == NULL) { | |
7689082b | 2076 | OPENSSL_cleanse(identity, sizeof(identity)); |
0bce0b02 MC |
2077 | OPENSSL_cleanse(psk, psklen); |
2078 | OPENSSL_clear_free(tmppsk, psklen); | |
2079 | OPENSSL_clear_free(tmpidentity, identitylen); | |
b9908bf9 | 2080 | goto memerr; |
7689082b | 2081 | } |
d02b48c6 | 2082 | |
0bce0b02 MC |
2083 | OPENSSL_free(s->s3->tmp.psk); |
2084 | s->s3->tmp.psk = tmppsk; | |
2085 | s->s3->tmp.psklen = psklen; | |
2086 | OPENSSL_free(s->session->psk_identity); | |
2087 | s->session->psk_identity = tmpidentity; | |
b9908bf9 MC |
2088 | s2n(identitylen, p); |
2089 | memcpy(p, identity, identitylen); | |
2090 | pskhdrlen = 2 + identitylen; | |
2091 | p += identitylen; | |
2092 | psk_err = 0; | |
2093 | psk_err: | |
0bce0b02 | 2094 | OPENSSL_cleanse(psk, psklen); |
b9908bf9 MC |
2095 | OPENSSL_cleanse(identity, sizeof(identity)); |
2096 | if (psk_err != 0) { | |
2097 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2098 | goto err; | |
0f113f3e | 2099 | } |
b9908bf9 MC |
2100 | } |
2101 | if (alg_k & SSL_kPSK) { | |
2102 | n = 0; | |
2103 | } else | |
2104 | #endif | |
2105 | ||
2106 | /* Fool emacs indentation */ | |
2107 | if (0) { | |
2108 | } | |
bc36ee62 | 2109 | #ifndef OPENSSL_NO_RSA |
b9908bf9 | 2110 | else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
0bce0b02 MC |
2111 | unsigned char *q; |
2112 | EVP_PKEY *pkey = NULL; | |
2113 | EVP_PKEY_CTX *pctx = NULL; | |
923ffa97 | 2114 | size_t enclen; |
0bce0b02 MC |
2115 | unsigned char *pms = NULL; |
2116 | size_t pmslen = 0; | |
b9908bf9 MC |
2117 | |
2118 | if (s->session->peer == NULL) { | |
2119 | /* | |
2120 | * We should always have a server certificate with SSL_kRSA. | |
2121 | */ | |
2122 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2123 | ERR_R_INTERNAL_ERROR); | |
2124 | goto err; | |
2125 | } | |
0f113f3e | 2126 | |
8382fd3a | 2127 | pkey = X509_get0_pubkey(s->session->peer); |
3aeb9348 | 2128 | if (EVP_PKEY_get0_RSA(pkey) == NULL) { |
361a1191 KR |
2129 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2130 | ERR_R_INTERNAL_ERROR); | |
361a1191 | 2131 | goto err; |
b9908bf9 | 2132 | } |
0f113f3e | 2133 | |
0bce0b02 MC |
2134 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
2135 | pms = OPENSSL_malloc(pmslen); | |
2136 | if (pms == NULL) | |
2137 | goto memerr; | |
2138 | ||
b9908bf9 MC |
2139 | pms[0] = s->client_version >> 8; |
2140 | pms[1] = s->client_version & 0xff; | |
0bce0b02 MC |
2141 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) { |
2142 | OPENSSL_clear_free(pms, pmslen); | |
b9908bf9 | 2143 | goto err; |
0bce0b02 | 2144 | } |
0f113f3e | 2145 | |
b9908bf9 MC |
2146 | q = p; |
2147 | /* Fix buf for TLS and beyond */ | |
2148 | if (s->version > SSL3_VERSION) | |
2149 | p += 2; | |
923ffa97 | 2150 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
923ffa97 DSH |
2151 | if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 |
2152 | || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { | |
0bce0b02 MC |
2153 | OPENSSL_clear_free(pms, pmslen); |
2154 | EVP_PKEY_CTX_free(pctx); | |
923ffa97 DSH |
2155 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2156 | ERR_R_EVP_LIB); | |
2157 | goto err; | |
2158 | } | |
2159 | if (EVP_PKEY_encrypt(pctx, p, &enclen, pms, pmslen) <= 0) { | |
0bce0b02 MC |
2160 | OPENSSL_clear_free(pms, pmslen); |
2161 | EVP_PKEY_CTX_free(pctx); | |
923ffa97 DSH |
2162 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2163 | SSL_R_BAD_RSA_ENCRYPT); | |
2164 | goto err; | |
2165 | } | |
2166 | n = enclen; | |
2167 | EVP_PKEY_CTX_free(pctx); | |
2168 | pctx = NULL; | |
0f113f3e | 2169 | # ifdef PKCS1_CHECK |
b9908bf9 MC |
2170 | if (s->options & SSL_OP_PKCS1_CHECK_1) |
2171 | p[1]++; | |
2172 | if (s->options & SSL_OP_PKCS1_CHECK_2) | |
2173 | tmp_buf[0] = 0x70; | |
0f113f3e | 2174 | # endif |
0f113f3e | 2175 | |
b9908bf9 MC |
2176 | /* Fix buf for TLS and beyond */ |
2177 | if (s->version > SSL3_VERSION) { | |
2178 | s2n(n, q); | |
2179 | n += 2; | |
0f113f3e | 2180 | } |
0bce0b02 MC |
2181 | |
2182 | s->s3->tmp.pms = pms; | |
2183 | s->s3->tmp.pmslen = pmslen; | |
b9908bf9 | 2184 | } |
f9b3bff6 | 2185 | #endif |
bc36ee62 | 2186 | #ifndef OPENSSL_NO_DH |
bc71f910 | 2187 | else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
fb79abe3 | 2188 | DH *dh_clnt = NULL; |
2ac6115d | 2189 | const BIGNUM *pub_key; |
0bce0b02 MC |
2190 | EVP_PKEY *ckey = NULL, *skey = NULL; |
2191 | ||
fb79abe3 DSH |
2192 | skey = s->s3->peer_tmp; |
2193 | if (skey == NULL) { | |
bc71f910 DSH |
2194 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2195 | ERR_R_INTERNAL_ERROR); | |
2196 | goto err; | |
0f113f3e | 2197 | } |
fb79abe3 DSH |
2198 | ckey = ssl_generate_pkey(skey, NID_undef); |
2199 | dh_clnt = EVP_PKEY_get0_DH(ckey); | |
0f113f3e | 2200 | |
fb79abe3 DSH |
2201 | if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) { |
2202 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2203 | ERR_R_INTERNAL_ERROR); | |
0bce0b02 | 2204 | EVP_PKEY_free(ckey); |
b9908bf9 MC |
2205 | goto err; |
2206 | } | |
fb79abe3 | 2207 | |
0f113f3e | 2208 | |
bc71f910 | 2209 | /* send off the data */ |
0aeddcfa MC |
2210 | DH_get0_key(dh_clnt, &pub_key, NULL); |
2211 | n = BN_num_bytes(pub_key); | |
bc71f910 | 2212 | s2n(n, p); |
0aeddcfa | 2213 | BN_bn2bin(pub_key, p); |
bc71f910 | 2214 | n += 2; |
fb79abe3 DSH |
2215 | EVP_PKEY_free(ckey); |
2216 | ckey = NULL; | |
b9908bf9 MC |
2217 | } |
2218 | #endif | |
0f113f3e | 2219 | |
b9908bf9 | 2220 | #ifndef OPENSSL_NO_EC |
ce0c1f2b | 2221 | else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
0bce0b02 MC |
2222 | unsigned char *encodedPoint = NULL; |
2223 | int encoded_pt_len = 0; | |
2224 | EVP_PKEY *ckey = NULL, *skey = NULL; | |
0f113f3e | 2225 | |
ce0c1f2b DSH |
2226 | skey = s->s3->peer_tmp; |
2227 | if ((skey == NULL) || EVP_PKEY_get0_EC_KEY(skey) == NULL) { | |
2228 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
266483d2 | 2229 | ERR_R_INTERNAL_ERROR); |
ce0c1f2b | 2230 | goto err; |
0f113f3e | 2231 | } |
b9908bf9 | 2232 | |
61dd9f7a | 2233 | ckey = ssl_generate_pkey(skey, NID_undef); |
b9908bf9 | 2234 | |
61dd9f7a DSH |
2235 | if (ssl_derive(s, ckey, skey) == 0) { |
2236 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
0bce0b02 | 2237 | EVP_PKEY_free(ckey); |
b9908bf9 MC |
2238 | goto err; |
2239 | } | |
2240 | ||
61dd9f7a DSH |
2241 | /* Generate encoding of client key */ |
2242 | encoded_pt_len = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey), | |
2243 | POINT_CONVERSION_UNCOMPRESSED, | |
2244 | &encodedPoint, NULL); | |
cae41364 | 2245 | |
c66ce5eb DSH |
2246 | if (encoded_pt_len == 0) { |
2247 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | |
0bce0b02 | 2248 | EVP_PKEY_free(ckey); |
c66ce5eb DSH |
2249 | goto err; |
2250 | } | |
b9908bf9 | 2251 | |
61dd9f7a DSH |
2252 | EVP_PKEY_free(ckey); |
2253 | ckey = NULL; | |
2254 | ||
c66ce5eb | 2255 | n = encoded_pt_len; |
b9908bf9 | 2256 | |
c66ce5eb DSH |
2257 | *p = n; /* length of encoded point */ |
2258 | /* Encoded point will be copied here */ | |
2259 | p += 1; | |
2260 | /* copy the point */ | |
2261 | memcpy(p, encodedPoint, n); | |
2262 | /* increment n to account for length field */ | |
2263 | n += 1; | |
0f113f3e | 2264 | |
b9908bf9 | 2265 | /* Free allocated memory */ |
b9908bf9 | 2266 | OPENSSL_free(encodedPoint); |
b9908bf9 MC |
2267 | } |
2268 | #endif /* !OPENSSL_NO_EC */ | |
2a9b9654 | 2269 | #ifndef OPENSSL_NO_GOST |
b9908bf9 MC |
2270 | else if (alg_k & SSL_kGOST) { |
2271 | /* GOST key exchange message creation */ | |
2272 | EVP_PKEY_CTX *pkey_ctx; | |
2273 | X509 *peer_cert; | |
2274 | size_t msglen; | |
2275 | unsigned int md_len; | |
2276 | unsigned char shared_ukm[32], tmp[256]; | |
2277 | EVP_MD_CTX *ukm_hash; | |
e44380a9 | 2278 | int dgst_nid = NID_id_GostR3411_94; |
0bce0b02 MC |
2279 | unsigned char *pms = NULL; |
2280 | size_t pmslen = 0; | |
2281 | ||
e44380a9 DB |
2282 | if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) |
2283 | dgst_nid = NID_id_GostR3411_2012_256; | |
2284 | ||
b9908bf9 | 2285 | |
b9908bf9 MC |
2286 | /* |
2287 | * Get server sertificate PKEY and create ctx from it | |
2288 | */ | |
2289 | peer_cert = s->session->peer; | |
2290 | if (!peer_cert) { | |
2291 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2292 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | |
77d514c5 MC |
2293 | goto err; |
2294 | } | |
2295 | ||
8382fd3a | 2296 | pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL); |
a71edf3b MC |
2297 | if (pkey_ctx == NULL) { |
2298 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2299 | ERR_R_MALLOC_FAILURE); | |
2300 | goto err; | |
2301 | } | |
b9908bf9 MC |
2302 | /* |
2303 | * If we have send a certificate, and certificate key | |
5f3d93e4 | 2304 | * parameters match those of server certificate, use |
b9908bf9 MC |
2305 | * certificate key for key exchange |
2306 | */ | |
0f113f3e | 2307 | |
b9908bf9 | 2308 | /* Otherwise, generate ephemeral key pair */ |
0bce0b02 MC |
2309 | pmslen = 32; |
2310 | pms = OPENSSL_malloc(pmslen); | |
2311 | if (pms == NULL) | |
2312 | goto memerr; | |
b9908bf9 | 2313 | |
5f3d93e4 MC |
2314 | if (pkey_ctx == NULL |
2315 | || EVP_PKEY_encrypt_init(pkey_ctx) <= 0 | |
2316 | /* Generate session key */ | |
2317 | || RAND_bytes(pms, pmslen) <= 0) { | |
b9908bf9 | 2318 | EVP_PKEY_CTX_free(pkey_ctx); |
0bce0b02 | 2319 | OPENSSL_clear_free(pms, pmslen); |
b9908bf9 MC |
2320 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2321 | ERR_R_INTERNAL_ERROR); | |
2322 | goto err; | |
2323 | }; | |
c660ec63 | 2324 | /* |
b9908bf9 | 2325 | * If we have client certificate, use its secret as peer key |
c660ec63 | 2326 | */ |
b9908bf9 MC |
2327 | if (s->s3->tmp.cert_req && s->cert->key->privatekey) { |
2328 | if (EVP_PKEY_derive_set_peer | |
2329 | (pkey_ctx, s->cert->key->privatekey) <= 0) { | |
2330 | /* | |
2331 | * If there was an error - just ignore it. Ephemeral key | |
2332 | * * would be used | |
2333 | */ | |
2334 | ERR_clear_error(); | |
c660ec63 DSH |
2335 | } |
2336 | } | |
b9908bf9 MC |
2337 | /* |
2338 | * Compute shared IV and store it in algorithm-specific context | |
2339 | * data | |
2340 | */ | |
bfb0641f | 2341 | ukm_hash = EVP_MD_CTX_new(); |
5f3d93e4 | 2342 | if (EVP_DigestInit(ukm_hash, |
e44380a9 | 2343 | EVP_get_digestbynid(dgst_nid)) <= 0 |
5f3d93e4 MC |
2344 | || EVP_DigestUpdate(ukm_hash, s->s3->client_random, |
2345 | SSL3_RANDOM_SIZE) <= 0 | |
2346 | || EVP_DigestUpdate(ukm_hash, s->s3->server_random, | |
2347 | SSL3_RANDOM_SIZE) <= 0 | |
2348 | || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { | |
bfb0641f | 2349 | EVP_MD_CTX_free(ukm_hash); |
0bce0b02 | 2350 | OPENSSL_clear_free(pms, pmslen); |
5f3d93e4 MC |
2351 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2352 | ERR_R_INTERNAL_ERROR); | |
2353 | goto err; | |
2354 | } | |
bfb0641f | 2355 | EVP_MD_CTX_free(ukm_hash); |
b9908bf9 MC |
2356 | if (EVP_PKEY_CTX_ctrl |
2357 | (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, | |
2358 | shared_ukm) < 0) { | |
0bce0b02 | 2359 | OPENSSL_clear_free(pms, pmslen); |
b9908bf9 MC |
2360 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2361 | SSL_R_LIBRARY_BUG); | |
2362 | goto err; | |
c660ec63 | 2363 | } |
b9908bf9 MC |
2364 | /* Make GOST keytransport blob message */ |
2365 | /* | |
2366 | * Encapsulate it into sequence | |
2367 | */ | |
2368 | *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; | |
2369 | msglen = 255; | |
5f3d93e4 | 2370 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { |
0bce0b02 | 2371 | OPENSSL_clear_free(pms, pmslen); |
b9908bf9 MC |
2372 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, |
2373 | SSL_R_LIBRARY_BUG); | |
c660ec63 DSH |
2374 | goto err; |
2375 | } | |
b9908bf9 MC |
2376 | if (msglen >= 0x80) { |
2377 | *(p++) = 0x81; | |
2378 | *(p++) = msglen & 0xff; | |
2379 | n = msglen + 3; | |
2380 | } else { | |
2381 | *(p++) = msglen & 0xff; | |
2382 | n = msglen + 2; | |
2383 | } | |
2384 | memcpy(p, tmp, msglen); | |
2385 | /* Check if pubkey from client certificate was used */ | |
2386 | if (EVP_PKEY_CTX_ctrl | |
2387 | (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { | |
2388 | /* Set flag "skip certificate verify" */ | |
2389 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | |
2390 | } | |
2391 | EVP_PKEY_CTX_free(pkey_ctx); | |
0bce0b02 MC |
2392 | s->s3->tmp.pms = pms; |
2393 | s->s3->tmp.pmslen = pmslen; | |
b9908bf9 | 2394 | } |
2a9b9654 | 2395 | #endif |
b9908bf9 MC |
2396 | #ifndef OPENSSL_NO_SRP |
2397 | else if (alg_k & SSL_kSRP) { | |
2398 | if (s->srp_ctx.A != NULL) { | |
2399 | /* send off the data */ | |
2400 | n = BN_num_bytes(s->srp_ctx.A); | |
2401 | s2n(n, p); | |
2402 | BN_bn2bin(s->srp_ctx.A, p); | |
2403 | n += 2; | |
2404 | } else { | |
2405 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2406 | ERR_R_INTERNAL_ERROR); | |
2407 | goto err; | |
2408 | } | |
2409 | OPENSSL_free(s->session->srp_username); | |
7644a9ae | 2410 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); |
b9908bf9 MC |
2411 | if (s->session->srp_username == NULL) { |
2412 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, | |
2413 | ERR_R_MALLOC_FAILURE); | |
69f68237 MC |
2414 | goto err; |
2415 | } | |
c660ec63 | 2416 | } |
b9908bf9 MC |
2417 | #endif |
2418 | else { | |
2419 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2420 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2421 | goto err; | |
2422 | } | |
2423 | ||
2424 | #ifndef OPENSSL_NO_PSK | |
2425 | n += pskhdrlen; | |
2426 | #endif | |
2427 | ||
2428 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) { | |
2429 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2430 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2431 | goto err; | |
2432 | } | |
2433 | ||
b9908bf9 | 2434 | return 1; |
c660ec63 DSH |
2435 | memerr: |
2436 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
b9908bf9 | 2437 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e | 2438 | err: |
0bce0b02 | 2439 | OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen); |
76106e60 | 2440 | s->s3->tmp.pms = NULL; |
7689082b DSH |
2441 | #ifndef OPENSSL_NO_PSK |
2442 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); | |
2443 | s->s3->tmp.psk = NULL; | |
0f113f3e | 2444 | #endif |
fe3a3291 | 2445 | ossl_statem_set_error(s); |
b9908bf9 MC |
2446 | return 0; |
2447 | } | |
2448 | ||
2449 | int tls_client_key_exchange_post_work(SSL *s) | |
2450 | { | |
2451 | unsigned char *pms = NULL; | |
2452 | size_t pmslen = 0; | |
2453 | ||
6f137370 MC |
2454 | pms = s->s3->tmp.pms; |
2455 | pmslen = s->s3->tmp.pmslen; | |
2456 | ||
b9908bf9 MC |
2457 | #ifndef OPENSSL_NO_SRP |
2458 | /* Check for SRP */ | |
2459 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
2460 | if (!srp_generate_client_master_secret(s)) { | |
2461 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, | |
2462 | ERR_R_INTERNAL_ERROR); | |
2463 | goto err; | |
2464 | } | |
2465 | return 1; | |
2466 | } | |
2467 | #endif | |
b9908bf9 MC |
2468 | |
2469 | if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { | |
2470 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2471 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE); | |
2472 | goto err; | |
2473 | } | |
2474 | if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { | |
2475 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2476 | SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR); | |
6f137370 MC |
2477 | /* ssl_generate_master_secret frees the pms even on error */ |
2478 | pms = NULL; | |
2479 | pmslen = 0; | |
b9908bf9 MC |
2480 | goto err; |
2481 | } | |
6f137370 MC |
2482 | pms = NULL; |
2483 | pmslen = 0; | |
473483d4 MC |
2484 | |
2485 | #ifndef OPENSSL_NO_SCTP | |
2486 | if (SSL_IS_DTLS(s)) { | |
2487 | unsigned char sctpauthkey[64]; | |
2488 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
2489 | ||
2490 | /* | |
2491 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
2492 | * used. | |
2493 | */ | |
141eb8c6 MC |
2494 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
2495 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
473483d4 MC |
2496 | |
2497 | if (SSL_export_keying_material(s, sctpauthkey, | |
2498 | sizeof(sctpauthkey), labelbuffer, | |
2499 | sizeof(labelbuffer), NULL, 0, 0) <= 0) | |
2500 | goto err; | |
2501 | ||
2502 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
2503 | sizeof(sctpauthkey), sctpauthkey); | |
2504 | } | |
2505 | #endif | |
2506 | ||
b9908bf9 MC |
2507 | return 1; |
2508 | err: | |
2509 | OPENSSL_clear_free(pms, pmslen); | |
2510 | s->s3->tmp.pms = NULL; | |
2511 | return 0; | |
0f113f3e | 2512 | } |
d02b48c6 | 2513 | |
b9908bf9 | 2514 | int tls_construct_client_verify(SSL *s) |
0f113f3e MC |
2515 | { |
2516 | unsigned char *p; | |
0f113f3e | 2517 | EVP_PKEY *pkey; |
a0f63828 | 2518 | const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys]; |
6e59a892 | 2519 | EVP_MD_CTX *mctx; |
0f113f3e | 2520 | unsigned u = 0; |
2a9b9654 | 2521 | unsigned long n = 0; |
a0f63828 DSH |
2522 | long hdatalen = 0; |
2523 | void *hdata; | |
0f113f3e | 2524 | |
bfb0641f | 2525 | mctx = EVP_MD_CTX_new(); |
6e59a892 RL |
2526 | if (mctx == NULL) { |
2527 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE); | |
2528 | goto err; | |
2529 | } | |
0f113f3e | 2530 | |
b9908bf9 MC |
2531 | p = ssl_handshake_start(s); |
2532 | pkey = s->cert->key->privatekey; | |
a0f63828 DSH |
2533 | |
2534 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
2535 | if (hdatalen <= 0) { | |
5f3d93e4 MC |
2536 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
2537 | goto err; | |
2538 | } | |
a0f63828 DSH |
2539 | if (SSL_USE_SIGALGS(s)) { |
2540 | if (!tls12_get_sigandhash(p, pkey, md)) { | |
b9908bf9 MC |
2541 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
2542 | goto err; | |
0f113f3e | 2543 | } |
a0f63828 | 2544 | p += 2; |
2a9b9654 | 2545 | n = 2; |
a0f63828 | 2546 | } |
855a54a9 | 2547 | #ifdef SSL_DEBUG |
a0f63828 | 2548 | fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md)); |
b9908bf9 | 2549 | #endif |
6e59a892 RL |
2550 | if (!EVP_SignInit_ex(mctx, md, NULL) |
2551 | || !EVP_SignUpdate(mctx, hdata, hdatalen) | |
a0f63828 | 2552 | || (s->version == SSL3_VERSION |
6e59a892 | 2553 | && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
a0f63828 DSH |
2554 | s->session->master_key_length, |
2555 | s->session->master_key)) | |
6e59a892 | 2556 | || !EVP_SignFinal(mctx, p + 2, &u, pkey)) { |
a0f63828 DSH |
2557 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB); |
2558 | goto err; | |
2559 | } | |
2a9b9654 | 2560 | #ifndef OPENSSL_NO_GOST |
3aeb9348 DSH |
2561 | { |
2562 | int pktype = EVP_PKEY_id(pkey); | |
2563 | if (pktype == NID_id_GostR3410_2001 | |
2564 | || pktype == NID_id_GostR3410_2012_256 | |
2565 | || pktype == NID_id_GostR3410_2012_512) | |
2566 | BUF_reverse(p + 2, NULL, u); | |
b9908bf9 | 2567 | } |
2a9b9654 | 2568 | #endif |
a0f63828 DSH |
2569 | |
2570 | s2n(u, p); | |
2a9b9654 | 2571 | n += u + 2; |
a0f63828 DSH |
2572 | /* Digest cached records and discard handshake buffer */ |
2573 | if (!ssl3_digest_cached_records(s, 0)) | |
2574 | goto err; | |
b9908bf9 MC |
2575 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) { |
2576 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
2577 | goto err; | |
0f113f3e | 2578 | } |
b9908bf9 | 2579 | |
bfb0641f | 2580 | EVP_MD_CTX_free(mctx); |
b9908bf9 | 2581 | return 1; |
0f113f3e | 2582 | err: |
bfb0641f | 2583 | EVP_MD_CTX_free(mctx); |
b9908bf9 | 2584 | return 0; |
0f113f3e MC |
2585 | } |
2586 | ||
2587 | /* | |
2588 | * Check a certificate can be used for client authentication. Currently check | |
2589 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
2590 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 DSH |
2591 | */ |
2592 | static int ssl3_check_client_certificate(SSL *s) | |
0f113f3e | 2593 | { |
0f113f3e MC |
2594 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) |
2595 | return 0; | |
2596 | /* If no suitable signature algorithm can't use certificate */ | |
d376e57d | 2597 | if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys]) |
0f113f3e MC |
2598 | return 0; |
2599 | /* | |
2600 | * If strict mode check suitability of chain before using it. This also | |
2601 | * adjusts suite B digest if necessary. | |
2602 | */ | |
2603 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
2604 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
2605 | return 0; | |
0f113f3e MC |
2606 | return 1; |
2607 | } | |
0d609395 | 2608 | |
be3583fa | 2609 | WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst) |
0f113f3e MC |
2610 | { |
2611 | X509 *x509 = NULL; | |
2612 | EVP_PKEY *pkey = NULL; | |
2613 | int i; | |
2614 | ||
b9908bf9 | 2615 | if (wst == WORK_MORE_A) { |
0f113f3e MC |
2616 | /* Let cert callback update client certificates if required */ |
2617 | if (s->cert->cert_cb) { | |
2618 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
2619 | if (i < 0) { | |
2620 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 2621 | return WORK_MORE_A; |
0f113f3e MC |
2622 | } |
2623 | if (i == 0) { | |
2624 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 2625 | ossl_statem_set_error(s); |
0f113f3e MC |
2626 | return 0; |
2627 | } | |
2628 | s->rwstate = SSL_NOTHING; | |
2629 | } | |
2630 | if (ssl3_check_client_certificate(s)) | |
b9908bf9 MC |
2631 | return WORK_FINISHED_CONTINUE; |
2632 | ||
2633 | /* Fall through to WORK_MORE_B */ | |
2634 | wst = WORK_MORE_B; | |
0f113f3e MC |
2635 | } |
2636 | ||
2637 | /* We need to get a client cert */ | |
b9908bf9 | 2638 | if (wst == WORK_MORE_B) { |
0f113f3e MC |
2639 | /* |
2640 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
2641 | * return(-1); We then get retied later | |
2642 | */ | |
0f113f3e MC |
2643 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
2644 | if (i < 0) { | |
2645 | s->rwstate = SSL_X509_LOOKUP; | |
b9908bf9 | 2646 | return WORK_MORE_B; |
0f113f3e MC |
2647 | } |
2648 | s->rwstate = SSL_NOTHING; | |
2649 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
0f113f3e MC |
2650 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
2651 | i = 0; | |
2652 | } else if (i == 1) { | |
2653 | i = 0; | |
b9908bf9 | 2654 | SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE, |
0f113f3e MC |
2655 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
2656 | } | |
2657 | ||
222561fe | 2658 | X509_free(x509); |
25aaa98a | 2659 | EVP_PKEY_free(pkey); |
0f113f3e MC |
2660 | if (i && !ssl3_check_client_certificate(s)) |
2661 | i = 0; | |
2662 | if (i == 0) { | |
2663 | if (s->version == SSL3_VERSION) { | |
2664 | s->s3->tmp.cert_req = 0; | |
2665 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); | |
b9908bf9 | 2666 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
2667 | } else { |
2668 | s->s3->tmp.cert_req = 2; | |
124037fd | 2669 | if (!ssl3_digest_cached_records(s, 0)) { |
dab18ab5 | 2670 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
fe3a3291 | 2671 | ossl_statem_set_error(s); |
dab18ab5 DSH |
2672 | return 0; |
2673 | } | |
0f113f3e MC |
2674 | } |
2675 | } | |
2676 | ||
b9908bf9 | 2677 | return WORK_FINISHED_CONTINUE; |
0f113f3e MC |
2678 | } |
2679 | ||
b9908bf9 MC |
2680 | /* Shouldn't ever get here */ |
2681 | return WORK_ERROR; | |
2682 | } | |
2683 | ||
2684 | int tls_construct_client_certificate(SSL *s) | |
2685 | { | |
2686 | if (!ssl3_output_cert_chain(s, | |
2687 | (s->s3->tmp.cert_req == | |
2688 | 2) ? NULL : s->cert->key)) { | |
2689 | SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
2690 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
fe3a3291 | 2691 | ossl_statem_set_error(s); |
b9908bf9 | 2692 | return 0; |
0f113f3e | 2693 | } |
b9908bf9 MC |
2694 | |
2695 | return 1; | |
0f113f3e MC |
2696 | } |
2697 | ||
2698 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
d02b48c6 | 2699 | |
36d16f8e | 2700 | int ssl3_check_cert_and_algorithm(SSL *s) |
0f113f3e | 2701 | { |
60f43e9e RL |
2702 | int i; |
2703 | #ifndef OPENSSL_NO_EC | |
2704 | int idx; | |
2705 | #endif | |
0f113f3e MC |
2706 | long alg_k, alg_a; |
2707 | EVP_PKEY *pkey = NULL; | |
26c79d56 | 2708 | int al = SSL_AD_HANDSHAKE_FAILURE; |
d02b48c6 | 2709 | |
0f113f3e MC |
2710 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
2711 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 2712 | |
0f113f3e | 2713 | /* we don't have a certificate */ |
55a9a16f | 2714 | if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK)) |
0f113f3e | 2715 | return (1); |
d02b48c6 | 2716 | |
0f113f3e | 2717 | /* This is the passed certificate */ |
d02b48c6 | 2718 | |
10bf4fc2 | 2719 | #ifndef OPENSSL_NO_EC |
60f43e9e | 2720 | idx = s->session->peer_type; |
0f113f3e | 2721 | if (idx == SSL_PKEY_ECC) { |
a273c6ee | 2722 | if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) { |
0f113f3e MC |
2723 | /* check failed */ |
2724 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); | |
2725 | goto f_err; | |
2726 | } else { | |
2727 | return 1; | |
2728 | } | |
2729 | } else if (alg_a & SSL_aECDSA) { | |
2730 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
2731 | SSL_R_MISSING_ECDSA_SIGNING_CERT); | |
2732 | goto f_err; | |
0f113f3e MC |
2733 | } |
2734 | #endif | |
8382fd3a | 2735 | pkey = X509_get0_pubkey(s->session->peer); |
a273c6ee | 2736 | i = X509_certificate_type(s->session->peer, pkey); |
0f113f3e MC |
2737 | |
2738 | /* Check that we have a certificate if we require one */ | |
2739 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { | |
2740 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
2741 | SSL_R_MISSING_RSA_SIGNING_CERT); | |
2742 | goto f_err; | |
2743 | } | |
bc36ee62 | 2744 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
2745 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
2746 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
2747 | SSL_R_MISSING_DSA_SIGNING_CERT); | |
2748 | goto f_err; | |
2749 | } | |
d02b48c6 | 2750 | #endif |
bc36ee62 | 2751 | #ifndef OPENSSL_NO_RSA |
361a1191 KR |
2752 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && |
2753 | !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { | |
2754 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
2755 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
2756 | goto f_err; | |
0f113f3e | 2757 | } |
79df9d62 | 2758 | #endif |
bc36ee62 | 2759 | #ifndef OPENSSL_NO_DH |
fb79abe3 | 2760 | if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) { |
26c79d56 KR |
2761 | al = SSL_AD_INTERNAL_ERROR; |
2762 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
0f113f3e | 2763 | goto f_err; |
0f113f3e | 2764 | } |
d02b48c6 RE |
2765 | #endif |
2766 | ||
0f113f3e MC |
2767 | return (1); |
2768 | f_err: | |
26c79d56 | 2769 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
0f113f3e MC |
2770 | return (0); |
2771 | } | |
2772 | ||
e481f9b9 | 2773 | #ifndef OPENSSL_NO_NEXTPROTONEG |
b9908bf9 MC |
2774 | int tls_construct_next_proto(SSL *s) |
2775 | { | |
2776 | unsigned int len, padding_len; | |
2777 | unsigned char *d; | |
2778 | ||
2779 | len = s->next_proto_negotiated_len; | |
2780 | padding_len = 32 - ((len + 2) % 32); | |
2781 | d = (unsigned char *)s->init_buf->data; | |
2782 | d[4] = len; | |
2783 | memcpy(d + 5, s->next_proto_negotiated, len); | |
2784 | d[5 + len] = padding_len; | |
2785 | memset(d + 6 + len, 0, padding_len); | |
2786 | *(d++) = SSL3_MT_NEXT_PROTO; | |
2787 | l2n3(2 + len + padding_len, d); | |
2788 | s->init_num = 4 + 2 + len + padding_len; | |
2789 | s->init_off = 0; | |
2790 | ||
2791 | return 1; | |
2792 | } | |
6434abbf | 2793 | #endif |
368888bc DSH |
2794 | |
2795 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) | |
0f113f3e MC |
2796 | { |
2797 | int i = 0; | |
368888bc | 2798 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
2799 | if (s->ctx->client_cert_engine) { |
2800 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | |
2801 | SSL_get_client_CA_list(s), | |
2802 | px509, ppkey, NULL, NULL, NULL); | |
2803 | if (i != 0) | |
2804 | return i; | |
2805 | } | |
2806 | #endif | |
2807 | if (s->ctx->client_cert_cb) | |
2808 | i = s->ctx->client_cert_cb(s, px509, ppkey); | |
2809 | return i; | |
2810 | } | |
d45ba43d MC |
2811 | |
2812 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, | |
e9fa092e | 2813 | unsigned char *p) |
d45ba43d MC |
2814 | { |
2815 | int i, j = 0; | |
4a640fb6 | 2816 | const SSL_CIPHER *c; |
d45ba43d MC |
2817 | unsigned char *q; |
2818 | int empty_reneg_info_scsv = !s->renegotiate; | |
2819 | /* Set disabled masks for this session */ | |
2820 | ssl_set_client_disabled(s); | |
2821 | ||
2822 | if (sk == NULL) | |
2823 | return (0); | |
2824 | q = p; | |
d45ba43d MC |
2825 | |
2826 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { | |
2827 | c = sk_SSL_CIPHER_value(sk, i); | |
2828 | /* Skip disabled ciphers */ | |
2829 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) | |
2830 | continue; | |
e9fa092e | 2831 | j = s->method->put_cipher_by_char(c, p); |
d45ba43d MC |
2832 | p += j; |
2833 | } | |
2834 | /* | |
2835 | * If p == q, no ciphers; caller indicates an error. Otherwise, add | |
2836 | * applicable SCSVs. | |
2837 | */ | |
2838 | if (p != q) { | |
2839 | if (empty_reneg_info_scsv) { | |
2840 | static SSL_CIPHER scsv = { | |
2841 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
2842 | }; | |
e9fa092e | 2843 | j = s->method->put_cipher_by_char(&scsv, p); |
d45ba43d | 2844 | p += j; |
d45ba43d MC |
2845 | } |
2846 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { | |
2847 | static SSL_CIPHER scsv = { | |
2848 | 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
2849 | }; | |
e9fa092e | 2850 | j = s->method->put_cipher_by_char(&scsv, p); |
d45ba43d MC |
2851 | p += j; |
2852 | } | |
2853 | } | |
2854 | ||
2855 | return (p - q); | |
2856 | } |