OriginatorPublicKey.ts (4710B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier"; 4 import { EMPTY_STRING } from "./constants"; 5 import { AsnError } from "./errors"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 import * as Schema from "./Schema"; 8 9 const ALGORITHM = "algorithm"; 10 const PUBLIC_KEY = "publicKey"; 11 const CLEAR_PROPS = [ 12 ALGORITHM, 13 PUBLIC_KEY 14 ]; 15 16 export interface IOriginatorPublicKey { 17 algorithm: AlgorithmIdentifier; 18 publicKey: asn1js.BitString; 19 } 20 21 export interface OriginatorPublicKeyJson { 22 algorithm: AlgorithmIdentifierJson; 23 publicKey: asn1js.BitStringJson; 24 } 25 26 export type OriginatorPublicKeyParameters = PkiObjectParameters & Partial<IOriginatorPublicKey>; 27 28 /** 29 * Represents the OriginatorPublicKey structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652) 30 */ 31 export class OriginatorPublicKey extends PkiObject implements IOriginatorPublicKey { 32 33 public static override CLASS_NAME = "OriginatorPublicKey"; 34 35 public algorithm!: AlgorithmIdentifier; 36 public publicKey!: asn1js.BitString; 37 38 /** 39 * Initializes a new instance of the {@link OriginatorPublicKey} class 40 * @param parameters Initialization parameters 41 */ 42 constructor(parameters: OriginatorPublicKeyParameters = {}) { 43 super(); 44 45 this.algorithm = pvutils.getParametersValue(parameters, ALGORITHM, OriginatorPublicKey.defaultValues(ALGORITHM)); 46 this.publicKey = pvutils.getParametersValue(parameters, PUBLIC_KEY, OriginatorPublicKey.defaultValues(PUBLIC_KEY)); 47 48 if (parameters.schema) { 49 this.fromSchema(parameters.schema); 50 } 51 } 52 53 /** 54 * Returns default values for all class members 55 * @param memberName String name for a class member 56 * @returns Default value 57 */ 58 public static override defaultValues(memberName: typeof ALGORITHM): AlgorithmIdentifier; 59 public static override defaultValues(memberName: typeof PUBLIC_KEY): asn1js.BitString; 60 public static override defaultValues(memberName: string): any; 61 public static override defaultValues(memberName: string): any { 62 switch (memberName) { 63 case ALGORITHM: 64 return new AlgorithmIdentifier(); 65 case PUBLIC_KEY: 66 return new asn1js.BitString(); 67 default: 68 return super.defaultValues(memberName); 69 } 70 } 71 72 /** 73 * Compare values with default values for all class members 74 * @param memberName String name for a class member 75 * @param memberValue Value to compare with default value 76 */ 77 public static compareWithDefault<T extends { isEqual(data: any): boolean; }>(memberName: string, memberValue: T): memberValue is T { 78 switch (memberName) { 79 case ALGORITHM: 80 case PUBLIC_KEY: 81 return (memberValue.isEqual(OriginatorPublicKey.defaultValues(memberName))); 82 default: 83 return super.defaultValues(memberName); 84 } 85 } 86 87 /** 88 * @inheritdoc 89 * @asn ASN.1 schema 90 * ```asn 91 * OriginatorPublicKey ::= SEQUENCE { 92 * algorithm AlgorithmIdentifier, 93 * publicKey BIT STRING } 94 *``` 95 */ 96 static override schema(parameters: Schema.SchemaParameters<{ 97 algorithm?: AlgorithmIdentifierSchema; 98 publicKey?: string; 99 }> = {}): Schema.SchemaType { 100 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 101 102 return (new asn1js.Sequence({ 103 name: (names.blockName || EMPTY_STRING), 104 value: [ 105 AlgorithmIdentifier.schema(names.algorithm || {}), 106 new asn1js.BitString({ name: (names.publicKey || EMPTY_STRING) }) 107 ] 108 })); 109 } 110 111 public fromSchema(schema: Schema.SchemaType): void { 112 // Clear input data first 113 pvutils.clearProps(schema, CLEAR_PROPS); 114 115 // Check the schema is valid 116 const asn1 = asn1js.compareSchema(schema, 117 schema, 118 OriginatorPublicKey.schema({ 119 names: { 120 algorithm: { 121 names: { 122 blockName: ALGORITHM 123 } 124 }, 125 publicKey: PUBLIC_KEY 126 } 127 }) 128 ); 129 AsnError.assertSchema(asn1, this.className); 130 131 // Get internal properties from parsed schema 132 this.algorithm = new AlgorithmIdentifier({ schema: asn1.result.algorithm }); 133 this.publicKey = asn1.result.publicKey; 134 } 135 136 public toSchema(): asn1js.Sequence { 137 //#region Construct and return new ASN.1 schema for this object 138 return (new asn1js.Sequence({ 139 value: [ 140 this.algorithm.toSchema(), 141 this.publicKey 142 ] 143 })); 144 //#endregion 145 } 146 147 public toJSON(): OriginatorPublicKeyJson { 148 return { 149 algorithm: this.algorithm.toJSON(), 150 publicKey: this.publicKey.toJSON(), 151 }; 152 } 153 154 }