From 29120b6b23e3a8154089c41101ec021d56c2e4fc Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 1 Feb 2023 13:53:13 +0700 Subject: Add GetInstanceFromChoice helper --- crypto/src/asn1/Asn1Utilities.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crypto/src/asn1/Asn1Utilities.cs b/crypto/src/asn1/Asn1Utilities.cs index 5605d4c54..6ccdfc43a 100644 --- a/crypto/src/asn1/Asn1Utilities.cs +++ b/crypto/src/asn1/Asn1Utilities.cs @@ -1,6 +1,8 @@ using System; using System.IO; +using Org.BouncyCastle.Utilities; + namespace Org.BouncyCastle.Asn1 { public abstract class Asn1Utilities @@ -29,6 +31,23 @@ namespace Org.BouncyCastle.Asn1 } + internal static TChoice GetInstanceFromChoice(Asn1TaggedObject taggedObject, bool declaredExplicit, + Func constructor) + where TChoice : Asn1Encodable, IAsn1Choice + { + if (!declaredExplicit) + { + var message = string.Format( + "Implicit tagging cannot be used with untagged choice type {0} (X.680 30.6, 30.8).", + Platform.GetTypeName(typeof(TChoice))); + + throw new ArgumentException(message, nameof(declaredExplicit)); + } + + return constructor(taggedObject.GetExplicitBaseObject()); + } + + internal static string GetTagText(Asn1Tag tag) { return GetTagText(tag.TagClass, tag.TagNo); -- cgit 1.4.1