|
12 | 12 |
|
13 | 13 | namespace Org.BouncyCastle.Security
|
14 | 14 | {
|
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 |
19 | 19 | {
|
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>(); |
22 | 22 |
|
23 | 23 | static AgreementUtilities()
|
24 | 24 | {
|
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 | + } |
28 | 58 |
|
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); |
31 | 62 | }
|
32 | 63 |
|
33 |
| - public static IBasicAgreement GetBasicAgreement( |
34 |
| - DerObjectIdentifier oid) |
| 64 | + public static IBasicAgreement GetBasicAgreement(DerObjectIdentifier oid) |
35 | 65 | {
|
36 |
| - return GetBasicAgreement(oid.Id); |
37 |
| - } |
| 66 | + if (oid == null) |
| 67 | + throw new ArgumentNullException(nameof(oid)); |
38 | 68 |
|
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) |
41 | 80 | {
|
42 |
| - string mechanism = GetMechanism(algorithm); |
| 81 | + if (algorithm == null) |
| 82 | + throw new ArgumentNullException(nameof(algorithm)); |
| 83 | + |
| 84 | + string mechanism = GetMechanism(algorithm) ?? algorithm.ToUpperInvariant(); |
43 | 85 |
|
| 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 | + { |
44 | 95 | if (mechanism == "DH" || mechanism == "DIFFIEHELLMAN")
|
45 | 96 | return new DHBasicAgreement();
|
46 | 97 |
|
47 | 98 | if (mechanism == "ECDH")
|
48 | 99 | return new ECDHBasicAgreement();
|
49 | 100 |
|
50 | 101 | if (mechanism == "ECDHC" || mechanism == "ECCDH")
|
51 |
| - return new ECDHCBasicAgreement(); |
| 102 | + return new ECDHCBasicAgreement(); |
52 | 103 |
|
53 | 104 | if (mechanism == "ECMQV")
|
54 | 105 | return new ECMqvBasicAgreement();
|
55 | 106 |
|
56 |
| - throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised."); |
| 107 | + return null; |
57 | 108 | }
|
58 | 109 |
|
59 | 110 | public static IBasicAgreement GetBasicAgreementWithKdf(DerObjectIdentifier agreeAlgOid,
|
60 | 111 | DerObjectIdentifier wrapAlgOid)
|
61 | 112 | {
|
62 |
| - return GetBasicAgreementWithKdf(agreeAlgOid.Id, wrapAlgOid.Id); |
| 113 | + return GetBasicAgreementWithKdf(agreeAlgOid, wrapAlgOid?.Id); |
63 | 114 | }
|
64 | 115 |
|
| 116 | + // TODO[api] Change parameter name to 'agreeAlgOid' |
65 | 117 | public static IBasicAgreement GetBasicAgreementWithKdf(DerObjectIdentifier oid, string wrapAlgorithm)
|
66 | 118 | {
|
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 | + } |
69 | 133 |
|
70 |
| - public static IBasicAgreement GetBasicAgreementWithKdf(string agreeAlgorithm, string wrapAlgorithm) |
| 134 | + public static IBasicAgreement GetBasicAgreementWithKdf(string agreeAlgorithm, string wrapAlgorithm) |
71 | 135 | {
|
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 | + } |
73 | 149 |
|
| 150 | + private static IBasicAgreement GetBasicAgreementWithKdfForMechanism(string mechanism, string wrapAlgorithm) |
| 151 | + { |
74 | 152 | // '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())); |
77 | 155 |
|
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())); |
80 | 158 |
|
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())); |
83 | 161 |
|
84 |
| - throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised."); |
85 |
| - } |
| 162 | + return null; |
| 163 | + } |
86 | 164 |
|
87 |
| - public static IRawAgreement GetRawAgreement( |
88 |
| - DerObjectIdentifier oid) |
| 165 | + public static IRawAgreement GetRawAgreement(DerObjectIdentifier oid) |
89 | 166 | {
|
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."); |
91 | 178 | }
|
92 | 179 |
|
93 | 180 | public static IRawAgreement GetRawAgreement(string algorithm)
|
94 | 181 | {
|
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 | + } |
96 | 193 |
|
| 194 | + private static IRawAgreement GetRawAgreementForMechanism(string mechanism) |
| 195 | + { |
97 | 196 | if (mechanism == "X25519")
|
98 | 197 | return new X25519Agreement();
|
99 | 198 |
|
100 | 199 | if (mechanism == "X448")
|
101 | 200 | return new X448Agreement();
|
102 | 201 |
|
103 |
| - throw new SecurityUtilityException("Raw Agreement " + algorithm + " not recognised."); |
| 202 | + return null; |
104 | 203 | }
|
105 | 204 |
|
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) |
112 | 206 | {
|
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 | + } |
114 | 215 |
|
115 |
| - return mechanism.ToUpperInvariant(); |
| 216 | + return null; |
116 | 217 | }
|
117 | 218 | }
|
118 | 219 | }
|
0 commit comments