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