Makes SCT_LIST_set_source return the number of successes
[openssl.git] / crypto / ct / ct_sct.c
1 /*
2  * Written by Rob Stradling (rob@comodo.com), Stephen Henson (steve@openssl.org)
3  * and Adam Eijdenberg (adam.eijdenberg@gmail.com) for the OpenSSL project 2016.
4  */
5 /* ====================================================================
6  * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #ifdef OPENSSL_NO_CT
60 # error "CT disabled"
61 #endif
62
63 #include <openssl/ct.h>
64 #include <openssl/err.h>
65 #include <openssl/evp.h>
66 #include <openssl/tls1.h>
67 #include <openssl/x509.h>
68
69 #include "ct_locl.h"
70
71 SCT *SCT_new(void)
72 {
73     SCT *sct = OPENSSL_zalloc(sizeof(*sct));
74
75     if (sct == NULL) {
76         CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
77         return NULL;
78     }
79
80     sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
81     sct->version = SCT_VERSION_NOT_SET;
82     return sct;
83 }
84
85 void SCT_free(SCT *sct)
86 {
87     if (sct == NULL)
88         return;
89
90     OPENSSL_free(sct->log_id);
91     OPENSSL_free(sct->ext);
92     OPENSSL_free(sct->sig);
93     OPENSSL_free(sct->sct);
94     OPENSSL_free(sct);
95 }
96
97 int SCT_set_version(SCT *sct, sct_version_t version)
98 {
99     if (version != SCT_VERSION_V1) {
100         CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
101         return 0;
102     }
103     sct->version = version;
104     return 1;
105 }
106
107 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
108 {
109     switch (entry_type) {
110     case CT_LOG_ENTRY_TYPE_X509:
111     case CT_LOG_ENTRY_TYPE_PRECERT:
112         sct->entry_type = entry_type;
113         return 1;
114     default:
115         CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
116         return 0;
117     }
118 }
119
120 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
121 {
122     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
123         CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
124         return 0;
125     }
126
127     OPENSSL_free(sct->log_id);
128     sct->log_id = log_id;
129     sct->log_id_len = log_id_len;
130     return 1;
131 }
132
133 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
134 {
135     if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
136         CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
137         return 0;
138     }
139
140     OPENSSL_free(sct->log_id);
141     sct->log_id = NULL;
142     sct->log_id_len = 0;
143
144     if (log_id != NULL && log_id_len > 0) {
145         sct->log_id = OPENSSL_memdup(log_id, log_id_len);
146         if (sct->log_id == NULL) {
147             CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
148             return 0;
149         }
150         sct->log_id_len = log_id_len;
151     }
152     return 1;
153 }
154
155
156 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
157 {
158     sct->timestamp = timestamp;
159 }
160
161 int SCT_set_signature_nid(SCT *sct, int nid)
162 {
163   switch (nid) {
164     case NID_sha256WithRSAEncryption:
165         sct->hash_alg = TLSEXT_hash_sha256;
166         sct->sig_alg = TLSEXT_signature_rsa;
167         return 1;
168     case NID_ecdsa_with_SHA256:
169         sct->hash_alg = TLSEXT_hash_sha256;
170         sct->sig_alg = TLSEXT_signature_ecdsa;
171         return 1;
172     default:
173         CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
174         return 0;
175     }
176 }
177
178 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
179 {
180     OPENSSL_free(sct->ext);
181     sct->ext = ext;
182     sct->ext_len = ext_len;
183 }
184
185 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
186 {
187     OPENSSL_free(sct->ext);
188     sct->ext = NULL;
189     sct->ext_len = 0;
190
191     if (ext != NULL && ext_len > 0) {
192         sct->ext = OPENSSL_memdup(ext, ext_len);
193         if (sct->ext == NULL) {
194             CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
195             return 0;
196         }
197         sct->ext_len = ext_len;
198     }
199     return 1;
200 }
201
202 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
203 {
204     OPENSSL_free(sct->sig);
205     sct->sig = sig;
206     sct->sig_len = sig_len;
207 }
208
209 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
210 {
211     OPENSSL_free(sct->sig);
212     sct->sig = NULL;
213     sct->sig_len = 0;
214
215     if (sig != NULL && sig_len > 0) {
216         sct->sig = OPENSSL_memdup(sig, sig_len);
217         if (sct->sig == NULL) {
218             CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
219             return 0;
220         }
221         sct->sig_len = sig_len;
222     }
223     return 1;
224 }
225
226 sct_version_t SCT_get_version(const SCT *sct)
227 {
228     return sct->version;
229 }
230
231 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
232 {
233     return sct->entry_type;
234 }
235
236 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
237 {
238     *log_id = sct->log_id;
239     return sct->log_id_len;
240 }
241
242 const char *SCT_get0_log_name(const SCT *sct)
243 {
244     return CTLOG_get0_name(sct->log);
245 }
246
247 uint64_t SCT_get_timestamp(const SCT *sct)
248 {
249     return sct->timestamp;
250 }
251
252 int SCT_get_signature_nid(const SCT *sct)
253 {
254     if (sct->version == SCT_VERSION_V1) {
255         if (sct->hash_alg == TLSEXT_hash_sha256) {
256             switch (sct->sig_alg) {
257             case TLSEXT_signature_ecdsa:
258                 return NID_ecdsa_with_SHA256;
259             case TLSEXT_signature_rsa:
260                 return NID_sha256WithRSAEncryption;
261             default:
262                 return NID_undef;
263             }
264         }
265     }
266     return NID_undef;
267 }
268
269 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
270 {
271     *ext = sct->ext;
272     return sct->ext_len;
273 }
274
275 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
276 {
277     *sig = sct->sig;
278     return sct->sig_len;
279 }
280
281 int SCT_is_complete(const SCT *sct)
282 {
283     switch (sct->version) {
284     case SCT_VERSION_NOT_SET:
285         return 0;
286     case SCT_VERSION_V1:
287         return sct->log_id != NULL && SCT_signature_is_complete(sct);
288     default:
289         return sct->sct != NULL; /* Just need cached encoding */
290     }
291 }
292
293 int SCT_signature_is_complete(const SCT *sct)
294 {
295     return SCT_get_signature_nid(sct) != NID_undef &&
296         sct->sig != NULL && sct->sig_len > 0;
297 }
298
299 sct_source_t SCT_get_source(const SCT *sct)
300 {
301     return sct->source;
302 }
303
304 int SCT_set_source(SCT *sct, sct_source_t source)
305 {
306     sct->source = source;
307     switch (source) {
308     case SCT_SOURCE_TLS_EXTENSION:
309     case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
310         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
311     case SCT_SOURCE_X509V3_EXTENSION:
312         return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
313     default: /* if we aren't sure, leave the log entry type alone */
314         return 1;
315     }
316 }
317
318 int SCT_LIST_set_source(const STACK_OF(SCT) *scts, sct_source_t source)
319 {
320     int i, ret = 0;
321
322     for (i = 0; i < sk_SCT_num(scts); ++i) {
323         if (SCT_set_source(sk_SCT_value(scts, i), source))
324             ++ret;
325     }
326
327     return ret;
328 }
329
330 CTLOG *SCT_get0_log(const SCT *sct)
331 {
332     return sct->log;
333 }
334
335 int SCT_set0_log(SCT *sct, const CTLOG_STORE *ct_logs)
336 {
337     sct->log = CTLOG_STORE_get0_log_by_id(ct_logs, sct->log_id, sct->log_id_len);
338
339     return sct->log != NULL;
340 }
341
342 int SCT_LIST_set0_logs(STACK_OF(SCT) *sct_list, const CTLOG_STORE *ct_logs)
343 {
344     int sct_logs_found = 0;
345     int i;
346
347     for (i = 0; i < sk_SCT_num(sct_list); ++i) {
348         SCT *sct = sk_SCT_value(sct_list, i);
349
350         if (sct->log == NULL)
351             SCT_set0_log(sct, ct_logs);
352         if (sct->log != NULL)
353             ++sct_logs_found;
354     }
355
356     return sct_logs_found;
357 }
358
359 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
360 {
361     return sct->validation_status;
362 }
363
364 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
365 {
366     int is_sct_valid = -1;
367     SCT_CTX *sctx = NULL;
368     X509_PUBKEY *pub = NULL, *log_pkey = NULL;
369
370     switch (sct->version) {
371     case SCT_VERSION_V1:
372         if (sct->log == NULL)
373             sct->log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
374                                                   sct->log_id,
375                                                   CT_V1_HASHLEN);
376         break;
377     default:
378         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
379         goto end;
380     }
381
382     if (sct->log == NULL) {
383         sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
384         goto end;
385     }
386
387     sctx = SCT_CTX_new();
388     if (sctx == NULL)
389         goto err;
390
391     if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(sct->log)) != 1)
392         goto err;
393     if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
394         goto err;
395
396     if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
397         EVP_PKEY *issuer_pkey;
398
399         if (ctx->issuer == NULL) {
400             sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
401             goto end;
402         }
403
404         issuer_pkey = X509_get0_pubkey(ctx->issuer);
405
406         if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
407             goto err;
408         if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
409             goto err;
410     }
411
412     if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
413         goto err;
414
415     sct->validation_status = SCT_verify(sctx, sct) == 1 ?
416             SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
417
418 end:
419     is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
420 err:
421     X509_PUBKEY_free(pub);
422     X509_PUBKEY_free(log_pkey);
423     SCT_CTX_free(sctx);
424
425     return is_sct_valid;
426 }
427
428 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
429 {
430     int are_scts_valid = 1;
431     int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
432     int i;
433
434     for (i = 0; i < sct_count; ++i) {
435         int is_sct_valid = -1;
436         SCT *sct = sk_SCT_value(scts, i);
437
438         if (sct == NULL)
439             continue;
440
441         is_sct_valid = SCT_validate(sct, ctx);
442         if (is_sct_valid < 0)
443             return is_sct_valid;
444         are_scts_valid &= is_sct_valid;
445     }
446
447     return are_scts_valid;
448 }