/src/openssl/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 | 906k | { |
20 | 906k | OPENSSL_clear_free(s->s3.tmp.key_block, s->s3.tmp.key_block_length); |
21 | 906k | s->s3.tmp.key_block = NULL; |
22 | 906k | s->s3.tmp.key_block_length = 0; |
23 | 906k | } |
24 | | |
25 | | int ssl3_init_finished_mac(SSL_CONNECTION *s) |
26 | 283k | { |
27 | 283k | BIO *buf = BIO_new(BIO_s_mem()); |
28 | | |
29 | 283k | if (buf == NULL) { |
30 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BIO_LIB); |
31 | 0 | return 0; |
32 | 0 | } |
33 | 283k | ssl3_free_digest_list(s); |
34 | 283k | s->s3.handshake_buffer = buf; |
35 | 283k | (void)BIO_set_close(s->s3.handshake_buffer, BIO_CLOSE); |
36 | 283k | return 1; |
37 | 283k | } |
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 | 1.14M | { |
46 | 1.14M | BIO_free(s->s3.handshake_buffer); |
47 | 1.14M | s->s3.handshake_buffer = NULL; |
48 | 1.14M | EVP_MD_CTX_free(s->s3.handshake_dgst); |
49 | 1.14M | s->s3.handshake_dgst = NULL; |
50 | 1.14M | } |
51 | | |
52 | | int ssl3_finish_mac(SSL_CONNECTION *s, const unsigned char *buf, size_t len) |
53 | 265k | { |
54 | 265k | int ret; |
55 | | |
56 | 265k | if (s->s3.handshake_dgst == NULL) { |
57 | | /* Note: this writes to a memory BIO so a failure is a fatal error */ |
58 | 158k | if (len > INT_MAX) { |
59 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_OVERFLOW_ERROR); |
60 | 0 | return 0; |
61 | 0 | } |
62 | 158k | ret = BIO_write(s->s3.handshake_buffer, (void *)buf, (int)len); |
63 | 158k | if (ret <= 0 || ret != (int)len) { |
64 | 5 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
65 | 5 | return 0; |
66 | 5 | } |
67 | 158k | } 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 | 106k | if (s->negotiated_version == DTLS1_3_VERSION) { |
82 | 592 | 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 | 106k | } else { |
87 | 106k | 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 | 106k | } |
92 | 106k | } |
93 | 265k | return 1; |
94 | 265k | } |
95 | | |
96 | | int ssl3_digest_cached_records(SSL_CONNECTION *s, int keep) |
97 | 83.8k | { |
98 | 83.8k | const EVP_MD *md; |
99 | 83.8k | long hdatalen; |
100 | 83.8k | void *hdata; |
101 | | |
102 | 83.8k | if (s->s3.handshake_dgst == NULL) { |
103 | 26.2k | hdatalen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata); |
104 | 26.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 | 26.2k | s->s3.handshake_dgst = EVP_MD_CTX_new(); |
110 | 26.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 | 26.2k | md = ssl_handshake_md(s); |
116 | 26.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 | 26.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 | 26.2k | if (!ssl3_finish_mac(s, hdata, hdatalen)) { |
126 | | /* SSLfatal() already called */ |
127 | 0 | return 0; |
128 | 0 | } |
129 | 26.2k | } |
130 | 83.8k | if (keep == 0) { |
131 | 36.5k | BIO_free(s->s3.handshake_buffer); |
132 | 36.5k | s->s3.handshake_buffer = NULL; |
133 | 36.5k | } |
134 | | |
135 | 83.8k | return 1; |
136 | 83.8k | } |