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