# js-x509-utils

> Universal Module of X.509 Certificate Utilities in JavaScript

Latest version **1.0.7** (published 2023-09-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-x509-utils
pnpm add js-x509-utils
yarn add js-x509-utils
bun add js-x509-utils
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2023-09-27 |
| First published | 2018-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 492.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jun Kurihara |
| Maintainers | kurihara |
| Keywords | crypto, x509, certificate, rfc5280 |

## Links

- npm: https://www.npmjs.com/package/js-x509-utils
- Repository: https://github.com/junkurihara/js-x509-utils
- Homepage: https://github.com/junkurihara/jscu/tree/master/packages/js-x509-utils#readme
- Issues: https://github.com/junkurihara/jscu/issues
- npm.io page: https://npm.io/package/js-x509-utils

## Dependencies (9)

- [bn.js](https://npm.io/package/bn.js.md) ~5.2.0
- [buffer](https://npm.io/package/buffer.md) ~6.0.0
- [asn1.js](https://npm.io/package/asn1.js.md) ~5.4.1
- [js-crypto-ec](https://npm.io/package/js-crypto-ec.md) ^1.0.7
- [js-crypto-rsa](https://npm.io/package/js-crypto-rsa.md) ^1.0.7
- [asn1.js-rfc5280](https://npm.io/package/asn1.js-rfc5280.md) ~3.0.0
- [js-crypto-random](https://npm.io/package/js-crypto-random.md) ^1.0.5
- [js-encoding-utils](https://npm.io/package/js-encoding-utils.md) 0.7.3
- [js-crypto-key-utils](https://npm.io/package/js-crypto-key-utils.md) ^1.0.7

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.0.7 (latest) — 2023-09-27
- 1.0.6 — 2023-09-02
- 1.0.5 — 2023-08-25
- 1.0.4 — 2022-02-04
- 1.0.3 — 2021-11-22
- 1.0.2 — 2021-03-13
- 1.0.1 — 2021-03-13
- 1.0.0 — 2020-09-30
- 0.5.3 — 2020-01-14
- 0.5.2 — 2019-12-08
- 0.5.1 — 2019-11-19
- 0.5.0 — 2019-11-19
- 0.4.11 — 2019-09-05
- 0.4.10 — 2019-09-05
- 0.4.9 — 2019-08-14
- … 34 more at https://npm.io/package/js-x509-utils/versions

## README

Universal Module of X.509 Certificate Utilities in JavaScript
--
[![npm version](https://badge.fury.io/js/js-x509-utils.svg)](https://badge.fury.io/js/js-x509-utils)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> **WARNING**: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

# Introduction and Overview
This library is designed to be 'universal' as a utility to handle X.509 in JavaScript, i.e., it works both on most browsers and on Node.js just by importing from npm/source code. This library provides APIs to convert between Json Web Key (JWK) and X.509 DER/PEM.

# Installation
At your project directory, do either one of the following.

- From npm/yarn:
  ```shell
  $ npm install --save js-x509-utils // npm
  $ yarn add js-x509-utils // yarn
  ```
- From GitHub:
  ```shell
  $ git clone https://github.com/junkurihara/jscu.git
  $ cd js-crypto-utils/packages/js-x509-utils
  & yarn build
  ```

Then you should import the package as follows.

```shell
import x509 from 'js-x509-utils'; // for npm
import x509 from 'path/to/js-x509-utils/dist/index.js'; // for github
```

The bundled file is also given as `js-x509-utils/dist/x509.bundle.js` for a use case where the module is imported as a `window.x509` object via `script` tags.

# Usage
This library always uses JWK-formatted keys ([RFC7517](https://tools.ietf.org/html/rfc7517)) to do any operations. If you utilize keys of other format, like PEM, please use [`js-crypto-key-utils`](https://github.com/junkurihara/js-crypto-key-utils) to convert them to JWK.


## Create X.509 from JWK-formatted public key
### ECDSA
```javascript
const publicJwk = {kty: 'EC', crv: 'P-256', x: '...', y: '...'}; // public key to be signed
const privateJwk = {ktyp: 'EC', crv: 'P-256', x: '...', y: '...', d: '...'}; // private key

const name = { // this is optional
      countryName: 'JP',
      stateOrProvinceName: 'Tokyo',
      localityName: 'Chiyoda',
      organizationName: 'example',
      organizationalUnitName: 'Research',
      commonName: 'example.com'
    };

// sign
x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'ecdsa-with-sha256', // signature algorithm
    days: 365, // expired in days
    issuer: name, // issuer
    subject: name // assume that issuer = subject, i.e., self-signed certificate
  },
  'pem' // output signature is in PEM. DER-encoded signature is available with 'der'.
).then( (cert) => {
  // now you get the certificate in PEM string
});
```

### RSA-PSS
```javascript
const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key to be signed
const privateJwk = {ktyp: 'RSA', n: '...', e: '...', d: '...', p: '...', q: '...', ...}; // private key

const name = {
  countryName: 'JP',
  stateOrProvinceName: 'Tokyo',
  localityName: 'Chiyoda',
  organizationName: 'example',
  organizationalUnitName: 'Research',
  commonName: 'example.com'
};

x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'rsassaPss',
    days: 365,
    issuer: name,
    subject: name,
    pssParams: {
      saltLength: 32, // if unspecified, 20 will be applied as default value
      hash: 'SHA-256' // if unspecified, 'SHA-1' will be applied as default value (but I do not not recommend SHA-1)
    }
  }
).then( (crt) => {
  // now you get a certificate
});
```

### RSASSA-PKCS1-v1_5
```javascript
const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key to be signed
const privateJwk = {ktyp: 'RSA', n: '...', e: '...', d: '...', p: '...', q: '...', ...}; // private key

const name = {
  countryName: 'JP',
  stateOrProvinceName: 'Tokyo',
  localityName: 'Chiyoda',
  organizationName: 'example',
  organizationalUnitName: 'Research',
  commonName: 'example.com'
};

x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'sha256WithRSAEncryption',
    days: 365,
    issuer: name,
    subject: name
  }
).then( (crt) => {
  // now you get a certificate
});
```


## Extract JWK from X.509 certificate
```javascript
const crtsample = '-----BEGIN CERTIFICATE-----...';
const jwkey = x509.toJwk(crtsample, 'pem');
// now you get JWK public key from PEM-formatted certificate
```

## Extract signature and message body from X.509 certificate
```javascript
const crtsample = '-----BEGIN CERTIFICATE-----...';
const parsed = x509.parse(crtsample, 'pem');
// now you get parsed object from PEM-formatted certificate
// {tbsCertificate, signatureValue, signatureAlgorithm}
```

# Note
## Limitations
Legacy IE is NOT supported at all. Also the followings are not supported in Edge.
- ECDSA: `ecdsa-with-sha1`
- RSASSA-PKCS-v1_5: `Sha1WithRsaEncryption`
- RSA-PSS: All

**Note that referring to RFC, `Sha1WithRsaEncryption` is the default algorithm when empty params for RSA-SSA-PKCS-v1_5, and hence MS environment does not support such existing certificates. Hence we strongly recommend to use other modern browsers.**


## ECDSA
At this point, this library supports only certificate signed with ECDSA using the following curve for elliptic curve cryptography.
- P-256 (secp256r1)
- P-384 (secp384r1)
- P-521 (secp521r1)
- P-256K (secp256k1)

# License
Licensed under the MIT license, see `LICENSE` file.

---
_Source: https://npm.io/package/js-x509-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
