您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tls.d.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. declare module "tls" {
  2. import * as crypto from "crypto";
  3. import * as dns from "dns";
  4. import * as net from "net";
  5. import * as stream from "stream";
  6. const CLIENT_RENEG_LIMIT: number;
  7. const CLIENT_RENEG_WINDOW: number;
  8. interface Certificate {
  9. /**
  10. * Country code.
  11. */
  12. C: string;
  13. /**
  14. * Street.
  15. */
  16. ST: string;
  17. /**
  18. * Locality.
  19. */
  20. L: string;
  21. /**
  22. * Organization.
  23. */
  24. O: string;
  25. /**
  26. * Organizational unit.
  27. */
  28. OU: string;
  29. /**
  30. * Common name.
  31. */
  32. CN: string;
  33. }
  34. interface PeerCertificate {
  35. subject: Certificate;
  36. issuer: Certificate;
  37. subjectaltname: string;
  38. infoAccess: { [index: string]: string[] | undefined };
  39. modulus: string;
  40. exponent: string;
  41. valid_from: string;
  42. valid_to: string;
  43. fingerprint: string;
  44. ext_key_usage: string[];
  45. serialNumber: string;
  46. raw: Buffer;
  47. }
  48. interface DetailedPeerCertificate extends PeerCertificate {
  49. issuerCertificate: DetailedPeerCertificate;
  50. }
  51. interface CipherNameAndProtocol {
  52. /**
  53. * The cipher name.
  54. */
  55. name: string;
  56. /**
  57. * SSL/TLS protocol version.
  58. */
  59. version: string;
  60. }
  61. class TLSSocket extends net.Socket {
  62. /**
  63. * Construct a new tls.TLSSocket object from an existing TCP socket.
  64. */
  65. constructor(socket: net.Socket, options?: {
  66. /**
  67. * An optional TLS context object from tls.createSecureContext()
  68. */
  69. secureContext?: SecureContext,
  70. /**
  71. * If true the TLS socket will be instantiated in server-mode.
  72. * Defaults to false.
  73. */
  74. isServer?: boolean,
  75. /**
  76. * An optional net.Server instance.
  77. */
  78. server?: net.Server,
  79. /**
  80. * If true the server will request a certificate from clients that
  81. * connect and attempt to verify that certificate. Defaults to
  82. * false.
  83. */
  84. requestCert?: boolean,
  85. /**
  86. * If true the server will reject any connection which is not
  87. * authorized with the list of supplied CAs. This option only has an
  88. * effect if requestCert is true. Defaults to false.
  89. */
  90. rejectUnauthorized?: boolean,
  91. /**
  92. * An array of strings or a Buffer naming possible NPN protocols.
  93. * (Protocols should be ordered by their priority.)
  94. */
  95. NPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array,
  96. /**
  97. * An array of strings or a Buffer naming possible ALPN protocols.
  98. * (Protocols should be ordered by their priority.) When the server
  99. * receives both NPN and ALPN extensions from the client, ALPN takes
  100. * precedence over NPN and the server does not send an NPN extension
  101. * to the client.
  102. */
  103. ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array,
  104. /**
  105. * SNICallback(servername, cb) <Function> A function that will be
  106. * called if the client supports SNI TLS extension. Two arguments
  107. * will be passed when called: servername and cb. SNICallback should
  108. * invoke cb(null, ctx), where ctx is a SecureContext instance.
  109. * (tls.createSecureContext(...) can be used to get a proper
  110. * SecureContext.) If SNICallback wasn't provided the default callback
  111. * with high-level API will be used (see below).
  112. */
  113. SNICallback?: (servername: string, cb: (err: Error | null, ctx: SecureContext) => void) => void,
  114. /**
  115. * An optional Buffer instance containing a TLS session.
  116. */
  117. session?: Buffer,
  118. /**
  119. * If true, specifies that the OCSP status request extension will be
  120. * added to the client hello and an 'OCSPResponse' event will be
  121. * emitted on the socket before establishing a secure communication
  122. */
  123. requestOCSP?: boolean
  124. });
  125. /**
  126. * A boolean that is true if the peer certificate was signed by one of the specified CAs, otherwise false.
  127. */
  128. authorized: boolean;
  129. /**
  130. * The reason why the peer's certificate has not been verified.
  131. * This property becomes available only when tlsSocket.authorized === false.
  132. */
  133. authorizationError: Error;
  134. /**
  135. * Static boolean value, always true.
  136. * May be used to distinguish TLS sockets from regular ones.
  137. */
  138. encrypted: boolean;
  139. /**
  140. * String containing the selected ALPN protocol.
  141. * When ALPN has no selected protocol, tlsSocket.alpnProtocol equals false.
  142. */
  143. alpnProtocol?: string;
  144. /**
  145. * Returns an object representing the cipher name and the SSL/TLS protocol version of the current connection.
  146. * @returns Returns an object representing the cipher name
  147. * and the SSL/TLS protocol version of the current connection.
  148. */
  149. getCipher(): CipherNameAndProtocol;
  150. /**
  151. * Returns an object representing the peer's certificate.
  152. * The returned object has some properties corresponding to the field of the certificate.
  153. * If detailed argument is true the full chain with issuer property will be returned,
  154. * if false only the top certificate without issuer property.
  155. * If the peer does not provide a certificate, it returns null or an empty object.
  156. * @param detailed - If true; the full chain with issuer property will be returned.
  157. * @returns An object representing the peer's certificate.
  158. */
  159. getPeerCertificate(detailed: true): DetailedPeerCertificate;
  160. getPeerCertificate(detailed?: false): PeerCertificate;
  161. getPeerCertificate(detailed?: boolean): PeerCertificate | DetailedPeerCertificate;
  162. /**
  163. * Returns a string containing the negotiated SSL/TLS protocol version of the current connection.
  164. * The value `'unknown'` will be returned for connected sockets that have not completed the handshaking process.
  165. * The value `null` will be returned for server sockets or disconnected client sockets.
  166. * See https://www.openssl.org/docs/man1.0.2/ssl/SSL_get_version.html for more information.
  167. * @returns negotiated SSL/TLS protocol version of the current connection
  168. */
  169. getProtocol(): string | null;
  170. /**
  171. * Could be used to speed up handshake establishment when reconnecting to the server.
  172. * @returns ASN.1 encoded TLS session or undefined if none was negotiated.
  173. */
  174. getSession(): Buffer | undefined;
  175. /**
  176. * NOTE: Works only with client TLS sockets.
  177. * Useful only for debugging, for session reuse provide session option to tls.connect().
  178. * @returns TLS session ticket or undefined if none was negotiated.
  179. */
  180. getTLSTicket(): Buffer | undefined;
  181. /**
  182. * Initiate TLS renegotiation process.
  183. *
  184. * NOTE: Can be used to request peer's certificate after the secure connection has been established.
  185. * ANOTHER NOTE: When running as the server, socket will be destroyed with an error after handshakeTimeout timeout.
  186. * @param options - The options may contain the following fields: rejectUnauthorized,
  187. * requestCert (See tls.createServer() for details).
  188. * @param callback - callback(err) will be executed with null as err, once the renegotiation
  189. * is successfully completed.
  190. * @return `undefined` when socket is destroy, `false` if negotiaion can't be initiated.
  191. */
  192. renegotiate(options: { rejectUnauthorized?: boolean, requestCert?: boolean }, callback: (err: Error | null) => void): undefined | boolean;
  193. /**
  194. * Set maximum TLS fragment size (default and maximum value is: 16384, minimum is: 512).
  195. * Smaller fragment size decreases buffering latency on the client: large fragments are buffered by
  196. * the TLS layer until the entire fragment is received and its integrity is verified;
  197. * large fragments can span multiple roundtrips, and their processing can be delayed due to packet
  198. * loss or reordering. However, smaller fragments add extra TLS framing bytes and CPU overhead,
  199. * which may decrease overall server throughput.
  200. * @param size - TLS fragment size (default and maximum value is: 16384, minimum is: 512).
  201. * @returns Returns true on success, false otherwise.
  202. */
  203. setMaxSendFragment(size: number): boolean;
  204. /**
  205. * events.EventEmitter
  206. * 1. OCSPResponse
  207. * 2. secureConnect
  208. */
  209. addListener(event: string, listener: (...args: any[]) => void): this;
  210. addListener(event: "OCSPResponse", listener: (response: Buffer) => void): this;
  211. addListener(event: "secureConnect", listener: () => void): this;
  212. addListener(event: "session", listener: (session: Buffer) => void): this;
  213. emit(event: string | symbol, ...args: any[]): boolean;
  214. emit(event: "OCSPResponse", response: Buffer): boolean;
  215. emit(event: "secureConnect"): boolean;
  216. emit(event: "session", session: Buffer): boolean;
  217. on(event: string, listener: (...args: any[]) => void): this;
  218. on(event: "OCSPResponse", listener: (response: Buffer) => void): this;
  219. on(event: "secureConnect", listener: () => void): this;
  220. on(event: "session", listener: (session: Buffer) => void): this;
  221. once(event: string, listener: (...args: any[]) => void): this;
  222. once(event: "OCSPResponse", listener: (response: Buffer) => void): this;
  223. once(event: "secureConnect", listener: () => void): this;
  224. once(event: "session", listener: (session: Buffer) => void): this;
  225. prependListener(event: string, listener: (...args: any[]) => void): this;
  226. prependListener(event: "OCSPResponse", listener: (response: Buffer) => void): this;
  227. prependListener(event: "secureConnect", listener: () => void): this;
  228. prependListener(event: "session", listener: (session: Buffer) => void): this;
  229. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  230. prependOnceListener(event: "OCSPResponse", listener: (response: Buffer) => void): this;
  231. prependOnceListener(event: "secureConnect", listener: () => void): this;
  232. prependOnceListener(event: "session", listener: (session: Buffer) => void): this;
  233. }
  234. interface TlsOptions extends SecureContextOptions {
  235. handshakeTimeout?: number;
  236. requestCert?: boolean;
  237. rejectUnauthorized?: boolean;
  238. NPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array;
  239. ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array;
  240. SNICallback?: (servername: string, cb: (err: Error | null, ctx: SecureContext) => void) => void;
  241. sessionTimeout?: number;
  242. ticketKeys?: Buffer;
  243. }
  244. interface ConnectionOptions extends SecureContextOptions {
  245. host?: string;
  246. port?: number;
  247. path?: string; // Creates unix socket connection to path. If this option is specified, `host` and `port` are ignored.
  248. socket?: net.Socket; // Establish secure connection on a given socket rather than creating a new socket
  249. rejectUnauthorized?: boolean; // Defaults to true
  250. NPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array;
  251. ALPNProtocols?: string[] | Buffer[] | Uint8Array[] | Buffer | Uint8Array;
  252. checkServerIdentity?: typeof checkServerIdentity;
  253. servername?: string; // SNI TLS Extension
  254. session?: Buffer;
  255. minDHSize?: number;
  256. secureContext?: SecureContext; // If not provided, the entire ConnectionOptions object will be passed to tls.createSecureContext()
  257. lookup?: net.LookupFunction;
  258. timeout?: number;
  259. }
  260. class Server extends net.Server {
  261. addContext(hostName: string, credentials: SecureContextOptions): void;
  262. /**
  263. * events.EventEmitter
  264. * 1. tlsClientError
  265. * 2. newSession
  266. * 3. OCSPRequest
  267. * 4. resumeSession
  268. * 5. secureConnection
  269. */
  270. addListener(event: string, listener: (...args: any[]) => void): this;
  271. addListener(event: "tlsClientError", listener: (err: Error, tlsSocket: TLSSocket) => void): this;
  272. addListener(event: "newSession", listener: (sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void) => void): this;
  273. addListener(event: "OCSPRequest", listener: (certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void) => void): this;
  274. addListener(event: "resumeSession", listener: (sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void) => void): this;
  275. addListener(event: "secureConnection", listener: (tlsSocket: TLSSocket) => void): this;
  276. emit(event: string | symbol, ...args: any[]): boolean;
  277. emit(event: "tlsClientError", err: Error, tlsSocket: TLSSocket): boolean;
  278. emit(event: "newSession", sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void): boolean;
  279. emit(event: "OCSPRequest", certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void): boolean;
  280. emit(event: "resumeSession", sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void): boolean;
  281. emit(event: "secureConnection", tlsSocket: TLSSocket): boolean;
  282. on(event: string, listener: (...args: any[]) => void): this;
  283. on(event: "tlsClientError", listener: (err: Error, tlsSocket: TLSSocket) => void): this;
  284. on(event: "newSession", listener: (sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void) => void): this;
  285. on(event: "OCSPRequest", listener: (certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void) => void): this;
  286. on(event: "resumeSession", listener: (sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void) => void): this;
  287. on(event: "secureConnection", listener: (tlsSocket: TLSSocket) => void): this;
  288. once(event: string, listener: (...args: any[]) => void): this;
  289. once(event: "tlsClientError", listener: (err: Error, tlsSocket: TLSSocket) => void): this;
  290. once(event: "newSession", listener: (sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void) => void): this;
  291. once(event: "OCSPRequest", listener: (certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void) => void): this;
  292. once(event: "resumeSession", listener: (sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void) => void): this;
  293. once(event: "secureConnection", listener: (tlsSocket: TLSSocket) => void): this;
  294. prependListener(event: string, listener: (...args: any[]) => void): this;
  295. prependListener(event: "tlsClientError", listener: (err: Error, tlsSocket: TLSSocket) => void): this;
  296. prependListener(event: "newSession", listener: (sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void) => void): this;
  297. prependListener(event: "OCSPRequest", listener: (certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void) => void): this;
  298. prependListener(event: "resumeSession", listener: (sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void) => void): this;
  299. prependListener(event: "secureConnection", listener: (tlsSocket: TLSSocket) => void): this;
  300. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  301. prependOnceListener(event: "tlsClientError", listener: (err: Error, tlsSocket: TLSSocket) => void): this;
  302. prependOnceListener(event: "newSession", listener: (sessionId: Buffer, sessionData: Buffer, callback: (err: Error, resp: Buffer) => void) => void): this;
  303. prependOnceListener(event: "OCSPRequest", listener: (certificate: Buffer, issuer: Buffer, callback: (err: Error | null, resp: Buffer) => void) => void): this;
  304. prependOnceListener(event: "resumeSession", listener: (sessionId: Buffer, callback: (err: Error, sessionData: Buffer) => void) => void): this;
  305. prependOnceListener(event: "secureConnection", listener: (tlsSocket: TLSSocket) => void): this;
  306. }
  307. interface SecurePair {
  308. encrypted: TLSSocket;
  309. cleartext: TLSSocket;
  310. }
  311. type SecureVersion = 'TLSv1.2' | 'TLSv1.1' | 'TLSv1';
  312. interface SecureContextOptions {
  313. pfx?: string | Buffer | Array<string | Buffer | Object>;
  314. key?: string | Buffer | Array<Buffer | Object>;
  315. passphrase?: string;
  316. cert?: string | Buffer | Array<string | Buffer>;
  317. ca?: string | Buffer | Array<string | Buffer>;
  318. ciphers?: string;
  319. honorCipherOrder?: boolean;
  320. ecdhCurve?: string;
  321. clientCertEngine?: string;
  322. crl?: string | Buffer | Array<string | Buffer>;
  323. dhparam?: string | Buffer;
  324. secureOptions?: number; // Value is a numeric bitmask of the `SSL_OP_*` options
  325. secureProtocol?: string; // SSL Method, e.g. SSLv23_method
  326. sessionIdContext?: string;
  327. /**
  328. * Optionally set the maximum TLS version to allow. One
  329. * of `TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified along with the
  330. * `secureProtocol` option, use one or the other. **Default:** `'TLSv1.2'`.
  331. */
  332. maxVersion?: SecureVersion;
  333. /**
  334. * Optionally set the minimum TLS version to allow. One
  335. * of `TLSv1.2'`, `'TLSv1.1'`, or `'TLSv1'`. Cannot be specified along with the
  336. * `secureProtocol` option, use one or the other. It is not recommended to use
  337. * less than TLSv1.2, but it may be required for interoperability.
  338. * **Default:** `'TLSv1.2'`, unless changed using CLI options. Using
  339. * `--tls-v1.0` changes the default to `'TLSv1'`. Using `--tls-v1.1` changes
  340. * the default to `'TLSv1.1'`.
  341. */
  342. minVersion?: SecureVersion;
  343. }
  344. interface SecureContext {
  345. context: any;
  346. }
  347. /*
  348. * Verifies the certificate `cert` is issued to host `host`.
  349. * @host The hostname to verify the certificate against
  350. * @cert PeerCertificate representing the peer's certificate
  351. *
  352. * Returns Error object, populating it with the reason, host and cert on failure. On success, returns undefined.
  353. */
  354. function checkServerIdentity(host: string, cert: PeerCertificate): Error | undefined;
  355. function createServer(secureConnectionListener?: (socket: TLSSocket) => void): Server;
  356. function createServer(options: TlsOptions, secureConnectionListener?: (socket: TLSSocket) => void): Server;
  357. function connect(options: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
  358. function connect(port: number, host?: string, options?: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
  359. function connect(port: number, options?: ConnectionOptions, secureConnectListener?: () => void): TLSSocket;
  360. /**
  361. * @deprecated
  362. */
  363. function createSecurePair(credentials?: SecureContext, isServer?: boolean, requestCert?: boolean, rejectUnauthorized?: boolean): SecurePair;
  364. function createSecureContext(details: SecureContextOptions): SecureContext;
  365. function getCiphers(): string[];
  366. const DEFAULT_ECDH_CURVE: string;
  367. }