All files / src TimeStampResp.js

57.14% Statements 24/42
45% Branches 18/40
66.67% Functions 6/9
57.14% Lines 24/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                                36x   36x         12x       36x 24x                   48x     36x   12x                                                                                       24x   24x                                                 24x         24x         24x 24x 24x                     12x   12x 12x 12x       12x                                                                                                             12x         12x         12x   12x            
import * as asn1js from "asn1js";
import { getParametersValue } from "pvutils";
import PKIStatusInfo from "./PKIStatusInfo.js";
import ContentInfo from "./ContentInfo.js";
import SignedData from "./SignedData.js";
//**************************************************************************************
/**
 * Class from RFC3161
 */
export default class TimeStampResp
{
	//**********************************************************************************
	/**
	 * Constructor for TimeStampResp class
	 * @param {Object} [parameters={}]
	 * @property {Object} [schema] asn1js parsed value
	 */
	constructor(parameters = {})
	{
		//region Internal properties of the object
		/**
		 * @type {PKIStatusInfo}
		 * @description status
		 */
		this.status = getParametersValue(parameters, "status", TimeStampResp.defaultValues("status"));
 
		if("timeStampToken" in parameters)
			/**
			 * @type {ContentInfo}
			 * @description timeStampToken
			 */
			this.timeStampToken = getParametersValue(parameters, "timeStampToken", TimeStampResp.defaultValues("timeStampToken"));
		//endregion
 
		//region If input argument array contains "schema" for this object
		if("schema" in parameters)
			this.fromSchema(parameters.schema);
		//endregion
	}
	//**********************************************************************************
	/**
	 * Return default values for all class members
	 * @param {string} memberName String name for a class member
	 */
	static defaultValues(memberName)
	{
		switch(memberName)
		{
			case "status":
				return new PKIStatusInfo();
			case "timeStampToken":
				return new ContentInfo();
			default:
				throw new Error(`Invalid member name for TimeStampResp class: ${memberName}`);
		}
	}
	//**********************************************************************************
	/**
	 * Compare values with default values for all class members
	 * @param {string} memberName String name for a class member
	 * @param {*} memberValue Value to compare with default value
	 */
	static compareWithDefault(memberName, memberValue)
	{
		switch(memberName)
		{
			case "status":
				return ((PKIStatusInfo.compareWithDefault("status", memberValue.status)) &&
						(("statusStrings" in memberValue) === false) &&
						(("failInfo" in memberValue) === false));
			case "timeStampToken":
				return ((memberValue.contentType === "") &&
						(memberValue.content instanceof asn1js.Any));
			default:
				throw new Error(`Invalid member name for TimeStampResp class: ${memberName}`);
		}
	}
	//**********************************************************************************
	/**
	 * Return value of asn1js schema for current class
	 * @param {Object} parameters Input parameters for the schema
	 * @returns {Object} asn1js schema object
	 */
	static schema(parameters = {})
	{
		//TimeStampResp ::= SEQUENCE  {
		//    status                  PKIStatusInfo,
		//    timeStampToken          TimeStampToken     OPTIONAL  }
 
		/**
		 * @type {Object}
		 * @property {string} [blockName]
		 * @property {string} [status]
		 * @property {string} [timeStampToken]
		 */
		const names = getParametersValue(parameters, "names", {});
 
		return (new asn1js.Sequence({
			name: (names.blockName || "TimeStampResp"),
			value: [
				PKIStatusInfo.schema(names.status || {
					names: {
						blockName: "TimeStampResp.status"
					}
				}),
				ContentInfo.schema(names.timeStampToken || {
					names: {
						blockName: "TimeStampResp.timeStampToken",
						optional: true
					}
				})
			]
		}));
	}
	//**********************************************************************************
	/**
	 * Convert parsed asn1js object into current class
	 * @param {!Object} schema
	 */
	fromSchema(schema)
	{
		//region Check the schema is valid
		const asn1 = asn1js.compareSchema(schema,
			schema,
			TimeStampResp.schema()
		);
 
		Iif(asn1.verified === false)
			throw new Error("Object's schema was not verified against input data for TSP_RESPONSE");
		//endregion
 
		//region Get internal properties from parsed schema
		this.status = new PKIStatusInfo({ schema: asn1.result["TimeStampResp.status"] });
		Eif("TimeStampResp.timeStampToken" in asn1.result)
			this.timeStampToken = new ContentInfo({ schema: asn1.result["TimeStampResp.timeStampToken"] });
		//endregion
	}
	//**********************************************************************************
	/**
	 * Convert current object to asn1js object and set correct values
	 * @returns {Object} asn1js object
	 */
	toSchema()
	{
		//region Create array for output sequence
		const outputArray = [];
 
		outputArray.push(this.status.toSchema());
		Eif("timeStampToken" in this)
			outputArray.push(this.timeStampToken.toSchema());
		//endregion
 
		//region Construct and return new ASN.1 schema for this object
		return (new asn1js.Sequence({
			value: outputArray
		}));
		//endregion
	}
	//**********************************************************************************
	/**
	 * Convertion for the class to JSON object
	 * @returns {Object}
	 */
	toJSON()
	{
		const _object = {
			status: this.status
		};
 
		if("timeStampToken" in this)
			_object.timeStampToken = this.timeStampToken.toJSON();
 
		return _object;
	}
	//**********************************************************************************
	/**
	 * Sign current TSP Response
	 * @param {Object} privateKey Private key for "subjectPublicKeyInfo" structure
	 * @param {string} [hashAlgorithm] Hashing algorithm. Default SHA-1
	 * @returns {Promise}
	 */
	sign(privateKey, hashAlgorithm)
	{
		//region Check that "timeStampToken" exists
		if(("timeStampToken" in this) === false)
			return Promise.reject("timeStampToken is absent in TSP response");
		//endregion
 
		//region Check that "timeStampToken" has a right internal format
		if(this.timeStampToken.contentType !== "1.2.840.113549.1.7.2") // Must be a CMS signed data
			return Promise.reject(`Wrong format of timeStampToken: ${this.timeStampToken.contentType}`);
		//endregion
 
		//region Sign internal signed data value
		const signed = new ContentInfo({ schema: this.timeStampToken.content });
 
		return signed.sign(privateKey, 0, hashAlgorithm);
		//endregion
	}
	//**********************************************************************************
	/**
	 * Verify current TSP Response
	 * @param {Object} verificationParameters Input parameters for verification
	 * @returns {Promise}
	 */
	verify(verificationParameters = { signer: 0, trustedCerts: [], data: new ArrayBuffer(0) })
	{
		//region Check that "timeStampToken" exists
		Iif(("timeStampToken" in this) === false)
			return Promise.reject("timeStampToken is absent in TSP response");
		//endregion
 
		//region Check that "timeStampToken" has a right internal format
		Iif(this.timeStampToken.contentType !== "1.2.840.113549.1.7.2") // Must be a CMS signed data
			return Promise.reject(`Wrong format of timeStampToken: ${this.timeStampToken.contentType}`);
		//endregion
 
		//region Verify internal signed data value
		const signed = new SignedData({ schema: this.timeStampToken.content });
 
		return signed.verify(verificationParameters);
		//endregion
	}
	//**********************************************************************************
}
//**************************************************************************************