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