1bc22749d3c2b222ff85196eae62f8cd750a7d61
[oweals/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 CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
21 {
22     CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));
23
24     if (ctx == NULL) {
25         CTerr(CT_F_CT_POLICY_EVAL_CTX_NEW, ERR_R_MALLOC_FAILURE);
26         return NULL;
27     }
28
29     // time(NULL) shouldn't ever fail, so don't bother checking for -1.
30     ctx->epoch_time_in_ms = time(NULL) * 1000;
31     return ctx;
32 }
33
34 void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
35 {
36     if (ctx == NULL)
37         return;
38     X509_free(ctx->cert);
39     X509_free(ctx->issuer);
40     OPENSSL_free(ctx);
41 }
42
43 int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
44 {
45     if (!X509_up_ref(cert))
46         return 0;
47     ctx->cert = cert;
48     return 1;
49 }
50
51 int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
52 {
53     if (!X509_up_ref(issuer))
54         return 0;
55     ctx->issuer = issuer;
56     return 1;
57 }
58
59 void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
60                                                CTLOG_STORE *log_store)
61 {
62     ctx->log_store = log_store;
63 }
64
65 void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
66 {
67     ctx->epoch_time_in_ms = time_in_ms;
68 }
69
70 X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
71 {
72     return ctx->cert;
73 }
74
75 X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
76 {
77     return ctx->issuer;
78 }
79
80 const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
81 {
82     return ctx->log_store;
83 }
84
85 uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
86 {
87     return ctx->epoch_time_in_ms;
88 }