Created
April 9, 2023 17:44
-
-
Save ediskandarov/077d197637eb3836ad59fe2b9eaa0010 to your computer and use it in GitHub Desktop.
cades plugin by CryptoPro. Signature creation and verification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ru"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Проверки подписи</title> | |
<script language="javascript" src="cadesplugin_api.js"></script> | |
<script> | |
const { cadesplugin } = window; | |
class Certificate { | |
constructor(cert) { | |
this._cert = cert; | |
} | |
async isValid() { | |
const res = await this._cert.IsValid(); | |
return await res.Result; | |
} | |
async getInfo() { | |
return await this._cert.GetInfo( | |
cadesplugin.CAPICOM_CERT_INFO_SUBJECT_SIMPLE_NAME | |
); | |
} | |
async issuerName() { | |
return await this._cert.IssuerName; | |
} | |
async subjectName() { | |
return await this._cert.SubjectName; | |
} | |
} | |
async function getProviderVersion() { | |
var ProviderName = | |
"Crypto-Pro GOST R 34.10-2012 Cryptographic Service Provider"; | |
var ProviderType = 80; | |
try { | |
var oAbout = await cadesplugin.CreateObjectAsync("CAdESCOM.About"); | |
oVersion = await oAbout.CSPVersion( | |
ProviderName, | |
parseInt(ProviderType, 10) | |
); | |
var Minor = await oVersion.MinorVersion; | |
var Major = await oVersion.MajorVersion; | |
var Build = await oVersion.BuildVersion; | |
var Version = await oVersion.toString(); | |
return Version; | |
} catch (er) { | |
throw er; | |
} | |
} | |
async function SignCreate(certSubjectName, dataToSign) { | |
const { | |
CAPICOM_CURRENT_USER_STORE, | |
CAPICOM_MY_STORE, | |
CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED, | |
CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, | |
CADESCOM_CADES_X_LONG_TYPE_1, | |
CAPICOM_CERTIFICATE_FIND_TIME_VALID, | |
} = cadesplugin; | |
var oStore = await cadesplugin.CreateObjectAsync("CAdESCOM.Store"); | |
await oStore.Open( | |
CAPICOM_CURRENT_USER_STORE, | |
CAPICOM_MY_STORE, | |
CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED | |
); | |
var oStoreCerts = await oStore.Certificates; | |
var oCertificates = await oStoreCerts.Find( | |
CAPICOM_CERTIFICATE_FIND_TIME_VALID | |
); | |
var certsCount = await oCertificates.Count; | |
if (certsCount === 0) { | |
const err = new Error("Certificate not found: " + certSubjectName); | |
console.error(err); | |
throw err; | |
} | |
var oCertificate = await oCertificates.Item(1); | |
const crt = new Certificate(oCertificate); | |
console.log("Certificate is valid", await crt.isValid()); | |
console.log("Certificate info", await crt.getInfo()); | |
console.log("Issuer name", await crt.issuerName()); | |
console.log("Subject name", await crt.subjectName()); | |
var oSigner = await cadesplugin.CreateObjectAsync("CAdESCOM.CPSigner"); | |
await oSigner.propset_Certificate(oCertificate); | |
await oSigner.propset_CheckCertificate(true); | |
// await oSigner.propset_TSAAddress("http://cryptopro.ru/tsp/"); | |
await oSigner.propset_TSAAddress( | |
"http://testca.cryptopro.ru/tsp/tsp.srf" | |
); | |
var oSignedData = await cadesplugin.CreateObjectAsync( | |
"CAdESCOM.CadesSignedData" | |
); | |
await oSignedData.propset_Content(dataToSign); | |
try { | |
var sSignedMessage = await oSignedData.SignCades( | |
oSigner, | |
CADESCOM_CADES_X_LONG_TYPE_1 | |
); | |
} catch (e) { | |
console.error(e); | |
throw e; | |
} | |
await oStore.Close(); | |
return sSignedMessage; | |
} | |
async function Verify(sSignedMessage) { | |
const { CADESCOM_CADES_X_LONG_TYPE_1 } = window.cadesplugin; | |
var oSignedData = await cadesplugin.CreateObjectAsync( | |
"CAdESCOM.CadesSignedData" | |
); | |
try { | |
await oSignedData.VerifyCades( | |
sSignedMessage, | |
CADESCOM_CADES_X_LONG_TYPE_1 | |
); | |
} catch (e) { | |
throw e; | |
} | |
return true; | |
} | |
async function getVersion() { | |
await cadesplugin; | |
return await getProviderVersion(); | |
} | |
async function main() { | |
console.log(await getVersion()); | |
const signature = await SignCreate("x", "Hello World!"); | |
console.log(signature); | |
const isValid = await Verify(signature); | |
console.log(isValid); | |
} | |
main().catch(console.error); | |
</script> | |
</head> | |
<body> | |
<p>Hello World!</p> | |
<p id="ProviderName"></p> | |
<p id="ProviderType"></p> | |
<p id="ProviderVersion"></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment