Skip to content

Commit a4a2e35

Browse files
committed
Extract OID constants into a separate class
1 parent 56bbab7 commit a4a2e35

6 files changed

+54
-33
lines changed

src/OpenGost.Security.Cryptography/CryptoConstants.cs

-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ internal static class CryptoConstants
1414
public const string Streebog512AlgorithmName = nameof(Streebog512);
1515
public const string HMACStreebog512AlgorithmName = nameof(HMACStreebog512);
1616

17-
public const string GostECDsa256OidValue = "1.2.643.7.1.1.1.1";
18-
public const string GostECDsa512OidValue = "1.2.643.7.1.1.1.2";
19-
public const string Streebog256OidValue = "1.2.643.7.1.1.2.2";
20-
public const string Streebog512OidValue = "1.2.643.7.1.1.2.3";
2117
}

src/OpenGost.Security.Cryptography/ECCurveOidMap.cs

+15-19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
internal static class ECCurveOidMap
44
{
5-
#region Constants
6-
75
private static ECCurve ECCurve256ParamsetTest { get; } = new ECCurve
86
{
97
CurveType = ECCurve.ECCurveType.PrimeShortWeierstrass,
@@ -415,8 +413,6 @@ internal static class ECCurveOidMap
415413
},
416414
};
417415

418-
#endregion
419-
420416
private static volatile IReadOnlyDictionary<string, ECCurve>? _ecCurveOidDictionary;
421417

