0

I'm trying to create HTTPS server with Node.js. I have followed some instructions from web and end up with this code:

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('D:\\NodeJs\\HTTPS\\keys\\pvtkey.pem', 'utf8'),
  cert: fs.readFileSync('D:\\NodeJs\\HTTPS\\keys\\cert.pem', 'utf8'),
  requestCert: false,
  rejectUnauthorized: false
};

https.createServer(options, function (req, res) {

  if (req.client.authorized) {
        res.writeHead(200, {"Content-Type": "application/json"});
        res.end('{"status":"approved"}');
    } else {
        res.writeHead(401, {"Content-Type": "application/json"});
        res.end('{"status":"denied"}');
    }

}).listen(443);

console.log('start listing');

When trying access from Chrome, I get the following error:

SSL connection error Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have. Error code: ERR_SSL_PROTOCOL_ERROR

Any advice?

aygul
  • 3,227
  • 12
  • 38
  • 42
Meto
  • 420
  • 2
  • 7
  • 17
  • You **MUST** have a valid SSL certificate installed on your server in order to utilize HTTPS connections. – Bud Damyanov Oct 02 '14 at 11:30
  • Read this question: http://stackoverflow.com/questions/6599470/node-js-socket-io-with-ssl – Bud Damyanov Oct 02 '14 at 11:53
  • I have changed https options to take pfx it works but with "identity not verified" – Meto Oct 02 '14 at 12:07
  • @AhmedMetwally: "identity not verified" is a problem with your certificate. Better ask another question about this problem and don't forget to include the details of your certificate (issuer, subject, SAN...) – Steffen Ullrich Oct 03 '14 at 06:13

2 Answers2

0

The problem is that your SSL certificate is either self signed or there is no SSL certificate available.

To bypass testing of SSL certificates you can add the following line:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
0

if you use windows server

I suggest the following steps

step 1:

To buy ssl from a trusted source, I used ssls.com

step 2:

install ssl on iis

step 3:

export ssl certificate to pfx file, use digiCertUtil.exe download here

enter image description here

enter image description here

step 4:

generate privkey.pem and fullchain.pem files use openssl

openssl pkcs12 -in example.pfx -nocerts -nodes -out privkey.pem -passin pass:pfx-password -passout pass:new-password

openssl pkcs12 -in example.pfx -nokeys -out fullchain.pem -passin pass:pfx-password -passout pass:new-password

step 5:

use in node.js app

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

var options = {
  key: fs.readFileSync('privkey.pem'),
  cert: fs.readFileSync('fullchain.pem')
};


var app = express();

app.get('/', (req, res) => {
  res.send('Hollo world')
})

https.createServer(options, app).listen(8883);