/src/openssl/crypto/ct/ct_policy.c
Line  | Count  | Source  | 
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 "internal/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  |  |  | 
28  |  | CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX *libctx,  | 
29  |  |                                               const char *propq)  | 
30  | 0  | { | 
31  | 0  |     CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));  | 
32  | 0  |     OSSL_TIME now;  | 
33  |  | 
  | 
34  | 0  |     if (ctx == NULL)  | 
35  | 0  |         return NULL;  | 
36  |  |  | 
37  | 0  |     ctx->libctx = libctx;  | 
38  | 0  |     if (propq != NULL) { | 
39  | 0  |         ctx->propq = OPENSSL_strdup(propq);  | 
40  | 0  |         if (ctx->propq == NULL) { | 
41  | 0  |             OPENSSL_free(ctx);  | 
42  | 0  |             return NULL;  | 
43  | 0  |         }  | 
44  | 0  |     }  | 
45  |  |  | 
46  | 0  |     now = ossl_time_add(ossl_time_now(),  | 
47  | 0  |                         ossl_seconds2time(SCT_CLOCK_DRIFT_TOLERANCE));  | 
48  | 0  |     ctx->epoch_time_in_ms = ossl_time2ms(now);  | 
49  |  | 
  | 
50  | 0  |     return ctx;  | 
51  | 0  | }  | 
52  |  |  | 
53  |  | CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)  | 
54  | 0  | { | 
55  | 0  |     return CT_POLICY_EVAL_CTX_new_ex(NULL, NULL);  | 
56  | 0  | }  | 
57  |  |  | 
58  |  | void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)  | 
59  | 0  | { | 
60  | 0  |     if (ctx == NULL)  | 
61  | 0  |         return;  | 
62  | 0  |     X509_free(ctx->cert);  | 
63  | 0  |     X509_free(ctx->issuer);  | 
64  | 0  |     OPENSSL_free(ctx->propq);  | 
65  | 0  |     OPENSSL_free(ctx);  | 
66  | 0  | }  | 
67  |  |  | 
68  |  | int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)  | 
69  | 0  | { | 
70  | 0  |     if (!X509_up_ref(cert))  | 
71  | 0  |         return 0;  | 
72  | 0  |     ctx->cert = cert;  | 
73  | 0  |     return 1;  | 
74  | 0  | }  | 
75  |  |  | 
76  |  | int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)  | 
77  | 0  | { | 
78  | 0  |     if (!X509_up_ref(issuer))  | 
79  | 0  |         return 0;  | 
80  | 0  |     ctx->issuer = issuer;  | 
81  | 0  |     return 1;  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,  | 
85  |  |                                                CTLOG_STORE *log_store)  | 
86  | 0  | { | 
87  | 0  |     ctx->log_store = log_store;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)  | 
91  | 0  | { | 
92  | 0  |     ctx->epoch_time_in_ms = time_in_ms;  | 
93  | 0  | }  | 
94  |  |  | 
95  |  | X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)  | 
96  | 0  | { | 
97  | 0  |     return ctx->cert;  | 
98  | 0  | }  | 
99  |  |  | 
100  |  | X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)  | 
101  | 0  | { | 
102  | 0  |     return ctx->issuer;  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)  | 
106  | 0  | { | 
107  | 0  |     return ctx->log_store;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)  | 
111  | 0  | { | 
112  | 0  |     return ctx->epoch_time_in_ms;  | 
113  | 0  | }  |