Cipher API implementation (client part) 53/292053/9
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 26 Apr 2023 21:11:33 +0000 (23:11 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 5 Jun 2023 14:05:26 +0000 (16:05 +0200)
Change-Id: I191f606819ae306f570fe538674f943e336ec86f

src/include/ckm/ckm-manager.h
src/manager/client-capi/ckmc-manager.cpp
src/manager/client/client-manager-impl.cpp
src/manager/client/client-manager-impl.h
src/manager/client/client-manager.cpp
src/manager/common/protocols.h

index 8076456..dd91e99 100644 (file)
@@ -192,6 +192,14 @@ public:
                                                 KeyType &keyType,
                                                 RawBuffer &wrappedKey);
 
+       int initializeCipher(const CryptoAlgorithm &params,
+                                                const Alias &keyAlias,
+                                                const Password &keyPassword,
+                                                bool encrypt,
+                                                int& requestId);
+       int updateCipher(int requestId, const RawBuffer &in, RawBuffer &out);
+       int finalizeCipher(int requestId, const RawBuffer &in, RawBuffer &out);
+
        static ManagerShPtr create();
 
 private:
index 47bd944..9c9ca34 100644 (file)
 #include <iostream>
 #include <string.h>
 #include <utils.h>
+#include <cxxabi.h>
 
 namespace {
 const CKM::CertificateShPtrVector EMPTY_CERT_VECTOR;
 const CKM::AliasVector EMPTY_ALIAS_VECTOR;
 
+struct CipherCtx
+{
+       CKM::ManagerShPtr mgr;
+       int requestId;
+};
+
 int _ckmc_alias_info_new(const char* alias,
                          bool is_password_protected,
                          ckmc_backend_id_e backend,
@@ -1124,3 +1131,127 @@ int ckmc_get_backend_info(ckmc_backend_id_e backend, ckmc_backend_info_h* ppinfo
 
        EXCEPTION_GUARD_END
 }
+
+KEY_MANAGER_CAPI
+int ckmc_cipher_initialize(ckmc_param_list_h params,
+                           const char *key_alias,
+                           const char *key_password,
+                           bool encrypt,
+                           ckmc_cipher_ctx_h *context)
+{
+       if (params == nullptr || key_alias == nullptr || context == nullptr)
+               return CKMC_ERROR_INVALID_PARAMETER;
+
+       const CKM::CryptoAlgorithm *ca = reinterpret_cast<const CKM::CryptoAlgorithm *>(params);
+
+       EXCEPTION_GUARD_START_CAPI
+
+       int ret = 0;
+       int requestId;
+       CKM::ManagerShPtr mgr;
+
+       if (*context != nullptr) {
+               auto ctx = reinterpret_cast<CipherCtx*>(*context);
+               requestId = ctx->requestId;
+               mgr = ctx->mgr;
+       } else {
+               requestId = 0;
+               mgr = CKM::Manager::create();
+       }
+
+       ret = to_ckmc_error(mgr->initializeCipher(*ca,
+                                                 CKM::Alias(key_alias),
+                                                 _tostring(key_password),
+                                                 encrypt,
+                                                 requestId));
+       if (ret == CKMC_ERROR_NONE && *context == nullptr) {
+               auto ctx = new(std::nothrow)(CipherCtx);
+               ctx->mgr = mgr;
+               ctx->requestId = requestId;
+               *context = reinterpret_cast<ckmc_cipher_ctx_h>(ctx);
+       }
+       return ret;
+
+       EXCEPTION_GUARD_END
+}
+
+KEY_MANAGER_CAPI
+int ckmc_cipher_update(ckmc_cipher_ctx_h context,
+                       const ckmc_raw_buffer_s in,
+                       ckmc_raw_buffer_s **ppout)
+{
+       if (context == nullptr || ppout == nullptr)
+               return CKMC_ERROR_INVALID_PARAMETER;
+
+       EXCEPTION_GUARD_START_CAPI
+
+       int ret = 0;
+       CKM::RawBuffer inBuffer(in.data, in.data + in.size);
+       CKM::RawBuffer outBuffer;
+
+       auto ctx = reinterpret_cast<CipherCtx*>(context);
+       ret = ctx->mgr->updateCipher(ctx->requestId, inBuffer, outBuffer);
+       if (ret != CKM_API_SUCCESS)
+               return to_ckmc_error(ret);
+
+       if (outBuffer.empty()) {
+               *ppout = nullptr;
+               return CKMC_ERROR_NONE;
+       }
+
+       return ckmc_buffer_new(outBuffer.data(), outBuffer.size(), ppout);
+       EXCEPTION_GUARD_END
+}
+
+KEY_MANAGER_CAPI
+int ckmc_cipher_finalize(ckmc_cipher_ctx_h context,
+                         const ckmc_raw_buffer_s *in,
+                         ckmc_raw_buffer_s **ppout)
+{
+       if (context == nullptr || ppout == nullptr)
+               return CKMC_ERROR_INVALID_PARAMETER;
+
+       EXCEPTION_GUARD_START_CAPI
+
+       int ret = 0;
+       CKM::RawBuffer outBuffer;
+       CKM::RawBuffer inBuffer;
+
+       if (in != nullptr)
+               inBuffer.assign(in->data, in->data + in->size);
+
+       auto ctx = reinterpret_cast<CipherCtx*>(context);
+       ret = ctx->mgr->finalizeCipher(ctx->requestId, inBuffer, outBuffer);
+       if (ret != CKM_API_SUCCESS)
+               return to_ckmc_error(ret);
+
+       if (outBuffer.empty()) {
+               *ppout = nullptr;
+               return CKMC_ERROR_NONE;
+       }
+
+       return ckmc_buffer_new(outBuffer.data(), outBuffer.size(), ppout);
+
+       EXCEPTION_GUARD_END
+}
+
+KEY_MANAGER_CAPI
+void ckmc_cipher_free(ckmc_cipher_ctx_h context)
+{
+       if (context == nullptr)
+               return;
+
+       auto ctx = reinterpret_cast<CipherCtx*>(context);
+       try {
+               ctx->mgr.reset();
+       } catch (const std::exception &e) {
+               LogError("STD exception " << e.what());
+       } catch (const abi::__forced_unwind &) {
+               LogDebug("abi::__forced_unwind caught. Thread cancelation.");
+               throw;
+       } catch (...) {
+               LogError("Unknown exception occured");
+       }
+
+       delete ctx;
+}
index 75da00f..9a89839 100644 (file)
@@ -832,4 +832,57 @@ int Manager::Impl::exportWrappedKey(const CryptoAlgorithm &params,
        EXCEPTION_GUARD_END
 }
 
+int Manager::Impl::initializeCipher(
+       const CryptoAlgorithm &params,
+       const Alias &keyAlias,
+       const Password &keyPassword,
+       bool encrypt,
+       int &requestId)
+{
+       EXCEPTION_GUARD_START_CPPAPI
+
+       AliasSupport helper(keyAlias);
+
+       return Request(*this,
+               EncryptionCommand::INITIALIZE_CIPHER,
+               m_encryptionConnection,
+               requestId,
+               CryptoAlgorithmSerializable(params),
+               helper.getName(),
+               helper.getOwner(),
+               keyPassword,
+               encrypt
+       ).maybeDeserialize(requestId);
+
+       EXCEPTION_GUARD_END
+}
+
+int Manager::Impl::updateCipher(int requestId, const RawBuffer &in, RawBuffer &out)
+{
+       EXCEPTION_GUARD_START_CPPAPI
+
+       return Request(*this,
+               EncryptionCommand::UPDATE_CIPHER,
+               m_encryptionConnection,
+               requestId,
+               in
+       ).maybeDeserialize(out);
+
+       EXCEPTION_GUARD_END
+}
+
+int Manager::Impl::finalizeCipher(int requestId, const RawBuffer &in, RawBuffer &out)
+{
+       EXCEPTION_GUARD_START_CPPAPI
+
+       return Request(*this,
+               EncryptionCommand::FINALIZE_CIPHER,
+               m_encryptionConnection,
+               requestId,
+               in
+       ).maybeDeserialize(out);
+
+       EXCEPTION_GUARD_END
+}
+
 } // namespace CKM
index c10b6b4..52ec1cc 100644 (file)
@@ -157,6 +157,14 @@ public:
                                                 KeyType &keyType,
                                                 RawBuffer &wrappedKey);
 
