Check that SCT timestamps are not in the future
[openssl.git] / crypto / ct / ct_sct.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 disabled"
12 #endif
13
14 #include <openssl/ct.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/tls1.h>
18 #include <openssl/x509.h>
19
20 #include "ct_locl.h"
21
22 SCT *SCT_new(void)
23 {
24     SCT *sct = OPENSSL_zalloc(sizeof(*sct));
25
26     if (sct == NULL) {
27         CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
28         return NULL;
29     }
30
31     sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
32     sct->version = SCT_VERSION_NOT_SET;
33     return sct;
34 }
35
36 void SCT_free(SCT *sct)
37 {
38     if (sct == NULL)
39         return;
40
41     OPENSSL_free(sct->log_id);
42     OPENSSL_free(sct->ext);
43     OPENSSL_free(sct->sig);
44     OPENSSL_free(sct->sct);
45     OPENSSL_free(sct);
46 }
47
48 int SCT_set_version(SCT *sct, sct_version_t version)
49 {
50     if (version != SCT_VERSION_V1) {
51         CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
52         return 0;
53     }
54     sct->version = version;
55     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
56     return 1;
57 }
58
59 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
60 {
61     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
62
63     switch (entry_type) {
64     case CT_LOG_ENTRY_TYPE_X509:
65     case CT_LOG_ENTRY_TYPE_PRECERT:
66         sct->entry_type = entry_type;
67         return 1;
68     case CT_LOG_ENTRY_TYPE_NOT_SET:
69         break;
70     }
71     CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
72     return 0;
73 }
74
75 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
76 {
77     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
78         CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
79         return 0;
80     }
81
82     OPENSSL_free(sct->log_id);
83     sct->log_id = log_id;
84     sct->log_id_len = log_id_len;
85     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
86     return 1;
87 }
88
89 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
90 {
91     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
92         CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
93         return 0;
94     }
95
96     OPENSSL_free(sct->log_id);
97     sct->log_id = NULL;
98     sct->log_id_len = 0;
99     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
100
101     if (log_id != NULL && log_id_len > 0) {
102         sct->log_id = OPENSSL_memdup(log_id, log_id_len);
103         if (sct->log_id == NULL) {
104             CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
105             return 0;
106         }
107         sct->log_id_len = log_id_len;
108     }
109     return 1;
110 }
111
112
113 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
114 {
115     sct->timestamp = timestamp;
116     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
117 }
118
119 int SCT_set_signature_nid(SCT *sct, int nid)
120 {
121     switch (nid) {
122     case NID_sha256WithRSAEncryption:
123         sct->hash_alg = TLSEXT_hash_sha256;
124         sct->sig_alg = TLSEXT_signature_rsa;
125         sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
126         return 1;
127     case NID_ecdsa_with_SHA256:
128         sct->hash_alg = TLSEXT_hash_sha256;
129         sct->sig_alg = TLSEXT_signature_ecdsa;
130         sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
131         return 1;
132     default:
133         CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
134         return 0;
135     }
136 }
137
138 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
139 {
140     OPENSSL_free(sct->ext);
141     sct->ext = ext;
142     sct->ext_len = ext_len;
143     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
144 }
145
146 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
147 {
148     OPENSSL_free(sct->ext);
149     sct->ext = NULL;
150     sct->ext_len = 0;
151     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
152
153     if (ext != NULL && ext_len > 0) {
154         sct->ext = OPENSSL_memdup(ext, ext_len);
155         if (sct->ext == NULL) {
156             CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
157             return 0;
158         }
159         sct->ext_len = ext_len;
160     }
161     return 1;
162 }
163
164 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
165 {
166     OPENSSL_free(sct->sig);
167     sct->sig = sig;
168     sct->sig_len = sig_len;
169     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
170 }
171
172 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
173 {
174     OPENSSL_free(sct->sig);
175     sct->sig = NULL;
176     sct->sig_len = 0;
177     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
178
179     if (sig != NULL && sig_len > 0) {
180         sct->sig = OPENSSL_memdup(sig, sig_len);
181         if (sct->sig == NULL) {
182             CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
183             return 0;
184         }
185         sct->sig_len = sig_len;
186     }
187     return 1;
188 }
189
190 sct_version_t SCT_get_version(const SCT *sct)
191 {
192     return sct->version;
193 }
194
195 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
196 {
197     return sct->entry_type;
198 }
199
200 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
201 {
202     *log_id = sct->log_id;
203     return sct->log_id_len;
204 }
205
206 uint64_t SCT_get_timestamp(const SCT *sct)
207 {
208     return sct->timestamp;
209 }
210
211 int SCT_get_signature_nid(const SCT *sct)
212 {
213     if (sct->version == SCT_VERSION_V1) {
214         if (sct->hash_alg == TLSEXT_hash_sha256) {
215             switch (sct->sig_alg) {
216             case TLSEXT_signature_ecdsa:
217                 return NID_ecdsa_with_SHA256;
218             case TLSEXT_signature_rsa:
219                 return NID_sha256WithRSAEncryption;
220             default:
221                 return NID_undef;
222             }
223         }
224     }
225     return NID_undef;
226 }
227
228 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
229 {
230     *ext = sct->ext;
231     return sct->ext_len;
232 }
233
234 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
235 {
236     *sig = sct->sig;
237     return sct->sig_len;
238 }
239
240 int SCT_is_complete(const SCT *sct)
241 {
242     switch (sct->version) {
243     case SCT_VERSION_NOT_SET:
244         return 0;
245     case SCT_VERSION_V1:
246         return sct->log_id != NULL && SCT_signature_is_complete(sct);
247     default:
248         return sct->sct != NULL; /* Just need cached encoding */
249     }
250 }
251
252 int SCT_signature_is_complete(const SCT *sct)
253 {
254     return SCT_get_signature_nid(sct) != NID_undef &&
255         sct->sig != NULL && sct->sig_len > 0;
256 }
257
258 sct_source_t SCT_get_source(const SCT *sct)
259 {
260     return sct->source;
261 }
262
263 int SCT_set_source(SCT *sct, sct_source_t source)
264 {
265     sct->source = source;
266     sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
267     switch (source) {
268     case SCT_SOURCE_TLS_EXTENSION:
269     case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
270         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
271     case SCT_SOURCE_X509V3_EXTENSION:
272         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
273     case SCT_SOURCE_UNKNOWN:
274         break;
275     }
276     /* if we aren't sure, leave the log entry type alone */
277     return 1;
278 }
279
280 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
281 {
282     return sct->validation_status;
283 }
284
285 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
286 {
287     int is_sct_valid = -1;
288     SCT_CTX *sctx = NULL;
289     X509_PUBKEY *pub = NULL, *log_pkey = NULL;
290     const CTLOG *log;
291
292     /*
293      * With an unrecognized SCT version we don't know what such an SCT means,
294      * let alone validate one.  So we return validation failure (0).
295      */
296     if (sct->version != SCT_VERSION_V1) {
297         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
298         return 0;
299     }
300
301     log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
302                                      sct->log_id, sct->log_id_len);
303
304     /* Similarly, an SCT from an unknown log also cannot be validated. */
305     if (log == NULL) {
306         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
307         return 0;
308     }
309
310     sctx = SCT_CTX_new();
311     if (sctx == NULL)
312         goto err;
313
314     if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
315         goto err;
316     if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
317         goto err;
318
319     if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
320         EVP_PKEY *issuer_pkey;
321
322         if (ctx->issuer == NULL) {
323             sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
324             goto end;
325         }
326
327         issuer_pkey = X509_get0_pubkey(ctx->issuer);
328
329         if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
330             goto err;
331         if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
332             goto err;
333     }
334
335     SCT_CTX_set_time(sctx, ctx->epoch_time_in_ms);
336
337     /*
338      * XXX: Potential for optimization.  This repeats some idempotent heavy
339      * lifting on the certificate for each candidate SCT, and appears to not
340      * use any information in the SCT itself, only the certificate is
341      * processed.  So it may make more sense to to do this just once, perhaps
342      * associated with the shared (by all SCTs) policy eval ctx.
343      *
344      * XXX: Failure here is global (SCT independent) and represents either an
345      * issue with the certificate (e.g. duplicate extensions) or an out of
346      * memory condition.  When the certificate is incompatible with CT, we just
347      * mark the SCTs invalid, rather than report a failure to determine the
348      * validation status.  That way, callbacks that want to do "soft" SCT
349      * processing will not abort handshakes with false positive internal
350      * errors.  Since the function does not distinguish between certificate
351      * issues (peer's fault) and internal problems (out fault) the safe thing
352      * to do is to report a validation failure and let the callback or
353      * application decide what to do.
354      */
355     if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
356         sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
357     else
358         sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ?
359             SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
360
361 end:
362     is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
363 err:
364     X509_PUBKEY_free(pub);
365     X509_PUBKEY_free(log_pkey);
366     SCT_CTX_free(sctx);
367
368     return is_sct_valid;
369 }
370
371 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
372 {
373     int are_scts_valid = 1;
374     int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
375     int i;
376
377     for (i = 0; i < sct_count; ++i) {
378         int is_sct_valid = -1;
379         SCT *sct = sk_SCT_value(scts, i);
380
381         if (sct == NULL)
382             continue;
383
384         is_sct_valid = SCT_validate(sct, ctx);
385         if (is_sct_valid < 0)
386             return is_sct_valid;
387         are_scts_valid &= is_sct_valid;
388     }
389
390     return are_scts_valid;
391 }