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