From 46230a012af2824a5e742db8e6dc5b23c720d696 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 11 Nov 2021 14:06:03 +0700 Subject: ASN.1: Update encoding in line with bc-java --- crypto/src/asn1/Asn1OctetString.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'crypto/src/asn1/Asn1OctetString.cs') diff --git a/crypto/src/asn1/Asn1OctetString.cs b/crypto/src/asn1/Asn1OctetString.cs index d5766e299..66d6fb8d1 100644 --- a/crypto/src/asn1/Asn1OctetString.cs +++ b/crypto/src/asn1/Asn1OctetString.cs @@ -58,14 +58,36 @@ namespace Org.BouncyCastle.Asn1 */ public static Asn1OctetString GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { + if (declaredExplicit) + { + if (!taggedObject.IsExplicit()) + throw new ArgumentException("object implicit - explicit expected."); + + return GetInstance(taggedObject.GetObject()); + } + Asn1Object baseObject = taggedObject.GetObject(); - if (declaredExplicit || baseObject is Asn1OctetString) + // If parsed as explicit though declared implicit, it should have been a set of one + if (taggedObject.IsExplicit()) { - return GetInstance(baseObject); + Asn1OctetString singleSegment = GetInstance(baseObject); + + if (taggedObject is BerTaggedObject) + return new BerOctetString(new Asn1OctetString[]{ singleSegment }); + + return singleSegment; } - return BerOctetString.FromSequence(Asn1Sequence.GetInstance(baseObject)); + if (baseObject is Asn1OctetString) + return (Asn1OctetString)baseObject; + + // Parser assumes implicit constructed encodings are sequences + if (baseObject is Asn1Sequence) + return ((Asn1Sequence)baseObject).ToAsn1OctetString(); + + throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(taggedObject), + "taggedObject"); } internal readonly byte[] contents; -- cgit 1.4.1