-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
208 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/im/toss/cert/sdk/PKCS7CertificateExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package im.toss.cert.sdk; | ||
|
||
import org.bouncycastle.cert.jcajce.JcaCertStoreBuilder; | ||
import org.bouncycastle.cms.CMSException; | ||
import org.bouncycastle.cms.CMSSignedData; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.cert.CertStore; | ||
import java.security.cert.CertStoreException; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.util.Collection; | ||
|
||
public class PKCS7CertificateExtractor { | ||
public static String extractCertificate(String pkcs7Data) { | ||
try { | ||
CMSSignedData signedData = new CMSSignedData(Base64Utils.decode(pkcs7Data)); | ||
CertStore certStore = new JcaCertStoreBuilder().addCertificates(signedData.getCertificates()).build(); | ||
Collection<? extends Certificate> certificates = certStore.getCertificates(null); | ||
return convertToPem(certificates.iterator().next()); | ||
} catch (CMSException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} catch (CertificateEncodingException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} catch (CertStoreException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException(e.getCause()); | ||
} | ||
} | ||
|
||
private static String convertToPem(Certificate certificate) throws CertificateEncodingException { | ||
String pemCertPre = "-----BEGIN CERTIFICATE-----\n"; | ||
String pemCertPost = "\n-----END CERTIFICATE-----"; | ||
String pemCert = Base64Utils.encodeToString(certificate.getEncoded()); | ||
return pemCertPre + pemCert + pemCertPost; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.