Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
[pkcs12] return null if pkcs keystore has no cert under friendly name (
Browse files Browse the repository at this point in the history
  • Loading branch information
quinlanj authored Oct 18, 2020
1 parent 75fd82d commit 6482938
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/pkcs12/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ describe('reading PKCS#12 files', () => {
const certificate = getX509CertificateByFriendlyName(p12, alias);
expect(certificate).toMatchSnapshot();
});
it('fails to read X.509 certificates from conventional p12 files using #getX509CertificateByFriendlyName', async () => {
it('returns null if there are no X.509 certificates under friendly name for p12 keystores using #getX509CertificateByFriendlyName', async () => {
const { base64EncodedP12, password } = conventionalP12;
const p12 = parsePKCS12(base64EncodedP12, password);
expect(() => getX509CertificateByFriendlyName(p12, 'ruhroh')).toThrow();
const certificate = getX509CertificateByFriendlyName(p12, 'no keystore here :(');
expect(certificate).toBe(null);
});
});
4 changes: 2 additions & 2 deletions packages/pkcs12/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export function getX509Certificate(p12: forge.pkcs12.Pkcs12Pfx): forge.pki.Certi
export function getX509CertificateByFriendlyName(
p12: forge.pkcs12.Pkcs12Pfx,
friendlyName: string
): forge.pki.Certificate {
): forge.pki.Certificate | null {
const certBagType = forge.pki.oids.certBag;
const bags = p12.getBags({ friendlyName, bagType: certBagType }).friendlyName;
if (!bags || bags.length === 0) {
throw new Error(`PKCS12: No certificates found under friendlyName: ${friendlyName}`);
return null;
}
const certificate = bags[0].cert;
if (!certificate) {
Expand Down

0 comments on commit 6482938

Please sign in to comment.