Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pem

Dexus588.9kMIT1.14.8TypeScript support: definitely-typed

Create private keys and certificates with node.js and io.js

readme

pem

Create private keys and certificates with node.js

Build Status npm version npm downloads pem documentation

JavaScript Style Guide

Installation

Install with npm

npm install pem

or use yarn

yarn add pem

:warning: Please make sure you have openssl or libressl already installed on your system/container, without them pem will not work.

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')
var pem = require('pem')

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

Express

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

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

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

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

API

Please have a look into the API documentation.

we had to clean up a bit

Custom extensions config file

You can specify custom OpenSSL extensions using the config or extFile options for createCertificate (or using csrConfigFile with createCSR).

extFile and csrConfigFile should be paths to the extension files. While config will generate a temporary file from the supplied file contents.

If you specify config then the v3_req section of your config file will be used.

The following would be an example of a Certificate Authority extensions file:

[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]
commonName = Common Name
commonName_max = 64

[v3_req]
basicConstraints = critical,CA:TRUE

While the following would specify subjectAltNames in the resulting certificate:

[req]
req_extensions = v3_req

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = host1.example.com
DNS.2 = host2.example.com
DNS.3 = host3.example.com

Note that createCertificate and createCSR supports the altNames option which would be easier to use in most cases.

:warning: Warning: If you specify altNames the custom extensions file will not be passed to OpenSSL.

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

:warning: CSR/Certificates with special chars

For more details, search in test/pem.spec.js: Create CSR with specialchars config file

If you use special chars like:

