CORE: add a provider argument to ossl_method_construct()
authorRichard Levitte <levitte@openssl.org>
Thu, 30 Sep 2021 07:32:57 +0000 (09:32 +0200)
committerRichard Levitte <levitte@openssl.org>
Wed, 27 Oct 2021 10:41:10 +0000 (12:41 +0200)
This makes it possible to limit the search of methods to that
particular provider.  This uses already available possibilities in
ossl_algorithm_do_all().

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/16725)

crypto/core_algorithm.c
crypto/core_fetch.c
crypto/encode_decode/decoder_meth.c
crypto/encode_decode/encoder_meth.c
crypto/evp/evp_fetch.c
crypto/store/store_meth.c
doc/internal/man3/ossl_method_construct.pod
include/internal/core.h

index 1a2e798c2c2d847413da99cb4a9ceb14afe69220..5ff33eff7c747b0945d1b009c727670246886bbd 100644 (file)
@@ -105,10 +105,23 @@ void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,
     cbdata.post = post;
     cbdata.data = data;
 
-    if (provider == NULL)
+    if (provider == NULL) {
         ossl_provider_doall_activated(libctx, algorithm_do_this, &cbdata);
-    else
+    } else {
+        OSSL_LIB_CTX *libctx2 = ossl_provider_libctx(provider);
+
+        /*
+         * If a provider is given, its library context MUST match the library
+         * context we're passed.  If this turns out not to be true, there is
+         * a programming error in the functions up the call stack.
+         */
+        if (!ossl_assert(ossl_lib_ctx_get_concrete(libctx)
+                         == ossl_lib_ctx_get_concrete(libctx2)))
+            return;
+
+        cbdata.libctx = libctx2;
         algorithm_do_this(provider, &cbdata);
+    }
 }
 
 char *ossl_algorithm_get1_first_name(const OSSL_ALGORITHM *algo)
index d315599ce67f7da35f4e07ee5b6e9a4d01d8e7f0..26eeaba3b7a210bee63d850b15425f798253bd75 100644 (file)
@@ -105,7 +105,7 @@ static void ossl_method_construct_this(OSSL_PROVIDER *provider,
 }
 
 void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
