From cefa1317c853c3f6d7eaf669771c3bf60454a921 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 13 Oct 2021 23:57:48 +0700 Subject: Improve ASN.1 set special handling --- crypto/src/asn1/Asn1Encodable.cs | 50 ++++++++++++++++++++++------------------ 1 file 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 { -- cgit 1.4.1