PBES2Params.ts (4454B)
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 KEY_DERIVATION_FUNC = "keyDerivationFunc"; 10 const ENCRYPTION_SCHEME = "encryptionScheme"; 11 const CLEAR_PROPS = [ 12 KEY_DERIVATION_FUNC, 13 ENCRYPTION_SCHEME 14 ]; 15 16 export interface IPBES2Params { 17 keyDerivationFunc: AlgorithmIdentifier; 18 encryptionScheme: AlgorithmIdentifier; 19 } 20 21 export interface PBES2ParamsJson { 22 keyDerivationFunc: AlgorithmIdentifierJson; 23 encryptionScheme: AlgorithmIdentifierJson; 24 } 25 26 export type PBES2ParamsParameters = PkiObjectParameters & Partial<IPBES2Params>; 27 28 /** 29 * Represents the PBES2Params structure described in [RFC2898](https://www.ietf.org/rfc/rfc2898.txt) 30 */ 31 export class PBES2Params extends PkiObject implements IPBES2Params { 32 33 public static override CLASS_NAME = "PBES2Params"; 34 35 public keyDerivationFunc!: AlgorithmIdentifier; 36 public encryptionScheme!: AlgorithmIdentifier; 37 38 /** 39 * Initializes a new instance of the {@link PBES2Params} class 40 * @param parameters Initialization parameters 41 */ 42 constructor(parameters: PBES2ParamsParameters = {}) { 43 super(); 44 45 this.keyDerivationFunc = pvutils.getParametersValue(parameters, KEY_DERIVATION_FUNC, PBES2Params.defaultValues(KEY_DERIVATION_FUNC)); 46 this.encryptionScheme = pvutils.getParametersValue(parameters, ENCRYPTION_SCHEME, PBES2Params.defaultValues(ENCRYPTION_SCHEME)); 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 KEY_DERIVATION_FUNC): AlgorithmIdentifier; 59 public static override defaultValues(memberName: typeof ENCRYPTION_SCHEME): AlgorithmIdentifier; 60 public static override defaultValues(memberName: string): any { 61 switch (memberName) { 62 case KEY_DERIVATION_FUNC: 63 return new AlgorithmIdentifier(); 64 case ENCRYPTION_SCHEME: 65 return new AlgorithmIdentifier(); 66 default: 67 return super.defaultValues(memberName); 68 } 69 } 70 71 /** 72 * @inheritdoc 73 * @asn ASN.1 schema 74 * ```asn 75 * PBES2-params ::= SEQUENCE { 76 * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}}, 77 * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}} } 78 *``` 79 */ 80 public static override schema(parameters: Schema.SchemaParameters<{ 81 keyDerivationFunc?: AlgorithmIdentifierSchema; 82 encryptionScheme?: AlgorithmIdentifierSchema; 83 }> = {}): Schema.SchemaType { 84 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 85 86 return (new asn1js.Sequence({ 87 name: (names.blockName || EMPTY_STRING), 88 value: [ 89 AlgorithmIdentifier.schema(names.keyDerivationFunc || {}), 90 AlgorithmIdentifier.schema(names.encryptionScheme || {}) 91 ] 92 })); 93 } 94 95 public fromSchema(schema: Schema.SchemaType): void { 96 // Clear input data first 97 pvutils.clearProps(schema, CLEAR_PROPS); 98 99 // Check the schema is valid 100 const asn1 = asn1js.compareSchema(schema, 101 schema, 102 PBES2Params.schema({ 103 names: { 104 keyDerivationFunc: { 105 names: { 106 blockName: KEY_DERIVATION_FUNC 107 } 108 }, 109 encryptionScheme: { 110 names: { 111 blockName: ENCRYPTION_SCHEME 112 } 113 } 114 } 115 }) 116 ); 117 AsnError.assertSchema(asn1, this.className); 118 119 // Get internal properties from parsed schema 120 this.keyDerivationFunc = new AlgorithmIdentifier({ schema: asn1.result.keyDerivationFunc }); 121 this.encryptionScheme = new AlgorithmIdentifier({ schema: asn1.result.encryptionScheme }); 122 } 123 124 public toSchema(): asn1js.Sequence { 125 // Construct and return new ASN.1 schema for this object 126 return (new asn1js.Sequence({ 127 value: [ 128 this.keyDerivationFunc.toSchema(), 129 this.encryptionScheme.toSchema() 130 ] 131 })); 132 } 133 134 public toJSON(): PBES2ParamsJson { 135 return { 136 keyDerivationFunc: this.keyDerivationFunc.toJSON(), 137 encryptionScheme: this.encryptionScheme.toJSON() 138 }; 139 } 140 141 }