/src/openssl40/crypto/ct/ct_oct.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2026 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 | 93.5k | { |
26 | 93.5k | size_t siglen; |
27 | 93.5k | size_t len_remaining = len; |
28 | 93.5k | const unsigned char *p; |
29 | | |
30 | 93.5k | 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 | 93.5k | if (len <= 4) { |
42 | 3.32k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
43 | 3.32k | return -1; |
44 | 3.32k | } |
45 | | |
46 | 90.2k | p = *in; |
47 | | /* Get hash and signature algorithm */ |
48 | 90.2k | sct->hash_alg = *p++; |
49 | 90.2k | sct->sig_alg = *p++; |
50 | 90.2k | if (SCT_get_signature_nid(sct) == NID_undef) { |
51 | 10.6k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
52 | 10.6k | return -1; |
53 | 10.6k | } |
54 | | /* Retrieve signature and check it is consistent with the buffer length */ |
55 | 79.6k | n2s(p, siglen); |
56 | 79.6k | len_remaining -= (p - *in); |
57 | 79.6k | if (siglen > len_remaining) { |
58 | 4.38k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
59 | 4.38k | return -1; |
60 | 4.38k | } |
61 | | |
62 | 75.2k | if (SCT_set1_signature(sct, p, siglen) != 1) |
63 | 0 | return -1; |
64 | 75.2k | len_remaining -= siglen; |
65 | 75.2k | *in = p + siglen; |
66 | | |
67 | 75.2k | return (int)(len - len_remaining); |
68 | 75.2k | } |
69 | | |
70 | | SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len) |
71 | 1.04M | { |
72 | 1.04M | SCT *sct = NULL; |
73 | 1.04M | const unsigned char *p; |
74 | | |
75 | 1.04M | 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.04M | if ((sct = SCT_new()) == NULL) |
81 | 0 | goto err; |
82 | | |
83 | 1.04M | p = *in; |
84 | | |
85 | 1.04M | sct->version = *p; |
86 | 1.04M | if (sct->version == SCT_VERSION_V1) { |
87 | 100k | int sig_len; |
88 | 100k | 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 | 100k | if (len < 43) { |
99 | 1.63k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
100 | 1.63k | goto err; |
101 | 1.63k | } |
102 | 98.6k | len -= 43; |
103 | 98.6k | p++; |
104 | 98.6k | sct->log_id = OPENSSL_memdup(p, CT_V1_HASHLEN); |
105 | 98.6k | if (sct->log_id == NULL) |
106 | 0 | goto err; |
107 | 98.6k | sct->log_id_len = CT_V1_HASHLEN; |
108 | 98.6k | p += CT_V1_HASHLEN; |
109 | | |
110 | 98.6k | n2l8(p, sct->timestamp); |
111 | | |
112 | 98.6k | n2s(p, len2); |
113 | 98.6k | if (len < len2) { |
114 | 5.10k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
115 | 5.10k | goto err; |
116 | 5.10k | } |
117 | 93.5k | if (len2 > 0) { |
118 | 28.1k | sct->ext = OPENSSL_memdup(p, len2); |
119 | 28.1k | if (sct->ext == NULL) |
120 | 0 | goto err; |
121 | 28.1k | } |
122 | 93.5k | sct->ext_len = len2; |
123 | 93.5k | p += len2; |
124 | 93.5k | len -= len2; |
125 | | |
126 | 93.5k | sig_len = o2i_SCT_signature(sct, &p, len); |
127 | 93.5k | if (sig_len <= 0) { |
128 | 18.3k | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
129 | 18.3k | goto err; |
130 | 18.3k | } |
131 | 75.2k | len -= sig_len; |
132 | 75.2k | *in = p + len; |
133 | 941k | } else { |
134 | | /* If not V1 just cache encoding */ |
135 | 941k | sct->sct = OPENSSL_memdup(p, len); |
136 | 941k | if (sct->sct == NULL) |
137 | 0 | goto err; |
138 | 941k | sct->sct_len = len; |
139 | 941k | *in = p + len; |
140 | 941k | } |
141 | | |
142 | 1.01M | if (psct != NULL) { |
143 | 0 | SCT_free(*psct); |
144 | 0 | *psct = sct; |
145 | 0 | } |
146 | | |
147 | 1.01M | return sct; |
148 | 25.0k | err: |
149 | 25.0k | SCT_free(sct); |
150 | 25.0k | return NULL; |
151 | 1.04M | } |
152 | | |
153 | | int i2o_SCT_signature(const SCT *sct, unsigned char **out) |
154 | 11.6k | { |
155 | 11.6k | size_t len; |
156 | 11.6k | unsigned char *p = NULL, *pstart = NULL; |
157 | | |
158 | 11.6k | 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 | 11.6k | 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 | 11.6k | len = 4 + sct->sig_len; |
174 | | |
175 | 11.6k | if (out != NULL) { |
176 | 11.6k | if (*out != NULL) { |
177 | 11.6k | p = *out; |
178 | 11.6k | *out += len; |
179 | 11.6k | } 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 | 11.6k | *p++ = sct->hash_alg; |
187 | 11.6k | *p++ = sct->sig_alg; |
188 | 11.6k | s2n(sct->sig_len, p); |
189 | 11.6k | memcpy(p, sct->sig, sct->sig_len); |
190 | 11.6k | } |
191 | | |
192 | 11.6k | return (int)len; |
193 | 0 | err: |
194 | 0 | OPENSSL_free(pstart); |
195 | 0 | return -1; |
196 | 11.6k | } |
197 | | |
198 | | int i2o_SCT(const SCT *sct, unsigned char **out) |
199 | 841k | { |
200 | 841k | size_t len; |
201 | 841k | unsigned char *p = NULL, *pstart = NULL; |
202 | | |
203 | 841k | if (!SCT_is_complete(sct)) { |
204 | 777 | ERR_raise(ERR_LIB_CT, CT_R_SCT_NOT_SET); |
205 | 777 | goto err; |
206 | 777 | } |
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 | 841k | if (sct->version == SCT_VERSION_V1) |
214 | 14.2k | len = 43 + sct->ext_len + 4 + sct->sig_len; |
215 | 826k | else |
216 | 826k | len = sct->sct_len; |
217 | | |
218 | 841k | if (len > INT_MAX) |
219 | 0 | return -1; |
220 | 841k | if (out == NULL) |
221 | 422k | return (int)len; |
222 | | |
223 | 418k | if (*out != NULL) { |
224 | 418k | p = *out; |
225 | 418k | *out += len; |
226 | 418k | } 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 | 418k | if (sct->version == SCT_VERSION_V1) { |
234 | 6.89k | *p++ = sct->version; |
235 | 6.89k | memcpy(p, sct->log_id, CT_V1_HASHLEN); |
236 | 6.89k | p += CT_V1_HASHLEN; |
237 | 6.89k | l2n8(sct->timestamp, p); |
238 | 6.89k | s2n(sct->ext_len, p); |
239 | 6.89k | if (sct->ext_len > 0) { |
240 | 2.87k | memcpy(p, sct->ext, sct->ext_len); |
241 | 2.87k | p += sct->ext_len; |
242 | 2.87k | } |
243 | 6.89k | if (i2o_SCT_signature(sct, &p) <= 0) |
244 | 0 | goto err; |
245 | 411k | } else { |
246 | 411k | memcpy(p, sct->sct, len); |
247 | 411k | } |
248 | | |
249 | 418k | return (int)len; |
250 | 777 | err: |
251 | 777 | OPENSSL_free(pstart); |
252 | 777 | return -1; |
253 | 418k | } |
254 | | |
255 | | STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
256 | | size_t len) |
257 | 130k | { |
258 | 130k | STACK_OF(SCT) *sk = NULL; |
259 | 130k | size_t list_len, sct_len; |
260 | | |
261 | 130k | if (len < 2 || len > MAX_SCT_LIST_SIZE) { |
262 | 5.22k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
263 | 5.22k | return NULL; |
264 | 5.22k | } |
265 | | |
266 | 125k | n2s(*pp, list_len); |
267 | 125k | if (list_len != len - 2) { |
268 | 14.3k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
269 | 14.3k | return NULL; |
270 | 14.3k | } |
271 | | |
272 | 110k | if (a == NULL || *a == NULL) { |
273 | 110k | sk = sk_SCT_new_null(); |
274 | 110k | if (sk == NULL) |
275 | 0 | return NULL; |
276 | 110k | } 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.12M | while (list_len > 0) { |
286 | 1.05M | SCT *sct; |
287 | | |
288 | 1.05M | if (list_len < 2) { |
289 | 2.10k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
290 | 2.10k | goto err; |
291 | 2.10k | } |
292 | 1.05M | n2s(*pp, sct_len); |
293 | 1.05M | list_len -= 2; |
294 | | |
295 | 1.05M | if (sct_len == 0 || sct_len > list_len) { |
296 | 12.1k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
297 | 12.1k | goto err; |
298 | 12.1k | } |
299 | 1.04M | list_len -= sct_len; |
300 | | |
301 | 1.04M | if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL) |
302 | 25.0k | goto err; |
303 | 1.01M | if (!sk_SCT_push(sk, sct)) { |
304 | 0 | SCT_free(sct); |
305 | 0 | goto err; |
306 | 0 | } |
307 | 1.01M | } |
308 | | |
309 | 71.6k | if (a != NULL && *a == NULL) |
310 | 0 | *a = sk; |
311 | 71.6k | return sk; |
312 | | |
313 | 39.2k | err: |
314 | 39.2k | if (a == NULL || *a == NULL) |
315 | 39.2k | SCT_LIST_free(sk); |
316 | 39.2k | return NULL; |
317 | 110k | } |
318 | | |
319 | | int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp) |
320 | 6.20k | { |
321 | 6.20k | int len, sct_len, i, is_pp_new = 0; |
322 | 6.20k | size_t len2; |
323 | 6.20k | unsigned char *p = NULL, *p2; |
324 | | |
325 | 6.20k | if (pp != NULL) { |
326 | 3.10k | if (*pp == NULL) { |
327 | 3.10k | if ((len = i2o_SCT_LIST(a, NULL)) == -1) { |
328 | 1.09k | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
329 | 1.09k | return -1; |
330 | 1.09k | } |
331 | 2.00k | if ((*pp = OPENSSL_malloc(len)) == NULL) |
332 | 0 | return -1; |
333 | 2.00k | is_pp_new = 1; |
334 | 2.00k | } |
335 | 2.00k | p = *pp + 2; |
336 | 2.00k | } |
337 | | |
338 | 5.11k | len2 = 2; |
339 | 1.09M | for (i = 0; i < sk_SCT_num(a); i++) { |
340 | 1.09M | if (pp != NULL) { |
341 | 543k | p2 = p; |
342 | 543k | p += 2; |
343 | 543k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1) |
344 | 0 | goto err; |
345 | 543k | s2n(sct_len, p2); |
346 | 552k | } else { |
347 | 552k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1) |
348 | 1.09k | goto err; |
349 | 552k | } |
350 | 1.09M | len2 += 2 + sct_len; |
351 | 1.09M | } |
352 | | |
353 | 4.01k | if (len2 > MAX_SCT_LIST_SIZE) |
354 | 0 | goto err; |
355 | | |
356 | 4.01k | if (pp != NULL) { |
357 | 2.00k | p = *pp; |
358 | 2.00k | s2n(len2 - 2, p); |
359 | 2.00k | if (!is_pp_new) |
360 | 0 | *pp += len2; |
361 | 2.00k | } |
362 | 4.01k | return (int)len2; |
363 | | |
364 | 1.09k | err: |
365 | 1.09k | if (is_pp_new) { |
366 | 0 | OPENSSL_free(*pp); |
367 | 0 | *pp = NULL; |
368 | 0 | } |
369 | 1.09k | return -1; |
370 | 4.01k | } |
371 | | |
372 | | STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
373 | | long len) |
374 | 156k | { |
375 | 156k | ASN1_OCTET_STRING *oct = NULL; |
376 | 156k | STACK_OF(SCT) *sk = NULL; |
377 | 156k | const unsigned char *p; |
378 | | |
379 | 156k | p = *pp; |
380 | 156k | if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL) |
381 | 26.4k | return NULL; |
382 | | |
383 | 130k | p = ASN1_STRING_get0_data(oct); |
384 | 130k | if ((sk = o2i_SCT_LIST(a, &p, ASN1_STRING_length(oct))) != NULL) |
385 | 71.6k | *pp += len; |
386 | | |
387 | 130k | ASN1_OCTET_STRING_free(oct); |
388 | 130k | return sk; |
389 | 156k | } |
390 | | |
391 | | int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **out) |
392 | 1.62k | { |
393 | 1.62k | ASN1_OCTET_STRING *oct; |
394 | 1.62k | unsigned char *data = NULL; |
395 | 1.62k | int len; |
396 | | |
397 | 1.62k | if ((len = i2o_SCT_LIST(a, &data)) == -1) |
398 | 634 | return -1; |
399 | | |
400 | 991 | oct = ASN1_OCTET_STRING_new(); |
401 | 991 | if (oct == NULL) { |
402 | 0 | OPENSSL_free(data); |
403 | 0 | return -1; |
404 | 0 | } |
405 | 991 | ASN1_STRING_set0(oct, data, len); |
406 | 991 | len = i2d_ASN1_OCTET_STRING(oct, out); |
407 | 991 | ASN1_OCTET_STRING_free(oct); |
408 | 991 | return len; |
409 | 991 | } |