1 | <!DOCTYPE html> |
---|
2 | <!-- release v4.1.8, copyright 2014 - 2015 Kartik Visweswaran --> |
---|
3 | <html lang="es"> |
---|
4 | <head> <!--Inicio del head --> |
---|
5 | <meta charset="UTF-8"/> |
---|
6 | <script type="text/javascript" src="js/jquery.min.js"></script> |
---|
7 | <script type="text/javascript" src="js/hwcrypto-legacy.js"></script> |
---|
8 | <script type="text/javascript" src="js/hwcrypto.js"></script> |
---|
9 | <script type="text/javascript" src="js/hex2base.js"></script> |
---|
10 | <script type="text/javascript" src="js/forge.min.js"></script> |
---|
11 | <script type="text/javascript" src="js/ajaxSetup.js"></script> |
---|
12 | |
---|
13 | |
---|
14 | <script type="text/javascript" charset="utf-8"> |
---|
15 | $(document).ready(function() { |
---|
16 | $.ajaxSetup({ |
---|
17 | error:function(x,e){ |
---|
18 | if(x.status==0){ |
---|
19 | alert('Está fuera de línea!!\n Por favor chequee su conexión.'); |
---|
20 | }else if(x.status==404){ |
---|
21 | alert('La URL solicitada no fue encontrada.'); |
---|
22 | }else if(x.status==500){ |
---|
23 | alert('Error interno de servidor.'); |
---|
24 | }else if(e=='parsererror'){ |
---|
25 | alert('Error.\nSolicitud de interpretación de cadena JSON inválida.'); |
---|
26 | }else if(e=='timeout'){ |
---|
27 | alert('Tiempo de espera alcanzado.'); |
---|
28 | }else { |
---|
29 | alert('Error desconocido.\n'+x.responseText); |
---|
30 | } |
---|
31 | } |
---|
32 | }); |
---|
33 | $("#botonEnvia").click(enviaFormulario); |
---|
34 | $("#botonCertificado").click(Sign1b); |
---|
35 | $("#botonCertificadoTarjeta").click(obtieneCertificado); |
---|
36 | $("#botonCertificadoTarjetaProcesa").click(colocaCertificado); |
---|
37 | $("#botonCertificadoProcesa").click(colocaCertificado2); |
---|
38 | $("#botonCertificadoFirma").click(procesaFirma2); |
---|
39 | $("#botonColocaFirma2").click(colocaFirma2); |
---|
40 | $("#botonDescargaArchivo").click(descargaArchivo); |
---|
41 | descargarArchivo = downQuery; |
---|
42 | |
---|
43 | } ); |
---|
44 | var descargarArchivo; |
---|
45 | function ejecutaDescarga(){ |
---|
46 | var opciones = new Object(); |
---|
47 | opciones.onCompletado = solicitudCompletada; |
---|
48 | opciones.onSend = antesEnvioSolicitud; |
---|
49 | opciones.onExito = exitoSolicitud; |
---|
50 | opciones.onProgreso = muestraProgreso; |
---|
51 | opciones.tipoResultado="binary"; |
---|
52 | opciones.id="1"; |
---|
53 | descargarArchivo.dArchivo($(this).attr("linkDescarga"),opciones); |
---|
54 | } |
---|
55 | |
---|
56 | function muestraProgreso(evt,xhr){ |
---|
57 | //$("#progresoDescarga").html(xhr.opciones.url); |
---|
58 | $("#progresoDescarga").html(evt.lengthComputable+"<br>"); |
---|
59 | if (evt.lengthComputable) { |
---|
60 | var percentComplete = evt.loaded / evt.total; |
---|
61 | $("#progresoDescarga").html(percentComplete.toFixed(2).replace(".",",")+" %"); |
---|
62 | }else{ |
---|
63 | $("#progresoDescarga").html("Progreso solo<br>"); |
---|
64 | } |
---|
65 | } |
---|
66 | function errorSolicitud(xhr, ajaxOptions, errorArrojado) { |
---|
67 | $("#progresoDescarga").append("Error"); |
---|
68 | $("#progresoDescarga").append("<br>"); |
---|
69 | $("#progresoDescarga").append(xhr.responseText); |
---|
70 | $("#progresoDescarga").append("<br>"); |
---|
71 | $("#progresoDescarga").append(errorArrojado); |
---|
72 | $("#progresoDescarga").append("<br>"); |
---|
73 | |
---|
74 | } |
---|
75 | function antesEnvioSolicitud(evt,opciones) { |
---|
76 | console.log("antesEnvioSolicitud") |
---|
77 | console.log(opciones.id); |
---|
78 | $("#progresoDescarga").html("Iniciando descarga"); |
---|
79 | //$("#progresoDescarga").html("antesEnvioSolicitud") |
---|
80 | } |
---|
81 | function solicitudCompletada(evt,state) { |
---|
82 | console.log("solicitudCompletada"); |
---|
83 | console.log(evt.opciones.id) |
---|
84 | $("#progresoDescarga").html(""); |
---|
85 | //$("#progresoDescarga").html("solicitudCompletada"); |
---|
86 | } |
---|
87 | function exitoSolicitud(data, status, xhr) { |
---|
88 | if(data){ |
---|
89 | //$("#progresoDescarga"+"B").append("Objetivo: " + xhr.opciones.url + "<br>"); |
---|
90 | var nombreArchivo = descargarArchivo.obtieneNombre(xhr); |
---|
91 | //$("#progresoDescarga"+"B").append("Archivo: " + nombreArchivo + "<br>"); |
---|
92 | var downloadUrl = URL.createObjectURL(data); |
---|
93 | if(xhr.opciones.id=="1"){ |
---|
94 | var a = document.createElement("a"); |
---|
95 | a.href = downloadUrl; |
---|
96 | a.download = nombreArchivo; |
---|
97 | document.body.appendChild(a); |
---|
98 | a.click(); |
---|
99 | }/*else{ |
---|
100 | var a = document.createElement("img"); |
---|
101 | a.src = downloadUrl; |
---|
102 | document.body.appendChild(a); |
---|
103 | }*/ |
---|
104 | } |
---|
105 | $("#progresoDescarga"+"B").append("exitoSolicitud 2<br>"); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | function arrayBufferToString( buffer ) { |
---|
117 | var binary = ''; |
---|
118 | var bytes = new Uint8Array( buffer ); |
---|
119 | var len = bytes.byteLength; |
---|
120 | for (var i = 0; i < len; i++) |
---|
121 | { |
---|
122 | binary += String.fromCharCode( bytes[ i ] ); |
---|
123 | } |
---|
124 | return binary; |
---|
125 | } |
---|
126 | |
---|
127 | function isHex(input) |
---|
128 | { |
---|
129 | if (input.length == 0) { return false; } |
---|
130 | |
---|
131 | for (var i=0; i<input.length; i++) |
---|
132 | { |
---|
133 | if ( !( (input.charAt(0) >= "0" && input.charAt(0) <= "9") || (input.charAt(0) >= "a" && input.charAt(0) <= "f") || (input.charAt(0) >= "A" && input.charAt(0) <= "F") ) ) { return false; } |
---|
134 | } |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | function convertPEMtoHex(valor) |
---|
139 | { |
---|
140 | notclicked=false; |
---|
141 | var input = valor; |
---|
142 | var buffer = ""; |
---|
143 | var hex = 0; |
---|
144 | var output = ""; |
---|
145 | for (var i=0; i<input.length; i++) |
---|
146 | { |
---|
147 | if (isHex(input.charAt(i))) { buffer += input.charAt(i); } |
---|
148 | |
---|
149 | if (buffer.length == 2) |
---|
150 | { |
---|
151 | hex = parseInt(buffer,16); |
---|
152 | output += String.fromCharCode(hex); |
---|
153 | buffer = ""; |
---|
154 | } |
---|
155 | } |
---|
156 | output = btoa(output).split(/(.{75})/).join("\n").replace(/\n+/g,"\n").trim(); |
---|
157 | return output; |
---|
158 | } |
---|
159 | |
---|
160 | var base64chr = new Array( |
---|
161 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
162 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
163 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
---|
164 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
---|
165 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
---|
166 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
---|
167 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
---|
168 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1); |
---|
169 | |
---|
170 | function base64decode(str) { |
---|
171 | var myhex = "0123456789ABCDEF"; |
---|
172 | var pos; |
---|
173 | var c1, c2, c3, c4; |
---|
174 | var i, len, out; |
---|
175 | len = str.length; |
---|
176 | i = 0; |
---|
177 | out = ""; |
---|
178 | while(i < len) { |
---|
179 | /* c1 */ |
---|
180 | do { |
---|
181 | c1 = base64chr[str.charCodeAt(i++) & 0xff]; |
---|
182 | } while(i < len && c1 == -1); |
---|
183 | if(c1 == -1){ break; } |
---|
184 | |
---|
185 | /* c2 */ |
---|
186 | do { |
---|
187 | c2 = base64chr[str.charCodeAt(i++) & 0xff]; |
---|
188 | } while(i < len && c2 == -1); |
---|
189 | if(c2 == -1){ break; } |
---|
190 | //out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); |
---|
191 | pos = (c1 << 2) | ((c2 & 0x30) >> 4); |
---|
192 | out += myhex.charAt(Math.floor(pos/16)) + myhex.charAt(pos%16); |
---|
193 | |
---|
194 | /* c3 */ |
---|
195 | do { |
---|
196 | c3 = str.charCodeAt(i++) & 0xff; |
---|
197 | if(c3 == 61) { return out; } |
---|
198 | c3 = base64chr[c3]; |
---|
199 | } while(i < len && c3 == -1); |
---|
200 | if(c3 == -1) { break; } |
---|
201 | //out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); |
---|
202 | pos = ((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2); |
---|
203 | out += myhex.charAt(Math.floor(pos/16)) + myhex.charAt(pos%16); |
---|
204 | |
---|
205 | /* c4 */ |
---|
206 | do { |
---|
207 | c4 = str.charCodeAt(i++) & 0xff; |
---|
208 | if(c4 == 61) { return out; } |
---|
209 | c4 = base64chr[c4]; |
---|
210 | } while(i < len && c4 == -1); |
---|
211 | if(c4 == -1) { break; } |
---|
212 | //out += String.fromCharCode(((c3 & 0x03) << 6) | c4); |
---|
213 | pos = ((c3 & 0x03) << 6) | c4; |
---|
214 | out += myhex.charAt(Math.floor(pos/16)) + myhex.charAt(pos%16); |
---|
215 | } |
---|
216 | return out; |
---|
217 | } |
---|
218 | |
---|
219 | function unconvertPEMtoHex(valor) |
---|
220 | { |
---|
221 | var input = valor; |
---|
222 | var output = base64decode(input); |
---|
223 | return output; |
---|
224 | } |
---|
225 | |
---|
226 | function Sign1(){ |
---|
227 | var formData = new FormData(); |
---|
228 | formData.append("upload", $("#file-sign")[0].files[0]); |
---|
229 | $.ajax({ |
---|
230 | url: "https://cert.uanesi.lan/pruebaCRTHEX.php", |
---|
231 | type: "post", |
---|
232 | dataType: "json", |
---|
233 | data: formData, |
---|
234 | xhr: function() { |
---|
235 | var myXhr = $.ajaxSettings.xhr(); |
---|
236 | if(myXhr.upload){ |
---|
237 | myXhr.upload.addEventListener('progress',progress, false); |
---|
238 | } |
---|
239 | return myXhr; |
---|
240 | }, |
---|
241 | cache: false, |
---|
242 | contentType: false, |
---|
243 | processData: false, |
---|
244 | crossDomain:true, |
---|
245 | //headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
246 | success: function(response) { |
---|
247 | $("#respuesta").html(""); |
---|
248 | for(var key in response) { |
---|
249 | //$("#respuesta").html($("#respuesta").html()+"<br>"+response[key]) |
---|
250 | } |
---|
251 | //console.log(response["cert"]); |
---|
252 | console.log(response["str"]["certHEX"]) |
---|
253 | certificadoActivo = response["str"]["certHEX"]; |
---|
254 | //fileIdActivo = response["fileId"]; |
---|
255 | //incluyeArchivo(fileIdActivo); |
---|
256 | //console.log(response) |
---|
257 | |
---|
258 | }, |
---|
259 | error: function(jqXHR, textStatus, errorThrown){ |
---|
260 | console.log('error: ' + textStatus); |
---|
261 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
262 | console.log('ajax error function: ' + jqXHR.responseText); |
---|
263 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
264 | } |
---|
265 | }) |
---|
266 | } |
---|
267 | function Sign1b(){ |
---|
268 | var formData = new FormData(); |
---|
269 | formData.append("upload", $("#file-sign")[0].files[0]); |
---|
270 | $.ajax({ |
---|
271 | url: "https://cert.uanesi.lan/pruebaCRTHEX.php", |
---|
272 | type: "post", |
---|
273 | dataType: "json", |
---|
274 | data: formData, |
---|
275 | xhr: function() { |
---|
276 | var myXhr = $.ajaxSettings.xhr(); |
---|
277 | if(myXhr.upload){ |
---|
278 | myXhr.upload.addEventListener('progress',progress, false); |
---|
279 | } |
---|
280 | return myXhr; |
---|
281 | }, |
---|
282 | cache: false, |
---|
283 | contentType: false, |
---|
284 | processData: false, |
---|
285 | crossDomain:true, |
---|
286 | //headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
287 | success: function(response) { |
---|
288 | $("#respuesta").html(""); |
---|
289 | for(var key in response) { |
---|
290 | //$("#respuesta").html($("#respuesta").html()+"<br>"+response[key]) |
---|
291 | } |
---|
292 | console.log(response["str"]); |
---|
293 | console.log(unconvertPEMtoHex(response["str"]["cert1"])) |
---|
294 | certificadoActivo = unconvertPEMtoHex(response["str"]["cert1"]); |
---|
295 | //certificadoActivo ='308208C6308206AEA00302010202024284300D06092A864886F70D01010B05003081F5310B30090603550406130256453110300E060355040813074D6972616E6461310F300D0603550407130642617275746131363034060355040A132D53697374656D61204E6163696F6E616C2064652043657274696669636163696F6E20456C656374726F6E696361312A3028060355040B132146756E646163696F6E20496E7374697475746F20646520496E67656E6965726961313A303806035504031331505343205075626C69636F2064656C204D707043544949207061726120656C2045737461646F2056656E657A6F6C616E6F3123302106092A864886F70D010901161461646D696E2D706B69406669692E676F622E7665301E170D3137303432303130313632355A170D3138303432303130313632355A3081CB310B30090603550406130256453110300E060355040A13074E61747572616C3110300E060355040B13074E61747572616C310F300D060355040813064D6572696461311330110603550407130A4C696265727461646F72313330310603550403132A546168656C20536567756E646F204A6F736520416E746F6E696F20526F6D65726F20436173616E6F76613125302306092A864886F70D0109011616746168656C2E726F6D65726F40676D61696C2E636F6D311630140603550405130D43492D562D313034343131343230820122300D06092A864886F70D01010105000382010F003082010A0282010100B90A80BF21B29D17E4FD2F91E262A25D19D777BBFC85588A03DA5EC13D77318B8E831C871A1AD2E46C2A3A3C11C77D37FEB6DF5F269939697D2025D8502F4A95F8AA2CC7497E341261762DA38B08964A7524F2753C37B7489E029D164D6D0FC6BA73C5BB29F48451309ED035B587C4F02027B8FB9B393953C56888385BCCCB230DF3836ABEFFA01F86F906E3F7DB05DF27C945D8BD98579BBF4FF548D5F47C58533736D5AD68712ECC1ECC7EB740FA6F3A5A350ECE5A7A10AA0187281467AB406EB2E96DE49BF67D4A13D04DC6CE9D37AB1A9EFC171C57F9D96E50BC281B656CFC8A442D98C5FF13660A1FDEA86301AE8396E6D9E0A770D6CD80E8F10FB1499F0203010001A38203863082038230090603551D1304023000300B0603551D0F0404030205E030290603551D250422302006082B0601050507030206082B06010505070304060A2B060104018237140202301D0603551D0E041604148BA67D1E3059255A1F78D9752F4D7A668BEC1628308201500603551D2304820147308201438014AA94985B01C817185028B5F6E15FFBFC895E69AEA1820126A48201223082011E313E303C060355040313354175746F72696461642064652043657274696669636163696F6E205261697A2064656C2045737461646F2056656E657A6F6C616E6F310B30090603550406130256453110300E06035504071307436172616361733119301706035504081310446973747269746F204361706974616C31363034060355040A132D53697374656D61204E6163696F6E616C2064652043657274696669636163696F6E20456C656374726F6E69636131433041060355040B133A5375706572696E74656E64656E63696120646520536572766963696F732064652043657274696669636163696F6E20456C656374726F6E6963613125302306092A864886F70D010901161661637261697A4073757363657274652E676F622E766582010D304606082B06010505070101043A3038303606082B06010505073001862A687474703A2F2F7075626C696361646F722D7073632E6669692E676F622E76653A323536302F6F6373703081820603551D20047B30793008060660865E0B0101306D060660865E0B01023063303006082B060105050702011624687474703A2F2F7075626C696361646F722D7073632E6669692E676F622E76652F647063302F06082B060105050702011623687474703A2F2F7075626C696361646F722D7073632E6669692E676F622E76652F70633081800603551D1104793077A018060560865E0202A00F0C0D43492D562D3130343431313432A018060560865E0204A00F0C0D6E61747572616C4F7468657273A01F060560865E0205A0160C14687474703A2F2F61722E6669692E676F622E7665A020060560865E0206A0170C15687474703A2F2F7777772E6669692E676F622E766530320603551D12042B3029820A6669692E676F622E7665A01B060560865E0208A0120C105249462D472D32303030343635302D3330460603551D1F043F303D303BA039A037863568747470733A2F2F7075626C696361646F722D7073632E6669692E676F622E76652F63726C7368613235362F636163726C2E63726C300D06092A864886F70D01010B050003820201003449AE4D31F07CDD7BC12CC9F7397A91CC16391BCB8457F5BF1A58205C69B7DCE14DE7EB11285D9BF41848837E4BA05002D6B5E40F0864747EDA924ABCB29D5204B7B0208D9BB31143B901745AE1747FB2E8CE6DF9968E24028977B9032165708A6B1FFDD5CBAB899B41D618A635F22FC805BEEAED8BA038734620021609F839AC944B6F0C0944EB7F69EA3D0A16152CC141C609A633653AEA8A08DB6605571BAF812F8F7C67EA65C255FE8DC2C95FB583A34244F4E2D11B53691ADCF9AED7A41EA1CD62A52AA686910521A421023040FC1AF9074534015EF3521A748F11C5C48832F7F044B8DBB083A47C7BEAC83E17ACF2FE98AA68984DF90565C9EFAA27007AD1FB23E1EC55405960FFD4E20AAFC1B355973D38F17D0E935BA763A09B7D7F3E007A9F74DA6F39FB50C918C57D97B28F7B731665767CD6EDFB7AD019CC574E4D979E60F90751C1EEE83096EF44D16254B6B234C7A1912571404276605B732AE44E866695535195EDEB90A128CFE5A4E8FF61099932F54D16E61DD3D0DD5A85AE5B108C5B2DC6A1D87CB63065D34470D3967EEFCDA26586BEC2EE06A92B94B116ABBB8D1899969E8F6B5F80C19DBC95B0DAA89944B8C1CE8A850C4839FE3B0F84323E7769F18B5AE823D0446FBCF0CBF21237F4788526BA078F1332054C869839BAD72395DBAF8552AB719267F66E5AD6023886928DEA0A08188B39CD8D8CE4'; |
---|
296 | |
---|
297 | //fileIdActivo = response["fileId"]; |
---|
298 | //incluyeArchivo(fileIdActivo); |
---|
299 | //console.log(response) |
---|
300 | |
---|
301 | }, |
---|
302 | error: function(jqXHR, textStatus, errorThrown){ |
---|
303 | console.log('error: ' + textStatus); |
---|
304 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
305 | console.log('ajax error function: ' + jqXHR.responseText); |
---|
306 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
307 | } |
---|
308 | }) |
---|
309 | } |
---|
310 | function Sign1c() |
---|
311 | { |
---|
312 | var formData = new FormData(); |
---|
313 | formData.append("upload", $("#file-sign")[0].files[0]); |
---|
314 | $.ajax({ |
---|
315 | url: "https://cert.uanesi.lan/pruebaCRT64.php", |
---|
316 | type: "post", |
---|
317 | dataType: "json", |
---|
318 | data: formData, |
---|
319 | xhr: function() { |
---|
320 | var myXhr = $.ajaxSettings.xhr(); |
---|
321 | if(myXhr.upload){ |
---|
322 | myXhr.upload.addEventListener('progress',progress, false); |
---|
323 | } |
---|
324 | return myXhr; |
---|
325 | }, |
---|
326 | cache: false, |
---|
327 | contentType: false, |
---|
328 | processData: false, |
---|
329 | crossDomain:true, |
---|
330 | //headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
331 | success: function(response) { |
---|
332 | var p12b64 = response["b64"]; |
---|
333 | console.log(p12b64); |
---|
334 | var password = $('#pfxp').val(); |
---|
335 | // decode p12 from base64 |
---|
336 | var p12Der = forge.util.decode64(p12b64); |
---|
337 | // get p12 as ASN.1 object |
---|
338 | var p12Asn1 = forge.asn1.fromDer(p12Der); |
---|
339 | // decrypt p12 using the password 'password' |
---|
340 | //var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password); |
---|
341 | // decrypt p12 using non-strict parsing mode (resolves some ASN.1 parse errors) |
---|
342 | var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password); |
---|
343 | // decrypt p12 using literally no password (eg: Mac OS X/apple push) |
---|
344 | //var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1); |
---|
345 | // decrypt p12 using an "empty" password (eg: OpenSSL with no password input) |
---|
346 | //var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, ''); |
---|
347 | // p12.safeContents is an array of safe contents, each of |
---|
348 | // which contains an array of safeBags |
---|
349 | |
---|
350 | // get bags by friendlyName |
---|
351 | var bags = p12.getBags({algorithm: '3des'}); |
---|
352 | // bags are key'd by attribute type (here "friendlyName") |
---|
353 | // and the key values are an array of matching objects |
---|
354 | var cert = bags.friendlyName[0]; |
---|
355 | |
---|
356 | // get bags by localKeyId |
---|
357 | var bags = p12.getBags({localKeyId: buffer}); |
---|
358 | // bags are key'd by attribute type (here "localKeyId") |
---|
359 | // and the key values are an array of matching objects |
---|
360 | var cert = bags.localKeyId[0]; |
---|
361 | |
---|
362 | // get bags by localKeyId (input in hex) |
---|
363 | var bags = p12.getBags({localKeyIdHex: '7b59377ff142d0be4565e9ac3d396c01401cd879'}); |
---|
364 | // bags are key'd by attribute type (here "localKeyId", *not* "localKeyIdHex") |
---|
365 | // and the key values are an array of matching objects |
---|
366 | var cert = bags.localKeyId[0]; |
---|
367 | |
---|
368 | // get bags by type |
---|
369 | var bags = p12.getBags({bagType: forge.pki.oids.certBag}); |
---|
370 | // bags are key'd by bagType and each bagType key's value |
---|
371 | // is an array of matches (in this case, certificate objects) |
---|
372 | var cert = bags[forge.pki.oids.certBag][0]; |
---|
373 | |
---|
374 | // get bags by friendlyName and filter on bag type |
---|
375 | var bags = p12.getBags({ |
---|
376 | friendlyName: 'test', |
---|
377 | bagType: forge.pki.oids.certBag |
---|
378 | }); |
---|
379 | |
---|
380 | // get key bags |
---|
381 | var bags = p12.getBags({bagType: forge.pki.oids.keyBag}); |
---|
382 | // get key |
---|
383 | var bag = bags[forge.pki.oids.keyBag][0]; |
---|
384 | var key = bag.key; |
---|
385 | // if the key is in a format unrecognized by forge then |
---|
386 | // bag.key will be `null`, use bag.asn1 to get the ASN.1 |
---|
387 | // representation of the key |
---|
388 | if(bag.key === null) { |
---|
389 | var keyAsn1 = bag.asn1; |
---|
390 | // can now convert back to DER/PEM/etc for export |
---|
391 | } |
---|
392 | |
---|
393 | // generate a p12 using AES (default) |
---|
394 | var p12Asn1 = forge.pkcs12.toPkcs12Asn1( |
---|
395 | privateKey, certificateChain, 'password'); |
---|
396 | |
---|
397 | // generate a p12 that can be imported by Chrome/Firefox/iOS |
---|
398 | // (requires the use of Triple DES instead of AES) |
---|
399 | var p12Asn1 = forge.pkcs12.toPkcs12Asn1( |
---|
400 | privateKey, certificateChain, 'password', |
---|
401 | {algorithm: '3des'}); |
---|
402 | |
---|
403 | // base64-encode p12 |
---|
404 | var p12Der = forge.asn1.toDer(p12Asn1).getBytes(); |
---|
405 | var p12b64 = forge.util.encode64(p12Der); |
---|
406 | |
---|
407 | // create download link for p12 |
---|
408 | var a = document.createElement('a'); |
---|
409 | a.download = 'example.p12'; |
---|
410 | a.setAttribute('href', 'data:application/x-pkcs12;base64,' + p12b64); |
---|
411 | a.appendChild(document.createTextNode('Download')); |
---|
412 | |
---|
413 | }, |
---|
414 | error: function(jqXHR, textStatus, errorThrown){ |
---|
415 | console.log('error: ' + textStatus); |
---|
416 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
417 | console.log('ajax error function: ' + jqXHR.responseText); |
---|
418 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
419 | } |
---|
420 | }) |
---|
421 | } |
---|
422 | function Sign1a(){ |
---|
423 | // Get PFX |
---|
424 | //var fileInput = document.getElementById('pfx'); |
---|
425 | var file = $("#file-sign")[0].files[0]; |
---|
426 | |
---|
427 | // Read it |
---|
428 | var reader = new FileReader(); |
---|
429 | reader.onload = function(e) |
---|
430 | { |
---|
431 | console.log("Carga archivo"); |
---|
432 | var contents = e.target.result; |
---|
433 | |
---|
434 | var pkcs12Der = arrayBufferToString(contents); |
---|
435 | var pkcs12B64 = forge.util.encode64(pkcs12Der); |
---|
436 | var privateKey; |
---|
437 | var pkcs12Asn1 = forge.asn1.fromDer(pkcs12Der); |
---|
438 | var password = $('#pfxp').val(); |
---|
439 | console.log("Using certificate:\n" + pkcs12Asn1); |
---|
440 | |
---|
441 | var pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, password); |
---|
442 | // load keys |
---|
443 | for(var sci = 0; sci < pkcs12.safeContents.length; ++sci) |
---|
444 | { |
---|
445 | console.log(sci) |
---|
446 | var safeContents = pkcs12.safeContents[sci]; |
---|
447 | for(var sbi = 0; sbi < safeContents.safeBags.length; ++sbi) |
---|
448 | { |
---|
449 | var safeBag = safeContents.safeBags[sbi]; |
---|
450 | console.log(safeBag) |
---|
451 | if(safeBag.type === forge.pki.oids.keyBag) |
---|
452 | { |
---|
453 | //Found plain private key |
---|
454 | privateKey = safeBag.key; |
---|
455 | console.log("1\n"+privateKey); |
---|
456 | } |
---|
457 | else |
---|
458 | if(safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag) |
---|
459 | { |
---|
460 | // found encrypted private key |
---|
461 | privateKey = safeBag.key; |
---|
462 | console.log("2\n"+privateKey); |
---|
463 | } |
---|
464 | else |
---|
465 | if(safeBag.type === forge.pki.oids.certBag) |
---|
466 | { |
---|
467 | // this bag has a certificate... |
---|
468 | cert = safeBag.cert; |
---|
469 | console.log("3\n"+cert); |
---|
470 | } |
---|
471 | else{ |
---|
472 | console.log("No se puede procesar") |
---|
473 | } |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | /*importCryptoKeyPkcs8(privateKey,true).then(function(cryptoKey) |
---|
478 | { |
---|
479 | // Signed! |
---|
480 | |
---|
481 | // Empty stuff |
---|
482 | var ser = forge.util.encode64($('#form1').serialize()); |
---|
483 | var digestToSignBuf = stringToArrayBuffer(ser); |
---|
484 | $('#pfxd').val(ser); |
---|
485 | var pem = forge.pki.certificateToPem(cert); |
---|
486 | $('#pfxc').val(forge.util.encode64(pem)); |
---|
487 | |
---|
488 | crypto.subtle.sign( |
---|
489 | {name: "RSASSA-PKCS1-v1_5"}, |
---|
490 | cryptoKey, |
---|
491 | digestToSignBuf) |
---|
492 | .then(function(signature){ |
---|
493 | sign = arrayBufferToString(signature); |
---|
494 | signatureB64 = forge.util.encode64(sign); |
---|
495 | $('#pfxs').val(signatureB64); |
---|
496 | |
---|
497 | |
---|
498 | |
---|
499 | }); |
---|
500 | |
---|
501 | });*/ |
---|
502 | |
---|
503 | } |
---|
504 | |
---|
505 | reader.readAsArrayBuffer(file); |
---|
506 | } |
---|
507 | |
---|
508 | function logGetCertificate() { |
---|
509 | |
---|
510 | console.log("logGetCertificate()"); |
---|
511 | // Clear log |
---|
512 | document.getElementById('log').innerHTML = ''; |
---|
513 | // Timestamp |
---|
514 | log_text("sign() clicked on " + new Date().toUTCString()); |
---|
515 | |
---|
516 | // Select hash |
---|
517 | //var hashtype = $("input[name=hash]:checked").val(); |
---|
518 | var hashtype = "SHA-256"; |
---|
519 | |
---|
520 | // Set backend if asked |
---|
521 | //var backend = $("input[name=backend]:checked").val() |
---|
522 | var backend = "autodetect"; |
---|
523 | |
---|
524 | // get language |
---|
525 | var lang = "es"; |
---|
526 | if (!window.hwcrypto.use(backend)) { |
---|
527 | log_text("Selecting backend failed."); |
---|
528 | } |
---|
529 | |
---|
530 | var hash = $("#hashvalue").val(); |
---|
531 | log_text("Signing " + hashtype + ": " + hash); |
---|
532 | // debug |
---|
533 | window.hwcrypto.debug().then(function(response) { |
---|
534 | log_text("Debug: " + response); |
---|
535 | }, function(err) { |
---|
536 | log_text("debug() failed: " + err); |
---|
537 | return; |
---|
538 | }); |
---|
539 | // Sign |
---|
540 | window.hwcrypto.getCertificate({lang: lang}).then(function(response) { |
---|
541 | var cert = response; |
---|
542 | log_text("Using certificate:\n" + hexToPem(response.hex)); |
---|
543 | }); |
---|
544 | } |
---|
545 | |
---|
546 | |
---|
547 | function getCertificate() { |
---|
548 | |
---|
549 | console.log("getCertificate()"); |
---|
550 | // Clear log |
---|
551 | // Timestamp |
---|
552 | console.log("sign() clicked on " + new Date().toUTCString()); |
---|
553 | |
---|
554 | // Set backend if asked |
---|
555 | //var backend = $("input[name=backend]:checked").val() |
---|
556 | var backend = "autodetect"; |
---|
557 | |
---|
558 | // get language |
---|
559 | var lang = "es"; |
---|
560 | if (!window.hwcrypto.use(backend)) { |
---|
561 | console.log("Selecting backend failed."); |
---|
562 | } |
---|
563 | window.hwcrypto.getCertificate({lang:"en"}).then(function(response) { |
---|
564 | var cert = response; |
---|
565 | console.log("Using certificate:\n" + hexToPem(response.hex)); |
---|
566 | return cert; |
---|
567 | }); |
---|
568 | } |
---|
569 | var certificadoActivo; |
---|
570 | function obtieneCertificado(){ |
---|
571 | window.hwcrypto.getCertificate({lang: "en"}).then(function(response) { |
---|
572 | var cert = response; |
---|
573 | certificadoActivo = cert; |
---|
574 | console.log("Using certificate:\n" + hexToPem(response.hex)); |
---|
575 | console.log("Using certificate(HEX):\n" + response.hex); |
---|
576 | if(totalArchivos.length>0){ |
---|
577 | $("button[name^='botonFirma-'").attr("disabled",false); |
---|
578 | } |
---|
579 | }); |
---|
580 | } |
---|
581 | var fileIdActivo; |
---|
582 | function enviaFormulario(){ |
---|
583 | //alert(1) |
---|
584 | //alert("https://murachi.uanesi.lan/Murachi/0.1/archivos/"+$("#contenidoQuery").val()); |
---|
585 | for(var n =0;n<$("#file-sign")[0].files.length;n++){ |
---|
586 | var formData = new FormData(); |
---|
587 | formData.append("upload", $("#file-sign")[0].files[n]); |
---|
588 | $.ajax({ |
---|
589 | url: "https://murachi.uanesi.lan/Murachi/0.1/archivos/"+$("#contenidoQuery").val(), |
---|
590 | type: "post", |
---|
591 | dataType: "json", |
---|
592 | data: formData, |
---|
593 | xhr: function() { |
---|
594 | var myXhr = $.ajaxSettings.xhr(); |
---|
595 | if(myXhr.upload){ |
---|
596 | myXhr.upload.addEventListener('progress',progress, false); |
---|
597 | } |
---|
598 | return myXhr; |
---|
599 | }, |
---|
600 | cache: false, |
---|
601 | contentType: false, |
---|
602 | processData: false, |
---|
603 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
604 | success: function(response) { |
---|
605 | $("#respuesta").html(""); |
---|
606 | for(var key in response) { |
---|
607 | $("#respuesta").html($("#respuesta").html()+"<br>"+response[key]) |
---|
608 | console.log(key); |
---|
609 | } |
---|
610 | fileIdActivo = response["fileId"]; |
---|
611 | incluyeArchivo(fileIdActivo); |
---|
612 | console.log(response) |
---|
613 | |
---|
614 | }, |
---|
615 | error: function(jqXHR, textStatus, errorThrown){ |
---|
616 | console.log('error: ' + textStatus); |
---|
617 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
618 | console.log('ajax error function: ' + jqXHR.responseText); |
---|
619 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
620 | } |
---|
621 | }) |
---|
622 | } |
---|
623 | } |
---|
624 | function progress(e){ |
---|
625 | |
---|
626 | if(e.lengthComputable){ |
---|
627 | var max = e.total; |
---|
628 | var current = e.loaded; |
---|
629 | |
---|
630 | var Percentage = (current * 100)/max; |
---|
631 | console.log(Percentage); |
---|
632 | |
---|
633 | |
---|
634 | if(Percentage >= 100) |
---|
635 | { |
---|
636 | // process completed |
---|
637 | $("#progresoCarga").html("100 % (Carga completada)"); |
---|
638 | }else{ |
---|
639 | $("#progresoCarga").html((Percentage.toFixed(2).replace(".",","))+" %"); |
---|
640 | |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | var hashActual; |
---|
645 | var sesionActual; |
---|
646 | var hashtype = "SHA-256"; |
---|
647 | var lang = "eng"; |
---|
648 | function colocaCertificado(){ |
---|
649 | var cert = certificadoActivo; |
---|
650 | /* |
---|
651 | var fileId = fileIdActivo; |
---|
652 | var fileId = "7778827d-1e20-4e78-a49e-79f7862ec44d"; |
---|
653 | */ |
---|
654 | //console.log("Using certificate:\n" + hexToPem(response.hex)); |
---|
655 | |
---|
656 | if(!cert){ |
---|
657 | alert("Debe seleccionar el certificado para firmar"); |
---|
658 | //console.log("cert: "+ cert.hex); |
---|
659 | }else{ |
---|
660 | if($("input[name=idArchivo]:checked").val()==undefined){ |
---|
661 | alert("Debe seleccionar primero un archivo"); |
---|
662 | }else{ |
---|
663 | var fileId = $("input[name=idArchivo]:checked").val(); |
---|
664 | var parameters = JSON.stringify({ |
---|
665 | "fileId":fileId, |
---|
666 | "certificate":cert.hex, |
---|
667 | "reason":"Certificado", |
---|
668 | "location":"MERIDA", |
---|
669 | "contact":"584247781500", |
---|
670 | "signatureVisible":true |
---|
671 | }); |
---|
672 | |
---|
673 | |
---|
674 | // ahora llamar al ajax de obtener la resena del pdf |
---|
675 | $.ajax({ |
---|
676 | url: "https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs", |
---|
677 | type: "post", |
---|
678 | dataType: "json", |
---|
679 | data: parameters, |
---|
680 | contentType: "application/json", |
---|
681 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
682 | success: function(data, textStatus, jqXHR){ |
---|
683 | for(var key in data) { |
---|
684 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
685 | console.log(key); |
---|
686 | } |
---|
687 | console.log(data) |
---|
688 | hashActual = data['hash']; |
---|
689 | sesionActual = data['sessionId']; |
---|
690 | }, function(err) { |
---|
691 | var error; |
---|
692 | if(err == "Error: user_cancel") { |
---|
693 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
694 | error = "El usuario cancelo la operación"; |
---|
695 | } |
---|
696 | else if(err == "Error: no_certificates") { |
---|
697 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
698 | error = "No hay certificado disponible"; |
---|
699 | } |
---|
700 | else if(err == "Error: no_implementation") { |
---|
701 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
702 | error = "No hay soporte para el manejo del certificado"; |
---|
703 | } |
---|
704 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
705 | } |
---|
706 | }); |
---|
707 | } |
---|
708 | } |
---|
709 | } |
---|
710 | |
---|
711 | function procesaIndividual(){ |
---|
712 | var cert = certificadoActivo; |
---|
713 | /* |
---|
714 | var fileId = fileIdActivo; |
---|
715 | var fileId = "7778827d-1e20-4e78-a49e-79f7862ec44d"; |
---|
716 | */ |
---|
717 | //console.log("Using certificate:\n" + hexToPem(response.hex)); |
---|
718 | var archivoEstimado = $(this).attr("id").replace("botonFirma-",""); |
---|
719 | if($("input[name=idArchivo]:checked").val()== archivoEstimado){ |
---|
720 | if(!cert){ |
---|
721 | alert("Debe seleccionar el certificado para firmar"); |
---|
722 | //console.log("cert: "+ cert.hex); |
---|
723 | }else{ |
---|
724 | if($("input[name=idArchivo]:checked").val()==undefined){ |
---|
725 | alert("Debe seleccionar primero un archivo"); |
---|
726 | }else{ |
---|
727 | var fileId = $("input[name=idArchivo]:checked").val(); |
---|
728 | var parameters = JSON.stringify({ |
---|
729 | "fileId":fileId, |
---|
730 | "certificate":cert.hex, |
---|
731 | "reason":"Certificado", |
---|
732 | "location":"MERIDA", |
---|
733 | "contact":"584247781500", |
---|
734 | "signatureVisible":true |
---|
735 | }); |
---|
736 | |
---|
737 | |
---|
738 | // ahora llamar al ajax de obtener la resena del pdf |
---|
739 | $.ajax({ |
---|
740 | url: "https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs", |
---|
741 | type: "post", |
---|
742 | dataType: "json", |
---|
743 | data: parameters, |
---|
744 | contentType: "application/json", |
---|
745 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
746 | success: function(data, textStatus, jqXHR){ |
---|
747 | for(var key in data) { |
---|
748 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
749 | console.log(key); |
---|
750 | } |
---|
751 | console.log(data) |
---|
752 | hashActual = data['hash']; |
---|
753 | sesionActual = data['sessionId']; |
---|
754 | |
---|
755 | $("#botonFirma-"+archivoEstimado).attr("disabled",true); |
---|
756 | window.hwcrypto.sign(certificadoActivo, {type: hashtype, hex: hashActual}, {lang: lang}).then(function(signature) { |
---|
757 | firmaActual = signature; |
---|
758 | console.log(hexToPem(signature.hex)); |
---|
759 | console.log(signature.hex); |
---|
760 | var fileIdAct = $("input[name=idArchivo]:checked").val(); |
---|
761 | |
---|
762 | $.ajax({ |
---|
763 | type: 'POST', |
---|
764 | contentType: 'application/json', |
---|
765 | //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs/resenas", |
---|
766 | url:"https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs/resenas", |
---|
767 | dataType: 'json', |
---|
768 | data: JSON.stringify({"signature":firmaActual.hex,fileId:fileIdAct}), |
---|
769 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
770 | success: function(data, textStatus, jqXHR){ |
---|
771 | for(var key in data) { |
---|
772 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
773 | console.log(key); |
---|
774 | var linkToDownload = "<a href=\"https://murachi.uanesi.lan/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>"; |
---|
775 | $("#respuesta").html($("#respuesta").html()+"<br>"+linkToDownload); |
---|
776 | idFileDescargaActual = data['signedFileId']; |
---|
777 | } |
---|
778 | console.log(data); |
---|
779 | $("#botonDescarga-"+archivoEstimado).attr("linkDescarga","https://murachi.uanesi.lan/Murachi/0.1/archivos/descargas/" + data['signedFileId']); |
---|
780 | $("#botonDescarga-"+archivoEstimado).attr("disabled",false); |
---|
781 | }, function(err) { |
---|
782 | var error; |
---|
783 | if(err == "Error: user_cancel") { |
---|
784 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
785 | error = "El usuario cancelo la operación"; |
---|
786 | } |
---|
787 | else if(err == "Error: no_certificates") { |
---|
788 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
789 | error = "No hay certificado disponible"; |
---|
790 | } |
---|
791 | else if(err == "Error: no_implementation") { |
---|
792 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
793 | error = "No hay soporte para el manejo del certificado"; |
---|
794 | } |
---|
795 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
796 | } |
---|
797 | }); |
---|
798 | }) |
---|
799 | |
---|
800 | |
---|
801 | |
---|
802 | }, function(err) { |
---|
803 | var error; |
---|
804 | if(err == "Error: user_cancel") { |
---|
805 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
806 | error = "El usuario cancelo la operación"; |
---|
807 | } |
---|
808 | else if(err == "Error: no_certificates") { |
---|
809 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
810 | error = "No hay certificado disponible"; |
---|
811 | } |
---|
812 | else if(err == "Error: no_implementation") { |
---|
813 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
814 | error = "No hay soporte para el manejo del certificado"; |
---|
815 | } |
---|
816 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
817 | } |
---|
818 | }); |
---|
819 | } |
---|
820 | } |
---|
821 | }else{ |
---|
822 | alert("Debe seleccionar este archivo para poder firmarlo") |
---|
823 | } |
---|
824 | } |
---|
825 | |
---|
826 | |
---|
827 | function colocaCertificado2(){ |
---|
828 | var cert = certificadoActivo; |
---|
829 | /* |
---|
830 | var fileId = fileIdActivo; |
---|
831 | var fileId = "7778827d-1e20-4e78-a49e-79f7862ec44d"; |
---|
832 | */ |
---|
833 | //console.log("Using certificate:\n" + hexToPem(response.hex)); |
---|
834 | |
---|
835 | if(!cert){ |
---|
836 | alert("Debe seleccionar el certificado para firmar"); |
---|
837 | //console.log("cert: "+ cert.hex); |
---|
838 | }else{ |
---|
839 | if($("input[name=idArchivo]:checked").val()==undefined){ |
---|
840 | alert("Debe seleccionar primero un archivo"); |
---|
841 | }else{ |
---|
842 | var fileId = $("input[name=idArchivo]:checked").val(); |
---|
843 | var parameters = JSON.stringify({ |
---|
844 | "fileId":fileId, |
---|
845 | "certificate":cert, |
---|
846 | "reason":"Certificado", |
---|
847 | "location":"MERIDA", |
---|
848 | "contact":"584247781500", |
---|
849 | "signatureVisible":true |
---|
850 | }); |
---|
851 | |
---|
852 | |
---|
853 | // ahora llamar al ajax de obtener la resena del pdf |
---|
854 | $.ajax({ |
---|
855 | url: "https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs", |
---|
856 | type: "post", |
---|
857 | dataType: "json", |
---|
858 | data: parameters, |
---|
859 | contentType: "application/json", |
---|
860 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
861 | success: function(data, textStatus, jqXHR){ |
---|
862 | for(var key in data) { |
---|
863 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
864 | console.log(key); |
---|
865 | } |
---|
866 | console.log(data) |
---|
867 | hashActual = data['hash']; |
---|
868 | sesionActual = data['sessionId']; |
---|
869 | }, function(err) { |
---|
870 | var error; |
---|
871 | if(err == "Error: user_cancel") { |
---|
872 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
873 | error = "El usuario cancelo la operación"; |
---|
874 | } |
---|
875 | else if(err == "Error: no_certificates") { |
---|
876 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
877 | error = "No hay certificado disponible"; |
---|
878 | } |
---|
879 | else if(err == "Error: no_implementation") { |
---|
880 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
881 | error = "No hay soporte para el manejo del certificado"; |
---|
882 | } |
---|
883 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
884 | } |
---|
885 | }); |
---|
886 | } |
---|
887 | } |
---|
888 | } |
---|
889 | var firmaActual; |
---|
890 | function procesaFirma(){ |
---|
891 | /* var cert = certificadoActivo; |
---|
892 | window.hwcrypto.sign(cert, {type: hashtype, hex: hashActual}, {lang: lang}).then(function(signature) { |
---|
893 | firmaActual = signature; |
---|
894 | console.log(hexToPem(signature.hex)); |
---|
895 | console.log(signature.hex); |
---|
896 | })*/ |
---|
897 | window.hwcrypto.sign(certificadoActivo, {type: hashtype, hex: hashActual}, {lang: lang}).then(function(signature) { |
---|
898 | firmaActual = signature; |
---|
899 | console.log(hexToPem(signature.hex)); |
---|
900 | console.log(signature.hex); |
---|
901 | }) |
---|
902 | } |
---|
903 | function procesaFirma2(){ |
---|
904 | |
---|
905 | $.ajax({ |
---|
906 | url: "https://cert.uanesi.lan/pruebaEncriptado.php?cadena="+hashActual, |
---|
907 | type: "post", |
---|
908 | dataType: "json", |
---|
909 | //data: formData, |
---|
910 | xhr: function() { |
---|
911 | var myXhr = $.ajaxSettings.xhr(); |
---|
912 | if(myXhr.upload){ |
---|
913 | myXhr.upload.addEventListener('progress',progress, false); |
---|
914 | } |
---|
915 | return myXhr; |
---|
916 | }, |
---|
917 | cache: false, |
---|
918 | contentType: false, |
---|
919 | processData: false, |
---|
920 | crossDomain:true, |
---|
921 | //headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
922 | success: function(data) { |
---|
923 | for(var key in data) { |
---|
924 | console.log(key); |
---|
925 | } |
---|
926 | firmaActual = data["signatureHEX"]; |
---|
927 | }, function(err) { |
---|
928 | console.log("Error al procesar los datos") |
---|
929 | } |
---|
930 | }); |
---|
931 | } |
---|
932 | var idFileDescargaActual; |
---|
933 | |
---|
934 | function colocaFirma(){ |
---|
935 | //var signature = response.hex.match(/.{1,64}/g).join("\n"); |
---|
936 | //log_text("Generated signature:\n" + response.hex.match(/.{1,64}/g).join("\n")); |
---|
937 | //log_text("Generated signature:\n" + signature.hex); |
---|
938 | |
---|
939 | //console.log("valor de la firma: " + signature.hex); |
---|
940 | |
---|
941 | var fileIdAct = $("input[name=idArchivo]:checked").val(); |
---|
942 | |
---|
943 | $.ajax({ |
---|
944 | type: 'POST', |
---|
945 | contentType: 'application/json', |
---|
946 | //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs/resenas", |
---|
947 | url:"https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs/resenas", |
---|
948 | dataType: 'json', |
---|
949 | data: JSON.stringify({"signature":firmaActual.hex,fileId:fileIdAct}), |
---|
950 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
951 | success: function(data, textStatus, jqXHR){ |
---|
952 | for(var key in data) { |
---|
953 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
954 | console.log(key); |
---|
955 | var linkToDownload = "<a href=\"https://murachi.uanesi.lan/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>"; |
---|
956 | $("#respuesta").html($("#respuesta").html()+"<br>"+linkToDownload); |
---|
957 | idFileDescargaActual = data['signedFileId']; |
---|
958 | } |
---|
959 | console.log(data); |
---|
960 | }, function(err) { |
---|
961 | var error; |
---|
962 | if(err == "Error: user_cancel") { |
---|
963 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
964 | error = "El usuario cancelo la operación"; |
---|
965 | } |
---|
966 | else if(err == "Error: no_certificates") { |
---|
967 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
968 | error = "No hay certificado disponible"; |
---|
969 | } |
---|
970 | else if(err == "Error: no_implementation") { |
---|
971 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
972 | error = "No hay soporte para el manejo del certificado"; |
---|
973 | } |
---|
974 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
975 | } |
---|
976 | }); |
---|
977 | } |
---|
978 | function colocaFirma2(){ |
---|
979 | //var signature = response.hex.match(/.{1,64}/g).join("\n"); |
---|
980 | //log_text("Generated signature:\n" + response.hex.match(/.{1,64}/g).join("\n")); |
---|
981 | //log_text("Generated signature:\n" + signature.hex); |
---|
982 | |
---|
983 | //console.log("valor de la firma: " + signature.hex); |
---|
984 | |
---|
985 | var fileIdAct = $("input[name=idArchivo]:checked").val(); |
---|
986 | |
---|
987 | $.ajax({ |
---|
988 | type: 'POST', |
---|
989 | contentType: 'application/json', |
---|
990 | //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs/resenas", |
---|
991 | url:"https://murachi.uanesi.lan/Murachi/0.1/archivos/pdfs/resenas", |
---|
992 | dataType: 'json', |
---|
993 | data: JSON.stringify({"signature":firmaActual,fileId:fileIdAct}), |
---|
994 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
995 | success: function(data, textStatus, jqXHR){ |
---|
996 | for(var key in data) { |
---|
997 | $("#respuesta").html($("#respuesta").html()+"<br>"+data[key]) |
---|
998 | console.log(key); |
---|
999 | var linkToDownload = "<a href=\"https://murachi.uanesi.lan/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>"; |
---|
1000 | $("#respuesta").html($("#respuesta").html()+"<br>"+linkToDownload); |
---|
1001 | idFileDescargaActual = data['signedFileId']; |
---|
1002 | } |
---|
1003 | console.log(data); |
---|
1004 | }, function(err) { |
---|
1005 | var error; |
---|
1006 | if(err == "Error: user_cancel") { |
---|
1007 | console.log("getCertificate() failed: El usuario cancelo la operación" ); |
---|
1008 | error = "El usuario cancelo la operación"; |
---|
1009 | } |
---|
1010 | else if(err == "Error: no_certificates") { |
---|
1011 | console.log("getCertificate() failed: No hay certificado disponible") ; |
---|
1012 | error = "No hay certificado disponible"; |
---|
1013 | } |
---|
1014 | else if(err == "Error: no_implementation") { |
---|
1015 | console.log("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
1016 | error = "No hay soporte para el manejo del certificado"; |
---|
1017 | } |
---|
1018 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
1019 | } |
---|
1020 | }); |
---|
1021 | } |
---|
1022 | var originalXhr = $.ajaxSettings.xhr; |
---|
1023 | $.ajaxSetup({ |
---|
1024 | xhr: function() { |
---|
1025 | var req = originalXhr(), that = this; |
---|
1026 | if (req) { |
---|
1027 | if (typeof req.addEventListener == "function" && that.progress !== undefined) { |
---|
1028 | req.addEventListener("progress", function(evt) { |
---|
1029 | that.progress(evt); |
---|
1030 | }, false); |
---|
1031 | } |
---|
1032 | if (typeof req.upload == "object" && that.progressUpload !== undefined) { |
---|
1033 | req.upload.addEventListener("progress", function(evt) { |
---|
1034 | that.progressUpload(evt); |
---|
1035 | }, false); |
---|
1036 | } |
---|
1037 | } |
---|
1038 | return req; |
---|
1039 | } |
---|
1040 | }); |
---|
1041 | function descargaArchivo(){ |
---|
1042 | $.ajax({ |
---|
1043 | type: 'GET', |
---|
1044 | contentType: false, |
---|
1045 | url: "https://murachi.uanesi.lan/Murachi/0.1/archivos/descargas/0fa2e3f8-b056-41c6-9f83-b1d93c870b97-signed.pdf", |
---|
1046 | dataType: false, |
---|
1047 | data: false, |
---|
1048 | progress: function(evt) { |
---|
1049 | if (evt.lengthComputable) { |
---|
1050 | console.log("Loaded " + parseInt( (evt.loaded / evt.total * 100), 10) + "%"); |
---|
1051 | } |
---|
1052 | else { |
---|
1053 | console.log("Length not computable."); |
---|
1054 | } |
---|
1055 | }, |
---|
1056 | progressUpload: function(evt) { |
---|
1057 | // See above |
---|
1058 | }, |
---|
1059 | success: function(data, textStatus, jqXHR){ |
---|
1060 | console.log("descargo el archivo"); |
---|
1061 | console.log(data); |
---|
1062 | }, |
---|
1063 | error: function(jqXHR, textStatus, errorThrown){ |
---|
1064 | console.log('error en descarga de archivo: ' + textStatus); |
---|
1065 | } |
---|
1066 | }); |
---|
1067 | } |
---|
1068 | function progresoDescarg(e){ |
---|
1069 | |
---|
1070 | if(e.lengthComputable){ |
---|
1071 | var max = e.total; |
---|
1072 | var current = e.loaded; |
---|
1073 | |
---|
1074 | var Percentage = (current * 100)/max; |
---|
1075 | console.log(Percentage); |
---|
1076 | |
---|
1077 | |
---|
1078 | if(Percentage >= 100) |
---|
1079 | { |
---|
1080 | // process completed |
---|
1081 | $("#progresodownload").html("100 % (Carga completada)"); |
---|
1082 | }else{ |
---|
1083 | $("#progresodownload").html((Percentage.toFixed(2).replace(".",","))+" %"); |
---|
1084 | |
---|
1085 | } |
---|
1086 | }else{ |
---|
1087 | console.log("No computable") |
---|
1088 | } |
---|
1089 | } |
---|
1090 | function reemplazaCadena(busca,cambia,texto){ |
---|
1091 | while(texto.indexOf(busca)>-1){ |
---|
1092 | texto = texto.replace(busca,cambia); |
---|
1093 | } |
---|
1094 | return texto; |
---|
1095 | } |
---|
1096 | var totalArchivos = new Array(); |
---|
1097 | function incluyeArchivo(idArchivo){ |
---|
1098 | totalArchivos.push(idArchivo); |
---|
1099 | var archivoNuevo = '<tr id="archivo-'+idArchivo+'">'+$("#archivoPrototipo").html()+'</tr>'; |
---|
1100 | archivoNuevo = reemplazaCadena('/-idArchivo-/',idArchivo,archivoNuevo); |
---|
1101 | archivoNuevo = reemplazaCadena('/-valorArchivo-/',idArchivo,archivoNuevo); |
---|
1102 | archivoNuevo = reemplazaCadena('/-valorRadioArchivo-/',idArchivo,archivoNuevo); |
---|
1103 | //archivoNuevo = reemplazaCadena('"montoUnitarioArticulo-"','"montoUnitarioArticulo-'+indice+'"',archivoNuevo); |
---|
1104 | //archivoNuevo = reemplazaCadena('"montoTotalArticulo-"','"montoTotalArticulo-'+indice+'"',archivoNuevo); |
---|
1105 | //archivoNuevo = reemplazaCadena('"cantidadArticulo-n-oferta"','"cantidadArticulo-'+indice+'-oferta"',archivoNuevo); |
---|
1106 | //archivoNuevo = reemplazaCadena('"montoUnitarioArticulo-n-oferta"','"montoUnitarioArticulo-'+indice+'-oferta"',archivoNuevo); |
---|
1107 | //archivoNuevo = reemplazaCadena('"montoTotalArticulo-n-oferta"','"montoTotalArticulo-'+indice+'-oferta"',archivoNuevo); |
---|
1108 | //archivoNuevo = reemplazaCadena('"botonEliminaArticulo-"','"botonEliminaArticulo-'+indice+'"',archivoNuevo); |
---|
1109 | //archivoNuevo = reemplazaCadena('"marcaArticulo-"','"marcaArticulo-'+indice+'"',archivoNuevo); |
---|
1110 | //archivoNuevo = reemplazaCadena('"exoneraArticulo-"','"exoneraArticulo-'+indice+'"',archivoNuevo); |
---|
1111 | |
---|
1112 | //$("#tablaArticulos").append(archivoNuevo); |
---|
1113 | $(archivoNuevo).insertBefore("#totalArchivos"); |
---|
1114 | $("#botonFirma-"+idArchivo).click(procesaIndividual); |
---|
1115 | $("#botonPosFirma-"+idArchivo).click(posFirma); |
---|
1116 | $("#botonDescarga-"+idArchivo).click(ejecutaDescarga); |
---|
1117 | var activo = false; |
---|
1118 | if(!certificadoActivo){ |
---|
1119 | activo = true; |
---|
1120 | } |
---|
1121 | $("#botonFirma-"+idArchivo).attr("disabled",activo); |
---|
1122 | $("#botonPosFirma-"+idArchivo).attr("disabled",activo); |
---|
1123 | $("#botonDescarga-"+idArchivo).attr("disabled",true); |
---|
1124 | $("#sinArchivos").css("display","none"); |
---|
1125 | $("#archivo-"+idArchivo).css("display","block"); |
---|
1126 | $("#cantArchivos").html(totalArchivos.length+" Archivos por firma") |
---|
1127 | } |
---|
1128 | </script> |
---|
1129 | |
---|
1130 | </head> |
---|
1131 | |
---|
1132 | |
---|
1133 | <body> |
---|
1134 | <div id="progresoupload"></div> |
---|
1135 | <div id="progresodownload"></div> |
---|
1136 | <input name="pfxp" type="text" id="pfxp" value="" placeholder="" style="display:none;"/> |
---|
1137 | <input name="contenidoQuery" type="text" id="contenidoQuery" value="" placeholder="" style="display:none;"/> |
---|
1138 | <table width="900" border="0" cellspacing="2" cellpadding="5" id="tablaArticulos" class="text-secondary"> |
---|
1139 | <tr> |
---|
1140 | <td colspan="5" style="text-align:center;">SELECCIONE UN ARCHIVO PARA COLOCARLO</td> |
---|
1141 | </tr> |
---|
1142 | <tr> |
---|
1143 | <td colspan="5" style="text-align:left;"><input id="file-sign" type="file" data-min-file-count="1" name="upload" multiple ></td> |
---|
1144 | </tr> |
---|
1145 | <tr> |
---|
1146 | <td colspan="5" style="text-align:left;"><button type="button" class="button" name="botonEnvia" id="botonEnvia">Cargar archivos</button></td> |
---|
1147 | </tr> |
---|
1148 | <tr> |
---|
1149 | <td colspan="5" style="text-align:left;" id="progresoCarga"> </td> |
---|
1150 | </tr> |
---|
1151 | <tr> |
---|
1152 | <td colspan="5" style="text-align:left;" id="progresoDescarga"> </td> |
---|
1153 | </tr> |
---|
1154 | |
---|
1155 | <tr> |
---|
1156 | <td colspan="5" style="text-align:center;">ARCHIVOS INCLUIDOS PARA LA FIRMA</td> |
---|
1157 | </tr> |
---|
1158 | <td colspan="5" id="sinArchivos" align="center" valign="middle" height="40"> |
---|
1159 | <strong>No se han ingresado items aún</strong> |
---|
1160 | </td> |
---|
1161 | <tr id="archivoPrototipo" style="display:none;"> |
---|
1162 | <td style="text-align:left;" width="20"><input type="hidden" value="/-valorArchivo-/" id="/-idArchivo-/" name="/-idArchivo-/"/><input type="radio" id="idArchivo" name="idArchivo" value="/-valorRadioArchivo-/"/></td> |
---|
1163 | <td style="text-align:left;" width="480">/-valorArchivo-/</td> |
---|
1164 | <td style="text-align:left;" width="150"><button type="button" class="button" name="botonPosFirma-/-idArchivo-/" id="botonPosFirma-/-idArchivo-/" disabled>Pos. Firma</button></td> |
---|
1165 | <td style="text-align:left;" width="150"><button type="button" class="button" name="botonFirma-/-idArchivo-/" id="botonFirma-/-idArchivo-/" disabled>Firma Archivo</button></td> |
---|
1166 | <td style="text-align:left;" width="180"><button type="button" class="button" name="botonDescarga-/-idArchivo-/" id="botonDescarga-/-idArchivo-/" disabled>Descarga Archivo</button></td> |
---|
1167 | </tr> |
---|
1168 | <tr id="totalArchivos"> |
---|
1169 | <td colspan="5" style="text-align:center;" id="cantArchivos"> </td> |
---|
1170 | </tr> |
---|
1171 | </table> |
---|
1172 | <br> |
---|
1173 | <br> |
---|
1174 | <br> |
---|
1175 | <button type="button" class="button" name="botonCertificadoTarjeta" id="botonCertificadoTarjeta">Obtiene Certificado desde Tarjeta</button> |
---|
1176 | <br> |
---|
1177 | <button type="button" class="button" name="botonCertificadoTarjetaProcesa" id="botonCertificadoTarjetaProcesa">Procesa Certificado desde Tarjeta</button> |
---|
1178 | <br> |
---|
1179 | <button type="button" class="button" name="botonCertificadoTarjetaFirma" id="botonCertificadoTarjetaFirma">Firma desde Tarjeta</button> |
---|
1180 | <br> |
---|
1181 | <button type="button" class="button" name="botonColocaFirma" id="botonColocaFirma">Coloca firma desde Tarjeta</button> |
---|
1182 | <br> |
---|
1183 | <br> |
---|
1184 | <br> |
---|
1185 | <br> |
---|
1186 | <button type="button" class="button" name="botonCertificado" id="botonCertificado">Obtiene Certificado</button> |
---|
1187 | <br> |
---|
1188 | <button type="button" class="button" name="botonCertificadoProcesa" id="botonCertificadoProcesa">Procesa Certificado sin Tarjeta</button> |
---|
1189 | <br> |
---|
1190 | <button type="button" class="button" name="botonCertificadoFirma" id="botonCertificadoFirma">Firma sin Tarjeta</button> |
---|
1191 | <br> |
---|
1192 | <button type="button" class="button" name="botonColocaFirma2" id="botonColocaFirma2">Coloca firma sin Tarjeta</button> |
---|
1193 | <br> |
---|
1194 | <br> |
---|
1195 | <br> |
---|
1196 | <br> |
---|
1197 | <br> |
---|
1198 | <button type="button" class="button" name="botonDescargaArchivo" id="botonDescargaArchivo">Descarga Archivo</button> |
---|
1199 | |
---|
1200 | <div id="respuesta"></div> |
---|
1201 | |
---|
1202 | </body> |
---|
1203 | </html> |
---|