index.d.ts 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*!
  2. * MIT License
  3. *
  4. * Copyright (c) Peculiar Ventures. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. */
  25. import * as asn1X509 from '@peculiar/asn1-x509';
  26. import { Extension as Extension$1, SubjectPublicKeyInfo, Attribute as Attribute$1, AlgorithmIdentifier, Name as Name$1, Certificate, RevokedCertificate, CertificateList, Version } from '@peculiar/asn1-x509';
  27. import * as asn1Rsa from '@peculiar/asn1-rsa';
  28. import { CertificationRequest } from '@peculiar/asn1-csr';
  29. import { BufferSource as BufferSource$1 } from 'pvtsutils';
  30. interface TextObjectConvertible {
  31. /**
  32. * Returns the object in textual representation
  33. */
  34. toTextObject(): TextObject;
  35. }
  36. type TextObjectItemType = string | number | boolean | Date | BufferSource | TextObject | TextObject[] | TextObjectConvertible;
  37. declare const NAME: unique symbol;
  38. declare const VALUE: unique symbol;
  39. declare class TextObject {
  40. static NAME: typeof NAME;
  41. static VALUE: typeof VALUE;
  42. [key: string | symbol]: TextObjectItemType;
  43. [NAME]: string;
  44. [VALUE]: string;
  45. constructor(name: string, items?: Record<string, TextObjectItemType>, value?: string);
  46. }
  47. interface AlgorithmSerializer {
  48. toTextObject(alg: asn1X509.AlgorithmIdentifier): TextObject;
  49. }
  50. declare abstract class DefaultAlgorithmSerializer {
  51. static toTextObject(alg: asn1X509.AlgorithmIdentifier): TextObject;
  52. }
  53. declare abstract class OidSerializer {
  54. static items: Record<string, string>;
  55. static toString(oid: string): string;
  56. }
  57. declare abstract class TextConverter {
  58. static oidSerializer: typeof OidSerializer;
  59. static algorithmSerializer: AlgorithmSerializer;
  60. static serialize(obj: TextObject): string;
  61. private static pad;
  62. private static serializeObj;
  63. private static serializeBufferSource;
  64. static serializeAlgorithm(alg: asn1X509.AlgorithmIdentifier): TextObject;
  65. }
  66. type AsnDataStringFormat = "asn" | "text" | "hex" | "base64" | "base64url";
  67. /**
  68. * Represents an ASN.1 data
  69. */
  70. declare abstract class AsnData<T> implements TextObjectConvertible {
  71. #private;
  72. static NAME: string;
  73. /**
  74. * Gets a DER encoded buffer
  75. */
  76. get rawData(): ArrayBuffer;
  77. /**
  78. * ASN.1 object
  79. */
  80. protected readonly asn: T;
  81. /**
  82. * Creates a new instance
  83. * @param raw DER encoded buffer
  84. * @param type ASN.1 convertible class for `@peculiar/asn1-schema` schema
  85. */
  86. constructor(raw: BufferSource, type: new () => T);
  87. /**
  88. * ASN.1 object
  89. * @param asn
  90. */
  91. constructor(asn: T);
  92. /**
  93. * Occurs on instance initialization
  94. * @param asn ASN.1 object
  95. */
  96. protected abstract onInit(asn: T): void;
  97. /**
  98. * Returns `true` if ASN.1 data is equal to another ASN.1 data, otherwise `false`
  99. * @param data Any data
  100. */
  101. equal(data: any): data is this;
  102. toString(format?: AsnDataStringFormat): string;
  103. protected getTextName(): string;
  104. toTextObject(): TextObject;
  105. protected toTextObjectEmpty(value?: string): TextObject;
  106. }
  107. /**
  108. * Represents the certificate extension
  109. */
  110. declare class Extension extends AsnData<Extension$1> {
  111. /**
  112. * Gets an extension identifier
  113. */
  114. type: string;
  115. /**
  116. * Indicates where extension is critical
  117. */
  118. critical: boolean;
  119. /**
  120. * Gets a DER encoded value of extension
  121. */
  122. value: ArrayBuffer;
  123. /**
  124. * Creates a new instance from DER encoded Buffer
  125. * @param raw DER encoded buffer
  126. */
  127. constructor(raw: BufferSource);
  128. /**
  129. * Creates a new instance
  130. * @param type Extension identifier
  131. * @param critical Indicates where extension is critical
  132. * @param value DER encoded value of extension
  133. */
  134. constructor(type: string, critical: boolean, value: BufferSource);
  135. protected onInit(asn: Extension$1): void;
  136. toTextObject(): TextObject;
  137. toTextObjectWithoutValue(): TextObject;
  138. }
  139. interface JsonGeneralName {
  140. type: GeneralNameType;
  141. value: string;
  142. }
  143. declare const DNS = "dns";
  144. declare const DN = "dn";
  145. declare const EMAIL = "email";
  146. declare const IP = "ip";
  147. declare const URL = "url";
  148. declare const GUID = "guid";
  149. declare const UPN = "upn";
  150. declare const REGISTERED_ID = "id";
  151. type GeneralNameType = typeof DNS | typeof DN | typeof EMAIL | typeof GUID | typeof IP | typeof URL | typeof UPN | typeof REGISTERED_ID;
  152. /**
  153. * Represents ASN.1 type of GeneralName.
  154. *
  155. * This class doesn't support no standard string format is
  156. * defined for otherName, X.400 name, EDI party name, or any other type of names.
  157. */
  158. declare class GeneralName extends AsnData<asn1X509.GeneralName> {
  159. /**
  160. * Type of the storing value
  161. */
  162. type: GeneralNameType;
  163. /**
  164. * Text representation of ASN.1 GeneralName
  165. */
  166. value: string;
  167. constructor(type: GeneralNameType, value: string);
  168. constructor(asn: asn1X509.GeneralName);
  169. constructor(raw: BufferSource);
  170. /**
  171. * Occurs on instance initialization
  172. * @param asn
  173. *
  174. * @throws Throws error if ASN.1 GeneralName contains
  175. * unsupported value (eg otherName, X400 address, EDI party name)
  176. */
  177. protected onInit(asn: asn1X509.GeneralName): void;
  178. toJSON(): JsonGeneralName;
  179. toTextObject(): TextObject;
  180. }
  181. type JsonGeneralNames = JsonGeneralName[];
  182. declare class GeneralNames extends AsnData<asn1X509.GeneralNames> {
  183. static NAME: string;
  184. items: readonly GeneralName[];
  185. constructor(json: JsonGeneralNames);
  186. constructor(asn: asn1X509.GeneralNames | asn1X509.GeneralName[]);
  187. constructor(raw: BufferSource);
  188. protected onInit(asn: asn1X509.GeneralNames): void;
  189. toJSON(): JsonGeneralNames;
  190. toTextObject(): TextObject;
  191. }
  192. type AsnExportType = "pem" | AsnDataStringFormat;
  193. type AsnEncodedType = BufferSource | string;
  194. declare abstract class PemData<T> extends AsnData<T> {
  195. static isAsnEncoded(data: any): data is AsnEncodedType;
  196. /**
  197. * Converts encoded raw to ArrayBuffer. Supported formats are HEX, DER, Base64, Base64Url, PEM
  198. * @param raw Encoded data
  199. */
  200. static toArrayBuffer(raw: BufferSource | string): ArrayBuffer;
  201. /**
  202. * PEM tag
  203. */
  204. protected abstract readonly tag: string;
  205. /**
  206. * Creates a new instance
  207. * @param raw Encoded buffer (DER, PEM, HEX, Base64, Base64Url)
  208. * @param type ASN.1 convertible class for `@peculiar/asn1-schema` schema
  209. */
  210. constructor(raw: AsnEncodedType, type: new () => T);
  211. /**
  212. * Creates a new instance
  213. * @param asn ASN.1 object
  214. */
  215. constructor(asn: T);
  216. /**
  217. * Returns encoded object in PEM format
  218. */
  219. toString(): string;
  220. /**
  221. * Returns encoded object in selected format
  222. * @param format hex, base64, base64url, pem, asn, text
  223. */
  224. toString(format: AsnExportType): string;
  225. }
  226. interface IPublicKeyContainer {
  227. publicKey: PublicKey;
  228. }
  229. /**
  230. * Public key type. Represents a public key in different formats.
  231. */
  232. type PublicKeyType = PublicKey | CryptoKey | IPublicKeyContainer | BufferSource;
  233. /**
  234. * Representation of Subject Public Key Info
  235. */
  236. declare class PublicKey extends PemData<SubjectPublicKeyInfo> {
  237. /**
  238. * Creates a new instance from a public key data
  239. * @param data Public key data
  240. * @param crypto Crypto provider. Default is from CryptoProvider
  241. */
  242. static create(data: PublicKeyType, crypto?: Crypto): Promise<PublicKey>;
  243. protected readonly tag: string;
  244. /**
  245. * Gets a key algorithm
  246. */
  247. algorithm: Algorithm;
  248. /**
  249. * Creates a new instance from ASN.1
  250. * @param asn ASN.1 object
  251. */
  252. constructor(asn: SubjectPublicKeyInfo);
  253. /**
  254. * Creates a new instance
  255. * @param raw Encoded buffer (DER, PEM, HEX, Base64, Base64Url)
  256. */
  257. constructor(raw: AsnEncodedType);
  258. /**
  259. * Returns a public CryptoKey
  260. * @param crypto Crypto provider. Default is from CryptoProvider
  261. */
  262. export(crypto?: Crypto): Promise<CryptoKey>;
  263. /**
  264. * Returns a public CryptoKey with specified parameters
  265. * @param algorithm Algorithm
  266. * @param keyUsages A list of key usages
  267. * @param crypto Crypto provider. Default is from CryptoProvider
  268. */
  269. export(algorithm: Algorithm | EcKeyImportParams | RsaHashedImportParams, keyUsages: KeyUsage[], crypto?: Crypto): Promise<CryptoKey>;
  270. protected onInit(asn: SubjectPublicKeyInfo): void;
  271. /**
  272. * Returns a SHA-1 public key thumbprint
  273. * @param crypto Crypto provider. Default is from CryptoProvider
  274. */
  275. getThumbprint(crypto?: Crypto): Promise<ArrayBuffer>;
  276. /**
  277. * Returns a public key thumbprint for specified mechanism
  278. * @param algorithm Hash algorithm
  279. * @param crypto Crypto provider. Default is from CryptoProvider
  280. */
  281. getThumbprint(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
  282. /**
  283. * Returns Subject Key Identifier as specified in {@link https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.2 RFC5280}
  284. * @param crypto Crypto provider. Default is from CryptoProvider
  285. */
  286. getKeyIdentifier(crypto?: Crypto): Promise<ArrayBuffer>;
  287. /**
  288. * Returns Subject Key Identifier for specified algorithm
  289. * @param algorithm Hash algorithm
  290. * @param crypto Crypto provider. Default is from CryptoProvider
  291. */
  292. getKeyIdentifier(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
  293. toTextObject(): TextObject;
  294. }
  295. interface CertificateIdentifier {
  296. /**
  297. * Name
  298. */
  299. name: asn1X509.GeneralName[] | asn1X509.GeneralNames | GeneralNames;
  300. /**
  301. * Hexadecimal string
  302. */
  303. serialNumber: string;
  304. }
  305. /**
  306. * Represents the Authority Key Identifier certificate extension
  307. */
  308. declare class AuthorityKeyIdentifierExtension extends Extension {
  309. static NAME: string;
  310. /**
  311. * Creates authority key identifier extension from certificate identifier
  312. * @param certId Certificate identifier
  313. * @param critical Indicates where extension is critical. Default is `false`
  314. * @param crypto WebCrypto provider. Default is from CryptoProvider
  315. */
  316. static create(certId: CertificateIdentifier, critical?: boolean, crypto?: Crypto): Promise<AuthorityKeyIdentifierExtension>;
  317. /**
  318. * Creates authority key identifier extension from public key data
  319. * @param publicKey Public key data
  320. * @param critical Indicates where extension is critical. Default is `false`
  321. * @param crypto WebCrypto provider. Default is from CryptoProvider
  322. */
  323. static create(publicKey: PublicKeyType, critical?: boolean, crypto?: Crypto): Promise<AuthorityKeyIdentifierExtension>;
  324. /**
  325. * Gets a hexadecimal representation of key identifier
  326. */
  327. keyId?: string;
  328. /**
  329. * Gets a certificate identifier in the issuer name and serial number
  330. */
  331. certId?: CertificateIdentifier;
  332. /**
  333. * Creates a new instance from DER encoded buffer
  334. * @param raw DER encoded buffer
  335. */
  336. constructor(raw: BufferSource);
  337. /**
  338. * Creates a new instance
  339. * @param identifier Hexadecimal representation of key identifier
  340. * @param critical Indicates where extension is critical. Default is `false`
  341. */
  342. constructor(identifier: string, critical?: boolean);
  343. /**
  344. * Creates a new instance
  345. * @param id Certificate identifier in the issuer name and serial number
  346. * @param critical Indicates where extension is critical. Default is `false`
  347. */
  348. constructor(id: CertificateIdentifier, critical?: boolean);
  349. protected onInit(asn: asn1X509.Extension): void;
  350. toTextObject(): TextObject;
  351. }
  352. /**
  353. * Represents the Basic Constraints certificate extension
  354. */
  355. declare class BasicConstraintsExtension extends Extension {
  356. static NAME: string;
  357. /**
  358. * Indicates whether the certified public key may be used
  359. * to verify certificate signatures
  360. */
  361. readonly ca: boolean;
  362. /**
  363. * Gets a maximum number of non-self-issued intermediate certificates that may
  364. * follow this certificate in a valid certification path
  365. */
  366. readonly pathLength?: number;
  367. /**
  368. * Creates a new instance from DER encoded buffer
  369. * @param raw DER encoded buffer
  370. */
  371. constructor(raw: BufferSource);
  372. /**
  373. * Creates a new instance
  374. * @param ca
  375. * @param pathLength
  376. * @param critical
  377. */
  378. constructor(ca: boolean, pathLength?: number, critical?: boolean);
  379. toTextObject(): TextObject;
  380. }
  381. declare enum ExtendedKeyUsage {
  382. serverAuth = "1.3.6.1.5.5.7.3.1",
  383. clientAuth = "1.3.6.1.5.5.7.3.2",
  384. codeSigning = "1.3.6.1.5.5.7.3.3",
  385. emailProtection = "1.3.6.1.5.5.7.3.4",
  386. timeStamping = "1.3.6.1.5.5.7.3.8",
  387. ocspSigning = "1.3.6.1.5.5.7.3.9"
  388. }
  389. type ExtendedKeyUsageType = asn1X509.ExtendedKeyUsage | string;
  390. /**
  391. * Represents the Extended Key Usage certificate extension
  392. */
  393. declare class ExtendedKeyUsageExtension extends Extension {
  394. static NAME: string;
  395. /**
  396. * Gets a list of purposes for which the certified public key may be used
  397. */
  398. readonly usages: ExtendedKeyUsageType[];
  399. /**
  400. * Creates a new instance from DER encoded buffer
  401. * @param raw DER encoded buffer
  402. */
  403. constructor(raw: BufferSource);
  404. /**
  405. * Creates a new instance
  406. * @param usages
  407. * @param critical
  408. */
  409. constructor(usages: ExtendedKeyUsageType[], critical?: boolean);
  410. toTextObject(): TextObject;
  411. }
  412. /**
  413. * X509 key usages flags
  414. */
  415. declare enum KeyUsageFlags {
  416. digitalSignature = 1,
  417. nonRepudiation = 2,
  418. keyEncipherment = 4,
  419. dataEncipherment = 8,
  420. keyAgreement = 16,
  421. keyCertSign = 32,
  422. cRLSign = 64,
  423. encipherOnly = 128,
  424. decipherOnly = 256
  425. }
  426. /**
  427. * Represents the Key Usage certificate extension
  428. */
  429. declare class KeyUsagesExtension extends Extension {
  430. static NAME: string;
  431. /**
  432. * Gets a key usages flag
  433. */
  434. readonly usages: KeyUsageFlags;
  435. /**
  436. * Creates a new instance from DER encoded buffer
  437. * @param raw DER encoded buffer
  438. */
  439. constructor(raw: BufferSource);
  440. /**
  441. * Creates a new instance
  442. * @param usages
  443. * @param critical
  444. */
  445. constructor(usages: KeyUsageFlags, critical?: boolean);
  446. toTextObject(): TextObject;
  447. }
  448. /**
  449. * Represents the Subject Key Identifier certificate extension
  450. */
  451. declare class SubjectKeyIdentifierExtension extends Extension {
  452. static NAME: string;
  453. /**
  454. * Creates subject key identifier extension from public key data
  455. * @param publicKey Public key data
  456. * @param critical Indicates where extension is critical. Default is `false`
  457. * @param crypto WebCrypto provider. Default is from CryptoProvider
  458. */
  459. static create(publicKey: PublicKeyType, critical?: boolean, crypto?: Crypto): Promise<SubjectKeyIdentifierExtension>;
  460. /**
  461. * Gets hexadecimal representation of key identifier
  462. */
  463. readonly keyId: string;
  464. /**
  465. * Creates a new instance from DER encoded buffer
  466. * @param raw DER encoded buffer
  467. */
  468. constructor(raw: BufferSource);
  469. /**
  470. * Creates a new instance
  471. * @param keyId Hexadecimal representation of key identifier
  472. * @param critical Indicates where extension is critical. Default is `false`
  473. */
  474. constructor(keyId: string, critical?: boolean);
  475. toTextObject(): TextObject;
  476. }
  477. /**
  478. * Represents the Subject Alternative Name certificate extension
  479. */
  480. declare class SubjectAlternativeNameExtension extends Extension {
  481. names: GeneralNames;
  482. static NAME: string;
  483. /**
  484. * Creates a new instance from DER encoded buffer
  485. * @param raw DER encoded buffer
  486. */
  487. constructor(raw: BufferSource);
  488. /**
  489. * Creates a new instance
  490. * @param data JSON representation of SAN
  491. * @param critical Indicates where extension is critical. Default is `false`
  492. */
  493. constructor(data?: JsonGeneralNames, critical?: boolean);
  494. onInit(asn: asn1X509.Extension): void;
  495. toTextObject(): TextObject;
  496. }
  497. /**
  498. * Represents the Certificate Policy extension
  499. */
  500. declare class CertificatePolicyExtension extends Extension {
  501. static NAME: string;
  502. /**
  503. * Gets the list of certificate policies
  504. */
  505. readonly policies: readonly string[];
  506. /**
  507. * Creates a new instance from DER encoded buffer
  508. * @param raw DER encoded buffer
  509. */
  510. constructor(raw: BufferSource);
  511. /**
  512. * Creates a new instance
  513. * @param policies
  514. * @param critical
  515. */
  516. constructor(policies: string[], critical?: boolean);
  517. toTextObject(): TextObject;
  518. }
  519. /**
  520. * Represents the CRL Distribution Points extension
  521. */
  522. declare class CRLDistributionPointsExtension extends Extension {
  523. static NAME: string;
  524. distributionPoints: asn1X509.DistributionPoint[];
  525. /**
  526. * Creates a new instance from DER encoded buffer
  527. * @param raw DER encoded buffer
  528. */
  529. constructor(raw: BufferSource);
  530. /**
  531. * Creates a new instance
  532. * @param value The value of the extension
  533. * @param critical Indicates whether the extension is critical. Default is `false`
  534. */
  535. constructor(value: asn1X509.DistributionPoint[], critical?: boolean);
  536. /**
  537. * Creates a new instance from an array of URLs
  538. * @param urls An array of URLs to be used as distribution points.
  539. * @param critical Indicates whether the extension is critical. Default is `false`
  540. */
  541. constructor(urls: string[], critical?: boolean);
  542. protected onInit(asn: asn1X509.Extension): void;
  543. toTextObject(): TextObject;
  544. }
  545. type AccessItemTypes = GeneralName | GeneralName[] | string | string[];
  546. interface AuthorityInfoAccessParams {
  547. ocsp?: AccessItemTypes;
  548. caIssuers?: AccessItemTypes;
  549. timeStamping?: AccessItemTypes;
  550. caRepository?: AccessItemTypes;
  551. }
  552. /**
  553. * Represents the Authority Info Access certificate extension
  554. */
  555. declare class AuthorityInfoAccessExtension extends Extension {
  556. static NAME: string;
  557. ocsp: GeneralName[];
  558. caIssuers: GeneralName[];
  559. timeStamping: GeneralName[];
  560. caRepository: GeneralName[];
  561. /**
  562. * Creates a new instance from DER encoded buffer
  563. * @param raw DER encoded buffer
  564. */
  565. constructor(raw: BufferSource);
  566. /**
  567. * Creates a new instance
  568. * @param value The value of the extension
  569. * @param critical Indicates whether the extension is critical. Default is `false`
  570. */
  571. constructor(value: asn1X509.AuthorityInfoAccessSyntax, critical?: boolean);
  572. /**
  573. * Creates a new instance
  574. * @param params The value of the extension
  575. * @param critical Indicates whether the extension is critical. Default is `false`
  576. */
  577. constructor(params: AuthorityInfoAccessParams, critical?: boolean);
  578. protected onInit(asn: asn1X509.Extension): void;
  579. toTextObject(): TextObject;
  580. }
  581. /**
  582. * Represents the Issuer Alternative Name certificate extension
  583. */
  584. declare class IssuerAlternativeNameExtension extends Extension {
  585. names: GeneralNames;
  586. static NAME: string;
  587. /**
  588. * Creates a new instance from DER encoded buffer
  589. * @param raw DER encoded buffer
  590. */
  591. constructor(raw: BufferSource);
  592. /**
  593. * Creates a new instance
  594. * @param data JSON representation of IAN
  595. * @param critical Indicates where extension is critical. Default is `false`
  596. */
  597. constructor(data?: JsonGeneralNames, critical?: boolean);
  598. onInit(asn: asn1X509.Extension): void;
  599. toTextObject(): TextObject;
  600. }
  601. /**
  602. * Static class to manage X509 extensions
  603. */
  604. declare class ExtensionFactory {
  605. private static items;
  606. /**
  607. * Registers a new X509 Extension class. If id already exists replaces it
  608. * @param id Extension identifier
  609. * @param type Extension class
  610. *
  611. * @example
  612. * ```js
  613. * ExtensionFactory.register(
  614. * asnX509.id_ce_basicConstraints,
  615. * extensions.BasicConstraintsExtension,
  616. * );
  617. * ```
  618. */
  619. static register(id: string, type: any): void;
  620. /**
  621. * Returns X509 Extension based on it's identifier
  622. * @param data DER encoded buffer
  623. *
  624. * @example
  625. * ```js
  626. * const ext = ExtensionFactory.create(asnExtRaw);
  627. * ```
  628. */
  629. static create(data: BufferSource): Extension;
  630. }
  631. /**
  632. * Represents the Attribute structure
  633. */
  634. declare class Attribute extends AsnData<Attribute$1> {
  635. static NAME: string;
  636. /**
  637. * Gets an attribute identifier
  638. */
  639. type: string;
  640. /**
  641. * Gets a list of DER encoded attribute values
  642. */
  643. values: ArrayBuffer[];
  644. /**
  645. * Crates a new instance
  646. * @param type Attribute identifier
  647. * @param values List of DER encoded attribute values
  648. */
  649. constructor(type: string, values?: BufferSource[]);
  650. /**
  651. * Crates a new instance from DER encoded buffer
  652. * @param raw DER encoded buffer
  653. */
  654. constructor(raw: BufferSource);
  655. protected onInit(asn: Attribute$1): void;
  656. toTextObject(): TextObject;
  657. toTextObjectWithoutValue(): TextObject;
  658. }
  659. declare class ChallengePasswordAttribute extends Attribute {
  660. static NAME: string;
  661. password: string;
  662. /**
  663. * Creates a new instance from DER encoded buffer
  664. * @param raw DER encoded buffer
  665. */
  666. constructor(raw: BufferSource);
  667. /**
  668. * Creates a new instance
  669. * @param value
  670. */
  671. constructor(value: string);
  672. protected onInit(asn: asn1X509.Attribute): void;
  673. toTextObject(): TextObject;
  674. }
  675. declare class ExtensionsAttribute extends Attribute {
  676. static NAME: string;
  677. items: Extension[];
  678. /**
  679. * Creates a new instance from DER encoded buffer
  680. * @param raw DER encoded buffer
  681. */
  682. constructor(raw: BufferSource);
  683. /**
  684. * Creates a new instance
  685. * @param extensions
  686. */
  687. constructor(extensions: Extension[]);
  688. protected onInit(asn: asn1X509.Attribute): void;
  689. toTextObject(): TextObject;
  690. }
  691. /**
  692. * Static class to manage X509 attributes
  693. */
  694. declare class AttributeFactory {
  695. private static items;
  696. /**
  697. * Registers a new X509 Attribute class. If id already exists replaces it
  698. * @param id Attribute identifier
  699. * @param type Attribute class
  700. *
  701. * @example
  702. * ```js
  703. * AttributeFactory.register(asnPkcs9.id_pkcs9_at_challengePassword, ChallengePasswordAttribute);
  704. * ```
  705. */
  706. static register(id: string, type: any): void;
  707. /**
  708. * Returns X509 Attribute based on it's identifier
  709. * @param data DER encoded buffer
  710. *
  711. * @example
  712. * ```js
  713. * const attr = AttributeFactory.create(asnAttrRaw);
  714. * ```
  715. */
  716. static create(data: BufferSource): Attribute;
  717. }
  718. /**
  719. * Dependency injection identifier for `IAsnSignatureFormatter` interface
  720. */
  721. declare const diAsnSignatureFormatter = "crypto.signatureFormatter";
  722. /**
  723. * Provides mechanism to convert ASN.1 signature value to WebCrypto and back
  724. *
  725. * To register it's implementation in global use `tsyringe` container
  726. * @example
  727. * ```
  728. * import { container } from "tsyringe";
  729. *
  730. * container.registerSingleton(diAsnSignatureFormatter, AsnDefaultSignatureFormatter);
  731. * ```
  732. */
  733. interface IAsnSignatureFormatter {
  734. /**
  735. * Converts ASN.1 signature to WebCrypto format
  736. * @param algorithm Key and signing algorithm
  737. * @param signature ASN.1 signature value in DER format
  738. */
  739. toAsnSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  740. /**
  741. * Converts WebCrypto signature to ASN.1 DER encoded signature value
  742. * @param algorithm
  743. * @param signature
  744. */
  745. toWebSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  746. }
  747. declare class AsnDefaultSignatureFormatter implements IAsnSignatureFormatter {
  748. toAsnSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  749. toWebSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  750. }
  751. interface UnknownAlgorithm extends Algorithm {
  752. name: string;
  753. parameters?: ArrayBuffer | null;
  754. }
  755. interface IAlgorithm {
  756. /**
  757. * Converts WebCrypto algorithm to ASN.1 algorithm
  758. * @param alg WebCrypto algorithm
  759. * @returns ASN.1 algorithm or null
  760. */
  761. toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null;
  762. /**
  763. * Converts ASN.1 algorithm to WebCrypto algorithm
  764. * @param alg ASN.1 algorithm
  765. * @returns WebCrypto algorithm or null
  766. */
  767. toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | null;
  768. }
  769. /**
  770. * Dependency Injection algorithm identifier
  771. */
  772. declare const diAlgorithm = "crypto.algorithm";
  773. declare class AlgorithmProvider {
  774. /**
  775. * Returns all registered algorithm providers
  776. */
  777. getAlgorithms(): IAlgorithm[];
  778. /**
  779. * Converts WebCrypto algorithm to ASN.1 algorithm
  780. * @param alg WebCrypto algorithm
  781. * @returns ASN.1 algorithm
  782. * @throws Error whenever cannot convert an algorithm
  783. */
  784. toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier;
  785. /**
  786. * ConvertsASN.1 algorithm to WebCrypto algorithm
  787. * @param alg ASN.1 algorithm
  788. * @returns algorithm
  789. */
  790. toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm;
  791. }
  792. declare const diAlgorithmProvider = "crypto.algorithmProvider";
  793. interface HashedAlgorithm extends Algorithm {
  794. hash: Algorithm;
  795. }
  796. /**
  797. * RSA algorithm provider
  798. */
  799. declare class RsaAlgorithm implements IAlgorithm {
  800. static createPssParams(hash: unknown, saltLength: number): asn1Rsa.RsaSaPssParams | null;
  801. static getHashAlgorithm(alg: unknown): AlgorithmIdentifier | null;
  802. toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null;
  803. toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | HashedAlgorithm | null;
  804. }
  805. /**
  806. * EC algorithm provider
  807. */
  808. declare class EcAlgorithm implements IAlgorithm {
  809. static SECP256K1: string;
  810. toAsnAlgorithm(alg: HashedAlgorithm | EcKeyGenParams): AlgorithmIdentifier | null;
  811. toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | null;
  812. }
  813. /**
  814. * SHA algorithm provider
  815. */
  816. declare class ShaAlgorithm implements IAlgorithm {
  817. toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null;
  818. toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | null;
  819. }
  820. declare class AsnEcSignatureFormatter implements IAsnSignatureFormatter {
  821. static namedCurveSize: Map<string, number>;
  822. static defaultNamedCurveSize: number;
  823. private addPadding;
  824. private removePadding;
  825. toAsnSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  826. toWebSignature(algorithm: Algorithm, signature: BufferSource): ArrayBuffer | null;
  827. }
  828. declare const idX25519 = "1.3.101.110";
  829. declare const idX448 = "1.3.101.111";
  830. declare const idEd25519 = "1.3.101.112";
  831. declare const idEd448 = "1.3.101.113";
  832. /**
  833. * ECDH-ES and EdDSA algorithm provider
  834. */
  835. declare class EdAlgorithm implements IAlgorithm {
  836. toAsnAlgorithm(alg: EcKeyGenParams): AlgorithmIdentifier | null;
  837. toWebAlgorithm(alg: AlgorithmIdentifier): HashedAlgorithm | EcKeyGenParams | Algorithm | null;
  838. }
  839. type IdOrName = Record<string, string>;
  840. declare class NameIdentifier {
  841. private items;
  842. constructor(names?: Record<string, string>);
  843. get(idOrName: string): string | null;
  844. findId(idOrName: string): string | null;
  845. register(id: string, name: string): void;
  846. }
  847. /**
  848. * JSON representation of Attribute and Value
  849. */
  850. type JsonAttributeAndStringValue = Record<string, string[]>;
  851. interface JsonAttributeObject {
  852. ia5String?: string;
  853. utf8String?: string;
  854. universalString?: string;
  855. bmpString?: string;
  856. printableString?: string;
  857. }
  858. type JsonAttributeAndObjectValue = Record<string, JsonAttributeObject[]>;
  859. type JsonAttributeAndValue = JsonAttributeAndStringValue | JsonAttributeAndObjectValue;
  860. /**
  861. * JSON array of Attribute and Value
  862. */
  863. type JsonName = JsonAttributeAndStringValue[];
  864. type JsonNameParams = JsonAttributeAndValue[];
  865. /**
  866. * UTF-8 String Representation of Distinguished Names
  867. *
  868. * https://tools.ietf.org/html/rfc2253
  869. */
  870. declare class Name {
  871. private extraNames;
  872. /**
  873. * Returns `true` if text is ASCII otherwise `false`
  874. * @param text Text
  875. * @returns
  876. */
  877. static isASCII(text: string): boolean;
  878. /**
  879. * Checks if a given string is a printable string.
  880. * A printable string contains only printable ASCII characters.
  881. *
  882. * @param text - The string to be checked.
  883. * @returns True if the string is a printable string, false otherwise.
  884. */
  885. static isPrintableString(text: string): boolean;
  886. /**
  887. * ASN.1 Name
  888. */
  889. private asn;
  890. /**
  891. * Creates a new instance
  892. * @param data
  893. * @param extraNames Extra identifiers for name customization
  894. * @example
  895. * const text = "URL=http://some.url.com, IP=192.168.0.1, GUID={8ee13e53-2c1c-42bb-8df7-39927c0bdbb6}";
  896. * const name = new x509.Name(text, {
  897. * "Email": "1.2.3.4.5.1",
  898. * "IP": "1.2.3.4.5.2",
  899. * "GUID": "1.2.3.4.5.3",
  900. * });
  901. */
  902. constructor(data: BufferSource | Name$1 | string | JsonNameParams, extraNames?: IdOrName);
  903. /**
  904. * Returns a list of string values filtered by specified id or name
  905. * @param idOrName ObjectIdentifier or string name
  906. * @returns Returns a list of strings. Returns an empty list if there are not any
  907. * values for specified id/name.
  908. */
  909. getField(idOrName: string): string[];
  910. private getName;
  911. /**
  912. * Returns string serialized Name
  913. */
  914. toString(): string;
  915. /**
  916. * Returns a JSON representation of the Name
  917. */
  918. toJSON(): JsonName;
  919. /**
  920. * Creates AsnName object from string
  921. * @param data
  922. */
  923. private fromString;
  924. /**
  925. * Creates AsnName from JSON
  926. * @param data
  927. */
  928. private fromJSON;
  929. /**
  930. * Gets the OID for a given type name
  931. * @param type The type name
  932. * @returns The OID string
  933. */
  934. private getTypeOid;
  935. /**
  936. * Creates an AttributeTypeAndValue object
  937. * @param type The type OID
  938. * @param value The value
  939. * @returns The AttributeTypeAndValue object
  940. */
  941. private createAttribute;
  942. /**
  943. * Processes a string value by unescaping and replacing special characters
  944. * @param value The string value
  945. * @returns The processed string value
  946. */
  947. private processStringValue;
  948. /**
  949. * Returns Name in DER encoded format
  950. */
  951. toArrayBuffer(): ArrayBuffer;
  952. /**
  953. * Returns a SHA-1 thumbprint
  954. * @param crypto Crypto provider. Default is from CryptoProvider
  955. */
  956. getThumbprint(crypto?: Crypto): Promise<ArrayBuffer>;
  957. /**
  958. * Returns a thumbprint for specified mechanism
  959. * @param algorithm Hash algorithm
  960. * @param crypto Crypto provider. Default is from CryptoProvider
  961. */
  962. getThumbprint(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
  963. }
  964. interface PemHeader {
  965. key: string;
  966. value: string;
  967. }
  968. /**
  969. * Represents PEM structure
  970. */
  971. interface PemStruct {
  972. /**
  973. * Type
  974. */
  975. type: string;
  976. /**
  977. * Headers
  978. */
  979. headers: PemHeader[];
  980. /**
  981. * Decoded message data
  982. */
  983. rawData: ArrayBuffer;
  984. }
  985. type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
  986. type PemStructEncodeParams = AtLeast<PemStruct, "type" | "rawData">;
  987. /**
  988. * Represents PEM Converter.
  989. */
  990. declare class PemConverter {
  991. static CertificateTag: string;
  992. static CrlTag: string;
  993. static CertificateRequestTag: string;
  994. static PublicKeyTag: string;
  995. static PrivateKeyTag: string;
  996. static isPem(data: any): data is string;
  997. static decodeWithHeaders(pem: string): PemStruct[];
  998. /**
  999. * Decodes PEM to a list of raws
  1000. * @param pem message in PEM format
  1001. */
  1002. static decode(pem: string): ArrayBuffer[];
  1003. /**
  1004. * Decodes PEM and returns first item from the list
  1005. * @param pem message in PEM format
  1006. * @throw Throws RangeError if list of decoded items is empty
  1007. */
  1008. static decodeFirst(pem: string): ArrayBuffer;
  1009. /**
  1010. * Encodes a list of PemStruct in PEM format
  1011. * @param structs A list of PemStruct
  1012. * @param tag PEM tag
  1013. */
  1014. static encode(structs: PemStructEncodeParams[]): string;
  1015. /**
  1016. * Encodes a raw data in PEM format
  1017. * @param rawData Raw data
  1018. * @param tag PEM tag
  1019. */
  1020. static encode(rawData: BufferSource, tag: string): string;
  1021. /**
  1022. * Encodes a list of raws in PEM format
  1023. * @param raws A list of raws
  1024. * @param tag PEM tag
  1025. */
  1026. static encode(rawData: BufferSource[], tag: string): string;
  1027. /**
  1028. * Encodes PEMStruct in PEM block
  1029. * @param pem PEM structure for encoding
  1030. * @returns Returns PEM encoded block
  1031. */
  1032. private static encodeStruct;
  1033. }
  1034. /**
  1035. * Representation of PKCS10 Certificate Request
  1036. */
  1037. declare class Pkcs10CertificateRequest extends PemData<CertificationRequest> implements IPublicKeyContainer {
  1038. #private;
  1039. static NAME: string;
  1040. protected readonly tag: string;
  1041. /**
  1042. * Gets the subject value from the certificate as an Name
  1043. */
  1044. get subjectName(): Name;
  1045. /**
  1046. * Gets a string subject name
  1047. */
  1048. get subject(): string;
  1049. /**
  1050. * Gets a signature algorithm
  1051. */
  1052. get signatureAlgorithm(): HashedAlgorithm;
  1053. /**
  1054. * Gets a signature
  1055. */
  1056. get signature(): ArrayBuffer;
  1057. /**
  1058. * Gets a public key of CSR
  1059. */
  1060. get publicKey(): PublicKey;
  1061. /**
  1062. * Gets a list fo CSR attributes
  1063. */
  1064. get attributes(): Attribute[];
  1065. /**
  1066. * Gets a list of CSR extensions
  1067. */
  1068. get extensions(): Extension[];
  1069. /**
  1070. * Gets the ToBeSigned block
  1071. */
  1072. private get tbs();
  1073. /**
  1074. * Creates a new instance fromDER encoded buffer
  1075. * @param raw DER encoded buffer
  1076. */
  1077. constructor(raw: AsnEncodedType);
  1078. /**
  1079. * Creates a new instance from ASN.1 CertificationRequest
  1080. * @param asn ASN.1 CertificationRequest
  1081. */
  1082. constructor(asn: CertificationRequest);
  1083. protected onInit(_asn: CertificationRequest): void;
  1084. /**
  1085. * Returns attribute of the specified type
  1086. * @param type Attribute identifier
  1087. * @returns Attribute or null
  1088. */
  1089. getAttribute(type: string): Attribute | null;
  1090. /**
  1091. * Returns a list of attributes of the specified type
  1092. * @param type Attribute identifier
  1093. */
  1094. getAttributes(type: string): Attribute[];
  1095. /**
  1096. * Returns extension of the specified type
  1097. * @param type Extension identifier
  1098. * @returns Extension or null
  1099. */
  1100. getExtension(type: string): Extension | null;
  1101. /**
  1102. * Returns a list of extension of the specified type
  1103. * @param type Extension identifier
  1104. */
  1105. getExtensions(type: string): Extension[];
  1106. /**
  1107. * Validates CSR signature
  1108. * @param crypto Crypto provider. Default is from CryptoProvider
  1109. */
  1110. verify(crypto?: Crypto): Promise<boolean>;
  1111. toTextObject(): TextObject;
  1112. }
  1113. type Pkcs10CertificateRequestCreateParamsName = string | JsonName | Name;
  1114. /**
  1115. * Pkcs10CertificateRequest create parameters
  1116. */
  1117. interface Pkcs10CertificateRequestCreateParams {
  1118. /**
  1119. * Subject name
  1120. */
  1121. name?: Pkcs10CertificateRequestCreateParamsName;
  1122. /**
  1123. * Extensions
  1124. */
  1125. extensions?: Extension[];
  1126. /**
  1127. * Attributes
  1128. */
  1129. attributes?: Attribute[];
  1130. /**
  1131. * Signing algorithm
  1132. */
  1133. signingAlgorithm: Algorithm | EcdsaParams;
  1134. /**
  1135. * Crypto key pair
  1136. */
  1137. keys: CryptoKeyPair;
  1138. }
  1139. /**
  1140. * Generator of PKCS10 certificate requests
  1141. */
  1142. declare class Pkcs10CertificateRequestGenerator {
  1143. /**
  1144. * Creates a new PKCS10 Certificate request
  1145. * @param params Create parameters
  1146. * @param crypto Crypto provider. Default is from CryptoProvider
  1147. */
  1148. static create(params: Pkcs10CertificateRequestCreateParams, crypto?: Crypto): Promise<Pkcs10CertificateRequest>;
  1149. }
  1150. type MapForEachCallback = (value: Crypto, key: string, map: Map<string, Crypto>) => void;
  1151. /**
  1152. * Crypto provider
  1153. */
  1154. declare class CryptoProvider {
  1155. static DEFAULT: string;
  1156. private items;
  1157. /**
  1158. * Returns `true` if data is CryptoKeyPair
  1159. * @param data
  1160. */
  1161. static isCryptoKeyPair(data: any): data is CryptoKeyPair;
  1162. static isCryptoKey(data: any): data is CryptoKey;
  1163. /**
  1164. * Creates a new instance
  1165. */
  1166. constructor();
  1167. clear(): void;
  1168. delete(key: string): boolean;
  1169. forEach(callbackfn: MapForEachCallback, thisArg?: any): void;
  1170. has(key: string): boolean;
  1171. get size(): number;
  1172. entries(): IterableIterator<[string, Crypto]>;
  1173. keys(): IterableIterator<string>;
  1174. values(): IterableIterator<Crypto>;
  1175. [Symbol.iterator](): IterableIterator<[string, Crypto]>;
  1176. [Symbol.toStringTag]: string;
  1177. /**
  1178. * Returns default crypto
  1179. * @throws Error whenever default provider not set
  1180. */
  1181. get(): Crypto;
  1182. /**
  1183. * Returns crypto by name
  1184. * @param key Crypto name
  1185. * @throws Error whenever provider with specified identifier does not exist
  1186. */
  1187. get(key: string): Crypto;
  1188. /**
  1189. * Sets default crypto
  1190. * @param value
  1191. */
  1192. set(value: Crypto): this;
  1193. /**
  1194. * Sets crypto with specified identifier
  1195. * @param key Identifier
  1196. * @param value crypto provider
  1197. */
  1198. set(key: string, value: Crypto): this;
  1199. }
  1200. /**
  1201. * Singleton crypto provider
  1202. */
  1203. declare const cryptoProvider: CryptoProvider;
  1204. /**
  1205. * Verification params of X509 certificate
  1206. */
  1207. interface X509CertificateVerifyParams {
  1208. date?: Date;
  1209. publicKey?: PublicKeyType;
  1210. signatureOnly?: boolean;
  1211. }
  1212. /**
  1213. * Representation of X509 certificate
  1214. */
  1215. declare class X509Certificate extends PemData<Certificate> implements IPublicKeyContainer {
  1216. #private;
  1217. static NAME: string;
  1218. protected readonly tag: string;
  1219. /**
  1220. * Gets a public key of the certificate
  1221. */
  1222. get publicKey(): PublicKey;
  1223. /**
  1224. * Gets a hexadecimal string of the serial number
  1225. */
  1226. get serialNumber(): string;
  1227. /**
  1228. * Gets the subject value from the certificate as an Name
  1229. */
  1230. get subjectName(): Name;
  1231. /**
  1232. * Gets a string subject name
  1233. */
  1234. get subject(): string;
  1235. /**
  1236. * Gets the issuer value from the certificate as an Name
  1237. */
  1238. get issuerName(): Name;
  1239. /**
  1240. * Gets a string issuer name
  1241. */
  1242. get issuer(): string;
  1243. /**
  1244. * Gets a date before which certificate can't be used
  1245. */
  1246. get notBefore(): Date;
  1247. /**
  1248. * Gets a date after which certificate can't be used
  1249. */
  1250. get notAfter(): Date;
  1251. /**
  1252. * Gets a signature algorithm
  1253. */
  1254. get signatureAlgorithm(): HashedAlgorithm;
  1255. /**
  1256. * Gets a signature
  1257. */
  1258. get signature(): ArrayBuffer;
  1259. /**
  1260. * Gets a list of certificate extensions
  1261. */
  1262. get extensions(): Extension[];
  1263. /**
  1264. * Gets a private key of the certificate
  1265. */
  1266. privateKey?: CryptoKey;
  1267. /**
  1268. * Gets the ToBeSigned block
  1269. */
  1270. private get tbs();
  1271. /**
  1272. * Creates a new instance from ASN.1 Certificate object
  1273. * @param asn ASN.1 Certificate object
  1274. */
  1275. constructor(asn: Certificate);
  1276. /**
  1277. * Creates a new instance
  1278. * @param raw Encoded buffer (DER, PEM, HEX, Base64, Base64Url)
  1279. */
  1280. constructor(raw: AsnEncodedType);
  1281. protected onInit(_asn: Certificate): void;
  1282. /**
  1283. * Returns an extension of specified type
  1284. * @param type Extension identifier
  1285. * @returns Extension or null
  1286. */
  1287. getExtension<T extends Extension>(type: string): T | null;
  1288. /**
  1289. * Returns an extension of specified type
  1290. * @param type Extension type
  1291. * @returns Extension or null
  1292. */
  1293. getExtension<T extends Extension>(type: new (raw: BufferSource) => T): T | null;
  1294. /**
  1295. * Returns a list of extensions of specified type
  1296. * @param type Extension identifier
  1297. */
  1298. getExtensions<T extends Extension>(type: string): T[];
  1299. /**
  1300. * Returns a list of extensions of specified type
  1301. * @param type Extension type
  1302. */
  1303. getExtensions<T extends Extension>(type: new (raw: BufferSource) => T): T[];
  1304. /**
  1305. * Validates a certificate signature
  1306. * @param params Verification parameters
  1307. * @param crypto Crypto provider. Default is from CryptoProvider
  1308. */
  1309. verify(params?: X509CertificateVerifyParams, crypto?: Crypto): Promise<boolean>;
  1310. /**
  1311. * Returns a SHA-1 certificate thumbprint
  1312. * @param crypto Crypto provider. Default is from CryptoProvider
  1313. */
  1314. getThumbprint(crypto?: Crypto): Promise<ArrayBuffer>;
  1315. /**
  1316. * Returns a certificate thumbprint for specified mechanism
  1317. * @param algorithm Hash algorithm
  1318. * @param crypto Crypto provider. Default is from CryptoProvider
  1319. */
  1320. getThumbprint(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
  1321. isSelfSigned(crypto?: Crypto): Promise<boolean>;
  1322. toTextObject(): TextObject;
  1323. }
  1324. type X509CertificatesExportType = AsnExportType | "pem-chain";
  1325. /**
  1326. * X509 Certificate collection
  1327. */
  1328. declare class X509Certificates extends Array<X509Certificate> implements TextObjectConvertible {
  1329. /**
  1330. * Creates a new instance
  1331. */
  1332. constructor();
  1333. /**
  1334. * Creates a new instance from encoded PKCS7 buffer
  1335. * @param raw Encoded PKCS7 buffer. Supported formats are DER, PEM, HEX, Base64, or Base64Url
  1336. */
  1337. constructor(raw: AsnEncodedType);
  1338. /**
  1339. * Creates a new instance form X509 certificate
  1340. * @param cert X509 certificate
  1341. */
  1342. constructor(cert: X509Certificate);
  1343. /**
  1344. * Creates a new instance from a list of x509 certificates
  1345. * @param certs List of x509 certificates
  1346. */
  1347. constructor(certs: X509Certificate[]);
  1348. /**
  1349. * Returns encoded object in PEM format
  1350. */
  1351. export(): string;
  1352. /**
  1353. * Returns encoded object in DER format
  1354. * @param format `der` format
  1355. */
  1356. export(format: "raw"): ArrayBuffer;
  1357. /**
  1358. * Returns encoded object in selected format
  1359. * @param format `hex`, `base64`, `base64url`, `pem`. Default is `pem`
  1360. */
  1361. export(format?: AsnExportType): string;
  1362. /**
  1363. * Import certificates from encoded PKCS7 data. Supported formats are HEX, DER,
  1364. * Base64, Base64Url, PEM
  1365. * @param data
  1366. */
  1367. import(data: AsnEncodedType): void;
  1368. /**
  1369. * Removes all items from collection
  1370. */
  1371. clear(): void;
  1372. toString(format?: X509CertificatesExportType): string;
  1373. toTextObject(): TextObject;
  1374. }
  1375. interface X509ChainBuilderParams {
  1376. certificates?: X509Certificate[];
  1377. }
  1378. /**
  1379. * Represents a chain-building engine for X509Certificate certificates
  1380. * @example
  1381. * ```js
  1382. * const chain = new x509.X509ChainBuilder({
  1383. * certificates: [
  1384. * new x509.X509Certificate(raw1),
  1385. * new x509.X509Certificate(raw2),
  1386. * // ...
  1387. * new x509.X509Certificate(rawN),
  1388. * ],
  1389. * });
  1390. *
  1391. * const cert = x509.X509Certificate(raw);
  1392. * const items = await chain.build(cert);
  1393. * ```
  1394. */
  1395. declare class X509ChainBuilder {
  1396. certificates: X509Certificate[];
  1397. constructor(params?: X509ChainBuilderParams);
  1398. build(cert: X509Certificate, crypto?: Crypto): Promise<X509Certificates>;
  1399. private findIssuer;
  1400. }
  1401. type X509CertificateCreateParamsName = string | JsonName | Name;
  1402. /**
  1403. * Base arguments for certificate creation
  1404. */
  1405. interface X509CertificateCreateParamsBase {
  1406. /**
  1407. * Hexadecimal serial number. If not specified, random value will be generated
  1408. */
  1409. serialNumber?: string;
  1410. /**
  1411. * Date before which certificate can't be used. Default is current date
  1412. */
  1413. notBefore?: Date;
  1414. /**
  1415. * Date after which certificate can't be used. Default is 1 year from now
  1416. */
  1417. notAfter?: Date;
  1418. /**
  1419. * List of extensions
  1420. */
  1421. extensions?: Extension[];
  1422. /**
  1423. * Signing algorithm. Default is SHA-256 with key algorithm
  1424. */
  1425. signingAlgorithm?: Algorithm | EcdsaParams;
  1426. }
  1427. /**
  1428. * Common parameters for X509 Certificate generation
  1429. */
  1430. interface X509CertificateCreateCommonParams extends X509CertificateCreateParamsBase {
  1431. subject?: X509CertificateCreateParamsName;
  1432. issuer?: X509CertificateCreateParamsName;
  1433. }
  1434. /**
  1435. * Parameters for X509 Certificate generation with private key
  1436. */
  1437. interface X509CertificateCreateWithKeyParams extends X509CertificateCreateCommonParams {
  1438. publicKey: PublicKeyType;
  1439. signingKey: CryptoKey;
  1440. }
  1441. /**
  1442. * Parameters for X509 Certificate generation with existing signature value
  1443. */
  1444. interface X509CertificateCreateWithSignatureParams extends X509CertificateCreateCommonParams {
  1445. /**
  1446. * Signature for manually initialized certificates
  1447. */
  1448. signature: BufferSource$1;
  1449. /**
  1450. * Manual signing requires CryptoKey that includes signature algorithm
  1451. */
  1452. publicKey: PublicKeyType;
  1453. }
  1454. type X509CertificateCreateParams = X509CertificateCreateWithKeyParams | X509CertificateCreateWithSignatureParams;
  1455. /**
  1456. * Parameters for self-signed X509 Certificate generation
  1457. */
  1458. interface X509CertificateCreateSelfSignedParams extends X509CertificateCreateParamsBase {
  1459. name?: X509CertificateCreateParamsName;
  1460. keys: CryptoKeyPair;
  1461. }
  1462. /**
  1463. * Generator of X509 certificates
  1464. */
  1465. declare class X509CertificateGenerator {
  1466. /**
  1467. * Creates a self-signed certificate
  1468. * @param params Parameters
  1469. * @param crypto Crypto provider. Default is from CryptoProvider
  1470. */
  1471. static createSelfSigned(params: X509CertificateCreateSelfSignedParams, crypto?: Crypto): Promise<X509Certificate>;
  1472. /**
  1473. * Creates a certificate signed by private key
  1474. * @param params Parameters
  1475. * @param crypto Crypto provider. Default is from CryptoProvider
  1476. */
  1477. static create(params: X509CertificateCreateParams, crypto?: Crypto): Promise<X509Certificate>;
  1478. }
  1479. /**
  1480. * Reason Code
  1481. * The reasonCode is a non-critical CRL entry extension that identifies
  1482. * the reason for the certificate revocation.
  1483. */
  1484. declare enum X509CrlReason {
  1485. unspecified = 0,
  1486. keyCompromise = 1,
  1487. cACompromise = 2,
  1488. affiliationChanged = 3,
  1489. superseded = 4,
  1490. cessationOfOperation = 5,
  1491. certificateHold = 6,
  1492. removeFromCRL = 8,
  1493. privilegeWithdrawn = 9,
  1494. aACompromise = 10
  1495. }
  1496. /**
  1497. * Representation of X509CrlEntry
  1498. */
  1499. declare class X509CrlEntry extends AsnData<RevokedCertificate> {
  1500. #private;
  1501. /**
  1502. * Gets a hexadecimal string of the serial number, the userCertificate
  1503. */
  1504. get serialNumber(): string;
  1505. /**
  1506. * Gets the revocation date
  1507. */
  1508. get revocationDate(): Date;
  1509. /**
  1510. * Gets the reason code
  1511. */
  1512. get reason(): X509CrlReason | undefined;
  1513. /**
  1514. * Gets the invalidity Date
  1515. * The invalidity date is a non-critical CRL entry extension that
  1516. * provides the date on which it is known or suspected that the private
  1517. * key was compromised or that the certificate otherwise became invalid.
  1518. */
  1519. get invalidity(): Date | undefined;
  1520. /**
  1521. * Gets crl entry extensions
  1522. */
  1523. get extensions(): Extension[];
  1524. /**
  1525. * Creates a new instance from DER encoded Buffer
  1526. * @param raw DER encoded buffer
  1527. */
  1528. constructor(raw: BufferSource);
  1529. /**
  1530. * Creates a new instance from ASN.1 object
  1531. * @param asn ASN.1 object
  1532. */
  1533. constructor(asn: RevokedCertificate);
  1534. /**
  1535. * Creates a new instance
  1536. * @param serialNumber Serial number of certificate
  1537. * @param revocationDate Revocation date
  1538. * @param extensions List of crl extensions
  1539. */
  1540. constructor(serialNumber: string, revocationDate: Date, extensions: Extension[]);
  1541. protected onInit(_asn: RevokedCertificate): void;
  1542. }
  1543. interface X509CrlVerifyParams {
  1544. publicKey: CryptoKey | PublicKey | X509Certificate;
  1545. }
  1546. /**
  1547. * Representation of X.509 Certificate Revocation List (CRL)
  1548. */
  1549. declare class X509Crl extends PemData<CertificateList> {
  1550. #private;
  1551. protected readonly tag: string;
  1552. /**
  1553. * Gets a version
  1554. */
  1555. get version(): Version | undefined;
  1556. /**
  1557. * Gets a signature algorithm
  1558. */
  1559. get signatureAlgorithm(): HashedAlgorithm;
  1560. /**
  1561. * Gets a signature
  1562. */
  1563. get signature(): ArrayBuffer;
  1564. /**
  1565. * Gets a string issuer name
  1566. */
  1567. get issuer(): string;
  1568. /**
  1569. * Gets the issuer value from the crl as an Name
  1570. */
  1571. get issuerName(): Name;
  1572. /**
  1573. * Gets a thisUpdate date from the CRL
  1574. */
  1575. get thisUpdate(): Date;
  1576. /**
  1577. * Gets a nextUpdate date from the CRL
  1578. */
  1579. get nextUpdate(): Date | undefined;
  1580. /**
  1581. * Gets a crlEntries from the CRL
  1582. *
  1583. * @remarks
  1584. * Reading this property parses all revoked certificates, which can be slow for large CRLs.
  1585. * Use findRevoked() for efficient searching of specific certificates.
  1586. */
  1587. get entries(): readonly X509CrlEntry[];
  1588. /**
  1589. * Gets a list of crl extensions
  1590. */
  1591. get extensions(): Extension[];
  1592. /**
  1593. * Gets the ToBeSigned block
  1594. */
  1595. private get tbs();
  1596. /**
  1597. * Gets the signature algorithm from tbsCertList
  1598. */
  1599. private get tbsCertListSignatureAlgorithm();
  1600. /**
  1601. * Gets the signature algorithm from CertificateList
  1602. */
  1603. private get certListSignatureAlgorithm();
  1604. /**
  1605. * Creates a new instance from ASN.1 CertificateList object
  1606. * @param asn ASN.1 CertificateList object
  1607. */
  1608. constructor(asn: CertificateList);
  1609. /**
  1610. * Creates a new instance
  1611. * @param raw Encoded buffer (DER, PEM, HEX, Base64, Base64Url)
  1612. */
  1613. constructor(raw: AsnEncodedType);
  1614. protected onInit(_asn: CertificateList): void;
  1615. /**
  1616. * Returns an extension of specified type
  1617. * @param type Extension identifier
  1618. * @returns Extension or null
  1619. */
  1620. getExtension<T extends Extension>(type: string): T | null;
  1621. /**
  1622. * Returns an extension of specified type
  1623. * @param type Extension type
  1624. * @returns Extension or null
  1625. */
  1626. getExtension<T extends Extension>(type: new (raw: BufferSource) => T): T | null;
  1627. /**
  1628. * Returns a list of extensions of specified type
  1629. * @param type Extension identifier
  1630. */
  1631. getExtensions<T extends Extension>(type: string): T[];
  1632. /**
  1633. * Returns a list of extensions of specified type
  1634. * @param type Extension type
  1635. */
  1636. getExtensions<T extends Extension>(type: new (raw: BufferSource) => T): T[];
  1637. /**
  1638. * Validates a crl signature
  1639. * @param params Verification parameters
  1640. * @param crypto Crypto provider. Default is from CryptoProvider
  1641. */
  1642. verify(params: X509CrlVerifyParams, crypto?: Crypto): Promise<boolean>;
  1643. /**
  1644. * Returns a SHA-1 certificate thumbprint
  1645. * @param crypto Crypto provider. Default is from CryptoProvider
  1646. */
  1647. getThumbprint(crypto?: Crypto): Promise<ArrayBuffer>;
  1648. /**
  1649. * Returns a certificate thumbprint for specified mechanism
  1650. * @param algorithm Hash algorithm
  1651. * @param crypto Crypto provider. Default is from CryptoProvider
  1652. */
  1653. getThumbprint(algorithm: globalThis.AlgorithmIdentifier, crypto?: Crypto): Promise<ArrayBuffer>;
  1654. /**
  1655. * Gets the CRL entry, with the given X509Certificate or certificate serialNumber.
  1656. *
  1657. * @param certOrSerialNumber certificate | serialNumber
  1658. */
  1659. findRevoked(certOrSerialNumber: X509Certificate | string): X509CrlEntry | null;
  1660. }
  1661. interface X509CrlEntryParams {
  1662. /**
  1663. * Hexadecimal serial number
  1664. */
  1665. serialNumber: string;
  1666. revocationDate?: Date;
  1667. reason?: X509CrlReason;
  1668. invalidity?: Date;
  1669. issuer?: X509CertificateCreateParamsName;
  1670. extensions?: Extension[];
  1671. }
  1672. /**
  1673. * Base arguments for crl creation
  1674. */
  1675. interface X509CrlCreateParamsBase {
  1676. issuer: X509CertificateCreateParamsName;
  1677. thisUpdate?: Date;
  1678. /**
  1679. * Signing algorithm
  1680. */
  1681. signingAlgorithm: Algorithm | EcdsaParams;
  1682. }
  1683. /**
  1684. * Parameters for X509 CRL generation
  1685. */
  1686. interface X509CrlCreateParams extends X509CrlCreateParamsBase {
  1687. nextUpdate?: Date;
  1688. extensions?: Extension[];
  1689. entries?: X509CrlEntryParams[];
  1690. signingKey: CryptoKey;
  1691. }
  1692. /**
  1693. * Generator of X509 crl
  1694. */
  1695. declare class X509CrlGenerator {
  1696. /**
  1697. * Creates a crl signed by private key
  1698. * @param params Parameters
  1699. * @param crypto Crypto provider. Default is from CryptoProvider
  1700. */
  1701. static create(params: X509CrlCreateParams, crypto?: Crypto): Promise<X509Crl>;
  1702. }
  1703. export { AlgorithmProvider, AsnData, AsnDefaultSignatureFormatter, AsnEcSignatureFormatter, Attribute, AttributeFactory, AuthorityInfoAccessExtension, AuthorityKeyIdentifierExtension, BasicConstraintsExtension, CRLDistributionPointsExtension, CertificatePolicyExtension, ChallengePasswordAttribute, CryptoProvider, DN, DNS, DefaultAlgorithmSerializer, EMAIL, EcAlgorithm, EdAlgorithm, ExtendedKeyUsage, ExtendedKeyUsageExtension, Extension, ExtensionFactory, ExtensionsAttribute, GUID, GeneralName, GeneralNames, IP, IssuerAlternativeNameExtension, KeyUsageFlags, KeyUsagesExtension, Name, NameIdentifier, OidSerializer, PemConverter, PemData, Pkcs10CertificateRequest, Pkcs10CertificateRequestGenerator, PublicKey, REGISTERED_ID, RsaAlgorithm, ShaAlgorithm, SubjectAlternativeNameExtension, SubjectKeyIdentifierExtension, TextConverter, TextObject, UPN, URL, X509Certificate, X509CertificateGenerator, X509Certificates, X509ChainBuilder, X509Crl, X509CrlEntry, X509CrlGenerator, X509CrlReason, cryptoProvider, diAlgorithm, diAlgorithmProvider, diAsnSignatureFormatter, idEd25519, idEd448, idX25519, idX448 };
  1704. export type { AccessItemTypes, AlgorithmSerializer, AsnDataStringFormat, AsnEncodedType, AsnExportType, AuthorityInfoAccessParams, CertificateIdentifier, ExtendedKeyUsageType, GeneralNameType, HashedAlgorithm, IAlgorithm, IAsnSignatureFormatter, IPublicKeyContainer, IdOrName, JsonAttributeAndObjectValue, JsonAttributeAndStringValue, JsonAttributeAndValue, JsonAttributeObject, JsonGeneralName, JsonGeneralNames, JsonName, JsonNameParams, MapForEachCallback, PemHeader, PemStruct, PemStructEncodeParams, Pkcs10CertificateRequestCreateParams, Pkcs10CertificateRequestCreateParamsName, PublicKeyType, TextObjectConvertible, TextObjectItemType, UnknownAlgorithm, X509CertificateCreateCommonParams, X509CertificateCreateParams, X509CertificateCreateParamsBase, X509CertificateCreateParamsName, X509CertificateCreateSelfSignedParams, X509CertificateCreateWithKeyParams, X509CertificateCreateWithSignatureParams, X509CertificateVerifyParams, X509CertificatesExportType, X509ChainBuilderParams, X509CrlCreateParams, X509CrlCreateParamsBase, X509CrlEntryParams, X509CrlVerifyParams };