OtherRevocationInfoFormat.ts (4125B)
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 { PkiObject, PkiObjectParameters } from "./PkiObject"; 6 import * as Schema from "./Schema"; 7 8 const OTHER_REV_INFO_FORMAT = "otherRevInfoFormat"; 9 const OTHER_REV_INFO = "otherRevInfo"; 10 const CLEAR_PROPS = [ 11 OTHER_REV_INFO_FORMAT, 12 OTHER_REV_INFO 13 ]; 14 15 export interface IOtherRevocationInfoFormat { 16 otherRevInfoFormat: string; 17 otherRevInfo: any; 18 } 19 20 export interface OtherRevocationInfoFormatJson { 21 otherRevInfoFormat: string; 22 otherRevInfo?: any; 23 } 24 25 export type OtherRevocationInfoFormatParameters = PkiObjectParameters & Partial<IOtherRevocationInfoFormat>; 26 27 /** 28 * Represents the OtherRevocationInfoFormat structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652) 29 */ 30 export class OtherRevocationInfoFormat extends PkiObject implements IOtherRevocationInfoFormat { 31 32 public static override CLASS_NAME = "OtherRevocationInfoFormat"; 33 34 public otherRevInfoFormat!: string; 35 public otherRevInfo: any; 36 37 /** 38 * Initializes a new instance of the {@link OtherRevocationInfoFormat} class 39 * @param parameters Initialization parameters 40 */ 41 constructor(parameters: OtherRevocationInfoFormatParameters = {}) { 42 super(); 43 44 this.otherRevInfoFormat = pvutils.getParametersValue(parameters, OTHER_REV_INFO_FORMAT, OtherRevocationInfoFormat.defaultValues(OTHER_REV_INFO_FORMAT)); 45 this.otherRevInfo = pvutils.getParametersValue(parameters, OTHER_REV_INFO, OtherRevocationInfoFormat.defaultValues(OTHER_REV_INFO)); 46 47 if (parameters.schema) { 48 this.fromSchema(parameters.schema); 49 } 50 } 51 52 /** 53 * Returns default values for all class members 54 * @param memberName String name for a class member 55 * @returns Default value 56 */ 57 static override defaultValues(memberName: typeof OTHER_REV_INFO_FORMAT): string; 58 static override defaultValues(memberName: typeof OTHER_REV_INFO): any; 59 static override defaultValues(memberName: string): any { 60 switch (memberName) { 61 case OTHER_REV_INFO_FORMAT: 62 return EMPTY_STRING; 63 case OTHER_REV_INFO: 64 return new asn1js.Any(); 65 default: 66 return super.defaultValues(memberName); 67 } 68 } 69 70 /** 71 * @inheritdoc 72 * @asn ASN.1 schema 73 * ```asn 74 * OtherCertificateFormat ::= SEQUENCE { 75 * otherRevInfoFormat OBJECT IDENTIFIER, 76 * otherRevInfo ANY DEFINED BY otherCertFormat } 77 *``` 78 */ 79 public static override schema(parameters: Schema.SchemaParameters<{ 80 otherRevInfoFormat?: string; 81 otherRevInfo?: string; 82 }> = {}): Schema.SchemaType { 83 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 84 85 return (new asn1js.Sequence({ 86 name: (names.blockName || EMPTY_STRING), 87 value: [ 88 new asn1js.ObjectIdentifier({ name: (names.otherRevInfoFormat || OTHER_REV_INFO_FORMAT) }), 89 new asn1js.Any({ name: (names.otherRevInfo || OTHER_REV_INFO) }) 90 ] 91 })); 92 } 93 94 public fromSchema(schema: Schema.SchemaType): void { 95 // Clear input data first 96 pvutils.clearProps(schema, CLEAR_PROPS); 97 98 // Check the schema is valid 99 const asn1 = asn1js.compareSchema(schema, 100 schema, 101 OtherRevocationInfoFormat.schema() 102 ); 103 AsnError.assertSchema(asn1, this.className); 104 105 // Get internal properties from parsed schema 106 this.otherRevInfoFormat = asn1.result.otherRevInfoFormat.valueBlock.toString(); 107 this.otherRevInfo = asn1.result.otherRevInfo; 108 } 109 110 public toSchema(): asn1js.Sequence { 111 // Construct and return new ASN.1 schema for this object 112 return (new asn1js.Sequence({ 113 value: [ 114 new asn1js.ObjectIdentifier({ value: this.otherRevInfoFormat }), 115 this.otherRevInfo 116 ] 117 })); 118 } 119 120 public toJSON(): OtherRevocationInfoFormatJson { 121 const res: OtherRevocationInfoFormatJson = { 122 otherRevInfoFormat: this.otherRevInfoFormat 123 }; 124 125 if (!(this.otherRevInfo instanceof asn1js.Any)) { 126 res.otherRevInfo = this.otherRevInfo.toJSON(); 127 } 128 129 return res; 130 } 131 132 }