[KeyManager] generateKeyPair implementation
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Mon, 27 Apr 2015 13:17:35 +0000 (15:17 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 11 May 2015 12:05:24 +0000 (21:05 +0900)
[Verification]
var myPrivKey = new tizen.Key("priv3", "priv_password", false);
var myPubKey = new tizen.Key("pub3", null, true);

tizen.keymanager.generateKeyPair(myPrivKey, myPubKey, "RSA", "2048", function() {
    console.log('ok');
  }, function(e) {
    console.log(e.code);
    console.log(e.message);
});

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

index 8c6022e..ce7f784 100644 (file)
 namespace extension {
 namespace keymanager {
 
+namespace {
+const char* kTypeRSA = "RSA";
+const char* kTypeECDSA = "ECDSA";
+}
 
 KeyManagerInstance::KeyManagerInstance() {
   using std::placeholders::_1;
@@ -27,6 +31,8 @@ KeyManagerInstance::KeyManagerInstance() {
       std::bind(&KeyManagerInstance::SaveKey, this, _1, _2));
   RegisterSyncHandler("KeyManager_removeKey",
       std::bind(&KeyManagerInstance::RemoveKey, this, _1, _2));
+  RegisterSyncHandler("KeyManager_generateKeyPair",
+      std::bind(&KeyManagerInstance::GenerateKeyPair, this, _1, _2));
 }
 
 KeyManagerInstance::~KeyManagerInstance() {
@@ -114,5 +120,65 @@ void KeyManagerInstance::RemoveKey(const picojson::value& args,
   }
 }
 
+void KeyManagerInstance::GenerateKeyPair(const picojson::value& args,
+    picojson::object& out) {
+  LoggerD("Enter");
+
+  const picojson::value& priv_key = args.get("privKeyName");
+  const picojson::value& pub_key = args.get("pubKeyName");
+  const std::string& priv_name = priv_key.get("name").get<std::string>();
+  const std::string& pub_name = pub_key.get("name").get<std::string>();
+  const std::string& type = args.get("type").get<std::string>();
+  int size = std::stoi(args.get("size").get<std::string>());
+
+  CKM::ManagerAsync::ObserverPtr observer(new CreateKeyObserver(this,
+    args.get("callbackId").get<double>()));
+
+  CKM::Password pass;
+  if (priv_key.get("password").is<std::string>()) {
+    pass = priv_key.get("password").get<std::string>().c_str();
+  }
+  CKM::Policy priv_policy(pass, priv_key.get("extractable").get<bool>());
+
+  if (pub_key.get("password").is<std::string>()) {
+    pass = pub_key.get("password").get<std::string>().c_str();
+  } else {
+    pass = "";
+  }
+  CKM::Policy pub_policy(pass, pub_key.get("extractable").get<bool>());
+
+  if (type == kTypeRSA) {
+    m_manager.createKeyPairRSA(observer, size, priv_name, pub_name, priv_policy, pub_policy);
+  } else if (type == kTypeECDSA) {
+    CKM::ElipticCurve eliptic = CKM::ElipticCurve::prime192v1;
+    if (args.get("ellipticCurveType").is<std::string>()) {
+      const std::string& eType = args.get("ellipticCurveType").get<std::string>();
+      if (eType == "PRIME256V1") {
+        eliptic = CKM::ElipticCurve::prime256v1;
+      } else if (eType == "EC_SECP384R1") {
+        eliptic = CKM::ElipticCurve::secp384r1;
+      }
+    }
+    m_manager.createKeyPairECDSA(observer, eliptic, priv_name, pub_name, priv_policy, pub_policy);
+  } else {
+    m_manager.createKeyPairDSA(observer, size, priv_name, pub_name, priv_policy, pub_policy);
+  }
+
+  ReportSuccess(out);
+}
+
+void KeyManagerInstance::OnCreateKeyPair(double callbackId,
+    const common::PlatformResult& result) {
+  LoggerD("Enter");
+  picojson::value::object dict;
+  dict["callbackId"] = picojson::value(callbackId);
+  if (result.IsError()) {
+    LoggerE("There was an error");
+    ReportError(result, &dict);
+  }
+  picojson::value res(dict);
+  PostMessage(res.serialize().c_str());
+}
+
 } // namespace keymanager
 } // namespace extension
index 1d9f0b6..025c7e9 100644 (file)
@@ -20,10 +20,12 @@ class KeyManagerInstance :
   KeyManagerInstance();
   virtual ~KeyManagerInstance();
   void OnSaveKey(double callbackId, const common::PlatformResult& result);
+  void OnCreateKeyPair(double callbackId, const common::PlatformResult& result);
  private:
   void GetKeyAliasList(picojson::value const& 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);
 
   CKM::ManagerAsync m_manager;
 };
index 5e9dc95..7c70c35 100644 (file)
@@ -41,6 +41,47 @@ void SaveKeyObserver::ReceivedSaveKey() {
     PlatformResult(ErrorCode::NO_ERROR)));
 }
 
+CreateKeyObserver::CreateKeyObserver(KeyManagerListener* listener, double callbackId):
+    CommonObserver(listener, callbackId) {}
+
+void CreateKeyObserver::ReceivedCreateKeyPairDSA() {
+  LoggerD("Enter");
+  CallSuccess();
+}
+
+void CreateKeyObserver::ReceivedCreateKeyPairECDSA() {
+  LoggerD("Enter");
+  CallSuccess();
+}
+
+void CreateKeyObserver::ReceivedCreateKeyPairRSA() {
+  LoggerD("Enter");
+  CallSuccess();
+}
+
+void CreateKeyObserver::CallSuccess() {
+  common::TaskQueue::GetInstance().Async(std::bind(
+    &KeyManagerListener::OnCreateKeyPair, listener, callbackId,
+    PlatformResult(ErrorCode::NO_ERROR)));
+}
+
+void CreateKeyObserver::ReceivedError(int error) {
+  LoggerD("Enter, error: %d", error);
+  ErrorCode code = ErrorCode::UNKNOWN_ERR;
+  std::string message = "Failed to create key pair";
+  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::OnCreateKeyPair, listener, callbackId,
+    PlatformResult(code, message)));
+}
 
 } // namespace keymanager
 } // namespace extension
index 86688e6..68f98e3 100644 (file)
@@ -14,8 +14,8 @@ namespace keymanager {
 class KeyManagerListener {
 public:
   virtual void OnSaveKey(double callbackId, const common::PlatformResult& result) = 0;
+  virtual void OnCreateKeyPair(double callbackId, const common::PlatformResult& result) = 0;
   virtual ~KeyManagerListener() {}
-
 };
 
 struct CommonObserver: public CKM::ManagerAsync::Observer {
@@ -33,6 +33,17 @@ struct SaveKeyObserver: public CommonObserver {
   void ReceivedSaveKey();
 };
 
+struct CreateKeyObserver: public CommonObserver {
+  CreateKeyObserver(KeyManagerListener* listener, double callbackId);
+  void ReceivedError(int error);
+  void ReceivedCreateKeyPairDSA();
+  void ReceivedCreateKeyPairECDSA();
+  void ReceivedCreateKeyPairRSA();
+private:
+  void CallSuccess();
+
+};
+
 } // namespace keymanager
 } // namespace extension