summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/Asn1Encodable.cs4
-rw-r--r--crypto/src/asn1/Asn1Object.cs2
-rw-r--r--crypto/src/asn1/Asn1OutputStream.cs42
-rw-r--r--crypto/src/asn1/BERBitString.cs8
-rw-r--r--crypto/src/asn1/BEROctetStringGenerator.cs4
-rw-r--r--crypto/src/asn1/BerNull.cs8
-rw-r--r--crypto/src/asn1/BerOctetString.cs17
-rw-r--r--crypto/src/asn1/BerOutputStream.cs1
-rw-r--r--crypto/src/asn1/BerSequence.cs16
-rw-r--r--crypto/src/asn1/BerSet.cs16
-rw-r--r--crypto/src/asn1/BerTaggedObject.cs19
-rw-r--r--crypto/src/asn1/DERExternal.cs4
-rw-r--r--crypto/src/asn1/DERSequenceGenerator.cs4
-rw-r--r--crypto/src/asn1/DERSetGenerator.cs4
-rw-r--r--crypto/src/asn1/DerApplicationSpecific.cs5
-rw-r--r--crypto/src/asn1/DerBMPString.cs5
-rw-r--r--crypto/src/asn1/DerBitString.cs7
-rw-r--r--crypto/src/asn1/DerBoolean.cs5
-rw-r--r--crypto/src/asn1/DerEnumerated.cs4
-rw-r--r--crypto/src/asn1/DerGeneralString.cs5
-rw-r--r--crypto/src/asn1/DerGeneralizedTime.cs5
-rw-r--r--crypto/src/asn1/DerGraphicString.cs4
-rw-r--r--crypto/src/asn1/DerIA5String.cs5
-rw-r--r--crypto/src/asn1/DerInteger.cs4
-rw-r--r--crypto/src/asn1/DerNull.cs4
-rw-r--r--crypto/src/asn1/DerNumericString.cs5
-rw-r--r--crypto/src/asn1/DerObjectIdentifier.cs4
-rw-r--r--crypto/src/asn1/DerOctetString.cs13
-rw-r--r--crypto/src/asn1/DerOutputStream.cs20
-rw-r--r--crypto/src/asn1/DerPrintableString.cs5
-rw-r--r--crypto/src/asn1/DerSequence.cs8
-rw-r--r--crypto/src/asn1/DerSet.cs8
-rw-r--r--crypto/src/asn1/DerT61String.cs5
-rw-r--r--crypto/src/asn1/DerTaggedObject.cs11
-rw-r--r--crypto/src/asn1/DerUTCTime.cs5
-rw-r--r--crypto/src/asn1/DerUTF8String.cs5
-rw-r--r--crypto/src/asn1/DerUniversalString.cs5
-rw-r--r--crypto/src/asn1/DerVideotexString.cs4
-rw-r--r--crypto/src/asn1/DerVisibleString.cs5
-rw-r--r--crypto/src/asn1/LazyDERSequence.cs7
-rw-r--r--crypto/src/asn1/LazyDERSet.cs7
-rw-r--r--crypto/src/cms/CMSSignedDataGenerator.cs4
-rw-r--r--crypto/src/pkcs/Pkcs12Store.cs11
43 files changed, 181 insertions, 153 deletions
diff --git a/crypto/src/asn1/Asn1Encodable.cs b/crypto/src/asn1/Asn1Encodable.cs
index f70b3a4cb..f3c0f725f 100644
--- a/crypto/src/asn1/Asn1Encodable.cs
+++ b/crypto/src/asn1/Asn1Encodable.cs
@@ -11,7 +11,7 @@ namespace Org.BouncyCastle.Asn1
 		public byte[] GetEncoded()
         {
             MemoryStream bOut = new MemoryStream();
-            Asn1OutputStream aOut = new Asn1OutputStream(bOut);
+            Asn1OutputStream aOut = Asn1OutputStream.Create(bOut);
 
             ToAsn1Object().Encode(aOut);
 
@@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Asn1
             if (encoding.Equals(Der))
             {
                 MemoryStream bOut = new MemoryStream();
-                DerOutputStream dOut = new DerOutputStream(bOut);
+                Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Der);
 
                 Asn1Object asn1Object = ToAsn1Object();
 
diff --git a/crypto/src/asn1/Asn1Object.cs b/crypto/src/asn1/Asn1Object.cs
index 4faa81ac8..d98f52b06 100644
--- a/crypto/src/asn1/Asn1Object.cs
+++ b/crypto/src/asn1/Asn1Object.cs
@@ -52,7 +52,7 @@ namespace Org.BouncyCastle.Asn1
             return this;
         }
 
