| 5 | |
| 6 | La función sign de la librería hwcripto firma el hash con la clave privada asociada con el certificado. Esta función requiere el certificado, el hash a firmar |
| 7 | |
| 8 | {{{ |
| 9 | //cert -> certificado del firmante |
| 10 | //hash -> hash a firmar con la clave asociada al certificado |
| 11 | //signature -> resultado de la operación (hash firmado) |
| 12 | |
| 13 | window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(function(signature) { |
| 14 | |
| 15 | } |
| 16 | |
| 17 | }}} |
| 18 | |
| 19 | El certificado que requiere la función sign es un objeto y tiene la siguiente forma: |
| 20 | |
| 21 | [[Image(certificado-campos-desplegado-portal-murachi.png)]] |
| 22 | [[BR]] |
| 23 | |
| 24 | Cuyo objeto de certificado es obtenido por la función hwcripto getCertificate. Ahora esta función solo funciona para dispositivo criptográfico, por lo que se intento construir el objeto de certificado con los datos del certificado firmante del .p12, se uso la librería javascript Ramda en la siguiente función: |
| 25 | |
| 26 | {{{ |
| 27 | //certBytesHex -> certificado firmante extraído del .p12 |
| 28 | |
| 29 | certifiedObject() { |
| 30 | |
| 31 | const hexadecimalToUint8Array = string => |
| 32 | new Uint8Array(R.map(byte => parseInt(byte, 16), R.splitEvery(2, string))) |
| 33 | |
| 34 | const uint8ArrayToHexadecimal = array => |
| 35 | R.reduce((string, byte) => string + byte.toString(16), '', array) |
| 36 | const badass = hexadecimalToUint8Array(certBytesHex); |
| 37 | Cert = ({ |
| 38 | "hex":certBytesHex, |
| 39 | "encoded":badass, |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | }}} |
| 44 | |
| 45 | El objeto certificado construido quedo de la siguiente manera: |
| 46 | |
| 47 | [[Image(certificado-(obtenido usando ramda)-campos-desplegados-.png)]] |
| 48 | |
| 49 | Pero no es reconocido por la función hash. al parecer es un objeto especifico construido por la función getCertificate y para probar esto se procedió a realizar al siguiente prueba, cargar tanto el certificado del archivo .p12 usando la librería Forge como el certificado extraído de un dispositivo criptografía usando la función getCerticate de la librería hwcripto, una vez obtenido el objeto de certificado, modificarlo colocando la información del certificado de software e firmar el hash. |
| 50 | |
42 | | |
43 | | //Funciones de conversión |
44 | | function transfCert() { |
45 | | |
46 | | console.log("transfCert"); |
47 | | const hexadecimalToUint8Array = string => |
48 | | new Uint8Array(R.map(byte => parseInt(byte, 16), R.splitEvery(2, string))) |
49 | | |
50 | | const uint8ArrayToHexadecimal = array => |
51 | | R.reduce((string, byte) => string + byte.toString(16), '', array) |
52 | | |
53 | | |
54 | | const badass = hexadecimalToUint8Array(certBytesHex); |
55 | | console.log(badass); |
56 | | P12aSN = badass; |
57 | | |
58 | | /*Cert = ({ |
59 | | "hex":certBytesHex, |
60 | | "encoded":badass, |
61 | | }); */ |
62 | | |
63 | | } |
158 | | //var derkey = forge.util.decode64(certP12bag); |
159 | | //p12Der = forge.util.decode64(certP12bag); |
160 | | // get p12 as ASN.1 object |
161 | | //p12Asn1 = forge.asn1.fromDer(p12Der); |
162 | | asn1Cert = forge.pki.certificateToAsn1(certP12bag); |
163 | | p12Der = forge.asn1.toDer(asn1Cert).getBytes(); |
164 | | //var p12Asn1 = forge.asn1.fromDer(certP12bag,false); |
165 | | console.log("***** Pedro ******"); |
166 | | //console.log(p12Asn1); |
167 | | //console.log("/////// p12Asn1 ////////"); |
168 | | console.log(asn1Cert); |
169 | | console.log("/////// asn1Cert ///////////"); |
170 | | console.log(p12Der); |
171 | | console.log("/////// p12Der /////////"); |
172 | | //console.log(certpem); |
173 | | //console.log("//////// certpem //////////"); |
174 | | //console.log(certP12bag); |
175 | | //console.log("//////// certP12bag //////////"); |
176 | | console.log(certBytesHex); |
177 | | console.log("****** Pedro *****"); |
178 | | transfCert(); |
179 | | window.hwcrypto.getCertificate({lang: "en"}).then(function(response) { |
180 | | certT = response; |
181 | | console.log("///// certT /////"); |
182 | | console.log(certT); |
183 | | console.log("///// certT /////"); |
184 | | certT.hex = certBytesHex; |
185 | | certT.encoded = P12aSN; |
186 | | console.log(certT); |
187 | | }) |
279 | | console.log(certT); |
280 | | */ |
281 | | |
282 | | var hashtype = "SHA-256"; |
283 | | var lang = "eng"; |
284 | | console.log("antes del window.hwcrypto.sign"); |
285 | | //window.hwcrypto.sign(certT, {type: hashtype, hex: hash}, {lang: lang}); |
286 | | window.hwcrypto.sign(certT, {type: hashtype, hex: hash}, {lang: lang}).then(function(signature) { |
287 | | console.log("entro......."); |
288 | | |
| 268 | var hashtype = "SHA-256"; |
| 269 | var lang = "eng"; |
| 270 | //función que firma el hash y le paso el objeto firmante modificado con la información del .p12 |
| 271 | window.hwcrypto.sign(certT, {type: hashtype, hex: hash}, {lang: lang}).then(function(signature) { |
| 272 | |