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