PROV & KEYMGMT: Add PSS-parameter support in the RSA KEYMGMT implementation
[openssl.git] / providers / defltprov.c
index 9400eee0c9900d01c19483ddede953303c3e8f2c..68a1a2b22284050cb5c3ab71e5a0e1044b341374 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2020 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
 #include <openssl/core_names.h>
 #include <openssl/params.h>
 #include "prov/bio.h"
+#include "prov/provider_ctx.h"
 #include "prov/providercommon.h"
 #include "prov/implementations.h"
 #include "prov/provider_util.h"
 #include "internal/nelem.h"
 
+/*
+ * Forward declarations to ensure that interface functions are correctly
+ * defined.
+ */
+static OSSL_provider_gettable_params_fn deflt_gettable_params;
+static OSSL_provider_get_params_fn deflt_get_params;
+static OSSL_provider_query_operation_fn deflt_query;
+
 #define ALGC(NAMES, FUNC, CHECK) { { NAMES, "provider=default", FUNC }, CHECK }
 #define ALG(NAMES, FUNC) ALGC(NAMES, FUNC, NULL)
 
@@ -35,12 +44,12 @@ static const OSSL_PARAM deflt_param_types[] = {
     OSSL_PARAM_END
 };
 
-static const OSSL_PARAM *deflt_gettable_params(const OSSL_PROVIDER *prov)
+static const OSSL_PARAM *deflt_gettable_params(void *provctx)
 {
     return deflt_param_types;
 }
 
-static int deflt_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
+static int deflt_get_params(void *provctx, OSSL_PARAM params[])
 {
     OSSL_PARAM *p;
 
@@ -254,43 +263,12 @@ static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
     ALG("DES-EDE3-CFB", tdes_ede3_cfb_functions),
     ALG("DES-EDE3-CFB8", tdes_ede3_cfb8_functions),
     ALG("DES-EDE3-CFB1", tdes_ede3_cfb1_functions),
+    ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", tdes_wrap_cbc_functions),
     ALG("DES-EDE-ECB:DES-EDE", tdes_ede2_ecb_functions),
     ALG("DES-EDE-CBC", tdes_ede2_cbc_functions),
     ALG("DES-EDE-OFB", tdes_ede2_ofb_functions),
     ALG("DES-EDE-CFB", tdes_ede2_cfb_functions),
-    ALG("DESX-CBC:DESX", tdes_desx_cbc_functions),
-    ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", tdes_wrap_cbc_functions),
-    ALG("DES-ECB", des_ecb_functions),
-    ALG("DES-CBC:DES", des_cbc_functions),
-    ALG("DES-OFB", des_ofb64_functions),
-    ALG("DES-CFB", des_cfb64_functions),
-    ALG("DES-CFB1", des_cfb1_functions),
-    ALG("DES-CFB8", des_cfb8_functions),
 #endif /* OPENSSL_NO_DES */
-#ifndef OPENSSL_NO_BF
-    ALG("BF-ECB", blowfish128ecb_functions),
-    ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions),
-    ALG("BF-OFB", blowfish64ofb64_functions),
-    ALG("BF-CFB", blowfish64cfb64_functions),
-#endif /* OPENSSL_NO_BF */
-#ifndef OPENSSL_NO_IDEA
-    ALG("IDEA-ECB", idea128ecb_functions),
-    ALG("IDEA-CBC:IDEA", idea128cbc_functions),
-    ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions),
-    ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions),
-#endif /* OPENSSL_NO_IDEA */
-#ifndef OPENSSL_NO_CAST
-    ALG("CAST5-ECB", cast5128ecb_functions),
-    ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions),
-    ALG("CAST5-OFB", cast564ofb64_functions),
-    ALG("CAST5-CFB", cast564cfb64_functions),
-#endif /* OPENSSL_NO_CAST */
-#ifndef OPENSSL_NO_SEED
-    ALG("SEED-ECB", seed128ecb_functions),
-    ALG("SEED-CBC:SEED", seed128cbc_functions),
-    ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions),
-    ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions),
-#endif /* OPENSSL_NO_SEED */
 #ifndef OPENSSL_NO_SM4
     ALG("SM4-ECB", sm4128ecb_functions),
     ALG("SM4-CBC:SM4", sm4128cbc_functions),
