2016-05-18 02:51:26 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2016-02-25 21:33:48 +08:00
|
|
|
*
|
2018-12-06 20:34:58 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2016-02-25 21:33:48 +08:00
|
|
|
*/
|
|
|
|
|
2016-03-01 04:25:08 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
#include <openssl/conf.h>
|
|
|
|
#include <openssl/ct.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/safestack.h>
|
|
|
|
|
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Information about a CT log server.
|
|
|
|
*/
|
|
|
|
struct ctlog_st {
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2020-04-03 23:25:18 +08:00
|
|
|
char *propq;
|
2016-02-25 21:33:48 +08:00
|
|
|
char *name;
|
|
|
|
uint8_t log_id[CT_V1_HASHLEN];
|
|
|
|
EVP_PKEY *public_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A store for multiple CTLOG instances.
|
|
|
|
* It takes ownership of any CTLOG instances added to it.
|
|
|
|
*/
|
|
|
|
struct ctlog_store_st {
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2020-04-03 23:25:18 +08:00
|
|
|
char *propq;
|
2016-02-25 21:33:48 +08:00
|
|
|
STACK_OF(CTLOG) *logs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The context when loading a CT log list from a CONF file. */
|
|
|
|
typedef struct ctlog_store_load_ctx_st {
|
|
|
|
CTLOG_STORE *log_store;
|
|
|
|
CONF *conf;
|
2016-03-04 00:06:59 +08:00
|
|
|
size_t invalid_log_entries;
|
2016-02-25 21:33:48 +08:00
|
|
|
} CTLOG_STORE_LOAD_CTX;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates an empty context for loading a CT log store.
|
|
|
|
* It should be populated before use.
|
|
|
|
*/
|
2018-06-21 03:41:05 +08:00
|
|
|
static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void);
|
2016-02-25 21:33:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Deletes a CT log store load context.
|
|
|
|
* Does not delete any of the fields.
|
|
|
|
*/
|
2016-03-01 04:25:08 +08:00
|
|
|
static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx);
|
2016-02-25 21:33:48 +08:00
|
|
|
|
2018-05-09 23:09:50 +08:00
|
|
|
static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(void)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-03-01 04:25:08 +08:00
|
|
|
CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
|
|
|
|
2016-06-28 03:58:32 +08:00
|
|
|
if (ctx == NULL)
|
2016-02-25 21:33:48 +08:00
|
|
|
CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2016-03-01 04:25:08 +08:00
|
|
|
static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Converts a log's public key into a SHA256 log ID */
|
2020-04-03 23:25:18 +08:00
|
|
|
static int ct_v1_log_id_from_pkey(CTLOG *log, EVP_PKEY *pkey)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned char *pkey_der = NULL;
|
|
|
|
int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der);
|
2020-01-09 11:14:13 +08:00
|
|
|
unsigned int len;
|
2020-04-03 23:25:18 +08:00
|
|
|
EVP_MD *sha256 = NULL;
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
if (pkey_der_len <= 0) {
|
|
|
|
CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID);
|
|
|
|
goto err;
|
|
|
|
}
|
2020-04-03 23:25:18 +08:00
|
|
|
sha256 = EVP_MD_fetch(log->libctx, "SHA2-256", log->propq);
|
|
|
|
if (sha256 == NULL) {
|
|
|
|
CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, ERR_LIB_EVP);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
ret = EVP_Digest(pkey_der, pkey_der_len, log->log_id, &len, sha256,
|
|
|
|
NULL);
|
2016-02-25 21:33:48 +08:00
|
|
|
err:
|
2020-04-03 23:25:18 +08:00
|
|
|
EVP_MD_free(sha256);
|
2016-02-25 21:33:48 +08:00
|
|
|
OPENSSL_free(pkey_der);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
CTLOG_STORE *CTLOG_STORE_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-03-01 04:25:08 +08:00
|
|
|
CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
|
|
|
|
|
2016-06-28 03:58:32 +08:00
|
|
|
if (ret == NULL) {
|
2020-04-03 23:25:18 +08:00
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
2016-06-28 03:58:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
ret->libctx = libctx;
|
|
|
|
if (propq != NULL) {
|
|
|
|
ret->propq = OPENSSL_strdup(propq);
|
|
|
|
if (ret->propq == NULL) {
|
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
ret->logs = sk_CTLOG_new_null();
|
2020-04-03 23:25:18 +08:00
|
|
|
if (ret->logs == NULL) {
|
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
2020-04-03 23:25:18 +08:00
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
return ret;
|
|
|
|
err:
|
2020-04-03 23:25:18 +08:00
|
|
|
CTLOG_STORE_free(ret);
|
2016-02-25 21:33:48 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
CTLOG_STORE *CTLOG_STORE_new(void)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return CTLOG_STORE_new_ex(NULL, NULL);
|
2020-04-03 23:25:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
void CTLOG_STORE_free(CTLOG_STORE *store)
|
|
|
|
{
|
|
|
|
if (store != NULL) {
|
2020-04-03 23:25:18 +08:00
|
|
|
OPENSSL_free(store->propq);
|
2016-02-25 21:33:48 +08:00
|
|
|
sk_CTLOG_pop_free(store->logs, CTLOG_free);
|
|
|
|
OPENSSL_free(store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
static int ctlog_new_from_conf(CTLOG_STORE *store, CTLOG **ct_log,
|
|
|
|
const CONF *conf, const char *section)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-06-28 03:58:32 +08:00
|
|
|
const char *description = NCONF_get_string(conf, section, "description");
|
2016-02-25 21:33:48 +08:00
|
|
|
char *pkey_base64;
|
|
|
|
|
|
|
|
if (description == NULL) {
|
|
|
|
CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);
|
2016-06-28 04:42:25 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pkey_base64 = NCONF_get_string(conf, section, "key");
|
|
|
|
if (pkey_base64 == NULL) {
|
|
|
|
CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY);
|
2016-06-28 04:42:25 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:42:23 +08:00
|
|
|
return CTLOG_new_from_base64_ex(ct_log, pkey_base64, description,
|
|
|
|
store->libctx, store->propq);
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
|
|
|
|
{
|
2018-09-24 09:21:18 +08:00
|
|
|
const char *fpath = ossl_safe_getenv(CTLOG_FILE_EVP);
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
if (fpath == NULL)
|
|
|
|
fpath = CTLOG_FILE;
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
return CTLOG_STORE_load_file(store, fpath);
|
|
|
|
}
|
|
|
|
|
2016-03-04 00:06:59 +08:00
|
|
|
/*
|
2016-06-28 04:42:25 +08:00
|
|
|
* Called by CONF_parse_list, which stops if this returns <= 0,
|
|
|
|
* Otherwise, one bad log entry would stop loading of any of
|
|
|
|
* the following log entries.
|
|
|
|
* It may stop parsing and returns -1 on any internal (malloc) error.
|
2016-03-04 00:06:59 +08:00
|
|
|
*/
|
|
|
|
static int ctlog_store_load_log(const char *log_name, int log_name_len,
|
|
|
|
void *arg)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
CTLOG_STORE_LOAD_CTX *load_ctx = arg;
|
2016-06-28 03:58:32 +08:00
|
|
|
CTLOG *ct_log = NULL;
|
2016-02-25 21:33:48 +08:00
|
|
|
/* log_name may not be null-terminated, so fix that before using it */
|
2016-03-04 00:08:55 +08:00
|
|
|
char *tmp;
|
2016-06-28 04:42:25 +08:00
|
|
|
int ret = 0;
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-03-04 00:08:55 +08:00
|
|
|
/* log_name will be NULL for empty list entries */
|
|
|
|
if (log_name == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
tmp = OPENSSL_strndup(log_name, log_name_len);
|
2016-06-28 03:58:32 +08:00
|
|
|
if (tmp == NULL)
|
|
|
|
goto mem_err;
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
ret = ctlog_new_from_conf(load_ctx->log_store, &ct_log, load_ctx->conf, tmp);
|
2016-02-25 21:33:48 +08:00
|
|
|
OPENSSL_free(tmp);
|
2016-06-28 04:42:25 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
/* Propagate any internal error */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
2016-03-04 00:06:59 +08:00
|
|
|
/* If we can't load this log, record that fact and skip it */
|
|
|
|
++load_ctx->invalid_log_entries;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-25 21:33:48 +08:00
|
|
|
|
2016-06-28 03:57:58 +08:00
|
|
|
if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) {
|
2016-06-28 03:58:32 +08:00
|
|
|
goto mem_err;
|
2016-06-28 03:57:58 +08:00
|
|
|
}
|
2016-02-25 21:33:48 +08:00
|
|
|
return 1;
|
2016-06-28 03:58:32 +08:00
|
|
|
|
|
|
|
mem_err:
|
|
|
|
CTLOG_free(ct_log);
|
|
|
|
CTerr(CT_F_CTLOG_STORE_LOAD_LOG, ERR_R_MALLOC_FAILURE);
|
|
|
|
return -1;
|
2016-02-25 21:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
|
|
|
|
{
|
2016-03-04 00:06:59 +08:00
|
|
|
int ret = 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
char *enabled_logs;
|
2016-03-01 04:25:08 +08:00
|
|
|
CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new();
|
|
|
|
|
2017-09-06 05:21:38 +08:00
|
|
|
if (load_ctx == NULL)
|
2017-09-18 04:52:13 +08:00
|
|
|
return 0;
|
2016-02-25 21:33:48 +08:00
|
|
|
load_ctx->log_store = store;
|
|
|
|
load_ctx->conf = NCONF_new(NULL);
|
|
|
|
if (load_ctx->conf == NULL)
|
|
|
|
goto end;
|
|
|
|
|
2016-03-08 02:38:06 +08:00
|
|
|
if (NCONF_load(load_ctx->conf, file, NULL) <= 0) {
|
2016-02-25 21:33:48 +08:00
|
|
|
CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs");
|
2016-03-04 00:08:01 +08:00
|
|
|
if (enabled_logs == NULL) {
|
|
|
|
CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-03-08 02:38:06 +08:00
|
|
|
if (!CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx) ||
|
|
|
|
load_ctx->invalid_log_entries > 0) {
|
2016-03-04 00:06:59 +08:00
|
|
|
CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
|
|
|
|
goto end;
|
|
|
|
}
|
2016-02-25 21:33:48 +08:00
|
|
|
|
2016-03-08 02:38:06 +08:00
|
|
|
ret = 1;
|
2016-02-25 21:33:48 +08:00
|
|
|
end:
|
|
|
|
NCONF_free(load_ctx->conf);
|
2016-03-01 04:25:08 +08:00
|
|
|
ctlog_store_load_ctx_free(load_ctx);
|
2016-02-25 21:33:48 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a new CTLOG object.
|
|
|
|
* Takes ownership of the public key.
|
|
|
|
* Copies the name.
|
|
|
|
*/
|
2020-10-15 17:55:50 +08:00
|
|
|
CTLOG *CTLOG_new_ex(EVP_PKEY *public_key, const char *name, OSSL_LIB_CTX *libctx,
|
2020-09-24 17:42:23 +08:00
|
|
|
const char *propq)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
2016-08-05 01:48:48 +08:00
|
|
|
CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-08-05 01:48:48 +08:00
|
|
|
if (ret == NULL) {
|
2020-04-03 23:25:18 +08:00
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
2016-06-28 03:58:32 +08:00
|
|
|
return NULL;
|
2016-08-05 01:48:48 +08:00
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
ret->libctx = libctx;
|
|
|
|
if (propq != NULL) {
|
|
|
|
ret->name = OPENSSL_strdup(propq);
|
|
|
|
if (ret->propq == NULL) {
|
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
ret->name = OPENSSL_strdup(name);
|
2016-06-28 03:58:32 +08:00
|
|
|
if (ret->name == NULL) {
|
2020-04-03 23:25:18 +08:00
|
|
|
CTerr(0, ERR_R_MALLOC_FAILURE);
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
2016-06-28 03:58:32 +08:00
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
if (ct_v1_log_id_from_pkey(ret, public_key) != 1)
|
2016-02-25 21:33:48 +08:00
|
|
|
goto err;
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-08-23 23:55:09 +08:00
|
|
|
ret->public_key = public_key;
|
2016-02-25 21:33:48 +08:00
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
CTLOG_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:25:18 +08:00
|
|
|
CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
|
|
|
|
{
|
2020-09-24 17:42:23 +08:00
|
|
|
return CTLOG_new_ex(public_key, name, NULL, NULL);
|
2020-04-03 23:25:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
/* Frees CT log and associated structures */
|
|
|
|
void CTLOG_free(CTLOG *log)
|
|
|
|
{
|
|
|
|
if (log != NULL) {
|
|
|
|
OPENSSL_free(log->name);
|
|
|
|
EVP_PKEY_free(log->public_key);
|
2020-04-03 23:25:18 +08:00
|
|
|
OPENSSL_free(log->propq);
|
2016-02-25 21:33:48 +08:00
|
|
|
OPENSSL_free(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 03:52:45 +08:00
|
|
|
const char *CTLOG_get0_name(const CTLOG *log)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
return log->name;
|
|
|
|
}
|
|
|
|
|
2016-03-05 03:52:45 +08:00
|
|
|
void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
|
|
|
|
size_t *log_id_len)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
*log_id = log->log_id;
|
|
|
|
*log_id_len = CT_V1_HASHLEN;
|
|
|
|
}
|
|
|
|
|
2016-03-05 03:52:45 +08:00
|
|
|
EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
return log->public_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a log ID, finds the matching log.
|
|
|
|
* Returns NULL if no match found.
|
|
|
|
*/
|
2016-03-09 02:55:55 +08:00
|
|
|
const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
|
|
|
|
const uint8_t *log_id,
|
|
|
|
size_t log_id_len)
|
2016-02-25 21:33:48 +08:00
|
|
|
{
|
|
|
|
int i;
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
|
2016-03-09 02:55:55 +08:00
|
|
|
const CTLOG *log = sk_CTLOG_value(store->logs, i);
|
2016-02-25 21:33:48 +08:00
|
|
|
if (memcmp(log->log_id, log_id, log_id_len) == 0)
|
|
|
|
return log;
|
|
|
|
}
|
2016-03-01 04:25:08 +08:00
|
|
|
|
2016-02-25 21:33:48 +08:00
|
|
|
return NULL;
|
|
|
|
}
|