SignedCertificateTimestampList.ts (4611B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import * as bs from "bytestreamjs"; 4 import { SignedCertificateTimestamp, SignedCertificateTimestampJson } from "./SignedCertificateTimestamp"; 5 import * as Schema from "./Schema"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 8 const TIMESTAMPS = "timestamps"; 9 10 export interface ISignedCertificateTimestampList { 11 timestamps: SignedCertificateTimestamp[]; 12 } 13 14 export interface SignedCertificateTimestampListJson { 15 timestamps: SignedCertificateTimestampJson[]; 16 } 17 18 export type SignedCertificateTimestampListParameters = PkiObjectParameters & Partial<ISignedCertificateTimestampList>; 19 20 /** 21 * Represents the SignedCertificateTimestampList structure described in [RFC6962](https://datatracker.ietf.org/doc/html/rfc6962) 22 */ 23 export class SignedCertificateTimestampList extends PkiObject implements ISignedCertificateTimestampList { 24 25 public static override CLASS_NAME = "SignedCertificateTimestampList"; 26 27 public timestamps!: SignedCertificateTimestamp[]; 28 29 /** 30 * Initializes a new instance of the {@link SignedCertificateTimestampList} class 31 * @param parameters Initialization parameters 32 */ 33 constructor(parameters: SignedCertificateTimestampListParameters = {}) { 34 super(); 35 36 this.timestamps = pvutils.getParametersValue(parameters, TIMESTAMPS, SignedCertificateTimestampList.defaultValues(TIMESTAMPS)); 37 38 if (parameters.schema) { 39 this.fromSchema(parameters.schema); 40 } 41 } 42 43 /** 44 * Returns default values for all class members 45 * @param memberName String name for a class member 46 * @returns Default value 47 */ 48 public static override defaultValues(memberName: typeof TIMESTAMPS): SignedCertificateTimestamp[]; 49 public static override defaultValues(memberName: string): any { 50 switch (memberName) { 51 case TIMESTAMPS: 52 return []; 53 default: 54 return super.defaultValues(memberName); 55 } 56 } 57 58 /** 59 * Compare values with default values for all class members 60 * @param memberName String name for a class member 61 * @param memberValue Value to compare with default value 62 */ 63 public static compareWithDefault(memberName: string, memberValue: any): boolean { 64 switch (memberName) { 65 case TIMESTAMPS: 66 return (memberValue.length === 0); 67 default: 68 return super.defaultValues(memberName); 69 } 70 } 71 72 /** 73 * @inheritdoc 74 * @asn ASN.1 schema 75 * ```asn 76 * SignedCertificateTimestampList ::= OCTET STRING 77 *``` 78 */ 79 public static override schema(parameters: Schema.SchemaParameters = {}): Schema.SchemaType { 80 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 81 82 names.optional ??= false; 83 84 return (new asn1js.OctetString({ 85 name: (names.blockName || "SignedCertificateTimestampList"), 86 optional: names.optional 87 })); 88 } 89 90 public fromSchema(schema: Schema.SchemaType): void { 91 //#region Check the schema is valid 92 if ((schema instanceof asn1js.OctetString) === false) { 93 throw new Error("Object's schema was not verified against input data for SignedCertificateTimestampList"); 94 } 95 //#endregion 96 //#region Get internal properties from parsed schema 97 const seqStream = new bs.SeqStream({ 98 stream: new bs.ByteStream({ 99 buffer: schema.valueBlock.valueHex 100 }) 101 }); 102 103 const dataLength = seqStream.getUint16(); 104 if (dataLength !== seqStream.length) { 105 throw new Error("Object's schema was not verified against input data for SignedCertificateTimestampList"); 106 } 107 108 while (seqStream.length) { 109 this.timestamps.push(new SignedCertificateTimestamp({ stream: seqStream })); 110 } 111 //#endregion 112 } 113 114 public toSchema(): asn1js.OctetString { 115 //#region Initial variables 116 const stream = new bs.SeqStream(); 117 118 let overallLength = 0; 119 120 const timestampsData = []; 121 //#endregion 122 //#region Get overall length 123 for (const timestamp of this.timestamps) { 124 const timestampStream = timestamp.toStream(); 125 timestampsData.push(timestampStream); 126 overallLength += timestampStream.stream.buffer.byteLength; 127 } 128 //#endregion 129 stream.appendUint16(overallLength); 130 131 //#region Set data from all timestamps 132 for (const timestamp of timestampsData) { 133 stream.appendView(timestamp.stream.view); 134 } 135 //#endregion 136 return new asn1js.OctetString({ valueHex: stream.stream.buffer.slice(0) }); 137 } 138 139 public toJSON(): SignedCertificateTimestampListJson { 140 return { 141 timestamps: Array.from(this.timestamps, o => o.toJSON()) 142 }; 143 } 144 145 }