Integration with CryptoService class.
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Mon, 16 Jun 2014 16:27:03 +0000 (18:27 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:58:24 +0000 (14:58 +0200)
Implementation of CKMLogic::createPairKeyRSA.
Replace KeyImpl with GenericKey class.

Change-Id: I24d2c89c3df702666b0b82ba2894ed6313e6393b

19 files changed:
src/include/ckm/ckm-type.h
src/include/ckm/key-manager.h
src/manager/CMakeLists.txt
src/manager/client/client-certificate.cpp
src/manager/client/client-common.cpp
src/manager/client/client-key.cpp
src/manager/client/client-manager-impl.cpp
src/manager/common/generic-key.h [moved from src/manager/common/key-impl.h with 60% similarity]
src/manager/common/key-ecdsa.h [new file with mode: 0644]
src/manager/common/key-impl.cpp [deleted file]
src/manager/common/key-rsa.cpp [new file with mode: 0644]
src/manager/common/key-rsa.h [new file with mode: 0644]
src/manager/common/protocols.cpp
src/manager/main/key-manager-main.cpp
src/manager/service/CryptoService.cpp
src/manager/service/CryptoService.h
src/manager/service/ckm-logic.cpp
src/manager/service/ckm-logic.h
src/manager/service/ckm-service.cpp

index 2e5dc76..30421b1 100644 (file)
@@ -88,7 +88,7 @@ enum class DBCMAlgType : int {
 };
 
 // Internal types
-class KeyImpl;
+class GenericKey;
 class CertificateImpl;
 
 } // namespace CKM
index 70d3a9f..b4e23a5 100644 (file)
@@ -72,11 +72,11 @@ public:
     KeyType getType() const;
     int getSize() const;
        ElipticCurve getCurve() const;
-    RawBuffer getKey() const;
-    KeyImpl* getImpl() const;
+    RawBuffer getDER() const;
+    GenericKey* getImpl() const;
 
 private:
