/src/openssl30/crypto/ct/ct_oct.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2020 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 | 8.06k | { |
26 | 8.06k | size_t siglen; |
27 | 8.06k | size_t len_remaining = len; |
28 | 8.06k | const unsigned char *p; |
29 | | |
30 | 8.06k | 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 | 8.06k | if (len <= 4) { |
42 | 5 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
43 | 5 | return -1; |
44 | 5 | } |
45 | | |
46 | 8.05k | p = *in; |
47 | | /* Get hash and signature algorithm */ |
48 | 8.05k | sct->hash_alg = *p++; |
49 | 8.05k | sct->sig_alg = *p++; |
50 | 8.05k | if (SCT_get_signature_nid(sct) == NID_undef) { |
51 | 50 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
52 | 50 | return -1; |
53 | 50 | } |
54 | | /* Retrieve signature and check it is consistent with the buffer length */ |
55 | 8.00k | n2s(p, siglen); |
56 | 8.00k | len_remaining -= (p - *in); |
57 | 8.00k | if (siglen > len_remaining) { |
58 | 26 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE); |
59 | 26 | return -1; |
60 | 26 | } |
61 | | |
62 | 7.98k | if (SCT_set1_signature(sct, p, siglen) != 1) |
63 | 0 | return -1; |
64 | 7.98k | len_remaining -= siglen; |
65 | 7.98k | *in = p + siglen; |
66 | | |
67 | 7.98k | return len - len_remaining; |
68 | 7.98k | } |
69 | | |
70 | | SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len) |
71 | 178k | { |
72 | 178k | SCT *sct = NULL; |
73 | 178k | const unsigned char *p; |
74 | | |
75 | 178k | 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 | 178k | if ((sct = SCT_new()) == NULL) |
81 | 0 | goto err; |
82 | | |
83 | 178k | p = *in; |
84 | | |
85 | 178k | sct->version = *p; |
86 | 178k | if (sct->version == SCT_VERSION_V1) { |
87 | 8.09k | int sig_len; |
88 | 8.09k | 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 | 8.09k | if (len < 43) { |
99 | 12 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
100 | 12 | goto err; |
101 | 12 | } |
102 | 8.08k | len -= 43; |
103 | 8.08k | p++; |
104 | 8.08k | sct->log_id = OPENSSL_memdup(p, CT_V1_HASHLEN); |
105 | 8.08k | if (sct->log_id == NULL) |
106 | 0 | goto err; |
107 | 8.08k | sct->log_id_len = CT_V1_HASHLEN; |
108 | 8.08k | p += CT_V1_HASHLEN; |
109 | | |
110 | 8.08k | n2l8(p, sct->timestamp); |
111 | | |
112 | 8.08k | n2s(p, len2); |
113 | 8.08k | if (len < len2) { |
114 | 22 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
115 | 22 | goto err; |
116 | 22 | } |
117 | 8.06k | if (len2 > 0) { |
118 | 779 | sct->ext = OPENSSL_memdup(p, len2); |
119 | 779 | if (sct->ext == NULL) |
120 | 0 | goto err; |
121 | 779 | } |
122 | 8.06k | sct->ext_len = len2; |
123 | 8.06k | p += len2; |
124 | 8.06k | len -= len2; |
125 | | |
126 | 8.06k | sig_len = o2i_SCT_signature(sct, &p, len); |
127 | 8.06k | if (sig_len <= 0) { |
128 | 81 | ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID); |
129 | 81 | goto err; |
130 | 81 | } |
131 | 7.98k | len -= sig_len; |
132 | 7.98k | *in = p + len; |
133 | 170k | } else { |
134 | | /* If not V1 just cache encoding */ |
135 | 170k | sct->sct = OPENSSL_memdup(p, len); |
136 | 170k | if (sct->sct == NULL) |
137 | 0 | goto err; |
138 | 170k | sct->sct_len = len; |
139 | 170k | *in = p + len; |
140 | 170k | } |
141 | | |
142 | 178k | if (psct != NULL) { |
143 | 0 | SCT_free(*psct); |
144 | 0 | *psct = sct; |
145 | 0 | } |
146 | | |
147 | 178k | return sct; |
148 | 115 | err: |
149 | 115 | SCT_free(sct); |
150 | 115 | return NULL; |
151 | 178k | } |
152 | | |
153 | | int i2o_SCT_signature(const SCT *sct, unsigned char **out) |
154 | 1.63k | { |
155 | 1.63k | size_t len; |
156 | 1.63k | unsigned char *p = NULL, *pstart = NULL; |
157 | | |
158 | 1.63k | 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 | 1.63k | 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 | 1.63k | len = 4 + sct->sig_len; |
174 | | |
175 | 1.63k | if (out != NULL) { |
176 | 1.63k | if (*out != NULL) { |
177 | 1.63k | p = *out; |
178 | 1.63k | *out += len; |
179 | 1.63k | } else { |
180 | 0 | pstart = p = OPENSSL_malloc(len); |
181 | 0 | if (p == NULL) { |
182 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); |
183 | 0 | goto err; |
184 | 0 | } |
185 | 0 | *out = p; |
186 | 0 | } |
187 | | |
188 | 1.63k | *p++ = sct->hash_alg; |
189 | 1.63k | *p++ = sct->sig_alg; |
190 | 1.63k | s2n(sct->sig_len, p); |
191 | 1.63k | memcpy(p, sct->sig, sct->sig_len); |
192 | 1.63k | } |
193 | | |
194 | 1.63k | return len; |
195 | 0 | err: |
196 | 0 | OPENSSL_free(pstart); |
197 | 0 | return -1; |
198 | 1.63k | } |
199 | | |
200 | | int i2o_SCT(const SCT *sct, unsigned char **out) |
201 | 139k | { |
202 | 139k | size_t len; |
203 | 139k | unsigned char *p = NULL, *pstart = NULL; |
204 | | |
205 | 139k | if (!SCT_is_complete(sct)) { |
206 | 242 | ERR_raise(ERR_LIB_CT, CT_R_SCT_NOT_SET); |
207 | 242 | goto err; |
208 | 242 | } |
209 | | /* |
210 | | * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes) |
211 | | * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions |
212 | | * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2 |
213 | | * bytes + ?) Signature |
214 | | */ |
215 | 139k | if (sct->version == SCT_VERSION_V1) |
216 | 3.43k | len = 43 + sct->ext_len + 4 + sct->sig_len; |
217 | 136k | else |
218 | 136k | len = sct->sct_len; |
219 | | |
220 | 139k | if (out == NULL) |
221 | 70.4k | return len; |
222 | | |
223 | 69.1k | if (*out != NULL) { |
224 | 69.1k | p = *out; |
225 | 69.1k | *out += len; |
226 | 69.1k | } else { |
227 | 0 | pstart = p = OPENSSL_malloc(len); |
228 | 0 | if (p == NULL) { |
229 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); |
230 | 0 | goto err; |
231 | 0 | } |
232 | 0 | *out = p; |
233 | 0 | } |
234 | | |
235 | 69.1k | if (sct->version == SCT_VERSION_V1) { |
236 | 1.63k | *p++ = sct->version; |
237 | 1.63k | memcpy(p, sct->log_id, CT_V1_HASHLEN); |
238 | 1.63k | p += CT_V1_HASHLEN; |
239 | 1.63k | l2n8(sct->timestamp, p); |
240 | 1.63k | s2n(sct->ext_len, p); |
241 | 1.63k | if (sct->ext_len > 0) { |
242 | 534 | memcpy(p, sct->ext, sct->ext_len); |
243 | 534 | p += sct->ext_len; |
244 | 534 | } |
245 | 1.63k | if (i2o_SCT_signature(sct, &p) <= 0) |
246 | 0 | goto err; |
247 | 67.5k | } else { |
248 | 67.5k | memcpy(p, sct->sct, len); |
249 | 67.5k | } |
250 | | |
251 | 69.1k | return len; |
252 | 242 | err: |
253 | 242 | OPENSSL_free(pstart); |
254 | 242 | return -1; |
255 | 69.1k | } |
256 | | |
257 | | STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
258 | | size_t len) |
259 | 936 | { |
260 | 936 | STACK_OF(SCT) *sk = NULL; |
261 | 936 | size_t list_len, sct_len; |
262 | | |
263 | 936 | if (len < 2 || len > MAX_SCT_LIST_SIZE) { |
264 | 47 | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
265 | 47 | return NULL; |
266 | 47 | } |
267 | | |
268 | 889 | n2s(*pp, list_len); |
269 | 889 | if (list_len != len - 2) { |
270 | 139 | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
271 | 139 | return NULL; |
272 | 139 | } |
273 | | |
274 | 750 | if (a == NULL || *a == NULL) { |
275 | 750 | sk = sk_SCT_new_null(); |
276 | 750 | if (sk == NULL) |
277 | 0 | return NULL; |
278 | 750 | } else { |
279 | 0 | SCT *sct; |
280 | | |
281 | | /* Use the given stack, but empty it first. */ |
282 | 0 | sk = *a; |
283 | 0 | while ((sct = sk_SCT_pop(sk)) != NULL) |
284 | 0 | SCT_free(sct); |
285 | 0 | } |
286 | | |
287 | 179k | while (list_len > 0) { |
288 | 178k | SCT *sct; |
289 | | |
290 | 178k | if (list_len < 2) { |
291 | 9 | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
292 | 9 | goto err; |
293 | 9 | } |
294 | 178k | n2s(*pp, sct_len); |
295 | 178k | list_len -= 2; |
296 | | |
297 | 178k | if (sct_len == 0 || sct_len > list_len) { |
298 | 51 | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
299 | 51 | goto err; |
300 | 51 | } |
301 | 178k | list_len -= sct_len; |
302 | | |
303 | 178k | if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL) |
304 | 115 | goto err; |
305 | 178k | if (!sk_SCT_push(sk, sct)) { |
306 | 0 | SCT_free(sct); |
307 | 0 | goto err; |
308 | 0 | } |
309 | 178k | } |
310 | | |
311 | 575 | if (a != NULL && *a == NULL) |
312 | 0 | *a = sk; |
313 | 575 | return sk; |
314 | | |
315 | 175 | err: |
316 | 175 | if (a == NULL || *a == NULL) |
317 | 175 | SCT_LIST_free(sk); |
318 | 175 | return NULL; |
319 | 750 | } |
320 | | |
321 | | int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp) |
322 | 1.15k | { |
323 | 1.15k | int len, sct_len, i, is_pp_new = 0; |
324 | 1.15k | size_t len2; |
325 | 1.15k | unsigned char *p = NULL, *p2; |
326 | | |
327 | 1.15k | if (pp != NULL) { |
328 | 575 | if (*pp == NULL) { |
329 | 575 | if ((len = i2o_SCT_LIST(a, NULL)) == -1) { |
330 | 242 | ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID); |
331 | 242 | return -1; |
332 | 242 | } |
333 | 333 | if ((*pp = OPENSSL_malloc(len)) == NULL) { |
334 | 0 | ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE); |
335 | 0 | return -1; |
336 | 0 | } |
337 | 333 | is_pp_new = 1; |
338 | 333 | } |
339 | 333 | p = *pp + 2; |
340 | 333 | } |
341 | | |
342 | 908 | len2 = 2; |
343 | 140k | for (i = 0; i < sk_SCT_num(a); i++) { |
344 | 139k | if (pp != NULL) { |
345 | 69.1k | p2 = p; |
346 | 69.1k | p += 2; |
347 | 69.1k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1) |
348 | 0 | goto err; |
349 | 69.1k | s2n(sct_len, p2); |
350 | 70.6k | } else { |
351 | 70.6k | if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1) |
352 | 242 | goto err; |
353 | 70.6k | } |
354 | 139k | len2 += 2 + sct_len; |
355 | 139k | } |
356 | | |
357 | 666 | if (len2 > MAX_SCT_LIST_SIZE) |
358 | 0 | goto err; |
359 | | |
360 | 666 | if (pp != NULL) { |
361 | 333 | p = *pp; |
362 | 333 | s2n(len2 - 2, p); |
363 | 333 | if (!is_pp_new) |
364 | 0 | *pp += len2; |
365 | 333 | } |
366 | 666 | return len2; |
367 | | |
368 | 242 | err: |
369 | 242 | if (is_pp_new) { |
370 | 0 | OPENSSL_free(*pp); |
371 | 0 | *pp = NULL; |
372 | 0 | } |
373 | 242 | return -1; |
374 | 666 | } |
375 | | |
376 | | STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp, |
377 | | long len) |
378 | 1.59k | { |
379 | 1.59k | ASN1_OCTET_STRING *oct = NULL; |
380 | 1.59k | STACK_OF(SCT) *sk = NULL; |
381 | 1.59k | const unsigned char *p; |
382 | | |
383 | 1.59k | p = *pp; |
384 | 1.59k | if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL) |
385 | 658 | return NULL; |
386 | | |
387 | 936 | p = oct->data; |
388 | 936 | if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL) |
389 | 575 | *pp += len; |
390 | | |
391 | 936 | ASN1_OCTET_STRING_free(oct); |
392 | 936 | return sk; |
393 | 1.59k | } |
394 | | |
395 | | int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **out) |
396 | 575 | { |
397 | 575 | ASN1_OCTET_STRING oct; |
398 | 575 | int len; |
399 | | |
400 | 575 | oct.data = NULL; |
401 | 575 | if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1) |
402 | 242 | return -1; |
403 | | |
404 | 333 | len = i2d_ASN1_OCTET_STRING(&oct, out); |
405 | 333 | OPENSSL_free(oct.data); |
406 | 333 | return len; |
407 | 575 | } |