/src/libressl/ssl/ssl_transcript.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: ssl_transcript.c,v 1.9 2022/11/26 16:08:56 tb Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2017 Joel Sing <jsing@openbsd.org> |
4 | | * |
5 | | * Permission to use, copy, modify, and distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | | */ |
17 | | |
18 | | #include <openssl/ssl.h> |
19 | | |
20 | | #include "ssl_local.h" |
21 | | #include "tls_internal.h" |
22 | | |
23 | | int |
24 | | tls1_transcript_hash_init(SSL *s) |
25 | 6.90k | { |
26 | 6.90k | const unsigned char *data; |
27 | 6.90k | const EVP_MD *md; |
28 | 6.90k | size_t len; |
29 | | |
30 | 6.90k | tls1_transcript_hash_free(s); |
31 | | |
32 | 6.90k | if (!ssl_get_handshake_evp_md(s, &md)) { |
33 | 0 | SSLerrorx(ERR_R_INTERNAL_ERROR); |
34 | 0 | goto err; |
35 | 0 | } |
36 | | |
37 | 6.90k | if ((s->s3->handshake_hash = EVP_MD_CTX_new()) == NULL) { |
38 | 0 | SSLerror(s, ERR_R_MALLOC_FAILURE); |
39 | 0 | goto err; |
40 | 0 | } |
41 | 6.90k | if (!EVP_DigestInit_ex(s->s3->handshake_hash, md, NULL)) { |
42 | 0 | SSLerror(s, ERR_R_EVP_LIB); |
43 | 0 | goto err; |
44 | 0 | } |
45 | | |
46 | 6.90k | if (!tls1_transcript_data(s, &data, &len)) { |
47 | 0 | SSLerror(s, SSL_R_BAD_HANDSHAKE_LENGTH); |
48 | 0 | goto err; |
49 | 0 | } |
50 | 6.90k | if (!tls1_transcript_hash_update(s, data, len)) { |
51 | 0 | SSLerror(s, ERR_R_EVP_LIB); |
52 | 0 | goto err; |
53 | 0 | } |
54 | | |
55 | 6.90k | return 1; |
56 | | |
57 | 0 | err: |
58 | 0 | tls1_transcript_hash_free(s); |
59 | |
|
60 | 0 | return 0; |
61 | 6.90k | } |
62 | | |
63 | | int |
64 | | tls1_transcript_hash_update(SSL *s, const unsigned char *buf, size_t len) |
65 | 41.8k | { |
66 | 41.8k | if (s->s3->handshake_hash == NULL) |
67 | 19.5k | return 1; |
68 | | |
69 | 22.3k | return EVP_DigestUpdate(s->s3->handshake_hash, buf, len); |
70 | 41.8k | } |
71 | | |
72 | | int |
73 | | tls1_transcript_hash_value(SSL *s, unsigned char *out, size_t len, |
74 | | size_t *outlen) |
75 | 1.74k | { |
76 | 1.74k | EVP_MD_CTX *mdctx = NULL; |
77 | 1.74k | unsigned int mdlen; |
78 | 1.74k | int ret = 0; |
79 | | |
80 | 1.74k | if (s->s3->handshake_hash == NULL) |
81 | 0 | goto err; |
82 | | |
83 | 1.74k | if (EVP_MD_CTX_size(s->s3->handshake_hash) > len) |
84 | 0 | goto err; |
85 | | |
86 | 1.74k | if ((mdctx = EVP_MD_CTX_new()) == NULL) { |
87 | 0 | SSLerror(s, ERR_R_MALLOC_FAILURE); |
88 | 0 | goto err; |
89 | 0 | } |
90 | 1.74k | if (!EVP_MD_CTX_copy_ex(mdctx, s->s3->handshake_hash)) { |
91 | 0 | SSLerror(s, ERR_R_EVP_LIB); |
92 | 0 | goto err; |
93 | 0 | } |
94 | 1.74k | if (!EVP_DigestFinal_ex(mdctx, out, &mdlen)) { |
95 | 0 | SSLerror(s, ERR_R_EVP_LIB); |
96 | 0 | goto err; |
97 | 0 | } |
98 | 1.74k | if (outlen != NULL) |
99 | 1.74k | *outlen = mdlen; |
100 | | |
101 | 1.74k | ret = 1; |
102 | | |
103 | 1.74k | err: |
104 | 1.74k | EVP_MD_CTX_free(mdctx); |
105 | | |
106 | 1.74k | return (ret); |
107 | 1.74k | } |
108 | | |
109 | | void |
110 | | tls1_transcript_hash_free(SSL *s) |
111 | 44.8k | { |
112 | 44.8k | EVP_MD_CTX_free(s->s3->handshake_hash); |
113 | 44.8k | s->s3->handshake_hash = NULL; |
114 | 44.8k | } |
115 | | |
116 | | int |
117 | | tls1_transcript_init(SSL *s) |
118 | 9.47k | { |
119 | 9.47k | if (s->s3->handshake_transcript != NULL) |
120 | 0 | return 0; |
121 | | |
122 | 9.47k | if ((s->s3->handshake_transcript = tls_buffer_new(0)) == NULL) |
123 | 0 | return 0; |
124 | | |
125 | 9.47k | tls1_transcript_reset(s); |
126 | | |
127 | 9.47k | return 1; |
128 | 9.47k | } |
129 | | |
130 | | void |
131 | | tls1_transcript_free(SSL *s) |
132 | 42.5k | { |
133 | 42.5k | tls_buffer_free(s->s3->handshake_transcript); |
134 | 42.5k | s->s3->handshake_transcript = NULL; |
135 | 42.5k | } |
136 | | |
137 | | void |
138 | | tls1_transcript_reset(SSL *s) |
139 | 9.55k | { |
140 | 9.55k | tls_buffer_clear(s->s3->handshake_transcript); |
141 | | |
142 | 9.55k | tls1_transcript_unfreeze(s); |
143 | 9.55k | } |
144 | | |
145 | | int |
146 | | tls1_transcript_append(SSL *s, const unsigned char *buf, size_t len) |
147 | 34.9k | { |
148 | 34.9k | if (s->s3->handshake_transcript == NULL) |
149 | 9.31k | return 1; |
150 | | |
151 | 25.6k | if (s->s3->flags & TLS1_FLAGS_FREEZE_TRANSCRIPT) |
152 | 5.22k | return 1; |
153 | | |
154 | 20.4k | return tls_buffer_append(s->s3->handshake_transcript, buf, len); |
155 | 25.6k | } |
156 | | |
157 | | int |
158 | | tls1_transcript_data(SSL *s, const unsigned char **data, size_t *len) |
159 | 6.90k | { |
160 | 6.90k | CBS cbs; |
161 | | |
162 | 6.90k | if (s->s3->handshake_transcript == NULL) |
163 | 0 | return 0; |
164 | | |
165 | 6.90k | if (!tls_buffer_data(s->s3->handshake_transcript, &cbs)) |
166 | 0 | return 0; |
167 | | |
168 | | /* XXX - change to caller providing a CBS argument. */ |
169 | 6.90k | *data = CBS_data(&cbs); |
170 | 6.90k | *len = CBS_len(&cbs); |
171 | | |
172 | 6.90k | return 1; |
173 | 6.90k | } |
174 | | |
175 | | void |
176 | | tls1_transcript_freeze(SSL *s) |
177 | 5.65k | { |
178 | 5.65k | s->s3->flags |= TLS1_FLAGS_FREEZE_TRANSCRIPT; |
179 | 5.65k | } |
180 | | |
181 | | void |
182 | | tls1_transcript_unfreeze(SSL *s) |
183 | 14.6k | { |
184 | 14.6k | s->s3->flags &= ~TLS1_FLAGS_FREEZE_TRANSCRIPT; |
185 | 14.6k | } |
186 | | |
187 | | int |
188 | | tls1_transcript_record(SSL *s, const unsigned char *buf, size_t len) |
189 | 34.9k | { |
190 | 34.9k | if (!tls1_transcript_hash_update(s, buf, len)) |
191 | 0 | return 0; |
192 | | |
193 | 34.9k | if (!tls1_transcript_append(s, buf, len)) |
194 | 0 | return 0; |
195 | | |
196 | 34.9k | return 1; |
197 | 34.9k | } |