Add checks for alloc failing.
[openssl.git] / crypto / ct / ct_log.c
index 6fc21b7269e3e44625d2d28567bc3997aa05a4d9..ec6fa384d52590787bd884ecc9def8de4e12804f 100644 (file)
@@ -58,15 +58,10 @@ static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new()
 {
     CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
 
-    if (ctx == NULL) {
+    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);
-    return NULL;
 }
 
 static void ctlog_store_load_ctx_free(CTLOG_STORE_LOAD_CTX* ctx)
@@ -98,8 +93,10 @@ CTLOG_STORE *CTLOG_STORE_new(void)
 {
     CTLOG_STORE *ret = OPENSSL_zalloc(sizeof(*ret));
 
-    if (ret == NULL)
-        goto err;
+    if (ret == NULL) {
+        CTerr(CT_F_CTLOG_STORE_NEW, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
 
     ret->logs = sk_CTLOG_new_null();
     if (ret->logs == NULL)
@@ -107,7 +104,7 @@ CTLOG_STORE *CTLOG_STORE_new(void)
 
     return ret;
 err:
-    CTLOG_STORE_free(ret);
+    OPENSSL_free(ret);
     return NULL;
 }
 
@@ -119,31 +116,23 @@ void CTLOG_STORE_free(CTLOG_STORE *store)
     }
 }
 
-static CTLOG *ctlog_new_from_conf(const CONF *conf, const char *section)
+static int ctlog_new_from_conf(CTLOG **ct_log, const CONF *conf, const char *section)
 {
-    CTLOG *ret = NULL;
-    char *description = NCONF_get_string(conf, section, "description");
+    const char *description = NCONF_get_string(conf, section, "description");
     char *pkey_base64;
 
     if (description == NULL) {
         CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);
-        goto end;
+        return 0;
     }
 
     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;
+        return 0;
     }
 
-    ret = CTLOG_new_from_base64(pkey_base64, description);
-    if (ret == NULL) {
-        CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_INVALID);
-        goto end;
-    }
-
-end:
-    return ret;
+    return CTLOG_new_from_base64(ct_log, pkey_base64, description);
 }
 
 int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
@@ -157,33 +146,50 @@ int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
 }
 
 /*
- * Called by CONF_parse_list, which stops if this returns <= 0, so don't unless
- * something very bad happens. Otherwise, one bad log entry would stop loading
- * of any of the following log entries.
+ * 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.
  */
 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;
+    CTLOG *ct_log = NULL;
     /* log_name may not be null-terminated, so fix that before using it */
     char *tmp;
+    int ret = 0;
 
     /* log_name will be NULL for empty list entries */
     if (log_name == NULL)
         return 1;
 
     tmp = OPENSSL_strndup(log_name, log_name_len);
-    ct_log = ctlog_new_from_conf(load_ctx->conf, tmp);
+    if (tmp == NULL)
+        goto mem_err;
+
+    ret = ctlog_new_from_conf(&ct_log, load_ctx->conf, tmp);
     OPENSSL_free(tmp);
-    if (ct_log == NULL) {
+
+    if (ret < 0) {
+        /* Propagate any internal error */
+        return ret;
+    }
+    if (ret == 0) {
         /* If we can't load this log, record that fact and skip it */
         ++load_ctx->invalid_log_entries;
         return 1;
     }
 
-    sk_CTLOG_push(load_ctx->log_store->logs, ct_log);
+    if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) {
+        goto mem_err;
+    }
     return 1;
+
+mem_err:
+    CTLOG_free(ct_log);
+    CTerr(CT_F_CTLOG_STORE_LOAD_LOG, ERR_R_MALLOC_FAILURE);
+    return -1;
 }
 
 int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
@@ -192,6 +198,8 @@ int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
     char *enabled_logs;
     CTLOG_STORE_LOAD_CTX* load_ctx = ctlog_store_load_ctx_new();
 
+    if (load_ctx == NULL)
+        goto end;
     load_ctx->log_store = store;
     load_ctx->conf = NCONF_new(NULL);
     if (load_ctx->conf == NULL)
@@ -228,35 +236,29 @@ end:
  */
 CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
 {
-    CTLOG *ret = CTLOG_new_null();
+    CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
 
-    if (ret == NULL)
-        goto err;
+    if (ret == NULL) {
+        CTerr(CT_F_CTLOG_NEW, ERR_R_MALLOC_FAILURE);
+        return NULL;
+    }
 
     ret->name = OPENSSL_strdup(name);
-    if (ret->name == NULL)
+    if (ret->name == NULL) {
+        CTerr(CT_F_CTLOG_NEW, ERR_R_MALLOC_FAILURE);
         goto err;
+    }
 
-    ret->public_key = public_key;
     if (ct_v1_log_id_from_pkey(public_key, ret->log_id) != 1)
         goto err;
 
+    ret->public_key = public_key;
     return ret;
 err:
     CTLOG_free(ret);
     return NULL;
 }
 
-CTLOG *CTLOG_new_null(void)
-{
-    CTLOG *ret = OPENSSL_zalloc(sizeof(*ret));
-
-    if (ret == NULL)
-        CTerr(CT_F_CTLOG_NEW_NULL, ERR_R_MALLOC_FAILURE);
-
-    return ret;
-}
-
 /* Frees CT log and associated structures */
 void CTLOG_free(CTLOG *log)
 {