JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4214
  • Score
    100M100P100Q119419F

Upgrade a regular `net.Stream` connection to a secure `tls` connection.

Package Exports

  • starttls

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (starttls) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Start TLS

Build Status

Upgrade a regular net.Stream connection to a secure tls connection.

Based on code by Andris Reinman, itself based on an older version by Nathan Rajlich.

Usage

This library has one method and accepts either an options hash or a prepared socket as the first argument. It returns a SecurePair.

starttls(options, [onSecure]), starttls(socket, [onSecure])

The following options are supported:

  • socket - if not provided, a socket will be created using net.createConnection
  • host - used to perform automatic certificate identity checking, to guard against MITM attacks
  • port - only used to create a socket (along with the host option) if socket is not provided
  • pair - if you want to provide your own SecurePair object

The onSecure callback is optional and receives null or an error object as the first argument (see below for error cases). Within the callback context, this refers to the same SecurePair object returned by starttls.

var net = require('net');
var starttls = require('starttls');
var options = {
    port: 21,
    host: example.com
};

net.createConnection(options, function() {
    options.socket = this;
    starttls(options, function(err) {
        if (err) {

            // Something bad happened!
            return;
        }

        this.cleartext.write('garbage');
    });
});

You should always check for an error before writing to the stream to avoid man-in-the-middle attacks. Errors are produced in the following cases:

  • the certificate authority authorization check failed or was negative
  • the server identity check was negative

If you only pass a socket object, server identity checking will not be performed automatically. In that case you should perform the check manually.

starttls(socket, function(err) {
    if (!tls.checkServerIdentity(host, this.cleartext.getPeerCertificate())) {

        // Hostname mismatch!
        // Report error and end connection...
    }
});

Example

See socks5-https-client for use-case.

Tests

Run make test or npm test to run tests.

License

Portions of this code copyright (c) 2012, Andris Reinman and copyright (c) 2011, Nathan Rajlich.

Modified and redistributed under an MIT license.