Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/ssl/s3_enc.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2005 Nokia. All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <stdio.h>
12
#include "ssl_local.h"
13
#include <openssl/evp.h>
14
#include <openssl/core_names.h>
15
#include "internal/cryptlib.h"
16
#include "internal/ssl_unwrap.h"
17
18
void ssl3_cleanup_key_block(SSL_CONNECTION *s)
19
715k
{
20
715k
    OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length);
21
715k
    s->s3.tmp.key_block = NULL;
22
715k
    s->s3.tmp.key_block_length = 0;
23
715k
}
24
25
int ssl3_init_finished_mac(SSL_CONNECTION *s)
26
225k
{
27
225k
    BIO *buf = BIO_new(BIO_s_mem());
28
29
225k
    if (buf == NULL) {
30
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BIO_LIB);
31
0
        return 0;
32
0
    }
33
225k
    ssl3_free_digest_list(s);
34
225k
    s->s3.handshake_buffer = buf;
35
225k
    (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE);
36
225k
    return 1;
37
225k
}
38
39
/*
40
 * Free digest list. Also frees handshake buffer since they are always freed
41
 * together.
42
 */
43
44
void ssl3_free_digest_list(SSL_CONNECTION *s)
45
905k
{
46
905k
    BIO_free(s->s3.handshake_buffer);
47
905k
    s->s3.handshake_buffer = NULL;
48
905k
    EVP_MD_CTX_free(s->s3.handshake_dgst);
49
905k
    s->s3.handshake_dgst = NULL;
50
905k
}
51
52
int ssl3_finish_mac(SSL_CONNECTION *s, const unsigned char *buf, size_t len)
53
143k
{
54
143k
    int ret;
55
56
143k
    if (s->s3.handshake_dgst == NULL) {
57
        /* Note: this writes to a memory BIO so a failure is a fatal error */
58
87.0k
        if (len > INT_MAX) {
59
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_OVERFLOW_ERROR);
60
0
            return 0;
61
0
        }
62
87.0k
        ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len);
63
87.0k
        if (ret <= 0 || ret != (int)len) {
64
2
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
65
2
            return 0;
66
2
        }
67
87.0k
    } else {
68
        /*
69
         * rfc9147:
70
         * In DTLS 1.3, the message transcript is computed over the
71
         * original TLS 1.3-style Handshake messages without the
72
         * message_seq, fragment_offset, and fragment_length values. Note
73
         * that this is a change from DTLS 1.2 where those values were
74
         * included in the transcript.
75
         *
76
         * So this means that we record the full handshake messages in
77
         * s->s3.handshake_buffer while s->s3.handshake_dgst is not in use and then
78
         * we calculate the digest when initiating s->s3.handshake_dgst at which
79
         * point we know what the protocol version is.
80
         */
81
56.2k
        if (s->negotiated_version == DTLS1_3_VERSION) {
82
0
            if (!dtls13_transcript_hash_update(s->s3.handshake_dgst, buf, len)) {
83
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
84
0
                return 0;
85
0
            }
86
56.2k
        } else {
87
56.2k
            if (!EVP_DigestUpdate(s->s3.handshake_dgst, buf, len)) {
88
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
89
0
                return 0;
90
0
            }
91
56.2k
        }
92
56.2k
    }
93
143k
    return 1;
94
143k
}
95
96
int ssl3_digest_cached_records(SSL_CONNECTION *s, int keep)
97
44.1k
{
98
44.1k
    const EVP_MD *md;
99
44.1k
    long hdatalen;
100
44.1k
    void *hdata;
101
102
44.1k
    if (s->s3.handshake_dgst == NULL) {
103
13.2k
        hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
104
13.2k
        if (hdatalen <= 0) {
105
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
106
0
            return 0;
107
0
        }
108
109
13.2k
        s->s3.handshake_dgst = EVP_MD_CTX_new();
110
13.2k
        if (s->s3.handshake_dgst == NULL) {
111
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
112
0
            return 0;
113
0
        }
114
115
13.2k
        md = ssl_handshake_md(s);
116
13.2k
        if (md == NULL) {
117
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
118
0
                SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
119
0
            return 0;
120
0
        }
121
13.2k
        if (!EVP_DigestInit_ex(s->s3.handshake_dgst, md, NULL)) {
122
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
123
0
            return 0;
124
0
        }
125
13.2k
        if (!ssl3_finish_mac(s, hdata, hdatalen)) {
126
            /* SSLfatal() already called */
127
0
            return 0;
128
0
        }
129
13.2k
    }
130
44.1k
    if (keep == 0) {
131
18.6k
        BIO_free(s->s3.handshake_buffer);
132
18.6k
        s->s3.handshake_buffer = NULL;
133
18.6k
    }
134
135
44.1k
    return 1;
136
44.1k
}