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
130 lines (112 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _pvutils = require("pvutils");
var _bytestreamjs = require("bytestreamjs");
var _DigitallySigned = require("./DigitallySigned.js");
var _DigitallySigned2 = _interopRequireDefault(_DigitallySigned);
var _utils = require("./utils.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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"); }); }; }
//**************************************************************************************
class SignedTreeHead {
//**********************************************************************************
/**
* Constructor for SignedTreeHead class
* @param {Object} [parameters={}]
* @property {Object} [schema] asn1js parsed value
*/
constructor(parameters = {}) {
//region Internal properties of the object
/**
* @type {Number}
* @description treeSize
*/
this.treeSize = (0, _pvutils.getParametersValue)(parameters, "treeSize", SignedTreeHead.constants("treeSize"));
/**
* @type {Date}
* @description timestamp
*/
this.timestamp = (0, _pvutils.getParametersValue)(parameters, "timestamp", SignedTreeHead.constants("timestamp"));
/**
* @type {ArrayBuffer}
* @description rootHash
*/
this.rootHash = (0, _pvutils.getParametersValue)(parameters, "rootHash", SignedTreeHead.constants("rootHash"));
/**
* @type {DigitallySigned}
* @description treeHeadSignature
*/
this.treeHeadSignature = (0, _pvutils.getParametersValue)(parameters, "treeHeadSignature", SignedTreeHead.constants("treeHeadSignature"));
//endregion
//region If input argument array contains "json" for this object
if ("json" in parameters) this.fromJSON(parameters.json);
//endregion
}
//**********************************************************************************
/**
* Return value for a constant by name
* @param {string} name String name for a constant
*/
static constants(name) {
switch (name) {
case "treeSize":
return 0;
case "timestamp":
return new Date();
case "rootHash":
return new ArrayBuffer(0);
case "treeHeadSignature":
return new _DigitallySigned2.default();
default:
throw new Error(`Invalid constant name for SignedTreeHead class: ${name}`);
}
}
//**********************************************************************************
/**
* Convert JSON value into current object
* @param {Object} json
* @param {String} json.tree_size
* @param {String} json.timestamp
* @param {String} json.sha256_root_hash
* @param {String} json.tree_head_signature
*/
fromJSON(json) {
this.treeSize = json.tree_size;
this.timestamp = new Date(json.timestamp);
this.rootHash = (0, _pvutils.stringToArrayBuffer)((0, _pvutils.fromBase64)(json.sha256_root_hash));
const stream = new _bytestreamjs.SeqStream({
buffer: (0, _pvutils.stringToArrayBuffer)((0, _pvutils.fromBase64)(json.tree_head_signature))
});
this.treeHeadSignature = new _DigitallySigned2.default({ stream });
}
//**********************************************************************************
/**
* Verify Signed Tree Head using given public key
* @param {PublicKeyInfo} publicKey Public key using for verification
* @return {Promise<Boolean>}
*/
verify(publicKey) {
var _this = this;
return _asyncToGenerator(function* () {
// digitally-signed struct {
// Version version;
// SignatureType signature_type = tree_hash;
// uint64 timestamp;
// uint64 tree_size;
// opaque sha256_root_hash[32];
// } TreeHeadSignature;
const stream = new _bytestreamjs.SeqStream();
stream.appendChar(0); // version
stream.appendChar(1); // signature_type = tree_hash;
_utils.utils.appendUint64(_this.timestamp.valueOf(), stream); // timestamp
_utils.utils.appendUint64(_this.treeSize, stream); // tree_size
stream.appendView(new Uint8Array(_this.rootHash)); // sha256_root_hash
return _this.treeHeadSignature.verify(stream.buffer, publicKey);
})();
}
//**********************************************************************************
}
exports.default = SignedTreeHead; //**************************************************************************************
//# sourceMappingURL=SignedTreeHead.js.map