RecipientEncryptedKeys.ts (4137B)
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 { RecipientEncryptedKey, RecipientEncryptedKeyJson } from "./RecipientEncryptedKey"; 7 import * as Schema from "./Schema"; 8 9 const ENCRYPTED_KEYS = "encryptedKeys"; 10 const RECIPIENT_ENCRYPTED_KEYS = "RecipientEncryptedKeys"; 11 const CLEAR_PROPS = [ 12 RECIPIENT_ENCRYPTED_KEYS, 13 ]; 14 15 export interface IRecipientEncryptedKeys { 16 encryptedKeys: RecipientEncryptedKey[]; 17 } 18 19 export interface RecipientEncryptedKeysJson { 20 encryptedKeys: RecipientEncryptedKeyJson[]; 21 } 22 23 export type RecipientEncryptedKeysParameters = PkiObjectParameters & Partial<IRecipientEncryptedKeys>; 24 25 export type RecipientEncryptedKeysSchema = Schema.SchemaParameters<{ 26 RecipientEncryptedKeys?: string; 27 }>; 28 29 /** 30 * Represents the RecipientEncryptedKeys structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652) 31 */ 32 export class RecipientEncryptedKeys extends PkiObject implements IRecipientEncryptedKeys { 33 34 public static override CLASS_NAME = "RecipientEncryptedKeys"; 35 36 public encryptedKeys!: RecipientEncryptedKey[]; 37 38 /** 39 * Initializes a new instance of the {@link RecipientEncryptedKeys} class 40 * @param parameters Initialization parameters 41 */ 42 constructor(parameters: RecipientEncryptedKeysParameters = {}) { 43 super(); 44 45 this.encryptedKeys = pvutils.getParametersValue(parameters, ENCRYPTED_KEYS, RecipientEncryptedKeys.defaultValues(ENCRYPTED_KEYS)); 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 public static override defaultValues(memberName: typeof ENCRYPTED_KEYS): RecipientEncryptedKey[]; 58 public static override defaultValues(memberName: string): any { 59 switch (memberName) { 60 case ENCRYPTED_KEYS: 61 return []; 62 default: 63 return super.defaultValues(memberName); 64 } 65 } 66 67 /** 68 * Compare values with default values for all class members 69 * @param memberName String name for a class member 70 * @param memberValue Value to compare with default value 71 */ 72 public static compareWithDefault(memberName: string, memberValue: any): boolean { 73 switch (memberName) { 74 case ENCRYPTED_KEYS: 75 return (memberValue.length === 0); 76 default: 77 return super.defaultValues(memberName); 78 } 79 } 80 81 /** 82 * @inheritdoc 83 * @asn ASN.1 schema 84 * ```asn 85 * RecipientEncryptedKeys ::= SEQUENCE OF RecipientEncryptedKey 86 *``` 87 */ 88 public static override schema(parameters: RecipientEncryptedKeysSchema = {}): Schema.SchemaType { 89 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 90 91 return (new asn1js.Sequence({ 92 name: (names.blockName || EMPTY_STRING), 93 value: [ 94 new asn1js.Repeated({ 95 name: (names.RecipientEncryptedKeys || EMPTY_STRING), 96 value: RecipientEncryptedKey.schema() 97 }) 98 ] 99 })); 100 } 101 102 public fromSchema(schema: Schema.SchemaType): void { 103 // Clear input data first 104 pvutils.clearProps(schema, CLEAR_PROPS); 105 106 // Check the schema is valid 107 const asn1 = asn1js.compareSchema(schema, 108 schema, 109 RecipientEncryptedKeys.schema({ 110 names: { 111 RecipientEncryptedKeys: RECIPIENT_ENCRYPTED_KEYS 112 } 113 }) 114 ); 115 AsnError.assertSchema(asn1, this.className); 116 117 // Get internal properties from parsed schema 118 this.encryptedKeys = Array.from(asn1.result.RecipientEncryptedKeys, element => new RecipientEncryptedKey({ schema: element })); 119 } 120 121 public toSchema(): asn1js.Sequence { 122 // Construct and return new ASN.1 schema for this object 123 return (new asn1js.Sequence({ 124 value: Array.from(this.encryptedKeys, o => o.toSchema()) 125 })); 126 } 127 128 public toJSON(): RecipientEncryptedKeysJson { 129 return { 130 encryptedKeys: Array.from(this.encryptedKeys, o => o.toJSON()) 131 }; 132 } 133 134 }