@peculiar/fortify-client-core
Version:
Fortify client core API for quickly connect to Fortify App
63 lines (62 loc) • 2.26 kB
JavaScript
/**
* @license
* Copyright (c) Peculiar Ventures, LLC.
*
* This source code is licensed under the BSD 3-Clause license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as x509 from '@peculiar/x509';
export var EName;
(function (EName) {
EName["CommonName"] = "CN";
EName["Country"] = "C";
EName["Region"] = "L";
EName["State"] = "ST";
EName["Organization"] = "O";
EName["OrganizationUnit"] = "OU";
})(EName || (EName = {}));
export class Create {
static async pkcs10(crypto, algorithm, subjectName) {
const keys = await crypto.subtle.generateKey(algorithm, false, Create.keyUsages);
const pkcs10 = await x509.Pkcs10CertificateRequestGenerator.create({
name: subjectName,
keys,
signingAlgorithm: algorithm,
extensions: [],
attributes: [],
}, crypto);
return {
der: pkcs10.rawData,
pem: pkcs10.toString('pem'),
privateKey: keys.privateKey,
publicKey: keys.publicKey,
};
}
static async x509(crypto, algorithm, subjectName) {
const keys = await crypto.subtle.generateKey(algorithm, false, Create.keyUsages);
const now = new Date();
const cert = await x509.X509CertificateGenerator.createSelfSigned({
serialNumber: '01',
name: subjectName,
notBefore: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
notAfter: new Date(now.getFullYear() + 1, now.getMonth(), now.getDate()),
signingAlgorithm: algorithm,
keys,
extensions: [
new x509.BasicConstraintsExtension(false, 0, true),
new x509.KeyUsagesExtension(x509.KeyUsageFlags.digitalSignature, true),
new x509.ExtendedKeyUsageExtension([
'1.3.6.1.5.5.7.3.2',
'1.3.6.1.5.5.7.3.4', // id-kp-emailProtection
], true),
],
}, crypto);
return {
der: cert.rawData,
pem: cert.toString('pem'),
privateKey: keys.privateKey,
publicKey: keys.publicKey,
};
}
}
Create.keyUsages = ['sign', 'verify'];