1ebfedea0SLionel Sambuc /* ssl/s3_both.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60ebfedea0SLionel Sambuc *
61ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc * are met:
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc * distribution.
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc * software must display the following acknowledgment:
75ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc *
78ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc * openssl-core@openssl.org.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc *
87ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc * acknowledgment:
89ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc *
92ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc * ====================================================================
105ebfedea0SLionel Sambuc *
106ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
108ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc */
111ebfedea0SLionel Sambuc /* ====================================================================
112ebfedea0SLionel Sambuc * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113ebfedea0SLionel Sambuc * ECC cipher suite support in OpenSSL originally developed by
114ebfedea0SLionel Sambuc * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115ebfedea0SLionel Sambuc */
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc #include <limits.h>
118ebfedea0SLionel Sambuc #include <string.h>
119ebfedea0SLionel Sambuc #include <stdio.h>
120ebfedea0SLionel Sambuc #include "ssl_locl.h"
121ebfedea0SLionel Sambuc #include <openssl/buffer.h>
122ebfedea0SLionel Sambuc #include <openssl/rand.h>
123ebfedea0SLionel Sambuc #include <openssl/objects.h>
124ebfedea0SLionel Sambuc #include <openssl/evp.h>
125ebfedea0SLionel Sambuc #include <openssl/x509.h>
126ebfedea0SLionel Sambuc
127*0a6a1f1dSLionel Sambuc /*
128*0a6a1f1dSLionel Sambuc * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
129*0a6a1f1dSLionel Sambuc * SSL3_RT_CHANGE_CIPHER_SPEC)
130*0a6a1f1dSLionel Sambuc */
ssl3_do_write(SSL * s,int type)131ebfedea0SLionel Sambuc int ssl3_do_write(SSL *s, int type)
132ebfedea0SLionel Sambuc {
133ebfedea0SLionel Sambuc int ret;
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
136ebfedea0SLionel Sambuc s->init_num);
137*0a6a1f1dSLionel Sambuc if (ret < 0)
138*0a6a1f1dSLionel Sambuc return (-1);
139ebfedea0SLionel Sambuc if (type == SSL3_RT_HANDSHAKE)
140*0a6a1f1dSLionel Sambuc /*
141*0a6a1f1dSLionel Sambuc * should not be done for 'Hello Request's, but in that case we'll
142*0a6a1f1dSLionel Sambuc * ignore the result anyway
143*0a6a1f1dSLionel Sambuc */
144*0a6a1f1dSLionel Sambuc ssl3_finish_mac(s, (unsigned char *)&s->init_buf->data[s->init_off],
145*0a6a1f1dSLionel Sambuc ret);
146ebfedea0SLionel Sambuc
147*0a6a1f1dSLionel Sambuc if (ret == s->init_num) {
148ebfedea0SLionel Sambuc if (s->msg_callback)
149*0a6a1f1dSLionel Sambuc s->msg_callback(1, s->version, type, s->init_buf->data,
150*0a6a1f1dSLionel Sambuc (size_t)(s->init_off + s->init_num), s,
151*0a6a1f1dSLionel Sambuc s->msg_callback_arg);
152ebfedea0SLionel Sambuc return (1);
153ebfedea0SLionel Sambuc }
154ebfedea0SLionel Sambuc s->init_off += ret;
155ebfedea0SLionel Sambuc s->init_num -= ret;
156ebfedea0SLionel Sambuc return (0);
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc
ssl3_send_finished(SSL * s,int a,int b,const char * sender,int slen)159ebfedea0SLionel Sambuc int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
160ebfedea0SLionel Sambuc {
161ebfedea0SLionel Sambuc unsigned char *p, *d;
162ebfedea0SLionel Sambuc int i;
163ebfedea0SLionel Sambuc unsigned long l;
164ebfedea0SLionel Sambuc
165*0a6a1f1dSLionel Sambuc if (s->state == a) {
166ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
167ebfedea0SLionel Sambuc p = &(d[4]);
168ebfedea0SLionel Sambuc
169ebfedea0SLionel Sambuc i = s->method->ssl3_enc->final_finish_mac(s,
170*0a6a1f1dSLionel Sambuc sender, slen,
171*0a6a1f1dSLionel Sambuc s->s3->tmp.finish_md);
172*0a6a1f1dSLionel Sambuc if (i <= 0)
173*0a6a1f1dSLionel Sambuc return 0;
174ebfedea0SLionel Sambuc s->s3->tmp.finish_md_len = i;
175ebfedea0SLionel Sambuc memcpy(p, s->s3->tmp.finish_md, i);
176ebfedea0SLionel Sambuc p += i;
177ebfedea0SLionel Sambuc l = i;
178ebfedea0SLionel Sambuc
179*0a6a1f1dSLionel Sambuc /*
180*0a6a1f1dSLionel Sambuc * Copy the finished so we can use it for renegotiation checks
181*0a6a1f1dSLionel Sambuc */
182*0a6a1f1dSLionel Sambuc if (s->type == SSL_ST_CONNECT) {
183ebfedea0SLionel Sambuc OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
184*0a6a1f1dSLionel Sambuc memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, i);
185ebfedea0SLionel Sambuc s->s3->previous_client_finished_len = i;
186*0a6a1f1dSLionel Sambuc } else {
187ebfedea0SLionel Sambuc OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
188*0a6a1f1dSLionel Sambuc memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, i);
189ebfedea0SLionel Sambuc s->s3->previous_server_finished_len = i;
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_WIN16
193*0a6a1f1dSLionel Sambuc /*
194*0a6a1f1dSLionel Sambuc * MSVC 1.5 does not clear the top bytes of the word unless I do
195*0a6a1f1dSLionel Sambuc * this.
196ebfedea0SLionel Sambuc */
197ebfedea0SLionel Sambuc l &= 0xffff;
198ebfedea0SLionel Sambuc #endif
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc *(d++) = SSL3_MT_FINISHED;
201ebfedea0SLionel Sambuc l2n3(l, d);
202ebfedea0SLionel Sambuc s->init_num = (int)l + 4;
203ebfedea0SLionel Sambuc s->init_off = 0;
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc s->state = b;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc /* SSL3_ST_SEND_xxxxxx_HELLO_B */
209ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
210ebfedea0SLionel Sambuc }
211ebfedea0SLionel Sambuc
212ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_NEXTPROTONEG
213*0a6a1f1dSLionel Sambuc /*
214*0a6a1f1dSLionel Sambuc * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
215*0a6a1f1dSLionel Sambuc * to far.
216*0a6a1f1dSLionel Sambuc */
ssl3_take_mac(SSL * s)217ebfedea0SLionel Sambuc static void ssl3_take_mac(SSL *s)
218ebfedea0SLionel Sambuc {
219ebfedea0SLionel Sambuc const char *sender;
220ebfedea0SLionel Sambuc int slen;
221*0a6a1f1dSLionel Sambuc /*
222*0a6a1f1dSLionel Sambuc * If no new cipher setup return immediately: other functions will set
223*0a6a1f1dSLionel Sambuc * the appropriate error.
224*0a6a1f1dSLionel Sambuc */
225*0a6a1f1dSLionel Sambuc if (s->s3->tmp.new_cipher == NULL)
226*0a6a1f1dSLionel Sambuc return;
227*0a6a1f1dSLionel Sambuc if (s->state & SSL_ST_CONNECT) {
228ebfedea0SLionel Sambuc sender = s->method->ssl3_enc->server_finished_label;
229ebfedea0SLionel Sambuc slen = s->method->ssl3_enc->server_finished_label_len;
230*0a6a1f1dSLionel Sambuc } else {
231ebfedea0SLionel Sambuc sender = s->method->ssl3_enc->client_finished_label;
232ebfedea0SLionel Sambuc slen = s->method->ssl3_enc->client_finished_label_len;
233ebfedea0SLionel Sambuc }
234ebfedea0SLionel Sambuc
235ebfedea0SLionel Sambuc s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
236*0a6a1f1dSLionel Sambuc sender,
237*0a6a1f1dSLionel Sambuc slen,
238*0a6a1f1dSLionel Sambuc s->s3->tmp.peer_finish_md);
239ebfedea0SLionel Sambuc }
240ebfedea0SLionel Sambuc #endif
241ebfedea0SLionel Sambuc
ssl3_get_finished(SSL * s,int a,int b)242ebfedea0SLionel Sambuc int ssl3_get_finished(SSL *s, int a, int b)
243ebfedea0SLionel Sambuc {
244ebfedea0SLionel Sambuc int al, i, ok;
245ebfedea0SLionel Sambuc long n;
246ebfedea0SLionel Sambuc unsigned char *p;
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_NEXTPROTONEG
249*0a6a1f1dSLionel Sambuc /*
250*0a6a1f1dSLionel Sambuc * the mac has already been generated when we received the change cipher
251*0a6a1f1dSLionel Sambuc * spec message and is in s->s3->tmp.peer_finish_md.
252ebfedea0SLionel Sambuc */
253ebfedea0SLionel Sambuc #endif
254ebfedea0SLionel Sambuc
255*0a6a1f1dSLionel Sambuc /* 64 argument should actually be 36+4 :-) */
256*0a6a1f1dSLionel Sambuc n = s->method->ssl_get_message(s, a, b, SSL3_MT_FINISHED, 64, &ok);
257ebfedea0SLionel Sambuc
258*0a6a1f1dSLionel Sambuc if (!ok)
259*0a6a1f1dSLionel Sambuc return ((int)n);
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc /* If this occurs, we have missed a message */
262*0a6a1f1dSLionel Sambuc if (!s->s3->change_cipher_spec) {
263ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
264ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
265ebfedea0SLionel Sambuc goto f_err;
266ebfedea0SLionel Sambuc }
267ebfedea0SLionel Sambuc s->s3->change_cipher_spec = 0;
268ebfedea0SLionel Sambuc
269ebfedea0SLionel Sambuc p = (unsigned char *)s->init_msg;
270ebfedea0SLionel Sambuc i = s->s3->tmp.peer_finish_md_len;
271ebfedea0SLionel Sambuc
272*0a6a1f1dSLionel Sambuc if (i != n) {
273ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
274ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
275ebfedea0SLionel Sambuc goto f_err;
276ebfedea0SLionel Sambuc }
277ebfedea0SLionel Sambuc
278*0a6a1f1dSLionel Sambuc if (CRYPTO_memcmp(p, s->s3->tmp.peer_finish_md, i) != 0) {
279ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
280ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
281ebfedea0SLionel Sambuc goto f_err;
282ebfedea0SLionel Sambuc }
283ebfedea0SLionel Sambuc
284*0a6a1f1dSLionel Sambuc /*
285*0a6a1f1dSLionel Sambuc * Copy the finished so we can use it for renegotiation checks
286*0a6a1f1dSLionel Sambuc */
287*0a6a1f1dSLionel Sambuc if (s->type == SSL_ST_ACCEPT) {
288ebfedea0SLionel Sambuc OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
289*0a6a1f1dSLionel Sambuc memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, i);
290ebfedea0SLionel Sambuc s->s3->previous_client_finished_len = i;
291*0a6a1f1dSLionel Sambuc } else {
292ebfedea0SLionel Sambuc OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
293*0a6a1f1dSLionel Sambuc memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, i);
294ebfedea0SLionel Sambuc s->s3->previous_server_finished_len = i;
295ebfedea0SLionel Sambuc }
296ebfedea0SLionel Sambuc
297ebfedea0SLionel Sambuc return (1);
298ebfedea0SLionel Sambuc f_err:
299ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
300ebfedea0SLionel Sambuc return (0);
301ebfedea0SLionel Sambuc }
302ebfedea0SLionel Sambuc
303*0a6a1f1dSLionel Sambuc /*-
304*0a6a1f1dSLionel Sambuc * for these 2 messages, we need to
305ebfedea0SLionel Sambuc * ssl->enc_read_ctx re-init
306ebfedea0SLionel Sambuc * ssl->s3->read_sequence zero
307ebfedea0SLionel Sambuc * ssl->s3->read_mac_secret re-init
308ebfedea0SLionel Sambuc * ssl->session->read_sym_enc assign
309ebfedea0SLionel Sambuc * ssl->session->read_compression assign
310ebfedea0SLionel Sambuc * ssl->session->read_hash assign
311ebfedea0SLionel Sambuc */
ssl3_send_change_cipher_spec(SSL * s,int a,int b)312ebfedea0SLionel Sambuc int ssl3_send_change_cipher_spec(SSL *s, int a, int b)
313ebfedea0SLionel Sambuc {
314ebfedea0SLionel Sambuc unsigned char *p;
315ebfedea0SLionel Sambuc
316*0a6a1f1dSLionel Sambuc if (s->state == a) {
317ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
318ebfedea0SLionel Sambuc *p = SSL3_MT_CCS;
319ebfedea0SLionel Sambuc s->init_num = 1;
320ebfedea0SLionel Sambuc s->init_off = 0;
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc s->state = b;
323ebfedea0SLionel Sambuc }
324ebfedea0SLionel Sambuc
325ebfedea0SLionel Sambuc /* SSL3_ST_CW_CHANGE_B */
326ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC));
327ebfedea0SLionel Sambuc }
328ebfedea0SLionel Sambuc
ssl3_add_cert_to_buf(BUF_MEM * buf,unsigned long * l,X509 * x)329ebfedea0SLionel Sambuc static int ssl3_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
330ebfedea0SLionel Sambuc {
331ebfedea0SLionel Sambuc int n;
332ebfedea0SLionel Sambuc unsigned char *p;
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc n = i2d_X509(x, NULL);
335*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
336ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
337ebfedea0SLionel Sambuc return (-1);
338ebfedea0SLionel Sambuc }
339ebfedea0SLionel Sambuc p = (unsigned char *)&(buf->data[*l]);
340ebfedea0SLionel Sambuc l2n3(n, p);
341ebfedea0SLionel Sambuc i2d_X509(x, &p);
342ebfedea0SLionel Sambuc *l += n + 3;
343ebfedea0SLionel Sambuc
344ebfedea0SLionel Sambuc return (0);
345ebfedea0SLionel Sambuc }
346ebfedea0SLionel Sambuc
ssl3_output_cert_chain(SSL * s,X509 * x)347ebfedea0SLionel Sambuc unsigned long ssl3_output_cert_chain(SSL *s, X509 *x)
348ebfedea0SLionel Sambuc {
349ebfedea0SLionel Sambuc unsigned char *p;
350ebfedea0SLionel Sambuc int i;
351ebfedea0SLionel Sambuc unsigned long l = 7;
352ebfedea0SLionel Sambuc BUF_MEM *buf;
353ebfedea0SLionel Sambuc int no_chain;
354ebfedea0SLionel Sambuc
355ebfedea0SLionel Sambuc if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs)
356ebfedea0SLionel Sambuc no_chain = 1;
357ebfedea0SLionel Sambuc else
358ebfedea0SLionel Sambuc no_chain = 0;
359ebfedea0SLionel Sambuc
360ebfedea0SLionel Sambuc /* TLSv1 sends a chain with nothing in it, instead of an alert */
361ebfedea0SLionel Sambuc buf = s->init_buf;
362*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow_clean(buf, 10)) {
363ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB);
364ebfedea0SLionel Sambuc return (0);
365ebfedea0SLionel Sambuc }
366*0a6a1f1dSLionel Sambuc if (x != NULL) {
367*0a6a1f1dSLionel Sambuc if (no_chain) {
368ebfedea0SLionel Sambuc if (ssl3_add_cert_to_buf(buf, &l, x))
369ebfedea0SLionel Sambuc return (0);
370*0a6a1f1dSLionel Sambuc } else {
371ebfedea0SLionel Sambuc X509_STORE_CTX xs_ctx;
372ebfedea0SLionel Sambuc
373*0a6a1f1dSLionel Sambuc if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store, x, NULL)) {
374ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB);
375ebfedea0SLionel Sambuc return (0);
376ebfedea0SLionel Sambuc }
377ebfedea0SLionel Sambuc X509_verify_cert(&xs_ctx);
378ebfedea0SLionel Sambuc /* Don't leave errors in the queue */
379ebfedea0SLionel Sambuc ERR_clear_error();
380*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
381ebfedea0SLionel Sambuc x = sk_X509_value(xs_ctx.chain, i);
382ebfedea0SLionel Sambuc
383*0a6a1f1dSLionel Sambuc if (ssl3_add_cert_to_buf(buf, &l, x)) {
384ebfedea0SLionel Sambuc X509_STORE_CTX_cleanup(&xs_ctx);
385ebfedea0SLionel Sambuc return 0;
386ebfedea0SLionel Sambuc }
387ebfedea0SLionel Sambuc }
388ebfedea0SLionel Sambuc X509_STORE_CTX_cleanup(&xs_ctx);
389ebfedea0SLionel Sambuc }
390ebfedea0SLionel Sambuc }
391ebfedea0SLionel Sambuc /* Thawte special :-) */
392*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) {
393ebfedea0SLionel Sambuc x = sk_X509_value(s->ctx->extra_certs, i);
394ebfedea0SLionel Sambuc if (ssl3_add_cert_to_buf(buf, &l, x))
395ebfedea0SLionel Sambuc return (0);
396ebfedea0SLionel Sambuc }
397ebfedea0SLionel Sambuc
398ebfedea0SLionel Sambuc l -= 7;
399ebfedea0SLionel Sambuc p = (unsigned char *)&(buf->data[4]);
400ebfedea0SLionel Sambuc l2n3(l, p);
401ebfedea0SLionel Sambuc l += 3;
402ebfedea0SLionel Sambuc p = (unsigned char *)&(buf->data[0]);
403ebfedea0SLionel Sambuc *(p++) = SSL3_MT_CERTIFICATE;
404ebfedea0SLionel Sambuc l2n3(l, p);
405ebfedea0SLionel Sambuc l += 4;
406ebfedea0SLionel Sambuc return (l);
407ebfedea0SLionel Sambuc }
408ebfedea0SLionel Sambuc
409*0a6a1f1dSLionel Sambuc /*
410*0a6a1f1dSLionel Sambuc * Obtain handshake message of message type 'mt' (any if mt == -1), maximum
411*0a6a1f1dSLionel Sambuc * acceptable body length 'max'. The first four bytes (msg_type and length)
412*0a6a1f1dSLionel Sambuc * are read in state 'st1', the body is read in state 'stn'.
413ebfedea0SLionel Sambuc */
ssl3_get_message(SSL * s,int st1,int stn,int mt,long max,int * ok)414ebfedea0SLionel Sambuc long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
415ebfedea0SLionel Sambuc {
416ebfedea0SLionel Sambuc unsigned char *p;
417ebfedea0SLionel Sambuc unsigned long l;
418ebfedea0SLionel Sambuc long n;
419ebfedea0SLionel Sambuc int i, al;
420ebfedea0SLionel Sambuc
421*0a6a1f1dSLionel Sambuc if (s->s3->tmp.reuse_message) {
422ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 0;
423*0a6a1f1dSLionel Sambuc if ((mt >= 0) && (s->s3->tmp.message_type != mt)) {
424ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
425ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
426ebfedea0SLionel Sambuc goto f_err;
427ebfedea0SLionel Sambuc }
428ebfedea0SLionel Sambuc *ok = 1;
429*0a6a1f1dSLionel Sambuc s->state = stn;
430ebfedea0SLionel Sambuc s->init_msg = s->init_buf->data + 4;
431ebfedea0SLionel Sambuc s->init_num = (int)s->s3->tmp.message_size;
432ebfedea0SLionel Sambuc return s->init_num;
433ebfedea0SLionel Sambuc }
434ebfedea0SLionel Sambuc
435ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
436ebfedea0SLionel Sambuc
437*0a6a1f1dSLionel Sambuc if (s->state == st1) { /* s->init_num < 4 */
438ebfedea0SLionel Sambuc int skip_message;
439ebfedea0SLionel Sambuc
440*0a6a1f1dSLionel Sambuc do {
441*0a6a1f1dSLionel Sambuc while (s->init_num < 4) {
442ebfedea0SLionel Sambuc i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE,
443*0a6a1f1dSLionel Sambuc &p[s->init_num],
444*0a6a1f1dSLionel Sambuc 4 - s->init_num, 0);
445*0a6a1f1dSLionel Sambuc if (i <= 0) {
446ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
447ebfedea0SLionel Sambuc *ok = 0;
448ebfedea0SLionel Sambuc return i;
449ebfedea0SLionel Sambuc }
450ebfedea0SLionel Sambuc s->init_num += i;
451ebfedea0SLionel Sambuc }
452ebfedea0SLionel Sambuc
453ebfedea0SLionel Sambuc skip_message = 0;
454ebfedea0SLionel Sambuc if (!s->server)
455ebfedea0SLionel Sambuc if (p[0] == SSL3_MT_HELLO_REQUEST)
456*0a6a1f1dSLionel Sambuc /*
457*0a6a1f1dSLionel Sambuc * The server may always send 'Hello Request' messages --
458*0a6a1f1dSLionel Sambuc * we are doing a handshake anyway now, so ignore them if
459*0a6a1f1dSLionel Sambuc * their format is correct. Does not count for 'Finished'
460*0a6a1f1dSLionel Sambuc * MAC.
461*0a6a1f1dSLionel Sambuc */
462*0a6a1f1dSLionel Sambuc if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
463ebfedea0SLionel Sambuc s->init_num = 0;
464ebfedea0SLionel Sambuc skip_message = 1;
465ebfedea0SLionel Sambuc
466ebfedea0SLionel Sambuc if (s->msg_callback)
467*0a6a1f1dSLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
468*0a6a1f1dSLionel Sambuc p, 4, s, s->msg_callback_arg);
469ebfedea0SLionel Sambuc }
470ebfedea0SLionel Sambuc }
471ebfedea0SLionel Sambuc while (skip_message);
472ebfedea0SLionel Sambuc
473ebfedea0SLionel Sambuc /* s->init_num == 4 */
474ebfedea0SLionel Sambuc
475*0a6a1f1dSLionel Sambuc if ((mt >= 0) && (*p != mt)) {
476ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
477ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
478ebfedea0SLionel Sambuc goto f_err;
479ebfedea0SLionel Sambuc }
480ebfedea0SLionel Sambuc if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&
481*0a6a1f1dSLionel Sambuc (st1 == SSL3_ST_SR_CERT_A) && (stn == SSL3_ST_SR_CERT_B)) {
482*0a6a1f1dSLionel Sambuc /*
483*0a6a1f1dSLionel Sambuc * At this point we have got an MS SGC second client hello (maybe
484*0a6a1f1dSLionel Sambuc * we should always allow the client to start a new handshake?).
485*0a6a1f1dSLionel Sambuc * We need to restart the mac. Don't increment
486*0a6a1f1dSLionel Sambuc * {num,total}_renegotiations because we have not completed the
487*0a6a1f1dSLionel Sambuc * handshake.
488*0a6a1f1dSLionel Sambuc */
489ebfedea0SLionel Sambuc ssl3_init_finished_mac(s);
490ebfedea0SLionel Sambuc }
491ebfedea0SLionel Sambuc
492ebfedea0SLionel Sambuc s->s3->tmp.message_type = *(p++);
493ebfedea0SLionel Sambuc
494ebfedea0SLionel Sambuc n2l3(p, l);
495*0a6a1f1dSLionel Sambuc if (l > (unsigned long)max) {
496ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
497ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
498ebfedea0SLionel Sambuc goto f_err;
499ebfedea0SLionel Sambuc }
500*0a6a1f1dSLionel Sambuc if (l > (INT_MAX - 4)) { /* BUF_MEM_grow takes an 'int' parameter */
501ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
502ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
503ebfedea0SLionel Sambuc goto f_err;
504ebfedea0SLionel Sambuc }
505*0a6a1f1dSLionel Sambuc if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l + 4)) {
506ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB);
507ebfedea0SLionel Sambuc goto err;
508ebfedea0SLionel Sambuc }
509ebfedea0SLionel Sambuc s->s3->tmp.message_size = l;
510ebfedea0SLionel Sambuc s->state = stn;
511ebfedea0SLionel Sambuc
512ebfedea0SLionel Sambuc s->init_msg = s->init_buf->data + 4;
513ebfedea0SLionel Sambuc s->init_num = 0;
514ebfedea0SLionel Sambuc }
515ebfedea0SLionel Sambuc
516ebfedea0SLionel Sambuc /* next state (stn) */
517ebfedea0SLionel Sambuc p = s->init_msg;
518ebfedea0SLionel Sambuc n = s->s3->tmp.message_size - s->init_num;
519*0a6a1f1dSLionel Sambuc while (n > 0) {
520*0a6a1f1dSLionel Sambuc i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &p[s->init_num],
521*0a6a1f1dSLionel Sambuc n, 0);
522*0a6a1f1dSLionel Sambuc if (i <= 0) {
523ebfedea0SLionel Sambuc s->rwstate = SSL_READING;
524ebfedea0SLionel Sambuc *ok = 0;
525ebfedea0SLionel Sambuc return i;
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc s->init_num += i;
528ebfedea0SLionel Sambuc n -= i;
529ebfedea0SLionel Sambuc }
530ebfedea0SLionel Sambuc
531ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_NEXTPROTONEG
532*0a6a1f1dSLionel Sambuc /*
533*0a6a1f1dSLionel Sambuc * If receiving Finished, record MAC of prior handshake messages for
534*0a6a1f1dSLionel Sambuc * Finished verification.
535*0a6a1f1dSLionel Sambuc */
536ebfedea0SLionel Sambuc if (*s->init_buf->data == SSL3_MT_FINISHED)
537ebfedea0SLionel Sambuc ssl3_take_mac(s);
538ebfedea0SLionel Sambuc #endif
539ebfedea0SLionel Sambuc
540ebfedea0SLionel Sambuc /* Feed this message into MAC computation. */
541ebfedea0SLionel Sambuc ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4);
542ebfedea0SLionel Sambuc if (s->msg_callback)
543*0a6a1f1dSLionel Sambuc s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
544*0a6a1f1dSLionel Sambuc (size_t)s->init_num + 4, s, s->msg_callback_arg);
545ebfedea0SLionel Sambuc *ok = 1;
546ebfedea0SLionel Sambuc return s->init_num;
547ebfedea0SLionel Sambuc f_err:
548ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
549ebfedea0SLionel Sambuc err:
550ebfedea0SLionel Sambuc *ok = 0;
551ebfedea0SLionel Sambuc return (-1);
552ebfedea0SLionel Sambuc }
553ebfedea0SLionel Sambuc
ssl_cert_type(X509 * x,EVP_PKEY * pkey)554ebfedea0SLionel Sambuc int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
555ebfedea0SLionel Sambuc {
556ebfedea0SLionel Sambuc EVP_PKEY *pk;
557ebfedea0SLionel Sambuc int ret = -1, i;
558ebfedea0SLionel Sambuc
559ebfedea0SLionel Sambuc if (pkey == NULL)
560ebfedea0SLionel Sambuc pk = X509_get_pubkey(x);
561ebfedea0SLionel Sambuc else
562ebfedea0SLionel Sambuc pk = pkey;
563*0a6a1f1dSLionel Sambuc if (pk == NULL)
564*0a6a1f1dSLionel Sambuc goto err;
565ebfedea0SLionel Sambuc
566ebfedea0SLionel Sambuc i = pk->type;
567*0a6a1f1dSLionel Sambuc if (i == EVP_PKEY_RSA) {
568ebfedea0SLionel Sambuc ret = SSL_PKEY_RSA_ENC;
569*0a6a1f1dSLionel Sambuc } else if (i == EVP_PKEY_DSA) {
570ebfedea0SLionel Sambuc ret = SSL_PKEY_DSA_SIGN;
571ebfedea0SLionel Sambuc }
572ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC
573*0a6a1f1dSLionel Sambuc else if (i == EVP_PKEY_EC) {
574ebfedea0SLionel Sambuc ret = SSL_PKEY_ECC;
575ebfedea0SLionel Sambuc }
576ebfedea0SLionel Sambuc #endif
577*0a6a1f1dSLionel Sambuc else if (i == NID_id_GostR3410_94 || i == NID_id_GostR3410_94_cc) {
578ebfedea0SLionel Sambuc ret = SSL_PKEY_GOST94;
579*0a6a1f1dSLionel Sambuc } else if (i == NID_id_GostR3410_2001 || i == NID_id_GostR3410_2001_cc) {
580ebfedea0SLionel Sambuc ret = SSL_PKEY_GOST01;
581ebfedea0SLionel Sambuc }
582ebfedea0SLionel Sambuc err:
583*0a6a1f1dSLionel Sambuc if (!pkey)
584*0a6a1f1dSLionel Sambuc EVP_PKEY_free(pk);
585ebfedea0SLionel Sambuc return (ret);
586ebfedea0SLionel Sambuc }
587ebfedea0SLionel Sambuc
ssl_verify_alarm_type(long type)588ebfedea0SLionel Sambuc int ssl_verify_alarm_type(long type)
589ebfedea0SLionel Sambuc {
590ebfedea0SLionel Sambuc int al;
591ebfedea0SLionel Sambuc
592*0a6a1f1dSLionel Sambuc switch (type) {
593ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
594ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_GET_CRL:
595ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
596ebfedea0SLionel Sambuc al = SSL_AD_UNKNOWN_CA;
597ebfedea0SLionel Sambuc break;
598ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
599ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
600ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
601ebfedea0SLionel Sambuc case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
602ebfedea0SLionel Sambuc case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
603ebfedea0SLionel Sambuc case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
604ebfedea0SLionel Sambuc case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
605ebfedea0SLionel Sambuc case X509_V_ERR_CERT_NOT_YET_VALID:
606ebfedea0SLionel Sambuc case X509_V_ERR_CRL_NOT_YET_VALID:
607ebfedea0SLionel Sambuc case X509_V_ERR_CERT_UNTRUSTED:
608ebfedea0SLionel Sambuc case X509_V_ERR_CERT_REJECTED:
609ebfedea0SLionel Sambuc al = SSL_AD_BAD_CERTIFICATE;
610ebfedea0SLionel Sambuc break;
611ebfedea0SLionel Sambuc case X509_V_ERR_CERT_SIGNATURE_FAILURE:
612ebfedea0SLionel Sambuc case X509_V_ERR_CRL_SIGNATURE_FAILURE:
613ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
614ebfedea0SLionel Sambuc break;
615ebfedea0SLionel Sambuc case X509_V_ERR_CERT_HAS_EXPIRED:
616ebfedea0SLionel Sambuc case X509_V_ERR_CRL_HAS_EXPIRED:
617ebfedea0SLionel Sambuc al = SSL_AD_CERTIFICATE_EXPIRED;
618ebfedea0SLionel Sambuc break;
619ebfedea0SLionel Sambuc case X509_V_ERR_CERT_REVOKED:
620ebfedea0SLionel Sambuc al = SSL_AD_CERTIFICATE_REVOKED;
621ebfedea0SLionel Sambuc break;
622ebfedea0SLionel Sambuc case X509_V_ERR_OUT_OF_MEM:
623ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
624ebfedea0SLionel Sambuc break;
625ebfedea0SLionel Sambuc case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
626ebfedea0SLionel Sambuc case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
627ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
628ebfedea0SLionel Sambuc case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
629ebfedea0SLionel Sambuc case X509_V_ERR_CERT_CHAIN_TOO_LONG:
630ebfedea0SLionel Sambuc case X509_V_ERR_PATH_LENGTH_EXCEEDED:
631ebfedea0SLionel Sambuc case X509_V_ERR_INVALID_CA:
632ebfedea0SLionel Sambuc al = SSL_AD_UNKNOWN_CA;
633ebfedea0SLionel Sambuc break;
634ebfedea0SLionel Sambuc case X509_V_ERR_APPLICATION_VERIFICATION:
635ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
636ebfedea0SLionel Sambuc break;
637ebfedea0SLionel Sambuc case X509_V_ERR_INVALID_PURPOSE:
638ebfedea0SLionel Sambuc al = SSL_AD_UNSUPPORTED_CERTIFICATE;
639ebfedea0SLionel Sambuc break;
640ebfedea0SLionel Sambuc default:
641ebfedea0SLionel Sambuc al = SSL_AD_CERTIFICATE_UNKNOWN;
642ebfedea0SLionel Sambuc break;
643ebfedea0SLionel Sambuc }
644ebfedea0SLionel Sambuc return (al);
645ebfedea0SLionel Sambuc }
646ebfedea0SLionel Sambuc
647ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_BUF_FREELISTS
648*0a6a1f1dSLionel Sambuc /*-
649*0a6a1f1dSLionel Sambuc * On some platforms, malloc() performance is bad enough that you can't just
650ebfedea0SLionel Sambuc * free() and malloc() buffers all the time, so we need to use freelists from
651ebfedea0SLionel Sambuc * unused buffers. Currently, each freelist holds memory chunks of only a
652ebfedea0SLionel Sambuc * given size (list->chunklen); other sized chunks are freed and malloced.
653ebfedea0SLionel Sambuc * This doesn't help much if you're using many different SSL option settings
654ebfedea0SLionel Sambuc * with a given context. (The options affecting buffer size are
655ebfedea0SLionel Sambuc * max_send_fragment, read buffer vs write buffer,
656ebfedea0SLionel Sambuc * SSL_OP_MICROSOFT_BIG_WRITE_BUFFER, SSL_OP_NO_COMPRESSION, and
657ebfedea0SLionel Sambuc * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS.) Using a separate freelist for every
658ebfedea0SLionel Sambuc * possible size is not an option, since max_send_fragment can take on many
659ebfedea0SLionel Sambuc * different values.
660ebfedea0SLionel Sambuc *
661ebfedea0SLionel Sambuc * If you are on a platform with a slow malloc(), and you're using SSL
662ebfedea0SLionel Sambuc * connections with many different settings for these options, and you need to
663ebfedea0SLionel Sambuc * use the SSL_MOD_RELEASE_BUFFERS feature, you have a few options:
664ebfedea0SLionel Sambuc * - Link against a faster malloc implementation.
665ebfedea0SLionel Sambuc * - Use a separate SSL_CTX for each option set.
666ebfedea0SLionel Sambuc * - Improve this code.
667ebfedea0SLionel Sambuc */
freelist_extract(SSL_CTX * ctx,int for_read,int sz)668*0a6a1f1dSLionel Sambuc static void *freelist_extract(SSL_CTX *ctx, int for_read, int sz)
669ebfedea0SLionel Sambuc {
670ebfedea0SLionel Sambuc SSL3_BUF_FREELIST *list;
671ebfedea0SLionel Sambuc SSL3_BUF_FREELIST_ENTRY *ent = NULL;
672ebfedea0SLionel Sambuc void *result = NULL;
673ebfedea0SLionel Sambuc
674ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
675ebfedea0SLionel Sambuc list = for_read ? ctx->rbuf_freelist : ctx->wbuf_freelist;
676ebfedea0SLionel Sambuc if (list != NULL && sz == (int)list->chunklen)
677ebfedea0SLionel Sambuc ent = list->head;
678*0a6a1f1dSLionel Sambuc if (ent != NULL) {
679ebfedea0SLionel Sambuc list->head = ent->next;
680ebfedea0SLionel Sambuc result = ent;
681ebfedea0SLionel Sambuc if (--list->len == 0)
682ebfedea0SLionel Sambuc list->chunklen = 0;
683ebfedea0SLionel Sambuc }
684ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
685ebfedea0SLionel Sambuc if (!result)
686ebfedea0SLionel Sambuc result = OPENSSL_malloc(sz);
687ebfedea0SLionel Sambuc return result;
688ebfedea0SLionel Sambuc }
689ebfedea0SLionel Sambuc
freelist_insert(SSL_CTX * ctx,int for_read,size_t sz,void * mem)690*0a6a1f1dSLionel Sambuc static void freelist_insert(SSL_CTX *ctx, int for_read, size_t sz, void *mem)
691ebfedea0SLionel Sambuc {
692ebfedea0SLionel Sambuc SSL3_BUF_FREELIST *list;
693ebfedea0SLionel Sambuc SSL3_BUF_FREELIST_ENTRY *ent;
694ebfedea0SLionel Sambuc
695ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
696ebfedea0SLionel Sambuc list = for_read ? ctx->rbuf_freelist : ctx->wbuf_freelist;
697ebfedea0SLionel Sambuc if (list != NULL &&
698ebfedea0SLionel Sambuc (sz == list->chunklen || list->chunklen == 0) &&
699*0a6a1f1dSLionel Sambuc list->len < ctx->freelist_max_len && sz >= sizeof(*ent)) {
700ebfedea0SLionel Sambuc list->chunklen = sz;
701ebfedea0SLionel Sambuc ent = mem;
702ebfedea0SLionel Sambuc ent->next = list->head;
703ebfedea0SLionel Sambuc list->head = ent;
704ebfedea0SLionel Sambuc ++list->len;
705ebfedea0SLionel Sambuc mem = NULL;
706ebfedea0SLionel Sambuc }
707ebfedea0SLionel Sambuc
708ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
709ebfedea0SLionel Sambuc if (mem)
710ebfedea0SLionel Sambuc OPENSSL_free(mem);
711ebfedea0SLionel Sambuc }
712ebfedea0SLionel Sambuc #else
713ebfedea0SLionel Sambuc # define freelist_extract(c,fr,sz) OPENSSL_malloc(sz)
714ebfedea0SLionel Sambuc # define freelist_insert(c,fr,sz,m) OPENSSL_free(m)
715ebfedea0SLionel Sambuc #endif
716ebfedea0SLionel Sambuc
ssl3_setup_read_buffer(SSL * s)717ebfedea0SLionel Sambuc int ssl3_setup_read_buffer(SSL *s)
718ebfedea0SLionel Sambuc {
719ebfedea0SLionel Sambuc unsigned char *p;
720ebfedea0SLionel Sambuc size_t len, align = 0, headerlen;
721ebfedea0SLionel Sambuc
722ebfedea0SLionel Sambuc if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
723ebfedea0SLionel Sambuc headerlen = DTLS1_RT_HEADER_LENGTH;
724ebfedea0SLionel Sambuc else
725ebfedea0SLionel Sambuc headerlen = SSL3_RT_HEADER_LENGTH;
726ebfedea0SLionel Sambuc
727ebfedea0SLionel Sambuc #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
728ebfedea0SLionel Sambuc align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
729ebfedea0SLionel Sambuc #endif
730ebfedea0SLionel Sambuc
731*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.buf == NULL) {
732ebfedea0SLionel Sambuc len = SSL3_RT_MAX_PLAIN_LENGTH
733*0a6a1f1dSLionel Sambuc + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
734*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) {
735ebfedea0SLionel Sambuc s->s3->init_extra = 1;
736ebfedea0SLionel Sambuc len += SSL3_RT_MAX_EXTRA;
737ebfedea0SLionel Sambuc }
738ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
739ebfedea0SLionel Sambuc if (!(s->options & SSL_OP_NO_COMPRESSION))
740ebfedea0SLionel Sambuc len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
741ebfedea0SLionel Sambuc #endif
742ebfedea0SLionel Sambuc if ((p = freelist_extract(s->ctx, 1, len)) == NULL)
743ebfedea0SLionel Sambuc goto err;
744ebfedea0SLionel Sambuc s->s3->rbuf.buf = p;
745ebfedea0SLionel Sambuc s->s3->rbuf.len = len;
746ebfedea0SLionel Sambuc }
747ebfedea0SLionel Sambuc
748ebfedea0SLionel Sambuc s->packet = &(s->s3->rbuf.buf[0]);
749ebfedea0SLionel Sambuc return 1;
750ebfedea0SLionel Sambuc
751ebfedea0SLionel Sambuc err:
752ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE);
753ebfedea0SLionel Sambuc return 0;
754ebfedea0SLionel Sambuc }
755ebfedea0SLionel Sambuc
ssl3_setup_write_buffer(SSL * s)756ebfedea0SLionel Sambuc int ssl3_setup_write_buffer(SSL *s)
757ebfedea0SLionel Sambuc {
758ebfedea0SLionel Sambuc unsigned char *p;
759ebfedea0SLionel Sambuc size_t len, align = 0, headerlen;
760ebfedea0SLionel Sambuc
761ebfedea0SLionel Sambuc if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
762ebfedea0SLionel Sambuc headerlen = DTLS1_RT_HEADER_LENGTH + 1;
763ebfedea0SLionel Sambuc else
764ebfedea0SLionel Sambuc headerlen = SSL3_RT_HEADER_LENGTH;
765ebfedea0SLionel Sambuc
766ebfedea0SLionel Sambuc #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
767ebfedea0SLionel Sambuc align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
768ebfedea0SLionel Sambuc #endif
769ebfedea0SLionel Sambuc
770*0a6a1f1dSLionel Sambuc if (s->s3->wbuf.buf == NULL) {
771ebfedea0SLionel Sambuc len = s->max_send_fragment
772*0a6a1f1dSLionel Sambuc + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
773ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
774ebfedea0SLionel Sambuc if (!(s->options & SSL_OP_NO_COMPRESSION))
775ebfedea0SLionel Sambuc len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
776ebfedea0SLionel Sambuc #endif
777ebfedea0SLionel Sambuc if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
778*0a6a1f1dSLionel Sambuc len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
779ebfedea0SLionel Sambuc
780ebfedea0SLionel Sambuc if ((p = freelist_extract(s->ctx, 0, len)) == NULL)
781ebfedea0SLionel Sambuc goto err;
782ebfedea0SLionel Sambuc s->s3->wbuf.buf = p;
783ebfedea0SLionel Sambuc s->s3->wbuf.len = len;
784ebfedea0SLionel Sambuc }
785ebfedea0SLionel Sambuc
786ebfedea0SLionel Sambuc return 1;
787ebfedea0SLionel Sambuc
788ebfedea0SLionel Sambuc err:
789ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
790ebfedea0SLionel Sambuc return 0;
791ebfedea0SLionel Sambuc }
792ebfedea0SLionel Sambuc
ssl3_setup_buffers(SSL * s)793ebfedea0SLionel Sambuc int ssl3_setup_buffers(SSL *s)
794ebfedea0SLionel Sambuc {
795ebfedea0SLionel Sambuc if (!ssl3_setup_read_buffer(s))
796ebfedea0SLionel Sambuc return 0;
797ebfedea0SLionel Sambuc if (!ssl3_setup_write_buffer(s))
798ebfedea0SLionel Sambuc return 0;
799ebfedea0SLionel Sambuc return 1;
800ebfedea0SLionel Sambuc }
801ebfedea0SLionel Sambuc
ssl3_release_write_buffer(SSL * s)802ebfedea0SLionel Sambuc int ssl3_release_write_buffer(SSL *s)
803ebfedea0SLionel Sambuc {
804*0a6a1f1dSLionel Sambuc if (s->s3->wbuf.buf != NULL) {
805ebfedea0SLionel Sambuc freelist_insert(s->ctx, 0, s->s3->wbuf.len, s->s3->wbuf.buf);
806ebfedea0SLionel Sambuc s->s3->wbuf.buf = NULL;
807ebfedea0SLionel Sambuc }
808ebfedea0SLionel Sambuc return 1;
809ebfedea0SLionel Sambuc }
810ebfedea0SLionel Sambuc
ssl3_release_read_buffer(SSL * s)811ebfedea0SLionel Sambuc int ssl3_release_read_buffer(SSL *s)
812ebfedea0SLionel Sambuc {
813*0a6a1f1dSLionel Sambuc if (s->s3->rbuf.buf != NULL) {
814ebfedea0SLionel Sambuc freelist_insert(s->ctx, 1, s->s3->rbuf.len, s->s3->rbuf.buf);
815ebfedea0SLionel Sambuc s->s3->rbuf.buf = NULL;
816ebfedea0SLionel Sambuc }
817ebfedea0SLionel Sambuc return 1;
818ebfedea0SLionel Sambuc }
819