1 package club.wpia.gigi.util;
3 import java.io.IOException;
4 import java.math.BigInteger;
5 import java.security.GeneralSecurityException;
6 import java.security.cert.CRLException;
7 import java.security.cert.CertificateEncodingException;
8 import java.security.cert.CertificateException;
9 import java.security.cert.X509CRL;
10 import java.security.cert.X509Certificate;
11 import java.util.HashSet;
12 import java.util.LinkedList;
15 import javax.servlet.ServletOutputStream;
17 import club.wpia.gigi.GigiApiException;
18 import club.wpia.gigi.dbObjects.CACertificate;
19 import club.wpia.gigi.dbObjects.Certificate;
20 import sun.security.pkcs.ContentInfo;
21 import sun.security.pkcs.PKCS7;
22 import sun.security.pkcs.SignerInfo;
23 import sun.security.util.DerOutputStream;
24 import sun.security.util.DerValue;
25 import sun.security.x509.AlgorithmId;
26 import sun.security.x509.X509CRLImpl;
27 import sun.security.x509.X509CertImpl;
29 public class CertExporter {
31 private CertExporter() {}
33 public static void writeCertCrt(Certificate c, ServletOutputStream out, boolean doChain, boolean includeAnchor, boolean includeLeaf) throws IOException, GeneralSecurityException, GigiApiException {
34 X509Certificate cert = c.cert();
36 out.println(PEM.encode("CERTIFICATE", cert.getEncoded()));
39 CACertificate ca = c.getParent();
40 while ( !ca.isSelfsigned()) {
41 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
45 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
50 public static void writeCertCer(Certificate c, ServletOutputStream out, boolean doChain, boolean includeAnchor) throws IOException, GeneralSecurityException, GigiApiException {
51 X509Certificate cert = c.cert();
53 PKCS7 p7 = toP7Chain(c);
54 p7.encodeSignedData(out);
56 out.write(cert.getEncoded());
60 private static PKCS7 toP7Chain(Certificate c) throws IOException, GeneralSecurityException, GigiApiException {
61 LinkedList<X509Certificate> ll = getChain(c);
62 PKCS7 p7 = new PKCS7(new AlgorithmId[0], new ContentInfo(ContentInfo.DATA_OID, null), ll.toArray(new X509Certificate[ll.size()]), new SignerInfo[0]) {
65 public void encodeSignedData(DerOutputStream out) throws IOException {
66 DerOutputStream signedData = new DerOutputStream();
67 BigInteger version = getVersion();
68 AlgorithmId[] digestAlgorithmIds = getDigestAlgorithmIds();
69 ContentInfo contentInfo = getContentInfo();
70 X509Certificate[] certificates = getCertificates();
71 X509CRL[] crls = getCRLs();
72 SignerInfo[] signerInfos = getSignerInfos();
75 signedData.putInteger(version);
78 signedData.putOrderedSetOf(DerValue.tag_Set, digestAlgorithmIds);
81 contentInfo.encode(signedData);
83 // certificates (optional)
84 if (certificates != null && certificates.length != 0) {
85 DerOutputStream sub = new DerOutputStream();
86 // cast to X509CertImpl[] since X509CertImpl implements
88 X509CertImpl implCerts[] = new X509CertImpl[certificates.length];
89 for (int i = 0; i < certificates.length; i++) {
91 sub.write(certificates[i].getEncoded());
92 } catch (CertificateEncodingException e) {
94 throw new IOException(e);
96 if (certificates[i] instanceof X509CertImpl) {
97 implCerts[i] = (X509CertImpl) certificates[i];
100 byte[] encoded = certificates[i].getEncoded();
101 implCerts[i] = new X509CertImpl(encoded);
102 } catch (CertificateException ce) {
104 throw new IOException(ce);
109 // Add the certificate set (tagged with [0] IMPLICIT)
110 // to the signed data
111 signedData.write((byte) 0xA0, sub);
116 if (crls != null && crls.length != 0) {
117 // cast to X509CRLImpl[] since X509CRLImpl implements
119 Set<X509CRLImpl> implCRLs = new HashSet<X509CRLImpl>(crls.length);
120 for (X509CRL crl : crls) {
121 if (crl instanceof X509CRLImpl) {
122 implCRLs.add((X509CRLImpl) crl);
125 byte[] encoded = crl.getEncoded();
126 implCRLs.add(new X509CRLImpl(encoded));
127 } catch (CRLException ce) {
128 throw new IOException(ce);
133 // Add the CRL set (tagged with [1] IMPLICIT)
134 // to the signed data
135 signedData.putOrderedSetOf((byte) 0xA1, implCRLs.toArray(new X509CRLImpl[implCRLs.size()]));
139 signedData.putOrderedSetOf(DerValue.tag_Set, signerInfos);
141 // making it a signed data block
142 DerValue signedDataSeq = new DerValue(DerValue.tag_Sequence, signedData.toByteArray());
144 // making it a content info sequence
145 ContentInfo block = new ContentInfo(ContentInfo.SIGNED_DATA_OID, signedDataSeq);
147 // writing out the contentInfo sequence
155 private static LinkedList<X509Certificate> getChain(Certificate c) throws IOException, GeneralSecurityException, GigiApiException {
156 LinkedList<X509Certificate> ll = new LinkedList<>();
158 CACertificate ca = c.getParent();
159 while ( !ca.isSelfsigned()) {
160 ll.add(ca.getCertificate());
163 ll.add(ca.getCertificate());