xref: /openbsd/lib/libcrypto/ct/ct.h (revision 76d0caae)
1 /*	$OpenBSD: ct.h,v 1.5 2021/12/05 09:37:46 tb Exp $ */
2 /*
3  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10 
11 #ifndef HEADER_CT_H
12 #define HEADER_CT_H
13 
14 #include <openssl/opensslconf.h>
15 
16 #ifndef OPENSSL_NO_CT
17 #include <openssl/ossl_typ.h>
18 #include <openssl/safestack.h>
19 #include <openssl/x509.h>
20 #include <openssl/cterr.h>
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* Minimum RSA key size, from RFC6962 */
26 #define SCT_MIN_RSA_BITS 2048
27 
28 /* All hashes are SHA256 in v1 of Certificate Transparency */
29 #define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
30 
31 typedef enum {
32 	CT_LOG_ENTRY_TYPE_NOT_SET = -1,
33 	CT_LOG_ENTRY_TYPE_X509 = 0,
34 	CT_LOG_ENTRY_TYPE_PRECERT = 1
35 } ct_log_entry_type_t;
36 
37 typedef enum {
38 	SCT_VERSION_NOT_SET = -1,
39 	SCT_VERSION_V1 = 0
40 } sct_version_t;
41 
42 typedef enum {
43 	SCT_SOURCE_UNKNOWN,
44 	SCT_SOURCE_TLS_EXTENSION,
45 	SCT_SOURCE_X509V3_EXTENSION,
46 	SCT_SOURCE_OCSP_STAPLED_RESPONSE
47 } sct_source_t;
48 
49 typedef enum {
50 	SCT_VALIDATION_STATUS_NOT_SET,
51 	SCT_VALIDATION_STATUS_UNKNOWN_LOG,
52 	SCT_VALIDATION_STATUS_VALID,
53 	SCT_VALIDATION_STATUS_INVALID,
54 	SCT_VALIDATION_STATUS_UNVERIFIED,
55 	SCT_VALIDATION_STATUS_UNKNOWN_VERSION
56 } sct_validation_status_t;
57 
58 DECLARE_STACK_OF(SCT)
59 DECLARE_STACK_OF(CTLOG)
60 
61 /******************************************
62  * CT policy evaluation context functions *
63  ******************************************/
64 
65 /*
66  * Creates a new, empty policy evaluation context.
67  * The caller is responsible for calling CT_POLICY_EVAL_CTX_free when finished
68  * with the CT_POLICY_EVAL_CTX.
69  */
70 CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void);
71 
72 /* Deletes a policy evaluation context and anything it owns. */
73 void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx);
74 
75 /* Gets the peer certificate that the SCTs are for */
76 X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx);
77 
78 /*
79  * Sets the certificate associated with the received SCTs.
80  * Increments the reference count of cert.
81  * Returns 1 on success, 0 otherwise.
82  */
83 int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert);
84 
85 /* Gets the issuer of the aforementioned certificate */
86 X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx);
87 
88 /*
89  * Sets the issuer of the certificate associated with the received SCTs.
90  * Increments the reference count of issuer.
91  * Returns 1 on success, 0 otherwise.
92  */
93 int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer);
94 
95 /* Gets the CT logs that are trusted sources of SCTs */
96 const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx);
97 
98 /* Sets the log store that is in use. It must outlive the CT_POLICY_EVAL_CTX. */
99 void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
100                                                CTLOG_STORE *log_store);
101 
102 /*
103  * Gets the time, in milliseconds since the Unix epoch, that will be used as the
104  * current time when checking whether an SCT was issued in the future.
105  * Such SCTs will fail validation, as required by RFC6962.
106  */
107 uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx);
108 
109 /*
110  * Sets the time to evaluate SCTs against, in milliseconds since the Unix epoch.
111  * If an SCT's timestamp is after this time, it will be interpreted as having
112  * been issued in the future. RFC6962 states that "TLS clients MUST reject SCTs
113  * whose timestamp is in the future", so an SCT will not validate in this case.
114  */
115 void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms);
116 
117 /*****************
118  * SCT functions *
119  *****************/
120 
121 /*
122  * Creates a new, blank SCT.
123  * The caller is responsible for calling SCT_free when finished with the SCT.
124  */
125 SCT *SCT_new(void);
126 
127 /*
128  * Creates a new SCT from some base64-encoded strings.
129  * The caller is responsible for calling SCT_free when finished with the SCT.
130  */
131 SCT *SCT_new_from_base64(unsigned char version, const char *logid_base64,
132     ct_log_entry_type_t entry_type, uint64_t timestamp,
133     const char *extensions_base64, const char *signature_base64);
134 
135 /*
136  * Frees the SCT and the underlying data structures.
137  */
138 void SCT_free(SCT *sct);
139 
140 /*
141  * Free a stack of SCTs, and the underlying SCTs themselves.
142  * Intended to be compatible with X509V3_EXT_FREE.
143  */
144 void SCT_LIST_free(STACK_OF(SCT) *a);
145 
146 /*
147  * Returns the version of the SCT.
148  */
149 sct_version_t SCT_get_version(const SCT *sct);
150 
151 /*
152  * Set the version of an SCT.
153  * Returns 1 on success, 0 if the version is unrecognized.
154  */
155 int SCT_set_version(SCT *sct, sct_version_t version);
156 
157 /*
158  * Returns the log entry type of the SCT.
159  */
160 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
161 
162 /*
163  * Set the log entry type of an SCT.
164  * Returns 1 on success, 0 otherwise.
165  */
166 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
167 
168 /*
169  * Gets the ID of the log that an SCT came from.
170  * Ownership of the log ID remains with the SCT.
171  * Returns the length of the log ID.
172  */
173 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
174 
175 /*
176  * Set the log ID of an SCT to point directly to the *log_id specified.
177  * The SCT takes ownership of the specified pointer.
178  * Returns 1 on success, 0 otherwise.
179  */
180 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
181 
182 /*
183  * Set the log ID of an SCT.
184  * This makes a copy of the log_id.
185  * Returns 1 on success, 0 otherwise.
186  */
187 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id,
188                            size_t log_id_len);
189 
190 /*
191  * Returns the timestamp for the SCT (epoch time in milliseconds).
192  */
193 uint64_t SCT_get_timestamp(const SCT *sct);
194 
195 /*
196  * Set the timestamp of an SCT (epoch time in milliseconds).
197  */
198 void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
199 
200 /*
201  * Return the NID for the signature used by the SCT.
202  * For CT v1, this will be either NID_sha256WithRSAEncryption or
203  * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset).
204  */
205 int SCT_get_signature_nid(const SCT *sct);
206 
207 /*
208  * Set the signature type of an SCT
209  * For CT v1, this should be either NID_sha256WithRSAEncryption or
210  * NID_ecdsa_with_SHA256.
211  * Returns 1 on success, 0 otherwise.
212  */
213 int SCT_set_signature_nid(SCT *sct, int nid);
214 
215 /*
216  * Set *ext to point to the extension data for the SCT. ext must not be NULL.
217  * The SCT retains ownership of this pointer.
218  * Returns length of the data pointed to.
219  */
220 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
221 
222 /*
223  * Set the extensions of an SCT to point directly to the *ext specified.
224  * The SCT takes ownership of the specified pointer.
225  */
226 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
227 
228 /*
229  * Set the extensions of an SCT.
230  * This takes a copy of the ext.
231  * Returns 1 on success, 0 otherwise.
232  */
233 int SCT_set1_extensions(SCT *sct, const unsigned char *ext,
234                                size_t ext_len);
235 
236 /*
237  * Set *sig to point to the signature for the SCT. sig must not be NULL.
238  * The SCT retains ownership of this pointer.
239  * Returns length of the data pointed to.
240  */
241 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
242 
243 /*
244  * Set the signature of an SCT to point directly to the *sig specified.
245  * The SCT takes ownership of the specified pointer.
246  */
247 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
248 
249 /*
250  * Set the signature of an SCT to be a copy of the *sig specified.
251  * Returns 1 on success, 0 otherwise.
252  */
253 int SCT_set1_signature(SCT *sct, const unsigned char *sig,
254     size_t sig_len);
255 
256 /*
257  * The origin of this SCT, e.g. TLS extension, OCSP response, etc.
258  */
259 sct_source_t SCT_get_source(const SCT *sct);
260 
261 /*
262  * Set the origin of this SCT, e.g. TLS extension, OCSP response, etc.
263  * Returns 1 on success, 0 otherwise.
264  */
265 int SCT_set_source(SCT *sct, sct_source_t source);
266 
267 /*
268  * Returns a text string describing the validation status of |sct|.
269  */
270 const char *SCT_validation_status_string(const SCT *sct);
271 
272 /*
273  * Pretty-prints an |sct| to |out|.
274  * It will be indented by the number of spaces specified by |indent|.
275  * If |logs| is not NULL, it will be used to lookup the CT log that the SCT came
276  * from, so that the log name can be printed.
277  */
278 void SCT_print(const SCT *sct, BIO *out, int indent, const CTLOG_STORE *logs);
279 
280 /*
281  * Pretty-prints an |sct_list| to |out|.
282  * It will be indented by the number of spaces specified by |indent|.
283  * SCTs will be delimited by |separator|.
284  * If |logs| is not NULL, it will be used to lookup the CT log that each SCT
285  * came from, so that the log names can be printed.
286  */
287 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
288     const char *separator, const CTLOG_STORE *logs);
289 
290 /*
291  * Gets the last result of validating this SCT.
292  * If it has not been validated yet, returns SCT_VALIDATION_STATUS_NOT_SET.
293  */
294 sct_validation_status_t SCT_get_validation_status(const SCT *sct);
295 
296 /*
297  * Validates the given SCT with the provided context.
298  * Sets the "validation_status" field of the SCT.
299  * Returns 1 if the SCT is valid and the signature verifies.
300  * Returns 0 if the SCT is invalid or could not be verified.
301  * Returns -1 if an error occurs.
302  */
303 int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx);
304 
305 /*
306  * Validates the given list of SCTs with the provided context.
307  * Sets the "validation_status" field of each SCT.
308  * Returns 1 if there are no invalid SCTs and all signatures verify.
309  * Returns 0 if at least one SCT is invalid or could not be verified.
310  * Returns a negative integer if an error occurs.
311  */
312 int SCT_LIST_validate(const STACK_OF(SCT) *scts,
313     CT_POLICY_EVAL_CTX *ctx);
314 
315 
316 /*********************************
317  * SCT parsing and serialisation *
318  *********************************/
319 
320 /*
321  * Serialize (to TLS format) a stack of SCTs and return the length.
322  * "a" must not be NULL.
323  * If "pp" is NULL, just return the length of what would have been serialized.
324  * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
325  * for data that caller is responsible for freeing (only if function returns
326  * successfully).
327  * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
328  * that "*pp" is large enough to accept all of the serialized data.
329  * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
330  * on success.
331  */
332 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
333 
334 /*
335  * Convert TLS format SCT list to a stack of SCTs.
336  * If "a" or "*a" is NULL, a new stack will be created that the caller is
337  * responsible for freeing (by calling SCT_LIST_free).
338  * "**pp" and "*pp" must not be NULL.
339  * Upon success, "*pp" will point to after the last bytes read, and a stack
340  * will be returned.
341  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
342  * not defined.
343  */
344 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
345     size_t len);
346 
347 /*
348  * Serialize (to DER format) a stack of SCTs and return the length.
349  * "a" must not be NULL.
350  * If "pp" is NULL, just returns the length of what would have been serialized.
351  * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
352  * for data that caller is responsible for freeing (only if function returns
353  * successfully).
354  * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
355  * that "*pp" is large enough to accept all of the serialized data.
356  * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
357  * on success.
358  */
359 int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
360 
361 /*
362  * Parses an SCT list in DER format and returns it.
363  * If "a" or "*a" is NULL, a new stack will be created that the caller is
364  * responsible for freeing (by calling SCT_LIST_free).
365  * "**pp" and "*pp" must not be NULL.
366  * Upon success, "*pp" will point to after the last bytes read, and a stack
367  * will be returned.
368  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
369  * not defined.
370  */
371 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
372     long len);
373 
374 /*
375  * Serialize (to TLS format) an |sct| and write it to |out|.
376  * If |out| is null, no SCT will be output but the length will still be returned.
377  * If |out| points to a null pointer, a string will be allocated to hold the
378  * TLS-format SCT. It is the responsibility of the caller to free it.
379  * If |out| points to an allocated string, the TLS-format SCT will be written
380  * to it.
381  * The length of the SCT in TLS format will be returned.
382  */
383 int i2o_SCT(const SCT *sct, unsigned char **out);
384 
385 /*
386  * Parses an SCT in TLS format and returns it.
387  * If |psct| is not null, it will end up pointing to the parsed SCT. If it
388  * already points to a non-null pointer, the pointer will be free'd.
389  * |in| should be a pointer to a string containing the TLS-format SCT.
390  * |in| will be advanced to the end of the SCT if parsing succeeds.
391  * |len| should be the length of the SCT in |in|.
392  * Returns NULL if an error occurs.
393  * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len'
394  * fields will be populated (with |in| and |len| respectively).
395  */
396 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
397 
398 /********************
399  * CT log functions *
400  ********************/
401 
402 /*
403  * Creates a new CT log instance with the given |public_key| and |name|.
404  * Takes ownership of |public_key| but copies |name|.
405  * Returns NULL if malloc fails or if |public_key| cannot be converted to DER.
406  * Should be deleted by the caller using CTLOG_free when no longer needed.
407  */
408 CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name);
409 
410 /*
411  * Creates a new CTLOG instance with the base64-encoded SubjectPublicKeyInfo DER
412  * in |pkey_base64|. The |name| is a string to help users identify this log.
413  * Returns 1 on success, 0 on failure.
414  * Should be deleted by the caller using CTLOG_free when no longer needed.
415  */
416 int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64,
417     const char *name);
418 
419 /*
420  * Deletes a CT log instance and its fields.
421  */
422 void CTLOG_free(CTLOG *log);
423 
424 /* Gets the name of the CT log */
425 const char *CTLOG_get0_name(const CTLOG *log);
426 /* Gets the ID of the CT log */
427 void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
428                        size_t *log_id_len);
429 /* Gets the public key of the CT log */
430 EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log);
431 
432 /**************************
433  * CT log store functions *
434  **************************/
435 
436 /*
437  * Creates a new CT log store.
438  * Should be deleted by the caller using CTLOG_STORE_free when no longer needed.
439  */
440 CTLOG_STORE *CTLOG_STORE_new(void);
441 
442 /*
443  * Deletes a CT log store and all of the CT log instances held within.
444  */
445 void CTLOG_STORE_free(CTLOG_STORE *store);
446 
447 /*
448  * Finds a CT log in the store based on its log ID.
449  * Returns the CT log, or NULL if no match is found.
450  */
451 const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
452     const uint8_t *log_id, size_t log_id_len);
453 
454 /*
455  * Loads a CT log list into a |store| from a |file|.
456  * Returns 1 if loading is successful, or 0 otherwise.
457  */
458 int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file);
459 
460 /*
461  * Loads the default CT log list into a |store|.
462  * Returns 1 if loading is successful, or 0 otherwise.
463  */
464 int CTLOG_STORE_load_default_file(CTLOG_STORE *store);
465 
466 #ifdef __cplusplus
467 }
468 #endif
469 #endif
470 #endif
471