2 * Public API for Certificate Transparency (CT).
3 * Written by Rob Percival (robpercival@google.com) for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
55 # error "CT is disabled"
61 # include <openssl/safestack.h>
62 # include <openssl/x509.h>
68 /* Minimum RSA key size, from RFC6962 */
69 # define SCT_MIN_RSA_BITS 2048
71 /* All hashes are SHA256 in v1 of Certificate Transparency */
72 # define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
75 CT_LOG_ENTRY_TYPE_NOT_SET = -1,
76 CT_LOG_ENTRY_TYPE_X509 = 0,
77 CT_LOG_ENTRY_TYPE_PRECERT = 1
78 } ct_log_entry_type_t;
81 SCT_VERSION_NOT_SET = -1,
89 /* Signed Certificate Timestamp (SCT) */
90 typedef struct sct_st SCT;
98 * Creates a new, blank SCT.
99 * The caller is responsible for calling SCT_free when finished with the SCT.
104 * Frees the SCT and the underlying data structures.
106 void SCT_free(SCT *sct);
109 * Free a stack of SCTs, and the underlying SCTs themselves.
110 * Intended to be compatible with X509V3_EXT_FREE.
112 void SCT_LIST_free(STACK_OF(SCT) *a);
115 * Returns the version of the SCT.
117 sct_version_t SCT_get_version(const SCT *sct);
120 * Set the version of an SCT.
121 * Returns 1 on success, 0 if the version is unrecognized.
123 int SCT_set_version(SCT *sct, sct_version_t version);
126 * Returns the log entry type of the SCT.
128 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
131 * Set the log entry type of an SCT.
132 * Returns 1 on success.
134 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
137 * Gets the ID of the log that an SCT came from.
138 * Ownership of the log ID remains with the SCT.
139 * Returns the length of the log ID.
141 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
144 * Set the log ID of an SCT to point directly to the *log_id specified.
145 * The SCT takes ownership of the specified pointer.
146 * Returns 1 on success.
148 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
151 * Set the log ID of an SCT.
152 * This makes a copy of the log_id.
153 * Returns 1 on success.
155 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len);
158 * Returns the timestamp for the SCT (epoch time in milliseconds).
160 uint64_t SCT_get_timestamp(const SCT *sct);
163 * Set the timestamp of an SCT (epoch time in milliseconds).
165 void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
168 * Return the NID for the signature used by the SCT.
169 * For CT v1, this will be either NID_sha256WithRSAEncryption or
170 * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset).
172 int SCT_get_signature_nid(const SCT *sct);
175 * Set the signature type of an SCT
176 * For CT v1, this should be either NID_sha256WithRSAEncryption or
177 * NID_ecdsa_with_SHA256.
178 * Returns 1 on success.
180 int SCT_set_signature_nid(SCT *sct, int nid);
183 * Set *ext to point to the extension data for the SCT. ext must not be NULL.
184 * The SCT retains ownership of this pointer.
185 * Returns length of the data pointed to.
187 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
190 * Set the extensions of an SCT to point directly to the *ext specified.
191 * The SCT takes ownership of the specified pointer.
193 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
196 * Set the extensions of an SCT.
197 * This takes a copy of the ext.
198 * Returns 1 on success.
200 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len);
203 * Set *sig to point to the signature for the SCT. sig must not be NULL.
204 * The SCT retains ownership of this pointer.
205 * Returns length of the data pointed to.
207 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
210 * Set the signature of an SCT to point directly to the *sig specified.
211 * The SCT takes ownership of the specified pointer.
213 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
216 * Set the signature of an SCT to be a copy of the *sig specified.
217 * Returns 1 on success.
219 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len);
222 * Pretty-prints an |sct| to |out|.
223 * It will be indented by the number of spaces specified by |indent|.
225 void SCT_print(const SCT *sct, BIO *out, int indent);
228 * Pretty-prints an |sct_list| to |out|.
229 * It will be indented by the number of spaces specified by |indent|.
230 * SCTs will be delimited by |separator|.
232 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
233 const char *separator);
235 /*********************************
236 * SCT parsing and serialisation *
237 *********************************/
240 * Serialize (to TLS format) a stack of SCTs and return the length.
241 * "a" must not be NULL.
242 * If "pp" is NULL, just return the length of what would have been serialized.
243 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
244 * for data that caller is responsible for freeing (only if function returns
246 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
247 * that "*pp" is large enough to accept all of the serializied data.
248 * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
251 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
254 * Convert TLS format SCT list to a stack of SCTs.
255 * If "a" or "*a" is NULL, a new stack will be created that the caller is
256 * responsible for freeing (by calling SCT_LIST_free).
257 * "**pp" and "*pp" must not be NULL.
258 * Upon success, "*pp" will point to after the last bytes read, and a stack
260 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
263 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
267 * Serialize (to DER format) a stack of SCTs and return the length.
268 * "a" must not be NULL.
269 * If "pp" is NULL, just returns the length of what would have been serialized.
270 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
271 * for data that caller is responsible for freeing (only if function returns
273 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
274 * that "*pp" is large enough to accept all of the serializied data.
275 * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
278 int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp);
281 * Parses an SCT list in DER format and returns it.
282 * If "a" or "*a" is NULL, a new stack will be created that the caller is
283 * responsible for freeing (by calling SCT_LIST_free).
284 * "**pp" and "*pp" must not be NULL.
285 * Upon success, "*pp" will point to after the last bytes read, and a stack
287 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
290 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
294 * Serialize (to TLS format) an |sct| and write it to |out|.
295 * If |out| is null, no SCT will be output but the length will still be returned.
296 * If |out| points to a null pointer, a string will be allocated to hold the
297 * TLS-format SCT. It is the responsibility of the caller to free it.
298 * If |out| points to an allocated string, the TLS-format SCT will be written
300 * The length of the SCT in TLS format will be returned.
302 int i2o_SCT(const SCT *sct, unsigned char **out);
305 * Parses an SCT in TLS format and returns it.
306 * If |psct| is not null, it will end up pointing to the parsed SCT. If it
307 * already points to a non-null pointer, the pointer will be free'd.
308 * |in| should be a pointer to a string contianing the TLS-format SCT.
309 * |in| will be advanced to the end of the SCT if parsing succeeds.
310 * |len| should be the length of the SCT in |in|.
311 * Returns NULL if an error occurs.
312 * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len'
313 * fields will be populated (with |in| and |len| respectively).
315 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
318 * Serialize (to TLS format) an |sct| signature and write it to |out|.
319 * If |out| is null, no signature will be output but the length will be returned.
320 * If |out| points to a null pointer, a string will be allocated to hold the
321 * TLS-format signature. It is the responsibility of the caller to free it.
322 * If |out| points to an allocated string, the signature will be written to it.
323 * The length of the signature in TLS format will be returned.
325 int i2o_SCT_signature(const SCT *sct, unsigned char **out);
328 * Parses an SCT signature in TLS format and populates the |sct| with it.
329 * |in| should be a pointer to a string contianing the TLS-format signature.
330 * |in| will be advanced to the end of the signature if parsing succeeds.
331 * |len| should be the length of the signature in |in|.
332 * Returns the number of bytes parsed, or a negative integer if an error occurs.
334 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len);
336 /* BEGIN ERROR CODES */
338 * The following lines are auto generated by the script mkerr.pl. Any changes
339 * made after this point may be overwritten when the script is next run.
341 void ERR_load_CT_strings(void);
343 /* Error codes for the CT functions. */
345 /* Function codes. */
346 # define CT_F_D2I_SCT_LIST 105
347 # define CT_F_I2D_SCT_LIST 106
348 # define CT_F_I2O_SCT 107
349 # define CT_F_I2O_SCT_LIST 108
350 # define CT_F_I2O_SCT_SIGNATURE 109
351 # define CT_F_O2I_SCT 110
352 # define CT_F_O2I_SCT_LIST 111
353 # define CT_F_O2I_SCT_SIGNATURE 112
354 # define CT_F_SCT_NEW 100
355 # define CT_F_SCT_SET0_LOG_ID 101
356 # define CT_F_SCT_SET1_EXTENSIONS 114
357 # define CT_F_SCT_SET1_LOG_ID 115
358 # define CT_F_SCT_SET1_SIGNATURE 116
359 # define CT_F_SCT_SET_LOG_ENTRY_TYPE 102
360 # define CT_F_SCT_SET_SIGNATURE_NID 103
361 # define CT_F_SCT_SET_VERSION 104
362 # define CT_F_SCT_SIGNATURE_IS_VALID 113
365 # define CT_R_INVALID_LOG_ID_LENGTH 100
366 # define CT_R_SCT_INVALID 104
367 # define CT_R_SCT_INVALID_SIGNATURE 107
368 # define CT_R_SCT_LIST_INVALID 105
369 # define CT_R_SCT_NOT_SET 106
370 # define CT_R_UNRECOGNIZED_SIGNATURE_NID 101
371 # define CT_R_UNSUPPORTED_ENTRY_TYPE 102
372 # define CT_R_UNSUPPORTED_VERSION 103