]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ct/ct_log.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / crypto / ct / ct_log.c
CommitLineData
d2e9e320 1/*
a28d06f3 2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
8c6afbc5 3 *
5477e842 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8c6afbc5
RP
8 */
9
0dfd6cf9
RP
10#include <stdlib.h>
11#include <string.h>
12
8c6afbc5
RP
13#include <openssl/conf.h>
14#include <openssl/ct.h>
15#include <openssl/err.h>
16#include <openssl/evp.h>
17#include <openssl/safestack.h>
18
19#include "internal/cryptlib.h"
20
21/*
22 * Information about a CT log server.
23 */
24struct ctlog_st {
b4250010 25 OSSL_LIB_CTX *libctx;
d4b2bfba 26 char *propq;
8c6afbc5
RP
27 char *name;
28 uint8_t log_id[CT_V1_HASHLEN];
29 EVP_PKEY *public_key;
30};
31
32/*
33 * A store for multiple CTLOG instances.
34 * It takes ownership of any CTLOG instances added to it.
35 */
36struct ctlog_store_st {
b4250010 37 OSSL_LIB_CTX *libctx;
d4b2bfba 38 char *propq;
8c6afbc5
RP
39 STACK_OF(CTLOG) *logs;
40};
41
42/* The context when loading a CT log list from a CONF file. */
43typedef struct ctlog_store_load_ctx_st {
44 CTLOG_STORE *log_store;
45 CONF *conf;
a930afb6 46 size_t invalid_log_entries;
8c6afbc5
RP
47} CTLOG_STORE_LOAD_CTX;
48
49/*
50 * Creates an empty context for loading a CT log store.
51 * It should be populated before use.
52 */
91860165 53static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void);
8c6afbc5
RP
54
55/*
56 * Deletes a CT log store load context.
57 * Does not delete any of the fields.
58 */
0dfd6cf9 59static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx);
8c6afbc5 60
3cb7c5cf 61static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void)
8c6afbc5 62{
0dfd6cf9
RP
63 CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
64
8c6afbc5 65 return ctx;
8c6afbc5
RP
66}
67
0dfd6cf9 68static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx)
8c6afbc5 69{
8c6afbc5
RP
70 OPENSSL_free(ctx);
71}
72
73/* Converts a log's public key into a SHA256 log ID */
d4b2bfba 74static int ct_v1_log_id_from_pkey(CTLOG *log, EVP_PKEY *pkey)
8c6afbc5
RP
75{
76 int ret = 0;
77 unsigned char *pkey_der = NULL;
78 int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der);
85d843c8 79 unsigned int len;
d4b2bfba 80 EVP_MD *sha256 = NULL;
0dfd6cf9 81
8c6afbc5 82 if (pkey_der_len <= 0) {
9311d0c4 83 ERR_raise(ERR_LIB_CT, CT_R_LOG_KEY_INVALID);
8c6afbc5
RP
84 goto err;
85 }
d4b2bfba
MC
86 sha256 = EVP_MD_fetch(log->libctx, "SHA2-256", log->propq);
87 if (sha256 == NULL) {
c5689319 88 ERR_raise(ERR_LIB_CT, ERR_R_EVP_LIB);
d4b2bfba
MC
89 goto err;
90 }
0dfd6cf9 91
d4b2bfba
MC
92 ret = EVP_Digest(pkey_der, pkey_der_len, log->log_id, &len, sha256,
93 NULL);
8c6afbc5 94err:
d4b2bfba 95 EVP_MD_free(sha256);
8c6afbc5
RP
96 OPENSSL_free(pkey_der);
97 return ret;
98}
99
b4250010 100CTLOG_STORE *CTLOG_STORE_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
8c6afbc5 101{
0dfd6cf9
RP
102 CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
103
e077455e 104 if (ret == NULL)
e57036f2 105 return NULL;
0dfd6cf9 106
d4b2bfba
MC
107 ret->libctx = libctx;
108 if (propq != NULL) {
109 ret->propq = OPENSSL_strdup(propq);
e077455e 110 if (ret->propq == NULL)
d4b2bfba 111 goto err;
d4b2bfba
MC
112 }
113
8c6afbc5 114 ret->logs = sk_CTLOG_new_null();
d4b2bfba 115 if (ret->logs == NULL) {
e077455e 116 ERR_raise(ERR_LIB_CT, ERR_R_CRYPTO_LIB);
8c6afbc5 117 goto err;
d4b2bfba 118 }
0dfd6cf9 119
8c6afbc5
RP
120 return ret;
121err:
d4b2bfba 122 CTLOG_STORE_free(ret);
8c6afbc5
RP
123 return NULL;
124}
125
d4b2bfba
MC
126CTLOG_STORE *CTLOG_STORE_new(void)
127{
d8652be0 128 return CTLOG_STORE_new_ex(NULL, NULL);
d4b2bfba
MC
129}
130
8c6afbc5
RP
131void CTLOG_STORE_free(CTLOG_STORE *store)
132{
133 if (store != NULL) {
d4b2bfba 134 OPENSSL_free(store->propq);
8c6afbc5
RP
135 sk_CTLOG_pop_free(store->logs, CTLOG_free);
136 OPENSSL_free(store);
137 }
138}
139
d4b2bfba
MC
140static int ctlog_new_from_conf(CTLOG_STORE *store, CTLOG **ct_log,
141 const CONF *conf, const char *section)
8c6afbc5 142{
e57036f2 143 const char *description = NCONF_get_string(conf, section, "description");
8c6afbc5 144 char *pkey_base64;
8c6afbc5
RP
145
146 if (description == NULL) {
9311d0c4 147 ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_MISSING_DESCRIPTION);
4aed8756 148 return 0;
8c6afbc5
RP
149 }
150
151 pkey_base64 = NCONF_get_string(conf, section, "key");
8c6afbc5 152 if (pkey_base64 == NULL) {
9311d0c4 153 ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_MISSING_KEY);
4aed8756 154 return 0;
8c6afbc5
RP
155 }
156
d8652be0
MC
157 return CTLOG_new_from_base64_ex(ct_log, pkey_base64, description,
158 store->libctx, store->propq);
8c6afbc5
RP
159}
160
161int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
162{
5c39a55d 163 const char *fpath = ossl_safe_getenv(CTLOG_FILE_EVP);
0dfd6cf9 164
8c6afbc5
RP
165 if (fpath == NULL)
166 fpath = CTLOG_FILE;
0dfd6cf9 167
8c6afbc5
RP
168 return CTLOG_STORE_load_file(store, fpath);
169}
170
a930afb6 171/*
4aed8756
F
172 * Called by CONF_parse_list, which stops if this returns <= 0,
173 * Otherwise, one bad log entry would stop loading of any of
174 * the following log entries.
175 * It may stop parsing and returns -1 on any internal (malloc) error.
a930afb6
RP
176 */
177static int ctlog_store_load_log(const char *log_name, int log_name_len,
178 void *arg)
8c6afbc5
RP
179{
180 CTLOG_STORE_LOAD_CTX *load_ctx = arg;
e57036f2 181 CTLOG *ct_log = NULL;
8c6afbc5 182 /* log_name may not be null-terminated, so fix that before using it */
2508c047 183 char *tmp;
4aed8756 184 int ret = 0;
0dfd6cf9 185
2508c047
RP
186 /* log_name will be NULL for empty list entries */
187 if (log_name == NULL)
188 return 1;
189
190 tmp = OPENSSL_strndup(log_name, log_name_len);
e57036f2 191 if (tmp == NULL)
e077455e 192 return -1;
e57036f2 193
d4b2bfba 194 ret = ctlog_new_from_conf(load_ctx->log_store, &ct_log, load_ctx->conf, tmp);
8c6afbc5 195 OPENSSL_free(tmp);
4aed8756
F
196
197 if (ret < 0) {
198 /* Propagate any internal error */
199 return ret;
200 }
201 if (ret == 0) {
a930afb6
RP
202 /* If we can't load this log, record that fact and skip it */
203 ++load_ctx->invalid_log_entries;
204 return 1;
205 }
8c6afbc5 206
68efafc5 207 if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) {
e077455e
RL
208 CTLOG_free(ct_log);
209 ERR_raise(ERR_LIB_CT, ERR_R_CRYPTO_LIB);
210 return -1;
68efafc5 211 }
8c6afbc5
RP
212 return 1;
213}
214
215int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
216{
a930afb6 217 int ret = 0;
8c6afbc5 218 char *enabled_logs;
0dfd6cf9
RP
219 CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new();
220
d3c3dfc5 221 if (load_ctx == NULL)
9be34ee5 222 return 0;
8c6afbc5
RP
223 load_ctx->log_store = store;
224 load_ctx->conf = NCONF_new(NULL);
225 if (load_ctx->conf == NULL)
226 goto end;
227
70073f3e 228 if (NCONF_load(load_ctx->conf, file, NULL) <= 0) {
9311d0c4 229 ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID);
8c6afbc5
RP
230 goto end;
231 }
232
233 enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs");
0c6ea565 234 if (enabled_logs == NULL) {
9311d0c4 235 ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID);
0c6ea565
RP
236 goto end;
237 }
238
70073f3e
RP
239 if (!CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx) ||
240 load_ctx->invalid_log_entries > 0) {
9311d0c4 241 ERR_raise(ERR_LIB_CT, CT_R_LOG_CONF_INVALID);
a930afb6
RP
242 goto end;
243 }
8c6afbc5 244
70073f3e 245 ret = 1;
8c6afbc5
RP
246end:
247 NCONF_free(load_ctx->conf);
0dfd6cf9 248 ctlog_store_load_ctx_free(load_ctx);
8c6afbc5
RP
249 return ret;
250}
251
252/*
253 * Initialize a new CTLOG object.
254 * Takes ownership of the public key.
255 * Copies the name.
256 */
b4250010 257CTLOG *CTLOG_new_ex(EVP_PKEY *public_key, const char *name, OSSL_LIB_CTX *libctx,
d8652be0 258 const char *propq)
8c6afbc5 259{
1ccbe6b3 260 CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
0dfd6cf9 261
e077455e 262 if (ret == NULL)
e57036f2 263 return NULL;
0dfd6cf9 264
d4b2bfba
MC
265 ret->libctx = libctx;
266 if (propq != NULL) {
163bf682 267 ret->propq = OPENSSL_strdup(propq);
e077455e 268 if (ret->propq == NULL)
d4b2bfba 269 goto err;
d4b2bfba
MC
270 }
271
8c6afbc5 272 ret->name = OPENSSL_strdup(name);
e077455e 273 if (ret->name == NULL)
8c6afbc5 274 goto err;
0dfd6cf9 275
d4b2bfba 276 if (ct_v1_log_id_from_pkey(ret, public_key) != 1)
8c6afbc5 277 goto err;
0dfd6cf9 278
986dbbbe 279 ret->public_key = public_key;
8c6afbc5
RP
280 return ret;
281err:
282 CTLOG_free(ret);
283 return NULL;
284}
285
d4b2bfba
MC
286CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
287{
d8652be0 288 return CTLOG_new_ex(public_key, name, NULL, NULL);
d4b2bfba
MC
289}
290
8c6afbc5
RP
291/* Frees CT log and associated structures */
292void CTLOG_free(CTLOG *log)
293{
294 if (log != NULL) {
295 OPENSSL_free(log->name);
8c6afbc5 296 EVP_PKEY_free(log->public_key);
d4b2bfba 297 OPENSSL_free(log->propq);
8c6afbc5
RP
298 OPENSSL_free(log);
299 }
300}
301
8c92c4ea 302const char *CTLOG_get0_name(const CTLOG *log)
8c6afbc5
RP
303{
304 return log->name;
305}
306
8c92c4ea
RP
307void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
308 size_t *log_id_len)
8c6afbc5
RP
309{
310 *log_id = log->log_id;
311 *log_id_len = CT_V1_HASHLEN;
312}
313
8c92c4ea 314EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log)
8c6afbc5
RP
315{
316 return log->public_key;
317}
318
319/*
320 * Given a log ID, finds the matching log.
321 * Returns NULL if no match found.
322 */
12d2d281
RP
323const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
324 const uint8_t *log_id,
325 size_t log_id_len)
8c6afbc5
RP
326{
327 int i;
0dfd6cf9 328
8c6afbc5 329 for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
12d2d281 330 const CTLOG *log = sk_CTLOG_value(store->logs, i);
8c6afbc5
RP
331 if (memcmp(log->log_id, log_id, log_id_len) == 0)
332 return log;
333 }
0dfd6cf9 334
8c6afbc5
RP
335 return NULL;
336}