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