import * as asn1js from "asn1js"; import * as pvtsutils from "pvtsutils"; import * as pvutils from "pvutils"; import * as common from "./common"; import { MessageImprint, HASHED_MESSAGE, HASH_ALGORITHM, MessageImprintSchema, MessageImprintJson } from "./MessageImprint"; import { Accuracy, AccuracyJson, AccuracySchema, MICROS, MILLIS, SECONDS } from "./Accuracy"; import { GeneralName, GeneralNameJson, GeneralNameSchema, TYPE, VALUE } from "./GeneralName"; import { Extension, ExtensionJson, ExtensionSchema } from "./Extension"; import * as Schema from "./Schema"; import { PkiObject, PkiObjectParameters } from "./PkiObject"; import { AsnError } from "./errors"; import { EMPTY_STRING } from "./constants";
export interface ITSTInfo { /** * Version of the time-stamp token. * * Conforming time-stamping servers MUST be able to provide version 1 time-stamp tokens.
*/
version: number; /** * TSA's policy under which the response was produced. * * If a similar field was present in the TimeStampReq, then it MUST have the same value, * otherwise an error (unacceptedPolicy) MUST be returned
*/
policy: string; /** * The messageImprint MUST have the same value as the similar field in * TimeStampReq, provided that the size of the hash value matches the * expected size of the hash algorithm identified in hashAlgorithm.
*/
messageImprint: MessageImprint; /** * Integer assigned by the TSA to each TimeStampToken. * * It MUST be unique for each TimeStampToken issued by a given TSA.
*/
serialNumber: asn1js.Integer; /** * Time at which the time-stamp token has been created by the TSA
*/
genTime: Date; /** * Represents the time deviation around the UTC time contained in GeneralizedTime
*/
accuracy?: Accuracy; /** * If the ordering field is missing, or if the ordering field is present * and set to false, then the genTime field only indicates the time at * which the time-stamp token has been created by the TSA.In such a * case, the ordering of time-stamp tokens issued by the same TSA or * different TSAs is only possible when the difference between the * genTime of the first time-stamp token and the genTime of the second * time-stamp token is greater than the sum of the accuracies of the * genTime for each time-stamp token. * * If the ordering field is present and set to true, every time-stamp * token from the same TSA can always be ordered based on the genTime * field, regardless of the genTime accuracy.
*/
ordering?: boolean; /** * Field MUST be present if it was present in the TimeStampReq. * In such a case it MUST equal the value provided in the TimeStampReq structure.
*/
nonce?: asn1js.Integer; /** * `tsa` field is to give a hint in identifying the name of the TSA. * If present, it MUST correspond to one of the subject names included * in the certificate that is to be used to verify the token.
*/
tsa?: GeneralName; /** * Additional information in the future. Extensions is defined in [RFC2459](https://datatracker.ietf.org/doc/html/rfc2459)
*/
extensions?: Extension[];
}
/** * Represents the TSTInfo structure described in [RFC3161](https://www.ietf.org/rfc/rfc3161.txt)
*/
export class TSTInfo extends PkiObject implements ITSTInfo {
publicstatic override CLASS_NAME = "TSTInfo";
public version!: number; public policy!: string; public messageImprint!: MessageImprint; public serialNumber!: asn1js.Integer; public genTime!: Date; public accuracy?: Accuracy; public ordering?: boolean; public nonce?: asn1js.Integer; public tsa?: GeneralName; public extensions?: Extension[];
/** * Initializes a new instance of the {@link TSTInfo} class * @param parameters Initialization parameters
*/
constructor(parameters: TSTInfoParameters = {}) { super();
if (ACCURACY in parameters) { this.accuracy = pvutils.getParametersValue(parameters, ACCURACY, TSTInfo.defaultValues(ACCURACY));
}
if (ORDERING in parameters) { this.ordering = pvutils.getParametersValue(parameters, ORDERING, TSTInfo.defaultValues(ORDERING));
}
if (NONCE in parameters) { this.nonce = pvutils.getParametersValue(parameters, NONCE, TSTInfo.defaultValues(NONCE));
}
if (TSA in parameters) { this.tsa = pvutils.getParametersValue(parameters, TSA, TSTInfo.defaultValues(TSA));
}
if (EXTENSIONS in parameters) { this.extensions = pvutils.getParametersValue(parameters, EXTENSIONS, TSTInfo.defaultValues(EXTENSIONS));
}
if (parameters.schema) { this.fromSchema(parameters.schema);
}
}
/** * Returns default values for all class members * @param memberName String name for a class member * @returns Default value
*/ publicstatic override defaultValues(memberName: typeof VERSION): number; publicstatic override defaultValues(memberName: typeof POLICY): string; publicstatic override defaultValues(memberName: typeof MESSAGE_IMPRINT): MessageImprint; publicstatic override defaultValues(memberName: typeof SERIAL_NUMBER): asn1js.Integer; publicstatic override defaultValues(memberName: typeof GEN_TIME): Date; publicstatic override defaultValues(memberName: typeof ACCURACY): Accuracy; publicstatic override defaultValues(memberName: typeof ORDERING): boolean; publicstatic override defaultValues(memberName: typeof NONCE): asn1js.Integer; publicstatic override defaultValues(memberName: typeof TSA): GeneralName; publicstatic override defaultValues(memberName: typeof EXTENSIONS): Extension[]; publicstatic override defaultValues(memberName: string): any { switch (memberName) { case VERSION: return0; case POLICY: return EMPTY_STRING; case MESSAGE_IMPRINT: returnnew MessageImprint(); case SERIAL_NUMBER: returnnew asn1js.Integer(); case GEN_TIME: returnnew Date(0, 0, 0); case ACCURACY: returnnew Accuracy(); case ORDERING: returnfalse; case NONCE: returnnew asn1js.Integer(); case TSA: returnnew GeneralName(); case EXTENSIONS: return []; default: returnsuper.defaultValues(memberName);
}
}
/** * Compare values with default values for all class members * @param memberName String name for a class member * @param memberValue Value to compare with default value
*/ publicstatic compareWithDefault(memberName: string, memberValue: any): boolean { switch (memberName) { case VERSION: case POLICY: case GEN_TIME: case ORDERING: return (memberValue === TSTInfo.defaultValues(ORDERING)); case MESSAGE_IMPRINT: return ((MessageImprint.compareWithDefault(HASH_ALGORITHM, memberValue.hashAlgorithm)) &&
(MessageImprint.compareWithDefault(HASHED_MESSAGE, memberValue.hashedMessage))); case SERIAL_NUMBER: case NONCE: return (memberValue.isEqual(TSTInfo.defaultValues(NONCE))); case ACCURACY: return ((Accuracy.compareWithDefault(SECONDS, memberValue.seconds)) &&
(Accuracy.compareWithDefault(MILLIS, memberValue.millis)) &&
(Accuracy.compareWithDefault(MICROS, memberValue.micros))); case TSA: return ((GeneralName.compareWithDefault(TYPE, memberValue.type)) &&
(GeneralName.compareWithDefault(VALUE, memberValue.value))); case EXTENSIONS: return (memberValue.length === 0); default: returnsuper.defaultValues(memberName);
}
}
public fromSchema(schema: Schema.SchemaType): void { // Clear input data first
pvutils.clearProps(schema, CLEAR_PROPS);
// Check the schema is valid const asn1 = asn1js.compareSchema(schema,
schema,
TSTInfo.schema()
);
AsnError.assertSchema(asn1, this.className);
// Get internal properties from parsed schema this.version = asn1.result[TST_INFO_VERSION].valueBlock.valueDec; this.policy = asn1.result[TST_INFO_POLICY].valueBlock.toString(); this.messageImprint = new MessageImprint({ schema: asn1.result[TST_INFO_MESSAGE_IMPRINT] }); this.serialNumber = asn1.result[TST_INFO_SERIAL_NUMBER]; this.genTime = asn1.result[TST_INFO_GEN_TIME].toDate(); if (TST_INFO_ACCURACY in asn1.result) this.accuracy = new Accuracy({ schema: asn1.result[TST_INFO_ACCURACY] }); if (TST_INFO_ORDERING in asn1.result) this.ordering = asn1.result[TST_INFO_ORDERING].valueBlock.value; if (TST_INFO_NONCE in asn1.result) this.nonce = asn1.result[TST_INFO_NONCE]; if (TST_INFO_TSA in asn1.result) this.tsa = new GeneralName({ schema: asn1.result[TST_INFO_TSA] }); if (TST_INFO_EXTENSIONS in asn1.result) this.extensions = Array.from(asn1.result[TST_INFO_EXTENSIONS], element => new Extension({ schema: element }));
}
public toSchema(): asn1js.Sequence { //#region Create array for output sequence const outputArray = [];
if (this.accuracy)
res.accuracy = this.accuracy.toJSON();
if (this.ordering !== undefined)
res.ordering = this.ordering;
if (this.nonce)
res.nonce = this.nonce.toJSON();
if (this.tsa)
res.tsa = this.tsa.toJSON();
if (this.extensions)
res.extensions = Array.from(this.extensions, o => o.toJSON());
return res;
}
/** * Verify current TST Info value * @param params Input parameters * @param crypto Crypto engine
*/ public async verify(params: TSTInfoVerifyParams, crypto = common.getCrypto(true)): Promise<boolean> {
//#region Get initial parameters if (!params.data) { thrownew Error("\"data\" is a mandatory attribute for TST_INFO verification");
} const data = params.data; //#endregion
//#region Check date if (params.notBefore) { if (this.genTime < params.notBefore) thrownew Error("Generation time for TSTInfo object is less than notBefore value");
}
if (params.notAfter) { if (this.genTime > params.notAfter) thrownew Error("Generation time for TSTInfo object is more than notAfter value");
} //#endregion
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.