Add serialization of CryptoAlgorithm 10/39110/2
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 8 May 2015 08:38:10 +0000 (10:38 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 8 May 2015 13:58:27 +0000 (15:58 +0200)
[Issue#] N/A
[Feature/Bug] N/A
[Problem] N/A
[Cause] CryptoAlgorithm needs serialization/deserialization methods.
[Solution] Serialization added.

[Verification] Run ckm-tests-internal --run_test=SERIALIZATION_TEST

Change-Id: I8556f366311f4e4a5255a33303bd7f42dc0cfcdd

src/manager/common/protocols.cpp
src/manager/common/protocols.h
tests/CMakeLists.txt
tests/test_serialization.cpp [new file with mode: 0644]

index 9dfb4c5..128f28f 100644 (file)
@@ -25,6 +25,7 @@
 #include <protocols.h>
 
 #include <dpl/serialization.h>
+#include <ckm/ckm-type.h>
 
 namespace CKM {
 
@@ -107,5 +108,68 @@ void PKCS12Serializable::Serialize(IStream &stream) const
         Serialization::Serialize(stream, it->getDER());
 };
 
+
+CryptoAlgorithmSerializable::CryptoAlgorithmSerializable() {}
+CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(CryptoAlgorithm &&algo) :
+        CryptoAlgorithm(std::move(algo))
+{
+}
+
+CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(IStream &stream)
+{
+    size_t plen = 0;
+    int type;
+    Deserializer<int,size_t>::Deserialize(stream, type, plen);
+    m_type = static_cast<AlgoType>(type);
+    while(plen) {
+        ParamName name;
+        uint64_t integer;
+        RawBuffer buffer;
+        int tmpName;
+        Deserializer<int>::Deserialize(stream, tmpName);
+        name = static_cast<ParamName>(tmpName);
+        switch (name) {
+        case ParamName::ED_IV:
+        case ParamName::ED_CTR:
+        case ParamName::ED_AAD:
+        case ParamName::ED_LABEL:
+            Deserializer<RawBuffer>::Deserialize(stream, buffer);
+            m_params.emplace(name, BufferParam::create(buffer));
+            break;
+
+        case ParamName::ED_CTR_LEN:
+        case ParamName::ED_TAG_LEN:
+        case ParamName::GEN_KEY_LEN:
+        case ParamName::GEN_EC:
+        case ParamName::SV_HASH_ALGO:
+        case ParamName::SV_RSA_PADDING:
+            Deserializer<uint64_t>::Deserialize(stream, integer);
+            m_params.emplace(name, IntParam::create(integer));
+            break;
+
+        default:
+            ThrowMsg(UnsupportedParam, "Unsupported param name");
+        }
+        plen--;
+    }
+}
+
+void CryptoAlgorithmSerializable::Serialize(IStream &stream) const
+{
+    Serializer<int,size_t>::Serialize(stream, static_cast<int>(m_type), m_params.size());
+    for(const auto& it : m_params) {
+        Serializer<int>::Serialize(stream, static_cast<int>(it.first));
+        uint64_t integer;
+        RawBuffer buffer;
+        if (CKM_API_SUCCESS == it.second->getInt(integer))
+            Serializer<uint64_t>::Serialize(stream, integer);
+        else if (CKM_API_SUCCESS == it.second->getBuffer(buffer))
+            Serializer<RawBuffer>::Serialize(stream, buffer);
+        else
+            ThrowMsg(UnsupportedParam, "Unsupported param type");
+    }
+
+}
+
 } // namespace CKM
 
index fa6c8a4..3739f12 100644 (file)
@@ -269,5 +269,16 @@ struct COMMON_API PKCS12Serializable : public PKCS12Impl, ISerializable {
     void Serialize(IStream &) const;
 };
 
+struct COMMON_API CryptoAlgorithmSerializable : public CryptoAlgorithm, ISerializable {
+    DECLARE_EXCEPTION_TYPE(Exception, Base);
+    DECLARE_EXCEPTION_TYPE(Exception, UnsupportedParam);
+
+    CryptoAlgorithmSerializable();
+    explicit CryptoAlgorithmSerializable(CryptoAlgorithm &&);
+    explicit CryptoAlgorithmSerializable(IStream &);
+
+    void Serialize(IStream &) const;
+};
+
 } // namespace CKM
 
