pki.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /// <reference types="node" />
  2. import { inspect } from 'util';
  3. import { ASN1, Template } from '@fidm/asn1';
  4. /**
  5. * ASN.1 Template for PKCS#8 Public Key.
  6. */
  7. export declare const publicKeyValidator: Template;
  8. /**
  9. * ASN.1 Template for PKCS#8 Private Key. https://tools.ietf.org/html/rfc5208
  10. */
  11. export declare const privateKeyValidator: Template;
  12. export declare type Verifier = (this: PublicKey, data: Buffer, signature: Buffer) => boolean;
  13. /**
  14. * PKCS#8 Public Key
  15. */
  16. export declare class PublicKey {
  17. /**
  18. * Parse an PublicKey for X.509 certificate from PKCS#8 PEM formatted buffer or PKCS#1 RSA PEM formatted buffer.
  19. * @param pem PEM formatted buffer
  20. */
  21. static fromPEM(pem: Buffer): PublicKey;
  22. /**
  23. * Registers an external Verifier with object identifier.
  24. * Built-in verifiers: Ed25519, RSA, others see https://nodejs.org/api/crypto.html#crypto_class_verify
  25. * ```js
  26. * PublicKey.addVerifier(getOID('Ed25519'), function (this: PublicKey, data: Buffer, signature: Buffer): boolean {
  27. * return ed25519.detached.verify(data, signature, this.keyRaw)
  28. * })
  29. * ```
  30. * @param oid algorithm object identifier
  31. * @param fn Verifier function
  32. */
  33. static addVerifier(oid: string, fn: Verifier): void;
  34. private static _verifiers;
  35. readonly oid: string;
  36. readonly algo: string;
  37. protected _pkcs8: ASN1;
  38. protected _keyRaw: Buffer;
  39. protected _finalKey: Buffer;
  40. protected _finalPEM: string;
  41. constructor(obj: ASN1);
  42. /**
  43. * underlying key buffer
  44. */
  45. readonly keyRaw: Buffer;
  46. /**
  47. * Returns true if the provided data and the given signature matched.
  48. * ```js
  49. * certificate.publicKey.verify(data, signature, 'sha256') // => true or false
  50. * ```
  51. * @param data data to verify
  52. * @param signature signature that signed by private key
  53. * @param hashAlgorithm hash algorithm, such as 'sha256', 'sha1'
  54. */
  55. verify(data: Buffer, signature: Buffer, hashAlgorithm: string): boolean;
  56. /**
  57. * Returns the digest of the PublicKey with given hash algorithm.
  58. * ```js
  59. * certificate.publicKey.getFingerprint('sha1', 'PublicKey') // => Buffer
  60. * ```
  61. * @param hashAlgorithm hash algorithm, such as 'sha256', 'sha1'
  62. * @param type 'PublicKey' or 'PublicKeyInfo'
  63. */
  64. getFingerprint(hashAlgorithm: string, type?: string): Buffer;
  65. /**
  66. * Returns an ASN.1 object of this PublicKey
  67. */
  68. toASN1(): ASN1;
  69. /**
  70. * Returns an DER formatted buffer of this PublicKey
  71. */
  72. toDER(): Buffer;
  73. /**
  74. * Returns an PEM formatted string of this PublicKey
  75. */
  76. toPEM(): string;
  77. /**
  78. * Return a friendly JSON object for debuging.
  79. */
  80. toJSON(): any;
  81. protected [inspect.custom](_depth: any, options: any): string;
  82. }
  83. export declare type Signer = (this: PrivateKey, data: Buffer) => Buffer;
  84. /**
  85. * PKCS#8 Private Key
  86. */
  87. export declare class PrivateKey {
  88. /**
  89. * Parse an PrivateKey for X.509 certificate from PKCS#8 PEM formatted buffer or PKCS#1 RSA PEM formatted buffer.
  90. * @param pem PEM formatted buffer
  91. */
  92. static fromPEM(pem: Buffer): PrivateKey;
  93. /**
  94. * Registers an external Signer with object identifier.
  95. * Built-in verifiers: Ed25519, RSA, others see https://nodejs.org/api/crypto.html#crypto_class_sign
  96. * ```js
  97. * PrivateKey.addSigner(getOID('Ed25519'), function (this: PrivateKey, data: Buffer): Buffer {
  98. * const key = this.keyRaw
  99. * if (key.length !== 64) {
  100. * throw new Error('Invalid signing key.')
  101. * }
  102. * return Buffer.from(ed25519.detached(data, key))
  103. * })
  104. * ```
  105. * @param oid algorithm object identifier
  106. * @param fn Verifier function
  107. */
  108. static addSigner(oid: string, fn: Signer): void;
  109. private static _signers;
  110. readonly version: number;
  111. readonly oid: string;
  112. readonly algo: string;
  113. protected _pkcs8: ASN1;
  114. protected _keyRaw: Buffer;
  115. protected _publicKeyRaw: Buffer | null;
  116. protected _finalKey: Buffer;
  117. protected _finalPEM: string;
  118. constructor(obj: ASN1);
  119. /**
  120. * underlying key buffer
  121. */
  122. readonly keyRaw: Buffer;
  123. /**
  124. * Returns publicKey buffer, it is used for Ed25519/Ed448.
  125. */
  126. readonly publicKeyRaw: Buffer | null;
  127. /**
  128. * Returns signature for the given data and hash algorithm.
  129. * @param data
  130. * @param hashAlgorithm
  131. */
  132. sign(data: Buffer, hashAlgorithm: string): Buffer;
  133. /**
  134. * Returns an ASN.1 object of this PrivateKey
  135. */
  136. toASN1(): ASN1;
  137. /**
  138. * Returns an DER formatted buffer of this PrivateKey
  139. */
  140. toDER(): Buffer;
  141. /**
  142. * Returns an PEM formatted string of this PrivateKey
  143. */
  144. toPEM(): string;
  145. /**
  146. * Return a friendly JSON object for debuging.
  147. */
  148. toJSON(): any;
  149. protected [inspect.custom](_depth: any, options: any): string;
  150. }
  151. /**
  152. * PKCS#1 RSA Public Key
  153. */
  154. export declare class RSAPublicKey extends PublicKey {
  155. static fromPublicKey(publicKey: PublicKey): RSAPublicKey;
  156. readonly modulus: string;
  157. readonly exponent: number;
  158. protected _pkcs1: ASN1;
  159. constructor(obj: ASN1);
  160. /**
  161. * Returns an PKCS#1 ASN.1 object of this RSAPublicKey
  162. */
  163. toASN1(): ASN1;
  164. /**
  165. * Returns an PKCS#1 DER formatted buffer of this RSAPublicKey
  166. */
  167. toDER(): Buffer;
  168. /**
  169. * Returns an PKCS#1 PEM formatted string of this RSAPublicKey
  170. */
  171. toPEM(): string;
  172. /**
  173. * Returns an PKCS#8 PEM formatted string of this RSAPublicKey
  174. */
  175. toPublicKeyPEM(): string;
  176. /**
  177. * Return a friendly JSON object for debuging.
  178. */
  179. toJSON(): any;
  180. protected [inspect.custom](_depth: any, options: any): string;
  181. }
  182. /**
  183. * PKCS#1 RSA Private Key
  184. */
  185. export declare class RSAPrivateKey extends PrivateKey {
  186. static fromPrivateKey(privateKey: PrivateKey): RSAPrivateKey;
  187. readonly publicExponent: number;
  188. readonly privateExponent: string;
  189. readonly modulus: string;
  190. readonly prime1: string;
  191. readonly prime2: string;
  192. readonly exponent1: string;
  193. readonly exponent2: string;
  194. readonly coefficient: string;
  195. protected _pkcs1: ASN1;
  196. constructor(obj: ASN1);
  197. /**
  198. * Returns an PKCS#1 ASN.1 object of this RSAPrivateKey
  199. */
  200. toASN1(): ASN1;
  201. /**
  202. * Returns an PKCS#1 DER formatted buffer of this RSAPrivateKey
  203. */
  204. toDER(): Buffer;
  205. /**
  206. * Returns an PKCS#1 PEM formatted string of this RSAPrivateKey
  207. */
  208. toPEM(): string;
  209. /**
  210. * Returns an PKCS#8 PEM formatted string of this RSAPrivateKey
  211. */
  212. toPrivateKeyPEM(): string;
  213. /**
  214. * Return a friendly JSON object for debuging.
  215. */
  216. toJSON(): any;
  217. protected [inspect.custom](_depth: any, options: any): string;
  218. }