Socket
Socket
Sign inDemoInstall

pem

Package Overview
Dependencies
2
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

pem


Version published
Weekly downloads
244K
decreased by-15.34%
Maintainers
1
Created
Weekly downloads
 

Package description

What is pem?

The 'pem' npm package is a tool for creating and managing PEM (Privacy Enhanced Mail) files, which are commonly used for SSL/TLS certificates and keys. It provides a variety of functionalities for generating, reading, and managing these files.

What are pem's main functionalities?

Generate SSL Certificates

This feature allows you to generate self-signed SSL certificates. The code sample demonstrates how to create a certificate that is valid for one day.

const pem = require('pem');
pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
  if (err) {
    throw err;
  }
  console.log(keys);
});

Read Certificate Information

This feature allows you to read and extract information from an existing certificate. The code sample shows how to read certificate information from a specified file path.

const pem = require('pem');
pem.readCertificateInfo('path/to/certificate.pem', function (err, info) {
  if (err) {
    throw err;
  }
  console.log(info);
});

Create Certificate Signing Request (CSR)

This feature allows you to create a Certificate Signing Request (CSR), which is used to request a certificate from a Certificate Authority (CA). The code sample demonstrates how to create a CSR with a specified common name.

const pem = require('pem');
pem.createCSR({ commonName: 'example.com' }, function (err, csr) {
  if (err) {
    throw err;
  }
  console.log(csr);
});

Create Private Key

This feature allows you to generate a private key. The code sample shows how to create a private key using the 'pem' package.

const pem = require('pem');
pem.createPrivateKey(function (err, key) {
  if (err) {
    throw err;
  }
  console.log(key);
});

Other packages similar to pem

Changelog

Source

v1.9.1

28 November 2016

  • fix: travis 414b433
  • improvement: travis versions 42b095f
  • improvement: use in forEach unlinkSync instand of unlink (async) 2f7a6b6

Readme

Source

pem

Create private keys and certificates with node.js

Build Status npm version

Installation

Install with npm

npm install pem

Examples

Here are some examples for creating an SSL key/cert on the fly, and running an HTTPS server on port 443. 443 is the standard HTTPS port, but requires root permissions on most systems. To get around this, you could use a higher port number, like 4300, and use https://localhost:4300 to access your server.

Basic https

var https = require('https'),
    pem = require('pem');

pem.createCertificate({days:1, selfSigned:true}, function(err, keys){
    https.createServer({key: keys.serviceKey, cert: keys.certificate}, function(req, res){
        res.end('o hai!')
    }).listen(443);
});

Express

var https = require('https'),
    pem = require('pem'),
    express = require('express');

pem.createCertificate({days:1, selfSigned:true}, function(err, keys){
  var app = express();

  app.get('/',  requireAuth, function(req, res){
    res.send('o hai!');
  });

  https.createServer({key: keys.serviceKey, cert: keys.certificate}, app).listen(443);
});

API

Create a dhparam key

Use createDhparam for creating dhparam keys

pem.createDhparam(keyBitsize, callback)

Where

  • keyBitsize is an optional size of the key, defaults to 512 (bit)
  • callback is a callback function with an error object and {dhparam}

Create a private key

Use createPrivateKey for creating private keys

pem.createPrivateKey(keyBitsize, [options,] callback)

Where

  • keyBitsize is an optional size of the key, defaults to 2048 (bit)
  • options is an optional object of the cipher and password (both required for encryption), defaults {cipher:'',password:''} (ciphers:["aes128", "aes192", "aes256", "camellia128", "camellia192", "camellia256", "des", "des3", "idea"])
  • callback is a callback function with an error object and {key}

Create a Certificate Signing Request

Use createCSR for creating certificate signing requests

pem.createCSR(options, callback)

Where

  • options is an optional options object
  • callback is a callback function with an error object and {csr, clientKey}

Possible options are the following

  • clientKey is an optional client key to use
  • clientKeyPassword the optional password for clientKey
  • keyBitsize - if clientKey is undefined, bit size to use for generating a new key (defaults to 2048)
  • hash is a hash function to use (either md5, sha1 or sha256, defaults to sha256)
  • country is a CSR country field
  • state is a CSR state field
  • locality is a CSR locality field
  • organization is a CSR organization field
  • organizationUnit is a CSR organizational unit field
  • commonName is a CSR common name field (defaults to localhost)
  • altNames is a list (Array) of subjectAltNames in the subjectAltName field (optional)
  • emailAddress is a CSR email address field
  • csrConfigFile is a CSR config file

