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