diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libssl/ssl_pkt.c | 1322 |
1 files changed, 0 insertions, 1322 deletions
diff --git a/src/lib/libssl/ssl_pkt.c b/src/lib/libssl/ssl_pkt.c deleted file mode 100644 index 7032175aac..0000000000 --- a/src/lib/libssl/ssl_pkt.c +++ /dev/null | |||
@@ -1,1322 +0,0 @@ | |||
1 | /* $OpenBSD: ssl_pkt.c,v 1.69 2025/03/12 14:03:55 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 <limits.h> | ||
114 | #include <stdio.h> | ||
115 | |||
116 | #include <openssl/buffer.h> | ||
117 | #include <openssl/evp.h> | ||
118 | |||
119 | #include "bytestring.h" | ||
120 | #include "dtls_local.h" | ||
121 | #include "ssl_local.h" | ||
122 | #include "tls_content.h" | ||
123 | |||
124 | static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, | ||
125 | unsigned int len); | ||
126 | static int ssl3_get_record(SSL *s); | ||
127 | |||
128 | /* | ||
129 | * Force a WANT_READ return for certain error conditions where | ||
130 | * we don't want to spin internally. | ||
131 | */ | ||
132 | void | ||
133 | ssl_force_want_read(SSL *s) | ||
134 | { | ||
135 | BIO *bio; | ||
136 | |||
137 | bio = SSL_get_rbio(s); | ||
138 | BIO_clear_retry_flags(bio); | ||
139 | BIO_set_retry_read(bio); | ||
140 | |||
141 | s->rwstate = SSL_READING; | ||
142 | } | ||
143 | |||
144 | /* | ||
145 | * If extend == 0, obtain new n-byte packet; if extend == 1, increase | ||
146 | * packet by another n bytes. | ||
147 | * The packet will be in the sub-array of s->s3->rbuf.buf specified | ||
148 | * by s->packet and s->packet_length. | ||
149 | * (If s->read_ahead is set, 'max' bytes may be stored in rbuf | ||
150 | * [plus s->packet_length bytes if extend == 1].) | ||
151 | */ | ||
152 | static int | ||
153 | ssl3_read_n(SSL *s, int n, int max, int extend) | ||
154 | { | ||
155 | SSL3_BUFFER_INTERNAL *rb = &(s->s3->rbuf); | ||
156 | int i, len, left; | ||
157 | size_t align; | ||
158 | unsigned char *pkt; | ||
159 | |||
160 | if (n <= 0) | ||
161 | return n; | ||
162 | |||
163 | if (rb->buf == NULL) { | ||
164 | if (!ssl3_setup_read_buffer(s)) | ||
165 | return -1; | ||
166 | } | ||
167 | if (rb->buf == NULL) | ||
168 | return -1; | ||
169 | |||
170 | left = rb->left; | ||
171 | align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH; | ||
172 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); | ||
173 | |||
174 | if (!extend) { | ||
175 | /* start with empty packet ... */ | ||
176 | if (left == 0) | ||
177 | rb->offset = align; | ||
178 | else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) { | ||
179 | /* check if next packet length is large | ||
180 | * enough to justify payload alignment... */ | ||
181 | pkt = rb->buf + rb->offset; | ||
182 | if (pkt[0] == SSL3_RT_APPLICATION_DATA && | ||
183 | (pkt[3]<<8|pkt[4]) >= 128) { | ||
184 | /* Note that even if packet is corrupted | ||
185 | * and its length field is insane, we can | ||
186 | * only be led to wrong decision about | ||
187 | * whether memmove will occur or not. | ||
188 | * Header values has no effect on memmove | ||
189 | * arguments and therefore no buffer | ||
190 | * overrun can be triggered. */ | ||
191 | memmove(rb->buf + align, pkt, left); | ||
192 | rb->offset = align; | ||
193 | } | ||
194 | } | ||
195 | s->packet = rb->buf + rb->offset; | ||
196 | s->packet_length = 0; | ||
197 | /* ... now we can act as if 'extend' was set */ | ||
198 | } | ||
199 | |||
200 | /* For DTLS/UDP reads should not span multiple packets | ||
201 | * because the read operation returns the whole packet | ||
202 | * at once (as long as it fits into the buffer). */ | ||
203 | if (SSL_is_dtls(s)) { | ||
204 | if (left > 0 && n > left) | ||
205 | n = left; | ||
206 | } | ||
207 | |||
208 | /* if there is enough in the buffer from a previous read, take some */ | ||
209 | if (left >= n) { | ||
210 | s->packet_length += n; | ||
211 | rb->left = left - n; | ||
212 | rb->offset += n; | ||
213 | return (n); | ||
214 | } | ||
215 | |||
216 | /* else we need to read more data */ | ||
217 | |||
218 | len = s->packet_length; | ||
219 | pkt = rb->buf + align; | ||
220 | /* Move any available bytes to front of buffer: | ||
221 | * 'len' bytes already pointed to by 'packet', | ||
222 | * 'left' extra ones at the end */ | ||
223 | if (s->packet != pkt) { | ||
224 | /* len > 0 */ | ||
225 | memmove(pkt, s->packet, len + left); | ||
226 | s->packet = pkt; | ||
227 | rb->offset = len + align; | ||
228 | } | ||
229 | |||
230 | if (n > (int)(rb->len - rb->offset)) { | ||
231 | /* does not happen */ | ||
232 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
233 | return -1; | ||
234 | } | ||
235 | |||
236 | if (s->read_ahead || SSL_is_dtls(s)) { | ||
237 | if (max < n) | ||
238 | max = n; | ||
239 | if (max > (int)(rb->len - rb->offset)) | ||
240 | max = rb->len - rb->offset; | ||
241 | } else { | ||
242 | /* ignore max parameter */ | ||
243 | max = n; | ||
244 | } | ||
245 | |||
246 | while (left < n) { | ||
247 | /* Now we have len+left bytes at the front of s->s3->rbuf.buf | ||
248 | * and need to read in more until we have len+n (up to | ||
249 | * len+max if possible) */ | ||
250 | |||
251 | errno = 0; | ||
252 | if (s->rbio != NULL) { | ||
253 | s->rwstate = SSL_READING; | ||
254 | i = BIO_read(s->rbio, pkt + len + left, max - left); | ||
255 | } else { | ||
256 | SSLerror(s, SSL_R_READ_BIO_NOT_SET); | ||
257 | i = -1; | ||
258 | } | ||
259 | |||
260 | if (i <= 0) { | ||
261 | rb->left = left; | ||
262 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
263 | !SSL_is_dtls(s)) { | ||
264 | if (len + left == 0) | ||
265 | ssl3_release_read_buffer(s); | ||
266 | } | ||
267 | return (i); | ||
268 | } | ||
269 | left += i; | ||
270 | |||
271 | /* | ||
272 | * reads should *never* span multiple packets for DTLS because | ||
273 | * the underlying transport protocol is message oriented as | ||
274 | * opposed to byte oriented as in the TLS case. | ||
275 | */ | ||
276 | if (SSL_is_dtls(s)) { | ||
277 | if (n > left) | ||
278 | n = left; /* makes the while condition false */ | ||
279 | } | ||
280 | } | ||
281 | |||
282 | /* done reading, now the book-keeping */ | ||
283 | rb->offset += n; | ||
284 | rb->left = left - n; | ||
285 | s->packet_length += n; | ||
286 | s->rwstate = SSL_NOTHING; | ||
287 | |||
288 | return (n); | ||
289 | } | ||
290 | |||
291 | int | ||
292 | ssl3_packet_read(SSL *s, int plen) | ||
293 | { | ||
294 | int n; | ||
295 | |||
296 | n = ssl3_read_n(s, plen, s->s3->rbuf.len, 0); | ||
297 | if (n <= 0) | ||
298 | return n; | ||
299 | if (s->packet_length < plen) | ||
300 | return s->packet_length; | ||
301 | |||
302 | return plen; | ||
303 | } | ||
304 | |||
305 | int | ||
306 | ssl3_packet_extend(SSL *s, int plen) | ||
307 | { | ||
308 | int rlen, n; | ||
309 | |||
310 | if (s->packet_length >= plen) | ||
311 | return plen; | ||
312 | rlen = plen - s->packet_length; | ||
313 | |||
314 | n = ssl3_read_n(s, rlen, rlen, 1); | ||
315 | if (n <= 0) | ||
316 | return n; | ||
317 | if (s->packet_length < plen) | ||
318 | return s->packet_length; | ||
319 | |||
320 | return plen; | ||
321 | } | ||
322 | |||
323 | /* Call this to get a new input record. | ||
324 | * It will return <= 0 if more data is needed, normally due to an error | ||
325 | * or non-blocking IO. | ||
326 | * When it finishes, one packet has been decoded and can be found in | ||
327 | * ssl->s3->rrec.type - is the type of record | ||
328 | * ssl->s3->rrec.data, - data | ||
329 | * ssl->s3->rrec.length, - number of bytes | ||
330 | */ | ||
331 | /* used only by ssl3_read_bytes */ | ||
332 | static int | ||
333 | ssl3_get_record(SSL *s) | ||
334 | { | ||
335 | SSL3_BUFFER_INTERNAL *rb = &(s->s3->rbuf); | ||
336 | SSL3_RECORD_INTERNAL *rr = &(s->s3->rrec); | ||
337 | uint8_t alert_desc; | ||
338 | int al, n; | ||
339 | int ret = -1; | ||
340 | |||
341 | again: | ||
342 | /* check if we have the header */ | ||
343 | if ((s->rstate != SSL_ST_READ_BODY) || | ||
344 | (s->packet_length < SSL3_RT_HEADER_LENGTH)) { | ||
345 | CBS header; | ||
346 | uint16_t len, ssl_version; | ||
347 | uint8_t type; | ||
348 | |||
349 | n = ssl3_packet_read(s, SSL3_RT_HEADER_LENGTH); | ||
350 | if (n <= 0) | ||
351 | return (n); | ||
352 | |||
353 | s->mac_packet = 1; | ||
354 | s->rstate = SSL_ST_READ_BODY; | ||
355 | |||
356 | if (s->server && s->first_packet) { | ||
357 | if ((ret = ssl_server_legacy_first_packet(s)) != 1) | ||
358 | return (ret); | ||
359 | ret = -1; | ||
360 | } | ||
361 | |||
362 | CBS_init(&header, s->packet, SSL3_RT_HEADER_LENGTH); | ||
363 | |||
364 | /* Pull apart the header into the SSL3_RECORD_INTERNAL */ | ||
365 | if (!CBS_get_u8(&header, &type) || | ||
366 | !CBS_get_u16(&header, &ssl_version) || | ||
367 | !CBS_get_u16(&header, &len)) { | ||
368 | SSLerror(s, SSL_R_BAD_PACKET_LENGTH); | ||
369 | goto err; | ||
370 | } | ||
371 | |||
372 | rr->type = type; | ||
373 | rr->length = len; | ||
374 | |||
375 | /* Lets check version */ | ||
376 | if (!s->first_packet && ssl_version != s->version) { | ||
377 | if ((s->version & 0xFF00) == (ssl_version & 0xFF00) && | ||
378 | !tls12_record_layer_write_protected(s->rl)) { | ||
379 | /* Send back error using their minor version number :-) */ | ||
380 | s->version = ssl_version; | ||
381 | } | ||
382 | SSLerror(s, SSL_R_WRONG_VERSION_NUMBER); | ||
383 | al = SSL_AD_PROTOCOL_VERSION; | ||
384 | goto fatal_err; | ||
385 | } | ||
386 | |||
387 | if ((ssl_version >> 8) != SSL3_VERSION_MAJOR) { | ||
388 | SSLerror(s, SSL_R_WRONG_VERSION_NUMBER); | ||
389 | goto err; | ||
390 | } | ||
391 | |||
392 | if (rr->length > rb->len - SSL3_RT_HEADER_LENGTH) { | ||
393 | al = SSL_AD_RECORD_OVERFLOW; | ||
394 | SSLerror(s, SSL_R_PACKET_LENGTH_TOO_LONG); | ||
395 | goto fatal_err; | ||
396 | } | ||
397 | } | ||
398 | |||
399 | n = ssl3_packet_extend(s, SSL3_RT_HEADER_LENGTH + rr->length); | ||
400 | if (n <= 0) | ||
401 | return (n); | ||
402 | if (n != SSL3_RT_HEADER_LENGTH + rr->length) | ||
403 | return (n); | ||
404 | |||
405 | s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */ | ||
406 | |||
407 | /* | ||
408 | * A full record has now been read from the wire, which now needs | ||
409 | * to be processed. | ||
410 | */ | ||
411 | tls12_record_layer_set_version(s->rl, s->version); | ||
412 | |||
413 | if (!tls12_record_layer_open_record(s->rl, s->packet, s->packet_length, | ||
414 | s->s3->rcontent)) { | ||
415 | tls12_record_layer_alert(s->rl, &alert_desc); | ||
416 | |||
417 | if (alert_desc == 0) | ||
418 | goto err; | ||
419 | |||
420 | if (alert_desc == SSL_AD_RECORD_OVERFLOW) | ||
421 | SSLerror(s, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); | ||
422 | else if (alert_desc == SSL_AD_BAD_RECORD_MAC) | ||
423 | SSLerror(s, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); | ||
424 | |||
425 | al = alert_desc; | ||
426 | goto fatal_err; | ||
427 | } | ||
428 | |||
429 | /* we have pulled in a full packet so zero things */ | ||
430 | s->packet_length = 0; | ||
431 | |||
432 | if (tls_content_remaining(s->s3->rcontent) == 0) { | ||
433 | /* | ||
434 | * Zero-length fragments are only permitted for application | ||
435 | * data, as per RFC 5246 section 6.2.1. | ||
436 | */ | ||
437 | if (rr->type != SSL3_RT_APPLICATION_DATA) { | ||
438 | SSLerror(s, SSL_R_BAD_LENGTH); | ||
439 | al = SSL_AD_UNEXPECTED_MESSAGE; | ||
440 | goto fatal_err; | ||
441 | } | ||
442 | |||
443 | tls_content_clear(s->s3->rcontent); | ||
444 | |||
445 | /* | ||
446 | * CBC countermeasures for known IV weaknesses can legitimately | ||
447 | * insert a single empty record, so we allow ourselves to read | ||
448 | * once past a single empty record without forcing want_read. | ||
449 | */ | ||
450 | if (s->empty_record_count++ > SSL_MAX_EMPTY_RECORDS) { | ||
451 | SSLerror(s, SSL_R_PEER_BEHAVING_BADLY); | ||
452 | return -1; | ||
453 | } | ||
454 | if (s->empty_record_count > 1) { | ||
455 | ssl_force_want_read(s); | ||
456 | return -1; | ||
457 | } | ||
458 | goto again; | ||
459 | } | ||
460 | |||
461 | s->empty_record_count = 0; | ||
462 | |||
463 | return (1); | ||
464 | |||
465 | fatal_err: | ||
466 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
467 | err: | ||
468 | return (ret); | ||
469 | } | ||
470 | |||
471 | /* Call this to write data in records of type 'type' | ||
472 | * It will return <= 0 if not all data has been sent or non-blocking IO. | ||
473 | */ | ||
474 | int | ||
475 | ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) | ||
476 | { | ||
477 | const unsigned char *buf = buf_; | ||
478 | unsigned int tot, n, nw; | ||
479 | int i; | ||
480 | |||
481 | if (len < 0) { | ||
482 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
483 | return -1; | ||
484 | } | ||
485 | |||
486 | s->rwstate = SSL_NOTHING; | ||
487 | tot = s->s3->wnum; | ||
488 | s->s3->wnum = 0; | ||
489 | |||
490 | if (SSL_in_init(s) && !s->in_handshake) { | ||
491 | i = s->handshake_func(s); | ||
492 | if (i < 0) | ||
493 | return (i); | ||
494 | if (i == 0) { | ||
495 | SSLerror(s, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
496 | return -1; | ||
497 | } | ||
498 | } | ||
499 | |||
500 | if (len < tot) | ||
501 | len = tot; | ||
502 | n = (len - tot); | ||
503 | for (;;) { | ||
504 | if (n > s->max_send_fragment) | ||
505 | nw = s->max_send_fragment; | ||
506 | else | ||
507 | nw = n; | ||
508 | |||
509 | i = do_ssl3_write(s, type, &(buf[tot]), nw); | ||
510 | if (i <= 0) { | ||
511 | s->s3->wnum = tot; | ||
512 | return i; | ||
513 | } | ||
514 | |||
515 | if ((i == (int)n) || (type == SSL3_RT_APPLICATION_DATA && | ||
516 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { | ||
517 | /* | ||
518 | * Next chunk of data should get another prepended | ||
519 | * empty fragment in ciphersuites with known-IV | ||
520 | * weakness. | ||
521 | */ | ||
522 | s->s3->empty_fragment_done = 0; | ||
523 | |||
524 | return tot + i; | ||
525 | } | ||
526 | |||
527 | n -= i; | ||
528 | tot += i; | ||
529 | } | ||
530 | } | ||
531 | |||
532 | static int | ||
533 | do_ssl3_write(SSL *s, int type, const unsigned char *buf, unsigned int len) | ||
534 | { | ||
535 | SSL3_BUFFER_INTERNAL *wb = &(s->s3->wbuf); | ||
536 | SSL_SESSION *sess = s->session; | ||
537 | int need_empty_fragment = 0; | ||
538 | size_t align, out_len; | ||
539 | CBB cbb; | ||
540 | int ret; | ||
541 | |||
542 | memset(&cbb, 0, sizeof(cbb)); | ||
543 | |||
544 | if (wb->buf == NULL) | ||
545 | if (!ssl3_setup_write_buffer(s)) | ||
546 | return -1; | ||
547 | |||
548 | /* | ||
549 | * First check if there is a SSL3_BUFFER_INTERNAL still being written | ||
550 | * out. This will happen with non blocking IO. | ||
551 | */ | ||
552 | if (wb->left != 0) | ||
553 | return (ssl3_write_pending(s, type, buf, len)); | ||
554 | |||
555 | /* If we have an alert to send, let's send it. */ | ||
556 | if (s->s3->alert_dispatch) { | ||
557 | if ((ret = ssl3_dispatch_alert(s)) <= 0) | ||
558 | return (ret); | ||
559 | /* If it went, fall through and send more stuff. */ | ||
560 | |||
561 | /* We may have released our buffer, if so get it again. */ | ||
562 | if (wb->buf == NULL) | ||
563 | if (!ssl3_setup_write_buffer(s)) | ||
564 | return -1; | ||
565 | } | ||
566 | |||
567 | if (len == 0) | ||
568 | return 0; | ||
569 | |||
570 | /* | ||
571 | * Countermeasure against known-IV weakness in CBC ciphersuites | ||
572 | * (see http://www.openssl.org/~bodo/tls-cbc.txt). Note that this | ||
573 | * is unnecessary for AEAD. | ||
574 | */ | ||
575 | if (sess != NULL && tls12_record_layer_write_protected(s->rl)) { | ||
576 | if (s->s3->need_empty_fragments && | ||
577 | !s->s3->empty_fragment_done && | ||
578 | type == SSL3_RT_APPLICATION_DATA) | ||
579 | need_empty_fragment = 1; | ||
580 | } | ||
581 | |||
582 | /* | ||
583 | * An extra fragment would be a couple of cipher blocks, which would | ||
584 | * be a multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real | ||
585 | * payload, then we can just simply pretend we have two headers. | ||
586 | */ | ||
587 | align = (size_t)wb->buf + SSL3_RT_HEADER_LENGTH; | ||
588 | if (need_empty_fragment) | ||
589 | align += SSL3_RT_HEADER_LENGTH; | ||
590 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); | ||
591 | wb->offset = align; | ||
592 | |||
593 | if (!CBB_init_fixed(&cbb, wb->buf + align, wb->len - align)) | ||
594 | goto err; | ||
595 | |||
596 | tls12_record_layer_set_version(s->rl, s->version); | ||
597 | |||
598 | if (need_empty_fragment) { | ||
599 | if (!tls12_record_layer_seal_record(s->rl, type, | ||
600 | buf, 0, &cbb)) | ||
601 | goto err; | ||
602 | s->s3->empty_fragment_done = 1; | ||
603 | } | ||
604 | |||
605 | if (!tls12_record_layer_seal_record(s->rl, type, buf, len, &cbb)) | ||
606 | goto err; | ||
607 | |||
608 | if (!CBB_finish(&cbb, NULL, &out_len)) | ||
609 | goto err; | ||
610 | |||
611 | wb->left = out_len; | ||
612 | |||
613 | /* | ||
614 | * Memorize arguments so that ssl3_write_pending can detect | ||
615 | * bad write retries later. | ||
616 | */ | ||
617 | s->s3->wpend_tot = len; | ||
618 | s->s3->wpend_buf = buf; | ||
619 | s->s3->wpend_type = type; | ||
620 | s->s3->wpend_ret = len; | ||
621 | |||
622 | /* We now just need to write the buffer. */ | ||
623 | return ssl3_write_pending(s, type, buf, len); | ||
624 | |||
625 | err: | ||
626 | CBB_cleanup(&cbb); | ||
627 | |||
628 | return -1; | ||
629 | } | ||
630 | |||
631 | /* if s->s3->wbuf.left != 0, we need to call this */ | ||
632 | int | ||
633 | ssl3_write_pending(SSL *s, int type, const unsigned char *buf, unsigned int len) | ||
634 | { | ||
635 | int i; | ||
636 | SSL3_BUFFER_INTERNAL *wb = &(s->s3->wbuf); | ||
637 | |||
638 | /* XXXX */ | ||
639 | if ((s->s3->wpend_tot > (int)len) || ((s->s3->wpend_buf != buf) && | ||
640 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || | ||
641 | (s->s3->wpend_type != type)) { | ||
642 | SSLerror(s, SSL_R_BAD_WRITE_RETRY); | ||
643 | return (-1); | ||
644 | } | ||
645 | |||
646 | for (;;) { | ||
647 | errno = 0; | ||
648 | if (s->wbio != NULL) { | ||
649 | s->rwstate = SSL_WRITING; | ||
650 | i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]), | ||
651 | (unsigned int)wb->left); | ||
652 | } else { | ||
653 | SSLerror(s, SSL_R_BIO_NOT_SET); | ||
654 | i = -1; | ||
655 | } | ||
656 | if (i == wb->left) { | ||
657 | wb->left = 0; | ||
658 | wb->offset += i; | ||
659 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
660 | !SSL_is_dtls(s)) | ||
661 | ssl3_release_write_buffer(s); | ||
662 | s->rwstate = SSL_NOTHING; | ||
663 | return (s->s3->wpend_ret); | ||
664 | } else if (i <= 0) { | ||
665 | /* | ||
666 | * For DTLS, just drop it. That's kind of the | ||
667 | * whole point in using a datagram service. | ||
668 | */ | ||
669 | if (SSL_is_dtls(s)) | ||
670 | wb->left = 0; | ||
671 | return (i); | ||
672 | } | ||
673 | wb->offset += i; | ||
674 | wb->left -= i; | ||
675 | } | ||
676 | } | ||
677 | |||
678 | static ssize_t | ||
679 | ssl3_read_cb(void *buf, size_t n, void *cb_arg) | ||
680 | { | ||
681 | SSL *s = cb_arg; | ||
682 | |||
683 | return tls_content_read(s->s3->rcontent, buf, n); | ||
684 | } | ||
685 | |||
686 | #define SSL3_ALERT_LENGTH 2 | ||
687 | |||
688 | int | ||
689 | ssl3_read_alert(SSL *s) | ||
690 | { | ||
691 | uint8_t alert_level, alert_descr; | ||
692 | ssize_t ret; | ||
693 | CBS cbs; | ||
694 | |||
695 | /* | ||
696 | * TLSv1.2 permits an alert to be fragmented across multiple records or | ||
697 | * for multiple alerts to be be coalesced into a single alert record. | ||
698 | * In the case of DTLS, there is no way to reassemble an alert | ||
699 | * fragmented across multiple records, hence a full alert must be | ||
700 | * available in the record. | ||
701 | */ | ||
702 | if (s->s3->alert_fragment == NULL) { | ||
703 | if ((s->s3->alert_fragment = tls_buffer_new(0)) == NULL) | ||
704 | return -1; | ||
705 | tls_buffer_set_capacity_limit(s->s3->alert_fragment, | ||
706 | SSL3_ALERT_LENGTH); | ||
707 | } | ||
708 | ret = tls_buffer_extend(s->s3->alert_fragment, SSL3_ALERT_LENGTH, | ||
709 | ssl3_read_cb, s); | ||
710 | if (ret <= 0 && ret != TLS_IO_WANT_POLLIN) | ||
711 | return -1; | ||
712 | if (ret != SSL3_ALERT_LENGTH) { | ||
713 | if (SSL_is_dtls(s)) { | ||
714 | SSLerror(s, SSL_R_BAD_LENGTH); | ||
715 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
716 | return -1; | ||
717 | } | ||
718 | return 1; | ||
719 | } | ||
720 | |||
721 | if (!tls_buffer_data(s->s3->alert_fragment, &cbs)) | ||
722 | return -1; | ||
723 | |||
724 | ssl_msg_callback_cbs(s, 0, SSL3_RT_ALERT, &cbs); | ||
725 | |||
726 | if (!CBS_get_u8(&cbs, &alert_level)) | ||
727 | return -1; | ||
728 | if (!CBS_get_u8(&cbs, &alert_descr)) | ||
729 | return -1; | ||
730 | |||
731 | tls_buffer_free(s->s3->alert_fragment); | ||
732 | s->s3->alert_fragment = NULL; | ||
733 | |||
734 | ssl_info_callback(s, SSL_CB_READ_ALERT, | ||
735 | (alert_level << 8) | alert_descr); | ||
736 | |||
737 | if (alert_level == SSL3_AL_WARNING) { | ||
738 | s->s3->warn_alert = alert_descr; | ||
739 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { | ||
740 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | ||
741 | return 0; | ||
742 | } | ||
743 | /* We requested renegotiation and the peer rejected it. */ | ||
744 | if (alert_descr == SSL_AD_NO_RENEGOTIATION) { | ||
745 | SSLerror(s, SSL_R_NO_RENEGOTIATION); | ||
746 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
747 | SSL_AD_HANDSHAKE_FAILURE); | ||
748 | return -1; | ||
749 | } | ||
750 | } else if (alert_level == SSL3_AL_FATAL) { | ||
751 | s->rwstate = SSL_NOTHING; | ||
752 | s->s3->fatal_alert = alert_descr; | ||
753 | SSLerror(s, SSL_AD_REASON_OFFSET + alert_descr); | ||
754 | ERR_asprintf_error_data("SSL alert number %d", alert_descr); | ||
755 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; | ||
756 | SSL_CTX_remove_session(s->ctx, s->session); | ||
757 | return 0; | ||
758 | } else { | ||
759 | SSLerror(s, SSL_R_UNKNOWN_ALERT_TYPE); | ||
760 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); | ||
761 | return -1; | ||
762 | } | ||
763 | |||
764 | return 1; | ||
765 | } | ||
766 | |||
767 | int | ||
768 | ssl3_read_change_cipher_spec(SSL *s) | ||
769 | { | ||
770 | const uint8_t ccs[1] = { SSL3_MT_CCS }; | ||
771 | |||
772 | /* | ||
773 | * 'Change Cipher Spec' is just a single byte, so we know exactly what | ||
774 | * the record payload has to look like. | ||
775 | */ | ||
776 | if (tls_content_remaining(s->s3->rcontent) != sizeof(ccs)) { | ||
777 | SSLerror(s, SSL_R_BAD_CHANGE_CIPHER_SPEC); | ||
778 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
779 | return -1; | ||
780 | } | ||
781 | if (!tls_content_equal(s->s3->rcontent, ccs, sizeof(ccs))) { | ||
782 | SSLerror(s, SSL_R_BAD_CHANGE_CIPHER_SPEC); | ||
783 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); | ||
784 | return -1; | ||
785 | } | ||
786 | |||
787 | /* XDTLS: check that epoch is consistent */ | ||
788 | |||
789 | ssl_msg_callback_cbs(s, 0, SSL3_RT_CHANGE_CIPHER_SPEC, | ||
790 | tls_content_cbs(s->s3->rcontent)); | ||
791 | |||
792 | /* Check that we have a cipher to change to. */ | ||
793 | if (s->s3->hs.cipher == NULL) { | ||
794 | SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); | ||
795 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
796 | return -1; | ||
797 | } | ||
798 | |||
799 | /* Check that we should be receiving a Change Cipher Spec. */ | ||
800 | if (SSL_is_dtls(s)) { | ||
801 | if (!s->d1->change_cipher_spec_ok) { | ||
802 | /* | ||
803 | * We can't process a CCS now, because previous | ||
804 | * handshake messages are still missing, so just | ||
805 | * drop it. | ||
806 | */ | ||
807 | tls_content_clear(s->s3->rcontent); | ||
808 | return 1; | ||
809 | } | ||
810 | s->d1->change_cipher_spec_ok = 0; | ||
811 | } else { | ||
812 | if ((s->s3->flags & SSL3_FLAGS_CCS_OK) == 0) { | ||
813 | SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); | ||
814 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
815 | SSL_AD_UNEXPECTED_MESSAGE); | ||
816 | return -1; | ||
817 | } | ||
818 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; | ||
819 | } | ||
820 | |||
821 | tls_content_clear(s->s3->rcontent); | ||
822 | |||
823 | s->s3->change_cipher_spec = 1; | ||
824 | if (!ssl3_do_change_cipher_spec(s)) | ||
825 | return -1; | ||
826 | |||
827 | return 1; | ||
828 | } | ||
829 | |||
830 | static int | ||
831 | ssl3_read_handshake_unexpected(SSL *s) | ||
832 | { | ||
833 | uint32_t hs_msg_length; | ||
834 | uint8_t hs_msg_type; | ||
835 | ssize_t ssret; | ||
836 | CBS cbs; | ||
837 | int ret; | ||
838 | |||
839 | /* | ||
840 | * We need four bytes of handshake data so we have a handshake message | ||
841 | * header - this may be in the same record or fragmented across multiple | ||
842 | * records. | ||
843 | */ | ||
844 | if (s->s3->handshake_fragment == NULL) { | ||
845 | if ((s->s3->handshake_fragment = tls_buffer_new(0)) == NULL) | ||
846 | return -1; | ||
847 | tls_buffer_set_capacity_limit(s->s3->handshake_fragment, | ||
848 | SSL3_HM_HEADER_LENGTH); | ||
849 | } | ||
850 | ssret = tls_buffer_extend(s->s3->handshake_fragment, SSL3_HM_HEADER_LENGTH, | ||
851 | ssl3_read_cb, s); | ||
852 | if (ssret <= 0 && ssret != TLS_IO_WANT_POLLIN) | ||
853 | return -1; | ||
854 | if (ssret != SSL3_HM_HEADER_LENGTH) | ||
855 | return 1; | ||
856 | |||
857 | if (s->in_handshake) { | ||
858 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
859 | return -1; | ||
860 | } | ||
861 | |||
862 | /* | ||
863 | * This code currently deals with HelloRequest and ClientHello messages - | ||
864 | * anything else is pushed to the handshake_func. Almost all of this | ||
865 | * belongs in the client/server handshake code. | ||
866 | */ | ||
867 | |||
868 | /* Parse handshake message header. */ | ||
869 | if (!tls_buffer_data(s->s3->handshake_fragment, &cbs)) | ||
870 | return -1; | ||
871 | if (!CBS_get_u8(&cbs, &hs_msg_type)) | ||
872 | return -1; | ||
873 | if (!CBS_get_u24(&cbs, &hs_msg_length)) | ||
874 | return -1; | ||
875 | |||
876 | if (hs_msg_type == SSL3_MT_HELLO_REQUEST) { | ||
877 | /* | ||
878 | * Incoming HelloRequest messages should only be received by a | ||
879 | * client. A server may send these at any time - a client should | ||
880 | * ignore the message if received in the middle of a handshake. | ||
881 | * See RFC 5246 sections 7.4 and 7.4.1.1. | ||
882 | */ | ||
883 | if (s->server) { | ||
884 | SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); | ||
885 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
886 | SSL_AD_UNEXPECTED_MESSAGE); | ||
887 | return -1; | ||
888 | } | ||
889 | |||
890 | if (hs_msg_length != 0) { | ||
891 | SSLerror(s, SSL_R_BAD_HELLO_REQUEST); | ||
892 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | ||
893 | return -1; | ||
894 | } | ||
895 | |||
896 | if (!tls_buffer_data(s->s3->handshake_fragment, &cbs)) | ||
897 | return -1; | ||
898 | ssl_msg_callback_cbs(s, 0, SSL3_RT_HANDSHAKE, &cbs); | ||
899 | |||
900 | tls_buffer_free(s->s3->handshake_fragment); | ||
901 | s->s3->handshake_fragment = NULL; | ||
902 | |||
903 | if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0) { | ||
904 | ssl3_send_alert(s, SSL3_AL_WARNING, | ||
905 | SSL_AD_NO_RENEGOTIATION); | ||
906 | return 1; | ||
907 | } | ||
908 | |||
909 | /* | ||
910 | * It should be impossible to hit this, but keep the safety | ||
911 | * harness for now... | ||
912 | */ | ||
913 | if (s->session == NULL || s->s3->hs.cipher == NULL) | ||
914 | return 1; | ||
915 | |||
916 | /* | ||
917 | * Ignore this message if we're currently handshaking, | ||
918 | * renegotiation is already pending or renegotiation is disabled | ||
919 | * via flags. | ||
920 | */ | ||
921 | if (!SSL_is_init_finished(s) || s->s3->renegotiate || | ||
922 | (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) != 0) | ||
923 | return 1; | ||
924 | |||
925 | if (!ssl3_renegotiate(s)) | ||
926 | return 1; | ||
927 | if (!ssl3_renegotiate_check(s)) | ||
928 | return 1; | ||
929 | |||
930 | } else if (hs_msg_type == SSL3_MT_CLIENT_HELLO) { | ||
931 | /* | ||
932 | * Incoming ClientHello messages should only be received by a | ||
933 | * server. A client may send these in response to server | ||
934 | * initiated renegotiation (HelloRequest) or in order to | ||
935 | * initiate renegotiation by the client. See RFC 5246 section | ||
936 | * 7.4.1.2. | ||
937 | */ | ||
938 | if (!s->server) { | ||
939 | SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); | ||
940 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
941 | SSL_AD_UNEXPECTED_MESSAGE); | ||
942 | return -1; | ||
943 | } | ||
944 | |||
945 | /* | ||
946 | * A client should not be sending a ClientHello unless we're not | ||
947 | * currently handshaking. | ||
948 | */ | ||
949 | if (!SSL_is_init_finished(s)) { | ||
950 | SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); | ||
951 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
952 | SSL_AD_UNEXPECTED_MESSAGE); | ||
953 | return -1; | ||
954 | } | ||
955 | |||
956 | if ((s->options & SSL_OP_NO_CLIENT_RENEGOTIATION) != 0 || | ||
957 | ((s->options & SSL_OP_NO_RENEGOTIATION) != 0 && | ||
958 | (s->options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) == 0)) { | ||
959 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
960 | SSL_AD_NO_RENEGOTIATION); | ||
961 | return -1; | ||
962 | } | ||
963 | |||
964 | if (s->session == NULL || s->s3->hs.cipher == NULL) { | ||
965 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
966 | return -1; | ||
967 | } | ||
968 | |||
969 | /* Client requested renegotiation but it is not permitted. */ | ||
970 | if (!s->s3->send_connection_binding || | ||
971 | (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) != 0) { | ||
972 | ssl3_send_alert(s, SSL3_AL_WARNING, | ||
973 | SSL_AD_NO_RENEGOTIATION); | ||
974 | return 1; | ||
975 | } | ||
976 | |||
977 | s->s3->hs.state = SSL_ST_ACCEPT; | ||
978 | s->renegotiate = 1; | ||
979 | s->new_session = 1; | ||
980 | |||
981 | } else { | ||
982 | SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); | ||
983 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
984 | return -1; | ||
985 | } | ||
986 | |||
987 | if ((ret = s->handshake_func(s)) < 0) | ||
988 | return ret; | ||
989 | if (ret == 0) { | ||
990 | SSLerror(s, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
991 | return -1; | ||
992 | } | ||
993 | |||
994 | if (!(s->mode & SSL_MODE_AUTO_RETRY)) { | ||
995 | if (s->s3->rbuf.left == 0) { | ||
996 | ssl_force_want_read(s); | ||
997 | return -1; | ||
998 | } | ||
999 | } | ||
1000 | |||
1001 | /* | ||
1002 | * We either finished a handshake or ignored the request, now try again | ||
1003 | * to obtain the (application) data we were asked for. | ||
1004 | */ | ||
1005 | return 1; | ||
1006 | } | ||
1007 | |||
1008 | /* Return up to 'len' payload bytes received in 'type' records. | ||
1009 | * 'type' is one of the following: | ||
1010 | * | ||
1011 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) | ||
1012 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) | ||
1013 | * - 0 (during a shutdown, no data has to be returned) | ||
1014 | * | ||
1015 | * If we don't have stored data to work from, read a SSL/TLS record first | ||
1016 | * (possibly multiple records if we still don't have anything to return). | ||
1017 | * | ||
1018 | * This function must handle any surprises the peer may have for us, such as | ||
1019 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really | ||
1020 | * a surprise, but handled as if it were), or renegotiation requests. | ||
1021 | * Also if record payloads contain fragments too small to process, we store | ||
1022 | * them until there is enough for the respective protocol (the record protocol | ||
1023 | * may use arbitrary fragmentation and even interleaving): | ||
1024 | * Change cipher spec protocol | ||
1025 | * just 1 byte needed, no need for keeping anything stored | ||
1026 | * Alert protocol | ||
1027 | * 2 bytes needed (AlertLevel, AlertDescription) | ||
1028 | * Handshake protocol | ||
1029 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have | ||
1030 | * to detect unexpected Client Hello and Hello Request messages | ||
1031 | * here, anything else is handled by higher layers | ||
1032 | * Application data protocol | ||
1033 | * none of our business | ||
1034 | */ | ||
1035 | int | ||
1036 | ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) | ||
1037 | { | ||
1038 | int rrcount = 0; | ||
1039 | ssize_t ssret; | ||
1040 | int ret; | ||
1041 | |||
1042 | if (s->s3->rbuf.buf == NULL) { | ||
1043 | if (!ssl3_setup_read_buffer(s)) | ||
1044 | return -1; | ||
1045 | } | ||
1046 | |||
1047 | if (s->s3->rcontent == NULL) { | ||
1048 | if ((s->s3->rcontent = tls_content_new()) == NULL) | ||
1049 | return -1; | ||
1050 | } | ||
1051 | |||
1052 | if (len < 0) { | ||
1053 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
1054 | return -1; | ||
1055 | } | ||
1056 | |||
1057 | if (type != 0 && type != SSL3_RT_APPLICATION_DATA && | ||
1058 | type != SSL3_RT_HANDSHAKE) { | ||
1059 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
1060 | return -1; | ||
1061 | } | ||
1062 | if (peek && type != SSL3_RT_APPLICATION_DATA) { | ||
1063 | SSLerror(s, ERR_R_INTERNAL_ERROR); | ||
1064 | return -1; | ||
1065 | } | ||
1066 | |||
1067 | if (type == SSL3_RT_HANDSHAKE && | ||
1068 | s->s3->handshake_fragment != NULL && | ||
1069 | tls_buffer_remaining(s->s3->handshake_fragment) > 0) { | ||
1070 | ssize_t ssn; | ||
1071 | |||
1072 | if ((ssn = tls_buffer_read(s->s3->handshake_fragment, buf, | ||
1073 | len)) <= 0) | ||
1074 | return -1; | ||
1075 | |||
1076 | if (tls_buffer_remaining(s->s3->handshake_fragment) == 0) { | ||
1077 | tls_buffer_free(s->s3->handshake_fragment); | ||
1078 | s->s3->handshake_fragment = NULL; | ||
1079 | } | ||
1080 | |||
1081 | return (int)ssn; | ||
1082 | } | ||
1083 | |||
1084 | if (SSL_in_init(s) && !s->in_handshake) { | ||
1085 | if ((ret = s->handshake_func(s)) < 0) | ||
1086 | return ret; | ||
1087 | if (ret == 0) { | ||
1088 | SSLerror(s, SSL_R_SSL_HANDSHAKE_FAILURE); | ||
1089 | return -1; | ||
1090 | } | ||
1091 | } | ||
1092 | |||
1093 | start: | ||
1094 | /* | ||
1095 | * Do not process more than three consecutive records, otherwise the | ||
1096 | * peer can cause us to loop indefinitely. Instead, return with an | ||
1097 | * SSL_ERROR_WANT_READ so the caller can choose when to handle further | ||
1098 | * processing. In the future, the total number of non-handshake and | ||
1099 | * non-application data records per connection should probably also be | ||
1100 | * limited... | ||
1101 | */ | ||
1102 | if (rrcount++ >= 3) { | ||
1103 | ssl_force_want_read(s); | ||
1104 | return -1; | ||
1105 | } | ||
1106 | |||
1107 | s->rwstate = SSL_NOTHING; | ||
1108 | |||
1109 | if (tls_content_remaining(s->s3->rcontent) == 0) { | ||
1110 | if ((ret = ssl3_get_record(s)) <= 0) | ||
1111 | return ret; | ||
1112 | } | ||
1113 | |||
1114 | /* We now have a packet which can be read and processed. */ | ||
1115 | |||
1116 | if (s->s3->change_cipher_spec && | ||
1117 | tls_content_type(s->s3->rcontent) != SSL3_RT_HANDSHAKE) { | ||
1118 | SSLerror(s, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); | ||
1119 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
1120 | return -1; | ||
1121 | } | ||
1122 | |||
1123 | /* | ||
1124 | * If the other end has shut down, throw anything we read away (even in | ||
1125 | * 'peek' mode). | ||
1126 | */ | ||
1127 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { | ||
1128 | s->rwstate = SSL_NOTHING; | ||
1129 | tls_content_clear(s->s3->rcontent); | ||
1130 | s->s3->rrec.length = 0; | ||
1131 | return 0; | ||
1132 | } | ||
1133 | |||
1134 | /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ | ||
1135 | if (tls_content_type(s->s3->rcontent) == type) { | ||
1136 | /* | ||
1137 | * Make sure that we are not getting application data when we | ||
1138 | * are doing a handshake for the first time. | ||
1139 | */ | ||
1140 | if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA && | ||
1141 | !tls12_record_layer_read_protected(s->rl)) { | ||
1142 | SSLerror(s, SSL_R_APP_DATA_IN_HANDSHAKE); | ||
1143 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1144 | SSL_AD_UNEXPECTED_MESSAGE); | ||
1145 | return -1; | ||
1146 | } | ||
1147 | |||
1148 | if (len <= 0) | ||
1149 | return len; | ||
1150 | |||
1151 | if (peek) { | ||
1152 | ssret = tls_content_peek(s->s3->rcontent, buf, len); | ||
1153 | } else { | ||
1154 | ssret = tls_content_read(s->s3->rcontent, buf, len); | ||
1155 | } | ||
1156 | if (ssret < INT_MIN || ssret > INT_MAX) | ||
1157 | return -1; | ||
1158 | if (ssret < 0) | ||
1159 | return (int)ssret; | ||
1160 | |||
1161 | if (tls_content_remaining(s->s3->rcontent) == 0) { | ||
1162 | s->rstate = SSL_ST_READ_HEADER; | ||
1163 | |||
1164 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && | ||
1165 | s->s3->rbuf.left == 0) | ||
1166 | ssl3_release_read_buffer(s); | ||
1167 | } | ||
1168 | |||
1169 | return ssret; | ||
1170 | } | ||
1171 | |||
1172 | if (tls_content_type(s->s3->rcontent) == SSL3_RT_ALERT) { | ||
1173 | if ((ret = ssl3_read_alert(s)) <= 0) | ||
1174 | return ret; | ||
1175 | goto start; | ||
1176 | } | ||
1177 | |||
1178 | if (s->shutdown & SSL_SENT_SHUTDOWN) { | ||
1179 | s->rwstate = SSL_NOTHING; | ||
1180 | tls_content_clear(s->s3->rcontent); | ||
1181 | s->s3->rrec.length = 0; | ||
1182 | return 0; | ||
1183 | } | ||
1184 | |||
1185 | if (tls_content_type(s->s3->rcontent) == SSL3_RT_APPLICATION_DATA) { | ||
1186 | /* | ||
1187 | * At this point, we were expecting handshake data, but have | ||
1188 | * application data. If the library was running inside | ||
1189 | * ssl3_read() (i.e. in_read_app_data is set) and it makes | ||
1190 | * sense to read application data at this point (session | ||
1191 | * renegotiation not yet started), we will indulge it. | ||
1192 | */ | ||
1193 | if (s->s3->in_read_app_data != 0 && | ||
1194 | s->s3->total_renegotiations != 0 && | ||
1195 | (((s->s3->hs.state & SSL_ST_CONNECT) && | ||
1196 | (s->s3->hs.state >= SSL3_ST_CW_CLNT_HELLO_A) && | ||
1197 | (s->s3->hs.state <= SSL3_ST_CR_SRVR_HELLO_A)) || ( | ||
1198 | (s->s3->hs.state & SSL_ST_ACCEPT) && | ||
1199 | (s->s3->hs.state <= SSL3_ST_SW_HELLO_REQ_A) && | ||
1200 | (s->s3->hs.state >= SSL3_ST_SR_CLNT_HELLO_A)))) { | ||
1201 | s->s3->in_read_app_data = 2; | ||
1202 | return -1; | ||
1203 | } else { | ||
1204 | SSLerror(s, SSL_R_UNEXPECTED_RECORD); | ||
1205 | ssl3_send_alert(s, SSL3_AL_FATAL, | ||
1206 | SSL_AD_UNEXPECTED_MESSAGE); | ||
1207 | return -1; | ||
1208 | } | ||
1209 | } | ||
1210 | |||
1211 | if (tls_content_type(s->s3->rcontent) == SSL3_RT_CHANGE_CIPHER_SPEC) { | ||
1212 | if ((ret = ssl3_read_change_cipher_spec(s)) <= 0) | ||
1213 | return ret; | ||
1214 | goto start; | ||
1215 | } | ||
1216 | |||
1217 | if (tls_content_type(s->s3->rcontent) == SSL3_RT_HANDSHAKE) { | ||
1218 | if ((ret = ssl3_read_handshake_unexpected(s)) <= 0) | ||
1219 | return ret; | ||
1220 | goto start; | ||
1221 | } | ||
1222 | |||
1223 | /* | ||
1224 | * Unknown record type - TLSv1.2 sends an unexpected message alert while | ||
1225 | * earlier versions silently ignore the record. | ||
1226 | */ | ||
1227 | if (ssl_effective_tls_version(s) <= TLS1_1_VERSION) { | ||
1228 | tls_content_clear(s->s3->rcontent); | ||
1229 | goto start; | ||
1230 | } | ||
1231 | SSLerror(s, SSL_R_UNEXPECTED_RECORD); | ||
1232 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); | ||
1233 | return -1; | ||
1234 | } | ||
1235 | |||
1236 | int | ||
1237 | ssl3_do_change_cipher_spec(SSL *s) | ||
1238 | { | ||
1239 | if (s->s3->hs.tls12.key_block == NULL) { | ||
1240 | if (s->session == NULL || s->session->master_key_length == 0) { | ||
1241 | /* might happen if dtls1_read_bytes() calls this */ | ||
1242 | SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); | ||
1243 | return (0); | ||
1244 | } | ||
1245 | |||
1246 | s->session->cipher_value = s->s3->hs.cipher->value; | ||
1247 | |||
1248 | if (!tls1_setup_key_block(s)) | ||
1249 | return (0); | ||
1250 | } | ||
1251 | |||
1252 | if (!tls1_change_read_cipher_state(s)) | ||
1253 | return (0); | ||
1254 | |||
1255 | /* | ||
1256 | * We have to record the message digest at this point so we can get it | ||
1257 | * before we read the finished message. | ||
1258 | */ | ||
1259 | if (!tls12_derive_peer_finished(s)) | ||
1260 | return (0); | ||
1261 | |||
1262 | return (1); | ||
1263 | } | ||
1264 | |||
1265 | static int | ||
1266 | ssl3_write_alert(SSL *s) | ||
1267 | { | ||
1268 | if (SSL_is_dtls(s)) | ||
1269 | return do_dtls1_write(s, SSL3_RT_ALERT, s->s3->send_alert, | ||
1270 | sizeof(s->s3->send_alert)); | ||
1271 | |||
1272 | return do_ssl3_write(s, SSL3_RT_ALERT, s->s3->send_alert, | ||
1273 | sizeof(s->s3->send_alert)); | ||
1274 | } | ||
1275 | |||
1276 | int | ||
1277 | ssl3_send_alert(SSL *s, int level, int desc) | ||
1278 | { | ||
1279 | /* If alert is fatal, remove session from cache. */ | ||
1280 | if (level == SSL3_AL_FATAL) | ||
1281 | SSL_CTX_remove_session(s->ctx, s->session); | ||
1282 | |||
1283 | s->s3->alert_dispatch = 1; | ||
1284 | s->s3->send_alert[0] = level; | ||
1285 | s->s3->send_alert[1] = desc; | ||
1286 | |||
1287 | /* | ||
1288 | * If data is still being written out, the alert will be dispatched at | ||
1289 | * some point in the future. | ||
1290 | */ | ||
1291 | if (s->s3->wbuf.left != 0) | ||
1292 | return -1; | ||
1293 | |||
1294 | return ssl3_dispatch_alert(s); | ||
1295 | } | ||
1296 | |||
1297 | int | ||
1298 | ssl3_dispatch_alert(SSL *s) | ||
1299 | { | ||
1300 | int ret; | ||
1301 | |||
1302 | s->s3->alert_dispatch = 0; | ||
1303 | if ((ret = ssl3_write_alert(s)) <= 0) { | ||
1304 | s->s3->alert_dispatch = 1; | ||
1305 | return ret; | ||
1306 | } | ||
1307 | |||
1308 | /* | ||
1309 | * Alert sent to BIO. If it is important, flush it now. | ||
1310 | * If the message does not get sent due to non-blocking IO, | ||
1311 | * we will not worry too much. | ||
1312 | */ | ||
1313 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) | ||
1314 | (void)BIO_flush(s->wbio); | ||
1315 | |||
1316 | ssl_msg_callback(s, 1, SSL3_RT_ALERT, s->s3->send_alert, 2); | ||
1317 | |||
1318 | ssl_info_callback(s, SSL_CB_WRITE_ALERT, | ||
1319 | (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]); | ||
1320 | |||
1321 | return ret; | ||
1322 | } | ||