]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/s3_clnt.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
3 | * All rights reserved. |
4 | * | |
5 | * This package is an SSL implementation written | |
6 | * by Eric Young (eay@cryptsoft.com). | |
7 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 8 | * |
d02b48c6 RE |
9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
13 | * included with this distribution is covered by the same copyright terms | |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 15 | * |
d02b48c6 RE |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | |
18 | * If this package is used in a product, Eric Young should be given attribution | |
19 | * as the author of the parts of the library used. | |
20 | * This can be in the form of a textual message at program startup or | |
21 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 22 | * |
d02b48c6 RE |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
31 | * 3. All advertising materials mentioning features or use of this software | |
32 | * must display the following acknowledgement: | |
33 | * "This product includes cryptographic software written by | |
34 | * Eric Young (eay@cryptsoft.com)" | |
35 | * The word 'cryptographic' can be left out if the rouines from the library | |
36 | * being used are not cryptographic related :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
d02b48c6 RE |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
0f113f3e | 52 | * |
d02b48c6 RE |
53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
55 | * copied and put under another distribution licence | |
56 | * [including the GNU Public Licence.] | |
57 | */ | |
8c74b5e5 | 58 | /* ==================================================================== |
52b8dad8 | 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
8c74b5e5 BM |
60 | * |
61 | * Redistribution and use in source and binary forms, with or without | |
62 | * modification, are permitted provided that the following conditions | |
63 | * are met: | |
64 | * | |
65 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 66 | * notice, this list of conditions and the following disclaimer. |
8c74b5e5 BM |
67 | * |
68 | * 2. Redistributions in binary form must reproduce the above copyright | |
69 | * notice, this list of conditions and the following disclaimer in | |
70 | * the documentation and/or other materials provided with the | |
71 | * distribution. | |
72 | * | |
73 | * 3. All advertising materials mentioning features or use of this | |
74 | * software must display the following acknowledgment: | |
75 | * "This product includes software developed by the OpenSSL Project | |
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
77 | * | |
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
79 | * endorse or promote products derived from this software without | |
80 | * prior written permission. For written permission, please contact | |
81 | * openssl-core@openssl.org. | |
82 | * | |
83 | * 5. Products derived from this software may not be called "OpenSSL" | |
84 | * nor may "OpenSSL" appear in their names without prior written | |
85 | * permission of the OpenSSL Project. | |
86 | * | |
87 | * 6. Redistributions of any form whatsoever must retain the following | |
88 | * acknowledgment: | |
89 | * "This product includes software developed by the OpenSSL Project | |
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
91 | * | |
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
104 | * ==================================================================== | |
105 | * | |
106 | * This product includes cryptographic software written by Eric Young | |
107 | * (eay@cryptsoft.com). This product includes software written by Tim | |
108 | * Hudson (tjh@cryptsoft.com). | |
109 | * | |
110 | */ | |
ea262260 BM |
111 | /* ==================================================================== |
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
113 | * | |
0f113f3e | 114 | * Portions of the attached software ("Contribution") are developed by |
ea262260 BM |
115 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
116 | * | |
117 | * The Contribution is licensed pursuant to the OpenSSL open source | |
118 | * license provided above. | |
119 | * | |
ea262260 BM |
120 | * ECC cipher suite support in OpenSSL originally written by |
121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. | |
122 | * | |
123 | */ | |
ddac1974 NL |
124 | /* ==================================================================== |
125 | * Copyright 2005 Nokia. All rights reserved. | |
126 | * | |
127 | * The portions of the attached software ("Contribution") is developed by | |
128 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
129 | * license. | |
130 | * | |
131 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
132 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
133 | * support (see RFC 4279) to OpenSSL. | |
134 | * | |
135 | * No patent licenses or other rights except those expressly stated in | |
136 | * the OpenSSL open source license shall be deemed granted or received | |
137 | * expressly, by implication, estoppel, or otherwise. | |
138 | * | |
139 | * No assurances are provided by Nokia that the Contribution does not | |
140 | * infringe the patent or other intellectual property rights of any third | |
141 | * party or that the license provides you with all the necessary rights | |
142 | * to make use of the Contribution. | |
143 | * | |
144 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
145 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
146 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
147 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
148 | * OTHERWISE. | |
149 | */ | |
d02b48c6 RE |
150 | |
151 | #include <stdio.h> | |
7b63c0fa LJ |
152 | #include "ssl_locl.h" |
153 | #include "kssl_lcl.h" | |
ec577822 BM |
154 | #include <openssl/buffer.h> |
155 | #include <openssl/rand.h> | |
156 | #include <openssl/objects.h> | |
157 | #include <openssl/evp.h> | |
dbad1690 | 158 | #include <openssl/md5.h> |
3eeaab4b | 159 | #ifndef OPENSSL_NO_DH |
0f113f3e | 160 | # include <openssl/dh.h> |
3eeaab4b | 161 | #endif |
d095b68d | 162 | #include <openssl/bn.h> |
368888bc | 163 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 164 | # include <openssl/engine.h> |
368888bc | 165 | #endif |
f9b3bff6 | 166 | |
0f113f3e | 167 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); |
6e3d0153 EK |
168 | #ifndef OPENSSL_NO_TLSEXT |
169 | static int ssl3_check_finished(SSL *s); | |
170 | #endif | |
ea262260 | 171 | |
3881d810 | 172 | #ifndef OPENSSL_NO_SSL3_METHOD |
4ebb342f | 173 | static const SSL_METHOD *ssl3_get_client_method(int ver) |
0f113f3e MC |
174 | { |
175 | if (ver == SSL3_VERSION) | |
176 | return (SSLv3_client_method()); | |
177 | else | |
178 | return (NULL); | |
179 | } | |
d02b48c6 | 180 | |
f3b656b2 | 181 | IMPLEMENT_ssl3_meth_func(SSLv3_client_method, |
0f113f3e MC |
182 | ssl_undefined_function, |
183 | ssl3_connect, ssl3_get_client_method) | |
3881d810 | 184 | #endif |
6b691a5c | 185 | int ssl3_connect(SSL *s) |
0f113f3e MC |
186 | { |
187 | BUF_MEM *buf = NULL; | |
188 | unsigned long Time = (unsigned long)time(NULL); | |
189 | void (*cb) (const SSL *ssl, int type, int val) = NULL; | |
190 | int ret = -1; | |
191 | int new_state, state, skip = 0; | |
d02b48c6 | 192 | |
0f113f3e MC |
193 | RAND_add(&Time, sizeof(Time), 0); |
194 | ERR_clear_error(); | |
195 | clear_sys_error(); | |
4817504d | 196 | |
0f113f3e MC |
197 | if (s->info_callback != NULL) |
198 | cb = s->info_callback; | |
199 | else if (s->ctx->info_callback != NULL) | |
200 | cb = s->ctx->info_callback; | |
201 | ||
202 | s->in_handshake++; | |
69f68237 | 203 | if (!SSL_in_init(s) || SSL_in_before(s)) { |
61986d32 | 204 | if (!SSL_clear(s)) |
69f68237 MC |
205 | return -1; |
206 | } | |
0f113f3e MC |
207 | |
208 | #ifndef OPENSSL_NO_HEARTBEATS | |
209 | /* | |
210 | * If we're awaiting a HeartbeatResponse, pretend we already got and | |
211 | * don't await it anymore, because Heartbeats don't make sense during | |
212 | * handshakes anyway. | |
213 | */ | |
214 | if (s->tlsext_hb_pending) { | |
215 | s->tlsext_hb_pending = 0; | |
216 | s->tlsext_hb_seq++; | |
217 | } | |
218 | #endif | |
219 | ||
220 | for (;;) { | |
221 | state = s->state; | |
222 | ||
223 | switch (s->state) { | |
224 | case SSL_ST_RENEGOTIATE: | |
225 | s->renegotiate = 1; | |
226 | s->state = SSL_ST_CONNECT; | |
227 | s->ctx->stats.sess_connect_renegotiate++; | |
228 | /* break */ | |
229 | case SSL_ST_BEFORE: | |
230 | case SSL_ST_CONNECT: | |
231 | case SSL_ST_BEFORE | SSL_ST_CONNECT: | |
232 | case SSL_ST_OK | SSL_ST_CONNECT: | |
233 | ||
234 | s->server = 0; | |
235 | if (cb != NULL) | |
236 | cb(s, SSL_CB_HANDSHAKE_START, 1); | |
237 | ||
238 | if ((s->version & 0xff00) != 0x0300) { | |
239 | SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); | |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) { | |
245 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW); | |
246 | return -1; | |
247 | } | |
248 | ||
249 | /* s->version=SSL3_VERSION; */ | |
250 | s->type = SSL_ST_CONNECT; | |
251 | ||
252 | if (s->init_buf == NULL) { | |
253 | if ((buf = BUF_MEM_new()) == NULL) { | |
254 | ret = -1; | |
255 | goto end; | |
256 | } | |
257 | if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { | |
258 | ret = -1; | |
259 | goto end; | |
260 | } | |
261 | s->init_buf = buf; | |
262 | buf = NULL; | |
263 | } | |
264 | ||
265 | if (!ssl3_setup_buffers(s)) { | |
266 | ret = -1; | |
267 | goto end; | |
268 | } | |
269 | ||
270 | /* setup buffing BIO */ | |
271 | if (!ssl_init_wbio_buffer(s, 0)) { | |
272 | ret = -1; | |
273 | goto end; | |
274 | } | |
275 | ||
276 | /* don't push the buffering BIO quite yet */ | |
277 | ||
278 | ssl3_init_finished_mac(s); | |
279 | ||
280 | s->state = SSL3_ST_CW_CLNT_HELLO_A; | |
281 | s->ctx->stats.sess_connect++; | |
282 | s->init_num = 0; | |
283 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; | |
284 | /* | |
285 | * Should have been reset by ssl3_get_finished, too. | |
286 | */ | |
287 | s->s3->change_cipher_spec = 0; | |
288 | break; | |
289 | ||
290 | case SSL3_ST_CW_CLNT_HELLO_A: | |
291 | case SSL3_ST_CW_CLNT_HELLO_B: | |
292 | ||
293 | s->shutdown = 0; | |
294 | ret = ssl3_client_hello(s); | |
295 | if (ret <= 0) | |
296 | goto end; | |
297 | s->state = SSL3_ST_CR_SRVR_HELLO_A; | |
298 | s->init_num = 0; | |
299 | ||
300 | /* turn on buffering for the next lot of output */ | |
301 | if (s->bbio != s->wbio) | |
302 | s->wbio = BIO_push(s->bbio, s->wbio); | |
303 | ||
304 | break; | |
305 | ||
306 | case SSL3_ST_CR_SRVR_HELLO_A: | |
307 | case SSL3_ST_CR_SRVR_HELLO_B: | |
308 | ret = ssl3_get_server_hello(s); | |
309 | if (ret <= 0) | |
310 | goto end; | |
311 | ||
312 | if (s->hit) { | |
313 | s->state = SSL3_ST_CR_FINISHED_A; | |
c519e89f | 314 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
315 | if (s->tlsext_ticket_expected) { |
316 | /* receive renewed session ticket */ | |
317 | s->state = SSL3_ST_CR_SESSION_TICKET_A; | |
318 | } | |
319 | #endif | |
320 | } else { | |
321 | s->state = SSL3_ST_CR_CERT_A; | |
322 | } | |
323 | s->init_num = 0; | |
324 | break; | |
325 | case SSL3_ST_CR_CERT_A: | |
326 | case SSL3_ST_CR_CERT_B: | |
6e3d0153 EK |
327 | #ifndef OPENSSL_NO_TLSEXT |
328 | /* Noop (ret = 0) for everything but EAP-FAST. */ | |
329 | ret = ssl3_check_finished(s); | |
330 | if (ret < 0) | |
331 | goto end; | |
332 | if (ret == 1) { | |
333 | s->hit = 1; | |
334 | s->state = SSL3_ST_CR_FINISHED_A; | |
335 | s->init_num = 0; | |
336 | break; | |
337 | } | |
338 | #endif | |
0f113f3e MC |
339 | /* Check if it is anon DH/ECDH, SRP auth */ |
340 | /* or PSK */ | |
341 | if (! | |
342 | (s->s3->tmp. | |
343 | new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) | |
344 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { | |
345 | ret = ssl3_get_server_certificate(s); | |
346 | if (ret <= 0) | |
347 | goto end; | |
67c8e7f4 | 348 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
349 | if (s->tlsext_status_expected) |
350 | s->state = SSL3_ST_CR_CERT_STATUS_A; | |
351 | else | |
352 | s->state = SSL3_ST_CR_KEY_EXCH_A; | |
353 | } else { | |
354 | skip = 1; | |
355 | s->state = SSL3_ST_CR_KEY_EXCH_A; | |
356 | } | |
67c8e7f4 | 357 | #else |
0f113f3e MC |
358 | } else |
359 | skip = 1; | |
360 | ||
361 | s->state = SSL3_ST_CR_KEY_EXCH_A; | |
362 | #endif | |
363 | s->init_num = 0; | |
364 | break; | |
365 | ||
366 | case SSL3_ST_CR_KEY_EXCH_A: | |
367 | case SSL3_ST_CR_KEY_EXCH_B: | |
368 | ret = ssl3_get_key_exchange(s); | |
369 | if (ret <= 0) | |
370 | goto end; | |
371 | s->state = SSL3_ST_CR_CERT_REQ_A; | |
372 | s->init_num = 0; | |
373 | ||
374 | /* | |
375 | * at this point we check that we have the required stuff from | |
376 | * the server | |
377 | */ | |
378 | if (!ssl3_check_cert_and_algorithm(s)) { | |
379 | ret = -1; | |
380 | goto end; | |
381 | } | |
382 | break; | |
383 | ||
384 | case SSL3_ST_CR_CERT_REQ_A: | |
385 | case SSL3_ST_CR_CERT_REQ_B: | |
386 | ret = ssl3_get_certificate_request(s); | |
387 | if (ret <= 0) | |
388 | goto end; | |
389 | s->state = SSL3_ST_CR_SRVR_DONE_A; | |
390 | s->init_num = 0; | |
391 | break; | |
392 | ||
393 | case SSL3_ST_CR_SRVR_DONE_A: | |
394 | case SSL3_ST_CR_SRVR_DONE_B: | |
395 | ret = ssl3_get_server_done(s); | |
396 | if (ret <= 0) | |
397 | goto end; | |
edc032b5 | 398 | #ifndef OPENSSL_NO_SRP |
0f113f3e MC |
399 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
400 | if ((ret = SRP_Calc_A_param(s)) <= 0) { | |
401 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC); | |
402 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
403 | goto end; | |
404 | } | |
405 | } | |
406 | #endif | |
407 | if (s->s3->tmp.cert_req) | |
408 | s->state = SSL3_ST_CW_CERT_A; | |
409 | else | |
410 | s->state = SSL3_ST_CW_KEY_EXCH_A; | |
411 | s->init_num = 0; | |
412 | ||
413 | break; | |
414 | ||
415 | case SSL3_ST_CW_CERT_A: | |
416 | case SSL3_ST_CW_CERT_B: | |
417 | case SSL3_ST_CW_CERT_C: | |
418 | case SSL3_ST_CW_CERT_D: | |
419 | ret = ssl3_send_client_certificate(s); | |
420 | if (ret <= 0) | |
421 | goto end; | |
422 | s->state = SSL3_ST_CW_KEY_EXCH_A; | |
423 | s->init_num = 0; | |
424 | break; | |
425 | ||
426 | case SSL3_ST_CW_KEY_EXCH_A: | |
427 | case SSL3_ST_CW_KEY_EXCH_B: | |
428 | ret = ssl3_send_client_key_exchange(s); | |
429 | if (ret <= 0) | |
430 | goto end; | |
431 | /* | |
432 | * EAY EAY EAY need to check for DH fix cert sent back | |
433 | */ | |
434 | /* | |
435 | * For TLS, cert_req is set to 2, so a cert chain of nothing is | |
436 | * sent, but no verify packet is sent | |
437 | */ | |
438 | /* | |
439 | * XXX: For now, we do not support client authentication in ECDH | |
440 | * cipher suites with ECDH (rather than ECDSA) certificates. We | |
441 | * need to skip the certificate verify message when client's | |
442 | * ECDH public key is sent inside the client certificate. | |
443 | */ | |
444 | if (s->s3->tmp.cert_req == 1) { | |
445 | s->state = SSL3_ST_CW_CERT_VRFY_A; | |
446 | } else { | |
447 | s->state = SSL3_ST_CW_CHANGE_A; | |
448 | } | |
449 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | |
450 | s->state = SSL3_ST_CW_CHANGE_A; | |
451 | } | |
452 | ||
453 | s->init_num = 0; | |
454 | break; | |
455 | ||
456 | case SSL3_ST_CW_CERT_VRFY_A: | |
457 | case SSL3_ST_CW_CERT_VRFY_B: | |
458 | ret = ssl3_send_client_verify(s); | |
459 | if (ret <= 0) | |
460 | goto end; | |
461 | s->state = SSL3_ST_CW_CHANGE_A; | |
462 | s->init_num = 0; | |
463 | break; | |
464 | ||
465 | case SSL3_ST_CW_CHANGE_A: | |
466 | case SSL3_ST_CW_CHANGE_B: | |
467 | ret = ssl3_send_change_cipher_spec(s, | |
468 | SSL3_ST_CW_CHANGE_A, | |
469 | SSL3_ST_CW_CHANGE_B); | |
470 | if (ret <= 0) | |
471 | goto end; | |
ee2ffc27 | 472 | |
bf48836c | 473 | #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) |
0f113f3e | 474 | s->state = SSL3_ST_CW_FINISHED_A; |
ee2ffc27 | 475 | #else |
0f113f3e MC |
476 | if (s->s3->next_proto_neg_seen) |
477 | s->state = SSL3_ST_CW_NEXT_PROTO_A; | |
478 | else | |
479 | s->state = SSL3_ST_CW_FINISHED_A; | |
ee2ffc27 | 480 | #endif |
0f113f3e | 481 | s->init_num = 0; |
d02b48c6 | 482 | |
0f113f3e | 483 | s->session->cipher = s->s3->tmp.new_cipher; |
09b6c2ef | 484 | #ifdef OPENSSL_NO_COMP |
0f113f3e | 485 | s->session->compress_meth = 0; |
09b6c2ef | 486 | #else |
0f113f3e MC |
487 | if (s->s3->tmp.new_compression == NULL) |
488 | s->session->compress_meth = 0; | |
489 | else | |
490 | s->session->compress_meth = s->s3->tmp.new_compression->id; | |
09b6c2ef | 491 | #endif |
0f113f3e MC |
492 | if (!s->method->ssl3_enc->setup_key_block(s)) { |
493 | ret = -1; | |
494 | goto end; | |
495 | } | |
d02b48c6 | 496 | |
0f113f3e MC |
497 | if (!s->method->ssl3_enc->change_cipher_state(s, |
498 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) | |
499 | { | |
500 | ret = -1; | |
501 | goto end; | |
502 | } | |
d02b48c6 | 503 | |
0f113f3e | 504 | break; |
d02b48c6 | 505 | |
bf48836c | 506 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) |
0f113f3e MC |
507 | case SSL3_ST_CW_NEXT_PROTO_A: |
508 | case SSL3_ST_CW_NEXT_PROTO_B: | |
509 | ret = ssl3_send_next_proto(s); | |
510 | if (ret <= 0) | |
511 | goto end; | |
512 | s->state = SSL3_ST_CW_FINISHED_A; | |
513 | break; | |
514 | #endif | |
515 | ||
516 | case SSL3_ST_CW_FINISHED_A: | |
517 | case SSL3_ST_CW_FINISHED_B: | |
518 | ret = ssl3_send_finished(s, | |
519 | SSL3_ST_CW_FINISHED_A, | |
520 | SSL3_ST_CW_FINISHED_B, | |
521 | s->method-> | |
522 | ssl3_enc->client_finished_label, | |
523 | s->method-> | |
524 | ssl3_enc->client_finished_label_len); | |
525 | if (ret <= 0) | |
526 | goto end; | |
527 | s->state = SSL3_ST_CW_FLUSH; | |
528 | ||
529 | /* clear flags */ | |
530 | s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; | |
531 | if (s->hit) { | |
532 | s->s3->tmp.next_state = SSL_ST_OK; | |
533 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) { | |
534 | s->state = SSL_ST_OK; | |
535 | s->s3->flags |= SSL3_FLAGS_POP_BUFFER; | |
536 | s->s3->delay_buf_pop_ret = 0; | |
537 | } | |
538 | } else { | |
6434abbf | 539 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
540 | /* |
541 | * Allow NewSessionTicket if ticket expected | |
542 | */ | |
543 | if (s->tlsext_ticket_expected) | |
544 | s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; | |
545 | else | |
6434abbf DSH |
546 | #endif |
547 | ||
0f113f3e MC |
548 | s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; |
549 | } | |
550 | s->init_num = 0; | |
551 | break; | |
d02b48c6 | 552 | |
0f113f3e MC |
553 | #ifndef OPENSSL_NO_TLSEXT |
554 | case SSL3_ST_CR_SESSION_TICKET_A: | |
555 | case SSL3_ST_CR_SESSION_TICKET_B: | |
556 | ret = ssl3_get_new_session_ticket(s); | |
557 | if (ret <= 0) | |
558 | goto end; | |
559 | s->state = SSL3_ST_CR_FINISHED_A; | |
560 | s->init_num = 0; | |
561 | break; | |
562 | ||
563 | case SSL3_ST_CR_CERT_STATUS_A: | |
564 | case SSL3_ST_CR_CERT_STATUS_B: | |
565 | ret = ssl3_get_cert_status(s); | |
566 | if (ret <= 0) | |
567 | goto end; | |
568 | s->state = SSL3_ST_CR_KEY_EXCH_A; | |
569 | s->init_num = 0; | |
570 | break; | |
571 | #endif | |
572 | ||
573 | case SSL3_ST_CR_FINISHED_A: | |
574 | case SSL3_ST_CR_FINISHED_B: | |
6e3d0153 EK |
575 | if (!s->s3->change_cipher_spec) |
576 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | |
0f113f3e MC |
577 | ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, |
578 | SSL3_ST_CR_FINISHED_B); | |
579 | if (ret <= 0) | |
580 | goto end; | |
581 | ||
582 | if (s->hit) | |
583 | s->state = SSL3_ST_CW_CHANGE_A; | |
584 | else | |
585 | s->state = SSL_ST_OK; | |
586 | s->init_num = 0; | |
587 | break; | |
588 | ||
589 | case SSL3_ST_CW_FLUSH: | |
590 | s->rwstate = SSL_WRITING; | |
591 | if (BIO_flush(s->wbio) <= 0) { | |
592 | ret = -1; | |
593 | goto end; | |
594 | } | |
595 | s->rwstate = SSL_NOTHING; | |
596 | s->state = s->s3->tmp.next_state; | |
597 | break; | |
598 | ||
599 | case SSL_ST_OK: | |
600 | /* clean a few things up */ | |
601 | ssl3_cleanup_key_block(s); | |
602 | ||
603 | if (s->init_buf != NULL) { | |
604 | BUF_MEM_free(s->init_buf); | |
605 | s->init_buf = NULL; | |
606 | } | |
607 | ||
608 | /* | |
609 | * If we are not 'joining' the last two packets, remove the | |
610 | * buffering now | |
611 | */ | |
612 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | |
613 | ssl_free_wbio_buffer(s); | |
614 | /* else do it later in ssl3_write */ | |
615 | ||
616 | s->init_num = 0; | |
617 | s->renegotiate = 0; | |
618 | s->new_session = 0; | |
619 | ||
620 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); | |
621 | if (s->hit) | |
622 | s->ctx->stats.sess_hit++; | |
623 | ||
624 | ret = 1; | |
625 | /* s->server=0; */ | |
626 | s->handshake_func = ssl3_connect; | |
627 | s->ctx->stats.sess_connect_good++; | |
628 | ||
629 | if (cb != NULL) | |
630 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | |
631 | ||
632 | goto end; | |
633 | /* break; */ | |
634 | ||
635 | default: | |
636 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE); | |
637 | ret = -1; | |
638 | goto end; | |
639 | /* break; */ | |
640 | } | |
641 | ||
642 | /* did we do anything */ | |
643 | if (!s->s3->tmp.reuse_message && !skip) { | |
644 | if (s->debug) { | |
645 | if ((ret = BIO_flush(s->wbio)) <= 0) | |
646 | goto end; | |
647 | } | |
648 | ||
649 | if ((cb != NULL) && (s->state != state)) { | |
650 | new_state = s->state; | |
651 | s->state = state; | |
652 | cb(s, SSL_CB_CONNECT_LOOP, 1); | |
653 | s->state = new_state; | |
654 | } | |
655 | } | |
656 | skip = 0; | |
657 | } | |
658 | end: | |
659 | s->in_handshake--; | |
660 | if (buf != NULL) | |
661 | BUF_MEM_free(buf); | |
662 | if (cb != NULL) | |
663 | cb(s, SSL_CB_CONNECT_EXIT, ret); | |
664 | return (ret); | |
665 | } | |
d02b48c6 | 666 | |
36d16f8e | 667 | int ssl3_client_hello(SSL *s) |
0f113f3e MC |
668 | { |
669 | unsigned char *buf; | |
670 | unsigned char *p, *d; | |
671 | int i; | |
672 | unsigned long l; | |
673 | int al = 0; | |
09b6c2ef | 674 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
675 | int j; |
676 | SSL_COMP *comp; | |
677 | #endif | |
678 | ||
679 | buf = (unsigned char *)s->init_buf->data; | |
680 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { | |
681 | SSL_SESSION *sess = s->session; | |
6e3d0153 EK |
682 | if ((sess == NULL) || (sess->ssl_version != s->version) || |
683 | #ifdef OPENSSL_NO_TLSEXT | |
684 | !sess->session_id_length || | |
685 | #else | |
686 | /* | |
687 | * In the case of EAP-FAST, we can have a pre-shared | |
688 | * "ticket" without a session ID. | |
689 | */ | |
690 | (!sess->session_id_length && !sess->tlsext_tick) || | |
691 | #endif | |
692 | (sess->not_resumable)) { | |
0f113f3e MC |
693 | if (!ssl_get_new_session(s, 0)) |
694 | goto err; | |
695 | } | |
696 | if (s->method->version == DTLS_ANY_VERSION) { | |
697 | /* Determine which DTLS version to use */ | |
698 | int options = s->options; | |
699 | /* If DTLS 1.2 disabled correct the version number */ | |
700 | if (options & SSL_OP_NO_DTLSv1_2) { | |
701 | if (tls1_suiteb(s)) { | |
702 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, | |
703 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); | |
704 | goto err; | |
705 | } | |
706 | /* | |
707 | * Disabling all versions is silly: return an error. | |
708 | */ | |
709 | if (options & SSL_OP_NO_DTLSv1) { | |
710 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION); | |
711 | goto err; | |
712 | } | |
713 | /* | |
714 | * Update method so we don't use any DTLS 1.2 features. | |
715 | */ | |
716 | s->method = DTLSv1_client_method(); | |
717 | s->version = DTLS1_VERSION; | |
718 | } else { | |
719 | /* | |
720 | * We only support one version: update method | |
721 | */ | |
722 | if (options & SSL_OP_NO_DTLSv1) | |
723 | s->method = DTLSv1_2_client_method(); | |
724 | s->version = DTLS1_2_VERSION; | |
725 | } | |
726 | s->client_version = s->version; | |
727 | } | |
728 | /* else use the pre-loaded session */ | |
729 | ||
730 | p = s->s3->client_random; | |
731 | ||
732 | /* | |
733 | * for DTLS if client_random is initialized, reuse it, we are | |
734 | * required to use same upon reply to HelloVerify | |
735 | */ | |
736 | if (SSL_IS_DTLS(s)) { | |
737 | size_t idx; | |
738 | i = 1; | |
739 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { | |
740 | if (p[idx]) { | |
741 | i = 0; | |
742 | break; | |
743 | } | |
744 | } | |
745 | } else | |
746 | i = 1; | |
747 | ||
e1b568dd MC |
748 | if (i && ssl_fill_hello_random(s, 0, p, |
749 | sizeof(s->s3->client_random)) <= 0) | |
750 | goto err; | |
0f113f3e MC |
751 | |
752 | /* Do the message type and length last */ | |
753 | d = p = ssl_handshake_start(s); | |
754 | ||
35a1cc90 MC |
755 | /*- |
756 | * version indicates the negotiated version: for example from | |
757 | * an SSLv2/v3 compatible client hello). The client_version | |
758 | * field is the maximum version we permit and it is also | |
759 | * used in RSA encrypted premaster secrets. Some servers can | |
760 | * choke if we initially report a higher version then | |
761 | * renegotiate to a lower one in the premaster secret. This | |
762 | * didn't happen with TLS 1.0 as most servers supported it | |
763 | * but it can with TLS 1.1 or later if the server only supports | |
764 | * 1.0. | |
765 | * | |
766 | * Possible scenario with previous logic: | |
767 | * 1. Client hello indicates TLS 1.2 | |
768 | * 2. Server hello says TLS 1.0 | |
769 | * 3. RSA encrypted premaster secret uses 1.2. | |
770 | * 4. Handhaked proceeds using TLS 1.0. | |
771 | * 5. Server sends hello request to renegotiate. | |
772 | * 6. Client hello indicates TLS v1.0 as we now | |
773 | * know that is maximum server supports. | |
774 | * 7. Server chokes on RSA encrypted premaster secret | |
775 | * containing version 1.0. | |
776 | * | |
777 | * For interoperability it should be OK to always use the | |
778 | * maximum version we support in client hello and then rely | |
779 | * on the checking of version to ensure the servers isn't | |
780 | * being inconsistent: for example initially negotiating with | |
781 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using | |
782 | * client_version in client hello and not resetting it to | |
783 | * the negotiated version. | |
784 | */ | |
0f113f3e MC |
785 | *(p++) = s->client_version >> 8; |
786 | *(p++) = s->client_version & 0xff; | |
0f113f3e MC |
787 | |
788 | /* Random stuff */ | |
789 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); | |
790 | p += SSL3_RANDOM_SIZE; | |
791 | ||
792 | /* Session ID */ | |
793 | if (s->new_session) | |
794 | i = 0; | |
795 | else | |
796 | i = s->session->session_id_length; | |
797 | *(p++) = i; | |
798 | if (i != 0) { | |
799 | if (i > (int)sizeof(s->session->session_id)) { | |
800 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
801 | goto err; | |
802 | } | |
803 | memcpy(p, s->session->session_id, i); | |
804 | p += i; | |
805 | } | |
806 | ||
807 | /* cookie stuff for DTLS */ | |
808 | if (SSL_IS_DTLS(s)) { | |
809 | if (s->d1->cookie_len > sizeof(s->d1->cookie)) { | |
810 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
811 | goto err; | |
812 | } | |
813 | *(p++) = s->d1->cookie_len; | |
814 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | |
815 | p += s->d1->cookie_len; | |
816 | } | |
817 | ||
818 | /* Ciphers supported */ | |
819 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0); | |
820 | if (i == 0) { | |
821 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); | |
822 | goto err; | |
823 | } | |
800e1cd9 | 824 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
0f113f3e MC |
825 | /* |
826 | * Some servers hang if client hello > 256 bytes as hack workaround | |
827 | * chop number of supported ciphers to keep it well below this if we | |
828 | * use TLS v1.2 | |
829 | */ | |
830 | if (TLS1_get_version(s) >= TLS1_2_VERSION | |
831 | && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) | |
832 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; | |
833 | #endif | |
834 | s2n(i, p); | |
835 | p += i; | |
836 | ||
837 | /* COMPRESSION */ | |
09b6c2ef | 838 | #ifdef OPENSSL_NO_COMP |
0f113f3e | 839 | *(p++) = 1; |
09b6c2ef | 840 | #else |
566dda07 | 841 | |
0f113f3e MC |
842 | if (!ssl_allow_compression(s) || !s->ctx->comp_methods) |
843 | j = 0; | |
844 | else | |
845 | j = sk_SSL_COMP_num(s->ctx->comp_methods); | |
846 | *(p++) = 1 + j; | |
847 | for (i = 0; i < j; i++) { | |
848 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); | |
849 | *(p++) = comp->id; | |
850 | } | |
09b6c2ef | 851 | #endif |
0f113f3e | 852 | *(p++) = 0; /* Add the NULL method */ |
761772d7 | 853 | |
ed3883d2 | 854 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
855 | /* TLS extensions */ |
856 | if (ssl_prepare_clienthello_tlsext(s) <= 0) { | |
857 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); | |
858 | goto err; | |
859 | } | |
860 | if ((p = | |
861 | ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, | |
862 | &al)) == NULL) { | |
863 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
864 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
865 | goto err; | |
866 | } | |
867 | #endif | |
868 | ||
869 | l = p - d; | |
61986d32 | 870 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) { |
77d514c5 MC |
871 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
872 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); | |
873 | goto err; | |
874 | } | |
0f113f3e MC |
875 | s->state = SSL3_ST_CW_CLNT_HELLO_B; |
876 | } | |
877 | ||
878 | /* SSL3_ST_CW_CLNT_HELLO_B */ | |
879 | return ssl_do_write(s); | |
880 | err: | |
881 | return (-1); | |
882 | } | |
d02b48c6 | 883 | |
36d16f8e | 884 | int ssl3_get_server_hello(SSL *s) |
0f113f3e MC |
885 | { |
886 | STACK_OF(SSL_CIPHER) *sk; | |
887 | const SSL_CIPHER *c; | |
888 | CERT *ct = s->cert; | |
889 | unsigned char *p, *d; | |
890 | int i, al = SSL_AD_INTERNAL_ERROR, ok; | |
891 | unsigned int j; | |
892 | long n; | |
09b6c2ef | 893 | #ifndef OPENSSL_NO_COMP |
0f113f3e MC |
894 | SSL_COMP *comp; |
895 | #endif | |
896 | /* | |
897 | * Hello verify request and/or server hello version may not match so set | |
898 | * first packet if we're negotiating version. | |
899 | */ | |
900 | if (SSL_IS_DTLS(s)) | |
901 | s->first_packet = 1; | |
902 | ||
903 | n = s->method->ssl_get_message(s, | |
904 | SSL3_ST_CR_SRVR_HELLO_A, | |
905 | SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok); | |
906 | ||
907 | if (!ok) | |
908 | return ((int)n); | |
909 | ||
910 | if (SSL_IS_DTLS(s)) { | |
911 | s->first_packet = 0; | |
912 | if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { | |
913 | if (s->d1->send_cookie == 0) { | |
914 | s->s3->tmp.reuse_message = 1; | |
915 | return 1; | |
916 | } else { /* already sent a cookie */ | |
917 | ||
918 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
919 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); | |
920 | goto f_err; | |
921 | } | |
922 | } | |
923 | } | |
924 | ||
925 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { | |
926 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
927 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); | |
928 | goto f_err; | |
929 | } | |
930 | ||
931 | d = p = (unsigned char *)s->init_msg; | |
932 | if (s->method->version == DTLS_ANY_VERSION) { | |
933 | /* Work out correct protocol version to use */ | |
934 | int hversion = (p[0] << 8) | p[1]; | |
935 | int options = s->options; | |
936 | if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2)) | |
937 | s->method = DTLSv1_2_client_method(); | |
938 | else if (tls1_suiteb(s)) { | |
939 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
940 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); | |
941 | s->version = hversion; | |
942 | al = SSL_AD_PROTOCOL_VERSION; | |
943 | goto f_err; | |
944 | } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1)) | |
945 | s->method = DTLSv1_client_method(); | |
946 | else { | |
947 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); | |
948 | s->version = hversion; | |
949 | al = SSL_AD_PROTOCOL_VERSION; | |
950 | goto f_err; | |
951 | } | |
952 | s->version = s->method->version; | |
953 | } | |
954 | ||
955 | if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { | |
956 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); | |
957 | s->version = (s->version & 0xff00) | p[1]; | |
958 | al = SSL_AD_PROTOCOL_VERSION; | |
959 | goto f_err; | |
960 | } | |
961 | p += 2; | |
962 | ||
963 | /* load the server hello data */ | |
964 | /* load the server random */ | |
965 | memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); | |
966 | p += SSL3_RANDOM_SIZE; | |
967 | ||
968 | s->hit = 0; | |
969 | ||
970 | /* get the session-id */ | |
971 | j = *(p++); | |
972 | ||
973 | if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) { | |
974 | al = SSL_AD_ILLEGAL_PARAMETER; | |
975 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); | |
976 | goto f_err; | |
977 | } | |
12bf56c0 | 978 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e | 979 | /* |
6e3d0153 EK |
980 | * Check if we can resume the session based on external pre-shared secret. |
981 | * EAP-FAST (RFC 4851) supports two types of session resumption. | |
982 | * Resumption based on server-side state works with session IDs. | |
983 | * Resumption based on pre-shared Protected Access Credentials (PACs) | |
984 | * works by overriding the SessionTicket extension at the application | |
985 | * layer, and does not send a session ID. (We do not know whether EAP-FAST | |
986 | * servers would honour the session ID.) Therefore, the session ID alone | |
987 | * is not a reliable indicator of session resumption, so we first check if | |
988 | * we can resume, and later peek at the next handshake message to see if the | |
989 | * server wants to resume. | |
0f113f3e | 990 | */ |
6e3d0153 EK |
991 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && |
992 | s->session->tlsext_tick) { | |
0f113f3e MC |
993 | SSL_CIPHER *pref_cipher = NULL; |
994 | s->session->master_key_length = sizeof(s->session->master_key); | |
995 | if (s->tls_session_secret_cb(s, s->session->master_key, | |
996 | &s->session->master_key_length, | |
997 | NULL, &pref_cipher, | |
998 | s->tls_session_secret_cb_arg)) { | |
999 | s->session->cipher = pref_cipher ? | |
1000 | pref_cipher : ssl_get_cipher_by_char(s, p + j); | |
6e3d0153 EK |
1001 | } else { |
1002 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); | |
1003 | al = SSL_AD_INTERNAL_ERROR; | |
1004 | goto f_err; | |
0f113f3e MC |
1005 | } |
1006 | } | |
1007 | #endif /* OPENSSL_NO_TLSEXT */ | |
1008 | ||
6e3d0153 | 1009 | if (j != 0 && j == s->session->session_id_length |
0f113f3e MC |
1010 | && memcmp(p, s->session->session_id, j) == 0) { |
1011 | if (s->sid_ctx_length != s->session->sid_ctx_length | |
1012 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { | |
1013 | /* actually a client application bug */ | |
1014 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1015 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
1016 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); | |
1017 | goto f_err; | |
1018 | } | |
1019 | s->hit = 1; | |
6e3d0153 | 1020 | } else { |
0f113f3e | 1021 | /* |
6e3d0153 EK |
1022 | * If we were trying for session-id reuse but the server |
1023 | * didn't echo the ID, make a new SSL_SESSION. | |
1024 | * In the case of EAP-FAST and PAC, we do not send a session ID, | |
1025 | * so the PAC-based session secret is always preserved. It'll be | |
1026 | * overwritten if the server refuses resumption. | |
0f113f3e MC |
1027 | */ |
1028 | if (s->session->session_id_length > 0) { | |
1029 | if (!ssl_get_new_session(s, 0)) { | |
1030 | goto f_err; | |
1031 | } | |
1032 | } | |
1033 | s->session->session_id_length = j; | |
1034 | memcpy(s->session->session_id, p, j); /* j could be 0 */ | |
1035 | } | |
1036 | p += j; | |
1037 | c = ssl_get_cipher_by_char(s, p); | |
1038 | if (c == NULL) { | |
1039 | /* unknown cipher */ | |
1040 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1041 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); | |
1042 | goto f_err; | |
1043 | } | |
1044 | /* Set version disabled mask now we know version */ | |
1045 | if (!SSL_USE_TLS1_2_CIPHERS(s)) | |
1046 | ct->mask_ssl = SSL_TLSV1_2; | |
1047 | else | |
1048 | ct->mask_ssl = 0; | |
1049 | /* | |
1050 | * If it is a disabled cipher we didn't send it in client hello, so | |
1051 | * return an error. | |
1052 | */ | |
1053 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { | |
1054 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1055 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); | |
1056 | goto f_err; | |
1057 | } | |
1058 | p += ssl_put_cipher_by_char(s, NULL, NULL); | |
1059 | ||
1060 | sk = ssl_get_ciphers_by_id(s); | |
1061 | i = sk_SSL_CIPHER_find(sk, c); | |
1062 | if (i < 0) { | |
1063 | /* we did not say we would use this cipher */ | |
1064 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1065 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); | |
1066 | goto f_err; | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * Depending on the session caching (internal/external), the cipher | |
1071 | * and/or cipher_id values may not be set. Make sure that cipher_id is | |
1072 | * set and use it for comparison. | |
1073 | */ | |
1074 | if (s->session->cipher) | |
1075 | s->session->cipher_id = s->session->cipher->id; | |
1076 | if (s->hit && (s->session->cipher_id != c->id)) { | |
9e9858d1 RS |
1077 | al = SSL_AD_ILLEGAL_PARAMETER; |
1078 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
1079 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); | |
1080 | goto f_err; | |
0f113f3e MC |
1081 | } |
1082 | s->s3->tmp.new_cipher = c; | |
1083 | /* | |
1084 | * Don't digest cached records if no sigalgs: we may need them for client | |
1085 | * authentication. | |
1086 | */ | |
1087 | if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s)) | |
1088 | goto f_err; | |
1089 | /* lets get the compression algorithm */ | |
1090 | /* COMPRESSION */ | |
09b6c2ef | 1091 | #ifdef OPENSSL_NO_COMP |
0f113f3e MC |
1092 | if (*(p++) != 0) { |
1093 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1094 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
1095 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); | |
1096 | goto f_err; | |
1097 | } | |
1098 | /* | |
1099 | * If compression is disabled we'd better not try to resume a session | |
1100 | * using compression. | |
1101 | */ | |
1102 | if (s->session->compress_meth != 0) { | |
1103 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); | |
1104 | goto f_err; | |
1105 | } | |
09b6c2ef | 1106 | #else |
0f113f3e MC |
1107 | j = *(p++); |
1108 | if (s->hit && j != s->session->compress_meth) { | |
1109 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1110 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
1111 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); | |
1112 | goto f_err; | |
1113 | } | |
1114 | if (j == 0) | |
1115 | comp = NULL; | |
1116 | else if (!ssl_allow_compression(s)) { | |
1117 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1118 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); | |
1119 | goto f_err; | |
1120 | } else | |
1121 | comp = ssl3_comp_find(s->ctx->comp_methods, j); | |
1122 | ||
1123 | if ((j != 0) && (comp == NULL)) { | |
1124 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1125 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, | |
1126 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); | |
1127 | goto f_err; | |
1128 | } else { | |
1129 | s->s3->tmp.new_compression = comp; | |
1130 | } | |
09b6c2ef | 1131 | #endif |
761772d7 | 1132 | |
ed3883d2 | 1133 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
1134 | /* TLS extensions */ |
1135 | if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) { | |
1136 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); | |
1137 | goto err; | |
1138 | } | |
1139 | #endif | |
1140 | ||
1141 | if (p != (d + n)) { | |
1142 | /* wrong packet length */ | |
1143 | al = SSL_AD_DECODE_ERROR; | |
1144 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); | |
1145 | goto f_err; | |
1146 | } | |
1147 | ||
1148 | return (1); | |
1149 | f_err: | |
1150 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1151 | err: | |
1152 | return (-1); | |
1153 | } | |
d02b48c6 | 1154 | |
36d16f8e | 1155 | int ssl3_get_server_certificate(SSL *s) |
0f113f3e MC |
1156 | { |
1157 | int al, i, ok, ret = -1; | |
1158 | unsigned long n, nc, llen, l; | |
1159 | X509 *x = NULL; | |
1160 | const unsigned char *q, *p; | |
1161 | unsigned char *d; | |
1162 | STACK_OF(X509) *sk = NULL; | |
1163 | SESS_CERT *sc; | |
1164 | EVP_PKEY *pkey = NULL; | |
1165 | int need_cert = 1; /* VRS: 0=> will allow null cert if auth == | |
1166 | * KRB5 */ | |
1167 | ||
1168 | n = s->method->ssl_get_message(s, | |
1169 | SSL3_ST_CR_CERT_A, | |
1170 | SSL3_ST_CR_CERT_B, | |
1171 | -1, s->max_cert_list, &ok); | |
1172 | ||
1173 | if (!ok) | |
1174 | return ((int)n); | |
1175 | ||
1176 | if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || | |
1177 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && | |
1178 | (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) { | |
1179 | s->s3->tmp.reuse_message = 1; | |
1180 | return (1); | |
1181 | } | |
1182 | ||
1183 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { | |
1184 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1185 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE); | |
1186 | goto f_err; | |
1187 | } | |
1188 | p = d = (unsigned char *)s->init_msg; | |
1189 | ||
1190 | if ((sk = sk_X509_new_null()) == NULL) { | |
1191 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); | |
66696478 | 1192 | goto done; |
0f113f3e MC |
1193 | } |
1194 | ||
1195 | n2l3(p, llen); | |
1196 | if (llen + 3 != n) { | |
1197 | al = SSL_AD_DECODE_ERROR; | |
1198 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); | |
1199 | goto f_err; | |
1200 | } | |
1201 | for (nc = 0; nc < llen;) { | |
1202 | n2l3(p, l); | |
1203 | if ((l + nc + 3) > llen) { | |
1204 | al = SSL_AD_DECODE_ERROR; | |
1205 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1206 | SSL_R_CERT_LENGTH_MISMATCH); | |
1207 | goto f_err; | |
1208 | } | |
1209 | ||
1210 | q = p; | |
1211 | x = d2i_X509(NULL, &q, l); | |
1212 | if (x == NULL) { | |
1213 | al = SSL_AD_BAD_CERTIFICATE; | |
1214 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); | |
1215 | goto f_err; | |
1216 | } | |
1217 | if (q != (p + l)) { | |
1218 | al = SSL_AD_DECODE_ERROR; | |
1219 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1220 | SSL_R_CERT_LENGTH_MISMATCH); | |
1221 | goto f_err; | |
1222 | } | |
1223 | if (!sk_X509_push(sk, x)) { | |
1224 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); | |
66696478 | 1225 | goto done; |
0f113f3e MC |
1226 | } |
1227 | x = NULL; | |
1228 | nc += l + 3; | |
1229 | p = q; | |
1230 | } | |
1231 | ||
1232 | i = ssl_verify_cert_chain(s, sk); | |
1233 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) | |
82d5d46c | 1234 | #ifndef OPENSSL_NO_KRB5 |
0f113f3e MC |
1235 | && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
1236 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) | |
1237 | #endif /* OPENSSL_NO_KRB5 */ | |
1238 | ) { | |
1239 | al = ssl_verify_alarm_type(s->verify_result); | |
1240 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1241 | SSL_R_CERTIFICATE_VERIFY_FAILED); | |
1242 | goto f_err; | |
1243 | } | |
1244 | ERR_clear_error(); /* but we keep s->verify_result */ | |
1245 | if (i > 1) { | |
1246 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i); | |
1247 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1248 | goto f_err; | |
1249 | } | |
1250 | ||
1251 | sc = ssl_sess_cert_new(); | |
1252 | if (sc == NULL) | |
66696478 | 1253 | goto done; |
0f113f3e | 1254 | |
62adbcee | 1255 | ssl_sess_cert_free(s->session->sess_cert); |
0f113f3e MC |
1256 | s->session->sess_cert = sc; |
1257 | ||
1258 | sc->cert_chain = sk; | |
1259 | /* | |
1260 | * Inconsistency alert: cert_chain does include the peer's certificate, | |
1261 | * which we don't include in s3_srvr.c | |
1262 | */ | |
1263 | x = sk_X509_value(sk, 0); | |
1264 | sk = NULL; | |
1265 | /* | |
1266 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end | |
1267 | */ | |
1268 | ||
1269 | pkey = X509_get_pubkey(x); | |
1270 | ||
1271 | /* VRS: allow null cert if auth == KRB5 */ | |
1272 | need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && | |
1273 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) | |
1274 | ? 0 : 1; | |
f9b3bff6 RL |
1275 | |
1276 | #ifdef KSSL_DEBUG | |
0f113f3e MC |
1277 | fprintf(stderr, "pkey,x = %p, %p\n", pkey, x); |
1278 | fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey)); | |
1279 | fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n", | |
1280 | s->s3->tmp.new_cipher->name, | |
1281 | s->s3->tmp.new_cipher->algorithm_mkey, | |
1282 | s->s3->tmp.new_cipher->algorithm_auth, need_cert); | |
1283 | #endif /* KSSL_DEBUG */ | |
1284 | ||
1285 | if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) { | |
1286 | x = NULL; | |
1287 | al = SSL3_AL_FATAL; | |
1288 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1289 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); | |
1290 | goto f_err; | |
1291 | } | |
1292 | ||
1293 | i = ssl_cert_type(x, pkey); | |
1294 | if (need_cert && i < 0) { | |
1295 | x = NULL; | |
1296 | al = SSL3_AL_FATAL; | |
1297 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1298 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
1299 | goto f_err; | |
1300 | } | |
1301 | ||
1302 | if (need_cert) { | |
1303 | int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
1304 | if (exp_idx >= 0 && i != exp_idx) { | |
1305 | x = NULL; | |
1306 | al = SSL_AD_ILLEGAL_PARAMETER; | |
1307 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, | |
1308 | SSL_R_WRONG_CERTIFICATE_TYPE); | |
1309 | goto f_err; | |
1310 | } | |
1311 | sc->peer_cert_type = i; | |
1312 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
1313 | /* | |
1314 | * Why would the following ever happen? We just created sc a couple | |
1315 | * of lines ago. | |
1316 | */ | |
222561fe | 1317 | X509_free(sc->peer_pkeys[i].x509); |
0f113f3e MC |
1318 | sc->peer_pkeys[i].x509 = x; |
1319 | sc->peer_key = &(sc->peer_pkeys[i]); | |
1320 | ||
222561fe | 1321 | X509_free(s->session->peer); |
0f113f3e MC |
1322 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
1323 | s->session->peer = x; | |
1324 | } else { | |
1325 | sc->peer_cert_type = i; | |
1326 | sc->peer_key = NULL; | |
1327 | ||
222561fe | 1328 | X509_free(s->session->peer); |
0f113f3e MC |
1329 | s->session->peer = NULL; |
1330 | } | |
1331 | s->session->verify_result = s->verify_result; | |
1332 | ||
1333 | x = NULL; | |
1334 | ret = 1; | |
66696478 RS |
1335 | goto done; |
1336 | ||
0f113f3e | 1337 | f_err: |
66696478 RS |
1338 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
1339 | done: | |
0f113f3e MC |
1340 | EVP_PKEY_free(pkey); |
1341 | X509_free(x); | |
1342 | sk_X509_pop_free(sk, X509_free); | |
1343 | return (ret); | |
1344 | } | |
d02b48c6 | 1345 | |
36d16f8e | 1346 | int ssl3_get_key_exchange(SSL *s) |
0f113f3e | 1347 | { |
bc36ee62 | 1348 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
1349 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; |
1350 | #endif | |
1351 | EVP_MD_CTX md_ctx; | |
1352 | unsigned char *param, *p; | |
1353 | int al, j, ok; | |
1354 | long i, param_len, n, alg_k, alg_a; | |
1355 | EVP_PKEY *pkey = NULL; | |
1356 | const EVP_MD *md = NULL; | |
bc36ee62 | 1357 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 1358 | RSA *rsa = NULL; |
79df9d62 | 1359 | #endif |
bc36ee62 | 1360 | #ifndef OPENSSL_NO_DH |
0f113f3e | 1361 | DH *dh = NULL; |
58964a49 | 1362 | #endif |
10bf4fc2 | 1363 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1364 | EC_KEY *ecdh = NULL; |
1365 | BN_CTX *bn_ctx = NULL; | |
1366 | EC_POINT *srvr_ecpoint = NULL; | |
1367 | int curve_nid = 0; | |
1368 | int encoded_pt_len = 0; | |
1369 | #endif | |
1370 | ||
1371 | EVP_MD_CTX_init(&md_ctx); | |
1372 | ||
1373 | /* | |
1374 | * use same message size as in ssl3_get_certificate_request() as | |
1375 | * ServerKeyExchange message may be skipped | |
1376 | */ | |
1377 | n = s->method->ssl_get_message(s, | |
1378 | SSL3_ST_CR_KEY_EXCH_A, | |
1379 | SSL3_ST_CR_KEY_EXCH_B, | |
1380 | -1, s->max_cert_list, &ok); | |
1381 | if (!ok) | |
1382 | return ((int)n); | |
1383 | ||
1384 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
1385 | ||
1386 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { | |
1387 | /* | |
1388 | * Can't skip server key exchange if this is an ephemeral | |
1389 | * ciphersuite. | |
1390 | */ | |
1391 | if (alg_k & (SSL_kDHE | SSL_kECDHE)) { | |
1392 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); | |
1393 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1394 | goto f_err; | |
1395 | } | |
ddac1974 | 1396 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1397 | /* |
1398 | * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no | |
1399 | * identity hint is sent. Set session->sess_cert anyway to avoid | |
1400 | * problems later. | |
1401 | */ | |
1402 | if (alg_k & SSL_kPSK) { | |
1403 | s->session->sess_cert = ssl_sess_cert_new(); | |
b548a1f1 | 1404 | OPENSSL_free(s->ctx->psk_identity_hint); |
0f113f3e MC |
1405 | s->ctx->psk_identity_hint = NULL; |
1406 | } | |
1407 | #endif | |
1408 | s->s3->tmp.reuse_message = 1; | |
1409 | return (1); | |
1410 | } | |
1411 | ||
1412 | param = p = (unsigned char *)s->init_msg; | |
1413 | if (s->session->sess_cert != NULL) { | |
bc36ee62 | 1414 | #ifndef OPENSSL_NO_RSA |
d6407083 RS |
1415 | RSA_free(s->session->sess_cert->peer_rsa_tmp); |
1416 | s->session->sess_cert->peer_rsa_tmp = NULL; | |
d02b48c6 | 1417 | #endif |
bc36ee62 | 1418 | #ifndef OPENSSL_NO_DH |
d6407083 RS |
1419 | DH_free(s->session->sess_cert->peer_dh_tmp); |
1420 | s->session->sess_cert->peer_dh_tmp = NULL; | |
ea262260 | 1421 | #endif |
10bf4fc2 | 1422 | #ifndef OPENSSL_NO_EC |
8fdc3734 RS |
1423 | EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); |
1424 | s->session->sess_cert->peer_ecdh_tmp = NULL; | |
d02b48c6 | 1425 | #endif |
0f113f3e MC |
1426 | } else { |
1427 | s->session->sess_cert = ssl_sess_cert_new(); | |
1428 | } | |
d02b48c6 | 1429 | |
0f113f3e MC |
1430 | /* Total length of the parameters including the length prefix */ |
1431 | param_len = 0; | |
f2be92b9 | 1432 | |
0f113f3e | 1433 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
d02b48c6 | 1434 | |
0f113f3e | 1435 | al = SSL_AD_DECODE_ERROR; |
f2be92b9 | 1436 | |
ddac1974 | 1437 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1438 | if (alg_k & SSL_kPSK) { |
1439 | char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1]; | |
1440 | ||
1441 | param_len = 2; | |
1442 | if (param_len > n) { | |
1443 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1444 | goto f_err; | |
1445 | } | |
1446 | n2s(p, i); | |
1447 | ||
1448 | /* | |
1449 | * Store PSK identity hint for later use, hint is used in | |
1450 | * ssl3_send_client_key_exchange. Assume that the maximum length of | |
1451 | * a PSK identity hint can be as long as the maximum length of a PSK | |
1452 | * identity. | |
1453 | */ | |
1454 | if (i > PSK_MAX_IDENTITY_LEN) { | |
1455 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1456 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); | |
1457 | goto f_err; | |
1458 | } | |
1459 | if (i > n - param_len) { | |
1460 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | |
1461 | SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); | |
1462 | goto f_err; | |
1463 | } | |
1464 | param_len += i; | |
1465 | ||
1466 | /* | |
1467 | * If received PSK identity hint contains NULL characters, the hint | |
1468 | * is truncated from the first NULL. p may not be ending with NULL, | |
1469 | * so create a NULL-terminated string. | |
1470 | */ | |
1471 | memcpy(tmp_id_hint, p, i); | |
1472 | memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i); | |
b548a1f1 | 1473 | OPENSSL_free(s->ctx->psk_identity_hint); |
0f113f3e MC |
1474 | s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint); |
1475 | if (s->ctx->psk_identity_hint == NULL) { | |
1476 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1477 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1478 | goto f_err; | |
1479 | } | |
1480 | ||
1481 | p += i; | |
1482 | n -= param_len; | |
1483 | } else | |
1484 | #endif /* !OPENSSL_NO_PSK */ | |
edc032b5 | 1485 | #ifndef OPENSSL_NO_SRP |
0f113f3e MC |
1486 | if (alg_k & SSL_kSRP) { |
1487 | param_len = 2; | |
1488 | if (param_len > n) { | |
1489 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1490 | goto f_err; | |
1491 | } | |
1492 | n2s(p, i); | |
1493 | ||
1494 | if (i > n - param_len) { | |
1495 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH); | |
1496 | goto f_err; | |
1497 | } | |
1498 | param_len += i; | |
1499 | ||
1500 | if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) { | |
1501 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1502 | goto err; | |
1503 | } | |
1504 | p += i; | |
1505 | ||
1506 | if (2 > n - param_len) { | |
1507 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1508 | goto f_err; | |
1509 | } | |
1510 | param_len += 2; | |
1511 | ||
1512 | n2s(p, i); | |
1513 | ||
1514 | if (i > n - param_len) { | |
1515 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH); | |
1516 | goto f_err; | |
1517 | } | |
1518 | param_len += i; | |
1519 | ||
1520 | if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) { | |
1521 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1522 | goto err; | |
1523 | } | |
1524 | p += i; | |
1525 | ||
1526 | if (1 > n - param_len) { | |
1527 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1528 | goto f_err; | |
1529 | } | |
1530 | param_len += 1; | |
1531 | ||
1532 | i = (unsigned int)(p[0]); | |
1533 | p++; | |
1534 | ||
1535 | if (i > n - param_len) { | |
1536 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH); | |
1537 | goto f_err; | |
1538 | } | |
1539 | param_len += i; | |
1540 | ||
1541 | if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) { | |
1542 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1543 | goto err; | |
1544 | } | |
1545 | p += i; | |
1546 | ||
1547 | if (2 > n - param_len) { | |
1548 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1549 | goto f_err; | |
1550 | } | |
1551 | param_len += 2; | |
1552 | ||
1553 | n2s(p, i); | |
1554 | ||
1555 | if (i > n - param_len) { | |
1556 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH); | |
1557 | goto f_err; | |
1558 | } | |
1559 | param_len += i; | |
1560 | ||
1561 | if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) { | |
1562 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1563 | goto err; | |
1564 | } | |
1565 | p += i; | |
1566 | n -= param_len; | |
1567 | ||
1568 | if (!srp_verify_server_param(s, &al)) { | |
1569 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); | |
1570 | goto f_err; | |
1571 | } | |
0989790b | 1572 | |
edc032b5 | 1573 | /* We must check if there is a certificate */ |
0f113f3e MC |
1574 | # ifndef OPENSSL_NO_RSA |
1575 | if (alg_a & SSL_aRSA) | |
1576 | pkey = | |
1577 | X509_get_pubkey(s->session-> | |
1578 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
1579 | # else | |
1580 | if (0) ; | |
1581 | # endif | |
1582 | # ifndef OPENSSL_NO_DSA | |
1583 | else if (alg_a & SSL_aDSS) | |
1584 | pkey = | |
1585 | X509_get_pubkey(s->session-> | |
1586 | sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. | |
1587 | x509); | |
1588 | # endif | |
1589 | } else | |
1590 | #endif /* !OPENSSL_NO_SRP */ | |
edc032b5 | 1591 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
1592 | if (alg_k & SSL_kRSA) { |
1593 | /* Temporary RSA keys only allowed in export ciphersuites */ | |
1594 | if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { | |
1595 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1596 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); | |
1597 | goto f_err; | |
1598 | } | |
1599 | if ((rsa = RSA_new()) == NULL) { | |
1600 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1601 | goto err; | |
1602 | } | |
1603 | ||
1604 | param_len = 2; | |
1605 | if (param_len > n) { | |
1606 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1607 | goto f_err; | |
1608 | } | |
1609 | n2s(p, i); | |
1610 | ||
1611 | if (i > n - param_len) { | |
1612 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH); | |
1613 | goto f_err; | |
1614 | } | |
1615 | param_len += i; | |
1616 | ||
1617 | if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) { | |
1618 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1619 | goto err; | |
1620 | } | |
1621 | p += i; | |
1622 | ||
1623 | if (2 > n - param_len) { | |
1624 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1625 | goto f_err; | |
1626 | } | |
1627 | param_len += 2; | |
1628 | ||
1629 | n2s(p, i); | |
1630 | ||
1631 | if (i > n - param_len) { | |
1632 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH); | |
1633 | goto f_err; | |
1634 | } | |
1635 | param_len += i; | |
1636 | ||
1637 | if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) { | |
1638 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1639 | goto err; | |
1640 | } | |
1641 | p += i; | |
1642 | n -= param_len; | |
1643 | ||
1644 | /* this should be because we are using an export cipher */ | |
1645 | if (alg_a & SSL_aRSA) | |
1646 | pkey = | |
1647 | X509_get_pubkey(s->session-> | |
1648 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
1649 | else { | |
1650 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
1651 | goto err; | |
1652 | } | |
1653 | s->session->sess_cert->peer_rsa_tmp = rsa; | |
1654 | rsa = NULL; | |
1655 | } | |
1656 | #else /* OPENSSL_NO_RSA */ | |
1657 | if (0) ; | |
d02b48c6 | 1658 | #endif |
bc36ee62 | 1659 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
1660 | else if (alg_k & SSL_kDHE) { |
1661 | if ((dh = DH_new()) == NULL) { | |
1662 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); | |
1663 | goto err; | |
1664 | } | |
1665 | ||
1666 | param_len = 2; | |
1667 | if (param_len > n) { | |
1668 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1669 | goto f_err; | |
1670 | } | |
1671 | n2s(p, i); | |
1672 | ||
1673 | if (i > n - param_len) { | |
1674 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); | |
1675 | goto f_err; | |
1676 | } | |
1677 | param_len += i; | |
1678 | ||
1679 | if (!(dh->p = BN_bin2bn(p, i, NULL))) { | |
1680 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1681 | goto err; | |
1682 | } | |
1683 | p += i; | |
1684 | ||
1685 | if (2 > n - param_len) { | |
1686 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1687 | goto f_err; | |
1688 | } | |
1689 | param_len += 2; | |
1690 | ||
1691 | n2s(p, i); | |
1692 | ||
1693 | if (i > n - param_len) { | |
1694 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH); | |
1695 | goto f_err; | |
1696 | } | |
1697 | param_len += i; | |
1698 | ||
1699 | if (!(dh->g = BN_bin2bn(p, i, NULL))) { | |
1700 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1701 | goto err; | |
1702 | } | |
1703 | p += i; | |
1704 | ||
1705 | if (2 > n - param_len) { | |
1706 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1707 | goto f_err; | |
1708 | } | |
1709 | param_len += 2; | |
1710 | ||
1711 | n2s(p, i); | |
1712 | ||
1713 | if (i > n - param_len) { | |
1714 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH); | |
1715 | goto f_err; | |
1716 | } | |
1717 | param_len += i; | |
1718 | ||
1719 | if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { | |
1720 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); | |
1721 | goto err; | |
1722 | } | |
1723 | p += i; | |
1724 | n -= param_len; | |
1725 | ||
1726 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { | |
1727 | al = SSL_AD_HANDSHAKE_FAILURE; | |
1728 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL); | |
1729 | goto f_err; | |
1730 | } | |
1731 | # ifndef OPENSSL_NO_RSA | |
1732 | if (alg_a & SSL_aRSA) | |
1733 | pkey = | |
1734 | X509_get_pubkey(s->session-> | |
1735 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
1736 | # else | |
1737 | if (0) ; | |
1738 | # endif | |
1739 | # ifndef OPENSSL_NO_DSA | |
1740 | else if (alg_a & SSL_aDSS) | |
1741 | pkey = | |
1742 | X509_get_pubkey(s->session-> | |
1743 | sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. | |
1744 | x509); | |
1745 | # endif | |
1746 | /* else anonymous DH, so no certificate or pkey. */ | |
1747 | ||
1748 | s->session->sess_cert->peer_dh_tmp = dh; | |
1749 | dh = NULL; | |
0f113f3e MC |
1750 | } |
1751 | #endif /* !OPENSSL_NO_DH */ | |
ea262260 | 1752 | |
10bf4fc2 | 1753 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1754 | else if (alg_k & SSL_kECDHE) { |
1755 | EC_GROUP *ngroup; | |
1756 | const EC_GROUP *group; | |
1757 | ||
1758 | if ((ecdh = EC_KEY_new()) == NULL) { | |
1759 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1760 | goto err; | |
1761 | } | |
1762 | ||
1763 | /* | |
1764 | * Extract elliptic curve parameters and the server's ephemeral ECDH | |
1765 | * public key. Keep accumulating lengths of various components in | |
1766 | * param_len and make sure it never exceeds n. | |
1767 | */ | |
1768 | ||
1769 | /* | |
1770 | * XXX: For now we only support named (not generic) curves and the | |
1771 | * ECParameters in this case is just three bytes. We also need one | |
1772 | * byte for the length of the encoded point | |
1773 | */ | |
1774 | param_len = 4; | |
1775 | if (param_len > n) { | |
1776 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1777 | goto f_err; | |
1778 | } | |
1779 | /* | |
1780 | * Check curve is one of our preferences, if not server has sent an | |
1781 | * invalid curve. ECParameters is 3 bytes. | |
1782 | */ | |
1783 | if (!tls1_check_curve(s, p, 3)) { | |
1784 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); | |
1785 | goto f_err; | |
1786 | } | |
1787 | ||
1788 | if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) { | |
1789 | al = SSL_AD_INTERNAL_ERROR; | |
1790 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | |
1791 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); | |
1792 | goto f_err; | |
1793 | } | |
1794 | ||
1795 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); | |
1796 | if (ngroup == NULL) { | |
1797 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); | |
1798 | goto err; | |
1799 | } | |
1800 | if (EC_KEY_set_group(ecdh, ngroup) == 0) { | |
1801 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); | |
1802 | goto err; | |
1803 | } | |
1804 | EC_GROUP_free(ngroup); | |
1805 | ||
1806 | group = EC_KEY_get0_group(ecdh); | |
1807 | ||
1808 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && | |
1809 | (EC_GROUP_get_degree(group) > 163)) { | |
1810 | al = SSL_AD_EXPORT_RESTRICTION; | |
1811 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, | |
1812 | SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); | |
1813 | goto f_err; | |
1814 | } | |
1815 | ||
1816 | p += 3; | |
1817 | ||
1818 | /* Next, get the encoded ECPoint */ | |
1819 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || | |
1820 | ((bn_ctx = BN_CTX_new()) == NULL)) { | |
1821 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
1822 | goto err; | |
1823 | } | |
1824 | ||
1825 | encoded_pt_len = *p; /* length of encoded point */ | |
1826 | p += 1; | |
1827 | ||
1828 | if ((encoded_pt_len > n - param_len) || | |
1829 | (EC_POINT_oct2point(group, srvr_ecpoint, | |
1830 | p, encoded_pt_len, bn_ctx) == 0)) { | |
1831 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); | |
1832 | goto f_err; | |
1833 | } | |
1834 | param_len += encoded_pt_len; | |
1835 | ||
1836 | n -= param_len; | |
1837 | p += encoded_pt_len; | |
1838 | ||
1839 | /* | |
1840 | * The ECC/TLS specification does not mention the use of DSA to sign | |
1841 | * ECParameters in the server key exchange message. We do support RSA | |
1842 | * and ECDSA. | |
1843 | */ | |
1844 | if (0) ; | |
1845 | # ifndef OPENSSL_NO_RSA | |
1846 | else if (alg_a & SSL_aRSA) | |
1847 | pkey = | |
1848 | X509_get_pubkey(s->session-> | |
1849 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); | |
1850 | # endif | |
10bf4fc2 | 1851 | # ifndef OPENSSL_NO_EC |
0f113f3e MC |
1852 | else if (alg_a & SSL_aECDSA) |
1853 | pkey = | |
1854 | X509_get_pubkey(s->session-> | |
1855 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); | |
1856 | # endif | |
1857 | /* else anonymous ECDH, so no certificate or pkey. */ | |
1858 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); | |
1859 | s->session->sess_cert->peer_ecdh_tmp = ecdh; | |
1860 | ecdh = NULL; | |
1861 | BN_CTX_free(bn_ctx); | |
1862 | bn_ctx = NULL; | |
1863 | EC_POINT_free(srvr_ecpoint); | |
1864 | srvr_ecpoint = NULL; | |
1865 | } else if (alg_k) { | |
1866 | al = SSL_AD_UNEXPECTED_MESSAGE; | |
1867 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); | |
1868 | goto f_err; | |
1869 | } | |
10bf4fc2 | 1870 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e MC |
1871 | |
1872 | /* p points to the next byte, there are 'n' bytes left */ | |
1873 | ||
1874 | /* if it was signed, check the signature */ | |
1875 | if (pkey != NULL) { | |
1876 | if (SSL_USE_SIGALGS(s)) { | |
1877 | int rv; | |
1878 | if (2 > n) { | |
1879 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1880 | goto f_err; | |
1881 | } | |
1882 | rv = tls12_check_peer_sigalg(&md, s, p, pkey); | |
1883 | if (rv == -1) | |
1884 | goto err; | |
1885 | else if (rv == 0) { | |
1886 | goto f_err; | |
1887 | } | |
a2f9200f | 1888 | #ifdef SSL_DEBUG |
0f113f3e MC |
1889 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
1890 | #endif | |
1891 | p += 2; | |
1892 | n -= 2; | |
1893 | } else | |
1894 | md = EVP_sha1(); | |
1895 | ||
1896 | if (2 > n) { | |
1897 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); | |
1898 | goto f_err; | |
1899 | } | |
1900 | n2s(p, i); | |
1901 | n -= 2; | |
1902 | j = EVP_PKEY_size(pkey); | |
1903 | ||
1904 | /* | |
1905 | * Check signature length. If n is 0 then signature is empty | |
1906 | */ | |
1907 | if ((i != n) || (n > j) || (n <= 0)) { | |
1908 | /* wrong packet length */ | |
1909 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); | |
1910 | goto f_err; | |
1911 | } | |
bc36ee62 | 1912 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
1913 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { |
1914 | int num; | |
1915 | unsigned int size; | |
1916 | ||
1917 | j = 0; | |
1918 | q = md_buf; | |
1919 | for (num = 2; num > 0; num--) { | |
1920 | EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); | |
1921 | EVP_DigestInit_ex(&md_ctx, (num == 2) | |
1922 | ? s->ctx->md5 : s->ctx->sha1, NULL); | |
1923 | EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1924 | SSL3_RANDOM_SIZE); | |
1925 | EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1926 | SSL3_RANDOM_SIZE); | |
1927 | EVP_DigestUpdate(&md_ctx, param, param_len); | |
1928 | EVP_DigestFinal_ex(&md_ctx, q, &size); | |
1929 | q += size; | |
1930 | j += size; | |
1931 | } | |
1932 | i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa); | |
1933 | if (i < 0) { | |
1934 | al = SSL_AD_DECRYPT_ERROR; | |
1935 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); | |
1936 | goto f_err; | |
1937 | } | |
1938 | if (i == 0) { | |
1939 | /* bad signature */ | |
1940 | al = SSL_AD_DECRYPT_ERROR; | |
1941 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); | |
1942 | goto f_err; | |
1943 | } | |
1944 | } else | |
1945 | #endif | |
1946 | { | |
1947 | EVP_VerifyInit_ex(&md_ctx, md, NULL); | |
1948 | EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), | |
1949 | SSL3_RANDOM_SIZE); | |
1950 | EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), | |
1951 | SSL3_RANDOM_SIZE); | |
1952 | EVP_VerifyUpdate(&md_ctx, param, param_len); | |
1953 | if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) { | |
1954 | /* bad signature */ | |
1955 | al = SSL_AD_DECRYPT_ERROR; | |
1956 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); | |
1957 | goto f_err; | |
1958 | } | |
1959 | } | |
1960 | } else { | |
1961 | /* aNULL, aSRP or kPSK do not need public keys */ | |
1962 | if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) { | |
1963 | /* Might be wrong key type, check it */ | |
1964 | if (ssl3_check_cert_and_algorithm(s)) | |
1965 | /* Otherwise this shouldn't happen */ | |
1966 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
1967 | goto err; | |
1968 | } | |
1969 | /* still data left over */ | |
1970 | if (n != 0) { | |
1971 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); | |
1972 | goto f_err; | |
1973 | } | |
1974 | } | |
1975 | EVP_PKEY_free(pkey); | |
1976 | EVP_MD_CTX_cleanup(&md_ctx); | |
1977 | return (1); | |
1978 | f_err: | |
1979 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1980 | err: | |
1981 | EVP_PKEY_free(pkey); | |
bc36ee62 | 1982 | #ifndef OPENSSL_NO_RSA |
d6407083 | 1983 | RSA_free(rsa); |
6b521df3 | 1984 | #endif |
bc36ee62 | 1985 | #ifndef OPENSSL_NO_DH |
d6407083 | 1986 | DH_free(dh); |
ea262260 | 1987 | #endif |
10bf4fc2 | 1988 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1989 | BN_CTX_free(bn_ctx); |
1990 | EC_POINT_free(srvr_ecpoint); | |
8fdc3734 | 1991 | EC_KEY_free(ecdh); |
6b521df3 | 1992 | #endif |
0f113f3e MC |
1993 | EVP_MD_CTX_cleanup(&md_ctx); |
1994 | return (-1); | |
1995 | } | |
d02b48c6 | 1996 | |
36d16f8e | 1997 | int ssl3_get_certificate_request(SSL *s) |
0f113f3e MC |
1998 | { |
1999 | int ok, ret = 0; | |
2000 | unsigned long n, nc, l; | |
2001 | unsigned int llen, ctype_num, i; | |
2002 | X509_NAME *xn = NULL; | |
2003 | const unsigned char *p, *q; | |
2004 | unsigned char *d; | |
2005 | STACK_OF(X509_NAME) *ca_sk = NULL; | |
2006 | ||
2007 | n = s->method->ssl_get_message(s, | |
2008 | SSL3_ST_CR_CERT_REQ_A, | |
2009 | SSL3_ST_CR_CERT_REQ_B, | |
2010 | -1, s->max_cert_list, &ok); | |
2011 | ||
2012 | if (!ok) | |
2013 | return ((int)n); | |
2014 | ||
2015 | s->s3->tmp.cert_req = 0; | |
2016 | ||
2017 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { | |
2018 | s->s3->tmp.reuse_message = 1; | |
2019 | /* | |
2020 | * If we get here we don't need any cached handshake records as we | |
2021 | * wont be doing client auth. | |
2022 | */ | |
2023 | if (s->s3->handshake_buffer) { | |
2024 | if (!ssl3_digest_cached_records(s)) | |
2025 | goto err; | |
2026 | } | |
2027 | return (1); | |
2028 | } | |
2029 | ||
2030 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { | |
2031 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | |
2032 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE); | |
2033 | goto err; | |
2034 | } | |
2035 | ||
2036 | /* TLS does not like anon-DH with client cert */ | |
2037 | if (s->version > SSL3_VERSION) { | |
2038 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { | |
2039 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | |
2040 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | |
2041 | SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); | |
2042 | goto err; | |
2043 | } | |
2044 | } | |
2045 | ||
2046 | p = d = (unsigned char *)s->init_msg; | |
2047 | ||
2048 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { | |
2049 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); | |
2050 | goto err; | |
2051 | } | |
2052 | ||
2053 | /* get the certificate types */ | |
2054 | ctype_num = *(p++); | |
b548a1f1 RS |
2055 | OPENSSL_free(s->cert->ctypes); |
2056 | s->cert->ctypes = NULL; | |
0f113f3e MC |
2057 | if (ctype_num > SSL3_CT_NUMBER) { |
2058 | /* If we exceed static buffer copy all to cert structure */ | |
2059 | s->cert->ctypes = OPENSSL_malloc(ctype_num); | |
2060 | if (s->cert->ctypes == NULL) { | |
2061 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); | |
2062 | goto err; | |
2063 | } | |
2064 | memcpy(s->cert->ctypes, p, ctype_num); | |
2065 | s->cert->ctype_num = (size_t)ctype_num; | |
2066 | ctype_num = SSL3_CT_NUMBER; | |
2067 | } | |
2068 | for (i = 0; i < ctype_num; i++) | |
2069 | s->s3->tmp.ctype[i] = p[i]; | |
2070 | p += p[-1]; | |
2071 | if (SSL_USE_SIGALGS(s)) { | |
2072 | n2s(p, llen); | |
2073 | /* | |
2074 | * Check we have enough room for signature algorithms and following | |
2075 | * length value. | |
2076 | */ | |
2077 | if ((unsigned long)(p - d + llen + 2) > n) { | |
2078 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2079 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | |
2080 | SSL_R_DATA_LENGTH_TOO_LONG); | |
2081 | goto err; | |
2082 | } | |
2083 | /* Clear certificate digests and validity flags */ | |
2084 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
2085 | s->cert->pkeys[i].digest = NULL; | |
2086 | s->cert->pkeys[i].valid_flags = 0; | |
2087 | } | |
2088 | if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) { | |
2089 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2090 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | |
2091 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); | |
2092 | goto err; | |
2093 | } | |
2094 | if (!tls1_process_sigalgs(s)) { | |
2095 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
2096 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); | |
2097 | goto err; | |
2098 | } | |
2099 | p += llen; | |
2100 | } | |
2101 | ||
2102 | /* get the CA RDNs */ | |
2103 | n2s(p, llen); | |
0f113f3e MC |
2104 | |
2105 | if ((unsigned long)(p - d + llen) != n) { | |
2106 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2107 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); | |
2108 | goto err; | |
2109 | } | |
2110 | ||
2111 | for (nc = 0; nc < llen;) { | |
2112 | n2s(p, l); | |
2113 | if ((l + nc + 2) > llen) { | |
0f113f3e MC |
2114 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
2115 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); | |
2116 | goto err; | |
2117 | } | |
2118 | ||
2119 | q = p; | |
2120 | ||
2121 | if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { | |
3c33c6f6 MC |
2122 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
2123 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); | |
2124 | goto err; | |
0f113f3e MC |
2125 | } |
2126 | ||
2127 | if (q != (p + l)) { | |
2128 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2129 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, | |
2130 | SSL_R_CA_DN_LENGTH_MISMATCH); | |
2131 | goto err; | |
2132 | } | |
2133 | if (!sk_X509_NAME_push(ca_sk, xn)) { | |
2134 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); | |
2135 | goto err; | |
2136 | } | |
2137 | ||
2138 | p += l; | |
2139 | nc += l + 2; | |
2140 | } | |
2141 | ||
0f113f3e MC |
2142 | /* we should setup a certificate to return.... */ |
2143 | s->s3->tmp.cert_req = 1; | |
2144 | s->s3->tmp.ctype_num = ctype_num; | |
222561fe | 2145 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
0f113f3e MC |
2146 | s->s3->tmp.ca_names = ca_sk; |
2147 | ca_sk = NULL; | |
2148 | ||
2149 | ret = 1; | |
2150 | err: | |
222561fe | 2151 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
0f113f3e MC |
2152 | return (ret); |
2153 | } | |
2154 | ||
2155 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
dfeab068 | 2156 | { |
0f113f3e | 2157 | return (X509_NAME_cmp(*a, *b)); |
dfeab068 | 2158 | } |
dfeab068 | 2159 | |
6434abbf DSH |
2160 | #ifndef OPENSSL_NO_TLSEXT |
2161 | int ssl3_get_new_session_ticket(SSL *s) | |
0f113f3e MC |
2162 | { |
2163 | int ok, al, ret = 0, ticklen; | |
2164 | long n; | |
2165 | const unsigned char *p; | |
2166 | unsigned char *d; | |
2167 | ||
2168 | n = s->method->ssl_get_message(s, | |
2169 | SSL3_ST_CR_SESSION_TICKET_A, | |
2170 | SSL3_ST_CR_SESSION_TICKET_B, | |
2171 | SSL3_MT_NEWSESSION_TICKET, 16384, &ok); | |
2172 | ||
2173 | if (!ok) | |
2174 | return ((int)n); | |
2175 | ||
2176 | if (n < 6) { | |
2177 | /* need at least ticket_lifetime_hint + ticket length */ | |
2178 | al = SSL_AD_DECODE_ERROR; | |
2179 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); | |
2180 | goto f_err; | |
2181 | } | |
2182 | ||
2183 | p = d = (unsigned char *)s->init_msg; | |
2184 | n2l(p, s->session->tlsext_tick_lifetime_hint); | |
2185 | n2s(p, ticklen); | |
2186 | /* ticket_lifetime_hint + ticket_length + ticket */ | |
2187 | if (ticklen + 6 != n) { | |
2188 | al = SSL_AD_DECODE_ERROR; | |
2189 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); | |
2190 | goto f_err; | |
2191 | } | |
b548a1f1 RS |
2192 | OPENSSL_free(s->session->tlsext_tick); |
2193 | s->session->tlsext_ticklen = 0; | |
0f113f3e MC |
2194 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
2195 | if (!s->session->tlsext_tick) { | |
2196 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); | |
2197 | goto err; | |
2198 | } | |
2199 | memcpy(s->session->tlsext_tick, p, ticklen); | |
2200 | s->session->tlsext_ticklen = ticklen; | |
2201 | /* | |
2202 | * There are two ways to detect a resumed ticket session. One is to set | |
2203 | * an appropriate session ID and then the server must return a match in | |
2204 | * ServerHello. This allows the normal client session ID matching to work | |
2205 | * and we know much earlier that the ticket has been accepted. The | |
2206 | * other way is to set zero length session ID when the ticket is | |
2207 | * presented and rely on the handshake to determine session resumption. | |
2208 | * We choose the former approach because this fits in with assumptions | |
2209 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is | |
2210 | * SHA256 is disabled) hash of the ticket. | |
2211 | */ | |
2212 | EVP_Digest(p, ticklen, | |
2213 | s->session->session_id, &s->session->session_id_length, | |
0f113f3e | 2214 | EVP_sha256(), NULL); |
0f113f3e MC |
2215 | ret = 1; |
2216 | return (ret); | |
2217 | f_err: | |
2218 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2219 | err: | |
2220 | return (-1); | |
2221 | } | |
67c8e7f4 DSH |
2222 | |
2223 | int ssl3_get_cert_status(SSL *s) | |
0f113f3e MC |
2224 | { |
2225 | int ok, al; | |
2226 | unsigned long resplen, n; | |
2227 | const unsigned char *p; | |
2228 | ||
2229 | n = s->method->ssl_get_message(s, | |
2230 | SSL3_ST_CR_CERT_STATUS_A, | |
2231 | SSL3_ST_CR_CERT_STATUS_B, | |
2232 | SSL3_MT_CERTIFICATE_STATUS, 16384, &ok); | |
2233 | ||
2234 | if (!ok) | |
2235 | return ((int)n); | |
2236 | if (n < 4) { | |
2237 | /* need at least status type + length */ | |
2238 | al = SSL_AD_DECODE_ERROR; | |
2239 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); | |
2240 | goto f_err; | |
2241 | } | |
2242 | p = (unsigned char *)s->init_msg; | |
2243 | if (*p++ != TLSEXT_STATUSTYPE_ocsp) { | |
2244 | al = SSL_AD_DECODE_ERROR; | |
2245 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); | |
2246 | goto f_err; | |
2247 | } | |
2248 | n2l3(p, resplen); | |
2249 | if (resplen + 4 != n) { | |
2250 | al = SSL_AD_DECODE_ERROR; | |
2251 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); | |
2252 | goto f_err; | |
2253 | } | |
b548a1f1 | 2254 | OPENSSL_free(s->tlsext_ocsp_resp); |
0f113f3e MC |
2255 | s->tlsext_ocsp_resp = BUF_memdup(p, resplen); |
2256 | if (!s->tlsext_ocsp_resp) { | |
2257 | al = SSL_AD_INTERNAL_ERROR; | |
2258 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); | |
2259 | goto f_err; | |
2260 | } | |
2261 | s->tlsext_ocsp_resplen = resplen; | |
2262 | if (s->ctx->tlsext_status_cb) { | |
2263 | int ret; | |
2264 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | |
2265 | if (ret == 0) { | |
2266 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | |
2267 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); | |
2268 | goto f_err; | |
2269 | } | |
2270 | if (ret < 0) { | |
2271 | al = SSL_AD_INTERNAL_ERROR; | |
2272 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); | |
2273 | goto f_err; | |
2274 | } | |
2275 | } | |
2276 | return 1; | |
2277 | f_err: | |
2278 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2279 | return (-1); | |
2280 | } | |
6434abbf | 2281 | #endif |
d02b48c6 | 2282 | |
36d16f8e | 2283 | int ssl3_get_server_done(SSL *s) |
0f113f3e MC |
2284 | { |
2285 | int ok, ret = 0; | |
2286 | long n; | |
2287 | ||
2288 | /* Second to last param should be very small, like 0 :-) */ | |
2289 | n = s->method->ssl_get_message(s, | |
2290 | SSL3_ST_CR_SRVR_DONE_A, | |
2291 | SSL3_ST_CR_SRVR_DONE_B, | |
2292 | SSL3_MT_SERVER_DONE, 30, &ok); | |
2293 | ||
2294 | if (!ok) | |
2295 | return ((int)n); | |
2296 | if (n > 0) { | |
2297 | /* should contain no data */ | |
2298 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | |
2299 | SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); | |
2300 | return -1; | |
2301 | } | |
2302 | ret = 1; | |
2303 | return (ret); | |
2304 | } | |
176f31dd | 2305 | |
36d16f8e | 2306 | int ssl3_send_client_key_exchange(SSL *s) |
0f113f3e MC |
2307 | { |
2308 | unsigned char *p; | |
2309 | int n; | |
2310 | unsigned long alg_k; | |
bc36ee62 | 2311 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
2312 | unsigned char *q; |
2313 | EVP_PKEY *pkey = NULL; | |
79df9d62 | 2314 | #endif |
bc36ee62 | 2315 | #ifndef OPENSSL_NO_KRB5 |
0f113f3e MC |
2316 | KSSL_ERR kssl_err; |
2317 | #endif /* OPENSSL_NO_KRB5 */ | |
10bf4fc2 | 2318 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2319 | EC_KEY *clnt_ecdh = NULL; |
2320 | const EC_POINT *srvr_ecpoint = NULL; | |
2321 | EVP_PKEY *srvr_pub_pkey = NULL; | |
2322 | unsigned char *encodedPoint = NULL; | |
2323 | int encoded_pt_len = 0; | |
2324 | BN_CTX *bn_ctx = NULL; | |
ea262260 | 2325 | #endif |
c660ec63 DSH |
2326 | unsigned char *pms = NULL; |
2327 | size_t pmslen = 0; | |
d02b48c6 | 2328 | |
0f113f3e MC |
2329 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) { |
2330 | p = ssl_handshake_start(s); | |
d02b48c6 | 2331 | |
0f113f3e | 2332 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
d02b48c6 | 2333 | |
0f113f3e MC |
2334 | /* Fool emacs indentation */ |
2335 | if (0) { | |
2336 | } | |
bc36ee62 | 2337 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
2338 | else if (alg_k & SSL_kRSA) { |
2339 | RSA *rsa; | |
c660ec63 DSH |
2340 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
2341 | pms = OPENSSL_malloc(pmslen); | |
2342 | if (!pms) | |
2343 | goto memerr; | |
0f113f3e MC |
2344 | |
2345 | if (s->session->sess_cert == NULL) { | |
2346 | /* | |
2347 | * We should always have a server certificate with SSL_kRSA. | |
2348 | */ | |
2349 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2350 | ERR_R_INTERNAL_ERROR); | |
2351 | goto err; | |
2352 | } | |
2353 | ||
2354 | if (s->session->sess_cert->peer_rsa_tmp != NULL) | |
2355 | rsa = s->session->sess_cert->peer_rsa_tmp; | |
2356 | else { | |
2357 | pkey = | |
2358 | X509_get_pubkey(s->session-> | |
2359 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC]. | |
2360 | x509); | |
2361 | if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) | |
2362 | || (pkey->pkey.rsa == NULL)) { | |
2363 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2364 | ERR_R_INTERNAL_ERROR); | |
2365 | goto err; | |
2366 | } | |
2367 | rsa = pkey->pkey.rsa; | |
2368 | EVP_PKEY_free(pkey); | |
2369 | } | |
2370 | ||
c660ec63 DSH |
2371 | pms[0] = s->client_version >> 8; |
2372 | pms[1] = s->client_version & 0xff; | |
2373 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) | |
0f113f3e MC |
2374 | goto err; |
2375 | ||
0f113f3e MC |
2376 | q = p; |
2377 | /* Fix buf for TLS and beyond */ | |
2378 | if (s->version > SSL3_VERSION) | |
2379 | p += 2; | |
c660ec63 | 2380 | n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING); |
0f113f3e MC |
2381 | # ifdef PKCS1_CHECK |
2382 | if (s->options & SSL_OP_PKCS1_CHECK_1) | |
2383 | p[1]++; | |
2384 | if (s->options & SSL_OP_PKCS1_CHECK_2) | |
2385 | tmp_buf[0] = 0x70; | |
2386 | # endif | |
2387 | if (n <= 0) { | |
2388 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2389 | SSL_R_BAD_RSA_ENCRYPT); | |
2390 | goto err; | |
2391 | } | |
2392 | ||
2393 | /* Fix buf for TLS and beyond */ | |
2394 | if (s->version > SSL3_VERSION) { | |
2395 | s2n(n, q); | |
2396 | n += 2; | |
2397 | } | |
0f113f3e | 2398 | } |
f9b3bff6 | 2399 | #endif |
bc36ee62 | 2400 | #ifndef OPENSSL_NO_KRB5 |
0f113f3e MC |
2401 | else if (alg_k & SSL_kKRB5) { |
2402 | krb5_error_code krb5rc; | |
2403 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | |
2404 | /* krb5_data krb5_ap_req; */ | |
2405 | krb5_data *enc_ticket; | |
2406 | krb5_data authenticator, *authp = NULL; | |
2407 | EVP_CIPHER_CTX ciph_ctx; | |
2408 | const EVP_CIPHER *enc = NULL; | |
2409 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
2410 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; | |
2411 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH]; | |
2412 | int padl, outl = sizeof(epms); | |
2413 | ||
2414 | EVP_CIPHER_CTX_init(&ciph_ctx); | |
2415 | ||
2416 | # ifdef KSSL_DEBUG | |
2417 | fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n", | |
2418 | alg_k, SSL_kKRB5); | |
2419 | # endif /* KSSL_DEBUG */ | |
2420 | ||
2421 | authp = NULL; | |
2422 | # ifdef KRB5SENDAUTH | |
2423 | if (KRB5SENDAUTH) | |
2424 | authp = &authenticator; | |
2425 | # endif /* KRB5SENDAUTH */ | |
2426 | ||
2427 | krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err); | |
2428 | enc = kssl_map_enc(kssl_ctx->enctype); | |
2429 | if (enc == NULL) | |
2430 | goto err; | |
2431 | # ifdef KSSL_DEBUG | |
2432 | { | |
2433 | fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc); | |
2434 | if (krb5rc && kssl_err.text) | |
2435 | fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n", | |
2436 | kssl_err.text); | |
2437 | } | |
2438 | # endif /* KSSL_DEBUG */ | |
2439 | ||
2440 | if (krb5rc) { | |
2441 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
2442 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); | |
2443 | goto err; | |
2444 | } | |
2445 | ||
50e735f9 MC |
2446 | /*- |
2447 | * 20010406 VRS - Earlier versions used KRB5 AP_REQ | |
2448 | * in place of RFC 2712 KerberosWrapper, as in: | |
2449 | * | |
2450 | * Send ticket (copy to *p, set n = length) | |
2451 | * n = krb5_ap_req.length; | |
2452 | * memcpy(p, krb5_ap_req.data, krb5_ap_req.length); | |
2453 | * if (krb5_ap_req.data) | |
2454 | * kssl_krb5_free_data_contents(NULL,&krb5_ap_req); | |
2455 | * | |
2456 | * Now using real RFC 2712 KerberosWrapper | |
2457 | * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) | |
2458 | * Note: 2712 "opaque" types are here replaced | |
2459 | * with a 2-byte length followed by the value. | |
2460 | * Example: | |
2461 | * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms | |
2462 | * Where "xx xx" = length bytes. Shown here with | |
2463 | * optional authenticator omitted. | |
2464 | */ | |
0f113f3e MC |
2465 | |
2466 | /* KerberosWrapper.Ticket */ | |
2467 | s2n(enc_ticket->length, p); | |
2468 | memcpy(p, enc_ticket->data, enc_ticket->length); | |
2469 | p += enc_ticket->length; | |
2470 | n = enc_ticket->length + 2; | |
2471 | ||
2472 | /* KerberosWrapper.Authenticator */ | |
2473 | if (authp && authp->length) { | |
2474 | s2n(authp->length, p); | |
2475 | memcpy(p, authp->data, authp->length); | |
2476 | p += authp->length; | |
2477 | n += authp->length + 2; | |
2478 | ||
2479 | free(authp->data); | |
2480 | authp->data = NULL; | |
2481 | authp->length = 0; | |
2482 | } else { | |
2483 | s2n(0, p); /* null authenticator length */ | |
2484 | n += 2; | |
2485 | } | |
2486 | ||
c660ec63 DSH |
2487 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
2488 | pms = OPENSSL_malloc(pmslen); | |
2489 | if (!pms) | |
2490 | goto memerr; | |
2491 | ||
2492 | pms[0] = s->client_version >> 8; | |
2493 | pms[1] = s->client_version & 0xff; | |
2494 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) | |
0f113f3e MC |
2495 | goto err; |
2496 | ||
35a1cc90 MC |
2497 | /*- |
2498 | * 20010420 VRS. Tried it this way; failed. | |
2499 | * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); | |
2500 | * EVP_CIPHER_CTX_set_key_length(&ciph_ctx, | |
2501 | * kssl_ctx->length); | |
2502 | * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); | |
2503 | */ | |
0f113f3e MC |
2504 | |
2505 | memset(iv, 0, sizeof iv); /* per RFC 1510 */ | |
2506 | EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv); | |
c660ec63 | 2507 | EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen); |
0f113f3e MC |
2508 | EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl); |
2509 | outl += padl; | |
2510 | if (outl > (int)sizeof epms) { | |
2511 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2512 | ERR_R_INTERNAL_ERROR); | |
2513 | goto err; | |
2514 | } | |
2515 | EVP_CIPHER_CTX_cleanup(&ciph_ctx); | |
2516 | ||
2517 | /* KerberosWrapper.EncryptedPreMasterSecret */ | |
2518 | s2n(outl, p); | |
2519 | memcpy(p, epms, outl); | |
2520 | p += outl; | |
2521 | n += outl + 2; | |
0f113f3e MC |
2522 | OPENSSL_cleanse(epms, outl); |
2523 | } | |
d02b48c6 | 2524 | #endif |
bc36ee62 | 2525 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
2526 | else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { |
2527 | DH *dh_srvr, *dh_clnt; | |
2528 | SESS_CERT *scert = s->session->sess_cert; | |
2529 | ||
2530 | if (scert == NULL) { | |
2531 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | |
2532 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2533 | SSL_R_UNEXPECTED_MESSAGE); | |
2534 | goto err; | |
2535 | } | |
2536 | ||
2537 | if (scert->peer_dh_tmp != NULL) | |
2538 | dh_srvr = scert->peer_dh_tmp; | |
2539 | else { | |
2540 | /* we get them from the cert */ | |
2541 | int idx = scert->peer_cert_type; | |
2542 | EVP_PKEY *spkey = NULL; | |
2543 | dh_srvr = NULL; | |
2544 | if (idx >= 0) | |
2545 | spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509); | |
2546 | if (spkey) { | |
2547 | dh_srvr = EVP_PKEY_get1_DH(spkey); | |
2548 | EVP_PKEY_free(spkey); | |
2549 | } | |
2550 | if (dh_srvr == NULL) { | |
2551 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2552 | ERR_R_INTERNAL_ERROR); | |
2553 | goto err; | |
2554 | } | |
2555 | } | |
2556 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { | |
2557 | /* Use client certificate key */ | |
2558 | EVP_PKEY *clkey = s->cert->key->privatekey; | |
2559 | dh_clnt = NULL; | |
2560 | if (clkey) | |
2561 | dh_clnt = EVP_PKEY_get1_DH(clkey); | |
2562 | if (dh_clnt == NULL) { | |
2563 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2564 | ERR_R_INTERNAL_ERROR); | |
2565 | goto err; | |
2566 | } | |
2567 | } else { | |
2568 | /* generate a new random key */ | |
2569 | if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { | |
2570 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
2571 | goto err; | |
2572 | } | |
2573 | if (!DH_generate_key(dh_clnt)) { | |
2574 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
2575 | DH_free(dh_clnt); | |
2576 | goto err; | |
2577 | } | |
2578 | } | |
2579 | ||
c660ec63 DSH |
2580 | pmslen = DH_size(dh_clnt); |
2581 | pms = OPENSSL_malloc(pmslen); | |
2582 | if (!pms) | |
2583 | goto memerr; | |
2584 | ||
0f113f3e MC |
2585 | /* |
2586 | * use the 'p' output buffer for the DH key, but make sure to | |
2587 | * clear it out afterwards | |
2588 | */ | |
2589 | ||
c660ec63 | 2590 | n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); |
0f113f3e MC |
2591 | if (scert->peer_dh_tmp == NULL) |
2592 | DH_free(dh_srvr); | |
2593 | ||
2594 | if (n <= 0) { | |
2595 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); | |
2596 | DH_free(dh_clnt); | |
2597 | goto err; | |
2598 | } | |
6b937f8b | 2599 | pmslen = n; |
0f113f3e | 2600 | |
0f113f3e MC |
2601 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
2602 | n = 0; | |
2603 | else { | |
2604 | /* send off the data */ | |
2605 | n = BN_num_bytes(dh_clnt->pub_key); | |
2606 | s2n(n, p); | |
2607 | BN_bn2bin(dh_clnt->pub_key, p); | |
2608 | n += 2; | |
2609 | } | |
2610 | ||
2611 | DH_free(dh_clnt); | |
2612 | ||
2613 | /* perhaps clean things up a bit EAY EAY EAY EAY */ | |
2614 | } | |
d02b48c6 | 2615 | #endif |
ea262260 | 2616 | |
10bf4fc2 | 2617 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2618 | else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) { |
2619 | const EC_GROUP *srvr_group = NULL; | |
2620 | EC_KEY *tkey; | |
2621 | int ecdh_clnt_cert = 0; | |
2622 | int field_size = 0; | |
2623 | ||
2624 | if (s->session->sess_cert == NULL) { | |
2625 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | |
2626 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2627 | SSL_R_UNEXPECTED_MESSAGE); | |
2628 | goto err; | |
2629 | } | |
2630 | ||
2631 | /* | |
2632 | * Did we send out the client's ECDH share for use in premaster | |
2633 | * computation as part of client certificate? If so, set | |
2634 | * ecdh_clnt_cert to 1. | |
2635 | */ | |
2636 | if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { | |
50e735f9 MC |
2637 | /*- |
2638 | * XXX: For now, we do not support client | |
2639 | * authentication using ECDH certificates. | |
2640 | * To add such support, one needs to add | |
2641 | * code that checks for appropriate | |
2642 | * conditions and sets ecdh_clnt_cert to 1. | |
2643 | * For example, the cert have an ECC | |
2644 | * key on the same curve as the server's | |
2645 | * and the key should be authorized for | |
2646 | * key agreement. | |
2647 | * | |
2648 | * One also needs to add code in ssl3_connect | |
2649 | * to skip sending the certificate verify | |
2650 | * message. | |
2651 | * | |
2652 | * if ((s->cert->key->privatekey != NULL) && | |
2653 | * (s->cert->key->privatekey->type == | |
2654 | * EVP_PKEY_EC) && ...) | |
2655 | * ecdh_clnt_cert = 1; | |
2656 | */ | |
0f113f3e MC |
2657 | } |
2658 | ||
2659 | if (s->session->sess_cert->peer_ecdh_tmp != NULL) { | |
2660 | tkey = s->session->sess_cert->peer_ecdh_tmp; | |
2661 | } else { | |
2662 | /* Get the Server Public Key from Cert */ | |
2663 | srvr_pub_pkey = | |
2664 | X509_get_pubkey(s->session-> | |
2665 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); | |
2666 | if ((srvr_pub_pkey == NULL) | |
2667 | || (srvr_pub_pkey->type != EVP_PKEY_EC) | |
2668 | || (srvr_pub_pkey->pkey.ec == NULL)) { | |
2669 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2670 | ERR_R_INTERNAL_ERROR); | |
2671 | goto err; | |
2672 | } | |
2673 | ||
2674 | tkey = srvr_pub_pkey->pkey.ec; | |
2675 | } | |
2676 | ||
2677 | srvr_group = EC_KEY_get0_group(tkey); | |
2678 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); | |
2679 | ||
2680 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { | |
2681 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2682 | ERR_R_INTERNAL_ERROR); | |
2683 | goto err; | |
2684 | } | |
2685 | ||
2686 | if ((clnt_ecdh = EC_KEY_new()) == NULL) { | |
2687 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2688 | ERR_R_MALLOC_FAILURE); | |
2689 | goto err; | |
2690 | } | |
2691 | ||
2692 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { | |
2693 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | |
2694 | goto err; | |
2695 | } | |
2696 | if (ecdh_clnt_cert) { | |
2697 | /* | |
2698 | * Reuse key info from our certificate We only need our | |
2699 | * private key to perform the ECDH computation. | |
2700 | */ | |
2701 | const BIGNUM *priv_key; | |
2702 | tkey = s->cert->key->privatekey->pkey.ec; | |
2703 | priv_key = EC_KEY_get0_private_key(tkey); | |
2704 | if (priv_key == NULL) { | |
2705 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2706 | ERR_R_MALLOC_FAILURE); | |
2707 | goto err; | |
2708 | } | |
2709 | if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { | |
2710 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); | |
2711 | goto err; | |
2712 | } | |
2713 | } else { | |
2714 | /* Generate a new ECDH key pair */ | |
2715 | if (!(EC_KEY_generate_key(clnt_ecdh))) { | |
2716 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2717 | ERR_R_ECDH_LIB); | |
2718 | goto err; | |
2719 | } | |
2720 | } | |
2721 | ||
2722 | /* | |
2723 | * use the 'p' output buffer for the ECDH key, but make sure to | |
2724 | * clear it out afterwards | |
2725 | */ | |
2726 | ||
2727 | field_size = EC_GROUP_get_degree(srvr_group); | |
2728 | if (field_size <= 0) { | |
2729 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); | |
2730 | goto err; | |
2731 | } | |
c660ec63 DSH |
2732 | pmslen = (field_size + 7) / 8; |
2733 | pms = OPENSSL_malloc(pmslen); | |
2734 | if (!pms) | |
2735 | goto memerr; | |
2736 | n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL); | |
2737 | if (n <= 0 || pmslen != (size_t)n) { | |
0f113f3e MC |
2738 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); |
2739 | goto err; | |
2740 | } | |
2741 | ||
0f113f3e MC |
2742 | if (ecdh_clnt_cert) { |
2743 | /* Send empty client key exch message */ | |
2744 | n = 0; | |
2745 | } else { | |
2746 | /* | |
2747 | * First check the size of encoding and allocate memory | |
2748 | * accordingly. | |
2749 | */ | |
2750 | encoded_pt_len = | |
2751 | EC_POINT_point2oct(srvr_group, | |
2752 | EC_KEY_get0_public_key(clnt_ecdh), | |
2753 | POINT_CONVERSION_UNCOMPRESSED, | |
2754 | NULL, 0, NULL); | |
2755 | ||
2756 | encodedPoint = (unsigned char *) | |
2757 | OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); | |
2758 | bn_ctx = BN_CTX_new(); | |
2759 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { | |
2760 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2761 | ERR_R_MALLOC_FAILURE); | |
2762 | goto err; | |
2763 | } | |
2764 | ||
2765 | /* Encode the public key */ | |
2766 | n = EC_POINT_point2oct(srvr_group, | |
2767 | EC_KEY_get0_public_key(clnt_ecdh), | |
2768 | POINT_CONVERSION_UNCOMPRESSED, | |
2769 | encodedPoint, encoded_pt_len, bn_ctx); | |
2770 | ||
2771 | *p = n; /* length of encoded point */ | |
2772 | /* Encoded point will be copied here */ | |
2773 | p += 1; | |
2774 | /* copy the point */ | |
2775 | memcpy((unsigned char *)p, encodedPoint, n); | |
2776 | /* increment n to account for length field */ | |
2777 | n += 1; | |
2778 | } | |
2779 | ||
2780 | /* Free allocated memory */ | |
2781 | BN_CTX_free(bn_ctx); | |
b548a1f1 | 2782 | OPENSSL_free(encodedPoint); |
8fdc3734 | 2783 | EC_KEY_free(clnt_ecdh); |
0f113f3e MC |
2784 | EVP_PKEY_free(srvr_pub_pkey); |
2785 | } | |
10bf4fc2 | 2786 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e MC |
2787 | else if (alg_k & SSL_kGOST) { |
2788 | /* GOST key exchange message creation */ | |
2789 | EVP_PKEY_CTX *pkey_ctx; | |
2790 | X509 *peer_cert; | |
2791 | size_t msglen; | |
2792 | unsigned int md_len; | |
2793 | int keytype; | |
c660ec63 | 2794 | unsigned char shared_ukm[32], tmp[256]; |
0f113f3e MC |
2795 | EVP_MD_CTX *ukm_hash; |
2796 | EVP_PKEY *pub_key; | |
2797 | ||
c660ec63 DSH |
2798 | pmslen = 32; |
2799 | pms = OPENSSL_malloc(pmslen); | |
2800 | if (!pms) | |
2801 | goto memerr; | |
2802 | ||
0f113f3e MC |
2803 | /* |
2804 | * Get server sertificate PKEY and create ctx from it | |
2805 | */ | |
2806 | peer_cert = | |
2807 | s->session-> | |
2808 | sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509; | |
2809 | if (!peer_cert) | |
2810 | peer_cert = | |
2811 | s->session-> | |
2812 | sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509; | |
2813 | if (!peer_cert) { | |
2814 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2815 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); | |
2816 | goto err; | |
2817 | } | |
2818 | ||
2819 | pkey_ctx = EVP_PKEY_CTX_new(pub_key = | |
2820 | X509_get_pubkey(peer_cert), NULL); | |
2821 | /* | |
2822 | * If we have send a certificate, and certificate key | |
2823 | * | |
2824 | * * parameters match those of server certificate, use | |
2825 | * certificate key for key exchange | |
2826 | */ | |
2827 | ||
2828 | /* Otherwise, generate ephemeral key pair */ | |
2829 | ||
2830 | EVP_PKEY_encrypt_init(pkey_ctx); | |
2831 | /* Generate session key */ | |
266483d2 MC |
2832 | if (RAND_bytes(pms, pmslen) <= 0) { |
2833 | EVP_PKEY_CTX_free(pkey_ctx); | |
2834 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2835 | ERR_R_INTERNAL_ERROR); | |
2836 | goto err; | |
2837 | }; | |
0f113f3e MC |
2838 | /* |
2839 | * If we have client certificate, use its secret as peer key | |
2840 | */ | |
2841 | if (s->s3->tmp.cert_req && s->cert->key->privatekey) { | |
2842 | if (EVP_PKEY_derive_set_peer | |
2843 | (pkey_ctx, s->cert->key->privatekey) <= 0) { | |
2844 | /* | |
2845 | * If there was an error - just ignore it. Ephemeral key | |
2846 | * * would be used | |
2847 | */ | |
2848 | ERR_clear_error(); | |
2849 | } | |
2850 | } | |
2851 | /* | |
2852 | * Compute shared IV and store it in algorithm-specific context | |
2853 | * data | |
2854 | */ | |
2855 | ukm_hash = EVP_MD_CTX_create(); | |
2856 | EVP_DigestInit(ukm_hash, | |
2857 | EVP_get_digestbynid(NID_id_GostR3411_94)); | |
2858 | EVP_DigestUpdate(ukm_hash, s->s3->client_random, | |
2859 | SSL3_RANDOM_SIZE); | |
2860 | EVP_DigestUpdate(ukm_hash, s->s3->server_random, | |
2861 | SSL3_RANDOM_SIZE); | |
2862 | EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); | |
2863 | EVP_MD_CTX_destroy(ukm_hash); | |
2864 | if (EVP_PKEY_CTX_ctrl | |
2865 | (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, | |
2866 | shared_ukm) < 0) { | |
2867 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2868 | SSL_R_LIBRARY_BUG); | |
2869 | goto err; | |
2870 | } | |
2871 | /* Make GOST keytransport blob message */ | |
2872 | /* | |
2873 | * Encapsulate it into sequence | |
2874 | */ | |
2875 | *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; | |
2876 | msglen = 255; | |
c660ec63 | 2877 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) { |
0f113f3e MC |
2878 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
2879 | SSL_R_LIBRARY_BUG); | |
2880 | goto err; | |
2881 | } | |
2882 | if (msglen >= 0x80) { | |
2883 | *(p++) = 0x81; | |
2884 | *(p++) = msglen & 0xff; | |
2885 | n = msglen + 3; | |
2886 | } else { | |
2887 | *(p++) = msglen & 0xff; | |
2888 | n = msglen + 2; | |
2889 | } | |
2890 | memcpy(p, tmp, msglen); | |
2891 | /* Check if pubkey from client certificate was used */ | |
2892 | if (EVP_PKEY_CTX_ctrl | |
2893 | (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { | |
2894 | /* Set flag "skip certificate verify" */ | |
2895 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | |
2896 | } | |
2897 | EVP_PKEY_CTX_free(pkey_ctx); | |
0f113f3e MC |
2898 | EVP_PKEY_free(pub_key); |
2899 | ||
2900 | } | |
edc032b5 | 2901 | #ifndef OPENSSL_NO_SRP |
0f113f3e MC |
2902 | else if (alg_k & SSL_kSRP) { |
2903 | if (s->srp_ctx.A != NULL) { | |
2904 | /* send off the data */ | |
2905 | n = BN_num_bytes(s->srp_ctx.A); | |
2906 | s2n(n, p); | |
2907 | BN_bn2bin(s->srp_ctx.A, p); | |
2908 | n += 2; | |
2909 | } else { | |
2910 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2911 | ERR_R_INTERNAL_ERROR); | |
2912 | goto err; | |
2913 | } | |
b548a1f1 | 2914 | OPENSSL_free(s->session->srp_username); |
0f113f3e MC |
2915 | s->session->srp_username = BUF_strdup(s->srp_ctx.login); |
2916 | if (s->session->srp_username == NULL) { | |
2917 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2918 | ERR_R_MALLOC_FAILURE); | |
2919 | goto err; | |
2920 | } | |
0f113f3e | 2921 | } |
edc032b5 | 2922 | #endif |
ddac1974 | 2923 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
2924 | else if (alg_k & SSL_kPSK) { |
2925 | /* | |
2926 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a | |
2927 | * \0-terminated identity. The last byte is for us for simulating | |
2928 | * strnlen. | |
2929 | */ | |
2930 | char identity[PSK_MAX_IDENTITY_LEN + 2]; | |
2931 | size_t identity_len; | |
2932 | unsigned char *t = NULL; | |
c660ec63 | 2933 | unsigned int psk_len = 0; |
0f113f3e MC |
2934 | int psk_err = 1; |
2935 | ||
2936 | n = 0; | |
2937 | if (s->psk_client_callback == NULL) { | |
2938 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2939 | SSL_R_PSK_NO_CLIENT_CB); | |
2940 | goto err; | |
2941 | } | |
2942 | ||
2943 | memset(identity, 0, sizeof(identity)); | |
c660ec63 DSH |
2944 | /* Allocate maximum size buffer */ |
2945 | pmslen = PSK_MAX_PSK_LEN * 2 + 4; | |
2946 | pms = OPENSSL_malloc(pmslen); | |
2947 | if (!pms) | |
2948 | goto memerr; | |
2949 | ||
0f113f3e MC |
2950 | psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint, |
2951 | identity, sizeof(identity) - 1, | |
c660ec63 | 2952 | pms, pmslen); |
0f113f3e MC |
2953 | if (psk_len > PSK_MAX_PSK_LEN) { |
2954 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2955 | ERR_R_INTERNAL_ERROR); | |
2956 | goto psk_err; | |
2957 | } else if (psk_len == 0) { | |
2958 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2959 | SSL_R_PSK_IDENTITY_NOT_FOUND); | |
2960 | goto psk_err; | |
2961 | } | |
c660ec63 DSH |
2962 | /* Change pmslen to real length */ |
2963 | pmslen = 2 + psk_len + 2 + psk_len; | |
0f113f3e MC |
2964 | identity[PSK_MAX_IDENTITY_LEN + 1] = '\0'; |
2965 | identity_len = strlen(identity); | |
2966 | if (identity_len > PSK_MAX_IDENTITY_LEN) { | |
2967 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2968 | ERR_R_INTERNAL_ERROR); | |
2969 | goto psk_err; | |
2970 | } | |
2971 | /* create PSK pre_master_secret */ | |
c660ec63 DSH |
2972 | t = pms; |
2973 | memmove(pms + psk_len + 4, pms, psk_len); | |
0f113f3e MC |
2974 | s2n(psk_len, t); |
2975 | memset(t, 0, psk_len); | |
2976 | t += psk_len; | |
2977 | s2n(psk_len, t); | |
2978 | ||
b548a1f1 | 2979 | OPENSSL_free(s->session->psk_identity_hint); |
0f113f3e MC |
2980 | s->session->psk_identity_hint = |
2981 | BUF_strdup(s->ctx->psk_identity_hint); | |
2982 | if (s->ctx->psk_identity_hint != NULL | |
2983 | && s->session->psk_identity_hint == NULL) { | |
2984 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2985 | ERR_R_MALLOC_FAILURE); | |
2986 | goto psk_err; | |
2987 | } | |
2988 | ||
b548a1f1 | 2989 | OPENSSL_free(s->session->psk_identity); |
0f113f3e MC |
2990 | s->session->psk_identity = BUF_strdup(identity); |
2991 | if (s->session->psk_identity == NULL) { | |
2992 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
2993 | ERR_R_MALLOC_FAILURE); | |
2994 | goto psk_err; | |
2995 | } | |
2996 | ||
0f113f3e MC |
2997 | s2n(identity_len, p); |
2998 | memcpy(p, identity, identity_len); | |
2999 | n = 2 + identity_len; | |
3000 | psk_err = 0; | |
3001 | psk_err: | |
3002 | OPENSSL_cleanse(identity, sizeof(identity)); | |
0f113f3e MC |
3003 | if (psk_err != 0) { |
3004 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
3005 | goto err; | |
3006 | } | |
3007 | } | |
3008 | #endif | |
3009 | else { | |
3010 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
3011 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
3012 | goto err; | |
3013 | } | |
3014 | ||
61986d32 | 3015 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) { |
77d514c5 MC |
3016 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
3017 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
3018 | goto err; | |
3019 | } | |
3020 | ||
0f113f3e MC |
3021 | s->state = SSL3_ST_CW_KEY_EXCH_B; |
3022 | } | |
3023 | ||
3024 | /* SSL3_ST_CW_KEY_EXCH_B */ | |
c660ec63 DSH |
3025 | n = ssl_do_write(s); |
3026 | #ifndef OPENSSL_NO_SRP | |
3027 | /* Check for SRP */ | |
3028 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { | |
3029 | /* | |
3030 | * If everything written generate master key: no need to save PMS as | |
3031 | * SRP_generate_client_master_secret generates it internally. | |
3032 | */ | |
3033 | if (n > 0) { | |
3034 | if ((s->session->master_key_length = | |
3035 | SRP_generate_client_master_secret(s, | |
3036 | s->session->master_key)) < | |
3037 | 0) { | |
3038 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, | |
3039 | ERR_R_INTERNAL_ERROR); | |
3040 | goto err; | |
3041 | } | |
3042 | } | |
3043 | } else | |
3044 | #endif | |
3045 | /* If we haven't written everything save PMS */ | |
3046 | if (n <= 0) { | |
3047 | s->cert->pms = pms; | |
3048 | s->cert->pmslen = pmslen; | |
3049 | } else { | |
3050 | /* If we don't have a PMS restore */ | |
3051 | if (pms == NULL) { | |
3052 | pms = s->cert->pms; | |
3053 | pmslen = s->cert->pmslen; | |
3054 | } | |
3055 | if (pms == NULL) { | |
3056 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3057 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
3058 | goto err; | |
3059 | } | |
3060 | s->session->master_key_length = | |
3061 | s->method->ssl3_enc->generate_master_secret(s, | |
3062 | s-> | |
3063 | session->master_key, | |
3064 | pms, pmslen); | |
4b45c6e5 | 3065 | OPENSSL_clear_free(pms, pmslen); |
c660ec63 | 3066 | s->cert->pms = NULL; |
61986d32 | 3067 | if (s->session->master_key_length < 0) { |
69f68237 MC |
3068 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
3069 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); | |
3070 | goto err; | |
3071 | } | |
c660ec63 DSH |
3072 | } |
3073 | return n; | |
3074 | memerr: | |
3075 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3076 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 3077 | err: |
4b45c6e5 RS |
3078 | OPENSSL_clear_free(pms, pmslen); |
3079 | s->cert->pms = NULL; | |
10bf4fc2 | 3080 | #ifndef OPENSSL_NO_EC |
0f113f3e | 3081 | BN_CTX_free(bn_ctx); |
b548a1f1 | 3082 | OPENSSL_free(encodedPoint); |
8fdc3734 | 3083 | EC_KEY_free(clnt_ecdh); |
0f113f3e MC |
3084 | EVP_PKEY_free(srvr_pub_pkey); |
3085 | #endif | |
3086 | return (-1); | |
3087 | } | |
d02b48c6 | 3088 | |
36d16f8e | 3089 | int ssl3_send_client_verify(SSL *s) |
0f113f3e MC |
3090 | { |
3091 | unsigned char *p; | |
3092 | unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; | |
3093 | EVP_PKEY *pkey; | |
3094 | EVP_PKEY_CTX *pctx = NULL; | |
3095 | EVP_MD_CTX mctx; | |
3096 | unsigned u = 0; | |
3097 | unsigned long n; | |
3098 | int j; | |
3099 | ||
3100 | EVP_MD_CTX_init(&mctx); | |
3101 | ||
3102 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) { | |
3103 | p = ssl_handshake_start(s); | |
3104 | pkey = s->cert->key->privatekey; | |
0e1dba93 | 3105 | /* Create context from key and test if sha1 is allowed as digest */ |
0f113f3e MC |
3106 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
3107 | EVP_PKEY_sign_init(pctx); | |
3108 | if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { | |
3109 | if (!SSL_USE_SIGALGS(s)) | |
3110 | s->method->ssl3_enc->cert_verify_mac(s, | |
3111 | NID_sha1, | |
3112 | &(data | |
3113 | [MD5_DIGEST_LENGTH])); | |
3114 | } else { | |
3115 | ERR_clear_error(); | |
3116 | } | |
3117 | /* | |
3118 | * For TLS v1.2 send signature algorithm and signature using agreed | |
3119 | * digest and cached handshake records. | |
3120 | */ | |
3121 | if (SSL_USE_SIGALGS(s)) { | |
3122 | long hdatalen = 0; | |
3123 | void *hdata; | |
3124 | const EVP_MD *md = s->cert->key->digest; | |
3125 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
3126 | if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { | |
3127 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3128 | goto err; | |
3129 | } | |
3130 | p += 2; | |
855a54a9 | 3131 | #ifdef SSL_DEBUG |
0f113f3e MC |
3132 | fprintf(stderr, "Using TLS 1.2 with client alg %s\n", |
3133 | EVP_MD_name(md)); | |
3134 | #endif | |
3135 | if (!EVP_SignInit_ex(&mctx, md, NULL) | |
3136 | || !EVP_SignUpdate(&mctx, hdata, hdatalen) | |
3137 | || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { | |
3138 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB); | |
3139 | goto err; | |
3140 | } | |
3141 | s2n(u, p); | |
3142 | n = u + 4; | |
0cfb0e75 DSH |
3143 | /* |
3144 | * For extended master secret we've already digested cached | |
3145 | * records. | |
3146 | */ | |
3147 | if (s->session->flags & SSL_SESS_FLAG_EXTMS) { | |
3148 | BIO_free(s->s3->handshake_buffer); | |
3149 | s->s3->handshake_buffer = NULL; | |
3150 | s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; | |
3151 | } else if (!ssl3_digest_cached_records(s)) | |
0f113f3e MC |
3152 | goto err; |
3153 | } else | |
bc36ee62 | 3154 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3155 | if (pkey->type == EVP_PKEY_RSA) { |
3156 | s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); | |
3157 | if (RSA_sign(NID_md5_sha1, data, | |
3158 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, | |
3159 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { | |
3160 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB); | |
3161 | goto err; | |
3162 | } | |
3163 | s2n(u, p); | |
3164 | n = u + 2; | |
3165 | } else | |
d02b48c6 | 3166 | #endif |
bc36ee62 | 3167 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
3168 | if (pkey->type == EVP_PKEY_DSA) { |
3169 | if (!DSA_sign(pkey->save_type, | |
3170 | &(data[MD5_DIGEST_LENGTH]), | |
3171 | SHA_DIGEST_LENGTH, &(p[2]), | |
3172 | (unsigned int *)&j, pkey->pkey.dsa)) { | |
3173 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB); | |
3174 | goto err; | |
3175 | } | |
3176 | s2n(j, p); | |
3177 | n = j + 2; | |
3178 | } else | |
ea262260 | 3179 | #endif |
10bf4fc2 | 3180 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
3181 | if (pkey->type == EVP_PKEY_EC) { |
3182 | if (!ECDSA_sign(pkey->save_type, | |
3183 | &(data[MD5_DIGEST_LENGTH]), | |
3184 | SHA_DIGEST_LENGTH, &(p[2]), | |
3185 | (unsigned int *)&j, pkey->pkey.ec)) { | |
3186 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB); | |
3187 | goto err; | |
3188 | } | |
3189 | s2n(j, p); | |
3190 | n = j + 2; | |
3191 | } else | |
3192 | #endif | |
3193 | if (pkey->type == NID_id_GostR3410_94 | |
3194 | || pkey->type == NID_id_GostR3410_2001) { | |
3195 | unsigned char signbuf[64]; | |
3196 | int i; | |
3197 | size_t sigsize = 64; | |
3198 | s->method->ssl3_enc->cert_verify_mac(s, | |
3199 | NID_id_GostR3411_94, data); | |
3200 | if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { | |
3201 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3202 | goto err; | |
3203 | } | |
3204 | for (i = 63, j = 0; i >= 0; j++, i--) { | |
3205 | p[2 + j] = signbuf[i]; | |
3206 | } | |
3207 | s2n(j, p); | |
3208 | n = j + 2; | |
3209 | } else { | |
3210 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); | |
3211 | goto err; | |
3212 | } | |
61986d32 | 3213 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) { |
77d514c5 MC |
3214 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
3215 | goto err; | |
3216 | } | |
0f113f3e MC |
3217 | s->state = SSL3_ST_CW_CERT_VRFY_B; |
3218 | } | |
3219 | EVP_MD_CTX_cleanup(&mctx); | |
3220 | EVP_PKEY_CTX_free(pctx); | |
3221 | return ssl_do_write(s); | |
3222 | err: | |
3223 | EVP_MD_CTX_cleanup(&mctx); | |
3224 | EVP_PKEY_CTX_free(pctx); | |
3225 | return (-1); | |
3226 | } | |
3227 | ||
3228 | /* | |
3229 | * Check a certificate can be used for client authentication. Currently check | |
3230 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client | |
3231 | * certificates can be used and optionally checks suitability for Suite B. | |
0d609395 DSH |
3232 | */ |
3233 | static int ssl3_check_client_certificate(SSL *s) | |
0f113f3e MC |
3234 | { |
3235 | unsigned long alg_k; | |
3236 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) | |
3237 | return 0; | |
3238 | /* If no suitable signature algorithm can't use certificate */ | |
3239 | if (SSL_USE_SIGALGS(s) && !s->cert->key->digest) | |
3240 | return 0; | |
3241 | /* | |
3242 | * If strict mode check suitability of chain before using it. This also | |
3243 | * adjusts suite B digest if necessary. | |
3244 | */ | |
3245 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && | |
3246 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) | |
3247 | return 0; | |
3248 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
3249 | /* See if we can use client certificate for fixed DH */ | |
3250 | if (alg_k & (SSL_kDHr | SSL_kDHd)) { | |
3251 | SESS_CERT *scert = s->session->sess_cert; | |
3252 | int i = scert->peer_cert_type; | |
3253 | EVP_PKEY *clkey = NULL, *spkey = NULL; | |
3254 | clkey = s->cert->key->privatekey; | |
3255 | /* If client key not DH assume it can be used */ | |
3256 | if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) | |
3257 | return 1; | |
3258 | if (i >= 0) | |
3259 | spkey = X509_get_pubkey(scert->peer_pkeys[i].x509); | |
3260 | if (spkey) { | |
3261 | /* Compare server and client parameters */ | |
3262 | i = EVP_PKEY_cmp_parameters(clkey, spkey); | |
3263 | EVP_PKEY_free(spkey); | |
3264 | if (i != 1) | |
3265 | return 0; | |
3266 | } | |
3267 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; | |
3268 | } | |
3269 | return 1; | |
3270 | } | |
0d609395 | 3271 | |
36d16f8e | 3272 | int ssl3_send_client_certificate(SSL *s) |
0f113f3e MC |
3273 | { |
3274 | X509 *x509 = NULL; | |
3275 | EVP_PKEY *pkey = NULL; | |
3276 | int i; | |
3277 | ||
3278 | if (s->state == SSL3_ST_CW_CERT_A) { | |
3279 | /* Let cert callback update client certificates if required */ | |
3280 | if (s->cert->cert_cb) { | |
3281 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); | |
3282 | if (i < 0) { | |
3283 | s->rwstate = SSL_X509_LOOKUP; | |
3284 | return -1; | |
3285 | } | |
3286 | if (i == 0) { | |
3287 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3288 | return 0; | |
3289 | } | |
3290 | s->rwstate = SSL_NOTHING; | |
3291 | } | |
3292 | if (ssl3_check_client_certificate(s)) | |
3293 | s->state = SSL3_ST_CW_CERT_C; | |
3294 | else | |
3295 | s->state = SSL3_ST_CW_CERT_B; | |
3296 | } | |
3297 | ||
3298 | /* We need to get a client cert */ | |
3299 | if (s->state == SSL3_ST_CW_CERT_B) { | |
3300 | /* | |
3301 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; | |
3302 | * return(-1); We then get retied later | |
3303 | */ | |
3304 | i = 0; | |
3305 | i = ssl_do_client_cert_cb(s, &x509, &pkey); | |
3306 | if (i < 0) { | |
3307 | s->rwstate = SSL_X509_LOOKUP; | |
3308 | return (-1); | |
3309 | } | |
3310 | s->rwstate = SSL_NOTHING; | |
3311 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { | |
3312 | s->state = SSL3_ST_CW_CERT_B; | |
3313 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) | |
3314 | i = 0; | |
3315 | } else if (i == 1) { | |
3316 | i = 0; | |
3317 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, | |
3318 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); | |
3319 | } | |
3320 | ||
222561fe | 3321 | X509_free(x509); |
0f113f3e MC |
3322 | if (pkey != NULL) |
3323 | EVP_PKEY_free(pkey); | |
3324 | if (i && !ssl3_check_client_certificate(s)) | |
3325 | i = 0; | |
3326 | if (i == 0) { | |
3327 | if (s->version == SSL3_VERSION) { | |
3328 | s->s3->tmp.cert_req = 0; | |
3329 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); | |
3330 | return (1); | |
3331 | } else { | |
3332 | s->s3->tmp.cert_req = 2; | |
3333 | } | |
3334 | } | |
3335 | ||
3336 | /* Ok, we have a cert */ | |
3337 | s->state = SSL3_ST_CW_CERT_C; | |
3338 | } | |
3339 | ||
3340 | if (s->state == SSL3_ST_CW_CERT_C) { | |
3341 | s->state = SSL3_ST_CW_CERT_D; | |
3342 | if (!ssl3_output_cert_chain(s, | |
3343 | (s->s3->tmp.cert_req == | |
3344 | 2) ? NULL : s->cert->key)) { | |
3345 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); | |
3346 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | |
3347 | return 0; | |
3348 | } | |
3349 | } | |
3350 | /* SSL3_ST_CW_CERT_D */ | |
3351 | return ssl_do_write(s); | |
3352 | } | |
3353 | ||
3354 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
d02b48c6 | 3355 | |
36d16f8e | 3356 | int ssl3_check_cert_and_algorithm(SSL *s) |
0f113f3e MC |
3357 | { |
3358 | int i, idx; | |
3359 | long alg_k, alg_a; | |
3360 | EVP_PKEY *pkey = NULL; | |
3361 | SESS_CERT *sc; | |
bc36ee62 | 3362 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 3363 | RSA *rsa; |
79df9d62 | 3364 | #endif |
bc36ee62 | 3365 | #ifndef OPENSSL_NO_DH |
0f113f3e | 3366 | DH *dh; |
79df9d62 | 3367 | #endif |
d02b48c6 | 3368 | |
0f113f3e MC |
3369 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
3370 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
d02b48c6 | 3371 | |
0f113f3e MC |
3372 | /* we don't have a certificate */ |
3373 | if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK)) | |
3374 | return (1); | |
c1ca9d32 | 3375 | |
0f113f3e MC |
3376 | sc = s->session->sess_cert; |
3377 | if (sc == NULL) { | |
3378 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); | |
3379 | goto err; | |
3380 | } | |
bc36ee62 | 3381 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 3382 | rsa = s->session->sess_cert->peer_rsa_tmp; |
79df9d62 | 3383 | #endif |
bc36ee62 | 3384 | #ifndef OPENSSL_NO_DH |
0f113f3e | 3385 | dh = s->session->sess_cert->peer_dh_tmp; |
79df9d62 | 3386 | #endif |
d02b48c6 | 3387 | |
0f113f3e | 3388 | /* This is the passed certificate */ |
d02b48c6 | 3389 | |
0f113f3e | 3390 | idx = sc->peer_cert_type; |
10bf4fc2 | 3391 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
3392 | if (idx == SSL_PKEY_ECC) { |
3393 | if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) { | |
3394 | /* check failed */ | |
3395 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); | |
3396 | goto f_err; | |
3397 | } else { | |
3398 | return 1; | |
3399 | } | |
3400 | } else if (alg_a & SSL_aECDSA) { | |
3401 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3402 | SSL_R_MISSING_ECDSA_SIGNING_CERT); | |
3403 | goto f_err; | |
3404 | } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) { | |
3405 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT); | |
3406 | goto f_err; | |
3407 | } | |
3408 | #endif | |
3409 | pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); | |
3410 | i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); | |
3411 | EVP_PKEY_free(pkey); | |
3412 | ||
3413 | /* Check that we have a certificate if we require one */ | |
3414 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { | |
3415 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3416 | SSL_R_MISSING_RSA_SIGNING_CERT); | |
3417 | goto f_err; | |
3418 | } | |
bc36ee62 | 3419 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
3420 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
3421 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3422 | SSL_R_MISSING_DSA_SIGNING_CERT); | |
3423 | goto f_err; | |
3424 | } | |
d02b48c6 | 3425 | #endif |
bc36ee62 | 3426 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3427 | if ((alg_k & SSL_kRSA) && |
3428 | !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) { | |
3429 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3430 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
3431 | goto f_err; | |
3432 | } | |
79df9d62 | 3433 | #endif |
bc36ee62 | 3434 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
3435 | if ((alg_k & SSL_kDHE) && |
3436 | !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) { | |
3437 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY); | |
3438 | goto f_err; | |
3439 | } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) && | |
3440 | !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) { | |
3441 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3442 | SSL_R_MISSING_DH_RSA_CERT); | |
3443 | goto f_err; | |
3444 | } | |
3445 | # ifndef OPENSSL_NO_DSA | |
3446 | else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) && | |
3447 | !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) { | |
3448 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3449 | SSL_R_MISSING_DH_DSA_CERT); | |
3450 | goto f_err; | |
3451 | } | |
3452 | # endif | |
d02b48c6 RE |
3453 | #endif |
3454 | ||
0f113f3e | 3455 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) { |
bc36ee62 | 3456 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3457 | if (alg_k & SSL_kRSA) { |
3458 | if (rsa == NULL | |
3459 | || RSA_size(rsa) * 8 > | |
3460 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { | |
3461 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3462 | SSL_R_MISSING_EXPORT_TMP_RSA_KEY); | |
3463 | goto f_err; | |
3464 | } | |
3465 | } else | |
d02b48c6 | 3466 | #endif |
bc36ee62 | 3467 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
3468 | if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { |
3469 | if (dh == NULL | |
3470 | || DH_size(dh) * 8 > | |
3471 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { | |
3472 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3473 | SSL_R_MISSING_EXPORT_TMP_DH_KEY); | |
3474 | goto f_err; | |
3475 | } | |
3476 | } else | |
3477 | #endif | |
3478 | { | |
3479 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, | |
3480 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
3481 | goto f_err; | |
3482 | } | |
3483 | } | |
3484 | return (1); | |
3485 | f_err: | |
3486 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
3487 | err: | |
3488 | return (0); | |
3489 | } | |
3490 | ||
6e3d0153 | 3491 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e | 3492 | /* |
6e3d0153 EK |
3493 | * Normally, we can tell if the server is resuming the session from |
3494 | * the session ID. EAP-FAST (RFC 4851), however, relies on the next server | |
3495 | * message after the ServerHello to determine if the server is resuming. | |
3496 | * Therefore, we allow EAP-FAST to peek ahead. | |
3497 | * ssl3_check_finished returns 1 if we are resuming from an external | |
3498 | * pre-shared secret, we have a "ticket" and the next server handshake message | |
3499 | * is Finished; and 0 otherwise. It returns -1 upon an error. | |
6434abbf | 3500 | */ |
6e3d0153 EK |
3501 | static int ssl3_check_finished(SSL *s) |
3502 | { | |
3503 | int ok = 0; | |
3504 | ||
3505 | if (s->version < TLS1_VERSION || !s->tls_session_secret_cb || | |
3506 | !s->session->tlsext_tick) | |
3507 | return 0; | |
3508 | ||
3509 | /* Need to permit this temporarily, in case the next message is Finished. */ | |
3510 | s->s3->flags |= SSL3_FLAGS_CCS_OK; | |
3511 | /* | |
3512 | * This function is called when we might get a Certificate message instead, | |
3513 | * so permit appropriate message length. | |
3514 | * We ignore the return value as we're only interested in the message type | |
3515 | * and not its length. | |
3516 | */ | |
3517 | s->method->ssl_get_message(s, | |
3518 | SSL3_ST_CR_CERT_A, | |
3519 | SSL3_ST_CR_CERT_B, | |
3520 | -1, s->max_cert_list, &ok); | |
3521 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; | |
3522 | ||
3523 | if (!ok) | |
3524 | return -1; | |
3525 | ||
3526 | s->s3->tmp.reuse_message = 1; | |
3527 | ||
3528 | if (s->s3->tmp.message_type == SSL3_MT_FINISHED) | |
3529 | return 1; | |
3530 | ||
3531 | /* If we're not done, then the CCS arrived early and we should bail. */ | |
3532 | if (s->s3->change_cipher_spec) { | |
3533 | SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY); | |
3534 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | |
3535 | return -1; | |
3536 | } | |
3537 | ||
3538 | return 0; | |
3539 | } | |
6434abbf | 3540 | |
bf48836c | 3541 | # ifndef OPENSSL_NO_NEXTPROTONEG |
ee2ffc27 | 3542 | int ssl3_send_next_proto(SSL *s) |
0f113f3e MC |
3543 | { |
3544 | unsigned int len, padding_len; | |
3545 | unsigned char *d; | |
3546 | ||
3547 | if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { | |
3548 | len = s->next_proto_negotiated_len; | |
3549 | padding_len = 32 - ((len + 2) % 32); | |
3550 | d = (unsigned char *)s->init_buf->data; | |
3551 | d[4] = len; | |
3552 | memcpy(d + 5, s->next_proto_negotiated, len); | |
3553 | d[5 + len] = padding_len; | |
3554 | memset(d + 6 + len, 0, padding_len); | |
3555 | *(d++) = SSL3_MT_NEXT_PROTO; | |
3556 | l2n3(2 + len + padding_len, d); | |
3557 | s->state = SSL3_ST_CW_NEXT_PROTO_B; | |
3558 | s->init_num = 4 + 2 + len + padding_len; | |
3559 | s->init_off = 0; | |
3560 | } | |
3561 | ||
3562 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE); | |
3563 | } | |
ee2ffc27 | 3564 | # endif |
6434abbf | 3565 | #endif |
368888bc DSH |
3566 | |
3567 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) | |
0f113f3e MC |
3568 | { |
3569 | int i = 0; | |
368888bc | 3570 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
3571 | if (s->ctx->client_cert_engine) { |
3572 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, | |
3573 | SSL_get_client_CA_list(s), | |
3574 | px509, ppkey, NULL, NULL, NULL); | |
3575 | if (i != 0) | |
3576 | return i; | |
3577 | } | |
3578 | #endif | |
3579 | if (s->ctx->client_cert_cb) | |
3580 | i = s->ctx->client_cert_cb(s, px509, ppkey); | |
3581 | return i; | |
3582 | } |