I want to share the experience of integrating the CryptoPro browser plugin into my bike. Who cares welcome to cat. I ask to forgive in advance for the heavy syllable
It so happened that one of the modules of my bike was destined to become the functionality of a digital signature. A quick glance at the supplier’s manual, I set aside for this one day, expecting to do everything in four hours. As a result, everything turned out in the week of coding: (.
Since I haven’t gone deep into the frontend yet, I use javascript as needed, I didn’t attach importance to the asynchronous code for working with the plugin.
Asynchronous code from SDKfunction SignCreate(certSubjectName, dataToSign) { return new Promise(function(resolve, reject){ cadesplugin.async_spawn(function *(args) { try { var oStore = yield cadesplugin.CreateObjectAsync("CAPICOM.Store"); yield oStore.Open(CAPICOM_CURRENT_USER_STORE, CAPICOM_MY_STORE, CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED); var CertificatesObj = yield oStore.Certificates; var oCertificates = yield CertificatesObj.Find( CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, certSubjectName); var Count = yield oCertificates.Count; if (Count == 0) { throw("Certificate not found: " + args[0]); } var oCertificate = yield oCertificates.Item(1); var oSigner = yield cadesplugin.CreateObjectAsync("CAdESCOM.CPSigner"); yield oSigner.propset_Certificate(oCertificate); var oSignedData = yield cadesplugin.CreateObjectAsync("CAdESCOM.CadesSignedData"); yield oSignedData.propset_Content(dataToSign); var sSignedMessage = yield oSignedData.SignCades(oSigner, CADESCOM_CADES_BES); yield oStore.Close(); args[2](sSignedMessage); } catch (e) { args[3]("Failed to create signature. Error: " + GetErrorMessage(err)); } }, certSubjectName, dataToSign, resolve, reject); }); }
Of course, I am not a system coder reading assembly language in hexadecimal codes, but I confess for the first time in my life I could not understand the meaning of this code for three days. It was necessary to raise the docks for promises and generators and delve into them to understand that cadesplugin.async_spawn receives a generator input, which is iterated to done (the front-runners can correct me and I don’t understand it completely).
Moreover, it is not immediately obvious, but working with a javascript plugin should be implemented in four versions:
- Promise Browser Synchronous Plugin
- Browser with promises asynchronous plugin
- Promis-free browser synchronous plugin
- Browser without promises asynchronous plugin (for completeness of the system, this probably does not happen)
How to understand that the plugin is synchronous: cades_plaugin.hasOwnProperty ("CreateObject");
How to understand that the browser with promises: !! window.promise;
In addition, as always, IE requires a separate connection, as I understood for the polyfil of promises (polifila tasted at the same time with promises and generators :)).
Once the conceptual scheme was understood, further coding was a matter of technique.
ps The only annoying circumstance that a signature is created is checked in FireFox, is created, but not checked in Crome. What is not clear, I
posted a forum CryptPro.
Update: pps CryptoPro saved me, I zakosyachil in asynchronous code. Thanks a lot to the support service.
That is generally all. I hope someone will save time and health, because judging by the CryptoPro forum, a lot of people suffer from the same problem.