Add algorithm param validation
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / store.cpp
index 2ed63af..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 {
@@ -38,65 +45,41 @@ Store::Store(CryptoBackend backendId)
     Internals::initialize();
 }
 
-GKeyShPtr Store::getKey(const Token &token) {
+GKeyUPtr Store::getKey(const Token &token) {
     if (token.backendId != m_backendId) {
-        LogError("Decider choose wrong backend!");
-        ThrowMsg(Exception::WrongBackend, "Decider choose wrong backend!");
+        ThrowErr(Exc::Crypto::WrongBackend, "Decider choose wrong backend!");
     }
 
     if (token.dataType.isKeyPrivate() || token.dataType.isKeyPublic()) {
-         return std::make_shared<AKey>(token.data, token.dataType);
+         return make_unique<AKey>(token.data, token.dataType);
     }
 
     if (token.dataType == DataType(DataType::KEY_AES)) {
-         return std::make_shared<SKey>(token.data, token.dataType);
+         return make_unique<SKey>(token.data, token.dataType);
     }
 
     if (token.dataType.isCertificate()) {
-        return std::make_shared<Cert>(token.data, token.dataType);
+        return make_unique<Cert>(token.data, token.dataType);
     }
 
-    LogDebug(
-        "This type of data is not supported by openssl backend: " << (int)token.dataType);
-    ThrowMsg(Exception::KeyNotSupported,
-        "This type of data is not supported by openssl backend: " << (int)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)
 {
-    AlgoType keyType = AlgoType::RSA_GEN;
-    algorithm.getParam(ParamName::ALGO_TYPE, keyType);
-
-    if(keyType == AlgoType::RSA_GEN || keyType == AlgoType::DSA_GEN)
-    {
-        int keyLength = 0;
-        if(!algorithm.getParam(ParamName::GEN_KEY_LEN, keyLength))
-            ThrowMsg(Crypto::Exception::InputParam, "Error, parameter GEN_KEY_LEN not found.");
-
-        if(keyType == AlgoType::RSA_GEN)
-            return Internals::createKeyPairRSA(m_backendId, keyLength);
-        else
-            return Internals::createKeyPairDSA(m_backendId, keyLength);
-    }
-    else if(keyType == AlgoType::ECDSA_GEN)
-    {
-        int ecType = 0;
-        if(!algorithm.getParam(ParamName::GEN_EC, ecType))
-            ThrowMsg(Crypto::Exception::InputParam, "Error, parameter GEN_EC not found.");
+    return Internals::generateAKey(m_backendId, algorithm);
+}
 
-        return Internals::createKeyPairECDSA(m_backendId, static_cast<ElipticCurve>(ecType));
-    }
-    ThrowMsg(Crypto::Exception::InputParam, "wrong key type");
+Token Store::generateSKey(const CryptoAlgorithm &algorithm)
+{
+    return Internals::generateSKey(m_backendId, algorithm);
 }
 
 Token Store::import(DataType dataType, const RawBuffer &buffer) {
     return Token(m_backendId, dataType, buffer);
 }
 
-
-
-
-
 } // namespace SW
 } // namespace Crypto
 } // namespace CKM