I am looking for the default options for nodejs internal crypto module functions generateKeyPairSync
or generateKeyPair
that matches openssl
defaults.
I am trying to create a simple https key
and crt
using the crypto
module. I am using the following code. I have just two questions:
References : https://nodejs.org/api/crypto.html#cryptogeneratekeypairsynctype-options and https://www.openssl.org/docs/faq.html
- What are the defaults options that is used by openssl for creating a rsa .key and .crt keys for simple https server using the crypto nodejs module.
- The keys are specific for a domain www.domain.com. how is that specified if i use the crypto module of nodejs?
openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -days 365 -out yourdomain.csr -subj "/C=US/ST=Utah/L=Lehi/O=Your Company, Inc./OU=IT/CN=yourdomain.com"
- How is this implemented since the crypto module generates the public and private key seperately and the private domain.key contains the public and private key for domain's https module. Extracting Your Public Key - The private key file contains both the
private
key and thepublic
key. You can extract your public key from your private key file if needed. Use the following command to extract your public key:openssl rsa -in yourdomain.key -pubout -out yourdomain_public.key
I am looking to replace the ?
s and the 0
s below in the next code to get the defaults similar to openssl certificates.
const {
generateKeyPairSync,
} = await import('node:crypto');
const {
publicKey,
privateKey,
} = generateKeyPairSync('rsa', {
modulusLength: 2048, // <number> Key size in bits (RSA, DSA). . 4096 works for me
publicExponent: 0, // <number> Public exponent (RSA). Default: 0x10001.
hashAlgorithm: "?", // <string> Name of the message digest (RSA-PSS).
mgf1HashAlgorithm: "?", // <string> Name of the message digest used by MGF1 (RSA-PSS).
saltLength: 0, // <number> Minimal salt length in bytes (RSA-PSS).
divisorLength: 0, // <number> Size of q in bits (DSA).
namedCurve: "?", // <string> Name of the curve to use (EC).
prime: new Buffer("?"), // <Buffer> The prime parameter (DH).
primeLength: 0, // <number> Prime length in bits (DH).
generator: 0, // <number> Custom generator (DH). Default: 2.
groupName: "?", // <string> Diffie-Hellman group name (DH). See crypto.getDiffieHellman().
paramEncoding: "?", // <string> Must be 'named' or 'explicit' (EC). Default: 'named'.
publicKeyEncoding: { // <Object> See keyObject.export().
type: 'spki', // two options: spki or pkcs1
format: 'pem',
},
privateKeyEncoding: { // <Object> See keyObject.export().
type: 'pkcs8', // two options: spki or pkcs1 or >> PBKDF2 as per https://www.openssl.org/docs/man3.0/man1/openssl-pkcs12.html
format: 'pem',
cipher: 'aes-256-cbc', // The default encryption algorithm is AES-256-CBC with PBKDF2 for key derivation as per https://www.openssl.org/docs/man3.0/man1/openssl-pkcs12.html
passphrase: 'top_secret', // do i need this?
},
});
I am then exporting the key to a file as below. I am dumping the files as .pem
files. Just a check is there something else for key extension that I should use?
const fs = require("fs");
var xKpem = key.export({type: "pcks1", format: "pem"});
fs.writeFileSync("./filename.?", xKpem);
I am refering to this document here https://www.openssl.org/docs/man3.0/man1/openssl-pkcs12.html and this reference here https://www.digicert.com/kb/ssl-support/openssl-quick-reference-guide.htm
I will be using this as follows:
const express = require('express')
const app = express()
const https = require('https')
const fs = require('fs')
const port = 3000
app.get('/', (req, res) => {
res.send('WORKING!')
})
const httpsOptions = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
}
const server = https.createServer(httpsOptions, app).listen(port, () => {
console.log('server running')
})