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