ObjectDigestInfo.ts (6601B)
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 DIGESTED_OBJECT_TYPE = "digestedObjectType"; 10 const OTHER_OBJECT_TYPE_ID = "otherObjectTypeID"; 11 const DIGEST_ALGORITHM = "digestAlgorithm"; 12 const OBJECT_DIGEST = "objectDigest"; 13 const CLEAR_PROPS = [ 14 DIGESTED_OBJECT_TYPE, 15 OTHER_OBJECT_TYPE_ID, 16 DIGEST_ALGORITHM, 17 OBJECT_DIGEST, 18 ]; 19 20 export interface IObjectDigestInfo { 21 digestedObjectType: asn1js.Enumerated; 22 otherObjectTypeID?: asn1js.ObjectIdentifier; 23 digestAlgorithm: AlgorithmIdentifier; 24 objectDigest: asn1js.BitString; 25 } 26 27 export type ObjectDigestInfoParameters = PkiObjectParameters & Partial<IObjectDigestInfo>; 28 29 export interface ObjectDigestInfoJson { 30 digestedObjectType: asn1js.EnumeratedJson; 31 otherObjectTypeID?: asn1js.ObjectIdentifierJson; 32 digestAlgorithm: AlgorithmIdentifierJson; 33 objectDigest: asn1js.BitStringJson; 34 } 35 36 /** 37 * Represents the ObjectDigestInfo structure described in [RFC5755](https://datatracker.ietf.org/doc/html/rfc5755) 38 */ 39 export class ObjectDigestInfo extends PkiObject implements IObjectDigestInfo { 40 41 public static override CLASS_NAME = "ObjectDigestInfo"; 42 43 public digestedObjectType!: asn1js.Enumerated; 44 public otherObjectTypeID?: asn1js.ObjectIdentifier; 45 public digestAlgorithm!: AlgorithmIdentifier; 46 public objectDigest!: asn1js.BitString; 47 48 /** 49 * Initializes a new instance of the {@link ObjectDigestInfo} class 50 * @param parameters Initialization parameters 51 */ 52 constructor(parameters: ObjectDigestInfoParameters = {}) { 53 super(); 54 55 this.digestedObjectType = pvutils.getParametersValue(parameters, DIGESTED_OBJECT_TYPE, ObjectDigestInfo.defaultValues(DIGESTED_OBJECT_TYPE)); 56 if (OTHER_OBJECT_TYPE_ID in parameters) { 57 this.otherObjectTypeID = pvutils.getParametersValue(parameters, OTHER_OBJECT_TYPE_ID, ObjectDigestInfo.defaultValues(OTHER_OBJECT_TYPE_ID)); 58 } 59 this.digestAlgorithm = pvutils.getParametersValue(parameters, DIGEST_ALGORITHM, ObjectDigestInfo.defaultValues(DIGEST_ALGORITHM)); 60 this.objectDigest = pvutils.getParametersValue(parameters, OBJECT_DIGEST, ObjectDigestInfo.defaultValues(OBJECT_DIGEST)); 61 62 if (parameters.schema) { 63 this.fromSchema(parameters.schema); 64 } 65 } 66 67 /** 68 * Returns default values for all class members 69 * @param memberName String name for a class member 70 * @returns Default value 71 */ 72 public static override defaultValues(memberName: typeof DIGESTED_OBJECT_TYPE): asn1js.Enumerated; 73 public static override defaultValues(memberName: typeof OTHER_OBJECT_TYPE_ID): asn1js.ObjectIdentifier; 74 public static override defaultValues(memberName: typeof DIGEST_ALGORITHM): AlgorithmIdentifier; 75 public static override defaultValues(memberName: typeof OBJECT_DIGEST): asn1js.BitString; 76 public static override defaultValues(memberName: string): any { 77 switch (memberName) { 78 case DIGESTED_OBJECT_TYPE: 79 return new asn1js.Enumerated(); 80 case OTHER_OBJECT_TYPE_ID: 81 return new asn1js.ObjectIdentifier(); 82 case DIGEST_ALGORITHM: 83 return new AlgorithmIdentifier(); 84 case OBJECT_DIGEST: 85 return new asn1js.BitString(); 86 default: 87 return super.defaultValues(memberName); 88 } 89 } 90 91 /** 92 * @inheritdoc 93 * @asn ASN.1 schema 94 * ```asn 95 * ObjectDigestInfo ::= SEQUENCE { 96 * digestedObjectType ENUMERATED { 97 * publicKey (0), 98 * publicKeyCert (1), 99 * otherObjectTypes (2) }, 100 * -- otherObjectTypes MUST NOT 101 * -- be used in this profile 102 * otherObjectTypeID OBJECT IDENTIFIER OPTIONAL, 103 * digestAlgorithm AlgorithmIdentifier, 104 * objectDigest BIT STRING 105 * } 106 *``` 107 */ 108 public static override schema(parameters: Schema.SchemaParameters<{ 109 digestedObjectType?: string; 110 otherObjectTypeID?: string; 111 digestAlgorithm?: AlgorithmIdentifierSchema; 112 objectDigest?: string; 113 }> = {}): Schema.SchemaType { 114 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 115 116 return (new asn1js.Sequence({ 117 name: (names.blockName || EMPTY_STRING), 118 value: [ 119 new asn1js.Enumerated({ name: (names.digestedObjectType || EMPTY_STRING) }), 120 new asn1js.ObjectIdentifier({ 121 optional: true, 122 name: (names.otherObjectTypeID || EMPTY_STRING) 123 }), 124 AlgorithmIdentifier.schema(names.digestAlgorithm || {}), 125 new asn1js.BitString({ name: (names.objectDigest || EMPTY_STRING) }), 126 ] 127 })); 128 } 129 130 public fromSchema(schema: Schema.SchemaType): void { 131 // Clear input data first 132 pvutils.clearProps(schema, CLEAR_PROPS); 133 134 // Check the schema is valid 135 const asn1 = asn1js.compareSchema(schema, 136 schema, 137 ObjectDigestInfo.schema({ 138 names: { 139 digestedObjectType: DIGESTED_OBJECT_TYPE, 140 otherObjectTypeID: OTHER_OBJECT_TYPE_ID, 141 digestAlgorithm: { 142 names: { 143 blockName: DIGEST_ALGORITHM 144 } 145 }, 146 objectDigest: OBJECT_DIGEST 147 } 148 }) 149 ); 150 AsnError.assertSchema(asn1, this.className); 151 152 //#region Get internal properties from parsed schema 153 this.digestedObjectType = asn1.result.digestedObjectType; 154 155 if (OTHER_OBJECT_TYPE_ID in asn1.result) { 156 this.otherObjectTypeID = asn1.result.otherObjectTypeID; 157 } 158 159 this.digestAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.digestAlgorithm }); 160 this.objectDigest = asn1.result.objectDigest; 161 //#endregion 162 } 163 164 public toSchema(): asn1js.Sequence { 165 const result = new asn1js.Sequence({ 166 value: [this.digestedObjectType] 167 }); 168 169 if (this.otherObjectTypeID) { 170 result.valueBlock.value.push(this.otherObjectTypeID); 171 } 172 173 result.valueBlock.value.push(this.digestAlgorithm.toSchema()); 174 result.valueBlock.value.push(this.objectDigest); 175 176 return result; 177 } 178 179 public toJSON(): ObjectDigestInfoJson { 180 const result: ObjectDigestInfoJson = { 181 digestedObjectType: this.digestedObjectType.toJSON(), 182 digestAlgorithm: this.digestAlgorithm.toJSON(), 183 objectDigest: this.objectDigest.toJSON(), 184 }; 185 186 if (this.otherObjectTypeID) { 187 result.otherObjectTypeID = this.otherObjectTypeID.toJSON(); 188 } 189 190 return result; 191 } 192 193 }