summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-07 18:22:15 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-07 18:22:15 +0700
commitd3174f39510f4befc32f59be70055828ab392751 (patch)
tree6595ec8258b20eb25e2edf7728d0b99a56f493ed
parentSeal some overrides (diff)
downloadBouncyCastle.NET-ed25519-d3174f39510f4befc32f59be70055828ab392751.tar.xz
Add DLExternal and use for parsing
-rw-r--r--crypto/src/asn1/ASN1StreamParser.cs3
-rw-r--r--crypto/src/asn1/BerSequence.cs5
-rw-r--r--crypto/src/asn1/DERExternal.cs26
-rw-r--r--crypto/src/asn1/DERExternalParser.cs2
-rw-r--r--crypto/src/asn1/DLExternal.cs52
-rw-r--r--crypto/src/asn1/DLSequence.cs9
6 files changed, 79 insertions, 18 deletions
diff --git a/crypto/src/asn1/ASN1StreamParser.cs b/crypto/src/asn1/ASN1StreamParser.cs
index 85b890a8c..78e34d870 100644
--- a/crypto/src/asn1/ASN1StreamParser.cs
+++ b/crypto/src/asn1/ASN1StreamParser.cs
@@ -122,13 +122,16 @@ namespace Org.BouncyCastle.Asn1
                 // TODO[asn1] DLConstructedBitStringParser
                 return new BerBitStringParser(this);
             case Asn1Tags.External:
+                // TODO[asn1] DLExternalParser
                 return new DerExternalParser(this);
             case Asn1Tags.OctetString:
                 // TODO[asn1] DLConstructedOctetStringParser
                 return new BerOctetStringParser(this);
             case Asn1Tags.Set:
+                // TODO[asn1] DLSetParser
                 return new DerSetParser(this);
             case Asn1Tags.Sequence:
+                // TODO[asn1] DLSequenceParser
                 return new DerSequenceParser(this);
             default:
 				throw new Asn1Exception("unknown DL object encountered: 0x" + univTagNo.ToString("X"));
diff --git a/crypto/src/asn1/BerSequence.cs b/crypto/src/asn1/BerSequence.cs
index 074359402..f1590caf8 100644
--- a/crypto/src/asn1/BerSequence.cs
+++ b/crypto/src/asn1/BerSequence.cs
@@ -79,9 +79,8 @@ namespace Org.BouncyCastle.Asn1
 
         internal override DerExternal ToAsn1External()
         {
-            // TODO There is currently no BerExternal class (or ToDLObject/ToDerObject)
-            //return ((Asn1Sequence)ToDLObject()).ToAsn1External();
-            return new DLSequence(elements).ToAsn1External();
+            // TODO[asn1] There is currently no BerExternal (or Asn1External)
+            return new DLExternal(this);
         }
 
         internal override Asn1OctetString ToAsn1OctetString()
diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs
index 45d4d663d..7bf35d615 100644
--- a/crypto/src/asn1/DERExternal.cs
+++ b/crypto/src/asn1/DERExternal.cs
@@ -57,14 +57,14 @@ namespace Org.BouncyCastle.Asn1
             return (DerExternal)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
-		private readonly DerObjectIdentifier directReference;
-		private readonly DerInteger indirectReference;
-        private readonly Asn1ObjectDescriptor dataValueDescriptor;
-		private readonly int encoding;
-		private readonly Asn1Object externalContent;
+		internal readonly DerObjectIdentifier directReference;
+        internal readonly DerInteger indirectReference;
+        internal readonly Asn1ObjectDescriptor dataValueDescriptor;
+        internal readonly int encoding;
+        internal readonly Asn1Object externalContent;
 
         public DerExternal(Asn1EncodableVector vector)
-            : this(new DerSequence(vector))
+            : this(new BerSequence(vector))
         {
         }
 
@@ -109,8 +109,15 @@ namespace Org.BouncyCastle.Asn1
 		* @param dataValueDescriptor The data value descriptor or <code>null</code> if not set.
 		* @param externalData The external data in its encoded form.
 		*/
+        [Obsolete("Pass 'externalData' at type Asn1TaggedObject")]
         public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
             Asn1ObjectDescriptor dataValueDescriptor, DerTaggedObject externalData)
+            : this(directReference, indirectReference, dataValueDescriptor, (Asn1TaggedObject)externalData)
+        {
+        }
+
+        public DerExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
+            Asn1ObjectDescriptor dataValueDescriptor, Asn1TaggedObject externalData)
         {
             this.directReference = directReference;
             this.indirectReference = indirectReference;
@@ -138,7 +145,7 @@ namespace Org.BouncyCastle.Asn1
             this.externalContent = CheckExternalContent(encoding, externalData);
         }
 
