Simplify serialization calls 19/294619/2
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 13 Jun 2023 14:47:07 +0000 (16:47 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 21 Jun 2023 20:06:36 +0000 (22:06 +0200)
Remove unnecessary casts and use type deduction.

Change-Id: I88d8ae4b8060a509984a6883289c4bb05a9c1015

src/manager/client-async/client-manager-async-impl.cpp
src/manager/client-async/client-manager-async-impl.h
src/manager/client/client-control.cpp
src/manager/client/client-manager-impl.cpp
src/manager/common/message-buffer.cpp
src/manager/common/protocols.cpp
src/manager/service/ckm-service.cpp

index f5082ec..92a18e4 100644 (file)
@@ -314,7 +314,7 @@ void ManagerAsync::Impl::createKeyAES(const ObserverPtr &observer,
                AliasSupport aliasHelper(keyAlias);
 
                sendToStorage(LogicCommand::CREATE_KEY_AES, observer,
-                                         static_cast<int>(size), PolicySerializable(policyKey),
+                                         size, PolicySerializable(policyKey),
                                          aliasHelper.getName(), aliasHelper.getOwner());
        });
 }
@@ -341,13 +341,12 @@ void ManagerAsync::Impl::crypt(
                CryptoAlgorithmSerializable cas(algo);
                m_counter++;
 
-               auto command = static_cast<int>(encryption ?
-                       EncryptionCommand::ENCRYPT : EncryptionCommand::DECRYPT);
+               auto command = encryption ? EncryptionCommand::ENCRYPT : EncryptionCommand::DECRYPT;
                auto send = SerializeMessage(command, m_counter, cas, helper.getName(), helper.getOwner(),
                                                                         password, input);
 
                thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_ENCRYPTION,
-                                                                                  std::move(send), m_counter, command));
+                                                                                  std::move(send), m_counter, static_cast<int>(command)));
        });
 }
 
index eb354ef..3caf47c 100644 (file)
@@ -155,7 +155,7 @@ private:
        void sendToStorage(LogicCommand command, const ObserverPtr &observer, Args &&... args)
        {
                m_counter++;
-               auto send = SerializeMessage(static_cast<int>(command), m_counter, std::forward<Args>(args)...);
+               auto send = SerializeMessage(command, m_counter, std::forward<Args>(args)...);
                thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_CKM_STORAGE,
                                                                                   std::move(send), m_counter, static_cast<int>(command)));
        }
index d0a4d18..50c1870 100644 (file)
@@ -46,9 +46,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::UNLOCK_USER_KEY),
-                                                                        user,
-                                                                        password);
+               auto send = SerializeMessage(ControlCommand::UNLOCK_USER_KEY, user, password);
 
                int retCode = m_controlConnection.processRequest(send, recv);
 
@@ -70,7 +68,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::LOCK_USER_KEY), user);
+               auto send = SerializeMessage(ControlCommand::LOCK_USER_KEY, user);
 
                int retCode = m_controlConnection.processRequest(send, recv);
 
@@ -92,7 +90,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::REMOVE_USER_DATA), user);
+               auto send = SerializeMessage(ControlCommand::REMOVE_USER_DATA, user);
 
                int retCode = m_controlConnection.processRequest(send, recv);
 
@@ -115,7 +113,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD),
+               auto send = SerializeMessage(ControlCommand::CHANGE_USER_PASSWORD,
                                                                         user,
                                                                         oldPassword,
                                                                         newPassword);
@@ -140,7 +138,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::RESET_USER_PASSWORD),
+               auto send = SerializeMessage(ControlCommand::RESET_USER_PASSWORD,
                                                                         user,
                                                                         newPassword);
 
@@ -164,7 +162,7 @@ public:
                        return CKM_API_ERROR_INPUT_PARAM;
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::REMOVE_APP_DATA), owner);
+               auto send = SerializeMessage(ControlCommand::REMOVE_APP_DATA, owner);
 
                int retCode = m_controlConnection.processRequest(send, recv);
 
