summary refs log tree commit diff
path: root/crypto/src/asn1/DerNumericString.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-06-05 17:09:26 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-06-05 17:09:26 +0700
commitfc860ec52e9f8eac943c92e7d8cfd812c7a7f1d9 (patch)
tree0baac94ba9a3dad934f081bdeb190d485cc4ec0d /crypto/src/asn1/DerNumericString.cs
parentRefactoring in Asn1 (diff)
downloadBouncyCastle.NET-ed25519-fc860ec52e9f8eac943c92e7d8cfd812c7a7f1d9.tar.xz
ASN.1: GetOptional method for all universal types
Diffstat (limited to 'crypto/src/asn1/DerNumericString.cs')
-rw-r--r--crypto/src/asn1/DerNumericString.cs17
1 files changed, 17 insertions, 0 deletions
diff --git a/crypto/src/asn1/DerNumericString.cs b/crypto/src/asn1/DerNumericString.cs
index 44adceb6b..81871f3c9 100644
--- a/crypto/src/asn1/DerNumericString.cs
+++ b/crypto/src/asn1/DerNumericString.cs
@@ -68,6 +68,23 @@ namespace Org.BouncyCastle.Asn1
             return (DerNumericString)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit);
         }
 
+        public static DerNumericString GetOptional(Asn1Encodable element)
+        {
+            if (element == null)
+                throw new ArgumentNullException(nameof(element));
+
+            if (element is DerNumericString existing)
+                return existing;
+
+            if (element is IAsn1Convertible asn1Convertible && !(element is Asn1Object) &&
+                asn1Convertible.ToAsn1Object() is DerNumericString converted)
+            {
+                return converted;
+            }
+
+            return null;
+        }
+
         private readonly byte[] m_contents;
 
         public DerNumericString(string str)