03cb51ffec1e2040df1b08d26bbfeefea9c99a68
[openssl.git] / crypto / ct / ct_log.c
1 /* Author: Adam Eijdenberg <adam.eijdenberg@gmail.com>. */
2 /* ====================================================================
3  * Copyright (c) 1998-2016 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include <openssl/conf.h>
60 #include <openssl/ct.h>
61 #include <openssl/err.h>
62 #include <openssl/evp.h>
63 #include <openssl/safestack.h>
64
65 #include "internal/cryptlib.h"
66
67 /*
68  * Information about a CT log server.
69  */
70 struct ctlog_st {
71     char *name;
72     uint8_t log_id[CT_V1_HASHLEN];
73     EVP_PKEY *public_key;
74 };
75
76 /*
77  * A store for multiple CTLOG instances.
78  * It takes ownership of any CTLOG instances added to it.
79  */
80 struct ctlog_store_st {
81     STACK_OF(CTLOG) *logs;
82 };
83
84 /* The context when loading a CT log list from a CONF file. */
85 typedef struct ctlog_store_load_ctx_st {
86     CTLOG_STORE *log_store;
87     CONF *conf;
88     size_t invalid_log_entries;
89 } CTLOG_STORE_LOAD_CTX;
90
91 /*
92  * Creates an empty context for loading a CT log store.
93  * It should be populated before use.
94  */
95 static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new();
96
97 /*
98  * Deletes a CT log store load context.
99  * Does not delete any of the fields.
100  */
101 static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx);
102
103 static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new()
104 {
105     CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
106
107     if (ctx == NULL) {
108         CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE);
109         goto err;
110     }
111
112     return ctx;
113 err:
114     ctlog_store_load_ctx_free(ctx);
115     return NULL;
116 }
117
118 static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx)
119 {
120     OPENSSL_free(ctx);
121 }
122
123 /* Converts a log's public key into a SHA256 log ID */
124 static int ct_v1_log_id_from_pkey(EVP_PKEY *pkey,
125                                   unsigned char log_id[CT_V1_HASHLEN])
126 {
127     int ret = 0;
128     unsigned char *pkey_der = NULL;
129     int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der);
130
131     if (pkey_der_len <= 0) {
132         CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID);
133         goto err;
134     }
135
136     SHA256(pkey_der, pkey_der_len, log_id);
137     ret = 1;
138 err:
139     OPENSSL_free(pkey_der);
140     return ret;
141 }
142
143 CTLOG_STORE *CTLOG_STORE_new(void)
144 {
145     CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
146
147     if (ret == NULL)
148         goto err;
149
150     ret->logs = sk_CTLOG_new_null();
151     if (ret->logs == NULL)
152         goto err;
153
154     return ret;
155 err:
156     CTLOG_STORE_free(ret);
157     return NULL;
158 }
159
160 void CTLOG_STORE_free(CTLOG_STORE *store)
161 {
162     if (store != NULL) {
163         sk_CTLOG_pop_free(store->logs, CTLOG_free);
164         OPENSSL_free(store);
165     }
166 }
167
168 static CTLOG *ctlog_new_from_conf(const CONF *conf, const char *section)
169 {
170     CTLOG *ret = NULL;
171     char *description = NCONF_get_string(conf, section, "description");
172     char *pkey_base64;
173
174     if (description == NULL) {
175         CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);
176         goto end;
177     }
178
179     pkey_base64 = NCONF_get_string(conf, section, "key");
180     if (pkey_base64 == NULL) {
181         CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY);
182         goto end;
183     }
184
185     ret = CTLOG_new_from_base64(pkey_base64, description);
186     if (ret == NULL) {
187         CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_INVALID);
188         goto end;
189     }
190
191 end:
192     return ret;
193 }
194
195 int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
196 {
197     const char *fpath = getenv(CTLOG_FILE_EVP);
198
199     if (fpath == NULL)
200       fpath = CTLOG_FILE;
201
202     return CTLOG_STORE_load_file(store, fpath);
203 }
204
205 /*
206  * Called by CONF_parse_list, which stops if this returns <= 0, so don't unless
207  * something very bad happens. Otherwise, one bad log entry would stop loading
208  * of any of the following log entries.
209  */
210 static int ctlog_store_load_log(const char *log_name, int log_name_len,
211                                 void *arg)
212 {
213     CTLOG_STORE_LOAD_CTX *load_ctx = arg;
214     CTLOG *ct_log;
215     /* log_name may not be null-terminated, so fix that before using it */
216     char *tmp;
217
218     /* log_name will be NULL for empty list entries */
219     if (log_name == NULL)
220         return 1;
221
222     tmp = OPENSSL_strndup(log_name, log_name_len);
223     ct_log = ctlog_new_from_conf(load_ctx->conf, tmp);
224     OPENSSL_free(tmp);
225     if (ct_log == NULL) {
226         /* If we can't load this log, record that fact and skip it */
227         ++load_ctx->invalid_log_entries;
228         return 1;
229     }
230
231     sk_CTLOG_push(load_ctx->log_store->logs, ct_log);
232     return 1;
233 }
234
235 int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
236 {
237     int ret = 0;
238     char *enabled_logs;
239     CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new();
240
241     load_ctx->log_store = store;
242     load_ctx->conf = NCONF_new(NULL);
243     if (load_ctx->conf == NULL)
244         goto end;
245
246     ret = NCONF_load(load_ctx->conf, file, NULL);
247     if (ret <= 0) {
248         CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
249         goto end;
250     }
251
252     enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs");
253     if (enabled_logs == NULL) {
254         ret = 0;
255         CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
256         goto end;
257     }
258
259     ret = CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx);
260     if (ret == 1 && load_ctx->invalid_log_entries > 0) {
261         ret = 0;
262         CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
263         goto end;
264     }
265
266 end:
267     NCONF_free(load_ctx->conf);
268     ctlog_store_load_ctx_free(load_ctx);
269     return ret;
270 }
271
272 /*
273  * Initialize a new CTLOG object.
274  * Takes ownership of the public key.
275  * Copies the name.
276  */
277 CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
278 {
279     CTLOG *ret = CTLOG_new_null();
280
281     if (ret == NULL)
282         goto err;
283
284     ret->name = OPENSSL_strdup(name);
285     if (ret->name == NULL)
286         goto err;
287
288     ret->public_key = public_key;
289     if (ct_v1_log_id_from_pkey(public_key, ret->log_id) != 1)
290         goto err;
291
292     return ret;
293 err:
294     CTLOG_free(ret);
295     return NULL;
296 }
297
298 CTLOG *CTLOG_new_null(void)
299 {
300     CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
301
302     if (ret == NULL)
303         CTerr(CT_F_CTLOG_NEW_NULL, ERR_R_MALLOC_FAILURE);
304
305     return ret;
306 }
307
308 /* Frees CT log and associated structures */
309 void CTLOG_free(CTLOG *log)
310 {
311     if (log != NULL) {
312         OPENSSL_free(log->name);
313         EVP_PKEY_free(log->public_key);
314         OPENSSL_free(log);
315     }
316 }
317
318 const char *CTLOG_get0_name(CTLOG *log)
319 {
320     return log->name;
321 }
322
323 void CTLOG_get0_log_id(CTLOG *log, uint8_t **log_id, size_t *log_id_len)
324 {
325     *log_id = log->log_id;
326     *log_id_len = CT_V1_HASHLEN;
327 }
328
329 EVP_PKEY *CTLOG_get0_public_key(CTLOG *log)
330 {
331     return log->public_key;
332 }
333
334 /*
335  * Given a log ID, finds the matching log.
336  * Returns NULL if no match found.
337  */
338 CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
339                                   const uint8_t *log_id,
340                                   size_t log_id_len)
341 {
342     int i;
343
344     for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
345         CTLOG *log = sk_CTLOG_value(store->logs, i);
346         if (memcmp(log->log_id, log_id, log_id_len) == 0)
347             return log;
348     }
349
350     return NULL;
351 }