key zeroisation fix for p12
[openssl.git] / crypto / async / async_wait.c
index 50c150e8c5adc8b6d2f7e114f716b278b1ae620d..b23e43e8c8624aa5a40df5bf9937dcfdef62d48f 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
@@ -47,9 +47,10 @@ int ASYNC_WAIT_CTX_set_wait_fd(ASYNC_WAIT_CTX *ctx, const void *key,
 {
     struct fd_lookup_st *fdlookup;
 
-    fdlookup = OPENSSL_zalloc(sizeof *fdlookup);
-    if (fdlookup == NULL)
+    if ((fdlookup = OPENSSL_zalloc(sizeof(*fdlookup))) == NULL) {
+        ASYNCerr(ASYNC_F_ASYNC_WAIT_CTX_SET_WAIT_FD, ERR_R_MALLOC_FAILURE);
         return 0;
+    }
 
     fdlookup->key = key;
     fdlookup->fd = fd;
@@ -138,12 +139,14 @@ int ASYNC_WAIT_CTX_get_changed_fds(ASYNC_WAIT_CTX *ctx, OSSL_ASYNC_FD *addfd,
 
 int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
 {
-    struct fd_lookup_st *curr;
+    struct fd_lookup_st *curr, *prev;
 
     curr = ctx->fds;
+    prev = NULL;
     while (curr != NULL) {
-        if (curr->del) {
+        if (curr->del == 1) {
             /* This one has been marked deleted already so do nothing */
+            prev = curr;
             curr = curr->next;
             continue;
         }
@@ -152,24 +155,13 @@ int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
             if (curr->add == 1) {
                 if (ctx->fds == curr) {
                     ctx->fds = curr->next;
-                }
-                else {
-                    struct fd_lookup_st *prev = ctx->fds;
-                    while (prev->next != curr && prev->next != NULL) {
-                        prev = prev->next;
-                    }
-                    if (prev->next == NULL) {
-                        return 1;
-                    }
+                } else {
                     prev->next = curr->next;
                 }
 
-                /*
-                 * The fd has just been added so it can't be used externally
-                 * and it is safe to call the cleanup function here
+                /* It is responsibility of the caller to cleanup before calling
+                 * ASYNC_WAIT_CTX_clear_fd
                  */
-                if (curr->cleanup != NULL)
-                    curr->cleanup(ctx, curr->key, curr->fd, curr->custom_data);
                 OPENSSL_free(curr);
                 ctx->numadd--;
                 return 1;
@@ -184,6 +176,7 @@ int ASYNC_WAIT_CTX_clear_fd(ASYNC_WAIT_CTX *ctx, const void *key)
             ctx->numdel++;
             return 1;
         }
+        prev = curr;
         curr = curr->next;
     }
     return 0;