Skip to content

Commit b30a735

Browse files
committed
Update SignerLocation to use DirectoryString
- obsolete old UTF8-based methods.
1 parent b2f9397 commit b30a735

File tree

3 files changed

+82
-57
lines changed

3 files changed

+82
-57
lines changed

crypto/src/asn1/esf/SignerLocation.cs

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections;
33

4-
using Org.BouncyCastle.Asn1;
4+
using Org.BouncyCastle.Asn1.X500;
55

66
namespace Org.BouncyCastle.Asn1.Esf
77
{
@@ -20,10 +20,9 @@ namespace Org.BouncyCastle.Asn1.Esf
2020
public class SignerLocation
2121
: Asn1Encodable
2222
{
23-
// TODO Should these be using DirectoryString?
24-
private DerUtf8String countryName;
25-
private DerUtf8String localityName;
26-
private Asn1Sequence postalAddress;
23+
private DirectoryString countryName;
24+
private DirectoryString localityName;
25+
private Asn1Sequence postalAddress;
2726

2827
public SignerLocation(
2928
Asn1Sequence seq)
@@ -33,10 +32,10 @@ public SignerLocation(
3332
switch (obj.TagNo)
3433
{
3534
case 0:
36-
this.countryName = DerUtf8String.GetInstance(obj, true);
35+
this.countryName = DirectoryString.GetInstance(obj, true);
3736
break;
3837
case 1:
39-
this.localityName = DerUtf8String.GetInstance(obj, true);
38+
this.localityName = DirectoryString.GetInstance(obj, true);
4039
break;
4140
case 2:
4241
bool isExplicit = obj.IsExplicit(); // handle erroneous implicitly tagged sequences
@@ -50,33 +49,36 @@ public SignerLocation(
5049
}
5150
}
5251

53-
public SignerLocation(
54-
DerUtf8String countryName,
55-
DerUtf8String localityName,
56-
Asn1Sequence postalAddress)
57-
{
58-
if (postalAddress != null && postalAddress.Count > 6)
59-
{
60-
throw new ArgumentException("postal address must contain less than 6 strings");
61-
}
62-
63-
if (countryName != null)
64-
{
65-
this.countryName = DerUtf8String.GetInstance(countryName.ToAsn1Object());
66-
}
67-
68-
if (localityName != null)
69-
{
70-
this.localityName = DerUtf8String.GetInstance(localityName.ToAsn1Object());
71-
}
72-
73-
if (postalAddress != null)
74-
{
75-
this.postalAddress = (Asn1Sequence) postalAddress.ToAsn1Object();
76-
}
77-
}
78-
79-
public static SignerLocation GetInstance(
52+
private SignerLocation(
53+
DirectoryString countryName,
54+
DirectoryString localityName,
55+
Asn1Sequence postalAddress)
56+
{
57+
if (postalAddress != null && postalAddress.Count > 6)
58+
throw new ArgumentException("postal address must contain less than 6 strings");
59+
60+
this.countryName = countryName;
61+
this.localityName = localityName;
62+
this.postalAddress = postalAddress;
63+
}
64+
65+
public SignerLocation(
66+
DirectoryString countryName,
67+
DirectoryString localityName,
68+
DirectoryString[] postalAddress)
69+
: this(countryName, localityName, new DerSequence(postalAddress))
70+
{
71+
}
72+
73+
public SignerLocation(
74+
DerUtf8String countryName,
75+
DerUtf8String localityName,
76+
Asn1Sequence postalAddress)
77+
: this(DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress)
78+
{
79+
}
80+
81+
public static SignerLocation GetInstance(
8082
object obj)
8183
{
8284
if (obj == null || obj is SignerLocation)
@@ -87,15 +89,41 @@ public static SignerLocation GetInstance(
8789
return new SignerLocation(Asn1Sequence.GetInstance(obj));
8890
}
8991

92+
public DirectoryString Country
93+
{
94+
get { return countryName; }
95+
}
96+
97+
public DirectoryString Locality
98+
{
99+
get { return localityName; }
100+
}
101+
102+
public DirectoryString[] GetPostal()
103+
{
104+
if (postalAddress == null)
105+
return null;
106+
107+
DirectoryString[] dirStrings = new DirectoryString[postalAddress.Count];
108+
for (int i = 0; i != dirStrings.Length; i++)
109+
{
110+
dirStrings[i] = DirectoryString.GetInstance(postalAddress[i]);
111+
}
112+
113+
return dirStrings;
114+
}
115+
116+
[Obsolete("Use 'Country' property instead")]
90117
public DerUtf8String CountryName
91118
{
92-
get { return countryName; }
119+
get { return countryName == null ? null : new DerUtf8String(countryName.GetString()); }
93120
}
94121

95-
public DerUtf8String LocalityName
96-
{
97-
get { return localityName; }
98-
}
122+
[Obsolete("Use 'Locality' property instead")]
123+
public DerUtf8String LocalityName
124+
{
125+
get { return localityName == null ? null : new DerUtf8String(localityName.GetString()); }
126+
}
99127

100128
public Asn1Sequence PostalAddress
101129
{

crypto/src/asn1/x500/DirectoryString.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ public class DirectoryString
99
{
1010
private readonly DerStringBase str;
1111

12-
public static DirectoryString GetInstance(
13-
object obj)
12+
public static DirectoryString GetInstance(object obj)
1413
{
15-
if (obj is DirectoryString)
16-
{
14+
if (obj == null || obj is DirectoryString)
1715
return (DirectoryString) obj;
18-
}
1916

20-
if (obj is DerStringBase)
17+
if (obj is DerStringBase)
2118
{
2219
if (obj is DerT61String
2320
|| obj is DerPrintableString

crypto/test/src/asn1/test/SignerLocationUnitTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
using NUnit.Framework;
44

5-
using Org.BouncyCastle.Asn1;
65
using Org.BouncyCastle.Asn1.Esf;
6+
using Org.BouncyCastle.Asn1.X500;
77
using Org.BouncyCastle.Utilities.Test;
88

99
namespace Org.BouncyCastle.Asn1.Tests
@@ -23,17 +23,17 @@ public override void PerformTest()
2323

2424
SignerLocation sl = new SignerLocation(countryName, null, null);
2525

26-
CheckConstruction(sl, countryName, null, null);
26+
CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, null);
2727

2828
DerUtf8String localityName = new DerUtf8String("Melbourne");
2929

3030
sl = new SignerLocation(null, localityName, null);
3131

32-
CheckConstruction(sl, null, localityName, null);
32+
CheckConstruction(sl, null, DirectoryString.GetInstance(localityName), null);
3333

3434
sl = new SignerLocation(countryName, localityName, null);
3535

36-
CheckConstruction(sl, countryName, localityName, null);
36+
CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), null);
3737

3838
Asn1Sequence postalAddress = new DerSequence(
3939
new DerUtf8String("line 1"),
@@ -45,11 +45,11 @@ public override void PerformTest()
4545

4646
sl = new SignerLocation(countryName, null, postalAddress);
4747

48-
CheckConstruction(sl, countryName, null, postalAddress);
48+
CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, postalAddress);
4949

5050
sl = new SignerLocation(countryName, localityName, postalAddress);
5151

52-
CheckConstruction(sl, countryName, localityName, postalAddress);
52+
CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress);
5353

5454
sl = SignerLocation.GetInstance(null);
5555

@@ -117,9 +117,9 @@ public override void PerformTest()
117117

118118
private void CheckConstruction(
119119
SignerLocation sl,
120-
DerUtf8String countryName,
121-
DerUtf8String localityName,
122-
Asn1Sequence postalAddress)
120+
DirectoryString countryName,
121+
DirectoryString localityName,
122+
Asn1Sequence postalAddress)
123123
{
124124
CheckValues(sl, countryName, localityName, postalAddress);
125125

@@ -137,9 +137,9 @@ private void CheckConstruction(
137137

138138
private void CheckValues(
139139
SignerLocation sl,
140-
DerUtf8String countryName,
141-
DerUtf8String localityName,
142-
Asn1Sequence postalAddress)
140+
DirectoryString countryName,
141+
DirectoryString localityName,
142+
Asn1Sequence postalAddress)
143143
{
144144
if (countryName != null)
145145
{

0 commit comments

Comments
 (0)