From: Tomasz Marciniak Date: Wed, 1 Jul 2015 09:56:40 +0000 (+0200) Subject: [KeyManager] save() implementation of Key class. X-Git-Tag: accepted/tizen/mobile/20150708.015027^2~1^2~45 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bd9c015d23a9c0b3a7b462f8b206da2e4195a5dd;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [KeyManager] save() implementation of Key class. [Verification] Code compiles without errors. Change-Id: Id0928ccc3a2b1e5ea35626e0472a6cc840b7d072 Signed-off-by: Tomasz Marciniak --- diff --git a/src/keymanager/keymanager_api.js b/src/keymanager/keymanager_api.js index e77a4f9..f63a53a 100755 --- a/src/keymanager/keymanager_api.js +++ b/src/keymanager/keymanager_api.js @@ -120,7 +120,7 @@ Key.prototype.save = function() { native.call('KeyManager_saveKey', { key: this, - rawKey: args.rawKey + rawKey: stripPemString(args.rawKey) }, function(msg) { if (native.isFailure(msg)) { if (type.isFunction(args.errorCallback)) { diff --git a/src/keymanager/keymanager_instance.cc b/src/keymanager/keymanager_instance.cc index 4c1d571..597ce37 100755 --- a/src/keymanager/keymanager_instance.cc +++ b/src/keymanager/keymanager_instance.cc @@ -84,6 +84,27 @@ ckmc_ec_type_e GetEllipticCurveType(const std::string& type) { } } +ckmc_key_type_e StringToKeyType(const std::string& type) { + LoggerD("Enter"); + + if ("KEY_RSA_PUBLIC" == type) { + return CKMC_KEY_RSA_PUBLIC; + } else if ("KEY_RSA_PRIVATE" == type) { + return CKMC_KEY_RSA_PRIVATE; + } else if ("KEY_ECDSA_PUBLIC" == type) { + return CKMC_KEY_ECDSA_PUBLIC; + } else if ("KEY_ECDSA_PRIVATE" == type) { + return CKMC_KEY_ECDSA_PRIVATE; + } else if ("KEY_DSA_PUBLIC" == type) { + return CKMC_KEY_DSA_PUBLIC; + } else if ("KEY_DSA_PRIVATE" == type) { + return CKMC_KEY_DSA_PRIVATE; + } else if ("KEY_AES" == type) { + return CKMC_KEY_AES; + } else { + return CKMC_KEY_NONE; + } +} } // namespace KeyManagerInstance::KeyManagerInstance() { @@ -158,6 +179,59 @@ void KeyManagerInstance::GetKey(const picojson::value& args, void KeyManagerInstance::SaveKey(const picojson::value& args, picojson::object& out) { LoggerD("Enter"); + + const picojson::value& key_obj = args.get("key"); + const std::string& alias = key_obj.get("name").get(); + const std::string& type = key_obj.get("keyType").get(); + bool extractable = key_obj.get("extractable").get(); + const double callback_id = args.get("callbackId").get(); + + std::string base64; + if (args.get("rawKey").is()) { + base64 = args.get("rawKey").get(); + } + + std::string pass; + if (key_obj.get("password").is()) { + pass = key_obj.get("password").get(); + } + + RawBuffer* raw_buffer = new RawBuffer(std::move(Base64ToRawBuffer(base64))); + ckmc_key_type_e key_type = StringToKeyType(type); + + auto save = [alias, pass, key_type, extractable, raw_buffer] + (const std::shared_ptr& response) -> void { + + ckmc_policy_s policy { const_cast(pass.c_str()), extractable }; + ckmc_key_s key { const_cast(&(*raw_buffer)[0]), + raw_buffer->size(), key_type, const_cast(pass.c_str()) }; + + int ret = ckmc_save_key(alias.c_str(), key, policy); + if (CKMC_ERROR_NONE != ret) { + PlatformResult result = PlatformResult(ErrorCode::NO_ERROR); + if (CKMC_ERROR_INVALID_PARAMETER == ret) { + result = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid parameter passed."); + } else { + result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to save key."); + } + common::tools::ReportError(result, &response->get()); + } else { + common::tools::ReportSuccess(response->get()); + } + + delete raw_buffer; + }; + + auto save_response = [this, callback_id](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", picojson::value(callback_id))); + this->PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue( + save, + save_response, + std::shared_ptr(new picojson::value(picojson::object()))); } void KeyManagerInstance::RemoveKey(const picojson::value& args,