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