-		internal abstract void Encode(DerOutputStream derOut);
+		internal abstract void Encode(Asn1OutputStream asn1Out);
 
 		protected abstract bool Asn1Equals(Asn1Object asn1Object);
 		protected abstract int Asn1GetHashCode();
diff --git a/crypto/src/asn1/Asn1OutputStream.cs b/crypto/src/asn1/Asn1OutputStream.cs
index ffbe5c05d..240c7891b 100644
--- a/crypto/src/asn1/Asn1OutputStream.cs
+++ b/crypto/src/asn1/Asn1OutputStream.cs
@@ -6,6 +6,24 @@ namespace Org.BouncyCastle.Asn1
     public class Asn1OutputStream
         : DerOutputStream
     {
+        public static Asn1OutputStream Create(Stream output)
+        {
+            return new Asn1OutputStream(output);
+        }
+
+        public static Asn1OutputStream Create(Stream output, string encoding)
+        {
+            if (Asn1Encodable.Der.Equals(encoding))
+            {
+                return new DerOutputStreamNew(output);
+            }
+            else
+            {
+                return new Asn1OutputStream(output);
+            }
+        }
+
+        [Obsolete("Use static Create method(s)")]
         public Asn1OutputStream(Stream os)
             : base(os)
         {
@@ -15,5 +33,29 @@ namespace Org.BouncyCastle.Asn1
         {
             get { return true; }
         }
+
+        public override void WriteObject(Asn1Encodable obj)
+        {
+            if (obj == null)
+            {
+                WriteNull();
+            }
+            else
+            {
+                obj.ToAsn1Object().Encode(this);
+            }
+        }
+
+        public override void WriteObject(Asn1Object obj)
+        {
+            if (obj == null)
+            {
+                WriteNull();
+            }
+            else
+            {
+                obj.Encode(this);
+            }
+        }
     }
 }
diff --git a/crypto/src/asn1/BERBitString.cs b/crypto/src/asn1/BERBitString.cs
index 4ee52d9f8..0db973a07 100644
--- a/crypto/src/asn1/BERBitString.cs
+++ b/crypto/src/asn1/BERBitString.cs
@@ -27,15 +27,15 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            if (derOut.IsBer)
+            if (asn1Out.IsBer)
             {
-                derOut.WriteEncoded(Asn1Tags.BitString, (byte)mPadBits, mData);
+                asn1Out.WriteEncoded(Asn1Tags.BitString, (byte)mPadBits, mData);
             }
             else
             {
-                base.Encode(derOut);
+                base.Encode(asn1Out);
             }
         }
     }
diff --git a/crypto/src/asn1/BEROctetStringGenerator.cs b/crypto/src/asn1/BEROctetStringGenerator.cs
index 0bef886f2..de039014b 100644
--- a/crypto/src/asn1/BEROctetStringGenerator.cs
+++ b/crypto/src/asn1/BEROctetStringGenerator.cs
@@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Asn1
 			private byte[] _buf;
 			private int    _off;
 			private readonly BerOctetStringGenerator _gen;
-			private readonly DerOutputStream _derOut;
+			private readonly Asn1OutputStream _derOut;
 
 			internal BufferedBerOctetStream(
 				BerOctetStringGenerator	gen,
@@ -57,7 +57,7 @@ namespace Org.BouncyCastle.Asn1
 				_gen = gen;
 				_buf = buf;
 				_off = 0;
-				_derOut = new DerOutputStream(_gen.Out);
+				_derOut = Asn1OutputStream.Create(_gen.Out, Asn1Encodable.Der);
 			}
 
 			public override void WriteByte(
diff --git a/crypto/src/asn1/BerNull.cs b/crypto/src/asn1/BerNull.cs
index 8df088d0c..346fea02e 100644
--- a/crypto/src/asn1/BerNull.cs
+++ b/crypto/src/asn1/BerNull.cs
@@ -15,15 +15,15 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		internal override void Encode(DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
-            if (derOut.IsBer)
+            if (asn1Out.IsBer)
 			{
-				derOut.WriteByte(Asn1Tags.Null);
+				asn1Out.WriteByte(Asn1Tags.Null);
 			}
 			else
 			{
-				base.Encode(derOut);
+				base.Encode(asn1Out);
 			}
 		}
 	}
diff --git a/crypto/src/asn1/BerOctetString.cs b/crypto/src/asn1/BerOctetString.cs
index 1478488a6..2140faef1 100644
--- a/crypto/src/asn1/BerOctetString.cs
+++ b/crypto/src/asn1/BerOctetString.cs
@@ -121,29 +121,28 @@ namespace Org.BouncyCastle.Asn1
 			return GetEnumerator();
 		}
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            if (derOut.IsBer)
+            if (asn1Out.IsBer)
             {
-                derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);
+                asn1Out.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);
 
