Coverage Report

Created: 2023-09-25 06:41

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