]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ct/ct_sct.c
Suppress CT callback as appropriate
[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 uint64_t SCT_get_timestamp(const SCT *sct)
255 {
256 return sct->timestamp;
257 }
258
259 int SCT_get_signature_nid(const SCT *sct)
260 {
261 if (sct->version == SCT_VERSION_V1) {
262 if (sct->hash_alg == TLSEXT_hash_sha256) {
263 switch (sct->sig_alg) {
264 case TLSEXT_signature_ecdsa:
265 return NID_ecdsa_with_SHA256;
266 case TLSEXT_signature_rsa:
267 return NID_sha256WithRSAEncryption;
268 default:
269 return NID_undef;
270 }
271 }
272 }
273 return NID_undef;
274 }
275
276 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
277 {
278 *ext = sct->ext;
279 return sct->ext_len;
280 }
281
282 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
283 {
284 *sig = sct->sig;
285 return sct->sig_len;
286 }
287
288 int SCT_is_complete(const SCT *sct)
289 {
290 switch (sct->version) {
291 case SCT_VERSION_NOT_SET:
292 return 0;
293 case SCT_VERSION_V1:
294 return sct->log_id != NULL && SCT_signature_is_complete(sct);
295 default:
296 return sct->sct != NULL; /* Just need cached encoding */
297 }
298 }
299
300 int SCT_signature_is_complete(const SCT *sct)
301 {
302 return SCT_get_signature_nid(sct) != NID_undef &&
303 sct->sig != NULL && sct->sig_len > 0;
304 }
305
306 sct_source_t SCT_get_source(const SCT *sct)
307 {
308 return sct->source;
309 }
310
311 int SCT_set_source(SCT *sct, sct_source_t source)
312 {
313 sct->source = source;
314 switch (source) {
315 case SCT_SOURCE_TLS_EXTENSION:
316 case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
317 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
318 case SCT_SOURCE_X509V3_EXTENSION:
319 return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
320 default: /* if we aren't sure, leave the log entry type alone */
321 return 1;
322 }
323 }
324
325 sct_validation_status_t SCT_get_validation_status(const SCT *sct)
326 {
327 return sct->validation_status;
328 }
329
330 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
331 {
332 int is_sct_valid = -1;
333 SCT_CTX *sctx = NULL;
334 X509_PUBKEY *pub = NULL, *log_pkey = NULL;
335 const CTLOG *log;
336
337 /*
338 * With an unrecognized SCT version we don't know what such an SCT means,
339 * let alone validate one. So we return validation failure (0).
340 */
341 if (sct->version != SCT_VERSION_V1) {
342 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
343 return 0;
344 }
345
346 log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
347 sct->log_id, sct->log_id_len);
348
349 /* Similarly, an SCT from an unknown log also cannot be validated. */
350 if (log == NULL) {
351 sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
352 return 0;
353 }
354
355 sctx = SCT_CTX_new();
356 if (sctx == NULL)
357 goto err;
358
359 if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
360 goto err;
361 if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
362 goto err;
363
364 if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
365 EVP_PKEY *issuer_pkey;
366
367 if (ctx->issuer == NULL) {
368 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
369 goto end;
370 }
371
372 issuer_pkey = X509_get0_pubkey(ctx->issuer);
373
374 if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
375 goto err;
376 if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
377 goto err;
378 }
379
380 /*
381 * XXX: Potential for optimization. This repeats some idempotent heavy
382 * lifting on the certificate for each candidate SCT, and appears to not
383 * use any information in the SCT itself, only the certificate is
384 * processed. So it may make more sense to to do this just once, perhaps
385 * associated with the shared (by all SCTs) policy eval ctx.
386 *
387 * XXX: Failure here is global (SCT independent) and represents either an
388 * issue with the certificate (e.g. duplicate extensions) or an out of
389 * memory condition. When the certificate is incompatible with CT, we just
390 * mark the SCTs invalid, rather than report a failure to determine the
391 * validation status. That way, callbacks that want to do "soft" SCT
392 * processing will not abort handshakes with false positive internal
393 * errors. Since the function does not distinguish between certificate
394 * issues (peer's fault) and internal problems (out fault) the safe thing
395 * to do is to report a validation failure and let the callback or
396 * application decide what to do.
397 */
398 if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
399 sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
400 else
401 sct->validation_status = SCT_verify(sctx, sct) == 1 ?
402 SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
403
404 end:
405 is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
406 err:
407 X509_PUBKEY_free(pub);
408 X509_PUBKEY_free(log_pkey);
409 SCT_CTX_free(sctx);
410
411 return is_sct_valid;
412 }
413
414 int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
415 {
416 int are_scts_valid = 1;
417 int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
418 int i;
419
420 for (i = 0; i < sct_count; ++i) {
421 int is_sct_valid = -1;
422 SCT *sct = sk_SCT_value(scts, i);
423
424 if (sct == NULL)
425 continue;
426
427 is_sct_valid = SCT_validate(sct, ctx);
428 if (is_sct_valid < 0)
429 return is_sct_valid;
430 are_scts_valid &= is_sct_valid;
431 }
432
433 return are_scts_valid;
434 }