namespace extension {
namespace keymanager {
+namespace {
+const char* kTypeRSA = "RSA";
+const char* kTypeECDSA = "ECDSA";
+}
KeyManagerInstance::KeyManagerInstance() {
using std::placeholders::_1;
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() {
}
}
+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
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
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 {
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