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.
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.
At your project directory, do either one of the following.
- From npm/yarn:
$ npm install --save js-x509-utils // npm $ yarn add js-x509-utils // yarn
- From GitHub:
$ git clone https://github.com/junkurihara/js-x509-utils.git
Then you should import the package as follows.
import x509 from 'js-x509-utils'; // for npm
import x509 from 'js-x509-utils/dist/index.js'; // for github
This library always uses JWK-formatted keys (RFC7517) to do any operations. If you utilize keys of other format, like PEM, please use js-crypto-key-utils
to convert them to JWK.
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
});
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',
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)
days: 365,
issuer: name,
subject: name
}
).then( (crt) => {
// now you get a certificate
});
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
});
const crtsample = '-----BEGIN CERTIFICATE-----...';
const jwkey = x509.toJwk(crtsample, 'pem');
// now you get JWK public key from PEM-formatted certificate
const crtsample = '-----BEGIN CERTIFICATE-----...';
const parsed = x509.parse(crtsample, 'pem');
// now you get parsed object from PEM-formatted certificate
// {tbsCertificate, signatureValue, signatureAlgorithm}
Due to the lack of native implementations of some primitive algorithms, the following signing algorithms do not work in legacy IE11.
- ECDSA:
ecdsa-with-sha1
andecdsa-with-sha512
- RSASSA-PKCS-v1_5:
Sha1WithRsaEncryption
andSha512WithRsaEncryption
- RSA-PSS: 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.
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)
Licensed under the MIT license, see LICENSE
file.