index 985688c..8c2b2b9 100644 (file)
@@ -33,6 +33,7 @@ SET(TEST_MERGED_SOURCES
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_safe-buffer.cpp
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_descriptor-set.cpp
     ${KEY_MANAGER_TEST_MERGED_SRC}/test_comm-manager.cpp
+    ${KEY_MANAGER_TEST_MERGED_SRC}/test_serialization.cpp
     ${KEY_MANAGER_PATH}/service/db-crypto.cpp
     ${KEY_MANAGER_PATH}/service/key-provider.cpp
     ${KEY_MANAGER_PATH}/client-async/descriptor-set.cpp
diff --git a/tests/test_serialization.cpp b/tests/test_serialization.cpp
new file mode 100644 (file)
index 0000000..6181102
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file       test_serialization.cpp
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#include <string>
+
+#include <boost/test/unit_test.hpp>
+#include <test_common.h>
+
+#include <ckm/ckm-raw-buffer.h>
+#include <protocols.h>
+#include <message-buffer.h>
+
+using namespace CKM;
+
+namespace {
+std::string IV_STR("1234567890123456");
+std::string AAD_STR("sdfdsgsghrtkghwiuho3irhfoewituhre");
+RawBuffer IV(IV_STR.begin(), IV_STR.end());
+RawBuffer AAD(AAD_STR.begin(), AAD_STR.end());
+
+struct BrokenParam : public BaseParam {
+    static BaseParamPtr create() { return BaseParamPtr(new BrokenParam()); }
+};
+
+} // namespace anonymous
+
+BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST)
+
+BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_positive) {
+    CryptoAlgorithm ca;
+    ca.m_type = AlgoType::AES_GCM;
+    ca.m_params.emplace(ParamName::ED_IV, BufferParam::create(IV));
+    ca.m_params.emplace(ParamName::ED_TAG_LEN, IntParam::create(128));
+    ca.m_params.emplace(ParamName::ED_AAD, BufferParam::create(AAD));
+
+    CryptoAlgorithmSerializable input(std::move(ca));
+    CryptoAlgorithmSerializable output;
+    auto msg = MessageBuffer::Serialize(input);
+    RawBuffer buffer = msg.Pop();
+    MessageBuffer resp;
+    resp.Push(buffer);
+    resp.Deserialize(output);
+
+    BOOST_REQUIRE_MESSAGE(input.m_type == output.m_type,
+                          "Algorithm types don't match: " << static_cast<int>(input.m_type) << "!="
+                          << static_cast<int>(output.m_type));
+
+    // compare params
+    auto iit = input.m_params.cbegin();
+    auto oit = output.m_params.cbegin();
+    for(;iit != input.m_params.cend() && oit != output.m_params.cend(); iit++, oit++ )
+    {
+        BOOST_REQUIRE_MESSAGE(iit->first == oit->first,
+                              "Param names do not match :" << static_cast<int>(iit->first) << "!="
+                              << static_cast<int>(oit->first));
+        uint64_t integer[2];
+        RawBuffer buffer[2];
+        if(CKM_API_SUCCESS == iit->second->getInt(integer[0]))
+        {
+            BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == oit->second->getInt(integer[1]),
+                                  "Param types do not match");
+            BOOST_REQUIRE_MESSAGE(integer[0] == integer[1], "Integer params do not match");
+        }
+        else if(CKM_API_SUCCESS == iit->second->getBuffer(buffer[0]))
+        {
+            BOOST_REQUIRE_MESSAGE(CKM_API_SUCCESS == oit->second->getBuffer(buffer[1]),
+                                  "Param types do not match");
+            BOOST_REQUIRE_MESSAGE(buffer[0] == buffer[1], "Integer params do not match");
+        }
+        else
+            BOOST_FAIL("Wrong param type");
+    }
+}
+
+BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_broken_param) {
+    CryptoAlgorithm ca;
+    ca.m_type = AlgoType::AES_GCM;
+    // unuspported param type
+    ca.m_params.emplace(ParamName::ED_IV, BrokenParam::create());
+
+    CryptoAlgorithmSerializable input(std::move(ca));
+    BOOST_REQUIRE_THROW(auto buffer = MessageBuffer::Serialize(input),
+                        CryptoAlgorithmSerializable::UnsupportedParam);
+}
+
+BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) {
+    CryptoAlgorithm ca;
+    ca.m_type = AlgoType::AES_GCM;
+    // unuspported param name
+    ca.m_params.emplace(static_cast<ParamName>(666), IntParam::create(666));
+
+    CryptoAlgorithmSerializable input(std::move(ca));
+    CryptoAlgorithmSerializable output;
+    auto msg = MessageBuffer::Serialize(input);
+    RawBuffer buffer = msg.Pop();
+    MessageBuffer resp;
+    resp.Push(buffer);
+    BOOST_REQUIRE_THROW(resp.Deserialize(output), CryptoAlgorithmSerializable::UnsupportedParam);
+}
+
+BOOST_AUTO_TEST_SUITE_END()