]> git.ipfire.org Git - thirdparty/openssl.git/blobdiff - crypto/evp/evp_rand.c
Copyright year updates
[thirdparty/openssl.git] / crypto / evp / evp_rand.c
index 7b1a44241ecbe43322d0d36cd53cb7a0d1c234c9..ecfc876cda8617b23914c97ac360b1ad12b2d66e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -7,13 +7,9 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/evp.h>
-
 #include <stdio.h>
 #include <stdlib.h>
-#include <openssl/engine.h>
 #include <openssl/evp.h>
-#include <openssl/x509v3.h>
 #include <openssl/rand.h>
 #include <openssl/core.h>
 #include <openssl/core_names.h>
@@ -22,7 +18,6 @@
 #include "internal/numbers.h"
 #include "internal/provider.h"
 #include "internal/core.h"
-#include "crypto/asn1.h"
 #include "crypto/evp.h"
 #include "evp_local.h"
 
@@ -32,7 +27,6 @@ struct evp_rand_st {
     char *type_name;
     const char *description;
     CRYPTO_REF_COUNT refcnt;
-    CRYPTO_RWLOCK *refcnt_lock;
 
     const OSSL_DISPATCH *dispatch;
     OSSL_FUNC_rand_newctx_fn *newctx;
@@ -60,7 +54,7 @@ static int evp_rand_up_ref(void *vrand)
     int ref = 0;
 
     if (rand != NULL)
-        return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->refcnt_lock);
+        return CRYPTO_UP_REF(&rand->refcnt, &ref);
     return 1;
 }
 
@@ -71,12 +65,12 @@ static void evp_rand_free(void *vrand)
 
     if (rand == NULL)
         return;
-    CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->refcnt_lock);
+    CRYPTO_DOWN_REF(&rand->refcnt, &ref);
     if (ref > 0)
         return;
     OPENSSL_free(rand->type_name);
     ossl_provider_free(rand->prov);
-    CRYPTO_THREAD_lock_free(rand->refcnt_lock);
+    CRYPTO_FREE_REF(&rand->refcnt);
     OPENSSL_free(rand);
 }
 
@@ -84,12 +78,13 @@ static void *evp_rand_new(void)
 {
     EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
 
-    if (rand == NULL
-            || (rand->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
+    if (rand == NULL)
+        return NULL;
+
+    if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
         OPENSSL_free(rand);
         return NULL;
     }
-    rand->refcnt = 1;
     return rand;
 }
 
@@ -129,7 +124,7 @@ static void *evp_rand_from_algorithm(int name_id,
 #endif
 
     if ((rand = evp_rand_new()) == NULL) {
-        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
         return NULL;
     }
     rand->name_id = name_id;
@@ -310,7 +305,7 @@ const char *EVP_RAND_get0_description(const EVP_RAND *rand)
 
 int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
 {
-    return evp_is_a(rand->prov, rand->name_id, NULL, name);
+    return rand != NULL && evp_is_a(rand->prov, rand->name_id, NULL, name);
 }
 
 const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand)
@@ -325,11 +320,11 @@ int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[])
     return 1;
 }
 
-static int evp_rand_ctx_up_ref(EVP_RAND_CTX *ctx)
+int EVP_RAND_CTX_up_ref(EVP_RAND_CTX *ctx)
 {
     int ref = 0;
 
-    return CRYPTO_UP_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
+    return CRYPTO_UP_REF(&ctx->refcnt, &ref);
 }
 
 EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
@@ -344,15 +339,16 @@ EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
     }
 
     ctx = OPENSSL_zalloc(sizeof(*ctx));
-    if (ctx == NULL || (ctx->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
+    if (ctx == NULL)
+        return NULL;
+    if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
         OPENSSL_free(ctx);
-        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
     if (parent != NULL) {
-        if (!evp_rand_ctx_up_ref(parent)) {
+        if (!EVP_RAND_CTX_up_ref(parent)) {
             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
-            CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+            CRYPTO_FREE_REF(&ctx->refcnt);
             OPENSSL_free(ctx);
             return NULL;
         }
@@ -362,16 +358,15 @@ EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
     if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
                                     parent_dispatch)) == NULL
             || !EVP_RAND_up_ref(rand)) {
-        ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
+        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
         rand->freectx(ctx->algctx);
-        CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+        CRYPTO_FREE_REF(&ctx->refcnt);
         OPENSSL_free(ctx);
         EVP_RAND_CTX_free(parent);
         return NULL;
     }
     ctx->meth = rand;
     ctx->parent = parent;
-    ctx->refcnt = 1;
     return ctx;
 }
 
@@ -383,14 +378,14 @@ void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
     if (ctx == NULL)
         return;
 
-    CRYPTO_DOWN_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
+    CRYPTO_DOWN_REF(&ctx->refcnt, &ref);
     if (ref > 0)
         return;
     parent = ctx->parent;
     ctx->meth->freectx(ctx->algctx);
     ctx->algctx = NULL;
     EVP_RAND_free(ctx->meth);
-    CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
+    CRYPTO_FREE_REF(&ctx->refcnt);
     OPENSSL_free(ctx);
     EVP_RAND_CTX_free(parent);
 }