1 package org.cacert.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 org.cacert.gigi.dbObjects.CACertificate;
18 import org.cacert.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) throws IOException, GeneralSecurityException {
34 X509Certificate cert = c.cert();
35 out.println(PEM.encode("CERTIFICATE", cert.getEncoded()));
37 CACertificate ca = c.getParent();
38 while ( !ca.isSelfsigned()) {
39 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
43 out.println(PEM.encode("CERTIFICATE", ca.getCertificate().getEncoded()));
48 public static void writeCertCer(Certificate c, ServletOutputStream out, boolean doChain, boolean includeAnchor) throws IOException, GeneralSecurityException {
49 X509Certificate cert = c.cert();
51 PKCS7 p7 = toP7Chain(c);
52 p7.encodeSignedData(out);
54 out.write(cert.getEncoded());
58 private static PKCS7 toP7Chain(Certificate c) throws IOException, GeneralSecurityException {
59 LinkedList<X509Certificate> ll = getChain(c);
60 PKCS7 p7 = new PKCS7(new AlgorithmId[0], new ContentInfo(ContentInfo.DATA_OID, null), ll.toArray(new X509Certificate[ll.size()]), new SignerInfo[0]) {
63 public void encodeSignedData(DerOutputStream out) throws IOException {
64 DerOutputStream signedData = new DerOutputStream();
65 BigInteger version = getVersion();
66 AlgorithmId[] digestAlgorithmIds = getDigestAlgorithmIds();
67 ContentInfo contentInfo = getContentInfo();
68 X509Certificate[] certificates = getCertificates();
69 X509CRL[] crls = getCRLs();
70 SignerInfo[] signerInfos = getSignerInfos();
73 signedData.putInteger(version);
76 signedData.putOrderedSetOf(DerValue.tag_Set, digestAlgorithmIds);
79 contentInfo.encode(signedData);
81 // certificates (optional)
82 if (certificates != null && certificates.length != 0) {
83 DerOutputStream sub = new DerOutputStream();
84 // cast to X509CertImpl[] since X509CertImpl implements
86 X509CertImpl implCerts[] = new X509CertImpl[certificates.length];
87 for (int i = 0; i < certificates.length; i++) {
89 sub.write(certificates[i].getEncoded());
90 } catch (CertificateEncodingException e) {
92 throw new IOException(e);
94 if (certificates[i] instanceof X509CertImpl) {
95 implCerts[i] = (X509CertImpl) certificates[i];
98 byte[] encoded = certificates[i].getEncoded();
99 implCerts[i] = new X509CertImpl(encoded);
100 } catch (CertificateException ce) {
102 throw new IOException(ce);
107 // Add the certificate set (tagged with [0] IMPLICIT)
108 // to the signed data
109 signedData.write((byte) 0xA0, sub);
114 if (crls != null && crls.length != 0) {
115 // cast to X509CRLImpl[] since X509CRLImpl implements
117 Set<X509CRLImpl> implCRLs = new HashSet<X509CRLImpl>(crls.length);
118 for (X509CRL crl : crls) {
119 if (crl instanceof X509CRLImpl) {
120 implCRLs.add((X509CRLImpl) crl);
123 byte[] encoded = crl.getEncoded();
124 implCRLs.add(new X509CRLImpl(encoded));
125 } catch (CRLException ce) {
126 throw new IOException(ce);
131 // Add the CRL set (tagged with [1] IMPLICIT)
132 // to the signed data
133 signedData.putOrderedSetOf((byte) 0xA1, implCRLs.toArray(new X509CRLImpl[implCRLs.size()]));
137 signedData.putOrderedSetOf(DerValue.tag_Set, signerInfos);
139 // making it a signed data block
140 DerValue signedDataSeq = new DerValue(DerValue.tag_Sequence, signedData.toByteArray());
142 // making it a content info sequence
143 ContentInfo block = new ContentInfo(ContentInfo.SIGNED_DATA_OID, signedDataSeq);
145 // writing out the contentInfo sequence
153 private static LinkedList<X509Certificate> getChain(Certificate c) throws IOException, GeneralSecurityException {
154 LinkedList<X509Certificate> ll = new LinkedList<>();
156 CACertificate ca = c.getParent();
157 while ( !ca.isSelfsigned()) {
158 ll.add(ca.getCertificate());
161 ll.add(ca.getCertificate());