[KeyManager] getKey implementation
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Mon, 27 Apr 2015 14:26:53 +0000 (16:26 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 11 May 2015 12:06:05 +0000 (21:06 +0900)
[Verification]
k = tizen.keymanager.getKey("pub2");
console.log(k.name);
console.log(k.password);
console.log(k.extractable);
console.log(k.keyType);
console.log(k.rawKey);

Change-Id: Ib1a83c8bac455b6a848079e7fe9d06a4a4b24fd2
Signed-off-by: Przemyslaw Ciezkowski <p.ciezkowski@samsung.com>
src/keymanager/keymanager_instance.cc
src/keymanager/keymanager_instance.h
src/keymanager/keymanager_observers.cc

index ce7f7847690277ac1244aadf200b66dae9caab40..f2a83a77a267f52250d99befb08a5ea53253320b 100644 (file)
@@ -27,6 +27,8 @@ KeyManagerInstance::KeyManagerInstance() {
 
   RegisterSyncHandler("KeyManager_getKeyAliasList",
       std::bind(&KeyManagerInstance::GetKeyAliasList, this, _1, _2));
+  RegisterSyncHandler("KeyManager_getKey",
+      std::bind(&KeyManagerInstance::GetKey, this, _1, _2));
   RegisterSyncHandler("KeyManager_saveKey",
       std::bind(&KeyManagerInstance::SaveKey, this, _1, _2));
   RegisterSyncHandler("KeyManager_removeKey",
@@ -180,5 +182,74 @@ void KeyManagerInstance::OnCreateKeyPair(double callbackId,
   PostMessage(res.serialize().c_str());
 }
 
+void KeyManagerInstance::GetKey(const picojson::value& args, picojson::object& out) {
+  LoggerD("Enter");
+  using CKM::KeyType;
+
+  const std::string& alias = args.get("name").get<std::string>();
+  CKM::Password pass;
+  if (args.get("password").is<std::string>()) {
+    pass = args.get("password").get<std::string>().c_str();
+  }
+
+  CKM::KeyShPtr key;
+  int ret = CKM::Manager::create()->getKey(alias, pass, key);
+  if (ret != CKM_API_SUCCESS) {
+    LoggerE("Failed to get key: %d", ret);
+    if (ret == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
+      ReportError(common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
+        "Key alias not found"), &out);
+    } else {
+      ReportError(common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
+        "Failed to get key"), &out);
+    }
+  } else {
+    picojson::object dict;
+    dict["name"] = args.get("name");
+    if (args.get("password").is<std::string>()) {
+      dict["password"] = args.get("password");
+    }
+    switch (key->getType()) {
+      case KeyType::KEY_NONE:
+        dict["keyType"] = picojson::value("KEY_NONE");
+        break;
+      case KeyType::KEY_RSA_PUBLIC:
+        dict["keyType"] = picojson::value("KEY_RSA_PUBLIC");
+        break;
+      case KeyType::KEY_RSA_PRIVATE:
+        dict["keyType"] = picojson::value("KEY_RSA_PRIVATE");
+        break;
+      case KeyType::KEY_ECDSA_PUBLIC:
+        dict["keyType"] = picojson::value("KEY_ECDSA_PUBLIC");
+        break;
+      case KeyType::KEY_ECDSA_PRIVATE:
+        dict["keyType"] = picojson::value("KEY_ECDSA_PRIVATE");
+        break;
+      case KeyType::KEY_DSA_PUBLIC:
+        dict["keyType"] = picojson::value("KEY_DSA_PUBLIC");
+        break;
+      case KeyType::KEY_DSA_PRIVATE:
+        dict["keyType"] = picojson::value("KEY_DSA_PRIVATE");
+        break;
+      case KeyType::KEY_AES:
+        dict["keyType"] = picojson::value("KEY_AES");
+        break;
+    }
+    CKM::RawBuffer buf = key->getDER();
+    if (!buf.empty()) {
+      gchar* base64 = g_base64_encode(&buf[0], buf.size());
+      dict["rawKey"] = picojson::value(std::string(base64));
+      g_free(base64);
+    } else {
+      dict["rawKey"] = picojson::value(std::string());
+    }
+    //if key was retrieved it is extractable from db
+    dict["extractable"] = picojson::value(true);
+
+    picojson::value res(dict);
+    ReportSuccess(res, out);
+  }
+}
+
 } // namespace keymanager
 } // namespace extension
index 025c7e9aa0b5affb5a3dbcffb35efefaea5129c2..1b5cabee562375a2d522320b0056952de25592e6 100644 (file)
@@ -23,6 +23,7 @@ class KeyManagerInstance :
   void OnCreateKeyPair(double callbackId, const common::PlatformResult& result);
  private:
   void GetKeyAliasList(picojson::value const& args, picojson::object& out);
+  void GetKey(const picojson::value& args, picojson::object& out);
   void SaveKey(const picojson::value& args, picojson::object& out);
   void RemoveKey(const picojson::value& args, picojson::object& out);
   void GenerateKeyPair(const picojson::value& args, picojson::object& out);
index 7c70c35836d3cb8a776363c98c044898303cfeb7..b3e06459154414300e4382cb4b90cf4244b322e3 100644 (file)
@@ -26,12 +26,20 @@ SaveKeyObserver::SaveKeyObserver(KeyManagerListener* listener, double callbackId
 void SaveKeyObserver::ReceivedError(int error) {
   LoggerD("Enter, error: %d", error);
   ErrorCode code = ErrorCode::UNKNOWN_ERR;
-  if (error == CKM_API_ERROR_INPUT_PARAM) {
-    code = ErrorCode::INVALID_VALUES_ERR;
+  std::string message =  "Failed to save key";
+  switch (error) {
+    case CKM_API_ERROR_INPUT_PARAM:
+      code = ErrorCode::INVALID_VALUES_ERR;
+      break;
+    case CKM_API_ERROR_DB_ALIAS_EXISTS:
+      code = ErrorCode::INVALID_VALUES_ERR;
+      message = "Key alias already exists";
+      break;
   }
   common::TaskQueue::GetInstance().Async(std::bind(
     &KeyManagerListener::OnSaveKey, listener, callbackId,
-    PlatformResult(code, "Failed to save key")));
+    PlatformResult(code, message
+  )));
 }
 
 void SaveKeyObserver::ReceivedSaveKey() {