Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ct/ct_log.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
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
8
 */
9
10
#include <stdlib.h>
11
#include <string.h>
12
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 {
25
    char *name;
26
    uint8_t log_id[CT_V1_HASHLEN];
27
    EVP_PKEY *public_key;
28
};
29
30
/*
31
 * A store for multiple CTLOG instances.
32
 * It takes ownership of any CTLOG instances added to it.
33
 */
34
struct ctlog_store_st {
35
    STACK_OF(CTLOG) *logs;
36
};
37
38
/* The context when loading a CT log list from a CONF file. */
39
typedef struct ctlog_store_load_ctx_st {
40
    CTLOG_STORE *log_store;
41
    CONF *conf;
42
    size_t invalid_log_entries;
43
} CTLOG_STORE_LOAD_CTX;
44
45
/*
46
 * Creates an empty context for loading a CT log store.
47
 * It should be populated before use.
48
 */
49
static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void);
50
51
/*
52
 * Deletes a CT log store load context.
53
 * Does not delete any of the fields.
54
 */
55
static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx);
56
57
static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void)
58
0
{
59
0
    CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
60
0
61
0
    if (ctx == NULL)
62
0
        CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE);
63
0
64
0
    return ctx;
65
0
}
66
67
static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx)
68
0
{
69
0
    OPENSSL_free(ctx);
70
0
}
71
72
/* Converts a log's public key into a SHA256 log ID */
73
static int ct_v1_log_id_from_pkey(EVP_PKEY *pkey,
74
                                  unsigned char log_id[CT_V1_HASHLEN])
75
0
{
76
0
    int ret = 0;
77
0
    unsigned char *pkey_der = NULL;
78
0
    int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der);
79
0
80
0
    if (pkey_der_len <= 0) {
81
0
        CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID);
82
0
        goto err;
83
0
    }
84
0
85
0
    SHA256(pkey_der, pkey_der_len, log_id);
86
0
    ret = 1;
87
0
err:
88
0
    OPENSSL_free(pkey_der);
89
0
    return ret;
90
0
}
91
92
CTLOG_STORE *CTLOG_STORE_new(void)
93
0
{
94
0
    CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
95
0
96
0
    if (ret == NULL) {
97
0
        CTerr(CT_F_CTLOG_STORE_NEW, ERR_R_MALLOC_FAILURE);
98
0
        return NULL;
99
0
    }
100
0
101
0
    ret->logs = sk_CTLOG_new_null();
102
0
    if (ret->logs == NULL)
103
0
        goto err;
104
0
105
0
    return ret;
106
0
err:
107
0
    OPENSSL_free(ret);
108
0
    return NULL;
109
0
}
110
111
void CTLOG_STORE_free(CTLOG_STORE *store)
112
0
{
113
0
    if (store != NULL) {
114
0
        sk_CTLOG_pop_free(store->logs, CTLOG_free);
115
0
        OPENSSL_free(store);
116
0
    }
117
0
}
118
119
static int ctlog_new_from_conf(CTLOG **ct_log, const CONF *conf, const char *section)
120
0
{
121
0
    const char *description = NCONF_get_string(conf, section, "description");
122
0
    char *pkey_base64;
123
0
124
0
    if (description == NULL) {
125
0
        CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);
126
0
        return 0;
127
0
    }
128
0
129
0
    pkey_base64 = NCONF_get_string(conf, section, "key");
130
0
    if (pkey_base64 == NULL) {
131
0
        CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY);
132
0
        return 0;
133
0
    }
134
0
135
0
    return CTLOG_new_from_base64(ct_log, pkey_base64, description);
136
0
}
137
138
int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
139
0
{
140
0
    const char *fpath = getenv(CTLOG_FILE_EVP);
141
0
142
0
    if (fpath == NULL)
143
0
      fpath = CTLOG_FILE;
144
0
145
0
    return CTLOG_STORE_load_file(store, fpath);
146
0
}
147
148
/*
149
 * Called by CONF_parse_list, which stops if this returns <= 0,
150
 * Otherwise, one bad log entry would stop loading of any of
151
 * the following log entries.
152
 * It may stop parsing and returns -1 on any internal (malloc) error.
153
 */
154
static int ctlog_store_load_log(const char *log_name, int log_name_len,
155
                                void *arg)
