Add checks on sk_TYPE_push() returned value
[openssl.git] / crypto / ct / ct_log.c
index f5a01fdbd329e385d5caf2af0eba9155cc2edd80..7298f1bfd44ee50a5b1bcacb97277938163bd60a 100644 (file)
@@ -1,56 +1,10 @@
-/* Author: Adam Eijdenberg <adam.eijdenberg@gmail.com>. */
-/* ====================================================================
- * Copyright (c) 1998-2016 The OpenSSL Project.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. All advertising materials mentioning features or use of this
- *    software must display the following acknowledgment:
- *    "This product includes software developed by the OpenSSL Project
- *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
- *
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
- *    endorse or promote products derived from this software without
- *    prior written permission. For written permission, please contact
- *    openssl-core@openssl.org.
- *
- * 5. Products derived from this software may not be called "OpenSSL"
- *    nor may "OpenSSL" appear in their names without prior written
- *    permission of the OpenSSL Project.
- *
- * 6. Redistributions of any form whatsoever must retain the following
- *    acknowledgment:
- *    "This product includes software developed by the OpenSSL Project
- *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
- *
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- * ====================================================================
- *
- * This product includes cryptographic software written by Eric Young
- * (eay@cryptsoft.com).  This product includes software written by Tim
- * Hudson (tjh@cryptsoft.com).
+/*
+ * Copyright 2016 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
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
  */
 
 #include <stdlib.h>
@@ -213,8 +167,13 @@ static int ctlog_store_load_log(const char *log_name, int log_name_len,
     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);
+    char *tmp;
+
+    /* 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);
     OPENSSL_free(tmp);
     if (ct_log == NULL) {
@@ -223,7 +182,10 @@ static int ctlog_store_load_log(const char *log_name, int log_name_len,
         return 1;
     }
 
-    sk_CTLOG_push(load_ctx->log_store->logs, ct_log);
+    if (!sk_CTLOG_push(load_ctx->log_store->logs, ct_log)) {
+        CTLOG_free(ct_log);
+        return -1;
+    }
     return 1;
 }
 
@@ -238,20 +200,24 @@ int CTLOG_STORE_load_file(CTLOG_STORE *store, const char *file)
     if (load_ctx->conf == NULL)
         goto end;
 
-    ret = NCONF_load(load_ctx->conf, file, NULL);
-    if (ret <= 0) {
+    if (NCONF_load(load_ctx->conf, file, NULL) <= 0) {
         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");
-    ret = CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx);
-    if (ret == 1 && load_ctx->invalid_log_entries > 0) {
-        ret = 0;
+    if (enabled_logs == NULL) {
         CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
         goto end;
     }
 
+    if (!CONF_parse_list(enabled_logs, ',', 1, ctlog_store_load_log, load_ctx) ||
+        load_ctx->invalid_log_entries > 0) {
+        CTerr(CT_F_CTLOG_STORE_LOAD_FILE, CT_R_LOG_CONF_INVALID);
+        goto end;
+    }
+
+    ret = 1;
 end:
     NCONF_free(load_ctx->conf);
     ctlog_store_load_ctx_free(load_ctx);
@@ -304,18 +270,19 @@ void CTLOG_free(CTLOG *log)
     }
 }
 
-const char *CTLOG_get0_name(CTLOG *log)
+const char *CTLOG_get0_name(const CTLOG *log)
 {
     return log->name;
 }
 
-void CTLOG_get0_log_id(CTLOG *log, uint8_t **log_id, size_t *log_id_len)
+void CTLOG_get0_log_id(const CTLOG *log, const uint8_t **log_id,
+                       size_t *log_id_len)
 {
     *log_id = log->log_id;
     *log_id_len = CT_V1_HASHLEN;
 }
 
-EVP_PKEY *CTLOG_get0_public_key(CTLOG *log)
+EVP_PKEY *CTLOG_get0_public_key(const CTLOG *log)
 {
     return log->public_key;
 }
@@ -324,14 +291,14 @@ EVP_PKEY *CTLOG_get0_public_key(CTLOG *log)
  * Given a log ID, finds the matching log.
  * Returns NULL if no match found.
  */
-CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
-                                  const uint8_t *log_id,
-                                  size_t log_id_len)
+const CTLOG *CTLOG_STORE_get0_log_by_id(const CTLOG_STORE *store,
+                                        const uint8_t *log_id,
+                                        size_t log_id_len)
 {
     int i;
 
     for (i = 0; i < sk_CTLOG_num(store->logs); ++i) {
-        CTLOG *log = sk_CTLOG_value(store->logs, i);
+        const CTLOG *log = sk_CTLOG_value(store->logs, i);
         if (memcmp(log->log_id, log_id, log_id_len) == 0)
             return log;
     }