]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ct/ct_sct.c
Removes SCT_LIST_set_source and SCT_LIST_set0_logs
[thirdparty/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 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
105 return 1;
106 }
107
108 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
109 {
110 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
111
112 switch (entry_type) {
113 case CT_LOG_ENTRY_TYPE_X509:
114 case CT_LOG_ENTRY_TYPE_PRECERT:
115 sct->entry_type = entry_type;
116 return 1;
117 default:
118 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
119 return 0;
120 }
121 }
122
123 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
124 {
125 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
126 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
127 return 0;
128 }
129
130 OPENSSL_free(sct->log_id);
131 sct->log_id = log_id;
132 sct->log_id_len = log_id_len;
133 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
134 return 1;
135 }
136
137 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
138 {
139 if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
140 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
141 return 0;
142 }
143
144 OPENSSL_free(sct->log_id);
145 sct->log_id = NULL;
146 sct->log_id_len = 0;
147 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
148
149 if (log_id != NULL && log_id_len > 0) {
150 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
151 if (sct->log_id == NULL) {
152 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
153 return 0;
154 }
155 sct->log_id_len = log_id_len;
156 }
157 return 1;
158 }
159
160
161 void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
162 {
163 sct->timestamp = timestamp;
164 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
165 }
166
167 int SCT_set_signature_nid(SCT *sct, int nid)
168 {
169 switch (nid) {
170 case NID_sha256WithRSAEncryption:
171 sct->hash_alg = TLSEXT_hash_sha256;
172 sct->sig_alg = TLSEXT_signature_rsa;
173 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
174 return 1;
175 case NID_ecdsa_with_SHA256:
176 sct->hash_alg = TLSEXT_hash_sha256;
177 sct->sig_alg = TLSEXT_signature_ecdsa;
178 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
179 return 1;
180 default:
181 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
182 return 0;
183 }
184 }
185
186 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
187 {
188 OPENSSL_free(sct->ext);
189 sct->ext = ext;
190 sct->ext_len = ext_len;
191 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
192 }
193
194 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
195 {
196 OPENSSL_free(sct->ext);
197 sct->ext = NULL;
198 sct->ext_len = 0;
199 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
200
201 if (ext != NULL && ext_len > 0) {
202 sct->ext = OPENSSL_memdup(ext, ext_len);
203 if (sct->ext == NULL) {
204 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
205 return 0;
206 }
207 sct->ext_len = ext_len;
208 }
209 return 1;
210 }
211
212 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
213 {
214 OPENSSL_free(sct->sig);
215 sct->sig = sig;
216 sct->sig_len = sig_len;
217 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
218 }
219
220 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
221 {
222 OPENSSL_free(sct->sig);
223 sct->sig = NULL;
224 sct->sig_len = 0;
225 sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
226
227 if (sig != NULL && sig_len > 0) {
228 sct->sig = OPENSSL_memdup(sig, sig_len);
229 if (sct->sig == NULL) {
230 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
231 return 0;
232 }
233 sct->sig_len = sig_len;
234 }
235 return 1;
236 }
237
238 sct_version_t SCT_get_version(const SCT *sct)
239 {
240 return sct->version;
241 }
242
243 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
244 {
245 return sct->entry_type;
246 }
247
248 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
249 {
250 *log_id = sct->log_id;
251 return sct->log_id_len;
252 }
253
254 const char *SCT_get0_log_name(const SCT *sct)
255 {
256 return CTLOG_get0_name(sct->log);
257 }
258
259 uint64_t SCT_get_timestamp(const SCT *sct)
260 {
261 return sct->timestamp;
262 }
263
264 int SCT_get_signature_nid(const SCT *sct)
265 {
266 if (sct->version == SCT_VERSION_V1) {
267 if (sct->hash_alg == TLSEXT_hash_sha256) {
268 switch (sct->sig_alg) {
269 case TLSEXT_signature_ecdsa:
270 return NID_ecdsa_with_SHA256;
271 case TLSEXT_signature_rsa:
272 return NID_sha256WithRSAEncryption;
273 default:
274 return NID_undef;
275 }
276 }
277 }
278 return NID_undef;
279 }
280
281 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
282 {
283 *ext = sct->ext;
284 return sct->ext_len;
285 }
286
287 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
288 {
289 *sig = sct->sig;
290 return sct->sig_len;
291 }
292
293 int SCT_is_complete(const SCT *sct)
294 {
295 switch (sct->version) {
296 case SCT_VERSION_NOT_SET:
297 return 0;
298 case SCT_VERSION_V1:
299 return sct->log_id != NULL && SCT_signature_is_complete(sct);
300 default:
301 return sct->sct != NULL; /* Just need cached encoding */
302 }
303 }
304
305 int SCT_signature_is_complete(const SCT *sct)
306 {
307 return SCT_get_signature_nid(sct) != NID_undef &&
308 sct->sig != NULL && sct->sig_len > 0;
309 }
310
311 sct_source_t SCT_get_source(const SCT *sct)
312 {
313 return sct->source;
314 }
315
316 int SCT_set_source(SCT *sct, sct_source_t source)
317 {
318 sct->source = source;
319 switch (source) {
320 case SCT_SOURCE_TLS_EXTENSION:
321 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
322 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
323 case SCT_SOURCE_X509V3_EXTENSION:
324 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
325 default: /* if we aren't sure, leave the log entry type alone */
326 return 1;
327 }
328 }
329
330 const 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 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
343 {
344 return sct->validation_status;
345 }
346
347 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
348 {
349 int is_sct_valid = -1;
350 SCT_CTX *sctx = NULL;
351 X509_PUBKEY *pub = NULL, *log_pkey = NULL;
352
353 switch (sct->version) {
354 case SCT_VERSION_V1:
355 if (sct->log == NULL)
356 sct->log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
357 sct->log_id,
358 CT_V1_HASHLEN);
359 break;
360 default:
361 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
362 goto end;
363 }
364
365 if (sct->log == NULL) {
366 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
367 goto end;
368 }
369
370 sctx = SCT_CTX_new();
371 if (sctx == NULL)
372 goto err;
373
374 if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(sct->log)) != 1)
375 goto err;
376 if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
377 goto err;
378
379 if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
380 EVP_PKEY *issuer_pkey;
381
382 if (ctx->issuer == NULL) {
383 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
384 goto end;
385 }
386
387 issuer_pkey = X509_get0_pubkey(ctx->issuer);
388
389 if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
390 goto err;
391 if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
392 goto err;
393 }
394
395 if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
396 goto err;
397
398 sct->validation_status = SCT_verify(sctx, sct) == 1 ?
399 SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
400
401 end:
402 is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
403 err:
404 X509_PUBKEY_free(pub);
405 X509_PUBKEY_free(log_pkey);
406 SCT_CTX_free(sctx);
407
408 return is_sct_valid;
409 }
410
411 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
412 {
413 int are_scts_valid = 1;
414 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
415 int i;
416
417 for (i = 0; i < sct_count; ++i) {
418 int is_sct_valid = -1;
419 SCT *sct = sk_SCT_value(scts, i);
420
421 if (sct == NULL)
422 continue;
423
424 is_sct_valid = SCT_validate(sct, ctx);
425 if (is_sct_valid < 0)
426 return is_sct_valid;
427 are_scts_valid &= is_sct_valid;
428 }
429
430 return are_scts_valid;
431 }