2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 # error "CT is disabled"
17 #include <openssl/asn1.h>
18 #include <openssl/buffer.h>
19 #include <openssl/ct.h>
20 #include <openssl/err.h>
24 int o2i_SCT_signature(SCT
*sct
, const unsigned char **in
, size_t len
)
27 size_t len_remaining
= len
;
28 const unsigned char *p
;
30 if (sct
->version
!= SCT_VERSION_V1
) {
31 CTerr(CT_F_O2I_SCT_SIGNATURE
, CT_R_UNSUPPORTED_VERSION
);
35 * digitally-signed struct header: (1 byte) Hash algorithm (1 byte)
36 * Signature algorithm (2 bytes + ?) Signature
38 * This explicitly rejects empty signatures: they're invalid for
39 * all supported algorithms.
42 CTerr(CT_F_O2I_SCT_SIGNATURE
, CT_R_SCT_INVALID_SIGNATURE
);
47 /* Get hash and signature algorithm */
50 if (SCT_get_signature_nid(sct
) == NID_undef
) {
51 CTerr(CT_F_O2I_SCT_SIGNATURE
, CT_R_SCT_INVALID_SIGNATURE
);
54 /* Retrieve signature and check it is consistent with the buffer length */
56 len_remaining
-= (p
- *in
);
57 if (siglen
> len_remaining
) {
58 CTerr(CT_F_O2I_SCT_SIGNATURE
, CT_R_SCT_INVALID_SIGNATURE
);
62 if (SCT_set1_signature(sct
, p
, siglen
) != 1)
64 len_remaining
-= siglen
;
67 return len
- len_remaining
;
70 SCT
*o2i_SCT(SCT
**psct
, const unsigned char **in
, size_t len
)
73 const unsigned char *p
;
75 if (len
== 0 || len
> MAX_SCT_SIZE
) {
76 CTerr(CT_F_O2I_SCT
, CT_R_SCT_INVALID
);
80 if ((sct
= SCT_new()) == NULL
)
86 if (sct
->version
== SCT_VERSION_V1
) {
90 * Fixed-length header:
92 * Version sct_version; (1 byte)
93 * log_id id; (32 bytes)
94 * uint64 timestamp; (8 bytes)
95 * CtExtensions extensions; (2 bytes + ?)
99 CTerr(CT_F_O2I_SCT
, CT_R_SCT_INVALID
);
104 sct
->log_id
= OPENSSL_memdup(p
, CT_V1_HASHLEN
);
105 if (sct
->log_id
== NULL
)
107 sct
->log_id_len
= CT_V1_HASHLEN
;
110 n2l8(p
, sct
->timestamp
);
114 CTerr(CT_F_O2I_SCT
, CT_R_SCT_INVALID
);
118 sct
->ext
= OPENSSL_memdup(p
, len2
);
119 if (sct
->ext
== NULL
)
126 sig_len
= o2i_SCT_signature(sct
, &p
, len
);
128 CTerr(CT_F_O2I_SCT
, CT_R_SCT_INVALID
);
134 /* If not V1 just cache encoding */
135 sct
->sct
= OPENSSL_memdup(p
, len
);
136 if (sct
->sct
== NULL
)
153 int i2o_SCT_signature(const SCT
*sct
, unsigned char **out
)
156 unsigned char *p
= NULL
, *pstart
= NULL
;
158 if (!SCT_signature_is_complete(sct
)) {
159 CTerr(CT_F_I2O_SCT_SIGNATURE
, CT_R_SCT_INVALID_SIGNATURE
);
163 if (sct
->version
!= SCT_VERSION_V1
) {
164 CTerr(CT_F_I2O_SCT_SIGNATURE
, CT_R_UNSUPPORTED_VERSION
);
169 * (1 byte) Hash algorithm
170 * (1 byte) Signature algorithm
171 * (2 bytes + ?) Signature
173 len
= 4 + sct
->sig_len
;
180 pstart
= p
= OPENSSL_malloc(len
);
182 CTerr(CT_F_I2O_SCT_SIGNATURE
, ERR_R_MALLOC_FAILURE
);
188 *p
++ = sct
->hash_alg
;
190 s2n(sct
->sig_len
, p
);
191 memcpy(p
, sct
->sig
, sct
->sig_len
);
196 OPENSSL_free(pstart
);
200 int i2o_SCT(const SCT
*sct
, unsigned char **out
)
203 unsigned char *p
= NULL
, *pstart
= NULL
;
205 if (!SCT_is_complete(sct
)) {
206 CTerr(CT_F_I2O_SCT
, CT_R_SCT_NOT_SET
);
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
215 if (sct
->version
== SCT_VERSION_V1
)
216 len
= 43 + sct
->ext_len
+ 4 + sct
->sig_len
;
227 pstart
= p
= OPENSSL_malloc(len
);
229 CTerr(CT_F_I2O_SCT
, ERR_R_MALLOC_FAILURE
);
235 if (sct
->version
== SCT_VERSION_V1
) {
237 memcpy(p
, sct
->log_id
, CT_V1_HASHLEN
);
239 l2n8(sct
->timestamp
, p
);
240 s2n(sct
->ext_len
, p
);
241 if (sct
->ext_len
> 0) {
242 memcpy(p
, sct
->ext
, sct
->ext_len
);
245 if (i2o_SCT_signature(sct
, &p
) <= 0)
248 memcpy(p
, sct
->sct
, len
);
253 OPENSSL_free(pstart
);
257 STACK_OF(SCT
) *o2i_SCT_LIST(STACK_OF(SCT
) **a
, const unsigned char **pp
,
260 STACK_OF(SCT
) *sk
= NULL
;
261 size_t list_len
, sct_len
;
263 if (len
< 2 || len
> MAX_SCT_LIST_SIZE
) {
264 CTerr(CT_F_O2I_SCT_LIST
, CT_R_SCT_LIST_INVALID
);
269 if (list_len
!= len
- 2) {
270 CTerr(CT_F_O2I_SCT_LIST
, CT_R_SCT_LIST_INVALID
);
274 if (a
== NULL
|| *a
== NULL
) {
275 sk
= sk_SCT_new_null();
281 /* Use the given stack, but empty it first. */
283 while ((sct
= sk_SCT_pop(sk
)) != NULL
)
287 while (list_len
> 0) {
291 CTerr(CT_F_O2I_SCT_LIST
, CT_R_SCT_LIST_INVALID
);
297 if (sct_len
== 0 || sct_len
> list_len
) {
298 CTerr(CT_F_O2I_SCT_LIST
, CT_R_SCT_LIST_INVALID
);
303 if ((sct
= o2i_SCT(NULL
, pp
, sct_len
)) == NULL
)
305 if (!sk_SCT_push(sk
, sct
)) {
311 if (a
!= NULL
&& *a
== NULL
)
316 if (a
== NULL
|| *a
== NULL
)
321 int i2o_SCT_LIST(const STACK_OF(SCT
) *a
, unsigned char **pp
)
323 int len
, sct_len
, i
, is_pp_new
= 0;
325 unsigned char *p
= NULL
, *p2
;
329 if ((len
= i2o_SCT_LIST(a
, NULL
)) == -1) {
330 CTerr(CT_F_I2O_SCT_LIST
, CT_R_SCT_LIST_INVALID
);
333 if ((*pp
= OPENSSL_malloc(len
)) == NULL
) {
334 CTerr(CT_F_I2O_SCT_LIST
, ERR_R_MALLOC_FAILURE
);
343 for (i
= 0; i
< sk_SCT_num(a
); i
++) {
347 if ((sct_len
= i2o_SCT(sk_SCT_value(a
, i
), &p
)) == -1)
351 if ((sct_len
= i2o_SCT(sk_SCT_value(a
, i
), NULL
)) == -1)
357 if (len2
> MAX_SCT_LIST_SIZE
)
376 STACK_OF(SCT
) *d2i_SCT_LIST(STACK_OF(SCT
) **a
, const unsigned char **pp
,
379 ASN1_OCTET_STRING
*oct
= NULL
;
380 STACK_OF(SCT
) *sk
= NULL
;
381 const unsigned char *p
;
384 if (d2i_ASN1_OCTET_STRING(&oct
, &p
, len
) == NULL
)
388 if ((sk
= o2i_SCT_LIST(a
, &p
, oct
->length
)) != NULL
)
391 ASN1_OCTET_STRING_free(oct
);
395 int i2d_SCT_LIST(const STACK_OF(SCT
) *a
, unsigned char **out
)
397 ASN1_OCTET_STRING oct
;
401 if ((oct
.length
= i2o_SCT_LIST(a
, &oct
.data
)) == -1)
404 len
= i2d_ASN1_OCTET_STRING(&oct
, out
);
405 OPENSSL_free(oct
.data
);