diff options
author | jsing <> | 2017-01-26 05:51:54 +0000 |
---|---|---|
committer | jsing <> | 2017-01-26 05:51:54 +0000 |
commit | cd3d8b6f4c16680100acc89db5b955e2aa399daa (patch) | |
tree | 7144c37b9ce9871f9463b116e916019cd0ecc97f /src/lib/libssl/s3_srvr.c | |
parent | 9366f4ef0d67a19fe5eca3feedbc756a4a8966b2 (diff) | |
download | openbsd-cd3d8b6f4c16680100acc89db5b955e2aa399daa.tar.gz openbsd-cd3d8b6f4c16680100acc89db5b955e2aa399daa.tar.bz2 openbsd-cd3d8b6f4c16680100acc89db5b955e2aa399daa.zip |
Rename s3_{both,clnt,pkt_srvr}.c to have an ssl_ prefix since they are no
longer SSLv3 code.
ok beck@
Diffstat (limited to 'src/lib/libssl/s3_srvr.c')
-rw-r--r-- | src/lib/libssl/s3_srvr.c | 2923 |
1 files changed, 0 insertions, 2923 deletions
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c deleted file mode 100644 index 28f0fc486a..0000000000 --- a/src/lib/libssl/s3_srvr.c +++ /dev/null | |||
@@ -1,2923 +0,0 @@ | |||
1 | /* $OpenBSD: s3_srvr.c,v 1.154 2017/01/26 05:31:25 jsing Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
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. | ||
8 | * | ||
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). | ||
15 | * | ||
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. | ||
22 | * | ||
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 :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
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. | ||
52 | * | ||
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 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. | ||
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 | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
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 | */ | ||
111 | /* ==================================================================== | ||
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
113 | * | ||
114 | * Portions of the attached software ("Contribution") are developed by | ||
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 | * | ||
120 | * ECC cipher suite support in OpenSSL originally written by | ||
121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | ||
122 | * | ||
123 | */ | ||
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 | */ | ||
150 | |||
151 | #include <stdio.h> | ||
152 | |||
153 | #include "ssl_locl.h" | ||
154 | |||
155 | #include <openssl/bn.h> | ||
156 | #include <openssl/buffer.h> | ||
157 | #include <openssl/curve25519.h> | ||
158 | #include <openssl/evp.h> | ||
159 | #include <openssl/dh.h> | ||
160 | #ifndef OPENSSL_NO_GOST | ||
161 | #include <openssl/gost.h> | ||
162 | #endif | ||
163 | #include <openssl/hmac.h> | ||
164 | #include <openssl/md5.h> | ||
165 | #include <openssl/objects.h> | ||
166 | #include <openssl/x509.h> | ||
167 | |||
168 | #include "bytestring.h" | ||
169 | |||
170 | int | ||
171 | ssl3_accept(SSL *s) | ||
172 | { | ||
173 | unsigned long alg_k; | ||
174 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
175 | int ret = -1; | ||
176 | int new_state, state, skip = 0; | ||
177 | |||
178 | ERR_clear_error(); | ||
179 | errno = 0; | ||
180 | |||
181 | if (s->internal->info_callback != NULL) | ||
182 | cb = s->internal->info_callback; | ||
183 | else if (s->ctx->internal->info_callback != NULL) | ||
184 | cb = s->ctx->internal->info_callback; | ||
185 | |||
186 | /* init things to blank */ | ||
187 | s->internal->in_handshake++; | ||
188 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
189 | SSL_clear(s); | ||
190 | |||
191 | if (s->cert == NULL) { | ||
192 | SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET); | ||
193 | ret = -1; | ||
194 | goto end; | ||
195 | } | ||
196 | |||
197 | for (;;) { | ||
198 | state = s->internal->state; | ||
199 | |||
200 | switch (s->internal->state) { | ||
201 | case SSL_ST_RENEGOTIATE: | ||
202 | s->internal->renegotiate = 1; | ||
203 | /* s->internal->state=SSL_ST_ACCEPT; */ | ||
204 | |||
205 | case SSL_ST_BEFORE: | ||
206 | case SSL_ST_ACCEPT: | ||
207 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
208 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
209 | |||
210 | s->server = 1; | ||
211 | if (cb != NULL) | ||
212 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
213 | |||
214 | if ((s->version >> 8) != 3) { | ||
215 | SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); | ||
216 | ret = -1; | ||
217 | goto end; | ||
218 | } | ||
219 | s->internal->type = SSL_ST_ACCEPT; | ||
220 | |||
221 | if (!ssl3_setup_init_buffer(s)) { | ||
222 | ret = -1; | ||
223 | goto end; | ||
224 | } | ||
225 | if (!ssl3_setup_buffers(s)) { | ||
226 | ret = -1; | ||
227 | goto end; | ||
228 | } | ||
229 | |||
230 | s->internal->init_num = 0; | ||
231 | |||
232 | if (s->internal->state != SSL_ST_RENEGOTIATE) { | ||
233 | /* | ||
234 | * Ok, we now need to push on a buffering BIO | ||
235 | * so that the output is sent in a way that | ||
236 | * TCP likes :-) | ||
237 | */ | ||
238 | if (!ssl_init_wbio_buffer(s, 1)) { | ||
239 | ret = -1; | ||
240 | goto end; | ||
241 | } | ||
242 | |||
243 | if (!tls1_init_finished_mac(s)) { | ||
244 | ret = -1; | ||
245 | goto end; | ||
246 | } | ||
247 | |||
248 | s->internal->state = SSL3_ST_SR_CLNT_HELLO_A; | ||
249 | s->ctx->internal->stats.sess_accept++; | ||
250 | } else if (!S3I(s)->send_connection_binding) { | ||
251 | /* | ||
252 | * Server attempting to renegotiate with | ||
253 | * client that doesn't support secure | ||
254 | * renegotiation. | ||
255 | */ | ||
256 | SSLerr(SSL_F_SSL3_ACCEPT, | ||
257 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | ||
258 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
259 | SSL_AD_HANDSHAKE_FAILURE); | ||
260 | ret = -1; | ||
261 | goto end; | ||
262 | } else { | ||
263 | /* | ||
264 | * s->internal->state == SSL_ST_RENEGOTIATE, | ||
265 | * we will just send a HelloRequest | ||
266 | */ | ||
267 | s->ctx->internal->stats.sess_accept_renegotiate++; | ||
268 | s->internal->state = SSL3_ST_SW_HELLO_REQ_A; | ||
269 | } | ||
270 | break; | ||
271 | |||
272 | case SSL3_ST_SW_HELLO_REQ_A: | ||
273 | case SSL3_ST_SW_HELLO_REQ_B: | ||
274 | |||
275 | s->internal->shutdown = 0; | ||
276 | ret = ssl3_send_hello_request(s); | ||
277 | if (ret <= 0) | ||
278 | goto end; | ||
279 | S3I(s)->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C; | ||
280 | s->internal->state = SSL3_ST_SW_FLUSH; | ||
281 | s->internal->init_num = 0; | ||
282 | |||
283 | if (!tls1_init_finished_mac(s)) { | ||
284 | ret = -1; | ||
285 | goto end; | ||
286 | } | ||
287 | break; | ||
288 | |||
289 | case SSL3_ST_SW_HELLO_REQ_C: | ||
290 | s->internal->state = SSL_ST_OK; | ||
291 | break; | ||
292 | |||
293 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
294 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
295 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
296 | |||
297 | s->internal->shutdown = 0; | ||
298 | if (s->internal->rwstate != SSL_X509_LOOKUP) { | ||
299 | ret = ssl3_get_client_hello(s); | ||
300 | if (ret <= 0) | ||
301 | goto end; | ||
302 | } | ||
303 | |||
304 | s->internal->renegotiate = 2; | ||
305 | s->internal->state = SSL3_ST_SW_SRVR_HELLO_A; | ||
306 | s->internal->init_num = 0; | ||
307 | break; | ||
308 | |||
309 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
310 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
311 | ret = ssl3_send_server_hello(s); | ||
312 | if (ret <= 0) | ||
313 | goto end; | ||
314 | if (s->internal->hit) { | ||
315 | if (s->internal->tlsext_ticket_expected) | ||
316 | s->internal->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
317 | else | ||
318 | s->internal->state = SSL3_ST_SW_CHANGE_A; | ||
319 | } | ||
320 | else | ||
321 | s->internal->state = SSL3_ST_SW_CERT_A; | ||
322 | s->internal->init_num = 0; | ||
323 | break; | ||
324 | |||
325 | case SSL3_ST_SW_CERT_A: | ||
326 | case SSL3_ST_SW_CERT_B: | ||
327 | /* Check if it is anon DH or anon ECDH. */ | ||
328 | if (!(S3I(s)->tmp.new_cipher->algorithm_auth & | ||
329 | SSL_aNULL)) { | ||
330 | ret = ssl3_send_server_certificate(s); | ||
331 | if (ret <= 0) | ||
332 | goto end; | ||
333 | if (s->internal->tlsext_status_expected) | ||
334 | s->internal->state = SSL3_ST_SW_CERT_STATUS_A; | ||
335 | else | ||
336 | s->internal->state = SSL3_ST_SW_KEY_EXCH_A; | ||
337 | } else { | ||
338 | skip = 1; | ||
339 | s->internal->state = SSL3_ST_SW_KEY_EXCH_A; | ||
340 | } | ||
341 | s->internal->init_num = 0; | ||
342 | break; | ||
343 | |||
344 | case SSL3_ST_SW_KEY_EXCH_A: | ||
345 | case SSL3_ST_SW_KEY_EXCH_B: | ||
346 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
347 | |||
348 | /* | ||
349 | * Only send if using a DH key exchange. | ||
350 | * | ||
351 | * For ECC ciphersuites, we send a ServerKeyExchange | ||
352 | * message only if the cipher suite is ECDHE. In other | ||
353 | * cases, the server certificate contains the server's | ||
354 | * public key for key exchange. | ||
355 | */ | ||
356 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
357 | ret = ssl3_send_server_key_exchange(s); | ||
358 | if (ret <= 0) | ||
359 | goto end; | ||
360 | } else | ||
361 | skip = 1; | ||
362 | |||
363 | s->internal->state = SSL3_ST_SW_CERT_REQ_A; | ||
364 | s->internal->init_num = 0; | ||
365 | break; | ||
366 | |||
367 | case SSL3_ST_SW_CERT_REQ_A: | ||
368 | case SSL3_ST_SW_CERT_REQ_B: | ||
369 | /* | ||
370 | * Determine whether or not we need to request a | ||
371 | * certificate. | ||
372 | * | ||
373 | * Do not request a certificate if: | ||
374 | * | ||
375 | * - We did not ask for it (SSL_VERIFY_PEER is unset). | ||
376 | * | ||
377 | * - SSL_VERIFY_CLIENT_ONCE is set and we are | ||
378 | * renegotiating. | ||
379 | * | ||
380 | * - We are using an anonymous ciphersuites | ||
381 | * (see section "Certificate request" in SSL 3 drafts | ||
382 | * and in RFC 2246) ... except when the application | ||
383 | * insists on verification (against the specs, but | ||
384 | * s3_clnt.c accepts this for SSL 3). | ||
385 | */ | ||
386 | if (!(s->verify_mode & SSL_VERIFY_PEER) || | ||
387 | ((s->session->peer != NULL) && | ||
388 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | ||
389 | ((S3I(s)->tmp.new_cipher->algorithm_auth & | ||
390 | SSL_aNULL) && !(s->verify_mode & | ||
391 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { | ||
392 | /* No cert request */ | ||
393 | skip = 1; | ||
394 | S3I(s)->tmp.cert_request = 0; | ||
395 | s->internal->state = SSL3_ST_SW_SRVR_DONE_A; | ||
396 | if (S3I(s)->handshake_buffer) { | ||
397 | if (!tls1_digest_cached_records(s)) { | ||
398 | ret = -1; | ||
399 | goto end; | ||
400 | } | ||
401 | } | ||
402 | } else { | ||
403 | S3I(s)->tmp.cert_request = 1; | ||
404 | ret = ssl3_send_certificate_request(s); | ||
405 | if (ret <= 0) | ||
406 | goto end; | ||
407 | s->internal->state = SSL3_ST_SW_SRVR_DONE_A; | ||
408 | s->internal->init_num = 0; | ||
409 | } | ||
410 | break; | ||
411 | |||
412 | case SSL3_ST_SW_SRVR_DONE_A: | ||
413 | case SSL3_ST_SW_SRVR_DONE_B: | ||
414 | ret = ssl3_send_server_done(s); | ||
415 | if (ret <= 0) | ||
416 | goto end; | ||
417 | S3I(s)->tmp.next_state = SSL3_ST_SR_CERT_A; | ||
418 | s->internal->state = SSL3_ST_SW_FLUSH; | ||
419 | s->internal->init_num = 0; | ||
420 | break; | ||
421 | |||
422 | case SSL3_ST_SW_FLUSH: | ||
423 | |||
424 | /* | ||
425 | * This code originally checked to see if | ||
426 | * any data was pending using BIO_CTRL_INFO | ||
427 | * and then flushed. This caused problems | ||
428 | * as documented in PR#1939. The proposed | ||
429 | * fix doesn't completely resolve this issue | ||
430 | * as buggy implementations of BIO_CTRL_PENDING | ||
431 | * still exist. So instead we just flush | ||
432 | * unconditionally. | ||
433 | */ | ||
434 | |||
435 | s->internal->rwstate = SSL_WRITING; | ||
436 | if (BIO_flush(s->wbio) <= 0) { | ||
437 | ret = -1; | ||
438 | goto end; | ||
439 | } | ||
440 | s->internal->rwstate = SSL_NOTHING; | ||
441 | |||
442 | s->internal->state = S3I(s)->tmp.next_state; | ||
443 | break; | ||
444 | |||
445 | case SSL3_ST_SR_CERT_A: | ||
446 | case SSL3_ST_SR_CERT_B: | ||
447 | if (S3I(s)->tmp.cert_request) { | ||
448 | ret = ssl3_get_client_certificate(s); | ||
449 | if (ret <= 0) | ||
450 | goto end; | ||
451 | } | ||
452 | s->internal->init_num = 0; | ||
453 | s->internal->state = SSL3_ST_SR_KEY_EXCH_A; | ||
454 | break; | ||
455 | |||
456 | case SSL3_ST_SR_KEY_EXCH_A: | ||
457 | case SSL3_ST_SR_KEY_EXCH_B: | ||
458 | ret = ssl3_get_client_key_exchange(s); | ||
459 | if (ret <= 0) | ||
460 | goto end; | ||
461 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
462 | if (ret == 2) { | ||
463 | /* | ||
464 | * For the ECDH ciphersuites when | ||
465 | * the client sends its ECDH pub key in | ||
466 | * a certificate, the CertificateVerify | ||
467 | * message is not sent. | ||
468 | * Also for GOST ciphersuites when | ||
469 | * the client uses its key from the certificate | ||
470 | * for key exchange. | ||
471 | */ | ||
472 | if (S3I(s)->next_proto_neg_seen) | ||
473 | s->internal->state = SSL3_ST_SR_NEXT_PROTO_A; | ||
474 | else | ||
475 | s->internal->state = SSL3_ST_SR_FINISHED_A; | ||
476 | s->internal->init_num = 0; | ||
477 | } else if (SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) { | ||
478 | s->internal->state = SSL3_ST_SR_CERT_VRFY_A; | ||
479 | s->internal->init_num = 0; | ||
480 | if (!s->session->peer) | ||
481 | break; | ||
482 | /* | ||
483 | * For sigalgs freeze the handshake buffer | ||
484 | * at this point and digest cached records. | ||
485 | */ | ||
486 | if (!S3I(s)->handshake_buffer) { | ||
487 | SSLerr(SSL_F_SSL3_ACCEPT, | ||
488 | ERR_R_INTERNAL_ERROR); | ||
489 | ret = -1; | ||
490 | goto end; | ||
491 | } | ||
492 | s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; | ||
493 | if (!tls1_digest_cached_records(s)) { | ||
494 | ret = -1; | ||
495 | goto end; | ||
496 | } | ||
497 | } else { | ||
498 | int offset = 0; | ||
499 | int dgst_num; | ||
500 | |||
501 | s->internal->state = SSL3_ST_SR_CERT_VRFY_A; | ||
502 | s->internal->init_num = 0; | ||
503 | |||
504 | /* | ||
505 | * We need to get hashes here so if there is | ||
506 | * a client cert, it can be verified | ||
507 | * FIXME - digest processing for | ||
508 | * CertificateVerify should be generalized. | ||
509 | * But it is next step | ||
510 | */ | ||
511 | if (S3I(s)->handshake_buffer) { | ||
512 | if (!tls1_digest_cached_records(s)) { | ||
513 | ret = -1; | ||
514 | goto end; | ||
515 | } | ||
516 | } | ||
517 | for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; | ||
518 | dgst_num++) | ||
519 | if (S3I(s)->handshake_dgst[dgst_num]) { | ||
520 | int dgst_size; | ||
521 | |||
522 | s->method->internal->ssl3_enc->cert_verify_mac(s, | ||
523 | EVP_MD_CTX_type( | ||
524 | S3I(s)->handshake_dgst[dgst_num]), | ||
525 | &(S3I(s)->tmp.cert_verify_md[offset])); | ||
526 | dgst_size = EVP_MD_CTX_size( | ||
527 | S3I(s)->handshake_dgst[dgst_num]); | ||
528 | if (dgst_size < 0) { | ||
529 | ret = -1; | ||
530 | goto end; | ||
531 | } | ||
532 | offset += dgst_size; | ||
533 | } | ||
534 | } | ||
535 | break; | ||
536 | |||
537 | case SSL3_ST_SR_CERT_VRFY_A: | ||
538 | case SSL3_ST_SR_CERT_VRFY_B: | ||
539 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
540 | |||
541 | /* we should decide if we expected this one */ | ||
542 | ret = ssl3_get_cert_verify(s); | ||
543 | if (ret <= 0) | ||
544 | goto end; | ||
545 | |||
546 | if (S3I(s)->next_proto_neg_seen) | ||
547 | s->internal->state = SSL3_ST_SR_NEXT_PROTO_A; | ||
548 | else | ||
549 | s->internal->state = SSL3_ST_SR_FINISHED_A; | ||
550 | s->internal->init_num = 0; | ||
551 | break; | ||
552 | |||
553 | case SSL3_ST_SR_NEXT_PROTO_A: | ||
554 | case SSL3_ST_SR_NEXT_PROTO_B: | ||
555 | ret = ssl3_get_next_proto(s); | ||
556 | if (ret <= 0) | ||
557 | goto end; | ||
558 | s->internal->init_num = 0; | ||
559 | s->internal->state = SSL3_ST_SR_FINISHED_A; | ||
560 | break; | ||
561 | |||
562 | case SSL3_ST_SR_FINISHED_A: | ||
563 | case SSL3_ST_SR_FINISHED_B: | ||
564 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
565 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | ||
566 | SSL3_ST_SR_FINISHED_B); | ||
567 | if (ret <= 0) | ||
568 | goto end; | ||
569 | if (s->internal->hit) | ||
570 | s->internal->state = SSL_ST_OK; | ||
571 | else if (s->internal->tlsext_ticket_expected) | ||
572 | s->internal->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
573 | else | ||
574 | s->internal->state = SSL3_ST_SW_CHANGE_A; | ||
575 | s->internal->init_num = 0; | ||
576 | break; | ||
577 | |||
578 | case SSL3_ST_SW_SESSION_TICKET_A: | ||
579 | case SSL3_ST_SW_SESSION_TICKET_B: | ||
580 | ret = ssl3_send_newsession_ticket(s); | ||
581 | if (ret <= 0) | ||
582 | goto end; | ||
583 | s->internal->state = SSL3_ST_SW_CHANGE_A; | ||
584 | s->internal->init_num = 0; | ||
585 | break; | ||
586 | |||
587 | case SSL3_ST_SW_CERT_STATUS_A: | ||
588 | case SSL3_ST_SW_CERT_STATUS_B: | ||
589 | ret = ssl3_send_cert_status(s); | ||
590 | if (ret <= 0) | ||
591 | goto end; | ||
592 | s->internal->state = SSL3_ST_SW_KEY_EXCH_A; | ||
593 | s->internal->init_num = 0; | ||
594 | break; | ||
595 | |||
596 | |||
597 | case SSL3_ST_SW_CHANGE_A: | ||
598 | case SSL3_ST_SW_CHANGE_B: | ||
599 | |||
600 | s->session->cipher = S3I(s)->tmp.new_cipher; | ||
601 | if (!s->method->internal->ssl3_enc->setup_key_block(s)) { | ||
602 | ret = -1; | ||
603 | goto end; | ||
604 | } | ||
605 | |||
606 | ret = ssl3_send_change_cipher_spec(s, | ||
607 | SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); | ||
608 | |||
609 | if (ret <= 0) | ||
610 | goto end; | ||
611 | s->internal->state = SSL3_ST_SW_FINISHED_A; | ||
612 | s->internal->init_num = 0; | ||
613 | |||
614 | if (!s->method->internal->ssl3_enc->change_cipher_state( | ||
615 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | ||
616 | ret = -1; | ||
617 | goto end; | ||
618 | } | ||
619 | |||
620 | break; | ||
621 | |||
622 | case SSL3_ST_SW_FINISHED_A: | ||
623 | case SSL3_ST_SW_FINISHED_B: | ||
624 | ret = ssl3_send_finished(s, | ||
625 | SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, | ||
626 | s->method->internal->ssl3_enc->server_finished_label, | ||
627 | s->method->internal->ssl3_enc->server_finished_label_len); | ||
628 | if (ret <= 0) | ||
629 | goto end; | ||
630 | s->internal->state = SSL3_ST_SW_FLUSH; | ||
631 | if (s->internal->hit) { | ||
632 | if (S3I(s)->next_proto_neg_seen) { | ||
633 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
634 | S3I(s)->tmp.next_state = | ||
635 | SSL3_ST_SR_NEXT_PROTO_A; | ||
636 | } else | ||
637 | S3I(s)->tmp.next_state = | ||
638 | SSL3_ST_SR_FINISHED_A; | ||
639 | } else | ||
640 | S3I(s)->tmp.next_state = SSL_ST_OK; | ||
641 | s->internal->init_num = 0; | ||
642 | break; | ||
643 | |||
644 | case SSL_ST_OK: | ||
645 | /* clean a few things up */ | ||
646 | tls1_cleanup_key_block(s); | ||
647 | |||
648 | BUF_MEM_free(s->internal->init_buf); | ||
649 | s->internal->init_buf = NULL; | ||
650 | |||
651 | /* remove buffering on output */ | ||
652 | ssl_free_wbio_buffer(s); | ||
653 | |||
654 | s->internal->init_num = 0; | ||
655 | |||
656 | /* skipped if we just sent a HelloRequest */ | ||
657 | if (s->internal->renegotiate == 2) { | ||
658 | s->internal->renegotiate = 0; | ||
659 | s->internal->new_session = 0; | ||
660 | |||
661 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | ||
662 | |||
663 | s->ctx->internal->stats.sess_accept_good++; | ||
664 | /* s->server=1; */ | ||
665 | s->internal->handshake_func = ssl3_accept; | ||
666 | |||
667 | if (cb != NULL) | ||
668 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
669 | } | ||
670 | |||
671 | ret = 1; | ||
672 | goto end; | ||
673 | /* break; */ | ||
674 | |||
675 | default: | ||
676 | SSLerr(SSL_F_SSL3_ACCEPT, | ||
677 | SSL_R_UNKNOWN_STATE); | ||
678 | ret = -1; | ||
679 | goto end; | ||
680 | /* break; */ | ||
681 | } | ||
682 | |||
683 | if (!S3I(s)->tmp.reuse_message && !skip) { | ||
684 | if (s->internal->debug) { | ||
685 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
686 | goto end; | ||
687 | } | ||
688 | |||
689 | |||
690 | if ((cb != NULL) && (s->internal->state != state)) { | ||
691 | new_state = s->internal->state; | ||
692 | s->internal->state = state; | ||
693 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | ||
694 | s->internal->state = new_state; | ||
695 | } | ||
696 | } | ||
697 | skip = 0; | ||
698 | } | ||
699 | end: | ||
700 | /* BIO_flush(s->wbio); */ | ||
701 | |||
702 | s->internal->in_handshake--; | ||
703 | if (cb != NULL) | ||
704 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | ||
705 | return (ret); | ||
706 | } | ||
707 | |||
708 | int | ||
709 | ssl3_send_hello_request(SSL *s) | ||
710 | { | ||
711 | if (s->internal->state == SSL3_ST_SW_HELLO_REQ_A) { | ||
712 | ssl3_handshake_msg_start(s, SSL3_MT_HELLO_REQUEST); | ||
713 | ssl3_handshake_msg_finish(s, 0); | ||
714 | |||
715 | s->internal->state = SSL3_ST_SW_HELLO_REQ_B; | ||
716 | } | ||
717 | |||
718 | /* SSL3_ST_SW_HELLO_REQ_B */ | ||
719 | return (ssl3_handshake_write(s)); | ||
720 | } | ||
721 | |||
722 | int | ||
723 | ssl3_get_client_hello(SSL *s) | ||
724 | { | ||
725 | int i, j, ok, al, ret = -1; | ||
726 | unsigned int cookie_len; | ||
727 | long n; | ||
728 | unsigned long id; | ||
729 | unsigned char *p, *d; | ||
730 | SSL_CIPHER *c; | ||
731 | STACK_OF(SSL_CIPHER) *ciphers = NULL; | ||
732 | unsigned long alg_k; | ||
733 | const SSL_METHOD *method; | ||
734 | uint16_t shared_version; | ||
735 | |||
736 | /* | ||
737 | * We do this so that we will respond with our native type. | ||
738 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, | ||
739 | * This down switching should be handled by a different method. | ||
740 | * If we are SSLv3, we will respond with SSLv3, even if prompted with | ||
741 | * TLSv1. | ||
742 | */ | ||
743 | if (s->internal->state == SSL3_ST_SR_CLNT_HELLO_A) { | ||
744 | s->internal->state = SSL3_ST_SR_CLNT_HELLO_B; | ||
745 | } | ||
746 | |||
747 | s->internal->first_packet = 1; | ||
748 | n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CLNT_HELLO_B, | ||
749 | SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO, | ||
750 | SSL3_RT_MAX_PLAIN_LENGTH, &ok); | ||
751 | |||
752 | if (!ok) | ||
753 | return ((int)n); | ||
754 | s->internal->first_packet = 0; | ||
755 | |||
756 | d = p = (unsigned char *)s->internal->init_msg; | ||
757 | |||
758 | if (2 > n) | ||
759 | goto truncated; | ||
760 | /* | ||
761 | * Use version from inside client hello, not from record header. | ||
762 | * (may differ: see RFC 2246, Appendix E, second paragraph) | ||
763 | */ | ||
764 | s->client_version = (((int)p[0]) << 8)|(int)p[1]; | ||
765 | p += 2; | ||
766 | |||
767 | if (ssl_max_shared_version(s, s->client_version, &shared_version) != 1) { | ||
768 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); | ||
769 | if ((s->client_version >> 8) == SSL3_VERSION_MAJOR && | ||
770 | !s->internal->enc_write_ctx && !s->internal->write_hash) { | ||
771 | /* | ||
772 | * Similar to ssl3_get_record, send alert using remote | ||
773 | * version number. | ||
774 | */ | ||
775 | s->version = s->client_version; | ||
776 | } | ||
777 | al = SSL_AD_PROTOCOL_VERSION; | ||
778 | goto f_err; | ||
779 | } | ||
780 | s->version = shared_version; | ||
781 | |||
782 | if ((method = tls1_get_server_method(shared_version)) == NULL) | ||
783 | method = dtls1_get_server_method(shared_version); | ||
784 | if (method == NULL) { | ||
785 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | ||
786 | goto err; | ||
787 | } | ||
788 | s->method = method; | ||
789 | |||
790 | /* | ||
791 | * If we require cookies (DTLS) and this ClientHello doesn't | ||
792 | * contain one, just return since we do not want to | ||
793 | * allocate any memory yet. So check cookie length... | ||
794 | */ | ||
795 | if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { | ||
796 | unsigned int session_length, cookie_length; | ||
797 | |||
798 | if (p - d + SSL3_RANDOM_SIZE + 1 >= n) | ||
799 | goto truncated; | ||
800 | session_length = *(p + SSL3_RANDOM_SIZE); | ||
801 | |||
802 | if (p - d + SSL3_RANDOM_SIZE + session_length + 1 >= n) | ||
803 | goto truncated; | ||
804 | cookie_length = p[SSL3_RANDOM_SIZE + session_length + 1]; | ||
805 | |||
806 | if (cookie_length == 0) | ||
807 | return (1); | ||
808 | } | ||
809 | |||
810 | if (p - d + SSL3_RANDOM_SIZE + 1 > n) | ||
811 | goto truncated; | ||
812 | |||
813 | /* load the client random */ | ||
814 | memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE); | ||
815 | p += SSL3_RANDOM_SIZE; | ||
816 | |||
817 | /* get the session-id */ | ||
818 | j= *(p++); | ||
819 | if (p - d + j > n) | ||
820 | goto truncated; | ||
821 | |||
822 | s->internal->hit = 0; | ||
823 | /* | ||
824 | * Versions before 0.9.7 always allow clients to resume sessions in | ||
825 | * renegotiation. 0.9.7 and later allow this by default, but optionally | ||
826 | * ignore resumption requests with flag | ||
827 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag | ||
828 | * rather than a change to default behavior so that applications | ||
829 | * relying on this for security won't even compile against older | ||
830 | * library versions). | ||
831 | * | ||
832 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() | ||
833 | * to request renegotiation but not a new session (s->internal->new_session | ||
834 | * remains unset): for servers, this essentially just means that the | ||
835 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be | ||
836 | * ignored. | ||
837 | */ | ||
838 | if ((s->internal->new_session && (s->internal->options & | ||
839 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { | ||
840 | if (!ssl_get_new_session(s, 1)) | ||
841 | goto err; | ||
842 | } else { | ||
843 | i = ssl_get_prev_session(s, p, j, d + n); | ||
844 | if (i == 1) { /* previous session */ | ||
845 | s->internal->hit = 1; | ||
846 | } else if (i == -1) | ||
847 | goto err; | ||
848 | else { | ||
849 | /* i == 0 */ | ||
850 | if (!ssl_get_new_session(s, 1)) | ||
851 | goto err; | ||
852 | } | ||
853 | } | ||
854 | |||
855 | p += j; | ||
856 | |||
857 | if (SSL_IS_DTLS(s)) { | ||
858 | /* cookie stuff */ | ||
859 | if (p - d + 1 > n) | ||
860 | goto truncated; | ||
861 | cookie_len = *(p++); | ||
862 | |||
863 | /* | ||
864 | * The ClientHello may contain a cookie even if the | ||
865 | * HelloVerify message has not been sent--make sure that it | ||
866 | * does not cause an overflow. | ||
867 | */ | ||
868 | if (cookie_len > sizeof(D1I(s)->rcvd_cookie)) { | ||
869 | /* too much data */ | ||
870 | al = SSL_AD_DECODE_ERROR; | ||
871 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
872 | SSL_R_COOKIE_MISMATCH); | ||
873 | goto f_err; | ||
874 | } | ||
875 | |||
876 | if (p - d + cookie_len > n) | ||
877 | goto truncated; | ||
878 | |||
879 | /* verify the cookie if appropriate option is set. */ | ||
880 | if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && | ||
881 | cookie_len > 0) { | ||
882 | memcpy(D1I(s)->rcvd_cookie, p, cookie_len); | ||
883 | |||
884 | if (s->ctx->internal->app_verify_cookie_cb != NULL) { | ||
885 | if (s->ctx->internal->app_verify_cookie_cb(s, | ||
886 | D1I(s)->rcvd_cookie, cookie_len) == 0) { | ||
887 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
888 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
889 | SSL_R_COOKIE_MISMATCH); | ||
890 | goto f_err; | ||
891 | } | ||
892 | /* else cookie verification succeeded */ | ||
893 | } else if (timingsafe_memcmp(D1I(s)->rcvd_cookie, D1I(s)->cookie, | ||
894 | D1I(s)->cookie_len) != 0) { | ||
895 | /* default verification */ | ||
896 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
897 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
898 | SSL_R_COOKIE_MISMATCH); | ||
899 | goto f_err; | ||
900 | } | ||
901 | |||
902 | ret = 2; | ||
903 | } | ||
904 | |||
905 | p += cookie_len; | ||
906 | } | ||
907 | |||
908 | if (p - d + 2 > n) | ||
909 | goto truncated; | ||
910 | n2s(p, i); | ||
911 | if ((i == 0) && (j != 0)) { | ||
912 | /* we need a cipher if we are not resuming a session */ | ||
913 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
914 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
915 | SSL_R_NO_CIPHERS_SPECIFIED); | ||
916 | goto f_err; | ||
917 | } | ||
918 | if (p - d + i > n) | ||
919 | goto truncated; | ||
920 | if (i > 0) { | ||
921 | if ((ciphers = ssl_bytes_to_cipher_list(s, p, i)) == NULL) | ||
922 | goto err; | ||
923 | } | ||
924 | p += i; | ||
925 | |||
926 | /* If it is a hit, check that the cipher is in the list */ | ||
927 | if ((s->internal->hit) && (i > 0)) { | ||
928 | j = 0; | ||
929 | id = s->session->cipher->id; | ||
930 | |||
931 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { | ||
932 | c = sk_SSL_CIPHER_value(ciphers, i); | ||
933 | if (c->id == id) { | ||
934 | j = 1; | ||
935 | break; | ||
936 | } | ||
937 | } | ||
938 | if (j == 0) { | ||
939 | /* | ||
940 | * We need to have the cipher in the cipher | ||
941 | * list if we are asked to reuse it | ||
942 | */ | ||
943 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
944 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
945 | SSL_R_REQUIRED_CIPHER_MISSING); | ||
946 | goto f_err; | ||
947 | } | ||
948 | } | ||
949 | |||
950 | /* compression */ | ||
951 | if (p - d + 1 > n) | ||
952 | goto truncated; | ||
953 | i= *(p++); | ||
954 | if (p - d + i > n) | ||
955 | goto truncated; | ||
956 | for (j = 0; j < i; j++) { | ||
957 | if (p[j] == 0) | ||
958 | break; | ||
959 | } | ||
960 | |||
961 | p += i; | ||
962 | if (j >= i) { | ||
963 | /* no compress */ | ||
964 | al = SSL_AD_DECODE_ERROR; | ||
965 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
966 | SSL_R_NO_COMPRESSION_SPECIFIED); | ||
967 | goto f_err; | ||
968 | } | ||
969 | |||
970 | /* TLS extensions*/ | ||
971 | if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) { | ||
972 | /* 'al' set by ssl_parse_clienthello_tlsext */ | ||
973 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT); | ||
974 | goto f_err; | ||
975 | } | ||
976 | if (ssl_check_clienthello_tlsext_early(s) <= 0) { | ||
977 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
978 | SSL_R_CLIENTHELLO_TLSEXT); | ||
979 | goto err; | ||
980 | } | ||
981 | |||
982 | /* | ||
983 | * Check if we want to use external pre-shared secret for this | ||
984 | * handshake for not reused session only. We need to generate | ||
985 | * server_random before calling tls_session_secret_cb in order to allow | ||
986 | * SessionTicket processing to use it in key derivation. | ||
987 | */ | ||
988 | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); | ||
989 | |||
990 | if (!s->internal->hit && s->internal->tls_session_secret_cb) { | ||
991 | SSL_CIPHER *pref_cipher = NULL; | ||
992 | |||
993 | s->session->master_key_length = sizeof(s->session->master_key); | ||
994 | if (s->internal->tls_session_secret_cb(s, s->session->master_key, | ||
995 | &s->session->master_key_length, ciphers, &pref_cipher, | ||
996 | s->internal->tls_session_secret_cb_arg)) { | ||
997 | s->internal->hit = 1; | ||
998 | s->session->ciphers = ciphers; | ||
999 | s->session->verify_result = X509_V_OK; | ||
1000 | |||
1001 | ciphers = NULL; | ||
1002 | |||
1003 | /* check if some cipher was preferred by call back */ | ||
1004 | pref_cipher = pref_cipher ? pref_cipher : | ||
1005 | ssl3_choose_cipher(s, s->session->ciphers, | ||
1006 | SSL_get_ciphers(s)); | ||
1007 | if (pref_cipher == NULL) { | ||
1008 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1009 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
1010 | SSL_R_NO_SHARED_CIPHER); | ||
1011 | goto f_err; | ||
1012 | } | ||
1013 | |||
1014 | s->session->cipher = pref_cipher; | ||
1015 | |||
1016 | sk_SSL_CIPHER_free(s->cipher_list); | ||
1017 | sk_SSL_CIPHER_free(s->internal->cipher_list_by_id); | ||
1018 | |||
1019 | s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); | ||
1020 | s->internal->cipher_list_by_id = | ||
1021 | sk_SSL_CIPHER_dup(s->session->ciphers); | ||
1022 | } | ||
1023 | } | ||
1024 | |||
1025 | /* | ||
1026 | * Given s->session->ciphers and SSL_get_ciphers, we must | ||
1027 | * pick a cipher | ||
1028 | */ | ||
1029 | |||
1030 | if (!s->internal->hit) { | ||
1031 | sk_SSL_CIPHER_free(s->session->ciphers); | ||
1032 | s->session->ciphers = ciphers; | ||
1033 | if (ciphers == NULL) { | ||
1034 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
1035 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
1036 | SSL_R_NO_CIPHERS_PASSED); | ||
1037 | goto f_err; | ||
1038 | } | ||
1039 | ciphers = NULL; | ||
1040 | c = ssl3_choose_cipher(s, s->session->ciphers, | ||
1041 | SSL_get_ciphers(s)); | ||
1042 | |||
1043 | if (c == NULL) { | ||
1044 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1045 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, | ||
1046 | SSL_R_NO_SHARED_CIPHER); | ||
1047 | goto f_err; | ||
1048 | } | ||
1049 | S3I(s)->tmp.new_cipher = c; | ||
1050 | } else { | ||
1051 | S3I(s)->tmp.new_cipher = s->session->cipher; | ||
1052 | } | ||
1053 | |||
1054 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
1055 | if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) || | ||
1056 | !(s->verify_mode & SSL_VERIFY_PEER)) { | ||
1057 | if (!tls1_digest_cached_records(s)) { | ||
1058 | al = SSL_AD_INTERNAL_ERROR; | ||
1059 | goto f_err; | ||
1060 | } | ||
1061 | } | ||
1062 | |||
1063 | /* | ||
1064 | * We now have the following setup. | ||
1065 | * client_random | ||
1066 | * cipher_list - our prefered list of ciphers | ||
1067 | * ciphers - the clients prefered list of ciphers | ||
1068 | * compression - basically ignored right now | ||
1069 | * ssl version is set - sslv3 | ||
1070 | * s->session - The ssl session has been setup. | ||
1071 | * s->internal->hit - session reuse flag | ||
1072 | * s->tmp.new_cipher - the new cipher to use. | ||
1073 | */ | ||
1074 | |||
1075 | /* Handles TLS extensions that we couldn't check earlier */ | ||
1076 | if (ssl_check_clienthello_tlsext_late(s) <= 0) { | ||
1077 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); | ||
1078 | goto err; | ||
1079 | } | ||
1080 | |||
1081 | if (ret < 0) | ||
1082 | ret = 1; | ||
1083 | if (0) { | ||
1084 | truncated: | ||
1085 | al = SSL_AD_DECODE_ERROR; | ||
1086 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_BAD_PACKET_LENGTH); | ||
1087 | f_err: | ||
1088 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1089 | } | ||
1090 | err: | ||
1091 | sk_SSL_CIPHER_free(ciphers); | ||
1092 | |||
1093 | return (ret); | ||
1094 | } | ||
1095 | |||
1096 | int | ||
1097 | ssl3_send_server_hello(SSL *s) | ||
1098 | { | ||
1099 | unsigned char *bufend; | ||
1100 | unsigned char *p, *d; | ||
1101 | CBB cbb, session_id; | ||
1102 | size_t outlen; | ||
1103 | int sl; | ||
1104 | |||
1105 | memset(&cbb, 0, sizeof(cbb)); | ||
1106 | |||
1107 | bufend = (unsigned char *)s->internal->init_buf->data + SSL3_RT_MAX_PLAIN_LENGTH; | ||
1108 | |||
1109 | if (s->internal->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
1110 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_SERVER_HELLO); | ||
1111 | |||
1112 | if (!CBB_init_fixed(&cbb, p, bufend - p)) | ||
1113 | goto err; | ||
1114 | |||
1115 | if (!CBB_add_u16(&cbb, s->version)) | ||
1116 | goto err; | ||
1117 | if (!CBB_add_bytes(&cbb, s->s3->server_random, | ||
1118 | sizeof(s->s3->server_random))) | ||
1119 | goto err; | ||
1120 | |||
1121 | /* | ||
1122 | * There are several cases for the session ID to send | ||
1123 | * back in the server hello: | ||
1124 | * | ||
1125 | * - For session reuse from the session cache, | ||
1126 | * we send back the old session ID. | ||
1127 | * - If stateless session reuse (using a session ticket) | ||
1128 | * is successful, we send back the client's "session ID" | ||
1129 | * (which doesn't actually identify the session). | ||
1130 | * - If it is a new session, we send back the new | ||
1131 | * session ID. | ||
1132 | * - However, if we want the new session to be single-use, | ||
1133 | * we send back a 0-length session ID. | ||
1134 | * | ||
1135 | * s->internal->hit is non-zero in either case of session reuse, | ||
1136 | * so the following won't overwrite an ID that we're supposed | ||
1137 | * to send back. | ||
1138 | */ | ||
1139 | if (!(s->ctx->internal->session_cache_mode & SSL_SESS_CACHE_SERVER) | ||
1140 | && !s->internal->hit) | ||
1141 | s->session->session_id_length = 0; | ||
1142 | |||
1143 | sl = s->session->session_id_length; | ||
1144 | if (sl > (int)sizeof(s->session->session_id)) { | ||
1145 | SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, | ||
1146 | ERR_R_INTERNAL_ERROR); | ||
1147 | goto err; | ||
1148 | } | ||
1149 | |||
1150 | if (!CBB_add_u8_length_prefixed(&cbb, &session_id)) | ||
1151 | goto err; | ||
1152 | if (!CBB_add_bytes(&session_id, s->session->session_id, sl)) | ||
1153 | goto err; | ||
1154 | |||
1155 | /* Cipher suite. */ | ||
1156 | if (!CBB_add_u16(&cbb, | ||
1157 | ssl3_cipher_get_value(S3I(s)->tmp.new_cipher))) | ||
1158 | goto err; | ||
1159 | |||
1160 | /* Compression method. */ | ||
1161 | if (!CBB_add_u8(&cbb, 0)) | ||
1162 | goto err; | ||
1163 | |||
1164 | if (!CBB_finish(&cbb, NULL, &outlen)) | ||
1165 | goto err; | ||
1166 | |||
1167 | if ((p = ssl_add_serverhello_tlsext(s, p + outlen, | ||
1168 | bufend)) == NULL) { | ||
1169 | SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, | ||
1170 | ERR_R_INTERNAL_ERROR); | ||
1171 | goto err; | ||
1172 | } | ||
1173 | |||
1174 | ssl3_handshake_msg_finish(s, p - d); | ||
1175 | } | ||
1176 | |||
1177 | /* SSL3_ST_SW_SRVR_HELLO_B */ | ||
1178 | return (ssl3_handshake_write(s)); | ||
1179 | |||
1180 | err: | ||
1181 | CBB_cleanup(&cbb); | ||
1182 | |||
1183 | return (-1); | ||
1184 | } | ||
1185 | |||
1186 | int | ||
1187 | ssl3_send_server_done(SSL *s) | ||
1188 | { | ||
1189 | if (s->internal->state == SSL3_ST_SW_SRVR_DONE_A) { | ||
1190 | ssl3_handshake_msg_start(s, SSL3_MT_SERVER_DONE); | ||
1191 | ssl3_handshake_msg_finish(s, 0); | ||
1192 | |||
1193 | s->internal->state = SSL3_ST_SW_SRVR_DONE_B; | ||
1194 | } | ||
1195 | |||
1196 | /* SSL3_ST_SW_SRVR_DONE_B */ | ||
1197 | return (ssl3_handshake_write(s)); | ||
1198 | } | ||
1199 | |||
1200 | int | ||
1201 | ssl3_send_server_kex_dhe(SSL *s, CBB *cbb) | ||
1202 | { | ||
1203 | CBB dh_p, dh_g, dh_Ys; | ||
1204 | DH *dh = NULL, *dhp; | ||
1205 | unsigned char *data; | ||
1206 | int al; | ||
1207 | |||
1208 | if (s->cert->dh_tmp_auto != 0) { | ||
1209 | if ((dhp = ssl_get_auto_dh(s)) == NULL) { | ||
1210 | al = SSL_AD_INTERNAL_ERROR; | ||
1211 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1212 | ERR_R_INTERNAL_ERROR); | ||
1213 | goto f_err; | ||
1214 | } | ||
1215 | } else | ||
1216 | dhp = s->cert->dh_tmp; | ||
1217 | |||
1218 | if (dhp == NULL && s->cert->dh_tmp_cb != NULL) | ||
1219 | dhp = s->cert->dh_tmp_cb(s, 0, | ||
1220 | SSL_C_PKEYLENGTH(S3I(s)->tmp.new_cipher)); | ||
1221 | |||
1222 | if (dhp == NULL) { | ||
1223 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1224 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1225 | SSL_R_MISSING_TMP_DH_KEY); | ||
1226 | goto f_err; | ||
1227 | } | ||
1228 | |||
1229 | if (S3I(s)->tmp.dh != NULL) { | ||
1230 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1231 | ERR_R_INTERNAL_ERROR); | ||
1232 | goto err; | ||
1233 | } | ||
1234 | |||
1235 | if (s->cert->dh_tmp_auto != 0) { | ||
1236 | dh = dhp; | ||
1237 | } else if ((dh = DHparams_dup(dhp)) == NULL) { | ||
1238 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
1239 | goto err; | ||
1240 | } | ||
1241 | S3I(s)->tmp.dh = dh; | ||
1242 | if (!DH_generate_key(dh)) { | ||
1243 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
1244 | goto err; | ||
1245 | } | ||
1246 | |||
1247 | /* | ||
1248 | * Serialize the DH parameters and public key. | ||
1249 | */ | ||
1250 | if (!CBB_add_u16_length_prefixed(cbb, &dh_p)) | ||
1251 | goto err; | ||
1252 | if (!CBB_add_space(&dh_p, &data, BN_num_bytes(dh->p))) | ||
1253 | goto err; | ||
1254 | BN_bn2bin(dh->p, data); | ||
1255 | |||
1256 | if (!CBB_add_u16_length_prefixed(cbb, &dh_g)) | ||
1257 | goto err; | ||
1258 | if (!CBB_add_space(&dh_g, &data, BN_num_bytes(dh->g))) | ||
1259 | goto err; | ||
1260 | BN_bn2bin(dh->g, data); | ||
1261 | |||
1262 | if (!CBB_add_u16_length_prefixed(cbb, &dh_Ys)) | ||
1263 | goto err; | ||
1264 | if (!CBB_add_space(&dh_Ys, &data, BN_num_bytes(dh->pub_key))) | ||
1265 | goto err; | ||
1266 | BN_bn2bin(dh->pub_key, data); | ||
1267 | |||
1268 | if (!CBB_flush(cbb)) | ||
1269 | goto err; | ||
1270 | |||
1271 | return (1); | ||
1272 | |||
1273 | f_err: | ||
1274 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1275 | err: | ||
1276 | return (-1); | ||
1277 | } | ||
1278 | |||
1279 | static int | ||
1280 | ssl3_send_server_kex_ecdhe_ecp(SSL *s, int nid, CBB *cbb) | ||
1281 | { | ||
1282 | CBB ecpoint; | ||
1283 | unsigned char *data; | ||
1284 | EC_KEY *ecdh = NULL, *ecdhp; | ||
1285 | const EC_GROUP *group; | ||
1286 | unsigned char *encodedPoint = NULL; | ||
1287 | int encodedlen = 0; | ||
1288 | int curve_id = 0; | ||
1289 | BN_CTX *bn_ctx = NULL; | ||
1290 | int al; | ||
1291 | |||
1292 | ecdhp = s->cert->ecdh_tmp; | ||
1293 | if (s->cert->ecdh_tmp_auto != 0) { | ||
1294 | if (nid != NID_undef) | ||
1295 | ecdhp = EC_KEY_new_by_curve_name(nid); | ||
1296 | } else if (ecdhp == NULL && s->cert->ecdh_tmp_cb != NULL) { | ||
1297 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, | ||
1298 | SSL_C_PKEYLENGTH(S3I(s)->tmp.new_cipher)); | ||
1299 | } | ||
1300 | if (ecdhp == NULL) { | ||
1301 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1302 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1303 | SSL_R_MISSING_TMP_ECDH_KEY); | ||
1304 | goto f_err; | ||
1305 | } | ||
1306 | |||
1307 | if (S3I(s)->tmp.ecdh != NULL) { | ||
1308 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1309 | ERR_R_INTERNAL_ERROR); | ||
1310 | goto err; | ||
1311 | } | ||
1312 | |||
1313 | /* Duplicate the ECDH structure. */ | ||
1314 | if (s->cert->ecdh_tmp_auto != 0) { | ||
1315 | ecdh = ecdhp; | ||
1316 | } else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) { | ||
1317 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1318 | ERR_R_ECDH_LIB); | ||
1319 | goto err; | ||
1320 | } | ||
1321 | S3I(s)->tmp.ecdh = ecdh; | ||
1322 | |||
1323 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || | ||
1324 | (EC_KEY_get0_private_key(ecdh) == NULL) || | ||
1325 | (s->internal->options & SSL_OP_SINGLE_ECDH_USE)) { | ||
1326 | if (!EC_KEY_generate_key(ecdh)) { | ||
1327 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1328 | ERR_R_ECDH_LIB); | ||
1329 | goto err; | ||
1330 | } | ||
1331 | } | ||
1332 | |||
1333 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || | ||
1334 | (EC_KEY_get0_public_key(ecdh) == NULL) || | ||
1335 | (EC_KEY_get0_private_key(ecdh) == NULL)) { | ||
1336 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1337 | ERR_R_ECDH_LIB); | ||
1338 | goto err; | ||
1339 | } | ||
1340 | |||
1341 | /* | ||
1342 | * Only named curves are supported in ECDH ephemeral key exchanges. | ||
1343 | * For supported named curves, curve_id is non-zero. | ||
1344 | */ | ||
1345 | if ((curve_id = tls1_ec_nid2curve_id( | ||
1346 | EC_GROUP_get_curve_name(group))) == 0) { | ||
1347 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1348 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | ||
1349 | goto err; | ||
1350 | } | ||
1351 | |||
1352 | /* | ||
1353 | * Encode the public key. First check the size of encoding and | ||
1354 | * allocate memory accordingly. | ||
1355 | */ | ||
1356 | encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), | ||
1357 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); | ||
1358 | |||
1359 | encodedPoint = malloc(encodedlen); | ||
1360 | |||
1361 | bn_ctx = BN_CTX_new(); | ||
1362 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { | ||
1363 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1364 | ERR_R_MALLOC_FAILURE); | ||
1365 | goto err; | ||
1366 | } | ||
1367 | |||
1368 | encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), | ||
1369 | POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encodedlen, bn_ctx); | ||
1370 | |||
1371 | if (encodedlen == 0) { | ||
1372 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
1373 | goto err; | ||
1374 | } | ||
1375 | |||
1376 | BN_CTX_free(bn_ctx); | ||
1377 | bn_ctx = NULL; | ||
1378 | |||
1379 | /* | ||
1380 | * Only named curves are supported in ECDH ephemeral key exchanges. | ||
1381 | * In this case the ServerKeyExchange message has: | ||
1382 | * [1 byte CurveType], [2 byte CurveName] | ||
1383 | * [1 byte length of encoded point], followed by | ||
1384 | * the actual encoded point itself. | ||
1385 | */ | ||
1386 | if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) | ||
1387 | goto err; | ||
1388 | if (!CBB_add_u16(cbb, curve_id)) | ||
1389 | goto err; | ||
1390 | if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) | ||
1391 | goto err; | ||
1392 | if (!CBB_add_space(&ecpoint, &data, encodedlen)) | ||
1393 | goto err; | ||
1394 | |||
1395 | memcpy(data, encodedPoint, encodedlen); | ||
1396 | |||
1397 | free(encodedPoint); | ||
1398 | encodedPoint = NULL; | ||
1399 | |||
1400 | if (!CBB_flush(cbb)) | ||
1401 | goto err; | ||
1402 | |||
1403 | return (1); | ||
1404 | |||
1405 | f_err: | ||
1406 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1407 | err: | ||
1408 | free(encodedPoint); | ||
1409 | BN_CTX_free(bn_ctx); | ||
1410 | |||
1411 | return (-1); | ||
1412 | } | ||
1413 | |||
1414 | static int | ||
1415 | ssl3_send_server_kex_ecdhe_ecx(SSL *s, int nid, CBB *cbb) | ||
1416 | { | ||
1417 | uint8_t *public_key = NULL; | ||
1418 | int curve_id; | ||
1419 | CBB ecpoint; | ||
1420 | int ret = -1; | ||
1421 | |||
1422 | /* Generate an X25519 key pair. */ | ||
1423 | if (S3I(s)->tmp.x25519 != NULL) { | ||
1424 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1425 | ERR_R_INTERNAL_ERROR); | ||
1426 | goto err; | ||
1427 | } | ||
1428 | if ((S3I(s)->tmp.x25519 = malloc(X25519_KEY_LENGTH)) == NULL) | ||
1429 | goto err; | ||
1430 | if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
1431 | goto err; | ||
1432 | X25519_keypair(public_key, S3I(s)->tmp.x25519); | ||
1433 | |||
1434 | /* Serialize public key. */ | ||
1435 | if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) { | ||
1436 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1437 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); | ||
1438 | goto err; | ||
1439 | } | ||
1440 | |||
1441 | if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) | ||
1442 | goto err; | ||
1443 | if (!CBB_add_u16(cbb, curve_id)) | ||
1444 | goto err; | ||
1445 | if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) | ||
1446 | goto err; | ||
1447 | if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH)) | ||
1448 | goto err; | ||
1449 | if (!CBB_flush(cbb)) | ||
1450 | goto err; | ||
1451 | |||
1452 | ret = 1; | ||
1453 | |||
1454 | err: | ||
1455 | free(public_key); | ||
1456 | |||
1457 | return (ret); | ||
1458 | } | ||
1459 | |||
1460 | static int | ||
1461 | ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb) | ||
1462 | { | ||
1463 | int nid; | ||
1464 | |||
1465 | nid = tls1_get_shared_curve(s); | ||
1466 | |||
1467 | if (s->cert->ecdh_tmp_auto != 0 && nid == NID_X25519) | ||
1468 | return ssl3_send_server_kex_ecdhe_ecx(s, nid, cbb); | ||
1469 | |||
1470 | return ssl3_send_server_kex_ecdhe_ecp(s, nid, cbb); | ||
1471 | } | ||
1472 | |||
1473 | int | ||
1474 | ssl3_send_server_key_exchange(SSL *s) | ||
1475 | { | ||
1476 | CBB cbb; | ||
1477 | unsigned char *params = NULL; | ||
1478 | size_t params_len; | ||
1479 | unsigned char *q; | ||
1480 | int j, num; | ||
1481 | unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | ||
1482 | unsigned int u; | ||
1483 | EVP_PKEY *pkey; | ||
1484 | const EVP_MD *md = NULL; | ||
1485 | unsigned char *p, *d; | ||
1486 | int al, i; | ||
1487 | unsigned long type; | ||
1488 | int n; | ||
1489 | int kn; | ||
1490 | BUF_MEM *buf; | ||
1491 | EVP_MD_CTX md_ctx; | ||
1492 | |||
1493 | memset(&cbb, 0, sizeof(cbb)); | ||
1494 | |||
1495 | EVP_MD_CTX_init(&md_ctx); | ||
1496 | if (s->internal->state == SSL3_ST_SW_KEY_EXCH_A) { | ||
1497 | type = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
1498 | |||
1499 | buf = s->internal->init_buf; | ||
1500 | |||
1501 | if (!CBB_init(&cbb, 0)) | ||
1502 | goto err; | ||
1503 | |||
1504 | if (type & SSL_kDHE) { | ||
1505 | if (ssl3_send_server_kex_dhe(s, &cbb) != 1) | ||
1506 | goto err; | ||
1507 | } else if (type & SSL_kECDHE) { | ||
1508 | if (ssl3_send_server_kex_ecdhe(s, &cbb) != 1) | ||
1509 | goto err; | ||
1510 | } else { | ||
1511 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1512 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1513 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | ||
1514 | goto f_err; | ||
1515 | } | ||
1516 | |||
1517 | if (!CBB_finish(&cbb, ¶ms, ¶ms_len)) | ||
1518 | goto err; | ||
1519 | |||
1520 | if (!(S3I(s)->tmp.new_cipher->algorithm_auth & SSL_aNULL)) { | ||
1521 | if ((pkey = ssl_get_sign_pkey( | ||
1522 | s, S3I(s)->tmp.new_cipher, &md)) == NULL) { | ||
1523 | al = SSL_AD_DECODE_ERROR; | ||
1524 | goto f_err; | ||
1525 | } | ||
1526 | kn = EVP_PKEY_size(pkey); | ||
1527 | } else { | ||
1528 | pkey = NULL; | ||
1529 | kn = 0; | ||
1530 | } | ||
1531 | |||
1532 | if (!BUF_MEM_grow_clean(buf, ssl3_handshake_msg_hdr_len(s) + | ||
1533 | params_len + kn)) { | ||
1534 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1535 | ERR_LIB_BUF); | ||
1536 | goto err; | ||
1537 | } | ||
1538 | |||
1539 | d = p = ssl3_handshake_msg_start(s, | ||
1540 | SSL3_MT_SERVER_KEY_EXCHANGE); | ||
1541 | |||
1542 | memcpy(p, params, params_len); | ||
1543 | |||
1544 | free(params); | ||
1545 | params = NULL; | ||
1546 | |||
1547 | n = params_len; | ||
1548 | p += params_len; | ||
1549 | |||
1550 | /* not anonymous */ | ||
1551 | if (pkey != NULL) { | ||
1552 | /* | ||
1553 | * n is the length of the params, they start at &(d[4]) | ||
1554 | * and p points to the space at the end. | ||
1555 | */ | ||
1556 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { | ||
1557 | q = md_buf; | ||
1558 | j = 0; | ||
1559 | for (num = 2; num > 0; num--) { | ||
1560 | if (!EVP_DigestInit_ex(&md_ctx, | ||
1561 | (num == 2) ? s->ctx->internal->md5 : | ||
1562 | s->ctx->internal->sha1, NULL)) | ||
1563 | goto err; | ||
1564 | EVP_DigestUpdate(&md_ctx, | ||
1565 | s->s3->client_random, | ||
1566 | SSL3_RANDOM_SIZE); | ||
1567 | EVP_DigestUpdate(&md_ctx, | ||
1568 | s->s3->server_random, | ||
1569 | SSL3_RANDOM_SIZE); | ||
1570 | EVP_DigestUpdate(&md_ctx, d, n); | ||
1571 | EVP_DigestFinal_ex(&md_ctx, q, | ||
1572 | (unsigned int *)&i); | ||
1573 | q += i; | ||
1574 | j += i; | ||
1575 | } | ||
1576 | if (RSA_sign(NID_md5_sha1, md_buf, j, | ||
1577 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { | ||
1578 | SSLerr( | ||
1579 | SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1580 | ERR_LIB_RSA); | ||
1581 | goto err; | ||
1582 | } | ||
1583 | s2n(u, p); | ||
1584 | n += u + 2; | ||
1585 | } else if (md) { | ||
1586 | /* Send signature algorithm. */ | ||
1587 | if (SSL_USE_SIGALGS(s)) { | ||
1588 | if (!tls12_get_sigandhash(p, pkey, md)) { | ||
1589 | /* Should never happen */ | ||
1590 | al = SSL_AD_INTERNAL_ERROR; | ||
1591 | SSLerr( | ||
1592 | SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1593 | ERR_R_INTERNAL_ERROR); | ||
1594 | goto f_err; | ||
1595 | } | ||
1596 | p += 2; | ||
1597 | } | ||
1598 | EVP_SignInit_ex(&md_ctx, md, NULL); | ||
1599 | EVP_SignUpdate(&md_ctx, | ||
1600 | s->s3->client_random, | ||
1601 | SSL3_RANDOM_SIZE); | ||
1602 | EVP_SignUpdate(&md_ctx, | ||
1603 | s->s3->server_random, | ||
1604 | SSL3_RANDOM_SIZE); | ||
1605 | EVP_SignUpdate(&md_ctx, d, n); | ||
1606 | if (!EVP_SignFinal(&md_ctx, &p[2], | ||
1607 | (unsigned int *)&i, pkey)) { | ||
1608 | SSLerr( | ||
1609 | SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1610 | ERR_LIB_EVP); | ||
1611 | goto err; | ||
1612 | } | ||
1613 | s2n(i, p); | ||
1614 | n += i + 2; | ||
1615 | if (SSL_USE_SIGALGS(s)) | ||
1616 | n += 2; | ||
1617 | } else { | ||
1618 | /* Is this error check actually needed? */ | ||
1619 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1620 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
1621 | SSL_R_UNKNOWN_PKEY_TYPE); | ||
1622 | goto f_err; | ||
1623 | } | ||
1624 | } | ||
1625 | |||
1626 | ssl3_handshake_msg_finish(s, n); | ||
1627 | } | ||
1628 | |||
1629 | s->internal->state = SSL3_ST_SW_KEY_EXCH_B; | ||
1630 | |||
1631 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1632 | |||
1633 | return (ssl3_handshake_write(s)); | ||
1634 | |||
1635 | f_err: | ||
1636 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1637 | err: | ||
1638 | free(params); | ||
1639 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1640 | CBB_cleanup(&cbb); | ||
1641 | |||
1642 | return (-1); | ||
1643 | } | ||
1644 | |||
1645 | int | ||
1646 | ssl3_send_certificate_request(SSL *s) | ||
1647 | { | ||
1648 | unsigned char *p, *d; | ||
1649 | int i, j, nl, off, n; | ||
1650 | STACK_OF(X509_NAME) *sk = NULL; | ||
1651 | X509_NAME *name; | ||
1652 | BUF_MEM *buf; | ||
1653 | |||
1654 | if (s->internal->state == SSL3_ST_SW_CERT_REQ_A) { | ||
1655 | buf = s->internal->init_buf; | ||
1656 | |||
1657 | d = p = ssl3_handshake_msg_start(s, | ||
1658 | SSL3_MT_CERTIFICATE_REQUEST); | ||
1659 | |||
1660 | /* get the list of acceptable cert types */ | ||
1661 | p++; | ||
1662 | n = ssl3_get_req_cert_type(s, p); | ||
1663 | d[0] = n; | ||
1664 | p += n; | ||
1665 | n++; | ||
1666 | |||
1667 | if (SSL_USE_SIGALGS(s)) { | ||
1668 | nl = tls12_get_req_sig_algs(s, p + 2); | ||
1669 | s2n(nl, p); | ||
1670 | p += nl + 2; | ||
1671 | n += nl + 2; | ||
1672 | } | ||
1673 | |||
1674 | off = n; | ||
1675 | p += 2; | ||
1676 | n += 2; | ||
1677 | |||
1678 | sk = SSL_get_client_CA_list(s); | ||
1679 | nl = 0; | ||
1680 | if (sk != NULL) { | ||
1681 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | ||
1682 | name = sk_X509_NAME_value(sk, i); | ||
1683 | j = i2d_X509_NAME(name, NULL); | ||
1684 | if (!BUF_MEM_grow_clean(buf, | ||
1685 | ssl3_handshake_msg_hdr_len(s) + n + j | ||
1686 | + 2)) { | ||
1687 | SSLerr( | ||
1688 | SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, | ||
1689 | ERR_R_BUF_LIB); | ||
1690 | goto err; | ||
1691 | } | ||
1692 | p = ssl3_handshake_msg_start(s, | ||
1693 | SSL3_MT_CERTIFICATE_REQUEST) + n; | ||
1694 | s2n(j, p); | ||
1695 | i2d_X509_NAME(name, &p); | ||
1696 | n += 2 + j; | ||
1697 | nl += 2 + j; | ||
1698 | } | ||
1699 | } | ||
1700 | /* else no CA names */ | ||
1701 | p = ssl3_handshake_msg_start(s, | ||
1702 | SSL3_MT_CERTIFICATE_REQUEST) + off; | ||
1703 | s2n(nl, p); | ||
1704 | |||
1705 | ssl3_handshake_msg_finish(s, n); | ||
1706 | |||
1707 | s->internal->state = SSL3_ST_SW_CERT_REQ_B; | ||
1708 | } | ||
1709 | |||
1710 | /* SSL3_ST_SW_CERT_REQ_B */ | ||
1711 | return (ssl3_handshake_write(s)); | ||
1712 | err: | ||
1713 | return (-1); | ||
1714 | } | ||
1715 | |||
1716 | static int | ||
1717 | ssl3_get_client_kex_rsa(SSL *s, unsigned char *p, long n) | ||
1718 | { | ||
1719 | unsigned char fakekey[SSL_MAX_MASTER_KEY_LENGTH]; | ||
1720 | unsigned char *d; | ||
1721 | RSA *rsa = NULL; | ||
1722 | EVP_PKEY *pkey = NULL; | ||
1723 | int i, al; | ||
1724 | |||
1725 | d = p; | ||
1726 | |||
1727 | arc4random_buf(fakekey, sizeof(fakekey)); | ||
1728 | fakekey[0] = s->client_version >> 8; | ||
1729 | fakekey[1] = s->client_version & 0xff; | ||
1730 | |||
1731 | pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; | ||
1732 | if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) || | ||
1733 | (pkey->pkey.rsa == NULL)) { | ||
1734 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1735 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1736 | SSL_R_MISSING_RSA_CERTIFICATE); | ||
1737 | goto f_err; | ||
1738 | } | ||
1739 | rsa = pkey->pkey.rsa; | ||
1740 | |||
1741 | if (2 > n) | ||
1742 | goto truncated; | ||
1743 | n2s(p, i); | ||
1744 | if (n != i + 2) { | ||
1745 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1746 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); | ||
1747 | goto err; | ||
1748 | } else | ||
1749 | n = i; | ||
1750 | |||
1751 | i = RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING); | ||
1752 | |||
1753 | ERR_clear_error(); | ||
1754 | |||
1755 | al = -1; | ||
1756 | |||
1757 | if (i != SSL_MAX_MASTER_KEY_LENGTH) { | ||
1758 | al = SSL_AD_DECODE_ERROR; | ||
1759 | /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */ | ||
1760 | } | ||
1761 | |||
1762 | if (p - d + 2 > n) /* needed in the SSL3 case */ | ||
1763 | goto truncated; | ||
1764 | if ((al == -1) && !((p[0] == (s->client_version >> 8)) && | ||
1765 | (p[1] == (s->client_version & 0xff)))) { | ||
1766 | /* | ||
1767 | * The premaster secret must contain the same version | ||
1768 | * number as the ClientHello to detect version rollback | ||
1769 | * attacks (strangely, the protocol does not offer such | ||
1770 | * protection for DH ciphersuites). | ||
1771 | * However, buggy clients exist that send the negotiated | ||
1772 | * protocol version instead if the server does not | ||
1773 | * support the requested protocol version. | ||
1774 | * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such | ||
1775 | * clients. | ||
1776 | */ | ||
1777 | if (!((s->internal->options & SSL_OP_TLS_ROLLBACK_BUG) && | ||
1778 | (p[0] == (s->version >> 8)) && | ||
1779 | (p[1] == (s->version & 0xff)))) { | ||
1780 | al = SSL_AD_DECODE_ERROR; | ||
1781 | /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */ | ||
1782 | |||
1783 | /* | ||
1784 | * The Klima-Pokorny-Rosa extension of | ||
1785 | * Bleichenbacher's attack | ||
1786 | * (http://eprint.iacr.org/2003/052/) exploits | ||
1787 | * the version number check as a "bad version | ||
1788 | * oracle" -- an alert would reveal that the | ||
1789 | * plaintext corresponding to some ciphertext | ||
1790 | * made up by the adversary is properly | ||
1791 | * formatted except that the version number is | ||
1792 | * wrong. | ||
1793 | * To avoid such attacks, we should treat this | ||
1794 | * just like any other decryption error. | ||
1795 | */ | ||
1796 | } | ||
1797 | } | ||
1798 | |||
1799 | if (al != -1) { | ||
1800 | /* | ||
1801 | * Some decryption failure -- use random value instead | ||
1802 | * as countermeasure against Bleichenbacher's attack | ||
1803 | * on PKCS #1 v1.5 RSA padding (see RFC 2246, | ||
1804 | * section 7.4.7.1). | ||
1805 | */ | ||
1806 | i = SSL_MAX_MASTER_KEY_LENGTH; | ||
1807 | p = fakekey; | ||
1808 | } | ||
1809 | |||
1810 | s->session->master_key_length = | ||
1811 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
1812 | s->session->master_key, p, i); | ||
1813 | |||
1814 | explicit_bzero(p, i); | ||
1815 | |||
1816 | return (1); | ||
1817 | truncated: | ||
1818 | al = SSL_AD_DECODE_ERROR; | ||
1819 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1820 | f_err: | ||
1821 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1822 | err: | ||
1823 | return (-1); | ||
1824 | } | ||
1825 | |||
1826 | static int | ||
1827 | ssl3_get_client_kex_dhe(SSL *s, unsigned char *p, long n) | ||
1828 | { | ||
1829 | BIGNUM *bn = NULL; | ||
1830 | int key_size, al; | ||
1831 | CBS cbs, dh_Yc; | ||
1832 | DH *dh; | ||
1833 | |||
1834 | if (n < 0) | ||
1835 | goto err; | ||
1836 | |||
1837 | CBS_init(&cbs, p, n); | ||
1838 | |||
1839 | if (!CBS_get_u16_length_prefixed(&cbs, &dh_Yc)) | ||
1840 | goto truncated; | ||
1841 | |||
1842 | if (CBS_len(&cbs) != 0) | ||
1843 | goto truncated; | ||
1844 | |||
1845 | if (S3I(s)->tmp.dh == NULL) { | ||
1846 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1847 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1848 | SSL_R_MISSING_TMP_DH_KEY); | ||
1849 | goto f_err; | ||
1850 | } | ||
1851 | dh = S3I(s)->tmp.dh; | ||
1852 | |||
1853 | if ((bn = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL)) == NULL) { | ||
1854 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1855 | SSL_R_BN_LIB); | ||
1856 | goto err; | ||
1857 | } | ||
1858 | |||
1859 | key_size = DH_compute_key(p, bn, dh); | ||
1860 | if (key_size <= 0) { | ||
1861 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
1862 | BN_clear_free(bn); | ||
1863 | goto err; | ||
1864 | } | ||
1865 | |||
1866 | s->session->master_key_length = | ||
1867 | s->method->internal->ssl3_enc->generate_master_secret( | ||
1868 | s, s->session->master_key, p, key_size); | ||
1869 | |||
1870 | explicit_bzero(p, key_size); | ||
1871 | |||
1872 | DH_free(S3I(s)->tmp.dh); | ||
1873 | S3I(s)->tmp.dh = NULL; | ||
1874 | |||
1875 | BN_clear_free(bn); | ||
1876 | |||
1877 | return (1); | ||
1878 | |||
1879 | truncated: | ||
1880 | al = SSL_AD_DECODE_ERROR; | ||
1881 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1882 | f_err: | ||
1883 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1884 | err: | ||
1885 | return (-1); | ||
1886 | } | ||
1887 | |||
1888 | static int | ||
1889 | ssl3_get_client_kex_ecdhe_ecp(SSL *s, unsigned char *p, long n) | ||
1890 | { | ||
1891 | EC_KEY *srvr_ecdh = NULL; | ||
1892 | EVP_PKEY *clnt_pub_pkey = NULL; | ||
1893 | EC_POINT *clnt_ecpoint = NULL; | ||
1894 | BN_CTX *bn_ctx = NULL; | ||
1895 | int i, al; | ||
1896 | |||
1897 | int ret = 1; | ||
1898 | int key_size; | ||
1899 | const EC_KEY *tkey; | ||
1900 | const EC_GROUP *group; | ||
1901 | const BIGNUM *priv_key; | ||
1902 | |||
1903 | /* Initialize structures for server's ECDH key pair. */ | ||
1904 | if ((srvr_ecdh = EC_KEY_new()) == NULL) { | ||
1905 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1906 | ERR_R_MALLOC_FAILURE); | ||
1907 | goto err; | ||
1908 | } | ||
1909 | |||
1910 | /* | ||
1911 | * Use the ephemeral values we saved when | ||
1912 | * generating the ServerKeyExchange message. | ||
1913 | */ | ||
1914 | tkey = S3I(s)->tmp.ecdh; | ||
1915 | |||
1916 | group = EC_KEY_get0_group(tkey); | ||
1917 | priv_key = EC_KEY_get0_private_key(tkey); | ||
1918 | |||
1919 | if (!EC_KEY_set_group(srvr_ecdh, group) || | ||
1920 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) { | ||
1921 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1922 | ERR_R_EC_LIB); | ||
1923 | goto err; | ||
1924 | } | ||
1925 | |||
1926 | /* Let's get client's public key */ | ||
1927 | if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) { | ||
1928 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1929 | ERR_R_MALLOC_FAILURE); | ||
1930 | goto err; | ||
1931 | } | ||
1932 | |||
1933 | if (n == 0L) { | ||
1934 | /* Client Publickey was in Client Certificate */ | ||
1935 | if (((clnt_pub_pkey = X509_get_pubkey( | ||
1936 | s->session->peer)) == NULL) || | ||
1937 | (clnt_pub_pkey->type != EVP_PKEY_EC)) { | ||
1938 | /* | ||
1939 | * XXX: For now, we do not support client | ||
1940 | * authentication using ECDH certificates | ||
1941 | * so this branch (n == 0L) of the code is | ||
1942 | * never executed. When that support is | ||
1943 | * added, we ought to ensure the key | ||
1944 | * received in the certificate is | ||
1945 | * authorized for key agreement. | ||
1946 | * ECDH_compute_key implicitly checks that | ||
1947 | * the two ECDH shares are for the same | ||
1948 | * group. | ||
1949 | */ | ||
1950 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1951 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1952 | SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); | ||
1953 | goto f_err; | ||
1954 | } | ||
1955 | |||
1956 | if (EC_POINT_copy(clnt_ecpoint, | ||
1957 | EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) | ||
1958 | == 0) { | ||
1959 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1960 | ERR_R_EC_LIB); | ||
1961 | goto err; | ||
1962 | } | ||
1963 | ret = 2; /* Skip certificate verify processing */ | ||
1964 | } else { | ||
1965 | /* | ||
1966 | * Get client's public key from encoded point | ||
1967 | * in the ClientKeyExchange message. | ||
1968 | */ | ||
1969 | if ((bn_ctx = BN_CTX_new()) == NULL) { | ||
1970 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1971 | ERR_R_MALLOC_FAILURE); | ||
1972 | goto err; | ||
1973 | } | ||
1974 | |||
1975 | /* Get encoded point length */ | ||
1976 | i = *p; | ||
1977 | |||
1978 | p += 1; | ||
1979 | if (n != 1 + i) { | ||
1980 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1981 | ERR_R_EC_LIB); | ||
1982 | goto err; | ||
1983 | } | ||
1984 | if (EC_POINT_oct2point(group, | ||
1985 | clnt_ecpoint, p, i, bn_ctx) == 0) { | ||
1986 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
1987 | ERR_R_EC_LIB); | ||
1988 | goto err; | ||
1989 | } | ||
1990 | /* | ||
1991 | * p is pointing to somewhere in the buffer | ||
1992 | * currently, so set it to the start. | ||
1993 | */ | ||
1994 | p = (unsigned char *)s->internal->init_buf->data; | ||
1995 | } | ||
1996 | |||
1997 | /* Compute the shared pre-master secret */ | ||
1998 | key_size = ECDH_size(srvr_ecdh); | ||
1999 | if (key_size <= 0) { | ||
2000 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
2001 | ERR_R_ECDH_LIB); | ||
2002 | goto err; | ||
2003 | } | ||
2004 | i = ECDH_compute_key(p, key_size, clnt_ecpoint, srvr_ecdh, | ||
2005 | NULL); | ||
2006 | if (i <= 0) { | ||
2007 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
2008 | ERR_R_ECDH_LIB); | ||
2009 | goto err; | ||
2010 | } | ||
2011 | |||
2012 | EVP_PKEY_free(clnt_pub_pkey); | ||
2013 | EC_POINT_free(clnt_ecpoint); | ||
2014 | EC_KEY_free(srvr_ecdh); | ||
2015 | BN_CTX_free(bn_ctx); | ||
2016 | EC_KEY_free(S3I(s)->tmp.ecdh); | ||
2017 | S3I(s)->tmp.ecdh = NULL; | ||
2018 | |||
2019 | /* Compute the master secret */ | ||
2020 | s->session->master_key_length = | ||
2021 | s->method->internal->ssl3_enc->generate_master_secret( | ||
2022 | s, s->session->master_key, p, i); | ||
2023 | |||
2024 | explicit_bzero(p, i); | ||
2025 | return (ret); | ||
2026 | |||
2027 | f_err: | ||
2028 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2029 | err: | ||
2030 | EVP_PKEY_free(clnt_pub_pkey); | ||
2031 | EC_POINT_free(clnt_ecpoint); | ||
2032 | EC_KEY_free(srvr_ecdh); | ||
2033 | BN_CTX_free(bn_ctx); | ||
2034 | return (-1); | ||
2035 | } | ||
2036 | |||
2037 | static int | ||
2038 | ssl3_get_client_kex_ecdhe_ecx(SSL *s, unsigned char *p, long n) | ||
2039 | { | ||
2040 | uint8_t *shared_key = NULL; | ||
2041 | CBS cbs, ecpoint; | ||
2042 | int ret = -1; | ||
2043 | |||
2044 | if (n < 0) | ||
2045 | goto err; | ||
2046 | |||
2047 | CBS_init(&cbs, p, n); | ||
2048 | if (!CBS_get_u8_length_prefixed(&cbs, &ecpoint)) | ||
2049 | goto err; | ||
2050 | if (CBS_len(&ecpoint) != X25519_KEY_LENGTH) | ||
2051 | goto err; | ||
2052 | |||
2053 | if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
2054 | goto err; | ||
2055 | if (!X25519(shared_key, S3I(s)->tmp.x25519, CBS_data(&ecpoint))) | ||
2056 | goto err; | ||
2057 | |||
2058 | explicit_bzero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH); | ||
2059 | free(S3I(s)->tmp.x25519); | ||
2060 | S3I(s)->tmp.x25519 = NULL; | ||
2061 | |||
2062 | s->session->master_key_length = | ||
2063 | s->method->internal->ssl3_enc->generate_master_secret( | ||
2064 | s, s->session->master_key, shared_key, X25519_KEY_LENGTH); | ||
2065 | |||
2066 | ret = 1; | ||
2067 | |||
2068 | err: | ||
2069 | if (shared_key != NULL) | ||
2070 | explicit_bzero(shared_key, X25519_KEY_LENGTH); | ||
2071 | free(shared_key); | ||
2072 | |||
2073 | return (ret); | ||
2074 | } | ||
2075 | |||
2076 | static int | ||
2077 | ssl3_get_client_kex_ecdhe(SSL *s, unsigned char *p, long n) | ||
2078 | { | ||
2079 | if (S3I(s)->tmp.x25519 != NULL) | ||
2080 | return ssl3_get_client_kex_ecdhe_ecx(s, p, n); | ||
2081 | |||
2082 | return ssl3_get_client_kex_ecdhe_ecp(s, p, n); | ||
2083 | } | ||
2084 | |||
2085 | static int | ||
2086 | ssl3_get_client_kex_gost(SSL *s, unsigned char *p, long n) | ||
2087 | { | ||
2088 | |||
2089 | EVP_PKEY_CTX *pkey_ctx; | ||
2090 | EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; | ||
2091 | unsigned char premaster_secret[32], *start; | ||
2092 | size_t outlen = 32, inlen; | ||
2093 | unsigned long alg_a; | ||
2094 | int Ttag, Tclass; | ||
2095 | long Tlen; | ||
2096 | int al; | ||
2097 | int ret = 0; | ||
2098 | |||
2099 | /* Get our certificate private key*/ | ||
2100 | alg_a = S3I(s)->tmp.new_cipher->algorithm_auth; | ||
2101 | if (alg_a & SSL_aGOST01) | ||
2102 | pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; | ||
2103 | |||
2104 | pkey_ctx = EVP_PKEY_CTX_new(pk, NULL); | ||
2105 | EVP_PKEY_decrypt_init(pkey_ctx); | ||
2106 | /* | ||
2107 | * If client certificate is present and is of the same type, | ||
2108 | * maybe use it for key exchange. | ||
2109 | * Don't mind errors from EVP_PKEY_derive_set_peer, because | ||
2110 | * it is completely valid to use a client certificate for | ||
2111 | * authorization only. | ||
2112 | */ | ||
2113 | client_pub_pkey = X509_get_pubkey(s->session->peer); | ||
2114 | if (client_pub_pkey) { | ||
2115 | if (EVP_PKEY_derive_set_peer(pkey_ctx, | ||
2116 | client_pub_pkey) <= 0) | ||
2117 | ERR_clear_error(); | ||
2118 | } | ||
2119 | if (2 > n) | ||
2120 | goto truncated; | ||
2121 | /* Decrypt session key */ | ||
2122 | if (ASN1_get_object((const unsigned char **)&p, &Tlen, &Ttag, | ||
2123 | &Tclass, n) != V_ASN1_CONSTRUCTED || | ||
2124 | Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) { | ||
2125 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
2126 | SSL_R_DECRYPTION_FAILED); | ||
2127 | goto gerr; | ||
2128 | } | ||
2129 | start = p; | ||
2130 | inlen = Tlen; | ||
2131 | if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, | ||
2132 | start, inlen) <=0) { | ||
2133 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
2134 | SSL_R_DECRYPTION_FAILED); | ||
2135 | goto gerr; | ||
2136 | } | ||
2137 | /* Generate master secret */ | ||
2138 | s->session->master_key_length = | ||
2139 | s->method->internal->ssl3_enc->generate_master_secret( | ||
2140 | s, s->session->master_key, premaster_secret, 32); | ||
2141 | /* Check if pubkey from client certificate was used */ | ||
2142 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, | ||
2143 | EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) | ||
2144 | ret = 2; | ||
2145 | else | ||
2146 | ret = 1; | ||
2147 | gerr: | ||
2148 | EVP_PKEY_free(client_pub_pkey); | ||
2149 | EVP_PKEY_CTX_free(pkey_ctx); | ||
2150 | if (ret) | ||
2151 | return (ret); | ||
2152 | else | ||
2153 | goto err; | ||
2154 | |||
2155 | truncated: | ||
2156 | al = SSL_AD_DECODE_ERROR; | ||
2157 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
2158 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2159 | err: | ||
2160 | return (-1); | ||
2161 | } | ||
2162 | |||
2163 | int | ||
2164 | ssl3_get_client_key_exchange(SSL *s) | ||
2165 | { | ||
2166 | unsigned long alg_k; | ||
2167 | unsigned char *p; | ||
2168 | int al, ok; | ||
2169 | long n; | ||
2170 | |||
2171 | /* 2048 maxlen is a guess. How long a key does that permit? */ | ||
2172 | n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A, | ||
2173 | SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok); | ||
2174 | if (!ok) | ||
2175 | return ((int)n); | ||
2176 | |||
2177 | p = (unsigned char *)s->internal->init_msg; | ||
2178 | |||
2179 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
2180 | |||
2181 | if (alg_k & SSL_kRSA) { | ||
2182 | if (ssl3_get_client_kex_rsa(s, p, n) != 1) | ||
2183 | goto err; | ||
2184 | } else if (alg_k & SSL_kDHE) { | ||
2185 | if (ssl3_get_client_kex_dhe(s, p, n) != 1) | ||
2186 | goto err; | ||
2187 | } else if (alg_k & SSL_kECDHE) { | ||
2188 | if (ssl3_get_client_kex_ecdhe(s, p, n) != 1) | ||
2189 | goto err; | ||
2190 | } else if (alg_k & SSL_kGOST) { | ||
2191 | if (ssl3_get_client_kex_gost(s, p, n) != 1) | ||
2192 | goto err; | ||
2193 | } else { | ||
2194 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
2195 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, | ||
2196 | SSL_R_UNKNOWN_CIPHER_TYPE); | ||
2197 | goto f_err; | ||
2198 | } | ||
2199 | |||
2200 | return (1); | ||
2201 | |||
2202 | f_err: | ||
2203 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2204 | err: | ||
2205 | return (-1); | ||
2206 | } | ||
2207 | |||
2208 | int | ||
2209 | ssl3_get_cert_verify(SSL *s) | ||
2210 | { | ||
2211 | EVP_PKEY *pkey = NULL; | ||
2212 | unsigned char *p; | ||
2213 | int al, ok, ret = 0; | ||
2214 | long n; | ||
2215 | int type = 0, i, j; | ||
2216 | X509 *peer; | ||
2217 | const EVP_MD *md = NULL; | ||
2218 | EVP_MD_CTX mctx; | ||
2219 | EVP_MD_CTX_init(&mctx); | ||
2220 | |||
2221 | n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CERT_VRFY_A, | ||
2222 | SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok); | ||
2223 | if (!ok) | ||
2224 | return ((int)n); | ||
2225 | |||
2226 | if (s->session->peer != NULL) { | ||
2227 | peer = s->session->peer; | ||
2228 | pkey = X509_get_pubkey(peer); | ||
2229 | type = X509_certificate_type(peer, pkey); | ||
2230 | } else { | ||
2231 | peer = NULL; | ||
2232 | pkey = NULL; | ||
2233 | } | ||
2234 | |||
2235 | if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) { | ||
2236 | S3I(s)->tmp.reuse_message = 1; | ||
2237 | if (peer != NULL) { | ||
2238 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
2239 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2240 | SSL_R_MISSING_VERIFY_MESSAGE); | ||
2241 | goto f_err; | ||
2242 | } | ||
2243 | ret = 1; | ||
2244 | goto end; | ||
2245 | } | ||
2246 | |||
2247 | if (peer == NULL) { | ||
2248 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2249 | SSL_R_NO_CLIENT_CERT_RECEIVED); | ||
2250 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
2251 | goto f_err; | ||
2252 | } | ||
2253 | |||
2254 | if (!(type & EVP_PKT_SIGN)) { | ||
2255 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2256 | SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); | ||
2257 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
2258 | goto f_err; | ||
2259 | } | ||
2260 | |||
2261 | if (S3I(s)->change_cipher_spec) { | ||
2262 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2263 | SSL_R_CCS_RECEIVED_EARLY); | ||
2264 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
2265 | goto f_err; | ||
2266 | } | ||
2267 | |||
2268 | /* we now have a signature that we need to verify */ | ||
2269 | p = (unsigned char *)s->internal->init_msg; | ||
2270 | /* | ||
2271 | * Check for broken implementations of GOST ciphersuites. | ||
2272 | * | ||
2273 | * If key is GOST and n is exactly 64, it is a bare | ||
2274 | * signature without length field. | ||
2275 | */ | ||
2276 | if (n == 64 && (pkey->type == NID_id_GostR3410_94 || | ||
2277 | pkey->type == NID_id_GostR3410_2001) ) { | ||
2278 | i = 64; | ||
2279 | } else { | ||
2280 | if (SSL_USE_SIGALGS(s)) { | ||
2281 | int sigalg = tls12_get_sigid(pkey); | ||
2282 | /* Should never happen */ | ||
2283 | if (sigalg == -1) { | ||
2284 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2285 | ERR_R_INTERNAL_ERROR); | ||
2286 | al = SSL_AD_INTERNAL_ERROR; | ||
2287 | goto f_err; | ||
2288 | } | ||
2289 | if (2 > n) | ||
2290 | goto truncated; | ||
2291 | /* Check key type is consistent with signature */ | ||
2292 | if (sigalg != (int)p[1]) { | ||
2293 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2294 | SSL_R_WRONG_SIGNATURE_TYPE); | ||
2295 | al = SSL_AD_DECODE_ERROR; | ||
2296 | goto f_err; | ||
2297 | } | ||
2298 | md = tls12_get_hash(p[0]); | ||
2299 | if (md == NULL) { | ||
2300 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2301 | SSL_R_UNKNOWN_DIGEST); | ||
2302 | al = SSL_AD_DECODE_ERROR; | ||
2303 | goto f_err; | ||
2304 | } | ||
2305 | p += 2; | ||
2306 | n -= 2; | ||
2307 | } | ||
2308 | if (2 > n) | ||
2309 | goto truncated; | ||
2310 | n2s(p, i); | ||
2311 | n -= 2; | ||
2312 | if (i > n) | ||
2313 | goto truncated; | ||
2314 | } | ||
2315 | j = EVP_PKEY_size(pkey); | ||
2316 | if ((i > j) || (n > j) || (n <= 0)) { | ||
2317 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2318 | SSL_R_WRONG_SIGNATURE_SIZE); | ||
2319 | al = SSL_AD_DECODE_ERROR; | ||
2320 | goto f_err; | ||
2321 | } | ||
2322 | |||
2323 | if (SSL_USE_SIGALGS(s)) { | ||
2324 | long hdatalen = 0; | ||
2325 | void *hdata; | ||
2326 | hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata); | ||
2327 | if (hdatalen <= 0) { | ||
2328 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2329 | ERR_R_INTERNAL_ERROR); | ||
2330 | al = SSL_AD_INTERNAL_ERROR; | ||
2331 | goto f_err; | ||
2332 | } | ||
2333 | if (!EVP_VerifyInit_ex(&mctx, md, NULL) || | ||
2334 | !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) { | ||
2335 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2336 | ERR_R_EVP_LIB); | ||
2337 | al = SSL_AD_INTERNAL_ERROR; | ||
2338 | goto f_err; | ||
2339 | } | ||
2340 | |||
2341 | if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) { | ||
2342 | al = SSL_AD_DECRYPT_ERROR; | ||
2343 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2344 | SSL_R_BAD_SIGNATURE); | ||
2345 | goto f_err; | ||
2346 | } | ||
2347 | } else | ||
2348 | if (pkey->type == EVP_PKEY_RSA) { | ||
2349 | i = RSA_verify(NID_md5_sha1, S3I(s)->tmp.cert_verify_md, | ||
2350 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i, | ||
2351 | pkey->pkey.rsa); | ||
2352 | if (i < 0) { | ||
2353 | al = SSL_AD_DECRYPT_ERROR; | ||
2354 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2355 | SSL_R_BAD_RSA_DECRYPT); | ||
2356 | goto f_err; | ||
2357 | } | ||
2358 | if (i == 0) { | ||
2359 | al = SSL_AD_DECRYPT_ERROR; | ||
2360 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2361 | SSL_R_BAD_RSA_SIGNATURE); | ||
2362 | goto f_err; | ||
2363 | } | ||
2364 | } else | ||
2365 | if (pkey->type == EVP_PKEY_DSA) { | ||
2366 | j = DSA_verify(pkey->save_type, | ||
2367 | &(S3I(s)->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), | ||
2368 | SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa); | ||
2369 | if (j <= 0) { | ||
2370 | /* bad signature */ | ||
2371 | al = SSL_AD_DECRYPT_ERROR; | ||
2372 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2373 | SSL_R_BAD_DSA_SIGNATURE); | ||
2374 | goto f_err; | ||
2375 | } | ||
2376 | } else | ||
2377 | if (pkey->type == EVP_PKEY_EC) { | ||
2378 | j = ECDSA_verify(pkey->save_type, | ||
2379 | &(S3I(s)->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), | ||
2380 | SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec); | ||
2381 | if (j <= 0) { | ||
2382 | /* bad signature */ | ||
2383 | al = SSL_AD_DECRYPT_ERROR; | ||
2384 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2385 | SSL_R_BAD_ECDSA_SIGNATURE); | ||
2386 | goto f_err; | ||
2387 | } | ||
2388 | } else | ||
2389 | #ifndef OPENSSL_NO_GOST | ||
2390 | if (pkey->type == NID_id_GostR3410_94 || | ||
2391 | pkey->type == NID_id_GostR3410_2001) { | ||
2392 | long hdatalen = 0; | ||
2393 | void *hdata; | ||
2394 | unsigned char signature[128]; | ||
2395 | unsigned int siglen = sizeof(signature); | ||
2396 | int nid; | ||
2397 | EVP_PKEY_CTX *pctx; | ||
2398 | |||
2399 | hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata); | ||
2400 | if (hdatalen <= 0) { | ||
2401 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2402 | ERR_R_INTERNAL_ERROR); | ||
2403 | al = SSL_AD_INTERNAL_ERROR; | ||
2404 | goto f_err; | ||
2405 | } | ||
2406 | if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) || | ||
2407 | !(md = EVP_get_digestbynid(nid))) { | ||
2408 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2409 | ERR_R_EVP_LIB); | ||
2410 | al = SSL_AD_INTERNAL_ERROR; | ||
2411 | goto f_err; | ||
2412 | } | ||
2413 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
2414 | if (!pctx) { | ||
2415 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2416 | ERR_R_EVP_LIB); | ||
2417 | al = SSL_AD_INTERNAL_ERROR; | ||
2418 | goto f_err; | ||
2419 | } | ||
2420 | if (!EVP_DigestInit_ex(&mctx, md, NULL) || | ||
2421 | !EVP_DigestUpdate(&mctx, hdata, hdatalen) || | ||
2422 | !EVP_DigestFinal(&mctx, signature, &siglen) || | ||
2423 | (EVP_PKEY_verify_init(pctx) <= 0) || | ||
2424 | (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) || | ||
2425 | (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_VERIFY, | ||
2426 | EVP_PKEY_CTRL_GOST_SIG_FORMAT, | ||
2427 | GOST_SIG_FORMAT_RS_LE, | ||
2428 | NULL) <= 0)) { | ||
2429 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2430 | ERR_R_EVP_LIB); | ||
2431 | al = SSL_AD_INTERNAL_ERROR; | ||
2432 | EVP_PKEY_CTX_free(pctx); | ||
2433 | goto f_err; | ||
2434 | } | ||
2435 | |||
2436 | if (EVP_PKEY_verify(pctx, p, i, signature, siglen) <= 0) { | ||
2437 | al = SSL_AD_DECRYPT_ERROR; | ||
2438 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2439 | SSL_R_BAD_SIGNATURE); | ||
2440 | EVP_PKEY_CTX_free(pctx); | ||
2441 | goto f_err; | ||
2442 | } | ||
2443 | |||
2444 | EVP_PKEY_CTX_free(pctx); | ||
2445 | } else | ||
2446 | #endif | ||
2447 | { | ||
2448 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, | ||
2449 | ERR_R_INTERNAL_ERROR); | ||
2450 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; | ||
2451 | goto f_err; | ||
2452 | } | ||
2453 | |||
2454 | |||
2455 | ret = 1; | ||
2456 | if (0) { | ||
2457 | truncated: | ||
2458 | al = SSL_AD_DECODE_ERROR; | ||
2459 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_PACKET_LENGTH); | ||
2460 | f_err: | ||
2461 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2462 | } | ||
2463 | end: | ||
2464 | if (S3I(s)->handshake_buffer) { | ||
2465 | BIO_free(S3I(s)->handshake_buffer); | ||
2466 | S3I(s)->handshake_buffer = NULL; | ||
2467 | s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; | ||
2468 | } | ||
2469 | EVP_MD_CTX_cleanup(&mctx); | ||
2470 | EVP_PKEY_free(pkey); | ||
2471 | return (ret); | ||
2472 | } | ||
2473 | |||
2474 | int | ||
2475 | ssl3_get_client_certificate(SSL *s) | ||
2476 | { | ||
2477 | CBS cbs, client_certs; | ||
2478 | int i, ok, al, ret = -1; | ||
2479 | X509 *x = NULL; | ||
2480 | long n; | ||
2481 | const unsigned char *q; | ||
2482 | STACK_OF(X509) *sk = NULL; | ||
2483 | |||
2484 | n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, | ||
2485 | -1, s->internal->max_cert_list, &ok); | ||
2486 | |||
2487 | if (!ok) | ||
2488 | return ((int)n); | ||
2489 | |||
2490 | if (S3I(s)->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { | ||
2491 | if ((s->verify_mode & SSL_VERIFY_PEER) && | ||
2492 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | ||
2493 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2494 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
2495 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
2496 | goto f_err; | ||
2497 | } | ||
2498 | /* | ||
2499 | * If tls asked for a client cert, | ||
2500 | * the client must return a 0 list. | ||
2501 | */ | ||
2502 | if (S3I(s)->tmp.cert_request) { | ||
2503 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2504 | SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST | ||
2505 | ); | ||
2506 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
2507 | goto f_err; | ||
2508 | } | ||
2509 | S3I(s)->tmp.reuse_message = 1; | ||
2510 | return (1); | ||
2511 | } | ||
2512 | |||
2513 | if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE) { | ||
2514 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
2515 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2516 | SSL_R_WRONG_MESSAGE_TYPE); | ||
2517 | goto f_err; | ||
2518 | } | ||
2519 | |||
2520 | if (n < 0) | ||
2521 | goto truncated; | ||
2522 | |||
2523 | CBS_init(&cbs, s->internal->init_msg, n); | ||
2524 | |||
2525 | if ((sk = sk_X509_new_null()) == NULL) { | ||
2526 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2527 | ERR_R_MALLOC_FAILURE); | ||
2528 | goto err; | ||
2529 | } | ||
2530 | |||
2531 | if (!CBS_get_u24_length_prefixed(&cbs, &client_certs) || | ||
2532 | CBS_len(&cbs) != 0) | ||
2533 | goto truncated; | ||
2534 | |||
2535 | while (CBS_len(&client_certs) > 0) { | ||
2536 | CBS cert; | ||
2537 | |||
2538 | if (!CBS_get_u24_length_prefixed(&client_certs, &cert)) { | ||
2539 | al = SSL_AD_DECODE_ERROR; | ||
2540 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2541 | SSL_R_CERT_LENGTH_MISMATCH); | ||
2542 | goto f_err; | ||
2543 | } | ||
2544 | |||
2545 | q = CBS_data(&cert); | ||
2546 | x = d2i_X509(NULL, &q, CBS_len(&cert)); | ||
2547 | if (x == NULL) { | ||
2548 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2549 | ERR_R_ASN1_LIB); | ||
2550 | goto err; | ||
2551 | } | ||
2552 | if (q != CBS_data(&cert) + CBS_len(&cert)) { | ||
2553 | al = SSL_AD_DECODE_ERROR; | ||
2554 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2555 | SSL_R_CERT_LENGTH_MISMATCH); | ||
2556 | goto f_err; | ||
2557 | } | ||
2558 | if (!sk_X509_push(sk, x)) { | ||
2559 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2560 | ERR_R_MALLOC_FAILURE); | ||
2561 | goto err; | ||
2562 | } | ||
2563 | x = NULL; | ||
2564 | } | ||
2565 | |||
2566 | if (sk_X509_num(sk) <= 0) { | ||
2567 | /* | ||
2568 | * TLS does not mind 0 certs returned. | ||
2569 | * Fail for TLS only if we required a certificate. | ||
2570 | */ | ||
2571 | if ((s->verify_mode & SSL_VERIFY_PEER) && | ||
2572 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { | ||
2573 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2574 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
2575 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
2576 | goto f_err; | ||
2577 | } | ||
2578 | /* No client certificate so digest cached records */ | ||
2579 | if (S3I(s)->handshake_buffer && !tls1_digest_cached_records(s)) { | ||
2580 | al = SSL_AD_INTERNAL_ERROR; | ||
2581 | goto f_err; | ||
2582 | } | ||
2583 | } else { | ||
2584 | i = ssl_verify_cert_chain(s, sk); | ||
2585 | if (i <= 0) { | ||
2586 | al = ssl_verify_alarm_type(s->verify_result); | ||
2587 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2588 | SSL_R_NO_CERTIFICATE_RETURNED); | ||
2589 | goto f_err; | ||
2590 | } | ||
2591 | } | ||
2592 | |||
2593 | X509_free(s->session->peer); | ||
2594 | s->session->peer = sk_X509_shift(sk); | ||
2595 | s->session->verify_result = s->verify_result; | ||
2596 | |||
2597 | /* | ||
2598 | * With the current implementation, sess_cert will always be NULL | ||
2599 | * when we arrive here | ||
2600 | */ | ||
2601 | if (SSI(s)->sess_cert == NULL) { | ||
2602 | SSI(s)->sess_cert = ssl_sess_cert_new(); | ||
2603 | if (SSI(s)->sess_cert == NULL) { | ||
2604 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2605 | ERR_R_MALLOC_FAILURE); | ||
2606 | goto err; | ||
2607 | } | ||
2608 | } | ||
2609 | sk_X509_pop_free(SSI(s)->sess_cert->cert_chain, X509_free); | ||
2610 | SSI(s)->sess_cert->cert_chain = sk; | ||
2611 | |||
2612 | /* | ||
2613 | * Inconsistency alert: cert_chain does *not* include the | ||
2614 | * peer's own certificate, while we do include it in s3_clnt.c | ||
2615 | */ | ||
2616 | |||
2617 | sk = NULL; | ||
2618 | |||
2619 | ret = 1; | ||
2620 | if (0) { | ||
2621 | truncated: | ||
2622 | al = SSL_AD_DECODE_ERROR; | ||
2623 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, | ||
2624 | SSL_R_BAD_PACKET_LENGTH); | ||
2625 | f_err: | ||
2626 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2627 | } | ||
2628 | err: | ||
2629 | X509_free(x); | ||
2630 | sk_X509_pop_free(sk, X509_free); | ||
2631 | |||
2632 | return (ret); | ||
2633 | } | ||
2634 | |||
2635 | int | ||
2636 | ssl3_send_server_certificate(SSL *s) | ||
2637 | { | ||
2638 | CBB cbb, server_cert; | ||
2639 | X509 *x; | ||
2640 | |||
2641 | /* | ||
2642 | * Server Certificate - RFC 5246, section 7.4.2. | ||
2643 | */ | ||
2644 | |||
2645 | memset(&cbb, 0, sizeof(cbb)); | ||
2646 | |||
2647 | if (s->internal->state == SSL3_ST_SW_CERT_A) { | ||
2648 | if ((x = ssl_get_server_send_cert(s)) == NULL) { | ||
2649 | SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, | ||
2650 | ERR_R_INTERNAL_ERROR); | ||
2651 | return (0); | ||
2652 | } | ||
2653 | |||
2654 | if (!ssl3_handshake_msg_start_cbb(s, &cbb, &server_cert, | ||
2655 | SSL3_MT_CERTIFICATE)) | ||
2656 | goto err; | ||
2657 | if (!ssl3_output_cert_chain(s, &server_cert, x)) | ||
2658 | goto err; | ||
2659 | if (!ssl3_handshake_msg_finish_cbb(s, &cbb)) | ||
2660 | goto err; | ||
2661 | |||
2662 | s->internal->state = SSL3_ST_SW_CERT_B; | ||
2663 | } | ||
2664 | |||
2665 | /* SSL3_ST_SW_CERT_B */ | ||
2666 | return (ssl3_handshake_write(s)); | ||
2667 | |||
2668 | err: | ||
2669 | CBB_cleanup(&cbb); | ||
2670 | |||
2671 | return (0); | ||
2672 | } | ||
2673 | |||
2674 | /* send a new session ticket (not necessarily for a new session) */ | ||
2675 | int | ||
2676 | ssl3_send_newsession_ticket(SSL *s) | ||
2677 | { | ||
2678 | unsigned char *d, *p, *macstart; | ||
2679 | unsigned char *senc = NULL; | ||
2680 | const unsigned char *const_p; | ||
2681 | int len, slen_full, slen; | ||
2682 | SSL_SESSION *sess; | ||
2683 | unsigned int hlen; | ||
2684 | EVP_CIPHER_CTX ctx; | ||
2685 | HMAC_CTX hctx; | ||
2686 | SSL_CTX *tctx = s->initial_ctx; | ||
2687 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
2688 | unsigned char key_name[16]; | ||
2689 | |||
2690 | if (s->internal->state == SSL3_ST_SW_SESSION_TICKET_A) { | ||
2691 | /* get session encoding length */ | ||
2692 | slen_full = i2d_SSL_SESSION(s->session, NULL); | ||
2693 | /* | ||
2694 | * Some length values are 16 bits, so forget it if session is | ||
2695 | * too long | ||
2696 | */ | ||
2697 | if (slen_full > 0xFF00) | ||
2698 | goto err; | ||
2699 | senc = malloc(slen_full); | ||
2700 | if (!senc) | ||
2701 | goto err; | ||
2702 | p = senc; | ||
2703 | i2d_SSL_SESSION(s->session, &p); | ||
2704 | |||
2705 | /* | ||
2706 | * Create a fresh copy (not shared with other threads) to | ||
2707 | * clean up | ||
2708 | */ | ||
2709 | const_p = senc; | ||
2710 | sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); | ||
2711 | if (sess == NULL) | ||
2712 | goto err; | ||
2713 | |||
2714 | /* ID is irrelevant for the ticket */ | ||
2715 | sess->session_id_length = 0; | ||
2716 | |||
2717 | slen = i2d_SSL_SESSION(sess, NULL); | ||
2718 | if (slen > slen_full) { | ||
2719 | /* shouldn't ever happen */ | ||
2720 | goto err; | ||
2721 | } | ||
2722 | p = senc; | ||
2723 | i2d_SSL_SESSION(sess, &p); | ||
2724 | SSL_SESSION_free(sess); | ||
2725 | |||
2726 | /* | ||
2727 | * Grow buffer if need be: the length calculation is as | ||
2728 | * follows 1 (size of message name) + 3 (message length | ||
2729 | * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) + | ||
2730 | * 16 (key name) + max_iv_len (iv length) + | ||
2731 | * session_length + max_enc_block_size (max encrypted session | ||
2732 | * length) + max_md_size (HMAC). | ||
2733 | */ | ||
2734 | if (!BUF_MEM_grow(s->internal->init_buf, ssl3_handshake_msg_hdr_len(s) + | ||
2735 | 22 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + | ||
2736 | EVP_MAX_MD_SIZE + slen)) | ||
2737 | goto err; | ||
2738 | |||
2739 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEWSESSION_TICKET); | ||
2740 | |||
2741 | EVP_CIPHER_CTX_init(&ctx); | ||
2742 | HMAC_CTX_init(&hctx); | ||
2743 | |||
2744 | /* | ||
2745 | * Initialize HMAC and cipher contexts. If callback present | ||
2746 | * it does all the work otherwise use generated values | ||
2747 | * from parent ctx. | ||
2748 | */ | ||
2749 | if (tctx->internal->tlsext_ticket_key_cb) { | ||
2750 | if (tctx->internal->tlsext_ticket_key_cb(s, | ||
2751 | key_name, iv, &ctx, &hctx, 1) < 0) { | ||
2752 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2753 | goto err; | ||
2754 | } | ||
2755 | } else { | ||
2756 | arc4random_buf(iv, 16); | ||
2757 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | ||
2758 | tctx->internal->tlsext_tick_aes_key, iv); | ||
2759 | HMAC_Init_ex(&hctx, tctx->internal->tlsext_tick_hmac_key, | ||
2760 | 16, tlsext_tick_md(), NULL); | ||
2761 | memcpy(key_name, tctx->internal->tlsext_tick_key_name, 16); | ||
2762 | } | ||
2763 | |||
2764 | /* | ||
2765 | * Ticket lifetime hint (advisory only): | ||
2766 | * We leave this unspecified for resumed session | ||
2767 | * (for simplicity), and guess that tickets for new | ||
2768 | * sessions will live as long as their sessions. | ||
2769 | */ | ||
2770 | l2n(s->internal->hit ? 0 : s->session->timeout, p); | ||
2771 | |||
2772 | /* Skip ticket length for now */ | ||
2773 | p += 2; | ||
2774 | /* Output key name */ | ||
2775 | macstart = p; | ||
2776 | memcpy(p, key_name, 16); | ||
2777 | p += 16; | ||
2778 | /* output IV */ | ||
2779 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); | ||
2780 | p += EVP_CIPHER_CTX_iv_length(&ctx); | ||
2781 | /* Encrypt session data */ | ||
2782 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); | ||
2783 | p += len; | ||
2784 | EVP_EncryptFinal_ex(&ctx, p, &len); | ||
2785 | p += len; | ||
2786 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2787 | |||
2788 | HMAC_Update(&hctx, macstart, p - macstart); | ||
2789 | HMAC_Final(&hctx, p, &hlen); | ||
2790 | HMAC_CTX_cleanup(&hctx); | ||
2791 | p += hlen; | ||
2792 | |||
2793 | /* Now write out lengths: p points to end of data written */ | ||
2794 | /* Total length */ | ||
2795 | len = p - d; | ||
2796 | |||
2797 | /* Skip ticket lifetime hint. */ | ||
2798 | p = d + 4; | ||
2799 | s2n(len - 6, p); /* Message length */ | ||
2800 | |||
2801 | ssl3_handshake_msg_finish(s, len); | ||
2802 | |||
2803 | s->internal->state = SSL3_ST_SW_SESSION_TICKET_B; | ||
2804 | |||
2805 | explicit_bzero(senc, slen_full); | ||
2806 | free(senc); | ||
2807 | } | ||
2808 | |||
2809 | /* SSL3_ST_SW_SESSION_TICKET_B */ | ||
2810 | return (ssl3_handshake_write(s)); | ||
2811 | |||
2812 | err: | ||
2813 | if (senc != NULL) | ||
2814 | explicit_bzero(senc, slen_full); | ||
2815 | free(senc); | ||
2816 | |||
2817 | return (-1); | ||
2818 | } | ||
2819 | |||
2820 | int | ||
2821 | ssl3_send_cert_status(SSL *s) | ||
2822 | { | ||
2823 | unsigned char *p; | ||
2824 | |||
2825 | if (s->internal->state == SSL3_ST_SW_CERT_STATUS_A) { | ||
2826 | /* | ||
2827 | * Grow buffer if need be: the length calculation is as | ||
2828 | * follows 1 (message type) + 3 (message length) + | ||
2829 | * 1 (ocsp response type) + 3 (ocsp response length) | ||
2830 | * + (ocsp response) | ||
2831 | */ | ||
2832 | if (!BUF_MEM_grow(s->internal->init_buf, SSL3_HM_HEADER_LENGTH + 4 + | ||
2833 | s->internal->tlsext_ocsp_resplen)) | ||
2834 | return (-1); | ||
2835 | |||
2836 | p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_STATUS); | ||
2837 | |||
2838 | *(p++) = s->tlsext_status_type; | ||
2839 | l2n3(s->internal->tlsext_ocsp_resplen, p); | ||
2840 | memcpy(p, s->internal->tlsext_ocsp_resp, s->internal->tlsext_ocsp_resplen); | ||
2841 | |||
2842 | ssl3_handshake_msg_finish(s, s->internal->tlsext_ocsp_resplen + 4); | ||
2843 | |||
2844 | s->internal->state = SSL3_ST_SW_CERT_STATUS_B; | ||
2845 | } | ||
2846 | |||
2847 | /* SSL3_ST_SW_CERT_STATUS_B */ | ||
2848 | return (ssl3_handshake_write(s)); | ||
2849 | } | ||
2850 | |||
2851 | /* | ||
2852 | * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. | ||
2853 | * It sets the next_proto member in s if found | ||
2854 | */ | ||
2855 | int | ||
2856 | ssl3_get_next_proto(SSL *s) | ||
2857 | { | ||
2858 | CBS cbs, proto, padding; | ||
2859 | int ok; | ||
2860 | long n; | ||
2861 | size_t len; | ||
2862 | |||
2863 | /* | ||
2864 | * Clients cannot send a NextProtocol message if we didn't see the | ||
2865 | * extension in their ClientHello | ||
2866 | */ | ||
2867 | if (!S3I(s)->next_proto_neg_seen) { | ||
2868 | SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, | ||
2869 | SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); | ||
2870 | return (-1); | ||
2871 | } | ||
2872 | |||
2873 | /* 514 maxlen is enough for the payload format below */ | ||
2874 | n = s->method->internal->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A, | ||
2875 | SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, 514, &ok); | ||
2876 | if (!ok) | ||
2877 | return ((int)n); | ||
2878 | |||
2879 | /* | ||
2880 | * s->internal->state doesn't reflect whether ChangeCipherSpec has been received | ||
2881 | * in this handshake, but S3I(s)->change_cipher_spec does (will be reset | ||
2882 | * by ssl3_get_finished). | ||
2883 | */ | ||
2884 | if (!S3I(s)->change_cipher_spec) { | ||
2885 | SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, | ||
2886 | SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); | ||
2887 | return (-1); | ||
2888 | } | ||
2889 | |||
2890 | if (n < 2) | ||
2891 | return (0); | ||
2892 | /* The body must be > 1 bytes long */ | ||
2893 | |||
2894 | CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); | ||
2895 | |||
2896 | /* | ||
2897 | * The payload looks like: | ||
2898 | * uint8 proto_len; | ||
2899 | * uint8 proto[proto_len]; | ||
2900 | * uint8 padding_len; | ||
2901 | * uint8 padding[padding_len]; | ||
2902 | */ | ||
2903 | if (!CBS_get_u8_length_prefixed(&cbs, &proto) || | ||
2904 | !CBS_get_u8_length_prefixed(&cbs, &padding) || | ||
2905 | CBS_len(&cbs) != 0) | ||
2906 | return 0; | ||
2907 | |||
2908 | /* | ||
2909 | * XXX We should not NULL it, but this matches old behavior of not | ||
2910 | * freeing before malloc. | ||
2911 | */ | ||
2912 | s->internal->next_proto_negotiated = NULL; | ||
2913 | s->internal->next_proto_negotiated_len = 0; | ||
2914 | |||
2915 | if (!CBS_stow(&proto, &s->internal->next_proto_negotiated, &len)) { | ||
2916 | SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, | ||
2917 | ERR_R_MALLOC_FAILURE); | ||
2918 | return (0); | ||
2919 | } | ||
2920 | s->internal->next_proto_negotiated_len = (uint8_t)len; | ||
2921 | |||
2922 | return (1); | ||
2923 | } | ||