diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-10-13 23:57:48 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-10-13 23:57:48 +0700 |
commit | cefa1317c853c3f6d7eaf669771c3bf60454a921 (patch) | |
tree | 30bc89f6cfb2877766288bf015eda288a19e2bde | |
parent | DER sequence/set encoding opts. (diff) | |
download | BouncyCastle.NET-ed25519-cefa1317c853c3f6d7eaf669771c3bf60454a921.tar.xz |
Improve ASN.1 set special handling
-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 { |