Bagaimana cara menggunakan diri menandatangani sertifikat untuk HTTPS Node.js server?

Saya sudah mulai menulis sebuah wrapper untuk API yang membutuhkan semua permintaan harus melalui HTTPS. Alih-alih membuat permintaan aktual API sementara saya mengembangkan dan menguji itu saya ingin menjalankan saya sendiri server lokal yang mengolok-olok tanggapan.

Saya bingung tentang bagaimana cara membuat sertifikat yang saya butuhkan untuk membuat HTTPS server dan mengirim permintaan untuk itu.

Server saya terlihat seperti ini:

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

https.createServer(options, function(req, res) {
  res.writeHead(200);
  res.end('OK\n');
}).listen(8000);

Pem file yang dihasilkan dengan:

openssl genrsa 1024 > key.pem
openssl req -x509 -new -key key.pem > cert.pem

Dan permintaan terlihat seperti ini:

var options = {
  host: 'localhost',
  port: 8000,
  path: '/api/v1/test'
};

https.request(options, function(res) {
  res.pipe(process.stdout);
}).end();

Dengan setup ini saya mendapatkan Kesalahan: DEPTH_ZERO_SELF_SIGNED_CERT, jadi saya pikir saya perlu menambahkan ca pilihan untuk permintaan.

Jadi pertanyaan saya adalah bagaimana saya harus menghasilkan berikut ini:

  1. Server key?
  2. Server cert?
  3. The ca untuk permintaan?

Saya telah membaca beberapa hal tentang menghasilkan sertifikat yang ditandatangani dengan openssl, tetapi dapat't tampaknya untuk membungkus kepala saya sekitar itu dan mencari tahu mana kunci dan sertifikat untuk digunakan di mana saya node kode.

Update

API menyediakan sertifikat CA untuk menggunakan bukan default. Kode berikut karya-karya yang menggunakan sertifikat mereka dan ini adalah apa yang saya ingin mereproduksi secara lokal.

var ca = fs.readFileSync('./certificate.pem');

var options = {
  host: 'example.com',
  path: '/api/v1/test',
  ca: ca
};
options.agent = new https.Agent(options);

https.request(options, function(res) {
  res.pipe(process.stdout);
}).end();
Larutan

Update (Jan 2018): Apakah anda need diri menandatangani sertifikat?

Atau akan nyata sertifikat mendapatkan pekerjaan yang dilakukan dengan baik? Apakah anda mempertimbangkan semua ini?