422418
private static IReadOnlyDictionary<string, ECCurve> ECCurveOidDictionary
@@ -427,21 +423,21 @@ private static IReadOnlyDictionary<string, ECCurve> ECCurveOidDictionary
427423
{
428424
var ecCurveOidDictionary = new Dictionary<string, ECCurve>
429425
{
430-
{ "1.2.643.7.1.2.1.1.0", ECCurve256ParamsetTest },
431-
{ "1.2.643.7.1.2.1.1.1", ECCurve256ParamsetA },
432-
{ "1.2.643.7.1.2.1.1.2", ECCurve256ParamsetB },
433-
{ "1.2.643.7.1.2.1.1.3", ECCurve256ParamsetC },
434-
{ "1.2.643.7.1.2.1.1.4", ECCurve256ParamsetD },
435-
{ "1.2.643.2.2.35.0", ECCurve256ParamsetTest },
436-
{ "1.2.643.2.2.35.1", ECCurve256ParamsetB },
437-
{ "1.2.643.2.2.35.2", ECCurve256ParamsetC },
438-
{ "1.2.643.2.2.35.3", ECCurve256ParamsetD },
439-
{ "1.2.643.2.2.36.0", ECCurve256ParamsetB },
440-
{ "1.2.643.2.2.36.1", ECCurve256ParamsetD },
441-
{ "1.2.643.7.1.2.1.2.0", ECCurve512ParamsetTest },
442-
{ "1.2.643.7.1.2.1.2.1", ECCurve512ParamsetA },
443-
{ "1.2.643.7.1.2.1.2.2", ECCurve512ParamsetB },
444-
{ "1.2.643.7.1.2.1.2.3", ECCurve512ParamsetC },
426+
[Oids.ECCurve256TestParamSet] = ECCurve256ParamsetTest,
427+
[Oids.ECCurve256ParamSetA] = ECCurve256ParamsetA,
428+
[Oids.ECCurve256ParamSetB] = ECCurve256ParamsetB,
429+
[Oids.ECCurve256ParamSetC] = ECCurve256ParamsetC,
430+
[Oids.ECCurve256ParamSetD] = ECCurve256ParamsetD,
431+
[Oids.ECCurveCryptoProTestParamSet] = ECCurve256ParamsetTest,
432+
[Oids.ECCurveCryptoProParamSetA] = ECCurve256ParamsetB,
433+
[Oids.ECCurveCryptoProParamSetB] = ECCurve256ParamsetC,
434+
[Oids.ECCurveCryptoProParamSetC] = ECCurve256ParamsetD,
435+
[Oids.ECCurveCryptoProParamSetXchA] = ECCurve256ParamsetB,
436+
[Oids.ECCurveCryptoProParamSetXchB] = ECCurve256ParamsetD,
437+
[Oids.ECCurve512TestParamSet] = ECCurve512ParamsetTest,
438+
[Oids.ECCurve512ParamSetA] = ECCurve512ParamsetA,
439+
[Oids.ECCurve512ParamSetB] = ECCurve512ParamsetB,
440+
[Oids.ECCurve512ParamSetC] = ECCurve512ParamsetC,
445441
};
446442

447443
_ecCurveOidDictionary = ecCurveOidDictionary;

src/OpenGost.Security.Cryptography/GostECDsaCertificateExtensions.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
namespace System.Security.Cryptography.X509Certificates;
88
#pragma warning restore IDE0130
99

10-
using static CryptoConstants;
11-
1210
/// <summary>
1311
/// Provides extension methods for retrieving GOST 34.10-2018 <see cref="ECDsa"/> implementations for the
1412
/// public and private keys of a <see cref="X509Certificate2"/> certificate.
@@ -71,8 +69,7 @@ public static class GostECDsaCertificateExtensions
7169
/// </exception>
7270
public static GostECDsa? GetGostECDsaPrivateKey(this X509Certificate2 certificate)
7371
{
74-
if (certificate is null)
75-
throw new ArgumentNullException(nameof(certificate));
72+
ArgumentNullException.ThrowIfNull(certificate);
7673
7774
if (!certificate.HasPrivateKey || !IsGostECDsa(certificate))
7875
return null;
@@ -84,12 +81,12 @@ public static class GostECDsaCertificateExtensions
8481
private static bool IsGostECDsa(X509Certificate2 certificate)
8582
{
8683
var value = certificate.PublicKey.Oid.Value;
87-
if (value is not GostECDsa256OidValue and not GostECDsa512OidValue)
84+
if (value is not Oids.GostECDsa256 and not Oids.GostECDsa512)
8885
return false;
8986

9087
foreach (var extension in certificate.Extensions)
9188
{
92-
if (extension.Oid?.Value == "2.5.29.15")
89+
if (extension.Oid?.Value == Oids.KeyUsage)
9390
{
9491
var ext = (X509KeyUsageExtension)extension;
9592

src/OpenGost.Security.Cryptography/GostECDsaManaged.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private void ThrowIfDisposed()
336336
private static ECCurve GetDefaultCurve(int keySize)
337337
=> keySize switch
338338
{
339-
512 => ECCurve.CreateFromValue("1.2.643.7.1.2.1.2.1"),
340-
_ => ECCurve.CreateFromValue("1.2.643.7.1.2.1.1.1"),
339+
512 => ECCurve.CreateFromValue(Oids.ECCurve512ParamSetA),
340+
_ => ECCurve.CreateFromValue(Oids.ECCurve256ParamSetA),
341341
};
342342
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace OpenGost.Security.Cryptography;
2+
3+
internal static class Oids
4+
{
5+
// Gost ECDSA
6+
internal const string GostECDsa256 = "1.2.643.7.1.1.1.1";
7+
internal const string GostECDsa512 = "1.2.643.7.1.1.1.2";
8+
9+
// Hash Algorithms
10+
internal const string Streebog256 = "1.2.643.7.1.1.2.2";
11+
internal const string Streebog512 = "1.2.643.7.1.1.2.3";
12+
13+
// Elliptic Curve identifiers
14+
internal const string ECCurveCryptoProTestParamSet = "1.2.643.2.2.35.0";
15+
internal const string ECCurveCryptoProParamSetA = "1.2.643.2.2.35.1";
16+
internal const string ECCurveCryptoProParamSetB = "1.2.643.2.2.35.2";
17+
internal const string ECCurveCryptoProParamSetC = "1.2.643.2.2.35.3";
18+
internal const string ECCurveCryptoProParamSetXchA = "1.2.643.2.2.36.0";
19+
internal const string ECCurveCryptoProParamSetXchB = "1.2.643.2.2.36.1";
20+
internal const string ECCurve256TestParamSet = "1.2.643.7.1.2.1.1.0";
21+
internal const string ECCurve256ParamSetA = "1.2.643.7.1.2.1.1.1";
22+
internal const string ECCurve256ParamSetB = "1.2.643.7.1.2.1.1.2";
23+
internal const string ECCurve256ParamSetC = "1.2.643.7.1.2.1.1.3";
24+
internal const string ECCurve256ParamSetD = "1.2.643.7.1.2.1.1.4";
25+
internal const string ECCurve512TestParamSet = "1.2.643.7.1.2.1.2.0";
26+
internal const string ECCurve512ParamSetA = "1.2.643.7.1.2.1.2.1";
27+
internal const string ECCurve512ParamSetB = "1.2.643.7.1.2.1.2.2";
28+
internal const string ECCurve512ParamSetC = "1.2.643.7.1.2.1.2.3";
29+
30+
// Cert Extensions
31+
internal const string KeyUsage = "2.5.29.15";
32+
}

src/OpenGost.Security.Cryptography/OpenGostCryptoConfig.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static void ConfigureCryptographicServicesCore()
3838
AddAlgorithm(typeof(HMACStreebog512), HMACStreebog512AlgorithmName, XmlDsigHMACStreebog512Url);
3939
AddAlgorithm(typeof(GostECDsa256SignatureDescription), XmlDsigGostECDsaStreebog256Url);
4040
AddAlgorithm(typeof(GostECDsa512SignatureDescription), XmlDsigGostECDsaStreebog512Url);
41-
AddOID(Streebog256OidValue, Streebog256AlgorithmName);
42-
AddOID(Streebog512OidValue, Streebog512AlgorithmName);
41+
AddOID(Oids.Streebog256, Streebog256AlgorithmName);
42+
AddOID(Oids.Streebog512, Streebog512AlgorithmName);
4343
}
4444
}

0 commit comments

Comments
 (0)