-        internal Asn1Sequence BuildSequence()
+        internal virtual Asn1Sequence BuildSequence()
         {
             Asn1EncodableVector v = new Asn1EncodableVector(4);
             v.AddOptional(directReference, indirectReference, dataValueDescriptor);
@@ -148,12 +155,13 @@ namespace Org.BouncyCastle.Asn1
 
         internal override IAsn1Encoding GetEncoding(int encoding)
         {
-            return BuildSequence().GetEncodingImplicit(encoding, Asn1Tags.Universal, Asn1Tags.External);
+            return BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDer, Asn1Tags.Universal,
+                Asn1Tags.External);
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
         {
-            return BuildSequence().GetEncodingImplicit(encoding, tagClass, tagNo);
+            return BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDer, tagClass, tagNo);
         }
 
         internal sealed override DerEncoding GetEncodingDer()
diff --git a/crypto/src/asn1/DERExternalParser.cs b/crypto/src/asn1/DERExternalParser.cs
index cb5e0d8b6..ab699a887 100644
--- a/crypto/src/asn1/DERExternalParser.cs
+++ b/crypto/src/asn1/DERExternalParser.cs
@@ -24,7 +24,7 @@ namespace Org.BouncyCastle.Asn1
 
         internal static DerExternal Parse(Asn1StreamParser sp)
         {
-            return new DerExternal(sp.ReadVector());
+            return new DLExternal(sp.ReadVector());
         }
     }
 }
diff --git a/crypto/src/asn1/DLExternal.cs b/crypto/src/asn1/DLExternal.cs
new file mode 100644
index 000000000..912ead579
--- /dev/null
+++ b/crypto/src/asn1/DLExternal.cs
@@ -0,0 +1,52 @@
+namespace Org.BouncyCastle.Asn1
+{
+    internal class DLExternal
+        : DerExternal
+    {
+        internal DLExternal(Asn1EncodableVector vector)
+            : base(vector)
+        {
+        }
+
+        internal DLExternal(Asn1Sequence sequence)
+            : base(sequence)
+        {
+        }
+
+        internal DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
+            Asn1ObjectDescriptor dataValueDescriptor, Asn1TaggedObject externalData)
+            : base(directReference, indirectReference, dataValueDescriptor, externalData)
+        {
+        }
+
+        internal DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
+            Asn1ObjectDescriptor dataValueDescriptor, int encoding, Asn1Object externalData)
+            : base(directReference, indirectReference, dataValueDescriptor, encoding, externalData)
+        {
+        }
+
+        internal override Asn1Sequence BuildSequence()
+        {
+            Asn1EncodableVector v = new Asn1EncodableVector(4);
+            v.AddOptional(directReference, indirectReference, dataValueDescriptor);
+            v.Add(new DLTaggedObject(0 == encoding, encoding, externalContent));
+            return new DLSequence(v);
+        }
+
+        internal override IAsn1Encoding GetEncoding(int encoding)
+        {
+            if (Asn1OutputStream.EncodingDer == encoding)
+                return base.GetEncoding(encoding);
+
+            return BuildSequence().GetEncodingImplicit(encoding, Asn1Tags.Universal, Asn1Tags.External);
+        }
+
+        internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
+        {
+            if (Asn1OutputStream.EncodingDer == encoding)
+                return base.GetEncodingImplicit(encoding, tagClass, tagNo);
+
+            return BuildSequence().GetEncodingImplicit(encoding, tagClass, tagNo);
+        }
+    }
+}
diff --git a/crypto/src/asn1/DLSequence.cs b/crypto/src/asn1/DLSequence.cs
index 7f3a73f7b..5a525f0a0 100644
--- a/crypto/src/asn1/DLSequence.cs
+++ b/crypto/src/asn1/DLSequence.cs
@@ -77,11 +77,10 @@ namespace Org.BouncyCastle.Asn1
             return new DLBitString(BerBitString.FlattenBitStrings(GetConstructedBitStrings()), false);
         }
 
-        // TODO[asn1] DLExternal
-        //internal override DerExternal ToAsn1External()
-        //{
-        //    return new DLExternal(this);
-        //}
+        internal override DerExternal ToAsn1External()
+        {
+            return new DLExternal(this);
+        }
 
         internal override Asn1Set ToAsn1Set()
         {