]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
LEGACY PROV: Reimplement the ERR building blocks in upcall terms
authorRichard Levitte <levitte@openssl.org>
Tue, 11 Jan 2022 17:30:20 +0000 (18:30 +0100)
committerRichard Levitte <levitte@openssl.org>
Fri, 21 Jan 2022 13:44:16 +0000 (14:44 +0100)
This involves the following functions:

ERR_new(), ERR_set_debug(), ERR_set_error(), ERR_vset_error(),
ERR_set_mark(), ERR_clear_last_mark(), ERR_pop_to_mark(void)

Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17474)

providers/legacyprov.c

index 93c4223a15c01c794a4faf056062818cbbe080ec..ad5d05d7394a30b1d3bbd7a90fe545ef1fcdbccf 100644 (file)
@@ -12,6 +12,7 @@
 #include <openssl/core.h>
 #include <openssl/core_dispatch.h>
 #include <openssl/core_names.h>
+#include <openssl/err.h>
 #include <openssl/params.h>
 #include "prov/provider_ctx.h"
 #include "prov/implementations.h"
@@ -33,6 +34,22 @@ OSSL_provider_init_fn ossl_legacy_provider_init;
 # define OSSL_provider_init ossl_legacy_provider_init
 #endif
 
+#ifndef STATIC_LEGACY
+/*
+ * Should these function pointers be stored in the provider side provctx?
+ * Could they ever be different from one init to the next? We assume not for
+ * now.
+ */
+
+/* Functions provided by the core */
+static OSSL_FUNC_core_new_error_fn *c_new_error;
+static OSSL_FUNC_core_set_error_debug_fn *c_set_error_debug;
+static OSSL_FUNC_core_vset_error_fn *c_vset_error;
+static OSSL_FUNC_core_set_error_mark_fn *c_set_error_mark;
+static OSSL_FUNC_core_clear_last_error_mark_fn *c_clear_last_error_mark;
+static OSSL_FUNC_core_pop_error_to_mark_fn *c_pop_error_to_mark;
+#endif
+
 /* Parameters we provide to the core */
 static const OSSL_PARAM legacy_param_types[] = {
     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
@@ -185,6 +202,41 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
                        void **provctx)
 {
     OSSL_LIB_CTX *libctx = NULL;
+#ifndef STATIC_LEGACY
+    const OSSL_DISPATCH *tmp;
+#endif
+
+#ifndef STATIC_LEGACY
+    for (tmp = in; tmp->function_id != 0; tmp++) {
+        /*
+         * We do not support the scenario of an application linked against
+         * multiple versions of libcrypto (e.g. one static and one dynamic),
+         * but sharing a single legacy.so. We do a simple sanity check here.
+         */
+#define set_func(c, f) if (c == NULL) c = f; else if (c != f) return 0;
+        switch (tmp->function_id) {
+        case OSSL_FUNC_CORE_NEW_ERROR:
+            set_func(c_new_error, OSSL_FUNC_core_new_error(tmp));
+            break;
+        case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
+            set_func(c_set_error_debug, OSSL_FUNC_core_set_error_debug(tmp));
+            break;
+        case OSSL_FUNC_CORE_VSET_ERROR:
+            set_func(c_vset_error, OSSL_FUNC_core_vset_error(tmp));
+            break;
+        case OSSL_FUNC_CORE_SET_ERROR_MARK:
+            set_func(c_set_error_mark, OSSL_FUNC_core_set_error_mark(tmp));
+            break;
+        case OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK:
+            set_func(c_clear_last_error_mark,
+                     OSSL_FUNC_core_clear_last_error_mark(tmp));
+            break;
+        case OSSL_FUNC_CORE_POP_ERROR_TO_MARK:
+            set_func(c_pop_error_to_mark, OSSL_FUNC_core_pop_error_to_mark(tmp));
+            break;
+        }
+    }
+#endif
 
     if ((*provctx = ossl_prov_ctx_new()) == NULL
         || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
@@ -200,3 +252,53 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
 
     return 1;
 }
+
+#ifndef STATIC_LEGACY
+/*
+ * Provider specific implementation of libcrypto functions in terms of
+ * upcalls.
+ */
+
+/*
+ * For ERR functions, we pass a NULL context.  This is valid to do as long
+ * as only error codes that the calling libcrypto supports are used.
+ */
+void ERR_new(void)
+{
+    c_new_error(NULL);
+}
+
+void ERR_set_debug(const char *file, int line, const char *func)
+{
+    c_set_error_debug(NULL, file, line, func);
+}
+
+void ERR_set_error(int lib, int reason, const char *fmt, ...)
+{
+    va_list args;
+
+    va_start(args, fmt);
+    c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
+    va_end(args);
+}
+
+void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
+{
+    c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
+}
+
+int ERR_set_mark(void)
+{
+    return c_set_error_mark(NULL);
+}
+
+int ERR_clear_last_mark(void)
+{
+    return c_clear_last_error_mark(NULL);
+}
+
+int ERR_pop_to_mark(void)
+{
+    return c_pop_error_to_mark(NULL);
+}
+#endif