diff options
Diffstat (limited to 'src/lib/libssl/s3_both.c')
-rw-r--r-- | src/lib/libssl/s3_both.c | 702 |
1 files changed, 0 insertions, 702 deletions
diff --git a/src/lib/libssl/s3_both.c b/src/lib/libssl/s3_both.c deleted file mode 100644 index a2ce9e9fa3..0000000000 --- a/src/lib/libssl/s3_both.c +++ /dev/null | |||
@@ -1,702 +0,0 @@ | |||
1 | /* $OpenBSD: s3_both.c,v 1.37 2014/12/14 21:49:29 bcook Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
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. | ||
8 | * | ||
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). | ||
15 | * | ||
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. | ||
22 | * | ||
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 :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
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. | ||
52 | * | ||
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 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | ||
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 | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
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 | */ | ||
111 | /* ==================================================================== | ||
112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | ||
113 | * ECC cipher suite support in OpenSSL originally developed by | ||
114 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. | ||
115 | */ | ||
116 | |||
117 | #include <limits.h> | ||
118 | #include <stdio.h> | ||
119 | #include <string.h> | ||
120 | |||
121 | #include "ssl_locl.h" | ||
122 | |||
123 | #include <openssl/buffer.h> | ||
124 | #include <openssl/evp.h> | ||
125 | #include <openssl/objects.h> | ||
126 | #include <openssl/x509.h> | ||
127 | |||
128 | /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */ | ||
129 | int | ||
130 | ssl3_do_write(SSL *s, int type) | ||
131 | { | ||
132 | int ret; | ||
133 | |||
134 | ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off], | ||
135 | s->init_num); | ||
136 | if (ret < 0) | ||
137 | return (-1); | ||
138 | if (type == SSL3_RT_HANDSHAKE) | ||
139 | /* should not be done for 'Hello Request's, but in that case | ||
140 | * we'll ignore the result anyway */ | ||
141 | ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off], ret); | ||
142 | |||
143 | if (ret == s->init_num) { | ||
144 | if (s->msg_callback) | ||
145 | s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg); | ||
146 | return (1); | ||
147 | } | ||
148 | s->init_off += ret; | ||
149 | s->init_num -= ret; | ||
150 | return (0); | ||
151 | } | ||
152 | |||
153 | int | ||
154 | ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen) | ||
155 | { | ||
156 | unsigned char *p, *d; | ||
157 | unsigned long l; | ||
158 | int md_len; | ||
159 | |||
160 | if (s->state == a) { | ||
161 | d = (unsigned char *)s->init_buf->data; | ||
162 | p = &(d[4]); | ||
163 | |||
164 | md_len = s->method->ssl3_enc->finish_mac_length; | ||
165 | if (s->method->ssl3_enc->final_finish_mac(s, sender, slen, | ||
166 | s->s3->tmp.finish_md) != md_len) | ||
167 | return (0); | ||
168 | s->s3->tmp.finish_md_len = md_len; | ||
169 | memcpy(p, s->s3->tmp.finish_md, md_len); | ||
170 | p += md_len; | ||
171 | l = md_len; | ||
172 | |||
173 | /* Copy finished so we can use it for renegotiation checks. */ | ||
174 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); | ||
175 | if (s->type == SSL_ST_CONNECT) { | ||
176 | memcpy(s->s3->previous_client_finished, | ||
177 | s->s3->tmp.finish_md, md_len); | ||
178 | s->s3->previous_client_finished_len = md_len; | ||
179 | } else { | ||
180 | memcpy(s->s3->previous_server_finished, | ||
181 | s->s3->tmp.finish_md, md_len); | ||
182 | s->s3->previous_server_finished_len = md_len; | ||
183 | } | ||
184 | |||
185 | *(d++) = SSL3_MT_FINISHED; | ||
186 | l2n3(l, d); | ||
187 | s->init_num = (int)l + 4; | ||
188 | s->init_off = 0; | ||
189 | |||
190 | s->state = b; | ||
191 | } | ||
192 | |||
193 | /* SSL3_ST_SEND_xxxxxx_HELLO_B */ | ||
194 | return (ssl3_do_write(s, SSL3_RT_HANDSHAKE)); | ||
195 | } | ||
196 | |||
197 | /* ssl3_take_mac calculates the Finished MAC for the handshakes messages seen to far. */ | ||
198 | static void | ||
199 | ssl3_take_mac(SSL *s) | ||
200 | { | ||
201 | const char *sender; | ||
202 | int slen; | ||
203 | /* If no new cipher setup return immediately: other functions will | ||
204 | * set the appropriate error. | ||
205 | */ | ||
206 | if (s->s3->tmp.new_cipher == NULL) | ||
207 | return; | ||
208 | if (s->state & SSL_ST_CONNECT) { | ||
209 | sender = s->method->ssl3_enc->server_finished_label; | ||
210 | slen = s->method->ssl3_enc->server_finished_label_len; | ||
211 | } else { | ||
212 | sender = s->method->ssl3_enc->client_finished_label; | ||
213 | slen = s->method->ssl3_enc->client_finished_label_len; | ||
214 | } | ||
215 | |||
216 | s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s, | ||
217 | sender, slen, s->s3->tmp.peer_finish_md); | ||
218 | } | ||
219 | |||
220 | int | ||
221 | ssl3_get_finished(SSL *s, int a, int b) | ||
222 | { | ||
223 | int al, ok, md_len; | ||
224 | long n; | ||
225 | unsigned char *p; | ||
226 | |||
227 | |||
228 | n = s->method->ssl_get_message(s, a, b, SSL3_MT_FINISHED, | ||
229 | 64, /* should actually be 36+4 :-) */ &ok); | ||
230 | |||
231 | if (!ok) | ||
232 | return ((int)n); | ||
233 | |||
234 | /* If this occurs, we have missed a message */ | ||
235 | if (!s->s3->change_cipher_spec) { | ||
236 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
237 | SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS); | ||
238 | goto f_err; | ||
239 | } | ||
240 | s->s3->change_cipher_spec = 0; | ||
241 | |||
242 | md_len = s->method->ssl3_enc->finish_mac_length; | ||
243 | p = (unsigned char *)s->init_msg; | ||
244 | |||
245 | if (s->s3->tmp.peer_finish_md_len != md_len || n != md_len) { | ||
246 | al = SSL_AD_DECODE_ERROR; | ||
247 | SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_BAD_DIGEST_LENGTH); | ||
248 | goto f_err; | ||
249 | } | ||
250 | |||
251 | if (timingsafe_memcmp(p, s->s3->tmp.peer_finish_md, md_len) != 0) { | ||
252 | al = SSL_AD_DECRYPT_ERROR; | ||
253 | SSLerr(SSL_F_SSL3_GET_FINISHED, SSL_R_DIGEST_CHECK_FAILED); | ||
254 | goto f_err; | ||
255 | } | ||
256 | |||
257 | /* Copy finished so we can use it for renegotiation checks. */ | ||
258 | OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); | ||
259 | if (s->type == SSL_ST_ACCEPT) { | ||
260 | memcpy(s->s3->previous_client_finished, | ||
261 | s->s3->tmp.peer_finish_md, md_len); | ||
262 | s->s3->previous_client_finished_len = md_len; | ||
263 | } else { | ||
264 | memcpy(s->s3->previous_server_finished, | ||
265 | s->s3->tmp.peer_finish_md, md_len); | ||
266 | s->s3->previous_server_finished_len = md_len; | ||
267 | } | ||
268 | |||
269 | return (1); | ||
270 | f_err: | ||
271 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
272 | return (0); | ||
273 | } | ||
274 | |||
275 | /* for these 2 messages, we need to | ||
276 | * ssl->enc_read_ctx re-init | ||
277 | * ssl->s3->read_sequence zero | ||
278 | * ssl->s3->read_mac_secret re-init | ||
279 | * ssl->session->read_sym_enc assign | ||
280 | * ssl->session->read_hash assign | ||
281 | */ | ||
282 | int | ||
283 | ssl3_send_change_cipher_spec(SSL *s, int a, int b) | ||
284 | { | ||
285 | unsigned char *p; | ||
286 | |||
287 | if (s->state == a) { | ||
288 | p = (unsigned char *)s->init_buf->data; | ||
289 | *p = SSL3_MT_CCS; | ||
290 | s->init_num = 1; | ||
291 | s->init_off = 0; | ||
292 | |||
293 | s->state = b; | ||
294 | } | ||
295 | |||
296 | /* SSL3_ST_CW_CHANGE_B */ | ||
297 | return (ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC)); | ||
298 | } | ||
299 | |||
300 | static int | ||
301 | ssl3_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) | ||
302 | { | ||
303 | int n; | ||
304 | unsigned char *p; | ||
305 | |||
306 | n = i2d_X509(x, NULL); | ||
307 | if (!BUF_MEM_grow_clean(buf, n + (*l) + 3)) { | ||
308 | SSLerr(SSL_F_SSL3_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); | ||
309 | return (-1); | ||
310 | } | ||
311 | p = (unsigned char *)&(buf->data[*l]); | ||
312 | l2n3(n, p); | ||
313 | i2d_X509(x, &p); | ||
314 | *l += n + 3; | ||
315 | |||
316 | return (0); | ||
317 | } | ||
318 | |||
319 | unsigned long | ||
320 | ssl3_output_cert_chain(SSL *s, X509 *x) | ||
321 | { | ||
322 | unsigned char *p; | ||
323 | int i; | ||
324 | unsigned long l = 7; | ||
325 | BUF_MEM *buf; | ||
326 | int no_chain; | ||
327 | |||
328 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs) | ||
329 | no_chain = 1; | ||
330 | else | ||
331 | no_chain = 0; | ||
332 | |||
333 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ | ||
334 | buf = s->init_buf; | ||
335 | if (!BUF_MEM_grow_clean(buf, 10)) { | ||
336 | SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB); | ||
337 | return (0); | ||
338 | } | ||
339 | if (x != NULL) { | ||
340 | if (no_chain) { | ||
341 | if (ssl3_add_cert_to_buf(buf, &l, x)) | ||
342 | return (0); | ||
343 | } else { | ||
344 | X509_STORE_CTX xs_ctx; | ||
345 | |||
346 | if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store, x, NULL)) { | ||
347 | SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB); | ||
348 | return (0); | ||
349 | } | ||
350 | X509_verify_cert(&xs_ctx); | ||
351 | /* Don't leave errors in the queue */ | ||
352 | ERR_clear_error(); | ||
353 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { | ||
354 | x = sk_X509_value(xs_ctx.chain, i); | ||
355 | |||
356 | if (ssl3_add_cert_to_buf(buf, &l, x)) { | ||
357 | X509_STORE_CTX_cleanup(&xs_ctx); | ||
358 | return 0; | ||
359 | } | ||
360 | } | ||
361 | X509_STORE_CTX_cleanup(&xs_ctx); | ||
362 | } | ||
363 | } | ||
364 | /* Thawte special :-) */ | ||
365 | for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) { | ||
366 | x = sk_X509_value(s->ctx->extra_certs, i); | ||
367 | if (ssl3_add_cert_to_buf(buf, &l, x)) | ||
368 | return (0); | ||
369 | } | ||
370 | |||
371 | l -= 7; | ||
372 | p = (unsigned char *)&(buf->data[4]); | ||
373 | l2n3(l, p); | ||
374 | l += 3; | ||
375 | p = (unsigned char *)&(buf->data[0]); | ||
376 | *(p++) = SSL3_MT_CERTIFICATE; | ||
377 | l2n3(l, p); | ||
378 | l += 4; | ||
379 | return (l); | ||
380 | } | ||
381 | |||
382 | /* Obtain handshake message of message type 'mt' (any if mt == -1), | ||
383 | * maximum acceptable body length 'max'. | ||
384 | * The first four bytes (msg_type and length) are read in state 'st1', | ||
385 | * the body is read in state 'stn'. | ||
386 | */ | ||
387 | long | ||
388 | ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok) | ||
389 | { | ||
390 | unsigned char *p; | ||
391 | unsigned long l; | ||
392 | long n; | ||
393 | int i, al; | ||
394 | |||
395 | if (s->s3->tmp.reuse_message) { | ||
396 | s->s3->tmp.reuse_message = 0; | ||
397 | if ((mt >= 0) && (s->s3->tmp.message_type != mt)) { | ||
398 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
399 | SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); | ||
400 | goto f_err; | ||
401 | } | ||
402 | *ok = 1; | ||
403 | s->init_msg = s->init_buf->data + 4; | ||
404 | s->init_num = (int)s->s3->tmp.message_size; | ||
405 | return s->init_num; | ||
406 | } | ||
407 | |||
408 | p = (unsigned char *)s->init_buf->data; | ||
409 | |||
410 | if (s->state == st1) /* s->init_num < 4 */ | ||
411 | { | ||
412 | int skip_message; | ||
413 | |||
414 | do { | ||
415 | while (s->init_num < 4) { | ||
416 | i = s->method->ssl_read_bytes(s, | ||
417 | SSL3_RT_HANDSHAKE, &p[s->init_num], | ||
418 | 4 - s->init_num, 0); | ||
419 | if (i <= 0) { | ||
420 | s->rwstate = SSL_READING; | ||
421 | *ok = 0; | ||
422 | return i; | ||
423 | } | ||
424 | s->init_num += i; | ||
425 | } | ||
426 | |||
427 | skip_message = 0; | ||
428 | if (!s->server && p[0] == SSL3_MT_HELLO_REQUEST) { | ||
429 | /* | ||
430 | * The server may always send 'Hello Request' | ||
431 | * messages -- we are doing a handshake anyway | ||
432 | * now, so ignore them if their format is | ||
433 | * correct. Does not count for 'Finished' MAC. | ||
434 | */ | ||
435 | if (p[1] == 0 && p[2] == 0 &&p[3] == 0) { | ||
436 | s->init_num = 0; | ||
437 | skip_message = 1; | ||
438 | |||
439 | if (s->msg_callback) | ||
440 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg); | ||
441 | } | ||
442 | } | ||
443 | } | ||
444 | while (skip_message); | ||
445 | |||
446 | /* s->init_num == 4 */ | ||
447 | |||
448 | if ((mt >= 0) && (*p != mt)) { | ||
449 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
450 | SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); | ||
451 | goto f_err; | ||
452 | } | ||
453 | if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) && | ||
454 | (st1 == SSL3_ST_SR_CERT_A) && (stn == SSL3_ST_SR_CERT_B)) { | ||
455 | /* At this point we have got an MS SGC second client | ||
456 | * hello (maybe we should always allow the client to | ||
457 | * start a new handshake?). We need to restart the mac. | ||
458 | * Don't increment {num,total}_renegotiations because | ||
459 | * we have not completed the handshake. */ | ||
460 | if (!ssl3_init_finished_mac(s)) { | ||
461 | SSLerr(SSL_F_SSL3_GET_MESSAGE, | ||
462 | ERR_R_MALLOC_FAILURE); | ||
463 | goto err; | ||
464 | } | ||
465 | } | ||
466 | |||
467 | s->s3->tmp.message_type= *(p++); | ||
468 | |||
469 | n2l3(p, l); | ||
470 | if (l > (unsigned long)max) { | ||
471 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
472 | SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE); | ||
473 | goto f_err; | ||
474 | } | ||
475 | if (l && !BUF_MEM_grow_clean(s->init_buf, l + 4)) { | ||
476 | SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB); | ||
477 | goto err; | ||
478 | } | ||
479 | s->s3->tmp.message_size = l; | ||
480 | s->state = stn; | ||
481 | |||
482 | s->init_msg = s->init_buf->data + 4; | ||
483 | s->init_num = 0; | ||
484 | } | ||
485 | |||
486 | /* next state (stn) */ | ||
487 | p = s->init_msg; | ||
488 | n = s->s3->tmp.message_size - s->init_num; | ||
489 | while (n > 0) { | ||
490 | i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, | ||
491 | &p[s->init_num], n, 0); | ||
492 | if (i <= 0) { | ||
493 | s->rwstate = SSL_READING; | ||
494 | *ok = 0; | ||
495 | return i; | ||
496 | } | ||
497 | s->init_num += i; | ||
498 | n -= i; | ||
499 | } | ||
500 | |||
501 | /* If receiving Finished, record MAC of prior handshake messages for | ||
502 | * Finished verification. */ | ||
503 | if (*s->init_buf->data == SSL3_MT_FINISHED) | ||
504 | ssl3_take_mac(s); | ||
505 | |||
506 | /* Feed this message into MAC computation. */ | ||
507 | ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4); | ||
508 | if (s->msg_callback) | ||
509 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, (size_t)s->init_num + 4, s, s->msg_callback_arg); | ||
510 | *ok = 1; | ||
511 | return s->init_num; | ||
512 | f_err: | ||
513 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
514 | err: | ||
515 | *ok = 0; | ||
516 | return (-1); | ||
517 | } | ||
518 | |||
519 | int | ||
520 | ssl_cert_type(X509 *x, EVP_PKEY *pkey) | ||
521 | { | ||
522 | EVP_PKEY *pk; | ||
523 | int ret = -1, i; | ||
524 | |||
525 | if (pkey == NULL) | ||
526 | pk = X509_get_pubkey(x); | ||
527 | else | ||
528 | pk = pkey; | ||
529 | if (pk == NULL) | ||
530 | goto err; | ||
531 | |||
532 | i = pk->type; | ||
533 | if (i == EVP_PKEY_RSA) { | ||
534 | ret = SSL_PKEY_RSA_ENC; | ||
535 | } else if (i == EVP_PKEY_DSA) { | ||
536 | ret = SSL_PKEY_DSA_SIGN; | ||
537 | } | ||
538 | else if (i == EVP_PKEY_EC) { | ||
539 | ret = SSL_PKEY_ECC; | ||
540 | } else if (i == NID_id_GostR3410_2001 || i == NID_id_GostR3410_2001_cc) { | ||
541 | ret = SSL_PKEY_GOST01; | ||
542 | } | ||
543 | err: | ||
544 | if (!pkey) | ||
545 | EVP_PKEY_free(pk); | ||
546 | return (ret); | ||
547 | } | ||
548 | |||
549 | int | ||
550 | ssl_verify_alarm_type(long type) | ||
551 | { | ||
552 | int al; | ||
553 | |||
554 | switch (type) { | ||
555 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | ||
556 | case X509_V_ERR_UNABLE_TO_GET_CRL: | ||
557 | case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: | ||
558 | al = SSL_AD_UNKNOWN_CA; | ||
559 | break; | ||
560 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: | ||
561 | case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: | ||
562 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: | ||
563 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | ||
564 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | ||
565 | case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: | ||
566 | case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: | ||
567 | case X509_V_ERR_CERT_NOT_YET_VALID: | ||
568 | case X509_V_ERR_CRL_NOT_YET_VALID: | ||
569 | case X509_V_ERR_CERT_UNTRUSTED: | ||
570 | case X509_V_ERR_CERT_REJECTED: | ||
571 | al = SSL_AD_BAD_CERTIFICATE; | ||
572 | break; | ||
573 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: | ||
574 | case X509_V_ERR_CRL_SIGNATURE_FAILURE: | ||
575 | al = SSL_AD_DECRYPT_ERROR; | ||
576 | break; | ||
577 | case X509_V_ERR_CERT_HAS_EXPIRED: | ||
578 | case X509_V_ERR_CRL_HAS_EXPIRED: | ||
579 | al = SSL_AD_CERTIFICATE_EXPIRED; | ||
580 | break; | ||
581 | case X509_V_ERR_CERT_REVOKED: | ||
582 | al = SSL_AD_CERTIFICATE_REVOKED; | ||
583 | break; | ||
584 | case X509_V_ERR_OUT_OF_MEM: | ||
585 | al = SSL_AD_INTERNAL_ERROR; | ||
586 | break; | ||
587 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | ||
588 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | ||
589 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: | ||
590 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: | ||
591 | case X509_V_ERR_CERT_CHAIN_TOO_LONG: | ||
592 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | ||
593 | case X509_V_ERR_INVALID_CA: | ||
594 | al = SSL_AD_UNKNOWN_CA; | ||
595 | break; | ||
596 | case X509_V_ERR_APPLICATION_VERIFICATION: | ||
597 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
598 | break; | ||
599 | case X509_V_ERR_INVALID_PURPOSE: | ||
600 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; | ||
601 | break; | ||
602 | default: | ||
603 | al = SSL_AD_CERTIFICATE_UNKNOWN; | ||
604 | break; | ||
605 | } | ||
606 | return (al); | ||
607 | } | ||
608 | |||
609 | int | ||
610 | ssl3_setup_read_buffer(SSL *s) | ||
611 | { | ||
612 | unsigned char *p; | ||
613 | size_t len, align, headerlen; | ||
614 | |||
615 | if (SSL_IS_DTLS(s)) | ||
616 | headerlen = DTLS1_RT_HEADER_LENGTH; | ||
617 | else | ||
618 | headerlen = SSL3_RT_HEADER_LENGTH; | ||
619 | |||
620 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); | ||
621 | |||
622 | if (s->s3->rbuf.buf == NULL) { | ||
623 | len = SSL3_RT_MAX_PLAIN_LENGTH + | ||
624 | SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align; | ||
625 | if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) { | ||
626 | s->s3->init_extra = 1; | ||
627 | len += SSL3_RT_MAX_EXTRA; | ||
628 | } | ||
629 | if ((p = malloc(len)) == NULL) | ||
630 | goto err; | ||
631 | s->s3->rbuf.buf = p; | ||
632 | s->s3->rbuf.len = len; | ||
633 | } | ||
634 | |||
635 | s->packet = &(s->s3->rbuf.buf[0]); | ||
636 | return 1; | ||
637 | |||
638 | err: | ||
639 | SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE); | ||
640 | return 0; | ||
641 | } | ||
642 | |||
643 | int | ||
644 | ssl3_setup_write_buffer(SSL *s) | ||
645 | { | ||
646 | unsigned char *p; | ||
647 | size_t len, align, headerlen; | ||
648 | |||
649 | if (SSL_IS_DTLS(s)) | ||
650 | headerlen = DTLS1_RT_HEADER_LENGTH + 1; | ||
651 | else | ||
652 | headerlen = SSL3_RT_HEADER_LENGTH; | ||
653 | |||
654 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); | ||
655 | |||
656 | if (s->s3->wbuf.buf == NULL) { | ||
657 | len = s->max_send_fragment + | ||
658 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align; | ||
659 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) | ||
660 | len += headerlen + align + | ||
661 | SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD; | ||
662 | |||
663 | if ((p = malloc(len)) == NULL) | ||
664 | goto err; | ||
665 | s->s3->wbuf.buf = p; | ||
666 | s->s3->wbuf.len = len; | ||
667 | } | ||
668 | |||
669 | return 1; | ||
670 | |||
671 | err: | ||
672 | SSLerr(SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE); | ||
673 | return 0; | ||
674 | } | ||
675 | |||
676 | |||
677 | int | ||
678 | ssl3_setup_buffers(SSL *s) | ||
679 | { | ||
680 | if (!ssl3_setup_read_buffer(s)) | ||
681 | return 0; | ||
682 | if (!ssl3_setup_write_buffer(s)) | ||
683 | return 0; | ||
684 | return 1; | ||
685 | } | ||
686 | |||
687 | int | ||
688 | ssl3_release_write_buffer(SSL *s) | ||
689 | { | ||
690 | free(s->s3->wbuf.buf); | ||
691 | s->s3->wbuf.buf = NULL; | ||
692 | return 1; | ||
693 | } | ||
694 | |||
695 | int | ||
696 | ssl3_release_read_buffer(SSL *s) | ||
697 | { | ||
698 | free(s->s3->rbuf.buf); | ||
699 | s->s3->rbuf.buf = NULL; | ||
700 | return 1; | ||
701 | } | ||
702 | |||