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_clnt.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_clnt.c')
-rw-r--r-- | src/lib/libssl/s3_clnt.c | 2795 |
1 files changed, 0 insertions, 2795 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c deleted file mode 100644 index 13915c2439..0000000000 --- a/src/lib/libssl/s3_clnt.c +++ /dev/null | |||
@@ -1,2795 +0,0 @@ | |||
1 | /* $OpenBSD: s3_clnt.c,v 1.175 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 <limits.h> | ||
152 | #include <stdint.h> | ||
153 | #include <stdio.h> | ||
154 | |||
155 | #include "ssl_locl.h" | ||
156 | |||
157 | #include <openssl/bn.h> | ||
158 | #include <openssl/buffer.h> | ||
159 | #include <openssl/curve25519.h> | ||
160 | #include <openssl/dh.h> | ||
161 | #include <openssl/evp.h> | ||
162 | #include <openssl/md5.h> | ||
163 | #include <openssl/objects.h> | ||
164 | |||
165 | #ifndef OPENSSL_NO_ENGINE | ||
166 | #include <openssl/engine.h> | ||
167 | #endif | ||
168 | #ifndef OPENSSL_NO_GOST | ||
169 | #include <openssl/gost.h> | ||
170 | #endif | ||
171 | |||
172 | #include "bytestring.h" | ||
173 | |||
174 | static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b); | ||
175 | |||
176 | int | ||
177 | ssl3_connect(SSL *s) | ||
178 | { | ||
179 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
180 | int ret = -1; | ||
181 | int new_state, state, skip = 0; | ||
182 | |||
183 | ERR_clear_error(); | ||
184 | errno = 0; | ||
185 | |||
186 | if (s->internal->info_callback != NULL) | ||
187 | cb = s->internal->info_callback; | ||
188 | else if (s->ctx->internal->info_callback != NULL) | ||
189 | cb = s->ctx->internal->info_callback; | ||
190 | |||
191 | s->internal->in_handshake++; | ||
192 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
193 | SSL_clear(s); | ||
194 | |||
195 | for (;;) { | ||
196 | state = s->internal->state; | ||
197 | |||
198 | switch (s->internal->state) { | ||
199 | case SSL_ST_RENEGOTIATE: | ||
200 | s->internal->renegotiate = 1; | ||
201 | s->internal->state = SSL_ST_CONNECT; | ||
202 | s->ctx->internal->stats.sess_connect_renegotiate++; | ||
203 | /* break */ | ||
204 | case SSL_ST_BEFORE: | ||
205 | case SSL_ST_CONNECT: | ||
206 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | ||
207 | case SSL_ST_OK|SSL_ST_CONNECT: | ||
208 | |||
209 | s->server = 0; | ||
210 | if (cb != NULL) | ||
211 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
212 | |||
213 | if ((s->version & 0xff00 ) != 0x0300) { | ||
214 | SSLerr(SSL_F_SSL3_CONNECT, | ||
215 | ERR_R_INTERNAL_ERROR); | ||
216 | ret = -1; | ||
217 | goto end; | ||
218 | } | ||
219 | |||
220 | /* s->version=SSL3_VERSION; */ | ||
221 | s->internal->type = SSL_ST_CONNECT; | ||
222 | |||
223 | if (!ssl3_setup_init_buffer(s)) { | ||
224 | ret = -1; | ||
225 | goto end; | ||
226 | } | ||
227 | if (!ssl3_setup_buffers(s)) { | ||
228 | ret = -1; | ||
229 | goto end; | ||
230 | } | ||
231 | if (!ssl_init_wbio_buffer(s, 0)) { | ||
232 | ret = -1; | ||
233 | goto end; | ||
234 | } | ||
235 | |||
236 | /* don't push the buffering BIO quite yet */ | ||
237 | |||
238 | if (!tls1_init_finished_mac(s)) { | ||
239 | ret = -1; | ||
240 | goto end; | ||
241 | } | ||
242 | |||
243 | s->internal->state = SSL3_ST_CW_CLNT_HELLO_A; | ||
244 | s->ctx->internal->stats.sess_connect++; | ||
245 | s->internal->init_num = 0; | ||
246 | break; | ||
247 | |||
248 | case SSL3_ST_CW_CLNT_HELLO_A: | ||
249 | case SSL3_ST_CW_CLNT_HELLO_B: | ||
250 | |||
251 | s->internal->shutdown = 0; | ||
252 | ret = ssl3_client_hello(s); | ||
253 | if (ret <= 0) | ||
254 | goto end; | ||
255 | s->internal->state = SSL3_ST_CR_SRVR_HELLO_A; | ||
256 | s->internal->init_num = 0; | ||
257 | |||
258 | /* turn on buffering for the next lot of output */ | ||
259 | if (s->bbio != s->wbio) | ||
260 | s->wbio = BIO_push(s->bbio, s->wbio); | ||
261 | |||
262 | break; | ||
263 | |||
264 | case SSL3_ST_CR_SRVR_HELLO_A: | ||
265 | case SSL3_ST_CR_SRVR_HELLO_B: | ||
266 | ret = ssl3_get_server_hello(s); | ||
267 | if (ret <= 0) | ||
268 | goto end; | ||
269 | |||
270 | if (s->internal->hit) { | ||
271 | s->internal->state = SSL3_ST_CR_FINISHED_A; | ||
272 | if (s->internal->tlsext_ticket_expected) { | ||
273 | /* receive renewed session ticket */ | ||
274 | s->internal->state = SSL3_ST_CR_SESSION_TICKET_A; | ||
275 | } | ||
276 | } else | ||
277 | s->internal->state = SSL3_ST_CR_CERT_A; | ||
278 | s->internal->init_num = 0; | ||
279 | break; | ||
280 | |||
281 | case SSL3_ST_CR_CERT_A: | ||
282 | case SSL3_ST_CR_CERT_B: | ||
283 | ret = ssl3_check_finished(s); | ||
284 | if (ret <= 0) | ||
285 | goto end; | ||
286 | if (ret == 2) { | ||
287 | s->internal->hit = 1; | ||
288 | if (s->internal->tlsext_ticket_expected) | ||
289 | s->internal->state = SSL3_ST_CR_SESSION_TICKET_A; | ||
290 | else | ||
291 | s->internal->state = SSL3_ST_CR_FINISHED_A; | ||
292 | s->internal->init_num = 0; | ||
293 | break; | ||
294 | } | ||
295 | /* Check if it is anon DH/ECDH. */ | ||
296 | if (!(S3I(s)->tmp.new_cipher->algorithm_auth & | ||
297 | SSL_aNULL)) { | ||
298 | ret = ssl3_get_server_certificate(s); | ||
299 | if (ret <= 0) | ||
300 | goto end; | ||
301 | if (s->internal->tlsext_status_expected) | ||
302 | s->internal->state = SSL3_ST_CR_CERT_STATUS_A; | ||
303 | else | ||
304 | s->internal->state = SSL3_ST_CR_KEY_EXCH_A; | ||
305 | } else { | ||
306 | skip = 1; | ||
307 | s->internal->state = SSL3_ST_CR_KEY_EXCH_A; | ||
308 | } | ||
309 | s->internal->init_num = 0; | ||
310 | break; | ||
311 | |||
312 | case SSL3_ST_CR_KEY_EXCH_A: | ||
313 | case SSL3_ST_CR_KEY_EXCH_B: | ||
314 | ret = ssl3_get_server_key_exchange(s); | ||
315 | if (ret <= 0) | ||
316 | goto end; | ||
317 | s->internal->state = SSL3_ST_CR_CERT_REQ_A; | ||
318 | s->internal->init_num = 0; | ||
319 | |||
320 | /* | ||
321 | * At this point we check that we have the | ||
322 | * required stuff from the server. | ||
323 | */ | ||
324 | if (!ssl3_check_cert_and_algorithm(s)) { | ||
325 | ret = -1; | ||
326 | goto end; | ||
327 | } | ||
328 | break; | ||
329 | |||
330 | case SSL3_ST_CR_CERT_REQ_A: | ||
331 | case SSL3_ST_CR_CERT_REQ_B: | ||
332 | ret = ssl3_get_certificate_request(s); | ||
333 | if (ret <= 0) | ||
334 | goto end; | ||
335 | s->internal->state = SSL3_ST_CR_SRVR_DONE_A; | ||
336 | s->internal->init_num = 0; | ||
337 | break; | ||
338 | |||
339 | case SSL3_ST_CR_SRVR_DONE_A: | ||
340 | case SSL3_ST_CR_SRVR_DONE_B: | ||
341 | ret = ssl3_get_server_done(s); | ||
342 | if (ret <= 0) | ||
343 | goto end; | ||
344 | if (S3I(s)->tmp.cert_req) | ||
345 | s->internal->state = SSL3_ST_CW_CERT_A; | ||
346 | else | ||
347 | s->internal->state = SSL3_ST_CW_KEY_EXCH_A; | ||
348 | s->internal->init_num = 0; | ||
349 | |||
350 | break; | ||
351 | |||
352 | case SSL3_ST_CW_CERT_A: | ||
353 | case SSL3_ST_CW_CERT_B: | ||
354 | case SSL3_ST_CW_CERT_C: | ||
355 | case SSL3_ST_CW_CERT_D: | ||
356 | ret = ssl3_send_client_certificate(s); | ||
357 | if (ret <= 0) | ||
358 | goto end; | ||
359 | s->internal->state = SSL3_ST_CW_KEY_EXCH_A; | ||
360 | s->internal->init_num = 0; | ||
361 | break; | ||
362 | |||
363 | case SSL3_ST_CW_KEY_EXCH_A: | ||
364 | case SSL3_ST_CW_KEY_EXCH_B: | ||
365 | ret = ssl3_send_client_key_exchange(s); | ||
366 | if (ret <= 0) | ||
367 | goto end; | ||
368 | /* | ||
369 | * EAY EAY EAY need to check for DH fix cert | ||
370 | * sent back | ||
371 | */ | ||
372 | /* | ||
373 | * For TLS, cert_req is set to 2, so a cert chain | ||
374 | * of nothing is sent, but no verify packet is sent | ||
375 | */ | ||
376 | /* | ||
377 | * XXX: For now, we do not support client | ||
378 | * authentication in ECDH cipher suites with | ||
379 | * ECDH (rather than ECDSA) certificates. | ||
380 | * We need to skip the certificate verify | ||
381 | * message when client's ECDH public key is sent | ||
382 | * inside the client certificate. | ||
383 | */ | ||
384 | if (S3I(s)->tmp.cert_req == 1) { | ||
385 | s->internal->state = SSL3_ST_CW_CERT_VRFY_A; | ||
386 | } else { | ||
387 | s->internal->state = SSL3_ST_CW_CHANGE_A; | ||
388 | S3I(s)->change_cipher_spec = 0; | ||
389 | } | ||
390 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | ||
391 | s->internal->state = SSL3_ST_CW_CHANGE_A; | ||
392 | S3I(s)->change_cipher_spec = 0; | ||
393 | } | ||
394 | |||
395 | s->internal->init_num = 0; | ||
396 | break; | ||
397 | |||
398 | case SSL3_ST_CW_CERT_VRFY_A: | ||
399 | case SSL3_ST_CW_CERT_VRFY_B: | ||
400 | ret = ssl3_send_client_verify(s); | ||
401 | if (ret <= 0) | ||
402 | goto end; | ||
403 | s->internal->state = SSL3_ST_CW_CHANGE_A; | ||
404 | s->internal->init_num = 0; | ||
405 | S3I(s)->change_cipher_spec = 0; | ||
406 | break; | ||
407 | |||
408 | case SSL3_ST_CW_CHANGE_A: | ||
409 | case SSL3_ST_CW_CHANGE_B: | ||
410 | ret = ssl3_send_change_cipher_spec(s, | ||
411 | SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B); | ||
412 | if (ret <= 0) | ||
413 | goto end; | ||
414 | |||
415 | if (S3I(s)->next_proto_neg_seen) | ||
416 | s->internal->state = SSL3_ST_CW_NEXT_PROTO_A; | ||
417 | else | ||
418 | s->internal->state = SSL3_ST_CW_FINISHED_A; | ||
419 | s->internal->init_num = 0; | ||
420 | |||
421 | s->session->cipher = S3I(s)->tmp.new_cipher; | ||
422 | if (!s->method->internal->ssl3_enc->setup_key_block(s)) { | ||
423 | ret = -1; | ||
424 | goto end; | ||
425 | } | ||
426 | |||
427 | if (!s->method->internal->ssl3_enc->change_cipher_state(s, | ||
428 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { | ||
429 | ret = -1; | ||
430 | goto end; | ||
431 | } | ||
432 | |||
433 | break; | ||
434 | |||
435 | case SSL3_ST_CW_NEXT_PROTO_A: | ||
436 | case SSL3_ST_CW_NEXT_PROTO_B: | ||
437 | ret = ssl3_send_next_proto(s); | ||
438 | if (ret <= 0) | ||
439 | goto end; | ||
440 | s->internal->state = SSL3_ST_CW_FINISHED_A; | ||
441 | break; | ||
442 | |||
443 | case SSL3_ST_CW_FINISHED_A: | ||
444 | case SSL3_ST_CW_FINISHED_B: | ||
445 | ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, | ||
446 | SSL3_ST_CW_FINISHED_B, | ||
447 | s->method->internal->ssl3_enc->client_finished_label, | ||
448 | s->method->internal->ssl3_enc->client_finished_label_len); | ||
449 | if (ret <= 0) | ||
450 | goto end; | ||
451 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
452 | s->internal->state = SSL3_ST_CW_FLUSH; | ||
453 | |||
454 | /* clear flags */ | ||
455 | s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; | ||
456 | if (s->internal->hit) { | ||
457 | S3I(s)->tmp.next_state = SSL_ST_OK; | ||
458 | if (s->s3->flags & | ||
459 | SSL3_FLAGS_DELAY_CLIENT_FINISHED) { | ||
460 | s->internal->state = SSL_ST_OK; | ||
461 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; | ||
462 | S3I(s)->delay_buf_pop_ret = 0; | ||
463 | } | ||
464 | } else { | ||
465 | /* Allow NewSessionTicket if ticket expected */ | ||
466 | if (s->internal->tlsext_ticket_expected) | ||
467 | S3I(s)->tmp.next_state = | ||
468 | SSL3_ST_CR_SESSION_TICKET_A; | ||
469 | else | ||
470 | |||
471 | S3I(s)->tmp.next_state = SSL3_ST_CR_FINISHED_A; | ||
472 | } | ||
473 | s->internal->init_num = 0; | ||
474 | break; | ||
475 | |||
476 | case SSL3_ST_CR_SESSION_TICKET_A: | ||
477 | case SSL3_ST_CR_SESSION_TICKET_B: | ||
478 | ret = ssl3_get_new_session_ticket(s); | ||
479 | if (ret <= 0) | ||
480 | goto end; | ||
481 | s->internal->state = SSL3_ST_CR_FINISHED_A; | ||
482 | s->internal->init_num = 0; | ||
483 | break; | ||
484 | |||
485 | case SSL3_ST_CR_CERT_STATUS_A: | ||
486 | case SSL3_ST_CR_CERT_STATUS_B: | ||
487 | ret = ssl3_get_cert_status(s); | ||
488 | if (ret <= 0) | ||
489 | goto end; | ||
490 | s->internal->state = SSL3_ST_CR_KEY_EXCH_A; | ||
491 | s->internal->init_num = 0; | ||
492 | break; | ||
493 | |||
494 | case SSL3_ST_CR_FINISHED_A: | ||
495 | case SSL3_ST_CR_FINISHED_B: | ||
496 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
497 | ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, | ||
498 | SSL3_ST_CR_FINISHED_B); | ||
499 | if (ret <= 0) | ||
500 | goto end; | ||
501 | |||
502 | if (s->internal->hit) | ||
503 | s->internal->state = SSL3_ST_CW_CHANGE_A; | ||
504 | else | ||
505 | s->internal->state = SSL_ST_OK; | ||
506 | s->internal->init_num = 0; | ||
507 | break; | ||
508 | |||
509 | case SSL3_ST_CW_FLUSH: | ||
510 | s->internal->rwstate = SSL_WRITING; | ||
511 | if (BIO_flush(s->wbio) <= 0) { | ||
512 | ret = -1; | ||
513 | goto end; | ||
514 | } | ||
515 | s->internal->rwstate = SSL_NOTHING; | ||
516 | s->internal->state = S3I(s)->tmp.next_state; | ||
517 | break; | ||
518 | |||
519 | case SSL_ST_OK: | ||
520 | /* clean a few things up */ | ||
521 | tls1_cleanup_key_block(s); | ||
522 | |||
523 | BUF_MEM_free(s->internal->init_buf); | ||
524 | s->internal->init_buf = NULL; | ||
525 | |||
526 | /* | ||
527 | * If we are not 'joining' the last two packets, | ||
528 | * remove the buffering now | ||
529 | */ | ||
530 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | ||
531 | ssl_free_wbio_buffer(s); | ||
532 | /* else do it later in ssl3_write */ | ||
533 | |||
534 | s->internal->init_num = 0; | ||
535 | s->internal->renegotiate = 0; | ||
536 | s->internal->new_session = 0; | ||
537 | |||
538 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); | ||
539 | if (s->internal->hit) | ||
540 | s->ctx->internal->stats.sess_hit++; | ||
541 | |||
542 | ret = 1; | ||
543 | /* s->server=0; */ | ||
544 | s->internal->handshake_func = ssl3_connect; | ||
545 | s->ctx->internal->stats.sess_connect_good++; | ||
546 | |||
547 | if (cb != NULL) | ||
548 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
549 | |||
550 | goto end; | ||
551 | /* break; */ | ||
552 | |||
553 | default: | ||
554 | SSLerr(SSL_F_SSL3_CONNECT, | ||
555 | SSL_R_UNKNOWN_STATE); | ||
556 | ret = -1; | ||
557 | goto end; | ||
558 | /* break; */ | ||
559 | } | ||
560 | |||
561 | /* did we do anything */ | ||
562 | if (!S3I(s)->tmp.reuse_message && !skip) { | ||
563 | if (s->internal->debug) { | ||
564 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
565 | goto end; | ||
566 | } | ||
567 | |||
568 | if ((cb != NULL) && (s->internal->state != state)) { | ||
569 | new_state = s->internal->state; | ||
570 | s->internal->state = state; | ||
571 | cb(s, SSL_CB_CONNECT_LOOP, 1); | ||
572 | s->internal->state = new_state; | ||
573 | } | ||
574 | } | ||
575 | skip = 0; | ||
576 | } | ||
577 | |||
578 | end: | ||
579 | s->internal->in_handshake--; | ||
580 | if (cb != NULL) | ||
581 | cb(s, SSL_CB_CONNECT_EXIT, ret); | ||
582 | |||
583 | return (ret); | ||
584 | } | ||
585 | |||
586 | int | ||
587 | ssl3_client_hello(SSL *s) | ||
588 | { | ||
589 | unsigned char *bufend, *p, *d; | ||
590 | uint16_t max_version; | ||
591 | size_t outlen; | ||
592 | int i; | ||
593 | |||
594 | bufend = (unsigned char *)s->internal->init_buf->data + SSL3_RT_MAX_PLAIN_LENGTH; | ||
595 | |||
596 | if (s->internal->state == SSL3_ST_CW_CLNT_HELLO_A) { | ||
597 | SSL_SESSION *sess = s->session; | ||
598 | |||
599 | if (ssl_supported_version_range(s, NULL, &max_version) != 1) { | ||
600 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
601 | SSL_R_NO_PROTOCOLS_AVAILABLE); | ||
602 | return (-1); | ||
603 | } | ||
604 | s->client_version = s->version = max_version; | ||
605 | |||
606 | if ((sess == NULL) || | ||
607 | (sess->ssl_version != s->version) || | ||
608 | (!sess->session_id_length && !sess->tlsext_tick) || | ||
609 | (sess->internal->not_resumable)) { | ||
610 | if (!ssl_get_new_session(s, 0)) | ||
611 | goto err; | ||
612 | } | ||
613 | /* else use the pre-loaded session */ | ||
614 | |||
615 | /* | ||
616 | * If a DTLS ClientHello message is being resent after a | ||
617 | * HelloVerifyRequest, we must retain the original client | ||
618 | * random value. | ||
619 | */ | ||
620 | if (!SSL_IS_DTLS(s) || D1I(s)->send_cookie == 0) | ||
621 | arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); | ||
622 | |||
623 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_CLIENT_HELLO); | ||
624 | |||
625 | /* | ||
626 | * Version indicates the negotiated version: for example from | ||
627 | * an SSLv2/v3 compatible client hello). The client_version | ||
628 | * field is the maximum version we permit and it is also | ||
629 | * used in RSA encrypted premaster secrets. Some servers can | ||
630 | * choke if we initially report a higher version then | ||
631 | * renegotiate to a lower one in the premaster secret. This | ||
632 | * didn't happen with TLS 1.0 as most servers supported it | ||
633 | * but it can with TLS 1.1 or later if the server only supports | ||
634 | * 1.0. | ||
635 | * | ||
636 | * Possible scenario with previous logic: | ||
637 | * 1. Client hello indicates TLS 1.2 | ||
638 | * 2. Server hello says TLS 1.0 | ||
639 | * 3. RSA encrypted premaster secret uses 1.2. | ||
640 | * 4. Handhaked proceeds using TLS 1.0. | ||
641 | * 5. Server sends hello request to renegotiate. | ||
642 | * 6. Client hello indicates TLS v1.0 as we now | ||
643 | * know that is maximum server supports. | ||
644 | * 7. Server chokes on RSA encrypted premaster secret | ||
645 | * containing version 1.0. | ||
646 | * | ||
647 | * For interoperability it should be OK to always use the | ||
648 | * maximum version we support in client hello and then rely | ||
649 | * on the checking of version to ensure the servers isn't | ||
650 | * being inconsistent: for example initially negotiating with | ||
651 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | ||
652 | * client_version in client hello and not resetting it to | ||
653 | * the negotiated version. | ||
654 | */ | ||
655 | |||
656 | *(p++) = s->client_version >> 8; | ||
657 | *(p++) = s->client_version & 0xff; | ||
658 | |||
659 | /* Random stuff */ | ||
660 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); | ||
661 | p += SSL3_RANDOM_SIZE; | ||
662 | |||
663 | /* Session ID */ | ||
664 | if (s->internal->new_session) | ||
665 | i = 0; | ||
666 | else | ||
667 | i = s->session->session_id_length; | ||
668 | *(p++) = i; | ||
669 | if (i != 0) { | ||
670 | if (i > (int)sizeof(s->session->session_id)) { | ||
671 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
672 | ERR_R_INTERNAL_ERROR); | ||
673 | goto err; | ||
674 | } | ||
675 | memcpy(p, s->session->session_id, i); | ||
676 | p += i; | ||
677 | } | ||
678 | |||
679 | /* DTLS Cookie. */ | ||
680 | if (SSL_IS_DTLS(s)) { | ||
681 | if (D1I(s)->cookie_len > sizeof(D1I(s)->cookie)) { | ||
682 | SSLerr(SSL_F_DTLS1_CLIENT_HELLO, | ||
683 | ERR_R_INTERNAL_ERROR); | ||
684 | goto err; | ||
685 | } | ||
686 | *(p++) = D1I(s)->cookie_len; | ||
687 | memcpy(p, D1I(s)->cookie, D1I(s)->cookie_len); | ||
688 | p += D1I(s)->cookie_len; | ||
689 | } | ||
690 | |||
691 | /* Ciphers supported */ | ||
692 | if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2], | ||
693 | bufend - &p[2], &outlen)) | ||
694 | goto err; | ||
695 | if (outlen == 0) { | ||
696 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
697 | SSL_R_NO_CIPHERS_AVAILABLE); | ||
698 | goto err; | ||
699 | } | ||
700 | s2n(outlen, p); | ||
701 | p += outlen; | ||
702 | |||
703 | /* add in (no) COMPRESSION */ | ||
704 | *(p++) = 1; | ||
705 | *(p++) = 0; /* Add the NULL method */ | ||
706 | |||
707 | /* TLS extensions*/ | ||
708 | if ((p = ssl_add_clienthello_tlsext(s, p, bufend)) == NULL) { | ||
709 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | ||
710 | ERR_R_INTERNAL_ERROR); | ||
711 | goto err; | ||
712 | } | ||
713 | |||
714 | ssl3_handshake_msg_finish(s, p - d); | ||
715 | |||
716 | s->internal->state = SSL3_ST_CW_CLNT_HELLO_B; | ||
717 | } | ||
718 | |||
719 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
720 | return (ssl3_handshake_write(s)); | ||
721 | |||
722 | err: | ||
723 | return (-1); | ||
724 | } | ||
725 | |||
726 | int | ||
727 | ssl3_get_server_hello(SSL *s) | ||
728 | { | ||
729 | CBS cbs, server_random, session_id; | ||
730 | uint16_t server_version, cipher_suite; | ||
731 | uint16_t min_version, max_version; | ||
732 | uint8_t compression_method; | ||
733 | STACK_OF(SSL_CIPHER) *sk; | ||
734 | const SSL_CIPHER *cipher; | ||
735 | const SSL_METHOD *method; | ||
736 | unsigned char *p; | ||
737 | unsigned long alg_k; | ||
738 | size_t outlen; | ||
739 | int i, al, ok; | ||
740 | long n; | ||
741 | |||
742 | s->internal->first_packet = 1; | ||
743 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, | ||
744 | SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, /* ?? */ &ok); | ||
745 | if (!ok) | ||
746 | return ((int)n); | ||
747 | s->internal->first_packet = 0; | ||
748 | |||
749 | if (n < 0) | ||
750 | goto truncated; | ||
751 | |||
752 | CBS_init(&cbs, s->internal->init_msg, n); | ||
753 | |||
754 | if (SSL_IS_DTLS(s)) { | ||
755 | if (S3I(s)->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { | ||
756 | if (D1I(s)->send_cookie == 0) { | ||
757 | S3I(s)->tmp.reuse_message = 1; | ||
758 | return (1); | ||
759 | } else { | ||
760 | /* Already sent a cookie. */ | ||
761 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
762 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
763 | SSL_R_BAD_MESSAGE_TYPE); | ||
764 | goto f_err; | ||
765 | } | ||
766 | } | ||
767 | } | ||
768 | |||
769 | if (S3I(s)->tmp.message_type != SSL3_MT_SERVER_HELLO) { | ||
770 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
771 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
772 | SSL_R_BAD_MESSAGE_TYPE); | ||
773 | goto f_err; | ||
774 | } | ||
775 | |||
776 | if (!CBS_get_u16(&cbs, &server_version)) | ||
777 | goto truncated; | ||
778 | |||
779 | if (ssl_supported_version_range(s, &min_version, &max_version) != 1) { | ||
780 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
781 | SSL_R_NO_PROTOCOLS_AVAILABLE); | ||
782 | goto err; | ||
783 | } | ||
784 | |||
785 | if (server_version < min_version || server_version > max_version) { | ||
786 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); | ||
787 | s->version = (s->version & 0xff00) | (server_version & 0xff); | ||
788 | al = SSL_AD_PROTOCOL_VERSION; | ||
789 | goto f_err; | ||
790 | } | ||
791 | s->version = server_version; | ||
792 | |||
793 | if ((method = tls1_get_client_method(server_version)) == NULL) | ||
794 | method = dtls1_get_client_method(server_version); | ||
795 | if (method == NULL) { | ||
796 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | ||
797 | goto err; | ||
798 | } | ||
799 | s->method = method; | ||
800 | |||
801 | /* Server random. */ | ||
802 | if (!CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE)) | ||
803 | goto truncated; | ||
804 | if (!CBS_write_bytes(&server_random, s->s3->server_random, | ||
805 | sizeof(s->s3->server_random), NULL)) | ||
806 | goto err; | ||
807 | |||
808 | /* Session ID. */ | ||
809 | if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) | ||
810 | goto truncated; | ||
811 | |||
812 | if ((CBS_len(&session_id) > sizeof(s->session->session_id)) || | ||
813 | (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE)) { | ||
814 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
815 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
816 | SSL_R_SSL3_SESSION_ID_TOO_LONG); | ||
817 | goto f_err; | ||
818 | } | ||
819 | |||
820 | /* Cipher suite. */ | ||
821 | if (!CBS_get_u16(&cbs, &cipher_suite)) | ||
822 | goto truncated; | ||
823 | |||
824 | /* | ||
825 | * Check if we want to resume the session based on external | ||
826 | * pre-shared secret. | ||
827 | */ | ||
828 | if (s->internal->tls_session_secret_cb) { | ||
829 | SSL_CIPHER *pref_cipher = NULL; | ||
830 | s->session->master_key_length = sizeof(s->session->master_key); | ||
831 | if (s->internal->tls_session_secret_cb(s, s->session->master_key, | ||
832 | &s->session->master_key_length, NULL, &pref_cipher, | ||
833 | s->internal->tls_session_secret_cb_arg)) { | ||
834 | s->session->cipher = pref_cipher ? pref_cipher : | ||
835 | ssl3_get_cipher_by_value(cipher_suite); | ||
836 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
837 | } | ||
838 | } | ||
839 | |||
840 | if (s->session->session_id_length != 0 && | ||
841 | CBS_mem_equal(&session_id, s->session->session_id, | ||
842 | s->session->session_id_length)) { | ||
843 | if (s->sid_ctx_length != s->session->sid_ctx_length || | ||
844 | timingsafe_memcmp(s->session->sid_ctx, | ||
845 | s->sid_ctx, s->sid_ctx_length) != 0) { | ||
846 | /* actually a client application bug */ | ||
847 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
848 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
849 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); | ||
850 | goto f_err; | ||
851 | } | ||
852 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | ||
853 | s->internal->hit = 1; | ||
854 | } else { | ||
855 | /* a miss or crap from the other end */ | ||
856 | |||
857 | /* If we were trying for session-id reuse, make a new | ||
858 | * SSL_SESSION so we don't stuff up other people */ | ||
859 | s->internal->hit = 0; | ||
860 | if (s->session->session_id_length > 0) { | ||
861 | if (!ssl_get_new_session(s, 0)) { | ||
862 | al = SSL_AD_INTERNAL_ERROR; | ||
863 | goto f_err; | ||
864 | } | ||
865 | } | ||
866 | |||
867 | /* | ||
868 | * XXX - improve the handling for the case where there is a | ||
869 | * zero length session identifier. | ||
870 | */ | ||
871 | if (!CBS_write_bytes(&session_id, s->session->session_id, | ||
872 | sizeof(s->session->session_id), &outlen)) | ||
873 | goto err; | ||
874 | s->session->session_id_length = outlen; | ||
875 | |||
876 | s->session->ssl_version = s->version; | ||
877 | } | ||
878 | |||
879 | if ((cipher = ssl3_get_cipher_by_value(cipher_suite)) == NULL) { | ||
880 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
881 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
882 | SSL_R_UNKNOWN_CIPHER_RETURNED); | ||
883 | goto f_err; | ||
884 | } | ||
885 | |||
886 | /* TLS v1.2 only ciphersuites require v1.2 or later. */ | ||
887 | if ((cipher->algorithm_ssl & SSL_TLSV1_2) && | ||
888 | (TLS1_get_version(s) < TLS1_2_VERSION)) { | ||
889 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
890 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
891 | SSL_R_WRONG_CIPHER_RETURNED); | ||
892 | goto f_err; | ||
893 | } | ||
894 | |||
895 | sk = ssl_get_ciphers_by_id(s); | ||
896 | i = sk_SSL_CIPHER_find(sk, cipher); | ||
897 | if (i < 0) { | ||
898 | /* we did not say we would use this cipher */ | ||
899 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
900 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
901 | SSL_R_WRONG_CIPHER_RETURNED); | ||
902 | goto f_err; | ||
903 | } | ||
904 | |||
905 | /* | ||
906 | * Depending on the session caching (internal/external), the cipher | ||
907 | * and/or cipher_id values may not be set. Make sure that | ||
908 | * cipher_id is set and use it for comparison. | ||
909 | */ | ||
910 | if (s->session->cipher) | ||
911 | s->session->cipher_id = s->session->cipher->id; | ||
912 | if (s->internal->hit && (s->session->cipher_id != cipher->id)) { | ||
913 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
914 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
915 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); | ||
916 | goto f_err; | ||
917 | } | ||
918 | S3I(s)->tmp.new_cipher = cipher; | ||
919 | |||
920 | /* | ||
921 | * Don't digest cached records if no sigalgs: we may need them for | ||
922 | * client authentication. | ||
923 | */ | ||
924 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
925 | if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) && | ||
926 | !tls1_digest_cached_records(s)) { | ||
927 | al = SSL_AD_INTERNAL_ERROR; | ||
928 | goto f_err; | ||
929 | } | ||
930 | |||
931 | if (!CBS_get_u8(&cbs, &compression_method)) | ||
932 | goto truncated; | ||
933 | |||
934 | if (compression_method != 0) { | ||
935 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
936 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | ||
937 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); | ||
938 | goto f_err; | ||
939 | } | ||
940 | |||
941 | /* TLS extensions. */ | ||
942 | p = (unsigned char *)CBS_data(&cbs); | ||
943 | if (!ssl_parse_serverhello_tlsext(s, &p, CBS_len(&cbs), &al)) { | ||
944 | /* 'al' set by ssl_parse_serverhello_tlsext */ | ||
945 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); | ||
946 | goto f_err; | ||
947 | } | ||
948 | if (ssl_check_serverhello_tlsext(s) <= 0) { | ||
949 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT); | ||
950 | goto err; | ||
951 | } | ||
952 | |||
953 | /* See if any data remains... */ | ||
954 | if (p - CBS_data(&cbs) != CBS_len(&cbs)) | ||
955 | goto truncated; | ||
956 | |||
957 | return (1); | ||
958 | |||
959 | truncated: | ||
960 | /* wrong packet length */ | ||
961 | al = SSL_AD_DECODE_ERROR; | ||
962 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); | ||
963 | f_err: | ||
964 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
965 | err: | ||
966 | return (-1); | ||
967 | } | ||
968 | |||
969 | int | ||
970 | ssl3_get_server_certificate(SSL *s) | ||
971 | { | ||
972 | int al, i, ok, ret = -1; | ||
973 | long n; | ||
974 | CBS cbs, cert_list; | ||
975 | X509 *x = NULL; | ||
976 | const unsigned char *q; | ||
977 | STACK_OF(X509) *sk = NULL; | ||
978 | SESS_CERT *sc; | ||
979 | EVP_PKEY *pkey = NULL; | ||
980 | |||
981 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_A, | ||
982 | SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list, &ok); | ||
983 | |||
984 | if (!ok) | ||
985 | return ((int)n); | ||
986 | |||
987 | if (S3I(s)->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { | ||
988 | S3I(s)->tmp.reuse_message = 1; | ||
989 | return (1); | ||
990 | } | ||
991 | |||
992 | if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE) { | ||
993 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
994 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
995 | SSL_R_BAD_MESSAGE_TYPE); | ||
996 | goto f_err; | ||
997 | } | ||
998 | |||
999 | |||
1000 | if ((sk = sk_X509_new_null()) == NULL) { | ||
1001 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1002 | ERR_R_MALLOC_FAILURE); | ||
1003 | goto err; | ||
1004 | } | ||
1005 | |||
1006 | if (n < 0) | ||
1007 | goto truncated; | ||
1008 | |||
1009 | CBS_init(&cbs, s->internal->init_msg, n); | ||
1010 | if (CBS_len(&cbs) < 3) | ||
1011 | goto truncated; | ||
1012 | |||
1013 | if (!CBS_get_u24_length_prefixed(&cbs, &cert_list) || | ||
1014 | CBS_len(&cbs) != 0) { | ||
1015 | al = SSL_AD_DECODE_ERROR; | ||
1016 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1017 | SSL_R_LENGTH_MISMATCH); | ||
1018 | goto f_err; | ||
1019 | } | ||
1020 | |||
1021 | while (CBS_len(&cert_list) > 0) { | ||
1022 | CBS cert; | ||
1023 | |||
1024 | if (CBS_len(&cert_list) < 3) | ||
1025 | goto truncated; | ||
1026 | if (!CBS_get_u24_length_prefixed(&cert_list, &cert)) { | ||
1027 | al = SSL_AD_DECODE_ERROR; | ||
1028 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1029 | SSL_R_CERT_LENGTH_MISMATCH); | ||
1030 | goto f_err; | ||
1031 | } | ||
1032 | |||
1033 | q = CBS_data(&cert); | ||
1034 | x = d2i_X509(NULL, &q, CBS_len(&cert)); | ||
1035 | if (x == NULL) { | ||
1036 | al = SSL_AD_BAD_CERTIFICATE; | ||
1037 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1038 | ERR_R_ASN1_LIB); | ||
1039 | goto f_err; | ||
1040 | } | ||
1041 | if (q != CBS_data(&cert) + CBS_len(&cert)) { | ||
1042 | al = SSL_AD_DECODE_ERROR; | ||
1043 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1044 | SSL_R_CERT_LENGTH_MISMATCH); | ||
1045 | goto f_err; | ||
1046 | } | ||
1047 | if (!sk_X509_push(sk, x)) { | ||
1048 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1049 | ERR_R_MALLOC_FAILURE); | ||
1050 | goto err; | ||
1051 | } | ||
1052 | x = NULL; | ||
1053 | } | ||
1054 | |||
1055 | i = ssl_verify_cert_chain(s, sk); | ||
1056 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) { | ||
1057 | al = ssl_verify_alarm_type(s->verify_result); | ||
1058 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1059 | SSL_R_CERTIFICATE_VERIFY_FAILED); | ||
1060 | goto f_err; | ||
1061 | |||
1062 | } | ||
1063 | ERR_clear_error(); /* but we keep s->verify_result */ | ||
1064 | |||
1065 | sc = ssl_sess_cert_new(); | ||
1066 | if (sc == NULL) | ||
1067 | goto err; | ||
1068 | ssl_sess_cert_free(SSI(s)->sess_cert); | ||
1069 | SSI(s)->sess_cert = sc; | ||
1070 | |||
1071 | sc->cert_chain = sk; | ||
1072 | /* | ||
1073 | * Inconsistency alert: cert_chain does include the peer's | ||
1074 | * certificate, which we don't include in s3_srvr.c | ||
1075 | */ | ||
1076 | x = sk_X509_value(sk, 0); | ||
1077 | sk = NULL; | ||
1078 | /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ | ||
1079 | |||
1080 | pkey = X509_get_pubkey(x); | ||
1081 | |||
1082 | if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { | ||
1083 | x = NULL; | ||
1084 | al = SSL3_AL_FATAL; | ||
1085 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1086 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); | ||
1087 | goto f_err; | ||
1088 | } | ||
1089 | |||
1090 | i = ssl_cert_type(x, pkey); | ||
1091 | if (i < 0) { | ||
1092 | x = NULL; | ||
1093 | al = SSL3_AL_FATAL; | ||
1094 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1095 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); | ||
1096 | goto f_err; | ||
1097 | } | ||
1098 | |||
1099 | sc->peer_cert_type = i; | ||
1100 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
1101 | /* | ||
1102 | * Why would the following ever happen? | ||
1103 | * We just created sc a couple of lines ago. | ||
1104 | */ | ||
1105 | X509_free(sc->peer_pkeys[i].x509); | ||
1106 | sc->peer_pkeys[i].x509 = x; | ||
1107 | sc->peer_key = &(sc->peer_pkeys[i]); | ||
1108 | |||
1109 | X509_free(s->session->peer); | ||
1110 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
1111 | s->session->peer = x; | ||
1112 | s->session->verify_result = s->verify_result; | ||
1113 | |||
1114 | x = NULL; | ||
1115 | ret = 1; | ||
1116 | |||
1117 | if (0) { | ||
1118 | truncated: | ||
1119 | /* wrong packet length */ | ||
1120 | al = SSL_AD_DECODE_ERROR; | ||
1121 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | ||
1122 | SSL_R_BAD_PACKET_LENGTH); | ||
1123 | f_err: | ||
1124 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1125 | } | ||
1126 | err: | ||
1127 | EVP_PKEY_free(pkey); | ||
1128 | X509_free(x); | ||
1129 | sk_X509_pop_free(sk, X509_free); | ||
1130 | |||
1131 | return (ret); | ||
1132 | } | ||
1133 | |||
1134 | static int | ||
1135 | ssl3_get_server_kex_dhe(SSL *s, EVP_PKEY **pkey, unsigned char **pp, long *nn) | ||
1136 | { | ||
1137 | CBS cbs, dhp, dhg, dhpk; | ||
1138 | BN_CTX *bn_ctx = NULL; | ||
1139 | SESS_CERT *sc = NULL; | ||
1140 | DH *dh = NULL; | ||
1141 | long alg_a; | ||
1142 | int al; | ||
1143 | |||
1144 | alg_a = S3I(s)->tmp.new_cipher->algorithm_auth; | ||
1145 | sc = SSI(s)->sess_cert; | ||
1146 | |||
1147 | if (*nn < 0) | ||
1148 | goto err; | ||
1149 | |||
1150 | CBS_init(&cbs, *pp, *nn); | ||
1151 | |||
1152 | if ((dh = DH_new()) == NULL) { | ||
1153 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
1154 | goto err; | ||
1155 | } | ||
1156 | |||
1157 | if (!CBS_get_u16_length_prefixed(&cbs, &dhp)) | ||
1158 | goto truncated; | ||
1159 | if ((dh->p = BN_bin2bn(CBS_data(&dhp), CBS_len(&dhp), NULL)) == NULL) { | ||
1160 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | ||
1161 | goto err; | ||
1162 | } | ||
1163 | |||
1164 | if (!CBS_get_u16_length_prefixed(&cbs, &dhg)) | ||
1165 | goto truncated; | ||
1166 | if ((dh->g = BN_bin2bn(CBS_data(&dhg), CBS_len(&dhg), NULL)) == NULL) { | ||
1167 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | ||
1168 | goto err; | ||
1169 | } | ||
1170 | |||
1171 | if (!CBS_get_u16_length_prefixed(&cbs, &dhpk)) | ||
1172 | goto truncated; | ||
1173 | if ((dh->pub_key = BN_bin2bn(CBS_data(&dhpk), CBS_len(&dhpk), | ||
1174 | NULL)) == NULL) { | ||
1175 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | ||
1176 | goto err; | ||
1177 | } | ||
1178 | |||
1179 | /* | ||
1180 | * Check the strength of the DH key just constructed. | ||
1181 | * Discard keys weaker than 1024 bits. | ||
1182 | */ | ||
1183 | if (DH_size(dh) < 1024 / 8) { | ||
1184 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); | ||
1185 | goto err; | ||
1186 | } | ||
1187 | |||
1188 | if (alg_a & SSL_aRSA) | ||
1189 | *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | ||
1190 | else if (alg_a & SSL_aDSS) | ||
1191 | *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); | ||
1192 | else | ||
1193 | /* XXX - Anonymous DH, so no certificate or pkey. */ | ||
1194 | *pkey = NULL; | ||
1195 | |||
1196 | sc->peer_dh_tmp = dh; | ||
1197 | |||
1198 | *nn = CBS_len(&cbs); | ||
1199 | *pp = (unsigned char *)CBS_data(&cbs); | ||
1200 | |||
1201 | return (1); | ||
1202 | |||
1203 | truncated: | ||
1204 | al = SSL_AD_DECODE_ERROR; | ||
1205 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1206 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1207 | |||
1208 | err: | ||
1209 | DH_free(dh); | ||
1210 | BN_CTX_free(bn_ctx); | ||
1211 | |||
1212 | return (-1); | ||
1213 | } | ||
1214 | |||
1215 | static int | ||
1216 | ssl3_get_server_kex_ecdhe_ecp(SSL *s, SESS_CERT *sc, int nid, CBS *public) | ||
1217 | { | ||
1218 | const EC_GROUP *group; | ||
1219 | EC_GROUP *ngroup = NULL; | ||
1220 | EC_POINT *point = NULL; | ||
1221 | BN_CTX *bn_ctx = NULL; | ||
1222 | EC_KEY *ecdh = NULL; | ||
1223 | int ret = -1; | ||
1224 | |||
1225 | /* | ||
1226 | * Extract the server's ephemeral ECDH public key. | ||
1227 | */ | ||
1228 | |||
1229 | if ((ecdh = EC_KEY_new()) == NULL) { | ||
1230 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | ||
1231 | goto err; | ||
1232 | } | ||
1233 | |||
1234 | if ((ngroup = EC_GROUP_new_by_curve_name(nid)) == NULL) { | ||
1235 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); | ||
1236 | goto err; | ||
1237 | } | ||
1238 | if (EC_KEY_set_group(ecdh, ngroup) == 0) { | ||
1239 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); | ||
1240 | goto err; | ||
1241 | } | ||
1242 | |||
1243 | group = EC_KEY_get0_group(ecdh); | ||
1244 | |||
1245 | if ((point = EC_POINT_new(group)) == NULL || | ||
1246 | (bn_ctx = BN_CTX_new()) == NULL) { | ||
1247 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | ||
1248 | goto err; | ||
1249 | } | ||
1250 | |||
1251 | if (EC_POINT_oct2point(group, point, CBS_data(public), | ||
1252 | CBS_len(public), bn_ctx) == 0) { | ||
1253 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); | ||
1254 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1255 | goto err; | ||
1256 | } | ||
1257 | |||
1258 | EC_KEY_set_public_key(ecdh, point); | ||
1259 | sc->peer_ecdh_tmp = ecdh; | ||
1260 | ecdh = NULL; | ||
1261 | |||
1262 | ret = 1; | ||
1263 | |||
1264 | err: | ||
1265 | BN_CTX_free(bn_ctx); | ||
1266 | EC_GROUP_free(ngroup); | ||
1267 | EC_POINT_free(point); | ||
1268 | EC_KEY_free(ecdh); | ||
1269 | |||
1270 | return (ret); | ||
1271 | } | ||
1272 | |||
1273 | static int | ||
1274 | ssl3_get_server_kex_ecdhe_ecx(SSL *s, SESS_CERT *sc, int nid, CBS *public) | ||
1275 | { | ||
1276 | size_t outlen; | ||
1277 | |||
1278 | if (nid != NID_X25519) { | ||
1279 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | ||
1280 | goto err; | ||
1281 | } | ||
1282 | |||
1283 | if (CBS_len(public) != X25519_KEY_LENGTH) { | ||
1284 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); | ||
1285 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1286 | goto err; | ||
1287 | } | ||
1288 | |||
1289 | if (!CBS_stow(public, &sc->peer_x25519_tmp, &outlen)) { | ||
1290 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | ||
1291 | goto err; | ||
1292 | } | ||
1293 | |||
1294 | return (1); | ||
1295 | |||
1296 | err: | ||
1297 | return (-1); | ||
1298 | } | ||
1299 | |||
1300 | static int | ||
1301 | ssl3_get_server_kex_ecdhe(SSL *s, EVP_PKEY **pkey, unsigned char **pp, long *nn) | ||
1302 | { | ||
1303 | CBS cbs, public; | ||
1304 | uint8_t curve_type; | ||
1305 | uint16_t curve_id; | ||
1306 | SESS_CERT *sc; | ||
1307 | long alg_a; | ||
1308 | int nid; | ||
1309 | int al; | ||
1310 | |||
1311 | alg_a = S3I(s)->tmp.new_cipher->algorithm_auth; | ||
1312 | sc = SSI(s)->sess_cert; | ||
1313 | |||
1314 | if (*nn < 0) | ||
1315 | goto err; | ||
1316 | |||
1317 | CBS_init(&cbs, *pp, *nn); | ||
1318 | |||
1319 | /* Only named curves are supported. */ | ||
1320 | if (!CBS_get_u8(&cbs, &curve_type) || | ||
1321 | curve_type != NAMED_CURVE_TYPE || | ||
1322 | !CBS_get_u16(&cbs, &curve_id)) { | ||
1323 | al = SSL_AD_DECODE_ERROR; | ||
1324 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | ||
1325 | goto f_err; | ||
1326 | } | ||
1327 | |||
1328 | /* | ||
1329 | * Check that the curve is one of our preferences - if it is not, | ||
1330 | * the server has sent us an invalid curve. | ||
1331 | */ | ||
1332 | if (tls1_check_curve(s, curve_id) != 1) { | ||
1333 | al = SSL_AD_DECODE_ERROR; | ||
1334 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); | ||
1335 | goto f_err; | ||
1336 | } | ||
1337 | |||
1338 | if ((nid = tls1_ec_curve_id2nid(curve_id)) == 0) { | ||
1339 | al = SSL_AD_INTERNAL_ERROR; | ||
1340 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1341 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); | ||
1342 | goto f_err; | ||
1343 | } | ||
1344 | |||
1345 | if (!CBS_get_u8_length_prefixed(&cbs, &public)) | ||
1346 | goto truncated; | ||
1347 | |||
1348 | if (nid == NID_X25519) { | ||
1349 | if (ssl3_get_server_kex_ecdhe_ecx(s, sc, nid, &public) != 1) | ||
1350 | goto err; | ||
1351 | } else { | ||
1352 | if (ssl3_get_server_kex_ecdhe_ecp(s, sc, nid, &public) != 1) | ||
1353 | goto err; | ||
1354 | } | ||
1355 | |||
1356 | /* | ||
1357 | * The ECC/TLS specification does not mention the use of DSA to sign | ||
1358 | * ECParameters in the server key exchange message. We do support RSA | ||
1359 | * and ECDSA. | ||
1360 | */ | ||
1361 | if (alg_a & SSL_aRSA) | ||
1362 | *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | ||
1363 | else if (alg_a & SSL_aECDSA) | ||
1364 | *pkey = X509_get_pubkey(sc->peer_pkeys[SSL_PKEY_ECC].x509); | ||
1365 | else | ||
1366 | /* XXX - Anonymous ECDH, so no certificate or pkey. */ | ||
1367 | *pkey = NULL; | ||
1368 | |||
1369 | *nn = CBS_len(&cbs); | ||
1370 | *pp = (unsigned char *)CBS_data(&cbs); | ||
1371 | |||
1372 | return (1); | ||
1373 | |||
1374 | truncated: | ||
1375 | al = SSL_AD_DECODE_ERROR; | ||
1376 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1377 | |||
1378 | f_err: | ||
1379 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1380 | |||
1381 | err: | ||
1382 | return (-1); | ||
1383 | } | ||
1384 | |||
1385 | int | ||
1386 | ssl3_get_server_key_exchange(SSL *s) | ||
1387 | { | ||
1388 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2]; | ||
1389 | EVP_MD_CTX md_ctx; | ||
1390 | unsigned char *param, *p; | ||
1391 | int al, i, j, param_len, ok; | ||
1392 | long n, alg_k, alg_a; | ||
1393 | EVP_PKEY *pkey = NULL; | ||
1394 | const EVP_MD *md = NULL; | ||
1395 | RSA *rsa = NULL; | ||
1396 | |||
1397 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
1398 | alg_a = S3I(s)->tmp.new_cipher->algorithm_auth; | ||
1399 | |||
1400 | /* | ||
1401 | * Use same message size as in ssl3_get_certificate_request() | ||
1402 | * as ServerKeyExchange message may be skipped. | ||
1403 | */ | ||
1404 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A, | ||
1405 | SSL3_ST_CR_KEY_EXCH_B, -1, s->internal->max_cert_list, &ok); | ||
1406 | if (!ok) | ||
1407 | return ((int)n); | ||
1408 | |||
1409 | EVP_MD_CTX_init(&md_ctx); | ||
1410 | |||
1411 | if (S3I(s)->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { | ||
1412 | /* | ||
1413 | * Do not skip server key exchange if this cipher suite uses | ||
1414 | * ephemeral keys. | ||
1415 | */ | ||
1416 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
1417 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1418 | SSL_R_UNEXPECTED_MESSAGE); | ||
1419 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1420 | goto f_err; | ||
1421 | } | ||
1422 | |||
1423 | S3I(s)->tmp.reuse_message = 1; | ||
1424 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1425 | return (1); | ||
1426 | } | ||
1427 | |||
1428 | if (SSI(s)->sess_cert != NULL) { | ||
1429 | DH_free(SSI(s)->sess_cert->peer_dh_tmp); | ||
1430 | SSI(s)->sess_cert->peer_dh_tmp = NULL; | ||
1431 | |||
1432 | EC_KEY_free(SSI(s)->sess_cert->peer_ecdh_tmp); | ||
1433 | SSI(s)->sess_cert->peer_ecdh_tmp = NULL; | ||
1434 | |||
1435 | free(SSI(s)->sess_cert->peer_x25519_tmp); | ||
1436 | SSI(s)->sess_cert->peer_x25519_tmp = NULL; | ||
1437 | } else { | ||
1438 | SSI(s)->sess_cert = ssl_sess_cert_new(); | ||
1439 | if (SSI(s)->sess_cert == NULL) | ||
1440 | goto err; | ||
1441 | } | ||
1442 | |||
1443 | param = p = (unsigned char *)s->internal->init_msg; | ||
1444 | param_len = n; | ||
1445 | |||
1446 | if (alg_k & SSL_kDHE) { | ||
1447 | if (ssl3_get_server_kex_dhe(s, &pkey, &p, &n) != 1) | ||
1448 | goto err; | ||
1449 | } else if (alg_k & SSL_kECDHE) { | ||
1450 | if (ssl3_get_server_kex_ecdhe(s, &pkey, &p, &n) != 1) | ||
1451 | goto err; | ||
1452 | } else if (alg_k != 0) { | ||
1453 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1454 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); | ||
1455 | goto f_err; | ||
1456 | } | ||
1457 | |||
1458 | param_len = param_len - n; | ||
1459 | |||
1460 | /* if it was signed, check the signature */ | ||
1461 | if (pkey != NULL) { | ||
1462 | if (SSL_USE_SIGALGS(s)) { | ||
1463 | int sigalg = tls12_get_sigid(pkey); | ||
1464 | /* Should never happen */ | ||
1465 | if (sigalg == -1) { | ||
1466 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1467 | ERR_R_INTERNAL_ERROR); | ||
1468 | goto err; | ||
1469 | } | ||
1470 | /* | ||
1471 | * Check key type is consistent | ||
1472 | * with signature | ||
1473 | */ | ||
1474 | if (2 > n) | ||
1475 | goto truncated; | ||
1476 | if (sigalg != (int)p[1]) { | ||
1477 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1478 | SSL_R_WRONG_SIGNATURE_TYPE); | ||
1479 | al = SSL_AD_DECODE_ERROR; | ||
1480 | goto f_err; | ||
1481 | } | ||
1482 | md = tls12_get_hash(p[0]); | ||
1483 | if (md == NULL) { | ||
1484 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1485 | SSL_R_UNKNOWN_DIGEST); | ||
1486 | al = SSL_AD_DECODE_ERROR; | ||
1487 | goto f_err; | ||
1488 | } | ||
1489 | p += 2; | ||
1490 | n -= 2; | ||
1491 | } else | ||
1492 | md = EVP_sha1(); | ||
1493 | |||
1494 | if (2 > n) | ||
1495 | goto truncated; | ||
1496 | n2s(p, i); | ||
1497 | n -= 2; | ||
1498 | j = EVP_PKEY_size(pkey); | ||
1499 | |||
1500 | if (i != n || n > j) { | ||
1501 | /* wrong packet length */ | ||
1502 | al = SSL_AD_DECODE_ERROR; | ||
1503 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1504 | SSL_R_WRONG_SIGNATURE_LENGTH); | ||
1505 | goto f_err; | ||
1506 | } | ||
1507 | |||
1508 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { | ||
1509 | int num; | ||
1510 | |||
1511 | j = 0; | ||
1512 | q = md_buf; | ||
1513 | for (num = 2; num > 0; num--) { | ||
1514 | if (!EVP_DigestInit_ex(&md_ctx, | ||
1515 | (num == 2) ? s->ctx->internal->md5 : | ||
1516 | s->ctx->internal->sha1, NULL)) { | ||
1517 | al = SSL_AD_INTERNAL_ERROR; | ||
1518 | goto f_err; | ||
1519 | } | ||
1520 | EVP_DigestUpdate(&md_ctx, | ||
1521 | s->s3->client_random, | ||
1522 | SSL3_RANDOM_SIZE); | ||
1523 | EVP_DigestUpdate(&md_ctx, | ||
1524 | s->s3->server_random, | ||
1525 | SSL3_RANDOM_SIZE); | ||
1526 | EVP_DigestUpdate(&md_ctx, param, param_len); | ||
1527 | EVP_DigestFinal_ex(&md_ctx, q, | ||
1528 | (unsigned int *)&i); | ||
1529 | q += i; | ||
1530 | j += i; | ||
1531 | } | ||
1532 | i = RSA_verify(NID_md5_sha1, md_buf, j, | ||
1533 | p, n, pkey->pkey.rsa); | ||
1534 | if (i < 0) { | ||
1535 | al = SSL_AD_DECRYPT_ERROR; | ||
1536 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1537 | SSL_R_BAD_RSA_DECRYPT); | ||
1538 | goto f_err; | ||
1539 | } | ||
1540 | if (i == 0) { | ||
1541 | /* bad signature */ | ||
1542 | al = SSL_AD_DECRYPT_ERROR; | ||
1543 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1544 | SSL_R_BAD_SIGNATURE); | ||
1545 | goto f_err; | ||
1546 | } | ||
1547 | } else { | ||
1548 | EVP_VerifyInit_ex(&md_ctx, md, NULL); | ||
1549 | EVP_VerifyUpdate(&md_ctx, s->s3->client_random, | ||
1550 | SSL3_RANDOM_SIZE); | ||
1551 | EVP_VerifyUpdate(&md_ctx, s->s3->server_random, | ||
1552 | SSL3_RANDOM_SIZE); | ||
1553 | EVP_VerifyUpdate(&md_ctx, param, param_len); | ||
1554 | if (EVP_VerifyFinal(&md_ctx, p,(int)n, pkey) <= 0) { | ||
1555 | /* bad signature */ | ||
1556 | al = SSL_AD_DECRYPT_ERROR; | ||
1557 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1558 | SSL_R_BAD_SIGNATURE); | ||
1559 | goto f_err; | ||
1560 | } | ||
1561 | } | ||
1562 | } else { | ||
1563 | /* aNULL does not need public keys. */ | ||
1564 | if (!(alg_a & SSL_aNULL)) { | ||
1565 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1566 | ERR_R_INTERNAL_ERROR); | ||
1567 | goto err; | ||
1568 | } | ||
1569 | /* still data left over */ | ||
1570 | if (n != 0) { | ||
1571 | al = SSL_AD_DECODE_ERROR; | ||
1572 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | ||
1573 | SSL_R_EXTRA_DATA_IN_MESSAGE); | ||
1574 | goto f_err; | ||
1575 | } | ||
1576 | } | ||
1577 | |||
1578 | EVP_PKEY_free(pkey); | ||
1579 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1580 | |||
1581 | return (1); | ||
1582 | |||
1583 | truncated: | ||
1584 | /* wrong packet length */ | ||
1585 | al = SSL_AD_DECODE_ERROR; | ||
1586 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_PACKET_LENGTH); | ||
1587 | |||
1588 | f_err: | ||
1589 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1590 | |||
1591 | err: | ||
1592 | EVP_PKEY_free(pkey); | ||
1593 | RSA_free(rsa); | ||
1594 | EVP_MD_CTX_cleanup(&md_ctx); | ||
1595 | |||
1596 | return (-1); | ||
1597 | } | ||
1598 | |||
1599 | int | ||
1600 | ssl3_get_certificate_request(SSL *s) | ||
1601 | { | ||
1602 | int ok, ret = 0; | ||
1603 | long n; | ||
1604 | uint8_t ctype_num; | ||
1605 | CBS cert_request, ctypes, rdn_list; | ||
1606 | X509_NAME *xn = NULL; | ||
1607 | const unsigned char *q; | ||
1608 | STACK_OF(X509_NAME) *ca_sk = NULL; | ||
1609 | |||
1610 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A, | ||
1611 | SSL3_ST_CR_CERT_REQ_B, -1, s->internal->max_cert_list, &ok); | ||
1612 | |||
1613 | if (!ok) | ||
1614 | return ((int)n); | ||
1615 | |||
1616 | S3I(s)->tmp.cert_req = 0; | ||
1617 | |||
1618 | if (S3I(s)->tmp.message_type == SSL3_MT_SERVER_DONE) { | ||
1619 | S3I(s)->tmp.reuse_message = 1; | ||
1620 | /* | ||
1621 | * If we get here we don't need any cached handshake records | ||
1622 | * as we wont be doing client auth. | ||
1623 | */ | ||
1624 | if (S3I(s)->handshake_buffer) { | ||
1625 | if (!tls1_digest_cached_records(s)) | ||
1626 | goto err; | ||
1627 | } | ||
1628 | return (1); | ||
1629 | } | ||
1630 | |||
1631 | if (S3I(s)->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { | ||
1632 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
1633 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1634 | SSL_R_WRONG_MESSAGE_TYPE); | ||
1635 | goto err; | ||
1636 | } | ||
1637 | |||
1638 | /* TLS does not like anon-DH with client cert */ | ||
1639 | if (S3I(s)->tmp.new_cipher->algorithm_auth & SSL_aNULL) { | ||
1640 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
1641 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1642 | SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); | ||
1643 | goto err; | ||
1644 | } | ||
1645 | |||
1646 | if (n < 0) | ||
1647 | goto truncated; | ||
1648 | CBS_init(&cert_request, s->internal->init_msg, n); | ||
1649 | |||
1650 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | ||
1651 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1652 | ERR_R_MALLOC_FAILURE); | ||
1653 | goto err; | ||
1654 | } | ||
1655 | |||
1656 | /* get the certificate types */ | ||
1657 | if (!CBS_get_u8(&cert_request, &ctype_num)) | ||
1658 | goto truncated; | ||
1659 | |||
1660 | if (ctype_num > SSL3_CT_NUMBER) | ||
1661 | ctype_num = SSL3_CT_NUMBER; | ||
1662 | if (!CBS_get_bytes(&cert_request, &ctypes, ctype_num) || | ||
1663 | !CBS_write_bytes(&ctypes, (uint8_t *)S3I(s)->tmp.ctype, | ||
1664 | sizeof(S3I(s)->tmp.ctype), NULL)) { | ||
1665 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1666 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1667 | goto err; | ||
1668 | } | ||
1669 | |||
1670 | if (SSL_USE_SIGALGS(s)) { | ||
1671 | CBS sigalgs; | ||
1672 | |||
1673 | if (CBS_len(&cert_request) < 2) { | ||
1674 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1675 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1676 | goto err; | ||
1677 | } | ||
1678 | |||
1679 | /* Check we have enough room for signature algorithms and | ||
1680 | * following length value. | ||
1681 | */ | ||
1682 | if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) { | ||
1683 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1684 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1685 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1686 | goto err; | ||
1687 | } | ||
1688 | if ((CBS_len(&sigalgs) & 1) || | ||
1689 | !tls1_process_sigalgs(s, CBS_data(&sigalgs), | ||
1690 | CBS_len(&sigalgs))) { | ||
1691 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1692 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1693 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); | ||
1694 | goto err; | ||
1695 | } | ||
1696 | } | ||
1697 | |||
1698 | /* get the CA RDNs */ | ||
1699 | if (CBS_len(&cert_request) < 2) { | ||
1700 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1701 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1702 | goto err; | ||
1703 | } | ||
1704 | |||
1705 | if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) || | ||
1706 | CBS_len(&cert_request) != 0) { | ||
1707 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1708 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1709 | SSL_R_LENGTH_MISMATCH); | ||
1710 | goto err; | ||
1711 | } | ||
1712 | |||
1713 | while (CBS_len(&rdn_list) > 0) { | ||
1714 | CBS rdn; | ||
1715 | |||
1716 | if (CBS_len(&rdn_list) < 2) { | ||
1717 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1718 | SSL_R_DATA_LENGTH_TOO_LONG); | ||
1719 | goto err; | ||
1720 | } | ||
1721 | |||
1722 | if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) { | ||
1723 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1724 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1725 | SSL_R_CA_DN_TOO_LONG); | ||
1726 | goto err; | ||
1727 | } | ||
1728 | |||
1729 | q = CBS_data(&rdn); | ||
1730 | if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) { | ||
1731 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1732 | SSL_AD_DECODE_ERROR); | ||
1733 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1734 | ERR_R_ASN1_LIB); | ||
1735 | goto err; | ||
1736 | } | ||
1737 | |||
1738 | if (q != CBS_data(&rdn) + CBS_len(&rdn)) { | ||
1739 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1740 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1741 | SSL_R_CA_DN_LENGTH_MISMATCH); | ||
1742 | goto err; | ||
1743 | } | ||
1744 | if (!sk_X509_NAME_push(ca_sk, xn)) { | ||
1745 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1746 | ERR_R_MALLOC_FAILURE); | ||
1747 | goto err; | ||
1748 | } | ||
1749 | xn = NULL; /* avoid free in err block */ | ||
1750 | } | ||
1751 | |||
1752 | /* we should setup a certificate to return.... */ | ||
1753 | S3I(s)->tmp.cert_req = 1; | ||
1754 | S3I(s)->tmp.ctype_num = ctype_num; | ||
1755 | sk_X509_NAME_pop_free(S3I(s)->tmp.ca_names, X509_NAME_free); | ||
1756 | S3I(s)->tmp.ca_names = ca_sk; | ||
1757 | ca_sk = NULL; | ||
1758 | |||
1759 | ret = 1; | ||
1760 | if (0) { | ||
1761 | truncated: | ||
1762 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | ||
1763 | SSL_R_BAD_PACKET_LENGTH); | ||
1764 | } | ||
1765 | err: | ||
1766 | X509_NAME_free(xn); | ||
1767 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); | ||
1768 | return (ret); | ||
1769 | } | ||
1770 | |||
1771 | static int | ||
1772 | ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) | ||
1773 | { | ||
1774 | return (X509_NAME_cmp(*a, *b)); | ||
1775 | } | ||
1776 | |||
1777 | int | ||
1778 | ssl3_get_new_session_ticket(SSL *s) | ||
1779 | { | ||
1780 | int ok, al, ret = 0; | ||
1781 | uint32_t lifetime_hint; | ||
1782 | long n; | ||
1783 | CBS cbs, session_ticket; | ||
1784 | |||
1785 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SESSION_TICKET_A, | ||
1786 | SSL3_ST_CR_SESSION_TICKET_B, -1, 16384, &ok); | ||
1787 | if (!ok) | ||
1788 | return ((int)n); | ||
1789 | |||
1790 | if (S3I(s)->tmp.message_type == SSL3_MT_FINISHED) { | ||
1791 | S3I(s)->tmp.reuse_message = 1; | ||
1792 | return (1); | ||
1793 | } | ||
1794 | if (S3I(s)->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) { | ||
1795 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1796 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1797 | SSL_R_BAD_MESSAGE_TYPE); | ||
1798 | goto f_err; | ||
1799 | } | ||
1800 | |||
1801 | if (n < 0) { | ||
1802 | al = SSL_AD_DECODE_ERROR; | ||
1803 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1804 | SSL_R_LENGTH_MISMATCH); | ||
1805 | goto f_err; | ||
1806 | } | ||
1807 | |||
1808 | CBS_init(&cbs, s->internal->init_msg, n); | ||
1809 | if (!CBS_get_u32(&cbs, &lifetime_hint) || | ||
1810 | #if UINT32_MAX > LONG_MAX | ||
1811 | lifetime_hint > LONG_MAX || | ||
1812 | #endif | ||
1813 | !CBS_get_u16_length_prefixed(&cbs, &session_ticket) || | ||
1814 | CBS_len(&cbs) != 0) { | ||
1815 | al = SSL_AD_DECODE_ERROR; | ||
1816 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1817 | SSL_R_LENGTH_MISMATCH); | ||
1818 | goto f_err; | ||
1819 | } | ||
1820 | s->session->tlsext_tick_lifetime_hint = (long)lifetime_hint; | ||
1821 | |||
1822 | if (!CBS_stow(&session_ticket, &s->session->tlsext_tick, | ||
1823 | &s->session->tlsext_ticklen)) { | ||
1824 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, | ||
1825 | ERR_R_MALLOC_FAILURE); | ||
1826 | goto err; | ||
1827 | } | ||
1828 | |||
1829 | /* | ||
1830 | * There are two ways to detect a resumed ticket sesion. | ||
1831 | * One is to set an appropriate session ID and then the server | ||
1832 | * must return a match in ServerHello. This allows the normal | ||
1833 | * client session ID matching to work and we know much | ||
1834 | * earlier that the ticket has been accepted. | ||
1835 | * | ||
1836 | * The other way is to set zero length session ID when the | ||
1837 | * ticket is presented and rely on the handshake to determine | ||
1838 | * session resumption. | ||
1839 | * | ||
1840 | * We choose the former approach because this fits in with | ||
1841 | * assumptions elsewhere in OpenSSL. The session ID is set | ||
1842 | * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the | ||
1843 | * ticket. | ||
1844 | */ | ||
1845 | EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket), | ||
1846 | s->session->session_id, &s->session->session_id_length, | ||
1847 | EVP_sha256(), NULL); | ||
1848 | ret = 1; | ||
1849 | return (ret); | ||
1850 | f_err: | ||
1851 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1852 | err: | ||
1853 | return (-1); | ||
1854 | } | ||
1855 | |||
1856 | int | ||
1857 | ssl3_get_cert_status(SSL *s) | ||
1858 | { | ||
1859 | CBS cert_status, response; | ||
1860 | size_t stow_len; | ||
1861 | int ok, al; | ||
1862 | long n; | ||
1863 | uint8_t status_type; | ||
1864 | |||
1865 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_STATUS_A, | ||
1866 | SSL3_ST_CR_CERT_STATUS_B, SSL3_MT_CERTIFICATE_STATUS, | ||
1867 | 16384, &ok); | ||
1868 | |||
1869 | if (!ok) | ||
1870 | return ((int)n); | ||
1871 | |||
1872 | if (n < 0) { | ||
1873 | /* need at least status type + length */ | ||
1874 | al = SSL_AD_DECODE_ERROR; | ||
1875 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1876 | SSL_R_LENGTH_MISMATCH); | ||
1877 | goto f_err; | ||
1878 | } | ||
1879 | |||
1880 | CBS_init(&cert_status, s->internal->init_msg, n); | ||
1881 | if (!CBS_get_u8(&cert_status, &status_type) || | ||
1882 | CBS_len(&cert_status) < 3) { | ||
1883 | /* need at least status type + length */ | ||
1884 | al = SSL_AD_DECODE_ERROR; | ||
1885 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1886 | SSL_R_LENGTH_MISMATCH); | ||
1887 | goto f_err; | ||
1888 | } | ||
1889 | |||
1890 | if (status_type != TLSEXT_STATUSTYPE_ocsp) { | ||
1891 | al = SSL_AD_DECODE_ERROR; | ||
1892 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1893 | SSL_R_UNSUPPORTED_STATUS_TYPE); | ||
1894 | goto f_err; | ||
1895 | } | ||
1896 | |||
1897 | if (!CBS_get_u24_length_prefixed(&cert_status, &response) || | ||
1898 | CBS_len(&cert_status) != 0) { | ||
1899 | al = SSL_AD_DECODE_ERROR; | ||
1900 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1901 | SSL_R_LENGTH_MISMATCH); | ||
1902 | goto f_err; | ||
1903 | } | ||
1904 | |||
1905 | if (!CBS_stow(&response, &s->internal->tlsext_ocsp_resp, | ||
1906 | &stow_len) || stow_len > INT_MAX) { | ||
1907 | s->internal->tlsext_ocsp_resplen = 0; | ||
1908 | al = SSL_AD_INTERNAL_ERROR; | ||
1909 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1910 | ERR_R_MALLOC_FAILURE); | ||
1911 | goto f_err; | ||
1912 | } | ||
1913 | s->internal->tlsext_ocsp_resplen = (int)stow_len; | ||
1914 | |||
1915 | if (s->ctx->internal->tlsext_status_cb) { | ||
1916 | int ret; | ||
1917 | ret = s->ctx->internal->tlsext_status_cb(s, | ||
1918 | s->ctx->internal->tlsext_status_arg); | ||
1919 | if (ret == 0) { | ||
1920 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | ||
1921 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1922 | SSL_R_INVALID_STATUS_RESPONSE); | ||
1923 | goto f_err; | ||
1924 | } | ||
1925 | if (ret < 0) { | ||
1926 | al = SSL_AD_INTERNAL_ERROR; | ||
1927 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, | ||
1928 | ERR_R_MALLOC_FAILURE); | ||
1929 | goto f_err; | ||
1930 | } | ||
1931 | } | ||
1932 | return (1); | ||
1933 | f_err: | ||
1934 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1935 | return (-1); | ||
1936 | } | ||
1937 | |||
1938 | int | ||
1939 | ssl3_get_server_done(SSL *s) | ||
1940 | { | ||
1941 | int ok, ret = 0; | ||
1942 | long n; | ||
1943 | |||
1944 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A, | ||
1945 | SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE, | ||
1946 | 30, /* should be very small, like 0 :-) */ &ok); | ||
1947 | |||
1948 | if (!ok) | ||
1949 | return ((int)n); | ||
1950 | if (n > 0) { | ||
1951 | /* should contain no data */ | ||
1952 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
1953 | SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); | ||
1954 | return (-1); | ||
1955 | } | ||
1956 | ret = 1; | ||
1957 | return (ret); | ||
1958 | } | ||
1959 | |||
1960 | static int | ||
1961 | ssl3_send_client_kex_rsa(SSL *s, SESS_CERT *sess_cert, CBB *cbb) | ||
1962 | { | ||
1963 | unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH]; | ||
1964 | unsigned char *enc_pms = NULL; | ||
1965 | EVP_PKEY *pkey = NULL; | ||
1966 | int ret = -1; | ||
1967 | int enc_len; | ||
1968 | CBB epms; | ||
1969 | |||
1970 | /* | ||
1971 | * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1. | ||
1972 | */ | ||
1973 | |||
1974 | pkey = X509_get_pubkey(sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | ||
1975 | if (pkey == NULL || pkey->type != EVP_PKEY_RSA || | ||
1976 | pkey->pkey.rsa == NULL) { | ||
1977 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1978 | ERR_R_INTERNAL_ERROR); | ||
1979 | goto err; | ||
1980 | } | ||
1981 | |||
1982 | pms[0] = s->client_version >> 8; | ||
1983 | pms[1] = s->client_version & 0xff; | ||
1984 | arc4random_buf(&pms[2], sizeof(pms) - 2); | ||
1985 | |||
1986 | if ((enc_pms = malloc(RSA_size(pkey->pkey.rsa))) == NULL) { | ||
1987 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1988 | ERR_R_MALLOC_FAILURE); | ||
1989 | goto err; | ||
1990 | } | ||
1991 | |||
1992 | enc_len = RSA_public_encrypt(sizeof(pms), pms, enc_pms, pkey->pkey.rsa, | ||
1993 | RSA_PKCS1_PADDING); | ||
1994 | if (enc_len <= 0) { | ||
1995 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
1996 | SSL_R_BAD_RSA_ENCRYPT); | ||
1997 | goto err; | ||
1998 | } | ||
1999 | |||
2000 | if (!CBB_add_u16_length_prefixed(cbb, &epms)) | ||
2001 | goto err; | ||
2002 | if (!CBB_add_bytes(&epms, enc_pms, enc_len)) | ||
2003 | goto err; | ||
2004 | if (!CBB_flush(cbb)) | ||
2005 | goto err; | ||
2006 | |||
2007 | s->session->master_key_length = | ||
2008 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
2009 | s->session->master_key, pms, sizeof(pms)); | ||
2010 | |||
2011 | ret = 1; | ||
2012 | |||
2013 | err: | ||
2014 | explicit_bzero(pms, sizeof(pms)); | ||
2015 | EVP_PKEY_free(pkey); | ||
2016 | free(enc_pms); | ||
2017 | |||
2018 | return (ret); | ||
2019 | } | ||
2020 | |||
2021 | static int | ||
2022 | ssl3_send_client_kex_dhe(SSL *s, SESS_CERT *sess_cert, CBB *cbb) | ||
2023 | { | ||
2024 | DH *dh_srvr = NULL, *dh_clnt = NULL; | ||
2025 | unsigned char *key = NULL; | ||
2026 | int key_size = 0, key_len; | ||
2027 | unsigned char *data; | ||
2028 | int ret = -1; | ||
2029 | CBB dh_Yc; | ||
2030 | |||
2031 | /* Ensure that we have an ephemeral key for DHE. */ | ||
2032 | if (sess_cert->peer_dh_tmp == NULL) { | ||
2033 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | ||
2034 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2035 | SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); | ||
2036 | goto err; | ||
2037 | } | ||
2038 | dh_srvr = sess_cert->peer_dh_tmp; | ||
2039 | |||
2040 | /* Generate a new random key. */ | ||
2041 | if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { | ||
2042 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
2043 | goto err; | ||
2044 | } | ||
2045 | if (!DH_generate_key(dh_clnt)) { | ||
2046 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
2047 | goto err; | ||
2048 | } | ||
2049 | key_size = DH_size(dh_clnt); | ||
2050 | if ((key = malloc(key_size)) == NULL) { | ||
2051 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2052 | ERR_R_MALLOC_FAILURE); | ||
2053 | goto err; | ||
2054 | } | ||
2055 | key_len = DH_compute_key(key, dh_srvr->pub_key, dh_clnt); | ||
2056 | if (key_len <= 0) { | ||
2057 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | ||
2058 | goto err; | ||
2059 | } | ||
2060 | |||
2061 | /* Generate master key from the result. */ | ||
2062 | s->session->master_key_length = | ||
2063 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
2064 | s->session->master_key, key, key_len); | ||
2065 | |||
2066 | if (!CBB_add_u16_length_prefixed(cbb, &dh_Yc)) | ||
2067 | goto err; | ||
2068 | if (!CBB_add_space(&dh_Yc, &data, BN_num_bytes(dh_clnt->pub_key))) | ||
2069 | goto err; | ||
2070 | BN_bn2bin(dh_clnt->pub_key, data); | ||
2071 | if (!CBB_flush(cbb)) | ||
2072 | goto err; | ||
2073 | |||
2074 | ret = 1; | ||
2075 | |||
2076 | err: | ||
2077 | DH_free(dh_clnt); | ||
2078 | if (key != NULL) | ||
2079 | explicit_bzero(key, key_size); | ||
2080 | free(key); | ||
2081 | |||
2082 | return (ret); | ||
2083 | } | ||
2084 | |||
2085 | static int | ||
2086 | ssl3_send_client_kex_ecdhe_ecp(SSL *s, SESS_CERT *sc, CBB *cbb) | ||
2087 | { | ||
2088 | const EC_GROUP *group = NULL; | ||
2089 | const EC_POINT *point = NULL; | ||
2090 | EC_KEY *ecdh = NULL; | ||
2091 | BN_CTX *bn_ctx = NULL; | ||
2092 | unsigned char *key = NULL; | ||
2093 | unsigned char *data; | ||
2094 | size_t encoded_len; | ||
2095 | int key_size = 0, key_len; | ||
2096 | int ret = -1; | ||
2097 | CBB ecpoint; | ||
2098 | |||
2099 | if ((group = EC_KEY_get0_group(sc->peer_ecdh_tmp)) == NULL || | ||
2100 | (point = EC_KEY_get0_public_key(sc->peer_ecdh_tmp)) == NULL) { | ||
2101 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2102 | ERR_R_INTERNAL_ERROR); | ||
2103 | goto err; | ||
2104 | } | ||
2105 | |||
2106 | if ((ecdh = EC_KEY_new()) == NULL) { | ||
2107 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2108 | ERR_R_MALLOC_FAILURE); | ||
2109 | goto err; | ||
2110 | } | ||
2111 | |||
2112 | if (!EC_KEY_set_group(ecdh, group)) { | ||
2113 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | ||
2114 | goto err; | ||
2115 | } | ||
2116 | |||
2117 | /* Generate a new ECDH key pair. */ | ||
2118 | if (!(EC_KEY_generate_key(ecdh))) { | ||
2119 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
2120 | goto err; | ||
2121 | } | ||
2122 | if ((key_size = ECDH_size(ecdh)) <= 0) { | ||
2123 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
2124 | goto err; | ||
2125 | } | ||
2126 | if ((key = malloc(key_size)) == NULL) { | ||
2127 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2128 | ERR_R_MALLOC_FAILURE); | ||
2129 | } | ||
2130 | key_len = ECDH_compute_key(key, key_size, point, ecdh, NULL); | ||
2131 | if (key_len <= 0) { | ||
2132 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
2133 | goto err; | ||
2134 | } | ||
2135 | |||
2136 | /* Generate master key from the result. */ | ||
2137 | s->session->master_key_length = | ||
2138 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
2139 | s->session->master_key, key, key_len); | ||
2140 | |||
2141 | encoded_len = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), | ||
2142 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); | ||
2143 | if (encoded_len == 0) { | ||
2144 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | ||
2145 | goto err; | ||
2146 | } | ||
2147 | |||
2148 | if ((bn_ctx = BN_CTX_new()) == NULL) { | ||
2149 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2150 | ERR_R_MALLOC_FAILURE); | ||
2151 | goto err; | ||
2152 | } | ||
2153 | |||
2154 | /* Encode the public key. */ | ||
2155 | if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) | ||
2156 | goto err; | ||
2157 | if (!CBB_add_space(&ecpoint, &data, encoded_len)) | ||
2158 | goto err; | ||
2159 | if (EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), | ||
2160 | POINT_CONVERSION_UNCOMPRESSED, data, encoded_len, | ||
2161 | bn_ctx) == 0) | ||
2162 | goto err; | ||
2163 | if (!CBB_flush(cbb)) | ||
2164 | goto err; | ||
2165 | |||
2166 | ret = 1; | ||
2167 | |||
2168 | err: | ||
2169 | if (key != NULL) | ||
2170 | explicit_bzero(key, key_size); | ||
2171 | free(key); | ||
2172 | |||
2173 | BN_CTX_free(bn_ctx); | ||
2174 | EC_KEY_free(ecdh); | ||
2175 | |||
2176 | return (ret); | ||
2177 | } | ||
2178 | |||
2179 | static int | ||
2180 | ssl3_send_client_kex_ecdhe_ecx(SSL *s, SESS_CERT *sc, CBB *cbb) | ||
2181 | { | ||
2182 | uint8_t *public_key = NULL, *private_key = NULL, *shared_key = NULL; | ||
2183 | int ret = -1; | ||
2184 | CBB ecpoint; | ||
2185 | |||
2186 | /* Generate X25519 key pair and derive shared key. */ | ||
2187 | if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
2188 | goto err; | ||
2189 | if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
2190 | goto err; | ||
2191 | if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL) | ||
2192 | goto err; | ||
2193 | X25519_keypair(public_key, private_key); | ||
2194 | if (!X25519(shared_key, private_key, sc->peer_x25519_tmp)) | ||
2195 | goto err; | ||
2196 | |||
2197 | /* Serialize the public key. */ | ||
2198 | if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) | ||
2199 | goto err; | ||
2200 | if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH)) | ||
2201 | goto err; | ||
2202 | if (!CBB_flush(cbb)) | ||
2203 | goto err; | ||
2204 | |||
2205 | /* Generate master key from the result. */ | ||
2206 | s->session->master_key_length = | ||
2207 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
2208 | s->session->master_key, shared_key, X25519_KEY_LENGTH); | ||
2209 | |||
2210 | ret = 1; | ||
2211 | |||
2212 | err: | ||
2213 | if (private_key != NULL) | ||
2214 | explicit_bzero(private_key, X25519_KEY_LENGTH); | ||
2215 | if (shared_key != NULL) | ||
2216 | explicit_bzero(shared_key, X25519_KEY_LENGTH); | ||
2217 | |||
2218 | free(public_key); | ||
2219 | free(private_key); | ||
2220 | free(shared_key); | ||
2221 | |||
2222 | return (ret); | ||
2223 | } | ||
2224 | |||
2225 | static int | ||
2226 | ssl3_send_client_kex_ecdhe(SSL *s, SESS_CERT *sc, CBB *cbb) | ||
2227 | { | ||
2228 | if (sc->peer_x25519_tmp != NULL) { | ||
2229 | if (ssl3_send_client_kex_ecdhe_ecx(s, sc, cbb) != 1) | ||
2230 | goto err; | ||
2231 | } else if (sc->peer_ecdh_tmp != NULL) { | ||
2232 | if (ssl3_send_client_kex_ecdhe_ecp(s, sc, cbb) != 1) | ||
2233 | goto err; | ||
2234 | } else { | ||
2235 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | ||
2236 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2237 | ERR_R_INTERNAL_ERROR); | ||
2238 | goto err; | ||
2239 | } | ||
2240 | |||
2241 | return (1); | ||
2242 | |||
2243 | err: | ||
2244 | return (-1); | ||
2245 | } | ||
2246 | |||
2247 | static int | ||
2248 | ssl3_send_client_kex_gost(SSL *s, SESS_CERT *sess_cert, CBB *cbb) | ||
2249 | { | ||
2250 | unsigned char premaster_secret[32], shared_ukm[32], tmp[256]; | ||
2251 | EVP_PKEY *pub_key = NULL; | ||
2252 | EVP_PKEY_CTX *pkey_ctx; | ||
2253 | X509 *peer_cert; | ||
2254 | size_t msglen; | ||
2255 | unsigned int md_len; | ||
2256 | EVP_MD_CTX *ukm_hash; | ||
2257 | int ret = -1; | ||
2258 | int nid; | ||
2259 | CBB gostblob; | ||
2260 | |||
2261 | /* Get server sertificate PKEY and create ctx from it */ | ||
2262 | peer_cert = sess_cert->peer_pkeys[SSL_PKEY_GOST01].x509; | ||
2263 | if (peer_cert == NULL) { | ||
2264 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2265 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | ||
2266 | goto err; | ||
2267 | } | ||
2268 | |||
2269 | pub_key = X509_get_pubkey(peer_cert); | ||
2270 | pkey_ctx = EVP_PKEY_CTX_new(pub_key, NULL); | ||
2271 | |||
2272 | /* | ||
2273 | * If we have send a certificate, and certificate key parameters match | ||
2274 | * those of server certificate, use certificate key for key exchange. | ||
2275 | * Otherwise, generate ephemeral key pair. | ||
2276 | */ | ||
2277 | EVP_PKEY_encrypt_init(pkey_ctx); | ||
2278 | |||
2279 | /* Generate session key. */ | ||
2280 | arc4random_buf(premaster_secret, 32); | ||
2281 | |||
2282 | /* | ||
2283 | * If we have client certificate, use its secret as peer key. | ||
2284 | */ | ||
2285 | if (S3I(s)->tmp.cert_req && s->cert->key->privatekey) { | ||
2286 | if (EVP_PKEY_derive_set_peer(pkey_ctx, | ||
2287 | s->cert->key->privatekey) <=0) { | ||
2288 | /* | ||
2289 | * If there was an error - just ignore it. | ||
2290 | * Ephemeral key would be used. | ||
2291 | */ | ||
2292 | ERR_clear_error(); | ||
2293 | } | ||
2294 | } | ||
2295 | |||
2296 | /* | ||
2297 | * Compute shared IV and store it in algorithm-specific context data. | ||
2298 | */ | ||
2299 | ukm_hash = EVP_MD_CTX_create(); | ||
2300 | if (ukm_hash == NULL) { | ||
2301 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2302 | ERR_R_MALLOC_FAILURE); | ||
2303 | goto err; | ||
2304 | } | ||
2305 | |||
2306 | if (ssl_get_algorithm2(s) & SSL_HANDSHAKE_MAC_GOST94) | ||
2307 | nid = NID_id_GostR3411_94; | ||
2308 | else | ||
2309 | nid = NID_id_tc26_gost3411_2012_256; | ||
2310 | if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid))) | ||
2311 | goto err; | ||
2312 | EVP_DigestUpdate(ukm_hash, s->s3->client_random, SSL3_RANDOM_SIZE); | ||
2313 | EVP_DigestUpdate(ukm_hash, s->s3->server_random, SSL3_RANDOM_SIZE); | ||
2314 | EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); | ||
2315 | EVP_MD_CTX_destroy(ukm_hash); | ||
2316 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, | ||
2317 | EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { | ||
2318 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG); | ||
2319 | goto err; | ||
2320 | } | ||
2321 | |||
2322 | /* | ||
2323 | * Make GOST keytransport blob message, encapsulate it into sequence. | ||
2324 | */ | ||
2325 | msglen = 255; | ||
2326 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, | ||
2327 | 32) < 0) { | ||
2328 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, SSL_R_LIBRARY_BUG); | ||
2329 | goto err; | ||
2330 | } | ||
2331 | |||
2332 | if (!CBB_add_asn1(cbb, &gostblob, CBS_ASN1_SEQUENCE)) | ||
2333 | goto err; | ||
2334 | if (!CBB_add_bytes(&gostblob, tmp, msglen)) | ||
2335 | goto err; | ||
2336 | if (!CBB_flush(cbb)) | ||
2337 | goto err; | ||
2338 | |||
2339 | /* Check if pubkey from client certificate was used. */ | ||
2340 | if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, | ||
2341 | NULL) > 0) { | ||
2342 | /* Set flag "skip certificate verify". */ | ||
2343 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | ||
2344 | } | ||
2345 | EVP_PKEY_CTX_free(pkey_ctx); | ||
2346 | s->session->master_key_length = | ||
2347 | s->method->internal->ssl3_enc->generate_master_secret(s, | ||
2348 | s->session->master_key, premaster_secret, 32); | ||
2349 | |||
2350 | ret = 1; | ||
2351 | |||
2352 | err: | ||
2353 | explicit_bzero(premaster_secret, sizeof(premaster_secret)); | ||
2354 | EVP_PKEY_free(pub_key); | ||
2355 | |||
2356 | return (ret); | ||
2357 | } | ||
2358 | |||
2359 | int | ||
2360 | ssl3_send_client_key_exchange(SSL *s) | ||
2361 | { | ||
2362 | SESS_CERT *sess_cert; | ||
2363 | unsigned long alg_k; | ||
2364 | CBB cbb, kex; | ||
2365 | |||
2366 | memset(&cbb, 0, sizeof(cbb)); | ||
2367 | |||
2368 | if (s->internal->state == SSL3_ST_CW_KEY_EXCH_A) { | ||
2369 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
2370 | |||
2371 | if ((sess_cert = SSI(s)->sess_cert) == NULL) { | ||
2372 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
2373 | SSL_AD_UNEXPECTED_MESSAGE); | ||
2374 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2375 | ERR_R_INTERNAL_ERROR); | ||
2376 | goto err; | ||
2377 | } | ||
2378 | |||
2379 | if (!ssl3_handshake_msg_start_cbb(s, &cbb, &kex, | ||
2380 | SSL3_MT_CLIENT_KEY_EXCHANGE)) | ||
2381 | goto err; | ||
2382 | |||
2383 | if (alg_k & SSL_kRSA) { | ||
2384 | if (ssl3_send_client_kex_rsa(s, sess_cert, &kex) != 1) | ||
2385 | goto err; | ||
2386 | } else if (alg_k & SSL_kDHE) { | ||
2387 | if (ssl3_send_client_kex_dhe(s, sess_cert, &kex) != 1) | ||
2388 | goto err; | ||
2389 | } else if (alg_k & SSL_kECDHE) { | ||
2390 | if (ssl3_send_client_kex_ecdhe(s, sess_cert, &kex) != 1) | ||
2391 | goto err; | ||
2392 | } else if (alg_k & SSL_kGOST) { | ||
2393 | if (ssl3_send_client_kex_gost(s, sess_cert, &kex) != 1) | ||
2394 | goto err; | ||
2395 | } else { | ||
2396 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
2397 | SSL_AD_HANDSHAKE_FAILURE); | ||
2398 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | ||
2399 | ERR_R_INTERNAL_ERROR); | ||
2400 | goto err; | ||
2401 | } | ||
2402 | |||
2403 | if (!ssl3_handshake_msg_finish_cbb(s, &cbb)) | ||
2404 | goto err; | ||
2405 | |||
2406 | s->internal->state = SSL3_ST_CW_KEY_EXCH_B; | ||
2407 | } | ||
2408 | |||
2409 | /* SSL3_ST_CW_KEY_EXCH_B */ | ||
2410 | return (ssl3_handshake_write(s)); | ||
2411 | |||
2412 | err: | ||
2413 | CBB_cleanup(&cbb); | ||
2414 | |||
2415 | return (-1); | ||
2416 | } | ||
2417 | |||
2418 | int | ||
2419 | ssl3_send_client_verify(SSL *s) | ||
2420 | { | ||
2421 | unsigned char *p; | ||
2422 | unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | ||
2423 | EVP_PKEY *pkey; | ||
2424 | EVP_PKEY_CTX *pctx = NULL; | ||
2425 | EVP_MD_CTX mctx; | ||
2426 | unsigned u = 0; | ||
2427 | unsigned long n; | ||
2428 | int j; | ||
2429 | |||
2430 | EVP_MD_CTX_init(&mctx); | ||
2431 | |||
2432 | if (s->internal->state == SSL3_ST_CW_CERT_VRFY_A) { | ||
2433 | p = ssl3_handshake_msg_start(s, SSL3_MT_CERTIFICATE_VERIFY); | ||
2434 | |||
2435 | /* | ||
2436 | * Create context from key and test if sha1 is allowed as | ||
2437 | * digest. | ||
2438 | */ | ||
2439 | pkey = s->cert->key->privatekey; | ||
2440 | pctx = EVP_PKEY_CTX_new(pkey, NULL); | ||
2441 | EVP_PKEY_sign_init(pctx); | ||
2442 | if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { | ||
2443 | if (!SSL_USE_SIGALGS(s)) | ||
2444 | s->method->internal->ssl3_enc->cert_verify_mac(s, | ||
2445 | NID_sha1, &(data[MD5_DIGEST_LENGTH])); | ||
2446 | } else { | ||
2447 | ERR_clear_error(); | ||
2448 | } | ||
2449 | /* | ||
2450 | * For TLS v1.2 send signature algorithm and signature | ||
2451 | * using agreed digest and cached handshake records. | ||
2452 | */ | ||
2453 | if (SSL_USE_SIGALGS(s)) { | ||
2454 | long hdatalen = 0; | ||
2455 | void *hdata; | ||
2456 | const EVP_MD *md = s->cert->key->digest; | ||
2457 | hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, | ||
2458 | &hdata); | ||
2459 | if (hdatalen <= 0 || | ||
2460 | !tls12_get_sigandhash(p, pkey, md)) { | ||
2461 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2462 | ERR_R_INTERNAL_ERROR); | ||
2463 | goto err; | ||
2464 | } | ||
2465 | p += 2; | ||
2466 | if (!EVP_SignInit_ex(&mctx, md, NULL) || | ||
2467 | !EVP_SignUpdate(&mctx, hdata, hdatalen) || | ||
2468 | !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { | ||
2469 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2470 | ERR_R_EVP_LIB); | ||
2471 | goto err; | ||
2472 | } | ||
2473 | s2n(u, p); | ||
2474 | n = u + 4; | ||
2475 | if (!tls1_digest_cached_records(s)) | ||
2476 | goto err; | ||
2477 | } else if (pkey->type == EVP_PKEY_RSA) { | ||
2478 | s->method->internal->ssl3_enc->cert_verify_mac( | ||
2479 | s, NID_md5, &(data[0])); | ||
2480 | if (RSA_sign(NID_md5_sha1, data, | ||
2481 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, &(p[2]), | ||
2482 | &u, pkey->pkey.rsa) <= 0 ) { | ||
2483 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2484 | ERR_R_RSA_LIB); | ||
2485 | goto err; | ||
2486 | } | ||
2487 | s2n(u, p); | ||
2488 | n = u + 2; | ||
2489 | } else if (pkey->type == EVP_PKEY_DSA) { | ||
2490 | if (!DSA_sign(pkey->save_type, | ||
2491 | &(data[MD5_DIGEST_LENGTH]), | ||
2492 | SHA_DIGEST_LENGTH, &(p[2]), | ||
2493 | (unsigned int *)&j, pkey->pkey.dsa)) { | ||
2494 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2495 | ERR_R_DSA_LIB); | ||
2496 | goto err; | ||
2497 | } | ||
2498 | s2n(j, p); | ||
2499 | n = j + 2; | ||
2500 | } else if (pkey->type == EVP_PKEY_EC) { | ||
2501 | if (!ECDSA_sign(pkey->save_type, | ||
2502 | &(data[MD5_DIGEST_LENGTH]), | ||
2503 | SHA_DIGEST_LENGTH, &(p[2]), | ||
2504 | (unsigned int *)&j, pkey->pkey.ec)) { | ||
2505 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2506 | ERR_R_ECDSA_LIB); | ||
2507 | goto err; | ||
2508 | } | ||
2509 | s2n(j, p); | ||
2510 | n = j + 2; | ||
2511 | #ifndef OPENSSL_NO_GOST | ||
2512 | } else if (pkey->type == NID_id_GostR3410_94 || | ||
2513 | pkey->type == NID_id_GostR3410_2001) { | ||
2514 | unsigned char signbuf[128]; | ||
2515 | long hdatalen = 0; | ||
2516 | void *hdata; | ||
2517 | const EVP_MD *md; | ||
2518 | int nid; | ||
2519 | size_t sigsize; | ||
2520 | |||
2521 | hdatalen = BIO_get_mem_data(S3I(s)->handshake_buffer, &hdata); | ||
2522 | if (hdatalen <= 0) { | ||
2523 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2524 | ERR_R_INTERNAL_ERROR); | ||
2525 | goto err; | ||
2526 | } | ||
2527 | if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) || | ||
2528 | !(md = EVP_get_digestbynid(nid))) { | ||
2529 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2530 | ERR_R_EVP_LIB); | ||
2531 | goto err; | ||
2532 | } | ||
2533 | if (!EVP_DigestInit_ex(&mctx, md, NULL) || | ||
2534 | !EVP_DigestUpdate(&mctx, hdata, hdatalen) || | ||
2535 | !EVP_DigestFinal(&mctx, signbuf, &u) || | ||
2536 | (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) || | ||
2537 | (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, | ||
2538 | EVP_PKEY_CTRL_GOST_SIG_FORMAT, | ||
2539 | GOST_SIG_FORMAT_RS_LE, | ||
2540 | NULL) <= 0) || | ||
2541 | (EVP_PKEY_sign(pctx, &(p[2]), &sigsize, | ||
2542 | signbuf, u) <= 0)) { | ||
2543 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2544 | ERR_R_EVP_LIB); | ||
2545 | goto err; | ||
2546 | } | ||
2547 | if (!tls1_digest_cached_records(s)) | ||
2548 | goto err; | ||
2549 | j = sigsize; | ||
2550 | s2n(j, p); | ||
2551 | n = j + 2; | ||
2552 | #endif | ||
2553 | } else { | ||
2554 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, | ||
2555 | ERR_R_INTERNAL_ERROR); | ||
2556 | goto err; | ||
2557 | } | ||
2558 | |||
2559 | s->internal->state = SSL3_ST_CW_CERT_VRFY_B; | ||
2560 | |||
2561 | ssl3_handshake_msg_finish(s, n); | ||
2562 | } | ||
2563 | |||
2564 | EVP_MD_CTX_cleanup(&mctx); | ||
2565 | EVP_PKEY_CTX_free(pctx); | ||
2566 | |||
2567 | return (ssl3_handshake_write(s)); | ||
2568 | |||
2569 | err: | ||
2570 | EVP_MD_CTX_cleanup(&mctx); | ||
2571 | EVP_PKEY_CTX_free(pctx); | ||
2572 | return (-1); | ||
2573 | } | ||
2574 | |||
2575 | int | ||
2576 | ssl3_send_client_certificate(SSL *s) | ||
2577 | { | ||
2578 | EVP_PKEY *pkey = NULL; | ||
2579 | X509 *x509 = NULL; | ||
2580 | CBB cbb, client_cert; | ||
2581 | int i; | ||
2582 | |||
2583 | memset(&cbb, 0, sizeof(cbb)); | ||
2584 | |||
2585 | if (s->internal->state == SSL3_ST_CW_CERT_A) { | ||
2586 | if ((s->cert == NULL) || (s->cert->key->x509 == NULL) || | ||
2587 | (s->cert->key->privatekey == NULL)) | ||
2588 | s->internal->state = SSL3_ST_CW_CERT_B; | ||
2589 | else | ||
2590 | s->internal->state = SSL3_ST_CW_CERT_C; | ||
2591 | } | ||
2592 | |||
2593 | /* We need to get a client cert */ | ||
2594 | if (s->internal->state == SSL3_ST_CW_CERT_B) { | ||
2595 | /* | ||
2596 | * If we get an error, we need to | ||
2597 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); | ||
2598 | * We then get retied later | ||
2599 | */ | ||
2600 | i = ssl_do_client_cert_cb(s, &x509, &pkey); | ||
2601 | if (i < 0) { | ||
2602 | s->internal->rwstate = SSL_X509_LOOKUP; | ||
2603 | return (-1); | ||
2604 | } | ||
2605 | s->internal->rwstate = SSL_NOTHING; | ||
2606 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | ||
2607 | s->internal->state = SSL3_ST_CW_CERT_B; | ||
2608 | if (!SSL_use_certificate(s, x509) || | ||
2609 | !SSL_use_PrivateKey(s, pkey)) | ||
2610 | i = 0; | ||
2611 | } else if (i == 1) { | ||
2612 | i = 0; | ||
2613 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, | ||
2614 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); | ||
2615 | } | ||
2616 | |||
2617 | X509_free(x509); | ||
2618 | EVP_PKEY_free(pkey); | ||
2619 | if (i == 0) | ||
2620 | S3I(s)->tmp.cert_req = 2; | ||
2621 | |||
2622 | /* Ok, we have a cert */ | ||
2623 | s->internal->state = SSL3_ST_CW_CERT_C; | ||
2624 | } | ||
2625 | |||
2626 | if (s->internal->state == SSL3_ST_CW_CERT_C) { | ||
2627 | if (!ssl3_handshake_msg_start_cbb(s, &cbb, &client_cert, | ||
2628 | SSL3_MT_CERTIFICATE)) | ||
2629 | goto err; | ||
2630 | if (!ssl3_output_cert_chain(s, &client_cert, | ||
2631 | (S3I(s)->tmp.cert_req == 2) ? NULL : s->cert->key->x509)) | ||
2632 | goto err; | ||
2633 | if (!ssl3_handshake_msg_finish_cbb(s, &cbb)) | ||
2634 | goto err; | ||
2635 | |||
2636 | s->internal->state = SSL3_ST_CW_CERT_D; | ||
2637 | } | ||
2638 | |||
2639 | /* SSL3_ST_CW_CERT_D */ | ||
2640 | return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); | ||
2641 | |||
2642 | err: | ||
2643 | CBB_cleanup(&cbb); | ||
2644 | |||
2645 | return (0); | ||
2646 | } | ||
2647 | |||
2648 | #define has_bits(i,m) (((i)&(m)) == (m)) | ||
2649 | |||
2650 | int | ||
2651 | ssl3_check_cert_and_algorithm(SSL *s) | ||
2652 | { | ||
2653 | int i, idx; | ||
2654 | long alg_k, alg_a; | ||
2655 | EVP_PKEY *pkey = NULL; | ||
2656 | SESS_CERT *sc; | ||
2657 | DH *dh; | ||
2658 | |||
2659 | alg_k = S3I(s)->tmp.new_cipher->algorithm_mkey; | ||
2660 | alg_a = S3I(s)->tmp.new_cipher->algorithm_auth; | ||
2661 | |||
2662 | /* We don't have a certificate. */ | ||
2663 | if (alg_a & SSL_aNULL) | ||
2664 | return (1); | ||
2665 | |||
2666 | sc = SSI(s)->sess_cert; | ||
2667 | if (sc == NULL) { | ||
2668 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2669 | ERR_R_INTERNAL_ERROR); | ||
2670 | goto err; | ||
2671 | } | ||
2672 | dh = SSI(s)->sess_cert->peer_dh_tmp; | ||
2673 | |||
2674 | /* This is the passed certificate. */ | ||
2675 | |||
2676 | idx = sc->peer_cert_type; | ||
2677 | if (idx == SSL_PKEY_ECC) { | ||
2678 | if (ssl_check_srvr_ecc_cert_and_alg( | ||
2679 | sc->peer_pkeys[idx].x509, s) == 0) { | ||
2680 | /* check failed */ | ||
2681 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2682 | SSL_R_BAD_ECC_CERT); | ||
2683 | goto f_err; | ||
2684 | } else { | ||
2685 | return (1); | ||
2686 | } | ||
2687 | } | ||
2688 | pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); | ||
2689 | i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); | ||
2690 | EVP_PKEY_free(pkey); | ||
2691 | |||
2692 | /* Check that we have a certificate if we require one. */ | ||
2693 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) { | ||
2694 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2695 | SSL_R_MISSING_RSA_SIGNING_CERT); | ||
2696 | goto f_err; | ||
2697 | } else if ((alg_a & SSL_aDSS) && | ||
2698 | !has_bits(i, EVP_PK_DSA|EVP_PKT_SIGN)) { | ||
2699 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2700 | SSL_R_MISSING_DSA_SIGNING_CERT); | ||
2701 | goto f_err; | ||
2702 | } | ||
2703 | if ((alg_k & SSL_kRSA) && | ||
2704 | !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) { | ||
2705 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2706 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | ||
2707 | goto f_err; | ||
2708 | } | ||
2709 | if ((alg_k & SSL_kDHE) && | ||
2710 | !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) { | ||
2711 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | ||
2712 | SSL_R_MISSING_DH_KEY); | ||
2713 | goto f_err; | ||
2714 | } | ||
2715 | |||
2716 | return (1); | ||
2717 | f_err: | ||
2718 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | ||
2719 | err: | ||
2720 | return (0); | ||
2721 | } | ||
2722 | |||
2723 | int | ||
2724 | ssl3_send_next_proto(SSL *s) | ||
2725 | { | ||
2726 | unsigned int len, padding_len; | ||
2727 | unsigned char *d, *p; | ||
2728 | |||
2729 | if (s->internal->state == SSL3_ST_CW_NEXT_PROTO_A) { | ||
2730 | d = p = ssl3_handshake_msg_start(s, SSL3_MT_NEXT_PROTO); | ||
2731 | |||
2732 | len = s->internal->next_proto_negotiated_len; | ||
2733 | padding_len = 32 - ((len + 2) % 32); | ||
2734 | *(p++) = len; | ||
2735 | memcpy(p, s->internal->next_proto_negotiated, len); | ||
2736 | p += len; | ||
2737 | *(p++) = padding_len; | ||
2738 | memset(p, 0, padding_len); | ||
2739 | p += padding_len; | ||
2740 | |||
2741 | ssl3_handshake_msg_finish(s, p - d); | ||
2742 | |||
2743 | s->internal->state = SSL3_ST_CW_NEXT_PROTO_B; | ||
2744 | } | ||
2745 | |||
2746 | return (ssl3_handshake_write(s)); | ||
2747 | } | ||
2748 | |||
2749 | /* | ||
2750 | * Check to see if handshake is full or resumed. Usually this is just a | ||
2751 | * case of checking to see if a cache hit has occurred. In the case of | ||
2752 | * session tickets we have to check the next message to be sure. | ||
2753 | */ | ||
2754 | |||
2755 | int | ||
2756 | ssl3_check_finished(SSL *s) | ||
2757 | { | ||
2758 | int ok; | ||
2759 | long n; | ||
2760 | |||
2761 | /* If we have no ticket it cannot be a resumed session. */ | ||
2762 | if (!s->session->tlsext_tick) | ||
2763 | return (1); | ||
2764 | /* this function is called when we really expect a Certificate | ||
2765 | * message, so permit appropriate message length */ | ||
2766 | n = s->method->internal->ssl_get_message(s, SSL3_ST_CR_CERT_A, | ||
2767 | SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list, &ok); | ||
2768 | if (!ok) | ||
2769 | return ((int)n); | ||
2770 | S3I(s)->tmp.reuse_message = 1; | ||
2771 | if ((S3I(s)->tmp.message_type == SSL3_MT_FINISHED) || | ||
2772 | (S3I(s)->tmp.message_type == SSL3_MT_NEWSESSION_TICKET)) | ||
2773 | return (2); | ||
2774 | |||
2775 | return (1); | ||
2776 | } | ||
2777 | |||
2778 | int | ||
2779 | ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) | ||
2780 | { | ||
2781 | int i = 0; | ||
2782 | |||
2783 | #ifndef OPENSSL_NO_ENGINE | ||
2784 | if (s->ctx->internal->client_cert_engine) { | ||
2785 | i = ENGINE_load_ssl_client_cert( | ||
2786 | s->ctx->internal->client_cert_engine, s, | ||
2787 | SSL_get_client_CA_list(s), px509, ppkey, NULL, NULL, NULL); | ||
2788 | if (i != 0) | ||
2789 | return (i); | ||
2790 | } | ||
2791 | #endif | ||
2792 | if (s->ctx->internal->client_cert_cb) | ||
2793 | i = s->ctx->internal->client_cert_cb(s, px509, ppkey); | ||
2794 | return (i); | ||
2795 | } | ||