Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const csr = pki.createCertificationRequest();
csr.publicKey = keys.publicKey;
csr.setSubject([
{
name: "commonName",
value: commonName,
valueTagClass: asn1.Type.UTF8,
// We specify UTF8 to encode a UTF8String (rather than the default of PRINTABLESTRING) in the
// commonName so that GlobalSign does not report a warning, and also because that happens to
// be what openssl(1) does when asked to create a CSR.
},
]);
csr.sign(keys.privateKey);
console.log("generateKeyAndCsr created new key & certificate request for", commonName);
return {
privateKeyAsPem: pki.privateKeyToPem(keys.privateKey),
csrAsPem: pki.certificationRequestToPem(csr),
};
};
it('should properly return a privateKey and a cert', () => {
const {privateKey, cert} = res.ca;
const privateKeyPem = pki.privateKeyToPem(privateKey);
expect(typeof privateKeyPem).toBe('string');
expect(privateKeyPem).toMatch(/^-----BEGIN RSA PRIVATE KEY-----\r\n.+/);
const certPem = pki.certificateToPem(cert);
expect(typeof certPem).toBe('string');
expect(certPem).toMatch(/^-----BEGIN CERTIFICATE-----\r\n.+/);
expect(cert.serialNumber).toMatch(/[0-9a-f]{16}/);
expect(getCertificateSubject(cert)).toEqual({commonName, ...attributes});
});
it('should have correct extensions', () => {
pki.rsa.generateKeyPair({ bits: 1024 }, (err, keyPair) => {
if (err !== null && err !== undefined) {
reject(err)
} else {
resolve(pki.privateKeyToPem(keyPair.privateKey))
}
})
})
}).tap(({privateKey, csr}) => Promise.all([
fs.writeFileAsync(path.join(this.dir, 'reqs', `${commonName}.req`), pki.certificationRequestToPem(csr)),
fs.writeFileAsync(path.join(this.dir, 'private', `${commonName}.key`), pki.privateKeyToPem(privateKey))
]));
}
private generateKeyPair(key: string) {
const keypair = pki.rsa.generateKeyPair({ bits: 1024 });
const privateKey = pki.privateKeyToPem(keypair.privateKey);
const publicKey = pki.publicKeyToPem(keypair.publicKey);
this.keyPairs[key] = { publicKey, privateKey };
return publicKey;
}
}
export function readPrivateKey(keyString: string | Buffer, passphrase: string | undefined, isOutputString?: boolean) {
return isString(passphrase) ? this.convertToString(pki.privateKeyToPem(pki.decryptRsaPrivateKey(String(keyString), passphrase)), isOutputString) : keyString;
}
/**
export const generateRsaKeyPems = () => {
const keyPair = generateRsaKeyPair()
return {
publicKey: pki.publicKeyToPem(keyPair.publicKey),
privateKey: pki.privateKeyToPem(keyPair.privateKey),
}
}