2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "ssl_local.h"
12 int ssl3_do_change_cipher_spec(SSL *s)
17 i = SSL3_CHANGE_CIPHER_SERVER_READ;
19 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
21 if (s->s3.tmp.key_block == NULL) {
22 if (s->session == NULL || s->session->master_key_length == 0) {
23 /* might happen if dtls1_read_bytes() calls this */
24 ERR_raise(ERR_LIB_SSL, SSL_R_CCS_RECEIVED_EARLY);
28 s->session->cipher = s->s3.tmp.new_cipher;
29 if (!s->method->ssl3_enc->setup_key_block(s)) {
30 /* SSLfatal() already called */
35 if (!s->method->ssl3_enc->change_cipher_state(s, i)) {
36 /* SSLfatal() already called */
43 int ssl3_send_alert(SSL *s, int level, int desc)
45 /* Map tls/ssl alert value to correct one */
46 if (SSL_TREAT_AS_TLS13(s))
47 desc = tls13_alert_code(desc);
49 desc = s->method->ssl3_enc->alert_value(desc);
50 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
51 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
52 * protocol_version alerts */
55 if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY)
57 /* If a fatal one, remove from cache */
58 if ((level == SSL3_AL_FATAL) && (s->session != NULL))
59 SSL_CTX_remove_session(s->session_ctx, s->session);
61 s->s3.alert_dispatch = 1;
62 s->s3.send_alert[0] = level;
63 s->s3.send_alert[1] = desc;
64 if (!RECORD_LAYER_write_pending(&s->rlayer)) {
65 /* data still being written out? */
66 return s->method->ssl_dispatch_alert(s);
69 * else data is still being written out, we will get written some time in
75 int ssl3_dispatch_alert(SSL *s)
79 void (*cb) (const SSL *ssl, int type, int val) = NULL;
82 s->s3.alert_dispatch = 0;
84 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3.send_alert[0], &alertlen, 1, 0,
87 s->s3.alert_dispatch = 1;
90 * Alert sent to BIO - now flush. If the message does not get sent due
91 * to non-blocking IO, we will not worry too much.
93 (void)BIO_flush(s->wbio);
96 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3.send_alert,
97 2, s, s->msg_callback_arg);
99 if (s->info_callback != NULL)
100 cb = s->info_callback;
101 else if (s->ctx->info_callback != NULL)
102 cb = s->ctx->info_callback;
105 j = (s->s3.send_alert[0] << 8) | s->s3.send_alert[1];
106 cb(s, SSL_CB_WRITE_ALERT, j);