156
0
{
157
0
    CTLOG_STORE_LOAD_CTX *load_ctx = arg;
158
0
    CTLOG *ct_log = NULL;
159
0
    /* log_name may not be null-terminated, so fix that before using it */
160
0
    char *tmp;
161
0
    int ret = 0;
162
0
163
0
    /* log_name will be NULL for empty list entries */
164
0
    if (log_name == NULL)
165
0
        return 1;
166
0
167
0
    tmp = OPENSSL_strndup(log_name, log_name_len);
168
0
    if (tmp == NULL)
169
0
        goto mem_err;
170
0
171
0
    ret = ctlog_new_from_conf(&ct_log, load_ctx->conf, tmp);
172
0
    OPENSSL_free(tmp);
173
0
174
0
    if (ret < 0) {
175
0
        /* Propagate any internal error */
176
0
        return ret;
177
0
    }
178
0
    if (ret == 0) {
179
0
        /* If we can't load this log, record that fact and skip it */
180
0
        ++load_ctx->invalid_log_entries;
181
0
        return 1;
182
0
    }
183
0
184
0
    if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) {
185
0
        goto mem_err;
186
0
    }
187
0
    return 1;
188
0
189
0
mem_err:
190
0
    CTLOG_free(ct_log);
191
0
    CTerr(CT_F_CTLOG_STORE_LOAD_LOG, ERR_R_MALLOC_FAILURE);
192
0
    return -1;
193
0
}
194
195
int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
196
0
{
197
0
    int ret = 0;
198
0
    char *enabled_logs;
199
0
    CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new();
200
0
201
0
    if (load_ctx == NULL)
202
0
        return 0;
203
0
    load_ctx->log_store = store;
204
0
    load_ctx->conf = NCONF_new(NULL);
205
0
    if (load_ctx->conf == NULL)
206
0
        goto end;
207
0
208
0
    if (NCONF_load(load_ctx->conf, file, NULL) <= 0) {
209
0
        CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
210
0
        goto end;
211
0
    }
212
0
213
0
    enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs");
214
0
    if (enabled_logs == NULL) {
215
0
        CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
216
0
        goto end;
217
0
    }
218
0
219
0
    if (!CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx) ||
220
0
        load_ctx->invalid_log_entries > 0) {
221
0
        CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
222
0
        goto end;
223
0
    }
224
0
225
0
    ret = 1;
226
0
end:
227
0
    NCONF_free(load_ctx->conf);
228
0
    ctlog_store_load_ctx_free(load_ctx);
229
0
    return ret;
230
0
}
231
232
/*
233
 * Initialize a new CTLOG object.
234
 * Takes ownership of the public key.
235
 * Copies the name.
236
 */
237
CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
238
0
{
239
0
    CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
240
0
241
0
    if (ret == NULL) {
242
0
        CTerr(CT_F_CTLOG_NEW, ERR_R_MALLOC_FAILURE);
243
0
        return NULL;
244
0
    }
245
0
246
0
    ret->name = OPENSSL_strdup(name);
247
0
    if (ret->name == NULL) {
248
0
        CTerr(CT_F_CTLOG_NEW, ERR_R_MALLOC_FAILURE);
249
0
        goto err;
250
0
    }
251
0
252
0
    if (ct_v1_log_id_from_pkey(public_key, ret->log_id) != 1)
253
0
        goto err;
254
0
255
0
    ret->public_key = public_key;
256
0
    return ret;
257
0
err:
258
0
    CTLOG_free(ret);
259
0
    return NULL;
260
0
}
261
262
/* Frees CT log and associated structures */
263
void CTLOG_free(CTLOG *log)
264
0
{
265
0
    if (log != NULL) {
266
0
        OPENSSL_free(log->name);
267
0
        EVP_PKEY_free(log->public_key);
268
0
        OPENSSL_free(log);
269
0
    }
270
0
}
271
272
const char *CTLOG_get0_name(const CTLOG *log)
273
0
{
274
0
    return log->name;
275
0
}
276
277
void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
278
                       size_t *log_id_len)
279
0
{
280
0
    *log_id = log->log_id;
281
0
    *log_id_len = CT_V1_HASHLEN;
282
0
}
283
284
EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log)
285
0
{
286
0
    return log->public_key;
287
0
}
288
289
/*
290
 * Given a log ID, finds the matching log.
291
 * Returns NULL if no match found.
292
 */
293
const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
294
                                        const uint8_t *log_id,
295
                                        size_t log_id_len)
296
0
{
297
0
    int i;
298
0
299
0
    for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
300
0
        const CTLOG *log = sk_CTLOG_value(store->logs, i);
301
0
        if (memcmp(log->log_id, log_id, log_id_len) == 0)
302
0
            return log;
303
0
    }
304
0
305
0
    return NULL;
306
0
}