x509.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /// <reference types="node" />
  2. import { inspect } from 'util';
  3. import { ASN1, Tag, BitString } from '@fidm/asn1';
  4. import { PublicKey } from './pki';
  5. /**
  6. * Attribute for X.509v3 certificate.
  7. */
  8. export interface Attribute {
  9. oid: string;
  10. value: any;
  11. valueTag: Tag;
  12. name: string;
  13. shortName: string;
  14. extensions?: Extension[];
  15. }
  16. /**
  17. * DistinguishedName for X.509v3 certificate.
  18. */
  19. export declare class DistinguishedName {
  20. uniqueId: BitString | null;
  21. attributes: Attribute[];
  22. constructor();
  23. readonly commonName: string;
  24. readonly organizationName: string;
  25. readonly organizationalUnitName: string;
  26. readonly countryName: string;
  27. readonly localityName: string;
  28. readonly serialName: string;
  29. getHash(): Buffer;
  30. getField(key: string): Attribute | null;
  31. addField(attr: any): void;
  32. setAttrs(attrs: any): void;
  33. toJSON(): any;
  34. private getFieldValue;
  35. }
  36. /**
  37. * X.509v3 Certificate.
  38. */
  39. export declare class Certificate {
  40. /**
  41. * Parse one or more X.509 certificates from PEM formatted buffer.
  42. * If there is no certificate, it will throw error.
  43. * @param data PEM formatted buffer
  44. */
  45. static fromPEMs(data: Buffer): Certificate[];
  46. /**
  47. * Parse an X.509 certificate from PEM formatted buffer.
  48. * @param data PEM formatted buffer
  49. */
  50. static fromPEM(data: Buffer): Certificate;
  51. readonly raw: Buffer;
  52. readonly version: number;
  53. readonly serialNumber: string;
  54. readonly signatureOID: string;
  55. readonly signatureAlgorithm: string;
  56. readonly infoSignatureOID: string;
  57. readonly signature: Buffer;
  58. readonly subjectKeyIdentifier: string;
  59. readonly authorityKeyIdentifier: string;
  60. readonly ocspServer: string;
  61. readonly issuingCertificateURL: string;
  62. readonly isCA: boolean;
  63. readonly maxPathLen: number;
  64. readonly basicConstraintsValid: boolean;
  65. readonly keyUsage: number;
  66. readonly dnsNames: string[];
  67. readonly emailAddresses: string[];
  68. readonly ipAddresses: string[];
  69. readonly uris: string[];
  70. readonly validFrom: Date;
  71. readonly validTo: Date;
  72. readonly issuer: DistinguishedName;
  73. readonly subject: DistinguishedName;
  74. readonly extensions: Extension[];
  75. readonly publicKey: PublicKey;
  76. readonly publicKeyRaw: Buffer;
  77. readonly tbsCertificate: ASN1;
  78. /**
  79. * Creates an X.509 certificate from an ASN.1 object
  80. * @param obj an ASN.1 object
  81. */
  82. constructor(obj: ASN1);
  83. /**
  84. * Gets an extension by its name or oid.
  85. * If extension exists and a key provided, it will return extension[key].
  86. * ```js
  87. * certificate.getExtension('keyUsage')
  88. * certificate.getExtension('2.5.29.15')
  89. * // => { oid: '2.5.29.15',
  90. * // critical: true,
  91. * // value: <Buffer 03 02 05 a0>,
  92. * // name: 'keyUsage',
  93. * // digitalSignature: true,
  94. * // nonRepudiation: false,
  95. * // keyEncipherment: true,
  96. * // dataEncipherment: false,
  97. * // keyAgreement: false,
  98. * // keyCertSign: false,
  99. * // cRLSign: false,
  100. * // encipherOnly: false,
  101. * // decipherOnly: false }
  102. * certificate.getExtension('keyUsage', 'keyCertSign') // => false
  103. * ```
  104. * @param name extension name or OID
  105. * @param key key in extension
  106. */
  107. getExtension(name: string, key?: string): any;
  108. /**
  109. * Returns null if a subject certificate is valid, or error if invalid.
  110. * Note that it does not check validity time, DNS name, ip or others.
  111. * @param child subject's Certificate
  112. */
  113. checkSignature(child: Certificate): Error | null;
  114. /**
  115. * Returns true if this certificate's issuer matches the passed
  116. * certificate's subject. Note that no signature check is performed.
  117. * @param parent issuer's Certificate
  118. */
  119. isIssuer(parent: Certificate): boolean;
  120. /**
  121. * Verifies the subjectKeyIdentifier extension value for this certificate
  122. * against its public key.
  123. */
  124. verifySubjectKeyIdentifier(): boolean;
  125. /**
  126. * Return a friendly JSON object for debuging.
  127. */
  128. toJSON(): any;
  129. protected [inspect.custom](_depth: any, options: any): string;
  130. }
  131. export interface Extension {
  132. oid: string;
  133. critical: boolean;
  134. value: Buffer;
  135. name: string;
  136. altNames?: any[];
  137. [index: string]: any;
  138. }