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;
104 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
108 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
110 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
112 switch (entry_type) {
113 case CT_LOG_ENTRY_TYPE_X509:
114 case CT_LOG_ENTRY_TYPE_PRECERT:
115 sct->entry_type = entry_type;
118 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
123 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
125 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
126 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
130 OPENSSL_free(sct->log_id);
131 sct->log_id = log_id;
132 sct->log_id_len = log_id_len;
133 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
137 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
139 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
140 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
144 OPENSSL_free(sct->log_id);
147 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
149 if (log_id != NULL && log_id_len > 0) {
150 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
151 if (sct->log_id == NULL) {
152 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
155 sct->log_id_len = log_id_len;
161 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
163 sct->timestamp = timestamp;
164 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
167 int SCT_set_signature_nid(SCT *sct, int nid)
170 case NID_sha256WithRSAEncryption:
171 sct->hash_alg = TLSEXT_hash_sha256;
172 sct->sig_alg = TLSEXT_signature_rsa;
173 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
175 case NID_ecdsa_with_SHA256:
176 sct->hash_alg = TLSEXT_hash_sha256;
177 sct->sig_alg = TLSEXT_signature_ecdsa;
178 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
181 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
186 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
188 OPENSSL_free(sct->ext);
190 sct->ext_len = ext_len;
191 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
194 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
196 OPENSSL_free(sct->ext);
199 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
201 if (ext != NULL && ext_len > 0) {
202 sct->ext = OPENSSL_memdup(ext, ext_len);
203 if (sct->ext == NULL) {
204 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
207 sct->ext_len = ext_len;
212 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
214 OPENSSL_free(sct->sig);
216 sct->sig_len = sig_len;
217 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
220 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
222 OPENSSL_free(sct->sig);
225 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
227 if (sig != NULL && sig_len > 0) {
228 sct->sig = OPENSSL_memdup(sig, sig_len);
229 if (sct->sig == NULL) {
230 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
233 sct->sig_len = sig_len;
238 sct_version_t SCT_get_version(const SCT *sct)
243 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
245 return sct->entry_type;
248 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
250 *log_id = sct->log_id;
251 return sct->log_id_len;
254 uint64_t SCT_get_timestamp(const SCT *sct)
256 return sct->timestamp;
259 int SCT_get_signature_nid(const SCT *sct)
261 if (sct->version == SCT_VERSION_V1) {
262 if (sct->hash_alg == TLSEXT_hash_sha256) {
263 switch (sct->sig_alg) {
264 case TLSEXT_signature_ecdsa:
265 return NID_ecdsa_with_SHA256;
266 case TLSEXT_signature_rsa:
267 return NID_sha256WithRSAEncryption;
276 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
282 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
288 int SCT_is_complete(const SCT *sct)
290 switch (sct->version) {
291 case SCT_VERSION_NOT_SET:
294 return sct->log_id != NULL && SCT_signature_is_complete(sct);
296 return sct->sct != NULL; /* Just need cached encoding */
300 int SCT_signature_is_complete(const SCT *sct)
302 return SCT_get_signature_nid(sct) != NID_undef &&
303 sct->sig != NULL && sct->sig_len > 0;
306 sct_source_t SCT_get_source(const SCT *sct)
311 int SCT_set_source(SCT *sct, sct_source_t source)
313 sct->source = source;
315 case SCT_SOURCE_TLS_EXTENSION:
316 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
317 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
318 case SCT_SOURCE_X509V3_EXTENSION:
319 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
320 default: /* if we aren't sure, leave the log entry type alone */
325 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
327 return sct->validation_status;
330 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
332 int is_sct_valid = -1;
333 SCT_CTX *sctx = NULL;
334 X509_PUBKEY *pub = NULL, *log_pkey = NULL;
337 if (sct->version != SCT_VERSION_V1) {
338 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
342 log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
343 sct->log_id, sct->log_id_len);
346 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
350 sctx = SCT_CTX_new();
354 if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
356 if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
359 if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
360 EVP_PKEY *issuer_pkey;
362 if (ctx->issuer == NULL) {
363 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
367 issuer_pkey = X509_get0_pubkey(ctx->issuer);
369 if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
371 if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
375 if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
378 sct->validation_status = SCT_verify(sctx, sct) == 1 ?
379 SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
382 is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
384 X509_PUBKEY_free(pub);
385 X509_PUBKEY_free(log_pkey);
391 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
393 int are_scts_valid = 1;
394 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
397 for (i = 0; i < sct_count; ++i) {
398 int is_sct_valid = -1;
399 SCT *sct = sk_SCT_value(scts, i);
404 is_sct_valid = SCT_validate(sct, ctx);
405 if (is_sct_valid < 0)
407 are_scts_valid &= is_sct_valid;
410 return are_scts_valid;