Use constants for PEM labels

pull/55/head
Richard Körber 2017-05-01 17:22:29 +02:00
parent fdc05ba70b
commit 231198992a
5 changed files with 29 additions and 8 deletions

View File

@ -110,7 +110,7 @@ public class Certificate extends AcmeResource {
public void writeCertificate(Writer out) throws IOException { public void writeCertificate(Writer out) throws IOException {
try { try {
for (X509Certificate cert : getCertificateChain()) { for (X509Certificate cert : getCertificateChain()) {
AcmeUtils.writeToPem(cert.getEncoded(), "CERTIFICATE", out); AcmeUtils.writeToPem(cert.getEncoded(), AcmeUtils.PemLabel.CERTIFICATE, out);
} }
} catch (CertificateEncodingException ex) { } catch (CertificateEncodingException ex) {
throw new IOException("Encoding error", ex); throw new IOException("Encoding error", ex);

View File

@ -55,6 +55,27 @@ public final class AcmeUtils {
private static final Base64.Encoder PEM_ENCODER = Base64.getMimeEncoder(64, "\n".getBytes()); private static final Base64.Encoder PEM_ENCODER = Base64.getMimeEncoder(64, "\n".getBytes());
/**
* Enumeration of PEM labels.
*/
public enum PemLabel {
CERTIFICATE("CERTIFICATE"),
CERTIFICATE_REQUEST("CERTIFICATE REQUEST"),
PRIVATE_KEY("PRIVATE KEY"),
PUBLIC_KEY("PUBLIC KEY");
private final String label;
PemLabel(String label) {
this.label = label;
}
@Override
public String toString() {
return label;
}
}
private AcmeUtils() { private AcmeUtils() {
// Utility class without constructor // Utility class without constructor
@ -242,14 +263,14 @@ public final class AcmeUtils {
* @param encoded * @param encoded
* Encoded data to write * Encoded data to write
* @param label * @param label
* PEM label, e.g. "CERTIFICATE" * {@link PemLabel} to be used
* @param out * @param out
* {@link Writer} to write to. It will not be closed after use! * {@link Writer} to write to. It will not be closed after use!
*/ */
public static void writeToPem(byte[] encoded, String label, Writer out) throws IOException { public static void writeToPem(byte[] encoded, PemLabel label, Writer out) throws IOException {
out.append("-----BEGIN ").append(label).append("-----\n"); out.append("-----BEGIN ").append(label.toString()).append("-----\n");
out.append(new String(PEM_ENCODER.encode(encoded))); out.append(new String(PEM_ENCODER.encode(encoded)));
out.append("\n-----END ").append(label).append("-----\n"); out.append("\n-----END ").append(label.toString()).append("-----\n");
} }
} }

View File

@ -802,7 +802,7 @@ public class DefaultConnectionTest {
for (X509Certificate cert : TestUtils.createCertificate()) { for (X509Certificate cert : TestUtils.createCertificate()) {
byte[] badCert = cert.getEncoded(); byte[] badCert = cert.getEncoded();
Arrays.sort(badCert); // break it Arrays.sort(badCert); // break it
AcmeUtils.writeToPem(badCert, "CERTIFICATE", w); AcmeUtils.writeToPem(badCert, AcmeUtils.PemLabel.CERTIFICATE, w);
} }
w.flush(); w.flush();
brokenPem = baos.toByteArray(); brokenPem = baos.toByteArray();

View File

@ -270,7 +270,7 @@ public class AcmeUtilsTest {
ByteArrayOutputStream pemFile = new ByteArrayOutputStream(); ByteArrayOutputStream pemFile = new ByteArrayOutputStream();
try (Writer w = new OutputStreamWriter(pemFile)) { try (Writer w = new OutputStreamWriter(pemFile)) {
for (X509Certificate cert : certChain) { for (X509Certificate cert : certChain) {
AcmeUtils.writeToPem(cert.getEncoded(), "CERTIFICATE", w); AcmeUtils.writeToPem(cert.getEncoded(), AcmeUtils.PemLabel.CERTIFICATE, w);
} }
} }

View File

@ -379,7 +379,7 @@ public class ClientTest {
// Create a validation certificate // Create a validation certificate
try (FileWriter fw = new FileWriter("tlssni.crt")) { try (FileWriter fw = new FileWriter("tlssni.crt")) {
X509Certificate cert = CertificateUtils.createTlsSni02Certificate(domainKeyPair, subject, sanB); X509Certificate cert = CertificateUtils.createTlsSni02Certificate(domainKeyPair, subject, sanB);
AcmeUtils.writeToPem(cert.getEncoded(), "CERTIFICATE", fw); AcmeUtils.writeToPem(cert.getEncoded(), AcmeUtils.PemLabel.CERTIFICATE, fw);
} catch (IOException | CertificateEncodingException ex) { } catch (IOException | CertificateEncodingException ex) {
throw new AcmeException("Could not write certificate", ex); throw new AcmeException("Could not write certificate", ex);
} }