summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-01 13:53:13 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-01 13:53:13 +0700
commit29120b6b23e3a8154089c41101ec021d56c2e4fc (patch)
tree4f6e4b1fcf8d00dbe1323ae2dfa87fd592c2a18d
parentMerge branch 'elephant' into 'master' (diff)
downloadBouncyCastle.NET-ed25519-29120b6b23e3a8154089c41101ec021d56c2e4fc.tar.xz
Add GetInstanceFromChoice helper
-rw-r--r--crypto/src/asn1/Asn1Utilities.cs19
1 files changed, 19 insertions, 0 deletions
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<TChoice>(Asn1TaggedObject taggedObject, bool declaredExplicit,
+            Func<object, TChoice> 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);