Skip to content

Commit 27c1d77

Browse files
committed
Overhaul algorithm registries
1 parent 8e43e04 commit 27c1d77

File tree

11 files changed

+884
-513
lines changed

11 files changed

+884
-513
lines changed

crypto/src/asn1/smime/SMIMECapabilities.cs

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

4+
using Org.BouncyCastle.Asn1.Misc;
45
using Org.BouncyCastle.Asn1.Nist;
6+
using Org.BouncyCastle.Asn1.Oiw;
57
using Org.BouncyCastle.Asn1.Pkcs;
68
using Org.BouncyCastle.Asn1.X509;
79

@@ -28,9 +30,9 @@ public class SmimeCapabilities
2830
public static readonly DerObjectIdentifier Aes256Cbc = NistObjectIdentifiers.IdAes256Cbc;
2931
public static readonly DerObjectIdentifier Aes192Cbc = NistObjectIdentifiers.IdAes192Cbc;
3032
public static readonly DerObjectIdentifier Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc;
31-
public static readonly DerObjectIdentifier IdeaCbc = new DerObjectIdentifier("1.3.6.1.4.1.188.7.1.1.2");
32-
public static readonly DerObjectIdentifier Cast5Cbc = new DerObjectIdentifier("1.2.840.113533.7.66.10");
33-
public static readonly DerObjectIdentifier DesCbc = new DerObjectIdentifier("1.3.14.3.2.7");
33+
public static readonly DerObjectIdentifier IdeaCbc = MiscObjectIdentifiers.as_sys_sec_alg_ideaCBC;
34+
public static readonly DerObjectIdentifier Cast5Cbc = MiscObjectIdentifiers.cast5CBC;
35+
public static readonly DerObjectIdentifier DesCbc = OiwObjectIdentifiers.DesCbc;
3436
public static readonly DerObjectIdentifier DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc;
3537
public static readonly DerObjectIdentifier RC2Cbc = PkcsObjectIdentifiers.RC2Cbc;
3638

crypto/src/asn1/smime/SMIMECapability.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22

33
using Org.BouncyCastle.Asn1;
4+
using Org.BouncyCastle.Asn1.Oiw;
45
using Org.BouncyCastle.Asn1.Pkcs;
56

67
namespace Org.BouncyCastle.Asn1.Smime
@@ -18,7 +19,7 @@ public class SmimeCapability
1819
/**
1920
* encryption algorithms preferences
2021
*/
21-
public static readonly DerObjectIdentifier DesCbc = new DerObjectIdentifier("1.3.14.3.2.7");
22+
public static readonly DerObjectIdentifier DesCbc = OiwObjectIdentifiers.DesCbc;
2223
public static readonly DerObjectIdentifier DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc;
2324
public static readonly DerObjectIdentifier RC2Cbc = PkcsObjectIdentifiers.RC2Cbc;
2425