-                derOut.WriteByte(0x80);
+                asn1Out.WriteByte(0x80);
 
                 //
                 // write out the octet array
                 //
                 foreach (Asn1OctetString oct in this)
                 {
-                    oct.Encode(derOut);
+                    oct.Encode(asn1Out);
                 }
 
-				derOut.WriteByte(0x00);
-                derOut.WriteByte(0x00);
+				asn1Out.WriteByte(0x00);
+                asn1Out.WriteByte(0x00);
             }
             else
             {
-                base.Encode(derOut);
+                base.Encode(asn1Out);
             }
         }
 
diff --git a/crypto/src/asn1/BerOutputStream.cs b/crypto/src/asn1/BerOutputStream.cs
index 98c9dd7f8..54b72cf06 100644
--- a/crypto/src/asn1/BerOutputStream.cs
+++ b/crypto/src/asn1/BerOutputStream.cs
@@ -7,6 +7,7 @@ namespace Org.BouncyCastle.Asn1
     public class BerOutputStream
         : DerOutputStream
     {
+        [Obsolete("Use 'Asn1OutputStream.Create' instead")]
         public BerOutputStream(Stream os)
             : base(os)
         {
diff --git a/crypto/src/asn1/BerSequence.cs b/crypto/src/asn1/BerSequence.cs
index 30c44b651..1f09ecada 100644
--- a/crypto/src/asn1/BerSequence.cs
+++ b/crypto/src/asn1/BerSequence.cs
@@ -39,24 +39,24 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
 		{
-			if (derOut.IsBer)
+			if (asn1Out.IsBer)
 			{
-				derOut.WriteByte(Asn1Tags.Sequence | Asn1Tags.Constructed);
-				derOut.WriteByte(0x80);
+				asn1Out.WriteByte(Asn1Tags.Sequence | Asn1Tags.Constructed);
+				asn1Out.WriteByte(0x80);
 
 				foreach (Asn1Encodable o in this)
 				{
-                    o.ToAsn1Object().Encode(derOut);
+                    o.ToAsn1Object().Encode(asn1Out);
 				}
 
-				derOut.WriteByte(0x00);
-				derOut.WriteByte(0x00);
+				asn1Out.WriteByte(0x00);
+				asn1Out.WriteByte(0x00);
 			}
 			else
 			{
-				base.Encode(derOut);
+				base.Encode(asn1Out);
 			}
 		}
 	}
diff --git a/crypto/src/asn1/BerSet.cs b/crypto/src/asn1/BerSet.cs
index 4e752559d..95214fea9 100644
--- a/crypto/src/asn1/BerSet.cs
+++ b/crypto/src/asn1/BerSet.cs
@@ -44,24 +44,24 @@ namespace Org.BouncyCastle.Asn1
         {
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            if (derOut.IsBer)
+            if (asn1Out.IsBer)
             {
-                derOut.WriteByte(Asn1Tags.Set | Asn1Tags.Constructed);
-                derOut.WriteByte(0x80);
+                asn1Out.WriteByte(Asn1Tags.Set | Asn1Tags.Constructed);
+                asn1Out.WriteByte(0x80);
 
                 foreach (Asn1Encodable o in this)
 				{
-                    o.ToAsn1Object().Encode(derOut);
+                    o.ToAsn1Object().Encode(asn1Out);
                 }
 
-                derOut.WriteByte(0x00);
-                derOut.WriteByte(0x00);
+                asn1Out.WriteByte(0x00);
+                asn1Out.WriteByte(0x00);
             }
             else
             {
-                base.Encode(derOut);
+                base.Encode(asn1Out);
             }
         }
     }
diff --git a/crypto/src/asn1/BerTaggedObject.cs b/crypto/src/asn1/BerTaggedObject.cs
index 9256fc16d..9dadc24ba 100644
--- a/crypto/src/asn1/BerTaggedObject.cs
+++ b/crypto/src/asn1/BerTaggedObject.cs
@@ -47,13 +47,12 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
-			if (derOut.IsBer)
+			if (asn1Out.IsBer)
 			{
-				derOut.WriteTag(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo);
-				derOut.WriteByte(0x80);
+				asn1Out.WriteTag(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo);
+				asn1Out.WriteByte(0x80);
 
 				if (!IsEmpty())
 				{
@@ -87,21 +86,21 @@ namespace Org.BouncyCastle.Asn1
 
 						foreach (Asn1Encodable o in eObj)
 						{
-							derOut.WriteObject(o);
+							asn1Out.WriteObject(o);
 						}
 					}
 					else
 					{
-						derOut.WriteObject(obj);
+						asn1Out.WriteObject(obj);
 					}
 				}
 
