Skip to content

Commit 1952703

Browse files
committed
Support for the SHA256-RSA-MGF1 signature method.
Signature description classes DigestAlgorithm changed.
1 parent 552b8ad commit 1952703

File tree

7 files changed

+38
-31
lines changed

7 files changed

+38
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void ValidateAlgorithm(string signatureAlgorithm)
2727
{
2828
return;
2929
}
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.");
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.");
3131

3232
}
3333

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureDescriptions/RSAPKCS1SHA1SignatureDescription.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public sealed class RSAPKCS1SHA1SignatureDescription : SignatureDescription
99
public RSAPKCS1SHA1SignatureDescription()
1010
{
1111
KeyAlgorithm = typeof(RSACryptoServiceProvider).AssemblyQualifiedName;
12-
DigestAlgorithm = typeof(SHA1Managed).AssemblyQualifiedName;
12+
DigestAlgorithm = "SHA1";
1313
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).AssemblyQualifiedName;
1414
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).AssemblyQualifiedName;
1515
}
@@ -22,7 +22,7 @@ public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgor
2222
}
2323

2424
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
25-
deformatter.SetHashAlgorithm("SHA1");
25+
deformatter.SetHashAlgorithm(DigestAlgorithm);
2626
return deformatter;
2727
}
2828

@@ -34,7 +34,7 @@ public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm
3434
}
3535

3636
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
37-
formatter.SetHashAlgorithm("SHA1");
37+
formatter.SetHashAlgorithm(DigestAlgorithm);
3838
return formatter;
3939
}
4040
}

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureDescriptions/RSAPKCS1SHA256SignatureDescription.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public sealed class RSAPKCS1SHA256SignatureDescription : SignatureDescription
99
public RSAPKCS1SHA256SignatureDescription()
1010
{
1111
KeyAlgorithm = typeof(RSACryptoServiceProvider).AssemblyQualifiedName;
12-
DigestAlgorithm = typeof(SHA256Managed).AssemblyQualifiedName;
12+
DigestAlgorithm = "SHA256";
1313
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).AssemblyQualifiedName;
1414
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).AssemblyQualifiedName;
1515
}
@@ -22,7 +22,7 @@ public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgor
2222
}
2323

2424
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
25-
deformatter.SetHashAlgorithm("SHA256");
25+
deformatter.SetHashAlgorithm(DigestAlgorithm);
2626
return deformatter;
2727
}
2828

@@ -34,7 +34,7 @@ public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm
3434
}
3535

3636
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
37-
formatter.SetHashAlgorithm("SHA256");
37+
formatter.SetHashAlgorithm(DigestAlgorithm);
3838
return formatter;
3939
}
4040
}

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureDescriptions/RSAPKCS1SHA384SignatureDescription.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public sealed class RSAPKCS1SHA384SignatureDescription : SignatureDescription
99
public RSAPKCS1SHA384SignatureDescription()
1010
{
1111
KeyAlgorithm = typeof(RSACryptoServiceProvider).AssemblyQualifiedName;
12-
DigestAlgorithm = typeof(SHA384Managed).AssemblyQualifiedName;
12+
DigestAlgorithm = "SHA384";
1313
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).AssemblyQualifiedName;
1414
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).AssemblyQualifiedName;
1515
}
@@ -22,7 +22,7 @@ public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgor
2222
}
2323

2424
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
25-
deformatter.SetHashAlgorithm("SHA384");
25+
deformatter.SetHashAlgorithm(DigestAlgorithm);
2626
return deformatter;
2727
}
2828

@@ -34,7 +34,7 @@ public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm
3434
}
3535

3636
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
37-
formatter.SetHashAlgorithm("SHA384");
37+
formatter.SetHashAlgorithm(DigestAlgorithm);
3838
return formatter;
3939
}
4040
}

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureDescriptions/RSAPKCS1SHA512SignatureDescription.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public sealed class RSAPKCS1SHA512SignatureDescription : SignatureDescription
99
public RSAPKCS1SHA512SignatureDescription()
1010
{
1111
KeyAlgorithm = typeof(RSACryptoServiceProvider).AssemblyQualifiedName;
12-
DigestAlgorithm = typeof(SHA512Managed).AssemblyQualifiedName;
12+
DigestAlgorithm = "SHA512";
1313
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).AssemblyQualifiedName;
1414
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).AssemblyQualifiedName;
1515
}
@@ -22,7 +22,7 @@ public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgor
2222
}
2323