crypto/src/crypto/util/AlgorithmIdentifierFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Org.BouncyCastle.Crypto.Utilities
1414
{
1515
public class AlgorithmIdentifierFactory
1616
{
17-
public static readonly DerObjectIdentifier IDEA_CBC = new DerObjectIdentifier("1.3.6.1.4.1.188.7.1.1.2");
18-
public static readonly DerObjectIdentifier CAST5_CBC = new DerObjectIdentifier("1.2.840.113533.7.66.10");
17+
public static readonly DerObjectIdentifier IDEA_CBC = MiscObjectIdentifiers.as_sys_sec_alg_ideaCBC;
18+
public static readonly DerObjectIdentifier CAST5_CBC = MiscObjectIdentifiers.cast5CBC;
1919

2020
private static readonly short[] rc2Table = {
2121
0xbd, 0x56, 0xea, 0xf2, 0xa2, 0xf1, 0xac, 0x2a, 0xb0, 0x93, 0xd1, 0x9c, 0x1b, 0x33, 0xfd, 0xd0,

crypto/src/security/AgreementUtilities.cs

Lines changed: 147 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,107 +12,208 @@
1212

1313
namespace Org.BouncyCastle.Security
1414
{
15-
/// <remarks>
16-
/// Utility class for creating IBasicAgreement objects from their names/Oids
17-
/// </remarks>
18-
public static class AgreementUtilities
15+
/// <remarks>
16+
/// Utility class for creating IBasicAgreement objects from their names/Oids
17+
/// </remarks>
18+
public static class AgreementUtilities
1919
{
20-
private static readonly IDictionary<string, string> Algorithms =
21-
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
20+
private static readonly Dictionary<DerObjectIdentifier, string> AlgorithmOidMap =
21+
new Dictionary<DerObjectIdentifier, string>();
2222

2323
static AgreementUtilities()
2424
{
25-
Algorithms[X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme.Id] = "ECCDHWITHSHA1KDF";
26-
Algorithms[X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme.Id] = "ECDHWITHSHA1KDF";
27-
Algorithms[X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme.Id] = "ECMQVWITHSHA1KDF";
25+
AlgorithmOidMap[X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme] = "ECCDHWITHSHA1KDF";
26+
AlgorithmOidMap[X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme] = "ECDHWITHSHA1KDF";
27+
AlgorithmOidMap[X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme] = "ECMQVWITHSHA1KDF";
28+
29+
AlgorithmOidMap[EdECObjectIdentifiers.id_X25519] = "X25519";
30+
AlgorithmOidMap[EdECObjectIdentifiers.id_X448] = "X448";
31+
32+
#if DEBUG
33+
//foreach (var key in AlgorithmMap.Keys)
34+
//{
35+
// if (DerObjectIdentifier.TryFromID(key, out var ignore))
36+
// throw new Exception("OID mapping belongs in AlgorithmOidMap: " + key);
37+
//}
38+
39+
//var mechanisms = new HashSet<string>(AlgorithmMap.Values);
40+
var mechanisms = new HashSet<string>();
41+
mechanisms.UnionWith(AlgorithmOidMap.Values);
42+
43+
foreach (var mechanism in mechanisms)
44+
{
45+
//if (AlgorithmMap.TryGetValue(mechanism, out var check))
46+
//{
47+
// if (mechanism != check)
48+
// throw new Exception("Mechanism mapping MUST be to self: " + mechanism);
49+
//}
50+
//else
51+
{
52+
if (!mechanism.Equals(mechanism.ToUpperInvariant()))
53+
throw new Exception("Unmapped mechanism MUST be uppercase: " + mechanism);
54+
}
55+
}
56+
#endif
57+
}
2858

29-
Algorithms[EdECObjectIdentifiers.id_X25519.Id] = "X25519";
30-
Algorithms[EdECObjectIdentifiers.id_X448.Id] = "X448";
59+
public static string GetAlgorithmName(DerObjectIdentifier oid)
60+
{
61+
return CollectionUtilities.GetValueOrNull(AlgorithmOidMap, oid);
3162
}
3263

33-
public static IBasicAgreement GetBasicAgreement(
34-
DerObjectIdentifier oid)
64+
public static IBasicAgreement GetBasicAgreement(DerObjectIdentifier oid)
3565
{
36-
return GetBasicAgreement(oid.Id);
37-
}
66+
if (oid == null)
67+
throw new ArgumentNullException(nameof(oid));
3868

39-
public static IBasicAgreement GetBasicAgreement(
40-
string algorithm)
69+
if (AlgorithmOidMap.TryGetValue(oid, out var mechanism))
70+
{
71+
var basicAgreement = GetBasicAgreementForMechanism(mechanism);
72+
if (basicAgreement != null)
73+
return basicAgreement;
74+
}
75+
76+
throw new SecurityUtilityException("Basic Agreement OID not recognised.");
77+
}
78+
79+
public static IBasicAgreement GetBasicAgreement(string algorithm)
4180
{
42-
string mechanism = GetMechanism(algorithm);
81+
if (algorithm == null)
82+
throw new ArgumentNullException(nameof(algorithm));
83+
84+
string mechanism = GetMechanism(algorithm) ?? algorithm.ToUpperInvariant();
4385

86+
var basicAgreement = GetBasicAgreementForMechanism(mechanism);
87+
if (basicAgreement != null)
88+
return basicAgreement;
89+
90+
throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised.");
91+
}
92+
93+
private static IBasicAgreement GetBasicAgreementForMechanism(string mechanism)
94+
{
4495
if (mechanism == "DH" || mechanism == "DIFFIEHELLMAN")
4596
return new DHBasicAgreement();
4697

4798
if (mechanism == "ECDH")
4899
return new ECDHBasicAgreement();
49100

50101
if (mechanism == "ECDHC" || mechanism == "ECCDH")
51-
return new ECDHCBasicAgreement();
102+
return new ECDHCBasicAgreement();
52103

53104
if (mechanism == "ECMQV")
54105
return new ECMqvBasicAgreement();
55106

56-
throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised.");
107+
return null;
57108
}
58109

59110
public static IBasicAgreement GetBasicAgreementWithKdf(DerObjectIdentifier agreeAlgOid,
60111
DerObjectIdentifier wrapAlgOid)
61112
{
62-
return GetBasicAgreementWithKdf(agreeAlgOid.Id, wrapAlgOid.Id);
113+
return GetBasicAgreementWithKdf(agreeAlgOid, wrapAlgOid?.Id);
63114
}
64115

116+
// TODO[api] Change parameter name to 'agreeAlgOid'
65117
public static IBasicAgreement GetBasicAgreementWithKdf(DerObjectIdentifier oid, string wrapAlgorithm)
66118
{
67-
return GetBasicAgreementWithKdf(oid.Id, wrapAlgorithm);
68-
}
119+
if (oid == null)
120+
throw new ArgumentNullException(nameof(oid));
121+
if (wrapAlgorithm == null)
122+
throw new ArgumentNullException(nameof(wrapAlgorithm));
123+
124+
if (AlgorithmOidMap.TryGetValue(oid, out var mechanism))
125+
{
126+
var basicAgreement = GetBasicAgreementWithKdfForMechanism(mechanism, wrapAlgorithm);
127+
if (basicAgreement != null)
128+
return basicAgreement;
129+
}
130+
131+
throw new SecurityUtilityException("Basic Agreement (with KDF) OID not recognised.");
132+
}
69133

70-
public static IBasicAgreement GetBasicAgreementWithKdf(string agreeAlgorithm, string wrapAlgorithm)
134+
public static IBasicAgreement GetBasicAgreementWithKdf(string agreeAlgorithm, string wrapAlgorithm)
71135
{
72-
string mechanism = GetMechanism(agreeAlgorithm);
136+
if (agreeAlgorithm == null)
137+
throw new ArgumentNullException(nameof(agreeAlgorithm));
138+
if (wrapAlgorithm == null)
139+
throw new ArgumentNullException(nameof(wrapAlgorithm));
140+
141+
string mechanism = GetMechanism(agreeAlgorithm) ?? agreeAlgorithm.ToUpperInvariant();
142+
143+
var basicAgreement = GetBasicAgreementWithKdfForMechanism(mechanism, wrapAlgorithm);
144+
if (basicAgreement != null)
145+
return basicAgreement;
146+
147+
throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised.");
148+
}
73149

150+
private static IBasicAgreement GetBasicAgreementWithKdfForMechanism(string mechanism, string wrapAlgorithm)
151+
{
74152
// 'DHWITHSHA1KDF' retained for backward compatibility
75-
if (mechanism == "DHWITHSHA1KDF" || mechanism == "ECDHWITHSHA1KDF")
76-
return new ECDHWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
153+
if (mechanism == "DHWITHSHA1KDF" || mechanism == "ECDHWITHSHA1KDF")
154+
return new ECDHWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
77155

78-
if (mechanism == "ECCDHWITHSHA1KDF")
79-
return new ECDHCWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
156+
if (mechanism == "ECCDHWITHSHA1KDF")
157+
return new ECDHCWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
80158

81-
if (mechanism == "ECMQVWITHSHA1KDF")
82-
return new ECMqvWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
159+
if (mechanism == "ECMQVWITHSHA1KDF")
160+
return new ECMqvWithKdfBasicAgreement(wrapAlgorithm, new ECDHKekGenerator(new Sha1Digest()));
83161

84-
throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised.");
85-
}
162+
return null;
163+
}
86164

87-
public static IRawAgreement GetRawAgreement(
88-
DerObjectIdentifier oid)
165+
public static IRawAgreement GetRawAgreement(DerObjectIdentifier oid)
89166
{
90-
return GetRawAgreement(oid.Id);
167+
if (oid == null)
168+
throw new ArgumentNullException(nameof(oid));
169+
170+
if (AlgorithmOidMap.TryGetValue(oid, out var mechanism))
171+
{
172+
var rawAgreement = GetRawAgreementForMechanism(mechanism);
173+
if (rawAgreement != null)
174+
return rawAgreement;
175+
}
176+
177+
throw new SecurityUtilityException("Raw Agreement OID not recognised.");
91178
}
92179

93180
public static IRawAgreement GetRawAgreement(string algorithm)
94181
{
95-
string mechanism = GetMechanism(algorithm);
182+
if (algorithm == null)
183+
throw new ArgumentNullException(nameof(algorithm));
184+
185+
string mechanism = GetMechanism(algorithm) ?? algorithm.ToUpperInvariant();
186+
187+
var rawAgreement = GetRawAgreementForMechanism(mechanism);
188+
if (rawAgreement != null)
189+
return rawAgreement;
190+
191+
throw new SecurityUtilityException("Raw Agreement " + algorithm + " not recognised.");
192+
}
96193

194+
private static IRawAgreement GetRawAgreementForMechanism(string mechanism)
195+
{
97196
if (mechanism == "X25519")
98197
return new X25519Agreement();
99198

100199
if (mechanism == "X448")
101200
return new X448Agreement();
102201

103-
throw new SecurityUtilityException("Raw Agreement " + algorithm + " not recognised.");
202+
return null;
104203
}
105204

106-
public static string GetAlgorithmName(DerObjectIdentifier oid)
107-
{
108-
return CollectionUtilities.GetValueOrNull(Algorithms, oid.Id);
109-
}
110-
111-
private static string GetMechanism(string algorithm)
205+
private static string GetMechanism(string algorithm)
112206
{
113-
var mechanism = CollectionUtilities.GetValueOrKey(Algorithms, algorithm);
207+
//if (AlgorithmMap.TryGetValue(algorithm, out var mechanism1))
208+
// return mechanism1;
209+
210+
if (DerObjectIdentifier.TryFromID(algorithm, out var oid))
211+
{
212+
if (AlgorithmOidMap.TryGetValue(oid, out var mechanism2))
213+
return mechanism2;
214+
}
114215

115-
return mechanism.ToUpperInvariant();
216+
return null;
116217
}
117218
}
118219
}

0 commit comments

Comments
 (0)