]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Unify the digest getting code inside providers.
authorPauli <paul.dale@oracle.com>
Thu, 5 Sep 2019 03:53:20 +0000 (13:53 +1000)
committerPauli <paul.dale@oracle.com>
Sat, 7 Sep 2019 06:01:53 +0000 (16:01 +1000)
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

providers/common/build.info
providers/common/include/internal/provider_util.h [new file with mode: 0644]
providers/common/include/internal/providercommon.h
providers/common/provider_util.c [new file with mode: 0644]

index 4d87b15e8dbcbd94075224d5b3308748230e1771..b098ca69580248ca726469b8fef15cef45daa663 100644 (file)
@@ -1,4 +1,5 @@
 SUBDIRS=digests ciphers macs kdfs exchange keymgmt
+$COMMON=provider_util.c
 
-SOURCE[../../libcrypto]=\
-        provider_err.c provlib.c
+SOURCE[../../libcrypto]=$COMMON provider_err.c provlib.c
+SOURCE[../fips]=$COMMON
diff --git a/providers/common/include/internal/provider_util.h b/providers/common/include/internal/provider_util.h
new file mode 100644 (file)
index 0000000..c25c658
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2019 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
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/provider.h>
+#include <openssl/engine.h>
+
+typedef struct {
+    /*
+     * References to the underlying cipher implementation.  |cipher| caches
+     * the cipher, always.  |alloc_cipher| only holds a reference to an
+     * explicitly fetched cipher.
+     */
+    const EVP_CIPHER *cipher;   /* cipher */
+    EVP_CIPHER *alloc_cipher;   /* fetched cipher */
+
+    /* Conditions for legacy EVP_CIPHER uses */
+    ENGINE *engine;             /* cipher engine */
+} PROV_CIPHER;
+
+typedef struct {
+    /*
+     * References to the underlying digest implementation.  |md| caches
+     * the digest, always.  |alloc_md| only holds a reference to an explicitly
+     * fetched digest.
+     */
+    const EVP_MD *md;           /* digest */
+    EVP_MD *alloc_md;           /* fetched digest */
+
+    /* Conditions for legacy EVP_MD uses */
+    ENGINE *engine;             /* digest engine */
+} PROV_DIGEST;
+
+/* Cipher functions */
+/*
+ * Load a cipher from the specified parameters with the specified context.
+ * The params "properties", "engine" and "cipher" are used to determine the
+ * implementation used.  If a provider cannot be found, it falls back to trying
+ * non-provider based implementations.
+ */
+int ossl_prov_cipher_load_from_params(PROV_CIPHER *pd,
+                                      const OSSL_PARAM params[],
+                                      OPENSSL_CTX *ctx);
+
+/* Reset the PROV_CIPHER fields and free any allocated cipher reference */
+void ossl_prov_cipher_reset(PROV_CIPHER *pd);
+
+/* Clone a PROV_CIPHER structure into a second */
+int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src);
+
+/* Query the cipher and associated engine (if any) */
+const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pd);
+ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pd);
+
+/* Digest functions */
+/*
+ * Load a digest from the specified parameters with the specified context.
+ * The params "properties", "engine" and "digest" are used to determine the
+ * implementation used.  If a provider cannot be found, it falls back to trying
+ * non-provider based implementations.
+ */
+int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
+                                      const OSSL_PARAM params[],
+                                      OPENSSL_CTX *ctx);
+
+/* Reset the PROV_DIGEST fields and free any allocated digest reference */
+void ossl_prov_digest_reset(PROV_DIGEST *pd);
+
+/* Clone a PROV_DIGEST structure into a second */
+int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src);
+
+/* Query the digest and associated engine (if any) */
+const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd);
+ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd);
index d54fafa971e6039a204aed0f14ef3cf224f251cb..569c08c0b1982c9d509ccc4a57975ca301b0d1fb 100644 (file)
@@ -12,3 +12,4 @@
 const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx);
 
 const char *ossl_prov_util_nid_to_name(int nid);
+
diff --git a/providers/common/provider_util.c b/providers/common/provider_util.c
new file mode 100644 (file)
index 0000000..babbfff
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2019 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
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/evp.h>
+#include <openssl/core_names.h>
+#include "internal/provider_util.h"
+
+void ossl_prov_cipher_reset(PROV_CIPHER *pc)
+{
+    EVP_CIPHER_free(pc->alloc_cipher);
+    pc->alloc_cipher = NULL;
+    pc->cipher = NULL;
+    pc->engine = NULL;
+}
+
+int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
+{
+    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
+        return 0;
+    dst->engine = src->engine;
+    dst->cipher = src->cipher;
+    dst->alloc_cipher = src->alloc_cipher;
+    return 1;
+}
+
+static int load_common(const OSSL_PARAM params[], const char **propquery,
+                       ENGINE **engine)
+{
+    const OSSL_PARAM *p;
+
+    *propquery = NULL;
+    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
+    if (p != NULL) {
+        if (p->data_type != OSSL_PARAM_UTF8_STRING)
+            return 0;
+        *propquery = p->data;
+    }
+
+    *engine = NULL;
+    /* TODO legacy stuff, to be removed */
+#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
+    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
+    if (p != NULL) {
+        if (p->data_type != OSSL_PARAM_UTF8_STRING)
+            return 0;
+        ENGINE_finish(*engine);
+        *engine = ENGINE_by_id(p->data);
+        if (*engine == NULL)
+            return 0;
+    }
+#endif
+    return 1;
+}
+
+int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
+                                      const OSSL_PARAM params[],
+                                      OPENSSL_CTX *ctx)
+{
+    const OSSL_PARAM *p;
+    const char *propquery;
+
+    if (!load_common(params, &propquery, &pc->engine))
+        return 0;
+
+    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
+    if (p == NULL)
+        return 1;
+    if (p->data_type != OSSL_PARAM_UTF8_STRING)
+        return 0;
+
+    EVP_CIPHER_free(pc->alloc_cipher);
+    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
+    /* TODO legacy stuff, to be removed */
+#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
+    if (pc->cipher == NULL)
+        pc->cipher = EVP_get_cipherbyname(p->data);
+#endif
+    return pc->cipher != NULL;
+}
+
+const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
+{
+    return pc->cipher;
+}
+
+ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
+{
+    return pc->engine;
+}
+
+void ossl_prov_digest_reset(PROV_DIGEST *pd)
+{
+    EVP_MD_free(pd->alloc_md);
+    pd->alloc_md = NULL;
+    pd->md = NULL;
+    pd->engine = NULL;
+}
+
+int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
+{
+    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
+        return 0;
+    dst->engine = src->engine;
+    dst->md = src->md;
+    dst->alloc_md = src->alloc_md;
+    return 1;
+}
+
+int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
+                                      const OSSL_PARAM params[],
+                                      OPENSSL_CTX *ctx)
+{
+    const OSSL_PARAM *p;
+    const char *propquery;
+
+    if (!load_common(params, &propquery, &pd->engine))
+        return 0;
+
+
+    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
+    if (p == NULL)
+        return 1;
+    if (p->data_type != OSSL_PARAM_UTF8_STRING)
+        return 0;
+
+    EVP_MD_free(pd->alloc_md);
+    pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
+    /* TODO legacy stuff, to be removed */
+#ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
+    if (pd->md == NULL)
+        pd->md = EVP_get_digestbyname(p->data);
+#endif
+    return pd->md != NULL;
+}
+
+const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
+{
+    return pd->md;
+}
+
+ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
+{
+    return pd->engine;
+}
+