-                            int force_store,
+                            OSSL_PROVIDER *provider, int force_store,
                             OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data)
 {
     void *method = NULL;
@@ -117,7 +117,7 @@ void *ossl_method_construct(OSSL_LIB_CTX *libctx, int operation_id,
         cbdata.force_store = force_store;
         cbdata.mcm = mcm;
         cbdata.mcm_data = mcm_data;
-        ossl_algorithm_do_all(libctx, operation_id, NULL,
+        ossl_algorithm_do_all(libctx, operation_id, provider,
                               ossl_method_construct_precondition,
                               ossl_method_construct_this,
                               ossl_method_construct_postcondition,
index 8f0786c941db4a876cdb4d858b32af554165ee36..82515a14a33abfdf1839d68ad897cb7a2d4097a2 100644 (file)
@@ -380,7 +380,7 @@ inner_ossl_decoder_fetch(struct decoder_data_st *methdata, int id,
         methdata->propquery = properties;
         methdata->flag_construct_error_occurred = 0;
         if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_DECODER,
-                                            0 /* !force_cache */,
+                                            NULL, 0 /* !force_cache */,
                                             &mcm, methdata)) != NULL) {
             /*
              * If construction did create a method for us, we know that
index 9f7ecc82cbd4dda7da56404c5f8a265f400b505c..6526f5e35810048cd2ad5f4d0adf1a263381e133 100644 (file)
@@ -390,7 +390,7 @@ inner_ossl_encoder_fetch(struct encoder_data_st *methdata, int id,
         methdata->propquery = properties;
         methdata->flag_construct_error_occurred = 0;
         if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_ENCODER,
-                                            0 /* !force_cache */,
+                                            NULL, 0 /* !force_cache */,
                                             &mcm, methdata)) != NULL) {
             /*
              * If construction did create a method for us, we know that
index 5303cf8859ea63ed62ac2dda2687ff7aa37045ec..a0fa6590ae343e8a11d8081a62e55b0c1eba186b 100644 (file)
@@ -234,7 +234,8 @@ static void destruct_evp_method(void *method, void *data)
 }
 
 static void *
-inner_evp_generic_fetch(struct evp_method_data_st *methdata, int operation_id,
+inner_evp_generic_fetch(struct evp_method_data_st *methdata,
+                        OSSL_PROVIDER *prov, int operation_id,
                         int name_id, const char *name,
                         const char *properties,
                         void *(*new_method)(int name_id,
@@ -315,7 +316,7 @@ inner_evp_generic_fetch(struct evp_method_data_st *methdata, int operation_id,
         methdata->destruct_method = free_method;
         methdata->flag_construct_error_occurred = 0;
         if ((method = ossl_method_construct(methdata->libctx, operation_id,
-                                            0 /* !force_cache */,
+                                            prov, 0 /* !force_cache */,
                                             &mcm, methdata)) != NULL) {
             /*
              * If construction did create a method for us, we know that
@@ -366,8 +367,8 @@ void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
 
     methdata.libctx = libctx;
     methdata.tmp_store = NULL;
-    method = inner_evp_generic_fetch(&methdata,
-                                     operation_id, 0, name, properties,
+    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
+                                     0, name, properties,
                                      new_method, up_ref_method, free_method);
     dealloc_tmp_evp_method_store(methdata.tmp_store);
     return method;
@@ -393,8 +394,8 @@ void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
 
     methdata.libctx = libctx;
     methdata.tmp_store = NULL;
-    method = inner_evp_generic_fetch(&methdata,
-                                     operation_id, name_id, NULL, properties,
+    method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
+                                     name_id, NULL, properties,
                                      new_method, up_ref_method, free_method);
     dealloc_tmp_evp_method_store(methdata.tmp_store);
     return method;
@@ -588,7 +589,7 @@ void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
 
     methdata.libctx = libctx;
     methdata.tmp_store = NULL;
-    (void)inner_evp_generic_fetch(&methdata, operation_id, 0, NULL, NULL,
+    (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
                                   new_method, up_ref_method, free_method);
 
     data.operation_id = operation_id;
index 61230a6c241d878f27bfae7a974ca6b7a1cbcb56..d6b0389af99661af0d954649c471cc24b757db09 100644 (file)
@@ -322,7 +322,7 @@ inner_loader_fetch(struct loader_data_st *methdata, int id,
         methdata->propquery = properties;
         methdata->flag_construct_error_occurred = 0;
         if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
-                                            0 /* !force_cache */,
+                                            NULL, 0 /* !force_cache */,
                                             &mcm, methdata)) != NULL) {
             /*
              * If construction did create a method for us, we know that there
index 46a17ba7b6d2c297e7c2e854409ba32945213fdd..f30e5609dce388196ece471f0ed2ede5b09b15ca 100644 (file)
@@ -27,7 +27,7 @@ OSSL_METHOD_CONSTRUCT_METHOD, ossl_method_construct
  typedef struct ossl_method_construct_method OSSL_METHOD_CONSTRUCT_METHOD;
 
  void *ossl_method_construct(OSSL_LIB_CTX *ctx, int operation_id,
-                             int force_cache,
+                             OSSL_PROVIDER *prov, int force_cache,
                              OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data);
 
 
@@ -57,6 +57,9 @@ providers for a dispatch table given an I<operation_id>, and then
 calling the appropriate functions given by the subsystem specific
 method creator through I<mcm> and the data in I<mcm_data> (which is
 passed by ossl_method_construct()).
+If I<prov> is not NULL, only that provider is considered, which is
+useful in the case a method must be found in that particular
+provider.
 
 This function assumes that the subsystem method creator implements
 reference counting and acts accordingly (i.e. it will call the
index 035b7268942dbbf5b34753c5b8023be9a45d6d2e..277f2bdac56486d3a4d996bea7e627764dc087f9 100644 (file)
@@ -43,7 +43,7 @@ typedef struct ossl_method_construct_method_st {
 } OSSL_METHOD_CONSTRUCT_METHOD;
 
 void *ossl_method_construct(OSSL_LIB_CTX *ctx, int operation_id,
-                            int force_cache,
+                            OSSL_PROVIDER *prov, int force_cache,
                             OSSL_METHOD_CONSTRUCT_METHOD *mcm, void *mcm_data);
 
 void ossl_algorithm_do_all(OSSL_LIB_CTX *libctx, int operation_id,