]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ct/ct_lib.c
GH752 ct_test uses testutil, so include that
[thirdparty/openssl.git] / crypto / ct / ct_lib.c
CommitLineData
3149baf8
AE
1/*
2 * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
3 * (steve@openssl.org) for the OpenSSL project 2014.
4 */
5/* ====================================================================
6 * Copyright (c) 2014 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 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#ifndef OPENSSL_NO_CT
60
61# include <limits.h>
62# include "internal/cryptlib.h"
3dc9589c 63# include "../../ssl/ssl_locl.h"
eb6d5f99 64# include "internal/ct_int.h"
3149baf8
AE
65
66SCT *SCT_new(void)
67{
68 SCT *sct = OPENSSL_zalloc(sizeof(SCT));
69 if (sct == NULL) {
70 CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
71 return NULL;
72 }
73 sct->entry_type = UNSET_ENTRY;
74 sct->version = UNSET_VERSION;
75 return sct;
76}
77
78void SCT_free(SCT *sct)
79{
5ad29c54 80 if (sct != NULL) {
3149baf8
AE
81 OPENSSL_free(sct->log_id);
82 OPENSSL_free(sct->ext);
83 OPENSSL_free(sct->sig);
84 OPENSSL_free(sct->sct);
85 OPENSSL_free(sct);
86 }
87}
88
89int SCT_set_version(SCT *sct, sct_version_t version)
90{
91 if (version != SCT_V1) {
92 CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
93 return 0;
94 }
95 sct->version = version;
96 return 1;
97}
98
99int SCT_set_log_entry_type(SCT *sct, log_entry_type_t entry_type)
100{
101 if (entry_type != X509_ENTRY && entry_type != PRECERT_ENTRY) {
102 CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
103 return 0;
104 }
105 sct->entry_type = entry_type;
106 return 1;
107}
108
109int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
110{
111 /* Currently only SHA-256 allowed so length must be SCT_V1_HASHLEN */
112 if (log_id_len != SCT_V1_HASHLEN) {
113 CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
114 return 0;
115 }
116 OPENSSL_free(sct->log_id);
117 sct->log_id = log_id;
118 sct->log_id_len = log_id_len;
119 return 1;
120}
121
5ad29c54
AE
122int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
123{
124 /* Currently only SHA-256 allowed so length must be SCT_V1_HASHLEN */
125 if (log_id_len != SCT_V1_HASHLEN) {
126 CTerr(CT_F_SCT_SET1_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
127 return 0;
128 }
129
130 OPENSSL_free(sct->log_id);
131 if (log_id == NULL || log_id_len == 0) {
132 sct->log_id = NULL;
133 } else {
134 sct->log_id = OPENSSL_memdup(log_id, log_id_len);
135
136 if (sct->log_id == NULL) {
137 CTerr(CT_F_SCT_SET1_LOG_ID, ERR_R_MALLOC_FAILURE);
138 return 0;
139 }
140 }
141
142 sct->log_id_len = log_id_len;
143 return 1;
144}
145
146
3149baf8
AE
147void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
148{
149 sct->timestamp = timestamp;
150}
151
152int SCT_set_signature_nid(SCT *sct, int nid)
153{
154 switch (nid) {
155 case NID_sha256WithRSAEncryption:
156 sct->hash_alg = TLSEXT_hash_sha256;
157 sct->sig_alg = TLSEXT_signature_rsa;
158 return 1;
159 case NID_ecdsa_with_SHA256:
160 sct->hash_alg = TLSEXT_hash_sha256;
161 sct->sig_alg = TLSEXT_signature_ecdsa;
162 return 1;
163 default:
164 CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
165 return 0;
166 }
167}
168
169void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
170{
171 OPENSSL_free(sct->ext);
172 sct->ext = ext;
173 sct->ext_len = ext_len;
174}
175
5ad29c54
AE
176int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
177{
178 OPENSSL_free(sct->ext);
179 sct->ext = NULL;
180 sct->ext_len = 0;
181
182 if (ext != NULL && ext_len > 0) {
183 sct->ext = OPENSSL_memdup(ext, ext_len);
184 if (sct->ext == NULL) {
185 CTerr(CT_F_SCT_SET1_EXTENSIONS, ERR_R_MALLOC_FAILURE);
186 return 0;
187 }
188 sct->ext_len = ext_len;
189 }
190 return 1;
191}
192
3149baf8
AE
193void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
194{
195 OPENSSL_free(sct->sig);
196 sct->sig = sig;
197 sct->sig_len = sig_len;
198}
199
5ad29c54
AE
200int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
201{
202 OPENSSL_free(sct->sig);
203 if (sig == NULL || sig_len == 0) {
204 sct->sig = NULL;
205 } else {
206 sct->sig = OPENSSL_memdup(sig, sig_len);
207 if (sct->sig == NULL) {
208 CTerr(CT_F_SCT_SET1_SIGNATURE, ERR_R_MALLOC_FAILURE);
209 return 0;
210 }
211 }
212 sct->sig_len = sig_len;
213 return 1;
214}
215
3149baf8
AE
216sct_version_t SCT_get_version(const SCT *sct)
217{
218 return sct->version;
219}
220
221log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
222{
223 return sct->entry_type;
224}
225
226size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
227{
228 *log_id = sct->log_id;
229 return sct->log_id_len;
230}
231
232uint64_t SCT_get_timestamp(const SCT *sct)
233{
234 return sct->timestamp;
235}
236
237int SCT_get_signature_nid(const SCT *sct)
238{
239 if (sct->version == SCT_V1) {
240 if (sct->hash_alg == TLSEXT_hash_sha256) {
241 switch (sct->sig_alg) {
242 case TLSEXT_signature_ecdsa:
243 return NID_ecdsa_with_SHA256;
244 case TLSEXT_signature_rsa:
245 return NID_sha256WithRSAEncryption;
246 default:
247 return NID_undef;
248 }
249 }
250 }
251 return NID_undef;
252}
253
254size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
255{
256 *ext = sct->ext;
257 return sct->ext_len;
258}
259
260size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
261{
262 *sig = sct->sig;
263 return sct->sig_len;
264}
265
5ad29c54
AE
266int SCT_is_valid(const SCT *sct)
267{
268 switch (sct->version) {
269 case UNSET_VERSION:
270 return 0;
271 case SCT_V1:
272 return sct->log_id != NULL && SCT_signature_is_valid(sct);
273 default:
274 return sct->sct != NULL; /* Just need cached encoding */
275 }
276}
277
3149baf8 278#endif