Use secure_getenv(3) when available.
[openssl.git] / crypto / ct / ct_log.c
index 1874d9121c52a7cdc155de5b452f7bd47f173565..c1bca3e1415e507b057085b9c39900f66e4b7d76 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -46,7 +46,7 @@ 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(void);
 
 /*
  * Deletes a CT log store load context.
@@ -54,7 +54,7 @@ static CTLOG_STORE_LOAD_CTX *ctlog_store_load_ctx_new();
  */
 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(void)
 {
     CTLOG_STORE_LOAD_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
 
@@ -137,7 +137,7 @@ static int ctlog_new_from_conf(CTLOG **ct_log, const CONF *conf, const char *sec
 
 int CTLOG_STORE_load_default_file(CTLOG_STORE *store)
 {
-    const char *fpath = getenv(CTLOG_FILE_EVP);
+    const char *fpath = ossl_safe_getenv(CTLOG_FILE_EVP);
 
     if (fpath == NULL)
       fpath = CTLOG_FILE;
@@ -198,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)
+        return 0;
     load_ctx->log_store = store;
     load_ctx->conf = NCONF_new(NULL);
     if (load_ctx->conf == NULL)
@@ -234,10 +236,12 @@ 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)
+    if (ret == NULL) {
+        CTerr(CT_F_CTLOG_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
+    }
 
     ret->name = OPENSSL_strdup(name);
     if (ret->name == NULL) {
@@ -245,26 +249,16 @@ CTLOG *CTLOG_new(EVP_PKEY *public_key, const char *name)
         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)
 {