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