diff options
Diffstat (limited to 'crypto/src/asn1/x500/DirectoryString.cs')
-rw-r--r-- | crypto/src/asn1/x500/DirectoryString.cs | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/crypto/src/asn1/x500/DirectoryString.cs b/crypto/src/asn1/x500/DirectoryString.cs index a04ed0056..c622113d4 100644 --- a/crypto/src/asn1/x500/DirectoryString.cs +++ b/crypto/src/asn1/x500/DirectoryString.cs @@ -11,30 +11,50 @@ namespace Org.BouncyCastle.Asn1.X500 public static DirectoryString GetInstance(object obj) { - if (obj == null || obj is DirectoryString) - return (DirectoryString) obj; - - if (obj is DerStringBase) - { - if (obj is DerT61String - || obj is DerPrintableString - || obj is DerUniversalString - || obj is DerUtf8String - || obj is DerBmpString) - { - return new DirectoryString((DerStringBase) obj); - } - } - - throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); - } + if (obj == null) + return null; + + if (obj is Asn1Encodable element) + { + var result = GetOptional(element); + if (result != null) + return result; + } + + throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj)); + } public static DirectoryString GetInstance(Asn1TaggedObject obj, bool isExplicit) => Asn1Utilities.GetInstanceChoice(obj, isExplicit, GetInstance); + public static DirectoryString GetOptional(Asn1Encodable element) + { + if (element == null) + throw new ArgumentNullException(nameof(element)); + + if (element is DirectoryString directoryString) + return directoryString; + + var innerObject = GetOptionalInnerObject(element); + if (innerObject != null) + return new DirectoryString(innerObject); + + return null; + } + public static DirectoryString GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance); + private static DerStringBase GetOptionalInnerObject(Asn1Encodable element) + { + return DerT61String.GetOptional(element) + ?? DerPrintableString.GetOptional(element) + ?? DerUniversalString.GetOptional(element) + ?? DerUtf8String.GetOptional(element) + ?? DerBmpString.GetOptional(element) + ?? (DerStringBase)null; + } + private DirectoryString( DerStringBase str) { |