|
15 | 15 | */
|
16 | 16 | package dev.sigstore.encryption.certificates;
|
17 | 17 |
|
18 |
| -import com.google.api.client.util.PemReader; |
19 | 18 | import com.google.common.collect.ImmutableList;
|
20 | 19 | import java.io.ByteArrayInputStream;
|
21 | 20 | import java.io.IOException;
|
|
27 | 26 | import java.util.Collections;
|
28 | 27 | import java.util.List;
|
29 | 28 | import java.util.Optional;
|
| 29 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 30 | +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 31 | +import org.bouncycastle.openssl.PEMParser; |
30 | 32 | import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
|
| 33 | +import org.bouncycastle.util.encoders.DecoderException; |
31 | 34 |
|
32 | 35 | public class Certificates {
|
33 | 36 |
|
@@ -96,36 +99,34 @@ public static byte[] toPemBytes(CertPath certs) throws IOException {
|
96 | 99 |
|
97 | 100 | /** Convert a PEM encoded certificate chain to a {@link CertPath}. */
|
98 | 101 | public static CertPath fromPemChain(String certs) throws CertificateException {
|
99 |
| - PemReader pemReader = null; |
100 |
| - try { |
101 |
| - pemReader = new PemReader(new StringReader(certs)); |
102 |
| - CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 102 | + try (PEMParser pemParser = new PEMParser(new StringReader(certs))) { |
103 | 103 | ArrayList<X509Certificate> certList = new ArrayList<>();
|
104 | 104 | while (true) {
|
105 | 105 | try {
|
106 |
| - PemReader.Section section = pemReader.readNextSection(); |
| 106 | + var section = pemParser.readObject(); // throws DecoderException |
107 | 107 | if (section == null) {
|
108 | 108 | break;
|
109 | 109 | }
|
110 |
| - byte[] certBytes = section.getBase64DecodedBytes(); |
111 |
| - certList.add( |
112 |
| - (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certBytes))); |
113 |
| - } catch (IOException | IllegalArgumentException ioe) { |
114 |
| - throw new CertificateParsingException("Error reading PEM section in cert chain", ioe); |
| 110 | + if (section instanceof X509CertificateHolder) { |
| 111 | + var certificate = |
| 112 | + new JcaX509CertificateConverter().getCertificate((X509CertificateHolder) section); |
| 113 | + certList.add(certificate); |
| 114 | + } else { |
| 115 | + throw new CertificateException( |
| 116 | + "Unsupported pem section: " |
| 117 | + + section.getClass().toString() |
| 118 | + + " is not an X509Certificate"); |
| 119 | + } |
| 120 | + } catch (IOException | DecoderException e) { |
| 121 | + throw new CertificateException("failed to parse PEM object to certificate", e); |
115 | 122 | }
|
116 | 123 | }
|
117 | 124 | if (certList.isEmpty()) {
|
118 |
| - throw new CertificateParsingException("no valid PEM certificates were found"); |
119 |
| - } |
120 |
| - return cf.generateCertPath(certList); |
121 |
| - } finally { |
122 |
| - if (pemReader != null) { |
123 |
| - try { |
124 |
| - pemReader.close(); |
125 |
| - } catch (IOException e) { |
126 |
| - // ignored |
127 |
| - } |
| 125 | + throw new CertificateException("no valid PEM certificates were found"); |
128 | 126 | }
|
| 127 | + return CertificateFactory.getInstance("X.509").generateCertPath(certList); |
| 128 | + } catch (IOException e) { |
| 129 | + throw new CertificateException("failed to close PEM parser", e); |
129 | 130 | }
|
130 | 131 | }
|
131 | 132 |
|
|
0 commit comments