|
20 | 20 | import android.content.Context; |
21 | 21 | import android.content.Intent; |
22 | 22 | import android.util.Log; |
23 | | -import com.android.org.bouncycastle.openssl.PEMReader; |
24 | | -import com.android.org.bouncycastle.openssl.PEMWriter; |
| 23 | +import com.android.org.bouncycastle.util.io.pem.PemObject; |
| 24 | +import com.android.org.bouncycastle.util.io.pem.PemReader; |
| 25 | +import com.android.org.bouncycastle.util.io.pem.PemWriter; |
25 | 26 | import java.io.ByteArrayInputStream; |
26 | 27 | import java.io.ByteArrayOutputStream; |
27 | 28 | import java.io.IOException; |
|
32 | 33 | import java.io.Writer; |
33 | 34 | import java.nio.charset.Charsets; |
34 | 35 | import java.security.KeyPair; |
| 36 | +import java.security.cert.Certificate; |
| 37 | +import java.security.cert.CertificateEncodingException; |
| 38 | +import java.security.cert.CertificateException; |
| 39 | +import java.security.cert.CertificateFactory; |
35 | 40 | import java.security.cert.X509Certificate; |
36 | 41 | import java.util.ArrayList; |
37 | 42 | import java.util.List; |
@@ -108,34 +113,41 @@ public class Credentials { |
108 | 113 | public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data"; |
109 | 114 |
|
110 | 115 | /** |
111 | | - * Convert objects to a PEM format, which is used for |
112 | | - * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY |
113 | | - * entries. |
| 116 | + * Convert objects to a PEM format which is used for |
| 117 | + * CA_CERTIFICATE and USER_CERTIFICATE entries. |
114 | 118 | */ |
115 | | - public static byte[] convertToPem(Object... objects) throws IOException { |
| 119 | + public static byte[] convertToPem(Certificate... objects) |
| 120 | + throws IOException, CertificateEncodingException { |
116 | 121 | ByteArrayOutputStream bao = new ByteArrayOutputStream(); |
117 | 122 | Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII); |
118 | | - PEMWriter pw = new PEMWriter(writer); |
119 | | - for (Object o : objects) { |
120 | | - pw.writeObject(o); |
| 123 | + PemWriter pw = new PemWriter(writer); |
| 124 | + for (Certificate o : objects) { |
| 125 | + pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded())); |
121 | 126 | } |
122 | 127 | pw.close(); |
123 | 128 | return bao.toByteArray(); |
124 | 129 | } |
125 | 130 | /** |
126 | 131 | * Convert objects from PEM format, which is used for |
127 | | - * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY |
128 | | - * entries. |
| 132 | + * CA_CERTIFICATE and USER_CERTIFICATE entries. |
129 | 133 | */ |
130 | | - public static List<Object> convertFromPem(byte[] bytes) throws IOException { |
| 134 | + public static List<X509Certificate> convertFromPem(byte[] bytes) |
| 135 | + throws IOException, CertificateException { |
131 | 136 | ByteArrayInputStream bai = new ByteArrayInputStream(bytes); |
132 | 137 | Reader reader = new InputStreamReader(bai, Charsets.US_ASCII); |
133 | | - PEMReader pr = new PEMReader(reader); |
134 | | - |
135 | | - List<Object> result = new ArrayList<Object>(); |
136 | | - Object o; |
137 | | - while ((o = pr.readObject()) != null) { |
138 | | - result.add(o); |
| 138 | + PemReader pr = new PemReader(reader); |
| 139 | + |
| 140 | + CertificateFactory cf = CertificateFactory.getInstance("X509"); |
| 141 | + |
| 142 | + List<X509Certificate> result = new ArrayList<X509Certificate>(); |
| 143 | + PemObject o; |
| 144 | + while ((o = pr.readPemObject()) != null) { |
| 145 | + if (o.getType().equals("CERTIFICATE")) { |
| 146 | + Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent())); |
| 147 | + result.add((X509Certificate) c); |
| 148 | + } else { |
| 149 | + throw new IllegalArgumentException("Unknown type " + o.getType()); |
| 150 | + } |
139 | 151 | } |
140 | 152 | pr.close(); |
141 | 153 | return result; |
|
0 commit comments