/src/openssl36/crypto/ct/ct_oct.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 | | #ifdef OPENSSL_NO_CT |
11 | | #error "CT is disabled" |
12 | | #endif |
13 | | |
14 | | #include <limits.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include <openssl/asn1.h> |
18 | | #include <openssl/buffer.h> |
19 | | #include <openssl/ct.h> |
20 | | #include <openssl/err.h> |
21 | | |
22 | | #include "ct_local.h" |
23 | | |
24 | | int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len) |
25 | 129k | { |
26 | 129k | size_t siglen; |
27 | 129k | size_t len_remaining = len; |
28 | 129k | const unsigned char *p; |
29 | | |
30 | 129k | if (sct->version != SCT_VERSION_V1) { |
31 | 0 | ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION); |
32 | 0 | return -1; |
33 | 0 | } |
34 | | /* |
35 | | * digitally-signed struct header: (1 byte) Hash algorithm (1 byte) |
36 | | * Signature algorithm (2 bytes + ?) Signature |
37 | | * |
38 | | * This explicitly rejects empty signatures: they're invalid for |
39 | | * all supported algorithms. |
40 | | */ |
41 | 129k | if (len <= 4) { |
42 | 5.76k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
43 | 5.76k | return -1; |
44 | 5.76k | } |
45 | | |
46 | 123k | p = *in; |
47 | | /* Get hash and signature algorithm */ |
48 | 123k | sct->hash_alg = *p++; |
49 | 123k | sct->sig_alg = *p++; |
50 | 123k | if (SCT_get_signature_nid(sct) == NID_undef) { |
51 | 11.5k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
52 | 11.5k | return -1; |
53 | 11.5k | } |
54 | | /* Retrieve signature and check it is consistent with the buffer length */ |
55 | 112k | n2s(p, siglen); |
56 | 112k | len_remaining -= (p - *in); |
57 | 112k | if (siglen > len_remaining) { |
58 | 10.3k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
59 | 10.3k | return -1; |
60 | 10.3k | } |
61 | | |
62 | 101k | if (SCT_set1_signature(sct, p, siglen) != 1) |
63 | 0 | return -1; |
64 | 101k | len_remaining -= siglen; |
65 | 101k | *in = p + siglen; |
66 | | |
67 | 101k | return (int)(len - len_remaining); |
68 | 101k | } |
69 | | |
70 | | SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len) |
71 | 1.06M | { |
72 | 1.06M | SCT *sct = NULL; |
73 | 1.06M | const unsigned char *p; |
74 | | |
75 | 1.06M | if (len == 0 || len > MAX_SCT_SIZE) { |
76 | 0 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
77 | 0 | goto err; |
78 | 0 | } |
79 | | |
80 | 1.06M | if ((sct = SCT_new()) == NULL) |
81 | 0 | goto err; |
82 | | |
83 | 1.06M | p = *in; |
84 | | |
85 | 1.06M | sct->version = *p; |
86 | 1.06M | if (sct->version == SCT_VERSION_V1) { |
87 | 140k | int sig_len; |
88 | 140k | size_t len2; |
89 | | /*- |
90 | | * Fixed-length header: |
91 | | * struct { |
92 | | * Version sct_version; (1 byte) |
93 | | * log_id id; (32 bytes) |
94 | | * uint64 timestamp; (8 bytes) |
95 | | * CtExtensions extensions; (2 bytes + ?) |
96 | | * } |
97 | | */ |
98 | 140k | if (len < 43) { |
99 | 2.24k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
100 | 2.24k | goto err; |
101 | 2.24k | } |
102 | 138k | len -= 43; |
103 | 138k | p++; |
104 | 138k | sct->log_id = OPENSSL_memdup(p, CT_V1_HASHLEN); |
105 | 138k | if (sct->log_id == NULL) |
106 | 0 | goto err; |
107 | 138k | sct->log_id_len = CT_V1_HASHLEN; |
108 | 138k | p += CT_V1_HASHLEN; |
109 | | |
110 | 138k | n2l8(p, sct->timestamp); |
111 | | |
112 | 138k | n2s(p, len2); |
113 | 138k | if (len < len2) { |
114 | 9.10k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
115 | 9.10k | goto err; |
116 | 9.10k | } |
117 | 129k | if (len2 > 0) { |
118 | 39.9k | sct->ext = OPENSSL_memdup(p, len2); |
119 | 39.9k | if (sct->ext == NULL) |
120 | 0 | goto err; |
121 | 39.9k | } |
122 | 129k | sct->ext_len = len2; |
123 | 129k | p += len2; |
124 | 129k | len -= len2; |
125 | | |
126 | 129k | sig_len = o2i_SCT_signature(sct, &p, len); |
127 | 129k | if (sig_len <= 0) { |
128 | 27.6k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
129 | 27.6k | goto err; |
130 | 27.6k | } |
131 | 101k | len -= sig_len; |
132 | 101k | *in = p + len; |
133 | 923k | } else { |
134 | | /* If not V1 just cache encoding */ |
135 | 923k | sct->sct = OPENSSL_memdup(p, len); |
136 | 923k | if (sct->sct == NULL) |
137 | 0 | goto err; |
138 | 923k | sct->sct_len = len; |
139 | 923k | *in = p + len; |
140 | 923k | } |
141 | | |
142 | 1.02M | if (psct != NULL) { |
143 | 0 | SCT_free(*psct); |
144 | 0 | *psct = sct; |
145 | 0 | } |
146 | | |
147 | 1.02M | return sct; |
148 | 39.0k | err: |
149 | 39.0k | SCT_free(sct); |
150 | 39.0k | return NULL; |
151 | 1.06M | } |
152 | | |
153 | | int i2o_SCT_signature(const SCT *sct, unsigned char **out) |
154 | 15.8k | { |
155 | 15.8k | size_t len; |
156 | 15.8k | unsigned char *p = NULL, *pstart = NULL; |
157 | | |
158 | 15.8k | if (!SCT_signature_is_complete(sct)) { |
159 | 0 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
160 | 0 | goto err; |
161 | 0 | } |
162 | | |
163 | 15.8k | if (sct->version != SCT_VERSION_V1) { |
164 | 0 | ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION); |
165 | 0 | goto err; |
166 | 0 | } |
167 | | |
168 | | /* |
169 | | * (1 byte) Hash algorithm |
170 | | * (1 byte) Signature algorithm |
171 | | * (2 bytes + ?) Signature |
172 | | */ |
173 | 15.8k | len = 4 + sct->sig_len; |
174 | | |
175 | 15.8k | if (out != NULL) { |
176 | 15.8k | if (*out != NULL) { |
177 | 15.8k | p = *out; |
178 | 15.8k | *out += len; |
179 | 15.8k | } else { |
180 | 0 | pstart = p = OPENSSL_malloc(len); |
181 | 0 | if (p == NULL) |
182 | 0 | goto err; |
183 | 0 | *out = p; |
184 | 0 | } |
185 | | |
186 | 15.8k | *p++ = sct->hash_alg; |
187 | 15.8k | *p++ = sct->sig_alg; |
188 | 15.8k | s2n(sct->sig_len, p); |
189 | 15.8k | memcpy(p, sct->sig, sct->sig_len); |
190 | 15.8k | } |
191 | | |
192 | 15.8k | return (int)len; |
193 | 0 | err: |
194 | 0 | OPENSSL_free(pstart); |
195 | 0 | return -1; |
196 | 15.8k | } |
197 | | |
198 | | int i2o_SCT(const SCT *sct, unsigned char **out) |
199 | 396k | { |
200 | 396k | size_t len; |
201 | 396k | unsigned char *p = NULL, *pstart = NULL; |
202 | | |
203 | 396k | if (!SCT_is_complete(sct)) { |
204 | 413 | ERR_raise(ERR_LIB_CT, CT_R_SCT_NOT_SET); |
205 | 413 | goto err; |
206 | 413 | } |
207 | | /* |
208 | | * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes) |
209 | | * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions |
210 | | * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2 |
211 | | * bytes + ?) Signature |
212 | | */ |
213 | 396k | if (sct->version == SCT_VERSION_V1) |
214 | 9.84k | len = 43 + sct->ext_len + 4 + sct->sig_len; |
215 | 386k | else |
216 | 386k | len = sct->sct_len; |
217 | | |
218 | 396k | if (len > INT_MAX) |
219 | 0 | return -1; |
220 | 396k | if (out == NULL) |
221 | 198k | return (int)len; |
222 | | |
223 | 197k | if (*out != NULL) { |
224 | 197k | p = *out; |
225 | 197k | *out += len; |
226 | 197k | } else { |
227 | 0 | pstart = p = OPENSSL_malloc(len); |
228 | 0 | if (p == NULL) |
229 | 0 | goto err; |
230 | 0 | *out = p; |
231 | 0 | } |
232 | | |
233 | 197k | if (sct->version == SCT_VERSION_V1) { |
234 | 4.82k | *p++ = sct->version; |
235 | 4.82k | memcpy(p, sct->log_id, CT_V1_HASHLEN); |
236 | 4.82k | p += CT_V1_HASHLEN; |
237 | 4.82k | l2n8(sct->timestamp, p); |
238 | 4.82k | s2n(sct->ext_len, p); |
239 | 4.82k | if (sct->ext_len > 0) { |
240 | 1.50k | memcpy(p, sct->ext, sct->ext_len); |
241 | 1.50k | p += sct->ext_len; |
242 | 1.50k | } |
243 | 4.82k | if (i2o_SCT_signature(sct, &p) <= 0) |
244 | 0 | goto err; |
245 | 192k | } else { |
246 | 192k | memcpy(p, sct->sct, len); |
247 | 192k | } |
248 | | |
249 | 197k | return (int)len; |
250 | 413 | err: |
251 | 413 | OPENSSL_free(pstart); |
252 | 413 | return -1; |
253 | 197k | } |
254 | | |
255 | | STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
256 | | size_t len) |
257 | 154k | { |
258 | 154k | STACK_OF(SCT) *sk = NULL; |
259 | 154k | size_t list_len, sct_len; |
260 | | |
261 | 154k | if (len < 2 || len > MAX_SCT_LIST_SIZE) { |
262 | 4.84k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
263 | 4.84k | return NULL; |
264 | 4.84k | } |
265 | | |
266 | 149k | n2s(*pp, list_len); |
267 | 149k | if (list_len != len - 2) { |
268 | 15.2k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
269 | 15.2k | return NULL; |
270 | 15.2k | } |
271 | | |
272 | 133k | if (a == NULL || *a == NULL) { |
273 | 133k | sk = sk_SCT_new_null(); |
274 | 133k | if (sk == NULL) |
275 | 0 | return NULL; |
276 | 133k | } else { |
277 | 0 | SCT *sct; |
278 | | |
279 | | /* Use the given stack, but empty it first. */ |
280 | 0 | sk = *a; |
281 | 0 | while ((sct = sk_SCT_pop(sk)) != NULL) |
282 | 0 | SCT_free(sct); |
283 | 0 | } |
284 | | |
285 | 1.15M | while (list_len > 0) { |
286 | 1.07M | SCT *sct; |
287 | | |
288 | 1.07M | if (list_len < 2) { |
289 | 2.44k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
290 | 2.44k | goto err; |
291 | 2.44k | } |
292 | 1.07M | n2s(*pp, sct_len); |
293 | 1.07M | list_len -= 2; |
294 | | |
295 | 1.07M | if (sct_len == 0 || sct_len > list_len) { |
296 | 8.84k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
297 | 8.84k | goto err; |
298 | 8.84k | } |
299 | 1.06M | list_len -= sct_len; |
300 | | |
301 | 1.06M | if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL) |
302 | 39.0k | goto err; |
303 | 1.02M | if (!sk_SCT_push(sk, sct)) { |
304 | 0 | SCT_free(sct); |
305 | 0 | goto err; |
306 | 0 | } |
307 | 1.02M | } |
308 | | |
309 | 83.5k | if (a != NULL && *a == NULL) |
310 | 0 | *a = sk; |
311 | 83.5k | return sk; |
312 | | |
313 | 50.3k | err: |
314 | 50.3k | if (a == NULL || *a == NULL) |
315 | 50.3k | SCT_LIST_free(sk); |
316 | 50.3k | return NULL; |
317 | 133k | } |
318 | | |
319 | | int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp) |
320 | 6.34k | { |
321 | 6.34k | int len, sct_len, i, is_pp_new = 0; |
322 | 6.34k | size_t len2; |
323 | 6.34k | unsigned char *p = NULL, *p2; |
324 | | |
325 | 6.34k | if (pp != NULL) { |
326 | 3.17k | if (*pp == NULL) { |
327 | 3.17k | if ((len = i2o_SCT_LIST(a, NULL)) == -1) { |
328 | 1.11k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
329 | 1.11k | return -1; |
330 | 1.11k | } |
331 | 2.06k | if ((*pp = OPENSSL_malloc(len)) == NULL) |
332 | 0 | return -1; |
333 | 2.06k | is_pp_new = 1; |
334 | 2.06k | } |
335 | 2.06k | p = *pp + 2; |
336 | 2.06k | } |
337 | | |
338 | 5.23k | len2 = 2; |
339 | 1.13M | for (i = 0; i < sk_SCT_num(a); i++) { |
340 | 1.12M | if (pp != NULL) { |
341 | 559k | p2 = p; |
342 | 559k | p += 2; |
343 | 559k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1) |
344 | 0 | goto err; |
345 | 559k | s2n(sct_len, p2); |
346 | 569k | } else { |
347 | 569k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1) |
348 | 1.11k | goto err; |
349 | 569k | } |
350 | 1.12M | len2 += 2 + sct_len; |
351 | 1.12M | } |
352 | | |
353 | 4.12k | if (len2 > MAX_SCT_LIST_SIZE) |
354 | 0 | goto err; |
355 | | |
356 | 4.12k | if (pp != NULL) { |
357 | 2.06k | p = *pp; |
358 | 2.06k | s2n(len2 - 2, p); |
359 | 2.06k | if (!is_pp_new) |
360 | 0 | *pp += len2; |
361 | 2.06k | } |
362 | 4.12k | return (int)len2; |
363 | | |
364 | 1.11k | err: |
365 | 1.11k | if (is_pp_new) { |
366 | 0 | OPENSSL_free(*pp); |
367 | 0 | *pp = NULL; |
368 | 0 | } |
369 | 1.11k | return -1; |
370 | 4.12k | } |
371 | | |
372 | | STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
373 | | long len) |
374 | 183k | { |
375 | 183k | ASN1_OCTET_STRING *oct = NULL; |
376 | 183k | STACK_OF(SCT) *sk = NULL; |
377 | 183k | const unsigned char *p; |
378 | | |
379 | 183k | p = *pp; |
380 | 183k | if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL) |
381 | 29.8k | return NULL; |
382 | | |
383 | 154k | p = oct->data; |
384 | 154k | if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL) |
385 | 83.5k | *pp += len; |
386 | | |
387 | 154k | ASN1_OCTET_STRING_free(oct); |
388 | 154k | return sk; |
389 | 183k | } |
390 | | |
391 | | int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **out) |
392 | 3.17k | { |
393 | 3.17k | ASN1_OCTET_STRING oct; |
394 | 3.17k | int len; |
395 | | |
396 | 3.17k | oct.data = NULL; |
397 | 3.17k | if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1) |
398 | 1.11k | return -1; |
399 | | |
400 | 2.06k | len = i2d_ASN1_OCTET_STRING(&oct, out); |
401 | 2.06k | OPENSSL_free(oct.data); |
402 | 2.06k | return len; |
403 | 3.17k | } |