-				derOut.WriteByte(0x00);
-				derOut.WriteByte(0x00);
+				asn1Out.WriteByte(0x00);
+				asn1Out.WriteByte(0x00);
 			}
 			else
 			{
-				base.Encode(derOut);
+				base.Encode(asn1Out);
 			}
 		}
 	}
diff --git a/crypto/src/asn1/DERExternal.cs b/crypto/src/asn1/DERExternal.cs
index c29975193..1e5a42f77 100644
--- a/crypto/src/asn1/DERExternal.cs
+++ b/crypto/src/asn1/DERExternal.cs
@@ -89,7 +89,7 @@ namespace Org.BouncyCastle.Asn1
 			ExternalContent = externalData.ToAsn1Object();
 		}
 
-		internal override void Encode(DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
 			MemoryStream ms = new MemoryStream();
 			WriteEncodable(ms, directReference);
@@ -97,7 +97,7 @@ namespace Org.BouncyCastle.Asn1
 			WriteEncodable(ms, dataValueDescriptor);
 			WriteEncodable(ms, new DerTaggedObject(Asn1Tags.External, externalContent));
 
-			derOut.WriteEncoded(Asn1Tags.Constructed, Asn1Tags.External, ms.ToArray());
+			asn1Out.WriteEncoded(Asn1Tags.Constructed, Asn1Tags.External, ms.ToArray());
 		}
 
 		protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DERSequenceGenerator.cs b/crypto/src/asn1/DERSequenceGenerator.cs
index c4348c78d..d5c8a7a64 100644
--- a/crypto/src/asn1/DERSequenceGenerator.cs
+++ b/crypto/src/asn1/DERSequenceGenerator.cs
@@ -24,13 +24,13 @@ namespace Org.BouncyCastle.Asn1
 		public override void AddObject(
 			Asn1Encodable obj)
 		{
-			new DerOutputStream(_bOut).WriteObject(obj);
+            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
 		}
 
         public override void AddObject(
             Asn1Object obj)
         {
-            new DerOutputStream(_bOut).WriteObject(obj);
+            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
         }
 
         public override Stream GetRawOutputStream()
diff --git a/crypto/src/asn1/DERSetGenerator.cs b/crypto/src/asn1/DERSetGenerator.cs
index a25dd7de8..6b4d03eec 100644
--- a/crypto/src/asn1/DERSetGenerator.cs
+++ b/crypto/src/asn1/DERSetGenerator.cs
@@ -24,13 +24,13 @@ namespace Org.BouncyCastle.Asn1
 		public override void AddObject(
 			Asn1Encodable obj)
 		{
-			new DerOutputStream(_bOut).WriteObject(obj);
+            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
 		}
 
         public override void AddObject(
             Asn1Object obj)
         {
-            new DerOutputStream(_bOut).WriteObject(obj);
+            Asn1OutputStream.Create(_bOut, Asn1Encodable.Der).WriteObject(obj);
         }
 
         public override Stream GetRawOutputStream()
diff --git a/crypto/src/asn1/DerApplicationSpecific.cs b/crypto/src/asn1/DerApplicationSpecific.cs
index a2d57bf9d..7bba8c5a9 100644
--- a/crypto/src/asn1/DerApplicationSpecific.cs
+++ b/crypto/src/asn1/DerApplicationSpecific.cs
@@ -163,8 +163,7 @@ namespace Org.BouncyCastle.Asn1
 			return FromByteArray(tmp);
 		}
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
 			int classBits = Asn1Tags.Application;
 			if (isConstructed)
@@ -172,7 +171,7 @@ namespace Org.BouncyCastle.Asn1
 				classBits |= Asn1Tags.Constructed; 
 			}
 
-			derOut.WriteEncoded(classBits, tag, octets);
+			asn1Out.WriteEncoded(classBits, tag, octets);
 		}
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerBMPString.cs b/crypto/src/asn1/DerBMPString.cs
index 061900b9c..eb9606096 100644
--- a/crypto/src/asn1/DerBMPString.cs
+++ b/crypto/src/asn1/DerBMPString.cs
@@ -111,8 +111,7 @@ namespace Org.BouncyCastle.Asn1
 			return this.str.Equals(other.str);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
             char[] c = str.ToCharArray();
             byte[] b = new byte[c.Length * 2];
@@ -123,7 +122,7 @@ namespace Org.BouncyCastle.Asn1
                 b[2 * i + 1] = (byte)c[i];
             }
 
-            derOut.WriteEncoded(Asn1Tags.BmpString, b);
+            asn1Out.WriteEncoded(Asn1Tags.BmpString, b);
         }
     }
 }
