summaryrefslogtreecommitdiff
path: root/src/org/spongycastle/util/io
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2013-05-20 09:55:50 +0200
committerArne Schwabe <arne@rfc2549.org>2013-05-20 09:55:50 +0200
commit2a9037c749d3395ac65105604ab4936c4af0fc56 (patch)
treeee7672009ac3575f8aeac6c37ac91b7590334f28 /src/org/spongycastle/util/io
parentffcd37e623136697da66e0c23bddc92b01ff0e41 (diff)
Add certificate CN print
Need to fix string for email and order --HG-- extra : rebase_source : c3d5858d53d2f8f340b0d4a07434021f194a247a
Diffstat (limited to 'src/org/spongycastle/util/io')
-rw-r--r--src/org/spongycastle/util/io/pem/PemReader.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/org/spongycastle/util/io/pem/PemReader.java b/src/org/spongycastle/util/io/pem/PemReader.java
new file mode 100644
index 00000000..cbbebab9
--- /dev/null
+++ b/src/org/spongycastle/util/io/pem/PemReader.java
@@ -0,0 +1,84 @@
+package org.spongycastle.util.io.pem;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.spongycastle.util.encoders.Base64;
+
+public class PemReader
+ extends BufferedReader
+{
+ private static final String BEGIN = "-----BEGIN ";
+ private static final String END = "-----END ";
+
+ public PemReader(Reader reader)
+ {
+ super(reader);
+ }
+
+ public PemObject readPemObject()
+ throws IOException
+ {
+ String line = readLine();
+
+ while (line != null && !line.startsWith(BEGIN))
+ {
+ line = readLine();
+ }
+
+ if (line != null)
+ {
+ line = line.substring(BEGIN.length());
+ int index = line.indexOf('-');
+ String type = line.substring(0, index);
+
+ if (index > 0)
+ {
+ return loadObject(type);
+ }
+ }
+
+ return null;
+ }
+
+ private PemObject loadObject(String type)
+ throws IOException
+ {
+ String line;
+ String endMarker = END + type;
+ StringBuffer buf = new StringBuffer();
+ List headers = new ArrayList();
+
+ while ((line = readLine()) != null)
+ {
+ if (line.indexOf(":") >= 0)
+ {
+ int index = line.indexOf(':');
+ String hdr = line.substring(0, index);
+ String value = line.substring(index + 1).trim();
+
+ headers.add(new PemHeader(hdr, value));
+
+ continue;
+ }
+
+ if (line.indexOf(endMarker) != -1)
+ {
+ break;
+ }
+
+ buf.append(line.trim());
+ }
+
+ if (line == null)
+ {
+ throw new IOException(endMarker + " not found");
+ }
+
+ return new PemObject(type, headers, Base64.decode(buf.toString()));
+ }
+
+}