@@ -298,27 +276,6 @@ static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = {
     ALG("SM4-OFB:SM4-OFB128", sm4128ofb128_functions),
     ALG("SM4-CFB:SM4-CFB128", sm4128cfb128_functions),
 #endif /* OPENSSL_NO_SM4 */
-#ifndef OPENSSL_NO_RC4
-    ALG("RC4", rc4128_functions),
-    ALG("RC4-40", rc440_functions),
-# ifndef OPENSSL_NO_MD5
-    ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions),
-# endif /* OPENSSL_NO_MD5 */
-#endif /* OPENSSL_NO_RC4 */
-#ifndef OPENSSL_NO_RC5
-    ALG("RC5-ECB", rc5128ecb_functions),
-    ALG("RC5-CBC", rc5128cbc_functions),
-    ALG("RC5-OFB", rc5128ofb64_functions),
-    ALG("RC5-CFB", rc5128cfb64_functions),
-#endif /* OPENSSL_NO_RC5 */
-#ifndef OPENSSL_NO_RC2
-    ALG("RC2-ECB", rc2128ecb_functions),
-    ALG("RC2-CBC", rc2128cbc_functions),
-    ALG("RC2-40-CBC", rc240cbc_functions),
-    ALG("RC2-64-CBC", rc264cbc_functions),
-    ALG("RC2-CFB", rc2128cfb128_functions),
-    ALG("RC2-OFB", rc2128ofb128_functions),
-#endif /* OPENSSL_NO_RC2 */
 #ifndef OPENSSL_NO_CHACHA
     ALG("ChaCha20", chacha20_functions),
 # ifndef OPENSSL_NO_POLY1305
