From ef229edc7a3c0ae8967d0ae038f8b8b5d15d1557 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 7 Jun 2021 02:54:10 +0700 Subject: Update from bc-java --- crypto/src/util/io/pem/PemReader.cs | 55 ++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 32 deletions(-) (limited to 'crypto/src/util/io/pem/PemReader.cs') diff --git a/crypto/src/util/io/pem/PemReader.cs b/crypto/src/util/io/pem/PemReader.cs index 61e132fa7..b806c7f63 100644 --- a/crypto/src/util/io/pem/PemReader.cs +++ b/crypto/src/util/io/pem/PemReader.cs @@ -33,12 +33,17 @@ namespace Org.BouncyCastle.Utilities.IO.Pem /// public PemObject ReadPemObject() { - string line = reader.ReadLine(); + string line = reader.ReadLine(); - if (line != null && Platform.StartsWith(line, BeginString)) - { - line = line.Substring(BeginString.Length); - int index = line.IndexOf('-'); + while (line != null && !Platform.StartsWith(line, BeginString)) + { + line = reader.ReadLine(); + } + + if (line != null) + { + line = line.Substring(BeginString.Length); + int index = line.IndexOf('-'); if (index > 0 && Platform.EndsWith(line, "-----") && (line.Length - index) == 5) { @@ -48,7 +53,7 @@ namespace Org.BouncyCastle.Utilities.IO.Pem } } - return null; + return null; } private PemObject LoadObject(string type) @@ -58,40 +63,26 @@ namespace Org.BouncyCastle.Utilities.IO.Pem StringBuilder buf = new StringBuilder(); string line; - while ((line = reader.ReadLine()) != null - && Platform.IndexOf(line, endMarker) == -1) + while ((line = reader.ReadLine()) != null) { int colonPos = line.IndexOf(':'); - - if (colonPos == -1) - { - buf.Append(line.Trim()); - } - else + if (colonPos >= 0) { - // Process field - string fieldName = line.Substring(0, colonPos).Trim(); + string hdr = line.Substring(0, colonPos).Trim(); + string val = line.Substring(colonPos + 1).Trim(); - if (Platform.StartsWith(fieldName, "X-")) - { - fieldName = fieldName.Substring(2); - } + headers.Add(new PemHeader(hdr, val)); + continue; + } - string fieldValue = line.Substring(colonPos + 1).Trim(); + if (Platform.IndexOf(line, endMarker) >= 0) + break; - headers.Add(new PemHeader(fieldName, fieldValue)); - } - } + buf.Append(line.Trim()); + } - if (line == null) - { + if (line == null) throw new IOException(endMarker + " not found"); - } - - if (buf.Length % 4 != 0) - { - throw new IOException("base64 data appears to be truncated"); - } return new PemObject(type, headers, Base64.Decode(buf.ToString())); } -- cgit 1.4.1