diff --git a/crypto/src/asn1/DerBitString.cs b/crypto/src/asn1/DerBitString.cs
index 26adc575b..282e97929 100644
--- a/crypto/src/asn1/DerBitString.cs
+++ b/crypto/src/asn1/DerBitString.cs
@@ -191,8 +191,7 @@ namespace Org.BouncyCastle.Asn1
 			}
 		}
 
-        internal override void Encode(
-			DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
 		{
             if (mPadBits > 0)
             {
@@ -209,12 +208,12 @@ namespace Org.BouncyCastle.Asn1
                      */
                     contents[contents.Length - 1] = (byte)(last ^ unusedBits);
 
-                    derOut.WriteEncoded(Asn1Tags.BitString, contents);
+                    asn1Out.WriteEncoded(Asn1Tags.BitString, contents);
                     return;
                 }
             }
 
-            derOut.WriteEncoded(Asn1Tags.BitString, (byte)mPadBits, mData);
+            asn1Out.WriteEncoded(Asn1Tags.BitString, (byte)mPadBits, mData);
 		}
 
         protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerBoolean.cs b/crypto/src/asn1/DerBoolean.cs
index 709f4ddce..3052c6e0d 100644
--- a/crypto/src/asn1/DerBoolean.cs
+++ b/crypto/src/asn1/DerBoolean.cs
@@ -81,11 +81,10 @@ namespace Org.BouncyCastle.Asn1
             get { return value != 0; }
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
             // TODO Should we make sure the byte value is one of '0' or '0xff' here?
-            derOut.WriteEncoded(Asn1Tags.Boolean, new byte[]{ value });
+            asn1Out.WriteEncoded(Asn1Tags.Boolean, new byte[]{ value });
         }
 
         protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerEnumerated.cs b/crypto/src/asn1/DerEnumerated.cs
index eb0555745..f00eab05e 100644
--- a/crypto/src/asn1/DerEnumerated.cs
+++ b/crypto/src/asn1/DerEnumerated.cs
@@ -119,9 +119,9 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.Enumerated, bytes);
+            asn1Out.WriteEncoded(Asn1Tags.Enumerated, bytes);
         }
 
         protected override bool Asn1Equals(Asn1Object asn1Object)
diff --git a/crypto/src/asn1/DerGeneralString.cs b/crypto/src/asn1/DerGeneralString.cs
index 553b0e09c..f6e800e73 100644
--- a/crypto/src/asn1/DerGeneralString.cs
+++ b/crypto/src/asn1/DerGeneralString.cs
@@ -61,10 +61,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(str);
         }
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.GeneralString, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.GeneralString, GetOctets());
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerGeneralizedTime.cs b/crypto/src/asn1/DerGeneralizedTime.cs
index b224ebe42..0b603d65d 100644
--- a/crypto/src/asn1/DerGeneralizedTime.cs
+++ b/crypto/src/asn1/DerGeneralizedTime.cs
@@ -295,10 +295,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(time);
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.GeneralizedTime, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.GeneralizedTime, GetOctets());
         }
 
         protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerGraphicString.cs b/crypto/src/asn1/DerGraphicString.cs
index f213f461d..879a2f658 100644
--- a/crypto/src/asn1/DerGraphicString.cs
+++ b/crypto/src/asn1/DerGraphicString.cs
@@ -79,9 +79,9 @@ namespace Org.BouncyCastle.Asn1
             return Arrays.Clone(mString);
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.GraphicString, mString);
+            asn1Out.WriteEncoded(Asn1Tags.GraphicString, mString);
         }
 
         protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerIA5String.cs b/crypto/src/asn1/DerIA5String.cs
index 63e91582e..22311fdb8 100644
--- a/crypto/src/asn1/DerIA5String.cs
+++ b/crypto/src/asn1/DerIA5String.cs
@@ -100,10 +100,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(str);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.IA5String, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.IA5String, GetOctets());
         }
 
 		protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerInteger.cs b/crypto/src/asn1/DerInteger.cs
index 67916538b..43e991b36 100644
--- a/crypto/src/asn1/DerInteger.cs
+++ b/crypto/src/asn1/DerInteger.cs
@@ -169,9 +169,9 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.Integer, bytes);
+            asn1Out.WriteEncoded(Asn1Tags.Integer, bytes);
         }
 
 		protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerNull.cs b/crypto/src/asn1/DerNull.cs
index fd7049699..60f040b40 100644
--- a/crypto/src/asn1/DerNull.cs
+++ b/crypto/src/asn1/DerNull.cs
@@ -16,9 +16,9 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		internal override void Encode(DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
-			derOut.WriteEncoded(Asn1Tags.Null, ZeroBytes);
+			asn1Out.WriteEncoded(Asn1Tags.Null, ZeroBytes);
 		}
 
 		protected override bool Asn1Equals(Asn1Object asn1Object)
