2 * Written by Rob Stradling (rob@comodo.com), Stephen Henson (steve@openssl.org)
3 * and Adam Eijdenberg (adam.eijdenberg@gmail.com) for the OpenSSL project 2016.
5 /* ====================================================================
6 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/ct.h>
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/tls1.h>
67 #include <openssl/x509.h>
73 SCT *sct = OPENSSL_zalloc(sizeof(*sct));
76 CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
80 sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
81 sct->version = SCT_VERSION_NOT_SET;
85 void SCT_free(SCT *sct)
90 OPENSSL_free(sct->log_id);
91 OPENSSL_free(sct->ext);
92 OPENSSL_free(sct->sig);
93 OPENSSL_free(sct->sct);
97 int SCT_set_version(SCT *sct, sct_version_t version)
99 if (version != SCT_VERSION_V1) {
100 CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
103 sct->version = version;
107 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
109 switch (entry_type) {
110 case CT_LOG_ENTRY_TYPE_X509:
111 case CT_LOG_ENTRY_TYPE_PRECERT:
112 sct->entry_type = entry_type;
115 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
120 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
122 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
123 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
127 OPENSSL_free(sct->log_id);
128 sct->log_id = log_id;
129 sct->log_id_len = log_id_len;
133 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
135 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
136 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
140 OPENSSL_free(sct->log_id);
144 if (log_id != NULL && log_id_len > 0) {
145 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
146 if (sct->log_id == NULL) {
147 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
150 sct->log_id_len = log_id_len;
156 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
158 sct->timestamp = timestamp;
161 int SCT_set_signature_nid(SCT *sct, int nid)
164 case NID_sha256WithRSAEncryption:
165 sct->hash_alg = TLSEXT_hash_sha256;
166 sct->sig_alg = TLSEXT_signature_rsa;
168 case NID_ecdsa_with_SHA256:
169 sct->hash_alg = TLSEXT_hash_sha256;
170 sct->sig_alg = TLSEXT_signature_ecdsa;
173 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
178 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
180 OPENSSL_free(sct->ext);
182 sct->ext_len = ext_len;
185 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
187 OPENSSL_free(sct->ext);
191 if (ext != NULL && ext_len > 0) {
192 sct->ext = OPENSSL_memdup(ext, ext_len);
193 if (sct->ext == NULL) {
194 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
197 sct->ext_len = ext_len;
202 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
204 OPENSSL_free(sct->sig);
206 sct->sig_len = sig_len;
209 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
211 OPENSSL_free(sct->sig);
215 if (sig != NULL && sig_len > 0) {
216 sct->sig = OPENSSL_memdup(sig, sig_len);
217 if (sct->sig == NULL) {
218 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
221 sct->sig_len = sig_len;
226 sct_version_t SCT_get_version(const SCT *sct)
231 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
233 return sct->entry_type;
236 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
238 *log_id = sct->log_id;
239 return sct->log_id_len;
242 const char *SCT_get0_log_name(const SCT *sct)
244 return CTLOG_get0_name(sct->log);
247 uint64_t SCT_get_timestamp(const SCT *sct)
249 return sct->timestamp;
252 int SCT_get_signature_nid(const SCT *sct)
254 if (sct->version == SCT_VERSION_V1) {
255 if (sct->hash_alg == TLSEXT_hash_sha256) {
256 switch (sct->sig_alg) {
257 case TLSEXT_signature_ecdsa:
258 return NID_ecdsa_with_SHA256;
259 case TLSEXT_signature_rsa:
260 return NID_sha256WithRSAEncryption;
269 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
275 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
281 int SCT_is_complete(const SCT *sct)
283 switch (sct->version) {
284 case SCT_VERSION_NOT_SET:
287 return sct->log_id != NULL && SCT_signature_is_complete(sct);
289 return sct->sct != NULL; /* Just need cached encoding */
293 int SCT_signature_is_complete(const SCT *sct)
295 return SCT_get_signature_nid(sct) != NID_undef &&
296 sct->sig != NULL && sct->sig_len > 0;
299 sct_source_t SCT_get_source(const SCT *sct)
304 int SCT_set_source(SCT *sct, sct_source_t source)
306 sct->source = source;
308 case SCT_SOURCE_TLS_EXTENSION:
309 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
310 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
311 case SCT_SOURCE_X509V3_EXTENSION:
312 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
313 default: /* if we aren't sure, leave the log entry type alone */
318 int SCT_LIST_set_source(const STACK_OF(SCT) *scts, sct_source_t source)
322 for (i = 0; i < sk_SCT_num(scts); ++i) {
323 ret = SCT_set_source(sk_SCT_value(scts, i), source);
331 CTLOG *SCT_get0_log(const SCT *sct)
336 int SCT_set0_log(SCT *sct, const CTLOG_STORE *ct_logs)
338 sct->log = CTLOG_STORE_get0_log_by_id(ct_logs, sct->log_id, sct->log_id_len);
340 return sct->log != NULL;
343 int SCT_LIST_set0_logs(STACK_OF(SCT) *sct_list, const CTLOG_STORE *ct_logs)
345 int sct_logs_found = 0;
348 for (i = 0; i < sk_SCT_num(sct_list); ++i) {
349 SCT *sct = sk_SCT_value(sct_list, i);
351 if (sct->log == NULL)
352 SCT_set0_log(sct, ct_logs);
353 if (sct->log != NULL)
357 return sct_logs_found;