Skip to content

Commit

Permalink
Add method to get chain as X509Certificate[] (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaarni authored Oct 26, 2024
1 parent bda5c0c commit e61252e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/main/java/fi/protonode/certy/Credential.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,17 @@ public X509Certificate getX509Certificate() throws CertificateException, NoSuchA
return (X509Certificate) certificate;
}

/**
* Returns certificate and its chain (if any).
*
* @return Array of certificates as {@code X509Certificate}.
*/
public X509Certificate[] getX509Certificates() throws CertificateException, NoSuchAlgorithmException {
ensureGenerated();

return Arrays.stream(getChain()).map(c -> (X509Certificate) c).toArray(X509Certificate[]::new);
}

/**
* Returns private key.
*
Expand Down
6 changes: 6 additions & 0 deletions lib/src/test/java/fi/protonode/certy/TestCredential.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ void testGetCertificatesWithChain() throws Exception {
chain = rootCa.getCertificates();
assertEquals(1, chain.length);
assertEquals(rootCa.getCertificate(), chain[0]);

X509Certificate[] x509chain = cred.getX509Certificates();
assertEquals(3, x509chain.length);
assertEquals(cred.getX509Certificate(), x509chain[0]);
assertEquals(subSubCa.getX509Certificate(), x509chain[1]);
assertEquals(subCa.getX509Certificate(), x509chain[2]);
}

@Test
Expand Down

0 comments on commit e61252e

Please sign in to comment.