-!$%^&*()_+|~=`{}[]:/;<>?,.@#

You should know that the result mey have escaped characters when you read it in your application. Will try to fix this in the future, but not sure.

Special thanks to

  • Andris Reinman (@andris9) - Initiator of pem

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v1.14.8

v1.14.7

4 April 2023

  • createCSR: allow special chars add note to README.md #232
  • createCSR: allow specialchars #232
  • createCertificate: now support without CSR and encrypt generated private key #255
  • readPkcs12: openssl v3.0.1 - fix #309 add -legacy && -tranditional option #309
  • Add Tests for #309, #302 #309 #302
  • Update generated files 1fc95c8
  • Update mocha 57d6162
  • Add Helper scripts cd960c4

v1.14.5

20 January 2022

  • Verify signing chain fix #306
  • Fixed typo in README.md #300
  • Chore(deps): Bump lodash from 4.17.15 to 4.17.19 #299
  • Chore(deps): Bump acorn from 7.1.0 to 7.3.1 #298
  • Using clientKey to create http server in examples #293
  • Chore(deps): Bump acorn from 7.1.0 to 7.1.1 #289
  • Chore(deps): Bump npm from 6.13.6 to 6.14.6 #297
  • Update @semantic-release/github to the latest version 🚀 #288
  • Update @semantic-release/changelog to the latest version 🚀 #286
  • Update @semantic-release/npm to the latest version 🚀 #287
  • Update @semantic-release/git to the latest version 🚀 #285
  • Update semantic-release to the latest version 🚀 #284
  • Delete package-lock.json 5e88b20
  • release-v1.yml: and docs e0c9bee
  • Support OPENSSL v3.0.0 & more 7ec8257

v1.14.4

25 January 2020

  • fix: travis.yaml #283
  • fix: update readme and deps 59c17d1
  • chore(release): 1.14.4 [skip ci] bb777c0

v1.14.3

17 September 2019

  • fix readCertificateInfo for the case when cert subject is empty #264
  • Greenkeeper/mocha 6.1.4 #252
  • Chore(deps): Bump lodash from 4.17.11 to 4.17.14 #253
  • Update marked to the latest version 🚀 #249
  • Greenkeeper/eslint plugin import 2.18.0 #250
  • Chore(deps): Bump js-yaml from 3.12.0 to 3.13.1 #248
  • Chore(deps-dev): Bump marked from 0.6.0 to 0.6.1 #247
  • Chore(deps): Bump lodash from 4.17.5 to 4.17.11 #242
  • fix(package): fix #264 & remove package-lock.josn #264
  • Greenkeeper/mocha 6.1.4 (#252) #235
  • chore(package): update eslint-plugin-import to version 2.18.0 #237
  • chore(release): 1.14.3 [skip ci] 97643fa
  • chore(package): update lockfile package-lock.json 200c2b3
  • Update package-lock.json e4051f6

v1.14.2

16 February 2019

  • Update eslint-plugin-import to the latest version 🚀 #231
  • chore(release): 1.14.2 [skip ci] e79cdaa
  • fix: Merge pull request #234 from pvangeepuram/fix-serviceKeyPassword-issue 1649e7b
  • chore(package): update lockfile package-lock.json 1ff03dc

v1.14.1

25 January 2019

  • fix(package): add altNames + csrConfigFile options to createCertificate #230
  • chore(release): 1.14.1 [skip ci] 2dc1b8b

v1.14.0

25 January 2019

  • feat(package): Support SAN Certificate from CSR #229
  • Update eslint-plugin-import to the latest version 🚀 #228
  • Update marked to the latest version 🚀 #226
  • Greenkeeper/semantic release 15.12.5 #225
  • Greenkeeper/ajv 6.6.1 #224
  • Greenkeeper/travis deploy once 5.0.10 #223
  • Update eslint-plugin-node to the latest version 🚀 #218
  • chore(release): 1.14.0 [skip ci] 09aa5e0
  • js fix e5cc4bb

v1.13.2

26 October 2018

  • fix(package): security fix #217
  • chore(release): 1.13.2 [skip ci] 9adaa05
  • Update helper.spec.js 8e8457c
  • Update helper.js 174faaf

v1.13.1

14 September 2018

  • fix(package): remove -utf8 option #214
  • chore(release): 1.13.1 [skip ci] 68ad452

v1.13.0

11 September 2018

  • chore(release): 1.13.0 [skip ci] d5a20dd
  • feat(package): add promisify methods pem.promisify.FUNCTIONNAME 8a0c1bc
  • chore(package): update tested openssl version 4a2754a

v1.12.8

11 September 2018

  • chore(package): dep updates and changelog 98a63c9
  • fix(pem): allow utf8 charset in fields 2cb97aa
  • fix(package): dep updates and fix deprecated api 610a403

v1.12.7

11 September 2018

  • fix(package): fix fetchCertificate to list all infos #196
  • fix(package): fix fetchCertificate to list all infos #196
  • chore(release): 1.12.7 [skip ci] 7d33439

v1.12.6

10 September 2018

  • Added support for -CAserial flag via options.serialFile for createCer… #171
  • Fix broken public key detection from PKCS#8 formatted private keys #212
  • Added support to specify the parameter encoding of an ecparam key #207
  • Update travis-deploy-once to the latest version 🚀 #199
  • Update eslint-plugin-import to the latest version 🚀 #194
  • Update package.json 6a3f259
  • chore(release): 1.12.6 [skip ci] 6975c5d
  • Update HISTORY.md via TravisCI 598a5ef

v1.12.5

10 April 2018

  • fix: Ensure synchronous tmpfile writes, deletes 37d7cdd
  • add package-lock.json fa5106e
  • Ensure synchronous tmpfile writes, deletes 85a00fb

v1.12.4

4 April 2018

  • fix: pin eslint-plugin-import to 2.10.0 #191
  • chore(package): update semantic-release to version 15.1.5 #190
  • chore(package): update cross-env to version 5.1.4 #189
  • Update eslint-config-standard to the latest version 🚀 #186
  • Update eslint-plugin-node to the latest version 🚀 #185
  • Update mocha to the latest version 🚀 #183
  • Update semantic-release to the latest version 🚀 #182
  • Update semantic-release to the latest version 🚀 #178
  • Update travis-deploy-once to the latest version 🚀 #181
  • chore(package): update cross-env to version 5.1.2 #180
  • Update docco to the latest version 🚀 #177
  • readCertificateInfo reports email addresses in subjectAltName field #170
  • chore(package): update semantic-release to version 15.1.5 #182
  • Update HISTORY.md via TravisCI 8e89517
  • Update HISTORY.md via TravisCI 7a10bc0
  • Update HISTORY.md via TravisCI f6a2b63

v1.12.3

10 October 2017

  • Update HISTORY.md via TravisCI 904f74d
  • fix(package): #117 verifySigningChain optional CA certificates b6aafd4

v1.12.2

8 October 2017

  • fix #150: serial can now be an 20 octets string, number or hex #150
  • Update HISTORY.md via TravisCI a99b0ee
  • Update HISTORY.md via TravisCI b58a0b2
  • chore(release-flow): create automatical a changelog before release and finish TravisCI b07cc19

v1.12.1

7 October 2017

  • fix #159; renamed helper methods #159
  • converter and its tests ddf77eb
  • Change readme.md and history.md 1118608
  • reviewed / extended tests and source code c89af35

v1.12.0

29 September 2017

  • fix(package): Re-Structuring #147
  • fix(package): consistent usage of -password -passin and -passout a7c5654
  • feat: add createEcparam to create EC private keys 6c991b5
  • standardjs setup / README.md review 519c2cf

v1.11.2

21 September 2017

  • fix(package): max 20 octets for serial #141
  • fix(package): max 20 octets for serial (#141) #84
  • Update .travis.yml bed4f44

v1.11.1

20 September 2017

  • fix(package): fix check pkcs12 openssl1.1.0 not printed "MAC verified OK" #140
  • fix(package): parse serial and getDhparamInfo #139
  • Update semantic-release to the latest version 🚀 #134
  • chore(package): update semantic-release to version 8.0.0 efe156e

v1.11.0

13 September 2017

v1.9.8

3 September 2017

  • Feature Request: add checking support and md5 hashing for getModulus #127
  • Allow array values for CSRs #124
  • fix a problem with parsing "Public Key size" in different OS #122
  • chore(package): update semantic-release to version 7.0.2 #128
  • Version 1.9.8 2eae57c
  • bump version 44b1f17

v1.9.6

27 April 2017

  • Documentation #121
  • add 3 fields to certificate parsing #120
  • Update README #119
  • Revert "Regex support for new format with spaces" #113
  • Regex support for new format with spaces #111
  • Greenkeeper/initial #110
  • Propagate original options from createCSR to createCertificate #106
  • Handle slproweb.com openssl Binaries outputting ' = ' rather than '=' to separate subject fields #98
  • Regex support for new format with spaces: " C = GB, ST = England, O = Organisation, OU = Provisioning, CN = CommonName, emailAddress = email@address.com" ca0b353
  • docs(readme): add Greenkeeper badge cfbf14e
  • chore(package): update dependencies 805fa3a

v1.9.4

1 December 2016

  • Fix error handling and deprecation warnings #93
  • Add support for DC Certificates fix #83 #83
  • release v1.9.3 bf6b4cd

v1.9.1

28 November 2016

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

v1.9.0

28 November 2016

  • Fix error handling and deprecation warnings #93
  • Added noop callback (Node 7.0 deprecation warning) #91
  • Fixing CSR subject generation #90
  • adding options for -CAcreateserial #87
  • combine getModulus and getModulusFromProtected #82
  • remove default empty string and replace forEach with map #81
  • refactor spawnWrapper #80
  • Only generate altNames config if it is a non-empty array #78
  • Only generate altNames config if it is a non-empty array (#78) #77
  • semver 3018e51

v1.8.3

2 May 2016

  • Support csr config #75
  • Allow commas in CSR fields #74
  • Update package.json versions #70
  • Refactor temp dir logic #63
  • Add CA certs to pkcs12 file and read pkcs12 files #62
  • Allow commas in CSR fields (#74) #73
  • prepare new release e7f9fba
  • refactor temp dir logic, uses a ponyfill 77e2ada
  • npm i which -S 912e32e

1.8.1

20 September 2015

  • No match fix at preg_match_all #61
  • Bump version to 1.8.1 a7e661d
  • Update .travis.yml 89a4518
  • Update travis.yml to run on container infrastructure. aaf979e

1.8.0

26 August 2015

  • Adds method to verify a certificate's signing chain #60
  • Added functionality to export key and certificate to PKCS12 keystore #59
  • Added functionality to get modulus from a password protected key #53
  • Include Issuer Data with Fetch Cert Data Function #50
  • fix / add #57, #56, #55, #54, #52 30f4a0a
  • added getModulusFromProtected method and tests 0a5cb13
  • added signed cert methods 2f8c1d1

v1.7.1

27 February 2015

  • Creating a CSR for an encrypted key #49
  • bump to 1.7.0 #2
  • Using files for passing key passwords ed3a7a2
  • Unlinked temporary password file for CSR generation 6e13a14

v1.7.0

25 February 2015

  • Private key encryption #48
  • Merge latest PEM version #1
  • Add optional params to createPrivateKey 942e937
  • Bumped version to v1.7.0 dbae78a

v1.6.0

24 February 2015

  • Add function to create dhparam keys #47
  • Updated tests 5034d3e
  • Add hash option to getFingerprint function d8556de
  • Bumped version to v1.6.0 11219a9

v1.5.0

19 January 2015

  • add node which to check if the user has openssl on their system b44c619
  • Added a testcase for testOpenSSLPath 2cd2135
  • Changed e-mail address in travis conf a308e55

v1.4.6

18 January 2015

  • Just added indentations #39
  • Fix to catch errors on spawn #38
  • Update pem.js #36
  • Applied unified code style e756da6
  • added the code necessary to catch the error event from the spawn and send it to the callback b2941ab
  • Bumped version to v1.4.6 fe10527

v1.4.5

5 January 2015

  • Bugfix pathOpenSSL typo; Add environmental option for openssl binary #33
  • Add missing serviceCertificate & serial option to the doc #32
  • bumped version to 1.4.5 524142d

v1.4.4

13 December 2014

  • Update README.md #28
  • Add hash option for createCertificate(), default to sha256 #29
  • Bumped version to v1.4.4 1acbb32

v1.4.3

9 December 2014

  • Update README.md to correct copy/paste error #26
  • Add OpenSSL path config() #25
  • bumped version to v1.4.3 0ed6ecd

v1.4.2

28 November 2014

  • getModulus() will now accept Buffers #24
  • Bumped version to v1.4.2 d21f40d

v1.4.1

10 May 2014

  • fix wildcard certificate creation #19
  • fix wildcard certificate 5a5a828
  • bumped version to v1.4.1 97c0ecc

v1.4.0

27 March 2014

  • Change to BEGIN(\sNEW)? CERTIFICATE REQUEST #15
  • bumped version to 1.4.0 aba2be9
  • fixed errors 1aee604
  • Change default settings for future requirements + EV Fix 0e017e0

v1.3.0

18 March 2014

  • Change SAN CSR process. #13
  • add SAN integration for readCertificateInfo #12
  • Easier to set temp dir, and all temp files are unlinked after use #11
  • altNames option to set subjectAltName #10
  • jshinted code 7f8f307
  • add altNames option dc33104
  • SAN (DNS, IP) 5060816

v0.2.2

17 June 2013

  • Add certificate validity information to readCertificateInfo callback #9
  • fix unit test 8aff869
  • add validity information in readCertificateInfo cfdae1c
  • validity as timestamps 338a94f

v0.2.1

9 June 2013

  • update README.md, and improve unit test for getModulus function #8
  • add getModulus function #7
  • use wrapper for openssl 44d5f58
  • add getModulus function, useful to check that a Private Key Matches a Certificate f1d13d6
  • merged README from konsumer branch ab2e5f5

v0.2.0

17 April 2013

  • Add getFingerprint, fix intermittent failure #1
  • Add a getFingerprint method, docs, and unit test. 0a720c7
  • update 52fdc25
  • travis file 6595e7d

v0.1.0

25 June 2012