1 /* Author: Adam Eijdenberg <adam.eijdenberg@gmail.com>. */
2 /* ====================================================================
3 * Copyright (c) 1998-2016 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
56 #include <openssl/conf.h>
57 #include <openssl/ct.h>
58 #include <openssl/err.h>
59 #include <openssl/evp.h>
60 #include <openssl/safestack.h>
62 #include "internal/cryptlib.h"
65 * Information about a CT log server.
69 uint8_t log_id[CT_V1_HASHLEN];
74 * A store for multiple CTLOG instances.
75 * It takes ownership of any CTLOG instances added to it.
77 struct ctlog_store_st {
78 STACK_OF(CTLOG) *logs;
81 /* The context when loading a CT log list from a CONF file. */
82 typedef struct ctlog_store_load_ctx_st {
83 CTLOG_STORE *log_store;
85 } CTLOG_STORE_LOAD_CTX;
88 * Creates an empty context for loading a CT log store.
89 * It should be populated before use.
91 static CTLOG_STORE_LOAD_CTX *CTLOG_STORE_LOAD_CTX_new();
94 * Deletes a CT log store load context.
95 * Does not delete any of the fields.
97 static void CTLOG_STORE_LOAD_CTX_free(CTLOG_STORE_LOAD_CTX* ctx);
99 static CTLOG_STORE_LOAD_CTX *CTLOG_STORE_LOAD_CTX_new()
101 CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(CTLOG_STORE_LOAD_CTX));
103 CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE);
110 CTLOG_STORE_LOAD_CTX_free(ctx);
114 static void CTLOG_STORE_LOAD_CTX_free(CTLOG_STORE_LOAD_CTX* ctx)
122 /* Converts a log's public key into a SHA256 log ID */
123 static int CT_v1_log_id_from_pkey(EVP_PKEY *pkey,
124 unsigned char log_id[CT_V1_HASHLEN])
127 unsigned char *pkey_der = NULL;
128 int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der);
129 if (pkey_der_len <= 0) {
130 CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID);
133 SHA256(pkey_der, pkey_der_len, log_id);
136 OPENSSL_free(pkey_der);
140 CTLOG_STORE *CTLOG_STORE_new(void)
142 CTLOG_STORE *ret = OPENSSL_malloc(sizeof(CTLOG_STORE));
145 ret->logs = sk_CTLOG_new_null();
146 if (ret->logs == NULL)
150 CTLOG_STORE_free(ret);
154 void CTLOG_STORE_free(CTLOG_STORE *store)
157 sk_CTLOG_pop_free(store->logs, CTLOG_free);
162 static CTLOG *CTLOG_new_from_conf(const CONF *conf, const char *section)
167 if (conf == NULL || section == NULL) {
168 CTerr(CT_F_CTLOG_NEW_FROM_CONF, ERR_R_PASSED_NULL_PARAMETER);
172 description = NCONF_get_string(conf, section, "description");
174 if (description == NULL) {
175 CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);
179 pkey_base64 = NCONF_get_string(conf, section, "key");
181 if (pkey_base64 == NULL) {
182 CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY);
186 ret = CTLOG_new_from_base64(pkey_base64, description);
188 CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_INVALID);
196 int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
198 char *fpath = (char *)getenv(CTLOG_FILE_EVP);
201 return CTLOG_STORE_load_file(store, fpath);
204 static int CTLOG_STORE_load_log(const char *log_name, int log_name_len, void *arg)
206 CTLOG_STORE_LOAD_CTX *load_ctx = arg;
209 /* log_name may not be null-terminated, so fix that before using it */
210 char *tmp = OPENSSL_strndup(log_name, log_name_len);
211 ct_log = CTLOG_new_from_conf(load_ctx->conf, tmp);
216 sk_CTLOG_push(load_ctx->log_store->logs, ct_log);
220 int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
224 CTLOG_STORE_LOAD_CTX* load_ctx = CTLOG_STORE_LOAD_CTX_new();
225 load_ctx->log_store = store;
226 load_ctx->conf = NCONF_new(NULL);
227 if (load_ctx->conf == NULL)
230 ret = NCONF_load(load_ctx->conf, file, NULL);
232 CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
236 enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs");
237 CONF_parse_list(enabled_logs, ',', 1, CTLOG_STORE_load_log, load_ctx);
240 NCONF_free(load_ctx->conf);
241 CTLOG_STORE_LOAD_CTX_free(load_ctx);
246 * Initialize a new CTLOG object.
247 * Takes ownership of the public key.
250 CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
253 if (public_key == NULL || name == NULL) {
254 CTerr(CT_F_CTLOG_NEW, ERR_R_PASSED_NULL_PARAMETER);
257 ret = CTLOG_new_null();
260 ret->name = OPENSSL_strdup(name);
261 if (ret->name == NULL)
263 ret->public_key = public_key;
264 if (CT_v1_log_id_from_pkey(public_key, ret->log_id) != 1)
272 CTLOG *CTLOG_new_null(void)
274 CTLOG *ret = OPENSSL_zalloc(sizeof(CTLOG));
276 CTerr(CT_F_CTLOG_NEW_NULL, ERR_R_MALLOC_FAILURE);
280 /* Frees CT log and associated structures */
281 void CTLOG_free(CTLOG *log)
284 OPENSSL_free(log->name);
287 EVP_PKEY_free(log->public_key);
288 log->public_key = NULL;
294 const char *CTLOG_get0_name(CTLOG *log)
299 void CTLOG_get0_log_id(CTLOG *log, uint8_t **log_id, size_t *log_id_len)
301 *log_id = log->log_id;
302 *log_id_len = CT_V1_HASHLEN;
305 EVP_PKEY *CTLOG_get0_public_key(CTLOG *log)
307 return log->public_key;
311 * Given a log ID, finds the matching log.
312 * Returns NULL if no match found.
314 CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
315 const uint8_t *log_id,
320 CTerr(CT_F_CTLOG_STORE_GET0_LOG_BY_ID, ERR_R_PASSED_NULL_PARAMETER);
323 for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
324 CTLOG *log = sk_CTLOG_value(store->logs, i);
325 if (memcmp(log->log_id, log_id, log_id_len) == 0)