diff --git a/crypto/src/asn1/DerNumericString.cs b/crypto/src/asn1/DerNumericString.cs
index a729f9e8e..1163d71f4 100644
--- a/crypto/src/asn1/DerNumericString.cs
+++ b/crypto/src/asn1/DerNumericString.cs
@@ -100,10 +100,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(str);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.NumericString, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.NumericString, GetOctets());
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs
index 29bd35e84..aa40c4c06 100644
--- a/crypto/src/asn1/DerObjectIdentifier.cs
+++ b/crypto/src/asn1/DerObjectIdentifier.cs
@@ -197,9 +197,9 @@ namespace Org.BouncyCastle.Asn1
             }
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.ObjectIdentifier, GetContents());
+            asn1Out.WriteEncoded(Asn1Tags.ObjectIdentifier, GetContents());
         }
 
         protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerOctetString.cs b/crypto/src/asn1/DerOctetString.cs
index d79607950..b9659bf84 100644
--- a/crypto/src/asn1/DerOctetString.cs
+++ b/crypto/src/asn1/DerOctetString.cs
@@ -22,19 +22,14 @@ namespace Org.BouncyCastle.Asn1
         {
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.OctetString, str);
+            asn1Out.WriteEncoded(Asn1Tags.OctetString, str);
         }
 
-		internal static void Encode(
-			DerOutputStream	derOut,
-			byte[]			bytes,
-			int				offset,
-			int				length)
+		internal static void Encode(Asn1OutputStream asn1Out, byte[] bytes, int offset, int length)
 		{
-			derOut.WriteEncoded(Asn1Tags.OctetString, bytes, offset, length);
+			asn1Out.WriteEncoded(Asn1Tags.OctetString, bytes, offset, length);
 		}
 	}
 }
diff --git a/crypto/src/asn1/DerOutputStream.cs b/crypto/src/asn1/DerOutputStream.cs
index c9c70a3f3..3ecdcd11f 100644
--- a/crypto/src/asn1/DerOutputStream.cs
+++ b/crypto/src/asn1/DerOutputStream.cs
@@ -5,9 +5,11 @@ using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Asn1
 {
+    [Obsolete("Use 'Asn1OutputStream' instead")]
     public class DerOutputStream
         : FilterStream
     {
+        [Obsolete("Use 'Asn1OutputStream.Create' instead")]
         public DerOutputStream(Stream os)
             : base(os)
         {
@@ -134,7 +136,7 @@ namespace Org.BouncyCastle.Asn1
 			}
 			else
 			{
-				obj.ToAsn1Object().Encode(this);
+				obj.ToAsn1Object().Encode(new DerOutputStreamNew(s));
 			}
 		}
 
@@ -147,8 +149,22 @@ namespace Org.BouncyCastle.Asn1
 			}
 			else
 			{
-				obj.Encode(this);
+				obj.Encode(new DerOutputStreamNew(s));
 			}
 		}
 	}
+
+    internal class DerOutputStreamNew
+        : Asn1OutputStream
+    {
+        internal DerOutputStreamNew(Stream os)
+            : base(os)
+        {
+        }
+
+        internal override bool IsBer
+        {
+            get { return false; }
+        }
+    }
 }
diff --git a/crypto/src/asn1/DerPrintableString.cs b/crypto/src/asn1/DerPrintableString.cs
index e1797346d..5198dc30c 100644
--- a/crypto/src/asn1/DerPrintableString.cs
+++ b/crypto/src/asn1/DerPrintableString.cs
@@ -100,10 +100,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(str);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.PrintableString, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.PrintableString, GetOctets());
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs
index 9d5951418..651350d91 100644
--- a/crypto/src/asn1/DerSequence.cs
+++ b/crypto/src/asn1/DerSequence.cs
@@ -53,17 +53,17 @@ namespace Org.BouncyCastle.Asn1
 		 * ASN.1 descriptions given. Rather than just outputing Sequence,
 		 * we also have to specify Constructed, and the objects length.
 		 */
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
             if (Count < 1)
             {
-                derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
+                asn1Out.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
                 return;
             }
 
             // TODO Intermediate buffer could be avoided if we could calculate expected length
             MemoryStream bOut = new MemoryStream();
-            DerOutputStream dOut = new DerOutputStream(bOut);
+            Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Asn1Encodable.Der);
 
             foreach (Asn1Encodable obj in this)
             {
@@ -80,7 +80,7 @@ namespace Org.BouncyCastle.Asn1
             int length = (int)bOut.Position;
 #endif
 
-            derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes, 0, length);
+            asn1Out.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, bytes, 0, length);
 
             Platform.Dispose(dOut);
         }
