Skip to content

Commit d80d39b

Browse files
committed
Refactor NameConstraints
1 parent d33a463 commit d80d39b

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

crypto/src/asn1/x509/NameConstraints.cs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
5-
using Org.BouncyCastle.Utilities;
63

74
namespace Org.BouncyCastle.Asn1.X509
85
{
9-
public class NameConstraints
6+
public class NameConstraints
107
: Asn1Encodable
118
{
12-
private Asn1Sequence permitted, excluded;
9+
private Asn1Sequence m_permitted, m_excluded;
1310

14-
public static NameConstraints GetInstance(
15-
object obj)
11+
public static NameConstraints GetInstance(object obj)
1612
{
17-
if (obj == null || obj is NameConstraints)
18-
{
19-
return (NameConstraints) obj;
20-
}
21-
22-
if (obj is Asn1Sequence)
23-
{
24-
return new NameConstraints((Asn1Sequence) obj);
25-
}
13+
if (obj == null)
14+
return null;
15+
if (obj is NameConstraints nameConstraints)
16+
return nameConstraints;
17+
#pragma warning disable CS0618 // Type or member is obsolete
18+
return new NameConstraints(Asn1Sequence.GetInstance(obj));
19+
#pragma warning restore CS0618 // Type or member is obsolete
20+
}
2621

27-
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
28-
}
22+
public static NameConstraints GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
23+
{
24+
return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
25+
}
2926

30-
public NameConstraints(
31-
Asn1Sequence seq)
27+
[Obsolete("Use 'GetInstance' instead")]
28+
public NameConstraints(Asn1Sequence seq)
3229
{
3330
foreach (Asn1TaggedObject o in seq)
3431
{
3532
switch (o.TagNo)
3633
{
37-
case 0:
38-
permitted = Asn1Sequence.GetInstance(o, false);
39-
break;
40-
case 1:
41-
excluded = Asn1Sequence.GetInstance(o, false);
42-
break;
34+
case 0:
35+
m_permitted = Asn1Sequence.GetInstance(o, false);
36+
break;
37+
case 1:
38+
m_excluded = Asn1Sequence.GetInstance(o, false);
39+
break;
4340
}
4441
}
4542
}
@@ -52,35 +49,32 @@ public NameConstraints(
5249
* @param permitted Permitted subtrees
5350
* @param excluded Excluded subtrees
5451
*/
55-
public NameConstraints(
56-
IList<GeneralSubtree> permitted,
57-
IList<GeneralSubtree> excluded)
52+
public NameConstraints(IList<GeneralSubtree> permitted, IList<GeneralSubtree> excluded)
5853
{
5954
if (permitted != null)
6055
{
61-
this.permitted = CreateSequence(permitted);
56+
this.m_permitted = CreateSequence(permitted);
6257
}
6358

6459
if (excluded != null)
6560
{
66-
this.excluded = CreateSequence(excluded);
61+
this.m_excluded = CreateSequence(excluded);
6762
}
6863
}
6964

7065
private DerSequence CreateSequence(IList<GeneralSubtree> subtrees)
7166
{
72-
return new DerSequence(subtrees.ToArray());
67+
Asn1EncodableVector v = new Asn1EncodableVector(subtrees.Count);
68+
foreach (var subtree in subtrees)
69+
{
70+
v.Add(subtree);
71+
}
72+
return new DerSequence(v);
7373
}
7474

75-
public Asn1Sequence PermittedSubtrees
76-
{
77-
get { return permitted; }
78-
}
75+
public Asn1Sequence PermittedSubtrees => m_permitted;
7976

80-
public Asn1Sequence ExcludedSubtrees
81-
{
82-
get { return excluded; }
83-
}
77+
public Asn1Sequence ExcludedSubtrees => m_excluded;
8478

8579
/*
8680
* NameConstraints ::= SEQUENCE { permittedSubtrees [0] GeneralSubtrees
@@ -89,8 +83,8 @@ public Asn1Sequence ExcludedSubtrees
8983
public override Asn1Object ToAsn1Object()
9084
{
9185
Asn1EncodableVector v = new Asn1EncodableVector(2);
92-
v.AddOptionalTagged(false, 0, permitted);
93-
v.AddOptionalTagged(false, 1, excluded);
86+
v.AddOptionalTagged(false, 0, m_permitted);
87+
v.AddOptionalTagged(false, 1, m_excluded);
9488
return new DerSequence(v);
9589
}
9690
}

crypto/src/pkix/Rfc3280CertPathUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ internal static void PrepareNextCertG(
16191619
PkixCertPathValidatorUtilities.GetExtensionValue(cert, X509Extensions.NameConstraints));
16201620
if (ncSeq != null)
16211621
{
1622-
nc = new NameConstraints(ncSeq);
1622+
nc = NameConstraints.GetInstance(ncSeq);
16231623
}
16241624
}
16251625
catch (Exception e)

0 commit comments

Comments
 (0)