Example #1
0
bool doSign(PluginInstance *obj, NPVariant *args, unsigned argCount, NPVariant *result) {
	EstEID_log("obj=%p, name=sign argCount=%u", obj, argCount);

	FAIL_IF_NOT_ALLOWED_SITE;

	if (argCount < 2) {
		browserFunctions->setexception(&obj->header, "Missing arguments");
		return false;
	}
	
	if(argCount > 2 && NPVARIANT_IS_OBJECT(args[2])){
		strncpy(pluginLanguage, getLanguageFromOptions(obj, args[2]), 2);
	}
	EstEID_setLocale(pluginLanguage);

	void* wnd = getNativeWindowHandle(obj);

	EstEID_PINPromptData pinPromptData = {promptForPIN, showAlert, wnd};
	NPUTF8* certId = createStringFromNPVariant(&args[0]);
	NPUTF8* hash = createStringFromNPVariant(&args[1]);
	char *signature = NULL;

#ifdef _WIN32
	DialogData dialogData;
	dialogData.pin2[0] = '\0';
	dialogData.minPin2Length = 5;
	dialogData.certId = certId;
	dialogData.hash = hash;
	dialogData.signature[0] = '\0';

	CK_SLOT_ID slotId;	
	if(EstEID_getSlotId(certId, &slotId)){
		if(EstEID_isPinPad(slotId)) {
			signature = EstEID_sign(certId, hash, pinPromptData);
		}
		else {
			DialogBoxParam(pluginInstance, MAKEINTRESOURCEW(IDD_PIN_DIALOG), (HWND)wnd, Pin2DialogProc, (LPARAM)&dialogData);
			LOG_LOCATION;
			signature = (char*)malloc(SIGNATURE_BUFFER_SIZE); // check?
			strcpy(signature, dialogData.signature);
		}
	}
	else {
		return false;
	}
#else
	signature = EstEID_sign(certId, hash, pinPromptData);
#endif
	LOG_LOCATION
	if (signature) {
		copyStringToNPVariant(signature, result);
		free(signature);
		return true;
	}
	else {
		EstEID_log("EstEID_error=%s", EstEID_error);
		browserFunctions->setexception(&obj->header, EstEID_error);
		return false;
	}
}
bool certGetProperty(CertInstance *obj, NPIdentifier name, NPVariant *variant) {
	NPUTF8* nameString = browserFunctions->utf8fromidentifier(name);
	EstEID_log("name=%s", (char *)nameString);
	const char *result = EstEID_mapGet(obj->certInfo, EstEID_getCertPropertyName(nameString));
	browserFunctions->memfree(nameString);
	if (result) return copyStringToNPVariant(result, variant);
	return false;
}
bool certGetProperty(CertInstance *obj, NPIdentifier name, NPVariant *variant) {
	NPUTF8* nameString = browserFunctions->utf8fromidentifier(name);
	EstEID_log("name=%s", (char *)nameString);
	if(isSameIdentifier(name, "id")){
		return copyStringToNPVariant(EstEID_getCertHash(obj->certificate), variant);
	}
	if(isSameIdentifier(name, "certificateAsHex") || isSameIdentifier(name, "cert")){
		return copyStringToNPVariant(obj->certificateAsHex, variant);
	}
	if(isSameIdentifier(name, "issuerCN")){
		return copyStringToNPVariant(obj->issuerCN, variant);
	}
	if(isSameIdentifier(name, "CN")){
		EstEID_log("CN=%s", obj->CN);
		return copyStringToNPVariant(obj->CN, variant);
	}
	if(isSameIdentifier(name, "validTo")){
		return copyStringToNPVariant(obj->validTo, variant);
	}
	if(isSameIdentifier(name, "validFrom")){
		return copyStringToNPVariant(obj->validFrom, variant);
	}
	if(isSameIdentifier(name, "keyUsage")){
		return copyStringToNPVariant("Non-Repudiation", variant);
	}
	return false;
}
bool pluginGetProperty(PluginInstance *obj, NPIdentifier name, NPVariant *variant) {
	LOG_LOCATION;
	if (isSameIdentifier(name, "version"))
		return copyStringToNPVariant(ESTEID_PLUGIN_VERSION, variant);
	else if (isSameIdentifier(name, "errorMessage")){
		EstEID_log("Reading error message: %s", EstEID_error);
		return copyStringToNPVariant(EstEID_error, variant);
	}
	else if (isSameIdentifier(name, "errorCode")) {
		INT32_TO_NPVARIANT(EstEID_errorCode, *variant);
		EstEID_log("returning errorCode=%i", EstEID_errorCode);
		return true;
	}
	else if (isSameIdentifier(name, "authCert") || isSameIdentifier(name, "signCert")){
		return doGetCertificate(obj, variant);
	}
	else if (isSameIdentifier(name, "pluginLanguage")){
		return copyStringToNPVariant(pluginLanguage, variant);
	}
	EstEID_log("returning false");
	return false;
}
bool doSign(PluginInstance *obj, NPVariant *args, unsigned argCount, NPVariant *result) {
	EstEID_log("obj=%p, name=sign argCount=%u", obj, argCount);

	FAIL_IF_NOT_ALLOWED_SITE;

	if (argCount < 2) {
		browserFunctions->setexception(&obj->header, "Missing arguments");
		return false;
	}
	
	if(argCount > 2 && NPVARIANT_IS_OBJECT(args[2])){
		strncpy(pluginLanguage, getLanguageFromOptions(obj, args[2]), 2);
	}
	EstEID_setLocale(pluginLanguage);

	if(argCount > 3 && NPVARIANT_IS_OBJECT(args[3])){
		strcpy(promptMessage, createStringFromNPVariant(args[3]));
	}

	void* wnd = getNativeWindowHandle(obj);

	EstEID_PINPromptData pinPromptData = {promptForPIN, showAlert, wnd, promptMessage, NULL};
	NPUTF8* certId = createStringFromNPVariant(&args[0]);
	NPUTF8* hash = createStringFromNPVariant(&args[1]);
	char *signature = NULL;

	signature = EstEID_sign(certId, hash, pinPromptData);

	LOG_LOCATION
	if (signature) {
		copyStringToNPVariant(signature, result);
		free(signature);
		return true;
	}
	else {
		EstEID_log("EstEID_error=%s", EstEID_error);
		browserFunctions->setexception(&obj->header, EstEID_error);
		return false;
	}
}