Create a certificate

Use createCertificate for creating private keys

pem.createCertificate(options, callback)

Where

  • options is an optional options object
  • callback is a callback function with an error object and {certificate, csr, clientKey, serviceKey}

Possible options include all the options for createCSR - in case csr parameter is not defined and a new CSR needs to be generated.

In addition, possible options are the following

  • serviceKey is a private key for signing the certificate, if not defined a new one is generated
  • serviceKeyPassword Password of the service key
  • serviceCertificate is the optional certificate for the serviceKey
  • serial is the unique serial number for the signed certificate, required if serviceCertificate is defined
  • selfSigned - if set to true and serviceKey is not defined, use clientKey for signing
  • csr is a CSR for the certificate, if not defined a new one is generated
  • days is the certificate expire time in days
  • extFile extension config file - without -extensions v3_req
  • config extension config file - with -extensions v3_req

Export a public key

Use getPublicKey for exporting a public key from a private key, CSR or certificate

pem.getPublicKey(certificate, callback)

Where

  • certificate is a PEM encoded private key, CSR or certificate
  • callback is a callback function with an error object and {publicKey}

Read certificate info

Use readCertificateInfo for reading subject data from a certificate or a CSR

pem.readCertificateInfo(certificate, callback)

Where

  • certificate is a PEM encoded CSR or a certificate
  • callback is a callback function with an error object and {serial, country, state, locality, organization, organizationUnit, commonName, emailAddress, validity{start, end}, san{dns, ip}?, issuer{country, state, locality, organization, organizationUnit} }

? san is only present if the CSR or certificate has SAN entries.

Get fingerprint

Use getFingerprint to get the default SHA1 fingerprint for a certificate

pem.getFingerprint(certificate, [hash,] callback)

Where

  • certificate is a PEM encoded certificate
  • hash is a hash function to use (either md5, sha1 or sha256, defaults to sha1)
  • callback is a callback function with an error object and {fingerprint}

Get modulus

Use getModulus to get the modulus for a certificate, a CSR or a private key. Modulus can be useful to check that a Private Key Matches a Certificate

pem.getModulus(certificate, [password,] callback)

Where

  • certificate is a PEM encoded certificate, CSR or private key
  • callback is a callback function with an error object and {modulus}

Get DH parameter information

Use getDhparamInfo to get the size and prime of DH parameters.

pem.getDhparamInfo(dhparam, callback)

Where

  • dhparam is a PEM encoded DH parameters string
  • callback is a callback function with an error object and {size, prime}

Export to a PKCS12 keystore

Use createPkcs12 to export a certificate, the private key and optionally any signing or intermediate CA certificates to a PKCS12 keystore.

pem.createPkcs12(clientKey, certificate, p12Password, [options], callback)

Where

  • clientKey is a PEM encoded private key
  • certificate is a PEM encoded certificate
  • p12Password is the password of the exported keystore
  • options is an optional options object with cipher, (one of "aes128", "aes192", "aes256", "camellia128", "camellia192", "camellia256", "des", "des3" or "idea"), clientKeyPassword and certFiles (an array of additional certificates to include - e.g. CA certificates)
  • callback is a callback function with an error object and {pkcs12} (binary)

Read a PKCS12 keystore

Use readPkcs12 to read a certificate, private key and CA certificates from a PKCS12 keystore.

pem.readPkcs12(bufferOrPath, [options], callback)

Where

  • bufferOrPath is a PKCS12 keystore as a Buffer or the path to a file
  • options is an optional options object with clientKeyPassword which will be used to encrypt the stored key and p12Password which will be used to open the keystore
  • callback is a callback function with an error object and {key: String, cert: String, ca: Array}

### Verify a certificate signing chain

Use verifySigningChain to assert that a given certificate has a valid signing chain.

pem.verifySigningChain(certificate, ca, callback)

Where

  • certificate is a PEM encoded certificate string
  • ca is a PEM encoded CA certificate string or an array of certificate strings
  • callback is a callback function with an error object and a boolean as arguments

Setting openssl location

In some systems the openssl executable might not be available by the default name or it is not included in $PATH. In this case you can define the location of the executable yourself as a one time action after you have loaded the pem module:

var pem = require('pem');
pem.config({
    pathOpenSSL: '/usr/local/bin/openssl'
});
...
// do something with the pem module

License

MIT

FAQs

Last updated on 28 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc