PKIStatusInfo.ts (6345B)
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 * as Schema from "./Schema"; 7 8 const STATUS = "status"; 9 const STATUS_STRINGS = "statusStrings"; 10 const FAIL_INFO = "failInfo"; 11 const CLEAR_PROPS = [ 12 STATUS, 13 STATUS_STRINGS, 14 FAIL_INFO 15 ]; 16 17 export interface IPKIStatusInfo { 18 status: PKIStatus; 19 statusStrings?: asn1js.Utf8String[]; 20 failInfo?: asn1js.BitString; 21 } 22 23 export interface PKIStatusInfoJson { 24 status: PKIStatus; 25 statusStrings?: asn1js.Utf8StringJson[]; 26 failInfo?: asn1js.BitStringJson; 27 } 28 29 export type PKIStatusInfoParameters = PkiObjectParameters & Partial<IPKIStatusInfo>; 30 31 export type PKIStatusInfoSchema = Schema.SchemaParameters<{ 32 status?: string; 33 statusStrings?: string; 34 failInfo?: string; 35 }>; 36 37 export enum PKIStatus { 38 granted = 0, 39 grantedWithMods = 1, 40 rejection = 2, 41 waiting = 3, 42 revocationWarning = 4, 43 revocationNotification = 5, 44 } 45 46 /** 47 * Represents the PKIStatusInfo structure described in [RFC3161](https://www.ietf.org/rfc/rfc3161.txt) 48 */ 49 export class PKIStatusInfo extends PkiObject implements IPKIStatusInfo { 50 51 public static override CLASS_NAME = "PKIStatusInfo"; 52 53 public status!: PKIStatus; 54 public statusStrings?: asn1js.Utf8String[]; 55 public failInfo?: asn1js.BitString; 56 57 /** 58 * Initializes a new instance of the {@link PBKDF2Params} class 59 * @param parameters Initialization parameters 60 */ 61 constructor(parameters: PKIStatusInfoParameters = {}) { 62 super(); 63 64 this.status = pvutils.getParametersValue(parameters, STATUS, PKIStatusInfo.defaultValues(STATUS)); 65 if (STATUS_STRINGS in parameters) { 66 this.statusStrings = pvutils.getParametersValue(parameters, STATUS_STRINGS, PKIStatusInfo.defaultValues(STATUS_STRINGS)); 67 } 68 if (FAIL_INFO in parameters) { 69 this.failInfo = pvutils.getParametersValue(parameters, FAIL_INFO, PKIStatusInfo.defaultValues(FAIL_INFO)); 70 } 71 72 if (parameters.schema) { 73 this.fromSchema(parameters.schema); 74 } 75 } 76 77 /** 78 * Returns default values for all class members 79 * @param memberName String name for a class member 80 * @returns Default value 81 */ 82 public static override defaultValues(memberName: typeof STATUS): number; 83 public static override defaultValues(memberName: typeof STATUS_STRINGS): asn1js.Utf8String[]; 84 public static override defaultValues(memberName: typeof FAIL_INFO): asn1js.BitString; 85 public static override defaultValues(memberName: string): any { 86 switch (memberName) { 87 case STATUS: 88 return 2; 89 case STATUS_STRINGS: 90 return []; 91 case FAIL_INFO: 92 return new asn1js.BitString(); 93 default: 94 return super.defaultValues(memberName); 95 } 96 } 97 98 /** 99 * Compare values with default values for all class members 100 * @param memberName String name for a class member 101 * @param memberValue Value to compare with default value 102 */ 103 public static compareWithDefault(memberName: string, memberValue: any): boolean { 104 switch (memberName) { 105 case STATUS: 106 return (memberValue === PKIStatusInfo.defaultValues(memberName)); 107 case STATUS_STRINGS: 108 return (memberValue.length === 0); 109 case FAIL_INFO: 110 return (memberValue.isEqual(PKIStatusInfo.defaultValues(memberName))); 111 default: 112 return super.defaultValues(memberName); 113 } 114 } 115 116 /** 117 * @inheritdoc 118 * @asn ASN.1 schema 119 * ```asn 120 * PKIStatusInfo ::= SEQUENCE { 121 * status PKIStatus, 122 * statusString PKIFreeText OPTIONAL, 123 * failInfo PKIFailureInfo OPTIONAL } 124 *``` 125 */ 126 public static override schema(parameters: PKIStatusInfoSchema = {}): Schema.SchemaType { 127 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 128 129 return (new asn1js.Sequence({ 130 name: (names.blockName || EMPTY_STRING), 131 value: [ 132 new asn1js.Integer({ name: (names.status || EMPTY_STRING) }), 133 new asn1js.Sequence({ 134 optional: true, 135 value: [ 136 new asn1js.Repeated({ 137 name: (names.statusStrings || EMPTY_STRING), 138 value: new asn1js.Utf8String() 139 }) 140 ] 141 }), 142 new asn1js.BitString({ 143 name: (names.failInfo || EMPTY_STRING), 144 optional: true 145 }) 146 ] 147 })); 148 } 149 150 public fromSchema(schema: Schema.SchemaType): void { 151 // Clear input data first 152 pvutils.clearProps(schema, CLEAR_PROPS); 153 154 // Check the schema is valid 155 const asn1 = asn1js.compareSchema(schema, 156 schema, 157 PKIStatusInfo.schema({ 158 names: { 159 status: STATUS, 160 statusStrings: STATUS_STRINGS, 161 failInfo: FAIL_INFO 162 } 163 }) 164 ); 165 AsnError.assertSchema(asn1, this.className); 166 167 //#region Get internal properties from parsed schema 168 const _status = asn1.result.status; 169 170 if ((_status.valueBlock.isHexOnly === true) || 171 (_status.valueBlock.valueDec < 0) || 172 (_status.valueBlock.valueDec > 5)) 173 throw new Error("PKIStatusInfo \"status\" has invalid value"); 174 175 this.status = _status.valueBlock.valueDec; 176 177 if (STATUS_STRINGS in asn1.result) 178 this.statusStrings = asn1.result.statusStrings; 179 if (FAIL_INFO in asn1.result) 180 this.failInfo = asn1.result.failInfo; 181 //#endregion 182 } 183 184 public toSchema(): asn1js.Sequence { 185 //#region Create array of output sequence 186 const outputArray = []; 187 188 outputArray.push(new asn1js.Integer({ value: this.status })); 189 190 if (this.statusStrings) { 191 outputArray.push(new asn1js.Sequence({ 192 optional: true, 193 value: this.statusStrings 194 })); 195 } 196 197 if (this.failInfo) { 198 outputArray.push(this.failInfo); 199 } 200 //#endregion 201 202 //#region Construct and return new ASN.1 schema for this object 203 return (new asn1js.Sequence({ 204 value: outputArray 205 })); 206 //#endregion 207 } 208 209 public toJSON(): PKIStatusInfoJson { 210 const res: PKIStatusInfoJson = { 211 status: this.status 212 }; 213 214 if (this.statusStrings) { 215 res.statusStrings = Array.from(this.statusStrings, o => o.toJSON()); 216 } 217 if (this.failInfo) { 218 res.failInfo = this.failInfo.toJSON(); 219 } 220 221 return res; 222 } 223 224 }