Migrate to openssl3.0 14/299214/12
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 21 Sep 2023 10:41:17 +0000 (12:41 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 2 Oct 2023 15:13:42 +0000 (17:13 +0200)
* Update RPM and CMake dependencies.
* Replace deprecated HMAC functions in sqlcipher.
* Update openssl error handling. We can't rely on function codes any
  more (all are 0 in openssl3.0).
* Adjust to argument constness changes.
* Get rid of FIPS_mode_set(). FIPS mode can be enabled in openssl
  configuration.
* Fix sk_X509_free visibility issue.
* Openssl3.0 does not allow X9.31 padding withraw messages (no digest).
  Update doc and tests accordingly.
* Handle new openssl errors.
* Ignore deprecation warnings.

Change-Id: I7dee7d3aa6e8b364553405202acde8f848621d4a

14 files changed:
CMakeLists.txt
misc/ckm_initial_values/CMakeLists.txt
packaging/key-manager.spec
src/include/ckmc/ckmc-manager.h
src/manager/CMakeLists.txt
src/manager/common/key-impl.cpp
src/manager/common/openssl-error-handler.cpp
src/manager/common/openssl-error-handler.h
src/manager/common/openssl_utils.h
src/manager/crypto/sw-backend/internals.cpp
src/manager/service/access-control.cpp
src/manager/service/ss-crypto.cpp
src/manager/sqlcipher/sqlcipher.c
unit-tests/test_sw-backend.cpp

index 2ad368e5b5a8c5f8c8a3e3122ea4314e3e58504a..24779e768aea2baea25c722dfdb21c001bf8419e 100644 (file)
@@ -29,13 +29,13 @@ INCLUDE(FindPkgConfig)
 ############################# compiler flags ##################################
 
 SET(CMAKE_C_FLAGS_PROFILING    "-g -O0 -pg -Wp,-U_FORTIFY_SOURCE")
-SET(CMAKE_CXX_FLAGS_PROFILING  "-g -std=c++17 -O0 -pg -Wp,-U_FORTIFY_SOURCE")
+SET(CMAKE_CXX_FLAGS_PROFILING  "-g -std=c++17 -O0 -pg -Wp,-U_FORTIFY_SOURCE -Wno-deprecated-declarations")
 SET(CMAKE_C_FLAGS_DEBUG        "-g -O0 -ggdb -Wp,-U_FORTIFY_SOURCE")
-SET(CMAKE_CXX_FLAGS_DEBUG      "-g -std=c++17 -O0 -ggdb -Wp,-U_FORTIFY_SOURCE -Wshadow")
+SET(CMAKE_CXX_FLAGS_DEBUG      "-g -std=c++17 -O0 -ggdb -Wp,-U_FORTIFY_SOURCE -Wshadow -Wno-deprecated-declarations")
 SET(CMAKE_C_FLAGS_RELEASE      "-g -O2")
-SET(CMAKE_CXX_FLAGS_RELEASE    "-g -std=c++17 -O2 -Wshadow")
+SET(CMAKE_CXX_FLAGS_RELEASE    "-g -std=c++17 -O2 -Wshadow -Wno-deprecated-declarations")
 SET(CMAKE_C_FLAGS_COVERAGE     "-g -O0 -ggdb --coverage -Wp,-U_FORTIFY_SOURCE")
-SET(CMAKE_CXX_FLAGS_COVERAGE   "-g -std=c++17 -O0 -ggdb --coverage -Wp,-U_FORTIFY_SOURCE")
+SET(CMAKE_CXX_FLAGS_COVERAGE   "-g -std=c++17 -O0 -ggdb --coverage -Wp,-U_FORTIFY_SOURCE -Wno-deprecated-declarations")
 
 # Force PIE
 SET(CMAKE_POSITION_INDEPENDENT_CODE "True")
@@ -128,7 +128,7 @@ PKG_CHECK_MODULES(KEY_MANAGER_DEP
     REQUIRED
     dlog
     glib-2.0
-    openssl1.1
+    openssl3
     libsmack
     libsystemd
     capi-base-common
index 4673a1803c84764cb66ae0b3d57d5b4fbe48a3f4..b437a65bcc3a93735f8fe448e0cdc7a829715516 100644 (file)
@@ -15,7 +15,7 @@ SET(CKM_INITIAL_VALUES "ckm_initial_values")
 
 PKG_CHECK_MODULES(CKM_INITIAL_VALUES_DEP
     REQUIRED
-    openssl1.1
+    openssl3
     libxml-2.0
     )
 
index 60c913a56ffa21839d715660034b7b2db8b9b45f..274ee353a5cc35e855d8620bad7a54e55f2687d6 100644 (file)
@@ -24,7 +24,7 @@ Source1004: libkey-manager-common.manifest
 BuildRequires: cmake
 BuildRequires: zip
 BuildRequires: pkgconfig(dlog)
-BuildRequires: pkgconfig(openssl1.1)
+BuildRequires: pkgconfig(openssl3)
 BuildRequires: libattr-devel
 BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(libsystemd)
@@ -153,7 +153,7 @@ Summary:    CKM initial values tool
 Group:      Security/Libraries
 License:    Apache-2.0
 BuildRequires: cmake
-BuildRequires: pkgconfig(openssl1.1)
+BuildRequires: pkgconfig(openssl3)
 BuildRequires: pkgconfig(libxml-2.0)
 Requires(post): /sbin/ldconfig
 Requires(postun): /sbin/ldconfig
index ee3cc6a48b12fe313dcac99feb2f2cc0188b50ab..a0f3267add2fe8245983e548b1fbf781d5274e14 100644 (file)
@@ -669,8 +669,8 @@ int ckmc_create_key_aes(size_t size, const char *key_alias, ckmc_policy_s key_po
  * @param[in] private_key_alias The name of private key
  * @param[in] password The password used in decrypting a private key value
  * @param[in] message The message that is signed with a private key
- * @param[in] hash The hash algorithm used in creating signature. CKMC_HASH_NONE is invalid for DSA
- *                 & ECDSA
+ * @param[in] hash The hash algorithm used in creating signature. CKMC_HASH_NONE is invalid for DSA,
+ *                 ECDSA and RSA with X9.31 padding
  * @param[in] padding The RSA padding algorithm used in creating signature. It is used only when the signature algorithm is RSA. If
  *                    @a padding is CKMC_NONE_PADDING you must use CKMC_HASH_NONE
  *                    and the message must be equal to key length
@@ -713,8 +713,8 @@ int ckmc_create_signature(const char *private_key_alias,
  * @param[in] password The password used in decrypting a public key value
  * @param[in] message The input on which the signature is created
  * @param[in] signature The signature that is verified with public key
- * @param[in] hash The hash algorithm used in verifying signature. CKMC_HASH_NONE is invalid for DSA
- *                 & ECDSA
+ * @param[in] hash The hash algorithm used in verifying signature. CKMC_HASH_NONE is invalid for
+ *                 DSA, ECDSA & RSA with X9.31 padding
  * @param[in] padding The RSA padding algorithm used in verifying signature. It is used only when
  *                    the signature algorithm is RSA. If @a padding is CKMC_NONE_PADDING you must
  *                    use CKMC_HASH_NONE and the message must be equal to key length
index ced080ad214127f31196243e8e960c0e5142140c..ac26360596e026ff1e29e92a1a2d81a2943f64bd 100644 (file)
@@ -1,7 +1,7 @@
 PKG_CHECK_MODULES(COMMON_DEP
     REQUIRED
     dlog
-    openssl1.1
+    openssl3
     libsmack
     libsystemd
     )
index 8a4ef6198744970445ab6567b4cf291dac1f3133..6e2f9c962c0798a7edddbc7e88045ec8e17e79b9 100644 (file)
@@ -51,9 +51,9 @@ int passcb(char *buff, int size, int /*rwflag*/, void *userdata)
        return ptr->size();
 }
 
-typedef int(*I2D_CONV)(BIO *, EVP_PKEY *);
+typedef int(*I2D_CONV)(BIO *, const EVP_PKEY *);
 
-CKM::RawBuffer i2d(I2D_CONV fun, EVP_PKEY *pkey)
+CKM::RawBuffer i2d(I2D_CONV fun, const EVP_PKEY *pkey)
 {
        auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem()));
 
index a43c42472e17d30ca2ec64de2d5c0468b60f17e7..585df582019529dc8c601d272d13bc6604da3ec9 100644 (file)
@@ -88,71 +88,69 @@ void errorHandle(const char *file, int line, const char *function, int openssl_r
 
        /* known errors */
        switch (err) {
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PUBLIC_ENCRYPT, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_PKEY_RSA_CTRL, RSA_R_KEY_SIZE_TOO_SMALL):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_MISSING_PRIVATE_KEY):
-       case ERR_PACK(ERR_LIB_RSA, RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_MISSING_PRIVATE_KEY):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION):
-       case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE):
-       case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DERIVE_SET_PEER, EVP_R_DIFFERENT_PARAMETERS):
-       case ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE):
-       case ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_MISSING_PRIVATE_KEY):
-       case ERR_PACK(ERR_LIB_DSA, DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE):
-       case ERR_PACK(ERR_LIB_DSA, DSA_F_DSA_DO_SIGN, DSA_R_MISSING_PRIVATE_KEY):
-       case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG):
-       case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG):
-       case ERR_PACK(ERR_LIB_ASN1, ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT):
-       case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT):
-       case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ):
-       case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ):
-       case ERR_PACK(ERR_LIB_PEM, PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_GREATER_THAN_MOD_LEN):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_TOO_LARGE_FOR_MODULUS):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_KEY_SIZE_TOO_SMALL):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_MISSING_PRIVATE_KEY):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_PADDING_MODE):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_X931_DIGEST):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_TOO_LARGE):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_OAEP_DECODING_ERROR):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_LENGTH):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_PADDING):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_BLOCK_TYPE_IS_NOT_01):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_BAD_FIXED_HEADER_DECRYPT):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_NULL_BEFORE_BLOCK_MISSING):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_BAD_PAD_BYTE_COUNT):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_PKCS_DECODING_ERROR):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_SLEN_CHECK_FAILED):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_FIRST_OCTET_INVALID):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_LAST_OCTET_INVALID):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_SLEN_RECOVERY_FAILED):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_HEADER):
+       case ERR_PACK(ERR_LIB_RSA, 0, RSA_R_INVALID_TRAILER):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_COMMAND_NOT_SUPPORTED):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_OPERATION_SET):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_OPERATION):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_WRONG_FINAL_BLOCK_LENGTH):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DIFFERENT_PARAMETERS):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BAD_DECRYPT):
+       case ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_DIGEST_TYPE):
+       case ERR_PACK(ERR_LIB_EC, 0, EC_R_MISSING_PRIVATE_KEY):
+       case ERR_PACK(ERR_LIB_DSA, 0, DSA_R_INVALID_DIGEST_TYPE):
+       case ERR_PACK(ERR_LIB_DSA, 0, DSA_R_MISSING_PRIVATE_KEY):
+       case ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_TOO_LONG):
+       case ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_HEADER_TOO_LONG):
+       case ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_WRONG_TAG):
+       case ERR_PACK(ERR_LIB_ASN1, 0, ERR_R_NESTED_ASN1_ERROR):
+       case ERR_PACK(ERR_LIB_ASN1, 0, ASN1_R_NOT_ENOUGH_DATA):
+       case ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_DECRYPT):
+       case ERR_PACK(ERR_LIB_PEM, 0, PEM_R_BAD_PASSWORD_READ):
+       case ERR_PACK(ERR_LIB_PEM, 0, PEM_R_NO_START_LINE):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_X931_DIGEST):
+       case ERR_PACK(ERR_LIB_PROV, 0, ERR_R_NESTED_ASN1_ERROR):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_PADDING_MODE):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_DIGEST_NOT_ALLOWED):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_DIGEST):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_DIGEST_LENGTH):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_KEY_SIZE_TOO_SMALL):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_INVALID_SIGNATURE_SIZE):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_ALGORITHM_MISMATCH):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_OUTPUT_BUFFER_TOO_SMALL):
+       case ERR_PACK(ERR_LIB_PROV, 0, PROV_R_BAD_DECRYPT):
                ret = CKM_API_ERROR_INPUT_PARAM;
                break;
