#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,
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;
+}
EXCEPTION_GUARD_END
}
+int Manager::Impl::initializeCipher(
+ const CryptoAlgorithm ¶ms,
+ 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
);
}
-int Manager::exportWrappedKey(
+int Manager::exportWrappedKey(
const CryptoAlgorithm ¶ms,
const Alias &wrappingKeyAlias,
const Password &wrappingKeyPassword,
);
}
+int Manager::initializeCipher(
+ const CryptoAlgorithm ¶ms,
+ 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 {