14
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

let's encrypt を node.js でつかう

Last updated at Posted at 2016-04-30

letsencrypt with node.js

個人的には今までSSL系はオレオレ証明書でなんとかしのぎ切ってきましたが、やりたいこと的にどうしてもvalidなSSL証明証が必要だったのでletsencryptを始めました。
記事内のexample.comは自分の持ってるドメインでやってね。

let's encrypt

https://letsencrypt.jp/ を参考に /etc/letsencrypt/配下に証明書などが揃った状態にします。

# ls /etc/letsencrypt/live/example.com/
cert.pem  chain.pem  fullchain.pem  privkey.pem

letsencryptの環境設定で80番ポートが未使用状態であることはキモっぽいです。

node.jsでつかう

app.js
"use strict"
const   
https = require('https'),
fs = require('fs'),
conf = require('./conf');
        
const httpsServ = https.createServer(
    {   
        key: fs.readFileSync (conf.key),
        cert: [fs.readFileSync(conf.cert)],
        ca:   [fs.readFileSync(conf.chain), fs.readFileSync(conf.fullchain)]
    },  
    (req, res) =>{
        res.write("hello");
        res.end();
    }
).listen(3001);
conf
exports.key='/etc/letsencrypt/live/example.com/privkey.pem';
exports.cert='/etc/letsencrypt/live/example.com/cert.pem';
exports.chain='/etc/letsencrypt/live/example.com/chain.pem';
exports.fullchain='/etc/letsencrypt/live/example.com/fullchain.pem';

/etc/letsencrypt/live配下はrootユーザでしか読み込めない設定になってるので適当になんとかすること。

curlで動作確認

修正に伴いこの項目の結果

$ curl https://example.com:3001
curl: (60) Peer's Certificate issuer is not recognized.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

どうも、自分の環境の情報だと単純にはできないらしい...
curlに-kオプションをつけるか以下のようにやることで、動作を確認。

$ curl https://example.com:3001 --cacert /etc/letsencrypt/live/example.com/fullchain.pem
hello

とりあえず大丈夫そう。。。かな?

2015/05/11修正
CAにfullchain.pemとchain.pemを指定すると怪しい挙動がなくなったので指定するのが適切な模様。

14
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?