summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-07-25 18:02:45 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-07-25 18:02:45 +0700
commit510ceb7d6b857d8d50059fc63fccf64eeb207bf4 (patch)
tree24afdb9eb9b37b590797d530585df76f644ced81
parentMark BER parser classes Obsolete (for removal from public API) (diff)
downloadBouncyCastle.NET-ed25519-510ceb7d6b857d8d50059fc63fccf64eeb207bf4.tar.xz
Make DL encoding public in Asn1 API
-rw-r--r--crypto/src/asn1/Asn1Encodable.cs5
-rw-r--r--crypto/src/asn1/Asn1OutputStream.cs11
-rw-r--r--crypto/src/asn1/BERBitString.cs2
-rw-r--r--crypto/src/asn1/BerSequence.cs2
-rw-r--r--crypto/src/asn1/BerSet.cs6
-rw-r--r--crypto/src/asn1/BerTaggedObject.cs2
-rw-r--r--crypto/src/asn1/DERExternal.cs25
-rw-r--r--crypto/src/asn1/DLExternal.cs20
-rw-r--r--crypto/src/asn1/DLOutputStream.cs19
-rw-r--r--crypto/src/asn1/DLSequence.cs18
-rw-r--r--crypto/src/asn1/DLSet.cs18
-rw-r--r--crypto/src/asn1/DLTaggedObject.cs14
-rw-r--r--crypto/src/tsp/TimeStampResponse.cs21
-rw-r--r--crypto/src/tsp/TimeStampToken.cs2
-rw-r--r--crypto/test/src/asn1/test/GeneralizedTimeTest.cs4
15 files changed, 98 insertions, 71 deletions
diff --git a/crypto/src/asn1/Asn1Encodable.cs b/crypto/src/asn1/Asn1Encodable.cs
index a6b8d38b9..cc808326e 100644
--- a/crypto/src/asn1/Asn1Encodable.cs
+++ b/crypto/src/asn1/Asn1Encodable.cs
@@ -5,8 +5,9 @@ namespace Org.BouncyCastle.Asn1
 	public abstract class Asn1Encodable
 		: IAsn1Convertible
     {
-		public const string Der = "DER";
-		public const string Ber = "BER";
+        public const string Ber = "BER";
+        public const string Der = "DER";
+        public const string DL = "DL";
 
         public virtual void EncodeTo(Stream output)
         {
diff --git a/crypto/src/asn1/Asn1OutputStream.cs b/crypto/src/asn1/Asn1OutputStream.cs
index 86006877b..7dd1f7883 100644
--- a/crypto/src/asn1/Asn1OutputStream.cs
+++ b/crypto/src/asn1/Asn1OutputStream.cs
@@ -14,11 +14,12 @@ namespace Org.BouncyCastle.Asn1
         : FilterStream
     {
         internal const int EncodingBer = 1;
-        internal const int EncodingDer = 2;
+        internal const int EncodingDL = 2;
+        internal const int EncodingDer = 3;
 
         public static Asn1OutputStream Create(Stream output)
         {
-            return Create(output, Asn1Encodable.Ber);
+            return new Asn1OutputStream(output, false);
         }
 
         public static Asn1OutputStream Create(Stream output, string encoding)
@@ -30,7 +31,8 @@ namespace Org.BouncyCastle.Asn1
         {
             if (Asn1Encodable.Der.Equals(encoding))
                 return new DerOutputStream(output, leaveOpen);
-
+            if (Asn1Encodable.DL.Equals(encoding))
+                return new DLOutputStream(output, leaveOpen);
             return new Asn1OutputStream(output, leaveOpen);
         }
 
@@ -38,7 +40,8 @@ namespace Org.BouncyCastle.Asn1
         {
             if (Asn1Encodable.Der.Equals(encoding))
                 return EncodingDer;
-
+            if (Asn1Encodable.DL.Equals(encoding))
+                return EncodingDL;
             return EncodingBer;
         }
 
diff --git a/crypto/src/asn1/BERBitString.cs b/crypto/src/asn1/BERBitString.cs
index 98159e1eb..9f809dcbc 100644
--- a/crypto/src/asn1/BERBitString.cs
+++ b/crypto/src/asn1/BERBitString.cs
@@ -4,7 +4,7 @@ using System.Diagnostics;
 namespace Org.BouncyCastle.Asn1
 {
     public class BerBitString
-        : DerBitString
+        : DLBitString
     {
         public static BerBitString FromSequence(Asn1Sequence seq)
         {
diff --git a/crypto/src/asn1/BerSequence.cs b/crypto/src/asn1/BerSequence.cs
index f1590caf8..f7eb0bccd 100644
--- a/crypto/src/asn1/BerSequence.cs
+++ b/crypto/src/asn1/BerSequence.cs
@@ -3,7 +3,7 @@ using System;
 namespace Org.BouncyCastle.Asn1
 {
 	public class BerSequence
-		: DerSequence
+		: DLSequence
 	{
 		public static new readonly BerSequence Empty = new BerSequence();
 
diff --git a/crypto/src/asn1/BerSet.cs b/crypto/src/asn1/BerSet.cs
index d75bfa46e..dcc0a2435 100644
--- a/crypto/src/asn1/BerSet.cs
+++ b/crypto/src/asn1/BerSet.cs
@@ -5,7 +5,7 @@ using Org.BouncyCastle.Utilities;
 namespace Org.BouncyCastle.Asn1
 {
     public class BerSet
-        : DerSet
+        : DLSet
     {
 		public static new readonly BerSet Empty = new BerSet();
 
@@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Asn1
         }
 
         public BerSet(params Asn1Encodable[] elements)
-            : base(elements, false)
+            : base(elements)
         {
         }
 
@@ -39,7 +39,7 @@ namespace Org.BouncyCastle.Asn1
          * create a set containing a vector of objects.
          */
         public BerSet(Asn1EncodableVector elementVector)
-            : base(elementVector, false)
+            : base(elementVector)
         {
         }
 
diff --git a/crypto/src/asn1/BerTaggedObject.cs b/crypto/src/asn1/BerTaggedObject.cs
index 9e9ce0a49..bdcd9aa0d 100644
--- a/crypto/src/asn1/BerTaggedObject.cs
+++ b/crypto/src/asn1/BerTaggedObject.cs
@@ -8,7 +8,7 @@ namespace Org.BouncyCastle.Asn1
 	 * rules (as with sequences).
 	 */
 	public class BerTaggedObject
-		: DerTaggedObject
+		: DLTaggedObject
 	{
         /**
 		 * @param tagNo the tag number for this object.
diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs
index 7bf35d615..8cb7d040b 100644
--- a/crypto/src/asn1/DERExternal.cs
+++ b/crypto/src/asn1/DERExternal.cs
@@ -153,26 +153,17 @@ namespace Org.BouncyCastle.Asn1
             return new DerSequence(v);
         }
 
-        internal override IAsn1Encoding GetEncoding(int encoding)
-        {
-            return BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDer, Asn1Tags.Universal,
-                Asn1Tags.External);
-        }
+        internal sealed override IAsn1Encoding GetEncoding(int encoding) =>
+            GetEncodingImplicit(encoding, Asn1Tags.Universal, Asn1Tags.External);
 
-        internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
-        {
-            return BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDer, tagClass, tagNo);
-        }
+        internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) =>
+            BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDer, tagClass, tagNo);
 
-        internal sealed override DerEncoding GetEncodingDer()
-        {
-            return BuildSequence().GetEncodingDerImplicit(Asn1Tags.Universal, Asn1Tags.External);
-        }
+        internal sealed override DerEncoding GetEncodingDer() =>
+            GetEncodingDerImplicit(Asn1Tags.Universal, Asn1Tags.External);
 
-        internal sealed override DerEncoding GetEncodingDerImplicit(int tagClass, int tagNo)
-        {
-            return BuildSequence().GetEncodingDerImplicit(tagClass, tagNo);
-        }
+        internal sealed override DerEncoding GetEncodingDerImplicit(int tagClass, int tagNo) =>
+            BuildSequence().GetEncodingDerImplicit(tagClass, tagNo);
 
         protected override int Asn1GetHashCode()
 		{
diff --git a/crypto/src/asn1/DLExternal.cs b/crypto/src/asn1/DLExternal.cs
index 912ead579..ac31495cc 100644
--- a/crypto/src/asn1/DLExternal.cs
+++ b/crypto/src/asn1/DLExternal.cs
@@ -1,25 +1,25 @@
 namespace Org.BouncyCastle.Asn1
 {
-    internal class DLExternal
+    public class DLExternal
         : DerExternal
     {
-        internal DLExternal(Asn1EncodableVector vector)
+        public DLExternal(Asn1EncodableVector vector)
             : base(vector)
         {
         }
 
-        internal DLExternal(Asn1Sequence sequence)
+        public DLExternal(Asn1Sequence sequence)
             : base(sequence)
         {
         }
 
-        internal DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
+        public DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
             Asn1ObjectDescriptor dataValueDescriptor, Asn1TaggedObject externalData)
             : base(directReference, indirectReference, dataValueDescriptor, externalData)
         {
         }
 
-        internal DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
+        public DLExternal(DerObjectIdentifier directReference, DerInteger indirectReference,
             Asn1ObjectDescriptor dataValueDescriptor, int encoding, Asn1Object externalData)
             : base(directReference, indirectReference, dataValueDescriptor, encoding, externalData)
         {
@@ -33,20 +33,12 @@
             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);
+            return BuildSequence().GetEncodingImplicit(Asn1OutputStream.EncodingDL, tagClass, tagNo);
         }
     }
 }
diff --git a/crypto/src/asn1/DLOutputStream.cs b/crypto/src/asn1/DLOutputStream.cs
new file mode 100644
index 000000000..54dc1a249
--- /dev/null
+++ b/crypto/src/asn1/DLOutputStream.cs
@@ -0,0 +1,19 @@
+using System;
+using System.IO;
+
+namespace Org.BouncyCastle.Asn1
+{
+    internal class DLOutputStream
+        : Asn1OutputStream
+    {
+        internal DLOutputStream(Stream os, bool leaveOpen)
+            : base(os, leaveOpen)
+        {
+        }
+
+        internal override int Encoding
+        {
+            get { return EncodingDL; }
+        }
+    }
+}
diff --git a/crypto/src/asn1/DLSequence.cs b/crypto/src/asn1/DLSequence.cs
index 5a525f0a0..d4298ba51 100644
--- a/crypto/src/asn1/DLSequence.cs
+++ b/crypto/src/asn1/DLSequence.cs
@@ -2,12 +2,12 @@
 
 namespace Org.BouncyCastle.Asn1
 {
-    internal class DLSequence
+    public class DLSequence
         : DerSequence
     {
-        internal static new readonly DLSequence Empty = new DLSequence();
+        public static new readonly DLSequence Empty = new DLSequence();
 
-        internal static new DLSequence FromVector(Asn1EncodableVector elementVector)
+        public static new DLSequence FromVector(Asn1EncodableVector elementVector)
         {
             return elementVector.Count < 1 ? Empty : new DLSequence(elementVector);
         }
@@ -15,7 +15,7 @@ namespace Org.BouncyCastle.Asn1
         /**
 		 * create an empty sequence
 		 */
-        internal DLSequence()
+        public DLSequence()
             : base()
         {
         }
@@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Asn1
         /**
 		 * create a sequence containing one object
 		 */
-        internal DLSequence(Asn1Encodable element)
+        public DLSequence(Asn1Encodable element)
             : base(element)
         {
         }
@@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Asn1
         {
         }
 
-        internal DLSequence(params Asn1Encodable[] elements)
+        public DLSequence(params Asn1Encodable[] elements)
             : base(elements)
         {
         }
@@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Asn1
         /**
 		 * create a sequence containing a vector of objects.
 		 */
-        internal DLSequence(Asn1EncodableVector elementVector)
+        public DLSequence(Asn1EncodableVector elementVector)
             : base(elementVector)
         {
         }
@@ -60,7 +60,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncoding(encoding);
 
             return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Sequence,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDL, elements));
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
@@ -69,7 +69,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
             return new ConstructedDLEncoding(tagClass, tagNo,
-                Asn1OutputStream.GetContentsEncodings(encoding, elements));
+                Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDL, elements));
         }
 
         internal override DerBitString ToAsn1BitString()
diff --git a/crypto/src/asn1/DLSet.cs b/crypto/src/asn1/DLSet.cs
index 4f44718cb..ad9901533 100644
--- a/crypto/src/asn1/DLSet.cs
+++ b/crypto/src/asn1/DLSet.cs
@@ -2,12 +2,12 @@
 
 namespace Org.BouncyCastle.Asn1
 {
-    internal class DLSet
+    public class DLSet
         : DerSet
     {
-        internal static new readonly DLSet Empty = new DLSet();
+        public static new readonly DLSet Empty = new DLSet();
 
-        internal static new DLSet FromVector(Asn1EncodableVector elementVector)
+        public static new DLSet FromVector(Asn1EncodableVector elementVector)
         {
             return elementVector.Count < 1 ? Empty : new DLSet(elementVector);
         }
@@ -15,7 +15,7 @@ namespace Org.BouncyCastle.Asn1
         /**
          * create an empty set
          */
-        internal DLSet()
+        public DLSet()
             : base()
         {
         }
@@ -23,12 +23,12 @@ namespace Org.BouncyCastle.Asn1
         /**
          * create a set containing one object
          */
-        internal DLSet(Asn1Encodable element)
+        public DLSet(Asn1Encodable element)
             : base(element)
         {
         }
 
-        internal DLSet(params Asn1Encodable[] elements)
+        public DLSet(params Asn1Encodable[] elements)
             : base(elements, false)
         {
         }
@@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Asn1
         /**
          * create a set containing a vector of objects.
          */
-        internal DLSet(Asn1EncodableVector elementVector)
+        public DLSet(Asn1EncodableVector elementVector)
             : base(elementVector, false)
         {
         }
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncoding(encoding);
 
             return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Set,
-                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
+                Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDL, m_elements));
         }
 
         internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
@@ -61,7 +61,7 @@ namespace Org.BouncyCastle.Asn1
                 return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
             return new ConstructedDLEncoding(tagClass, tagNo,
-                Asn1OutputStream.GetContentsEncodings(encoding, m_elements));
+                Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDL, m_elements));
         }
     }
 }