diff --git a/crypto/src/asn1/DerSet.cs b/crypto/src/asn1/DerSet.cs
index 3818d286f..0808434d7 100644
--- a/crypto/src/asn1/DerSet.cs
+++ b/crypto/src/asn1/DerSet.cs
@@ -70,17 +70,17 @@ namespace Org.BouncyCastle.Asn1
 		 * ASN.1 descriptions given. Rather than just outputing Set,
 		 * we also have to specify Constructed, and the objects length.
 		 */
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
             if (Count < 1)
             {
-                derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
+                asn1Out.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, Asn1OctetString.EmptyOctets);
                 return;
             }
 
             // TODO Intermediate buffer could be avoided if we could calculate expected length
             MemoryStream bOut = new MemoryStream();
-            DerOutputStream dOut = new DerOutputStream(bOut);
+            Asn1OutputStream dOut = Asn1OutputStream.Create(bOut, Asn1Encodable.Der);
 
             foreach (Asn1Encodable obj in this)
             {
@@ -97,7 +97,7 @@ namespace Org.BouncyCastle.Asn1
             int length = (int)bOut.Position;
 #endif
 
-            derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, bytes, 0, length);
+            asn1Out.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, bytes, 0, length);
 
             Platform.Dispose(dOut);
         }
diff --git a/crypto/src/asn1/DerT61String.cs b/crypto/src/asn1/DerT61String.cs
index 746ccfe70..40906f3c1 100644
--- a/crypto/src/asn1/DerT61String.cs
+++ b/crypto/src/asn1/DerT61String.cs
@@ -77,10 +77,9 @@ namespace Org.BouncyCastle.Asn1
             return str;
         }
 
-        internal override void Encode(
-            DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.T61String, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.T61String, GetOctets());
         }
 
         public byte[] GetOctets()
diff --git a/crypto/src/asn1/DerTaggedObject.cs b/crypto/src/asn1/DerTaggedObject.cs
index 717d724b6..182d5ee47 100644
--- a/crypto/src/asn1/DerTaggedObject.cs
+++ b/crypto/src/asn1/DerTaggedObject.cs
@@ -42,8 +42,7 @@ namespace Org.BouncyCastle.Asn1
 		{
 		}
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
 			if (!IsEmpty())
 			{
@@ -51,7 +50,7 @@ namespace Org.BouncyCastle.Asn1
 
 				if (explicitly)
 				{
-					derOut.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, bytes);
+					asn1Out.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, bytes);
 				}
 				else
 				{
@@ -59,13 +58,13 @@ namespace Org.BouncyCastle.Asn1
 					// need to mark constructed types... (preserve Constructed tag)
 					//
 					int flags = (bytes[0] & Asn1Tags.Constructed) | Asn1Tags.Tagged;
-					derOut.WriteTag(flags, tagNo);
-					derOut.Write(bytes, 1, bytes.Length - 1);
+					asn1Out.WriteTag(flags, tagNo);
+					asn1Out.Write(bytes, 1, bytes.Length - 1);
 				}
 			}
 			else
 			{
-				derOut.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, new byte[0]);
+				asn1Out.WriteEncoded(Asn1Tags.Constructed | Asn1Tags.Tagged, tagNo, new byte[0]);
 			}
 		}
 	}
diff --git a/crypto/src/asn1/DerUTCTime.cs b/crypto/src/asn1/DerUTCTime.cs
index 5d058619d..7c8330874 100644
--- a/crypto/src/asn1/DerUTCTime.cs
+++ b/crypto/src/asn1/DerUTCTime.cs
@@ -237,10 +237,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(time);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.UtcTime, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.UtcTime, GetOctets());
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerUTF8String.cs b/crypto/src/asn1/DerUTF8String.cs
index 758a5068d..72e882afe 100644
--- a/crypto/src/asn1/DerUTF8String.cs
+++ b/crypto/src/asn1/DerUTF8String.cs
@@ -89,10 +89,9 @@ namespace Org.BouncyCastle.Asn1
 			return this.str.Equals(other.str);
         }
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.Utf8String, Encoding.UTF8.GetBytes(str));
+            asn1Out.WriteEncoded(Asn1Tags.Utf8String, Encoding.UTF8.GetBytes(str));
         }
     }
 }
diff --git a/crypto/src/asn1/DerUniversalString.cs b/crypto/src/asn1/DerUniversalString.cs
index 284d0f8c5..e7367dc38 100644
--- a/crypto/src/asn1/DerUniversalString.cs
+++ b/crypto/src/asn1/DerUniversalString.cs
@@ -86,10 +86,9 @@ namespace Org.BouncyCastle.Asn1
             return (byte[]) str.Clone();
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.UniversalString, this.str);
+            asn1Out.WriteEncoded(Asn1Tags.UniversalString, this.str);
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/DerVideotexString.cs b/crypto/src/asn1/DerVideotexString.cs
index b25401044..c52987368 100644
--- a/crypto/src/asn1/DerVideotexString.cs
+++ b/crypto/src/asn1/DerVideotexString.cs
@@ -79,9 +79,9 @@ namespace Org.BouncyCastle.Asn1
             return Arrays.Clone(mString);
         }
 
