【问题标题】:How can you get yourself a key and certificate pair for using in https?您如何获得在 https 中使用的密钥和证书对?
【发布时间】:2013-11-09 05:31:36
【问题描述】:

我正在尝试在 Node.js 中使用 https 模块。

代码如下:

var options = {
    key : <key comes here>,
    cert : <key comes here>
};

https.createServer(options, app).listen(app.get('port'));

【问题讨论】:

    标签: node.js security https openssl


    【解决方案1】:

    您可以使用 OpenSSL, 私钥是这样创建的

    openssl genrsa -out ryans-key.pem 1024
    

    获取证书的第一步是创建“证书签名请求”(CSR) 文件。这是通过以下方式完成的:

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

    要使用 CSR 创建自签名证书,请执行以下操作:

    openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
    

    要创建 .pfx 或 .p12,请执行以下操作:

    openssl pkcs12 -export -in agent5-cert.pem -inkey agent5-key.pem \
        -certfile ca-cert.pem -out agent5.pfx
    

    这是一个简单的回显服务器示例

    var tls = require('tls');
    var fs = require('fs');
    
    var options = {
      key: fs.readFileSync('server-key.pem'),
      cert: fs.readFileSync('server-cert.pem'),
    
      // This is necessary only if using the client certificate authentication.
      requestCert: true,
    
      // This is necessary only if the client uses the self-signed certificate.
      ca: [ fs.readFileSync('client-cert.pem') ]
    };
    
    var server = tls.createServer(options, function(cleartextStream) {
      console.log('server connected',
                  cleartextStream.authorized ? 'authorized' : 'unauthorized');
      cleartextStream.write("welcome!\n");
      cleartextStream.setEncoding('utf8');
      cleartextStream.pipe(cleartextStream);
    });
    server.listen(8000, function() {
      console.log('server bound');
    });
    

    您可以参考http://nodejs.org/api/tls.html了解更多信息,

    问候

    【讨论】:

      【解决方案2】:

      按照Node.js website 上的openssl 说明进行操作。

      【讨论】:

        猜你喜欢
        • 2019-02-05
        • 2018-10-29
        • 2019-07-30
        • 2011-01-04
        • 2019-02-15
        • 2023-03-30
        • 2014-08-29
        • 2021-05-05
        • 1970-01-01
        相关资源
        最近更新 更多