summary refs log tree commit diff
path: root/crypto/src/x509
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-21 13:53:34 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-21 13:53:34 +0700
commitfd62c077b82454d94ee2419992c886ed821777e3 (patch)
tree2ecd30ac0d0894fd3981be7232aa6237ba65afb7 /crypto/src/x509
parentASN.1: Tagged object parser improvements (diff)
downloadBouncyCastle.NET-ed25519-fd62c077b82454d94ee2419992c886ed821777e3.tar.xz
Use Seek when available
Diffstat (limited to 'crypto/src/x509')
-rw-r--r--crypto/src/x509/X509AttrCertParser.cs29
-rw-r--r--crypto/src/x509/X509CertPairParser.cs21
-rw-r--r--crypto/src/x509/X509CertificateParser.cs29
-rw-r--r--crypto/src/x509/X509CrlParser.cs31
4 files changed, 69 insertions, 41 deletions
diff --git a/crypto/src/x509/X509AttrCertParser.cs b/crypto/src/x509/X509AttrCertParser.cs
index a5c07362e..ce708ed8d 100644
--- a/crypto/src/x509/X509AttrCertParser.cs
+++ b/crypto/src/x509/X509AttrCertParser.cs
@@ -131,20 +131,27 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)  // assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)  // assume ascii PEM encoded.
 				{
-					return ReadPemCertificate(pis);
+					return ReadPemCertificate(inStream);
 				}
 
-				return ReadDerCertificate(new Asn1InputStream(pis));
+				return ReadDerCertificate(new Asn1InputStream(inStream));
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CertPairParser.cs b/crypto/src/x509/X509CertPairParser.cs
index 82612599b..89a8a8e96 100644
--- a/crypto/src/x509/X509CertPairParser.cs
+++ b/crypto/src/x509/X509CertPairParser.cs
@@ -62,15 +62,22 @@ namespace Org.BouncyCastle.X509
 
 			try
 			{
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
 
-				if (tag < 0)
-					return null;
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
 
-				pis.Unread(tag);
-
-				return ReadDerCrossCertificatePair(pis);
+                return ReadDerCrossCertificatePair(inStream);
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CertificateParser.cs b/crypto/src/x509/X509CertificateParser.cs
index 8f0e7406c..a8bba7bc2 100644
--- a/crypto/src/x509/X509CertificateParser.cs
+++ b/crypto/src/x509/X509CertificateParser.cs
@@ -141,20 +141,27 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)  // assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)  // assume ascii PEM encoded.
 				{
-					return ReadPemCertificate(pis);
+					return ReadPemCertificate(inStream);
 				}
 
-				return ReadDerCertificate(new Asn1InputStream(pis));
+				return ReadDerCertificate(new Asn1InputStream(inStream));
 			}
 			catch (Exception e)
 			{
diff --git a/crypto/src/x509/X509CrlParser.cs b/crypto/src/x509/X509CrlParser.cs
index d830bb9a6..59f7a23c4 100644
--- a/crypto/src/x509/X509CrlParser.cs
+++ b/crypto/src/x509/X509CrlParser.cs
@@ -140,22 +140,29 @@ namespace Org.BouncyCastle.X509
 					return null;
 				}
 
-				PushbackStream pis = new PushbackStream(inStream);
-				int tag = pis.ReadByte();
-
-				if (tag < 0)
-					return null;
-
-				pis.Unread(tag);
-
-				if (tag != 0x30)	// assume ascii PEM encoded.
+                int tag = inStream.ReadByte();
+                if (tag < 0)
+                    return null;
+
+                if (inStream.CanSeek)
+                {
+                    inStream.Seek(-1L, SeekOrigin.Current);
+                }
+                else
+                {
+                    PushbackStream pis = new PushbackStream(inStream);
+                    pis.Unread(tag);
+                    inStream = pis;
+                }
+
+                if (tag != 0x30)	// assume ascii PEM encoded.
 				{
-					return ReadPemCrl(pis);
+					return ReadPemCrl(inStream);
 				}
 
 				Asn1InputStream asn1 = lazyAsn1
-					?	new LazyAsn1InputStream(pis)
-					:	new Asn1InputStream(pis);
+					?	new LazyAsn1InputStream(inStream)
+					:	new Asn1InputStream(inStream);
 
 				return ReadDerCrl(asn1);
 			}