summary refs log tree commit diff
path: root/crypto/src/x509/X509CertificateParser.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/x509/X509CertificateParser.cs')
-rw-r--r--crypto/src/x509/X509CertificateParser.cs69
1 files changed, 24 insertions, 45 deletions
diff --git a/crypto/src/x509/X509CertificateParser.cs b/crypto/src/x509/X509CertificateParser.cs
index ceab31108..ce50dc8ed 100644
--- a/crypto/src/x509/X509CertificateParser.cs
+++ b/crypto/src/x509/X509CertificateParser.cs
@@ -1,12 +1,11 @@
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Pkcs;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Security.Certificates;
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.X509
@@ -22,12 +21,11 @@ namespace Org.BouncyCastle.X509
 	{
 		private static readonly PemParser PemCertParser = new PemParser("CERTIFICATE");
 
-		private Asn1Set	sData;
-		private int		sDataObjectCount;
-		private Stream	currentStream;
+		private Asn1Set sData;
+		private int sDataObjectCount;
+		private Stream currentStream;
 
-		private X509Certificate ReadDerCertificate(
-			Asn1InputStream dIn)
+		private X509Certificate ReadDerCertificate(Asn1InputStream dIn)
 		{
 			Asn1Sequence seq = (Asn1Sequence)dIn.ReadObject();
 
@@ -42,7 +40,14 @@ namespace Org.BouncyCastle.X509
 				}
 			}
 
-			return CreateX509Certificate(X509CertificateStructure.GetInstance(seq));
+			return new X509Certificate(X509CertificateStructure.GetInstance(seq));
+		}
+
+		private X509Certificate ReadPemCertificate(Stream inStream)
+		{
+			Asn1Sequence seq = PemCertParser.ReadPemObject(inStream);
+
+			return seq == null ? null : new X509Certificate(X509CertificateStructure.GetInstance(seq));
 		}
 
 		private X509Certificate GetCertificate()
@@ -54,38 +59,18 @@ namespace Org.BouncyCastle.X509
 					object obj = sData[sDataObjectCount++];
 
 					if (obj is Asn1Sequence)
-					{
-						return CreateX509Certificate(
-							X509CertificateStructure.GetInstance(obj));
-					}
+						return new X509Certificate(X509CertificateStructure.GetInstance(obj));
 				}
 			}
 
 			return null;
 		}
 
-		private X509Certificate ReadPemCertificate(
-			Stream inStream)
-		{
-			Asn1Sequence seq = PemCertParser.ReadPemObject(inStream);
-
-			return seq == null
-				?	null
-				:	CreateX509Certificate(X509CertificateStructure.GetInstance(seq));
-		}
-
-		protected virtual X509Certificate CreateX509Certificate(
-			X509CertificateStructure c)
-		{
-			return new X509Certificate(c);
-		}
-
 		/// <summary>
 		/// Create loading data from byte array.
 		/// </summary>
 		/// <param name="input"></param>
-		public X509Certificate ReadCertificate(
-			byte[] input)
+		public X509Certificate ReadCertificate(byte[] input)
 		{
 			return ReadCertificate(new MemoryStream(input, false));
 		}
@@ -94,8 +79,7 @@ namespace Org.BouncyCastle.X509
 		/// Create loading data from byte array.
 		/// </summary>
 		/// <param name="input"></param>
-		public ICollection ReadCertificates(
-			byte[] input)
+		public IList<X509Certificate> ReadCertificates(byte[] input)
 		{
 			return ReadCertificates(new MemoryStream(input, false));
 		}
@@ -104,8 +88,7 @@ namespace Org.BouncyCastle.X509
 		 * Generates a certificate object and initializes it with the data
 		 * read from the input stream inStream.
 		 */
-		public X509Certificate ReadCertificate(
-			Stream inStream)
+		public X509Certificate ReadCertificate(Stream inStream)
 		{
 			if (inStream == null)
 				throw new ArgumentNullException("inStream");
@@ -130,9 +113,7 @@ namespace Org.BouncyCastle.X509
 				if (sData != null)
 				{
 					if (sDataObjectCount != sData.Count)
-					{
 						return GetCertificate();
-					}
 
 					sData = null;
 					sDataObjectCount = 0;
@@ -155,9 +136,7 @@ namespace Org.BouncyCastle.X509
                 }
 
                 if (tag != 0x30)  // assume ascii PEM encoded.
-				{
 					return ReadPemCertificate(inStream);
-				}
 
 				return ReadDerCertificate(new Asn1InputStream(inStream));
 			}
@@ -171,18 +150,18 @@ namespace Org.BouncyCastle.X509
 		 * Returns a (possibly empty) collection view of the certificates
 		 * read from the given input stream inStream.
 		 */
-		public ICollection ReadCertificates(
-			Stream inStream)
+		public IList<X509Certificate> ReadCertificates(Stream inStream)
 		{
-			X509Certificate cert;
-            IList certs = Platform.CreateArrayList();
+			return new List<X509Certificate>(ParseCertificates(inStream));
+		}
 
+		public IEnumerable<X509Certificate> ParseCertificates(Stream inStream)
+		{
+			X509Certificate cert;
 			while ((cert = ReadCertificate(inStream)) != null)
 			{
-				certs.Add(cert);
+				yield return cert;
 			}
-
-			return certs;
 		}
 	}
 }