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/ossl_typ.h>
62 # include <openssl/safestack.h>
63 # include <openssl/x509.h>
69 /* Minimum RSA key size, from RFC6962 */
70 # define SCT_MIN_RSA_BITS 2048
72 /* All hashes are SHA256 in v1 of Certificate Transparency */
73 # define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
76 CT_LOG_ENTRY_TYPE_NOT_SET = -1,
77 CT_LOG_ENTRY_TYPE_X509 = 0,
78 CT_LOG_ENTRY_TYPE_PRECERT = 1
79 } ct_log_entry_type_t;
82 SCT_VERSION_NOT_SET = -1,
93 * Creates a new, blank SCT.
94 * The caller is responsible for calling SCT_free when finished with the SCT.
99 * Frees the SCT and the underlying data structures.
101 void SCT_free(SCT *sct);
104 * Free a stack of SCTs, and the underlying SCTs themselves.
105 * Intended to be compatible with X509V3_EXT_FREE.
107 void SCT_LIST_free(STACK_OF(SCT) *a);
110 * Returns the version of the SCT.
112 sct_version_t SCT_get_version(const SCT *sct);
115 * Set the version of an SCT.
116 * Returns 1 on success, 0 if the version is unrecognized.
118 int SCT_set_version(SCT *sct, sct_version_t version);
121 * Returns the log entry type of the SCT.
123 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
126 * Set the log entry type of an SCT.
127 * Returns 1 on success.
129 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
132 * Gets the ID of the log that an SCT came from.
133 * Ownership of the log ID remains with the SCT.
134 * Returns the length of the log ID.
136 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
139 * Set the log ID of an SCT to point directly to the *log_id specified.
140 * The SCT takes ownership of the specified pointer.
141 * Returns 1 on success.
143 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
146 * Set the log ID of an SCT.
147 * This makes a copy of the log_id.
148 * Returns 1 on success.
150 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len);
153 * Returns the timestamp for the SCT (epoch time in milliseconds).
155 uint64_t SCT_get_timestamp(const SCT *sct);
158 * Set the timestamp of an SCT (epoch time in milliseconds).
160 void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
163 * Return the NID for the signature used by the SCT.
164 * For CT v1, this will be either NID_sha256WithRSAEncryption or
165 * NID_ecdsa_with_SHA256 (or NID_undef if incorrect/unset).
167 int SCT_get_signature_nid(const SCT *sct);
170 * Set the signature type of an SCT
171 * For CT v1, this should be either NID_sha256WithRSAEncryption or
172 * NID_ecdsa_with_SHA256.
173 * Returns 1 on success.
175 int SCT_set_signature_nid(SCT *sct, int nid);
178 * Set *ext to point to the extension data for the SCT. ext must not be NULL.
179 * The SCT retains ownership of this pointer.
180 * Returns length of the data pointed to.
182 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
185 * Set the extensions of an SCT to point directly to the *ext specified.
186 * The SCT takes ownership of the specified pointer.
188 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
191 * Set the extensions of an SCT.
192 * This takes a copy of the ext.
193 * Returns 1 on success.
195 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len);
198 * Set *sig to point to the signature for the SCT. sig must not be NULL.
199 * The SCT retains ownership of this pointer.
200 * Returns length of the data pointed to.
202 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
205 * Set the signature of an SCT to point directly to the *sig specified.
206 * The SCT takes ownership of the specified pointer.
208 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
211 * Set the signature of an SCT to be a copy of the *sig specified.
212 * Returns 1 on success.
214 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len);
217 * Pretty-prints an |sct| to |out|.
218 * It will be indented by the number of spaces specified by |indent|.
220 void SCT_print(const SCT *sct, BIO *out, int indent);
223 * Pretty-prints an |sct_list| to |out|.
224 * It will be indented by the number of spaces specified by |indent|.
225 * SCTs will be delimited by |separator|.
227 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
228 const char *separator);
230 /*********************************
231 * SCT parsing and serialisation *
232 *********************************/
235 * Serialize (to TLS format) a stack of SCTs and return the length.
236 * "a" must not be NULL.
237 * If "pp" is NULL, just return the length of what would have been serialized.
238 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
239 * for data that caller is responsible for freeing (only if function returns
241 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
242 * that "*pp" is large enough to accept all of the serializied data.
243 * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
246 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
249 * Convert TLS format SCT list to a stack of SCTs.
250 * If "a" or "*a" is NULL, a new stack will be created that the caller is
251 * responsible for freeing (by calling SCT_LIST_free).
252 * "**pp" and "*pp" must not be NULL.
253 * Upon success, "*pp" will point to after the last bytes read, and a stack
255 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
258 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
262 * Serialize (to DER format) a stack of SCTs and return the length.
263 * "a" must not be NULL.
264 * If "pp" is NULL, just returns the length of what would have been serialized.
265 * If "pp" is not NULL and "*pp" is null, function will allocate a new pointer
266 * for data that caller is responsible for freeing (only if function returns
268 * If "pp" is NULL and "*pp" is not NULL, caller is responsible for ensuring
269 * that "*pp" is large enough to accept all of the serializied data.
270 * Returns < 0 on error, >= 0 indicating bytes written (or would have been)
273 int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp);
276 * Parses an SCT list in DER format and returns it.
277 * If "a" or "*a" is NULL, a new stack will be created that the caller is
278 * responsible for freeing (by calling SCT_LIST_free).
279 * "**pp" and "*pp" must not be NULL.
280 * Upon success, "*pp" will point to after the last bytes read, and a stack
282 * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
285 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
289 * Serialize (to TLS format) an |sct| and write it to |out|.
290 * If |out| is null, no SCT will be output but the length will still be returned.
291 * If |out| points to a null pointer, a string will be allocated to hold the
292 * TLS-format SCT. It is the responsibility of the caller to free it.
293 * If |out| points to an allocated string, the TLS-format SCT will be written
295 * The length of the SCT in TLS format will be returned.
297 int i2o_SCT(const SCT *sct, unsigned char **out);
300 * Parses an SCT in TLS format and returns it.
301 * If |psct| is not null, it will end up pointing to the parsed SCT. If it
302 * already points to a non-null pointer, the pointer will be free'd.
303 * |in| should be a pointer to a string contianing the TLS-format SCT.
304 * |in| will be advanced to the end of the SCT if parsing succeeds.
305 * |len| should be the length of the SCT in |in|.
306 * Returns NULL if an error occurs.
307 * If the SCT is an unsupported version, only the SCT's 'sct' and 'sct_len'
308 * fields will be populated (with |in| and |len| respectively).
310 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
313 * Serialize (to TLS format) an |sct| signature and write it to |out|.
314 * If |out| is null, no signature will be output but the length will be returned.
315 * If |out| points to a null pointer, a string will be allocated to hold the
316 * TLS-format signature. It is the responsibility of the caller to free it.
317 * If |out| points to an allocated string, the signature will be written to it.
318 * The length of the signature in TLS format will be returned.
320 int i2o_SCT_signature(const SCT *sct, unsigned char **out);
323 * Parses an SCT signature in TLS format and populates the |sct| with it.
324 * |in| should be a pointer to a string contianing the TLS-format signature.
325 * |in| will be advanced to the end of the signature if parsing succeeds.
326 * |len| should be the length of the signature in |in|.
327 * Returns the number of bytes parsed, or a negative integer if an error occurs.
329 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len);
331 /* BEGIN ERROR CODES */
333 * The following lines are auto generated by the script mkerr.pl. Any changes
334 * made after this point may be overwritten when the script is next run.
336 void ERR_load_CT_strings(void);
338 /* Error codes for the CT functions. */
340 /* Function codes. */
341 # define CT_F_D2I_SCT_LIST 105
342 # define CT_F_I2D_SCT_LIST 106
343 # define CT_F_I2O_SCT 107
344 # define CT_F_I2O_SCT_LIST 108
345 # define CT_F_I2O_SCT_SIGNATURE 109
346 # define CT_F_O2I_SCT 110
347 # define CT_F_O2I_SCT_LIST 111
348 # define CT_F_O2I_SCT_SIGNATURE 112
349 # define CT_F_SCT_NEW 100
350 # define CT_F_SCT_SET0_LOG_ID 101
351 # define CT_F_SCT_SET1_EXTENSIONS 114
352 # define CT_F_SCT_SET1_LOG_ID 115
353 # define CT_F_SCT_SET1_SIGNATURE 116
354 # define CT_F_SCT_SET_LOG_ENTRY_TYPE 102
355 # define CT_F_SCT_SET_SIGNATURE_NID 103
356 # define CT_F_SCT_SET_VERSION 104
357 # define CT_F_SCT_SIGNATURE_IS_VALID 113
360 # define CT_R_INVALID_LOG_ID_LENGTH 100
361 # define CT_R_SCT_INVALID 104
362 # define CT_R_SCT_INVALID_SIGNATURE 107
363 # define CT_R_SCT_LIST_INVALID 105
364 # define CT_R_SCT_NOT_SET 106
365 # define CT_R_UNRECOGNIZED_SIGNATURE_NID 101
366 # define CT_R_UNSUPPORTED_ENTRY_TYPE 102
367 # define CT_R_UNSUPPORTED_VERSION 103