+       int initializeCipher(const CryptoAlgorithm &params,
+                                                const Alias &keyAlias,
+                                                const Password &keyPassword,
+                                                bool encrypt,
+                                                int &requestId);
+       int updateCipher(int requestId, const RawBuffer &in, RawBuffer &out);
+       int finalizeCipher(int requestId, const RawBuffer &in, RawBuffer &out);
+
 private:
        int saveBinaryData(
                const Alias &alias,
index ee0d368..46a822a 100644 (file)
@@ -314,7 +314,7 @@ int Manager::importWrappedKey(
        );
 }
 
-int  Manager::exportWrappedKey(
+int Manager::exportWrappedKey(
        const CryptoAlgorithm &params,
        const Alias &wrappingKeyAlias,
        const Password &wrappingKeyPassword,
@@ -334,6 +334,26 @@ int  Manager::exportWrappedKey(
        );
 }
 
+int Manager::initializeCipher(
+       const CryptoAlgorithm &params,
+       const Alias &keyAlias,
+       const Password &keyPassword,
+       bool encrypt,
+       int& requestId)
+{
+       return m_impl->initializeCipher(params, keyAlias, keyPassword, encrypt, requestId);
+}
+
+int Manager::updateCipher(int requestId, const RawBuffer &in, RawBuffer &out)
+{
+       return m_impl->updateCipher(requestId, in, out);
+}
+
+int Manager::finalizeCipher(int requestId, const RawBuffer &in, RawBuffer &out)
+{
+       return m_impl->finalizeCipher(requestId, in, out);
+}
+
 ManagerShPtr Manager::create()
 {
        try {
index 3b1cad8..4a2c2bb 100644 (file)
@@ -74,7 +74,10 @@ enum class LogicCommand : int {
 
 enum class EncryptionCommand : int {
        ENCRYPT,
-       DECRYPT
+       DECRYPT,
+       INITIALIZE_CIPHER,
+       UPDATE_CIPHER,
+       FINALIZE_CIPHER
 };
 
 // (client side) Alias = (service side) Owner::Name