]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
8e2f6b79 | 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 | |
8e2f6b79 | 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 | 49 | |
de469ef2 | 50 | |
d02b48c6 | 51 | #include <stdio.h> |
8ba708e5 | 52 | #include "../ssl_locl.h" |
61ae935a | 53 | #include "statem_locl.h" |
68570797 | 54 | #include "internal/constant_time_locl.h" |
ec577822 BM |
55 | #include <openssl/buffer.h> |
56 | #include <openssl/rand.h> | |
57 | #include <openssl/objects.h> | |
58 | #include <openssl/evp.h> | |
6434abbf | 59 | #include <openssl/hmac.h> |
ec577822 | 60 | #include <openssl/x509.h> |
3c27208f | 61 | #include <openssl/dh.h> |
d095b68d | 62 | #include <openssl/bn.h> |
dbad1690 | 63 | #include <openssl/md5.h> |
f9b3bff6 | 64 | |
38a3cbfb EK |
65 | static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, |
66 | PACKET *cipher_suites, | |
67 | STACK_OF(SSL_CIPHER) **skp, | |
68 | int sslv2format, int *al); | |
d45ba43d | 69 | |
61ae935a MC |
70 | /* |
71 | * server_read_transition() encapsulates the logic for the allowed handshake | |
72 | * state transitions when the server is reading messages from the client. The | |
73 | * message type that the client has sent is provided in |mt|. The current state | |
74 | * is in |s->statem.hand_state|. | |
75 | * | |
76 | * Valid return values are: | |
77 | * 1: Success (transition allowed) | |
78 | * 0: Error (transition not allowed) | |
79 | */ | |
8481f583 | 80 | int ossl_statem_server_read_transition(SSL *s, int mt) |
61ae935a | 81 | { |
d6f1a6e9 | 82 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
83 | |
84 | switch(st->hand_state) { | |
85 | case TLS_ST_BEFORE: | |
86 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
87 | if (mt == SSL3_MT_CLIENT_HELLO) { | |
88 | st->hand_state = TLS_ST_SR_CLNT_HELLO; | |
89 | return 1; | |
90 | } | |
91 | break; | |
92 | ||
93 | case TLS_ST_SW_SRVR_DONE: | |
94 | /* | |
95 | * If we get a CKE message after a ServerDone then either | |
96 | * 1) We didn't request a Certificate | |
97 | * OR | |
98 | * 2) If we did request one then | |
99 | * a) We allow no Certificate to be returned | |
100 | * AND | |
101 | * b) We are running SSL3 (in TLS1.0+ the client must return a 0 | |
102 | * list if we requested a certificate) | |
103 | */ | |
104 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE | |
105 | && (!s->s3->tmp.cert_request | |
106 | || (!((s->verify_mode & SSL_VERIFY_PEER) && | |
107 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | |
108 | && (s->version == SSL3_VERSION)))) { | |
109 | st->hand_state = TLS_ST_SR_KEY_EXCH; | |
110 | return 1; | |
111 | } else if (s->s3->tmp.cert_request) { | |
112 | if (mt == SSL3_MT_CERTIFICATE) { | |
113 | st->hand_state = TLS_ST_SR_CERT; | |
114 | return 1; | |
f100b031 | 115 | } |
61ae935a MC |
116 | } |
117 | break; | |
118 | ||
119 | case TLS_ST_SR_CERT: | |
120 | if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { | |
121 | st->hand_state = TLS_ST_SR_KEY_EXCH; | |
122 | return 1; | |
123 | } | |
124 | break; | |
125 | ||
126 | case TLS_ST_SR_KEY_EXCH: | |
127 | /* | |
128 | * We should only process a CertificateVerify message if we have | |
129 | * received a Certificate from the client. If so then |s->session->peer| | |
130 | * will be non NULL. In some instances a CertificateVerify message is | |
131 | * not required even if the peer has sent a Certificate (e.g. such as in | |
a71a4966 | 132 | * the case of static DH). In that case |st->no_cert_verify| should be |
61ae935a MC |
133 | * set. |
134 | */ | |
a71a4966 | 135 | if (s->session->peer == NULL || st->no_cert_verify) { |
61ae935a MC |
136 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
137 | /* | |
138 | * For the ECDH ciphersuites when the client sends its ECDH | |
139 | * pub key in a certificate, the CertificateVerify message is | |
140 | * not sent. Also for GOST ciphersuites when the client uses | |
141 | * its key from the certificate for key exchange. | |
142 | */ | |
143 | st->hand_state = TLS_ST_SR_CHANGE; | |
144 | return 1; | |
145 | } | |
146 | } else { | |
147 | if (mt == SSL3_MT_CERTIFICATE_VERIFY) { | |
148 | st->hand_state = TLS_ST_SR_CERT_VRFY; | |
149 | return 1; | |
150 | } | |
151 | } | |
152 | break; | |
153 | ||
154 | case TLS_ST_SR_CERT_VRFY: | |
155 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
156 | st->hand_state = TLS_ST_SR_CHANGE; | |
157 | return 1; | |
158 | } | |
159 | break; | |
160 | ||
161 | case TLS_ST_SR_CHANGE: | |
162 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
163 | if (s->s3->next_proto_neg_seen) { | |
164 | if (mt == SSL3_MT_NEXT_PROTO) { | |
165 | st->hand_state = TLS_ST_SR_NEXT_PROTO; | |
166 | return 1; | |
167 | } | |
168 | } else { | |
169 | #endif | |
170 | if (mt == SSL3_MT_FINISHED) { | |
171 | st->hand_state = TLS_ST_SR_FINISHED; | |
172 | return 1; | |
173 | } | |
174 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
175 | } | |
176 | #endif | |
177 | break; | |
178 | ||
179 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
180 | case TLS_ST_SR_NEXT_PROTO: | |
181 | if (mt == SSL3_MT_FINISHED) { | |
182 | st->hand_state = TLS_ST_SR_FINISHED; | |
183 | return 1; | |
184 | } | |
185 | break; | |
186 | #endif | |
187 | ||
188 | case TLS_ST_SW_FINISHED: | |
189 | if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { | |
190 | st->hand_state = TLS_ST_SR_CHANGE; | |
191 | return 1; | |
192 | } | |
193 | break; | |
194 | ||
195 | default: | |
196 | break; | |
197 | } | |
198 | ||
199 | /* No valid transition found */ | |
200 | return 0; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Should we send a ServerKeyExchange message? | |
205 | * | |
206 | * Valid return values are: | |
207 | * 1: Yes | |
208 | * 0: No | |
209 | */ | |
bb3e20cf | 210 | static int send_server_key_exchange(SSL *s) |
61ae935a MC |
211 | { |
212 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
213 | ||
214 | /* | |
361a1191 | 215 | * only send a ServerKeyExchange if DH or fortezza but we have a |
61ae935a MC |
216 | * sign only certificate PSK: may send PSK identity hints For |
217 | * ECC ciphersuites, we send a serverKeyExchange message only if | |
218 | * the cipher suite is either ECDH-anon or ECDHE. In other cases, | |
219 | * the server certificate contains the server's public key for | |
220 | * key exchange. | |
221 | */ | |
bc71f910 | 222 | if (alg_k & (SSL_kDHE|SSL_kECDHE) |
61ae935a MC |
223 | /* |
224 | * PSK: send ServerKeyExchange if PSK identity hint if | |
225 | * provided | |
226 | */ | |
227 | #ifndef OPENSSL_NO_PSK | |
228 | /* Only send SKE if we have identity hint for plain PSK */ | |
229 | || ((alg_k & (SSL_kPSK | SSL_kRSAPSK)) | |
230 | && s->cert->psk_identity_hint) | |
231 | /* For other PSK always send SKE */ | |
232 | || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK))) | |
233 | #endif | |
234 | #ifndef OPENSSL_NO_SRP | |
235 | /* SRP: send ServerKeyExchange */ | |
236 | || (alg_k & SSL_kSRP) | |
237 | #endif | |
238 | ) { | |
239 | return 1; | |
240 | } | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
245 | /* | |
246 | * Should we send a CertificateRequest message? | |
247 | * | |
248 | * Valid return values are: | |
249 | * 1: Yes | |
250 | * 0: No | |
251 | */ | |
bb3e20cf | 252 | static int send_certificate_request(SSL *s) |
61ae935a MC |
253 | { |
254 | if ( | |
255 | /* don't request cert unless asked for it: */ | |
256 | s->verify_mode & SSL_VERIFY_PEER | |
257 | /* | |
258 | * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert | |
259 | * during re-negotiation: | |
260 | */ | |
261 | && ((s->session->peer == NULL) || | |
262 | !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) | |
263 | /* | |
264 | * never request cert in anonymous ciphersuites (see | |
265 | * section "Certificate request" in SSL 3 drafts and in | |
266 | * RFC 2246): | |
267 | */ | |
268 | && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) | |
269 | /* | |
270 | * ... except when the application insists on | |
d4d78943 | 271 | * verification (against the specs, but statem_clnt.c accepts |
61ae935a MC |
272 | * this for SSL 3) |
273 | */ | |
274 | || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | |
275 | /* don't request certificate for SRP auth */ | |
276 | && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP) | |
277 | /* | |
278 | * With normal PSK Certificates and Certificate Requests | |
279 | * are omitted | |
280 | */ | |
b7fa1f98 | 281 | && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) { |
61ae935a MC |
282 | return 1; |
283 | } | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | /* | |
289 | * server_write_transition() works out what handshake state to move to next | |
290 | * when the server is writing messages to be sent to the client. | |
291 | */ | |
8481f583 | 292 | WRITE_TRAN ossl_statem_server_write_transition(SSL *s) |
61ae935a | 293 | { |
d6f1a6e9 | 294 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
295 | |
296 | switch(st->hand_state) { | |
297 | case TLS_ST_BEFORE: | |
298 | /* Just go straight to trying to read from the client */; | |
299 | return WRITE_TRAN_FINISHED; | |
300 | ||
301 | case TLS_ST_OK: | |
302 | /* We must be trying to renegotiate */ | |
303 | st->hand_state = TLS_ST_SW_HELLO_REQ; | |
304 | return WRITE_TRAN_CONTINUE; | |
305 | ||
306 | case TLS_ST_SW_HELLO_REQ: | |
307 | st->hand_state = TLS_ST_OK; | |
fe3a3291 | 308 | ossl_statem_set_in_init(s, 0); |
61ae935a MC |
309 | return WRITE_TRAN_CONTINUE; |
310 | ||
311 | case TLS_ST_SR_CLNT_HELLO: | |
312 | if (SSL_IS_DTLS(s) && !s->d1->cookie_verified | |
313 | && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | |
314 | st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST; | |
315 | else | |
316 | st->hand_state = TLS_ST_SW_SRVR_HELLO; | |
317 | return WRITE_TRAN_CONTINUE; | |
318 | ||
319 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
320 | return WRITE_TRAN_FINISHED; | |
321 | ||
322 | case TLS_ST_SW_SRVR_HELLO: | |
323 | if (s->hit) { | |
324 | if (s->tlsext_ticket_expected) | |
325 | st->hand_state = TLS_ST_SW_SESSION_TICKET; | |
326 | else | |
327 | st->hand_state = TLS_ST_SW_CHANGE; | |
328 | } else { | |
329 | /* Check if it is anon DH or anon ECDH, */ | |
330 | /* normal PSK or SRP */ | |
331 | if (!(s->s3->tmp.new_cipher->algorithm_auth & | |
332 | (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { | |
333 | st->hand_state = TLS_ST_SW_CERT; | |
334 | } else if (send_server_key_exchange(s)) { | |
335 | st->hand_state = TLS_ST_SW_KEY_EXCH; | |
336 | } else if (send_certificate_request(s)) { | |
337 | st->hand_state = TLS_ST_SW_CERT_REQ; | |
338 | } else { | |
339 | st->hand_state = TLS_ST_SW_SRVR_DONE; | |
340 | } | |
341 | } | |
342 | return WRITE_TRAN_CONTINUE; | |
343 | ||
344 | case TLS_ST_SW_CERT: | |
345 | if (s->tlsext_status_expected) { | |
346 | st->hand_state = TLS_ST_SW_CERT_STATUS; | |
347 | return WRITE_TRAN_CONTINUE; | |
348 | } | |
349 | /* Fall through */ | |
350 | ||
351 | case TLS_ST_SW_CERT_STATUS: | |
352 | if (send_server_key_exchange(s)) { | |
353 | st->hand_state = TLS_ST_SW_KEY_EXCH; | |
354 | return WRITE_TRAN_CONTINUE; | |
355 | } | |
356 | /* Fall through */ | |
357 | ||
358 | case TLS_ST_SW_KEY_EXCH: | |
359 | if (send_certificate_request(s)) { | |
360 | st->hand_state = TLS_ST_SW_CERT_REQ; | |
361 | return WRITE_TRAN_CONTINUE; | |
362 | } | |
363 | /* Fall through */ | |
364 | ||
365 | case TLS_ST_SW_CERT_REQ: | |
366 | st->hand_state = TLS_ST_SW_SRVR_DONE; | |
367 | return WRITE_TRAN_CONTINUE; | |
368 | ||
369 | case TLS_ST_SW_SRVR_DONE: | |
370 | return WRITE_TRAN_FINISHED; | |
371 | ||
372 | case TLS_ST_SR_FINISHED: | |
373 | if (s->hit) { | |
374 | st->hand_state = TLS_ST_OK; | |
fe3a3291 | 375 | ossl_statem_set_in_init(s, 0); |
61ae935a MC |
376 | return WRITE_TRAN_CONTINUE; |
377 | } else if (s->tlsext_ticket_expected) { | |
378 | st->hand_state = TLS_ST_SW_SESSION_TICKET; | |
379 | } else { | |
380 | st->hand_state = TLS_ST_SW_CHANGE; | |
381 | } | |
382 | return WRITE_TRAN_CONTINUE; | |
383 | ||
384 | case TLS_ST_SW_SESSION_TICKET: | |
385 | st->hand_state = TLS_ST_SW_CHANGE; | |
386 | return WRITE_TRAN_CONTINUE; | |
387 | ||
388 | case TLS_ST_SW_CHANGE: | |
389 | st->hand_state = TLS_ST_SW_FINISHED; | |
390 | return WRITE_TRAN_CONTINUE; | |
391 | ||
392 | case TLS_ST_SW_FINISHED: | |
393 | if (s->hit) { | |
394 | return WRITE_TRAN_FINISHED; | |
395 | } | |
396 | st->hand_state = TLS_ST_OK; | |
fe3a3291 | 397 | ossl_statem_set_in_init(s, 0); |
61ae935a MC |
398 | return WRITE_TRAN_CONTINUE; |
399 | ||
400 | default: | |
401 | /* Shouldn't happen */ | |
402 | return WRITE_TRAN_ERROR; | |
403 | } | |
404 | } | |
405 | ||
406 | /* | |
407 | * Perform any pre work that needs to be done prior to sending a message from | |
408 | * the server to the client. | |
409 | */ | |
8481f583 | 410 | WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst) |
61ae935a | 411 | { |
d6f1a6e9 | 412 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
413 | |
414 | switch(st->hand_state) { | |
415 | case TLS_ST_SW_HELLO_REQ: | |
416 | s->shutdown = 0; | |
417 | if (SSL_IS_DTLS(s)) | |
418 | dtls1_clear_record_buffer(s); | |
419 | break; | |
420 | ||
421 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
422 | s->shutdown = 0; | |
423 | if (SSL_IS_DTLS(s)) { | |
424 | dtls1_clear_record_buffer(s); | |
425 | /* We don't buffer this message so don't use the timer */ | |
426 | st->use_timer = 0; | |
427 | } | |
428 | break; | |
429 | ||
430 | case TLS_ST_SW_SRVR_HELLO: | |
431 | if (SSL_IS_DTLS(s)) { | |
432 | /* | |
433 | * Messages we write from now on should be bufferred and | |
434 | * retransmitted if necessary, so we need to use the timer now | |
435 | */ | |
436 | st->use_timer = 1; | |
437 | } | |
438 | break; | |
439 | ||
440 | case TLS_ST_SW_SRVR_DONE: | |
441 | #ifndef OPENSSL_NO_SCTP | |
442 | if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) | |
443 | return dtls_wait_for_dry(s); | |
444 | #endif | |
445 | return WORK_FINISHED_CONTINUE; | |
446 | ||
447 | case TLS_ST_SW_SESSION_TICKET: | |
448 | if (SSL_IS_DTLS(s)) { | |
449 | /* | |
450 | * We're into the last flight. We don't retransmit the last flight | |
451 | * unless we need to, so we don't use the timer | |
452 | */ | |
453 | st->use_timer = 0; | |
454 | } | |
455 | break; | |
456 | ||
457 | case TLS_ST_SW_CHANGE: | |
458 | s->session->cipher = s->s3->tmp.new_cipher; | |
459 | if (!s->method->ssl3_enc->setup_key_block(s)) { | |
fe3a3291 | 460 | ossl_statem_set_error(s); |
61ae935a MC |
461 | return WORK_ERROR; |
462 | } | |
463 | if (SSL_IS_DTLS(s)) { | |
464 | /* | |
465 | * We're into the last flight. We don't retransmit the last flight | |
466 | * unless we need to, so we don't use the timer. This might have | |
467 | * already been set to 0 if we sent a NewSessionTicket message, | |
468 | * but we'll set it again here in case we didn't. | |
469 | */ | |
470 | st->use_timer = 0; | |
471 | } | |
472 | return WORK_FINISHED_CONTINUE; | |
473 | ||
474 | case TLS_ST_OK: | |
475 | return tls_finish_handshake(s, wst); | |
476 | ||
477 | default: | |
478 | /* No pre work to be done */ | |
479 | break; | |
480 | } | |
481 | ||
482 | return WORK_FINISHED_CONTINUE; | |
483 | } | |
484 | ||
485 | /* | |
486 | * Perform any work that needs to be done after sending a message from the | |
487 | * server to the client. | |
488 | */ | |
8481f583 | 489 | WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst) |
61ae935a | 490 | { |
d6f1a6e9 | 491 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
492 | |
493 | s->init_num = 0; | |
494 | ||
495 | switch(st->hand_state) { | |
496 | case TLS_ST_SW_HELLO_REQ: | |
497 | if (statem_flush(s) != 1) | |
498 | return WORK_MORE_A; | |
2c4a056f MC |
499 | if (!ssl3_init_finished_mac(s)) { |
500 | ossl_statem_set_error(s); | |
501 | return WORK_ERROR; | |
502 | } | |
61ae935a MC |
503 | break; |
504 | ||
505 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
506 | if (statem_flush(s) != 1) | |
507 | return WORK_MORE_A; | |
508 | /* HelloVerifyRequest resets Finished MAC */ | |
2c4a056f MC |
509 | if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) { |
510 | ossl_statem_set_error(s); | |
511 | return WORK_ERROR; | |
512 | } | |
61ae935a MC |
513 | /* |
514 | * The next message should be another ClientHello which we need to | |
515 | * treat like it was the first packet | |
516 | */ | |
517 | s->first_packet = 1; | |
518 | break; | |
519 | ||
520 | case TLS_ST_SW_SRVR_HELLO: | |
521 | #ifndef OPENSSL_NO_SCTP | |
522 | if (SSL_IS_DTLS(s) && s->hit) { | |
523 | unsigned char sctpauthkey[64]; | |
524 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
525 | ||
526 | /* | |
527 | * Add new shared key for SCTP-Auth, will be ignored if no | |
528 | * SCTP used. | |
529 | */ | |
141eb8c6 MC |
530 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
531 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
61ae935a MC |
532 | |
533 | if (SSL_export_keying_material(s, sctpauthkey, | |
534 | sizeof(sctpauthkey), labelbuffer, | |
535 | sizeof(labelbuffer), NULL, 0, 0) <= 0) { | |
fe3a3291 | 536 | ossl_statem_set_error(s); |
61ae935a MC |
537 | return WORK_ERROR; |
538 | } | |
539 | ||
540 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, | |
541 | sizeof(sctpauthkey), sctpauthkey); | |
542 | } | |
543 | #endif | |
544 | break; | |
545 | ||
546 | case TLS_ST_SW_CHANGE: | |
547 | #ifndef OPENSSL_NO_SCTP | |
548 | if (SSL_IS_DTLS(s) && !s->hit) { | |
549 | /* | |
550 | * Change to new shared key of SCTP-Auth, will be ignored if | |
551 | * no SCTP used. | |
552 | */ | |
553 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
554 | 0, NULL); | |
555 | } | |
556 | #endif | |
557 | if (!s->method->ssl3_enc->change_cipher_state(s, | |
558 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | |
fe3a3291 | 559 | ossl_statem_set_error(s); |
61ae935a MC |
560 | return WORK_ERROR; |
561 | } | |
562 | ||
563 | if (SSL_IS_DTLS(s)) | |
564 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | |
565 | break; | |
566 | ||
567 | case TLS_ST_SW_SRVR_DONE: | |
568 | if (statem_flush(s) != 1) | |
569 | return WORK_MORE_A; | |
570 | break; | |
571 | ||
572 | case TLS_ST_SW_FINISHED: | |
573 | if (statem_flush(s) != 1) | |
574 | return WORK_MORE_A; | |
575 | #ifndef OPENSSL_NO_SCTP | |
576 | if (SSL_IS_DTLS(s) && s->hit) { | |
577 | /* | |
578 | * Change to new shared key of SCTP-Auth, will be ignored if | |
579 | * no SCTP used. | |
580 | */ | |
581 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, | |
582 | 0, NULL); | |
583 | } | |
584 | #endif | |
585 | break; | |
586 | ||
587 | default: | |
588 | /* No post work to be done */ | |
589 | break; | |
590 | } | |
591 | ||
592 | return WORK_FINISHED_CONTINUE; | |
593 | } | |
594 | ||
595 | /* | |
596 | * Construct a message to be sent from the server to the client. | |
597 | * | |
598 | * Valid return values are: | |
599 | * 1: Success | |
600 | * 0: Error | |
601 | */ | |
8481f583 | 602 | int ossl_statem_server_construct_message(SSL *s) |
61ae935a | 603 | { |
d6f1a6e9 | 604 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
605 | |
606 | switch(st->hand_state) { | |
607 | case DTLS_ST_SW_HELLO_VERIFY_REQUEST: | |
608 | return dtls_construct_hello_verify_request(s); | |
609 | ||
610 | case TLS_ST_SW_HELLO_REQ: | |
611 | return tls_construct_hello_request(s); | |
612 | ||
613 | case TLS_ST_SW_SRVR_HELLO: | |
614 | return tls_construct_server_hello(s); | |
615 | ||
616 | case TLS_ST_SW_CERT: | |
617 | return tls_construct_server_certificate(s); | |
618 | ||
619 | case TLS_ST_SW_KEY_EXCH: | |
620 | return tls_construct_server_key_exchange(s); | |
621 | ||
622 | case TLS_ST_SW_CERT_REQ: | |
623 | return tls_construct_certificate_request(s); | |
624 | ||
625 | case TLS_ST_SW_SRVR_DONE: | |
626 | return tls_construct_server_done(s); | |
627 | ||
628 | case TLS_ST_SW_SESSION_TICKET: | |
629 | return tls_construct_new_session_ticket(s); | |
630 | ||
631 | case TLS_ST_SW_CERT_STATUS: | |
632 | return tls_construct_cert_status(s); | |
633 | ||
634 | case TLS_ST_SW_CHANGE: | |
635 | if (SSL_IS_DTLS(s)) | |
636 | return dtls_construct_change_cipher_spec(s); | |
637 | else | |
638 | return tls_construct_change_cipher_spec(s); | |
639 | ||
640 | case TLS_ST_SW_FINISHED: | |
641 | return tls_construct_finished(s, | |
642 | s->method-> | |
643 | ssl3_enc->server_finished_label, | |
644 | s->method-> | |
645 | ssl3_enc->server_finished_label_len); | |
646 | ||
647 | default: | |
648 | /* Shouldn't happen */ | |
649 | break; | |
650 | } | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
8a18bc25 AG |
655 | /* |
656 | * Maximum size (excluding the Handshake header) of a ClientHello message, | |
657 | * calculated as follows: | |
658 | * | |
659 | * 2 + # client_version | |
660 | * 32 + # only valid length for random | |
661 | * 1 + # length of session_id | |
662 | * 32 + # maximum size for session_id | |
663 | * 2 + # length of cipher suites | |
664 | * 2^16-2 + # maximum length of cipher suites array | |
665 | * 1 + # length of compression_methods | |
666 | * 2^8-1 + # maximum length of compression methods | |
667 | * 2 + # length of extensions | |
668 | * 2^16-1 # maximum length of extensions | |
669 | */ | |
670 | #define CLIENT_HELLO_MAX_LENGTH 131396 | |
671 | ||
61ae935a MC |
672 | #define CLIENT_KEY_EXCH_MAX_LENGTH 2048 |
673 | #define NEXT_PROTO_MAX_LENGTH 514 | |
674 | ||
675 | /* | |
676 | * Returns the maximum allowed length for the current message that we are | |
677 | * reading. Excludes the message header. | |
678 | */ | |
8481f583 | 679 | unsigned long ossl_statem_server_max_message_size(SSL *s) |
61ae935a | 680 | { |
d6f1a6e9 | 681 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
682 | |
683 | switch(st->hand_state) { | |
684 | case TLS_ST_SR_CLNT_HELLO: | |
8a18bc25 | 685 | return CLIENT_HELLO_MAX_LENGTH; |
61ae935a MC |
686 | |
687 | case TLS_ST_SR_CERT: | |
688 | return s->max_cert_list; | |
689 | ||
690 | case TLS_ST_SR_KEY_EXCH: | |
691 | return CLIENT_KEY_EXCH_MAX_LENGTH; | |
692 | ||
693 | case TLS_ST_SR_CERT_VRFY: | |
694 | return SSL3_RT_MAX_PLAIN_LENGTH; | |
695 | ||
696 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
697 | case TLS_ST_SR_NEXT_PROTO: | |
698 | return NEXT_PROTO_MAX_LENGTH; | |
699 | #endif | |
700 | ||
701 | case TLS_ST_SR_CHANGE: | |
702 | return CCS_MAX_LENGTH; | |
703 | ||
704 | case TLS_ST_SR_FINISHED: | |
705 | return FINISHED_MAX_LENGTH; | |
706 | ||
707 | default: | |
708 | /* Shouldn't happen */ | |
709 | break; | |
710 | } | |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
715 | /* | |
716 | * Process a message that the server has received from the client. | |
717 | */ | |
8481f583 | 718 | MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt) |
61ae935a | 719 | { |
d6f1a6e9 | 720 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
721 | |
722 | switch(st->hand_state) { | |
723 | case TLS_ST_SR_CLNT_HELLO: | |
724 | return tls_process_client_hello(s, pkt); | |
725 | ||
726 | case TLS_ST_SR_CERT: | |
727 | return tls_process_client_certificate(s, pkt); | |
728 | ||
729 | case TLS_ST_SR_KEY_EXCH: | |
730 | return tls_process_client_key_exchange(s, pkt); | |
731 | ||
732 | case TLS_ST_SR_CERT_VRFY: | |
733 | return tls_process_cert_verify(s, pkt); | |
734 | ||
735 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
736 | case TLS_ST_SR_NEXT_PROTO: | |
737 | return tls_process_next_proto(s, pkt); | |
738 | #endif | |
739 | ||
740 | case TLS_ST_SR_CHANGE: | |
741 | return tls_process_change_cipher_spec(s, pkt); | |
742 | ||
743 | case TLS_ST_SR_FINISHED: | |
744 | return tls_process_finished(s, pkt); | |
745 | ||
746 | default: | |
747 | /* Shouldn't happen */ | |
748 | break; | |
749 | } | |
750 | ||
751 | return MSG_PROCESS_ERROR; | |
752 | } | |
753 | ||
754 | /* | |
755 | * Perform any further processing required following the receipt of a message | |
756 | * from the client | |
757 | */ | |
8481f583 | 758 | WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst) |
61ae935a | 759 | { |
d6f1a6e9 | 760 | OSSL_STATEM *st = &s->statem; |
61ae935a MC |
761 | |
762 | switch(st->hand_state) { | |
763 | case TLS_ST_SR_CLNT_HELLO: | |
764 | return tls_post_process_client_hello(s, wst); | |
765 | ||
766 | case TLS_ST_SR_KEY_EXCH: | |
767 | return tls_post_process_client_key_exchange(s, wst); | |
768 | ||
769 | case TLS_ST_SR_CERT_VRFY: | |
770 | #ifndef OPENSSL_NO_SCTP | |
771 | if ( /* Is this SCTP? */ | |
772 | BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
773 | /* Are we renegotiating? */ | |
774 | && s->renegotiate | |
775 | && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { | |
776 | s->s3->in_read_app_data = 2; | |
777 | s->rwstate = SSL_READING; | |
778 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
779 | BIO_set_retry_read(SSL_get_rbio(s)); | |
d99b0691 | 780 | ossl_statem_set_sctp_read_sock(s, 1); |
61ae935a MC |
781 | return WORK_MORE_A; |
782 | } else { | |
d99b0691 | 783 | ossl_statem_set_sctp_read_sock(s, 0); |
61ae935a MC |
784 | } |
785 | #endif | |
786 | return WORK_FINISHED_CONTINUE; | |
787 | ||
61ae935a MC |
788 | default: |
789 | break; | |
790 | } | |
791 | ||
792 | /* Shouldn't happen */ | |
793 | return WORK_ERROR; | |
794 | } | |
795 | ||
edc032b5 | 796 | #ifndef OPENSSL_NO_SRP |
71fa4513 | 797 | static int ssl_check_srp_ext_ClientHello(SSL *s, int *al) |
0f113f3e MC |
798 | { |
799 | int ret = SSL_ERROR_NONE; | |
800 | ||
801 | *al = SSL_AD_UNRECOGNIZED_NAME; | |
802 | ||
803 | if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) && | |
804 | (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { | |
805 | if (s->srp_ctx.login == NULL) { | |
806 | /* | |
807 | * RFC 5054 says SHOULD reject, we do so if There is no srp | |
808 | * login name | |
809 | */ | |
810 | ret = SSL3_AL_FATAL; | |
811 | *al = SSL_AD_UNKNOWN_PSK_IDENTITY; | |
812 | } else { | |
813 | ret = SSL_srp_server_param_with_username(s, al); | |
814 | } | |
815 | } | |
816 | return ret; | |
817 | } | |
edc032b5 BL |
818 | #endif |
819 | ||
e27f234a MC |
820 | int tls_construct_hello_request(SSL *s) |
821 | { | |
822 | if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) { | |
823 | SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 824 | ossl_statem_set_error(s); |
e27f234a MC |
825 | return 0; |
826 | } | |
827 | ||
828 | return 1; | |
829 | } | |
830 | ||
8ba708e5 MC |
831 | unsigned int dtls_raw_hello_verify_request(unsigned char *buf, |
832 | unsigned char *cookie, | |
833 | unsigned char cookie_len) | |
834 | { | |
835 | unsigned int msg_len; | |
836 | unsigned char *p; | |
837 | ||
838 | p = buf; | |
839 | /* Always use DTLS 1.0 version: see RFC 6347 */ | |
840 | *(p++) = DTLS1_VERSION >> 8; | |
841 | *(p++) = DTLS1_VERSION & 0xFF; | |
842 | ||
843 | *(p++) = (unsigned char)cookie_len; | |
844 | memcpy(p, cookie, cookie_len); | |
845 | p += cookie_len; | |
846 | msg_len = p - buf; | |
847 | ||
848 | return msg_len; | |
849 | } | |
850 | ||
851 | int dtls_construct_hello_verify_request(SSL *s) | |
852 | { | |
853 | unsigned int len; | |
854 | unsigned char *buf; | |
855 | ||
856 | buf = (unsigned char *)s->init_buf->data; | |
857 | ||
858 | if (s->ctx->app_gen_cookie_cb == NULL || | |
859 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | |
860 | &(s->d1->cookie_len)) == 0 || | |
861 | s->d1->cookie_len > 255) { | |
f0659bdb | 862 | SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST, |
8ba708e5 | 863 | SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
fe3a3291 | 864 | ossl_statem_set_error(s); |
8ba708e5 MC |
865 | return 0; |
866 | } | |
867 | ||
868 | len = dtls_raw_hello_verify_request(&buf[DTLS1_HM_HEADER_LENGTH], | |
869 | s->d1->cookie, s->d1->cookie_len); | |
870 | ||
a773b52a | 871 | dtls1_set_message_header(s, DTLS1_MT_HELLO_VERIFY_REQUEST, len, 0, |
8ba708e5 MC |
872 | len); |
873 | len += DTLS1_HM_HEADER_LENGTH; | |
874 | ||
875 | /* number of bytes to write */ | |
876 | s->init_num = len; | |
877 | s->init_off = 0; | |
878 | ||
879 | return 1; | |
880 | } | |
881 | ||
be3583fa | 882 | MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt) |
e27f234a MC |
883 | { |
884 | int i, al = SSL_AD_INTERNAL_ERROR; | |
885 | unsigned int j, complen = 0; | |
886 | unsigned long id; | |
4a640fb6 | 887 | const SSL_CIPHER *c; |
e27f234a MC |
888 | #ifndef OPENSSL_NO_COMP |
889 | SSL_COMP *comp = NULL; | |
890 | #endif | |
891 | STACK_OF(SSL_CIPHER) *ciphers = NULL; | |
4fa52141 | 892 | int protverr; |
e27f234a | 893 | /* |cookie| will only be initialized for DTLS. */ |
73999b62 | 894 | PACKET session_id, cipher_suites, compression, extensions, cookie; |
e27f234a | 895 | int is_v2_record; |
6e3ff632 | 896 | static const unsigned char null_compression = 0; |
e27f234a | 897 | |
b3e2272c EK |
898 | is_v2_record = RECORD_LAYER_is_sslv2_record(&s->rlayer); |
899 | ||
bbafa47b | 900 | PACKET_null_init(&cookie); |
32ec4153 | 901 | /* First lets get s->client_version set correctly */ |
b3e2272c | 902 | if (is_v2_record) { |
9ceb2426 MC |
903 | unsigned int version; |
904 | unsigned int mt; | |
32ec4153 MC |
905 | /*- |
906 | * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 | |
907 | * header is sent directly on the wire, not wrapped as a TLS | |
908 | * record. Our record layer just processes the message length and passes | |
909 | * the rest right through. Its format is: | |
910 | * Byte Content | |
911 | * 0-1 msg_length - decoded by the record layer | |
912 | * 2 msg_type - s->init_msg points here | |
913 | * 3-4 version | |
914 | * 5-6 cipher_spec_length | |
915 | * 7-8 session_id_length | |
916 | * 9-10 challenge_length | |
917 | * ... ... | |
918 | */ | |
919 | ||
73999b62 | 920 | if (!PACKET_get_1(pkt, &mt) |
9ceb2426 | 921 | || mt != SSL2_MT_CLIENT_HELLO) { |
32ec4153 MC |
922 | /* |
923 | * Should never happen. We should have tested this in the record | |
924 | * layer in order to have determined that this is a SSLv2 record | |
925 | * in the first place | |
926 | */ | |
e27f234a | 927 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
d45ba43d | 928 | goto err; |
32ec4153 MC |
929 | } |
930 | ||
73999b62 | 931 | if (!PACKET_get_net_2(pkt, &version)) { |
9ceb2426 | 932 | /* No protocol version supplied! */ |
e27f234a | 933 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL); |
9ceb2426 MC |
934 | goto err; |
935 | } | |
936 | if (version == 0x0002) { | |
32ec4153 | 937 | /* This is real SSLv2. We don't support it. */ |
e27f234a | 938 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL); |
32ec4153 | 939 | goto err; |
9ceb2426 | 940 | } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) { |
32ec4153 | 941 | /* SSLv3/TLS */ |
9ceb2426 | 942 | s->client_version = version; |
32ec4153 MC |
943 | } else { |
944 | /* No idea what protocol this is */ | |
e27f234a | 945 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL); |
32ec4153 MC |
946 | goto err; |
947 | } | |
948 | } else { | |
949 | /* | |
9ceb2426 MC |
950 | * use version from inside client hello, not from record header (may |
951 | * differ: see RFC 2246, Appendix E, second paragraph) | |
32ec4153 | 952 | */ |
73999b62 | 953 | if(!PACKET_get_net_2(pkt, (unsigned int *)&s->client_version)) { |
32ec4153 | 954 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 955 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT); |
32ec4153 MC |
956 | goto f_err; |
957 | } | |
5e9f0eeb MC |
958 | } |
959 | ||
4fa52141 VD |
960 | /* |
961 | * Do SSL/TLS version negotiation if applicable. For DTLS we just check | |
962 | * versions are potentially compatible. Version negotiation comes later. | |
963 | */ | |
32ec4153 | 964 | if (!SSL_IS_DTLS(s)) { |
4fa52141 VD |
965 | protverr = ssl_choose_server_version(s); |
966 | } else if (s->method->version != DTLS_ANY_VERSION && | |
967 | DTLS_VERSION_LT(s->client_version, s->version)) { | |
968 | protverr = SSL_R_VERSION_TOO_LOW; | |
969 | } else { | |
32ec4153 MC |
970 | protverr = 0; |
971 | } | |
972 | ||
973 | if (protverr) { | |
4fa52141 | 974 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr); |
32ec4153 | 975 | if ((!s->enc_write_ctx && !s->write_hash)) { |
0f113f3e MC |
976 | /* |
977 | * similar to ssl3_get_record, send alert using remote version | |
978 | * number | |
979 | */ | |
980 | s->version = s->client_version; | |
981 | } | |
982 | al = SSL_AD_PROTOCOL_VERSION; | |
983 | goto f_err; | |
984 | } | |
985 | ||
b3e2272c EK |
986 | /* Parse the message and load client random. */ |
987 | if (is_v2_record) { | |
32ec4153 MC |
988 | /* |
989 | * Handle an SSLv2 backwards compatible ClientHello | |
990 | * Note, this is only for SSLv3+ using the backward compatible format. | |
991 | * Real SSLv2 is not supported, and is rejected above. | |
992 | */ | |
ec30e856 | 993 | unsigned int cipher_len, session_id_len, challenge_len; |
b3e2272c | 994 | PACKET challenge; |
0f113f3e | 995 | |
73999b62 MC |
996 | if (!PACKET_get_net_2(pkt, &cipher_len) |
997 | || !PACKET_get_net_2(pkt, &session_id_len) | |
998 | || !PACKET_get_net_2(pkt, &challenge_len)) { | |
e27f234a MC |
999 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
1000 | SSL_R_RECORD_LENGTH_MISMATCH); | |
6c3cca57 AE |
1001 | al = SSL_AD_DECODE_ERROR; |
1002 | goto f_err; | |
5e9f0eeb | 1003 | } |
0f113f3e | 1004 | |
293b5ca4 AG |
1005 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
1006 | al = SSL_AD_DECODE_ERROR; | |
1007 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); | |
1008 | goto f_err; | |
1009 | } | |
1010 | ||
73999b62 MC |
1011 | if (!PACKET_get_sub_packet(pkt, &cipher_suites, cipher_len) |
1012 | || !PACKET_get_sub_packet(pkt, &session_id, session_id_len) | |
1013 | || !PACKET_get_sub_packet(pkt, &challenge, challenge_len) | |
b3e2272c | 1014 | /* No extensions. */ |
73999b62 | 1015 | || PACKET_remaining(pkt) != 0) { |
f0659bdb MC |
1016 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
1017 | SSL_R_RECORD_LENGTH_MISMATCH); | |
9ceb2426 MC |
1018 | al = SSL_AD_DECODE_ERROR; |
1019 | goto f_err; | |
1020 | } | |
1021 | ||
cb21df32 | 1022 | /* Load the client random and compression list. */ |
b3e2272c EK |
1023 | challenge_len = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE : |
1024 | challenge_len; | |
32ec4153 | 1025 | memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE); |
b3e2272c EK |
1026 | if (!PACKET_copy_bytes(&challenge, |
1027 | s->s3->client_random + SSL3_RANDOM_SIZE - | |
cb21df32 DB |
1028 | challenge_len, challenge_len) |
1029 | /* Advertise only null compression. */ | |
1030 | || !PACKET_buf_init(&compression, &null_compression, 1)) { | |
f0659bdb | 1031 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
b3e2272c | 1032 | al = SSL_AD_INTERNAL_ERROR; |
9ceb2426 MC |
1033 | goto f_err; |
1034 | } | |
b3e2272c | 1035 | |
b3e2272c | 1036 | PACKET_null_init(&extensions); |
0f113f3e | 1037 | } else { |
b3e2272c | 1038 | /* Regular ClientHello. */ |
73999b62 MC |
1039 | if (!PACKET_copy_bytes(pkt, s->s3->client_random, SSL3_RANDOM_SIZE) |
1040 | || !PACKET_get_length_prefixed_1(pkt, &session_id)) { | |
9ceb2426 | 1041 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1042 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); |
9ceb2426 MC |
1043 | goto f_err; |
1044 | } | |
32ec4153 | 1045 | |
293b5ca4 AG |
1046 | if (PACKET_remaining(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
1047 | al = SSL_AD_DECODE_ERROR; | |
1048 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); | |
1049 | goto f_err; | |
1050 | } | |
1051 | ||
b3e2272c | 1052 | if (SSL_IS_DTLS(s)) { |
73999b62 | 1053 | if (!PACKET_get_length_prefixed_1(pkt, &cookie)) { |
32ec4153 | 1054 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1055 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); |
32ec4153 MC |
1056 | goto f_err; |
1057 | } | |
b3e2272c EK |
1058 | /* |
1059 | * If we require cookies and this ClientHello doesn't contain one, | |
1060 | * just return since we do not want to allocate any memory yet. | |
1061 | * So check cookie length... | |
1062 | */ | |
1063 | if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { | |
1064 | if (PACKET_remaining(&cookie) == 0) | |
32ec4153 | 1065 | return 1; |
b3e2272c | 1066 | } |
5e9f0eeb | 1067 | } |
0f113f3e | 1068 | |
73999b62 MC |
1069 | if (!PACKET_get_length_prefixed_2(pkt, &cipher_suites) |
1070 | || !PACKET_get_length_prefixed_1(pkt, &compression)) { | |
b3e2272c | 1071 | al = SSL_AD_DECODE_ERROR; |
f0659bdb | 1072 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH); |
b3e2272c EK |
1073 | goto f_err; |
1074 | } | |
1075 | /* Could be empty. */ | |
73999b62 | 1076 | extensions = *pkt; |
b3e2272c EK |
1077 | } |
1078 | ||
1ed65871 DB |
1079 | if (SSL_IS_DTLS(s)) { |
1080 | /* Empty cookie was already handled above by returning early. */ | |
1081 | if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { | |
1082 | if (s->ctx->app_verify_cookie_cb != NULL) { | |
1083 | if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookie), | |
1084 | PACKET_remaining(&cookie)) == 0) { | |
1085 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1086 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, | |
1087 | SSL_R_COOKIE_MISMATCH); | |
1088 | goto f_err; | |
1089 | /* else cookie verification succeeded */ | |
1090 | } | |
1091 | /* default verification */ | |
1092 | } else if (!PACKET_equal(&cookie, s->d1->cookie, | |
1093 | s->d1->cookie_len)) { | |
1094 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1095 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); | |
1096 | goto f_err; | |
1097 | } | |
1098 | s->d1->cookie_verified = 1; | |
1099 | } | |
1100 | if (s->method->version == DTLS_ANY_VERSION) { | |
1101 | protverr = ssl_choose_server_version(s); | |
1102 | if (protverr != 0) { | |
1103 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr); | |
1104 | s->version = s->client_version; | |
1105 | al = SSL_AD_PROTOCOL_VERSION; | |
1106 | goto f_err; | |
1107 | } | |
1108 | } | |
1109 | } | |
1110 | ||
b3e2272c EK |
1111 | s->hit = 0; |
1112 | ||
1113 | /* | |
1114 | * We don't allow resumption in a backwards compatible ClientHello. | |
1115 | * TODO(openssl-team): in TLS1.1+, session_id MUST be empty. | |
1116 | * | |
1117 | * Versions before 0.9.7 always allow clients to resume sessions in | |
1118 | * renegotiation. 0.9.7 and later allow this by default, but optionally | |
1119 | * ignore resumption requests with flag | |
1120 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather | |
1121 | * than a change to default behavior so that applications relying on | |
1122 | * this for security won't even compile against older library versions). | |
1123 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to | |
1124 | * request renegotiation but not a new session (s->new_session remains | |
1125 | * unset): for servers, this essentially just means that the | |
1126 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be | |
1127 | * ignored. | |
1128 | */ | |
1129 | if (is_v2_record || | |
1130 | (s->new_session && | |
1131 | (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { | |
1132 | if (!ssl_get_new_session(s, 1)) | |
1133 | goto err; | |
1134 | } else { | |
1135 | i = ssl_get_prev_session(s, &extensions, &session_id); | |
0f113f3e | 1136 | /* |
b3e2272c EK |
1137 | * Only resume if the session's version matches the negotiated |
1138 | * version. | |
1139 | * RFC 5246 does not provide much useful advice on resumption | |
1140 | * with a different protocol version. It doesn't forbid it but | |
1141 | * the sanity of such behaviour would be questionable. | |
1142 | * In practice, clients do not accept a version mismatch and | |
1143 | * will abort the handshake with an error. | |
0f113f3e | 1144 | */ |
b3e2272c EK |
1145 | if (i == 1 && s->version == s->session->ssl_version) { |
1146 | /* previous session */ | |
1147 | s->hit = 1; | |
1148 | } else if (i == -1) { | |
1149 | goto err; | |
32ec4153 | 1150 | } else { |
b3e2272c EK |
1151 | /* i == 0 */ |
1152 | if (!ssl_get_new_session(s, 1)) | |
32ec4153 | 1153 | goto err; |
0f113f3e | 1154 | } |
b3e2272c | 1155 | } |
0f113f3e | 1156 | |
38a3cbfb EK |
1157 | if (ssl_bytes_to_cipher_list(s, &cipher_suites, &(ciphers), |
1158 | is_v2_record, &al) == NULL) { | |
b3e2272c EK |
1159 | goto f_err; |
1160 | } | |
5e9f0eeb | 1161 | |
b3e2272c EK |
1162 | /* If it is a hit, check that the cipher is in the list */ |
1163 | if (s->hit) { | |
1164 | j = 0; | |
1165 | id = s->session->cipher->id; | |
d02b48c6 | 1166 | |
413c4f45 | 1167 | #ifdef CIPHER_DEBUG |
b3e2272c EK |
1168 | fprintf(stderr, "client sent %d ciphers\n", |
1169 | sk_SSL_CIPHER_num(ciphers)); | |
413c4f45 | 1170 | #endif |
b3e2272c EK |
1171 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
1172 | c = sk_SSL_CIPHER_value(ciphers, i); | |
413c4f45 | 1173 | #ifdef CIPHER_DEBUG |
b3e2272c EK |
1174 | fprintf(stderr, "client [%2d of %2d]:%s\n", |
1175 | i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c)); | |
88f2a4cf | 1176 | #endif |
b3e2272c EK |
1177 | if (c->id == id) { |
1178 | j = 1; | |
1179 | break; | |
32ec4153 | 1180 | } |
0f113f3e | 1181 | } |
b3e2272c | 1182 | if (j == 0) { |
ec30e856 | 1183 | /* |
b3e2272c EK |
1184 | * we need to have the cipher in the cipher list if we are asked |
1185 | * to reuse it | |
ec30e856 | 1186 | */ |
b3e2272c | 1187 | al = SSL_AD_ILLEGAL_PARAMETER; |
f0659bdb | 1188 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
b3e2272c | 1189 | SSL_R_REQUIRED_CIPHER_MISSING); |
32ec4153 MC |
1190 | goto f_err; |
1191 | } | |
b3e2272c | 1192 | } |
9ceb2426 | 1193 | |
b3e2272c EK |
1194 | complen = PACKET_remaining(&compression); |
1195 | for (j = 0; j < complen; j++) { | |
1196 | if (PACKET_data(&compression)[j] == 0) | |
1197 | break; | |
0f113f3e | 1198 | } |
32ec4153 | 1199 | |
b3e2272c EK |
1200 | if (j >= complen) { |
1201 | /* no compress */ | |
1202 | al = SSL_AD_DECODE_ERROR; | |
f0659bdb | 1203 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED); |
b3e2272c EK |
1204 | goto f_err; |
1205 | } | |
f100b031 | 1206 | |
0f113f3e MC |
1207 | /* TLS extensions */ |
1208 | if (s->version >= SSL3_VERSION) { | |
b3e2272c | 1209 | if (!ssl_parse_clienthello_tlsext(s, &extensions)) { |
f0659bdb | 1210 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT); |
0f113f3e MC |
1211 | goto err; |
1212 | } | |
1213 | } | |
1214 | ||
1215 | /* | |
1216 | * Check if we want to use external pre-shared secret for this handshake | |
1217 | * for not reused session only. We need to generate server_random before | |
1218 | * calling tls_session_secret_cb in order to allow SessionTicket | |
1219 | * processing to use it in key derivation. | |
1220 | */ | |
1221 | { | |
1222 | unsigned char *pos; | |
1223 | pos = s->s3->server_random; | |
1224 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) { | |
1225 | goto f_err; | |
1226 | } | |
1227 | } | |
1228 | ||
1229 | if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) { | |
4a640fb6 | 1230 | const SSL_CIPHER *pref_cipher = NULL; |
0f113f3e MC |
1231 | |
1232 | s->session->master_key_length = sizeof(s->session->master_key); | |
1233 | if (s->tls_session_secret_cb(s, s->session->master_key, | |
1234 | &s->session->master_key_length, ciphers, | |
1235 | &pref_cipher, | |
1236 | s->tls_session_secret_cb_arg)) { | |
1237 | s->hit = 1; | |
1238 | s->session->ciphers = ciphers; | |
1239 | s->session->verify_result = X509_V_OK; | |
1240 | ||
1241 | ciphers = NULL; | |
1242 | ||
1243 | /* check if some cipher was preferred by call back */ | |
1244 | pref_cipher = | |
1245 | pref_cipher ? pref_cipher : ssl3_choose_cipher(s, | |
1246 | s-> | |
1247 | session->ciphers, | |
1248 | SSL_get_ciphers | |
1249 | (s)); | |
1250 | if (pref_cipher == NULL) { | |
1251 | al = SSL_AD_HANDSHAKE_FAILURE; | |
e27f234a | 1252 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER); |
0f113f3e MC |
1253 | goto f_err; |
1254 | } | |
1255 | ||
1256 | s->session->cipher = pref_cipher; | |
25aaa98a | 1257 | sk_SSL_CIPHER_free(s->cipher_list); |
0f113f3e | 1258 | s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); |
25aaa98a | 1259 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
0f113f3e MC |
1260 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); |
1261 | } | |
1262 | } | |
58ece833 | 1263 | |
0f113f3e MC |
1264 | /* |
1265 | * Worst case, we will use the NULL compression, but if we have other | |
b2ce0337 | 1266 | * options, we will now look for them. We have complen-1 compression |
0f113f3e MC |
1267 | * algorithms from the client, starting at q. |
1268 | */ | |
1269 | s->s3->tmp.new_compression = NULL; | |
09b6c2ef | 1270 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
1271 | /* This only happens if we have a cache hit */ |
1272 | if (s->session->compress_meth != 0) { | |
1273 | int m, comp_id = s->session->compress_meth; | |
9ceb2426 | 1274 | unsigned int k; |
0f113f3e MC |
1275 | /* Perform sanity checks on resumed compression algorithm */ |
1276 | /* Can't disable compression */ | |
1277 | if (!ssl_allow_compression(s)) { | |
e27f234a | 1278 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
0f113f3e MC |
1279 | SSL_R_INCONSISTENT_COMPRESSION); |
1280 | goto f_err; | |
1281 | } | |
1282 | /* Look for resumed compression method */ | |
1283 | for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) { | |
1284 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, m); | |
1285 | if (comp_id == comp->id) { | |
1286 | s->s3->tmp.new_compression = comp; | |
1287 | break; | |
1288 | } | |
1289 | } | |
1290 | if (s->s3->tmp.new_compression == NULL) { | |
e27f234a | 1291 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
0f113f3e MC |
1292 | SSL_R_INVALID_COMPRESSION_ALGORITHM); |
1293 | goto f_err; | |
1294 | } | |
1295 | /* Look for resumed method in compression list */ | |
9ceb2426 | 1296 | for (k = 0; k < complen; k++) { |
ec30e856 | 1297 | if (PACKET_data(&compression)[k] == comp_id) |
0f113f3e MC |
1298 | break; |
1299 | } | |
9ceb2426 | 1300 | if (k >= complen) { |
0f113f3e | 1301 | al = SSL_AD_ILLEGAL_PARAMETER; |
e27f234a | 1302 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, |
8fdc99cb | 1303 | SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); |
0f113f3e MC |
1304 | goto f_err; |
1305 | } | |
1306 | } else if (s->hit) | |
1307 | comp = NULL; | |
1308 | else if (ssl_allow_compression(s) && s->ctx->comp_methods) { | |
df6741c9 | 1309 | /* See if we have a match */ |
9ceb2426 MC |
1310 | int m, nn, v, done = 0; |
1311 | unsigned int o; | |
0f113f3e MC |
1312 | |
1313 | nn = sk_SSL_COMP_num(s->ctx->comp_methods); | |
1314 | for (m = 0; m < nn; m++) { | |
1315 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, m); | |
1316 | v = comp->id; | |
b2ce0337 | 1317 | for (o = 0; o < complen; o++) { |
ec30e856 | 1318 | if (v == PACKET_data(&compression)[o]) { |
0f113f3e MC |
1319 | done = 1; |
1320 | break; | |
1321 | } | |
1322 | } | |
1323 | if (done) | |
1324 | break; | |
1325 | } | |
1326 | if (done) | |
1327 | s->s3->tmp.new_compression = comp; | |
1328 | else | |
1329 | comp = NULL; | |
1330 | } | |
e6f418bc | 1331 | #else |
0f113f3e MC |
1332 | /* |
1333 | * If compression is disabled we'd better not try to resume a session | |
1334 | * using compression. | |
1335 | */ | |
1336 | if (s->session->compress_meth != 0) { | |
e27f234a | 1337 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
0f113f3e MC |
1338 | goto f_err; |
1339 | } | |
09b6c2ef | 1340 | #endif |
413c4f45 | 1341 | |
0f113f3e MC |
1342 | /* |
1343 | * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher | |
1344 | */ | |
d02b48c6 | 1345 | |
0f113f3e | 1346 | if (!s->hit) { |
09b6c2ef | 1347 | #ifdef OPENSSL_NO_COMP |
0f113f3e | 1348 | s->session->compress_meth = 0; |
09b6c2ef | 1349 | #else |
0f113f3e | 1350 | s->session->compress_meth = (comp == NULL) ? 0 : comp->id; |
09b6c2ef | 1351 | #endif |
25aaa98a | 1352 | sk_SSL_CIPHER_free(s->session->ciphers); |
0f113f3e MC |
1353 | s->session->ciphers = ciphers; |
1354 | if (ciphers == NULL) { | |
3ae91cfb | 1355 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 1356 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
1357 | goto f_err; |
1358 | } | |
1359 | ciphers = NULL; | |
1360 | if (!tls1_set_server_sigalgs(s)) { | |
e27f234a | 1361 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); |
0f113f3e MC |
1362 | goto err; |
1363 | } | |
e27f234a MC |
1364 | } |
1365 | ||
1366 | sk_SSL_CIPHER_free(ciphers); | |
1367 | return MSG_PROCESS_CONTINUE_PROCESSING; | |
1368 | f_err: | |
1369 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1370 | err: | |
fe3a3291 | 1371 | ossl_statem_set_error(s); |
e27f234a MC |
1372 | |
1373 | sk_SSL_CIPHER_free(ciphers); | |
1374 | return MSG_PROCESS_ERROR; | |
1375 | ||
1376 | } | |
1377 | ||
be3583fa | 1378 | WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst) |
e27f234a | 1379 | { |
d13dd4be | 1380 | int al = SSL_AD_HANDSHAKE_FAILURE; |
4a640fb6 | 1381 | const SSL_CIPHER *cipher; |
e27f234a MC |
1382 | |
1383 | if (wst == WORK_MORE_A) { | |
1384 | if (!s->hit) { | |
1385 | /* Let cert callback update server certificates if required */ | |
1386 | if (s->cert->cert_cb) { | |
1387 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
1388 | if (rv == 0) { | |
1389 | al = SSL_AD_INTERNAL_ERROR; | |
1390 | SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_CERT_CB_ERROR); | |
1391 | goto f_err; | |
1392 | } | |
1393 | if (rv < 0) { | |
1394 | s->rwstate = SSL_X509_LOOKUP; | |
1395 | return WORK_MORE_A; | |
1396 | } | |
1397 | s->rwstate = SSL_NOTHING; | |
0f113f3e | 1398 | } |
e27f234a MC |
1399 | cipher = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s)); |
1400 | ||
1401 | if (cipher == NULL) { | |
e27f234a MC |
1402 | SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER); |
1403 | goto f_err; | |
0f113f3e | 1404 | } |
e27f234a MC |
1405 | s->s3->tmp.new_cipher = cipher; |
1406 | /* check whether we should disable session resumption */ | |
1407 | if (s->not_resumable_session_cb != NULL) | |
1408 | s->session->not_resumable = s->not_resumable_session_cb(s, | |
1409 | ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0)); | |
1410 | if (s->session->not_resumable) | |
1411 | /* do not send a session ticket */ | |
1412 | s->tlsext_ticket_expected = 0; | |
1413 | } else { | |
1414 | /* Session-id reuse */ | |
1415 | s->s3->tmp.new_cipher = s->session->cipher; | |
0f113f3e | 1416 | } |
0f113f3e | 1417 | |
28f4580c | 1418 | if (!(s->verify_mode & SSL_VERIFY_PEER)) { |
d13dd4be MC |
1419 | if (!ssl3_digest_cached_records(s, 0)) { |
1420 | al = SSL_AD_INTERNAL_ERROR; | |
e27f234a | 1421 | goto f_err; |
d13dd4be | 1422 | } |
0f113f3e | 1423 | } |
0f113f3e | 1424 | |
e27f234a MC |
1425 | /*- |
1426 | * we now have the following setup. | |
1427 | * client_random | |
1428 | * cipher_list - our prefered list of ciphers | |
1429 | * ciphers - the clients prefered list of ciphers | |
1430 | * compression - basically ignored right now | |
1431 | * ssl version is set - sslv3 | |
1432 | * s->session - The ssl session has been setup. | |
1433 | * s->hit - session reuse flag | |
1434 | * s->s3->tmp.new_cipher- the new cipher to use. | |
1435 | */ | |
0f113f3e | 1436 | |
e27f234a MC |
1437 | /* Handles TLS extensions that we couldn't check earlier */ |
1438 | if (s->version >= SSL3_VERSION) { | |
1439 | if (ssl_check_clienthello_tlsext_late(s) <= 0) { | |
d13dd4be MC |
1440 | SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, |
1441 | SSL_R_CLIENTHELLO_TLSEXT); | |
e27f234a MC |
1442 | goto f_err; |
1443 | } | |
1444 | } | |
0f113f3e | 1445 | |
e27f234a MC |
1446 | wst = WORK_MORE_B; |
1447 | } | |
1448 | #ifndef OPENSSL_NO_SRP | |
1449 | if (wst == WORK_MORE_B) { | |
1450 | int ret; | |
1451 | if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) { | |
1452 | /* | |
1453 | * callback indicates further work to be done | |
1454 | */ | |
1455 | s->rwstate = SSL_X509_LOOKUP; | |
1456 | return WORK_MORE_B; | |
1457 | } | |
1458 | if (ret != SSL_ERROR_NONE) { | |
1459 | /* | |
1460 | * This is not really an error but the only means to for | |
1461 | * a client to detect whether srp is supported. | |
1462 | */ | |
1463 | if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY) | |
1464 | SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, | |
1465 | SSL_R_CLIENTHELLO_TLSEXT); | |
1466 | goto f_err; | |
0f113f3e MC |
1467 | } |
1468 | } | |
e27f234a MC |
1469 | #endif |
1470 | s->renegotiate = 2; | |
0f113f3e | 1471 | |
e27f234a | 1472 | return WORK_FINISHED_STOP; |
0f113f3e | 1473 | f_err: |
e27f234a | 1474 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
fe3a3291 | 1475 | ossl_statem_set_error(s); |
e27f234a MC |
1476 | return WORK_ERROR; |
1477 | } | |
1478 | ||
e27f234a | 1479 | int tls_construct_server_hello(SSL *s) |
0f113f3e MC |
1480 | { |
1481 | unsigned char *buf; | |
1482 | unsigned char *p, *d; | |
1483 | int i, sl; | |
1484 | int al = 0; | |
1485 | unsigned long l; | |
1486 | ||
e27f234a | 1487 | buf = (unsigned char *)s->init_buf->data; |
e481f9b9 | 1488 | |
e27f234a MC |
1489 | /* Do the message type and length last */ |
1490 | d = p = ssl_handshake_start(s); | |
0f113f3e | 1491 | |
e27f234a MC |
1492 | *(p++) = s->version >> 8; |
1493 | *(p++) = s->version & 0xff; | |
0f113f3e | 1494 | |
e27f234a MC |
1495 | /* |
1496 | * Random stuff. Filling of the server_random takes place in | |
1497 | * tls_process_client_hello() | |
1498 | */ | |
1499 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); | |
1500 | p += SSL3_RANDOM_SIZE; | |
0f113f3e | 1501 | |
e27f234a MC |
1502 | /*- |
1503 | * There are several cases for the session ID to send | |
1504 | * back in the server hello: | |
1505 | * - For session reuse from the session cache, | |
1506 | * we send back the old session ID. | |
1507 | * - If stateless session reuse (using a session ticket) | |
1508 | * is successful, we send back the client's "session ID" | |
1509 | * (which doesn't actually identify the session). | |
1510 | * - If it is a new session, we send back the new | |
1511 | * session ID. | |
1512 | * - However, if we want the new session to be single-use, | |
1513 | * we send back a 0-length session ID. | |
1514 | * s->hit is non-zero in either case of session reuse, | |
1515 | * so the following won't overwrite an ID that we're supposed | |
1516 | * to send back. | |
1517 | */ | |
1518 | if (s->session->not_resumable || | |
1519 | (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) | |
1520 | && !s->hit)) | |
1521 | s->session->session_id_length = 0; | |
1522 | ||
1523 | sl = s->session->session_id_length; | |
1524 | if (sl > (int)sizeof(s->session->session_id)) { | |
1525 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 1526 | ossl_statem_set_error(s); |
e27f234a MC |
1527 | return 0; |
1528 | } | |
1529 | *(p++) = sl; | |
1530 | memcpy(p, s->session->session_id, sl); | |
1531 | p += sl; | |
0f113f3e | 1532 | |
e27f234a MC |
1533 | /* put the cipher */ |
1534 | i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p); | |
1535 | p += i; | |
0f113f3e | 1536 | |
e27f234a | 1537 | /* put the compression method */ |
09b6c2ef | 1538 | #ifdef OPENSSL_NO_COMP |
e27f234a | 1539 | *(p++) = 0; |
09b6c2ef | 1540 | #else |
e27f234a MC |
1541 | if (s->s3->tmp.new_compression == NULL) |
1542 | *(p++) = 0; | |
1543 | else | |
1544 | *(p++) = s->s3->tmp.new_compression->id; | |
09b6c2ef | 1545 | #endif |
e481f9b9 | 1546 | |
e27f234a MC |
1547 | if (ssl_prepare_serverhello_tlsext(s) <= 0) { |
1548 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT); | |
fe3a3291 | 1549 | ossl_statem_set_error(s); |
e27f234a MC |
1550 | return 0; |
1551 | } | |
1552 | if ((p = | |
1553 | ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, | |
1554 | &al)) == NULL) { | |
1555 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1556 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 1557 | ossl_statem_set_error(s); |
e27f234a MC |
1558 | return 0; |
1559 | } | |
e481f9b9 | 1560 | |
e27f234a MC |
1561 | /* do the header */ |
1562 | l = (p - d); | |
1563 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) { | |
1564 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 1565 | ossl_statem_set_error(s); |
e27f234a | 1566 | return 0; |
0f113f3e | 1567 | } |
d02b48c6 | 1568 | |
e27f234a | 1569 | return 1; |
0f113f3e | 1570 | } |
d02b48c6 | 1571 | |
e27f234a MC |
1572 | int tls_construct_server_done(SSL *s) |
1573 | { | |
1574 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) { | |
1575 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 1576 | ossl_statem_set_error(s); |
e27f234a MC |
1577 | return 0; |
1578 | } | |
1579 | ||
1580 | if (!s->s3->tmp.cert_request) { | |
1581 | if (!ssl3_digest_cached_records(s, 0)) { | |
fe3a3291 | 1582 | ossl_statem_set_error(s); |
e27f234a MC |
1583 | } |
1584 | } | |
1585 | ||
1586 | return 1; | |
1587 | } | |
1588 | ||
e27f234a | 1589 | int tls_construct_server_key_exchange(SSL *s) |
0f113f3e | 1590 | { |
bc36ee62 | 1591 | #ifndef OPENSSL_NO_DH |
e2b420fd | 1592 | EVP_PKEY *pkdh = NULL; |
ea262260 | 1593 | #endif |
10bf4fc2 | 1594 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1595 | unsigned char *encodedPoint = NULL; |
1596 | int encodedlen = 0; | |
1597 | int curve_id = 0; | |
d02b48c6 | 1598 | #endif |
0f113f3e MC |
1599 | EVP_PKEY *pkey; |
1600 | const EVP_MD *md = NULL; | |
1601 | unsigned char *p, *d; | |
1602 | int al, i; | |
1603 | unsigned long type; | |
1604 | int n; | |
0f113f3e MC |
1605 | BIGNUM *r[4]; |
1606 | int nr[4], kn; | |
1607 | BUF_MEM *buf; | |
bfb0641f | 1608 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
0f113f3e | 1609 | |
6e59a892 RL |
1610 | if (md_ctx == NULL) { |
1611 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1612 | al = SSL_AD_INTERNAL_ERROR; | |
1613 | goto f_err; | |
1614 | } | |
0f113f3e | 1615 | |
e27f234a | 1616 | type = s->s3->tmp.new_cipher->algorithm_mkey; |
e27f234a MC |
1617 | |
1618 | buf = s->init_buf; | |
0f113f3e | 1619 | |
e27f234a MC |
1620 | r[0] = r[1] = r[2] = r[3] = NULL; |
1621 | n = 0; | |
85269210 | 1622 | #ifndef OPENSSL_NO_PSK |
e27f234a MC |
1623 | if (type & SSL_PSK) { |
1624 | /* | |
1625 | * reserve size for record length and PSK identity hint | |
1626 | */ | |
1627 | n += 2; | |
1628 | if (s->cert->psk_identity_hint) | |
1629 | n += strlen(s->cert->psk_identity_hint); | |
1630 | } | |
1631 | /* Plain PSK or RSAPSK nothing to do */ | |
1632 | if (type & (SSL_kPSK | SSL_kRSAPSK)) { | |
1633 | } else | |
85269210 | 1634 | #endif /* !OPENSSL_NO_PSK */ |
bc36ee62 | 1635 | #ifndef OPENSSL_NO_DH |
e27f234a | 1636 | if (type & (SSL_kDHE | SSL_kDHEPSK)) { |
94d61512 BL |
1637 | CERT *cert = s->cert; |
1638 | ||
e2b420fd DSH |
1639 | EVP_PKEY *pkdhp = NULL; |
1640 | DH *dh; | |
1641 | ||
e27f234a | 1642 | if (s->cert->dh_tmp_auto) { |
e2b420fd DSH |
1643 | DH *dhp = ssl_get_auto_dh(s); |
1644 | pkdh = EVP_PKEY_new(); | |
1645 | if (pkdh == NULL || dhp == NULL) { | |
1646 | DH_free(dhp); | |
e27f234a MC |
1647 | al = SSL_AD_INTERNAL_ERROR; |
1648 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
0f113f3e | 1649 | ERR_R_INTERNAL_ERROR); |
e27f234a | 1650 | goto f_err; |
0f113f3e | 1651 | } |
e2b420fd DSH |
1652 | EVP_PKEY_assign_DH(pkdh, dhp); |
1653 | pkdhp = pkdh; | |
1654 | } else { | |
1655 | pkdhp = cert->dh_tmp; | |
1656 | } | |
1657 | if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) { | |
1658 | DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024); | |
1659 | pkdh = ssl_dh_to_pkey(dhp); | |
1660 | if (pkdh == NULL) { | |
1661 | al = SSL_AD_INTERNAL_ERROR; | |
1662 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1663 | ERR_R_INTERNAL_ERROR); | |
1664 | goto f_err; | |
1665 | } | |
1666 | pkdhp = pkdh; | |
1667 | } | |
1668 | if (pkdhp == NULL) { | |
e27f234a MC |
1669 | al = SSL_AD_HANDSHAKE_FAILURE; |
1670 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1671 | SSL_R_MISSING_TMP_DH_KEY); | |
1672 | goto f_err; | |
1673 | } | |
1674 | if (!ssl_security(s, SSL_SECOP_TMP_DH, | |
e2b420fd | 1675 | EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) { |
e27f234a MC |
1676 | al = SSL_AD_HANDSHAKE_FAILURE; |
1677 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1678 | SSL_R_DH_KEY_TOO_SMALL); | |
1679 | goto f_err; | |
1680 | } | |
e2b420fd | 1681 | if (s->s3->tmp.pkey != NULL) { |
e27f234a MC |
1682 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, |
1683 | ERR_R_INTERNAL_ERROR); | |
1684 | goto err; | |
1685 | } | |
0f113f3e | 1686 | |
e2b420fd | 1687 | s->s3->tmp.pkey = ssl_generate_pkey(pkdhp, NID_undef); |
e27f234a | 1688 | |
e2b420fd DSH |
1689 | if (s->s3->tmp.pkey == NULL) { |
1690 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
ffaef3f1 | 1691 | goto err; |
e27f234a | 1692 | } |
e2b420fd DSH |
1693 | |
1694 | dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey); | |
1695 | ||
1696 | EVP_PKEY_free(pkdh); | |
1697 | pkdh = NULL; | |
1698 | ||
0aeddcfa MC |
1699 | DH_get0_pqg(dh, &r[0], NULL, &r[1]); |
1700 | DH_get0_key(dh, &r[2], NULL); | |
e27f234a | 1701 | } else |
d02b48c6 | 1702 | #endif |
10bf4fc2 | 1703 | #ifndef OPENSSL_NO_EC |
e27f234a | 1704 | if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
57be4444 | 1705 | int nid; |
e27f234a | 1706 | |
880d9d86 | 1707 | if (s->s3->tmp.pkey != NULL) { |
e27f234a MC |
1708 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, |
1709 | ERR_R_INTERNAL_ERROR); | |
1710 | goto err; | |
1711 | } | |
1712 | ||
57be4444 DSH |
1713 | /* Get NID of appropriate shared curve */ |
1714 | nid = tls1_shared_curve(s, -2); | |
1715 | curve_id = tls1_ec_nid2curve_id(nid); | |
1716 | if (curve_id == 0) { | |
e27f234a MC |
1717 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, |
1718 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | |
1719 | goto err; | |
1720 | } | |
880d9d86 DSH |
1721 | s->s3->tmp.pkey = ssl_generate_pkey(NULL, nid); |
1722 | /* Generate a new key for this curve */ | |
1723 | if (s->s3->tmp.pkey == NULL) { | |
57be4444 | 1724 | al = SSL_AD_INTERNAL_ERROR; |
880d9d86 | 1725 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB); |
57be4444 DSH |
1726 | goto f_err; |
1727 | } | |
1728 | ||
880d9d86 DSH |
1729 | /* Encode the public key. */ |
1730 | encodedlen = EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(s->s3->tmp.pkey), | |
1731 | POINT_CONVERSION_UNCOMPRESSED, | |
cae41364 | 1732 | &encodedPoint, NULL); |
0f113f3e | 1733 | |
e27f234a | 1734 | if (encodedlen == 0) { |
cae41364 | 1735 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB); |
e27f234a MC |
1736 | goto err; |
1737 | } | |
0f113f3e | 1738 | |
e27f234a | 1739 | /* |
57be4444 DSH |
1740 | * We only support named (not generic) curves in ECDH ephemeral key |
1741 | * exchanges. In this situation, we need four additional bytes to | |
1742 | * encode the entire ServerECDHParams structure. | |
e27f234a MC |
1743 | */ |
1744 | n += 4 + encodedlen; | |
0f113f3e | 1745 | |
e27f234a MC |
1746 | /* |
1747 | * We'll generate the serverKeyExchange message explicitly so we | |
1748 | * can set these to NULLs | |
1749 | */ | |
1750 | r[0] = NULL; | |
1751 | r[1] = NULL; | |
1752 | r[2] = NULL; | |
1753 | r[3] = NULL; | |
1754 | } else | |
10bf4fc2 | 1755 | #endif /* !OPENSSL_NO_EC */ |
edc032b5 | 1756 | #ifndef OPENSSL_NO_SRP |
e27f234a MC |
1757 | if (type & SSL_kSRP) { |
1758 | if ((s->srp_ctx.N == NULL) || | |
1759 | (s->srp_ctx.g == NULL) || | |
1760 | (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { | |
1761 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1762 | SSL_R_MISSING_SRP_PARAM); | |
1763 | goto err; | |
0f113f3e | 1764 | } |
e27f234a MC |
1765 | r[0] = s->srp_ctx.N; |
1766 | r[1] = s->srp_ctx.g; | |
1767 | r[2] = s->srp_ctx.s; | |
1768 | r[3] = s->srp_ctx.B; | |
1769 | } else | |
1770 | #endif | |
1771 | { | |
1772 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1773 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1774 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
1775 | goto f_err; | |
1776 | } | |
1777 | for (i = 0; i < 4 && r[i] != NULL; i++) { | |
1778 | nr[i] = BN_num_bytes(r[i]); | |
edc032b5 | 1779 | #ifndef OPENSSL_NO_SRP |
e27f234a MC |
1780 | if ((i == 2) && (type & SSL_kSRP)) |
1781 | n += 1 + nr[i]; | |
1782 | else | |
edc032b5 | 1783 | #endif |
e27f234a MC |
1784 | n += 2 + nr[i]; |
1785 | } | |
0f113f3e | 1786 | |
e27f234a MC |
1787 | if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL|SSL_aSRP)) |
1788 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) { | |
1789 | if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md)) | |
1790 | == NULL) { | |
1791 | al = SSL_AD_DECODE_ERROR; | |
1792 | goto f_err; | |
0f113f3e | 1793 | } |
e27f234a MC |
1794 | kn = EVP_PKEY_size(pkey); |
1795 | } else { | |
1796 | pkey = NULL; | |
1797 | kn = 0; | |
1798 | } | |
0f113f3e | 1799 | |
e27f234a MC |
1800 | if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) { |
1801 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_BUF); | |
1802 | goto err; | |
1803 | } | |
1804 | d = p = ssl_handshake_start(s); | |
0f113f3e | 1805 | |
85269210 | 1806 | #ifndef OPENSSL_NO_PSK |
e27f234a MC |
1807 | if (type & SSL_PSK) { |
1808 | /* copy PSK identity hint */ | |
1809 | if (s->cert->psk_identity_hint) { | |
1810 | s2n(strlen(s->cert->psk_identity_hint), p); | |
1811 | strncpy((char *)p, s->cert->psk_identity_hint, | |
1812 | strlen(s->cert->psk_identity_hint)); | |
1813 | p += strlen(s->cert->psk_identity_hint); | |
1814 | } else { | |
1815 | s2n(0, p); | |
85269210 | 1816 | } |
e27f234a | 1817 | } |
85269210 DSH |
1818 | #endif |
1819 | ||
e27f234a | 1820 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
edc032b5 | 1821 | #ifndef OPENSSL_NO_SRP |
e27f234a MC |
1822 | if ((i == 2) && (type & SSL_kSRP)) { |
1823 | *p = nr[i]; | |
1824 | p++; | |
1825 | } else | |
edc032b5 | 1826 | #endif |
e27f234a MC |
1827 | s2n(nr[i], p); |
1828 | BN_bn2bin(r[i], p); | |
1829 | p += nr[i]; | |
1830 | } | |
d02b48c6 | 1831 | |
10bf4fc2 | 1832 | #ifndef OPENSSL_NO_EC |
e27f234a MC |
1833 | if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { |
1834 | /* | |
1835 | * XXX: For now, we only support named (not generic) curves. In | |
1836 | * this situation, the serverKeyExchange message has: [1 byte | |
1837 | * CurveType], [2 byte CurveName] [1 byte length of encoded | |
1838 | * point], followed by the actual encoded point itself | |
1839 | */ | |
1840 | *p = NAMED_CURVE_TYPE; | |
1841 | p += 1; | |
1842 | *p = 0; | |
1843 | p += 1; | |
1844 | *p = curve_id; | |
1845 | p += 1; | |
1846 | *p = encodedlen; | |
1847 | p += 1; | |
1848 | memcpy(p, encodedPoint, encodedlen); | |
1849 | OPENSSL_free(encodedPoint); | |
1850 | encodedPoint = NULL; | |
1851 | p += encodedlen; | |
1852 | } | |
ea262260 BM |
1853 | #endif |
1854 | ||
e27f234a MC |
1855 | /* not anonymous */ |
1856 | if (pkey != NULL) { | |
1857 | /* | |
1858 | * n is the length of the params, they start at &(d[4]) and p | |
1859 | * points to the space at the end. | |
1860 | */ | |
e27f234a MC |
1861 | if (md) { |
1862 | /* send signature algorithm */ | |
1863 | if (SSL_USE_SIGALGS(s)) { | |
1864 | if (!tls12_get_sigandhash(p, pkey, md)) { | |
1865 | /* Should never happen */ | |
1866 | al = SSL_AD_INTERNAL_ERROR; | |
1867 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, | |
1868 | ERR_R_INTERNAL_ERROR); | |
1869 | goto f_err; | |
0f113f3e | 1870 | } |
e27f234a MC |
1871 | p += 2; |
1872 | } | |
a2f9200f | 1873 | #ifdef SSL_DEBUG |
e27f234a | 1874 | fprintf(stderr, "Using hash %s\n", EVP_MD_name(md)); |
a2f9200f | 1875 | #endif |
6e59a892 RL |
1876 | if (EVP_SignInit_ex(md_ctx, md, NULL) <= 0 |
1877 | || EVP_SignUpdate(md_ctx, &(s->s3->client_random[0]), | |
5f3d93e4 | 1878 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 | 1879 | || EVP_SignUpdate(md_ctx, &(s->s3->server_random[0]), |
5f3d93e4 | 1880 | SSL3_RANDOM_SIZE) <= 0 |
6e59a892 RL |
1881 | || EVP_SignUpdate(md_ctx, d, n) <= 0 |
1882 | || EVP_SignFinal(md_ctx, &(p[2]), | |
5f3d93e4 | 1883 | (unsigned int *)&i, pkey) <= 0) { |
e27f234a | 1884 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP); |
5f3d93e4 MC |
1885 | al = SSL_AD_INTERNAL_ERROR; |
1886 | goto f_err; | |
0f113f3e | 1887 | } |
e27f234a MC |
1888 | s2n(i, p); |
1889 | n += i + 2; | |
1890 | if (SSL_USE_SIGALGS(s)) | |
1891 | n += 2; | |
1892 | } else { | |
1893 | /* Is this error check actually needed? */ | |
77d514c5 | 1894 | al = SSL_AD_HANDSHAKE_FAILURE; |
e27f234a MC |
1895 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, |
1896 | SSL_R_UNKNOWN_PKEY_TYPE); | |
77d514c5 MC |
1897 | goto f_err; |
1898 | } | |
0f113f3e MC |
1899 | } |
1900 | ||
e27f234a MC |
1901 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) { |
1902 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1903 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
1904 | goto f_err; | |
1905 | } | |
1906 | ||
bfb0641f | 1907 | EVP_MD_CTX_free(md_ctx); |
e27f234a | 1908 | return 1; |
0f113f3e MC |
1909 | f_err: |
1910 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1911 | err: | |
e2b420fd DSH |
1912 | #ifndef OPENSSL_NO_DH |
1913 | EVP_PKEY_free(pkdh); | |
1914 | #endif | |
556efe79 | 1915 | #ifndef OPENSSL_NO_EC |
b548a1f1 | 1916 | OPENSSL_free(encodedPoint); |
ea262260 | 1917 | #endif |
bfb0641f | 1918 | EVP_MD_CTX_free(md_ctx); |
fe3a3291 | 1919 | ossl_statem_set_error(s); |
e27f234a | 1920 | return 0; |
0f113f3e | 1921 | } |
d02b48c6 | 1922 | |
e27f234a | 1923 | int tls_construct_certificate_request(SSL *s) |
0f113f3e MC |
1924 | { |
1925 | unsigned char *p, *d; | |
1926 | int i, j, nl, off, n; | |
1927 | STACK_OF(X509_NAME) *sk = NULL; | |
1928 | X509_NAME *name; | |
1929 | BUF_MEM *buf; | |
1930 | ||
e27f234a | 1931 | buf = s->init_buf; |
0f113f3e | 1932 | |
e27f234a | 1933 | d = p = ssl_handshake_start(s); |
0f113f3e | 1934 | |
e27f234a MC |
1935 | /* get the list of acceptable cert types */ |
1936 | p++; | |
1937 | n = ssl3_get_req_cert_type(s, p); | |
1938 | d[0] = n; | |
1939 | p += n; | |
1940 | n++; | |
0f113f3e | 1941 | |
e27f234a MC |
1942 | if (SSL_USE_SIGALGS(s)) { |
1943 | const unsigned char *psigs; | |
1944 | unsigned char *etmp = p; | |
1945 | nl = tls12_get_psigalgs(s, &psigs); | |
1946 | /* Skip over length for now */ | |
0f113f3e | 1947 | p += 2; |
e27f234a MC |
1948 | nl = tls12_copy_sigalgs(s, p, psigs, nl); |
1949 | /* Now fill in length */ | |
1950 | s2n(nl, etmp); | |
1951 | p += nl; | |
1952 | n += nl + 2; | |
1953 | } | |
0f113f3e | 1954 | |
e27f234a MC |
1955 | off = n; |
1956 | p += 2; | |
1957 | n += 2; | |
1958 | ||
1959 | sk = SSL_get_client_CA_list(s); | |
1960 | nl = 0; | |
1961 | if (sk != NULL) { | |
1962 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
1963 | name = sk_X509_NAME_value(sk, i); | |
1964 | j = i2d_X509_NAME(name, NULL); | |
1965 | if (!BUF_MEM_grow_clean | |
1966 | (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) { | |
1967 | SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, | |
1968 | ERR_R_BUF_LIB); | |
1969 | goto err; | |
0f113f3e | 1970 | } |
e27f234a MC |
1971 | p = ssl_handshake_start(s) + n; |
1972 | s2n(j, p); | |
1973 | i2d_X509_NAME(name, &p); | |
1974 | n += 2 + j; | |
1975 | nl += 2 + j; | |
0f113f3e | 1976 | } |
e27f234a MC |
1977 | } |
1978 | /* else no CA names */ | |
1979 | p = ssl_handshake_start(s) + off; | |
1980 | s2n(nl, p); | |
d02b48c6 | 1981 | |
e27f234a MC |
1982 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) { |
1983 | SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR); | |
1984 | goto err; | |
0f113f3e | 1985 | } |
d02b48c6 | 1986 | |
e27f234a MC |
1987 | s->s3->tmp.cert_request = 1; |
1988 | ||
1989 | return 1; | |
0f113f3e | 1990 | err: |
fe3a3291 | 1991 | ossl_statem_set_error(s); |
e27f234a | 1992 | return 0; |
0f113f3e | 1993 | } |
d02b48c6 | 1994 | |
be3583fa | 1995 | MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt) |
e27f234a MC |
1996 | { |
1997 | int al; | |
0f113f3e | 1998 | unsigned long alg_k; |
bc36ee62 | 1999 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 2000 | RSA *rsa = NULL; |
79df9d62 | 2001 | #endif |
b22d7113 | 2002 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
880d9d86 | 2003 | EVP_PKEY *ckey = NULL; |
ea262260 | 2004 | #endif |
73999b62 | 2005 | PACKET enc_premaster; |
b6981744 | 2006 | unsigned char *rsa_decrypt = NULL; |
ea262260 | 2007 | |
0f113f3e | 2008 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
d02b48c6 | 2009 | |
85269210 DSH |
2010 | #ifndef OPENSSL_NO_PSK |
2011 | /* For PSK parse and retrieve identity, obtain PSK key */ | |
2012 | if (alg_k & SSL_PSK) { | |
2013 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
2014 | size_t psklen; | |
73999b62 | 2015 | PACKET psk_identity; |
efcdbcbe | 2016 | |
73999b62 | 2017 | if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) { |
85269210 | 2018 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2019 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
85269210 DSH |
2020 | goto f_err; |
2021 | } | |
6d41fc80 | 2022 | if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) { |
85269210 | 2023 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2024 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
85269210 DSH |
2025 | SSL_R_DATA_LENGTH_TOO_LONG); |
2026 | goto f_err; | |
2027 | } | |
2028 | if (s->psk_server_callback == NULL) { | |
2029 | al = SSL_AD_INTERNAL_ERROR; | |
e27f234a | 2030 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
85269210 DSH |
2031 | SSL_R_PSK_NO_SERVER_CB); |
2032 | goto f_err; | |
2033 | } | |
2034 | ||
6d41fc80 | 2035 | if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) { |
f0659bdb | 2036 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
85269210 | 2037 | al = SSL_AD_INTERNAL_ERROR; |
efcdbcbe MC |
2038 | goto f_err; |
2039 | } | |
85269210 DSH |
2040 | |
2041 | psklen = s->psk_server_callback(s, s->session->psk_identity, | |
2042 | psk, sizeof(psk)); | |
2043 | ||
2044 | if (psklen > PSK_MAX_PSK_LEN) { | |
2045 | al = SSL_AD_INTERNAL_ERROR; | |
e27f234a | 2046 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
85269210 DSH |
2047 | goto f_err; |
2048 | } else if (psklen == 0) { | |
2049 | /* | |
2050 | * PSK related to the given identity not found | |
2051 | */ | |
e27f234a | 2052 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
85269210 DSH |
2053 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
2054 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; | |
2055 | goto f_err; | |
2056 | } | |
2057 | ||
2058 | OPENSSL_free(s->s3->tmp.psk); | |
7644a9ae | 2059 | s->s3->tmp.psk = OPENSSL_memdup(psk, psklen); |
85269210 DSH |
2060 | OPENSSL_cleanse(psk, psklen); |
2061 | ||
2062 | if (s->s3->tmp.psk == NULL) { | |
2063 | al = SSL_AD_INTERNAL_ERROR; | |
e27f234a | 2064 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
85269210 DSH |
2065 | goto f_err; |
2066 | } | |
2067 | ||
2068 | s->s3->tmp.psklen = psklen; | |
85269210 DSH |
2069 | } |
2070 | if (alg_k & SSL_kPSK) { | |
2071 | /* Identity extracted earlier: should be nothing left */ | |
73999b62 | 2072 | if (PACKET_remaining(pkt) != 0) { |
85269210 | 2073 | al = SSL_AD_HANDSHAKE_FAILURE; |
e27f234a | 2074 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH); |
85269210 DSH |
2075 | goto f_err; |
2076 | } | |
2077 | /* PSK handled by ssl_generate_master_secret */ | |
2078 | if (!ssl_generate_master_secret(s, NULL, 0, 0)) { | |
2079 | al = SSL_AD_INTERNAL_ERROR; | |
e27f234a | 2080 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
85269210 DSH |
2081 | goto f_err; |
2082 | } | |
2083 | } else | |
2084 | #endif | |
bc36ee62 | 2085 | #ifndef OPENSSL_NO_RSA |
85269210 | 2086 | if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
0f113f3e MC |
2087 | unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
2088 | int decrypt_len; | |
2089 | unsigned char decrypt_good, version_good; | |
2090 | size_t j; | |
2091 | ||
2092 | /* FIX THIS UP EAY EAY EAY EAY */ | |
3aeb9348 DSH |
2093 | rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey); |
2094 | if (rsa == NULL) { | |
361a1191 KR |
2095 | al = SSL_AD_HANDSHAKE_FAILURE; |
2096 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, | |
2097 | SSL_R_MISSING_RSA_CERTIFICATE); | |
2098 | goto f_err; | |
0f113f3e MC |
2099 | } |
2100 | ||
20ca916d EK |
2101 | /* SSLv3 and pre-standard DTLS omit the length bytes. */ |
2102 | if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { | |
73999b62 | 2103 | enc_premaster = *pkt; |
20ca916d | 2104 | } else { |
73999b62 MC |
2105 | if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster) |
2106 | || PACKET_remaining(pkt) != 0) { | |
1c9ed1d8 KR |
2107 | al = SSL_AD_DECODE_ERROR; |
2108 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, | |
2109 | SSL_R_LENGTH_MISMATCH); | |
2110 | goto f_err; | |
efcdbcbe | 2111 | } |
0f113f3e MC |
2112 | } |
2113 | ||
2114 | /* | |
20ca916d EK |
2115 | * We want to be sure that the plaintext buffer size makes it safe to |
2116 | * iterate over the entire size of a premaster secret | |
2117 | * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because | |
2118 | * their ciphertext cannot accommodate a premaster secret anyway. | |
0f113f3e | 2119 | */ |
20ca916d EK |
2120 | if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) { |
2121 | al = SSL_AD_INTERNAL_ERROR; | |
f0659bdb | 2122 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
20ca916d | 2123 | RSA_R_KEY_SIZE_TOO_SMALL); |
0f113f3e MC |
2124 | goto f_err; |
2125 | } | |
2126 | ||
20ca916d EK |
2127 | rsa_decrypt = OPENSSL_malloc(RSA_size(rsa)); |
2128 | if (rsa_decrypt == NULL) { | |
efcdbcbe | 2129 | al = SSL_AD_INTERNAL_ERROR; |
f0659bdb | 2130 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
efcdbcbe MC |
2131 | goto f_err; |
2132 | } | |
20ca916d | 2133 | |
0f113f3e MC |
2134 | /* |
2135 | * We must not leak whether a decryption failure occurs because of | |
2136 | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, | |
2137 | * section 7.4.7.1). The code follows that advice of the TLS RFC and | |
2138 | * generates a random premaster secret for the case that the decrypt | |
2139 | * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 | |
2140 | */ | |
2141 | ||
266483d2 | 2142 | if (RAND_bytes(rand_premaster_secret, |
20ca916d | 2143 | sizeof(rand_premaster_secret)) <= 0) { |
0f113f3e | 2144 | goto err; |
20ca916d EK |
2145 | } |
2146 | ||
2147 | decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster), | |
2148 | PACKET_data(&enc_premaster), | |
2149 | rsa_decrypt, rsa, RSA_PKCS1_PADDING); | |
0f113f3e MC |
2150 | ERR_clear_error(); |
2151 | ||
2152 | /* | |
2153 | * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will | |
2154 | * be 0xff if so and zero otherwise. | |
2155 | */ | |
2156 | decrypt_good = | |
2157 | constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH); | |
2158 | ||
2159 | /* | |
2160 | * If the version in the decrypted pre-master secret is correct then | |
2161 | * version_good will be 0xff, otherwise it'll be zero. The | |
2162 | * Klima-Pokorny-Rosa extension of Bleichenbacher's attack | |
2163 | * (http://eprint.iacr.org/2003/052/) exploits the version number | |
2164 | * check as a "bad version oracle". Thus version checks are done in | |
2165 | * constant time and are treated like any other decryption error. | |
2166 | */ | |
2167 | version_good = | |
20ca916d EK |
2168 | constant_time_eq_8(rsa_decrypt[0], |
2169 | (unsigned)(s->client_version >> 8)); | |
0f113f3e | 2170 | version_good &= |
20ca916d EK |
2171 | constant_time_eq_8(rsa_decrypt[1], |
2172 | (unsigned)(s->client_version & 0xff)); | |
0f113f3e MC |
2173 | |
2174 | /* | |
2175 | * The premaster secret must contain the same version number as the | |
2176 | * ClientHello to detect version rollback attacks (strangely, the | |
2177 | * protocol does not offer such protection for DH ciphersuites). | |
2178 | * However, buggy clients exist that send the negotiated protocol | |
2179 | * version instead if the server does not support the requested | |
2180 | * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such | |
2181 | * clients. | |
2182 | */ | |
2183 | if (s->options & SSL_OP_TLS_ROLLBACK_BUG) { | |
2184 | unsigned char workaround_good; | |
2185 | workaround_good = | |
20ca916d | 2186 | constant_time_eq_8(rsa_decrypt[0], (unsigned)(s->version >> 8)); |
0f113f3e | 2187 | workaround_good &= |
20ca916d EK |
2188 | constant_time_eq_8(rsa_decrypt[1], |
2189 | (unsigned)(s->version & 0xff)); | |
0f113f3e MC |
2190 | version_good |= workaround_good; |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * Both decryption and version must be good for decrypt_good to | |
2195 | * remain non-zero (0xff). | |
2196 | */ | |
2197 | decrypt_good &= version_good; | |
2198 | ||
2199 | /* | |
2200 | * Now copy rand_premaster_secret over from p using | |
2201 | * decrypt_good_mask. If decryption failed, then p does not | |
2202 | * contain valid plaintext, however, a check above guarantees | |
2203 | * it is still sufficiently large to read from. | |
2204 | */ | |
2205 | for (j = 0; j < sizeof(rand_premaster_secret); j++) { | |
20ca916d EK |
2206 | rsa_decrypt[j] = |
2207 | constant_time_select_8(decrypt_good, rsa_decrypt[j], | |
2208 | rand_premaster_secret[j]); | |
0f113f3e MC |
2209 | } |
2210 | ||
20ca916d EK |
2211 | if (!ssl_generate_master_secret(s, rsa_decrypt, |
2212 | sizeof(rand_premaster_secret), 0)) { | |
69f68237 | 2213 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 2214 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
69f68237 MC |
2215 | goto f_err; |
2216 | } | |
20ca916d EK |
2217 | OPENSSL_free(rsa_decrypt); |
2218 | rsa_decrypt = NULL; | |
0f113f3e | 2219 | } else |
4c5fac4a | 2220 | #endif |
bc36ee62 | 2221 | #ifndef OPENSSL_NO_DH |
bc71f910 | 2222 | if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
e2b420fd DSH |
2223 | EVP_PKEY *skey = NULL; |
2224 | DH *cdh; | |
f100b031 | 2225 | unsigned int i; |
0aeddcfa | 2226 | BIGNUM *pub_key; |
5c001c32 | 2227 | const unsigned char *data; |
efcdbcbe | 2228 | |
73999b62 | 2229 | if (!PACKET_get_net_2(pkt, &i)) { |
85269210 | 2230 | if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
d3cc5e61 | 2231 | al = SSL_AD_HANDSHAKE_FAILURE; |
e27f234a | 2232 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
d3cc5e61 MC |
2233 | SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
2234 | goto f_err; | |
2235 | } | |
0f113f3e | 2236 | i = 0; |
d3cc5e61 | 2237 | } |
73999b62 | 2238 | if (PACKET_remaining(pkt) != i) { |
7538cb82 DSH |
2239 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
2240 | SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); | |
2241 | goto err; | |
0f113f3e | 2242 | } |
e2b420fd DSH |
2243 | skey = s->s3->tmp.pkey; |
2244 | if (skey == NULL) { | |
0f113f3e | 2245 | al = SSL_AD_HANDSHAKE_FAILURE; |
e27f234a | 2246 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
0f113f3e MC |
2247 | SSL_R_MISSING_TMP_DH_KEY); |
2248 | goto f_err; | |
e2b420fd | 2249 | } |
0f113f3e | 2250 | |
73999b62 | 2251 | if (PACKET_remaining(pkt) == 0L) { |
bc71f910 DSH |
2252 | al = SSL_AD_HANDSHAKE_FAILURE; |
2253 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, | |
2254 | SSL_R_MISSING_TMP_DH_KEY); | |
2255 | goto f_err; | |
2256 | } | |
2257 | if (!PACKET_get_bytes(pkt, &data, i)) { | |
2258 | /* We already checked we have enough data */ | |
2259 | al = SSL_AD_INTERNAL_ERROR; | |
2260 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, | |
2261 | ERR_R_INTERNAL_ERROR); | |
2262 | goto f_err; | |
efcdbcbe | 2263 | } |
e2b420fd DSH |
2264 | ckey = EVP_PKEY_new(); |
2265 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) { | |
e27f234a | 2266 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB); |
0f113f3e MC |
2267 | goto err; |
2268 | } | |
e2b420fd | 2269 | cdh = EVP_PKEY_get0_DH(ckey); |
0aeddcfa MC |
2270 | pub_key = BN_bin2bn(data, i, NULL); |
2271 | ||
2272 | if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) { | |
2273 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2274 | if (pub_key != NULL) | |
2275 | BN_free(pub_key); | |
0f113f3e MC |
2276 | goto err; |
2277 | } | |
2278 | ||
e2b420fd | 2279 | if (ssl_derive(s, skey, ckey) == 0) { |
69f68237 | 2280 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 2281 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
69f68237 MC |
2282 | goto f_err; |
2283 | } | |
e2b420fd DSH |
2284 | |
2285 | EVP_PKEY_free(ckey); | |
2286 | ckey = NULL; | |
5b326dc5 DSH |
2287 | EVP_PKEY_free(s->s3->tmp.pkey); |
2288 | s->s3->tmp.pkey = NULL; | |
e2b420fd | 2289 | |
0f113f3e | 2290 | } else |
d02b48c6 | 2291 | #endif |
ea262260 | 2292 | |
556efe79 | 2293 | #ifndef OPENSSL_NO_EC |
ce0c1f2b DSH |
2294 | if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
2295 | EVP_PKEY *skey = s->s3->tmp.pkey; | |
0f113f3e | 2296 | |
73999b62 | 2297 | if (PACKET_remaining(pkt) == 0L) { |
c66ce5eb DSH |
2298 | /* We don't support ECDH client auth */ |
2299 | al = SSL_AD_HANDSHAKE_FAILURE; | |
2300 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, | |
2301 | SSL_R_MISSING_TMP_ECDH_KEY); | |
2302 | goto f_err; | |
0f113f3e | 2303 | } else { |
f100b031 | 2304 | unsigned int i; |
5c001c32 | 2305 | const unsigned char *data; |
f100b031 | 2306 | |
0f113f3e MC |
2307 | /* |
2308 | * Get client's public key from encoded point in the | |
2309 | * ClientKeyExchange message. | |
2310 | */ | |
0f113f3e MC |
2311 | |
2312 | /* Get encoded point length */ | |
73999b62 | 2313 | if (!PACKET_get_1(pkt, &i)) { |
efcdbcbe | 2314 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2315 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
efcdbcbe MC |
2316 | SSL_R_LENGTH_MISMATCH); |
2317 | goto f_err; | |
2318 | } | |
73999b62 MC |
2319 | if (!PACKET_get_bytes(pkt, &data, i) |
2320 | || PACKET_remaining(pkt) != 0) { | |
e27f234a | 2321 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); |
0f113f3e MC |
2322 | goto err; |
2323 | } | |
880d9d86 DSH |
2324 | ckey = EVP_PKEY_new(); |
2325 | if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) { | |
2326 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EVP_LIB); | |
2327 | goto err; | |
2328 | } | |
2329 | if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(ckey), data, i, | |
2330 | NULL) == 0) { | |
e27f234a | 2331 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); |
0f113f3e MC |
2332 | goto err; |
2333 | } | |
0f113f3e MC |
2334 | } |
2335 | ||
880d9d86 | 2336 | if (ssl_derive(s, skey, ckey) == 0) { |
69f68237 | 2337 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 2338 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
69f68237 MC |
2339 | goto f_err; |
2340 | } | |
880d9d86 DSH |
2341 | |
2342 | EVP_PKEY_free(ckey); | |
2343 | ckey = NULL; | |
5b326dc5 DSH |
2344 | EVP_PKEY_free(s->s3->tmp.pkey); |
2345 | s->s3->tmp.pkey = NULL; | |
880d9d86 | 2346 | |
e27f234a | 2347 | return MSG_PROCESS_CONTINUE_PROCESSING; |
0f113f3e | 2348 | } else |
ddac1974 | 2349 | #endif |
edc032b5 | 2350 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 2351 | if (alg_k & SSL_kSRP) { |
f100b031 | 2352 | unsigned int i; |
5c001c32 | 2353 | const unsigned char *data; |
f100b031 | 2354 | |
73999b62 MC |
2355 | if (!PACKET_get_net_2(pkt, &i) |
2356 | || !PACKET_get_bytes(pkt, &data, i)) { | |
0f113f3e | 2357 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2358 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_BAD_SRP_A_LENGTH); |
0f113f3e MC |
2359 | goto f_err; |
2360 | } | |
efcdbcbe | 2361 | if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) { |
e27f234a | 2362 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB); |
0f113f3e MC |
2363 | goto err; |
2364 | } | |
2365 | if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 | |
2366 | || BN_is_zero(s->srp_ctx.A)) { | |
2367 | al = SSL_AD_ILLEGAL_PARAMETER; | |
e27f234a | 2368 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
0f113f3e MC |
2369 | SSL_R_BAD_SRP_PARAMETERS); |
2370 | goto f_err; | |
2371 | } | |
b548a1f1 | 2372 | OPENSSL_free(s->session->srp_username); |
7644a9ae | 2373 | s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); |
0f113f3e | 2374 | if (s->session->srp_username == NULL) { |
e27f234a | 2375 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
2376 | goto err; |
2377 | } | |
2378 | ||
57b272b0 | 2379 | if (!srp_generate_server_master_secret(s)) { |
e27f234a | 2380 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
0f113f3e MC |
2381 | goto err; |
2382 | } | |
0f113f3e MC |
2383 | } else |
2384 | #endif /* OPENSSL_NO_SRP */ | |
2a9b9654 | 2385 | #ifndef OPENSSL_NO_GOST |
0f113f3e | 2386 | if (alg_k & SSL_kGOST) { |
0f113f3e MC |
2387 | EVP_PKEY_CTX *pkey_ctx; |
2388 | EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; | |
b6981744 EK |
2389 | unsigned char premaster_secret[32]; |
2390 | const unsigned char *start; | |
0f113f3e MC |
2391 | size_t outlen = 32, inlen; |
2392 | unsigned long alg_a; | |
2393 | int Ttag, Tclass; | |
2394 | long Tlen; | |
73999b62 | 2395 | long sess_key_len; |
5c001c32 | 2396 | const unsigned char *data; |
0f113f3e MC |
2397 | |
2398 | /* Get our certificate private key */ | |
2399 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
e44380a9 DB |
2400 | if (alg_a & SSL_aGOST12) { |
2401 | /* | |
2402 | * New GOST ciphersuites have SSL_aGOST01 bit too | |
2403 | */ | |
2404 | pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey; | |
2405 | if (pk == NULL) { | |
2406 | pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; | |
2407 | } | |
2408 | if (pk == NULL) { | |
2409 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; | |
2410 | } | |
2411 | } else if (alg_a & SSL_aGOST01) { | |
0f113f3e | 2412 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; |
e44380a9 | 2413 | } |
0f113f3e MC |
2414 | |
2415 | pkey_ctx = EVP_PKEY_CTX_new(pk, NULL); | |
a71edf3b MC |
2416 | if (pkey_ctx == NULL) { |
2417 | al = SSL_AD_INTERNAL_ERROR; | |
2418 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
2419 | goto f_err; | |
2420 | } | |
5f3d93e4 MC |
2421 | if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { |
2422 | al = SSL_AD_INTERNAL_ERROR; | |
2423 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
2424 | goto f_err; | |
2425 | } | |
0f113f3e MC |
2426 | /* |
2427 | * If client certificate is present and is of the same type, maybe | |
2428 | * use it for key exchange. Don't mind errors from | |
2429 | * EVP_PKEY_derive_set_peer, because it is completely valid to use a | |
2430 | * client certificate for authorization only. | |
2431 | */ | |
8382fd3a | 2432 | client_pub_pkey = X509_get0_pubkey(s->session->peer); |
0f113f3e MC |
2433 | if (client_pub_pkey) { |
2434 | if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0) | |
2435 | ERR_clear_error(); | |
2436 | } | |
2437 | /* Decrypt session key */ | |
73999b62 MC |
2438 | sess_key_len = PACKET_remaining(pkt); |
2439 | if (!PACKET_get_bytes(pkt, &data, sess_key_len)) { | |
efcdbcbe | 2440 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 2441 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
5f3d93e4 | 2442 | goto gerr; |
efcdbcbe | 2443 | } |
73999b62 MC |
2444 | if (ASN1_get_object ((const unsigned char **)&data, &Tlen, &Ttag, |
2445 | &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED | |
2446 | || Ttag != V_ASN1_SEQUENCE | |
0f113f3e | 2447 | || Tclass != V_ASN1_UNIVERSAL) { |
5f3d93e4 | 2448 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2449 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
0f113f3e MC |
2450 | SSL_R_DECRYPTION_FAILED); |
2451 | goto gerr; | |
2452 | } | |
efcdbcbe | 2453 | start = data; |
0f113f3e MC |
2454 | inlen = Tlen; |
2455 | if (EVP_PKEY_decrypt | |
2456 | (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) { | |
ba4f1331 | 2457 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2458 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, |
0f113f3e MC |
2459 | SSL_R_DECRYPTION_FAILED); |
2460 | goto gerr; | |
2461 | } | |
2462 | /* Generate master secret */ | |
57b272b0 DSH |
2463 | if (!ssl_generate_master_secret(s, premaster_secret, |
2464 | sizeof(premaster_secret), 0)) { | |
69f68237 | 2465 | al = SSL_AD_INTERNAL_ERROR; |
e27f234a | 2466 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
5f3d93e4 | 2467 | goto gerr; |
69f68237 | 2468 | } |
0f113f3e MC |
2469 | /* Check if pubkey from client certificate was used */ |
2470 | if (EVP_PKEY_CTX_ctrl | |
2471 | (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) | |
a71a4966 | 2472 | s->statem.no_cert_verify = 1; |
e27f234a | 2473 | |
e27f234a MC |
2474 | EVP_PKEY_CTX_free(pkey_ctx); |
2475 | return MSG_PROCESS_CONTINUE_PROCESSING; | |
0f113f3e | 2476 | gerr: |
0f113f3e | 2477 | EVP_PKEY_CTX_free(pkey_ctx); |
5f3d93e4 | 2478 | goto f_err; |
2a9b9654 MC |
2479 | } else |
2480 | #endif | |
2481 | { | |
0f113f3e | 2482 | al = SSL_AD_HANDSHAKE_FAILURE; |
e27f234a | 2483 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE); |
0f113f3e MC |
2484 | goto f_err; |
2485 | } | |
2486 | ||
e27f234a | 2487 | return MSG_PROCESS_CONTINUE_PROCESSING; |
0f113f3e MC |
2488 | f_err: |
2489 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
556efe79 | 2490 | #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP) |
0f113f3e | 2491 | err: |
ea262260 | 2492 | #endif |
b22d7113 | 2493 | #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) |
880d9d86 | 2494 | EVP_PKEY_free(ckey); |
85269210 | 2495 | #endif |
c849c6d9 | 2496 | OPENSSL_free(rsa_decrypt); |
85269210 DSH |
2497 | #ifndef OPENSSL_NO_PSK |
2498 | OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen); | |
2499 | s->s3->tmp.psk = NULL; | |
58964a49 | 2500 | #endif |
fe3a3291 | 2501 | ossl_statem_set_error(s); |
e27f234a | 2502 | return MSG_PROCESS_ERROR; |
0f113f3e | 2503 | } |
d02b48c6 | 2504 | |
be3583fa | 2505 | WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst) |
94836de2 | 2506 | { |
94836de2 | 2507 | #ifndef OPENSSL_NO_SCTP |
c130dd8e MC |
2508 | if (wst == WORK_MORE_A) { |
2509 | if (SSL_IS_DTLS(s)) { | |
2510 | unsigned char sctpauthkey[64]; | |
2511 | char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; | |
2512 | /* | |
2513 | * Add new shared key for SCTP-Auth, will be ignored if no SCTP | |
2514 | * used. | |
2515 | */ | |
141eb8c6 MC |
2516 | memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
2517 | sizeof(DTLS1_SCTP_AUTH_LABEL)); | |
c130dd8e MC |
2518 | |
2519 | if (SSL_export_keying_material(s, sctpauthkey, | |
2520 | sizeof(sctpauthkey), labelbuffer, | |
2521 | sizeof(labelbuffer), NULL, 0, 0) <= 0) { | |
fe3a3291 | 2522 | ossl_statem_set_error(s); |
c130dd8e MC |
2523 | return WORK_ERROR;; |
2524 | } | |
94836de2 | 2525 | |
c130dd8e MC |
2526 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
2527 | sizeof(sctpauthkey), sctpauthkey); | |
94836de2 | 2528 | } |
c130dd8e MC |
2529 | wst = WORK_MORE_B; |
2530 | } | |
94836de2 | 2531 | |
c130dd8e MC |
2532 | if ((wst == WORK_MORE_B) |
2533 | /* Is this SCTP? */ | |
2534 | && BIO_dgram_is_sctp(SSL_get_wbio(s)) | |
2535 | /* Are we renegotiating? */ | |
2536 | && s->renegotiate | |
2537 | /* Are we going to skip the CertificateVerify? */ | |
a71a4966 | 2538 | && (s->session->peer == NULL || s->statem.no_cert_verify) |
c130dd8e MC |
2539 | && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) { |
2540 | s->s3->in_read_app_data = 2; | |
2541 | s->rwstate = SSL_READING; | |
2542 | BIO_clear_retry_flags(SSL_get_rbio(s)); | |
2543 | BIO_set_retry_read(SSL_get_rbio(s)); | |
d99b0691 | 2544 | ossl_statem_set_sctp_read_sock(s, 1); |
c130dd8e MC |
2545 | return WORK_MORE_B; |
2546 | } else { | |
fe3a3291 | 2547 | ossl_statem_set_sctp_read_sock(s, 0); |
94836de2 MC |
2548 | } |
2549 | #endif | |
2550 | ||
a71a4966 | 2551 | if (s->statem.no_cert_verify) { |
94836de2 MC |
2552 | /* No certificate verify so we no longer need the handshake_buffer */ |
2553 | BIO_free(s->s3->handshake_buffer); | |
e44380a9 | 2554 | s->s3->handshake_buffer = NULL; |
94836de2 | 2555 | return WORK_FINISHED_CONTINUE; |
28f4580c | 2556 | } else { |
94836de2 MC |
2557 | if (!s->session->peer) { |
2558 | /* No peer certificate so we no longer need the handshake_buffer */ | |
2559 | BIO_free(s->s3->handshake_buffer); | |
2560 | return WORK_FINISHED_CONTINUE; | |
2561 | } | |
2562 | if (!s->s3->handshake_buffer) { | |
2563 | SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE, | |
2564 | ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 2565 | ossl_statem_set_error(s); |
94836de2 MC |
2566 | return WORK_ERROR; |
2567 | } | |
2568 | /* | |
2569 | * For sigalgs freeze the handshake buffer. If we support | |
2570 | * extms we've done this already so this is a no-op | |
2571 | */ | |
2572 | if (!ssl3_digest_cached_records(s, 1)) { | |
fe3a3291 | 2573 | ossl_statem_set_error(s); |
94836de2 MC |
2574 | return WORK_ERROR; |
2575 | } | |
94836de2 MC |
2576 | } |
2577 | ||
2578 | return WORK_FINISHED_CONTINUE; | |
2579 | } | |
2580 | ||
be3583fa | 2581 | MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt) |
e27f234a MC |
2582 | { |
2583 | EVP_PKEY *pkey = NULL; | |
b6981744 | 2584 | const unsigned char *sig, *data; |
5ca17d8c | 2585 | #ifndef OPENSSL_NO_GOST |
b6981744 | 2586 | unsigned char *gost_data = NULL; |
5ca17d8c | 2587 | #endif |
e27f234a | 2588 | int al, ret = MSG_PROCESS_ERROR; |
28f4580c | 2589 | int type = 0, j; |
e27f234a MC |
2590 | unsigned int len; |
2591 | X509 *peer; | |
2592 | const EVP_MD *md = NULL; | |
28f4580c DSH |
2593 | long hdatalen = 0; |
2594 | void *hdata; | |
2595 | ||
bfb0641f | 2596 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
6e59a892 RL |
2597 | |
2598 | if (mctx == NULL) { | |
2599 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
2600 | al = SSL_AD_INTERNAL_ERROR; | |
2601 | goto f_err; | |
2602 | } | |
e27f234a | 2603 | |
a0bd6493 | 2604 | peer = s->session->peer; |
8382fd3a | 2605 | pkey = X509_get0_pubkey(peer); |
a0bd6493 | 2606 | type = X509_certificate_type(peer, pkey); |
0f113f3e MC |
2607 | |
2608 | if (!(type & EVP_PKT_SIGN)) { | |
e27f234a | 2609 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, |
0f113f3e MC |
2610 | SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); |
2611 | al = SSL_AD_ILLEGAL_PARAMETER; | |
2612 | goto f_err; | |
2613 | } | |
2614 | ||
0f113f3e MC |
2615 | /* Check for broken implementations of GOST ciphersuites */ |
2616 | /* | |
2617 | * If key is GOST and n is exactly 64, it is bare signature without | |
e44380a9 | 2618 | * length field (CryptoPro implementations at least till CSP 4.0) |
0f113f3e | 2619 | */ |
2a9b9654 | 2620 | #ifndef OPENSSL_NO_GOST |
3aeb9348 DSH |
2621 | if (PACKET_remaining(pkt) == 64 |
2622 | && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) { | |
f532a35d | 2623 | len = 64; |
2a9b9654 MC |
2624 | } else |
2625 | #endif | |
2626 | { | |
0f113f3e | 2627 | if (SSL_USE_SIGALGS(s)) { |
f532a35d MC |
2628 | int rv; |
2629 | ||
73999b62 | 2630 | if (!PACKET_get_bytes(pkt, &sig, 2)) { |
f532a35d MC |
2631 | al = SSL_AD_DECODE_ERROR; |
2632 | goto f_err; | |
2633 | } | |
2634 | rv = tls12_check_peer_sigalg(&md, s, sig, pkey); | |
0f113f3e MC |
2635 | if (rv == -1) { |
2636 | al = SSL_AD_INTERNAL_ERROR; | |
2637 | goto f_err; | |
2638 | } else if (rv == 0) { | |
2639 | al = SSL_AD_DECODE_ERROR; | |
2640 | goto f_err; | |
2641 | } | |
f37f20ff | 2642 | #ifdef SSL_DEBUG |
0f113f3e | 2643 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
f37f20ff | 2644 | #endif |
28f4580c | 2645 | } else { |
aa430c74 DSH |
2646 | /* Use default digest for this key type */ |
2647 | int idx = ssl_cert_type(NULL, pkey); | |
2648 | if (idx >= 0) | |
2649 | md = s->s3->tmp.md[idx]; | |
2650 | if (md == NULL) { | |
2651 | al = SSL_AD_INTERNAL_ERROR; | |
2652 | goto f_err; | |
2653 | } | |
0f113f3e | 2654 | } |
aa430c74 | 2655 | |
73999b62 | 2656 | if (!PACKET_get_net_2(pkt, &len)) { |
e27f234a | 2657 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2658 | al = SSL_AD_DECODE_ERROR; |
2659 | goto f_err; | |
2660 | } | |
2661 | } | |
2662 | j = EVP_PKEY_size(pkey); | |
73999b62 MC |
2663 | if (((int)len > j) || ((int)PACKET_remaining(pkt) > j) |
2664 | || (PACKET_remaining(pkt) == 0)) { | |
e27f234a | 2665 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE); |
0f113f3e MC |
2666 | al = SSL_AD_DECODE_ERROR; |
2667 | goto f_err; | |
2668 | } | |
73999b62 | 2669 | if (!PACKET_get_bytes(pkt, &data, len)) { |
e27f234a | 2670 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH); |
f532a35d MC |
2671 | al = SSL_AD_DECODE_ERROR; |
2672 | goto f_err; | |
2673 | } | |
0f113f3e | 2674 | |
28f4580c DSH |
2675 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); |
2676 | if (hdatalen <= 0) { | |
2677 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR); | |
2678 | al = SSL_AD_INTERNAL_ERROR; | |
2679 | goto f_err; | |
2680 | } | |
f37f20ff | 2681 | #ifdef SSL_DEBUG |
28f4580c | 2682 | fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md)); |
f37f20ff | 2683 | #endif |
6e59a892 RL |
2684 | if (!EVP_VerifyInit_ex(mctx, md, NULL) |
2685 | || !EVP_VerifyUpdate(mctx, hdata, hdatalen)) { | |
28f4580c DSH |
2686 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); |
2687 | al = SSL_AD_INTERNAL_ERROR; | |
2688 | goto f_err; | |
2689 | } | |
0f113f3e | 2690 | |
2a9b9654 | 2691 | #ifndef OPENSSL_NO_GOST |
3aeb9348 DSH |
2692 | { |
2693 | int pktype = EVP_PKEY_id(pkey); | |
2694 | if (pktype == NID_id_GostR3410_2001 | |
2695 | || pktype == NID_id_GostR3410_2012_256 | |
b6981744 EK |
2696 | || pktype == NID_id_GostR3410_2012_512) { |
2697 | if ((gost_data = OPENSSL_malloc(len)) == NULL) { | |
2698 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE); | |
2699 | al = SSL_AD_INTERNAL_ERROR; | |
2700 | goto f_err; | |
2701 | } | |
2702 | BUF_reverse(gost_data, data, len); | |
2703 | data = gost_data; | |
2704 | } | |
28f4580c | 2705 | } |
2a9b9654 | 2706 | #endif |
e44380a9 | 2707 | |
28f4580c | 2708 | if (s->version == SSL3_VERSION |
6e59a892 | 2709 | && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET, |
28f4580c DSH |
2710 | s->session->master_key_length, |
2711 | s->session->master_key)) { | |
2712 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB); | |
2713 | al = SSL_AD_INTERNAL_ERROR; | |
2714 | goto f_err; | |
2715 | } | |
2716 | ||
6e59a892 | 2717 | if (EVP_VerifyFinal(mctx, data, len, pkey) <= 0) { |
28f4580c DSH |
2718 | al = SSL_AD_DECRYPT_ERROR; |
2719 | SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE); | |
0f113f3e MC |
2720 | goto f_err; |
2721 | } | |
2722 | ||
c130dd8e | 2723 | ret = MSG_PROCESS_CONTINUE_PROCESSING; |
0f113f3e MC |
2724 | if (0) { |
2725 | f_err: | |
2726 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
fe3a3291 | 2727 | ossl_statem_set_error(s); |
0f113f3e | 2728 | } |
25aaa98a RS |
2729 | BIO_free(s->s3->handshake_buffer); |
2730 | s->s3->handshake_buffer = NULL; | |
bfb0641f | 2731 | EVP_MD_CTX_free(mctx); |
5ca17d8c | 2732 | #ifndef OPENSSL_NO_GOST |
b6981744 | 2733 | OPENSSL_free(gost_data); |
5ca17d8c | 2734 | #endif |
e27f234a | 2735 | return ret; |
0f113f3e | 2736 | } |
d02b48c6 | 2737 | |
be3583fa | 2738 | MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt) |
e27f234a | 2739 | { |
20dbe585 | 2740 | int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR; |
e27f234a MC |
2741 | X509 *x = NULL; |
2742 | unsigned long l, llen; | |
b6981744 | 2743 | const unsigned char *certstart, *certbytes; |
e27f234a | 2744 | STACK_OF(X509) *sk = NULL; |
73999b62 | 2745 | PACKET spkt; |
0f113f3e MC |
2746 | |
2747 | if ((sk = sk_X509_new_null()) == NULL) { | |
e27f234a MC |
2748 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
2749 | goto f_err; | |
0f113f3e MC |
2750 | } |
2751 | ||
73999b62 MC |
2752 | if (!PACKET_get_net_3(pkt, &llen) |
2753 | || !PACKET_get_sub_packet(pkt, &spkt, llen) | |
2754 | || PACKET_remaining(pkt) != 0) { | |
0f113f3e | 2755 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2756 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
0f113f3e MC |
2757 | goto f_err; |
2758 | } | |
0bc09ecd MC |
2759 | |
2760 | while (PACKET_remaining(&spkt) > 0) { | |
2761 | if (!PACKET_get_net_3(&spkt, &l) | |
2762 | || !PACKET_get_bytes(&spkt, &certbytes, l)) { | |
0f113f3e | 2763 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2764 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2765 | SSL_R_CERT_LENGTH_MISMATCH); |
2766 | goto f_err; | |
2767 | } | |
2768 | ||
0bc09ecd MC |
2769 | certstart = certbytes; |
2770 | x = d2i_X509(NULL, (const unsigned char **)&certbytes, l); | |
0f113f3e | 2771 | if (x == NULL) { |
e27f234a MC |
2772 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB); |
2773 | goto f_err; | |
0f113f3e | 2774 | } |
0bc09ecd | 2775 | if (certbytes != (certstart + l)) { |
0f113f3e | 2776 | al = SSL_AD_DECODE_ERROR; |
e27f234a | 2777 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2778 | SSL_R_CERT_LENGTH_MISMATCH); |
2779 | goto f_err; | |
2780 | } | |
2781 | if (!sk_X509_push(sk, x)) { | |
e27f234a MC |
2782 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
2783 | goto f_err; | |
0f113f3e MC |
2784 | } |
2785 | x = NULL; | |
0f113f3e MC |
2786 | } |
2787 | ||
2788 | if (sk_X509_num(sk) <= 0) { | |
2789 | /* TLS does not mind 0 certs returned */ | |
2790 | if (s->version == SSL3_VERSION) { | |
2791 | al = SSL_AD_HANDSHAKE_FAILURE; | |
e27f234a | 2792 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2793 | SSL_R_NO_CERTIFICATES_RETURNED); |
2794 | goto f_err; | |
2795 | } | |
2796 | /* Fail for TLS only if we required a certificate */ | |
2797 | else if ((s->verify_mode & SSL_VERIFY_PEER) && | |
2798 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | |
e27f234a | 2799 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2800 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
2801 | al = SSL_AD_HANDSHAKE_FAILURE; | |
2802 | goto f_err; | |
2803 | } | |
2804 | /* No client certificate so digest cached records */ | |
124037fd | 2805 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) { |
0f113f3e MC |
2806 | goto f_err; |
2807 | } | |
2808 | } else { | |
2809 | EVP_PKEY *pkey; | |
2810 | i = ssl_verify_cert_chain(s, sk); | |
2811 | if (i <= 0) { | |
2812 | al = ssl_verify_alarm_type(s->verify_result); | |
e27f234a | 2813 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2814 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
2815 | goto f_err; | |
2816 | } | |
2817 | if (i > 1) { | |
e27f234a | 2818 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i); |
0f113f3e MC |
2819 | al = SSL_AD_HANDSHAKE_FAILURE; |
2820 | goto f_err; | |
2821 | } | |
8382fd3a | 2822 | pkey = X509_get0_pubkey(sk_X509_value(sk, 0)); |
0f113f3e MC |
2823 | if (pkey == NULL) { |
2824 | al = SSL3_AD_HANDSHAKE_FAILURE; | |
e27f234a | 2825 | SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, |
0f113f3e MC |
2826 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
2827 | goto f_err; | |
2828 | } | |
0f113f3e MC |
2829 | } |
2830 | ||
222561fe | 2831 | X509_free(s->session->peer); |
0f113f3e MC |
2832 | s->session->peer = sk_X509_shift(sk); |
2833 | s->session->verify_result = s->verify_result; | |
2834 | ||
c34b0f99 DSH |
2835 | sk_X509_pop_free(s->session->peer_chain, X509_free); |
2836 | s->session->peer_chain = sk; | |
0f113f3e MC |
2837 | /* |
2838 | * Inconsistency alert: cert_chain does *not* include the peer's own | |
d4d78943 | 2839 | * certificate, while we do include it in statem_clnt.c |
0f113f3e | 2840 | */ |
0f113f3e | 2841 | sk = NULL; |
e27f234a | 2842 | ret = MSG_PROCESS_CONTINUE_READING; |
66696478 RS |
2843 | goto done; |
2844 | ||
0f113f3e | 2845 | f_err: |
66696478 | 2846 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
fe3a3291 | 2847 | ossl_statem_set_error(s); |
66696478 | 2848 | done: |
222561fe RS |
2849 | X509_free(x); |
2850 | sk_X509_pop_free(sk, X509_free); | |
e27f234a | 2851 | return ret; |
0f113f3e | 2852 | } |
d02b48c6 | 2853 | |
e27f234a MC |
2854 | int tls_construct_server_certificate(SSL *s) |
2855 | { | |
2856 | CERT_PKEY *cpk; | |
2857 | ||
2858 | cpk = ssl_get_server_send_pkey(s); | |
2859 | if (cpk == NULL) { | |
2860 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 2861 | ossl_statem_set_error(s); |
e27f234a MC |
2862 | return 0; |
2863 | } | |
2864 | ||
2865 | if (!ssl3_output_cert_chain(s, cpk)) { | |
2866 | SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
fe3a3291 | 2867 | ossl_statem_set_error(s); |
e27f234a MC |
2868 | return 0; |
2869 | } | |
2870 | ||
2871 | return 1; | |
2872 | } | |
2873 | ||
e27f234a MC |
2874 | int tls_construct_new_session_ticket(SSL *s) |
2875 | { | |
2876 | unsigned char *senc = NULL; | |
846ec07d | 2877 | EVP_CIPHER_CTX *ctx; |
bf7c6817 | 2878 | HMAC_CTX *hctx = NULL; |
e27f234a MC |
2879 | unsigned char *p, *macstart; |
2880 | const unsigned char *const_p; | |
2881 | int len, slen_full, slen; | |
2882 | SSL_SESSION *sess; | |
2883 | unsigned int hlen; | |
2884 | SSL_CTX *tctx = s->initial_ctx; | |
2885 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
d139723b KR |
2886 | unsigned char key_name[TLSEXT_KEYNAME_LENGTH]; |
2887 | int iv_len; | |
e27f234a MC |
2888 | |
2889 | /* get session encoding length */ | |
2890 | slen_full = i2d_SSL_SESSION(s->session, NULL); | |
2891 | /* | |
2892 | * Some length values are 16 bits, so forget it if session is too | |
2893 | * long | |
2894 | */ | |
2895 | if (slen_full == 0 || slen_full > 0xFF00) { | |
fe3a3291 | 2896 | ossl_statem_set_error(s); |
e27f234a MC |
2897 | return 0; |
2898 | } | |
2899 | senc = OPENSSL_malloc(slen_full); | |
a71edf3b | 2900 | if (senc == NULL) { |
fe3a3291 | 2901 | ossl_statem_set_error(s); |
e27f234a MC |
2902 | return 0; |
2903 | } | |
0f113f3e | 2904 | |
846ec07d | 2905 | ctx = EVP_CIPHER_CTX_new(); |
bf7c6817 | 2906 | hctx = HMAC_CTX_new(); |
0f113f3e | 2907 | |
e27f234a MC |
2908 | p = senc; |
2909 | if (!i2d_SSL_SESSION(s->session, &p)) | |
2910 | goto err; | |
687eaf27 | 2911 | |
e27f234a MC |
2912 | /* |
2913 | * create a fresh copy (not shared with other threads) to clean up | |
2914 | */ | |
2915 | const_p = senc; | |
2916 | sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); | |
2917 | if (sess == NULL) | |
2918 | goto err; | |
2919 | sess->session_id_length = 0; /* ID is irrelevant for the ticket */ | |
0f113f3e | 2920 | |
e27f234a MC |
2921 | slen = i2d_SSL_SESSION(sess, NULL); |
2922 | if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */ | |
2923 | SSL_SESSION_free(sess); | |
2924 | goto err; | |
2925 | } | |
2926 | p = senc; | |
2927 | if (!i2d_SSL_SESSION(sess, &p)) { | |
2928 | SSL_SESSION_free(sess); | |
2929 | goto err; | |
2930 | } | |
2931 | SSL_SESSION_free(sess); | |
0f113f3e | 2932 | |
e27f234a MC |
2933 | /*- |
2934 | * Grow buffer if need be: the length calculation is as | |
2935 | * follows handshake_header_length + | |
2936 | * 4 (ticket lifetime hint) + 2 (ticket length) + | |
d139723b KR |
2937 | * sizeof(keyname) + max_iv_len (iv length) + |
2938 | * max_enc_block_size (max encrypted session * length) + | |
2939 | * max_md_size (HMAC) + session_length. | |
e27f234a MC |
2940 | */ |
2941 | if (!BUF_MEM_grow(s->init_buf, | |
d139723b KR |
2942 | SSL_HM_HEADER_LENGTH(s) + 6 + sizeof(key_name) + |
2943 | EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + | |
2944 | EVP_MAX_MD_SIZE + slen)) | |
e27f234a | 2945 | goto err; |
0f113f3e | 2946 | |
e27f234a MC |
2947 | p = ssl_handshake_start(s); |
2948 | /* | |
2949 | * Initialize HMAC and cipher contexts. If callback present it does | |
2950 | * all the work otherwise use generated values from parent ctx. | |
2951 | */ | |
2952 | if (tctx->tlsext_ticket_key_cb) { | |
5c753de6 TS |
2953 | /* if 0 is returned, write an empty ticket */ |
2954 | int ret = tctx->tlsext_ticket_key_cb(s, key_name, iv, ctx, | |
2955 | hctx, 1); | |
2956 | ||
2957 | if (ret == 0) { | |
2958 | l2n(0, p); /* timeout */ | |
2959 | s2n(0, p); /* length */ | |
2960 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, p - ssl_handshake_start(s))) | |
2961 | goto err; | |
2962 | OPENSSL_free(senc); | |
2963 | EVP_CIPHER_CTX_free(ctx); | |
2964 | HMAC_CTX_free(hctx); | |
2965 | return 1; | |
2966 | } | |
2967 | if (ret < 0) | |
e27f234a | 2968 | goto err; |
d139723b | 2969 | iv_len = EVP_CIPHER_CTX_iv_length(ctx); |
e27f234a | 2970 | } else { |
d139723b KR |
2971 | const EVP_CIPHER *cipher = EVP_aes_256_cbc(); |
2972 | ||
2973 | iv_len = EVP_CIPHER_iv_length(cipher); | |
2974 | if (RAND_bytes(iv, iv_len) <= 0) | |
687eaf27 | 2975 | goto err; |
d139723b | 2976 | if (!EVP_EncryptInit_ex(ctx, cipher, NULL, |
e27f234a | 2977 | tctx->tlsext_tick_aes_key, iv)) |
687eaf27 | 2978 | goto err; |
4e2e1ec9 TS |
2979 | if (!HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, |
2980 | sizeof(tctx->tlsext_tick_hmac_key), | |
e27f234a | 2981 | EVP_sha256(), NULL)) |
4f9fab6b | 2982 | goto err; |
4e2e1ec9 TS |
2983 | memcpy(key_name, tctx->tlsext_tick_key_name, |
2984 | sizeof(tctx->tlsext_tick_key_name)); | |
0f113f3e MC |
2985 | } |
2986 | ||
e27f234a MC |
2987 | /* |
2988 | * Ticket lifetime hint (advisory only): We leave this unspecified | |
2989 | * for resumed session (for simplicity), and guess that tickets for | |
2990 | * new sessions will live as long as their sessions. | |
2991 | */ | |
2992 | l2n(s->hit ? 0 : s->session->timeout, p); | |
2993 | ||
2994 | /* Skip ticket length for now */ | |
2995 | p += 2; | |
2996 | /* Output key name */ | |
2997 | macstart = p; | |
d139723b KR |
2998 | memcpy(p, key_name, sizeof(key_name)); |
2999 | p += sizeof(key_name); | |
e27f234a | 3000 | /* output IV */ |
d139723b KR |
3001 | memcpy(p, iv, iv_len); |
3002 | p += iv_len; | |
e27f234a | 3003 | /* Encrypt session data */ |
846ec07d | 3004 | if (!EVP_EncryptUpdate(ctx, p, &len, senc, slen)) |
e27f234a MC |
3005 | goto err; |
3006 | p += len; | |
846ec07d | 3007 | if (!EVP_EncryptFinal(ctx, p, &len)) |
e27f234a MC |
3008 | goto err; |
3009 | p += len; | |
3010 | ||
bf7c6817 | 3011 | if (!HMAC_Update(hctx, macstart, p - macstart)) |
e27f234a | 3012 | goto err; |
bf7c6817 | 3013 | if (!HMAC_Final(hctx, p, &hlen)) |
e27f234a MC |
3014 | goto err; |
3015 | ||
846ec07d | 3016 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 3017 | HMAC_CTX_free(hctx); |
846ec07d RL |
3018 | ctx = NULL; |
3019 | hctx = NULL; | |
e27f234a MC |
3020 | |
3021 | p += hlen; | |
3022 | /* Now write out lengths: p points to end of data written */ | |
3023 | /* Total length */ | |
3024 | len = p - ssl_handshake_start(s); | |
3025 | /* Skip ticket lifetime hint */ | |
3026 | p = ssl_handshake_start(s) + 4; | |
3027 | s2n(len - 6, p); | |
3028 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) | |
3029 | goto err; | |
3030 | OPENSSL_free(senc); | |
3031 | ||
3032 | return 1; | |
687eaf27 | 3033 | err: |
b548a1f1 | 3034 | OPENSSL_free(senc); |
846ec07d | 3035 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 3036 | HMAC_CTX_free(hctx); |
fe3a3291 | 3037 | ossl_statem_set_error(s); |
e27f234a | 3038 | return 0; |
0f113f3e | 3039 | } |
67c8e7f4 | 3040 | |
e27f234a MC |
3041 | int tls_construct_cert_status(SSL *s) |
3042 | { | |
3043 | unsigned char *p; | |
3044 | /*- | |
3045 | * Grow buffer if need be: the length calculation is as | |
3046 | * follows 1 (message type) + 3 (message length) + | |
3047 | * 1 (ocsp response type) + 3 (ocsp response length) | |
3048 | * + (ocsp response) | |
3049 | */ | |
3050 | if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) { | |
fe3a3291 | 3051 | ossl_statem_set_error(s); |
e27f234a MC |
3052 | return 0; |
3053 | } | |
3054 | ||
3055 | p = (unsigned char *)s->init_buf->data; | |
3056 | ||
3057 | /* do the header */ | |
3058 | *(p++) = SSL3_MT_CERTIFICATE_STATUS; | |
3059 | /* message length */ | |
3060 | l2n3(s->tlsext_ocsp_resplen + 4, p); | |
3061 | /* status type */ | |
3062 | *(p++) = s->tlsext_status_type; | |
3063 | /* length of OCSP response */ | |
3064 | l2n3(s->tlsext_ocsp_resplen, p); | |
3065 | /* actual response */ | |
3066 | memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); | |
3067 | /* number of bytes to write */ | |
3068 | s->init_num = 8 + s->tlsext_ocsp_resplen; | |
3069 | s->init_off = 0; | |
3070 | ||
3071 | return 1; | |
3072 | } | |
3073 | ||
e481f9b9 | 3074 | #ifndef OPENSSL_NO_NEXTPROTONEG |
e27f234a MC |
3075 | /* |
3076 | * tls_process_next_proto reads a Next Protocol Negotiation handshake message. | |
3077 | * It sets the next_proto member in s if found | |
3078 | */ | |
be3583fa | 3079 | MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt) |
e27f234a | 3080 | { |
73999b62 | 3081 | PACKET next_proto, padding; |
e27f234a MC |
3082 | size_t next_proto_len; |
3083 | ||
50e735f9 MC |
3084 | /*- |
3085 | * The payload looks like: | |
3086 | * uint8 proto_len; | |
3087 | * uint8 proto[proto_len]; | |
3088 | * uint8 padding_len; | |
3089 | * uint8 padding[padding_len]; | |
3090 | */ | |
73999b62 MC |
3091 | if (!PACKET_get_length_prefixed_1(pkt, &next_proto) |
3092 | || !PACKET_get_length_prefixed_1(pkt, &padding) | |
3093 | || PACKET_remaining(pkt) > 0) { | |
e27f234a | 3094 | SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH); |
c3fc7eea | 3095 | goto err; |
cf9b0b6f | 3096 | } |
0f113f3e | 3097 | |
6d41fc80 EK |
3098 | if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated, |
3099 | &next_proto_len)) { | |
3100 | s->next_proto_negotiated_len = 0; | |
c3fc7eea MC |
3101 | goto err; |
3102 | } | |
3103 | ||
6d41fc80 | 3104 | s->next_proto_negotiated_len = (unsigned char)next_proto_len; |
0f113f3e | 3105 | |
e27f234a | 3106 | return MSG_PROCESS_CONTINUE_READING; |
c3fc7eea | 3107 | err: |
fe3a3291 | 3108 | ossl_statem_set_error(s); |
e27f234a | 3109 | return MSG_PROCESS_ERROR; |
0f113f3e | 3110 | } |
6434abbf | 3111 | #endif |
d45ba43d MC |
3112 | |
3113 | #define SSLV2_CIPHER_LEN 3 | |
3114 | ||
38a3cbfb EK |
3115 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, |
3116 | PACKET *cipher_suites, | |
d45ba43d | 3117 | STACK_OF(SSL_CIPHER) **skp, |
38a3cbfb EK |
3118 | int sslv2format, int *al |
3119 | ) | |
d45ba43d MC |
3120 | { |
3121 | const SSL_CIPHER *c; | |
3122 | STACK_OF(SSL_CIPHER) *sk; | |
38a3cbfb EK |
3123 | int n; |
3124 | /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */ | |
3125 | unsigned char cipher[SSLV2_CIPHER_LEN]; | |
d45ba43d | 3126 | |
38a3cbfb EK |
3127 | s->s3->send_connection_binding = 0; |
3128 | ||
3129 | n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN; | |
3130 | ||
3131 | if (PACKET_remaining(cipher_suites) == 0) { | |
3132 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED); | |
3133 | *al = SSL_AD_ILLEGAL_PARAMETER; | |
3134 | return NULL; | |
d45ba43d | 3135 | } |
38a3cbfb EK |
3136 | |
3137 | if (PACKET_remaining(cipher_suites) % n != 0) { | |
d45ba43d MC |
3138 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, |
3139 | SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); | |
38a3cbfb EK |
3140 | *al = SSL_AD_DECODE_ERROR; |
3141 | return NULL; | |
d45ba43d | 3142 | } |
38a3cbfb | 3143 | |
d45ba43d MC |
3144 | if ((skp == NULL) || (*skp == NULL)) { |
3145 | sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */ | |
3146 | if(sk == NULL) { | |
3147 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); | |
38a3cbfb | 3148 | *al = SSL_AD_INTERNAL_ERROR; |
d45ba43d MC |
3149 | return NULL; |
3150 | } | |
3151 | } else { | |
3152 | sk = *skp; | |
3153 | sk_SSL_CIPHER_zero(sk); | |
3154 | } | |
3155 | ||
38a3cbfb EK |
3156 | if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw, |
3157 | &s->s3->tmp.ciphers_rawlen)) { | |
3158 | *al = SSL_AD_INTERNAL_ERROR; | |
d45ba43d MC |
3159 | goto err; |
3160 | } | |
d45ba43d | 3161 | |
38a3cbfb EK |
3162 | while (PACKET_copy_bytes(cipher_suites, cipher, n)) { |
3163 | /* | |
20218b58 EK |
3164 | * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the |
3165 | * first byte set to zero, while true SSLv2 ciphers have a non-zero | |
3166 | * first byte. We don't support any true SSLv2 ciphers, so skip them. | |
38a3cbfb EK |
3167 | */ |
3168 | if (sslv2format && cipher[0] != '\0') | |
3169 | continue; | |
3170 | ||
d45ba43d | 3171 | /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */ |
38a3cbfb EK |
3172 | if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && |
3173 | (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) { | |
d45ba43d MC |
3174 | /* SCSV fatal if renegotiating */ |
3175 | if (s->renegotiate) { | |
3176 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, | |
3177 | SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); | |
38a3cbfb | 3178 | *al = SSL_AD_HANDSHAKE_FAILURE; |
d45ba43d MC |
3179 | goto err; |
3180 | } | |
3181 | s->s3->send_connection_binding = 1; | |
d45ba43d MC |
3182 | continue; |
3183 | } | |
3184 | ||
3185 | /* Check for TLS_FALLBACK_SCSV */ | |
38a3cbfb EK |
3186 | if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) && |
3187 | (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) { | |
d45ba43d MC |
3188 | /* |
3189 | * The SCSV indicates that the client previously tried a higher | |
3190 | * version. Fail if the current version is an unexpected | |
3191 | * downgrade. | |
3192 | */ | |
4fa52141 | 3193 | if (!ssl_check_version_downgrade(s)) { |
d45ba43d MC |
3194 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, |
3195 | SSL_R_INAPPROPRIATE_FALLBACK); | |
38a3cbfb | 3196 | *al = SSL_AD_INAPPROPRIATE_FALLBACK; |
d45ba43d MC |
3197 | goto err; |
3198 | } | |
d45ba43d MC |
3199 | continue; |
3200 | } | |
3201 | ||
38a3cbfb EK |
3202 | /* For SSLv2-compat, ignore leading 0-byte. */ |
3203 | c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher); | |
d45ba43d MC |
3204 | if (c != NULL) { |
3205 | if (!sk_SSL_CIPHER_push(sk, c)) { | |
3206 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE); | |
38a3cbfb | 3207 | *al = SSL_AD_INTERNAL_ERROR; |
d45ba43d MC |
3208 | goto err; |
3209 | } | |
3210 | } | |
3211 | } | |
38a3cbfb EK |
3212 | if (PACKET_remaining(cipher_suites) > 0) { |
3213 | *al = SSL_AD_INTERNAL_ERROR; | |
3214 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR); | |
3215 | goto err; | |
3216 | } | |
d45ba43d MC |
3217 | |
3218 | if (skp != NULL) | |
3219 | *skp = sk; | |
3220 | return (sk); | |
3221 | err: | |
3222 | if ((skp == NULL) || (*skp == NULL)) | |
3223 | sk_SSL_CIPHER_free(sk); | |
38a3cbfb | 3224 | return NULL; |
d45ba43d | 3225 | } |