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