sl@0
|
1 |
/* ssl/s3_both.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
/* ====================================================================
|
sl@0
|
59 |
* Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
|
sl@0
|
60 |
*
|
sl@0
|
61 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
62 |
* modification, are permitted provided that the following conditions
|
sl@0
|
63 |
* are met:
|
sl@0
|
64 |
*
|
sl@0
|
65 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
66 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
67 |
*
|
sl@0
|
68 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
69 |
* notice, this list of conditions and the following disclaimer in
|
sl@0
|
70 |
* the documentation and/or other materials provided with the
|
sl@0
|
71 |
* distribution.
|
sl@0
|
72 |
*
|
sl@0
|
73 |
* 3. All advertising materials mentioning features or use of this
|
sl@0
|
74 |
* software must display the following acknowledgment:
|
sl@0
|
75 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
76 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
sl@0
|
77 |
*
|
sl@0
|
78 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
sl@0
|
79 |
* endorse or promote products derived from this software without
|
sl@0
|
80 |
* prior written permission. For written permission, please contact
|
sl@0
|
81 |
* openssl-core@openssl.org.
|
sl@0
|
82 |
*
|
sl@0
|
83 |
* 5. Products derived from this software may not be called "OpenSSL"
|
sl@0
|
84 |
* nor may "OpenSSL" appear in their names without prior written
|
sl@0
|
85 |
* permission of the OpenSSL Project.
|
sl@0
|
86 |
*
|
sl@0
|
87 |
* 6. Redistributions of any form whatsoever must retain the following
|
sl@0
|
88 |
* acknowledgment:
|
sl@0
|
89 |
* "This product includes software developed by the OpenSSL Project
|
sl@0
|
90 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
sl@0
|
91 |
*
|
sl@0
|
92 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
sl@0
|
93 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
94 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
sl@0
|
95 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
sl@0
|
96 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
sl@0
|
97 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
sl@0
|
98 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
sl@0
|
99 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
100 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
101 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
sl@0
|
102 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
sl@0
|
103 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
104 |
* ====================================================================
|
sl@0
|
105 |
*
|
sl@0
|
106 |
* This product includes cryptographic software written by Eric Young
|
sl@0
|
107 |
* (eay@cryptsoft.com). This product includes software written by Tim
|
sl@0
|
108 |
* Hudson (tjh@cryptsoft.com).
|
sl@0
|
109 |
*
|
sl@0
|
110 |
*/
|
sl@0
|
111 |
/* ====================================================================
|
sl@0
|
112 |
* Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
|
sl@0
|
113 |
* ECC cipher suite support in OpenSSL originally developed by
|
sl@0
|
114 |
* SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
|
sl@0
|
115 |
*/
|
sl@0
|
116 |
|
sl@0
|
117 |
#include <limits.h>
|
sl@0
|
118 |
#include <string.h>
|
sl@0
|
119 |
#include <stdio.h>
|
sl@0
|
120 |
#include "ssl_locl.h"
|
sl@0
|
121 |
#include <openssl/buffer.h>
|
sl@0
|
122 |
#include <openssl/rand.h>
|
sl@0
|
123 |
#include <openssl/objects.h>
|
sl@0
|
124 |
#include <openssl/evp.h>
|
sl@0
|
125 |
#include <openssl/x509.h>
|
sl@0
|
126 |
|
sl@0
|
127 |
/* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
|
sl@0
|
128 |
int ssl3_do_write(SSL *s, int type)
|
sl@0
|
129 |
{
|
sl@0
|
130 |
int ret;
|
sl@0
|
131 |
|
sl@0
|
132 |
ret=ssl3_write_bytes(s,type,&s->init_buf->data[s->init_off],
|
sl@0
|
133 |
s->init_num);
|
sl@0
|
134 |
if (ret < 0) return(-1);
|
sl@0
|
135 |
if (type == SSL3_RT_HANDSHAKE)
|
sl@0
|
136 |
/* should not be done for 'Hello Request's, but in that case
|
sl@0
|
137 |
* we'll ignore the result anyway */
|
sl@0
|
138 |
ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off],ret);
|
sl@0
|
139 |
|
sl@0
|
140 |
if (ret == s->init_num)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
if (s->msg_callback)
|
sl@0
|
143 |
s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg);
|
sl@0
|
144 |
return(1);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
s->init_off+=ret;
|
sl@0
|
147 |
s->init_num-=ret;
|
sl@0
|
148 |
return(0);
|
sl@0
|
149 |
}
|
sl@0
|
150 |
|
sl@0
|
151 |
int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen)
|
sl@0
|
152 |
{
|
sl@0
|
153 |
unsigned char *p,*d;
|
sl@0
|
154 |
int i;
|
sl@0
|
155 |
unsigned long l;
|
sl@0
|
156 |
|
sl@0
|
157 |
if (s->state == a)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
d=(unsigned char *)s->init_buf->data;
|
sl@0
|
160 |
p= &(d[4]);
|
sl@0
|
161 |
|
sl@0
|
162 |
i=s->method->ssl3_enc->final_finish_mac(s,
|
sl@0
|
163 |
&(s->s3->finish_dgst1),
|
sl@0
|
164 |
&(s->s3->finish_dgst2),
|
sl@0
|
165 |
sender,slen,s->s3->tmp.finish_md);
|
sl@0
|
166 |
s->s3->tmp.finish_md_len = i;
|
sl@0
|
167 |
memcpy(p, s->s3->tmp.finish_md, i);
|
sl@0
|
168 |
p+=i;
|
sl@0
|
169 |
l=i;
|
sl@0
|
170 |
|
sl@0
|
171 |
#ifdef OPENSSL_SYS_WIN16
|
sl@0
|
172 |
/* MSVC 1.5 does not clear the top bytes of the word unless
|
sl@0
|
173 |
* I do this.
|
sl@0
|
174 |
*/
|
sl@0
|
175 |
l&=0xffff;
|
sl@0
|
176 |
#endif
|
sl@0
|
177 |
|
sl@0
|
178 |
*(d++)=SSL3_MT_FINISHED;
|
sl@0
|
179 |
l2n3(l,d);
|
sl@0
|
180 |
s->init_num=(int)l+4;
|
sl@0
|
181 |
s->init_off=0;
|
sl@0
|
182 |
|
sl@0
|
183 |
s->state=b;
|
sl@0
|
184 |
}
|
sl@0
|
185 |
|
sl@0
|
186 |
/* SSL3_ST_SEND_xxxxxx_HELLO_B */
|
sl@0
|
187 |
return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
|
sl@0
|
188 |
}
|
sl@0
|
189 |
|
sl@0
|
190 |
int ssl3_get_finished(SSL *s, int a, int b)
|
sl@0
|
191 |
{
|
sl@0
|
192 |
int al,i,ok;
|
sl@0
|
193 |
long n;
|
sl@0
|
194 |
unsigned char *p;
|
sl@0
|
195 |
|
sl@0
|
196 |
/* the mac has already been generated when we received the
|
sl@0
|
197 |
* change cipher spec message and is in s->s3->tmp.peer_finish_md
|
sl@0
|
198 |
*/
|
sl@0
|
199 |
|
sl@0
|
200 |
n=s->method->ssl_get_message(s,
|
sl@0
|
201 |
a,
|
sl@0
|
202 |
b,
|
sl@0
|
203 |
SSL3_MT_FINISHED,
|
sl@0
|
204 |
64, /* should actually be 36+4 :-) */
|
sl@0
|
205 |
&ok);
|
sl@0
|
206 |
|
sl@0
|
207 |
if (!ok) return((int)n);
|
sl@0
|
208 |
|
sl@0
|
209 |
/* If this occurs, we have missed a message */
|
sl@0
|
210 |
if (!s->s3->change_cipher_spec)
|
sl@0
|
211 |
{
|
sl@0
|
212 |
al=SSL_AD_UNEXPECTED_MESSAGE;
|
sl@0
|
213 |
SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_GOT_A_FIN_BEFORE_A_CCS);
|
sl@0
|
214 |
goto f_err;
|
sl@0
|
215 |
}
|
sl@0
|
216 |
s->s3->change_cipher_spec=0;
|
sl@0
|
217 |
|
sl@0
|
218 |
p = (unsigned char *)s->init_msg;
|
sl@0
|
219 |
i = s->s3->tmp.peer_finish_md_len;
|
sl@0
|
220 |
|
sl@0
|
221 |
if (i != n)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
al=SSL_AD_DECODE_ERROR;
|
sl@0
|
224 |
SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_BAD_DIGEST_LENGTH);
|
sl@0
|
225 |
goto f_err;
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
if (memcmp(p, s->s3->tmp.peer_finish_md, i) != 0)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
al=SSL_AD_DECRYPT_ERROR;
|
sl@0
|
231 |
SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_DIGEST_CHECK_FAILED);
|
sl@0
|
232 |
goto f_err;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
return(1);
|
sl@0
|
236 |
f_err:
|
sl@0
|
237 |
ssl3_send_alert(s,SSL3_AL_FATAL,al);
|
sl@0
|
238 |
return(0);
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
/* for these 2 messages, we need to
|
sl@0
|
242 |
* ssl->enc_read_ctx re-init
|
sl@0
|
243 |
* ssl->s3->read_sequence zero
|
sl@0
|
244 |
* ssl->s3->read_mac_secret re-init
|
sl@0
|
245 |
* ssl->session->read_sym_enc assign
|
sl@0
|
246 |
* ssl->session->read_compression assign
|
sl@0
|
247 |
* ssl->session->read_hash assign
|
sl@0
|
248 |
*/
|
sl@0
|
249 |
int ssl3_send_change_cipher_spec(SSL *s, int a, int b)
|
sl@0
|
250 |
{
|
sl@0
|
251 |
unsigned char *p;
|
sl@0
|
252 |
|
sl@0
|
253 |
if (s->state == a)
|
sl@0
|
254 |
{
|
sl@0
|
255 |
p=(unsigned char *)s->init_buf->data;
|
sl@0
|
256 |
*p=SSL3_MT_CCS;
|
sl@0
|
257 |
s->init_num=1;
|
sl@0
|
258 |
s->init_off=0;
|
sl@0
|
259 |
|
sl@0
|
260 |
s->state=b;
|
sl@0
|
261 |
}
|
sl@0
|
262 |
|
sl@0
|
263 |
/* SSL3_ST_CW_CHANGE_B */
|
sl@0
|
264 |
return(ssl3_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC));
|
sl@0
|
265 |
}
|
sl@0
|
266 |
|
sl@0
|
267 |
unsigned long ssl3_output_cert_chain(SSL *s, X509 *x)
|
sl@0
|
268 |
{
|
sl@0
|
269 |
unsigned char *p;
|
sl@0
|
270 |
int n,i;
|
sl@0
|
271 |
unsigned long l=7;
|
sl@0
|
272 |
BUF_MEM *buf;
|
sl@0
|
273 |
X509_STORE_CTX xs_ctx;
|
sl@0
|
274 |
X509_OBJECT obj;
|
sl@0
|
275 |
|
sl@0
|
276 |
int no_chain;
|
sl@0
|
277 |
|
sl@0
|
278 |
if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs)
|
sl@0
|
279 |
no_chain = 1;
|
sl@0
|
280 |
else
|
sl@0
|
281 |
no_chain = 0;
|
sl@0
|
282 |
|
sl@0
|
283 |
/* TLSv1 sends a chain with nothing in it, instead of an alert */
|
sl@0
|
284 |
buf=s->init_buf;
|
sl@0
|
285 |
if (!BUF_MEM_grow_clean(buf,10))
|
sl@0
|
286 |
{
|
sl@0
|
287 |
SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
|
sl@0
|
288 |
return(0);
|
sl@0
|
289 |
}
|
sl@0
|
290 |
if (x != NULL)
|
sl@0
|
291 |
{
|
sl@0
|
292 |
if(!no_chain && !X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,NULL,NULL))
|
sl@0
|
293 |
{
|
sl@0
|
294 |
SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB);
|
sl@0
|
295 |
return(0);
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|
sl@0
|
298 |
for (;;)
|
sl@0
|
299 |
{
|
sl@0
|
300 |
n=i2d_X509(x,NULL);
|
sl@0
|
301 |
if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
|
sl@0
|
302 |
{
|
sl@0
|
303 |
SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
|
sl@0
|
304 |
return(0);
|
sl@0
|
305 |
}
|
sl@0
|
306 |
p=(unsigned char *)&(buf->data[l]);
|
sl@0
|
307 |
l2n3(n,p);
|
sl@0
|
308 |
i2d_X509(x,&p);
|
sl@0
|
309 |
l+=n+3;
|
sl@0
|
310 |
|
sl@0
|
311 |
if (no_chain)
|
sl@0
|
312 |
break;
|
sl@0
|
313 |
|
sl@0
|
314 |
if (X509_NAME_cmp(X509_get_subject_name(x),
|
sl@0
|
315 |
X509_get_issuer_name(x)) == 0) break;
|
sl@0
|
316 |
|
sl@0
|
317 |
i=X509_STORE_get_by_subject(&xs_ctx,X509_LU_X509,
|
sl@0
|
318 |
X509_get_issuer_name(x),&obj);
|
sl@0
|
319 |
if (i <= 0) break;
|
sl@0
|
320 |
x=obj.data.x509;
|
sl@0
|
321 |
/* Count is one too high since the X509_STORE_get uped the
|
sl@0
|
322 |
* ref count */
|
sl@0
|
323 |
X509_free(x);
|
sl@0
|
324 |
}
|
sl@0
|
325 |
if (!no_chain)
|
sl@0
|
326 |
X509_STORE_CTX_cleanup(&xs_ctx);
|
sl@0
|
327 |
}
|
sl@0
|
328 |
|
sl@0
|
329 |
/* Thawte special :-) */
|
sl@0
|
330 |
if (s->ctx->extra_certs != NULL)
|
sl@0
|
331 |
for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++)
|
sl@0
|
332 |
{
|
sl@0
|
333 |
x=sk_X509_value(s->ctx->extra_certs,i);
|
sl@0
|
334 |
n=i2d_X509(x,NULL);
|
sl@0
|
335 |
if (!BUF_MEM_grow_clean(buf,(int)(n+l+3)))
|
sl@0
|
336 |
{
|
sl@0
|
337 |
SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB);
|
sl@0
|
338 |
return(0);
|
sl@0
|
339 |
}
|
sl@0
|
340 |
p=(unsigned char *)&(buf->data[l]);
|
sl@0
|
341 |
l2n3(n,p);
|
sl@0
|
342 |
i2d_X509(x,&p);
|
sl@0
|
343 |
l+=n+3;
|
sl@0
|
344 |
}
|
sl@0
|
345 |
|
sl@0
|
346 |
l-=7;
|
sl@0
|
347 |
p=(unsigned char *)&(buf->data[4]);
|
sl@0
|
348 |
l2n3(l,p);
|
sl@0
|
349 |
l+=3;
|
sl@0
|
350 |
p=(unsigned char *)&(buf->data[0]);
|
sl@0
|
351 |
*(p++)=SSL3_MT_CERTIFICATE;
|
sl@0
|
352 |
l2n3(l,p);
|
sl@0
|
353 |
l+=4;
|
sl@0
|
354 |
return(l);
|
sl@0
|
355 |
}
|
sl@0
|
356 |
|
sl@0
|
357 |
/* Obtain handshake message of message type 'mt' (any if mt == -1),
|
sl@0
|
358 |
* maximum acceptable body length 'max'.
|
sl@0
|
359 |
* The first four bytes (msg_type and length) are read in state 'st1',
|
sl@0
|
360 |
* the body is read in state 'stn'.
|
sl@0
|
361 |
*/
|
sl@0
|
362 |
long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
|
sl@0
|
363 |
{
|
sl@0
|
364 |
unsigned char *p;
|
sl@0
|
365 |
unsigned long l;
|
sl@0
|
366 |
long n;
|
sl@0
|
367 |
int i,al;
|
sl@0
|
368 |
|
sl@0
|
369 |
if (s->s3->tmp.reuse_message)
|
sl@0
|
370 |
{
|
sl@0
|
371 |
s->s3->tmp.reuse_message=0;
|
sl@0
|
372 |
if ((mt >= 0) && (s->s3->tmp.message_type != mt))
|
sl@0
|
373 |
{
|
sl@0
|
374 |
al=SSL_AD_UNEXPECTED_MESSAGE;
|
sl@0
|
375 |
SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
|
sl@0
|
376 |
goto f_err;
|
sl@0
|
377 |
}
|
sl@0
|
378 |
*ok=1;
|
sl@0
|
379 |
s->init_msg = s->init_buf->data + 4;
|
sl@0
|
380 |
s->init_num = (int)s->s3->tmp.message_size;
|
sl@0
|
381 |
return s->init_num;
|
sl@0
|
382 |
}
|
sl@0
|
383 |
|
sl@0
|
384 |
p=(unsigned char *)s->init_buf->data;
|
sl@0
|
385 |
|
sl@0
|
386 |
if (s->state == st1) /* s->init_num < 4 */
|
sl@0
|
387 |
{
|
sl@0
|
388 |
int skip_message;
|
sl@0
|
389 |
|
sl@0
|
390 |
do
|
sl@0
|
391 |
{
|
sl@0
|
392 |
while (s->init_num < 4)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,
|
sl@0
|
395 |
&p[s->init_num],4 - s->init_num, 0);
|
sl@0
|
396 |
if (i <= 0)
|
sl@0
|
397 |
{
|
sl@0
|
398 |
s->rwstate=SSL_READING;
|
sl@0
|
399 |
*ok = 0;
|
sl@0
|
400 |
return i;
|
sl@0
|
401 |
}
|
sl@0
|
402 |
s->init_num+=i;
|
sl@0
|
403 |
}
|
sl@0
|
404 |
|
sl@0
|
405 |
skip_message = 0;
|
sl@0
|
406 |
if (!s->server)
|
sl@0
|
407 |
if (p[0] == SSL3_MT_HELLO_REQUEST)
|
sl@0
|
408 |
/* The server may always send 'Hello Request' messages --
|
sl@0
|
409 |
* we are doing a handshake anyway now, so ignore them
|
sl@0
|
410 |
* if their format is correct. Does not count for
|
sl@0
|
411 |
* 'Finished' MAC. */
|
sl@0
|
412 |
if (p[1] == 0 && p[2] == 0 &&p[3] == 0)
|
sl@0
|
413 |
{
|
sl@0
|
414 |
s->init_num = 0;
|
sl@0
|
415 |
skip_message = 1;
|
sl@0
|
416 |
|
sl@0
|
417 |
if (s->msg_callback)
|
sl@0
|
418 |
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg);
|
sl@0
|
419 |
}
|
sl@0
|
420 |
}
|
sl@0
|
421 |
while (skip_message);
|
sl@0
|
422 |
|
sl@0
|
423 |
/* s->init_num == 4 */
|
sl@0
|
424 |
|
sl@0
|
425 |
if ((mt >= 0) && (*p != mt))
|
sl@0
|
426 |
{
|
sl@0
|
427 |
al=SSL_AD_UNEXPECTED_MESSAGE;
|
sl@0
|
428 |
SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);
|
sl@0
|
429 |
goto f_err;
|
sl@0
|
430 |
}
|
sl@0
|
431 |
if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&
|
sl@0
|
432 |
(st1 == SSL3_ST_SR_CERT_A) &&
|
sl@0
|
433 |
(stn == SSL3_ST_SR_CERT_B))
|
sl@0
|
434 |
{
|
sl@0
|
435 |
/* At this point we have got an MS SGC second client
|
sl@0
|
436 |
* hello (maybe we should always allow the client to
|
sl@0
|
437 |
* start a new handshake?). We need to restart the mac.
|
sl@0
|
438 |
* Don't increment {num,total}_renegotiations because
|
sl@0
|
439 |
* we have not completed the handshake. */
|
sl@0
|
440 |
ssl3_init_finished_mac(s);
|
sl@0
|
441 |
}
|
sl@0
|
442 |
|
sl@0
|
443 |
s->s3->tmp.message_type= *(p++);
|
sl@0
|
444 |
|
sl@0
|
445 |
n2l3(p,l);
|
sl@0
|
446 |
if (l > (unsigned long)max)
|
sl@0
|
447 |
{
|
sl@0
|
448 |
al=SSL_AD_ILLEGAL_PARAMETER;
|
sl@0
|
449 |
SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
|
sl@0
|
450 |
goto f_err;
|
sl@0
|
451 |
}
|
sl@0
|
452 |
if (l > (INT_MAX-4)) /* BUF_MEM_grow takes an 'int' parameter */
|
sl@0
|
453 |
{
|
sl@0
|
454 |
al=SSL_AD_ILLEGAL_PARAMETER;
|
sl@0
|
455 |
SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);
|
sl@0
|
456 |
goto f_err;
|
sl@0
|
457 |
}
|
sl@0
|
458 |
if (l && !BUF_MEM_grow_clean(s->init_buf,(int)l+4))
|
sl@0
|
459 |
{
|
sl@0
|
460 |
SSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);
|
sl@0
|
461 |
goto err;
|
sl@0
|
462 |
}
|
sl@0
|
463 |
s->s3->tmp.message_size=l;
|
sl@0
|
464 |
s->state=stn;
|
sl@0
|
465 |
|
sl@0
|
466 |
s->init_msg = s->init_buf->data + 4;
|
sl@0
|
467 |
s->init_num = 0;
|
sl@0
|
468 |
}
|
sl@0
|
469 |
|
sl@0
|
470 |
/* next state (stn) */
|
sl@0
|
471 |
p = s->init_msg;
|
sl@0
|
472 |
n = s->s3->tmp.message_size - s->init_num;
|
sl@0
|
473 |
while (n > 0)
|
sl@0
|
474 |
{
|
sl@0
|
475 |
i=s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0);
|
sl@0
|
476 |
if (i <= 0)
|
sl@0
|
477 |
{
|
sl@0
|
478 |
s->rwstate=SSL_READING;
|
sl@0
|
479 |
*ok = 0;
|
sl@0
|
480 |
return i;
|
sl@0
|
481 |
}
|
sl@0
|
482 |
s->init_num += i;
|
sl@0
|
483 |
n -= i;
|
sl@0
|
484 |
}
|
sl@0
|
485 |
ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4);
|
sl@0
|
486 |
if (s->msg_callback)
|
sl@0
|
487 |
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, (size_t)s->init_num + 4, s, s->msg_callback_arg);
|
sl@0
|
488 |
*ok=1;
|
sl@0
|
489 |
return s->init_num;
|
sl@0
|
490 |
f_err:
|
sl@0
|
491 |
ssl3_send_alert(s,SSL3_AL_FATAL,al);
|
sl@0
|
492 |
err:
|
sl@0
|
493 |
*ok=0;
|
sl@0
|
494 |
return(-1);
|
sl@0
|
495 |
}
|
sl@0
|
496 |
|
sl@0
|
497 |
int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
|
sl@0
|
498 |
{
|
sl@0
|
499 |
EVP_PKEY *pk;
|
sl@0
|
500 |
int ret= -1,i;
|
sl@0
|
501 |
|
sl@0
|
502 |
if (pkey == NULL)
|
sl@0
|
503 |
pk=X509_get_pubkey(x);
|
sl@0
|
504 |
else
|
sl@0
|
505 |
pk=pkey;
|
sl@0
|
506 |
if (pk == NULL) goto err;
|
sl@0
|
507 |
|
sl@0
|
508 |
i=pk->type;
|
sl@0
|
509 |
if (i == EVP_PKEY_RSA)
|
sl@0
|
510 |
{
|
sl@0
|
511 |
ret=SSL_PKEY_RSA_ENC;
|
sl@0
|
512 |
}
|
sl@0
|
513 |
else if (i == EVP_PKEY_DSA)
|
sl@0
|
514 |
{
|
sl@0
|
515 |
ret=SSL_PKEY_DSA_SIGN;
|
sl@0
|
516 |
}
|
sl@0
|
517 |
#ifndef OPENSSL_NO_EC
|
sl@0
|
518 |
else if (i == EVP_PKEY_EC)
|
sl@0
|
519 |
{
|
sl@0
|
520 |
ret = SSL_PKEY_ECC;
|
sl@0
|
521 |
}
|
sl@0
|
522 |
#endif
|
sl@0
|
523 |
|
sl@0
|
524 |
err:
|
sl@0
|
525 |
if(!pkey) EVP_PKEY_free(pk);
|
sl@0
|
526 |
return(ret);
|
sl@0
|
527 |
}
|
sl@0
|
528 |
|
sl@0
|
529 |
int ssl_verify_alarm_type(long type)
|
sl@0
|
530 |
{
|
sl@0
|
531 |
int al;
|
sl@0
|
532 |
|
sl@0
|
533 |
switch(type)
|
sl@0
|
534 |
{
|
sl@0
|
535 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
|
sl@0
|
536 |
case X509_V_ERR_UNABLE_TO_GET_CRL:
|
sl@0
|
537 |
case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
|
sl@0
|
538 |
al=SSL_AD_UNKNOWN_CA;
|
sl@0
|
539 |
break;
|
sl@0
|
540 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
|
sl@0
|
541 |
case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
|
sl@0
|
542 |
case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
|
sl@0
|
543 |
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
|
sl@0
|
544 |
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
|
sl@0
|
545 |
case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
|
sl@0
|
546 |
case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
|
sl@0
|
547 |
case X509_V_ERR_CERT_NOT_YET_VALID:
|
sl@0
|
548 |
case X509_V_ERR_CRL_NOT_YET_VALID:
|
sl@0
|
549 |
case X509_V_ERR_CERT_UNTRUSTED:
|
sl@0
|
550 |
case X509_V_ERR_CERT_REJECTED:
|
sl@0
|
551 |
al=SSL_AD_BAD_CERTIFICATE;
|
sl@0
|
552 |
break;
|
sl@0
|
553 |
case X509_V_ERR_CERT_SIGNATURE_FAILURE:
|
sl@0
|
554 |
case X509_V_ERR_CRL_SIGNATURE_FAILURE:
|
sl@0
|
555 |
al=SSL_AD_DECRYPT_ERROR;
|
sl@0
|
556 |
break;
|
sl@0
|
557 |
case X509_V_ERR_CERT_HAS_EXPIRED:
|
sl@0
|
558 |
case X509_V_ERR_CRL_HAS_EXPIRED:
|
sl@0
|
559 |
al=SSL_AD_CERTIFICATE_EXPIRED;
|
sl@0
|
560 |
break;
|
sl@0
|
561 |
case X509_V_ERR_CERT_REVOKED:
|
sl@0
|
562 |
al=SSL_AD_CERTIFICATE_REVOKED;
|
sl@0
|
563 |
break;
|
sl@0
|
564 |
case X509_V_ERR_OUT_OF_MEM:
|
sl@0
|
565 |
al=SSL_AD_INTERNAL_ERROR;
|
sl@0
|
566 |
break;
|
sl@0
|
567 |
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
|
sl@0
|
568 |
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
|
sl@0
|
569 |
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
|
sl@0
|
570 |
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
|
sl@0
|
571 |
case X509_V_ERR_CERT_CHAIN_TOO_LONG:
|
sl@0
|
572 |
case X509_V_ERR_PATH_LENGTH_EXCEEDED:
|
sl@0
|
573 |
case X509_V_ERR_INVALID_CA:
|
sl@0
|
574 |
al=SSL_AD_UNKNOWN_CA;
|
sl@0
|
575 |
break;
|
sl@0
|
576 |
case X509_V_ERR_APPLICATION_VERIFICATION:
|
sl@0
|
577 |
al=SSL_AD_HANDSHAKE_FAILURE;
|
sl@0
|
578 |
break;
|
sl@0
|
579 |
case X509_V_ERR_INVALID_PURPOSE:
|
sl@0
|
580 |
al=SSL_AD_UNSUPPORTED_CERTIFICATE;
|
sl@0
|
581 |
break;
|
sl@0
|
582 |
default:
|
sl@0
|
583 |
al=SSL_AD_CERTIFICATE_UNKNOWN;
|
sl@0
|
584 |
break;
|
sl@0
|
585 |
}
|
sl@0
|
586 |
return(al);
|
sl@0
|
587 |
}
|
sl@0
|
588 |
|
sl@0
|
589 |
int ssl3_setup_buffers(SSL *s)
|
sl@0
|
590 |
{
|
sl@0
|
591 |
unsigned char *p;
|
sl@0
|
592 |
unsigned int extra;
|
sl@0
|
593 |
size_t len;
|
sl@0
|
594 |
|
sl@0
|
595 |
if (s->s3->rbuf.buf == NULL)
|
sl@0
|
596 |
{
|
sl@0
|
597 |
if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
|
sl@0
|
598 |
extra=SSL3_RT_MAX_EXTRA;
|
sl@0
|
599 |
else
|
sl@0
|
600 |
extra=0;
|
sl@0
|
601 |
len = SSL3_RT_MAX_PACKET_SIZE + extra;
|
sl@0
|
602 |
if ((p=OPENSSL_malloc(len)) == NULL)
|
sl@0
|
603 |
goto err;
|
sl@0
|
604 |
s->s3->rbuf.buf = p;
|
sl@0
|
605 |
s->s3->rbuf.len = len;
|
sl@0
|
606 |
}
|
sl@0
|
607 |
|
sl@0
|
608 |
if (s->s3->wbuf.buf == NULL)
|
sl@0
|
609 |
{
|
sl@0
|
610 |
len = SSL3_RT_MAX_PACKET_SIZE;
|
sl@0
|
611 |
len += SSL3_RT_HEADER_LENGTH + 256; /* extra space for empty fragment */
|
sl@0
|
612 |
if ((p=OPENSSL_malloc(len)) == NULL)
|
sl@0
|
613 |
goto err;
|
sl@0
|
614 |
s->s3->wbuf.buf = p;
|
sl@0
|
615 |
s->s3->wbuf.len = len;
|
sl@0
|
616 |
}
|
sl@0
|
617 |
s->packet= &(s->s3->rbuf.buf[0]);
|
sl@0
|
618 |
return(1);
|
sl@0
|
619 |
err:
|
sl@0
|
620 |
SSLerr(SSL_F_SSL3_SETUP_BUFFERS,ERR_R_MALLOC_FAILURE);
|
sl@0
|
621 |
return(0);
|
sl@0
|
622 |
}
|