2 * Copyright 2016-2020 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
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>
26 SCT *sct = OPENSSL_zalloc(sizeof(*sct));
29 CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
33 sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
34 sct->version = SCT_VERSION_NOT_SET;
38 void SCT_free(SCT *sct)
43 OPENSSL_free(sct->log_id);
44 OPENSSL_free(sct->ext);
45 OPENSSL_free(sct->sig);
46 OPENSSL_free(sct->sct);
50 void SCT_LIST_free(STACK_OF(SCT) *a)
52 sk_SCT_pop_free(a, SCT_free);
55 int SCT_set_version(SCT *sct, sct_version_t version)
57 if (version != SCT_VERSION_V1) {
58 CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
61 sct->version = version;
62 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
66 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
68 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
71 case CT_LOG_ENTRY_TYPE_X509:
72 case CT_LOG_ENTRY_TYPE_PRECERT:
73 sct->entry_type = entry_type;
75 case CT_LOG_ENTRY_TYPE_NOT_SET:
78 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
82 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
84 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
85 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
89 OPENSSL_free(sct->log_id);
91 sct->log_id_len = log_id_len;
92 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
96 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
98 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
99 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
103 OPENSSL_free(sct->log_id);
106 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
108 if (log_id != NULL && log_id_len > 0) {
109 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
110 if (sct->log_id == NULL) {
111 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
114 sct->log_id_len = log_id_len;
120 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
122 sct->timestamp = timestamp;
123 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
126 int SCT_set_signature_nid(SCT *sct, int nid)
129 case NID_sha256WithRSAEncryption:
130 sct->hash_alg = TLSEXT_hash_sha256;
131 sct->sig_alg = TLSEXT_signature_rsa;
132 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
134 case NID_ecdsa_with_SHA256:
135 sct->hash_alg = TLSEXT_hash_sha256;
136 sct->sig_alg = TLSEXT_signature_ecdsa;
137 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
140 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
145 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
147 OPENSSL_free(sct->ext);
149 sct->ext_len = ext_len;
150 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
153 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
155 OPENSSL_free(sct->ext);
158 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
160 if (ext != NULL && ext_len > 0) {
161 sct->ext = OPENSSL_memdup(ext, ext_len);
162 if (sct->ext == NULL) {
163 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
166 sct->ext_len = ext_len;
171 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
173 OPENSSL_free(sct->sig);
175 sct->sig_len = sig_len;
176 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
179 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
181 OPENSSL_free(sct->sig);
184 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
186 if (sig != NULL && sig_len > 0) {
187 sct->sig = OPENSSL_memdup(sig, sig_len);
188 if (sct->sig == NULL) {
189 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
192 sct->sig_len = sig_len;
197 sct_version_t SCT_get_version(const SCT *sct)
202 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
204 return sct->entry_type;
207 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
209 *log_id = sct->log_id;
210 return sct->log_id_len;
213 uint64_t SCT_get_timestamp(const SCT *sct)
215 return sct->timestamp;
218 int SCT_get_signature_nid(const SCT *sct)
220 if (sct->version == SCT_VERSION_V1) {
221 if (sct->hash_alg == TLSEXT_hash_sha256) {
222 switch (sct->sig_alg) {
223 case TLSEXT_signature_ecdsa:
224 return NID_ecdsa_with_SHA256;
225 case TLSEXT_signature_rsa:
226 return NID_sha256WithRSAEncryption;
235 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
241 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
247 int SCT_is_complete(const SCT *sct)
249 switch (sct->version) {
250 case SCT_VERSION_NOT_SET:
253 return sct->log_id != NULL && SCT_signature_is_complete(sct);
255 return sct->sct != NULL; /* Just need cached encoding */
259 int SCT_signature_is_complete(const SCT *sct)
261 return SCT_get_signature_nid(sct) != NID_undef &&
262 sct->sig != NULL && sct->sig_len > 0;
265 sct_source_t SCT_get_source(const SCT *sct)
270 int SCT_set_source(SCT *sct, sct_source_t source)
272 sct->source = source;
273 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
275 case SCT_SOURCE_TLS_EXTENSION:
276 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
277 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
278 case SCT_SOURCE_X509V3_EXTENSION:
279 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
280 case SCT_SOURCE_UNKNOWN:
283 /* if we aren't sure, leave the log entry type alone */
287 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
289 return sct->validation_status;
292 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
294 int is_sct_valid = -1;
295 SCT_CTX *sctx = NULL;
296 X509_PUBKEY *pub = NULL, *log_pkey = NULL;
300 * With an unrecognized SCT version we don't know what such an SCT means,
301 * let alone validate one. So we return validation failure (0).
303 if (sct->version != SCT_VERSION_V1) {
304 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
308 log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
309 sct->log_id, sct->log_id_len);
311 /* Similarly, an SCT from an unknown log also cannot be validated. */
313 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
317 sctx = SCT_CTX_new(ctx->libctx, ctx->propq);
321 if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
323 if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
326 if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
327 EVP_PKEY *issuer_pkey;
329 if (ctx->issuer == NULL) {
330 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
334 issuer_pkey = X509_get0_pubkey(ctx->issuer);
336 if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
338 if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
342 SCT_CTX_set_time(sctx, ctx->epoch_time_in_ms);
345 * XXX: Potential for optimization. This repeats some idempotent heavy
346 * lifting on the certificate for each candidate SCT, and appears to not
347 * use any information in the SCT itself, only the certificate is
348 * processed. So it may make more sense to to do this just once, perhaps
349 * associated with the shared (by all SCTs) policy eval ctx.
351 * XXX: Failure here is global (SCT independent) and represents either an
352 * issue with the certificate (e.g. duplicate extensions) or an out of
353 * memory condition. When the certificate is incompatible with CT, we just
354 * mark the SCTs invalid, rather than report a failure to determine the
355 * validation status. That way, callbacks that want to do "soft" SCT
356 * processing will not abort handshakes with false positive internal
357 * errors. Since the function does not distinguish between certificate
358 * issues (peer's fault) and internal problems (out fault) the safe thing
359 * to do is to report a validation failure and let the callback or
360 * application decide what to do.
362 if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
363 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
365 sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ?
366 SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
369 is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
371 X509_PUBKEY_free(pub);
372 X509_PUBKEY_free(log_pkey);
378 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
380 int are_scts_valid = 1;
381 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
384 for (i = 0; i < sct_count; ++i) {
385 int is_sct_valid = -1;
386 SCT *sct = sk_SCT_value(scts, i);
391 is_sct_valid = SCT_validate(sct, ctx);
392 if (is_sct_valid < 0)
394 are_scts_valid &= is_sct_valid;
397 return are_scts_valid;