Home | History | Annotate | Download | only in ssl
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) cryptsoft.com).
      6  * The implementation was written so as to conform with Netscapes SSL.
      7  *
      8  * This library is free for commercial and non-commercial use as long as
      9  * the following conditions are aheared to.  The following conditions
     10  * apply to all code found in this distribution, be it the RC4, RSA,
     11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     12  * included with this distribution is covered by the same copyright terms
     13  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     14  *
     15  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  * the code are not to be removed.
     17  * If this package is used in a product, Eric Young should be given attribution
     18  * as the author of the parts of the library used.
     19  * This can be in the form of a textual message at program startup or
     20  * in documentation (online or textual) provided with the package.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *    "This product includes cryptographic software written by
     33  *     Eric Young (eay (at) cryptsoft.com)"
     34  *    The word 'cryptographic' can be left out if the rouines from the library
     35  *    being used are not cryptographic related :-).
     36  * 4. If you include any Windows specific code (or a derivative thereof) from
     37  *    the apps directory (application code) you must include an acknowledgement:
     38  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  *
     52  * The licence and distribution terms for any publically available version or
     53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     54  * copied and put under another distribution licence
     55  * [including the GNU Public Licence.]
     56  */
     57 /* ====================================================================
     58  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
     59  *
     60  * Redistribution and use in source and binary forms, with or without
     61  * modification, are permitted provided that the following conditions
     62  * are met:
     63  *
     64  * 1. Redistributions of source code must retain the above copyright
     65  *    notice, this list of conditions and the following disclaimer.
     66  *
     67  * 2. Redistributions in binary form must reproduce the above copyright
     68  *    notice, this list of conditions and the following disclaimer in
     69  *    the documentation and/or other materials provided with the
     70  *    distribution.
     71  *
     72  * 3. All advertising materials mentioning features or use of this
     73  *    software must display the following acknowledgment:
     74  *    "This product includes software developed by the OpenSSL Project
     75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
     76  *
     77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
     78  *    endorse or promote products derived from this software without
     79  *    prior written permission. For written permission, please contact
     80  *    openssl-core (at) openssl.org.
     81  *
     82  * 5. Products derived from this software may not be called "OpenSSL"
     83  *    nor may "OpenSSL" appear in their names without prior written
     84  *    permission of the OpenSSL Project.
     85  *
     86  * 6. Redistributions of any form whatsoever must retain the following
     87  *    acknowledgment:
     88  *    "This product includes software developed by the OpenSSL Project
     89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
     90  *
     91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
     92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
     95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    102  * OF THE POSSIBILITY OF SUCH DAMAGE.
    103  * ====================================================================
    104  *
    105  * This product includes cryptographic software written by Eric Young
    106  * (eay (at) cryptsoft.com).  This product includes software written by Tim
    107  * Hudson (tjh (at) cryptsoft.com). */
    108 
    109 #include <openssl/ssl.h>
    110 
    111 #include <assert.h>
    112 #include <limits.h>
    113 #include <string.h>
    114 
    115 #include <openssl/buf.h>
    116 #include <openssl/err.h>
    117 #include <openssl/evp.h>
    118 #include <openssl/mem.h>
    119 #include <openssl/rand.h>
    120 
    121 #include "../crypto/internal.h"
    122 #include "internal.h"
    123 
    124 
    125 static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
    126 
    127 /* ssl3_get_record reads a new input record. On success, it places it in
    128  * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
    129  * more data is needed. */
    130 static int ssl3_get_record(SSL *ssl) {
    131 again:
    132   switch (ssl->s3->recv_shutdown) {
    133     case ssl_shutdown_none:
    134       break;
    135     case ssl_shutdown_fatal_alert:
    136       OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
    137       return -1;
    138     case ssl_shutdown_close_notify:
    139       return 0;
    140   }
    141 
    142   CBS body;
    143   uint8_t type, alert = SSL_AD_DECODE_ERROR;
    144   size_t consumed;
    145   enum ssl_open_record_t open_ret =
    146       tls_open_record(ssl, &type, &body, &consumed, &alert,
    147                       ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
    148   if (open_ret != ssl_open_record_partial) {
    149     ssl_read_buffer_consume(ssl, consumed);
    150   }
    151   switch (open_ret) {
    152     case ssl_open_record_partial: {
    153       int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
    154       if (read_ret <= 0) {
    155         return read_ret;
    156       }
    157       goto again;
    158     }
    159 
    160     case ssl_open_record_success: {
    161       if (CBS_len(&body) > 0xffff) {
    162         OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
    163         return -1;
    164       }
    165 
    166       SSL3_RECORD *rr = &ssl->s3->rrec;
    167       rr->type = type;
    168       rr->length = (uint16_t)CBS_len(&body);
    169       rr->data = (uint8_t *)CBS_data(&body);
    170       return 1;
    171     }
    172 
    173     case ssl_open_record_discard:
    174       goto again;
    175 
    176     case ssl_open_record_close_notify:
    177       return 0;
    178 
    179     case ssl_open_record_fatal_alert:
    180       return -1;
    181 
    182     case ssl_open_record_error:
    183       ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
    184       return -1;
    185   }
    186 
    187   assert(0);
    188   OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    189   return -1;
    190 }
    191 
    192 int ssl3_write_app_data(SSL *ssl, int *out_needs_handshake, const uint8_t *buf,
    193                         int len) {
    194   assert(ssl_can_write(ssl));
    195   assert(ssl->s3->aead_write_ctx != NULL);
    196 
    197   *out_needs_handshake = 0;
    198 
    199   unsigned tot, n, nw;
    200 
    201   assert(ssl->s3->wnum <= INT_MAX);
    202   tot = ssl->s3->wnum;
    203   ssl->s3->wnum = 0;
    204 
    205   /* Ensure that if we end up with a smaller value of data to write out than
    206    * the the original len from a write which didn't complete for non-blocking
    207    * I/O and also somehow ended up avoiding the check for this in
    208    * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
    209    * end up with (len-tot) as a large number that will then promptly send
    210    * beyond the end of the users buffer ... so we trap and report the error in
    211    * a way the user will notice. */
    212   if (len < 0 || (size_t)len < tot) {
    213     OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
    214     return -1;
    215   }
    216 
    217   const int is_early_data_write =
    218       !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write;
    219 
    220   n = len - tot;
    221   for (;;) {
    222     /* max contains the maximum number of bytes that we can put into a
    223      * record. */
    224     unsigned max = ssl->max_send_fragment;
    225     if (is_early_data_write && max > ssl->session->ticket_max_early_data -
    226                                          ssl->s3->hs->early_data_written) {
    227       max = ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written;
    228       if (max == 0) {
    229         ssl->s3->wnum = tot;
    230         ssl->s3->hs->can_early_write = 0;
    231         *out_needs_handshake = 1;
    232         return -1;
    233       }
    234     }
    235 
    236     if (n > max) {
    237       nw = max;
    238     } else {
    239       nw = n;
    240     }
    241 
    242     int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &buf[tot], nw);
    243     if (ret <= 0) {
    244       ssl->s3->wnum = tot;
    245       return ret;
    246     }
    247 
    248     if (is_early_data_write) {
    249       ssl->s3->hs->early_data_written += ret;
    250     }
    251 
    252     if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
    253       return tot + ret;
    254     }
    255 
    256     n -= ret;
    257     tot += ret;
    258   }
    259 }
    260 
    261 static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
    262                               unsigned int len) {
    263   if (ssl->s3->wpend_tot > (int)len ||
    264       (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
    265        ssl->s3->wpend_buf != buf) ||
    266       ssl->s3->wpend_type != type) {
    267     OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
    268     return -1;
    269   }
    270 
    271   int ret = ssl_write_buffer_flush(ssl);
    272   if (ret <= 0) {
    273     return ret;
    274   }
    275   ssl->s3->wpend_pending = 0;
    276   return ssl->s3->wpend_ret;
    277 }
    278 
    279 /* do_ssl3_write writes an SSL record of the given type. */
    280 static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
    281   /* If there is still data from the previous record, flush it. */
    282   if (ssl->s3->wpend_pending) {
    283     return ssl3_write_pending(ssl, type, buf, len);
    284   }
    285 
    286   if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
    287     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    288     return -1;
    289   }
    290 
    291   if (len == 0) {
    292     return 0;
    293   }
    294 
    295   size_t flight_len = 0;
    296   if (ssl->s3->pending_flight != NULL) {
    297     flight_len =
    298         ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset;
    299   }
    300 
    301   size_t max_out = len + SSL_max_seal_overhead(ssl);
    302   if (max_out < len || max_out + flight_len < max_out) {
    303     OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
    304     return -1;
    305   }
    306   max_out += flight_len;
    307 
    308   uint8_t *out;
    309   size_t ciphertext_len;
    310   if (!ssl_write_buffer_init(ssl, &out, max_out)) {
    311     return -1;
    312   }
    313 
    314   /* Add any unflushed handshake data as a prefix. This may be a KeyUpdate
    315    * acknowledgment or 0-RTT key change messages. |pending_flight| must be clear
    316    * when data is added to |write_buffer| or it will be written in the wrong
    317    * order. */
    318   if (ssl->s3->pending_flight != NULL) {
    319     OPENSSL_memcpy(
    320         out, ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
    321         flight_len);
    322     BUF_MEM_free(ssl->s3->pending_flight);
    323     ssl->s3->pending_flight = NULL;
    324     ssl->s3->pending_flight_offset = 0;
    325   }
    326 
    327   if (!tls_seal_record(ssl, out + flight_len, &ciphertext_len,
    328                        max_out - flight_len, type, buf, len)) {
    329     return -1;
    330   }
    331   ssl_write_buffer_set_len(ssl, flight_len + ciphertext_len);
    332 
    333   /* Now that we've made progress on the connection, uncork KeyUpdate
    334    * acknowledgments. */
    335   ssl->s3->key_update_pending = 0;
    336 
    337   /* memorize arguments so that ssl3_write_pending can detect bad write retries
    338    * later */
    339   ssl->s3->wpend_tot = len;
    340   ssl->s3->wpend_buf = buf;
    341   ssl->s3->wpend_type = type;
    342   ssl->s3->wpend_ret = len;
    343   ssl->s3->wpend_pending = 1;
    344 
    345   /* we now just need to write the buffer */
    346   return ssl3_write_pending(ssl, type, buf, len);
    347 }
    348 
    349 static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) {
    350   SSL3_RECORD *rr = &ssl->s3->rrec;
    351 
    352   if (len <= 0) {
    353     return len;
    354   }
    355 
    356   if (len > (int)rr->length) {
    357     len = (int)rr->length;
    358   }
    359 
    360   OPENSSL_memcpy(out, rr->data, len);
    361   if (!peek) {
    362     rr->length -= len;
    363     rr->data += len;
    364     if (rr->length == 0) {
    365       /* The record has been consumed, so we may now clear the buffer. */
    366       ssl_read_buffer_discard(ssl);
    367     }
    368   }
    369   return len;
    370 }
    371 
    372 int ssl3_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
    373                        int peek) {
    374   assert(ssl_can_read(ssl));
    375   assert(ssl->s3->aead_read_ctx != NULL);
    376   *out_got_handshake = 0;
    377 
    378   ssl->method->release_current_message(ssl, 0 /* don't free buffer */);
    379 
    380   SSL3_RECORD *rr = &ssl->s3->rrec;
    381 
    382   for (;;) {
    383     /* A previous iteration may have read a partial handshake message. Do not
    384      * allow more app data in that case. */
    385     int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0;
    386 
    387     /* Get new packet if necessary. */
    388     if (rr->length == 0 && !has_hs_data) {
    389       int ret = ssl3_get_record(ssl);
    390       if (ret <= 0) {
    391         return ret;
    392       }
    393     }
    394 
    395     if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) {
    396       /* If reading 0-RTT data, reject handshake data. 0-RTT data is terminated
    397        * by an alert. */
    398       if (SSL_in_init(ssl)) {
    399         OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
    400         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
    401         return -1;
    402       }
    403 
    404       /* Post-handshake data prior to TLS 1.3 is always renegotiation, which we
    405        * never accept as a server. Otherwise |ssl3_get_message| will send
    406        * |SSL_R_EXCESSIVE_MESSAGE_SIZE|. */
    407       if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
    408         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
    409         OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
    410         return -1;
    411       }
    412 
    413       /* Parse post-handshake handshake messages. */
    414       int ret = ssl3_get_message(ssl);
    415       if (ret <= 0) {
    416         return ret;
    417       }
    418       *out_got_handshake = 1;
    419       return -1;
    420     }
    421 
    422     const int is_early_data_read = ssl->server &&
    423                                    ssl->s3->hs != NULL &&
    424                                    ssl->s3->hs->can_early_read &&
    425                                    ssl3_protocol_version(ssl) >= TLS1_3_VERSION;
    426 
    427     /* Handle the end_of_early_data alert. */
    428     if (rr->type == SSL3_RT_ALERT &&
    429         rr->length == 2 &&
    430         rr->data[0] == SSL3_AL_WARNING &&
    431         rr->data[1] == TLS1_AD_END_OF_EARLY_DATA &&
    432         is_early_data_read) {
    433       /* Consume the record. */
    434       rr->length = 0;
    435       ssl_read_buffer_discard(ssl);
    436       /* Stop accepting early data. */
    437       ssl->s3->hs->can_early_read = 0;
    438       *out_got_handshake = 1;
    439       return -1;
    440     }
    441 
    442     if (rr->type != SSL3_RT_APPLICATION_DATA) {
    443       OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
    444       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
    445       return -1;
    446     }
    447 
    448     if (is_early_data_read) {
    449       if (rr->length > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) {
    450         OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA);
    451         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
    452         return -1;
    453       }
    454 
    455       ssl->s3->hs->early_data_read += rr->length;
    456     }
    457 
    458     if (rr->length != 0) {
    459       return consume_record(ssl, buf, len, peek);
    460     }
    461 
    462     /* Discard empty records and loop again. */
    463   }
    464 }
    465 
    466 int ssl3_read_change_cipher_spec(SSL *ssl) {
    467   SSL3_RECORD *rr = &ssl->s3->rrec;
    468 
    469   if (rr->length == 0) {
    470     int ret = ssl3_get_record(ssl);
    471     if (ret <= 0) {
    472       return ret;
    473     }
    474   }
    475 
    476   if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
    477     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
    478     OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
    479     return -1;
    480   }
    481 
    482   if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
    483     OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
    484     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
    485     return -1;
    486   }
    487 
    488   ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data,
    489                       rr->length);
    490 
    491   rr->length = 0;
    492   ssl_read_buffer_discard(ssl);
    493   return 1;
    494 }
    495 
    496 void ssl3_read_close_notify(SSL *ssl) {
    497   /* Read records until an error or close_notify. */
    498   while (ssl3_get_record(ssl) > 0) {
    499     ;
    500   }
    501 }
    502 
    503 int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) {
    504   SSL3_RECORD *rr = &ssl->s3->rrec;
    505 
    506   for (;;) {
    507     /* Get new packet if necessary. */
    508     if (rr->length == 0) {
    509       int ret = ssl3_get_record(ssl);
    510       if (ret <= 0) {
    511         return ret;
    512       }
    513     }
    514 
    515     /* WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the
    516      * ServerHello and send the remaining encrypted application data records
    517      * as-is. This manifests as an application data record when we expect
    518      * handshake. Report a dedicated error code for this case. */
    519     if (!ssl->server && rr->type == SSL3_RT_APPLICATION_DATA &&
    520         ssl->s3->aead_read_ctx == NULL) {
    521       OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE);
    522       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
    523       return -1;
    524     }
    525 
    526     /* Accept server_plaintext_handshake records when the content type TLS 1.3
    527      * variant is enabled. */
    528     if (rr->type != SSL3_RT_HANDSHAKE &&
    529         !(!ssl->server &&
    530           ssl->tls13_variant == tls13_record_type_experiment &&
    531           ssl->s3->aead_read_ctx == NULL &&
    532           rr->type == SSL3_RT_PLAINTEXT_HANDSHAKE)) {
    533       OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
    534       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
    535       return -1;
    536     }
    537 
    538     if (rr->length != 0) {
    539       return consume_record(ssl, buf, len, 0 /* consume data */);
    540     }
    541 
    542     /* Discard empty records and loop again. */
    543   }
    544 }
    545 
    546 int ssl3_send_alert(SSL *ssl, int level, int desc) {
    547   /* It is illegal to send an alert when we've already sent a closing one. */
    548   if (ssl->s3->send_shutdown != ssl_shutdown_none) {
    549     OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
    550     return -1;
    551   }
    552 
    553   if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
    554     ssl->s3->send_shutdown = ssl_shutdown_close_notify;
    555   } else {
    556     assert(level == SSL3_AL_FATAL);
    557     ssl->s3->send_shutdown = ssl_shutdown_fatal_alert;
    558   }
    559 
    560   ssl->s3->alert_dispatch = 1;
    561   ssl->s3->send_alert[0] = level;
    562   ssl->s3->send_alert[1] = desc;
    563   if (!ssl_write_buffer_is_pending(ssl)) {
    564     /* Nothing is being written out, so the alert may be dispatched
    565      * immediately. */
    566     return ssl->method->dispatch_alert(ssl);
    567   }
    568 
    569   /* The alert will be dispatched later. */
    570   return -1;
    571 }
    572 
    573 int ssl3_dispatch_alert(SSL *ssl) {
    574   int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
    575   if (ret <= 0) {
    576     return ret;
    577   }
    578   ssl->s3->alert_dispatch = 0;
    579 
    580   /* If the alert is fatal, flush the BIO now. */
    581   if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
    582     BIO_flush(ssl->wbio);
    583   }
    584 
    585   ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert,
    586                       2);
    587 
    588   int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
    589   ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
    590 
    591   return 1;
    592 }
    593