@@ -373,7 +330,7 @@ static const OSSL_ALGORITHM deflt_keyexch[] = {
     { "DH:dhKeyAgreement", "provider=default", dh_keyexch_functions },
 #endif
 #ifndef OPENSSL_NO_EC
-    { "ECDH:id-ecPublicKey", "provider=default", ecdh_keyexch_functions },
+    { "ECDH", "provider=default", ecdh_keyexch_functions },
     { "X25519", "provider=default", x25519_keyexch_functions },
     { "X448", "provider=default", x448_keyexch_functions },
 #endif
@@ -385,6 +342,11 @@ static const OSSL_ALGORITHM deflt_signature[] = {
     { "DSA:dsaEncryption", "provider=default", dsa_signature_functions },
 #endif
     { "RSA:rsaEncryption", "provider=default", rsa_signature_functions },
+#ifndef OPENSSL_NO_EC
+    { "ED25519:Ed25519", "provider=default", ed25519_signature_functions },
+    { "ED448:Ed448", "provider=default", ed448_signature_functions },
+    { "ECDSA", "provider=default", ecdsa_signature_functions },
+#endif
     { NULL, NULL, NULL }
 };
 
@@ -401,10 +363,13 @@ static const OSSL_ALGORITHM deflt_keymgmt[] = {
     { "DSA:dsaEncryption", "provider=default", dsa_keymgmt_functions },
 #endif
     { "RSA:rsaEncryption", "provider=default", rsa_keymgmt_functions },
+    { "RSA-PSS:RSASSA-PSS", "provider=default", rsapss_keymgmt_functions },
 #ifndef OPENSSL_NO_EC
     { "EC:id-ecPublicKey", "provider=default", ec_keymgmt_functions },
     { "X25519", "provider=default", x25519_keymgmt_functions },
     { "X448", "provider=default", x448_keymgmt_functions },
+    { "ED25519", "provider=default", ed25519_keymgmt_functions },
+    { "ED448", "provider=default", ed448_keymgmt_functions },
 #endif
     { NULL, NULL, NULL }
 };
@@ -471,17 +436,17 @@ static const OSSL_ALGORITHM deflt_serializer[] = {
 #endif
 
 #ifndef OPENSSL_NO_EC
-    { "X25519", "provider=default,format=text,type=private",
+    { "X25519", "provider=default,fips=yes,format=text,type=private",
       x25519_priv_print_serializer_functions },
-    { "X25519", "provider=default,format=text,type=public",
+    { "X25519", "provider=default,fips=yes,format=text,type=public",
       x25519_pub_print_serializer_functions },
-    { "X25519", "provider=default,format=der,type=private",
+    { "X25519", "provider=default,fips=yes,format=der,type=private",
       x25519_priv_der_serializer_functions },
-    { "X25519", "provider=default,format=der,type=public",
+    { "X25519", "provider=default,fips=yes,format=der,type=public",
       x25519_pub_der_serializer_functions },
-    { "X25519", "provider=default,format=pem,type=private",
+    { "X25519", "provider=default,fips=yes,format=pem,type=private",
       x25519_priv_pem_serializer_functions },
-    { "X25519", "provider=default,format=pem,type=public",
+    { "X25519", "provider=default,fips=yes,format=pem,type=public",
       x25519_pub_pem_serializer_functions },
 
     { "X448", "provider=default,format=text,type=private",
@@ -496,13 +461,56 @@ static const OSSL_ALGORITHM deflt_serializer[] = {
       x448_priv_pem_serializer_functions },
     { "X448", "provider=default,format=pem,type=public",
       x448_pub_pem_serializer_functions },
-#endif
 
+    { "ED25519", "provider=default,fips=yes,format=text,type=private",
+      ed25519_priv_print_serializer_functions },
+    { "ED25519", "provider=default,fips=yes,format=text,type=public",
+      ed25519_pub_print_serializer_functions },
+    { "ED25519", "provider=default,fips=yes,format=der,type=private",
+      ed25519_priv_der_serializer_functions },
+    { "ED25519", "provider=default,fips=yes,format=der,type=public",
+      ed25519_pub_der_serializer_functions },
+    { "ED25519", "provider=default,fips=yes,format=pem,type=private",
+      ed25519_priv_pem_serializer_functions },
+    { "ED25519", "provider=default,fips=yes,format=pem,type=public",
+      ed25519_pub_pem_serializer_functions },
+
+    { "ED448", "provider=default,format=text,type=private",
+      ed448_priv_print_serializer_functions },
+    { "ED448", "provider=default,format=text,type=public",
+      ed448_pub_print_serializer_functions },
+    { "ED448", "provider=default,format=der,type=private",
+      ed448_priv_der_serializer_functions },
+    { "ED448", "provider=default,format=der,type=public",
+      ed448_pub_der_serializer_functions },
+    { "ED448", "provider=default,format=pem,type=private",
+      ed448_priv_pem_serializer_functions },
+    { "ED448", "provider=default,format=pem,type=public",
+      ed448_pub_pem_serializer_functions },
+
+    { "EC", "provider=default,fips=yes,format=text,type=private",
+      ec_priv_text_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=text,type=public",
+      ec_pub_text_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=text,type=parameters",
+      ec_param_text_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=der,type=private",
+      ec_priv_der_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=der,type=public",
+      ec_pub_der_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=der,type=parameters",
+      ec_param_der_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=pem,type=private",
+      ec_priv_pem_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=pem,type=public",
+      ec_pub_pem_serializer_functions },
+    { "EC", "provider=default,fips=yes,format=pem,type=parameters",
+      ec_param_pem_serializer_functions },
+#endif
     { NULL, NULL, NULL }
 };
 
-static const OSSL_ALGORITHM *deflt_query(OSSL_PROVIDER *prov,
-                                         int operation_id,
+static const OSSL_ALGORITHM *deflt_query(void *provctx, int operation_id,
                                          int *no_cache)
 {
     *no_cache = 0;
@@ -530,8 +538,14 @@ static const OSSL_ALGORITHM *deflt_query(OSSL_PROVIDER *prov,
     return NULL;
 }
 
+static void deflt_teardown(void *provctx)
+{
+    PROV_CTX_free(provctx);
+}
+
 /* Functions we provide to the core */
 static const OSSL_DISPATCH deflt_dispatch_table[] = {
+    { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))deflt_teardown },
     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))deflt_gettable_params },
     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))deflt_get_params },
     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))deflt_query },
@@ -569,13 +583,20 @@ int ossl_default_provider_init(const OSSL_PROVIDER *provider,
     if (c_get_libctx == NULL)
         return 0;
 
-    *out = deflt_dispatch_table;
-
     /*
      * We want to make sure that all calls from this provider that requires
      * a library context use the same context as the one used to call our
      * functions.  We do that by passing it along as the provider context.
+     *
+     * This is special for built-in providers.  External providers should
+     * create their own library context.
      */
-    *provctx = c_get_libctx(provider);
+    if ((*provctx = PROV_CTX_new()) == NULL)
+        return 0;
+    PROV_CTX_set0_library_context(*provctx, c_get_libctx(provider));
+    PROV_CTX_set0_provider(*provctx, provider);
+
+    *out = deflt_dispatch_table;
+
     return 1;
 }