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