diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2017-09-17 18:47:43 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2017-09-17 18:47:43 +0700 |
commit | b30a73590e054908e41231dfd200881dcaea0907 (patch) | |
tree | f1ab227d10d91f9bee6e32f26c324956c6a394b1 | |
parent | Sketch out test changes for ECGOST3410-2012 (diff) | |
download | BouncyCastle.NET-ed25519-b30a73590e054908e41231dfd200881dcaea0907.tar.xz |
Update SignerLocation to use DirectoryString
- obsolete old UTF8-based methods.
-rw-r--r-- | crypto/src/asn1/esf/SignerLocation.cs | 106 | ||||
-rw-r--r-- | crypto/src/asn1/x500/DirectoryString.cs | 9 | ||||
-rw-r--r-- | crypto/test/src/asn1/test/SignerLocationUnitTest.cs | 24 |
3 files changed, 82 insertions, 57 deletions
diff --git a/crypto/src/asn1/esf/SignerLocation.cs b/crypto/src/asn1/esf/SignerLocation.cs index d2cef51bb..16dbcd01f 100644 --- a/crypto/src/asn1/esf/SignerLocation.cs +++ b/crypto/src/asn1/esf/SignerLocation.cs @@ -1,7 +1,7 @@ using System; using System.Collections; -using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.X500; namespace Org.BouncyCastle.Asn1.Esf { @@ -20,10 +20,9 @@ namespace Org.BouncyCastle.Asn1.Esf public class SignerLocation : Asn1Encodable { - // TODO Should these be using DirectoryString? - private DerUtf8String countryName; - private DerUtf8String localityName; - private Asn1Sequence postalAddress; + private DirectoryString countryName; + private DirectoryString localityName; + private Asn1Sequence postalAddress; public SignerLocation( Asn1Sequence seq) @@ -33,10 +32,10 @@ namespace Org.BouncyCastle.Asn1.Esf switch (obj.TagNo) { case 0: - this.countryName = DerUtf8String.GetInstance(obj, true); + this.countryName = DirectoryString.GetInstance(obj, true); break; case 1: - this.localityName = DerUtf8String.GetInstance(obj, true); + this.localityName = DirectoryString.GetInstance(obj, true); break; case 2: bool isExplicit = obj.IsExplicit(); // handle erroneous implicitly tagged sequences @@ -50,33 +49,36 @@ namespace Org.BouncyCastle.Asn1.Esf } } - public SignerLocation( - DerUtf8String countryName, - DerUtf8String localityName, - Asn1Sequence postalAddress) - { - if (postalAddress != null && postalAddress.Count > 6) - { - throw new ArgumentException("postal address must contain less than 6 strings"); - } - - if (countryName != null) - { - this.countryName = DerUtf8String.GetInstance(countryName.ToAsn1Object()); - } - - if (localityName != null) - { - this.localityName = DerUtf8String.GetInstance(localityName.ToAsn1Object()); - } - - if (postalAddress != null) - { - this.postalAddress = (Asn1Sequence) postalAddress.ToAsn1Object(); - } - } - - public static SignerLocation GetInstance( + private SignerLocation( + DirectoryString countryName, + DirectoryString localityName, + Asn1Sequence postalAddress) + { + if (postalAddress != null && postalAddress.Count > 6) + throw new ArgumentException("postal address must contain less than 6 strings"); + + this.countryName = countryName; + this.localityName = localityName; + this.postalAddress = postalAddress; + } + + public SignerLocation( + DirectoryString countryName, + DirectoryString localityName, + DirectoryString[] postalAddress) + : this(countryName, localityName, new DerSequence(postalAddress)) + { + } + + public SignerLocation( + DerUtf8String countryName, + DerUtf8String localityName, + Asn1Sequence postalAddress) + : this(DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress) + { + } + + public static SignerLocation GetInstance( object obj) { if (obj == null || obj is SignerLocation) @@ -87,15 +89,41 @@ namespace Org.BouncyCastle.Asn1.Esf return new SignerLocation(Asn1Sequence.GetInstance(obj)); } + public DirectoryString Country + { + get { return countryName; } + } + + public DirectoryString Locality + { + get { return localityName; } + } + + public DirectoryString[] GetPostal() + { + if (postalAddress == null) + return null; + + DirectoryString[] dirStrings = new DirectoryString[postalAddress.Count]; + for (int i = 0; i != dirStrings.Length; i++) + { + dirStrings[i] = DirectoryString.GetInstance(postalAddress[i]); + } + + return dirStrings; + } + + [Obsolete("Use 'Country' property instead")] public DerUtf8String CountryName { - get { return countryName; } + get { return countryName == null ? null : new DerUtf8String(countryName.GetString()); } } - public DerUtf8String LocalityName - { - get { return localityName; } - } + [Obsolete("Use 'Locality' property instead")] + public DerUtf8String LocalityName + { + get { return localityName == null ? null : new DerUtf8String(localityName.GetString()); } + } public Asn1Sequence PostalAddress { diff --git a/crypto/src/asn1/x500/DirectoryString.cs b/crypto/src/asn1/x500/DirectoryString.cs index d907c6456..6585fcdf5 100644 --- a/crypto/src/asn1/x500/DirectoryString.cs +++ b/crypto/src/asn1/x500/DirectoryString.cs @@ -9,15 +9,12 @@ namespace Org.BouncyCastle.Asn1.X500 { private readonly DerStringBase str; - public static DirectoryString GetInstance( - object obj) + public static DirectoryString GetInstance(object obj) { - if (obj is DirectoryString) - { + if (obj == null || obj is DirectoryString) return (DirectoryString) obj; - } - if (obj is DerStringBase) + if (obj is DerStringBase) { if (obj is DerT61String || obj is DerPrintableString diff --git a/crypto/test/src/asn1/test/SignerLocationUnitTest.cs b/crypto/test/src/asn1/test/SignerLocationUnitTest.cs index bf20f1fda..650e2fcfd 100644 --- a/crypto/test/src/asn1/test/SignerLocationUnitTest.cs +++ b/crypto/test/src/asn1/test/SignerLocationUnitTest.cs @@ -2,8 +2,8 @@ using System; using NUnit.Framework; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Esf; +using Org.BouncyCastle.Asn1.X500; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Asn1.Tests @@ -23,17 +23,17 @@ namespace Org.BouncyCastle.Asn1.Tests SignerLocation sl = new SignerLocation(countryName, null, null); - CheckConstruction(sl, countryName, null, null); + CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, null); DerUtf8String localityName = new DerUtf8String("Melbourne"); sl = new SignerLocation(null, localityName, null); - CheckConstruction(sl, null, localityName, null); + CheckConstruction(sl, null, DirectoryString.GetInstance(localityName), null); sl = new SignerLocation(countryName, localityName, null); - CheckConstruction(sl, countryName, localityName, null); + CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), null); Asn1Sequence postalAddress = new DerSequence( new DerUtf8String("line 1"), @@ -45,11 +45,11 @@ namespace Org.BouncyCastle.Asn1.Tests sl = new SignerLocation(countryName, null, postalAddress); - CheckConstruction(sl, countryName, null, postalAddress); + CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, postalAddress); sl = new SignerLocation(countryName, localityName, postalAddress); - CheckConstruction(sl, countryName, localityName, postalAddress); + CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress); sl = SignerLocation.GetInstance(null); @@ -117,9 +117,9 @@ namespace Org.BouncyCastle.Asn1.Tests private void CheckConstruction( SignerLocation sl, - DerUtf8String countryName, - DerUtf8String localityName, - Asn1Sequence postalAddress) + DirectoryString countryName, + DirectoryString localityName, + Asn1Sequence postalAddress) { CheckValues(sl, countryName, localityName, postalAddress); @@ -137,9 +137,9 @@ namespace Org.BouncyCastle.Asn1.Tests private void CheckValues( SignerLocation sl, - DerUtf8String countryName, - DerUtf8String localityName, - Asn1Sequence postalAddress) + DirectoryString countryName, + DirectoryString localityName, + Asn1Sequence postalAddress) { if (countryName != null) { |