Add algorithm param validation
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / store.cpp
index 38d36c3..4d6b1bf 100644 (file)
  */
 #include <memory>
 
-#include <dpl/log/log.h>
-
 #include <generic-backend/exception.h>
 #include <sw-backend/key.h>
 #include <sw-backend/store.h>
+#include <sw-backend/internals.h>
+
+namespace {
+
+template <typename T, typename ...Args>
+std::unique_ptr<T> make_unique(Args&& ...args) {
+    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+} // namespace anonymous
 
 namespace CKM {
 namespace Crypto {
 namespace SW {
 
-Id Store::getBackendId() const { return Id::OpenSSL; }
+Store::Store(CryptoBackend backendId)
+  : GStore(backendId)
+{
+    // initialize openssl internals
+    Internals::initialize();
+}
+
+GKeyUPtr Store::getKey(const Token &token) {
+    if (token.backendId != m_backendId) {
+        ThrowErr(Exc::Crypto::WrongBackend, "Decider choose wrong backend!");
+    }
+
+    if (token.dataType.isKeyPrivate() || token.dataType.isKeyPublic()) {
+         return make_unique<AKey>(token.data, token.dataType);
+    }
 
-GKeyShPtr Store::getKey(const Token &token) {
-    if (token.backendId != getBackendId()) {
-        LogDebug("Decider choose wrong backend!");
-        ThrowMsg(Exception::WrongBackend, "Decider choose wrong backend!");
+    if (token.dataType == DataType(DataType::KEY_AES)) {
+         return make_unique<SKey>(token.data, token.dataType);
     }
 
-    switch (token.keyType) {
-    case KeyType::KEY_RSA_PUBLIC:
-    case KeyType::KEY_RSA_PRIVATE:
-    case KeyType::KEY_DSA_PUBLIC:
-    case KeyType::KEY_DSA_PRIVATE:
-    case KeyType::KEY_ECDSA_PUBLIC:
-    case KeyType::KEY_ECDSA_PRIVATE:
-         return std::make_shared<AKey>(token.buffer, token.keyType);
-    case KeyType::KEY_AES:
-         return std::make_shared<SKey>(token.buffer, token.keyType);
-    default:
-         LogDebug(
-            "This type of key is not supported by openssl backend: " << (int)token.keyType);
-         ThrowMsg(Exception::KeyNotSupported,
-            "This type of key is not supported by openssl backend: " << (int)token.keyType);
+    if (token.dataType.isCertificate()) {
+        return make_unique<Cert>(token.data, token.dataType);
     }
 
+    ThrowErr(Exc::Crypto::KeyNotSupported,
+        "This type of data is not supported by openssl backend: ", (int)token.dataType);
+}
+
+TokenPair Store::generateAKey(const CryptoAlgorithm &algorithm)
+{
+    return Internals::generateAKey(m_backendId, algorithm);
+}
+
+Token Store::generateSKey(const CryptoAlgorithm &algorithm)
+{
+    return Internals::generateSKey(m_backendId, algorithm);
 }
 
-Token Store::import(KeyType keyType, const RawBuffer &buffer) {
-    Token token;
-    token.buffer = buffer;
-    token.keyType = keyType;
-    token.backendId = getBackendId();
-    return token;
+Token Store::import(DataType dataType, const RawBuffer &buffer) {
+    return Token(m_backendId, dataType, buffer);
 }
 
 } // namespace SW