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

[pkcs12] return null if pkcs keystore has no cert under friendly name #2805

Merged
merged 1 commit into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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