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