]>
Commit | Line | Data |
---|---|---|
d2e9e320 | 1 | /* |
33388b44 | 2 | * Copyright 2016-2020 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 | */ | |
24 | struct 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 | */ | |
36 | struct 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. */ | |
43 | typedef 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 | 53 | static 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 | 59 | static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx); |
8c6afbc5 | 60 | |
3cb7c5cf | 61 | static 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 | ||
e57036f2 | 65 | if (ctx == NULL) |
8c6afbc5 | 66 | CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE); |
8c6afbc5 RP |
67 | |
68 | return ctx; | |
8c6afbc5 RP |
69 | } |
70 | ||
0dfd6cf9 | 71 | static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx) |
8c6afbc5 | 72 | { |
8c6afbc5 RP |
73 | OPENSSL_free(ctx); |
74 | } | |
75 | ||
76 | /* Converts a log's public key into a SHA256 log ID */ | |
d4b2bfba | 77 | static int ct_v1_log_id_from_pkey(CTLOG *log, EVP_PKEY *pkey) |
8c6afbc5 RP |
78 | { |
79 | int ret = 0; | |
80 | unsigned char *pkey_der = NULL; | |
81 | int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der); | |
85d843c8 | 82 | unsigned int len; |
d4b2bfba | 83 | EVP_MD *sha256 = NULL; |
0dfd6cf9 | 84 | |
8c6afbc5 RP |
85 | if (pkey_der_len <= 0) { |
86 | CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID); | |
87 | goto err; | |
88 | } | |
d4b2bfba MC |
89 | sha256 = EVP_MD_fetch(log->libctx, "SHA2-256", log->propq); |
90 | if (sha256 == NULL) { | |
91 | CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, ERR_LIB_EVP); | |
92 | goto err; | |
93 | } | |
0dfd6cf9 | 94 | |
d4b2bfba MC |
95 | ret = EVP_Digest(pkey_der, pkey_der_len, log->log_id, &len, sha256, |
96 | NULL); | |
8c6afbc5 | 97 | err: |
d4b2bfba | 98 | EVP_MD_free(sha256); |
8c6afbc5 RP |
99 | OPENSSL_free(pkey_der); |
100 | return ret; | |
101 | } | |
102 | ||
b4250010 | 103 | CTLOG_STORE *CTLOG_STORE_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
8c6afbc5 | 104 | { |
0dfd6cf9 RP |
105 | CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret)); |
106 | ||
e57036f2 | 107 | if (ret == NULL) { |
d4b2bfba | 108 | CTerr(0, ERR_R_MALLOC_FAILURE); |
e57036f2 F |
109 | return NULL; |
110 | } | |
0dfd6cf9 | 111 | |
d4b2bfba MC |
112 | ret->libctx = libctx; |
113 | if (propq != NULL) { | |
114 | ret->propq = OPENSSL_strdup(propq); | |
115 | if (ret->propq == NULL) { | |
116 | CTerr(0, ERR_R_MALLOC_FAILURE); | |
117 | goto err; | |
118 | } | |
119 | } | |
120 | ||
8c6afbc5 | 121 | ret->logs = sk_CTLOG_new_null(); |
d4b2bfba MC |
122 | if (ret->logs == NULL) { |
123 | CTerr(0, ERR_R_MALLOC_FAILURE); | |
8c6afbc5 | 124 | goto err; |
d4b2bfba | 125 | } |
0dfd6cf9 | 126 | |
8c6afbc5 RP |
127 | return ret; |
128 | err: | |
d4b2bfba | 129 | CTLOG_STORE_free(ret); |
8c6afbc5 RP |
130 | return NULL; |
131 | } | |
132 | ||
d4b2bfba MC |
133 | CTLOG_STORE *CTLOG_STORE_new(void) |
134 | { | |
d8652be0 | 135 | return CTLOG_STORE_new_ex(NULL, NULL); |
d4b2bfba MC |
136 | } |
137 | ||
8c6afbc5 RP |
138 | void CTLOG_STORE_free(CTLOG_STORE *store) |
139 | { | |
140 | if (store != NULL) { | |
d4b2bfba | 141 | OPENSSL_free(store->propq); |
8c6afbc5 RP |
142 | sk_CTLOG_pop_free(store->logs, CTLOG_free); |
143 | OPENSSL_free(store); | |
144 | } | |
145 | } | |
146 | ||
d4b2bfba MC |
147 | static int ctlog_new_from_conf(CTLOG_STORE *store, CTLOG **ct_log, |
148 | const CONF *conf, const char *section) | |
8c6afbc5 | 149 | { |
e57036f2 | 150 | const char *description = NCONF_get_string(conf, section, "description"); |
8c6afbc5 | 151 | char *pkey_base64; |
8c6afbc5 RP |
152 | |
153 | if (description == NULL) { | |
154 | CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION); | |
4aed8756 | 155 | return 0; |
8c6afbc5 RP |
156 | } |
157 | ||
158 | pkey_base64 = NCONF_get_string(conf, section, "key"); | |
8c6afbc5 RP |
159 | if (pkey_base64 == NULL) { |
160 | CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY); | |
4aed8756 | 161 | return 0; |
8c6afbc5 RP |
162 | } |
163 | ||
d8652be0 MC |
164 | return CTLOG_new_from_base64_ex(ct_log, pkey_base64, description, |
165 | store->libctx, store->propq); | |
8c6afbc5 RP |
166 | } |
167 | ||
168 | int CTLOG_STORE_load_default_file(CTLOG_STORE *store) | |
169 | { | |
5c39a55d | 170 | const char *fpath = ossl_safe_getenv(CTLOG_FILE_EVP); |
0dfd6cf9 | 171 | |
8c6afbc5 RP |
172 | if (fpath == NULL) |
173 | fpath = CTLOG_FILE; | |
0dfd6cf9 | 174 | |
8c6afbc5 RP |
175 | return CTLOG_STORE_load_file(store, fpath); |
176 | } | |
177 | ||
a930afb6 | 178 | /* |
4aed8756 F |
179 | * Called by CONF_parse_list, which stops if this returns <= 0, |
180 | * Otherwise, one bad log entry would stop loading of any of | |
181 | * the following log entries. | |
182 | * It may stop parsing and returns -1 on any internal (malloc) error. | |
a930afb6 RP |
183 | */ |
184 | static int ctlog_store_load_log(const char *log_name, int log_name_len, | |
185 | void *arg) | |
8c6afbc5 RP |
186 | { |
187 | CTLOG_STORE_LOAD_CTX *load_ctx = arg; | |
e57036f2 | 188 | CTLOG *ct_log = NULL; |
8c6afbc5 | 189 | /* log_name may not be null-terminated, so fix that before using it */ |
2508c047 | 190 | char *tmp; |
4aed8756 | 191 | int ret = 0; |
0dfd6cf9 | 192 | |
2508c047 RP |
193 | /* log_name will be NULL for empty list entries */ |
194 | if (log_name == NULL) | |
195 | return 1; | |
196 | ||
197 | tmp = OPENSSL_strndup(log_name, log_name_len); | |
e57036f2 F |
198 | if (tmp == NULL) |
199 | goto mem_err; | |
200 | ||
d4b2bfba | 201 | ret = ctlog_new_from_conf(load_ctx->log_store, &ct_log, load_ctx->conf, tmp); |
8c6afbc5 | 202 | OPENSSL_free(tmp); |
4aed8756 F |
203 | |
204 | if (ret < 0) { | |
205 | /* Propagate any internal error */ | |
206 | return ret; | |
207 | } | |
208 | if (ret == 0) { | |
a930afb6 RP |
209 | /* If we can't load this log, record that fact and skip it */ |
210 | ++load_ctx->invalid_log_entries; | |
211 | return 1; | |
212 | } | |
8c6afbc5 | 213 | |
68efafc5 | 214 | if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) { |
e57036f2 | 215 | goto mem_err; |
68efafc5 | 216 | } |
8c6afbc5 | 217 | return 1; |
e57036f2 F |
218 | |
219 | mem_err: | |
220 | CTLOG_free(ct_log); | |
221 | CTerr(CT_F_CTLOG_STORE_LOAD_LOG, ERR_R_MALLOC_FAILURE); | |
222 | return -1; | |
8c6afbc5 RP |
223 | } |
224 | ||
225 | int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file) | |
226 | { | |
a930afb6 | 227 | int ret = 0; |
8c6afbc5 | 228 | char *enabled_logs; |
0dfd6cf9 RP |
229 | CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new(); |
230 | ||
d3c3dfc5 | 231 | if (load_ctx == NULL) |
9be34ee5 | 232 | return 0; |
8c6afbc5 RP |
233 | load_ctx->log_store = store; |
234 | load_ctx->conf = NCONF_new(NULL); | |
235 | if (load_ctx->conf == NULL) | |
236 | goto end; | |
237 | ||
70073f3e | 238 | if (NCONF_load(load_ctx->conf, file, NULL) <= 0) { |
8c6afbc5 RP |
239 | CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID); |
240 | goto end; | |
241 | } | |
242 | ||
243 | enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs"); | |
0c6ea565 | 244 | if (enabled_logs == NULL) { |
0c6ea565 RP |
245 | CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID); |
246 | goto end; | |
247 | } | |
248 | ||
70073f3e RP |
249 | if (!CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx) || |
250 | load_ctx->invalid_log_entries > 0) { | |
a930afb6 RP |
251 | CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID); |
252 | goto end; | |
253 | } | |
8c6afbc5 | 254 | |
70073f3e | 255 | ret = 1; |
8c6afbc5 RP |
256 | end: |
257 | NCONF_free(load_ctx->conf); | |
0dfd6cf9 | 258 | ctlog_store_load_ctx_free(load_ctx); |
8c6afbc5 RP |
259 | return ret; |
260 | } | |
261 | ||
262 | /* | |
263 | * Initialize a new CTLOG object. | |
264 | * Takes ownership of the public key. | |
265 | * Copies the name. | |
266 | */ | |
b4250010 | 267 | CTLOG *CTLOG_new_ex(EVP_PKEY *public_key, const char *name, OSSL_LIB_CTX *libctx, |
d8652be0 | 268 | const char *propq) |
8c6afbc5 | 269 | { |
1ccbe6b3 | 270 | CTLOG *ret = OPENSSL_zalloc(sizeof(*ret)); |
0dfd6cf9 | 271 | |
1ccbe6b3 | 272 | if (ret == NULL) { |
d4b2bfba | 273 | CTerr(0, ERR_R_MALLOC_FAILURE); |
e57036f2 | 274 | return NULL; |
1ccbe6b3 | 275 | } |
0dfd6cf9 | 276 | |
d4b2bfba MC |
277 | ret->libctx = libctx; |
278 | if (propq != NULL) { | |
279 | ret->name = OPENSSL_strdup(propq); | |
280 | if (ret->propq == NULL) { | |
281 | CTerr(0, ERR_R_MALLOC_FAILURE); | |
282 | goto err; | |
283 | } | |
284 | } | |
285 | ||
8c6afbc5 | 286 | ret->name = OPENSSL_strdup(name); |
e57036f2 | 287 | if (ret->name == NULL) { |
d4b2bfba | 288 | CTerr(0, ERR_R_MALLOC_FAILURE); |
8c6afbc5 | 289 | goto err; |
e57036f2 | 290 | } |
0dfd6cf9 | 291 | |
d4b2bfba | 292 | if (ct_v1_log_id_from_pkey(ret, public_key) != 1) |
8c6afbc5 | 293 | goto err; |
0dfd6cf9 | 294 | |
986dbbbe | 295 | ret->public_key = public_key; |
8c6afbc5 RP |
296 | return ret; |
297 | err: | |
298 | CTLOG_free(ret); | |
299 | return NULL; | |
300 | } | |
301 | ||
d4b2bfba MC |
302 | CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name) |
303 | { | |
d8652be0 | 304 | return CTLOG_new_ex(public_key, name, NULL, NULL); |
d4b2bfba MC |
305 | } |
306 | ||
8c6afbc5 RP |
307 | /* Frees CT log and associated structures */ |
308 | void CTLOG_free(CTLOG *log) | |
309 | { | |
310 | if (log != NULL) { | |
311 | OPENSSL_free(log->name); | |
8c6afbc5 | 312 | EVP_PKEY_free(log->public_key); |
d4b2bfba | 313 | OPENSSL_free(log->propq); |
8c6afbc5 RP |
314 | OPENSSL_free(log); |
315 | } | |
316 | } | |
317 | ||
8c92c4ea | 318 | const char *CTLOG_get0_name(const CTLOG *log) |
8c6afbc5 RP |
319 | { |
320 | return log->name; | |
321 | } | |
322 | ||
8c92c4ea RP |
323 | void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id, |
324 | size_t *log_id_len) | |
8c6afbc5 RP |
325 | { |
326 | *log_id = log->log_id; | |
327 | *log_id_len = CT_V1_HASHLEN; | |
328 | } | |
329 | ||
8c92c4ea | 330 | EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log) |
8c6afbc5 RP |
331 | { |
332 | return log->public_key; | |
333 | } | |
334 | ||
335 | /* | |
336 | * Given a log ID, finds the matching log. | |
337 | * Returns NULL if no match found. | |
338 | */ | |
12d2d281 RP |
339 | const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store, |
340 | const uint8_t *log_id, | |
341 | size_t log_id_len) | |
8c6afbc5 RP |
342 | { |
343 | int i; | |
0dfd6cf9 | 344 | |
8c6afbc5 | 345 | for (i = 0; i < sk_CTLOG_num(store->logs); ++i) { |
12d2d281 | 346 | const CTLOG *log = sk_CTLOG_value(store->logs, i); |
8c6afbc5 RP |
347 | if (memcmp(log->log_id, log_id, log_id_len) == 0) |
348 | return log; | |
349 | } | |
0dfd6cf9 | 350 | |
8c6afbc5 RP |
351 | return NULL; |
352 | } |