CORE: Add an internal function to distinguish the global default context
authorRichard Levitte <levitte@openssl.org>
Tue, 23 Jun 2020 08:09:20 +0000 (10:09 +0200)
committerRichard Levitte <levitte@openssl.org>
Sun, 28 Jun 2020 08:55:52 +0000 (10:55 +0200)
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12228)

crypto/context.c
crypto/evp/evp_fetch.c
include/internal/cryptlib.h

index 615c55c8c299a660c64fcd79166b66994691158c..de289fcb68ef810ecaa256b5fcc1e1769bae578e 100644 (file)
@@ -214,6 +214,15 @@ int openssl_ctx_is_default(OPENSSL_CTX *ctx)
     return 0;
 }
 
     return 0;
 }
 
+int openssl_ctx_is_global_default(OPENSSL_CTX *ctx)
+{
+#ifndef FIPS_MODULE
+    if (openssl_ctx_get_concrete(ctx) == &default_context_int)
+        return 1;
+#endif
+    return 0;
+}
+
 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
                                     CRYPTO_EX_DATA *ad, int index,
                                     long argl_ign, void *argp)
 static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
                                     CRYPTO_EX_DATA *ad, int index,
                                     long argl_ign, void *argp)
index f63e135d3ab3bcb190a56d7f2a0fa3d80964938e..5cb59d98fc58f8838bebc267726d0f517aef8bda 100644 (file)
@@ -286,6 +286,17 @@ inner_evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
     return method;
 }
 
     return method;
 }
 
+#ifndef FIPS_MODULE
+static const char *libctx_descriptor(OPENSSL_CTX *libctx)
+{
+    if (openssl_ctx_is_global_default(libctx))
+        return "Global default library context";
+    if (openssl_ctx_is_default(libctx))
+        return "Thread-local default library context";
+    return "Non-default library context";
+}
+#endif
+
 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
                         const char *name, const char *properties,
                         void *(*new_method)(int name_id,
 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
                         const char *name, const char *properties,
                         void *(*new_method)(int name_id,
@@ -306,9 +317,7 @@ void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
 #else
         ERR_raise_data(ERR_LIB_EVP, code,
                        "%s, Algorithm (%s), Properties (%s)",
 #else
         ERR_raise_data(ERR_LIB_EVP, code,
                        "%s, Algorithm (%s), Properties (%s)",
-                       (openssl_ctx_is_default(libctx)
-                        ? "Default library context"
-                        : "Non-default library context"),
+                       libctx_descriptor(libctx),
                        name = NULL ? "<null>" : name,
                        properties == NULL ? "<null>" : properties);
 #endif
                        name = NULL ? "<null>" : name,
                        properties == NULL ? "<null>" : properties);
 #endif
@@ -350,9 +359,7 @@ void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
 
             ERR_raise_data(ERR_LIB_EVP, code,
                            "%s, Algorithm (%s), Properties (%s)",
 
             ERR_raise_data(ERR_LIB_EVP, code,
                            "%s, Algorithm (%s), Properties (%s)",
-                           (openssl_ctx_is_default(libctx)
-                            ? "Default library context"
-                            : "Non-default library context"),
+                           libctx_descriptor(libctx),
                            name = NULL ? "<null>" : name,
                            properties == NULL ? "<null>" : properties);
         }
                            name = NULL ? "<null>" : name,
                            properties == NULL ? "<null>" : properties);
         }
index 5118bfbe575b14bb21b5aa37bc0a1d481414ab34..fba1d5643fead9723c0beaa64dc1e9de9a1344b7 100644 (file)
@@ -168,6 +168,7 @@ typedef struct openssl_ctx_method {
 
 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
 int openssl_ctx_is_default(OPENSSL_CTX *ctx);
 
 OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
 int openssl_ctx_is_default(OPENSSL_CTX *ctx);
+int openssl_ctx_is_global_default(OPENSSL_CTX *ctx);
 
 /* Functions to retrieve pointers to data by index */
 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */,
 
 /* Functions to retrieve pointers to data by index */
 void *openssl_ctx_get_data(OPENSSL_CTX *, int /* index */,