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.dbObjects.CACertificate;
18 import club.wpia.gigi.dbObjects.Certificate;
19 import sun.security.pkcs.ContentInfo;
20 import sun.security.pkcs.PKCS7;
21 import sun.security.pkcs.SignerInfo;
22 import sun.security.util.DerOutputStream;
23 import sun.security.util.DerValue;
24 import sun.security.x509.AlgorithmId;
25 import sun.security.x509.X509CRLImpl;
26 import sun.security.x509.X509CertImpl;
28 public class CertExporter {
30 private CertExporter() {}
32 public static void writeCertCrt(Certificate c, ServletOutputStream out, boolean doChain, boolean includeAnchor, boolean includeLeaf) throws IOException, GeneralSecurityException {
33 X509Certificate cert = c.cert();
35 out.println(PEM.encode("CERTIFICATE", cert.getEncoded()));
38 CACertificate ca = c.getParent();
39 while ( !ca.isSelfsigned()) {
40 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
44 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
49 public static void writeCertCer(Certificate c, ServletOutputStream out, boolean doChain, boolean includeAnchor) throws IOException, GeneralSecurityException {
50 X509Certificate cert = c.cert();
52 PKCS7 p7 = toP7Chain(c);
53 p7.encodeSignedData(out);
55 out.write(cert.getEncoded());
59 private static PKCS7 toP7Chain(Certificate c) throws IOException, GeneralSecurityException {
60 LinkedList<X509Certificate> ll = getChain(c);
61 PKCS7 p7 = new PKCS7(new AlgorithmId[0], new ContentInfo(ContentInfo.DATA_OID, null), ll.toArray(new X509Certificate[ll.size()]), new SignerInfo[0]) {
64 public void encodeSignedData(DerOutputStream out) throws IOException {
65 DerOutputStream signedData = new DerOutputStream();
66 BigInteger version = getVersion();
67 AlgorithmId[] digestAlgorithmIds = getDigestAlgorithmIds();
68 ContentInfo contentInfo = getContentInfo();
69 X509Certificate[] certificates = getCertificates();
70 X509CRL[] crls = getCRLs();
71 SignerInfo[] signerInfos = getSignerInfos();
74 signedData.putInteger(version);
77 signedData.putOrderedSetOf(DerValue.tag_Set, digestAlgorithmIds);
80 contentInfo.encode(signedData);
82 // certificates (optional)
83 if (certificates != null && certificates.length != 0) {
84 DerOutputStream sub = new DerOutputStream();
85 // cast to X509CertImpl[] since X509CertImpl implements
87 X509CertImpl implCerts[] = new X509CertImpl[certificates.length];
88 for (int i = 0; i < certificates.length; i++) {
90 sub.write(certificates[i].getEncoded());
91 } catch (CertificateEncodingException e) {
93 throw new IOException(e);
95 if (certificates[i] instanceof X509CertImpl) {
96 implCerts[i] = (X509CertImpl) certificates[i];
99 byte[] encoded = certificates[i].getEncoded();
100 implCerts[i] = new X509CertImpl(encoded);
101 } catch (CertificateException ce) {
103 throw new IOException(ce);
108 // Add the certificate set (tagged with [0] IMPLICIT)
109 // to the signed data
110 signedData.write((byte) 0xA0, sub);
115 if (crls != null && crls.length != 0) {
116 // cast to X509CRLImpl[] since X509CRLImpl implements
118 Set<X509CRLImpl> implCRLs = new HashSet<X509CRLImpl>(crls.length);
119 for (X509CRL crl : crls) {
120 if (crl instanceof X509CRLImpl) {
121 implCRLs.add((X509CRLImpl) crl);
124 byte[] encoded = crl.getEncoded();
125 implCRLs.add(new X509CRLImpl(encoded));
126 } catch (CRLException ce) {
127 throw new IOException(ce);
132 // Add the CRL set (tagged with [1] IMPLICIT)
133 // to the signed data
134 signedData.putOrderedSetOf((byte) 0xA1, implCRLs.toArray(new X509CRLImpl[implCRLs.size()]));
138 signedData.putOrderedSetOf(DerValue.tag_Set, signerInfos);
140 // making it a signed data block
141 DerValue signedDataSeq = new DerValue(DerValue.tag_Sequence, signedData.toByteArray());
143 // making it a content info sequence
144 ContentInfo block = new ContentInfo(ContentInfo.SIGNED_DATA_OID, signedDataSeq);
146 // writing out the contentInfo sequence
154 private static LinkedList<X509Certificate> getChain(Certificate c) throws IOException, GeneralSecurityException {
155 LinkedList<X509Certificate> ll = new LinkedList<>();
157 CACertificate ca = c.getParent();
158 while ( !ca.isSelfsigned()) {
159 ll.add(ca.getCertificate());
162 ll.add(ca.getCertificate());