diff options
author | cvs2svn <admin@example.com> | 2015-03-08 16:48:49 +0000 |
---|---|---|
committer | cvs2svn <admin@example.com> | 2015-03-08 16:48:49 +0000 |
commit | decf84ba5550c1656a7fdb51b5b81969590c3f03 (patch) | |
tree | 44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libssl/s3_pkt.c | |
parent | 7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff) | |
download | openbsd-libressl-v2.1.5.tar.gz openbsd-libressl-v2.1.5.tar.bz2 openbsd-libressl-v2.1.5.zip |
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libssl/s3_pkt.c')
-rw-r--r-- | src/lib/libssl/s3_pkt.c | 1401 |
1 files changed, 0 insertions, 1401 deletions
diff --git a/src/lib/libssl/s3_pkt.c b/src/lib/libssl/s3_pkt.c deleted file mode 100644 index 117e6ec2da..0000000000 --- a/src/lib/libssl/s3_pkt.c +++ /dev/null | |||
@@ -1,1401 +0,0 @@ | |||
1 | /* $OpenBSD: s3_pkt.c,v 1.54 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 | #include <errno.h> | ||
113 | #include <stdio.h> | ||
114 | |||
115 | #include "ssl_locl.h" | ||
116 | |||
117 | #include <openssl/buffer.h> | ||
118 | #include <openssl/evp.h> | ||
119 | |||
120 | static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, | ||
121 | unsigned int len, int create_empty_fragment); | ||
122 | static int ssl3_get_record(SSL *s); | ||
123 | |||
124 | /* If extend == 0, obtain new n-byte packet; if extend == 1, increase | ||
125 | * packet by another n bytes. | ||
126 | * The packet will be in the sub-array of s->s3->rbuf.buf specified | ||
127 | * by s->packet and s->packet_length. | ||
128 | * (If s->read_ahead is set, 'max' bytes may be stored in rbuf | ||
129 | * [plus s->packet_length bytes if extend == 1].) | ||
130 | */ | ||
131 | int | ||
132 | ssl3_read_n(SSL *s, int n, int max, int extend) | ||
133 | { | ||
134 | int i, len, left; | ||
135 | size_t align; | ||
136 | unsigned char *pkt; | ||
137 | SSL3_BUFFER *rb; | ||
138 | |||
139 | if (n <= 0) | ||
140 | return n; | ||
141 | |||
142 | rb = &(s->s3->rbuf); | ||
143 | if (rb->buf == NULL) | ||
144 | if (!ssl3_setup_read_buffer(s)) | ||
145 | return -1; | ||
146 | |||
147 | left = rb->left; | ||
148 | align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH; | ||
149 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); | ||
150 | |||
151 | if (!extend) { | ||
152 | /* start with empty packet ... */ | ||
153 | if (left == 0) | ||
154 | rb->offset = align; | ||
155 | else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) { | ||
156 | /* check if next packet length is large | ||
157 | * enough to justify payload alignment... */ | ||
158 | pkt = rb->buf + rb->offset; | ||
159 | if (pkt[0] == SSL3_RT_APPLICATION_DATA && | ||
160 | (pkt[3]<<8|pkt[4]) >= 128) { | ||
161 | /* Note that even if packet is corrupted | ||
162 | * and its length field is insane, we can | ||
163 | * only be led to wrong decision about | ||
164 | * whether memmove will occur or not. | ||
165 | * Header values has no effect on memmove | ||
166 | * arguments and therefore no buffer | ||
167 | * overrun can be triggered. */ | ||
168 | memmove(rb->buf + align, pkt, left); | ||
169 | rb->offset = align; | ||
170 | } | ||
171 | } | ||
172 | s->packet = rb->buf + rb->offset; | ||
173 | s->packet_length = 0; | ||
174 | /* ... now we can act as if 'extend' was set */ | ||
175 | } | ||
176 | |||
177 | /* For DTLS/UDP reads should not span multiple packets | ||
178 | * because the read operation returns the whole packet | ||
179 | * at once (as long as it fits into the buffer). */ | ||
180 | if (SSL_IS_DTLS(s)) { | ||
181 | if (left > 0 && n > left) | ||
182 | n = left; | ||
183 | } | ||
184 | |||
185 | /* if there is enough in the buffer from a previous read, take some */ | ||
186 | if (left >= n) { | ||
187 | s->packet_length += n; | ||
188 | rb->left = left - n; | ||
189 | rb->offset += n; | ||
190 | return (n); | ||
191 | } | ||
192 | |||
193 | /* else we need to read more data */ | ||
194 | |||
195 | len = s->packet_length; | ||
196 | pkt = rb->buf + align; | ||
197 | /* Move any available bytes to front of buffer: | ||
198 | * 'len' bytes already pointed to by 'packet', | ||
199 | * 'left' extra ones at the end */ | ||
200 | if (s->packet != pkt) { | ||
201 | /* len > 0 */ | ||
202 | memmove(pkt, s->packet, len + left); | ||
203 | s->packet = pkt; | ||
204 | rb->offset = len + align; | ||
205 | } | ||
206 | |||
207 | if (n > (int)(rb->len - rb->offset)) { | ||
208 | /* does not happen */ | ||
209 | SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR); | ||
210 | return -1; | ||
211 | } | ||
212 | |||
213 | if (!s->read_ahead) { | ||
214 | /* ignore max parameter */ | ||
215 | max = n; | ||
216 | } else { | ||
217 | if (max < n) | ||
218 | max = n; | ||
219 | if (max > (int)(rb->len - rb->offset)) | ||
220 | max = rb->len - rb->offset; | ||
221 | } | ||
222 | |||
223 | while (left < n) { | ||
224 | /* Now we have len+left bytes at the front of s->s3->rbuf.buf | ||
225 | * and need to read in more until we have len+n (up to | ||
226 | * len+max if possible) */ | ||
227 | |||
228 | errno = 0; | ||
229 | if (s->rbio != NULL) { | ||
230 | s->rwstate = SSL_READING; | ||
231 | i = BIO_read(s->rbio, pkt + len + left, max - left); | ||
232 | } else { | ||
233 | SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET); | ||
234 | i = -1; | ||
235 | } | ||
236 | |||
237 | if (i <= 0) { | ||
238 | rb->left = left; | ||
239 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
240 | !SSL_IS_DTLS(s)) { | ||
241 | if (len + left == 0) | ||
242 | ssl3_release_read_buffer(s); | ||
243 | } | ||
244 | return (i); | ||
245 | } | ||
246 | left += i; | ||
247 | |||
248 | /* | ||
249 | * reads should *never* span multiple packets for DTLS because | ||
250 | * the underlying transport protocol is message oriented as | ||
251 | * opposed to byte oriented as in the TLS case. | ||
252 | */ | ||
253 | if (SSL_IS_DTLS(s)) { | ||
254 | if (n > left) | ||
255 | n = left; /* makes the while condition false */ | ||
256 | } | ||
257 | } | ||
258 | |||
259 | /* done reading, now the book-keeping */ | ||
260 | rb->offset += n; | ||
261 | rb->left = left - n; | ||
262 | s->packet_length += n; | ||
263 | s->rwstate = SSL_NOTHING; | ||
264 | return (n); | ||
265 | } | ||
266 | |||
267 | /* Call this to get a new input record. | ||
268 | * It will return <= 0 if more data is needed, normally due to an error | ||
269 | * or non-blocking IO. | ||
270 | * When it finishes, one packet has been decoded and can be found in | ||
271 | * ssl->s3->rrec.type - is the type of record | ||
272 | * ssl->s3->rrec.data, - data | ||
273 | * ssl->s3->rrec.length, - number of bytes | ||
274 | */ | ||
275 | /* used only by ssl3_read_bytes */ | ||
276 | static int | ||
277 | ssl3_get_record(SSL *s) | ||
278 | { | ||
279 | int ssl_major, ssl_minor, al; | ||
280 | int enc_err, n, i, ret = -1; | ||
281 | SSL3_RECORD *rr; | ||
282 | SSL_SESSION *sess; | ||
283 | unsigned char *p; | ||
284 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
285 | short version; | ||
286 | unsigned mac_size, orig_len; | ||
287 | size_t extra; | ||
288 | |||
289 | rr = &(s->s3->rrec); | ||
290 | sess = s->session; | ||
291 | |||
292 | if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) | ||
293 | extra = SSL3_RT_MAX_EXTRA; | ||
294 | else | ||
295 | extra = 0; | ||
296 | |||
297 | if (extra && !s->s3->init_extra) { | ||
298 | /* An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER | ||
299 | * set after ssl3_setup_buffers() was done */ | ||
300 | SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR); | ||
301 | return -1; | ||
302 | } | ||
303 | |||
304 | again: | ||
305 | /* check if we have the header */ | ||
306 | if ((s->rstate != SSL_ST_READ_BODY) || | ||
307 | (s->packet_length < SSL3_RT_HEADER_LENGTH)) { | ||
308 | n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); | ||
309 | if (n <= 0) | ||
310 | return(n); /* error or non-blocking */ | ||
311 | s->rstate = SSL_ST_READ_BODY; | ||
312 | |||
313 | p = s->packet; | ||
314 | |||
315 | /* Pull apart the header into the SSL3_RECORD */ | ||
316 | rr->type= *(p++); | ||
317 | ssl_major= *(p++); | ||
318 | ssl_minor= *(p++); | ||
319 | version = (ssl_major << 8)|ssl_minor; | ||
320 | n2s(p, rr->length); | ||
321 | |||
322 | /* Lets check version */ | ||
323 | if (!s->first_packet) { | ||
324 | if (version != s->version) { | ||
325 | SSLerr(SSL_F_SSL3_GET_RECORD, | ||
326 | SSL_R_WRONG_VERSION_NUMBER); | ||
327 | if ((s->version & 0xFF00) == (version & 0xFF00) && | ||
328 | !s->enc_write_ctx && !s->write_hash) | ||
329 | /* Send back error using their minor version number :-) */ | ||
330 | s->version = (unsigned short)version; | ||
331 | al = SSL_AD_PROTOCOL_VERSION; | ||
332 | goto f_err; | ||
333 | } | ||
334 | } | ||
335 | |||
336 | if ((version >> 8) != SSL3_VERSION_MAJOR) { | ||
337 | SSLerr(SSL_F_SSL3_GET_RECORD, | ||
338 | SSL_R_WRONG_VERSION_NUMBER); | ||
339 | goto err; | ||
340 | } | ||
341 | |||
342 | if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) { | ||
343 | al = SSL_AD_RECORD_OVERFLOW; | ||
344 | SSLerr(SSL_F_SSL3_GET_RECORD, | ||
345 | SSL_R_PACKET_LENGTH_TOO_LONG); | ||
346 | goto f_err; | ||
347 | } | ||
348 | |||
349 | /* now s->rstate == SSL_ST_READ_BODY */ | ||
350 | } | ||
351 | |||
352 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ | ||
353 | |||
354 | if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) { | ||
355 | /* now s->packet_length == SSL3_RT_HEADER_LENGTH */ | ||
356 | i = rr->length; | ||
357 | n = ssl3_read_n(s, i, i, 1); | ||
358 | if (n <= 0) | ||
359 | return(n); /* error or non-blocking io */ | ||
360 | /* now n == rr->length, | ||
361 | * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */ | ||
362 | } | ||
363 | |||
364 | s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */ | ||
365 | |||
366 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, | ||
367 | * and we have that many bytes in s->packet | ||
368 | */ | ||
369 | rr->input = &(s->packet[SSL3_RT_HEADER_LENGTH]); | ||
370 | |||
371 | /* ok, we can now read from 's->packet' data into 'rr' | ||
372 | * rr->input points at rr->length bytes, which | ||
373 | * need to be copied into rr->data by either | ||
374 | * the decryption or by the decompression | ||
375 | * When the data is 'copied' into the rr->data buffer, | ||
376 | * rr->input will be pointed at the new buffer */ | ||
377 | |||
378 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] | ||
379 | * rr->length bytes of encrypted compressed stuff. */ | ||
380 | |||
381 | /* check is not needed I believe */ | ||
382 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) { | ||
383 | al = SSL_AD_RECORD_OVERFLOW; | ||
384 | SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); | ||
385 | goto f_err; | ||
386 | } | ||
387 | |||
388 | /* decrypt in place in 'rr->input' */ | ||
389 | rr->data = rr->input; | ||
390 | |||
391 | enc_err = s->method->ssl3_enc->enc(s, 0); | ||
392 | /* enc_err is: | ||
393 | * 0: (in non-constant time) if the record is publically invalid. | ||
394 | * 1: if the padding is valid | ||
395 | * -1: if the padding is invalid */ | ||
396 | if (enc_err == 0) { | ||
397 | al = SSL_AD_DECRYPTION_FAILED; | ||
398 | SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG); | ||
399 | goto f_err; | ||
400 | } | ||
401 | |||
402 | |||
403 | /* r->length is now the compressed data plus mac */ | ||
404 | if ((sess != NULL) && (s->enc_read_ctx != NULL) && | ||
405 | (EVP_MD_CTX_md(s->read_hash) != NULL)) { | ||
406 | /* s->read_hash != NULL => mac_size != -1 */ | ||
407 | unsigned char *mac = NULL; | ||
408 | unsigned char mac_tmp[EVP_MAX_MD_SIZE]; | ||
409 | |||
410 | mac_size = EVP_MD_CTX_size(s->read_hash); | ||
411 | OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE); | ||
412 | |||
413 | /* kludge: *_cbc_remove_padding passes padding length in rr->type */ | ||
414 | orig_len = rr->length + ((unsigned int)rr->type >> 8); | ||
415 | |||
416 | /* orig_len is the length of the record before any padding was | ||
417 | * removed. This is public information, as is the MAC in use, | ||
418 | * therefore we can safely process the record in a different | ||
419 | * amount of time if it's too short to possibly contain a MAC. | ||
420 | */ | ||
421 | if (orig_len < mac_size || | ||
422 | /* CBC records must have a padding length byte too. */ | ||
423 | (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE && | ||
424 | orig_len < mac_size + 1)) { | ||
425 | al = SSL_AD_DECODE_ERROR; | ||
426 | SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT); | ||
427 | goto f_err; | ||
428 | } | ||
429 | |||
430 | if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) { | ||
431 | /* We update the length so that the TLS header bytes | ||
432 | * can be constructed correctly but we need to extract | ||
433 | * the MAC in constant time from within the record, | ||
434 | * without leaking the contents of the padding bytes. | ||
435 | * */ | ||
436 | mac = mac_tmp; | ||
437 | ssl3_cbc_copy_mac(mac_tmp, rr, mac_size, orig_len); | ||
438 | rr->length -= mac_size; | ||
439 | } else { | ||
440 | /* In this case there's no padding, so |orig_len| | ||
441 | * equals |rec->length| and we checked that there's | ||
442 | * enough bytes for |mac_size| above. */ | ||
443 | rr->length -= mac_size; | ||
444 | mac = &rr->data[rr->length]; | ||
445 | } | ||
446 | |||
447 | i = s->method->ssl3_enc->mac(s,md,0 /* not send */); | ||
448 | if (i < 0 || mac == NULL || | ||
449 | timingsafe_memcmp(md, mac, (size_t)mac_size) != 0) | ||
450 | enc_err = -1; | ||
451 | if (rr->length > | ||
452 | SSL3_RT_MAX_COMPRESSED_LENGTH + extra + mac_size) | ||
453 | enc_err = -1; | ||
454 | } | ||
455 | |||
456 | if (enc_err < 0) { | ||
457 | /* | ||
458 | * A separate 'decryption_failed' alert was introduced with | ||
459 | * TLS 1.0, SSL 3.0 only has 'bad_record_mac'. But unless a | ||
460 | * decryption failure is directly visible from the ciphertext | ||
461 | * anyway, we should not reveal which kind of error | ||
462 | * occurred -- this might become visible to an attacker | ||
463 | * (e.g. via a logfile) | ||
464 | */ | ||
465 | al = SSL_AD_BAD_RECORD_MAC; | ||
466 | SSLerr(SSL_F_SSL3_GET_RECORD, | ||
467 | SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); | ||
468 | goto f_err; | ||
469 | } | ||
470 | |||
471 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) { | ||
472 | al = SSL_AD_RECORD_OVERFLOW; | ||
473 | SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG); | ||
474 | goto f_err; | ||
475 | } | ||
476 | |||
477 | rr->off = 0; | ||
478 | /* | ||
479 | * So at this point the following is true | ||
480 | * | ||
481 | * ssl->s3->rrec.type is the type of record | ||
482 | * ssl->s3->rrec.length == number of bytes in record | ||
483 | * ssl->s3->rrec.off == offset to first valid byte | ||
484 | * ssl->s3->rrec.data == where to take bytes from, increment | ||
485 | * after use :-). | ||
486 | */ | ||
487 | |||
488 | /* we have pulled in a full packet so zero things */ | ||
489 | s->packet_length = 0; | ||
490 | |||
491 | /* just read a 0 length packet */ | ||
492 | if (rr->length == 0) | ||
493 | goto again; | ||
494 | |||
495 | return (1); | ||
496 | |||
497 | f_err: | ||
498 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
499 | err: | ||
500 | return (ret); | ||
501 | } | ||
502 | |||
503 | /* Call this to write data in records of type 'type' | ||
504 | * It will return <= 0 if not all data has been sent or non-blocking IO. | ||
505 | */ | ||
506 | int | ||
507 | ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) | ||
508 | { | ||
509 | const unsigned char *buf = buf_; | ||
510 | unsigned int tot, n, nw; | ||
511 | int i; | ||
512 | |||
513 | if (len < 0) { | ||
514 | SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR); | ||
515 | return -1; | ||
516 | } | ||
517 | |||
518 | s->rwstate = SSL_NOTHING; | ||
519 | tot = s->s3->wnum; | ||
520 | s->s3->wnum = 0; | ||
521 | |||
522 | if (SSL_in_init(s) && !s->in_handshake) { | ||
523 | i = s->handshake_func(s); | ||
524 | if (i < 0) | ||
525 | return (i); | ||
526 | if (i == 0) { | ||
527 | SSLerr(SSL_F_SSL3_WRITE_BYTES, | ||
528 | SSL_R_SSL_HANDSHAKE_FAILURE); | ||
529 | return -1; | ||
530 | } | ||
531 | } | ||
532 | |||
533 | if (len < tot) | ||
534 | len = tot; | ||
535 | n = (len - tot); | ||
536 | for (;;) { | ||
537 | if (n > s->max_send_fragment) | ||
538 | nw = s->max_send_fragment; | ||
539 | else | ||
540 | nw = n; | ||
541 | |||
542 | i = do_ssl3_write(s, type, &(buf[tot]), nw, 0); | ||
543 | if (i <= 0) { | ||
544 | s->s3->wnum = tot; | ||
545 | return i; | ||
546 | } | ||
547 | |||
548 | if ((i == (int)n) || (type == SSL3_RT_APPLICATION_DATA && | ||
549 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { | ||
550 | /* | ||
551 | * Next chunk of data should get another prepended | ||
552 | * empty fragment in ciphersuites with known-IV | ||
553 | * weakness. | ||
554 | */ | ||
555 | s->s3->empty_fragment_done = 0; | ||
556 | |||
557 | return tot + i; | ||
558 | } | ||
559 | |||
560 | n -= i; | ||
561 | tot += i; | ||
562 | } | ||
563 | } | ||
564 | |||
565 | static int | ||
566 | do_ssl3_write(SSL *s, int type, const unsigned char *buf, | ||
567 | unsigned int len, int create_empty_fragment) | ||
568 | { | ||
569 | unsigned char *p, *plen; | ||
570 | int i, mac_size, clear = 0; | ||
571 | int prefix_len = 0; | ||
572 | int eivlen; | ||
573 | size_t align; | ||
574 | SSL3_RECORD *wr; | ||
575 | SSL3_BUFFER *wb = &(s->s3->wbuf); | ||
576 | SSL_SESSION *sess; | ||
577 | |||
578 | if (wb->buf == NULL) | ||
579 | if (!ssl3_setup_write_buffer(s)) | ||
580 | return -1; | ||
581 | |||
582 | /* first check if there is a SSL3_BUFFER still being written | ||
583 | * out. This will happen with non blocking IO */ | ||
584 | if (wb->left != 0) | ||
585 | return (ssl3_write_pending(s, type, buf, len)); | ||
586 | |||
587 | /* If we have an alert to send, lets send it */ | ||
588 | if (s->s3->alert_dispatch) { | ||
589 | i = s->method->ssl_dispatch_alert(s); | ||
590 | if (i <= 0) | ||
591 | return (i); | ||
592 | /* if it went, fall through and send more stuff */ | ||
593 | /* we may have released our buffer, so get it again */ | ||
594 | if (wb->buf == NULL) | ||
595 | if (!ssl3_setup_write_buffer(s)) | ||
596 | return -1; | ||
597 | } | ||
598 | |||
599 | if (len == 0 && !create_empty_fragment) | ||
600 | return 0; | ||
601 | |||
602 | wr = &(s->s3->wrec); | ||
603 | sess = s->session; | ||
604 | |||
605 | if ((sess == NULL) || (s->enc_write_ctx == NULL) || | ||
606 | (EVP_MD_CTX_md(s->write_hash) == NULL)) { | ||
607 | clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */ | ||
608 | mac_size = 0; | ||
609 | } else { | ||
610 | mac_size = EVP_MD_CTX_size(s->write_hash); | ||
611 | if (mac_size < 0) | ||
612 | goto err; | ||
613 | } | ||
614 | |||
615 | /* | ||
616 | * 'create_empty_fragment' is true only when this function calls | ||
617 | * itself. | ||
618 | */ | ||
619 | if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) { | ||
620 | /* | ||
621 | * Countermeasure against known-IV weakness in CBC ciphersuites | ||
622 | * (see http://www.openssl.org/~bodo/tls-cbc.txt) | ||
623 | */ | ||
624 | if (s->s3->need_empty_fragments && | ||
625 | type == SSL3_RT_APPLICATION_DATA) { | ||
626 | /* recursive function call with 'create_empty_fragment' set; | ||
627 | * this prepares and buffers the data for an empty fragment | ||
628 | * (these 'prefix_len' bytes are sent out later | ||
629 | * together with the actual payload) */ | ||
630 | prefix_len = do_ssl3_write(s, type, buf, 0, 1); | ||
631 | if (prefix_len <= 0) | ||
632 | goto err; | ||
633 | |||
634 | if (prefix_len > | ||
635 | (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) { | ||
636 | /* insufficient space */ | ||
637 | SSLerr(SSL_F_DO_SSL3_WRITE, | ||
638 | ERR_R_INTERNAL_ERROR); | ||
639 | goto err; | ||
640 | } | ||
641 | } | ||
642 | |||
643 | s->s3->empty_fragment_done = 1; | ||
644 | } | ||
645 | |||
646 | if (create_empty_fragment) { | ||
647 | /* extra fragment would be couple of cipher blocks, | ||
648 | * which would be multiple of SSL3_ALIGN_PAYLOAD, so | ||
649 | * if we want to align the real payload, then we can | ||
650 | * just pretent we simply have two headers. */ | ||
651 | align = (size_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH; | ||
652 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); | ||
653 | |||
654 | p = wb->buf + align; | ||
655 | wb->offset = align; | ||
656 | } else if (prefix_len) { | ||
657 | p = wb->buf + wb->offset + prefix_len; | ||
658 | } else { | ||
659 | align = (size_t)wb->buf + SSL3_RT_HEADER_LENGTH; | ||
660 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); | ||
661 | |||
662 | p = wb->buf + align; | ||
663 | wb->offset = align; | ||
664 | } | ||
665 | |||
666 | /* write the header */ | ||
667 | |||
668 | *(p++) = type&0xff; | ||
669 | wr->type = type; | ||
670 | |||
671 | *(p++) = (s->version >> 8); | ||
672 | /* Some servers hang if iniatial client hello is larger than 256 | ||
673 | * bytes and record version number > TLS 1.0 | ||
674 | */ | ||
675 | if (s->state == SSL3_ST_CW_CLNT_HELLO_B && !s->renegotiate && | ||
676 | TLS1_get_version(s) > TLS1_VERSION) | ||
677 | *(p++) = 0x1; | ||
678 | else | ||
679 | *(p++) = s->version&0xff; | ||
680 | |||
681 | /* field where we are to write out packet length */ | ||
682 | plen = p; | ||
683 | p += 2; | ||
684 | |||
685 | /* Explicit IV length. */ | ||
686 | if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) { | ||
687 | int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx); | ||
688 | if (mode == EVP_CIPH_CBC_MODE) { | ||
689 | eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx); | ||
690 | if (eivlen <= 1) | ||
691 | eivlen = 0; | ||
692 | } | ||
693 | /* Need explicit part of IV for GCM mode */ | ||
694 | else if (mode == EVP_CIPH_GCM_MODE) | ||
695 | eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN; | ||
696 | else | ||
697 | eivlen = 0; | ||
698 | } else if (s->aead_write_ctx != NULL && | ||
699 | s->aead_write_ctx->variable_nonce_in_record) { | ||
700 | eivlen = s->aead_write_ctx->variable_nonce_len; | ||
701 | } else | ||
702 | eivlen = 0; | ||
703 | |||
704 | /* lets setup the record stuff. */ | ||
705 | wr->data = p + eivlen; | ||
706 | wr->length = (int)len; | ||
707 | wr->input = (unsigned char *)buf; | ||
708 | |||
709 | /* we now 'read' from wr->input, wr->length bytes into wr->data */ | ||
710 | |||
711 | memcpy(wr->data, wr->input, wr->length); | ||
712 | wr->input = wr->data; | ||
713 | |||
714 | /* we should still have the output to wr->data and the input | ||
715 | * from wr->input. Length should be wr->length. | ||
716 | * wr->data still points in the wb->buf */ | ||
717 | |||
718 | if (mac_size != 0) { | ||
719 | if (s->method->ssl3_enc->mac(s, | ||
720 | &(p[wr->length + eivlen]), 1) < 0) | ||
721 | goto err; | ||
722 | wr->length += mac_size; | ||
723 | } | ||
724 | |||
725 | wr->input = p; | ||
726 | wr->data = p; | ||
727 | |||
728 | if (eivlen) { | ||
729 | /* if (RAND_pseudo_bytes(p, eivlen) <= 0) | ||
730 | goto err; | ||
731 | */ | ||
732 | wr->length += eivlen; | ||
733 | } | ||
734 | |||
735 | /* ssl3_enc can only have an error on read */ | ||
736 | s->method->ssl3_enc->enc(s, 1); | ||
737 | |||
738 | /* record length after mac and block padding */ | ||
739 | s2n(wr->length, plen); | ||
740 | |||
741 | /* we should now have | ||
742 | * wr->data pointing to the encrypted data, which is | ||
743 | * wr->length long */ | ||
744 | wr->type=type; /* not needed but helps for debugging */ | ||
745 | wr->length += SSL3_RT_HEADER_LENGTH; | ||
746 | |||
747 | if (create_empty_fragment) { | ||
748 | /* we are in a recursive call; | ||
749 | * just return the length, don't write out anything here | ||
750 | */ | ||
751 | return wr->length; | ||
752 | } | ||
753 | |||
754 | /* now let's set up wb */ | ||
755 | wb->left = prefix_len + wr->length; | ||
756 | |||
757 | /* memorize arguments so that ssl3_write_pending can detect | ||
758 | * bad write retries later */ | ||
759 | s->s3->wpend_tot = len; | ||
760 | s->s3->wpend_buf = buf; | ||
761 | s->s3->wpend_type = type; | ||
762 | s->s3->wpend_ret = len; | ||
763 | |||
764 | /* we now just need to write the buffer */ | ||
765 | return ssl3_write_pending(s, type, buf, len); | ||
766 | err: | ||
767 | return -1; | ||
768 | } | ||
769 | |||
770 | /* if s->s3->wbuf.left != 0, we need to call this */ | ||
771 | int | ||
772 | ssl3_write_pending(SSL *s, int type, const unsigned char *buf, unsigned int len) | ||
773 | { | ||
774 | int i; | ||
775 | SSL3_BUFFER *wb = &(s->s3->wbuf); | ||
776 | |||
777 | /* XXXX */ | ||
778 | if ((s->s3->wpend_tot > (int)len) || ((s->s3->wpend_buf != buf) && | ||
779 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || | ||
780 | (s->s3->wpend_type != type)) { | ||
781 | SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY); | ||
782 | return (-1); | ||
783 | } | ||
784 | |||
785 | for (;;) { | ||
786 | errno = 0; | ||
787 | if (s->wbio != NULL) { | ||
788 | s->rwstate = SSL_WRITING; | ||
789 | i = BIO_write(s->wbio, | ||
790 | (char *)&(wb->buf[wb->offset]), | ||
791 | (unsigned int)wb->left); | ||
792 | } else { | ||
793 | SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET); | ||
794 | i = -1; | ||
795 | } | ||
796 | if (i == wb->left) { | ||
797 | wb->left = 0; | ||
798 | wb->offset += i; | ||
799 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
800 | !SSL_IS_DTLS(s)) | ||
801 | ssl3_release_write_buffer(s); | ||
802 | s->rwstate = SSL_NOTHING; | ||
803 | return (s->s3->wpend_ret); | ||
804 | } else if (i <= 0) { | ||
805 | /* | ||
806 | * For DTLS, just drop it. That's kind of the | ||
807 | * whole point in using a datagram service. | ||
808 | */ | ||
809 | if (SSL_IS_DTLS(s)) | ||
810 | wb->left = 0; | ||
811 | return (i); | ||
812 | } | ||
813 | wb->offset += i; | ||
814 | wb->left -= i; | ||
815 | } | ||
816 | } | ||
817 | |||
818 | /* Return up to 'len' payload bytes received in 'type' records. | ||
819 | * 'type' is one of the following: | ||
820 | * | ||
821 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | ||
822 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | ||
823 | * - 0 (during a shutdown, no data has to be returned) | ||
824 | * | ||
825 | * If we don't have stored data to work from, read a SSL/TLS record first | ||
826 | * (possibly multiple records if we still don't have anything to return). | ||
827 | * | ||
828 | * This function must handle any surprises the peer may have for us, such as | ||
829 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really | ||
830 | * a surprise, but handled as if it were), or renegotiation requests. | ||
831 | * Also if record payloads contain fragments too small to process, we store | ||
832 | * them until there is enough for the respective protocol (the record protocol | ||
833 | * may use arbitrary fragmentation and even interleaving): | ||
834 | * Change cipher spec protocol | ||
835 | * just 1 byte needed, no need for keeping anything stored | ||
836 | * Alert protocol | ||
837 | * 2 bytes needed (AlertLevel, AlertDescription) | ||
838 | * Handshake protocol | ||
839 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | ||
840 | * to detect unexpected Client Hello and Hello Request messages | ||
841 | * here, anything else is handled by higher layers | ||
842 | * Application data protocol | ||
843 | * none of our business | ||
844 | */ | ||
845 | int | ||
846 | ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) | ||
847 | { | ||
848 | int al, i, j, ret; | ||
849 | unsigned int n; | ||
850 | SSL3_RECORD *rr; | ||
851 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; | ||
852 | |||
853 | if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ | ||
854 | if (!ssl3_setup_read_buffer(s)) | ||
855 | return (-1); | ||
856 | |||
857 | if (len < 0) { | ||
858 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
859 | return -1; | ||
860 | } | ||
861 | |||
862 | if ((type && type != SSL3_RT_APPLICATION_DATA && | ||
863 | type != SSL3_RT_HANDSHAKE) || | ||
864 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { | ||
865 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
866 | return -1; | ||
867 | } | ||
868 | |||
869 | if ((type == SSL3_RT_HANDSHAKE) && | ||
870 | (s->s3->handshake_fragment_len > 0)) { | ||
871 | /* (partially) satisfy request from storage */ | ||
872 | unsigned char *src = s->s3->handshake_fragment; | ||
873 | unsigned char *dst = buf; | ||
874 | unsigned int k; | ||
875 | |||
876 | /* peek == 0 */ | ||
877 | n = 0; | ||
878 | while ((len > 0) && (s->s3->handshake_fragment_len > 0)) { | ||
879 | *dst++ = *src++; | ||
880 | len--; | ||
881 | s->s3->handshake_fragment_len--; | ||
882 | n++; | ||
883 | } | ||
884 | /* move any remaining fragment bytes: */ | ||
885 | for (k = 0; k < s->s3->handshake_fragment_len; k++) | ||
886 | s->s3->handshake_fragment[k] = *src++; | ||
887 | return n; | ||
888 | } | ||
889 | |||
890 | /* | ||
891 | * Now s->s3->handshake_fragment_len == 0 if | ||
892 | * type == SSL3_RT_HANDSHAKE. | ||
893 | */ | ||
894 | if (!s->in_handshake && SSL_in_init(s)) { | ||
895 | /* type == SSL3_RT_APPLICATION_DATA */ | ||
896 | i = s->handshake_func(s); | ||
897 | if (i < 0) | ||
898 | return (i); | ||
899 | if (i == 0) { | ||
900 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
901 | SSL_R_SSL_HANDSHAKE_FAILURE); | ||
902 | return (-1); | ||
903 | } | ||
904 | } | ||
905 | start: | ||
906 | s->rwstate = SSL_NOTHING; | ||
907 | |||
908 | /* | ||
909 | * s->s3->rrec.type - is the type of record | ||
910 | * s->s3->rrec.data, - data | ||
911 | * s->s3->rrec.off, - offset into 'data' for next read | ||
912 | * s->s3->rrec.length, - number of bytes. | ||
913 | */ | ||
914 | rr = &(s->s3->rrec); | ||
915 | |||
916 | /* get new packet if necessary */ | ||
917 | if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) { | ||
918 | ret = ssl3_get_record(s); | ||
919 | if (ret <= 0) | ||
920 | return (ret); | ||
921 | } | ||
922 | |||
923 | /* we now have a packet which can be read and processed */ | ||
924 | |||
925 | if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, | ||
926 | * reset by ssl3_get_finished */ | ||
927 | && (rr->type != SSL3_RT_HANDSHAKE)) { | ||
928 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
929 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
930 | SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); | ||
931 | goto f_err; | ||
932 | } | ||
933 | |||
934 | /* If the other end has shut down, throw anything we read away | ||
935 | * (even in 'peek' mode) */ | ||
936 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | ||
937 | rr->length = 0; | ||
938 | s->rwstate = SSL_NOTHING; | ||
939 | return (0); | ||
940 | } | ||
941 | |||
942 | |||
943 | /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ | ||
944 | if (type == rr->type) { | ||
945 | /* make sure that we are not getting application data when we | ||
946 | * are doing a handshake for the first time */ | ||
947 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && | ||
948 | (s->enc_read_ctx == NULL)) { | ||
949 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
950 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
951 | SSL_R_APP_DATA_IN_HANDSHAKE); | ||
952 | goto f_err; | ||
953 | } | ||
954 | |||
955 | if (len <= 0) | ||
956 | return (len); | ||
957 | |||
958 | if ((unsigned int)len > rr->length) | ||
959 | n = rr->length; | ||
960 | else | ||
961 | n = (unsigned int)len; | ||
962 | |||
963 | memcpy(buf, &(rr->data[rr->off]), n); | ||
964 | if (!peek) { | ||
965 | rr->length -= n; | ||
966 | rr->off += n; | ||
967 | if (rr->length == 0) { | ||
968 | s->rstate = SSL_ST_READ_HEADER; | ||
969 | rr->off = 0; | ||
970 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
971 | s->s3->rbuf.left == 0) | ||
972 | ssl3_release_read_buffer(s); | ||
973 | } | ||
974 | } | ||
975 | return (n); | ||
976 | } | ||
977 | |||
978 | |||
979 | /* If we get here, then type != rr->type; if we have a handshake | ||
980 | * message, then it was unexpected (Hello Request or Client Hello). */ | ||
981 | |||
982 | { | ||
983 | /* | ||
984 | * In case of record types for which we have 'fragment' | ||
985 | * storage, * fill that so that we can process the data | ||
986 | * at a fixed place. | ||
987 | */ | ||
988 | unsigned int dest_maxlen = 0; | ||
989 | unsigned char *dest = NULL; | ||
990 | unsigned int *dest_len = NULL; | ||
991 | |||
992 | if (rr->type == SSL3_RT_HANDSHAKE) { | ||
993 | dest_maxlen = sizeof s->s3->handshake_fragment; | ||
994 | dest = s->s3->handshake_fragment; | ||
995 | dest_len = &s->s3->handshake_fragment_len; | ||
996 | } else if (rr->type == SSL3_RT_ALERT) { | ||
997 | dest_maxlen = sizeof s->s3->alert_fragment; | ||
998 | dest = s->s3->alert_fragment; | ||
999 | dest_len = &s->s3->alert_fragment_len; | ||
1000 | } | ||
1001 | if (dest_maxlen > 0) { | ||
1002 | /* available space in 'dest' */ | ||
1003 | n = dest_maxlen - *dest_len; | ||
1004 | if (rr->length < n) | ||
1005 | n = rr->length; /* available bytes */ | ||
1006 | |||
1007 | /* now move 'n' bytes: */ | ||
1008 | while (n-- > 0) { | ||
1009 | dest[(*dest_len)++] = rr->data[rr->off++]; | ||
1010 | rr->length--; | ||
1011 | } | ||
1012 | |||
1013 | if (*dest_len < dest_maxlen) | ||
1014 | goto start; /* fragment was too small */ | ||
1015 | } | ||
1016 | } | ||
1017 | |||
1018 | /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; | ||
1019 | * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT. | ||
1020 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ | ||
1021 | |||
1022 | /* If we are a client, check for an incoming 'Hello Request': */ | ||
1023 | if ((!s->server) && (s->s3->handshake_fragment_len >= 4) && | ||
1024 | (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && | ||
1025 | (s->session != NULL) && (s->session->cipher != NULL)) { | ||
1026 | s->s3->handshake_fragment_len = 0; | ||
1027 | |||
1028 | if ((s->s3->handshake_fragment[1] != 0) || | ||
1029 | (s->s3->handshake_fragment[2] != 0) || | ||
1030 | (s->s3->handshake_fragment[3] != 0)) { | ||
1031 | al = SSL_AD_DECODE_ERROR; | ||
1032 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST); | ||
1033 | goto f_err; | ||
1034 | } | ||
1035 | |||
1036 | if (s->msg_callback) | ||
1037 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, | ||
1038 | s->s3->handshake_fragment, 4, s, | ||
1039 | s->msg_callback_arg); | ||
1040 | |||
1041 | if (SSL_is_init_finished(s) && | ||
1042 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && | ||
1043 | !s->s3->renegotiate) { | ||
1044 | ssl3_renegotiate(s); | ||
1045 | if (ssl3_renegotiate_check(s)) { | ||
1046 | i = s->handshake_func(s); | ||
1047 | if (i < 0) | ||
1048 | return (i); | ||
1049 | if (i == 0) { | ||
1050 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1051 | SSL_R_SSL_HANDSHAKE_FAILURE); | ||
1052 | return (-1); | ||
1053 | } | ||
1054 | |||
1055 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | ||
1056 | if (s->s3->rbuf.left == 0) { | ||
1057 | /* no read-ahead left? */ | ||
1058 | BIO *bio; | ||
1059 | /* In the case where we try to read application data, | ||
1060 | * but we trigger an SSL handshake, we return -1 with | ||
1061 | * the retry option set. Otherwise renegotiation may | ||
1062 | * cause nasty problems in the blocking world */ | ||
1063 | s->rwstate = SSL_READING; | ||
1064 | bio = SSL_get_rbio(s); | ||
1065 | BIO_clear_retry_flags(bio); | ||
1066 | BIO_set_retry_read(bio); | ||
1067 | return (-1); | ||
1068 | } | ||
1069 | } | ||
1070 | } | ||
1071 | } | ||
1072 | /* we either finished a handshake or ignored the request, | ||
1073 | * now try again to obtain the (application) data we were asked for */ | ||
1074 | goto start; | ||
1075 | } | ||
1076 | /* If we are a server and get a client hello when renegotiation isn't | ||
1077 | * allowed send back a no renegotiation alert and carry on. | ||
1078 | * WARNING: experimental code, needs reviewing (steve) | ||
1079 | */ | ||
1080 | if (s->server && | ||
1081 | SSL_is_init_finished(s) && | ||
1082 | !s->s3->send_connection_binding && | ||
1083 | (s->version > SSL3_VERSION) && | ||
1084 | (s->s3->handshake_fragment_len >= 4) && | ||
1085 | (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) && | ||
1086 | (s->session != NULL) && (s->session->cipher != NULL)) { | ||
1087 | /*s->s3->handshake_fragment_len = 0;*/ | ||
1088 | rr->length = 0; | ||
1089 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); | ||
1090 | goto start; | ||
1091 | } | ||
1092 | if (s->s3->alert_fragment_len >= 2) { | ||
1093 | int alert_level = s->s3->alert_fragment[0]; | ||
1094 | int alert_descr = s->s3->alert_fragment[1]; | ||
1095 | |||
1096 | s->s3->alert_fragment_len = 0; | ||
1097 | |||
1098 | if (s->msg_callback) | ||
1099 | s->msg_callback(0, s->version, SSL3_RT_ALERT, | ||
1100 | s->s3->alert_fragment, 2, s, s->msg_callback_arg); | ||
1101 | |||
1102 | if (s->info_callback != NULL) | ||
1103 | cb = s->info_callback; | ||
1104 | else if (s->ctx->info_callback != NULL) | ||
1105 | cb = s->ctx->info_callback; | ||
1106 | |||
1107 | if (cb != NULL) { | ||
1108 | j = (alert_level << 8) | alert_descr; | ||
1109 | cb(s, SSL_CB_READ_ALERT, j); | ||
1110 | } | ||
1111 | |||
1112 | if (alert_level == 1) { | ||
1113 | /* warning */ | ||
1114 | s->s3->warn_alert = alert_descr; | ||
1115 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { | ||
1116 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | ||
1117 | return (0); | ||
1118 | } | ||
1119 | /* This is a warning but we receive it if we requested | ||
1120 | * renegotiation and the peer denied it. Terminate with | ||
1121 | * a fatal alert because if application tried to | ||
1122 | * renegotiatie it presumably had a good reason and | ||
1123 | * expects it to succeed. | ||
1124 | * | ||
1125 | * In future we might have a renegotiation where we | ||
1126 | * don't care if the peer refused it where we carry on. | ||
1127 | */ | ||
1128 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { | ||
1129 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
1130 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1131 | SSL_R_NO_RENEGOTIATION); | ||
1132 | goto f_err; | ||
1133 | } | ||
1134 | } else if (alert_level == 2) { | ||
1135 | /* fatal */ | ||
1136 | s->rwstate = SSL_NOTHING; | ||
1137 | s->s3->fatal_alert = alert_descr; | ||
1138 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1139 | SSL_AD_REASON_OFFSET + alert_descr); | ||
1140 | ERR_asprintf_error_data("SSL alert number %d", | ||
1141 | alert_descr); | ||
1142 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | ||
1143 | SSL_CTX_remove_session(s->ctx, s->session); | ||
1144 | return (0); | ||
1145 | } else { | ||
1146 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
1147 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE); | ||
1148 | goto f_err; | ||
1149 | } | ||
1150 | |||
1151 | goto start; | ||
1152 | } | ||
1153 | |||
1154 | if (s->shutdown & SSL_SENT_SHUTDOWN) { | ||
1155 | /* but we have not received a shutdown */ | ||
1156 | s->rwstate = SSL_NOTHING; | ||
1157 | rr->length = 0; | ||
1158 | return (0); | ||
1159 | } | ||
1160 | |||
1161 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { | ||
1162 | /* 'Change Cipher Spec' is just a single byte, so we know | ||
1163 | * exactly what the record payload has to look like */ | ||
1164 | if ((rr->length != 1) || (rr->off != 0) || | ||
1165 | (rr->data[0] != SSL3_MT_CCS)) { | ||
1166 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
1167 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1168 | SSL_R_BAD_CHANGE_CIPHER_SPEC); | ||
1169 | goto f_err; | ||
1170 | } | ||
1171 | |||
1172 | /* Check we have a cipher to change to */ | ||
1173 | if (s->s3->tmp.new_cipher == NULL) { | ||
1174 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1175 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1176 | SSL_R_CCS_RECEIVED_EARLY); | ||
1177 | goto f_err; | ||
1178 | } | ||
1179 | |||
1180 | /* Check that we should be receiving a Change Cipher Spec. */ | ||
1181 | if (!(s->s3->flags & SSL3_FLAGS_CCS_OK)) { | ||
1182 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1183 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1184 | SSL_R_CCS_RECEIVED_EARLY); | ||
1185 | goto f_err; | ||
1186 | } | ||
1187 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; | ||
1188 | |||
1189 | rr->length = 0; | ||
1190 | |||
1191 | if (s->msg_callback) { | ||
1192 | s->msg_callback(0, s->version, | ||
1193 | SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, | ||
1194 | s->msg_callback_arg); | ||
1195 | } | ||
1196 | |||
1197 | s->s3->change_cipher_spec = 1; | ||
1198 | if (!ssl3_do_change_cipher_spec(s)) | ||
1199 | goto err; | ||
1200 | else | ||
1201 | goto start; | ||
1202 | } | ||
1203 | |||
1204 | /* Unexpected handshake message (Client Hello, or protocol violation) */ | ||
1205 | if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) { | ||
1206 | if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && | ||
1207 | !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) { | ||
1208 | s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; | ||
1209 | s->renegotiate = 1; | ||
1210 | s->new_session = 1; | ||
1211 | } | ||
1212 | i = s->handshake_func(s); | ||
1213 | if (i < 0) | ||
1214 | return (i); | ||
1215 | if (i == 0) { | ||
1216 | SSLerr(SSL_F_SSL3_READ_BYTES, | ||
1217 | SSL_R_SSL_HANDSHAKE_FAILURE); | ||
1218 | return (-1); | ||
1219 | } | ||
1220 | |||
1221 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | ||
1222 | if (s->s3->rbuf.left == 0) { /* no read-ahead left? */ | ||
1223 | BIO *bio; | ||
1224 | /* In the case where we try to read application data, | ||
1225 | * but we trigger an SSL handshake, we return -1 with | ||
1226 | * the retry option set. Otherwise renegotiation may | ||
1227 | * cause nasty problems in the blocking world */ | ||
1228 | s->rwstate = SSL_READING; | ||
1229 | bio = SSL_get_rbio(s); | ||
1230 | BIO_clear_retry_flags(bio); | ||
1231 | BIO_set_retry_read(bio); | ||
1232 | return (-1); | ||
1233 | } | ||
1234 | } | ||
1235 | goto start; | ||
1236 | } | ||
1237 | |||
1238 | switch (rr->type) { | ||
1239 | default: | ||
1240 | /* | ||
1241 | * TLS up to v1.1 just ignores unknown message types: | ||
1242 | * TLS v1.2 give an unexpected message alert. | ||
1243 | */ | ||
1244 | if (s->version >= TLS1_VERSION && | ||
1245 | s->version <= TLS1_1_VERSION) { | ||
1246 | rr->length = 0; | ||
1247 | goto start; | ||
1248 | } | ||
1249 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1250 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | ||
1251 | goto f_err; | ||
1252 | case SSL3_RT_CHANGE_CIPHER_SPEC: | ||
1253 | case SSL3_RT_ALERT: | ||
1254 | case SSL3_RT_HANDSHAKE: | ||
1255 | /* we already handled all of these, with the possible exception | ||
1256 | * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that | ||
1257 | * should not happen when type != rr->type */ | ||
1258 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1259 | SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); | ||
1260 | goto f_err; | ||
1261 | case SSL3_RT_APPLICATION_DATA: | ||
1262 | /* At this point, we were expecting handshake data, | ||
1263 | * but have application data. If the library was | ||
1264 | * running inside ssl3_read() (i.e. in_read_app_data | ||
1265 | * is set) and it makes sense to read application data | ||
1266 | * at this point (session renegotiation not yet started), | ||
1267 | * we will indulge it. | ||
1268 | */ | ||
1269 | if (s->s3->in_read_app_data && | ||
1270 | (s->s3->total_renegotiations != 0) && | ||
1271 | (((s->state & SSL_ST_CONNECT) && | ||
1272 | (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && | ||
1273 | (s->state <= SSL3_ST_CR_SRVR_HELLO_A)) || | ||
1274 | ((s->state & SSL_ST_ACCEPT) && | ||
1275 | (s->state <= SSL3_ST_SW_HELLO_REQ_A) && | ||
1276 | (s->state >= SSL3_ST_SR_CLNT_HELLO_A)))) { | ||
1277 | s->s3->in_read_app_data = 2; | ||
1278 | return (-1); | ||
1279 | } else { | ||
1280 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
1281 | SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD); | ||
1282 | goto f_err; | ||
1283 | } | ||
1284 | } | ||
1285 | /* not reached */ | ||
1286 | |||
1287 | f_err: | ||
1288 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1289 | err: | ||
1290 | return (-1); | ||
1291 | } | ||
1292 | |||
1293 | int | ||
1294 | ssl3_do_change_cipher_spec(SSL *s) | ||
1295 | { | ||
1296 | int i; | ||
1297 | const char *sender; | ||
1298 | int slen; | ||
1299 | |||
1300 | if (s->state & SSL_ST_ACCEPT) | ||
1301 | i = SSL3_CHANGE_CIPHER_SERVER_READ; | ||
1302 | else | ||
1303 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; | ||
1304 | |||
1305 | if (s->s3->tmp.key_block == NULL) { | ||
1306 | if (s->session == NULL || s->session->master_key_length == 0) { | ||
1307 | /* might happen if dtls1_read_bytes() calls this */ | ||
1308 | SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, | ||
1309 | SSL_R_CCS_RECEIVED_EARLY); | ||
1310 | return (0); | ||
1311 | } | ||
1312 | |||
1313 | s->session->cipher = s->s3->tmp.new_cipher; | ||
1314 | if (!s->method->ssl3_enc->setup_key_block(s)) | ||
1315 | return (0); | ||
1316 | } | ||
1317 | |||
1318 | if (!s->method->ssl3_enc->change_cipher_state(s, i)) | ||
1319 | return (0); | ||
1320 | |||
1321 | /* we have to record the message digest at | ||
1322 | * this point so we can get it before we read | ||
1323 | * the finished message */ | ||
1324 | if (s->state & SSL_ST_CONNECT) { | ||
1325 | sender = s->method->ssl3_enc->server_finished_label; | ||
1326 | slen = s->method->ssl3_enc->server_finished_label_len; | ||
1327 | } else { | ||
1328 | sender = s->method->ssl3_enc->client_finished_label; | ||
1329 | slen = s->method->ssl3_enc->client_finished_label_len; | ||
1330 | } | ||
1331 | |||
1332 | i = s->method->ssl3_enc->final_finish_mac(s, sender, slen, | ||
1333 | s->s3->tmp.peer_finish_md); | ||
1334 | if (i == 0) { | ||
1335 | SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR); | ||
1336 | return 0; | ||
1337 | } | ||
1338 | s->s3->tmp.peer_finish_md_len = i; | ||
1339 | |||
1340 | return (1); | ||
1341 | } | ||
1342 | |||
1343 | int | ||
1344 | ssl3_send_alert(SSL *s, int level, int desc) | ||
1345 | { | ||
1346 | /* Map tls/ssl alert value to correct one */ | ||
1347 | desc = s->method->ssl3_enc->alert_value(desc); | ||
1348 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) { | ||
1349 | /* SSL 3.0 does not have protocol_version alerts */ | ||
1350 | desc = SSL_AD_HANDSHAKE_FAILURE; | ||
1351 | } | ||
1352 | if (desc < 0) | ||
1353 | return -1; | ||
1354 | /* If a fatal one, remove from cache */ | ||
1355 | if ((level == 2) && (s->session != NULL)) | ||
1356 | SSL_CTX_remove_session(s->ctx, s->session); | ||
1357 | |||
1358 | s->s3->alert_dispatch = 1; | ||
1359 | s->s3->send_alert[0] = level; | ||
1360 | s->s3->send_alert[1] = desc; | ||
1361 | if (s->s3->wbuf.left == 0) /* data still being written out? */ | ||
1362 | return s->method->ssl_dispatch_alert(s); | ||
1363 | |||
1364 | /* else data is still being written out, we will get written | ||
1365 | * some time in the future */ | ||
1366 | return -1; | ||
1367 | } | ||
1368 | |||
1369 | int | ||
1370 | ssl3_dispatch_alert(SSL *s) | ||
1371 | { | ||
1372 | int i, j; | ||
1373 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
1374 | |||
1375 | s->s3->alert_dispatch = 0; | ||
1376 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0); | ||
1377 | if (i <= 0) { | ||
1378 | s->s3->alert_dispatch = 1; | ||
1379 | } else { | ||
1380 | /* Alert sent to BIO. If it is important, flush it now. | ||
1381 | * If the message does not get sent due to non-blocking IO, | ||
1382 | * we will not worry too much. */ | ||
1383 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) | ||
1384 | (void)BIO_flush(s->wbio); | ||
1385 | |||
1386 | if (s->msg_callback) | ||
1387 | s->msg_callback(1, s->version, SSL3_RT_ALERT, | ||
1388 | s->s3->send_alert, 2, s, s->msg_callback_arg); | ||
1389 | |||
1390 | if (s->info_callback != NULL) | ||
1391 | cb = s->info_callback; | ||
1392 | else if (s->ctx->info_callback != NULL) | ||
1393 | cb = s->ctx->info_callback; | ||
1394 | |||
1395 | if (cb != NULL) { | ||
1396 | j = (s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; | ||
1397 | cb(s, SSL_CB_WRITE_ALERT, j); | ||
1398 | } | ||
1399 | } | ||
1400 | return (i); | ||
1401 | } | ||