Implementing HTTPS Servers and Clients

Hypertext Transfer Protocol Secure (HTTPS) is a communications protocol that provides secure communication between HTTP clients and servers. HTTPS is really just HTTP running on top of the Transport Layer Security/Secure Sockets Layer (TLS/SSL) protocol, which is where it gets its security capabilities. HTTPS provides security in two main ways. First, it uses long-term public and secret keys to exchange a short-term session key so that data can be encrypted between client and server. It also provides authentication so that you can ensure that the webserver you are connecting to is the one you actually think it is, thus preventing man-in-the-middle attacks, in which requests are rerouted through a third party.

The following sections discuss implementing HTTPS servers and clients in your Node.js environment, using the https module. Before getting started using HTTPS, you need to generate a private key and a public certificate. There are several ways to do this, depending on your platform. One of the simplest methods is to use the OpenSSL library for your platform.

To generate a private key, first execute the following OpenSSL commands to generate a private key:

openssl genrsa -out server.pem 2048

Next, use the following command to create a certificate-signing request file:

openssl req -new -key server.pem -out server.csr


Note

When creating the certificate-signing request file, you need to answer several questions. When prompted for the common name, you should enter in the domain name of the server you want to connect to. Otherwise, the certificate will not work. Also, you can enter additional domain names and IP addresses in the Subject Alternative Names field.


Then, to create a self-signed certificate that you can use for your own purpose or for testing, use the following command:

openssl x509 -req -days 365 -in server.csr -signkey server.pem -out server.crt


Note

The self-signed certificate is fine for testing purposes and internal use. However, if you are implementing an external web service that needs to be protected on the Internet, you may want to get a certificate signed by a certificate authority. If you want to create a certificate that is signed by a third-party certificate authority, you need to take additional steps.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.156