summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 01:51:32 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-11-08 01:51:32 +0700
commit58a813a047df69785ce1180b2bcfb71d4d3c0180 (patch)
treee0b7d2e63fdc7dceb967744853ea610b6fdc9abc /crypto
parentRemove unnecessary static references (diff)
downloadBouncyCastle.NET-ed25519-58a813a047df69785ce1180b2bcfb71d4d3c0180.tar.xz
Utility methods and guards
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/Asn1Sequence.cs36
-rw-r--r--crypto/src/asn1/Asn1Set.cs11
-rw-r--r--crypto/src/asn1/BerSequence.cs10
-rw-r--r--crypto/src/asn1/DerSequence.cs11
-rw-r--r--crypto/src/asn1/LazyDERSequence.cs17
-rw-r--r--crypto/src/asn1/LazyDERSet.cs17
6 files changed, 95 insertions, 7 deletions
diff --git a/crypto/src/asn1/Asn1Sequence.cs b/crypto/src/asn1/Asn1Sequence.cs
index 8ea554932..a8f2af48a 100644
--- a/crypto/src/asn1/Asn1Sequence.cs
+++ b/crypto/src/asn1/Asn1Sequence.cs
@@ -157,6 +157,7 @@ namespace Org.BouncyCastle.Asn1
                 Asn1Sequence outer)
             {
                 this.outer = outer;
+                // NOTE: Call Count here to 'force' a LazyDerSequence
                 this.max = outer.Count;
             }
 
@@ -214,8 +215,8 @@ namespace Org.BouncyCastle.Asn1
 
         protected override int Asn1GetHashCode()
         {
-            //return Arrays.GetHashCode(elements);
-            int i = elements.Length;
+            // NOTE: Call Count here to 'force' a LazyDerSequence
+            int i = Count;
             int hc = i + 1;
 
             while (--i >= 0)
@@ -233,6 +234,7 @@ namespace Org.BouncyCastle.Asn1
             if (null == that)
                 return false;
 
+            // NOTE: Call Count here (on both) to 'force' a LazyDerSequence
             int count = this.Count;
             if (that.Count != count)
                 return false;
@@ -259,7 +261,37 @@ namespace Org.BouncyCastle.Asn1
             return CollectionUtilities.ToString(elements);
         }
 
+        // TODO[asn1] Preferably return an Asn1BitString[] (doesn't exist yet)
+        internal DerBitString[] GetConstructedBitStrings()
+        {
+            // NOTE: Call Count here to 'force' a LazyDerSequence
+            int count = Count;
+            DerBitString[] bitStrings = new DerBitString[count];
+            for (int i = 0; i < count; ++i)
+            {
+                bitStrings[i] = DerBitString.GetInstance(elements[i]);
+            }
+            return bitStrings;
+        }
+
+        internal Asn1OctetString[] GetConstructedOctetStrings()
+        {
+            // NOTE: Call Count here to 'force' a LazyDerSequence
+            int count = Count;
+            Asn1OctetString[] octetStrings = new Asn1OctetString[count];
+            for (int i = 0; i < count; ++i)
+            {
+                octetStrings[i] = Asn1OctetString.GetInstance(elements[i]);
+            }
+            return octetStrings;
+        }
+
+        // TODO[asn1] Preferably return an Asn1BitString (doesn't exist yet)
+        internal abstract DerBitString ToAsn1BitString();
+
         // TODO[asn1] Preferably return an Asn1External (doesn't exist yet)
         internal abstract DerExternal ToAsn1External();
+
+        internal abstract Asn1OctetString ToAsn1OctetString();
     }
 }
diff --git a/crypto/src/asn1/Asn1Set.cs b/crypto/src/asn1/Asn1Set.cs
index af0d7319f..bf0567926 100644
--- a/crypto/src/asn1/Asn1Set.cs
+++ b/crypto/src/asn1/Asn1Set.cs
@@ -192,6 +192,7 @@ namespace Org.BouncyCastle.Asn1
                 Asn1Set outer)
             {
                 this.outer = outer;
+                // NOTE: Call Count here to 'force' a LazyDerSet
                 this.max = outer.Count;
             }
 
