diff options
-rw-r--r-- | crypto/src/asn1/Asn1Encodable.cs | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/crypto/src/asn1/Asn1Encodable.cs b/crypto/src/asn1/Asn1Encodable.cs index 6314bf76f..f42f0bbcf 100644 --- a/crypto/src/asn1/Asn1Encodable.cs +++ b/crypto/src/asn1/Asn1Encodable.cs @@ -18,35 +18,39 @@ namespace Org.BouncyCastle.Asn1 return bOut.ToArray(); } - public byte[] GetEncoded( - string encoding) - { - if (encoding.Equals(Der)) - { - MemoryStream bOut = new MemoryStream(); - DerOutputStream dOut = new DerOutputStream(bOut); - - if (this is Asn1Set) - { - dOut.WriteObject(new DerSet((this as Asn1Set).elements)); - } - else - { - dOut.WriteObject(this); - } - - return bOut.ToArray(); - } + public byte[] GetEncoded(string encoding) + { + if (encoding.Equals(Der)) + { + MemoryStream bOut = new MemoryStream(); + DerOutputStream dOut = new DerOutputStream(bOut); - return GetEncoded(); - } + Asn1Object asn1Object = ToAsn1Object(); + + Asn1Set asn1Set = asn1Object as Asn1Set; + if (null != asn1Set) + { + /* + * NOTE: Even a DerSet isn't necessarily already in sorted order (particularly from DerSetParser), + * so all sets have to be converted here. + */ + asn1Object = new DerSet(asn1Set.elements); + } + + asn1Object.Encode(dOut); + + return bOut.ToArray(); + } + + return GetEncoded(); + } - /** + /** * Return the DER encoding of the object, null if the DER encoding can not be made. * * @return a DER byte array, null otherwise. */ - public byte[] GetDerEncoded() + public byte[] GetDerEncoded() { try { |