-        internal override void Encode(DerOutputStream derOut)
+        internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.VideotexString, mString);
+            asn1Out.WriteEncoded(Asn1Tags.VideotexString, mString);
         }
 
         protected override int Asn1GetHashCode()
diff --git a/crypto/src/asn1/DerVisibleString.cs b/crypto/src/asn1/DerVisibleString.cs
index e1112201a..3d25e788e 100644
--- a/crypto/src/asn1/DerVisibleString.cs
+++ b/crypto/src/asn1/DerVisibleString.cs
@@ -86,10 +86,9 @@ namespace Org.BouncyCastle.Asn1
             return Strings.ToAsciiByteArray(str);
         }
 
-		internal override void Encode(
-            DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
         {
-            derOut.WriteEncoded(Asn1Tags.VisibleString, GetOctets());
+            asn1Out.WriteEncoded(Asn1Tags.VisibleString, GetOctets());
         }
 
 		protected override bool Asn1Equals(
diff --git a/crypto/src/asn1/LazyDERSequence.cs b/crypto/src/asn1/LazyDERSequence.cs
index 78e9df151..d0548ebb2 100644
--- a/crypto/src/asn1/LazyDERSequence.cs
+++ b/crypto/src/asn1/LazyDERSequence.cs
@@ -57,18 +57,17 @@ namespace Org.BouncyCastle.Asn1
 			}
 		}
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
 			lock (this)
 			{
 				if (encoded == null)
 				{
-					base.Encode(derOut);
+					base.Encode(asn1Out);
 				}
 				else
 				{
-					derOut.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, encoded);
+					asn1Out.WriteEncoded(Asn1Tags.Sequence | Asn1Tags.Constructed, encoded);
 				}
 			}
 		}
diff --git a/crypto/src/asn1/LazyDERSet.cs b/crypto/src/asn1/LazyDERSet.cs
index 25dbdd0a4..653054bf9 100644
--- a/crypto/src/asn1/LazyDERSet.cs
+++ b/crypto/src/asn1/LazyDERSet.cs
@@ -57,18 +57,17 @@ namespace Org.BouncyCastle.Asn1
 			}
 		}
 
-		internal override void Encode(
-			DerOutputStream derOut)
+		internal override void Encode(Asn1OutputStream asn1Out)
 		{
 			lock (this)
 			{
 				if (encoded == null)
 				{
-					base.Encode(derOut);
+					base.Encode(asn1Out);
 				}
 				else
 				{
-					derOut.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, encoded);
+					asn1Out.WriteEncoded(Asn1Tags.Set | Asn1Tags.Constructed, encoded);
 				}
 			}
 		}
diff --git a/crypto/src/cms/CMSSignedDataGenerator.cs b/crypto/src/cms/CMSSignedDataGenerator.cs
index f2676a440..beabab911 100644
--- a/crypto/src/cms/CMSSignedDataGenerator.cs
+++ b/crypto/src/cms/CMSSignedDataGenerator.cs
@@ -165,8 +165,8 @@ namespace Org.BouncyCastle.Cms
 
 					signedAttr = outer.GetAttributeSet(signed);
 
-					// sig must be composed from the DER encoding.
-					new DerOutputStream(sigStr).WriteObject(signedAttr);
+                    // sig must be composed from the DER encoding.
+                    Asn1OutputStream.Create(sigStr, Asn1Encodable.Der).WriteObject(signedAttr);
 				}
                 else if (content != null)
                 {
diff --git a/crypto/src/pkcs/Pkcs12Store.cs b/crypto/src/pkcs/Pkcs12Store.cs
index f09198694..2d7de67e6 100644
--- a/crypto/src/pkcs/Pkcs12Store.cs
+++ b/crypto/src/pkcs/Pkcs12Store.cs
@@ -1030,15 +1030,8 @@ namespace Org.BouncyCastle.Pkcs
             //
             Pfx pfx = new Pfx(mainInfo, macData);
 
-            DerOutputStream derOut;
-            if (useDerEncoding)
-            {
-                derOut = new DerOutputStream(stream);
-            }
-            else
-            {
-                derOut = new Asn1OutputStream(stream);
-            }
+            Asn1OutputStream derOut = Asn1OutputStream.Create(stream,
+                useDerEncoding ? Asn1Encodable.Der : Asn1Encodable.Ber);
 
             derOut.WriteObject(pfx);
         }