diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-16 22:37:52 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-16 22:37:52 +0700 |
commit | be15049124860ccb7335b92215e8b8dfa1821bf9 (patch) | |
tree | 7a86ed2110021307465a617076d601ca812c69ed /crypto/src/asn1/DerSequence.cs | |
parent | Make cast more specific (diff) | |
download | BouncyCastle.NET-ed25519-be15049124860ccb7335b92215e8b8dfa1821bf9.tar.xz |
ASN.1: Staged encoding
Diffstat (limited to 'crypto/src/asn1/DerSequence.cs')
-rw-r--r-- | crypto/src/asn1/DerSequence.cs | 63 |
1 files changed, 6 insertions, 57 deletions
diff --git a/crypto/src/asn1/DerSequence.cs b/crypto/src/asn1/DerSequence.cs index e60796091..2ac977fe0 100644 --- a/crypto/src/asn1/DerSequence.cs +++ b/crypto/src/asn1/DerSequence.cs @@ -12,8 +12,6 @@ namespace Org.BouncyCastle.Asn1 return elementVector.Count < 1 ? Empty : new DerSequence(elementVector); } - private int m_contentsLengthDer = -1; - /** * create an empty sequence */ @@ -48,56 +46,16 @@ namespace Org.BouncyCastle.Asn1 { } - internal override int EncodedLength(int encoding, bool withID) + internal override IAsn1Encoding GetEncoding(int encoding) { - return Asn1OutputStream.GetLengthOfEncodingDL(withID, GetContentsLengthDer()); + return new ConstructedDLEncoding(Asn1Tags.Universal, Asn1Tags.Sequence, + Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDer, elements)); } - /* - * A note on the implementation: - * <p> - * As Der requires the constructed, definite-length model to - * be used for structured types, this varies slightly from the - * ASN.1 descriptions given. Rather than just outputing Sequence, - * we also have to specify Constructed, and the objects length. - */ - internal override void Encode(Asn1OutputStream asn1Out, bool withID) + internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) { - asn1Out = asn1Out.GetDerSubStream(); - - asn1Out.WriteIdentifier(withID, Asn1Tags.Constructed | Asn1Tags.Sequence); - - int count = elements.Length; - if (m_contentsLengthDer >= 0 || count > 16) - { - asn1Out.WriteDL(GetContentsLengthDer()); - - for (int i = 0; i < count; ++i) - { - Asn1Object asn1Object = elements[i].ToAsn1Object(); - asn1Object.Encode(asn1Out, true); - } - } - else - { - int contentsLength = 0; - - Asn1Object[] asn1Objects = new Asn1Object[count]; - for (int i = 0; i < count; ++i) - { - Asn1Object asn1Object = elements[i].ToAsn1Object(); - asn1Objects[i] = asn1Object; - contentsLength += asn1Object.EncodedLength(asn1Out.Encoding, true); - } - - this.m_contentsLengthDer = contentsLength; - asn1Out.WriteDL(contentsLength); - - for (int i = 0; i < count; ++i) - { - asn1Objects[i].Encode(asn1Out, true); - } - } + return new ConstructedDLEncoding(tagClass, tagNo, + Asn1OutputStream.GetContentsEncodings(Asn1OutputStream.EncodingDer, elements)); } internal override DerBitString ToAsn1BitString() @@ -120,14 +78,5 @@ namespace Org.BouncyCastle.Asn1 // NOTE: DLSet is intentional, we don't want sorting return new DLSet(false, elements); } - - private int GetContentsLengthDer() - { - if (m_contentsLengthDer < 0) - { - m_contentsLengthDer = CalculateContentsLength(Asn1OutputStream.EncodingDer); - } - return m_contentsLengthDer; - } } } |