forked from jitbit/AspNetSaml
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
175 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Security.Cryptography.Xml; | ||
using System.Xml; | ||
|
||
namespace CoreSaml2Utils.Utilities; | ||
|
||
/// <summary> | ||
/// https://github.com/optiklab/SAML-integration-utilities/blob/main/src/SamlIntegration.Utilities/Helpers/SamlSignedXml.cs | ||
/// </summary> | ||
internal class SamlSignedXml : SignedXml | ||
{ | ||
private readonly string _referenceAttributeId; | ||
|
||
public SamlSignedXml(XmlDocument document, string referenceAttributeId) : base(document) | ||
{ | ||
_referenceAttributeId = referenceAttributeId; | ||
} | ||
|
||
public SamlSignedXml(XmlElement element, string referenceAttributeId) : base(element) | ||
{ | ||
_referenceAttributeId = referenceAttributeId; | ||
} | ||
|
||
public override XmlElement GetIdElement(XmlDocument document, string idValue) | ||
=> (XmlElement)document.SelectSingleNode($"//*[@{_referenceAttributeId}='{idValue}']"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Security.Cryptography.Xml; | ||
using System.Xml; | ||
|
||
namespace CoreSaml2Utils.Utilities; | ||
|
||
/// <summary> | ||
/// https://github.com/optiklab/SAML-integration-utilities/blob/main/src/SamlIntegration.Utilities/Helpers/SigningHelper.cs#L8-L68 | ||
/// </summary> | ||
internal class SigningHelper | ||
{ | ||
internal static SamlSignedXml SignXml(XmlDocument doc, X509Certificate2 certificate, string referenceId, string referenceValue) | ||
{ | ||
var samlSignedXml = new SamlSignedXml(doc, referenceId); | ||
return SignXml(samlSignedXml, certificate, referenceValue); | ||
} | ||
|
||
internal static SamlSignedXml SignXml(XmlElement element, X509Certificate2 certificate, string referenceId, string referenceValue) | ||
{ | ||
var samlSignedXml = new SamlSignedXml(element, referenceId); | ||
return SignXml(samlSignedXml, certificate, referenceValue); | ||
} | ||
|
||
private static SamlSignedXml SignXml(SamlSignedXml samlSignedXml, X509Certificate2 certificate, string referenceValue) | ||
{ | ||
samlSignedXml.SigningKey = certificate.PrivateKey; | ||
samlSignedXml.SignedInfo.CanonicalizationMethod = SamlSignedXml.XmlDsigExcC14NTransformUrl; | ||
|
||
// Create a reference to be signed. | ||
var reference = new Reference | ||
{ | ||
Uri = "#" + referenceValue | ||
}; | ||
|
||
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); | ||
reference.AddTransform(new XmlDsigExcC14NTransform()); | ||
|
||
// Add the reference to the SignedXml object. | ||
samlSignedXml.AddReference(reference); | ||
|
||
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). | ||
var keyInfo = new KeyInfo(); | ||
keyInfo.AddClause(new KeyInfoX509Data(certificate)); | ||
|
||
samlSignedXml.KeyInfo = keyInfo; | ||
|
||
// Compute the signature. | ||
samlSignedXml.ComputeSignature(); | ||
|
||
return samlSignedXml; | ||
} | ||
} |