Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/s3_msg.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include "ssl_local.h"
11
#include "internal/ssl_unwrap.h"
12
13
int ssl3_do_change_cipher_spec(SSL_CONNECTION *s)
14
0
{
15
0
    int i;
16
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
17
18
0
    if (s->server)
19
0
        i = SSL3_CHANGE_CIPHER_SERVER_READ;
20
0
    else
21
0
        i = SSL3_CHANGE_CIPHER_CLIENT_READ;
22
23
0
    if (s->s3.tmp.key_block == NULL) {
24
0
        if (s->session == NULL || s->session->master_key_length == 0) {
25
            /* might happen if dtls1_read_bytes() calls this */
26
0
            ERR_raise(ERR_LIB_SSL, SSL_R_CCS_RECEIVED_EARLY);
27
0
            return 0;
28
0
        }
29
30
0
        s->session->cipher = s->s3.tmp.new_cipher;
31
0
        if (!ssl->method->ssl3_enc->setup_key_block(s)) {
32
            /* SSLfatal() already called */
33
0
            return 0;
34
0
        }
35
0
    }
36
37
0
    if (!ssl->method->ssl3_enc->change_cipher_state(s, i)) {
38
        /* SSLfatal() already called */
39
0
        return 0;
40
0
    }
41
42
0
    return 1;
43
0
}
44
45
int ssl3_send_alert(SSL_CONNECTION *s, int level, int desc)
46
0
{
47
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
48
49
    /* Map tls/ssl alert value to correct one */
50
0
    if (SSL_CONNECTION_TREAT_AS_TLS13(s))
51
0
        desc = tls13_alert_code(desc);
52
0
    else
53
0
        desc = ssl->method->ssl3_enc->alert_value(desc);
54
0
    if (desc < 0)
55
0
        return -1;
56
0
    if (s->shutdown & SSL_SENT_SHUTDOWN && desc != SSL_AD_CLOSE_NOTIFY)
57
0
        return -1;
58
    /* If a fatal one, remove from cache */
59
0
    if ((level == SSL3_AL_FATAL) && (s->session != NULL))
60
0
        SSL_CTX_remove_session(s->session_ctx, s->session);
61
62
0
    s->s3.alert_dispatch = SSL_ALERT_DISPATCH_PENDING;
63
0
    s->s3.send_alert[0] = level;
64
0
    s->s3.send_alert[1] = desc;
65
0
    if (!RECORD_LAYER_write_pending(&s->rlayer)) {
66
        /* data still being written out? */
67
0
        return ssl->method->ssl_dispatch_alert(ssl);
68
0
    }
69
    /*
70
     * else data is still being written out, we will get written some time in
71
     * the future
72
     */
73
0
    return -1;
74
0
}
75
76
int ssl3_dispatch_alert(SSL *s)
77
0
{
78
0
    int i, j;
79
0
    void (*cb)(const SSL *ssl, int type, int val) = NULL;
80
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
81
0
    OSSL_RECORD_TEMPLATE templ;
82
83
0
    if (sc == NULL)
84
0
        return -1;
85
86
0
    if (sc->rlayer.wrlmethod == NULL) {
87
        /* No write record layer so we can't sent and alert. We just ignore it */
88
0
        sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
89
0
        return 1;
90
0
    }
91
92
0
    templ.type = SSL3_RT_ALERT;
93
0
    templ.version = (sc->version == TLS1_3_VERSION) ? TLS1_2_VERSION
94
0
                                                    : sc->version;
95
0
    if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
96
0
        && !sc->renegotiate
97
0
        && TLS1_get_version(s) > TLS1_VERSION
98
0
        && sc->hello_retry_request == SSL_HRR_NONE) {
99
0
        templ.version = TLS1_VERSION;
100
0
    }
101
0
    templ.buf = &sc->s3.send_alert[0];
102
0
    templ.buflen = 2;
103
104
0
    if (RECORD_LAYER_write_pending(&sc->rlayer)) {
105
0
        if (sc->s3.alert_dispatch != SSL_ALERT_DISPATCH_RETRY) {
106
            /*
107
             * We have a write pending but it wasn't from a previous call to
108
             * this function! Can we ever get here? Maybe via API misuse??
109
             * Give up.
110
             */
111
0
            sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
112
0
            return -1;
113
0
        }
114
        /* Retry what we've already got pending */
115
0
        i = HANDLE_RLAYER_WRITE_RETURN(sc,
116
0
            sc->rlayer.wrlmethod->retry_write_records(sc->rlayer.wrl));
117
0
        if (i <= 0) {
118
            /* Could be NBIO. Keep alert_dispatch as SSL_ALERT_DISPATCH_RETRY */
119
0
            return -1;
120
0
        }
121
0
        sc->rlayer.wpend_tot = 0;
122
0
        sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
123
0
        return 1;
124
0
    }
125
126
0
    i = HANDLE_RLAYER_WRITE_RETURN(sc,
127
0
        sc->rlayer.wrlmethod->write_records(sc->rlayer.wrl, &templ, 1));
128
129
0
    if (i <= 0) {
130
0
        sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_RETRY;
131
0
        sc->rlayer.wpend_tot = templ.buflen;
132
0
        sc->rlayer.wpend_type = templ.type;
133
0
        sc->rlayer.wpend_buf = templ.buf;
134
0
    } else {
135
        /*
136
         * Alert sent to BIO - now flush. If the message does not get sent due
137
         * to non-blocking IO, we will not worry too much.
138
         */
139
0
        (void)BIO_flush(sc->wbio);
140
0
        sc->s3.alert_dispatch = SSL_ALERT_DISPATCH_NONE;
141
142
0
        if (sc->msg_callback)
143
0
            sc->msg_callback(1, sc->version, SSL3_RT_ALERT, sc->s3.send_alert,
144
0
                2, s, sc->msg_callback_arg);
145
146
0
        if (sc->info_callback != NULL)
147
0
            cb = sc->info_callback;
148
0
        else if (s->ctx->info_callback != NULL)
149
0
            cb = s->ctx->info_callback;
150
151
0
        if (cb != NULL) {
152
0
            j = (sc->s3.send_alert[0] << 8) | sc->s3.send_alert[1];
153
0
            cb(s, SSL_CB_WRITE_ALERT, j);
154
0
        }
155
0
    }
156
0
    return i;
157
0
}