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