2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
14 #include <openssl/ct.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/tls1.h>
18 #include <openssl/x509.h>
24 SCT *sct = OPENSSL_zalloc(sizeof(*sct));
27 CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
31 sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
32 sct->version = SCT_VERSION_NOT_SET;
36 void SCT_free(SCT *sct)
41 OPENSSL_free(sct->log_id);
42 OPENSSL_free(sct->ext);
43 OPENSSL_free(sct->sig);
44 OPENSSL_free(sct->sct);
48 int SCT_set_version(SCT *sct, sct_version_t version)
50 if (version != SCT_VERSION_V1) {
51 CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
54 sct->version = version;
55 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
59 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
61 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
64 case CT_LOG_ENTRY_TYPE_X509:
65 case CT_LOG_ENTRY_TYPE_PRECERT:
66 sct->entry_type = entry_type;
69 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
74 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
76 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
77 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
81 OPENSSL_free(sct->log_id);
83 sct->log_id_len = log_id_len;
84 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
88 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
90 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
91 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
95 OPENSSL_free(sct->log_id);
98 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
100 if (log_id != NULL && log_id_len > 0) {
101 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
102 if (sct->log_id == NULL) {
103 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
106 sct->log_id_len = log_id_len;
112 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
114 sct->timestamp = timestamp;
115 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
118 int SCT_set_signature_nid(SCT *sct, int nid)
121 case NID_sha256WithRSAEncryption:
122 sct->hash_alg = TLSEXT_hash_sha256;
123 sct->sig_alg = TLSEXT_signature_rsa;
124 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
126 case NID_ecdsa_with_SHA256:
127 sct->hash_alg = TLSEXT_hash_sha256;
128 sct->sig_alg = TLSEXT_signature_ecdsa;
129 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
132 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
137 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
139 OPENSSL_free(sct->ext);
141 sct->ext_len = ext_len;
142 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
145 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
147 OPENSSL_free(sct->ext);
150 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
152 if (ext != NULL && ext_len > 0) {
153 sct->ext = OPENSSL_memdup(ext, ext_len);
154 if (sct->ext == NULL) {
155 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
158 sct->ext_len = ext_len;
163 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
165 OPENSSL_free(sct->sig);
167 sct->sig_len = sig_len;
168 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
171 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
173 OPENSSL_free(sct->sig);
176 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
178 if (sig != NULL && sig_len > 0) {
179 sct->sig = OPENSSL_memdup(sig, sig_len);
180 if (sct->sig == NULL) {
181 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
184 sct->sig_len = sig_len;
189 sct_version_t SCT_get_version(const SCT *sct)
194 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
196 return sct->entry_type;
199 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
201 *log_id = sct->log_id;
202 return sct->log_id_len;
205 uint64_t SCT_get_timestamp(const SCT *sct)
207 return sct->timestamp;
210 int SCT_get_signature_nid(const SCT *sct)
212 if (sct->version == SCT_VERSION_V1) {
213 if (sct->hash_alg == TLSEXT_hash_sha256) {
214 switch (sct->sig_alg) {
215 case TLSEXT_signature_ecdsa:
216 return NID_ecdsa_with_SHA256;
217 case TLSEXT_signature_rsa:
218 return NID_sha256WithRSAEncryption;
227 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
233 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
239 int SCT_is_complete(const SCT *sct)
241 switch (sct->version) {
242 case SCT_VERSION_NOT_SET:
245 return sct->log_id != NULL && SCT_signature_is_complete(sct);
247 return sct->sct != NULL; /* Just need cached encoding */
251 int SCT_signature_is_complete(const SCT *sct)
253 return SCT_get_signature_nid(sct) != NID_undef &&
254 sct->sig != NULL && sct->sig_len > 0;
257 sct_source_t SCT_get_source(const SCT *sct)
262 int SCT_set_source(SCT *sct, sct_source_t source)
264 sct->source = source;
265 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
267 case SCT_SOURCE_TLS_EXTENSION:
268 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
269 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
270 case SCT_SOURCE_X509V3_EXTENSION:
271 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
272 default: /* if we aren't sure, leave the log entry type alone */
277 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
279 return sct->validation_status;
282 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
284 int is_sct_valid = -1;
285 SCT_CTX *sctx = NULL;
286 X509_PUBKEY *pub = NULL, *log_pkey = NULL;
290 * With an unrecognized SCT version we don't know what such an SCT means,
291 * let alone validate one. So we return validation failure (0).
293 if (sct->version != SCT_VERSION_V1) {
294 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
298 log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
299 sct->log_id, sct->log_id_len);
301 /* Similarly, an SCT from an unknown log also cannot be validated. */
303 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
307 sctx = SCT_CTX_new();
311 if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
313 if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
316 if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
317 EVP_PKEY *issuer_pkey;
319 if (ctx->issuer == NULL) {
320 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
324 issuer_pkey = X509_get0_pubkey(ctx->issuer);
326 if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
328 if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
333 * XXX: Potential for optimization. This repeats some idempotent heavy
334 * lifting on the certificate for each candidate SCT, and appears to not
335 * use any information in the SCT itself, only the certificate is
336 * processed. So it may make more sense to to do this just once, perhaps
337 * associated with the shared (by all SCTs) policy eval ctx.
339 * XXX: Failure here is global (SCT independent) and represents either an
340 * issue with the certificate (e.g. duplicate extensions) or an out of
341 * memory condition. When the certificate is incompatible with CT, we just
342 * mark the SCTs invalid, rather than report a failure to determine the
343 * validation status. That way, callbacks that want to do "soft" SCT
344 * processing will not abort handshakes with false positive internal
345 * errors. Since the function does not distinguish between certificate
346 * issues (peer's fault) and internal problems (out fault) the safe thing
347 * to do is to report a validation failure and let the callback or
348 * application decide what to do.
350 if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
351 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
353 sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ?
354 SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
357 is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
359 X509_PUBKEY_free(pub);
360 X509_PUBKEY_free(log_pkey);
366 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
368 int are_scts_valid = 1;
369 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
372 for (i = 0; i < sct_count; ++i) {
373 int is_sct_valid = -1;
374 SCT *sct = sk_SCT_value(scts, i);
379 is_sct_valid = SCT_validate(sct, ctx);
380 if (is_sct_valid < 0)
382 are_scts_valid &= is_sct_valid;
385 return are_scts_valid;