diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-07 16:24:01 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-07 16:24:01 +0700 |
commit | e78d715bfd0c981d5f11dfae4442491289ad5690 (patch) | |
tree | 58550dec8481292a828d50b41d558602f0ff22ec /crypto/src/asn1/DerSet.cs | |
parent | Remove lazy ASN.1 behaviour (diff) | |
download | BouncyCastle.NET-ed25519-e78d715bfd0c981d5f11dfae4442491289ad5690.tar.xz |
Sort DER sets without encoding elements
Diffstat (limited to 'crypto/src/asn1/DerSet.cs')
-rw-r--r-- | crypto/src/asn1/DerSet.cs | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/crypto/src/asn1/DerSet.cs b/crypto/src/asn1/DerSet.cs index db4c7b1a2..99a8dc284 100644 --- a/crypto/src/asn1/DerSet.cs +++ b/crypto/src/asn1/DerSet.cs @@ -1,5 +1,7 @@ using System; +using Org.BouncyCastle.Utilities; + namespace Org.BouncyCastle.Asn1 { /** @@ -61,31 +63,37 @@ namespace Org.BouncyCastle.Asn1 internal override IAsn1Encoding GetEncoding(int encoding) { - return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Set, - Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDer, GetSortedElements())); + return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Set, GetSortedDerEncodings()); } internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) { - return new ConstructedDLEncoding(tagClass, tagNo, - Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDer, GetSortedElements())); + return new ConstructedDLEncoding(tagClass, tagNo, GetSortedDerEncodings()); } - private Asn1Encodable[] GetSortedElements() + internal override DerEncoding GetEncodingDer() { - if (m_sortedElements == null) - { - int count = m_elements.Length; - Asn1Object[] asn1Objects = new Asn1Object[count]; - for (int i = 0; i < count; ++i) - { - asn1Objects[i] = m_elements[i].ToAsn1Object(); - } - Sort(asn1Objects); - m_sortedElements = asn1Objects; - } + return new ConstructedDerEncoding(Asn1Tags.Universal, Asn1Tags.Set, GetSortedDerEncodings()); + } + + internal override DerEncoding GetEncodingDerImplicit(int tagClass, int tagNo) + { + return new ConstructedDerEncoding(tagClass, tagNo, GetSortedDerEncodings()); + } - return m_sortedElements; + private DerEncoding[] GetSortedDerEncodings() + { + return Objects.EnsureSingletonInitialized(ref m_sortedDerEncodings, m_elements, CreateSortedDerEncodings); + } + + private static DerEncoding[] CreateSortedDerEncodings(Asn1Encodable[] elements) + { + var derEncodings = Asn1OutputStream.GetContentsEncodingsDer(elements); + if (derEncodings.Length > 1) + { + Array.Sort(derEncodings); + } + return derEncodings; } } } |