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