/src/openssl/crypto/ct/ct_sct.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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_local.h" |
21 | | |
22 | | SCT *SCT_new(void) |
23 | 0 | { |
24 | 0 | SCT *sct = OPENSSL_zalloc(sizeof(*sct)); |
25 | |
|
26 | 0 | if (sct == NULL) |
27 | 0 | return NULL; |
28 | | |
29 | 0 | sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET; |
30 | 0 | sct->version = SCT_VERSION_NOT_SET; |
31 | 0 | return sct; |
32 | 0 | } |
33 | | |
34 | | void SCT_free(SCT *sct) |
35 | 0 | { |
36 | 0 | if (sct == NULL) |
37 | 0 | return; |
38 | | |
39 | 0 | OPENSSL_free(sct->log_id); |
40 | 0 | OPENSSL_free(sct->ext); |
41 | 0 | OPENSSL_free(sct->sig); |
42 | 0 | OPENSSL_free(sct->sct); |
43 | 0 | OPENSSL_free(sct); |
44 | 0 | } |
45 | | |
46 | | void SCT_LIST_free(STACK_OF(SCT) *a) |
47 | 0 | { |
48 | 0 | sk_SCT_pop_free(a, SCT_free); |
49 | 0 | } |
50 | | |
51 | | int SCT_set_version(SCT *sct, sct_version_t version) |
52 | 0 | { |
53 | 0 | if (version != SCT_VERSION_V1) { |
54 | 0 | ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION); |
55 | 0 | return 0; |
56 | 0 | } |
57 | 0 | sct->version = version; |
58 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
59 | 0 | return 1; |
60 | 0 | } |
61 | | |
62 | | int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type) |
63 | 0 | { |
64 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
65 | |
|
66 | 0 | switch (entry_type) { |
67 | 0 | case CT_LOG_ENTRY_TYPE_X509: |
68 | 0 | case CT_LOG_ENTRY_TYPE_PRECERT: |
69 | 0 | sct->entry_type = entry_type; |
70 | 0 | return 1; |
71 | 0 | case CT_LOG_ENTRY_TYPE_NOT_SET: |
72 | 0 | break; |
73 | 0 | } |
74 | 0 | ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_ENTRY_TYPE); |
75 | 0 | return 0; |
76 | 0 | } |
77 | | |
78 | | int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len) |
79 | 0 | { |
80 | 0 | if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) { |
81 | 0 | ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH); |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | 0 | OPENSSL_free(sct->log_id); |
86 | 0 | sct->log_id = log_id; |
87 | 0 | sct->log_id_len = log_id_len; |
88 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
89 | 0 | return 1; |
90 | 0 | } |
91 | | |
92 | | int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len) |
93 | 0 | { |
94 | 0 | if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) { |
95 | 0 | ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH); |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 0 | OPENSSL_free(sct->log_id); |
100 | 0 | sct->log_id = NULL; |
101 | 0 | sct->log_id_len = 0; |
102 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
103 | |
|
104 | 0 | if (log_id != NULL && log_id_len > 0) { |
105 | 0 | sct->log_id = OPENSSL_memdup(log_id, log_id_len); |
106 | 0 | if (sct->log_id == NULL) |
107 | 0 | return 0; |
108 | 0 | sct->log_id_len = log_id_len; |
109 | 0 | } |
110 | 0 | return 1; |
111 | 0 | } |
112 | | |
113 | | |
114 | | void SCT_set_timestamp(SCT *sct, uint64_t timestamp) |
115 | 0 | { |
116 | 0 | sct->timestamp = timestamp; |
117 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
118 | 0 | } |
119 | | |
120 | | int SCT_set_signature_nid(SCT *sct, int nid) |
121 | 0 | { |
122 | 0 | switch (nid) { |
123 | 0 | case NID_sha256WithRSAEncryption: |
124 | 0 | sct->hash_alg = TLSEXT_hash_sha256; |
125 | 0 | sct->sig_alg = TLSEXT_signature_rsa; |
126 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
127 | 0 | return 1; |
128 | 0 | case NID_ecdsa_with_SHA256: |
129 | 0 | sct->hash_alg = TLSEXT_hash_sha256; |
130 | 0 | sct->sig_alg = TLSEXT_signature_ecdsa; |
131 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
132 | 0 | return 1; |
133 | 0 | default: |
134 | 0 | ERR_raise(ERR_LIB_CT, CT_R_UNRECOGNIZED_SIGNATURE_NID); |
135 | 0 | return 0; |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | | void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len) |
140 | 0 | { |
141 | 0 | OPENSSL_free(sct->ext); |
142 | 0 | sct->ext = ext; |
143 | 0 | sct->ext_len = ext_len; |
144 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
145 | 0 | } |
146 | | |
147 | | int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len) |
148 | 0 | { |
149 | 0 | OPENSSL_free(sct->ext); |
150 | 0 | sct->ext = NULL; |
151 | 0 | sct->ext_len = 0; |
152 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
153 | |
|
154 | 0 | if (ext != NULL && ext_len > 0) { |
155 | 0 | sct->ext = OPENSSL_memdup(ext, ext_len); |
156 | 0 | if (sct->ext == NULL) |
157 | 0 | return 0; |
158 | 0 | sct->ext_len = ext_len; |
159 | 0 | } |
160 | 0 | return 1; |
161 | 0 | } |
162 | | |
163 | | void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len) |
164 | 0 | { |
165 | 0 | OPENSSL_free(sct->sig); |
166 | 0 | sct->sig = sig; |
167 | 0 | sct->sig_len = sig_len; |
168 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
169 | 0 | } |
170 | | |
171 | | int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len) |
172 | 0 | { |
173 | 0 | OPENSSL_free(sct->sig); |
174 | 0 | sct->sig = NULL; |
175 | 0 | sct->sig_len = 0; |
176 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
177 | |
|
178 | 0 | if (sig != NULL && sig_len > 0) { |
179 | 0 | sct->sig = OPENSSL_memdup(sig, sig_len); |
180 | 0 | if (sct->sig == NULL) |
181 | 0 | return 0; |
182 | 0 | sct->sig_len = sig_len; |
183 | 0 | } |
184 | 0 | return 1; |
185 | 0 | } |
186 | | |
187 | | sct_version_t SCT_get_version(const SCT *sct) |
188 | 0 | { |
189 | 0 | return sct->version; |
190 | 0 | } |
191 | | |
192 | | ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct) |
193 | 0 | { |
194 | 0 | return sct->entry_type; |
195 | 0 | } |
196 | | |
197 | | size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id) |
198 | 0 | { |
199 | 0 | *log_id = sct->log_id; |
200 | 0 | return sct->log_id_len; |
201 | 0 | } |
202 | | |
203 | | uint64_t SCT_get_timestamp(const SCT *sct) |
204 | 0 | { |
205 | 0 | return sct->timestamp; |
206 | 0 | } |
207 | | |
208 | | int SCT_get_signature_nid(const SCT *sct) |
209 | 0 | { |
210 | 0 | if (sct->version == SCT_VERSION_V1) { |
211 | 0 | if (sct->hash_alg == TLSEXT_hash_sha256) { |
212 | 0 | switch (sct->sig_alg) { |
213 | 0 | case TLSEXT_signature_ecdsa: |
214 | 0 | return NID_ecdsa_with_SHA256; |
215 | 0 | case TLSEXT_signature_rsa: |
216 | 0 | return NID_sha256WithRSAEncryption; |
217 | 0 | default: |
218 | 0 | return NID_undef; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | } |
222 | 0 | return NID_undef; |
223 | 0 | } |
224 | | |
225 | | size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext) |
226 | 0 | { |
227 | 0 | *ext = sct->ext; |
228 | 0 | return sct->ext_len; |
229 | 0 | } |
230 | | |
231 | | size_t SCT_get0_signature(const SCT *sct, unsigned char **sig) |
232 | 0 | { |
233 | 0 | *sig = sct->sig; |
234 | 0 | return sct->sig_len; |
235 | 0 | } |
236 | | |
237 | | int SCT_is_complete(const SCT *sct) |
238 | 0 | { |
239 | 0 | switch (sct->version) { |
240 | 0 | case SCT_VERSION_NOT_SET: |
241 | 0 | return 0; |
242 | 0 | case SCT_VERSION_V1: |
243 | 0 | return sct->log_id != NULL && SCT_signature_is_complete(sct); |
244 | 0 | default: |
245 | 0 | return sct->sct != NULL; /* Just need cached encoding */ |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | | int SCT_signature_is_complete(const SCT *sct) |
250 | 0 | { |
251 | 0 | return SCT_get_signature_nid(sct) != NID_undef && |
252 | 0 | sct->sig != NULL && sct->sig_len > 0; |
253 | 0 | } |
254 | | |
255 | | sct_source_t SCT_get_source(const SCT *sct) |
256 | 0 | { |
257 | 0 | return sct->source; |
258 | 0 | } |
259 | | |
260 | | int SCT_set_source(SCT *sct, sct_source_t source) |
261 | 0 | { |
262 | 0 | sct->source = source; |
263 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET; |
264 | 0 | switch (source) { |
265 | 0 | case SCT_SOURCE_TLS_EXTENSION: |
266 | 0 | case SCT_SOURCE_OCSP_STAPLED_RESPONSE: |
267 | 0 | return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509); |
268 | 0 | case SCT_SOURCE_X509V3_EXTENSION: |
269 | 0 | return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT); |
270 | 0 | case SCT_SOURCE_UNKNOWN: |
271 | 0 | break; |
272 | 0 | } |
273 | | /* if we aren't sure, leave the log entry type alone */ |
274 | 0 | return 1; |
275 | 0 | } |
276 | | |
277 | | sct_validation_status_t SCT_get_validation_status(const SCT *sct) |
278 | 0 | { |
279 | 0 | return sct->validation_status; |
280 | 0 | } |
281 | | |
282 | | int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx) |
283 | 0 | { |
284 | 0 | int is_sct_valid = -1; |
285 | 0 | SCT_CTX *sctx = NULL; |
286 | 0 | X509_PUBKEY *pub = NULL, *log_pkey = NULL; |
287 | 0 | const CTLOG *log; |
288 | | |
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 | | */ |
293 | 0 | if (sct->version != SCT_VERSION_V1) { |
294 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION; |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | 0 | log = CTLOG_STORE_get0_log_by_id(ctx->log_store, |
299 | 0 | sct->log_id, sct->log_id_len); |
300 | | |
301 | | /* Similarly, an SCT from an unknown log also cannot be validated. */ |
302 | 0 | if (log == NULL) { |
303 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG; |
304 | 0 | return 0; |
305 | 0 | } |
306 | | |
307 | 0 | sctx = SCT_CTX_new(ctx->libctx, ctx->propq); |
308 | 0 | if (sctx == NULL) |
309 | 0 | goto err; |
310 | | |
311 | 0 | if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1) |
312 | 0 | goto err; |
313 | 0 | if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1) |
314 | 0 | goto err; |
315 | | |
316 | 0 | if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) { |
317 | 0 | EVP_PKEY *issuer_pkey; |
318 | |
|
319 | 0 | if (ctx->issuer == NULL) { |
320 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED; |
321 | 0 | goto end; |
322 | 0 | } |
323 | | |
324 | 0 | issuer_pkey = X509_get0_pubkey(ctx->issuer); |
325 | |
|
326 | 0 | if (X509_PUBKEY_set(&pub, issuer_pkey) != 1) |
327 | 0 | goto err; |
328 | 0 | if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1) |
329 | 0 | goto err; |
330 | 0 | } |
331 | | |
332 | 0 | SCT_CTX_set_time(sctx, ctx->epoch_time_in_ms); |
333 | | |
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 |
338 | | * processed. So it may make more sense to do this just once, perhaps |
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 | | */ |
352 | 0 | if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1) |
353 | 0 | sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED; |
354 | 0 | else |
355 | 0 | sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ? |
356 | 0 | SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID; |
357 | |
|
358 | 0 | end: |
359 | 0 | is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID; |
360 | 0 | err: |
361 | 0 | X509_PUBKEY_free(pub); |
362 | 0 | X509_PUBKEY_free(log_pkey); |
363 | 0 | SCT_CTX_free(sctx); |
364 | |
|
365 | 0 | return is_sct_valid; |
366 | 0 | } |
367 | | |
368 | | int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx) |
369 | 0 | { |
370 | 0 | int are_scts_valid = 1; |
371 | 0 | int sct_count = scts != NULL ? sk_SCT_num(scts) : 0; |
372 | 0 | int i; |
373 | |
|
374 | 0 | for (i = 0; i < sct_count; ++i) { |
375 | 0 | int is_sct_valid = -1; |
376 | 0 | SCT *sct = sk_SCT_value(scts, i); |
377 | |
|
378 | 0 | if (sct == NULL) |
379 | 0 | continue; |
380 | | |
381 | 0 | is_sct_valid = SCT_validate(sct, ctx); |
382 | 0 | if (is_sct_valid < 0) |
383 | 0 | return is_sct_valid; |
384 | 0 | are_scts_valid &= is_sct_valid; |
385 | 0 | } |
386 | | |
387 | 0 | return are_scts_valid; |
388 | 0 | } |