From b978d4e9d8e6690f445ce424b7747d72c7a18e65 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Wed, 1 Jul 2015 08:04:35 +0200
Subject: [PATCH] [KeyManager] Implementation of getKey() method.
Change-Id: Iddd6b513a6ce9ce67b2b8c2cd282ce0a206aa1db
Signed-off-by: Pawel Andruszkiewicz
---
src/keymanager/keymanager_instance.cc | 79 +++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/src/keymanager/keymanager_instance.cc b/src/keymanager/keymanager_instance.cc
index 7b1dce6..e597c31 100755
--- a/src/keymanager/keymanager_instance.cc
+++ b/src/keymanager/keymanager_instance.cc
@@ -105,6 +105,44 @@ ckmc_key_type_e StringToKeyType(const std::string& type) {
return CKMC_KEY_NONE;
}
}
+
+std::string KeyTypeToString(ckmc_key_type_e type) {
+ LoggerD("Enter");
+
+ switch (type) {
+ case CKMC_KEY_NONE:
+ return "KEY_NONE";
+
+ case CKMC_KEY_RSA_PUBLIC:
+ return "KEY_RSA_PUBLIC";
+
+ case CKMC_KEY_RSA_PRIVATE:
+ return "KEY_RSA_PRIVATE";
+
+ case CKMC_KEY_ECDSA_PUBLIC:
+ return "KEY_ECDSA_PUBLIC";
+
+ case CKMC_KEY_ECDSA_PRIVATE:
+ return "KEY_ECDSA_PRIVATE";
+
+ case CKMC_KEY_DSA_PUBLIC:
+ return "KEY_DSA_PUBLIC";
+
+ case CKMC_KEY_DSA_PRIVATE:
+ return "KEY_DSA_PRIVATE";
+
+ case CKMC_KEY_AES:
+ return "KEY_AES";
+ }
+
+ LoggerE("Unknown key type");
+ return "KEY_UNKNOWN";
+}
+
+RawBuffer ToRawBuffer(const ckmc_key_s* key) {
+ return RawBuffer(key->raw_key, key->raw_key + key->key_size);
+}
+
} // namespace
KeyManagerInstance::KeyManagerInstance() {
@@ -170,6 +208,47 @@ void KeyManagerInstance::GetDataAliasList(const picojson::value& args,
void KeyManagerInstance::GetKey(const picojson::value& args,
picojson::object& out) {
LoggerD("Enter");
+ const auto& key_alias = args.get("name").get();
+ const auto& password_value = args.get("password");
+
+ std::string password;
+
+ if (password_value.is()) {
+ password = password_value.get();
+ }
+
+ ckmc_key_s* key = nullptr;
+ int ret = ckmc_get_key(key_alias.c_str(), password.c_str(), &key);
+
+ if (CKMC_ERROR_NONE == ret) {
+ picojson::object result;
+
+ result["name"] = picojson::value(key_alias);
+ result["password"] = picojson::value(key->password);
+ // if key was retrieved it is extractable from DB
+ result["extractable"] = picojson::value(true);
+ result["keyType"] = picojson::value(KeyTypeToString(key->key_type));
+ result["rawKey"] = picojson::value(RawBufferToBase64(ToRawBuffer(key)));
+
+ ckmc_key_free(key);
+ ReportSuccess(picojson::value{result}, out);
+ } else {
+ LoggerE("Failed to get key: %d", ret);
+
+ PlatformResult error(ErrorCode::UNKNOWN_ERR, "Failed to get key");
+
+ switch (ret) {
+ case CKMC_ERROR_DB_ALIAS_UNKNOWN:
+ error = PlatformResult(ErrorCode::NOT_FOUND_ERR, "Failed to find key");
+ break;
+
+ case CKMC_ERROR_INVALID_PARAMETER:
+ error = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Input parameter is invalid");
+ break;
+ }
+
+ ReportError(error, &out);
+ }
}
void KeyManagerInstance::SaveKey(const picojson::value& args,
--
2.7.4