TLS support for X25519
[oweals/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/ossl_typ.h>
62 # include <openssl/safestack.h>
63 # include <openssl/x509.h>
64
65 # ifdef  __cplusplus
66 extern "C" {
67 # endif
68
69 /* Minimum RSA key size, from RFC6962 */
70 # define SCT_MIN_RSA_BITS 2048
71
72 /* All hashes are SHA256 in v1 of Certificate Transparency */
73 # define CT_V1_HASHLEN SHA256_DIGEST_LENGTH
74
75 typedef enum {
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;
80
81 typedef enum {
82     SCT_VERSION_NOT_SET = -1,
83     SCT_VERSION_V1 = 0
84 } sct_version_t;
85
86 DEFINE_STACK_OF(SCT)
87
88 /*****************
89  * SCT functions *
90  *****************/
91
92 /*
93  * Creates a new, blank SCT.
94  * The caller is responsible for calling SCT_free when finished with the SCT.
95  */
96 SCT *SCT_new(void);
97
98 /*
99  * Frees the SCT and the underlying data structures.
100  */
101 void SCT_free(SCT *sct);
102
103 /*
104  * Free a stack of SCTs, and the underlying SCTs themselves.
105  * Intended to be compatible with X509V3_EXT_FREE.
106  */
107 void SCT_LIST_free(STACK_OF(SCT) *a);
108
109 /*
110  * Returns the version of the SCT.
111  */
112 sct_version_t SCT_get_version(const SCT *sct);
113
114 /*
115  * Set the version of an SCT.
116  * Returns 1 on success, 0 if the version is unrecognized.
117  */
118 int SCT_set_version(SCT *sct, sct_version_t version);
119
120 /*
121  * Returns the log entry type of the SCT.
122  */
123 ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct);
124
125 /*
126  * Set the log entry type of an SCT.
127  * Returns 1 on success.
128  */
129 int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type);
130
131 /*
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.
135  */
136 size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id);
137
138 /*
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.
142  */
143 int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len);
144
145 /*
146  * Set the log ID of an SCT.
147  * This makes a copy of the log_id.
148  * Returns 1 on success.
149  */
150 int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len);
151
152 /*
153  * Returns the timestamp for the SCT (epoch time in milliseconds).
154  */
155 uint64_t SCT_get_timestamp(const SCT *sct);
156
157 /*
158  * Set the timestamp of an SCT (epoch time in milliseconds).
159  */
160 void SCT_set_timestamp(SCT *sct, uint64_t timestamp);
161
162 /*
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).
166  */
167 int SCT_get_signature_nid(const SCT *sct);
168
169 /*
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.
174  */
175 int SCT_set_signature_nid(SCT *sct, int nid);
176
177 /*
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.
181  */
182 size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext);
183
184 /*
185  * Set the extensions of an SCT to point directly to the *ext specified.
186  * The SCT takes ownership of the specified pointer.
187  */
188 void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len);
189
190 /*
191  * Set the extensions of an SCT.
192  * This takes a copy of the ext.
193  * Returns 1 on success.
194  */
195 int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len);
196
197 /*
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.
201  */
202 size_t SCT_get0_signature(const SCT *sct, unsigned char **sig);
203
204 /*
205  * Set the signature of an SCT to point directly to the *sig specified.
206  * The SCT takes ownership of the specified pointer.
207  */
208 void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len);
209
210 /*
211  * Set the signature of an SCT to be a copy of the *sig specified.
212  * Returns 1 on success.
213  */
214 int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len);
215
216 /*
217  * Pretty-prints an |sct| to |out|.
218  * It will be indented by the number of spaces specified by |indent|.
219  */
220 void SCT_print(const SCT *sct, BIO *out, int indent);
221
222 /*
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|.
226  */
227 void SCT_LIST_print(const STACK_OF(SCT) *sct_list, BIO *out, int indent,
228                     const char *separator);
229
230 /*********************************
231  * SCT parsing and serialisation *
232  *********************************/
233
234 /*
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
240  * successfully).
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)
244  * on success.
245  */
246 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp);
247
248 /*
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
254  * will be returned.
255  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
256  * not defined.
257  */
258 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
259                             size_t len);
260
261 /*
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
267  * successfully).
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)
271  * on success.
272  */
273 int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp);
274
275 /*
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
281  * will be returned.
282  * Upon failure, a NULL pointer will be returned, and the position of "*pp" is
283  * not defined.
284  */
285 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
286                             long len);
287
288 /*
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
294  * to it.
295  * The length of the SCT in TLS format will be returned.
296  */
297 int i2o_SCT(const SCT *sct, unsigned char **out);
298
299 /*
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).
309  */
310 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len);
311
312 /*
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.
319 */
320 int i2o_SCT_signature(const SCT *sct, unsigned char **out);
321
322 /*
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.
328 */
329 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len);
330
331 /* BEGIN ERROR CODES */
332 /*
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.
335  */
336 void ERR_load_CT_strings(void);
337
338 /* Error codes for the CT functions. */
339
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
358
359 /* Reason codes. */
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
368
369 #ifdef  __cplusplus
370 }
371 #endif
372 #endif