function HermesFDARemote(urlBase, servicioId, sesionReadyCallback, sesionErrorCallback, desconectadoCallback) { // propiedades this.version = 1; this.lastErrorMessage = ""; this.description = "Proxy remoto de componente de firma digital de Hermes 1.1"; this.pinLength = 0; this.lastError = 0; this.lastLowLevelError = 0; this.pinModuleName = ""; this.isPinMissing = false; this.isPinInvalid = false; this.pinRetryCount = 0; this.pin = ""; this.isInstalled = true; this.pinNeeded = false; this.isUpdated = true; // propiedades del servicio this.servicioId = servicioId; this.cliente = new HermesMsg(urlBase); // Establecer una sesión segura this.sesionEstablecida = false; this.Desconectado = desconectadoCallback; // eventos this.onCertificatesReady = null; this.onUpdated = null; this.onCertificateReady = null; this.onSignatureReady = null; // codigos de comandos this.ListarCertificados = 1; this.ObtenerCertificado = 2; this.FirmarDigitalmente = 3; } HermesFDARemote.prototype.IniciarSesion = function (sesionReadyCallback, sesionErrorCallback) { var obj = this; var result = this.cliente.IniciarSesion( this.servicioId, function () { obj.sesionEstablecida = true; if (sesionReadyCallback != null) { try { sesionReadyCallback(); } catch (e) { } } }, function () { if (sesionErrorCallback != null) { try { sesionErrorCallback(); } catch (e) { } } } ); } HermesFDARemote.prototype.isInstalled = function () { return this.isInstalled; } HermesFDARemote.prototype.getVersion = function () { return this.version; } HermesFDARemote.prototype.processPinChar = function (char) { this.pin += char; this.pinLength = this.pin.length; } HermesFDARemote.prototype.deletePinChar = function () { if (this.pin.length > 0) { this.pin = this.pin.substr(0, this.pin.length - 1); this.pinLength = this.pin.length; } } HermesFDARemote.prototype.getDigitalSignatureCertificates = function () { // Tratar de resolver desde el cache var certificateList = getFromCache("getDigitalSignatureCertificates"); if (certificateList != null) { this.lastError = 0; this.lastErrorMessage = ""; this.lastLowLevelError = 0; return certificateList; } // Es necesario que haya una sesión segura if (!this.sesionEstablecida) { this.lastError = 12; this.lastErrorMessage = "No se puede establecer una sesión"; if (this.onCertificatesReady != null) { this.onCertificatesReady(""); } } var comando = this.CrearComando(this.ListarCertificados, null); var obj = this; this.cliente.EnviarMensaje( this.servicioId, comando, function (respuesta) { respuesta = JSON.parse(respuesta); obj.ProcesarRespuesta(respuesta, respuesta.Certificados); if (respuesta.Certificados != null) { addToCache("getDigitalSignatureCertificates", respuesta.Certificados); } if (obj.onCertificatesReady != null) { obj.onCertificatesReady(respuesta.Certificados); } }, function(error) { obj.ActualizarEstado(); if (obj.onCertificatesReady != null) { obj.onCertificatesReady(""); } if (obj.servicioId == null && obj.Desconectado != null) { obj.Desconectado(); } } ); } HermesFDARemote.prototype.getCertificate = function (certId) { // Tratar de resolver desde el cache var certificado = getFromCache(certId); if (certificado != null) { this.lastError = 0; this.lastErrorMessage = ""; this.lastLowLevelError = 0; return certificado; } // Es necesario que haya una sesión segura if (!this.sesionEstablecida) { this.lastError = 12; this.lastErrorMessage = "No se puede establecer una sesión"; if (this.onCertificateReady != null) { this.onCertificateReady(""); } } var comando = this.CrearComando(this.ObtenerCertificado, certId); var obj = this; this.cliente.EnviarMensaje( this.servicioId, comando, function (respuesta) { respuesta = JSON.parse(respuesta); obj.ProcesarRespuesta(respuesta, respuesta.Certificado); if (respuesta.Certificado != null) { addToCache(certId, respuesta.Certificado); } if (obj.onCertificateReady != null) { obj.onCertificateReady(respuesta.Certificado); } }, function (error) { obj.ActualizarEstado(); if (obj.onCertificateReady != null) { obj.onCertificateReady(""); } if (obj.servicioId == null && obj.Desconectado != null) { obj.Desconectado(); } } ); } HermesFDARemote.prototype.signPkcs1 = function(certId, digest) { // Es necesario que haya una sesión segura if (!this.sesionEstablecida) { this.lastError = 12; this.lastErrorMessage = "No se puede establecer una sesión"; if (this.onSignatureReady != null) { this.onSignatureReady(""); } } var comando = this.CrearComando(this.FirmarDigitalmente, { certId: certId, digest: digest }); var obj = this; this.cliente.EnviarMensaje( this.servicioId, comando, function (respuesta) { respuesta = JSON.parse(respuesta); obj.ProcesarRespuesta(respuesta, respuesta.Pkcs1); if (obj.onSignatureReady != null) { obj.onSignatureReady(respuesta.Pkcs1); } }, function (error) { obj.ActualizarEstado(); if (obj.onSignatureReady != null) { obj.onSignatureReady(""); } if (obj.servicioId == null && obj.Desconectado != null) { obj.Desconectado(); } } ); } HermesFDARemote.prototype.CrearComando = function(codigo, argumento) { return JSON.stringify({Codigo:codigo, Pin:this.pin, Argumento:argumento}); } HermesFDARemote.prototype.ProcesarRespuesta = function (respuesta, dato) { var error = dato == null || dato == ""; if (error) { this.lastError = respuesta.lastError; this.lastErrorMessage = respuesta.lastErrorMessage; this.lastLowLevelError = respuesta.lastLowLevelError } else { this.lastError = 0; this.lastErrorMessage = ""; this.lastLowLevelError = 0; } this.pinModuleName = respuesta.pinModuleName; this.isPinMissing = respuesta.isPinMissing; this.isPinInvalid =respuesta.isPinInvalid; this.pinRetryCount = respuesta.pinRetryCount; this.pinNeeded = respuesta.pinNeeded; this.isInstalled = respuesta.isInstalled; this.isUpdated = respuesta.isUpdated; this.pinLength = respuesta.pinLength; if (this.pinLength == 0) { this.pin = ""; } } HermesFDARemote.prototype.ActualizarEstado = function (respuesta) { // Si el cliente está en estado Desocupado significa que el servidor // dejo de responder if (this.cliente.EstadoMensaje == this.cliente.Desocupado) { this.lastError = 12; this.lastErrorMessage = "No se puede establecer una conexión con el lector"; this.servicioId = null; } } function RutaBaseServicioFirma() { var rutaBase = HermesFDAPrepareSignaturePageURL; var i = rutaBase.indexOf("FirmaDigitalAvanzada/PrepararCades"); if (i > 0) { rutaBase = rutaBase.substr(0, i); } return rutaBase; } function ObtenerIdServicio(pedirAUsuario) { var servicioId = readCookie("ServicioId"); if (pedirAUsuario && servicioId == null) { servicioId = prompt("Introduzca el c\xf3digo de acceso remoto:"); if (servicioId == null || servicioId == "") { eraseCookie("ServicioId"); } else { createCookie("ServicioId", servicioId, null); } } return servicioId == null || servicioId == "" ? null : parseInt(servicioId, 16); } function BorrarServicioId() { eraseCookie("ServicioId"); } function createCookie(name, value, days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/"; } function readCookie(name) { var nameEQ = encodeURIComponent(name) + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) === ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length)); } return null; } function eraseCookie(name) { createCookie(name, "", -1); }