Fix parameter validation in ocsp
[platform/core/security/key-manager.git] / src / manager / client / client-manager-impl.cpp
index 9316cd8..3bb1ef9 100644 (file)
@@ -29,6 +29,7 @@
 #include <message-buffer.h>
 #include <protocols.h>
 #include <key-impl.h>
+#include <key-aes-impl.h>
 #include <certificate-impl.h>
 
 namespace CKM {
@@ -322,7 +323,11 @@ int ManagerImpl::getKey(const Alias &alias, const Password &password, KeyShPtr &
     if (retCode != CKM_API_SUCCESS)
         return retCode;
 
-    KeyShPtr keyParsed(new KeyImpl(rawData));
+    KeyShPtr keyParsed;
+    if(DataType::KEY_AES == recvDataType)
+        keyParsed = KeyShPtr(new KeyAESImpl(rawData));
+    else
+        keyParsed = KeyShPtr(new KeyImpl(rawData));
 
     if (keyParsed->empty()) {
         LogDebug("Key empty - failed to parse!");
@@ -456,6 +461,41 @@ int ManagerImpl::createKeyPairECDSA(
     return this->createKeyPair(CKM::KeyType::KEY_ECDSA_PUBLIC, static_cast<int>(type), privateKeyAlias, publicKeyAlias, policyPrivateKey, policyPublicKey);
 }
 
+int ManagerImpl::createKeyAES(
+    const int size,
+    const Alias &keyAlias,
+    const Policy &policyKey)
+{
+    // proceed with sending request
+    int my_counter = ++m_counter;
+
+    return try_catch([&] {
+
+        MessageBuffer recv;
+        AliasSupport aliasHelper(keyAlias);
+        auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_KEY_AES),
+                                             my_counter,
+                                             static_cast<int>(size),
+                                             PolicySerializable(policyKey),
+                                             aliasHelper.getName(),
+                                             aliasHelper.getLabel());
+
+        int retCode = m_storageConnection.processRequest(send.Pop(), recv);
+        if (CKM_API_SUCCESS != retCode)
+            return retCode;
+
+        int command;
+        int counter;
+        recv.Deserialize(command, counter, retCode);
+        if (counter != my_counter) {
+            return CKM_API_ERROR_UNKNOWN;
+        }
+
+        return retCode;
+    });
+}
+
+
 int ManagerImpl::createKeyPair(
     const KeyType key_type,
     const int     additional_param,
@@ -465,22 +505,25 @@ int ManagerImpl::createKeyPair(
     const Policy &policyPublicKey)
 {
     // input type check
-    LogicCommand cmd_type;
+    CryptoAlgorithm keyGenAlgorithm;
     switch(key_type)
     {
         case KeyType::KEY_RSA_PUBLIC:
         case KeyType::KEY_RSA_PRIVATE:
-            cmd_type = LogicCommand::CREATE_KEY_PAIR_RSA;
+            keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::RSA_GEN);
+            keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
             break;
 
         case KeyType::KEY_DSA_PUBLIC:
         case KeyType::KEY_DSA_PRIVATE:
-            cmd_type = LogicCommand::CREATE_KEY_PAIR_DSA;
+            keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::DSA_GEN);
+            keyGenAlgorithm.setParam(ParamName::GEN_KEY_LEN, additional_param);
             break;
 
         case KeyType::KEY_ECDSA_PUBLIC:
         case KeyType::KEY_ECDSA_PRIVATE:
-            cmd_type = LogicCommand::CREATE_KEY_PAIR_ECDSA;
+            keyGenAlgorithm.setParam(ParamName::ALGO_TYPE, AlgoType::ECDSA_GEN);
+            keyGenAlgorithm.setParam(ParamName::GEN_EC, additional_param);
             break;
 
         default:
@@ -495,9 +538,9 @@ int ManagerImpl::createKeyPair(
         MessageBuffer recv;
         AliasSupport privateHelper(privateKeyAlias);
         AliasSupport publicHelper(publicKeyAlias);
-        auto send = MessageBuffer::Serialize(static_cast<int>(cmd_type),
+        auto send = MessageBuffer::Serialize(static_cast<int>(LogicCommand::CREATE_KEY_PAIR),
                                              my_counter,
-                                             static_cast<int>(additional_param),
+                                             CryptoAlgorithmSerializable(keyGenAlgorithm),
                                              PolicySerializable(policyPrivateKey),
                                              PolicySerializable(policyPublicKey),
                                              privateHelper.getName(),
@@ -668,6 +711,10 @@ int ManagerImpl::ocspCheck(const CertificateShPtrVector &certChain, int &ocspSta
 
         RawBufferVector rawCertChain;
         for (auto &e: certChain) {
+            if (!e || e->empty()) {
+                LogError("Empty certificate");
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
             rawCertChain.push_back(e->getDER());
         }
 
@@ -720,11 +767,12 @@ int ManagerImpl::setPermission(const Alias &alias,
     });
 }
 
-int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
-                         const Alias &keyAlias,
-                         const Password &password,
-                         const RawBuffer& plain,
-                         RawBuffer& encrypted)
+int ManagerImpl::crypt(EncryptionCommand command,
+          const CryptoAlgorithm &algo,
+          const Alias &keyAlias,
+          const Password &password,
+          const RawBuffer& input,
+          RawBuffer& output)
 {
     int my_counter = ++m_counter;
 
@@ -732,13 +780,13 @@ int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
         MessageBuffer recv;
         AliasSupport helper(keyAlias);
         CryptoAlgorithmSerializable cas(algo);
-        auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::ENCRYPT),
+        auto send = MessageBuffer::Serialize(static_cast<int>(command),
                                              my_counter,
                                              cas,
                                              helper.getName(),
                                              helper.getLabel(),
                                              password,
-                                             plain);
+                                             input);
 
         int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
         if (CKM_API_SUCCESS != retCode)
@@ -746,7 +794,7 @@ int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
 
         int command;
         int counter;
-        recv.Deserialize(command, counter, encrypted);
+        recv.Deserialize(command, counter, retCode, output);
 
         if (my_counter != counter) {
             return CKM_API_ERROR_UNKNOWN;
@@ -756,40 +804,22 @@ int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
     });
 }
 
+int ManagerImpl::encrypt(const CryptoAlgorithm &algo,
+            const Alias &keyAlias,
+            const Password &password,
+            const RawBuffer& plain,
+            RawBuffer& encrypted)
+{
+    return crypt(EncryptionCommand::ENCRYPT, algo, keyAlias, password, plain, encrypted);
+}
+
 int ManagerImpl::decrypt(const CryptoAlgorithm &algo,
                          const Alias &keyAlias,
                          const Password &password,
                          const RawBuffer& encrypted,
                          RawBuffer& decrypted)
 {
-    int my_counter = ++m_counter;
-
-    return try_catch([&] {
-        MessageBuffer recv;
-        AliasSupport helper(keyAlias);
-        CryptoAlgorithmSerializable cas(algo);
-        auto send = MessageBuffer::Serialize(static_cast<int>(EncryptionCommand::DECRYPT),
-                                             my_counter,
-                                             cas,
-                                             helper.getName(),
-                                             helper.getLabel(),
-                                             password,
-                                             encrypted);
-
-        int retCode = m_encryptionConnection.processRequest(send.Pop(), recv);
-        if (CKM_API_SUCCESS != retCode)
-            return retCode;
-
-        int command;
-        int counter;
-        recv.Deserialize(command, counter, decrypted);
-
-        if (my_counter != counter) {
-            return CKM_API_ERROR_UNKNOWN;
-        }
-
-        return retCode;
-    });
+    return crypt(EncryptionCommand::DECRYPT, algo, keyAlias, password, encrypted, decrypted);
 }
 
 ManagerShPtr Manager::create() {