I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit bf51d5d9 authored by zzz's avatar zzz
Browse files

CertUtil: Consolidate PEM encoding (DRY)

parent 5d1d8b6d
No related branches found
No related tags found
No related merge requests found
...@@ -103,16 +103,7 @@ public final class CertUtil { ...@@ -103,16 +103,7 @@ public final class CertUtil {
throws IOException, CertificateEncodingException { throws IOException, CertificateEncodingException {
// Get the encoded form which is suitable for exporting // Get the encoded form which is suitable for exporting
byte[] buf = cert.getEncoded(); byte[] buf = cert.getEncoded();
PrintWriter wr = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); writePEM(buf, "CERTIFICATE", out);
wr.println("-----BEGIN CERTIFICATE-----");
String b64 = Base64.encode(buf, true); // true = use standard alphabet
for (int i = 0; i < b64.length(); i += LINE_LENGTH) {
wr.println(b64.substring(i, Math.min(i + LINE_LENGTH, b64.length())));
}
wr.println("-----END CERTIFICATE-----");
wr.flush();
if (wr.checkError())
throw new IOException("Failed write to " + out);
} }
/** /**
...@@ -131,13 +122,27 @@ public final class CertUtil { ...@@ -131,13 +122,27 @@ public final class CertUtil {
byte[] buf = pk.getEncoded(); byte[] buf = pk.getEncoded();
if (buf == null) if (buf == null)
throw new InvalidKeyException("encoding unsupported for this key"); throw new InvalidKeyException("encoding unsupported for this key");
writePEM(buf, "PRIVATE KEY", out);
}
/**
* Modified from:
* http://www.exampledepot.com/egs/java.security.cert/ExportCert.html
*
* Writes data in base64 format.
* Does NOT close the stream. Throws on all errors.
*
* @since 0.9.25 consolidated from other methods
*/
private static void writePEM(byte[] buf, String what, OutputStream out)
throws IOException {
PrintWriter wr = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); PrintWriter wr = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
wr.println("-----BEGIN PRIVATE KEY-----"); wr.println("-----BEGIN " + what + "-----");
String b64 = Base64.encode(buf, true); // true = use standard alphabet String b64 = Base64.encode(buf, true); // true = use standard alphabet
for (int i = 0; i < b64.length(); i += LINE_LENGTH) { for (int i = 0; i < b64.length(); i += LINE_LENGTH) {
wr.println(b64.substring(i, Math.min(i + LINE_LENGTH, b64.length()))); wr.println(b64.substring(i, Math.min(i + LINE_LENGTH, b64.length())));
} }
wr.println("-----END PRIVATE KEY-----"); wr.println("-----END " + what + "-----");
wr.flush(); wr.flush();
if (wr.checkError()) if (wr.checkError())
throw new IOException("Failed write to " + out); throw new IOException("Failed write to " + out);
...@@ -363,18 +368,7 @@ public final class CertUtil { ...@@ -363,18 +368,7 @@ public final class CertUtil {
private static void exportCRL(X509CRL crl, OutputStream out) private static void exportCRL(X509CRL crl, OutputStream out)
throws IOException, CRLException { throws IOException, CRLException {
byte[] buf = crl.getEncoded(); byte[] buf = crl.getEncoded();
if (buf == null) writePEM(buf, "X509 CRL", out);
throw new CRLException("encoding unsupported for this CRL");
PrintWriter wr = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
wr.println("-----BEGIN X509 CRL-----");
String b64 = Base64.encode(buf, true); // true = use standard alphabet
for (int i = 0; i < b64.length(); i += LINE_LENGTH) {
wr.println(b64.substring(i, Math.min(i + LINE_LENGTH, b64.length())));
}
wr.println("-----END X509 CRL-----");
wr.flush();
if (wr.checkError())
throw new IOException("Failed write to " + out);
} }
/**** /****
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment