0f274439716cb968e117ccc18df1a485b599bc1d
[openssl.git] / crypto / ct / ct_policy.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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_locl.h"
19
20 // Number of seconds in the future that an SCT timestamp can be, by default,
21 // without being considered invalid. This is added to time() when setting a
22 // default value for CT_POLICY_EVAL_CTX.epoch_time_in_ms.
23 // It can be overridden by calling CT_POLICY_EVAL_CTX_set_time().
24 static const time_t SCT_CLOCK_DRIFT_TOLERANCE = 300;
25
26 CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
27 {
28     CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));
29
30     if (ctx == NULL) {
31         CTerr(CT_F_CT_POLICY_EVAL_CTX_NEW, ERR_R_MALLOC_FAILURE);
32         return NULL;
33     }
34
35     // time(NULL) shouldn't ever fail, so don't bother checking for -1.
36     ctx->epoch_time_in_ms = (uint64_t)(time(NULL) + SCT_CLOCK_DRIFT_TOLERANCE) *
37             1000;
38
39     return ctx;
40 }
41
42 void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
43 {
44     if (ctx == NULL)
45         return;
46     X509_free(ctx->cert);
47     X509_free(ctx->issuer);
48     OPENSSL_free(ctx);
49 }
50
51 int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
52 {
53     if (!X509_up_ref(cert))
54         return 0;
55     ctx->cert = cert;
56     return 1;
57 }
58
59 int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
60 {
61     if (!X509_up_ref(issuer))
62         return 0;
63     ctx->issuer = issuer;
64     return 1;
65 }
66
67 void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
68                                                CTLOG_STORE *log_store)
69 {
70     ctx->log_store = log_store;
71 }
72
73 void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
74 {
75     ctx->epoch_time_in_ms = time_in_ms;
76 }
77
78 X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
79 {
80     return ctx->cert;
81 }
82
83 X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
84 {
85     return ctx->issuer;
86 }
87
88 const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
89 {
90     return ctx->log_store;
91 }
92
93 uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
94 {
95     return ctx->epoch_time_in_ms;
96 }