-       case ERR_PACK(ERR_LIB_X509, X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY):
-       case ERR_PACK(ERR_LIB_X509, X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE):
-       case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY, EVP_R_OPERATON_NOT_INITIALIZED):
+       case ERR_PACK(ERR_LIB_X509, 0, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY):
+       case ERR_PACK(ERR_LIB_X509, 0, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE):
+       case ERR_PACK(ERR_LIB_EVP, 0, EVP_R_OPERATON_NOT_INITIALIZED):
                ret = CKM_API_ERROR_VERIFICATION_FAILED;
                break;
        }
 
-       /* known rsa padding errors */
-       if (ret == CKM_API_SUCCESS && ERR_GET_LIB(err) == ERR_LIB_RSA) {
-               switch (ERR_GET_FUNC(err)) {
-               case RSA_F_CHECK_PADDING_MD:
-               case RSA_F_RSA_PADDING_CHECK_NONE:
-               case RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP:
-               case RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1:
-               case RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1:
-               case RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2:
-               case RSA_F_RSA_PADDING_CHECK_SSLV23:
-               case RSA_F_RSA_PADDING_CHECK_X931:
-               case RSA_F_RSA_PADDING_ADD_NONE:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_OAEP:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_PSS:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1:
-               case RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2:
-               case RSA_F_RSA_PADDING_ADD_SSLV23:
-               case RSA_F_RSA_PADDING_ADD_X931:
-                       ret = CKM_API_ERROR_INPUT_PARAM;
-                       break;
-               }
-       }
-
        /* fatal errors */
        int reason = ERR_GET_REASON(err);
        if (ret == CKM_API_SUCCESS && reason <= GENERIC_REASON_MAX && (err & ERR_R_FATAL) > 0) {
index 407d2fa0c9647daf5030292200d46c412cb90a0b..e451ba23282fab2a6c260b178df96b92bbb30d59 100644 (file)
@@ -22,6 +22,7 @@
 #include <ckm/ckm-type.h>
 #include <openssl/evp.h>
 #include <openssl/err.h>
+#include <openssl/proverr.h>
 #include <sw-backend/obj.h>
 
 #include <symbol-visibility.h>
@@ -52,4 +53,4 @@ COMMON_API void errorHandle(const char *file, int line, const char *function, in
        errorHandle(__FILE__, __LINE__, __func__, openssl_ret); \
 }
 
-} // namespace CKM
\ No newline at end of file
+} // namespace CKM
index d3e25f0c38752fa6883f361117345b226fdd1cd1..b5d3b404304d480188194523401354e0c371af83 100644 (file)
 
 namespace CKM {
 
+inline void X509_stack_free_wrapper(STACK_OF(X509)* stack)
+{
+       sk_X509_free(stack);
+}
+
 inline auto create_x509_stack()
 {
-       return uptr<sk_X509_free>(sk_X509_new_null());
+       return uptr<X509_stack_free_wrapper>(sk_X509_new_null());
 }
 
 } // namespace CKM
