Public API for Certificate Transparency
[openssl.git] / include / openssl / ct.h
1 /*
2 * Public API for Certificate Transparency (CT).
3 * Written by Rob Percival (robpercival@google.com) for the OpenSSL project.
4 */
5 /* ====================================================================
6 * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
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
18 *    distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 *    acknowledgment:
36 *    "This product includes software developed by the OpenSSL Project
37 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 */
53
54 #ifdef OPENSSL_NO_CT
55 # error "CT is disabled"
56 #endif
57
58 #ifndef HEADER_CT_H
59 # define HEADER_CT_H
60
61 # include <openssl/safestack.h>
62 # include <openssl/x509.h>
63
64 # ifdef  __cplusplus
65 extern "C" {
66 # endif
67
68 /* Minimum RSA key size, from RFC6962 */
69 # define SCT_MIN_RSA_BITS 2048
70
71 /* All hashes are SHA256 in v1 of Certificate Transparency */
72 # define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
73
74 typedef enum {
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;
79
80 typedef enum {
81     SCT_VERSION_NOT_SET = -1,
82     SCT_VERSION_V1 = 0
83 } sct_version_t;
84
85 /*******************
86  * Data structures *
87  *******************/
88
89 /* Signed Certificate Timestamp (SCT) */
90 typedef struct sct_st SCT;
91 DEFINE_STACK_OF(SCT)
92
93 /*****************
94  * SCT functions *
95  *****************/
96
97 /*
98  * Creates a new, blank SCT.
99  * The caller is responsible for calling SCT_free when finished with the SCT.
100  */
101 SCT *SCT_new(void);
102
103 /*
104  * Frees the SCT and the underlying data structures.
105  */
106 void SCT_free(SCT *sct);
107
108 /*
109  * Free a stack of SCTs, and the underlying SCTs themselves.
110  * Intended to be compatible with X509V3_EXT_FREE.
111  */
112 void SCT_LIST_free(STACK_OF(SCT) *a);
113
114 /*
115  * Returns the version of the SCT.
116  */
117 sct_version_t SCT_get_version(const SCT *sct);
118
119 /*
120  * Set the version of an SCT.
121  * Returns 1 on success, 0 if the version is unrecognized.
122  */
123 int SCT_set_version(SCT *sct, sct_version_t version);
124
125 /*
126  * Returns the log entry type of the SCT.
127  */
128 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
129
130 /*
131  * Set the log entry type of an SCT.
132  * Returns 1 on success.
133  */
134 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
135
136 /*
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.
140  */
141 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
142
143 /*
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.
147  */
148 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
149
150 /*
151  * Set the log ID of an SCT.
152  * This makes a copy of the log_id.
153  * Returns 1 on success.
154  */
155 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len);
156
157 /*
158  * Returns the timestamp for the SCT (epoch time in milliseconds).
159  */
160 uint64_t SCT_get_timestamp(const SCT *sct);
161
162 /*
163  * Set the timestamp of an SCT (epoch time in milliseconds).
164  */
165 void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
166
167 /*
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).
171  */
172 int SCT_get_signature_nid(const SCT *sct);
173
174 /*
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.
179  */
180 int SCT_set_signature_nid(SCT *sct, int nid);
181
182 /*
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.
186  */
187 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
188
189 /*
190  * Set the extensions of an SCT to point directly to the *ext specified.
191  * The SCT takes ownership of the specified pointer.
192  */
193 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
194
195 /*
196  * Set the extensions of an SCT.
197  * This takes a copy of the ext.
198  * Returns 1 on success.
199  */
200 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len);
201
202 /*
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.
206  */
207 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
208
209 /*
210  * Set the signature of an SCT to point directly to the *sig specified.
211  * The SCT takes ownership of the specified pointer.
212  */
213 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
214
215 /*
216  * Set the signature of an SCT to be a copy of the *sig specified.
217  * Returns 1 on success.
218  */
219 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len);
220
221 /*
222  * Pretty-prints an |sct| to |out|.
223  * It will be indented by the number of spaces specified by |indent|.
224  */
225 void SCT_print(const SCT *sct, BIO *out, int indent);
226
227 /*
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|.
231  */
232 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
233                     const char *separator);
234
235 /*********************************
236  * SCT parsing and serialisation *
237  *********************************/
238
239 /*
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
245  * successfully).
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)
249  * on success.
250  */
251 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
252
253 /*
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
259  * will be returned.
260  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
261  * not defined.
262  */
263 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
264                             size_t len);
265
266 /*
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
272  * successfully).
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)
276  * on success.
277  */
278 int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp);
279
280 /*
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
286  * will be returned.
287  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
288  * not defined.
289  */
290 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
291                             long len);
292
293 /*
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
299  * to it.
300  * The length of the SCT in TLS format will be returned.
301  */
302 int i2o_SCT(const SCT *sct, unsigned char **out);
303
304 /*
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).
314  */
315 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
316
317 /*
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.
324 */
325 int i2o_SCT_signature(const SCT *sct, unsigned char **out);
326
327 /*
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.
333 */
334 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len);
335
336 /* BEGIN ERROR CODES */
337 /*
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.
340  */
341 void ERR_load_CT_strings(void);
342
343 /* Error codes for the CT functions. */
344
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
363
364 /* Reason codes. */
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
373
374 #ifdef  __cplusplus
375 }
376 #endif
377 #endif