From: Przemyslaw Ciezkowski
Date: Fri, 24 Apr 2015 13:50:06 +0000 (+0200)
Subject: [KeyManager] saveKey implementation
X-Git-Tag: submit/tizen_mobile/20150603.064609~1^2~50
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=09b8043604a9b1a90742eb0ec5a64900ddf7ad12;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git
[KeyManager] saveKey implementation
[Verification]
var myPubKey = new tizen.Key("pub1", null, true);
myPubKey.saveKey("MIIBIj...wIDAQAB", function() {
console.log("ok");
}, function(e) {
console.log("error: " + e.message);
});
Change-Id: I5f58737b0ce5d794c1b611100c0a760d26a441ed
Signed-off-by: Przemyslaw Ciezkowski
---
diff --git a/src/keymanager/keymanager.gyp b/src/keymanager/keymanager.gyp
index 99ae7ff..65ddb7f 100644
--- a/src/keymanager/keymanager.gyp
+++ b/src/keymanager/keymanager.gyp
@@ -15,12 +15,15 @@
'keymanager_extension.h',
'keymanager_instance.cc',
'keymanager_instance.h',
+ 'keymanager_observers.h',
+ 'keymanager_observers.cc',
],
'conditions': [
['tizen == 1', {
'variables': {
'packages': [
'key-manager',
+ 'libpcrecpp',
]
},
}],
diff --git a/src/keymanager/keymanager_instance.cc b/src/keymanager/keymanager_instance.cc
index 949327e..106d1c4 100644
--- a/src/keymanager/keymanager_instance.cc
+++ b/src/keymanager/keymanager_instance.cc
@@ -6,6 +6,8 @@
#include
#include
+#include
+#include
#include "common/logger.h"
#include "common/picojson.h"
@@ -21,6 +23,8 @@ KeyManagerInstance::KeyManagerInstance() {
RegisterSyncHandler("KeyManager_getKeyAliasList",
std::bind(&KeyManagerInstance::GetKeyAliasList, this, _1, _2));
+ RegisterSyncHandler("KeyManager_saveKey",
+ std::bind(&KeyManagerInstance::SaveKey, this, _1, _2));
}
KeyManagerInstance::~KeyManagerInstance() {
@@ -45,5 +49,48 @@ void KeyManagerInstance::GetKeyAliasList(const picojson::value& args,
}
}
+void KeyManagerInstance::SaveKey(const picojson::value& args,
+ picojson::object& out) {
+ LoggerD("Enter");
+
+ const picojson::value& key_object = args.get("key");
+ const std::string& alias = key_object.get("name").get();
+ std::string password;
+ if (key_object.get("password").is()) {
+ password = key_object.get("password").get();
+ }
+ std::string base64 = args.get("rawKey").get();
+ pcrecpp::RE_Options opt;
+ opt.set_multiline(true);
+ //remove first line and last line
+ pcrecpp::RE("-----[^-]*-----", opt).GlobalReplace("", &base64);
+ gsize len = 0;
+ guchar* raw_data = g_base64_decode(base64.c_str(), &len);
+ CKM::RawBuffer raw_buffer;
+ raw_buffer.assign(raw_data, raw_data + len);
+ g_free(raw_data);
+ CKM::Password pass(password.c_str());
+ CKM::KeyShPtr key = CKM::Key::create(raw_buffer, pass);
+ CKM::Policy policy(pass, key_object.get("extractable").get());
+ CKM::ManagerAsync::ObserverPtr observer(new SaveKeyObserver(this,
+ args.get("callbackId").get()));
+ m_manager.saveKey(observer, alias, key, policy);
+
+ ReportSuccess(out);
+}
+
+void KeyManagerInstance::OnSaveKey(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
diff --git a/src/keymanager/keymanager_instance.h b/src/keymanager/keymanager_instance.h
index b34fce0..517f0b4 100644
--- a/src/keymanager/keymanager_instance.h
+++ b/src/keymanager/keymanager_instance.h
@@ -6,16 +6,25 @@
#define KEYMANAGER_KEYMANAGER_INSTANCE_H_
#include "common/extension.h"
+#include
+#include "keymanager/keymanager_observers.h"
+
namespace extension {
namespace keymanager {
-class KeyManagerInstance : public common::ParsedInstance {
+class KeyManagerInstance :
+ public common::ParsedInstance,
+ public KeyManagerListener {
public:
KeyManagerInstance();
virtual ~KeyManagerInstance();
+ void OnSaveKey(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);
+
+ CKM::ManagerAsync m_manager;
};
} // namespace keymanager
diff --git a/src/keymanager/keymanager_observers.cc b/src/keymanager/keymanager_observers.cc
new file mode 100644
index 0000000..5e9dc95
--- /dev/null
+++ b/src/keymanager/keymanager_observers.cc
@@ -0,0 +1,46 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "keymanager/keymanager_observers.h"
+#include "common/extension.h"
+#include "common/logger.h"
+#include "common/task-queue.h"
+
+namespace extension {
+namespace keymanager {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+CommonObserver::CommonObserver(KeyManagerListener* _listener, double _callbackId):
+ listener(_listener), callbackId(_callbackId) {
+
+}
+
+SaveKeyObserver::SaveKeyObserver(KeyManagerListener* listener, double callbackId):
+ CommonObserver(listener, 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;
+ }
+ common::TaskQueue::GetInstance().Async(std::bind(
+ &KeyManagerListener::OnSaveKey, listener, callbackId,
+ PlatformResult(code, "Failed to save key")));
+}
+
+void SaveKeyObserver::ReceivedSaveKey() {
+ LoggerD("Enter");
+ common::TaskQueue::GetInstance().Async(std::bind(
+ &KeyManagerListener::OnSaveKey, listener, callbackId,
+ PlatformResult(ErrorCode::NO_ERROR)));
+}
+
+
+} // namespace keymanager
+} // namespace extension
diff --git a/src/keymanager/keymanager_observers.h b/src/keymanager/keymanager_observers.h
new file mode 100644
index 0000000..86688e6
--- /dev/null
+++ b/src/keymanager/keymanager_observers.h
@@ -0,0 +1,40 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef KEYMANAGER_KEYMANAGER_OBSERVERS_H_
+#define KEYMANAGER_KEYMANAGER_OBSERVERS_H_
+
+#include
+#include "common/platform_result.h"
+
+namespace extension {
+namespace keymanager {
+
+class KeyManagerListener {
+public:
+ virtual void OnSaveKey(double callbackId, const common::PlatformResult& result) = 0;
+ virtual ~KeyManagerListener() {}
+
+};
+
+struct CommonObserver: public CKM::ManagerAsync::Observer {
+ CommonObserver(KeyManagerListener* listener, double callbackId);
+ virtual ~CommonObserver() {}
+
+ protected:
+ KeyManagerListener* listener;
+ double callbackId;
+};
+
+struct SaveKeyObserver: public CommonObserver {
+ SaveKeyObserver(KeyManagerListener* listener, double callbackId);
+ void ReceivedError(int error);
+ void ReceivedSaveKey();
+};
+
+} // namespace keymanager
+} // namespace extension
+
+#endif // KEYMANAGER_KEYMANAGER_OBSERVERS_H_
+