UNPKG

xml-fiesta

Version:

Electronic signed document XML Protocol for Node & Browser

105 lines 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var common_1 = require("./common"); var certificate_1 = require("./certificate"); var errors_1 = require("./errors"); var jsrsasign = require('jsrsasign'); var ConservancyRecord = (function () { function ConservancyRecord(caCert, userCert, record, timestamp, signedHash) { this.caCert = caCert; this.userCert = userCert; this.record = record; this.timestamp = timestamp; this.signedHash = signedHash; try { this.caCertificate = new certificate_1.default(null, common_1.b64toHex(this.caCert)); } catch (err) { this.caCertificate = null; } try { this.userCertificate = new certificate_1.default(null, common_1.b64toHex(this.userCert)); } catch (err) { this.userCertificate = null; } this.recordHex = common_1.b64toHex(this.record); if (!jsrsasign.ASN1HEX.isASN1HEX(this.recordHex)) { throw new errors_1.InvalidRecordError('The record provided is invalid'); } this.positions = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.recordHex, 0); } ConservancyRecord.prototype.caName = function () { if (this.caCertificate) { return this.caCertificate.getSubject().O; } }; ConservancyRecord.prototype.userName = function () { if (this.userCertificate) { return this.userCertificate.getSubject().O; } }; ConservancyRecord.prototype.timestampHex = function () { return jsrsasign.ASN1HEX.getHexOfTLV_AtObj(this.recordHex, this.positions[2]); }; ConservancyRecord.prototype.archiveHex = function () { return jsrsasign.ASN1HEX.getHexOfTLV_AtObj(this.recordHex, this.positions[1]); }; ConservancyRecord.prototype.archiveSignature = function () { var ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), 0); ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), ar_pos[3]); return jsrsasign.ASN1HEX.getHexOfV_AtObj(this.archiveHex(), ar_pos[1]); }; ConservancyRecord.prototype.archiveSignedHash = function () { var ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), 0); ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), ar_pos[1]); ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), ar_pos[0]); ar_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.archiveHex(), ar_pos[1]); var signedHashH = jsrsasign.ASN1HEX.getHexOfV_AtObj(this.archiveHex(), ar_pos[1]); return common_1.hextoAscii(signedHashH.replace(/^[0]+/g, '')); }; ConservancyRecord.prototype.validArchiveHash = function () { if (this.signedHash !== this.archiveSignedHash()) { return false; } return this.userCertificate.verifyString(this.signedHash, this.archiveSignature()); }; ConservancyRecord.prototype.recordTimestamp = function () { var ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), 0); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[0]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[1]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[1]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[0]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[2]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[1]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[0]); ts_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.timestampHex(), ts_pos[0]); var date = jsrsasign.ASN1HEX.getHexOfV_AtObj(this.timestampHex(), ts_pos[4]); return common_1.parseDate(common_1.hextoAscii(date)); }; ConservancyRecord.prototype.equalTimestamps = function () { return Date.parse(this.timestamp) === this.recordTimestamp().getTime(); }; ConservancyRecord.prototype.signedData = function () { var nameHex = jsrsasign.ASN1HEX.getHexOfTLV_AtObj(this.recordHex, this.positions[0]); return nameHex + this.archiveHex() + this.timestampHex(); }; ConservancyRecord.prototype.signature = function () { var signature_pos = jsrsasign.ASN1HEX.getPosArrayOfChildren_AtObj(this.recordHex, this.positions[3]); return jsrsasign.ASN1HEX.getHexOfV_AtObj(this.recordHex, signature_pos[1]); }; ConservancyRecord.prototype.valid = function () { if (!this.caCertificate) { return false; } return this.caCertificate.verifyHexString(this.signedData(), this.signature()); }; ConservancyRecord.prototype.isCa = function (caPemCert) { if (this.caCertificate) { return this.caCertificate.isCa(caPemCert); } }; return ConservancyRecord; }()); exports.default = ConservancyRecord; //# sourceMappingURL=conservancyRecord.js.map