-    std::shared_ptr<KeyImpl> m_impl;
+    std::shared_ptr<GenericKey> m_impl;
 };
 
 class Certificate {
index 67bda87..3d83a19 100644 (file)
@@ -20,7 +20,6 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/common/message-buffer.cpp
     ${COMMON_PATH}/common/smack-check.cpp
     ${COMMON_PATH}/common/certificate-impl.cpp
-    ${COMMON_PATH}/common/key-impl.cpp
     ${COMMON_PATH}/dpl/log/src/abstract_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/log.cpp
index c1087ce..9ef015c 100644 (file)
@@ -13,7 +13,7 @@
  *  limitations under the License
  *
  *
- * @file        client-key-impl.h
+ * @file        client-certificate.h
  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
  * @version     1.0
  * @brief       Certificate class implementation.
index f3df3fb..8564d79 100644 (file)
@@ -41,7 +41,7 @@ IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
 
 namespace {
 
-const int POLL_TIMEOUT = 4000;
+const int POLL_TIMEOUT = 8000;
 
 void centKeyClientEnableLogSystem(void) {
     CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CENT_KEY_CLIENT");
index 9162ff8..c0c1dfc 100644 (file)
 #include <ckm/ckm-type.h>
 #include <ckm/key-manager.h>
 
-#include <key-impl.h>
+#include <dpl/log/log.h>
+
+#include <key-rsa.h>
 
 namespace CKM {
 
 Key::Key()
-  : m_impl(new KeyImpl())
+  : m_impl(NULL)
 {}
 
 Key::Key(
     const RawBuffer &rawData,
     KeyType type,
     const std::string &password)
-  : m_impl(new KeyImpl(rawData, type, password))
-{}
+{
+    switch (type) {
+        case KeyType::KEY_RSA_PRIVATE:
+            m_impl.reset(new KeyRSAPrivate(rawData, password));
+            break;
+        case KeyType::KEY_RSA_PUBLIC:
+            m_impl.reset(new KeyRSAPublic(rawData, password));
+            break;
+        default:
+            LogError("Key Type not implemented");
+    }
+}
 
 Key::Key(const Key &second) {
     m_impl = second.m_impl;
@@ -45,8 +57,7 @@ Key& Key::operator=(const Key &second) {
     return *this;
 }
 
-Key::~Key(){
-}
+Key::~Key(){}
 
 bool Key::empty() const {
     if (m_impl)
@@ -60,13 +71,13 @@ KeyType Key::getType() const {
     return KeyType::KEY_NONE;
 }
 
-RawBuffer Key::getKey() const {
+RawBuffer Key::getDER() const {
     if (m_impl)
-        return m_impl->getKey();
+        return m_impl->getDER();
     return RawBuffer();
 }
 
-KeyImpl* Key::getImpl() const {
+GenericKey* Key::getImpl() const {
     if (m_impl)
         return m_impl.get();
     return NULL;
index 5ecdd54..91ee44f 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <client-manager-impl.h>
 #include <client-common.h>
-#include <key-impl.h>
 #include <message-buffer.h>
 #include <protocols.h>
 
@@ -74,7 +73,7 @@ int Manager::ManagerImpl::saveBinaryData(
 }
 
 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
-    return saveBinaryData(alias, toDBDataType(key.getType()), key.getKey(), policy);
+    return saveBinaryData(alias, toDBDataType(key.getType()), key.getDER(), policy);
 }
 
 int Manager::ManagerImpl::saveCertificate(
similarity index 60%
rename from src/manager/common/key-impl.h
rename to src/manager/common/generic-key.h
index e79b296..7ce538c 100644 (file)
@@ -13,7 +13,7 @@
  *  limitations under the License
  *
  *
- * @file        client-key-impl.h
+ * @file        generic-key.h
  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
  * @version     1.0
  * @brief       Key implementation.
 
 namespace CKM {
 
-class KeyImpl
-{
+class GenericKey {
 public:
-    KeyImpl();
-    KeyImpl(const RawBuffer &data, KeyType type, const std::string &password);
-    KeyImpl(const KeyImpl &);
-    KeyImpl(KeyImpl &&);
-    KeyImpl& operator=(const KeyImpl &);
-    KeyImpl& operator=(KeyImpl &&);
+    virtual KeyType getType() const = 0;
+    virtual RawBuffer getDER() const = 0;
+    virtual EVP_PKEY* getEVPKEY() const = 0;
 
-    KeyType getType() const {
-        return m_type;
-    }
-
-    RawBuffer getKey() const {
-        return m_key;
-    }
-
-    bool empty() const {
-        return (m_type == KeyType::KEY_NONE) || m_key.empty();
-    }
-
-    virtual ~KeyImpl();
-private:
-    KeyType m_type;
-    RawBuffer m_key;
+    virtual bool empty() const = 0;
+    virtual ~GenericKey(){}
 };
 
 } // namespace CKM
diff --git a/src/manager/common/key-ecdsa.h b/src/manager/common/key-ecdsa.h
new file mode 100644 (file)
index 0000000..af63479
--- /dev/null
@@ -0,0 +1,124 @@
+#pragma once
+
+#include <memory>
+
+#include <openssl/pem.h>
+#include <openssl/rsa.h>
+#include <openssl/bio.h>
+
+#include <generic-key.h>
+
+namespace CKM {
+
+class KeyECDSA : public GenericKey {
+public:
+    KeyECDSA()
+      : m_ecdsa(NULL)
+    {}
+
+    KeyECDSA(void *ecdsa)
+      : m_ecdsa(ecdsa)
+    {}
+
+    virtual bool empty() const {
+        return m_ecdsa == NULL;
+    }
+
+    virtual ~KeyECDSA() {
+        free(m_ecdsa);
+    }
+
+    EVP_PKEY *getEVPKEY() const {
+        return NULL;
+    }
+
+protected:
+    void *m_ecdsa;
+};
+
+
+class KeyECDSAPublic : public KeyECDSA {
+public:
+    KeyECDSAPublic(){}
+
+    KeyECDSAPublic(void *ecdsa)
+      : KeyECDSA(ecdsa)
+    {}
+
+    KeyECDSAPublic(const RawBuffer &data, const std::string &password)
+    {
+        (void) data;
+        (void) password;
+    }
+
+    KeyECDSAPublic(const KeyECDSAPublic &second)
+      : KeyECDSA(second.m_ecdsa)
+    {}
+
+    KeyECDSAPublic(KeyECDSAPublic &&second) {
+        (void) second;
+    }
+
+    KeyECDSAPublic& operator=(const KeyECDSAPublic &second) {
+        (void) second;
+        return *this;
+    }
+
+    KeyECDSAPublic& operator=(KeyECDSAPublic &&second) {
+        (void) second;
+        return *this;
+    }
+
+    virtual RawBuffer getDER() const {
+        return RawBuffer();
+    }
+
+    virtual KeyType getType() const {
+        return KeyType::KEY_ECDSA_PUBLIC;
+    }
+};
+
+class KeyECDSAPrivate : public KeyECDSA {
+public:
+    KeyECDSAPrivate(){}
+
+    KeyECDSAPrivate(void *ecdsa)
+      : KeyECDSA(ecdsa)
+    {}
+
+    KeyECDSAPrivate(const KeyECDSAPrivate &second)
+      : KeyECDSA(second.m_ecdsa)
+    {}
+
+    KeyECDSAPrivate(KeyECDSAPrivate &&second) {
+        (void) second;
+    }
+
+    KeyECDSAPrivate(const RawBuffer &data, const std::string &password)
+    {
+        (void) data;
+        (void) password;
+    }
+
+    KeyECDSAPrivate& operator=(const KeyECDSAPrivate &second) {
+        (void) second;
+        return *this;
+    }
+
+    KeyECDSAPrivate& operator=(KeyECDSAPrivate &&second) {
+        (void) second;
+        return *this;
+    }
+
+    virtual RawBuffer getDER() const {
+        return RawBuffer();
+    }
+
+    virtual KeyType getType() const {
+        return KeyType::KEY_ECDSA_PRIVATE;
+    }
+
+};
+
+} // namespace CKM
+
diff --git a/src/manager/common/key-impl.cpp b/src/manager/common/key-impl.cpp
deleted file mode 100644 (file)
index 384b3c7..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/* Copyright (c) 2000 - 2013 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        client-echo.cpp
- * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @version     1.0
- * @brief       Key implementation.
- */
-#include <memory>
-
-#include <openssl/bio.h>
-#include <openssl/pem.h>
-
-#include <key-impl.h>
-
-namespace {
-
-const char PEM_FIRST_CHAR = '-';
-typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
-
-} // namespace anonymous
-
-namespace CKM {
-
-KeyImpl::KeyImpl()
-  : m_type(KeyType::KEY_NONE)
-{}
-
-KeyImpl::KeyImpl(const KeyImpl &second)
-  : m_type(second.m_type)
-  , m_key(second.m_key)
-{}
-
-KeyImpl::KeyImpl(KeyImpl &&second)
-  : m_type(second.m_type)
-  , m_key(std::move(second.m_key))
-{}
-
-KeyImpl& KeyImpl::operator=(const KeyImpl &second) {
-    m_type = second.m_type;
-    m_key = second.m_key;
-    return *this;
-}
-
-KeyImpl& KeyImpl::operator=(KeyImpl &&second) {
-    m_type = std::move(second.m_type);
-    m_key = std::move(second.m_key);
-    return *this;
-}
-
-KeyImpl::KeyImpl(const RawBuffer &data, KeyType type, const std::string &password)
-  : m_type(KeyType::KEY_NONE)
-{
-    int ret = 0;
-    RSA *rsa = NULL;
-    char *pass = NULL;
-    std::string passtmp(password);
-
-    if (!passtmp.empty()) {
-        pass = const_cast<char *>(passtmp.c_str());
-    }
-
-    if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PUBLIC) {
-        BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
-        if (NULL == bio.get())
-            return;
-        BIO_write(bio.get(), data.data(), data.size());
-        rsa = PEM_read_bio_RSA_PUBKEY(bio.get(), NULL, NULL, pass);
-    } else if (data[0] == PEM_FIRST_CHAR && type == KeyType::KEY_RSA_PRIVATE) {
-        BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
-        if (NULL == bio.get())
-            return;
-        BIO_write(bio.get(), data.data(), data.size());
-        rsa = PEM_read_bio_RSAPrivateKey(bio.get(), NULL, NULL, pass);
-    } else if (type == KeyType::KEY_RSA_PUBLIC) {
-        // First we will try to read der file
-        const unsigned char *p = static_cast<const unsigned char*>(data.data());
-        rsa = d2i_RSA_PUBKEY(NULL, &p, data.size());
-        if (rsa == NULL) {
-            // This is internal der format used by openssl?
-            BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
-            BIO_write(bio.get(), data.data(), data.size());
-            rsa = d2i_RSAPublicKey_bio(bio.get(), NULL);
-        }
-    } else if (type == KeyType::KEY_RSA_PRIVATE) {
-        BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
-        if (NULL == bio.get())
-            return;
-        BIO_write(bio.get(), data.data(), data.size());
-        rsa = d2i_RSAPrivateKey_bio(bio.get(), NULL);
-    } else {
-        return;
-    }
-
-    if (!rsa)
-        return;
-
-    BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
-
-    if (NULL == bio.get())
-        return;
-
-    if (type == KeyType::KEY_RSA_PUBLIC) {
-        ret = i2d_RSAPublicKey_bio(bio.get(), rsa);
-    } else {
-        ret = i2d_RSAPrivateKey_bio(bio.get(), rsa);
-    }
-
-    if (ret == 0)
-        return;
-
-    m_key.resize(data.size());
-    ret = BIO_read(bio.get(), m_key.data(), m_key.size());
-    if (ret <= 0) {
-        m_key.clear();
-        return;
-    }
-
-    m_key.resize(ret);
-    m_type = type;
-}
-
-KeyImpl::~KeyImpl(){}
-
-} // namespace CKM
-
diff --git a/src/manager/common/key-rsa.cpp b/src/manager/common/key-rsa.cpp
new file mode 100644 (file)
index 0000000..ed81f0e
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        key-rsa.cpp
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Implementation of keys.
+ */
+
+// TODO move function definition from header
+
diff --git a/src/manager/common/key-rsa.h b/src/manager/common/key-rsa.h
new file mode 100644 (file)
index 0000000..19c50a8
--- /dev/null
@@ -0,0 +1,248 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        key-rsa.h
+ * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version     1.0
+ * @brief       Implementation of keys.
+ */
+#pragma once
+
+#include <memory>
+
+#include <openssl/pem.h>
+#include <openssl/rsa.h>
+#include <openssl/bio.h>
+
+#include <generic-key.h>
+
+namespace {
+
+const char PEM_FIRST_CHAR = '-';
+typedef std::unique_ptr<BIO, std::function<void(BIO*)>> BioUniquePtr;
+
+} // namespace anonymous
+
+namespace CKM {
+
+class KeyRSA : public GenericKey {
+public:
+    typedef int(*I2D_FUNCTION_PTR)(BIO*, RSA*);
+
+    static RSA* RSA_dup(RSA *rsa) {
+        if (rsa)
+            RSA_up_ref(rsa);
+        return rsa;
+    }
+
+    KeyRSA(RSA *rsa)
+      : m_rsa(RSA_dup(rsa))
+    {}
+
+    KeyRSA()
+      : m_rsa(NULL)
+    {}
+
+    virtual bool empty() const {
+        return m_rsa == NULL;
+    }
+
+    virtual ~KeyRSA() {
+        RSA_free(m_rsa);
+    }
+
+    virtual RawBuffer extractDER(I2D_FUNCTION_PTR ptr) const {
+        if (NULL == m_rsa)
+            return RawBuffer();
+
+        BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+
+        if (NULL == bio.get())
+            return RawBuffer();
+
+        if (0 == ptr(bio.get(), m_rsa))
+            return RawBuffer();
+
+        RawBuffer out(8192);
+
+        int ret = BIO_read(bio.get(), out.data(), out.size());
+        if (ret <= 0) {
+            return RawBuffer();
+        }
+
+        out.resize(ret);
+        return out;
+    }
+
+    EVP_PKEY *getEVPKEY() const {
+        if (m_rsa == NULL)
+            return NULL;
+        EVP_PKEY *pkey = EVP_PKEY_new();
+        EVP_PKEY_assign_RSA(pkey, m_rsa);
+        return pkey;
+    }
+
+    RSA *getRSA() {
+        return m_rsa;
+    }
+
+protected:
+    RSA *m_rsa;
+};
+
+
+class KeyRSAPublic : public KeyRSA {
+public:
+    KeyRSAPublic(){}
+
+    KeyRSAPublic(RSA *rsa)
+      : KeyRSA(rsa)
+    {}
+
+    KeyRSAPublic(const RawBuffer &data, const std::string &password)
+    {
+        char *pass = NULL;
+        std::string passtmp(password);
+
+        if (!passtmp.empty()) {
+            pass = const_cast<char*>(passtmp.c_str());
+        }
+
+        if (data[0] == PEM_FIRST_CHAR) {
+            BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+            if (NULL == bio.get())
+                return;
+            BIO_write(bio.get(), data.data(), data.size());
+            m_rsa = PEM_read_bio_RSA_PUBKEY(bio.get(), NULL, NULL, pass);
+        } else {
+            // First we will try to read der file
+            const unsigned char *p = static_cast<const unsigned char*>(data.data());
+            m_rsa = d2i_RSA_PUBKEY(NULL, &p, data.size());
+            if (m_rsa == NULL) {
+                // This is internal der format used by openssl?
+                BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+                BIO_write(bio.get(), data.data(), data.size());
+                m_rsa = d2i_RSAPublicKey_bio(bio.get(), NULL);
+            }
+        }
+    }
+
+    KeyRSAPublic(const KeyRSAPublic &second)
+      : KeyRSA(second.m_rsa)
+    {}
+
+    KeyRSAPublic(KeyRSAPublic &&second) {
+        m_rsa = second.m_rsa;
+        second.m_rsa = NULL;
+    }
+
+    KeyRSAPublic& operator=(const KeyRSAPublic &second) {
+        if (this == &second)
+            return *this;
+        RSA_free(m_rsa);
+        m_rsa = RSA_dup(second.m_rsa);
+        return *this;
+    }
+
+    KeyRSAPublic& operator=(KeyRSAPublic &&second) {
+        if (this == &second)
+            return *this;
+        m_rsa = second.m_rsa;
+        second.m_rsa = NULL;
+        return *this;
+    }
+
+    virtual RawBuffer getDER() const {
+        return extractDER(i2d_RSAPublicKey_bio);
+    }
+
+    virtual KeyType getType() const {
+        return KeyType::KEY_RSA_PUBLIC;
+    }
+};
+
+class KeyRSAPrivate : public KeyRSA {
+public:
+    KeyRSAPrivate(){}
+
+    KeyRSAPrivate(RSA *rsa)
+      : KeyRSA(rsa)
+    {}
+
+    KeyRSAPrivate(const KeyRSAPrivate &second)
+      : KeyRSA(second.m_rsa)
+    {}
+
+    KeyRSAPrivate(KeyRSAPrivate &&second) {
+        m_rsa = second.m_rsa;
+        second.m_rsa = NULL;
+    }
+
+    KeyRSAPrivate(const RawBuffer &data, const std::string &password)
+    {
+        char *pass = NULL;
+        std::string passtmp(password);
+
+        if (!passtmp.empty()) {
+            pass = const_cast<char*>(passtmp.c_str());
+        }
+
+        if (data[0] == PEM_FIRST_CHAR) {
+            BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+            if (NULL == bio.get())
+                return;
+            BIO_write(bio.get(), data.data(), data.size());
+            m_rsa = PEM_read_bio_RSAPrivateKey(bio.get(), NULL, NULL, pass);
+        } else {
+            BioUniquePtr bio(BIO_new(BIO_s_mem()), BIO_free_all);
+            if (NULL == bio.get())
+                return;
+            BIO_write(bio.get(), data.data(), data.size());
+            m_rsa = d2i_RSAPrivateKey_bio(bio.get(), NULL);
+        }
+    }
+
+    KeyRSAPrivate& operator=(const KeyRSAPrivate &second) {
+        if (this == &second)
+            return *this;
+        RSA_free(m_rsa);
+        m_rsa = RSA_dup(second.m_rsa);
+        return *this;
+    }
+
+    KeyRSAPrivate& operator=(KeyRSAPrivate &&second) {
+        if (this == &second)
+            return *this;
+        RSA_free(m_rsa);
+        m_rsa = second.m_rsa;
+        second.m_rsa = NULL;
+        return *this;
+    }
+
+    virtual RawBuffer getDER() const {
+        return extractDER(i2d_RSAPrivateKey_bio);
+    }
+
+    virtual KeyType getType() const {
+        return KeyType::KEY_RSA_PRIVATE;
+    }
+
+};
+
+} // namespace CKM
+
index 0bf08a2..d60c90d 100644 (file)
@@ -36,10 +36,12 @@ DBDataType toDBDataType(KeyType key) {
     switch(key) {
     case KeyType::KEY_RSA_PUBLIC:  return DBDataType::KEY_RSA_PUBLIC;
     case KeyType::KEY_RSA_PRIVATE: return DBDataType::KEY_RSA_PRIVATE;
+    case KeyType::KEY_ECDSA_PUBLIC: return DBDataType::KEY_ECDSA_PUBLIC;
+    case KeyType::KEY_ECDSA_PRIVATE: return DBDataType::KEY_ECDSA_PRIVATE;
+    case KeyType::KEY_AES: return DBDataType::KEY_AES;
     default:
         // TODO
         throw 1;
-
     }
 }
 
index b014193..87e55d3 100644 (file)
@@ -37,6 +37,7 @@
 #include <ckm-service.h>
 
 #include <key-provider.h>
+#include <CryptoService.h>
 
 IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
 
@@ -88,6 +89,7 @@ int main(void) {
         OPENSSL_config(NULL);
 
         CKM::KeyProvider::initializeLibrary();
+        CKM::CryptoService::initialize();
 
         {
             LogInfo("Start!");
index bc64635..8918f09 100644 (file)
 #include <openssl/x509v3.h>
 #include <openssl/obj_mac.h>
 #include <ckm/ckm-type.h>
-#include <key-impl.h>
+#include <key-rsa.h>
+#include <key-ecdsa.h>
 #include <CryptoService.h>
 #include <key-manager-util.h>
 
+#include <dpl/log/log.h>
+
 #define OPENSSL_SUCCESS 1       // DO NOTCHANGE THIS VALUE
 #define OPENSSL_FAIL    0       // DO NOTCHANGE THIS VALUE
 
@@ -32,65 +35,65 @@ CryptoService::~CryptoService(){
 }
 
 // The returned (EVP_PKEY *) should be freed like this [if(pkey) EVP_PKEY_free(pkey);] after use.
-void to_string_rsa_private_key(RSA *pkey, unsigned char **derPrivateKey, int *length) {
-       unsigned char *ucTmp;
-       *length = i2d_RSAPrivateKey(pkey, NULL);
-       *derPrivateKey = (unsigned char *)malloc(*length);
-       ucTmp = *derPrivateKey;
-       i2d_RSAPrivateKey(pkey, &ucTmp);
-}
-
-void to_string_rsa_public_key(RSA *pkey, unsigned char **derPublicKey, int *length) {
-       unsigned char *ucTmp;
-       *length = i2d_RSA_PUBKEY(pkey, NULL);
-       *derPublicKey = (unsigned char *)malloc(*length);
-       ucTmp = *derPublicKey;
-       i2d_RSA_PUBKEY(pkey, &ucTmp);
-}
-
-void to_string_ec_private_key(EC_KEY *pkey, unsigned char **derPrivateKey, int *length) {
-       unsigned char *ucTmp;
-       *length = i2d_ECPrivateKey(pkey, NULL);
-       *derPrivateKey = (unsigned char *)malloc(*length);
-       ucTmp = *derPrivateKey;
-       i2d_ECPrivateKey(pkey, &ucTmp);
-}
-
-void to_string_ec_public_key(EC_KEY *pkey, unsigned char **derPublicKey, int *length) {
-       unsigned char *ucTmp;   //RawData test;
-       *length = i2d_EC_PUBKEY(pkey, NULL);
-       *derPublicKey = (unsigned char *)malloc(*length);
-       ucTmp = *derPublicKey;
-       i2d_EC_PUBKEY(pkey, &ucTmp);
-}
+//void to_string_rsa_private_key(RSA *pkey, unsigned char **derPrivateKey, int *length) {
+//     unsigned char *ucTmp;
+//     *length = i2d_RSAPrivateKey(pkey, NULL);
+//     *derPrivateKey = (unsigned char *)malloc(*length);
+//     ucTmp = *derPrivateKey;
+//     i2d_RSAPrivateKey(pkey, &ucTmp);
+//}
+
+//void to_string_rsa_public_key(RSA *pkey, unsigned char **derPublicKey, int *length) {
+//     unsigned char *ucTmp;
+//     *length = i2d_RSA_PUBKEY(pkey, NULL);
+//     *derPublicKey = (unsigned char *)malloc(*length);
+//     ucTmp = *derPublicKey;
+//     i2d_RSA_PUBKEY(pkey, &ucTmp);
+//}
+
+//void to_string_ec_private_key(EC_KEY *pkey, unsigned char **derPrivateKey, int *length) {
+//     unsigned char *ucTmp;
+//     *length = i2d_ECPrivateKey(pkey, NULL);
+//     *derPrivateKey = (unsigned char *)malloc(*length);
+//     ucTmp = *derPrivateKey;
+//     i2d_ECPrivateKey(pkey, &ucTmp);
+//}
+//
+//void to_string_ec_public_key(EC_KEY *pkey, unsigned char **derPublicKey, int *length) {
+//     unsigned char *ucTmp;   //RawData test;
+//     *length = i2d_EC_PUBKEY(pkey, NULL);
+//     *derPublicKey = (unsigned char *)malloc(*length);
+//     ucTmp = *derPublicKey;
+//     i2d_EC_PUBKEY(pkey, &ucTmp);
+//}
 
 // The returned (EVP_PKEY *) should be freed like this [if(pkey) EVP_PKEY_free(pkey);] after use.
-EVP_PKEY *to_pkey_rsa_public_key(const unsigned char *derPublicKey, int length) {
-       EVP_PKEY *pkey = EVP_PKEY_new();
-       RSA *rsa;
-
-       BIO *bio = BIO_new(BIO_s_mem());
-    BIO_write(bio, derPublicKey, length);
-    rsa = d2i_RSA_PUBKEY_bio(bio, NULL);
-    BIO_free_all(bio);
-    EVP_PKEY_set1_RSA(pkey,rsa);
-
-       return pkey;
-}
+//EVP_PKEY *to_pkey_rsa_public_key(const unsigned char *derPublicKey, int length) {
+//     EVP_PKEY *pkey = EVP_PKEY_new();
+//     RSA *rsa;
+//
+//     BIO *bio = BIO_new(BIO_s_mem());
+//    BIO_write(bio, derPublicKey, length);
+//    rsa = d2i_RSA_PUBKEY_bio(bio, NULL);
+//    BIO_free_all(bio);
+//    EVP_PKEY_set1_RSA(pkey,rsa);
+//
+//     return pkey;
+//}
 
 // The returned (EVP_PKEY *) should be freed like this [if(pkey) EVP_PKEY_free(pkey);] after use.
-EVP_PKEY *to_pkey_rsa_private_key(const unsigned char *derPrivateKey, int length) {
-       EVP_PKEY *pkey = EVP_PKEY_new();
-       RSA *rsa;
-
-       BIO *bio = BIO_new(BIO_s_mem());
-    BIO_write(bio, derPrivateKey, length);
-    rsa = d2i_RSAPrivateKey_bio(bio, NULL);
-    BIO_free_all(bio);
-    EVP_PKEY_set1_RSA(pkey,rsa);
-
-       return pkey;
-}
+//EVP_PKEY *to_pkey_rsa_private_key(const unsigned char *derPrivateKey, int length) {
+//     EVP_PKEY *pkey = EVP_PKEY_new();
+//     RSA *rsa;
+//
+//     BIO *bio = BIO_new(BIO_s_mem());
+//    BIO_write(bio, derPrivateKey, length);
+//    rsa = d2i_RSAPrivateKey_bio(bio, NULL);
+//    BIO_free_all(bio);
+//    EVP_PKEY_set1_RSA(pkey,rsa);
+//
+//     return pkey;
+//}
 
 // The returned (EVP_PKEY *) should be freed like this [if(pkey) EVP_PKEY_free(pkey);] after use.
 EVP_PKEY *to_pkey_ec_public_key(const unsigned char *derPublicKey, int length) {
@@ -120,7 +123,7 @@ EVP_PKEY *to_pkey_ec_private_key(const unsigned char *derPrivateKey, int length)
        return pkey;
 }
 
-int CryptoService::initalize() {
+int CryptoService::initialize() {
        int mode, ret, rc;
 
        // try to initialize using ERR_load_crypto_strings and OpenSSL_add_all_algorithms
@@ -152,14 +155,12 @@ int CryptoService::initalize() {
 }
 
 int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048, 4096]
-               KeyImpl &createdPrivateKey,  // returned value
-               KeyImpl &createdPublicKey)  // returned value
+               KeyRSAPrivate &createdPrivateKey,  // returned value
+        KeyRSAPublic &createdPublicKey)  // returned value
 {
        EVP_PKEY_CTX *ctx = NULL;
        EVP_PKEY *pkey = NULL;
 
-       unsigned char *derPrivateKey = NULL, *derPublicKey = NULL;
-       int priKeyLength, pubKeyLength;
        RawBuffer priKey_tmp, pubKey_tmp;
        const std::string null_password;
 
@@ -171,7 +172,9 @@ int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048,
        EVP_PKEY_CTX_new(pparam, NULL);
        EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
 
-       if(!ctx) {
+    LogDebug("Generating RSA key pair start.");
+
+    if(!ctx) {
                return CKM_CRYPTO_CTX_ERROR;
        }
 
@@ -190,32 +193,14 @@ int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048,
                return CKM_CRYPTO_PKEYGEN_ERROR;
        }
 
-       // convert to rsa key
-       RSA *rsa_key = EVP_PKEY_get1_RSA(pkey);
-
-       // Extract private and public key
-       to_string_rsa_private_key(rsa_key, &derPrivateKey, &priKeyLength);
-       to_string_rsa_public_key(rsa_key, &derPublicKey, &pubKeyLength);
+    LogDebug("Generating RSA key pair end.");
 
-       // Key copy to vector structure
-       priKey_tmp.assign(derPrivateKey, derPrivateKey+priKeyLength);
-       pubKey_tmp.assign(derPublicKey, derPublicKey+pubKeyLength);
-
-       // Create two keys
-       KeyImpl privateKey(priKey_tmp, KeyType::KEY_RSA_PRIVATE, null_password);
-       KeyImpl Publickey(pubKey_tmp, KeyType::KEY_RSA_PUBLIC, null_password);
+    // convert to rsa key
+       RSA *rsa_key = EVP_PKEY_get1_RSA(pkey);
 
-       // Two made key copy to reference structure
-       createdPrivateKey = privateKey;
-       createdPublicKey = Publickey;
+    createdPrivateKey = KeyRSAPrivate(rsa_key);
+    createdPublicKey = KeyRSAPublic(rsa_key);
 
-       RawBuffer data;
-       data = privateKey.getKey();
-
-       if(derPrivateKey)
-               free(derPrivateKey);
-       if(derPublicKey)
-               free(derPublicKey);
        if(pkey)
                EVP_PKEY_free(pkey);
        if(ctx)
@@ -225,11 +210,9 @@ int CryptoService::createKeyPairRSA(const int size, // size in bits [1024, 2048,
 }
 
 int CryptoService::createKeyPairECDSA(ElipticCurve type,
-               KeyImpl &createdPrivateKey,  // returned value
-               KeyImpl &createdPublicKey)  // returned value
+               KeyECDSAPrivate &createdPrivateKey,  // returned value
+        KeyECDSAPublic &createdPublicKey)  // returned value
 {
-               unsigned char *derPrivateKey = NULL, *derPublicKey = NULL;
-               int priKeyLength, pubKeyLength;
                int ecCurve = -1;
                EVP_PKEY_CTX *pctx = NULL;
                EVP_PKEY_CTX *kctx = NULL;
@@ -304,31 +287,9 @@ int CryptoService::createKeyPairECDSA(ElipticCurve type,
                // convert to rsa key
                EC_KEY *ec_key = EVP_PKEY_get1_EC_KEY(pkey);
 
-               // Extract private and public key
-               to_string_ec_private_key(ec_key, &derPrivateKey, &priKeyLength);
-               to_string_ec_public_key(ec_key, &derPublicKey, &pubKeyLength);
-
-               // Key copy to vector structure
-               priKey_tmp.assign(derPrivateKey, derPrivateKey+priKeyLength);
-               pubKey_tmp.assign(derPublicKey, derPublicKey+pubKeyLength);
+        createdPrivateKey = KeyECDSAPrivate(ec_key);
+        createdPublicKey = KeyECDSAPublic(ec_key);
 
-               // Create two keys
-
-               //Key(const RawBuffer &rawData, KeyType type, const std::string &password = std::string());
-
-               KeyImpl privateKey(priKey_tmp, KeyType::KEY_ECDSA_PRIVATE, NULL);
-               KeyImpl Publickey(pubKey_tmp, KeyType::KEY_ECDSA_PUBLIC, NULL);
-
-               // Two made key copy to reference structure
-               // To operate this function, client-key-impl should be modified
-
-               createdPrivateKey = privateKey;
-               createdPublicKey = Publickey;
-
-               if(derPrivateKey)
-                       free(derPrivateKey);
-               if(derPublicKey)
-                       free(derPublicKey);
                if(pkey)
                        EVP_PKEY_free(pkey);
                if(pparam)
@@ -341,7 +302,7 @@ int CryptoService::createKeyPairECDSA(ElipticCurve type,
                return CKM_CRYPTO_CREATEKEY_SUCCESS;
 }
 
-int CryptoService::createSignature(const KeyImpl &privateKey,
+int CryptoService::createSignature(const GenericKey &privateKey,
                          const RawBuffer &message,
                          const HashAlgorithm hashAlgo,
                          const RSAPaddingAlgorithm padAlgo,
@@ -383,19 +344,11 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
                default:
                        return CKM_CRYPTO_NOT_SUPPORT_ALGO_ERROR;
                }
+    } else if (privateKey.getType() != KeyType::KEY_ECDSA_PRIVATE) {
+        return CKM_CRYPTO_NOT_SUPPORT_KEY_TYPE;
+    }
 
-               data = privateKey.getKey();
-               unsigned char derPrivateKey[data.size()];
-               memcpy(derPrivateKey, data.data(),data.size());
-               private_pkey = to_pkey_rsa_private_key(derPrivateKey, data.size());
-       } else if(privateKey.getType()==KeyType::KEY_ECDSA_PRIVATE) {
-               data = privateKey.getKey();
-               unsigned char derPrivateKey[data.size()];
-               memcpy(derPrivateKey, data.data(),data.size());
-               private_pkey = to_pkey_ec_private_key(derPrivateKey, data.size());
-       } else {
-               return CKM_CRYPTO_NOT_SUPPORT_KEY_TYPE;
-       }
+    private_pkey = privateKey.getEVPKEY();
 
        // Create the Message Digest Context
        if(!(mdctx = EVP_MD_CTX_create())) {
@@ -403,6 +356,7 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        }
        if(EVP_SUCCESS != EVP_DigestSignInit(mdctx, &pctx, md_algo, NULL, private_pkey)) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if(private_pkey) EVP_PKEY_free(private_pkey);
                return CKM_SIG_GEN_ERROR;
        }
 
@@ -410,6 +364,7 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        if(privateKey.getType()==KeyType::KEY_RSA_PRIVATE) {
                if(EVP_SUCCESS != EVP_PKEY_CTX_set_rsa_padding(pctx, rsa_padding)) {
                        if(mdctx) EVP_MD_CTX_destroy(mdctx);
+            if(private_pkey) EVP_PKEY_free(private_pkey);
                        return CKM_SIG_GEN_ERROR;
                }
        }
@@ -419,6 +374,7 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        memcpy(msg, message.data(),message.size());
        if(EVP_SUCCESS != EVP_DigestSignUpdate(mdctx, msg, message.size())) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if(private_pkey) EVP_PKEY_free(private_pkey);
                return CKM_SIG_GEN_ERROR;
        }
 
@@ -428,6 +384,7 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        size_t slen;
        if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, NULL, &slen)) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if(private_pkey) EVP_PKEY_free(private_pkey);
                return CKM_SIG_GEN_ERROR;
        }
        /* Allocate memory for the signature based on size in slen */
@@ -436,6 +393,7 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        /* Obtain the signature */
        if(EVP_SUCCESS != EVP_DigestSignFinal(mdctx, sig, &slen)) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if(private_pkey) EVP_PKEY_free(private_pkey);
                return CKM_SIG_GEN_ERROR;
        }
 
@@ -445,10 +403,11 @@ int CryptoService::createSignature(const KeyImpl &privateKey,
        /* Success */
        ret = EVP_SUCCESS;
        if(mdctx) EVP_MD_CTX_destroy(mdctx);
+    if(private_pkey) EVP_PKEY_free(private_pkey);
        return ret;
 }
 
-int CryptoService::verifySignature(const KeyImpl &publicKey,
+int CryptoService::verifySignature(const GenericKey &publicKey,
                     const RawBuffer &message,
                     const RawBuffer &signature,
                     const HashAlgorithm hashAlgo,
@@ -491,19 +450,15 @@ int CryptoService::verifySignature(const KeyImpl &publicKey,
                        return CKM_CRYPTO_NOT_SUPPORT_ALGO_ERROR;
                }
 
-               data = publicKey.getKey();
-               unsigned char derPublicKey[data.size()];
-               memcpy(derPublicKey, data.data(),data.size());
-               public_pkey = to_pkey_rsa_public_key(derPublicKey, data.size());
-       } else if(publicKey.getType()==KeyType::KEY_ECDSA_PUBLIC) {
-               data = publicKey.getKey();
-               unsigned char derPublicKey[data.size()];
-               memcpy(derPublicKey, data.data(),data.size());
-               public_pkey = to_pkey_ec_public_key(derPublicKey, data.size());
-       } else {
+       } else if(publicKey.getType() != KeyType::KEY_ECDSA_PUBLIC) {
                return CKM_CRYPTO_NOT_SUPPORT_KEY_TYPE;
        }
 
+    public_pkey = publicKey.getEVPKEY();
+
+    if (NULL == public_pkey)
+        return CKM_CRYPTO_PKEYSET_ERROR;
+
        char msg[message.size()];
        memcpy(msg, message.data(),message.size());
 
@@ -512,33 +467,39 @@ int CryptoService::verifySignature(const KeyImpl &publicKey,
 
        /* Create the Message Digest Context */
        if(!(mdctx = EVP_MD_CTX_create())) {
+        if (public_pkey) EVP_PKEY_free(public_pkey);
                return CKM_SIG_VERIFY_OPER_ERROR;
        }
 
        if(EVP_SUCCESS != EVP_DigestVerifyInit(mdctx, &pctx, md_algo, NULL, public_pkey)) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if (public_pkey) EVP_PKEY_free(public_pkey);
                return CKM_SIG_VERIFY_OPER_ERROR;
        }
 
        if(publicKey.getType()==KeyType::KEY_RSA_PUBLIC) {
                if(EVP_SUCCESS != EVP_PKEY_CTX_set_rsa_padding(pctx, rsa_padding))  {
                        if(mdctx) EVP_MD_CTX_destroy(mdctx);
+            if (public_pkey) EVP_PKEY_free(public_pkey);
                        return CKM_SIG_VERIFY_OPER_ERROR;
                }
        }
 
        if(EVP_SUCCESS != EVP_DigestVerifyUpdate(mdctx, msg, message.size()) ) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if (public_pkey) EVP_PKEY_free(public_pkey);
                return CKM_SIG_VERIFY_OPER_ERROR;
     }
 
        if(EVP_SUCCESS != EVP_DigestVerifyFinal(mdctx, sig, signature.size()) ) {
                if(mdctx) EVP_MD_CTX_destroy(mdctx);
+        if (public_pkey) EVP_PKEY_free(public_pkey);
                return CKM_SIG_VERIFY_OPER_ERROR;
     }
 
        ret = EVP_SUCCESS;
        if(mdctx) EVP_MD_CTX_destroy(mdctx);
+    if (public_pkey) EVP_PKEY_free(public_pkey);
        return ret;
 }
 
index 88cd3ce..b641ecb 100644 (file)
@@ -2,7 +2,8 @@
 
 #include <iostream>
 
-#include <key-impl.h>
+#include <key-rsa.h>
+#include <key-ecdsa.h>
 #include <certificate-impl.h>
 #include <ckm/ckm-type.h>
 #include <string.h>
@@ -49,23 +50,23 @@ class CryptoService {
      // And system certificates are loaded in the memory during initialization.
      //    FIPS_MODE - ON, OFF(Default)
      //    antropy source - /dev/random,/dev/urandom(Default)
-     static int initalize();
+     static int initialize();
 
      int createKeyPairRSA(const int size,      // size in bits [1024, 2048, 4096]
-                         KeyImpl &createdPrivateKey,  // returned value ==> Key &createdPrivateKey,
-                         KeyImpl &createdPublicKey);  // returned value ==> Key &createdPublicKey
+                         KeyRSAPrivate &createdPrivateKey,  // returned value ==> Key &createdPrivateKey,
+                         KeyRSAPublic &createdPublicKey);  // returned value ==> Key &createdPublicKey
 
      int createKeyPairECDSA(ElipticCurve type1,
-                                        KeyImpl &createdPrivateKey,  // returned value
-                                        KeyImpl &createdPublicKey);  // returned value
+                                        KeyECDSAPrivate &createdPrivateKey,  // returned value
+                                        KeyECDSAPublic &createdPublicKey);  // returned value
 
-     int createSignature(const KeyImpl &privateKey,
+     int createSignature(const GenericKey &privateKey,
                          const RawBuffer &message,
                          const HashAlgorithm hashAlgo,
                          const RSAPaddingAlgorithm padAlgo,
                          RawBuffer &signature);
 
-     int verifySignature(const KeyImpl &publicKey,
+     int verifySignature(const GenericKey &publicKey,
                          const RawBuffer &message,
                          const RawBuffer &signature,
                          const HashAlgorithm hashAlgo,
index f0d86a2..47b479e 100644 (file)
@@ -26,8 +26,9 @@
 #include <ckm/ckm-type.h>
 #include <key-provider.h>
 #include <file-system.h>
-
+#include <CryptoService.h>
 #include <ckm-logic.h>
+#include <key-rsa.h>
 
 namespace CKM {
 
@@ -328,25 +329,71 @@ RawBuffer CKMLogic::getDataList(
     return response.Pop();
 }
 
+int CKMLogic::createKeyPairRSAHelper(
+    Credentials &cred,
+    int size,
+    const Alias &aliasPrivate,
+    const Alias &aliasPublic,
+    const PolicySerializable &policyPrivate,
+    const PolicySerializable &policyPublic)
+{
+    if (0 >= m_userDataMap.count(cred.uid))
+        return KEY_MANAGER_API_ERROR_DB_LOCKED;
+
+    auto &handler = m_userDataMap[cred.uid];
+    KeyRSAPrivate prv;
+    KeyRSAPublic pub;
+    CryptoService cr;
+    int retCode;
+
+    if (CKM_CRYPTO_CREATEKEY_SUCCESS != (retCode = cr.createKeyPairRSA(size, prv, pub))) {
+        LogError("CryptoService failed with code: " << retCode);
+        return KEY_MANAGER_API_ERROR_SERVER_ERROR; // TODO error code
+    }
+
+    retCode = saveDataHelper(cred,
+                            toDBDataType(prv.getType()),
+                            aliasPrivate,
+                            prv.getDER(),
+                            policyPrivate);
+
+    if (KEY_MANAGER_API_SUCCESS != retCode)
+        return retCode;
+
+    retCode = saveDataHelper(cred,
+                            toDBDataType(pub.getType()),
+                            aliasPublic,
+                            pub.getDER(),
+                            policyPublic);
+
+    if (KEY_MANAGER_API_SUCCESS != retCode) {
+        handler.database.deleteDBRow(aliasPrivate, cred.smackLabel);
+    }
+
+    return retCode;
+}
+
 RawBuffer CKMLogic::createKeyPairRSA(
     Credentials &cred,
     int commandId,
     int size,
-    const Alias &privateKeyAlias,
-    const Alias &publicKeyAlias,
-    PolicySerializable policyPrivateKey,
-    PolicySerializable policyPublicKey)
+    const Alias &aliasPrivate,
+    const Alias &aliasPublic,
+    const PolicySerializable &policyPrivate,
+    const PolicySerializable &policyPublic)
 {
-    (void)cred;
-    (void)size;
-    (void)privateKeyAlias;
-    (void)publicKeyAlias,
-    (void)policyPrivateKey;
-    (void)policyPublicKey;
+    int retCode = createKeyPairRSAHelper(
+                        cred,
+                        size,
+                        aliasPrivate,
+                        aliasPublic,
+                        policyPrivate,
+                        policyPublic);
+
     MessageBuffer response;
     Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
     Serialization::Serialize(response, commandId);
-    Serialization::Serialize(response, static_cast<int>(KEY_MANAGER_API_SUCCESS));
+    Serialization::Serialize(response, retCode);
  
     return response.Pop();
 }
@@ -355,18 +402,18 @@ RawBuffer CKMLogic::createKeyPairECDSA(
     Credentials &cred,
     int commandId,
     int type,
-    const Alias &privateKeyAlias,
-    const Alias &publicKeyAlias,
-    PolicySerializable policyPrivateKey,
-    PolicySerializable policyPublicKey)
+    const Alias &aliasPrivate,
+    const Alias &aliasPublic,
+    const PolicySerializable &policyPrivate,
+    const PolicySerializable &policyPublic)
 {
     (void)cred;
     (void)type;
-    (void)privateKeyAlias;
-    (void)publicKeyAlias,
-    (void)policyPrivateKey;
-    (void)policyPublicKey;
-    
+    (void)aliasPrivate;
+    (void)aliasPublic;
+    (void)policyPrivate;
+    (void)policyPublic;
+
     MessageBuffer response;
     Serialization::Serialize(response, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
     Serialization::Serialize(response, commandId);
index 1d70216..5814092 100644 (file)
@@ -94,19 +94,19 @@ public:
         Credentials &cred,
         int commandId,
         int size,
-        const Alias &privateKeyAlias,
-        const Alias &publicKeyAlias,
-        PolicySerializable policyPrivateKey,
-        PolicySerializable policyPublicKey);
+        const Alias &aliasPrivate,
+        const Alias &alaisPublic,
+        const PolicySerializable &policyPrivate,
+        const PolicySerializable &policyPublic);
 
     RawBuffer createKeyPairECDSA(
         Credentials &cred,
         int commandId,
         int type,
-        const Alias &privateKeyAlias,
-        const Alias &publicKeyAlias,
-        PolicySerializable policyPrivateKey,
-        PolicySerializable policyPublicKey);
+        const Alias &aliasPrivate,
+        const Alias &aliasPublic,
+        const PolicySerializable &policyPrivate,
+        const PolicySerializable &policyPublic);
 
 private:
     int saveDataHelper(
@@ -123,6 +123,14 @@ private:
         const std::string &password,
         DBRow &row);
 
+    int createKeyPairRSAHelper(
+        Credentials &cred,
+        int size,
+        const Alias &aliasPrivate,
+        const Alias &aliasPublic,
+        const PolicySerializable &policyPrivate,
+        const PolicySerializable &policyPublic);
+
     std::map<uid_t, UserData> m_userDataMap;
 };
 
index 6c82641..c409214 100644 (file)
@@ -30,7 +30,6 @@
 
 #include <ckm-service.h>
 #include <ckm-logic.h>
-#include <key-impl.h>
 
 namespace {
 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
@@ -207,6 +206,8 @@ RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
             Deserialization::Deserialize(buffer, size);
             Deserialization::Deserialize(buffer, policyPrivateKey);
             Deserialization::Deserialize(buffer, policyPublicKey);
+            Deserialization::Deserialize(buffer, privateKeyAlias);
+            Deserialization::Deserialize(buffer, publicKeyAlias);
             return m_logic->createKeyPairRSA(
                 cred,
                 commandId,
@@ -226,6 +227,8 @@ RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
             Deserialization::Deserialize(buffer, type);
             Deserialization::Deserialize(buffer, policyPrivateKey);
             Deserialization::Deserialize(buffer, policyPublicKey);
+            Deserialization::Deserialize(buffer, privateKeyAlias);
+            Deserialization::Deserialize(buffer, publicKeyAlias);
             return m_logic->createKeyPairECDSA(
                 cred,
                 commandId,