1*4724848cSchristos /*
2*4724848cSchristos * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include "ssl_local.h"
11*4724848cSchristos
ssl3_do_change_cipher_spec(SSL * s)12*4724848cSchristos int ssl3_do_change_cipher_spec(SSL *s)
13*4724848cSchristos {
14*4724848cSchristos int i;
15*4724848cSchristos
16*4724848cSchristos if (s->server)
17*4724848cSchristos i = SSL3_CHANGE_CIPHER_SERVER_READ;
18*4724848cSchristos else
19*4724848cSchristos i = SSL3_CHANGE_CIPHER_CLIENT_READ;
20*4724848cSchristos
21*4724848cSchristos if (s->s3->tmp.key_block == NULL) {
22*4724848cSchristos if (s->session == NULL || s->session->master_key_length == 0) {
23*4724848cSchristos /* might happen if dtls1_read_bytes() calls this */
24*4724848cSchristos SSLerr(SSL_F_SSL3_DO_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
25*4724848cSchristos return 0;
26*4724848cSchristos }
27*4724848cSchristos
28*4724848cSchristos s->session->cipher = s->s3->tmp.new_cipher;
29*4724848cSchristos if (!s->method->ssl3_enc->setup_key_block(s))
30*4724848cSchristos return 0;
31*4724848cSchristos }
32*4724848cSchristos
33*4724848cSchristos if (!s->method->ssl3_enc->change_cipher_state(s, i))
34*4724848cSchristos return 0;
35*4724848cSchristos
36*4724848cSchristos return 1;
37*4724848cSchristos }
38*4724848cSchristos
ssl3_send_alert(SSL * s,int level,int desc)39*4724848cSchristos int ssl3_send_alert(SSL *s, int level, int desc)
40*4724848cSchristos {
41*4724848cSchristos /* Map tls/ssl alert value to correct one */
42*4724848cSchristos if (SSL_TREAT_AS_TLS13(s))
43*4724848cSchristos desc = tls13_alert_code(desc);
44*4724848cSchristos else
45*4724848cSchristos desc = s->method->ssl3_enc->alert_value(desc);
46*4724848cSchristos if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
47*4724848cSchristos desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have
48*4724848cSchristos * protocol_version alerts */
49*4724848cSchristos if (desc < 0)
50*4724848cSchristos return -1;
51*4724848cSchristos if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY)
52*4724848cSchristos return -1;
53*4724848cSchristos /* If a fatal one, remove from cache */
54*4724848cSchristos if ((level == SSL3_AL_FATAL) && (s->session != NULL))
55*4724848cSchristos SSL_CTX_remove_session(s->session_ctx, s->session);
56*4724848cSchristos
57*4724848cSchristos s->s3->alert_dispatch = 1;
58*4724848cSchristos s->s3->send_alert[0] = level;
59*4724848cSchristos s->s3->send_alert[1] = desc;
60*4724848cSchristos if (!RECORD_LAYER_write_pending(&s->rlayer)) {
61*4724848cSchristos /* data still being written out? */
62*4724848cSchristos return s->method->ssl_dispatch_alert(s);
63*4724848cSchristos }
64*4724848cSchristos /*
65*4724848cSchristos * else data is still being written out, we will get written some time in
66*4724848cSchristos * the future
67*4724848cSchristos */
68*4724848cSchristos return -1;
69*4724848cSchristos }
70*4724848cSchristos
ssl3_dispatch_alert(SSL * s)71*4724848cSchristos int ssl3_dispatch_alert(SSL *s)
72*4724848cSchristos {
73*4724848cSchristos int i, j;
74*4724848cSchristos size_t alertlen;
75*4724848cSchristos void (*cb) (const SSL *ssl, int type, int val) = NULL;
76*4724848cSchristos size_t written;
77*4724848cSchristos
78*4724848cSchristos s->s3->alert_dispatch = 0;
79*4724848cSchristos alertlen = 2;
80*4724848cSchristos i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], &alertlen, 1, 0,
81*4724848cSchristos &written);
82*4724848cSchristos if (i <= 0) {
83*4724848cSchristos s->s3->alert_dispatch = 1;
84*4724848cSchristos } else {
85*4724848cSchristos /*
86*4724848cSchristos * Alert sent to BIO - now flush. If the message does not get sent due
87*4724848cSchristos * to non-blocking IO, we will not worry too much.
88*4724848cSchristos */
89*4724848cSchristos (void)BIO_flush(s->wbio);
90*4724848cSchristos
91*4724848cSchristos if (s->msg_callback)
92*4724848cSchristos s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
93*4724848cSchristos 2, s, s->msg_callback_arg);
94*4724848cSchristos
95*4724848cSchristos if (s->info_callback != NULL)
96*4724848cSchristos cb = s->info_callback;
97*4724848cSchristos else if (s->ctx->info_callback != NULL)
98*4724848cSchristos cb = s->ctx->info_callback;
99*4724848cSchristos
100*4724848cSchristos if (cb != NULL) {
101*4724848cSchristos j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
102*4724848cSchristos cb(s, SSL_CB_WRITE_ALERT, j);
103*4724848cSchristos }
104*4724848cSchristos }
105*4724848cSchristos return i;
106*4724848cSchristos }
107