diff --git a/crypto/src/asn1/DLTaggedObject.cs b/crypto/src/asn1/DLTaggedObject.cs
index d49dedc9a..87705ea80 100644
--- a/crypto/src/asn1/DLTaggedObject.cs
+++ b/crypto/src/asn1/DLTaggedObject.cs
@@ -2,25 +2,25 @@
 
 namespace Org.BouncyCastle.Asn1
 {
-    internal class DLTaggedObject
+    public class DLTaggedObject
         : DerTaggedObject
     {
-        internal DLTaggedObject(int tagNo, Asn1Encodable obj)
+        public DLTaggedObject(int tagNo, Asn1Encodable obj)
             : base(tagNo, obj)
         {
         }
 
-        internal DLTaggedObject(int tagClass, int tagNo, Asn1Encodable obj)
+        public DLTaggedObject(int tagClass, int tagNo, Asn1Encodable obj)
             : base(tagClass, tagNo, obj)
         {
         }
 
-        internal DLTaggedObject(bool isExplicit, int tagNo, Asn1Encodable obj)
+        public DLTaggedObject(bool isExplicit, int tagNo, Asn1Encodable obj)
             : base(isExplicit, tagNo, obj)
         {
         }
 
-        internal DLTaggedObject(bool isExplicit, int tagClass, int tagNo, Asn1Encodable obj)
+        public DLTaggedObject(bool isExplicit, int tagClass, int tagNo, Asn1Encodable obj)
             : base(isExplicit, tagClass, tagNo, obj)
         {
         }
@@ -35,6 +35,8 @@ namespace Org.BouncyCastle.Asn1
             if (Asn1OutputStream.EncodingDer == encoding)
                 return base.GetEncoding(encoding);
 
+            encoding = Asn1OutputStream.EncodingDL;
+
             Asn1Object baseObject = GetBaseObject().ToAsn1Object();
 
             if (!IsExplicit())
@@ -48,6 +50,8 @@ namespace Org.BouncyCastle.Asn1
             if (Asn1OutputStream.EncodingDer == encoding)
                 return base.GetEncodingImplicit(encoding, tagClass, tagNo);
 
+            encoding = Asn1OutputStream.EncodingDL;
+
             Asn1Object baseObject = GetBaseObject().ToAsn1Object();
 
             if (!IsExplicit())
diff --git a/crypto/src/tsp/TimeStampResponse.cs b/crypto/src/tsp/TimeStampResponse.cs
index 702194911..60ad79f5c 100644
--- a/crypto/src/tsp/TimeStampResponse.cs
+++ b/crypto/src/tsp/TimeStampResponse.cs
@@ -10,10 +10,10 @@ using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Tsp
 {
-	/**
+    /**
 	 * Base class for an RFC 3161 Time Stamp Response object.
 	 */
-	public class TimeStampResponse
+    public class TimeStampResponse
 	{
 		private TimeStampResp	resp;
 		private TimeStampToken	timeStampToken;
@@ -180,5 +180,22 @@ namespace Org.BouncyCastle.Tsp
 		{
 			return resp.GetEncoded();
 		}
+
+        /**
+         * return the ASN.1 encoded representation of this object for the specific encoding type.
+         *
+         * @param encoding encoding style ("DER", "DL", "BER")
+         */
+        public byte[] GetEncoded(string encoding)
+        {
+            if (Asn1Encodable.DL.Equals(encoding))
+            {
+                if (timeStampToken == null)
+                    return new DLSequence(resp.Status).GetEncoded(encoding);
+
+                return new DLSequence(resp.Status, timeStampToken.ToCmsSignedData().ContentInfo).GetEncoded(encoding);
+            }
+            return resp.GetEncoded(encoding);
+		}
 	}
 }
diff --git a/crypto/src/tsp/TimeStampToken.cs b/crypto/src/tsp/TimeStampToken.cs
index f2f86da67..8e9f09723 100644
--- a/crypto/src/tsp/TimeStampToken.cs
+++ b/crypto/src/tsp/TimeStampToken.cs
@@ -234,7 +234,7 @@ namespace Org.BouncyCastle.Tsp
 		 */
 		public byte[] GetEncoded()
 		{
-            return tsToken.GetEncoded(Asn1Encodable.Der);
+            return tsToken.GetEncoded(Asn1Encodable.DL);
         }
 
         /**
diff --git a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs
index 266ca2753..c1d6b27ca 100644
--- a/crypto/test/src/asn1/test/GeneralizedTimeTest.cs
+++ b/crypto/test/src/asn1/test/GeneralizedTimeTest.cs
@@ -145,14 +145,14 @@ namespace Org.BouncyCastle.Asn1.Tests
                 // check BER encoding is still "as given"
                 Asn1GeneralizedTime ber = new Asn1GeneralizedTime("202208091215Z");
 
-                //IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), ber.GetEncoded(Asn1Encodable.DL)));
+                IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), ber.GetEncoded(Asn1Encodable.DL)));
                 IsTrue(Arrays.AreEqual(Hex.Decode("180d3230323230383039313231355a"), ber.GetEncoded(Asn1Encodable.Ber)));
                 IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), ber.GetEncoded(Asn1Encodable.Der)));
 
                 // check always uses DER encoding
                 DerGeneralizedTime der = new DerGeneralizedTime("202208091215Z");
 
-                //IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.DL)));
+                IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.DL)));
                 IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.Ber)));
                 IsTrue(Arrays.AreEqual(Hex.Decode("180f32303232303830393132313530305a"), der.GetEncoded(Asn1Encodable.Der)));
             }