using System;
using System.Collections.Generic;
-using System.Linq;
+using System.Diagnostics;
using System.Security.Cryptography;
namespace Internal.Cryptography
// ---- PAL layer ends here ----
// -----------------------------
+ /// <summary>Expected size of <see cref="s_extraFriendlyNameToOid"/>.</summary>
+ private const int ExtraFriendlyNameToOidCount = 8;
+
// There are places inside the framework where Oid.FromFriendlyName is called
// (to pass in an OID group restriction for Windows) and an exception is not tolerated.
//
// and OpenSSL produce different answers. Since the answers originally came from OpenSSL
// on macOS, this preserves the OpenSSL names.
private static readonly Dictionary<string, string> s_extraFriendlyNameToOid =
- new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
+ new Dictionary<string, string>(ExtraFriendlyNameToOidCount, StringComparer.OrdinalIgnoreCase)
{
{ "pkcs7-data", "1.2.840.113549.1.7.1" },
{ "contentType", "1.2.840.113549.1.9.3" },
};
private static readonly Dictionary<string, string> s_extraOidToFriendlyName =
- s_extraFriendlyNameToOid.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
+ InvertWithDefaultComparer(s_extraFriendlyNameToOid);
+
+#if DEBUG
+ static partial void ExtraStaticDebugValidation()
+ {
+ // Validate we hardcoded the right dictionary size
+ Debug.Assert(s_extraFriendlyNameToOid.Count == ExtraFriendlyNameToOidCount,
+ $"Expected {nameof(s_extraFriendlyNameToOid)}.{nameof(s_extraFriendlyNameToOid.Count)} == {ExtraFriendlyNameToOidCount}, got {s_extraFriendlyNameToOid.Count}");
+ Debug.Assert(s_extraOidToFriendlyName.Count == ExtraFriendlyNameToOidCount,
+ $"Expected {nameof(s_extraOidToFriendlyName)}.{nameof(s_extraOidToFriendlyName.Count)} == {ExtraFriendlyNameToOidCount}, got {s_extraOidToFriendlyName.Count}");
+ }
+#endif
}
}
// See the LICENSE file in the project root for more information.
using System;
-using System.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Security.Cryptography;
namespace Internal.Cryptography
return mappedOid;
}
+ /// <summary>Expected size of <see cref="s_friendlyNameToOid"/>.</summary>
+ private const int FriendlyNameToOidCount = 103;
+
// This table was originally built by extracting every szOID #define out of wincrypt.h,
// and running them through new Oid(string) on Windows 10. Then, take the list of everything
// which produced a FriendlyName value, and run it through two other languages. If all 3 agree
// is to prevent issues wherein an identifier is different between CoreFX\Windows and CoreFX\Unix;
// since any existing code would be using the Windows identifier, it is the de facto standard.
private static readonly Dictionary<string, string> s_friendlyNameToOid =
- new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
+ new Dictionary<string, string>(FriendlyNameToOidCount, StringComparer.OrdinalIgnoreCase)
{
{ "3des", "1.2.840.113549.3.7" },
{ "aes128", "2.16.840.1.101.3.4.1.2" },
};
private static readonly Dictionary<string, string> s_oidToFriendlyName =
- s_friendlyNameToOid.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
+ InvertWithDefaultComparer(s_friendlyNameToOid);
private static readonly Dictionary<string, string> s_compatOids =
new Dictionary<string, string>
{ "1.3.14.3.2.4", "md4RSA" },
{ "1.3.14.7.2.3.1", "md2RSA" },
};
+
+ private static Dictionary<string, string> InvertWithDefaultComparer(Dictionary<string, string> source)
+ {
+ var result = new Dictionary<string, string>(source.Count);
+ foreach (KeyValuePair<string, string> item in source)
+ {
+ result.Add(item.Value, item.Key);
+ }
+ return result;
+ }
+
+#if DEBUG
+ static OidLookup()
+ {
+ // Validate we hardcoded the right dictionary size
+ Debug.Assert(s_friendlyNameToOid.Count == FriendlyNameToOidCount,
+ $"Expected {nameof(s_friendlyNameToOid)}.{nameof(s_friendlyNameToOid.Count)} == {FriendlyNameToOidCount}, got {s_friendlyNameToOid.Count}");
+ Debug.Assert(s_oidToFriendlyName.Count == FriendlyNameToOidCount,
+ $"Expected {nameof(s_oidToFriendlyName)}.{nameof(s_oidToFriendlyName.Count)} == {FriendlyNameToOidCount}, got {s_oidToFriendlyName.Count}");
+
+ ExtraStaticDebugValidation();
+ }
+
+ static partial void ExtraStaticDebugValidation();
+#endif
}
}