Add algorithm param validation
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / store.cpp
index 9f18575..4d6b1bf 100644 (file)
 #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 {
@@ -36,21 +45,21 @@ Store::Store(CryptoBackend backendId)
     Internals::initialize();
 }
 
-GKeyShPtr Store::getKey(const Token &token) {
+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 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);
     }
 
     ThrowErr(Exc::Crypto::KeyNotSupported,
@@ -59,38 +68,12 @@ GKeyShPtr Store::getKey(const Token &token) {
 
 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))
-            ThrowErr(Exc::Crypto::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))
-            ThrowErr(Exc::Crypto::InputParam, "Error, parameter GEN_EC not found.");
-
-        return Internals::createKeyPairECDSA(m_backendId, static_cast<ElipticCurve>(ecType));
-    }
-    ThrowErr(Exc::Crypto::InputParam, "wrong key type");
+    return Internals::generateAKey(m_backendId, algorithm);
 }
 
 Token Store::generateSKey(const CryptoAlgorithm &algorithm)
 {
-    int keyLength = 0;
-    if(!algorithm.getParam(ParamName::GEN_KEY_LEN, keyLength))
-        ThrowErr(Exc::Crypto::InputParam, "Error, parameter GEN_KEY_LEN not found.");
-
-    return Internals::createKeyAES(m_backendId, keyLength);
+    return Internals::generateSKey(m_backendId, algorithm);
 }
 
 Token Store::import(DataType dataType, const RawBuffer &buffer) {