import * as asn1js from "asn1js"; import * as pvutils from "pvutils"; import { Attribute, AttributeJson } from "./Attribute"; import * as Schema from "./Schema"; import { PkiObject, PkiObjectParameters } from "./PkiObject"; import { AsnError } from "./errors";
/** * Represents the SafeBag structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
*/
export class SafeBag<T extends BagType = BagType> extends PkiObject implements ISafeBag<T> {
publicstatic override CLASS_NAME = "SafeBag";
public bagId!: string; public bagValue!: T; public bagAttributes?: Attribute[];
/** * Initializes a new instance of the {@link SafeBag} class * @param parameters Initialization parameters
*/
constructor(parameters: SafeBagParameters<T> = {}) { super();
this.bagId = pvutils.getParametersValue(parameters, BAG_ID, SafeBag.defaultValues(BAG_ID)); this.bagValue = pvutils.getParametersValue(parameters, BAG_VALUE, SafeBag.defaultValues(BAG_VALUE)) as unknown as T; if (BAG_ATTRIBUTES in parameters) { this.bagAttributes = pvutils.getParametersValue(parameters, BAG_ATTRIBUTES, SafeBag.defaultValues(BAG_ATTRIBUTES));
}
if (parameters.schema) { this.fromSchema(parameters.schema);
}
}
/** * Returns default values for all class members * @param memberName String name for a class member * @returns Default value
*/ publicstatic override defaultValues(memberName: typeof BAG_ID): string; publicstatic override defaultValues(memberName: typeof BAG_VALUE): BagType; publicstatic override defaultValues(memberName: typeof BAG_ATTRIBUTES): Attribute[]; publicstatic override defaultValues(memberName: string): any { switch (memberName) { case BAG_ID: return EMPTY_STRING; case BAG_VALUE: return (new asn1js.Any()); case BAG_ATTRIBUTES: return []; default: returnsuper.defaultValues(memberName);
}
}
/** * Compare values with default values for all class members * @param memberName String name for a class member * @param memberValue Value to compare with default value
*/ publicstatic compareWithDefault(memberName: string, memberValue: any): boolean { switch (memberName) { case BAG_ID: return (memberValue === EMPTY_STRING); case BAG_VALUE: return (memberValue instanceof asn1js.Any); case BAG_ATTRIBUTES: return (memberValue.length === 0); default: returnsuper.defaultValues(memberName);
}
}
//#region Get internal properties from parsed schema this.bagId = asn1.result.bagId.valueBlock.toString();
const bagType = SafeBagValueFactory.find(this.bagId); if (!bagType) { thrownew Error(`Invalid BAG_ID for SafeBag: ${this.bagId}`);
} this.bagValue = new bagType({ schema: asn1.result.bagValue }) as unknown as T;
if (BAG_ATTRIBUTES in asn1.result) { this.bagAttributes = Array.from(asn1.result.bagAttributes, element => new Attribute({ schema: element }));
} //#endregion
}
public toSchema(): asn1js.Sequence { //#region Construct and return new ASN.1 schema for this object const outputArray = [ new asn1js.ObjectIdentifier({ value: this.bagId }), new asn1js.Constructed({
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0// [0]
},
value: [this.bagValue.toSchema()]
})
];
if (this.bagAttributes) {
outputArray.push(new asn1js.Set({
value: Array.from(this.bagAttributes, o => o.toSchema())
}));
}
return (new asn1js.Sequence({
value: outputArray
})); //#endregion
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.