Skip to content

Commit 552b8ad

Browse files
authored
Merge pull request #137 from SINC-GmbH/fix_for_sha256-rsa-MGF_support
extend SignatureAlgorithms of sha256-rsa-MGF
2 parents c5894af + e424ea2 commit 552b8ad

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

src/ITfoxtec.Identity.Saml2/Cryptography/Saml2Signer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static Saml2Signer()
2020
CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), Saml2SecurityAlgorithms.RsaSha256Signature);
2121
CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA384SignatureDescription), Saml2SecurityAlgorithms.RsaSha384Signature);
2222
CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA512SignatureDescription), Saml2SecurityAlgorithms.RsaSha512Signature);
23+
CryptoConfig.AddAlgorithm(typeof(RSAPSSSHA256SignatureDescription), Saml2SecurityAlgorithms.RsaPssSha256Signature);
2324
}
2425
#endif
2526

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureAlgorithm.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ public static void ValidateAlgorithm(string signatureAlgorithm)
2323
{
2424
return;
2525
}
26+
else if (Saml2SecurityAlgorithms.RsaPssSha256Signature.Equals(signatureAlgorithm, StringComparison.InvariantCulture))
27+
{
28+
return;
29+
}
30+
throw new NotSupportedException($"Only SHA1 ({Saml2SecurityAlgorithms.RsaSha1Signature}), SHA256 ({Saml2SecurityAlgorithms.RsaSha256Signature}), SHA384 ({Saml2SecurityAlgorithms.RsaSha384Signature}), SHA512 ({Saml2SecurityAlgorithms.RsaSha512Signature}) and Sha256 Rsa MGF1 ({Saml2SecurityAlgorithms.RsaPssSha256Signature}) is supported.");
2631

27-
throw new NotSupportedException($"Only SHA1 ({Saml2SecurityAlgorithms.RsaSha1Signature}), SHA256 ({Saml2SecurityAlgorithms.RsaSha256Signature}), SHA384 ({Saml2SecurityAlgorithms.RsaSha384Signature}) and SHA512 ({Saml2SecurityAlgorithms.RsaSha512Signature}) is supported.");
2832
}
2933

3034
public static string DigestMethod(string signatureAlgorithm)
@@ -45,11 +49,15 @@ public static string DigestMethod(string signatureAlgorithm)
4549
{
4650
return Saml2SecurityAlgorithms.Sha512Digest;
4751
}
52+
else if (Saml2SecurityAlgorithms.RsaPssSha256Signature.Equals(signatureAlgorithm, StringComparison.InvariantCulture))
53+
{
54+
return Saml2SecurityAlgorithms.Sha256Digest;
55+
}
4856
else
4957
{
5058
ValidateAlgorithm(signatureAlgorithm);
5159
throw new InvalidOperationException();
52-
}
60+
}
5361
}
5462
}
5563
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Security.Cryptography;
2+
3+
namespace ITfoxtec.Identity.Saml2.Cryptography
4+
{
5+
public class RSAPSSSHA256SignatureDescription : SignatureDescription
6+
{
7+
public RSAPSSSHA256SignatureDescription()
8+
{
9+
using (var rsa = RSA.Create())
10+
{
11+
this.KeyAlgorithm = rsa.GetType().AssemblyQualifiedName; // Does not like a simple algorithm name, but wants a type name (AssembyQualifiedName in Core)
12+
}
13+
14+
this.DigestAlgorithm = "SHA256"; // Somehow wants a simple algorithm name
15+
this.FormatterAlgorithm = typeof(RsaPssSignatureFormatter).FullName;
16+
this.DeformatterAlgorithm = typeof(RsaPssSignatureDeformatter).FullName;
17+
}
18+
19+
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
20+
{
21+
var signatureFormatter = new RsaPssSignatureFormatter();
22+
signatureFormatter.SetKey(key);
23+
signatureFormatter.SetHashAlgorithm(this.DigestAlgorithm);
24+
return signatureFormatter;
25+
}
26+
27+
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
28+
{
29+
var signatureDeformatter = new RsaPssSignatureDeformatter();
30+
signatureDeformatter.SetKey(key);
31+
signatureDeformatter.SetHashAlgorithm(this.DigestAlgorithm);
32+
return signatureDeformatter;
33+
}
34+
35+
public class RsaPssSignatureFormatter : AsymmetricSignatureFormatter
36+
{
37+
private RSA Key { get; set; }
38+
private string HashAlgorithmName { get; set; }
39+
40+
public override void SetKey(AsymmetricAlgorithm key)
41+
{
42+
this.Key = (RSA)key;
43+
}
44+
45+
public override void SetHashAlgorithm(string strName)
46+
{
47+
// Verify the name
48+
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
49+
50+
this.HashAlgorithmName = strName;
51+
}
52+
53+
public override byte[] CreateSignature(byte[] rgbHash)
54+
{
55+
return this.Key.SignHash(rgbHash, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
56+
}
57+
}
58+
59+
public class RsaPssSignatureDeformatter : AsymmetricSignatureDeformatter
60+
{
61+
private RSA Key { get; set; }
62+
private string HashAlgorithmName { get; set; }
63+
64+
public override void SetKey(AsymmetricAlgorithm key)
65+
{
66+
this.Key = (RSA)key;
67+
}
68+
69+
public override void SetHashAlgorithm(string strName)
70+
{
71+
// Verify the name
72+
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
73+
74+
this.HashAlgorithmName = strName;
75+
}
76+
77+
public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
78+
{
79+
return this.Key.VerifyHash(rgbHash, rgbSignature, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
80+
}
81+
}
82+
}
83+
}

src/ITfoxtec.Identity.Saml2/Schemas/Saml2SecurityAlgorithms.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static class Saml2SecurityAlgorithms
3737
/// URI for the RSA-SHA-512 signature method for signing XML.
3838
/// </summary>
3939
public const string RsaSha512Signature = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
40-
40+
/// <summary>
41+
/// URI for the sha256-rsa-mgf1 signature method for signing XML.
42+
/// </summary>
43+
public const string RsaPssSha256Signature = "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1";
4144
}
4245
}

0 commit comments

Comments
 (0)