1 /*
2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #ifdef OPENSSL_NO_CT
11 # error "CT is disabled"
12 #endif
13
14 #include <openssl/ct.h>
15 #include <openssl/err.h>
16 #include <time.h>
17
18 #include "ct_local.h"
19
20 /*
21 * Number of seconds in the future that an SCT timestamp can be, by default,
22 * without being considered invalid. This is added to time() when setting a
23 * default value for CT_POLICY_EVAL_CTX.epoch_time_in_ms.
24 * It can be overridden by calling CT_POLICY_EVAL_CTX_set_time().
25 */
26 static const time_t SCT_CLOCK_DRIFT_TOLERANCE = 300;
27
CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX * libctx,const char * propq)28 CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX *libctx,
29 const char *propq)
30 {
31 CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));
32
33 if (ctx == NULL) {
34 ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
35 return NULL;
36 }
37
38 ctx->libctx = libctx;
39 if (propq != NULL) {
40 ctx->propq = OPENSSL_strdup(propq);
41 if (ctx->propq == NULL) {
42 ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
43 OPENSSL_free(ctx);
44 return NULL;
45 }
46 }
47
48 /* time(NULL) shouldn't ever fail, so don't bother checking for -1. */
49 ctx->epoch_time_in_ms = (uint64_t)(time(NULL) + SCT_CLOCK_DRIFT_TOLERANCE) *
50 1000;
51
52 return ctx;
53 }
54
CT_POLICY_EVAL_CTX_new(void)55 CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
56 {
57 return CT_POLICY_EVAL_CTX_new_ex(NULL, NULL);
58 }
59
CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX * ctx)60 void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
61 {
62 if (ctx == NULL)
63 return;
64 X509_free(ctx->cert);
65 X509_free(ctx->issuer);
66 OPENSSL_free(ctx->propq);
67 OPENSSL_free(ctx);
68 }
69
CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX * ctx,X509 * cert)70 int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
71 {
72 if (!X509_up_ref(cert))
73 return 0;
74 ctx->cert = cert;
75 return 1;
76 }
77
CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX * ctx,X509 * issuer)78 int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
79 {
80 if (!X509_up_ref(issuer))
81 return 0;
82 ctx->issuer = issuer;
83 return 1;
84 }
85
CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX * ctx,CTLOG_STORE * log_store)86 void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
87 CTLOG_STORE *log_store)
88 {
89 ctx->log_store = log_store;
90 }
91
CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX * ctx,uint64_t time_in_ms)92 void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
93 {
94 ctx->epoch_time_in_ms = time_in_ms;
95 }
96
CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX * ctx)97 X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
98 {
99 return ctx->cert;
100 }
101
CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX * ctx)102 X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
103 {
104 return ctx->issuer;
105 }
106
CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX * ctx)107 const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
108 {
109 return ctx->log_store;
110 }
111
CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX * ctx)112 uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
113 {
114 return ctx->epoch_time_in_ms;
115 }
116