2424
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
25-
deformatter.SetHashAlgorithm("SHA512");
25+
deformatter.SetHashAlgorithm(DigestAlgorithm);
2626
return deformatter;
2727
}
2828

@@ -34,7 +34,7 @@ public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm
3434
}
3535

3636
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
37-
formatter.SetHashAlgorithm("SHA512");
37+
formatter.SetHashAlgorithm(DigestAlgorithm);
3838
return formatter;
3939
}
4040
}

src/ITfoxtec.Identity.Saml2/Cryptography/SignatureDescriptions/RSAPSSSHA256SignatureDescription.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
using System.Security.Cryptography;
1+
using System;
2+
using System.Security.Cryptography;
23

34
namespace ITfoxtec.Identity.Saml2.Cryptography
45
{
56
public class RSAPSSSHA256SignatureDescription : SignatureDescription
67
{
78
public RSAPSSSHA256SignatureDescription()
89
{
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;
10+
KeyAlgorithm = typeof(RSACryptoServiceProvider).AssemblyQualifiedName;
11+
DigestAlgorithm = "SHA256";
12+
FormatterAlgorithm = typeof(RsaPssSignatureFormatter).FullName;
13+
DeformatterAlgorithm = typeof(RsaPssSignatureDeformatter).FullName;
1714
}
1815

1916
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
2017
{
18+
if (key == null)
19+
{
20+
throw new ArgumentNullException("key");
21+
}
22+
2123
var signatureFormatter = new RsaPssSignatureFormatter();
2224
signatureFormatter.SetKey(key);
23-
signatureFormatter.SetHashAlgorithm(this.DigestAlgorithm);
25+
signatureFormatter.SetHashAlgorithm(DigestAlgorithm);
2426
return signatureFormatter;
2527
}
2628

2729
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
2830
{
31+
if (key == null)
32+
{
33+
throw new ArgumentNullException("key");
34+
}
35+
2936
var signatureDeformatter = new RsaPssSignatureDeformatter();
3037
signatureDeformatter.SetKey(key);
31-
signatureDeformatter.SetHashAlgorithm(this.DigestAlgorithm);
38+
signatureDeformatter.SetHashAlgorithm(DigestAlgorithm);
3239
return signatureDeformatter;
3340
}
3441

@@ -39,20 +46,20 @@ public class RsaPssSignatureFormatter : AsymmetricSignatureFormatter
3946

4047
public override void SetKey(AsymmetricAlgorithm key)
4148
{
42-
this.Key = (RSA)key;
49+
Key = (RSA)key;
4350
}
4451

4552
public override void SetHashAlgorithm(string strName)
4653
{
4754
// Verify the name
4855
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
4956

50-
this.HashAlgorithmName = strName;
57+
HashAlgorithmName = strName;
5158
}
5259

5360
public override byte[] CreateSignature(byte[] rgbHash)
5461
{
55-
return this.Key.SignHash(rgbHash, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
62+
return Key.SignHash(rgbHash, new HashAlgorithmName(HashAlgorithmName), RSASignaturePadding.Pss);
5663
}
5764
}
5865

@@ -63,20 +70,20 @@ public class RsaPssSignatureDeformatter : AsymmetricSignatureDeformatter
6370

6471
public override void SetKey(AsymmetricAlgorithm key)
6572
{
66-
this.Key = (RSA)key;
73+
Key = (RSA)key;
6774
}
6875

6976
public override void SetHashAlgorithm(string strName)
7077
{
7178
// Verify the name
7279
Oid.FromFriendlyName(strName, OidGroup.HashAlgorithm);
7380

74-
this.HashAlgorithmName = strName;
81+
HashAlgorithmName = strName;
7582
}
7683

7784
public override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
7885
{
79-
return this.Key.VerifyHash(rgbHash, rgbSignature, new HashAlgorithmName(this.HashAlgorithmName), RSASignaturePadding.Pss);
86+
return Key.VerifyHash(rgbHash, rgbSignature, new HashAlgorithmName(HashAlgorithmName), RSASignaturePadding.Pss);
8087
}
8188
}
8289
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static class Saml2SecurityAlgorithms
3838
/// </summary>
3939
public const string RsaSha512Signature = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
4040
/// <summary>
41-
/// URI for the sha256-rsa-mgf1 signature method for signing XML.
41+
/// URI for the SHA256-RSA-MGF1 signature method for signing XML.
4242
/// </summary>
4343
public const string RsaPssSha256Signature = "http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1";
4444
}

0 commit comments

Comments
 (0)