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