diff options
author | Rob Percival <robpercival@google.com> | 2016-02-29 20:25:08 +0000 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2016-03-01 11:59:28 -0500 |
commit | 0dfd6cf901d34b5774fa406c44fcfbe9e3ef6d5e (patch) | |
tree | 3a3cfeb521f93f8f6226558d65ed297c14147794 /crypto/ct/ct_log.c | |
parent | e9fd74ac96c27cbee33d4b91a2caa0017c304975 (diff) |
Addresses review comments from richsalz
Reviewed-by: Ben Laurie <ben@openssl.org>
Reviewed-by: Rich Salz <rsalz@openssl.org>
Diffstat (limited to 'crypto/ct/ct_log.c')
-rw-r--r-- | crypto/ct/ct_log.c | 85 |
1 files changed, 40 insertions, 45 deletions
diff --git a/crypto/ct/ct_log.c b/crypto/ct/ct_log.c index aa3afc4ce3..14f3bcc1fd 100644 --- a/crypto/ct/ct_log.c +++ b/crypto/ct/ct_log.c @@ -53,6 +53,9 @@ * */ +#include <stdlib.h> +#include <string.h> + #include <openssl/conf.h> #include <openssl/ct.h> #include <openssl/err.h> @@ -88,48 +91,47 @@ typedef struct ctlog_store_load_ctx_st { * Creates an empty context for loading a CT log store. * It should be populated before use. */ -static CTLOG_STORE_LOAD_CTX *CTLOG_STORE_LOAD_CTX_new(); +static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new(); /* * Deletes a CT log store load context. * Does not delete any of the fields. */ -static void CTLOG_STORE_LOAD_CTX_free(CTLOG_STORE_LOAD_CTX* ctx); +static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx); -static CTLOG_STORE_LOAD_CTX *CTLOG_STORE_LOAD_CTX_new() +static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new() { - CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(CTLOG_STORE_LOAD_CTX)); + CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); + if (ctx == NULL) { CTerr(CT_F_CTLOG_STORE_LOAD_CTX_NEW, ERR_R_MALLOC_FAILURE); goto err; } return ctx; - err: - CTLOG_STORE_LOAD_CTX_free(ctx); + ctlog_store_load_ctx_free(ctx); return NULL; } -static void CTLOG_STORE_LOAD_CTX_free(CTLOG_STORE_LOAD_CTX* ctx) +static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx) { - if (ctx == NULL) - return; - OPENSSL_free(ctx); } /* Converts a log's public key into a SHA256 log ID */ -static int CT_v1_log_id_from_pkey(EVP_PKEY *pkey, +static int ct_v1_log_id_from_pkey(EVP_PKEY *pkey, unsigned char log_id[CT_V1_HASHLEN]) { int ret = 0; unsigned char *pkey_der = NULL; int pkey_der_len = i2d_PUBKEY(pkey, &pkey_der); + if (pkey_der_len <= 0) { CTerr(CT_F_CT_V1_LOG_ID_FROM_PKEY, CT_R_LOG_KEY_INVALID); goto err; } + SHA256(pkey_der, pkey_der_len, log_id); ret = 1; err: @@ -139,12 +141,15 @@ err: CTLOG_STORE *CTLOG_STORE_new(void) { - CTLOG_STORE *ret = OPENSSL_malloc(sizeof(CTLOG_STORE)); + CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret)); + if (ret == NULL) goto err; + ret->logs = sk_CTLOG_new_null(); if (ret->logs == NULL) goto err; + return ret; err: CTLOG_STORE_free(ret); @@ -159,17 +164,11 @@ void CTLOG_STORE_free(CTLOG_STORE *store) } } -static CTLOG *CTLOG_new_from_conf(const CONF *conf, const char *section) +static CTLOG *ctlog_new_from_conf(const CONF *conf, const char *section) { CTLOG *ret = NULL; - char *description; + char *description = NCONF_get_string(conf, section, "description"); char *pkey_base64; - if (conf == NULL || section == NULL) { - CTerr(CT_F_CTLOG_NEW_FROM_CONF, ERR_R_PASSED_NULL_PARAMETER); - goto end; - } - - description = NCONF_get_string(conf, section, "description"); if (description == NULL) { CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION); @@ -177,7 +176,6 @@ static CTLOG *CTLOG_new_from_conf(const CONF *conf, const char *section) } 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); goto end; @@ -195,20 +193,22 @@ end: int CTLOG_STORE_load_default_file(CTLOG_STORE *store) { - char *fpath = (char *)getenv(CTLOG_FILE_EVP); + const char *fpath = getenv(CTLOG_FILE_EVP); + if (fpath == NULL) fpath = CTLOG_FILE; + return CTLOG_STORE_load_file(store, fpath); } -static int CTLOG_STORE_load_log(const char *log_name, int log_name_len, void *arg) +static int ctlog_store_load_log(const char *log_name, int log_name_len, void *arg) { CTLOG_STORE_LOAD_CTX *load_ctx = arg; CTLOG *ct_log; - /* log_name may not be null-terminated, so fix that before using it */ char *tmp = OPENSSL_strndup(log_name, log_name_len); - ct_log = CTLOG_new_from_conf(load_ctx->conf, tmp); + + ct_log = ctlog_new_from_conf(load_ctx->conf, tmp); OPENSSL_free(tmp); if (ct_log == NULL) return 0; @@ -221,7 +221,8 @@ int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file) { int ret = -1; char *enabled_logs; - CTLOG_STORE_LOAD_CTX* load_ctx = CTLOG_STORE_LOAD_CTX_new(); + CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new(); + load_ctx->log_store = store; load_ctx->conf = NCONF_new(NULL); if (load_ctx->conf == NULL) @@ -234,11 +235,11 @@ int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file) } enabled_logs = NCONF_get_string(load_ctx->conf, NULL, "enabled_logs"); - CONF_parse_list(enabled_logs, ',', 1, CTLOG_STORE_load_log, load_ctx); + CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx); end: NCONF_free(load_ctx->conf); - CTLOG_STORE_LOAD_CTX_free(load_ctx); + ctlog_store_load_ctx_free(load_ctx); return ret; } @@ -249,20 +250,19 @@ end: */ CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name) { - CTLOG *ret = NULL; - if (public_key == NULL || name == NULL) { - CTerr(CT_F_CTLOG_NEW, ERR_R_PASSED_NULL_PARAMETER); - goto err; - } - ret = CTLOG_new_null(); + CTLOG *ret = CTLOG_new_null(); + if (ret == NULL) goto err; + ret->name = OPENSSL_strdup(name); if (ret->name == NULL) goto err; + ret->public_key = public_key; - if (CT_v1_log_id_from_pkey(public_key, ret->log_id) != 1) + if (ct_v1_log_id_from_pkey(public_key, ret->log_id) != 1) goto err; + return ret; err: CTLOG_free(ret); @@ -271,9 +271,11 @@ err: CTLOG *CTLOG_new_null(void) { - CTLOG *ret = OPENSSL_zalloc(sizeof(CTLOG)); + CTLOG *ret = OPENSSL_zalloc(sizeof(*ret)); + if (ret == NULL) CTerr(CT_F_CTLOG_NEW_NULL, ERR_R_MALLOC_FAILURE); + return ret; } @@ -282,11 +284,7 @@ void CTLOG_free(CTLOG *log) { if (log != NULL) { OPENSSL_free(log->name); - log->name = NULL; - EVP_PKEY_free(log->public_key); - log->public_key = NULL; - OPENSSL_free(log); } } @@ -316,15 +314,12 @@ CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store, size_t log_id_len) { int i; - if (store == NULL) { - CTerr(CT_F_CTLOG_STORE_GET0_LOG_BY_ID, ERR_R_PASSED_NULL_PARAMETER); - goto end; - } + for (i = 0; i < sk_CTLOG_num(store->logs); ++i) { CTLOG *log = sk_CTLOG_value(store->logs, i); if (memcmp(log->log_id, log_id, log_id_len) == 0) return log; } -end: + return NULL; } |