我正在设置一个新的ssl服务器,并想使用node js代码创建一个CA
我想将此OpenSSL命令转换为NodeJS代码
openssl genrsa -aes256 -passout pass:123456 -out intermediate/private/client.key.pem 2048
openssl req -config intermediate/openssl.cnf -passin pass:123456 -key intermediate/private/client.key.pem -new -sha256 -out intermediate/csr/client.csr.pem -subj "/C=US/ST=California/L=Mountain View/O=Alice Ltd/OU=Alice Ltd Web Services/CN=client"
openssl ca -batch -config intermediate/openssl.cnf -extensions usr_cert -days 375 -notext -md sha256 -key intermediate -in intermediate/csr/client.csr.pem -out intermediate/certs/client.cert.pem
openssl x509 -noout -text -in intermediate/certs/client.cert.pem
openssl verify -CAfile intermediate/certs/ca-chain.cert.pem intermediate/certs/client.cert.pem
这是使用OpenSSL创建证书的方法 OpenSSL Certificate Authority
我尝试了这个节点库pem
这是我已结束的问题can't create csr from private key #244 github
此代码可生成带有密码的 privateKey
var privateKeyOptions = {
cipher: 'aes256',
password: '123456'
}
pem.createPrivateKey(2048, privateKeyOptions, function(err, key) {
console.log(key.key)
fs.writeFile('/root/ca/intermediate/private/client.key.pem', key.key, function(err) {
if (err) {
throw err
}
});
});
当我尝试使用此代码使用此密钥生成 CSR
时var csrOptions = {
clientKey: '/root/ca/intermediate/private/client.key.pem',
clientKeyPassword: '123456',
hash: 'sha256',
country: 'US',
state: 'California',
locality: 'Mountain View',
organization: 'Alice Ltd',
organizationUnit: 'Alice Ltd Web Services',
commonName: 'pass:client',
}
pem.createCSR( csrOptions , function(err, csr) {
if (err) {
throw err
} else {
console.log(csr.clientKey)
console.log(csr.csr)
}
});
我收到此错误
/root/sslnode/index2.js:37
抛出错误 ^
错误:无效的openssl退出代码:1
%openssl req -new -sha256 -config /root/ca/intermediate/openssl.cnf -key / tmp / 54f976cb9cbd0e2dd53b755badb6e6e3fe2256ad -passin文件:/ tmp / 3f4640f1d95ca955f1c44c7f2c4b729347813
无法加载私钥 140563986715072:错误:0906D06C:PEM例程:PEM_read_bio:无法启动>行:../ crypto / pem / pem_lib.c:691:正在检查:任何私钥
完成后(/root/sslnode/node_modules/pem/lib/openssl.js:158:27)
在ChildProcess。 (/root/sslnode/node_modules/pem/lib/openssl.js:175:7)
在ChildProcess.emit(events.js:188:13)
atmayClose(internal / child_process.js:978:16)
在Process.ChildProcess._handle.onexit(内部/child_process.js:265:5)
答案 0 :(得分:1)
搜索后,我在
中发现错误clientKey: '/root/ca/intermediate/private/client.key.pem',
此参数使用字符串而不是路径
clientKey: fs.readFileSync('/root/ca/intermediate/private/client.key.pem'),
,无需定义openssl.cnf
csrConfigFile: '/root/ca/intermediate/openssl.cnf'