Service implementation.
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Mon, 2 Jun 2014 10:05:52 +0000 (12:05 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:56:41 +0000 (14:56 +0200)
* Add support for ADD, GET, REMOVE commands.
* Rename StorageCommand to LogicCommand.
* Move KeyImpl from client library to common.

Change-Id: Icd86f5dd6f7442565e542d637bf6bda9fd389aa1

14 files changed:
src/include/ckm/ckm-type.h
src/include/ckm/key-manager.h
src/manager/client/client-key-impl.cpp
src/manager/client/client-key-impl.h
src/manager/client/client-key.cpp
src/manager/client/client-manager-impl.cpp
src/manager/client/client-manager-impl.h
src/manager/common/connection-info.h
src/manager/common/protocols.cpp
src/manager/common/protocols.h
src/manager/service/ckm-logic.cpp
src/manager/service/ckm-logic.h
src/manager/service/ckm-service.cpp
src/manager/service/ckm-service.h

index 7308613..88f92f8 100644 (file)
@@ -69,5 +69,8 @@ enum class RSAPaddingAlgorithm : int {
     XRSA_X931_PADDING
 };
 
+// Internal types
+class KeyImpl;
+
 } // namespace CKM
 
index 64f9bc2..ab0b29e 100644 (file)
@@ -64,7 +64,6 @@ private:
 
 class Key {
 public:
-    class KeyImpl;
     enum class ECType : unsigned int {
         prime192v1
           // TODO
@@ -72,10 +71,8 @@ public:
 
     Key();
     Key(const RawData &rawData, KeyType type, const RawData &password = RawData()); // Import key
-    Key(const Key &key) = delete;
-    Key(Key &&key) = delete;
-    Key& operator=(const Key &key) = delete;
-    Key& operator=(Key &&key) = delete;
+    Key(const Key &key);
+    Key& operator=(const Key &key);
     virtual ~Key(); // This destructor must overwrite memory used by key with some random data.
 
     bool empty() const;
index 93df029..6381306 100644 (file)
@@ -31,40 +31,40 @@ const char PEM_FIRST_CHAR = '-';
 
 namespace CKM {
 
-Key::KeyImpl::KeyImpl()
+KeyImpl::KeyImpl()
   : m_type(KeyType::KEY_NONE)
 {}
 
-Key::KeyImpl::KeyImpl(const KeyImpl &second)
+KeyImpl::KeyImpl(const KeyImpl &second)
   : m_type(second.m_type)
   , m_key(second.m_key)
 {}
 
-Key::KeyImpl::KeyImpl(KeyImpl &&second)
+KeyImpl::KeyImpl(KeyImpl &&second)
   : m_type(second.m_type)
   , m_key(std::move(second.m_key))
 {}
 
-Key::KeyImpl& Key::KeyImpl::operator=(const KeyImpl &second) {
+KeyImpl& KeyImpl::operator=(const KeyImpl &second) {
     m_type = second.m_type;
     m_key = second.m_key;
     return *this;
 }
 
-Key::KeyImpl& Key::KeyImpl::operator=(KeyImpl &&second) {
+KeyImpl& KeyImpl::operator=(KeyImpl &&second) {
     m_type = std::move(second.m_type);
     m_key = std::move(second.m_key);
     return *this;
 }
 
-Key::KeyImpl::KeyImpl(IStream &stream) {
-    int type;
-    Deserialization::Deserialize(stream, type);
-    Deserialization::Deserialize(stream, m_key);
-    m_type = static_cast<KeyType>(type);
-}
+//KeyImpl::KeyImpl(IStream &stream) {
+//    int type;
+//    Deserialization::Deserialize(stream, type);
+//    Deserialization::Deserialize(stream, m_key);
+//    m_type = static_cast<KeyType>(type);
+//}
 
-Key::KeyImpl::KeyImpl(const RawData &data, KeyType type, const RawData &password)
+KeyImpl::KeyImpl(const RawData &data, KeyType type, const RawData &password)
   : m_type(KeyType::KEY_NONE)
 {
     int size = 0;
@@ -118,12 +118,12 @@ Key::KeyImpl::KeyImpl(const RawData &data, KeyType type, const RawData &password
     BIO_free_all(bio);
 }
 
-void Key::KeyImpl::Serialize(IStream &stream) const {
-    Serialization::Serialize(stream, static_cast<int>(m_type));
-    Serialization::Serialize(stream, m_key);
-}
+//void KeyImpl::Serialize(IStream &stream) const {
+//    Serialization::Serialize(stream, static_cast<int>(m_type));
+//    Serialization::Serialize(stream, m_key);
+//}
 
-Key::KeyImpl::~KeyImpl(){}
+KeyImpl::~KeyImpl(){}
 
 } // namespace CKM
 
index 89afe01..5429be2 100644 (file)
  */
 #pragma once
 
-#include <dpl/serialization.h>
+//#include <dpl/serialization.h>
 
 #include <ckm/ckm-type.h>
 #include <ckm/key-manager.h>
 
 namespace CKM {
 
-class Key::KeyImpl : public ISerializable {
+class KeyImpl
+//  : public ISerializable
+{
 public:
     KeyImpl();
-    KeyImpl(IStream &stream);
+//  KeyImpl(IStream &stream);
     KeyImpl(const RawData &data, KeyType type, const RawData &password);
     KeyImpl(const KeyImpl &);
     KeyImpl(KeyImpl &&);
@@ -46,10 +48,10 @@ public:
     }
 
     bool empty() const {
-        return m_key.empty();
+        return (m_type == KeyType::KEY_NONE) || m_key.empty();
     }
 
-    void Serialize(IStream &stream) const;
+//    void Serialize(IStream &stream) const;
 
     virtual ~KeyImpl();
 private:
index 8a22e65..d0af14e 100644 (file)
@@ -18,6 +18,7 @@
  * @version     1.0
  * @brief       Key - api implementation.
  */
+#include <ckm/ckm-type.h>
 #include <ckm/key-manager.h>
 
 #include <client-key-impl.h>
@@ -35,22 +36,40 @@ Key::Key(
   : m_impl(new KeyImpl(rawData, type, password))
 {}
 
-Key::~Key(){}
+Key::Key(const Key &second) {
+    m_impl = second.m_impl;
+}
+
+Key& Key::operator=(const Key &second) {
+    m_impl = second.m_impl;
+    return *this;
+}
+
+Key::~Key(){
+}
 
 bool Key::empty() const {
-    return m_impl->empty();
+    if (m_impl)
+        return m_impl->empty();
+    return true;
 }
 
 KeyType Key::getType() const {
-    return m_impl->getType();
+    if (m_impl)
+        return m_impl->getType();
+    return KeyType::KEY_NONE;
 }
 
 RawData Key::getKey() const {
-    return m_impl->getKey();
+    if (m_impl)
+        return m_impl->getKey();
+    return RawData();
 }
 
-Key::KeyImpl* Key::getImpl() const {
-    return m_impl.get();
+KeyImpl* Key::getImpl() const {
+    if (m_impl)
+        return m_impl.get();
+    return NULL;
 };
 
 
index ec5c4ec..ee28f91 100644 (file)
  * @version     1.0
  * @brief       Manager implementation.
  */
+#include <dpl/serialization.h>
+
 #include <client-manager-impl.h>
-#include <message-buffer.h>
 #include <client-common.h>
-#include <dpl/serialization.h>
-#include <protocols.h>
 #include <client-key-impl.h>
+#include <message-buffer.h>
+#include <protocols.h>
 
 namespace CKM {
 
@@ -35,11 +36,11 @@ int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Poli
             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
 
         MessageBuffer send, recv;
-        Serialization::Serialize(send, static_cast<int>(StorageCommand::SAVE));
+        Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
         Serialization::Serialize(send, m_counter);
         Serialization::Serialize(send, static_cast<int>(toDBDataType(key.getType())));
         Serialization::Serialize(send, alias);
-        Serialization::Serialize(send, key.getImpl());
+        Serialization::Serialize(send, key.getKey());
         Serialization::Serialize(send, PolicySerializable(policy));
 
         int retCode = sendToServer(
@@ -73,7 +74,7 @@ int Manager::ManagerImpl::removeKey(const Alias &alias) {
             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
 
         MessageBuffer send, recv;
-        Serialization::Serialize(send, static_cast<int>(StorageCommand::REMOVE));
+        Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
         Serialization::Serialize(send, m_counter);
         Serialization::Serialize(send, static_cast<int>(DBDataType::KEY_RSA_PUBLIC));
         Serialization::Serialize(send, alias);
@@ -103,15 +104,21 @@ int Manager::ManagerImpl::removeKey(const Alias &alias) {
     });
 }
 
-int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Key &key) {
+int Manager::ManagerImpl::getBinaryData(
+    const Alias &alias,
+    DBDataType sendDataType,
+    const RawData &password,
+    DBDataType &recvDataType,
+    RawData &rawData)
+{
     return try_catch([&] {
         if (alias.empty())
             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
 
         MessageBuffer send, recv;
-        Serialization::Serialize(send, static_cast<int>(StorageCommand::GET));
+        Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
         Serialization::Serialize(send, m_counter);
-        Serialization::Serialize(send, static_cast<int>(DBDataType::KEY_RSA_PUBLIC));
+        Serialization::Serialize(send, static_cast<int>(sendDataType));
         Serialization::Serialize(send, alias);
         Serialization::Serialize(send, password);
 
@@ -132,8 +139,12 @@ int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Ke
         Deserialization::Deserialize(recv, opType);
         Deserialization::Deserialize(recv, retCode);
 
-        if (retCode == KEY_MANAGER_API_SUCCESS)
-            Deserialization::Deserialize(recv, *(key.getImpl()));
+        if (retCode == KEY_MANAGER_API_SUCCESS) {
+            int tmpDataType;
+            Deserialization::Deserialize(recv, tmpDataType);
+            Deserialization::Deserialize(recv, rawData);
+            recvDataType = static_cast<DBDataType>(tmpDataType);
+        }
 
         if (counter != m_counter) {
             return KEY_MANAGER_API_ERROR_UNKNOWN;
@@ -143,5 +154,29 @@ int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Ke
     });
 }
 
+int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Key &key) {
+    DBDataType recvDataType;
+    RawData rawData;
+
+    int retCode = getBinaryData(
+        alias,
+        DBDataType::KEY_RSA_PUBLIC,
+        password,
+        recvDataType,
+        rawData);
+
+    if (retCode != KEY_MANAGER_API_SUCCESS)
+        return retCode;
+
+    Key keyParsed(rawData, toKeyType(recvDataType));
+
+    if (keyParsed.empty())
+        return KEY_MANAGER_API_ERROR_BAD_RESPONSE;
+
+    key = keyParsed;
+
+    return KEY_MANAGER_API_SUCCESS;
+}
+
 } // namespace CKM
 
index 8e8f0d9..fa910ad 100644 (file)
@@ -20,6 +20,9 @@
  */
 #pragma once
 
+#include <protocols.h>
+
+#include <ckm/ckm-type.h>
 #include <ckm/key-manager.h>
 
 namespace CKM {
@@ -35,7 +38,14 @@ public:
     int removeKey(const Alias &alias);
     int getKey(const Alias &alias, const RawData &password, Key &key);
 
-private:
+protected:
+    int getBinaryData(
+        const Alias &alias,
+        DBDataType sendDataType,
+        const RawData &password,
+        DBDataType &recvDataType,
+        RawData &rawData);
+
     int m_counter;
 };
 
index b824a35..dc12ac0 100644 (file)
 
 namespace CKM
 {
+    struct Credentials {
+        std::string realUser;
+        std::string realSmackLabel;
+    };
+
     struct ConnectionInfo {
         InterfaceID interfaceID;
         MessageBuffer buffer;
+        Credentials credentials;
     };
 
     typedef std::map<int, ConnectionInfo> ConnectionInfoMap;
index 89ea258..04367de 100644 (file)
@@ -43,6 +43,20 @@ DBDataType toDBDataType(KeyType key) {
     }
 }
 
+KeyType toKeyType(DBDataType dbtype) {
+    switch(dbtype) {
+    case DBDataType::KEY_RSA_PUBLIC: return KeyType::KEY_RSA_PUBLIC;
+    case DBDataType::KEY_RSA_PRIVATE: return KeyType::KEY_RSA_PRIVATE;
+    default:
+        // TODO
+        throw 1;
+    }
+}
+
+PolicySerializable::PolicySerializable()
+{}
+
+
 PolicySerializable::PolicySerializable(const Policy &policy)
   : Policy(policy)
 {}
index 860feb5..6a71c5b 100644 (file)
@@ -40,7 +40,7 @@ enum class ControlCommand : int {
     RESET_USER_PASSWORD
 };
 
-enum class StorageCommand : int {
+enum class LogicCommand : int {
     GET,
     SAVE,
     REMOVE,
@@ -57,10 +57,12 @@ enum class DBDataType : int {
 };
 
 DBDataType toDBDataType(KeyType key);
+KeyType toKeyType(DBDataType dbDataType);
 
 class IStream;
 
 struct PolicySerializable : public Policy, ISerializable {
+    PolicySerializable();
     explicit PolicySerializable(const Policy &);
     explicit PolicySerializable(IStream &);
     void Serialize(IStream &) const;
index 8609a87..ab1806a 100644 (file)
@@ -62,5 +62,50 @@ RawBuffer CKMLogic::resetUserPassword(
     return RawBuffer();
 }
 
+RawBuffer CKMLogic::saveData(
+    Credentials &cred,
+    int commandId,
+    DBDataType dataType,
+    const Alias &alias,
+    const RawData &key,
+    const PolicySerializable &policy)
+{
+    (void)cred;
+    (void)commandId;
+    (void)dataType;
+    (void)alias;
+    (void)key;
+    (void)policy;
+    return RawData();
+}
+
+RawBuffer CKMLogic::removeData(
+    Credentials &cred,
+    int commandId,
+    DBDataType dataType,
+    const Alias &alias)
+{
+    (void)cred;
+    (void)commandId;
+    (void)dataType;
+    (void)alias;
+    return RawData();
+}
+
+RawBuffer CKMLogic::getData(
+    Credentials &cred,
+    int commandId,
+    DBDataType dataType,
+    const Alias &alias,
+    const RawData &password)
+{
+    (void)cred;
+    (void)commandId;
+    (void)dataType;
+    (void)alias;
+    (void)password;
+    return RawData();
+}
+
 } // namespace CKM
 
index 657dab3..f295333 100644 (file)
@@ -24,6 +24,9 @@
 #include <string>
 #include <vector>
 #include <message-buffer.h>
+#include <protocols.h>
+#include <ckm/ckm-type.h>
+#include <connection-info.h>
 
 namespace CKM {
 
@@ -50,6 +53,28 @@ public:
     RawBuffer resetUserPassword(
         const std::string &user,
         const RawBuffer &newPassword);
+
+    RawBuffer saveData(
+        Credentials &cred,
+        int commandId,
+        DBDataType dataType,
+        const Alias &alias,
+        const RawData &key,
+        const PolicySerializable &policy);
+
+    RawBuffer removeData(
+        Credentials &cred,
+        int commandId,
+        DBDataType dataType,
+        const Alias &alias);
+
+    RawBuffer getData(
+        Credentials &cred,
+        int commandId,
+        DBDataType dataType,
+        const Alias &alias,
+        const RawData &password);
+
 private:
 
 };
index b58349e..5460d54 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <ckm-service.h>
 #include <ckm-logic.h>
+#include <client-key-impl.h>
 
 namespace {
 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
@@ -68,25 +69,24 @@ void CKMService::process(const ReadEvent &event) {
     LogDebug("Read event");
     auto &info = m_connectionInfoMap[event.connectionID.counter];
     info.buffer.Push(event.rawBuffer);
-    while(processOne(event.connectionID, info.buffer, info.interfaceID));
+    while(processOne(event.connectionID, info));
 }
 
 bool CKMService::processOne(
     const ConnectionID &conn,
-    MessageBuffer &buffer,
-    InterfaceID interfaceID)
+    ConnectionInfo &info)
 {
     LogDebug ("process One");
     RawBuffer response;
 
     Try {
-        if (!buffer.Ready())
+        if (!info.buffer.Ready())
             return false;
 
-        if (interfaceID == SOCKET_ID_CONTROL)
-            response = processControl(buffer);
+        if (info.interfaceID == SOCKET_ID_CONTROL)
+            response = processControl(info.buffer);
         else
-            response = processStorage(conn, buffer);
+            response = processStorage(info.credentials, info.buffer);
 
         m_serviceManager->Write(conn, response);
 
@@ -133,10 +133,63 @@ RawBuffer CKMService::processControl(MessageBuffer &buffer) {
     }
 }
 
-RawBuffer CKMService::processStorage(const ConnectionID &conn, MessageBuffer &buffer){
-    (void)conn;
-    (void)buffer;
-    return RawBuffer();
+RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
+    int command;
+    int commandId;
+    int tmpDataType;
+    Alias alias;
+    std::string user;
+    LogicCommand sc;
+
+    Deserialization::Deserialize(buffer, command);
+    Deserialization::Deserialize(buffer, commandId);
+
+    sc = static_cast<LogicCommand>(command);
+
+    switch(sc) {
+        case LogicCommand::SAVE:
+        {
+            RawData rawData;
+            PolicySerializable policy;
+            Deserialization::Deserialize(buffer, tmpDataType);
+            Deserialization::Deserialize(buffer, alias);
+            Deserialization::Deserialize(buffer, rawData);
+            Deserialization::Deserialize(buffer, policy);
+            return m_logic->saveData(
+                cred,
+                commandId,
+                static_cast<DBDataType>(tmpDataType),
+                alias,
+                rawData,
+                policy);
+        }
+        case LogicCommand::REMOVE:
+        {
+            Deserialization::Deserialize(buffer, tmpDataType);
+            Deserialization::Deserialize(buffer, alias);
+            return m_logic->removeData(
+                cred,
+                commandId,
+                static_cast<DBDataType>(tmpDataType),
+                alias);
+        }
+        case LogicCommand::GET:
+        {
+            RawData password;
+            Deserialization::Deserialize(buffer, tmpDataType);
+            Deserialization::Deserialize(buffer, alias);
+            Deserialization::Deserialize(buffer, password);
+            return m_logic->getData(
+                cred,
+                commandId,
+                static_cast<DBDataType>(tmpDataType),
+                alias,
+                password);
+        }
+        default:
+        // TODO
+            throw 1; // broken protocol
+    }
 }
 
 
index 2c7a66a..770e954 100644 (file)
@@ -56,14 +56,13 @@ public:
 private:
     bool processOne(
         const ConnectionID &conn,
-        MessageBuffer &buffer,
-        InterfaceID interfaceID);
+        ConnectionInfo &info);
 
     RawBuffer processControl(
         MessageBuffer &buffer);
 
     RawBuffer processStorage(
-        const ConnectionID &conn,
+        Credentials &cred,
         MessageBuffer &buffer);
 
     ConnectionInfoMap m_connectionInfoMap;