index 17b8cad4aa4a4aa267ef1188adc01a5327008c75..ea2e7167a381432f81fececf2299a594a7feb48d 100644 (file)
@@ -59,10 +59,10 @@ namespace Internals {
 namespace {
 typedef Uptr<EVP_PKEY_CTX_free, EVP_PKEY_CTX> EvpPkeyCtxUPtr;
 
-typedef int(*I2D_CONV)(BIO *, EVP_PKEY *);
+typedef int(*I2D_CONV)(BIO *, const EVP_PKEY *);
 
 
-RawBuffer i2d(I2D_CONV fun, EVP_PKEY *pkey)
+RawBuffer i2d(I2D_CONV fun, const EVP_PKEY *pkey)
 {
        auto bio = uptr<BIO_free_all>(BIO_new(BIO_s_mem()));
 
@@ -718,13 +718,13 @@ RawBuffer decryptDataAesGcmPacked(
                           aad);
 }
 
-EC_KEY* getEcKey(EVP_PKEY* evpKey)
+const EC_KEY* getEcKey(const EVP_PKEY* evpKey)
 {
        int subType = EVP_PKEY_type(EVP_PKEY_id(evpKey));
        if (subType != EVP_PKEY_EC)
                ThrowErr(Exc::Crypto::InputParam, "Invalid key type: ", subType);
 
-       EC_KEY *ecKey = EVP_PKEY_get0_EC_KEY(evpKey);
+       const EC_KEY *ecKey = EVP_PKEY_get0_EC_KEY(evpKey);
        if (!ecKey)
                ThrowErr(Exc::Crypto::InternalError, "Can't get EC key");
 
@@ -1023,7 +1023,7 @@ Data deriveECDH(const EvpShPtr &pkey, const CryptoAlgorithm &alg)
 {
        validateParams<IsEcdh>(alg);
 
-       EC_KEY *ecKey = getEcKey(pkey.get());
+       const EC_KEY *ecKey = getEcKey(pkey.get());
 
        // get private key curve name
        int prvCurve = getCurve(ecKey);
index 39abb7256853980f8ede3388a5b94a9965632d63..31058c7fcc13d24a8a41bf56524ba8c623af2aa6 100644 (file)
@@ -23,7 +23,6 @@
 #include <dpl/log/log.h>
 #include <ckm/ckm-error.h>
 #include <ckm/ckm-type.h>
-#include <openssl/crypto.h>
 
 namespace {
 const uid_t SYSTEM_SVC_MAX_UID = (5000 - 1);
@@ -40,14 +39,6 @@ void AccessControl::updateCCMode()
        /* newMode should be extracted from global property like buxton in product */
        int newMode = 0;
 
-       if ((newMode == 1) == m_ccMode)
-               return;
-
-       if (FIPS_mode_set(newMode) == 0) {
-               LogError("Error to FIPS_mode_set with param " << newMode);
-               return;
-       }
-
        m_ccMode = (newMode == 1);
 }
 
index fe49f06ead6cfe1e290cd5165e5b9782aa3903a4..51a8b5696e4a1190050870975d1bcd0e7b0f5627 100644 (file)
@@ -104,15 +104,15 @@ RawBuffer _decrypt(const RawBuffer &key, const RawBuffer &iv, const RawBuffer &c
 
        auto ctx = ctxptr.get();
 
-       int ec = ::EVP_CIPHER_CTX_set_padding(ctx, 1);
+       int ec = ::EVP_CipherInit(ctx, algo, key.data(), iv.data(), 0 /* decrypt flag */);
        if (ec != 1) {
-               LogError("Failed to evp ctx set padding. ec: " << ec);
+               LogError("Failed to evp cipher init. ec: " << ec);
                return RawBuffer();
        }
 
-       ec = ::EVP_CipherInit(ctx, algo, key.data(), iv.data(), 0 /* decrypt flag */);
+       ec = ::EVP_CIPHER_CTX_set_padding(ctx, 1);
        if (ec != 1) {
-               LogError("Failed to evp cipher init. ec: " << ec);
+               LogError("Failed to evp ctx set padding. ec: " << ec);
                return RawBuffer();
        }
 
index eadacc51f64893cc9d47f08a33f8fcb870388cb3..a6499846e2c7f33dc20b1a60623f5565cdb357ac 100644 (file)
@@ -20562,7 +20562,7 @@ typedef struct {
   const char* (*get_provider_name)(void *ctx);
   int (*add_random)(void *ctx, void *buffer, int length);
   int (*random)(void *ctx, void *buffer, int length);
-  int (*hmac)(void *ctx, int algorithm, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out);
+  int (*hmac)(void *ctx, int algorithm, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out, int out_sz);
   int (*kdf)(void *ctx, int algorithm, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key);
   int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out);
   const char* (*get_cipher)(void *ctx);
@@ -23769,7 +23769,7 @@ static void sqlcipher_put4byte_le(unsigned char *p, u32 v) {
   p[3] = (u8)(v>>24);
 }
 
-static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) {
+static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out, int out_sz) {
   unsigned char pgno_raw[sizeof(pgno)];
   /* we may convert page number to consistent representation before calculating MAC for
      compatibility across big-endian and little-endian platforms.
@@ -23794,7 +23794,7 @@ static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, uns
     ctx->provider_ctx, ctx->hmac_algorithm, c_ctx->hmac_key,
     ctx->key_sz, in,
     in_sz, (unsigned char*) &pgno_raw,
-    sizeof(pgno), out);
+    sizeof(pgno), out, out_sz);
 }
 
 /*
@@ -23838,7 +23838,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
   }
 
   if((ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT) && !ctx->skip_read_hmac) {
-    if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
+    if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out, ctx->hmac_sz) != SQLITE_OK) {
       CODEC_TRACE("codec_cipher: hmac operation on decrypt failed for pgno=%d\n", pgno);
       goto error;
     }
@@ -23869,7 +23869,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
   };
 
   if((ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
-    if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
+    if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out, ctx->hmac_sz) != SQLITE_OK) {
       CODEC_TRACE("codec_cipher: hmac operation on encrypt failed for pgno=%d\n", pgno);
       goto error;
     };
@@ -24103,7 +24103,7 @@ int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *col
       result = sqlite3_mprintf("error reading %d bytes from file page %d at offset %d\n", read_sz, page, offset);
       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
-    } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out) != SQLITE_OK) {
+    } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out, ctx->hmac_sz) != SQLITE_OK) {
       result = sqlite3_mprintf("HMAC operation failed for page %d", page);
       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
@@ -25034,6 +25034,7 @@ int sqlcipher_nss_setup(sqlcipher_provider *p) {
 #include <openssl/objects.h>
 #include <openssl/hmac.h>
 #include <openssl/err.h>
+#include <openssl/core_names.h>
 
 static unsigned int openssl_init_count = 0;
 
@@ -25163,41 +25164,52 @@ static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
   return (rc == 1) ? SQLITE_OK : SQLITE_ERROR;
 }
 
-static int sqlcipher_openssl_hmac(void *ctx, int algorithm, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
-  unsigned int outlen;
+static int sqlcipher_openssl_hmac(void *ctx, int algorithm, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out, int out_sz) {
+  size_t outlen;
   int rc = SQLITE_OK;
-  HMAC_CTX* hctx = NULL;
+  EVP_MAC_CTX* hctx = NULL;
+  EVP_MAC* mac = NULL;
+  OSSL_PARAM params[2];
+  char* digest_name = NULL;
 
   if(in == NULL) goto error;
 
-  hctx = HMAC_CTX_new();
+  mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
+  if (mac == NULL) goto error;
+
+  hctx = EVP_MAC_CTX_new(mac);
   if(hctx == NULL) goto error;
 
   switch(algorithm) {
     case SQLCIPHER_HMAC_SHA1:
-      if(!HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha1(), NULL)) goto error;
+      digest_name = "SHA1";
       break;
     case SQLCIPHER_HMAC_SHA256:
-      if(!HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha256(), NULL)) goto error;
+      digest_name = "SHA2-256";
       break;
     case SQLCIPHER_HMAC_SHA512:
-      if(!HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha512(), NULL)) goto error;
+      digest_name = "SHA2-512";
       break;
     default:
       goto error;
   }
+  params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name, sizeof(digest_name));
+  params[1] = OSSL_PARAM_construct_end();
+
+  if(!EVP_MAC_init(hctx, hmac_key, key_sz, params)) goto error;
 
-  if(!HMAC_Update(hctx, in, in_sz)) goto error;
+  if(!EVP_MAC_update(hctx, in, in_sz)) goto error;
   if(in2 != NULL) {
-    if(!HMAC_Update(hctx, in2, in2_sz)) goto error;
+    if(!EVP_MAC_update(hctx, in2, in2_sz)) goto error;
   }
-  if(!HMAC_Final(hctx, out, &outlen)) goto error;
+  if(!EVP_MAC_final(hctx, out, &outlen, out_sz)) goto error;
 
   goto cleanup;
 error:
   rc = SQLITE_ERROR;
 cleanup:
-  if(hctx) HMAC_CTX_free(hctx);
+  if(hctx) EVP_MAC_CTX_free(hctx);
+  EVP_MAC_free(mac);
   return rc;
 }
 
index 488f560dcf9085a5dcae69dcc0286c43b55a20f0..d5e29a90034c415b0b7139bcef200d6951555dd7 100644 (file)
@@ -935,7 +935,6 @@ POSITIVE_TEST_CASE(sign)
        signVerify(AlgoType::RSA_GEN, 4096, HashAlgorithm::NONE, RSAPaddingAlgorithm::PKCS1);
        signVerify(AlgoType::RSA_GEN, 2048, HashAlgorithm::NONE, RSAPaddingAlgorithm::PKCS1);
        signVerify(AlgoType::RSA_GEN, 1024, HashAlgorithm::NONE, RSAPaddingAlgorithm::PKCS1);
-       signVerify(AlgoType::RSA_GEN, 1024, HashAlgorithm::NONE, RSAPaddingAlgorithm::X931);
 }
 
 NEGATIVE_TEST_CASE(sign)
@@ -1050,6 +1049,7 @@ NEGATIVE_TEST_CASE(sign)
        // no hash forbidden
        invalidSign(shortMsg, AlgoType::DSA_SV, HashAlgorithm::NONE);
        invalidSign(shortMsg, AlgoType::ECDSA_SV, HashAlgorithm::NONE);
+       invalidSign(paddingMsg, AlgoType::RSA_SV, HashAlgorithm::NONE, RSAPaddingAlgorithm::X931);
 
        // non-none hash + no padding forbidden
        invalidSign(equalMsg, AlgoType::RSA_SV, HashAlgorithm::SHA256, RSAPaddingAlgorithm::NONE);