2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "keymanager/keymanager_instance.h"
20 #include <ckm/ckm-manager.h>
25 #include "common/logger.h"
26 #include "common/picojson.h"
27 #include "common/platform_result.h"
30 namespace keymanager {
33 const char* kTypeRSA = "RSA";
34 const char* kTypeECDSA = "ECDSA";
37 KeyManagerInstance::KeyManagerInstance() {
39 using std::placeholders::_1;
40 using std::placeholders::_2;
42 RegisterSyncHandler("KeyManager_getKeyAliasList",
43 std::bind(&KeyManagerInstance::GetKeyAliasList, this, _1, _2));
44 RegisterSyncHandler("KeyManager_getCertificatesAliasList",
45 std::bind(&KeyManagerInstance::GetCertificateAliasList, this, _1, _2));
46 RegisterSyncHandler("KeyManager_getDataAliasList",
47 std::bind(&KeyManagerInstance::GetDataAliasList, this, _1, _2));
48 RegisterSyncHandler("KeyManager_getKey",
49 std::bind(&KeyManagerInstance::GetKey, this, _1, _2));
50 RegisterSyncHandler("KeyManager_saveKey",
51 std::bind(&KeyManagerInstance::SaveKey, this, _1, _2));
52 RegisterSyncHandler("KeyManager_removeKey",
53 std::bind(&KeyManagerInstance::RemoveKey, this, _1, _2));
54 RegisterSyncHandler("KeyManager_generateKeyPair",
55 std::bind(&KeyManagerInstance::GenerateKeyPair, this, _1, _2));
56 RegisterSyncHandler("KeyManager_getCertificate",
57 std::bind(&KeyManagerInstance::GetCertificate, this, _1, _2));
58 RegisterSyncHandler("KeyManager_saveCertificate",
59 std::bind(&KeyManagerInstance::SaveCertificate, this, _1, _2));
60 RegisterSyncHandler("KeyManager_loadCertificateFromFile",
61 std::bind(&KeyManagerInstance::LoadCertificateFromFile, this, _1, _2));
62 RegisterSyncHandler("KeyManager_removeCertificate",
63 std::bind(&KeyManagerInstance::RemoveCertificate, this, _1, _2));
64 RegisterSyncHandler("KeyManager_saveData",
65 std::bind(&KeyManagerInstance::SaveData, this, _1, _2));
66 RegisterSyncHandler("KeyManager_removeData",
67 std::bind(&KeyManagerInstance::RemoveData, this, _1, _2));
68 RegisterSyncHandler("KeyManager_getData",
69 std::bind(&KeyManagerInstance::GetData, this, _1, _2));
70 RegisterSyncHandler("KeyManager_createSignature",
71 std::bind(&KeyManagerInstance::CreateSignature, this, _1, _2));
72 RegisterSyncHandler("KeyManager_verifySignature",
73 std::bind(&KeyManagerInstance::VerifySignature, this, _1, _2));
74 RegisterSyncHandler("KeyManager_loadFromPKCS12File",
75 std::bind(&KeyManagerInstance::LoadFromPKCS12File, this, _1, _2));
76 RegisterSyncHandler("KeyManager_allowAccessControl",
77 std::bind(&KeyManagerInstance::AllowAccessControl, this, _1, _2));
78 RegisterSyncHandler("KeyManager_denyAccessControl",
79 std::bind(&KeyManagerInstance::DenyAccessControl, this, _1, _2));
82 KeyManagerInstance::~KeyManagerInstance() {
86 void KeyManagerInstance::GetAliasList(
87 std::function<int(CKM::AliasVector&)> coreFunc, picojson::object& out) {
89 CKM::AliasVector result;
90 int ret = coreFunc(result);
91 if (ret != CKM_API_SUCCESS) {
92 LoggerE("Failed to fetch list of alias: %d", ret);
93 ReportError(common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
94 "Failed to fetch list of alias"), &out);
96 picojson::array aliases;
97 for (auto& item: result) {
98 aliases.push_back(picojson::value(item));
100 picojson::value res(aliases);
101 ReportSuccess(res, out);
105 void KeyManagerInstance::GetKeyAliasList(const picojson::value& args,
106 picojson::object& out) {
108 using std::placeholders::_1;
110 std::bind(&CKM::Manager::getKeyAliasVector, CKM::Manager::create(), _1),
114 void KeyManagerInstance::GetCertificateAliasList(const picojson::value& args,
115 picojson::object& out) {
117 using std::placeholders::_1;
119 std::bind(&CKM::Manager::getCertificateAliasVector, CKM::Manager::create(), _1),
123 void KeyManagerInstance::GetDataAliasList(const picojson::value& args,
124 picojson::object& out) {
126 using std::placeholders::_1;
128 std::bind(&CKM::Manager::getDataAliasVector, CKM::Manager::create(), _1),
132 CKM::RawBuffer Base64ToRawBuffer(const std::string base64) {
135 guchar* raw_data = g_base64_decode(base64.c_str(), &len);
136 CKM::RawBuffer rawBuffer;
138 rawBuffer.assign(raw_data, raw_data + len);
144 void KeyManagerInstance::SaveKey(const picojson::value& args,
145 picojson::object& out) {
148 const picojson::value& key_object = args.get("key");
149 const std::string& alias = key_object.get("name").get<std::string>();
150 std::string password;
151 if (key_object.get("password").is<std::string>()) {
152 password = key_object.get("password").get<std::string>();
154 std::string base64 = args.get("rawKey").get<std::string>();
155 pcrecpp::RE_Options opt;
156 opt.set_multiline(true);
157 //remove first line and last line
158 pcrecpp::RE("-----[^-]*-----", opt).GlobalReplace("", &base64);
159 CKM::RawBuffer raw_buffer = Base64ToRawBuffer(base64);
160 CKM::Password pass(password.c_str());
161 CKM::KeyShPtr key = CKM::Key::create(raw_buffer, pass);
162 CKM::Policy policy(pass, key_object.get("extractable").get<bool>());
163 CKM::ManagerAsync::ObserverPtr observer(new SaveKeyObserver(this,
164 args.get("callbackId").get<double>()));
165 m_manager.saveKey(observer, alias, key, policy);
170 void KeyManagerInstance::OnSaveKey(double callbackId,
171 const common::PlatformResult& result) {
173 picojson::value::object dict;
174 dict["callbackId"] = picojson::value(callbackId);
175 if (result.IsError()) {
176 LoggerE("There was an error");
177 ReportError(result, &dict);
179 picojson::value res(dict);
180 PostMessage(res.serialize().c_str());
183 void KeyManagerInstance::RemoveKey(const picojson::value& args,
184 picojson::object& out) {
187 const std::string& alias = args.get("key").get("name").get<std::string>();
188 common::PlatformResult res = RemoveAlias(alias);
190 ReportError(res, &out);
196 common::PlatformResult KeyManagerInstance::RemoveAlias(
197 const std::string& alias) {
199 int ret = CKM::Manager::create()->removeAlias(alias);
200 if (ret != CKM_API_SUCCESS) {
201 LoggerE("Failed to remove alias: %d", ret);
202 if (ret == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
203 return common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
204 "Key alias not found");
206 return common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
207 "Failed to remove alias");
210 return common::PlatformResult(common::ErrorCode::NO_ERROR);
213 void KeyManagerInstance::GenerateKeyPair(const picojson::value& args,
214 picojson::object& out) {
217 const picojson::value& priv_key = args.get("privKeyName");
218 const picojson::value& pub_key = args.get("pubKeyName");
219 const std::string& priv_name = priv_key.get("name").get<std::string>();
220 const std::string& pub_name = pub_key.get("name").get<std::string>();
221 const std::string& type = args.get("type").get<std::string>();
222 int size = std::stoi(args.get("size").get<std::string>());
224 CKM::ManagerAsync::ObserverPtr observer(new CreateKeyObserver(this,
225 args.get("callbackId").get<double>()));
228 if (priv_key.get("password").is<std::string>()) {
229 pass = priv_key.get("password").get<std::string>().c_str();
231 CKM::Policy priv_policy(pass, priv_key.get("extractable").get<bool>());
233 if (pub_key.get("password").is<std::string>()) {
234 pass = pub_key.get("password").get<std::string>().c_str();
238 CKM::Policy pub_policy(pass, pub_key.get("extractable").get<bool>());
240 if (type == kTypeRSA) {
241 m_manager.createKeyPairRSA(observer, size, priv_name, pub_name, priv_policy, pub_policy);
242 } else if (type == kTypeECDSA) {
243 CKM::ElipticCurve eliptic = CKM::ElipticCurve::prime192v1;
244 if (args.get("ellipticCurveType").is<std::string>()) {
245 const std::string& eType = args.get("ellipticCurveType").get<std::string>();
246 if (eType == "PRIME256V1") {
247 eliptic = CKM::ElipticCurve::prime256v1;
248 } else if (eType == "EC_SECP384R1") {
249 eliptic = CKM::ElipticCurve::secp384r1;
252 m_manager.createKeyPairECDSA(observer, eliptic, priv_name, pub_name, priv_policy, pub_policy);
254 m_manager.createKeyPairDSA(observer, size, priv_name, pub_name, priv_policy, pub_policy);
260 void KeyManagerInstance::OnCreateKeyPair(double callbackId,
261 const common::PlatformResult& result) {
263 picojson::value::object dict;
264 dict["callbackId"] = picojson::value(callbackId);
265 if (result.IsError()) {
266 LoggerE("There was an error");
267 ReportError(result, &dict);
269 picojson::value res(dict);
270 PostMessage(res.serialize().c_str());
273 std::string RawBufferToBase64(const CKM::RawBuffer &buf) {
277 gchar* base64 = g_base64_encode(&buf[0], buf.size());
284 void KeyManagerInstance::GetKey(const picojson::value& args, picojson::object& out) {
288 const std::string& alias = args.get("name").get<std::string>();
290 if (args.get("password").is<std::string>()) {
291 pass = args.get("password").get<std::string>().c_str();
295 int ret = CKM::Manager::create()->getKey(alias, pass, key);
296 if (ret != CKM_API_SUCCESS) {
297 LoggerE("Failed to get key: %d", ret);
298 if (ret == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
299 ReportError(common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
300 "Key alias not found"), &out);
302 ReportError(common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
303 "Failed to get key"), &out);
306 picojson::object dict;
307 dict["name"] = args.get("name");
308 if (args.get("password").is<std::string>()) {
309 dict["password"] = args.get("password");
311 switch (key->getType()) {
312 case KeyType::KEY_NONE:
313 dict["keyType"] = picojson::value("KEY_NONE");
315 case KeyType::KEY_RSA_PUBLIC:
316 dict["keyType"] = picojson::value("KEY_RSA_PUBLIC");
318 case KeyType::KEY_RSA_PRIVATE:
319 dict["keyType"] = picojson::value("KEY_RSA_PRIVATE");
321 case KeyType::KEY_ECDSA_PUBLIC:
322 dict["keyType"] = picojson::value("KEY_ECDSA_PUBLIC");
324 case KeyType::KEY_ECDSA_PRIVATE:
325 dict["keyType"] = picojson::value("KEY_ECDSA_PRIVATE");
327 case KeyType::KEY_DSA_PUBLIC:
328 dict["keyType"] = picojson::value("KEY_DSA_PUBLIC");
330 case KeyType::KEY_DSA_PRIVATE:
331 dict["keyType"] = picojson::value("KEY_DSA_PRIVATE");
333 case KeyType::KEY_AES:
334 dict["keyType"] = picojson::value("KEY_AES");
337 dict["rawKey"] = picojson::value(RawBufferToBase64(key->getDER()));
338 //if key was retrieved it is extractable from db
339 dict["extractable"] = picojson::value(true);
341 picojson::value res(dict);
342 ReportSuccess(res, out);
346 void KeyManagerInstance::GetCertificate(const picojson::value& args,
347 picojson::object& out) {
351 if (args.get("password").is<std::string>()) {
352 pass = args.get("password").get<std::string>().c_str();
354 const std::string& alias = args.get("name").get<std::string>();
355 CKM::CertificateShPtr cert;
356 int ret = CKM::Manager::create()->getCertificate(alias, pass, cert);
357 if (ret != CKM_API_SUCCESS) {
358 LoggerE("Failed to get cert: %d", ret);
359 if (ret == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
360 ReportError(common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
361 "Cert alias not found"), &out);
363 ReportError(common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
364 "Failed to get cert"), &out);
367 picojson::object dict;
368 dict["name"] = args.get("name");
369 if (args.get("password").is<std::string>()) {
370 dict["password"] = args.get("password");
372 //if cert was retrieved it is extractable from db
373 dict["extractable"] = picojson::value(true);
374 dict["rawCert"] = picojson::value(RawBufferToBase64(cert->getDER()));
376 picojson::value res(dict);
377 ReportSuccess(res, out);
381 void KeyManagerInstance::SaveCertificate(const picojson::value& args,
382 picojson::object& out) {
385 const picojson::value& crt = args.get("certificate");
386 const std::string& alias = crt.get("name").get<std::string>();
387 std::string password;
388 if (crt.get("password").is<std::string>()) {
389 password = crt.get("password").get<std::string>();
391 std::string base64 = args.get("rawCert").get<std::string>();
396 crt.get("extractable").get<bool>(),
397 args.get("callbackId").get<double>());
401 void KeyManagerInstance::SaveCert(std::string &base64,
402 const std::string &password,
403 const std::string &alias,
407 pcrecpp::RE_Options opt;
408 opt.set_multiline(true);
409 //remove first line and last line
410 pcrecpp::RE("-----[^-]*-----", opt).GlobalReplace("", &base64);
411 CKM::RawBuffer rawBuffer = Base64ToRawBuffer(base64);
412 CKM::Password pass(password.c_str());
413 CKM::CertificateShPtr cert = CKM::Certificate::create(rawBuffer,
414 CKM::DataFormat::FORM_DER);
415 CKM::Policy policy(pass, extractable);
416 CKM::ManagerAsync::ObserverPtr observer(new SaveCertObserver(this,
418 m_manager.saveCertificate(observer, alias, cert, policy);
421 void KeyManagerInstance::OnSaveCert(double callbackId,
422 const common::PlatformResult& result) {
424 picojson::value::object dict;
425 dict["callbackId"] = picojson::value(callbackId);
426 if (result.IsError()) {
427 LoggerE("There was an error");
428 ReportError(result, &dict);
430 picojson::value res(dict);
431 PostMessage(res.serialize().c_str());
434 void KeyManagerInstance::LoadCertificateFromFile(const picojson::value& args,
435 picojson::object& out) {
438 const picojson::value& crt = args.get("certificate");
439 const std::string& file = args.get("fileURI").get<std::string>();
440 std::string password;
441 if (crt.get("password").is<std::string>()) {
442 password = crt.get("password").get<std::string>();
444 LoadFileCert* reader = new LoadFileCert(this,
445 args.get("callbackId").get<double>(),
447 crt.get("name").get<std::string>(),
448 crt.get("extractable").get<bool>());
449 reader->LoadFileAsync(file);
454 void KeyManagerInstance::OnCertFileLoaded(LoadFileCert* reader,
455 const common::PlatformResult& result) {
458 if (result.IsError()) {
459 LoggerE("There was an error");
460 picojson::value::object dict;
461 dict["callbackId"] = picojson::value(reader->callback_id);
462 ReportError(result, &dict);
463 picojson::value res(dict);
464 PostMessage(res.serialize().c_str());
466 SaveCert(reader->file_content, reader->password, reader->alias,
467 reader->extractable, reader->callback_id);
472 void KeyManagerInstance::RemoveCertificate(const picojson::value& args,
473 picojson::object& out) {
476 const std::string& alias = args.get("certificate").get("name").get<std::string>();
477 common::PlatformResult res = RemoveAlias(alias);
479 ReportError(res, &out);
481 picojson::value result;
482 ReportSuccess(result, out);
486 void KeyManagerInstance::SaveData(const picojson::value& args,
487 picojson::object& out) {
490 const picojson::value& data = args.get("data");
491 const std::string& alias = data.get("name").get<std::string>();
492 std::string password;
493 if (data.get("password").is<std::string>()) {
494 password = data.get("password").get<std::string>();
496 std::string base64 = args.get("rawData").get<std::string>();
498 CKM::RawBuffer raw_buffer = Base64ToRawBuffer(base64);
499 CKM::Password pass(password.c_str());
500 CKM::Policy policy(pass, data.get("extractable").get<bool>());
501 CKM::ManagerAsync::ObserverPtr observer(new SaveDataObserver(this,
502 args.get("callbackId").get<double>()));
503 m_manager.saveData(observer, alias, raw_buffer, policy);
508 void KeyManagerInstance::OnSaveData(double callbackId,
509 const common::PlatformResult& result) {
512 picojson::value::object dict;
513 dict["callbackId"] = picojson::value(callbackId);
514 if (result.IsError()) {
515 LoggerE("There was an error");
516 ReportError(result, &dict);
518 picojson::value res(dict);
519 PostMessage(res.serialize().c_str());
522 void KeyManagerInstance::RemoveData(const picojson::value& args,
523 picojson::object& out) {
525 const std::string& alias = args.get("data").get("name").get<std::string>();
526 common::PlatformResult res = RemoveAlias(alias);
528 ReportError(res, &out);
534 void KeyManagerInstance::GetData(const picojson::value& args,
535 picojson::object& out) {
538 const std::string& alias = args.get("name").get<std::string>();
540 if (args.get("password").is<std::string>()) {
541 pass = args.get("password").get<std::string>().c_str();
544 CKM::RawBuffer raw_buffer;
545 int ret = CKM::Manager::create()->getData(alias, pass, raw_buffer);
546 if (ret != CKM_API_SUCCESS) {
547 LoggerE("Failed to get data: %d", ret);
548 if (ret == CKM_API_ERROR_DB_ALIAS_UNKNOWN) {
549 ReportError(common::PlatformResult(common::ErrorCode::NOT_FOUND_ERR,
550 "Data alias not found"), &out);
552 ReportError(common::PlatformResult(common::ErrorCode::UNKNOWN_ERR,
553 "Failed to get data"), &out);
556 picojson::object dict;
557 dict["name"] = args.get("name");
558 if (args.get("password").is<std::string>()) {
559 dict["password"] = args.get("password");
561 dict["rawData"] = picojson::value(RawBufferToBase64(raw_buffer));
562 //if key was retrieved it is extractable from db
563 dict["extractable"] = picojson::value(true);
565 picojson::value res(dict);
566 ReportSuccess(res, out);
570 CKM::HashAlgorithm StringToHashAlgorithm(const std::string &hashAlgorithmType) {
572 using CKM::HashAlgorithm;
573 if (hashAlgorithmType == "HASH_SHA1") {
574 return HashAlgorithm::SHA1;
575 } else if (hashAlgorithmType == "HASH_SHA256") {
576 return HashAlgorithm::SHA256;
577 } else if (hashAlgorithmType == "HASH_SHA384") {
578 return HashAlgorithm::SHA384;
579 } else if (hashAlgorithmType == "HASH_SHA512") {
580 return HashAlgorithm::SHA512;
582 return HashAlgorithm::NONE;
585 CKM::RSAPaddingAlgorithm StringToRSAPadding(const std::string &padding) {
587 if (padding == "PADDING_PKCS1") {
588 return CKM::RSAPaddingAlgorithm::PKCS1;
590 return CKM::RSAPaddingAlgorithm::X931;
593 void KeyManagerInstance::CreateSignature(const picojson::value& args,
594 picojson::object& out) {
597 using CKM::HashAlgorithm;
598 using CKM::RSAPaddingAlgorithm;
599 const std::string& alias = args.get("privKeyAlias").get<std::string>();
600 std::string base64 = args.get("message").get<std::string>();
602 if (args.get("password").is<std::string>()) {
603 pass = args.get("password").get<std::string>().c_str();
605 CKM::RawBuffer raw_buffer = Base64ToRawBuffer(base64);
606 HashAlgorithm hash = StringToHashAlgorithm(
607 args.get("hashAlgorithmType").get<std::string>());
608 RSAPaddingAlgorithm alg = StringToRSAPadding(
609 args.get("padding").get<std::string>());
610 CKM::ManagerAsync::ObserverPtr observer(new CreateSignatureObserver(this,
611 args.get("callbackId").get<double>()));
612 m_manager.createSignature(observer, alias, pass, raw_buffer, hash, alg);
617 void KeyManagerInstance::OnCreateSignature(double callbackId,
618 const common::PlatformResult& result, CKM::RawBuffer buffer) {
621 picojson::value::object dict;
622 dict["callbackId"] = picojson::value(callbackId);
623 if (result.IsError()) {
624 LoggerE("There was an error");
625 ReportError(result, &dict);
627 dict["result"] = picojson::value(RawBufferToBase64(buffer));
629 picojson::value res(dict);
630 PostMessage(res.serialize().c_str());
633 void KeyManagerInstance::VerifySignature(const picojson::value& args, picojson::object& out) {
635 using CKM::HashAlgorithm;
636 using CKM::RSAPaddingAlgorithm;
638 const std::string& alias = args.get("pubKeyAlias").get<std::string>();
639 const std::string& message_string = args.get("message").get<std::string>();
640 const std::string& signature_string = args.get("signature").get<std::string>();
642 if (args.get("password").is<std::string>()) {
643 pass = args.get("password").get<std::string>().c_str();
645 CKM::RawBuffer message = Base64ToRawBuffer(message_string);
646 CKM::RawBuffer signature = Base64ToRawBuffer(signature_string);
648 HashAlgorithm hash = StringToHashAlgorithm(
649 args.get("hashAlgorithmType").get<std::string>());
650 RSAPaddingAlgorithm alg = StringToRSAPadding(
651 args.get("padding").get<std::string>());
652 CKM::ManagerAsync::ObserverPtr observer(new VerifySignatureObserver(this,
653 args.get("callbackId").get<double>()));
654 m_manager.verifySignature(observer, alias, pass, message, signature, hash, alg);
659 void KeyManagerInstance::OnVerifySignature(double callbackId,
660 const common::PlatformResult& result) {
663 picojson::value::object dict;
664 dict["callbackId"] = picojson::value(callbackId);
665 if (result.IsError()) {
666 LoggerE("There was an error");
667 ReportError(result, &dict);
669 picojson::value res(dict);
670 PostMessage(res.serialize().c_str());
673 void KeyManagerInstance::LoadFromPKCS12File(const picojson::value& args,
674 picojson::object& out) {
677 const std::string& file = args.get("fileURI").get<std::string>();
678 const std::string& key_alias = args.get("privKeyName").get<std::string>();
679 const std::string& cert_alias = args.get("certificateName").get<std::string>();
680 std::string password;
681 if (args.get("password").is<std::string>()) {
682 password = args.get("password").get<std::string>();
684 LoadFilePKCS12* reader = new LoadFilePKCS12(this,
685 args.get("callbackId").get<double>(),
689 reader->LoadFileAsync(file);
694 void KeyManagerInstance::OnPKCS12FileLoaded(LoadFilePKCS12* reader,
695 const common::PlatformResult& result) {
698 if (result.IsError()) {
699 LoggerE("There was an error");
700 picojson::value::object dict;
701 dict["callbackId"] = picojson::value(reader->callback_id);
702 ReportError(result, &dict);
703 picojson::value res(dict);
704 PostMessage(res.serialize().c_str());
706 CKM::Password pass(reader->password.c_str());
707 CKM::PKCS12ShPtr pkcs = CKM::PKCS12::create(reader->file_content, pass);
709 LoggerE("Failed to parse PKCS12 file");
710 picojson::value::object dict;
711 dict["callbackId"] = picojson::value(reader->callback_id);
712 common::PlatformResult err(common::ErrorCode::INVALID_VALUES_ERR,
713 "Failed to parse PKCS12 file");
714 ReportError(err, &dict);
715 picojson::value res(dict);
716 PostMessage(res.serialize().c_str());
718 CKM::Policy keyPolicy;
719 CKM::Policy certPolicy;
720 CKM::ManagerAsync::ObserverPtr observer(new SavePKCS12Observer(this,
721 reader->callback_id));
722 m_manager.saveCertificate(observer, reader->cert_alias, pkcs->getCertificate(), certPolicy);
723 m_manager.saveKey(observer, reader->key_alias, pkcs->getKey(), keyPolicy);
729 void KeyManagerInstance::OnSavePKCS12(double callbackId,
730 const common::PlatformResult& result) {
733 picojson::value::object dict;
734 dict["callbackId"] = picojson::value(callbackId);
735 if (result.IsError()) {
736 LoggerE("There was an error");
737 ReportError(result, &dict);
739 picojson::value res(dict);
740 PostMessage(res.serialize().c_str());
743 void KeyManagerInstance::AllowAccessControl(const picojson::value& args,
744 picojson::object& out) {
747 const std::string& dataName = args.get("dataName").get<std::string>();
748 const std::string& id = args.get("id").get<std::string>();
749 const std::string& access = args.get("accessControlType").get<std::string>();
750 CKM::PermissionMask mask = CKM::Permission::READ;
751 if (access == "READ_REMOVE") {
752 mask = mask | CKM::Permission::REMOVE;
754 CKM::ManagerAsync::ObserverPtr observer(new AllowAccessObserver(this,
755 args.get("callbackId").get<double>()));
756 m_manager.setPermission(observer, dataName, id, mask);
761 void KeyManagerInstance::OnAllowAccess(double callbackId,
762 const common::PlatformResult& result) {
765 picojson::value::object dict;
766 dict["callbackId"] = picojson::value(callbackId);
767 if (result.IsError()) {
768 LoggerE("There was an error");
769 ReportError(result, &dict);
771 picojson::value res(dict);
772 PostMessage(res.serialize().c_str());
775 void KeyManagerInstance::DenyAccessControl(const picojson::value& args,
776 picojson::object& out) {
779 const std::string& dataName = args.get("dataName").get<std::string>();
780 const std::string& id = args.get("id").get<std::string>();
781 CKM::PermissionMask mask = CKM::Permission::NONE;
782 CKM::ManagerAsync::ObserverPtr observer(new DenyAccessObserver(this,
783 args.get("callbackId").get<double>()));
784 m_manager.setPermission(observer, dataName, id, mask);
789 void KeyManagerInstance::OnDenyAccess(double callbackId,
790 const common::PlatformResult& result) {
793 picojson::value::object dict;
794 dict["callbackId"] = picojson::value(callbackId);
795 if (result.IsError()) {
796 LoggerE("There was an error");
797 ReportError(result, &dict);
799 picojson::value res(dict);
800 PostMessage(res.serialize().c_str());
803 } // namespace keymanager
804 } // namespace extension