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

171 lines (147 loc) 5.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _asn1js = require("asn1js"); var asn1js = _interopRequireWildcard(_asn1js); var _pvutils = require("pvutils"); var _bytestreamjs = require("bytestreamjs"); var _pkijs = require("pkijs"); var _TransItem = require("./TransItem.js"); var _TransItem2 = _interopRequireDefault(_TransItem); 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; } } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /* eslint-disable no-useless-escape */ //************************************************************************************** class LogV2 { //********************************************************************************** /** * Constructor for LogV2 class * @param {Object} [parameters={}] * @property {Object} [schema] asn1js parsed value */ constructor(parameters = {}) { //region Internal properties of the object /** * @type {Function} * @description fetch */ this.fetch = (0, _pvutils.getParametersValue)(parameters, "fetch", LogV2.constants("fetch")); /** * @type {Function} * @description encode */ this.encode = (0, _pvutils.getParametersValue)(parameters, "encode", LogV2.constants("encode")); /** * @type {String} * @description url */ this.url = (0, _pvutils.getParametersValue)(parameters, "url", LogV2.constants("url")); /** * @type {String} * @description hashAlgorithm */ this.hashAlgorithm = (0, _pvutils.getParametersValue)(parameters, "hashAlgorithm", LogV2.constants("hashAlgorithm")); /** * @type {String} * @description signatureAlgorithm */ this.signatureAlgorithm = (0, _pvutils.getParametersValue)(parameters, "signatureAlgorithm", LogV2.constants("signatureAlgorithm")); if ("key" in parameters) { const asn1 = asn1js.fromBER((0, _pvutils.stringToArrayBuffer)((0, _pvutils.fromBase64)(parameters.key))); if (asn1.offset !== -1) { /** * @type {PublicKeyInfo} * @description key */ this.key = new _pkijs.PublicKeyInfo({ schema: asn1.result }); } } if ("log_id" in parameters) { /** * @type {String} * @description logID */ this.logID = (0, _pvutils.getParametersValue)(parameters, "log_id", LogV2.constants("logID")); } if ("maximum_merge_delay" in parameters) { /** * @type {Number} * @description maximumMergeDelay */ this.maximumMergeDelay = (0, _pvutils.getParametersValue)(parameters, "maximum_merge_delay", LogV2.constants("maximumMergeDelay")); } if ("final_sth" in parameters) { this.finalSTH = { treeSize: parameters.final_sth.tree_size, timestamp: new Date(parameters.final_sth.timestamp), rootHash: (0, _pvutils.stringToArrayBuffer)((0, _pvutils.fromBase64)(parameters.final_sth.sha256_root_hash)), signature: new _TransItem2.default({ stream: new _bytestreamjs.SeqStream({ buffer: (0, _pvutils.stringToArrayBuffer)((0, _pvutils.fromBase64)(parameters.final_sth.tree_head_signature)) }) }) }; } //endregion } //********************************************************************************** /** * Return value for a constant by name * @param {string} name String name for a constant */ static constants(name) { switch (name) { case "fetch": return _asyncToGenerator(function* () { return Promise.reject("Uninitialized fetch function for LogV2 class"); }); case "encode": return () => { throw new Error("Uninitialized encode function for LogV2 class"); }; case "url": return ""; case "hashAlgorithm": return "SHA-256"; case "signatureAlgorithm": return "ECDSA"; case "logID": return ""; case "maximumMergeDelay": return 0; default: throw new Error(`Invalid constant name for LogV2 class: ${name}`); } } //********************************************************************************** set url(value) { if (value === "") return; const match = value.match(/(?:http[s]?:\/\/)?([^?\/s]+.*)/); if (match === null) throw new Error("Base URL for LogV2 class must be set to a correct value"); this._url = `https://${match[1].replace(/\/*$/g, "")}/ct/v2`; } //**********************************************************************************\ get url() { return this._url; } //********************************************************************************** /** * Implement call to "get-sth" Certificate Transparency Log API * @return {Promise<SignedTreeHead>} Latest Signed Tree Head */ get_sth() { var _this = this; return _asyncToGenerator(function* () { const api = "get-sth"; const json = yield _this.fetch(`${_this.url}/${api}`).then(function (result) { if (result.ok) return result.json(); return Promise.reject(`ERROR while fetching ${api}: ${result.statusText}`); }); return json; })(); } //********************************************************************************** } exports.default = LogV2; //************************************************************************************** //# sourceMappingURL=LogV2.js.map