Guest User

Untitled

a guest
Mar 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. TLS Socket: server and client
  2. The only major differences between this and a regular TCP connection are the private Key and the public certificate that you’ll have to set into an option object.
  3. How to Create a Key and Certificate
  4. The first step in this security process is the creation of a private Key. And what is this private key? Basically, it's a set of random noise that's used to encrypt information. In theory, you could create one key, and use it to encrypt whatever you want. But it is best practice to have different keys for specific things. Because if someone steals your private key, it's similar to having someone steal your house keys. Imagine if you used the same key to lock your car, garage, office, etc.
  5.  
  6. openssl genrsa -out private-key.pem 1024
  7.  
  8. Once we have our private key, we can create a CSR (certificate signing request), which is our request to have the private key signed by a fancy authority. That is why you have to input information related to your company. This information will be seen by the signing authority, and used to verify you. In our case, it doesn’t matter what you type, since in the next step we're going to sign our certificate ourselves.
  9.  
  10. openssl req -new -key private-key.pem -out csr.pem
  11.  
  12. Now that we have our paper work filled out, it's time to pretend that we're a cool signing authority.
  13.  
  14. openssl x509 -req -in csr.pem -signkey private-key.pem -out public-cert.pem
  15.  
  16. Now that you have the private key and the public cert, you can establish a secure connection between two NodeJS apps. And, as you can see in the example code, it is a very simple process.
  17.  
  18. Important!
  19. Since we created the public cert ourselves, in all honesty, our certificate is worthless, because we are nobodies. The NodeJS server won't trust such a certificate by default, and that is why we need to tell it to actually trust our cert with the following option rejectUnauthorized: false. Very important: never set this variable to true in a production environment.
Add Comment
Please, Sign In to add comment