/src/gnutls/lib/ext/compress_certificate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2022 Red Hat, Inc. |
3 | | * |
4 | | * Author: Zoltan Fridrich |
5 | | * |
6 | | * This file is part of GnuTLS. |
7 | | * |
8 | | * The GnuTLS is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public License |
10 | | * as published by the Free Software Foundation; either version 2.1 of |
11 | | * the License, or (at your option) any later version. |
12 | | * |
13 | | * This library is distributed in the hope that it will be useful, but |
14 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
20 | | * |
21 | | */ |
22 | | |
23 | | #include "compress.h" |
24 | | #include "errors.h" |
25 | | #include "gnutls_int.h" |
26 | | #include "hello_ext_lib.h" |
27 | | #include "num.h" |
28 | | #include "ext/compress_certificate.h" |
29 | | |
30 | | /* Converts compression algorithm number established in RFC8879 to internal compression method type |
31 | | */ |
32 | | gnutls_compression_method_t |
33 | | _gnutls_compress_certificate_num2method(uint16_t num) |
34 | 0 | { |
35 | 0 | switch (num) { |
36 | 0 | case 1: |
37 | 0 | return GNUTLS_COMP_ZLIB; |
38 | 0 | case 2: |
39 | 0 | return GNUTLS_COMP_BROTLI; |
40 | 0 | case 3: |
41 | 0 | return GNUTLS_COMP_ZSTD; |
42 | 0 | default: |
43 | 0 | return GNUTLS_COMP_UNKNOWN; |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | /* Converts compression method type to compression algorithm number established in RFC8879 |
48 | | */ |
49 | | int _gnutls_compress_certificate_method2num(gnutls_compression_method_t method) |
50 | 0 | { |
51 | 0 | switch (method) { |
52 | 0 | case GNUTLS_COMP_ZLIB: |
53 | 0 | return 1; |
54 | 0 | case GNUTLS_COMP_BROTLI: |
55 | 0 | return 2; |
56 | 0 | case GNUTLS_COMP_ZSTD: |
57 | 0 | return 3; |
58 | 0 | default: |
59 | 0 | return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | /* Returns 1 if the method is set as supported compression method for the session, |
64 | | * returns 0 otherwise |
65 | | */ |
66 | | bool _gnutls_compress_certificate_is_method_enabled( |
67 | | gnutls_session_t session, gnutls_compression_method_t method) |
68 | 0 | { |
69 | 0 | int ret; |
70 | 0 | unsigned i; |
71 | 0 | compress_certificate_ext_st *priv; |
72 | 0 | gnutls_ext_priv_data_t epriv; |
73 | |
|
74 | 0 | ret = _gnutls_hello_ext_get_priv( |
75 | 0 | session, GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, &epriv); |
76 | 0 | if (ret < 0) |
77 | 0 | return false; |
78 | 0 | priv = epriv; |
79 | |
|
80 | 0 | for (i = 0; i < priv->methods_len; ++i) |
81 | 0 | if (priv->methods[i] == method) |
82 | 0 | return true; |
83 | | |
84 | 0 | return false; |
85 | 0 | } |
86 | | |
87 | | /* Returns true when compression methods are set, false otherwise |
88 | | */ |
89 | | bool _gnutls_compress_certificate_is_set(gnutls_session_t session) |
90 | 1.15M | { |
91 | 1.15M | compress_certificate_ext_st *priv; |
92 | 1.15M | gnutls_ext_priv_data_t epriv; |
93 | | |
94 | 1.15M | if (_gnutls_hello_ext_get_priv( |
95 | 1.15M | session, GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, &epriv) < 0) |
96 | 1.15M | return false; |
97 | 0 | priv = epriv; |
98 | |
|
99 | 0 | return priv->methods_len != 0; |
100 | 1.15M | } |
101 | | |
102 | | /** |
103 | | * gnutls_compress_certificate_get_selected_method: |
104 | | * @session: is a #gnutls_session_t type. |
105 | | * |
106 | | * This function returns the certificate compression method that has been |
107 | | * selected to compress the certificate before sending it to the peer. |
108 | | * The selection is done based on the local list of supported compression |
109 | | * methods and the peer's requested compression methods. |
110 | | * |
111 | | * Returns: selected certificate compression method. |
112 | | * |
113 | | * Since 3.7.4 |
114 | | **/ |
115 | | gnutls_compression_method_t |
116 | | gnutls_compress_certificate_get_selected_method(gnutls_session_t session) |
117 | 1.50k | { |
118 | 1.50k | return session->internals.compress_certificate_method; |
119 | 1.50k | } |
120 | | |
121 | | /** |
122 | | * gnutls_compress_certificate_set_methods: |
123 | | * @session: is a #gnutls_session_t type. |
124 | | * @methods: is a list of supported compression methods. |
125 | | * @methods_len: number of compression methods in @methods |
126 | | * |
127 | | * This function sets the supported compression methods for certificate compression |
128 | | * for the given session. The list of supported compression methods will be used |
129 | | * for a) requesting the compression of peer's certificate and b) selecting the |
130 | | * method to compress the local certificate before sending it to the peer. |
131 | | * The order of compression methods inside the list does matter as the method |
132 | | * that appears earlier in the list will be preferred before the later ones. |
133 | | * Note that even if you set the list of supported compression methods, the |
134 | | * compression might not be used if the peer does not support any of your chosen |
135 | | * compression methods. |
136 | | * |
137 | | * The list of supported compression methods must meet the following criteria: |
138 | | * Argument @methods must be an array of valid compression methods of type |
139 | | * #gnutls_compression_method_t. Argument @methods_len must contain the number of |
140 | | * compression methods stored in the @methods array and must be within range <1, 127>. |
141 | | * The length constraints are defined by %MIN_COMPRESS_CERTIFICATE_METHODS |
142 | | * and %MAX_COMPRESS_CERTIFICATE_METHODS macros located in the header file |
143 | | * compress_certificate.h. |
144 | | * |
145 | | * If either @methods or @methods_len is equal to 0, current list of supported |
146 | | * compression methods will be unset. |
147 | | * |
148 | | * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code. |
149 | | * |
150 | | * Since 3.7.4 |
151 | | **/ |
152 | | int gnutls_compress_certificate_set_methods( |
153 | | gnutls_session_t session, const gnutls_compression_method_t *methods, |
154 | | size_t methods_len) |
155 | 0 | { |
156 | 0 | int ret; |
157 | 0 | unsigned i; |
158 | 0 | compress_certificate_ext_st *priv; |
159 | |
|
160 | 0 | if (methods == NULL || methods_len == 0) { |
161 | 0 | _gnutls_hello_ext_unset_priv( |
162 | 0 | session, GNUTLS_EXTENSION_COMPRESS_CERTIFICATE); |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 0 | if (methods_len > MAX_COMPRESS_CERTIFICATE_METHODS) |
167 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
168 | | |
169 | 0 | for (i = 0; i < methods_len; ++i) |
170 | 0 | if ((ret = _gnutls_compression_init_method(methods[i])) < 0) |
171 | 0 | return gnutls_assert_val(ret); |
172 | | |
173 | 0 | priv = gnutls_malloc(sizeof(*priv)); |
174 | 0 | if (priv == NULL) |
175 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
176 | | |
177 | 0 | priv->methods_len = methods_len; |
178 | 0 | memcpy(priv->methods, methods, methods_len * sizeof(*methods)); |
179 | 0 | _gnutls_hello_ext_set_priv(session, |
180 | 0 | GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, priv); |
181 | |
|
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | | int _gnutls_compress_certificate_recv_params(gnutls_session_t session, |
186 | | const uint8_t *data, |
187 | | size_t data_size) |
188 | 175 | { |
189 | 175 | int ret; |
190 | 175 | unsigned i, j; |
191 | 175 | uint16_t num; |
192 | 175 | uint8_t bytes_len; |
193 | 175 | size_t methods_len; |
194 | 175 | gnutls_compression_method_t methods[MAX_COMPRESS_CERTIFICATE_METHODS]; |
195 | 175 | gnutls_compression_method_t method; |
196 | 175 | compress_certificate_ext_st *priv; |
197 | 175 | gnutls_ext_priv_data_t epriv; |
198 | | |
199 | 175 | ret = _gnutls_hello_ext_get_priv( |
200 | 175 | session, GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, &epriv); |
201 | 175 | if (ret < 0) |
202 | 175 | return 0; |
203 | 0 | priv = epriv; |
204 | |
|
205 | 0 | DECR_LEN(data_size, 1); |
206 | 0 | bytes_len = *data; |
207 | |
|
208 | 0 | if (bytes_len < 2 || bytes_len > 254 || bytes_len % 2 == 1) |
209 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
210 | | |
211 | 0 | DECR_LEN(data_size, bytes_len); |
212 | 0 | if (data_size > 0) |
213 | 0 | return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
214 | | |
215 | 0 | methods_len = 0; |
216 | 0 | for (i = 0; i < bytes_len / 2; ++i) { |
217 | 0 | num = _gnutls_read_uint16(data + i + i + 1); |
218 | 0 | method = _gnutls_compress_certificate_num2method(num); |
219 | 0 | if (method != GNUTLS_COMP_UNKNOWN) |
220 | 0 | methods[methods_len++] = method; |
221 | 0 | } |
222 | |
|
223 | 0 | method = GNUTLS_COMP_UNKNOWN; |
224 | 0 | for (i = 0; i < methods_len; ++i) |
225 | 0 | for (j = 0; j < priv->methods_len; ++j) |
226 | 0 | if (methods[i] == priv->methods[j]) { |
227 | 0 | method = methods[i]; |
228 | 0 | goto endloop; |
229 | 0 | } |
230 | 0 | endloop: |
231 | 0 | session->internals.compress_certificate_method = method; |
232 | |
|
233 | 0 | return 0; |
234 | 0 | } |
235 | | |
236 | | int _gnutls_compress_certificate_send_params(gnutls_session_t session, |
237 | | gnutls_buffer_st *data) |
238 | 34.5k | { |
239 | 34.5k | int ret, num; |
240 | 34.5k | unsigned i; |
241 | 34.5k | uint8_t bytes_len; |
242 | 34.5k | uint8_t bytes[2 * MAX_COMPRESS_CERTIFICATE_METHODS]; |
243 | 34.5k | compress_certificate_ext_st *priv; |
244 | 34.5k | gnutls_ext_priv_data_t epriv; |
245 | | |
246 | 34.5k | ret = _gnutls_hello_ext_get_priv( |
247 | 34.5k | session, GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, &epriv); |
248 | 34.5k | if (ret < 0) |
249 | 34.5k | return 0; |
250 | 0 | priv = epriv; |
251 | |
|
252 | 0 | bytes_len = 2 * priv->methods_len; |
253 | 0 | for (i = 0; i < priv->methods_len; ++i) { |
254 | 0 | num = _gnutls_compress_certificate_method2num(priv->methods[i]); |
255 | 0 | _gnutls_write_uint16(num, bytes + i + i); |
256 | 0 | } |
257 | |
|
258 | 0 | ret = _gnutls_buffer_append_data_prefix(data, 8, bytes, bytes_len); |
259 | 0 | if (ret < 0) |
260 | 0 | return gnutls_assert_val(ret); |
261 | | |
262 | 0 | session->internals.hsk_flags |= HSK_COMP_CRT_REQ_SENT; |
263 | |
|
264 | 0 | return bytes_len + 1; |
265 | 0 | } |
266 | | |
267 | | const hello_ext_entry_st ext_mod_compress_certificate = { |
268 | | .name = "Compress Certificate", |
269 | | .tls_id = 27, |
270 | | .gid = GNUTLS_EXTENSION_COMPRESS_CERTIFICATE, |
271 | | .client_parse_point = GNUTLS_EXT_TLS, |
272 | | .server_parse_point = GNUTLS_EXT_TLS, |
273 | | .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS | |
274 | | GNUTLS_EXT_FLAG_CLIENT_HELLO, |
275 | | .recv_func = _gnutls_compress_certificate_recv_params, |
276 | | .send_func = _gnutls_compress_certificate_send_params, |
277 | | .deinit_func = _gnutls_hello_ext_default_deinit |
278 | | }; |