From 4c3c4694b6331625eee6e9ec1c92b0d17c6db7bf Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 9 Nov 2021 16:27:36 +0700 Subject: Fix DER encoding of lazy objects --- crypto/src/asn1/LazyDLSequence.cs | 16 ++++++++++++---- crypto/src/asn1/LazyDLSet.cs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crypto/src/asn1/LazyDLSequence.cs b/crypto/src/asn1/LazyDLSequence.cs index fe8e71794..63152e446 100644 --- a/crypto/src/asn1/LazyDLSequence.cs +++ b/crypto/src/asn1/LazyDLSequence.cs @@ -65,6 +65,7 @@ namespace Org.BouncyCastle.Asn1 internal override int EncodedLength(bool withID) { + // TODO This depends on knowing it's BER byte[] encoded = GetContents(); if (encoded != null) { @@ -76,11 +77,18 @@ namespace Org.BouncyCastle.Asn1 internal override void Encode(Asn1OutputStream asn1Out, bool withID) { - byte[] encoded = GetContents(); - if (encoded != null) + if (asn1Out.IsBer) { - asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Sequence, encoded); - return; + byte[] encoded = GetContents(); + if (encoded != null) + { + asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Sequence, encoded); + return; + } + } + else + { + Force(); } base.Encode(asn1Out, withID); diff --git a/crypto/src/asn1/LazyDLSet.cs b/crypto/src/asn1/LazyDLSet.cs index f644f69e5..0d4d84310 100644 --- a/crypto/src/asn1/LazyDLSet.cs +++ b/crypto/src/asn1/LazyDLSet.cs @@ -65,6 +65,7 @@ namespace Org.BouncyCastle.Asn1 internal override int EncodedLength(bool withID) { + // TODO This depends on knowing it's BER byte[] encoded = GetContents(); if (encoded != null) { @@ -76,11 +77,18 @@ namespace Org.BouncyCastle.Asn1 internal override void Encode(Asn1OutputStream asn1Out, bool withID) { - byte[] encoded = GetContents(); - if (encoded != null) + if (asn1Out.IsBer) { - asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, encoded); - return; + byte[] encoded = GetContents(); + if (encoded != null) + { + asn1Out.WriteEncodingDL(withID, Asn1Tags.Constructed | Asn1Tags.Set, encoded); + return; + } + } + else + { + Force(); } base.Encode(asn1Out, withID); -- cgit 1.4.1