@@ -227,8 +228,8 @@ namespace Org.BouncyCastle.Asn1
 
         protected override int Asn1GetHashCode()
         {
-            //return Arrays.GetHashCode(elements);
-            int i = elements.Length;
+            // NOTE: Call Count here to 'force' a LazyDerSet
+            int i = Count;
             int hc = i + 1;
 
             while (--i >= 0)
@@ -246,6 +247,7 @@ namespace Org.BouncyCastle.Asn1
             if (null == that)
                 return false;
 
+            // NOTE: Call Count here (on both) to 'force' a LazyDerSet
             int count = this.Count;
             if (that.Count != count)
                 return false;
@@ -264,7 +266,9 @@ namespace Org.BouncyCastle.Asn1
 
         protected internal void Sort()
         {
-            if (elements.Length < 2)
+            // NOTE: Call Count here to 'force' a LazyDerSet
+            int count = Count;
+            if (count < 2)
                 return;
 
 #if PORTABLE
@@ -275,7 +279,6 @@ namespace Org.BouncyCastle.Asn1
                 .Select(t => t.Item)
                 .ToArray();
 #else
-            int count = elements.Length;
             byte[][] keys = new byte[count][];
             for (int i = 0; i < count; ++i)
             {
diff --git a/crypto/src/asn1/BerSequence.cs b/crypto/src/asn1/BerSequence.cs
index 1795025f2..e0525efa6 100644
--- a/crypto/src/asn1/BerSequence.cs
+++ b/crypto/src/asn1/BerSequence.cs
@@ -65,11 +65,21 @@ namespace Org.BouncyCastle.Asn1
 			}
 		}
 
+        internal override DerBitString ToAsn1BitString()
+        {
+            return new BerBitString(GetConstructedBitStrings());
+        }
+
         internal override DerExternal ToAsn1External()
         {
             // TODO There is currently no BerExternal class (or ToDLObject/ToDerObject)
             //return ((Asn1Sequence)ToDLObject()).ToAsn1External();
             return new DerSequence(elements, false).ToAsn1External();
         }
+
+        internal override Asn1OctetString ToAsn1OctetString()
+        {
+            return new BerOctetString(GetConstructedOctetStrings());
+        }
     }
 }
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs
index d703cd0f5..5b315a909 100644
--- a/crypto/src/asn1/DerSequence.cs
+++ b/crypto/src/asn1/DerSequence.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -90,9 +89,19 @@ namespace Org.BouncyCastle.Asn1
             Platform.Dispose(dOut);
         }
 
+        internal override DerBitString ToAsn1BitString()
+        {
+            return new DerBitString(BerBitString.FlattenBitStrings(GetConstructedBitStrings()), false);
+        }
+
         internal override DerExternal ToAsn1External()
         {
             return new DerExternal(this);
         }
+
+        internal override Asn1OctetString ToAsn1OctetString()
+        {
+            return new DerOctetString(BerOctetString.FlattenOctetStrings(GetConstructedOctetStrings()));
+        }
     }
 }
diff --git a/crypto/src/asn1/LazyDERSequence.cs b/crypto/src/asn1/LazyDERSequence.cs
index 26ec1efa3..e1ee29c81 100644
--- a/crypto/src/asn1/LazyDERSequence.cs
+++ b/crypto/src/asn1/LazyDERSequence.cs
@@ -44,6 +44,9 @@ namespace Org.BouncyCastle.Asn1
 
         public override IEnumerator GetEnumerator()
         {
+            // TODO[asn1] Support lazy enumeration
+            // lock (this) if (null != encoded) return new LazyConstructionEnumeration(encoded);
+
             Parse();
 
             return base.GetEnumerator();
@@ -59,6 +62,20 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
+        public override Asn1Encodable[] ToArray()
+        {
+            Parse();
+
+            return base.ToArray();
+        }
+
+        public override string ToString()
+        {
+            Parse();
+
+            return base.ToString();
+        }
+
         internal override int EncodedLength(bool withID)
         {
             lock (this)
diff --git a/crypto/src/asn1/LazyDERSet.cs b/crypto/src/asn1/LazyDERSet.cs
index 32d9e2691..36e7bf53c 100644
--- a/crypto/src/asn1/LazyDERSet.cs
+++ b/crypto/src/asn1/LazyDERSet.cs
@@ -44,6 +44,9 @@ namespace Org.BouncyCastle.Asn1
 
         public override IEnumerator GetEnumerator()
         {
+            // TODO[asn1] Support lazy enumeration
+            // lock (this) if (null != encoded) return new LazyConstructionEnumeration(encoded);
+
             Parse();
 
             return base.GetEnumerator();
@@ -59,6 +62,20 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
+        public override Asn1Encodable[] ToArray()
+        {
+            Parse();
+
+            return base.ToArray();
+        }
+
+        public override string ToString()
+        {
+            Parse();
+
+            return base.ToString();
+        }
+
         internal override int EncodedLength(bool withID)
         {
             lock (this)