AltName.ts (3214B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { EMPTY_STRING } from "./constants"; 4 import { AsnError } from "./errors"; 5 import { GeneralName, GeneralNameJson } from "./GeneralName"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 import * as Schema from "./Schema"; 8 9 const ALT_NAMES = "altNames"; 10 const CLEAR_PROPS = [ 11 ALT_NAMES 12 ]; 13 14 export interface IAltName { 15 /** 16 * Array of alternative names in GeneralName type 17 */ 18 altNames: GeneralName[]; 19 } 20 21 export type AltNameParameters = PkiObjectParameters & Partial<IAltName>; 22 23 export interface AltNameJson { 24 altNames: GeneralNameJson[]; 25 } 26 27 /** 28 * Represents the AltName structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5280) 29 */ 30 export class AltName extends PkiObject implements IAltName { 31 32 public static override CLASS_NAME = "AltName"; 33 34 public altNames!: GeneralName[]; 35 36 /** 37 * Initializes a new instance of the {@link AltName} class 38 * @param parameters Initialization parameters 39 */ 40 constructor(parameters: AltNameParameters = {}) { 41 super(); 42 43 this.altNames = pvutils.getParametersValue(parameters, ALT_NAMES, AltName.defaultValues(ALT_NAMES)); 44 45 if (parameters.schema) { 46 this.fromSchema(parameters.schema); 47 } 48 } 49 50 /** 51 * Returns default values for all class members 52 * @param memberName String name for a class member 53 * @returns Default value 54 */ 55 public static override defaultValues(memberName: typeof ALT_NAMES): GeneralName[]; 56 public static override defaultValues(memberName: string): any { 57 switch (memberName) { 58 case ALT_NAMES: 59 return []; 60 default: 61 return super.defaultValues(memberName); 62 } 63 } 64 65 /** 66 * @inheritdoc 67 * @asn ASN.1 schema 68 * ```asn 69 * AltName ::= GeneralNames 70 *``` 71 */ 72 public static override schema(parameters: Schema.SchemaParameters<{ altNames?: string; }> = {}): Schema.SchemaType { 73 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 74 75 return (new asn1js.Sequence({ 76 name: (names.blockName || EMPTY_STRING), 77 value: [ 78 new asn1js.Repeated({ 79 name: (names.altNames || EMPTY_STRING), 80 value: GeneralName.schema() 81 }) 82 ] 83 })); 84 } 85 86 public fromSchema(schema: Schema.SchemaType): void { 87 // Clear input data first 88 pvutils.clearProps(schema, CLEAR_PROPS); 89 90 // Check the schema is valid 91 const asn1 = asn1js.compareSchema(schema, 92 schema, 93 AltName.schema({ 94 names: { 95 altNames: ALT_NAMES 96 } 97 }) 98 ); 99 AsnError.assertSchema(asn1, this.className); 100 101 // Get internal properties from parsed schema 102 if (ALT_NAMES in asn1.result) { 103 this.altNames = Array.from(asn1.result.altNames, element => new GeneralName({ schema: element })); 104 } 105 } 106 107 public toSchema(): asn1js.Sequence { 108 //#region Construct and return new ASN.1 schema for this object 109 return (new asn1js.Sequence({ 110 value: Array.from(this.altNames, o => o.toSchema()) 111 })); 112 //#endregion 113 } 114 115 public toJSON(): AltNameJson { 116 return { 117 altNames: Array.from(this.altNames, o => o.toJSON()) 118 }; 119 } 120 121 }