index.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. declare enum ASN1Class {
  2. UNIVERSAL = 0x00,
  3. APPLICATION = 0x40,
  4. CONTEXT_SPECIFIC = 0x80,
  5. PRIVATE = 0xc0,
  6. }
  7. interface CertificateFieldOptions {
  8. name?: string | undefined;
  9. type?: string | undefined;
  10. shortName?: string | undefined;
  11. }
  12. interface CertificateField extends CertificateFieldOptions {
  13. valueConstructed?: boolean | undefined;
  14. valueTagClass?: ASN1Class | undefined;
  15. value?: any[] | string | undefined;
  16. extensions?: any[] | undefined;
  17. }
  18. /**
  19. * Subject Alternative Name entry types:
  20. * - 1: email (rfc822Name)
  21. * - 2: DNS name
  22. * - 6: URI
  23. * - 7: IP address
  24. */
  25. declare interface SubjectAltNameEntry {
  26. /**
  27. * Type of the alternative name:
  28. * - 1: email (rfc822Name)
  29. * - 2: DNS name
  30. * - 6: URI
  31. * - 7: IP address
  32. */
  33. type: 1 | 2 | 6 | 7;
  34. /** Value for types 1, 2, 6 (email, DNS, URI) */
  35. value?: string;
  36. /** IP address for type 7 (IPv4 or IPv6) */
  37. ip?: string;
  38. }
  39. declare interface BasicConstraintsExtension {
  40. name: 'basicConstraints';
  41. /** Is this a CA certificate? */
  42. cA?: boolean;
  43. /** Maximum depth of valid certificate chain */
  44. pathLenConstraint?: number;
  45. /** Mark extension as critical */
  46. critical?: boolean;
  47. }
  48. declare interface KeyUsageExtension {
  49. name: 'keyUsage';
  50. digitalSignature?: boolean;
  51. nonRepudiation?: boolean;
  52. /** Also known as contentCommitment */
  53. contentCommitment?: boolean;
  54. keyEncipherment?: boolean;
  55. dataEncipherment?: boolean;
  56. keyAgreement?: boolean;
  57. /** For CA certificates */
  58. keyCertSign?: boolean;
  59. /** For CA certificates */
  60. cRLSign?: boolean;
  61. encipherOnly?: boolean;
  62. decipherOnly?: boolean;
  63. /** Mark extension as critical */
  64. critical?: boolean;
  65. }
  66. declare interface ExtKeyUsageExtension {
  67. name: 'extKeyUsage';
  68. /** TLS server authentication */
  69. serverAuth?: boolean;
  70. /** TLS client authentication */
  71. clientAuth?: boolean;
  72. codeSigning?: boolean;
  73. emailProtection?: boolean;
  74. timeStamping?: boolean;
  75. /** Mark extension as critical */
  76. critical?: boolean;
  77. }
  78. declare interface SubjectAltNameExtension {
  79. name: 'subjectAltName';
  80. altNames: SubjectAltNameEntry[];
  81. /** Mark extension as critical */
  82. critical?: boolean;
  83. }
  84. declare type CertificateExtension =
  85. | BasicConstraintsExtension
  86. | KeyUsageExtension
  87. | ExtKeyUsageExtension
  88. | SubjectAltNameExtension;
  89. declare interface ClientCertificateOptions {
  90. /**
  91. * Key size for the client certificate in bits (RSA only)
  92. * @default 2048
  93. */
  94. keySize?: number
  95. /**
  96. * Key type for client certificate
  97. * @default inherits from main keyType
  98. */
  99. keyType?: 'rsa' | 'ec'
  100. /**
  101. * Elliptic curve for client certificate (EC only)
  102. * @default "P-256"
  103. */
  104. curve?: 'P-256' | 'P-384' | 'P-521'
  105. /**
  106. * Signature algorithm for client certificate
  107. * @default inherits from main algorithm or "sha1"
  108. */
  109. algorithm?: string
  110. /**
  111. * Client certificate's common name
  112. * @default "John Doe jdoe123"
  113. */
  114. cn?: string
  115. /**
  116. * The date before which the client certificate should not be valid
  117. * @default now
  118. */
  119. notBeforeDate?: Date
  120. /**
  121. * The date after which the client certificate should not be valid
  122. * @default notBeforeDate + 1 year
  123. */
  124. notAfterDate?: Date
  125. }
  126. declare interface SelfsignedOptions {
  127. /**
  128. * The date before which the certificate should not be valid
  129. *
  130. * @default now */
  131. notBeforeDate?: Date
  132. /**
  133. * The date after which the certificate should not be valid
  134. *
  135. * @default notBeforeDate + 365 days */
  136. notAfterDate?: Date
  137. /**
  138. * Key type: "rsa" or "ec" (elliptic curve)
  139. * @default "rsa"
  140. */
  141. keyType?: 'rsa' | 'ec'
  142. /**
  143. * the size for the private key in bits (RSA only)
  144. * @default 2048
  145. */
  146. keySize?: number
  147. /**
  148. * The elliptic curve to use (EC only): "P-256", "P-384", or "P-521"
  149. * @default "P-256"
  150. */
  151. curve?: 'P-256' | 'P-384' | 'P-521'
  152. /**
  153. * Certificate extensions. Supports basicConstraints, keyUsage, extKeyUsage, and subjectAltName.
  154. * If not provided, defaults are used including DNS SAN matching commonName.
  155. * @example
  156. * ```typescript
  157. * extensions: [
  158. * { name: 'basicConstraints', cA: false },
  159. * { name: 'keyUsage', digitalSignature: true, keyEncipherment: true },
  160. * { name: 'subjectAltName', altNames: [
  161. * { type: 2, value: 'localhost' },
  162. * { type: 7, ip: '127.0.0.1' },
  163. * { type: 7, ip: '::1' }
  164. * ]}
  165. * ]
  166. * ```
  167. */
  168. extensions?: CertificateExtension[];
  169. /**
  170. * The signature algorithm: sha256, sha384, sha512 or sha1
  171. * @default "sha1"
  172. */
  173. algorithm?: string
  174. /**
  175. * include PKCS#7 as part of the output
  176. * @default false
  177. */
  178. pkcs7?: boolean
  179. /**
  180. * generate client cert signed by the original key
  181. * Can be `true` for defaults or an options object
  182. * @default false
  183. */
  184. clientCertificate?: boolean | ClientCertificateOptions
  185. /**
  186. * client certificate's common name
  187. * @default "John Doe jdoe123"
  188. * @deprecated Use clientCertificate.cn instead
  189. */
  190. clientCertificateCN?: string
  191. /**
  192. * the size for the client private key in bits
  193. * @default 2048
  194. * @deprecated Use clientCertificate.keySize instead
  195. */
  196. clientCertificateKeySize?: number
  197. /**
  198. * existing key pair to use instead of generating new keys
  199. */
  200. keyPair?: {
  201. privateKey: string
  202. publicKey: string
  203. }
  204. /**
  205. * CA certificate and key for signing (if not provided, generates self-signed)
  206. */
  207. ca?: {
  208. /** CA private key in PEM format */
  209. key: string
  210. /** CA certificate in PEM format */
  211. cert: string
  212. }
  213. /**
  214. * Passphrase to encrypt the private key (PKCS#8 encrypted format)
  215. * When provided, the private key will be encrypted using AES-256-CBC
  216. */
  217. passphrase?: string
  218. }
  219. declare interface GenerateResult {
  220. private: string
  221. public: string
  222. cert: string
  223. fingerprint: string
  224. pkcs7?: string
  225. clientprivate?: string
  226. clientpublic?: string
  227. clientcert?: string
  228. clientpkcs7?: string
  229. }
  230. /**
  231. * Generate a certificate (async only)
  232. *
  233. * @param attrs Certificate attributes
  234. * @param opts Generation options
  235. * @returns Promise that resolves with certificate data
  236. *
  237. * @example
  238. * ```typescript
  239. * // Self-signed certificate
  240. * const pems = await generate();
  241. *
  242. * const pems = await generate([{ name: 'commonName', value: 'example.com' }]);
  243. *
  244. * const pems = await generate(null, {
  245. * keySize: 2048,
  246. * algorithm: 'sha256'
  247. * });
  248. *
  249. * // CA-signed certificate
  250. * const pems = await generate([{ name: 'commonName', value: 'localhost' }], {
  251. * algorithm: 'sha256',
  252. * ca: {
  253. * key: fs.readFileSync('/path/to/ca.key', 'utf8'),
  254. * cert: fs.readFileSync('/path/to/ca.crt', 'utf8')
  255. * }
  256. * });
  257. * ```
  258. */
  259. export declare function generate(
  260. attrs?: CertificateField[],
  261. opts?: SelfsignedOptions
  262. ): Promise<GenerateResult>