ECCCMSSharedInfo.ts (6489B)
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_INFO = "keyInfo"; 10 const ENTITY_U_INFO = "entityUInfo"; 11 const SUPP_PUB_INFO = "suppPubInfo"; 12 const CLEAR_PROPS = [ 13 KEY_INFO, 14 ENTITY_U_INFO, 15 SUPP_PUB_INFO 16 ]; 17 18 export interface IECCCMSSharedInfo { 19 keyInfo: AlgorithmIdentifier; 20 entityUInfo?: asn1js.OctetString; 21 suppPubInfo: asn1js.OctetString; 22 } 23 24 export interface ECCCMSSharedInfoJson { 25 keyInfo: AlgorithmIdentifierJson; 26 entityUInfo?: asn1js.OctetStringJson; 27 suppPubInfo: asn1js.OctetStringJson; 28 } 29 30 export type ECCCMSSharedInfoParameters = PkiObjectParameters & Partial<IECCCMSSharedInfo>; 31 32 /** 33 * Represents the ECCCMSSharedInfo structure described in [RFC6318](https://datatracker.ietf.org/doc/html/rfc6318) 34 */ 35 export class ECCCMSSharedInfo extends PkiObject implements IECCCMSSharedInfo { 36 37 public static override CLASS_NAME = "ECCCMSSharedInfo"; 38 39 public keyInfo!: AlgorithmIdentifier; 40 public entityUInfo?: asn1js.OctetString; 41 public suppPubInfo!: asn1js.OctetString; 42 43 /** 44 * Initializes a new instance of the {@link ECCCMSSharedInfo} class 45 * @param parameters Initialization parameters 46 */ 47 constructor(parameters: ECCCMSSharedInfoParameters = {}) { 48 super(); 49 50 this.keyInfo = pvutils.getParametersValue(parameters, KEY_INFO, ECCCMSSharedInfo.defaultValues(KEY_INFO)); 51 if (ENTITY_U_INFO in parameters) { 52 this.entityUInfo = pvutils.getParametersValue(parameters, ENTITY_U_INFO, ECCCMSSharedInfo.defaultValues(ENTITY_U_INFO)); 53 } 54 this.suppPubInfo = pvutils.getParametersValue(parameters, SUPP_PUB_INFO, ECCCMSSharedInfo.defaultValues(SUPP_PUB_INFO)); 55 56 if (parameters.schema) { 57 this.fromSchema(parameters.schema); 58 } 59 } 60 61 /** 62 * Returns default values for all class members 63 * @param memberName String name for a class member 64 * @returns Default value 65 */ 66 public static override defaultValues(memberName: typeof KEY_INFO): AlgorithmIdentifier; 67 public static override defaultValues(memberName: typeof ENTITY_U_INFO): asn1js.OctetString; 68 public static override defaultValues(memberName: typeof SUPP_PUB_INFO): asn1js.OctetString; 69 public static override defaultValues(memberName: string): any { 70 switch (memberName) { 71 case KEY_INFO: 72 return new AlgorithmIdentifier(); 73 case ENTITY_U_INFO: 74 return new asn1js.OctetString(); 75 case SUPP_PUB_INFO: 76 return new asn1js.OctetString(); 77 default: 78 return super.defaultValues(memberName); 79 } 80 } 81 82 /** 83 * Compare values with default values for all class members 84 * @param memberName String name for a class member 85 * @param memberValue Value to compare with default value 86 */ 87 public static compareWithDefault(memberName: string, memberValue: any): boolean { 88 switch (memberName) { 89 case KEY_INFO: 90 case ENTITY_U_INFO: 91 case SUPP_PUB_INFO: 92 return (memberValue.isEqual(ECCCMSSharedInfo.defaultValues(memberName as typeof SUPP_PUB_INFO))); 93 default: 94 return super.defaultValues(memberName); 95 } 96 } 97 98 /** 99 * @inheritdoc 100 * @asn ASN.1 schema 101 * ```asn 102 * ECC-CMS-SharedInfo ::= SEQUENCE { 103 * keyInfo AlgorithmIdentifier, 104 * entityUInfo [0] EXPLICIT OCTET STRING OPTIONAL, 105 * suppPubInfo [2] EXPLICIT OCTET STRING } 106 *``` 107 */ 108 public static override schema(parameters: Schema.SchemaParameters<{ 109 keyInfo?: AlgorithmIdentifierSchema; 110 entityUInfo?: string; 111 suppPubInfo?: string; 112 }> = {}): Schema.SchemaType { 113 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 114 115 return (new asn1js.Sequence({ 116 name: (names.blockName || EMPTY_STRING), 117 value: [ 118 AlgorithmIdentifier.schema(names.keyInfo || {}), 119 new asn1js.Constructed({ 120 name: (names.entityUInfo || EMPTY_STRING), 121 idBlock: { 122 tagClass: 3, // CONTEXT-SPECIFIC 123 tagNumber: 0 // [0] 124 }, 125 optional: true, 126 value: [new asn1js.OctetString()] 127 }), 128 new asn1js.Constructed({ 129 name: (names.suppPubInfo || EMPTY_STRING), 130 idBlock: { 131 tagClass: 3, // CONTEXT-SPECIFIC 132 tagNumber: 2 // [2] 133 }, 134 value: [new asn1js.OctetString()] 135 }) 136 ] 137 })); 138 } 139 140 public fromSchema(schema: Schema.SchemaType): void { 141 // Clear input data first 142 pvutils.clearProps(schema, CLEAR_PROPS); 143 144 // Check the schema is valid 145 const asn1 = asn1js.compareSchema(schema, 146 schema, 147 ECCCMSSharedInfo.schema({ 148 names: { 149 keyInfo: { 150 names: { 151 blockName: KEY_INFO 152 } 153 }, 154 entityUInfo: ENTITY_U_INFO, 155 suppPubInfo: SUPP_PUB_INFO 156 } 157 }) 158 ); 159 AsnError.assertSchema(asn1, this.className); 160 161 // Get internal properties from parsed schema 162 this.keyInfo = new AlgorithmIdentifier({ schema: asn1.result.keyInfo }); 163 if (ENTITY_U_INFO in asn1.result) 164 this.entityUInfo = asn1.result.entityUInfo.valueBlock.value[0]; 165 this.suppPubInfo = asn1.result.suppPubInfo.valueBlock.value[0]; 166 } 167 168 public toSchema(): asn1js.Sequence { 169 //#region Create output array for sequence 170 const outputArray = []; 171 172 outputArray.push(this.keyInfo.toSchema()); 173 174 if (this.entityUInfo) { 175 outputArray.push(new asn1js.Constructed({ 176 idBlock: { 177 tagClass: 3, // CONTEXT-SPECIFIC 178 tagNumber: 0 // [0] 179 }, 180 value: [this.entityUInfo] 181 })); 182 } 183 184 outputArray.push(new asn1js.Constructed({ 185 idBlock: { 186 tagClass: 3, // CONTEXT-SPECIFIC 187 tagNumber: 2 // [2] 188 }, 189 value: [this.suppPubInfo] 190 })); 191 //#endregion 192 193 //#region Construct and return new ASN.1 schema for this object 194 return new asn1js.Sequence({ 195 value: outputArray 196 }); 197 //#endregion 198 } 199 200 public toJSON(): ECCCMSSharedInfoJson { 201 const res: ECCCMSSharedInfoJson = { 202 keyInfo: this.keyInfo.toJSON(), 203 suppPubInfo: this.suppPubInfo.toJSON(), 204 }; 205 206 if (this.entityUInfo) { 207 res.entityUInfo = this.entityUInfo.toJSON(); 208 } 209 210 return res; 211 } 212 213 }