Skip to content

Commit e2544b0

Browse files
committed
Refactoring in Asn1.X509
1 parent 48f280f commit e2544b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1548
-2265
lines changed
Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,65 @@
11
using System;
22

3-
using Org.BouncyCastle.Utilities;
4-
53
namespace Org.BouncyCastle.Asn1.X509
64
{
7-
/**
5+
/**
86
* The AccessDescription object.
97
* <pre>
108
* AccessDescription ::= SEQUENCE {
119
* accessMethod OBJECT IDENTIFIER,
1210
* accessLocation GeneralName }
1311
* </pre>
1412
*/
15-
public class AccessDescription
13+
public class AccessDescription
1614
: Asn1Encodable
1715
{
1816
public readonly static DerObjectIdentifier IdADCAIssuers = new DerObjectIdentifier("1.3.6.1.5.5.7.48.2");
1917
public readonly static DerObjectIdentifier IdADOcsp = new DerObjectIdentifier("1.3.6.1.5.5.7.48.1");
2018

21-
private readonly DerObjectIdentifier accessMethod;
22-
private readonly GeneralName accessLocation;
19+
public static AccessDescription GetInstance(object obj)
20+
{
21+
if (obj == null)
22+
return null;
23+
if (obj is AccessDescription accessDescription)
24+
return accessDescription;
25+
return new AccessDescription(Asn1Sequence.GetInstance(obj));
26+
}
2327

24-
public static AccessDescription GetInstance(
25-
object obj)
26-
{
27-
if (obj is AccessDescription)
28-
return (AccessDescription) obj;
28+
public static AccessDescription GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
29+
new AccessDescription(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
2930

30-
if (obj is Asn1Sequence)
31-
return new AccessDescription((Asn1Sequence) obj);
31+
public static AccessDescription GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
32+
new AccessDescription(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
3233

33-
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
34-
}
34+
private readonly DerObjectIdentifier m_accessMethod;
35+
private readonly GeneralName m_accessLocation;
3536

36-
private AccessDescription(
37-
Asn1Sequence seq)
37+
private AccessDescription(Asn1Sequence seq)
3838
{
39-
if (seq.Count != 2)
40-
throw new ArgumentException("wrong number of elements in sequence");
39+
int count = seq.Count;
40+
if (count != 2)
41+
throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
4142

42-
accessMethod = DerObjectIdentifier.GetInstance(seq[0]);
43-
accessLocation = GeneralName.GetInstance(seq[1]);
43+
m_accessMethod = DerObjectIdentifier.GetInstance(seq[0]);
44+
m_accessLocation = GeneralName.GetInstance(seq[1]);
4445
}
4546

46-
/**
47+
/**
4748
* create an AccessDescription with the oid and location provided.
4849
*/
49-
public AccessDescription(
50-
DerObjectIdentifier oid,
51-
GeneralName location)
52-
{
53-
accessMethod = oid;
54-
accessLocation = location;
55-
}
50+
// TODO[api] Change parameter names
51+
public AccessDescription(DerObjectIdentifier oid, GeneralName location)
52+
{
53+
m_accessMethod = oid ?? throw new ArgumentNullException(nameof(oid));
54+
m_accessLocation = location ?? throw new ArgumentNullException(nameof(location));
55+
}
5656

57-
/**
58-
*
59-
* @return the access method.
60-
*/
61-
public DerObjectIdentifier AccessMethod
62-
{
63-
get { return accessMethod; }
64-
}
57+
public DerObjectIdentifier AccessMethod => m_accessMethod;
6558

66-
/**
67-
*
68-
* @return the access location
69-
*/
70-
public GeneralName AccessLocation
71-
{
72-
get { return accessLocation; }
73-
}
59+
public GeneralName AccessLocation => m_accessLocation;
7460

75-
public override Asn1Object ToAsn1Object()
76-
{
77-
return new DerSequence(accessMethod, accessLocation);
78-
}
61+
public override Asn1Object ToAsn1Object() => new DerSequence(m_accessMethod, m_accessLocation);
7962

80-
public override string ToString()
81-
{
82-
return "AccessDescription: Oid(" + this.accessMethod.Id + ")";
83-
}
63+
public override string ToString() => "AccessDescription: Oid(" + m_accessMethod.Id + ")";
8464
}
8565
}

crypto/src/asn1/x509/AlgorithmIdentifier.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ namespace Org.BouncyCastle.Asn1.X509
55
public class AlgorithmIdentifier
66
: Asn1Encodable
77
{
8-
private readonly DerObjectIdentifier algorithm;
9-
private readonly Asn1Encodable parameters;
10-
118
public static AlgorithmIdentifier GetInstance(object obj)
129
{
1310
if (obj == null)
@@ -38,45 +35,39 @@ public static AlgorithmIdentifier GetOptional(Asn1Encodable element)
3835
public static AlgorithmIdentifier GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
3936
new AlgorithmIdentifier(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
4037

41-
public AlgorithmIdentifier(
42-
DerObjectIdentifier algorithm)
38+
private readonly DerObjectIdentifier m_algorithm;
39+
private readonly Asn1Encodable m_parameters;
40+
41+
internal AlgorithmIdentifier(Asn1Sequence seq)
4342
{
44-
this.algorithm = algorithm;
43+
int count = seq.Count;
44+
if (count < 1 || count > 2)
45+
throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
46+
47+
m_algorithm = DerObjectIdentifier.GetInstance(seq[0]);
48+
m_parameters = count < 2 ? null : seq[1];
4549
}
4650

47-
public AlgorithmIdentifier(
48-
DerObjectIdentifier algorithm,
49-
Asn1Encodable parameters)
51+
public AlgorithmIdentifier(DerObjectIdentifier algorithm)
52+
: this(algorithm, null)
5053
{
51-
this.algorithm = algorithm;
52-
this.parameters = parameters;
5354
}
5455

55-
internal AlgorithmIdentifier(
56-
Asn1Sequence seq)
56+
public AlgorithmIdentifier(DerObjectIdentifier algorithm, Asn1Encodable parameters)
5757
{
58-
if (seq.Count < 1 || seq.Count > 2)
59-
throw new ArgumentException("Bad sequence size: " + seq.Count);
60-
61-
this.algorithm = DerObjectIdentifier.GetInstance(seq[0]);
62-
this.parameters = seq.Count < 2 ? null : seq[1];
58+
m_algorithm = algorithm ?? throw new ArgumentNullException(nameof(algorithm));
59+
m_parameters = parameters;
6360
}
6461

6562
/// <summary>
6663
/// Return the OID in the Algorithm entry of this identifier.
6764
/// </summary>
68-
public virtual DerObjectIdentifier Algorithm
69-
{
70-
get { return algorithm; }
71-
}
65+
public virtual DerObjectIdentifier Algorithm => m_algorithm;
7266

7367
/// <summary>
7468
/// Return the parameters structure in the Parameters entry of this identifier.
7569
/// </summary>
76-
public virtual Asn1Encodable Parameters
77-
{
78-
get { return parameters; }
79-
}
70+
public virtual Asn1Encodable Parameters => m_parameters;
8071

8172
/**
8273
* Produce an object suitable for an Asn1OutputStream.
@@ -88,9 +79,9 @@ public virtual Asn1Encodable Parameters
8879
*/
8980
public override Asn1Object ToAsn1Object()
9081
{
91-
Asn1EncodableVector v = new Asn1EncodableVector(algorithm);
92-
v.AddOptional(parameters);
93-
return new DerSequence(v);
82+
return m_parameters == null
83+
? new DerSequence(m_algorithm)
84+
: new DerSequence(m_algorithm, m_parameters);
9485
}
9586
}
9687
}

crypto/src/asn1/x509/AltSignatureAlgorithm.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ namespace Org.BouncyCastle.Asn1.X509
2828
public class AltSignatureAlgorithm
2929
: Asn1Encodable
3030
{
31-
private readonly AlgorithmIdentifier m_algorithm;
32-
3331
public static AltSignatureAlgorithm GetInstance(object obj)
3432
{
3533
if (obj == null)
@@ -39,20 +37,23 @@ public static AltSignatureAlgorithm GetInstance(object obj)
3937
return new AltSignatureAlgorithm(AlgorithmIdentifier.GetInstance(obj));
4038
}
4139

42-
public static AltSignatureAlgorithm GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
43-
{
44-
return GetInstance(AlgorithmIdentifier.GetInstance(taggedObject, declaredExplicit));
45-
}
40+
public static AltSignatureAlgorithm GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
41+
new AltSignatureAlgorithm(AlgorithmIdentifier.GetInstance(taggedObject, declaredExplicit));
42+
43+
public static AltSignatureAlgorithm GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
44+
new AltSignatureAlgorithm(AlgorithmIdentifier.GetTagged(taggedObject, declaredExplicit));
4645

4746
public static AltSignatureAlgorithm FromExtensions(X509Extensions extensions)
4847
{
4948
return GetInstance(
5049
X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.AltSignatureAlgorithm));
5150
}
5251

52+
private readonly AlgorithmIdentifier m_algorithm;
53+
5354
public AltSignatureAlgorithm(AlgorithmIdentifier algorithm)
5455
{
55-
m_algorithm = algorithm;
56+
m_algorithm = algorithm ?? throw new ArgumentNullException(nameof(algorithm));
5657
}
5758

5859
public AltSignatureAlgorithm(DerObjectIdentifier algorithm)
@@ -67,9 +68,6 @@ public AltSignatureAlgorithm(DerObjectIdentifier algorithm, Asn1Encodable parame
6768

6869
public AlgorithmIdentifier Algorithm => m_algorithm;
6970

70-
public override Asn1Object ToAsn1Object()
71-
{
72-
return m_algorithm.ToAsn1Object();
73-
}
71+
public override Asn1Object ToAsn1Object() => m_algorithm.ToAsn1Object();
7472
}
7573
}

crypto/src/asn1/x509/AltSignatureValue.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ namespace Org.BouncyCastle.Asn1.X509
2929
public class AltSignatureValue
3030
: Asn1Encodable
3131
{
32-
private readonly DerBitString m_signature;
33-
3432
public static AltSignatureValue GetInstance(object obj)
3533
{
3634
if (obj == null)
@@ -40,19 +38,22 @@ public static AltSignatureValue GetInstance(object obj)
4038
return new AltSignatureValue(DerBitString.GetInstance(obj));
4139
}
4240

43-
public static AltSignatureValue GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
44-
{
45-
return GetInstance(DerBitString.GetInstance(taggedObject, declaredExplicit));
46-
}
41+
public static AltSignatureValue GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
42+
new AltSignatureValue(DerBitString.GetInstance(taggedObject, declaredExplicit));
43+
44+
public static AltSignatureValue GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
45+
new AltSignatureValue(DerBitString.GetTagged(taggedObject, declaredExplicit));
4746

4847
public static AltSignatureValue FromExtensions(X509Extensions extensions)
4948
{
5049
return GetInstance(X509Extensions.GetExtensionParsedValue(extensions, X509Extensions.AltSignatureValue));
5150
}
5251

53-
private AltSignatureValue(DerBitString signature)
52+
private readonly DerBitString m_signature;
53+
54+
public AltSignatureValue(DerBitString signature)
5455
{
55-
m_signature = signature;
56+
m_signature = signature ?? throw new ArgumentNullException(nameof(signature));
5657
}
5758

5859
public AltSignatureValue(byte[] signature)
Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,47 @@
11
using System;
22

3-
using Org.BouncyCastle.Utilities;
4-
53
namespace Org.BouncyCastle.Asn1.X509
64
{
75
public class AttCertValidityPeriod
86
: Asn1Encodable
97
{
10-
private readonly Asn1GeneralizedTime notBeforeTime;
11-
private readonly Asn1GeneralizedTime notAfterTime;
12-
13-
public static AttCertValidityPeriod GetInstance(
14-
object obj)
8+
public static AttCertValidityPeriod GetInstance(object obj)
159
{
16-
if (obj is AttCertValidityPeriod || obj == null)
17-
{
18-
return (AttCertValidityPeriod) obj;
19-
}
10+
if (obj == null)
11+
return null;
12+
if (obj is AttCertValidityPeriod attCertValidityPeriod)
13+
return attCertValidityPeriod;
14+
return new AttCertValidityPeriod(Asn1Sequence.GetInstance(obj));
15+
}
2016

21-
if (obj is Asn1Sequence)
22-
{
23-
return new AttCertValidityPeriod((Asn1Sequence) obj);
24-
}
17+
public static AttCertValidityPeriod GetInstance(Asn1TaggedObject obj, bool explicitly) =>
18+
new AttCertValidityPeriod(Asn1Sequence.GetInstance(obj, explicitly));
2519

26-
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
27-
}
20+
public static AttCertValidityPeriod GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
21+
new AttCertValidityPeriod(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
2822

29-
public static AttCertValidityPeriod GetInstance(
30-
Asn1TaggedObject obj,
31-
bool explicitly)
32-
{
33-
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
34-
}
23+
private readonly Asn1GeneralizedTime m_notBeforeTime;
24+
private readonly Asn1GeneralizedTime m_notAfterTime;
3525

36-
private AttCertValidityPeriod(
37-
Asn1Sequence seq)
26+
private AttCertValidityPeriod(Asn1Sequence seq)
3827
{
39-
if (seq.Count != 2)
40-
throw new ArgumentException("Bad sequence size: " + seq.Count);
28+
int count = seq.Count;
29+
if (count != 2)
30+
throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
4131

42-
notBeforeTime = Asn1GeneralizedTime.GetInstance(seq[0]);
43-
notAfterTime = Asn1GeneralizedTime.GetInstance(seq[1]);
32+
m_notBeforeTime = Asn1GeneralizedTime.GetInstance(seq[0]);
33+
m_notAfterTime = Asn1GeneralizedTime.GetInstance(seq[1]);
4434
}
4535

46-
public AttCertValidityPeriod(
47-
Asn1GeneralizedTime notBeforeTime,
48-
Asn1GeneralizedTime notAfterTime)
36+
public AttCertValidityPeriod(Asn1GeneralizedTime notBeforeTime, Asn1GeneralizedTime notAfterTime)
4937
{
50-
this.notBeforeTime = notBeforeTime;
51-
this.notAfterTime = notAfterTime;
38+
m_notBeforeTime = notBeforeTime ?? throw new ArgumentNullException(nameof(notBeforeTime));
39+
m_notAfterTime = notAfterTime ?? throw new ArgumentNullException(nameof(notAfterTime));
5240
}
5341

54-
public Asn1GeneralizedTime NotBeforeTime
55-
{
56-
get { return notBeforeTime; }
57-
}
42+
public Asn1GeneralizedTime NotBeforeTime => m_notBeforeTime;
5843

59-
public Asn1GeneralizedTime NotAfterTime
60-
{
61-
get { return notAfterTime; }
62-
}
44+
public Asn1GeneralizedTime NotAfterTime => m_notAfterTime;
6345

6446
/**
6547
* Produce an object suitable for an Asn1OutputStream.
@@ -70,9 +52,6 @@ public Asn1GeneralizedTime NotAfterTime
7052
* }
7153
* </pre>
7254
*/
73-
public override Asn1Object ToAsn1Object()
74-
{
75-
return new DerSequence(notBeforeTime, notAfterTime);
76-
}
55+
public override Asn1Object ToAsn1Object() => new DerSequence(m_notBeforeTime, m_notAfterTime);
7756
}
7857
}

0 commit comments

Comments
 (0)