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