Skip to content

Commit 5d35a70

Browse files
committed
v61.4.0 - Adding a IdentifyAttributes extension method to X509Certificate2
1 parent 3d21e00 commit 5d35a70

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

InterlockLedger.Tags/Extensions/X509Certificate2Extensions.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,35 @@
3030
//
3131
// ******************************************************************************************************************************
3232

33-
33+
using System.Security.Cryptography;
3434
using System.Security.Cryptography.X509Certificates;
3535

3636
namespace InterlockLedger.Tags;
37+
3738
public static class X509Certificate2Extensions
3839
{
39-
public static KeyStrength KeyStrengthGuess(this X509Certificate2 certificate)
40-
=> certificate.Required().GetRSAPublicKey().KeyStrengthGuess();
40+
public static (KeyStrength keyStrength, Algorithm algorithm) IdentifyAttributes(this X509Certificate2 certificate) =>
41+
certificate.Selector(
42+
(rsa) => (rsa.KeyStrengthGuess(), Algorithm.RSA),
43+
(ec) => (KeyStrength.Strong, Algorithm.EcDSA),
44+
() => (KeyStrength.Normal, Algorithm.Invalid));
45+
46+
public static TagPubKey PubKey(this X509Certificate2 certificate) =>
47+
certificate.Selector<TagPubKey>(
48+
(rsa) => new TagPubRSAKey(rsa.ExportParameters(false)),
49+
(ec) => new TagPubEcDSAKey(ec.ExportParameters(false)));
4150

42-
public static TagPubKey PubKey(this X509Certificate2 certificate)
43-
=> TagPubKey.Resolve(certificate);
44-
}
51+
private static T Selector<T>(this X509Certificate2 certificate,
52+
Func<RSA, T> rsaFunc,
53+
Func<ECDsa, T> ecdsaFunc,
54+
Func<T>? errorFunc = null) =>
55+
certificate.GetRSAPublicKey() switch {
56+
RSA rsa => rsaFunc(rsa),
57+
_ => certificate.GetECDsaPublicKey() switch {
58+
ECDsa ec => ecdsaFunc(ec),
59+
_ => errorFunc is null
60+
? throw new NotSupportedException("Not yet supporting other kinds of certificates, than RSA and EcDSA!")
61+
: errorFunc()
62+
}
63+
};
64+
}

InterlockLedger.Tags/InterlockLedger.Tags.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
<Import Project="..\version.props" />
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<LangVersion>preview</LangVersion>
@@ -13,9 +13,7 @@
1313
<Product>InterlockLedger</Product>
1414
<RepositoryType>git</RepositoryType>
1515
<RepositoryUrl>https://github.com/interlockledger/interlockledger-tags.git</RepositoryUrl>
16-
<Version>61.3.0</Version>
1716
<PackageId>InterlockLedger.Tags</PackageId>
18-
<PackageReleaseNotes>Needed one more public property in ISigningKey EncodedBytes</PackageReleaseNotes>
1917
<ImplicitUsings>enable</ImplicitUsings>
2018
<Nullable>enable</Nullable>
2119
<PackageReadmeFile>README.md</PackageReadmeFile>

version.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Version>61.4.0</Version>
4+
<PackageReleaseNotes>Adding a IdentifyAttributes extension method to X509Certificate2</PackageReleaseNotes>
5+
</PropertyGroup>
6+
</Project>

0 commit comments

Comments
 (0)