UNPKG

ctjs

Version:

CTjs is a full set of classes necessary to work with any kind of Certificate Transparency log (V1 as from RFC6962, or V2 as from RFC6962-bis). In CTjs you could find all necessary validation/verification functions for all related data shipped with full-fe

168 lines (140 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _asn1js = require("asn1js"); var asn1js = _interopRequireWildcard(_asn1js); var _pvutils = require("pvutils"); var _pkijs = require("pkijs"); var _PreCert = require("./PreCert.js"); var _PreCert2 = _interopRequireDefault(_PreCert); var _LogEntryType = require("./LogEntryType.js"); var _LogEntryType2 = _interopRequireDefault(_LogEntryType); var _BaseClass = require("./BaseClass.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } //************************************************************************************** class TimestampedEntry extends _BaseClass.BaseClass { //********************************************************************************** /** * Constructor for TimestampedEntry class * @param {Object} [parameters={}] * @property {Object} [schema] asn1js parsed value */ constructor(parameters = {}) { super(parameters); //region Internal properties of the object /** * @type {Date} * @description timestamp */ this.timestamp = (0, _pvutils.getParametersValue)(parameters, "timestamp", TimestampedEntry.constants("timestamp")); /** * @type {Number} * @description entryType */ this.entryType = (0, _pvutils.getParametersValue)(parameters, "entryType", TimestampedEntry.constants("entryType")); /** * @type {Certificate|PreCert} * @description signedEntry */ this.signedEntry = (0, _pvutils.getParametersValue)(parameters, "signedEntry", TimestampedEntry.constants("signedEntry")); /** * @type {ArrayBuffer} * @description extensions */ this.extensions = (0, _pvutils.getParametersValue)(parameters, "extensions", TimestampedEntry.constants("extensions")); //endregion //region If input argument array contains "stream" for this object if ("stream" in parameters) this.fromStream(parameters.stream); //endregion } //********************************************************************************** /** * Return value for a constant by name * @param {string} name String name for a constant */ static constants(name) { switch (name) { case "timestamp": return new Date(); case "entryType": return _LogEntryType2.default.constants("x509_entry"); case "signedEntry": return {}; case "extensions": return new ArrayBuffer(0); default: throw new Error(`Invalid constant name for TimestampedEntry class: ${name}`); } } //********************************************************************************** /** * Convert SeqStream data into current class * @param {!SeqStream} stream */ fromStream(stream) { // struct { // uint64 timestamp; // LogEntryType entry_type; // select(entry_type) { // case x509_entry: ASN.1Cert; // case precert_entry: PreCert; // } signed_entry; // CtExtensions extensions; // } TimestampedEntry; this.timestamp = new Date((0, _pvutils.utilFromBase)(new Uint8Array(stream.getBlock(8)), 8)); this.entryType = stream.getUint16(); switch (this.entryType) { case _LogEntryType2.default.constants("x509_entry"): { const certificateLength = stream.getUint24(); const asn1 = asn1js.fromBER(new Uint8Array(stream.getBlock(certificateLength)).buffer.slice(0)); if (asn1.offset === -1) throw new Error("Object's stream was not correct for TimestampedEntry"); this.signedEntry = new _pkijs.Certificate({ schema: asn1.result }); } break; case _LogEntryType2.default.constants("precert_entry"): this.signedEntry = new _PreCert2.default({ stream }); break; default: throw new Error("Object's stream was not correct for TimestampedEntry"); } const extensionsLength = stream.getUint16(); if (extensionsLength) this.extensions = new Uint8Array(stream.getBlock(extensionsLength)).buffer.slice(0); } //********************************************************************************** /** * Convert current object to SeqStream data * @param {!SeqStream} stream * @returns {boolean} Result of the function */ toStream(stream) { const timeBuffer = new ArrayBuffer(8); const timeView = new Uint8Array(timeBuffer); const baseArray = (0, _pvutils.utilToBase)(this.timestamp.valueOf(), 8); timeView.set(new Uint8Array(baseArray), 8 - baseArray.byteLength); stream.appendView(timeView); stream.appendUint16(this.entryType); switch (this.entryType) { case _LogEntryType2.default.constants("x509_entry"): { const buffer = this.signedEntry.toSchema().toBER(false); stream.appendUint24(buffer.byteLength); stream.appendView(new Uint8Array(buffer)); } break; case _LogEntryType2.default.constants("precert_entry"): this.signedEntry.toStream(stream); break; default: throw new Error("Incorrect entryType value for TimestampedEntry"); } stream.appendUint16(this.extensions.byteLength); if (this.extensions.byteLength !== 0) stream.appendView(new Uint8Array(this.extensions)); return true; } //********************************************************************************** } exports.default = TimestampedEntry; //************************************************************************************** //# sourceMappingURL=TimestampedEntry.js.map