(Catatan: Let's Mengenkripsi juga dapat mengeluarkan sertifikat untuk jaringan pribadi)

ScreenCast

https://coolaj86.com/articles/how-to-create-a-csr-for-https-tls-ssl-rsa-pems/

Lengkap, contoh Kerja

  • menciptakan sertifikat
  • berjalan node.js server
  • tidak ada peringatan atau kesalahan dalam node.js klien
  • tidak ada peringatan atau kesalahan dalam cURL

https://github.com/coolaj86/nodejs-self-signed-certificate-example

Menggunakan localhost.greenlock.domain sebagai contoh (ini poin ke 127.0.0.1):

server.js

'use strict';

var https = require('https')
  , port = process.argv[2] || 8043
  , fs = require('fs')
  , path = require('path')
  , server
  , options
  ;

require('ssl-root-cas')
  .inject()
  .addFile(path.join(__dirname, 'server', 'my-private-root-ca.cert.pem'))
  ;

options = {
  // this is ONLY the PRIVATE KEY
  key: fs.readFileSync(path.join(__dirname, 'server', 'privkey.pem'))
  // You DO NOT specify `ca`, that's only for peer authentication
//, ca: [ fs.readFileSync(path.join(__dirname, 'server', 'my-private-root-ca.cert.pem'))]
  // This should contain both cert.pem AND chain.pem (in that order) 
, cert: fs.readFileSync(path.join(__dirname, 'server', 'fullchain.pem'))
};

function app(req, res) {
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, encrypted world!');
}

server = https.createServer(options, app).listen(port, function () {
  port = server.address().port;
  console.log('Listening on https://127.0.0.1:' + port);
  console.log('Listening on https://' + server.address().address + ':' + port);
  console.log('Listening on https://localhost.greenlock.domains:' + port);
});

client.js

'use strict';

var https = require('https')
  , fs = require('fs')
  , path = require('path')
  , ca = fs.readFileSync(path.join(__dirname, 'client', 'my-private-root-ca.cert.pem'))
  , port = process.argv[2] || 8043
  , hostname = process.argv[3] || 'localhost.greenlock.domains'
  ;

var options = {
  host: hostname
, port: port
, path: '/'
, ca: ca
};
options.agent = new https.Agent(options);

https.request(options, function(res) {
  res.pipe(process.stdout);
}).end();

Dan script yang membuat file sertifikat:

make-certs.sh

#!/bin/bash
FQDN=$1

# make directories to work from
mkdir -p server/ client/ all/

# Create your very own Root Certificate Authority
openssl genrsa \
  -out all/my-private-root-ca.privkey.pem \
  2048

# Self-sign your Root Certificate Authority
# Since this is private, the details can be as bogus as you like
openssl req \
  -x509 \
  -new \
  -nodes \
  -key all/my-private-root-ca.privkey.pem \
  -days 1024 \
  -out all/my-private-root-ca.cert.pem \
  -subj "/C=US/ST=Utah/L=Provo/O=ACME Signing Authority Inc/CN=example.com"

# Create a Device Certificate for each domain,
# such as example.com, *.example.com, awesome.example.com
# NOTE: You MUST match CN to the domain name or ip address you want to use
openssl genrsa \
  -out all/privkey.pem \
  2048

# Create a request from your Device, which your Root CA will sign
openssl req -new \
  -key all/privkey.pem \
  -out all/csr.pem \
  -subj "/C=US/ST=Utah/L=Provo/O=ACME Tech Inc/CN=${FQDN}"

# Sign the request from Device with your Root CA
openssl x509 \
  -req -in all/csr.pem \
  -CA all/my-private-root-ca.cert.pem \
  -CAkey all/my-private-root-ca.privkey.pem \
  -CAcreateserial \
  -out all/cert.pem \
  -days 500

# Put things in their proper place
rsync -a all/{privkey,cert}.pem server/
cat all/cert.pem > server/fullchain.pem         # we have no intermediates in this case
rsync -a all/my-private-root-ca.cert.pem server/
rsync -a all/my-private-root-ca.cert.pem client/

# create DER format crt for iOS Mobile Safari, etc
openssl x509 -outform der -in all/my-private-root-ca.cert.pem -out client/my-private-root-ca.crt

Misalnya:

bash make-certs.sh 'localhost.greenlock.domains'

Mudah-mudahan hal ini menempatkan paku di peti mati yang satu ini.

Dan beberapa penjelasan lebih lanjut: https://github.com/coolaj86/node-ssl-root-cas/wiki/Painless-Self-Signed-Certificates-in-node.js

Install pribadi cert Mobile Safari pada iOS

Yang anda butuhkan untuk membuat salinan dari sertifikat root ca DER format dengan .crt ekstensi:

# create DER format crt for iOS Mobile Safari, etc
openssl x509 -outform der -in all/my-private-root-ca.cert.pem -out client/my-private-root-ca.crt

Kemudian anda hanya dapat melayani file dengan server web anda. Ketika anda mengklik link yang anda harus bertanya jika anda ingin menginstal sertifikat.

Untuk contoh bagaimana ini bekerja, anda dapat mencoba menginstal MIT's Otoritas Sertifikat: https://ca.mit.edu/mitca.crt

Terkait Contoh

Komentar (4)

Coba tambahkan ini dengan permintaan anda pilihan

var options = {
  host: 'localhost',
  port: 8000,
  path: '/api/v1/test',
  // These next three lines
  rejectUnauthorized: false,
  requestCert: true,
  agent: false
};
Komentar (0)

Anda pembangkitan kunci terlihat baik-baik saja. Seharusnya kau't perlu ca karena anda tidak't menolak unsigned permintaan.

Tambahkan .toString() untuk akhir anda readFileSync metode sehingga anda benar-benar melewati sebuah string, bukan file objek.

Komentar (3)

Mencoba menambahkan

  agent: false,
  rejectUnauthorized: false
Komentar (1)

Prosedur ini memungkinkan anda untuk membuat sebuah otoritas sertifikat & sertifikat :

  1. ambil ini ca.cnf file untuk digunakan sebagai konfigurasi shortcut :

wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/ca.cnf


  1. membuat sertifikat baru otoritas menggunakan konfigurasi ini :

openssl req -new -x509 -days 9999 -config ca.cnf -keyout ca-kunci.pem-out ca-cert.pem


  1. sekarang kita telah memiliki sertifikat otoritas di ca-kunci.pem dan ca-cert.pem, let's menghasilkan sebuah private key untuk server :

openssl genrsa-out key.pem 4096


  1. ambil ini server.cnf file untuk digunakan sebagai konfigurasi shortcut :

wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/server.cnf


  1. membuat certificate signing request menggunakan konfigurasi ini :

openssl req -new -config server.cnf -kunci kunci.pem-program csr.pem


  1. tanda permintaan :

openssl x509 -req -extfile server.cnf -hari 999 -passin "pass:password" -dalam csr.pem-CA ca-cert.pem-CAkey ca-kunci.pem-CAcreateserial -out cert.pem

Saya menemukan prosedur ini di sini, bersama dengan informasi lebih lanjut tentang cara untuk menggunakan sertifikat ini.

Komentar (1)