@@ -183,7 +181,7 @@ public:
                EXCEPTION_GUARD_START_CPPAPI
 
                MessageBuffer recv;
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::UPDATE_CC_MODE));
+               auto send = SerializeMessage(ControlCommand::UPDATE_CC_MODE);
 
                int retCode = m_controlConnection.processRequest(send, recv);
 
@@ -206,8 +204,8 @@ public:
 
                MessageBuffer recv;
                AliasSupport helper(alias);
-               auto send = SerializeMessage(static_cast<int>(ControlCommand::SET_PERMISSION),
-                                                                        static_cast<int>(user),
+               auto send = SerializeMessage(ControlCommand::SET_PERMISSION,
+                                                                        user,
                                                                         helper.getName(),
                                                                         helper.getOwner(),
                                                                         accessor,
index 4d6e59e..0204028 100644 (file)
@@ -101,7 +101,7 @@ Manager::Impl::Request::Request(Manager::Impl &impl, Cmd cmd, CKM::ServiceConnec
 {
        static_assert(sizeof cmd <= sizeof(int));
        const auto msgId = m_msgId = ++impl.m_counter;
-       m_retCode = doRequest(m_recv, conn, static_cast<int>(cmd), msgId, std::forward<T>(t)...);
+       m_retCode = doRequest(m_recv, conn, cmd, msgId, std::forward<T>(t)...);
 }
 
 Manager::Impl::Request::operator bool() const {
@@ -216,8 +216,7 @@ int Manager::Impl::getPKCS12(const Alias &alias, const Password &keyPass,
        int msgId = ++m_counter;
        MessageBuffer recv;
 
-       int retCode = doRequest(recv, m_storageConnection,
-                       static_cast<int>(LogicCommand::GET_PKCS12), msgId,
+       int retCode = doRequest(recv, m_storageConnection, LogicCommand::GET_PKCS12, msgId,
                        helper.getName(), helper.getOwner(), keyPass, certPass);
 
        if (CKM_API_SUCCESS != retCode)
@@ -476,7 +475,7 @@ int Manager::Impl::createKeyAES(
        AliasSupport aliasHelper(keyAlias);
 
        return Request(*this, LogicCommand::CREATE_KEY_AES, m_storageConnection,
-                       static_cast<int>(size), PolicySerializable(policyKey),
+                       size, PolicySerializable(policyKey),
                        aliasHelper.getName(), aliasHelper.getOwner()
                ).maybeDeserialize();
 
index 237f716..4dc5c16 100644 (file)
@@ -80,7 +80,7 @@ void MessageSerializer::Sizer::Write(size_t num, const void*)
 MessageSerializer::Writer::Writer(size_t size)
 {
        m_buffer.reserve(sizeof(size_t) + size);
-       Serializer<size_t>::Serialize(*this, size);
+       Serialization::Serialize(*this, size);
 }
 
 void MessageSerializer::Writer::Read(size_t, void*)
index d402548..d270f0c 100644 (file)
@@ -51,16 +51,14 @@ void PolicySerializable::Serialize(IStream &stream) const
 {
        Serialization::Serialize(stream, password);
        Serialization::Serialize(stream, extractable);
-       Serialization::Serialize(stream, static_cast<int>(backend));
+       Serialization::Serialize(stream, backend);
 }
 
 void PolicySerializable::Deserialize(IStream &stream)
 {
-       int policyBackend;
        Deserialization::Deserialize(stream, password);
        Deserialization::Deserialize(stream, extractable);
-       Deserialization::Deserialize(stream, policyBackend);
-       backend = static_cast<PolicyBackend>(policyBackend);
+       Deserialization::Deserialize(stream, backend);
 }
 
 PKCS12Serializable::PKCS12Serializable()
@@ -212,15 +210,13 @@ CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(IStream &stream)
 void CryptoAlgorithmSerializable::Deserialize(IStream &stream)
 {
        size_t plen = 0;
-       Deserializer<size_t>::Deserialize(stream, plen);
+       Deserialization::Deserialize(stream, plen);
 
        while (plen) {
                ParamName name;
                uint64_t integer;
                RawBuffer buffer;
-               int tmpName;
-               Deserializer<int>::Deserialize(stream, tmpName);
-               name = static_cast<ParamName>(tmpName);
+               Deserialization::Deserialize(stream, name);
 
                switch (name) {
                case ParamName::ED_IV:
@@ -230,7 +226,7 @@ void CryptoAlgorithmSerializable::Deserialize(IStream &stream)
                case ParamName::KBKDF_CONTEXT:
                case ParamName::KBKDF_FIXED_INPUT:
                case ParamName::ECDH_PUBKEY:
-                       Deserializer<RawBuffer>::Deserialize(stream, buffer);
+                       Deserialization::Deserialize(stream, buffer);
                        setParam(name, buffer);
                        break;
 
@@ -248,7 +244,7 @@ void CryptoAlgorithmSerializable::Deserialize(IStream &stream)
                case ParamName::KBKDF_LLEN:
                case ParamName::KBKDF_COUNTER_LOCATION:
                case ParamName::KBKDF_NO_SEPARATOR:
-                       Deserializer<uint64_t>::Deserialize(stream, integer);
+                       Deserialization::Deserialize(stream, integer);
                        setParam(name, integer);
                        break;
 
@@ -262,17 +258,17 @@ void CryptoAlgorithmSerializable::Deserialize(IStream &stream)
 
 void CryptoAlgorithmSerializable::Serialize(IStream &stream) const
 {
-       Serializer<size_t>::Serialize(stream, m_params.size());
+       Serialization::Serialize(stream, m_params.size());
 
        for (const auto &it : m_params) {
-               Serializer<int>::Serialize(stream, static_cast<int>(it.first));
+               Serialization::Serialize(stream, it.first);
                uint64_t integer;
                RawBuffer buffer;
 
                if (it.second->getInt(integer))
-                       Serializer<uint64_t>::Serialize(stream, integer);
+                       Serialization::Serialize(stream, integer);
                else if (it.second->getBuffer(buffer))
-                       Serializer<RawBuffer>::Serialize(stream, buffer);
+                       Serialization::Serialize(stream, buffer);
                else
                        ThrowMsg(UnsupportedParam, "Unsupported param type");
        }
index 7bfebbe..1803635 100644 (file)
@@ -116,21 +116,18 @@ bool CKMService::ProcessOne(
 
 RawBuffer CKMService::ProcessControl(MessageBuffer &buffer, bool allowed)
 {
-       int command = 0;
        uid_t user = 0;
-       ControlCommand cc;
+       ControlCommand command;
        Password newPass, oldPass;
        ClientId explicitOwner;
 
        buffer.Deserialize(command);
 
-       LogDebug("Process control. Command: " << command);
+       LogDebug("Process control. Command: " << static_cast<int>(command));
 
        std::function<RawBuffer(void)> logicFunc;
 
-       cc = static_cast<ControlCommand>(command);
-
-       switch (cc) {
+       switch (command) {
        case ControlCommand::UNLOCK_USER_KEY:
                buffer.Deserialize(user, newPass);
                logicFunc = [&]() {
@@ -213,7 +210,7 @@ RawBuffer CKMService::ProcessControl(MessageBuffer &buffer, bool allowed)
 
 RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
 {
-       int command = 0;
+       LogicCommand command;
        int msgId = 0;
        DataType tmpDataType;
        Name name;
@@ -230,9 +227,9 @@ RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
        Password nullPassword("");
        m_logic->unlockUserKey(cred.clientUid, nullPassword);
 
-       LogDebug("Process storage. Command: " << command);
+       LogDebug("Process storage. Command: " << static_cast<int>(command));
 
-       switch (static_cast<LogicCommand>(command)) {
+       switch (command) {
        case LogicCommand::SAVE: {
                RawBuffer rawData;
                PolicySerializable policy;