Skip to content

Commit ea67c23

Browse files
committed
Refactor X509SignatureUtilities
1 parent 27c1d77 commit ea67c23

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

crypto/src/x509/X509SignatureUtil.cs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Org.BouncyCastle.Asn1.X509;
88
using Org.BouncyCastle.Asn1.X9;
99
using Org.BouncyCastle.Security;
10+
using Org.BouncyCastle.Utilities;
1011

1112
namespace Org.BouncyCastle.X509
1213
{
@@ -17,43 +18,15 @@ internal static bool AreEquivalentAlgorithms(AlgorithmIdentifier id1, AlgorithmI
1718
if (!id1.Algorithm.Equals(id2.Algorithm))
1819
return false;
1920

20-
Asn1Encodable p1 = id1.Parameters;
21-
Asn1Encodable p2 = id2.Parameters;
21+
// TODO Java has a property to control whether absent parameters can match NULL parameters
22+
{
23+
if (IsAbsentOrEmptyParameters(id1.Parameters) && IsAbsentOrEmptyParameters(id2.Parameters))
24+
return true;
25+
}
2226

23-
if (p1 == p2)
24-
return true;
25-
if (p1 == null)
26-
return p2.ToAsn1Object() is Asn1Null;
27-
if (p2 == null)
28-
return p1.ToAsn1Object() is Asn1Null;
29-
30-
return p1.Equals(p2);
27+
return Objects.Equals(id1.Parameters, id2.Parameters);
3128
}
3229

33-
internal static string GetSignatureName(AlgorithmIdentifier sigAlgID)
34-
{
35-
DerObjectIdentifier sigAlgOid = sigAlgID.Algorithm;
36-
Asn1Encodable parameters = sigAlgID.Parameters;
37-
38-
if (parameters != null && !DerNull.Instance.Equals(parameters))
39-
{
40-
if (PkcsObjectIdentifiers.IdRsassaPss.Equals(sigAlgOid))
41-
{
42-
RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
43-
44-
return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
45-
}
46-
if (X9ObjectIdentifiers.ECDsaWithSha2.Equals(sigAlgOid))
47-
{
48-
Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
49-
50-
return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
51-
}
52-
}
53-
54-
return SignerUtilities.GetEncodingName(sigAlgOid) ?? sigAlgOid.GetID();
55-
}
56-
5730
/**
5831
* Return the digest algorithm using one of the standard JCA string
5932
* representations rather than the algorithm identifier (if possible).
@@ -103,7 +76,36 @@ private static string GetDigestAlgName(DerObjectIdentifier digestAlgOID)
10376
else
10477
{
10578
return digestAlgOID.GetID();
79+
}
80+
}
81+
82+
internal static string GetSignatureName(AlgorithmIdentifier sigAlgID)
83+
{
84+
DerObjectIdentifier sigAlgOid = sigAlgID.Algorithm;
85+
Asn1Encodable parameters = sigAlgID.Parameters;
86+
87+
if (!IsAbsentOrEmptyParameters(parameters))
88+
{
89+
if (PkcsObjectIdentifiers.IdRsassaPss.Equals(sigAlgOid))
90+
{
91+
RsassaPssParameters rsaParams = RsassaPssParameters.GetInstance(parameters);
92+
93+
return GetDigestAlgName(rsaParams.HashAlgorithm.Algorithm) + "withRSAandMGF1";
94+
}
95+
if (X9ObjectIdentifiers.ECDsaWithSha2.Equals(sigAlgOid))
96+
{
97+
Asn1Sequence ecDsaParams = Asn1Sequence.GetInstance(parameters);
98+
99+
return GetDigestAlgName((DerObjectIdentifier)ecDsaParams[0]) + "withECDSA";
100+
}
106101
}
102+
103+
return SignerUtilities.GetEncodingName(sigAlgOid) ?? sigAlgOid.GetID();
107104
}
105+
106+
private static bool IsAbsentOrEmptyParameters(Asn1Encodable parameters)
107+
{
108+
return parameters == null || DerNull.Instance.Equals(parameters);
109+
}
108110
}
109111
}

0 commit comments

Comments
 (0)