tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SafeBagValueFactory.ts (1797B)


      1 export type BagType = PrivateKeyInfo | PKCS8ShroudedKeyBag | CertBag | CRLBag | SecretBag | SafeContents;
      2 export type BagTypeJson = PrivateKeyInfoJson | JsonWebKey | PKCS8ShroudedKeyBagJson | CertBagJson | CRLBagJson | SecretBagJson | SafeContentsJson;
      3 
      4 export interface BagTypeConstructor<T extends BagType> {
      5  new(params: { schema: any; }): T;
      6 }
      7 
      8 export class SafeBagValueFactory {
      9  private static items?: Record<string, BagTypeConstructor<BagType>>;
     10 
     11  private static getItems(): Record<string, BagTypeConstructor<BagType>> {
     12    if (!this.items) {
     13      this.items = {};
     14 
     15      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.1", PrivateKeyInfo);
     16      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.2", PKCS8ShroudedKeyBag);
     17      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.3", CertBag);
     18      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.4", CRLBag);
     19      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.5", SecretBag);
     20      SafeBagValueFactory.register("1.2.840.113549.1.12.10.1.6", SafeContents);
     21    }
     22 
     23    return this.items;
     24  }
     25  public static register<T extends BagType = BagType>(id: string, type: BagTypeConstructor<T>): void {
     26    this.getItems()[id] = type;
     27  }
     28 
     29  public static find(id: string): BagTypeConstructor<BagType> | null {
     30    return this.getItems()[id] || null;
     31  }
     32 
     33 }
     34 
     35 //! NOTE Bag type must be imported after the SafeBagValueFactory declaration
     36 import { CertBag, CertBagJson } from "./CertBag";
     37 import { CRLBag, CRLBagJson } from "./CRLBag";
     38 import { PKCS8ShroudedKeyBag, PKCS8ShroudedKeyBagJson } from "./PKCS8ShroudedKeyBag";
     39 import { PrivateKeyInfo, PrivateKeyInfoJson } from "./PrivateKeyInfo";
     40 import { SafeContents, SafeContentsJson } from "./SafeContents";
     41 import { SecretBag, SecretBagJson } from "./SecretBag";