2016-03-01 01:33:02 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-18 02:51:26 +08:00
|
|
|
*
|
2018-12-06 20:34:58 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
2016-03-01 01:33:02 +08:00
|
|
|
|
|
|
|
#ifdef OPENSSL_NO_CT
|
|
|
|
# error "CT is disabled"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <openssl/ct.h>
|
|
|
|
#include <openssl/err.h>
|
2016-09-12 23:57:38 +08:00
|
|
|
#include <time.h>
|
2016-03-01 01:33:02 +08:00
|
|
|
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ct_local.h"
|
2016-03-01 01:33:02 +08:00
|
|
|
|
2016-09-15 03:26:23 +08:00
|
|
|
/*
|
|
|
|
* Number of seconds in the future that an SCT timestamp can be, by default,
|
|
|
|
* without being considered invalid. This is added to time() when setting a
|
|
|
|
* default value for CT_POLICY_EVAL_CTX.epoch_time_in_ms.
|
|
|
|
* It can be overridden by calling CT_POLICY_EVAL_CTX_set_time().
|
|
|
|
*/
|
2016-09-13 00:02:58 +08:00
|
|
|
static const time_t SCT_CLOCK_DRIFT_TOLERANCE = 300;
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
|
|
|
CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
|
2016-03-01 01:33:02 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
ctx->libctx = libctx;
|
|
|
|
if (propq != NULL) {
|
|
|
|
ctx->propq = OPENSSL_strdup(propq);
|
|
|
|
if (ctx->propq == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
|
2020-04-27 05:59:02 +08:00
|
|
|
OPENSSL_free(ctx);
|
2020-04-03 23:25:18 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 03:26:23 +08:00
|
|
|
/* time(NULL) shouldn't ever fail, so don't bother checking for -1. */
|
2016-09-15 03:25:01 +08:00
|
|
|
ctx->epoch_time_in_ms = (uint64_t)(time(NULL) + SCT_CLOCK_DRIFT_TOLERANCE) *
|
|
|
|
1000;
|
|
|
|
|
2016-03-01 01:33:02 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return CT_POLICY_EVAL_CTX_new_ex(NULL, NULL);
|
2020-04-03 23:25:18 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 01:33:02 +08:00
|
|
|
void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
|
|
|
|
{
|
2016-08-23 05:21:30 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2016-08-05 21:17:31 +08:00
|
|
|
X509_free(ctx->cert);
|
|
|
|
X509_free(ctx->issuer);
|
2020-04-03 23:25:18 +08:00
|
|
|
OPENSSL_free(ctx->propq);
|
2016-03-01 01:33:02 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:47:02 +08:00
|
|
|
int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
2016-08-15 21:47:02 +08:00
|
|
|
if (!X509_up_ref(cert))
|
|
|
|
return 0;
|
|
|
|
ctx->cert = cert;
|
|
|
|
return 1;
|
2016-03-01 01:33:02 +08:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:47:02 +08:00
|
|
|
int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
2016-08-15 21:47:02 +08:00
|
|
|
if (!X509_up_ref(issuer))
|
|
|
|
return 0;
|
|
|
|
ctx->issuer = issuer;
|
|
|
|
return 1;
|
2016-03-01 01:33:02 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 21:17:31 +08:00
|
|
|
void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
|
|
|
|
CTLOG_STORE *log_store)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
|
|
|
ctx->log_store = log_store;
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:02:46 +08:00
|
|
|
void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
|
|
|
|
{
|
|
|
|
ctx->epoch_time_in_ms = time_in_ms;
|
|
|
|
}
|
|
|
|
|
2016-03-11 07:26:41 +08:00
|
|
|
X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
|
|
|
return ctx->cert;
|
|
|
|
}
|
|
|
|
|
2016-03-11 07:26:41 +08:00
|
|
|
X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
|
|
|
return ctx->issuer;
|
|
|
|
}
|
|
|
|
|
2016-03-11 07:26:41 +08:00
|
|
|
const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
|
2016-03-01 01:33:02 +08:00
|
|
|
{
|
|
|
|
return ctx->log_store;
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:02:46 +08:00
|
|
|
uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
|
|
|
|
{
|
|
|
|
return ctx->epoch_time_in_ms;
|
|
|
|
}
|