1
1
using CIE . AspNetCore . Authentication . Resources ;
2
2
using System ;
3
- using System . Collections . Concurrent ;
4
3
using System . Linq ;
5
4
using System . Security . Cryptography ;
6
5
using System . Security . Cryptography . X509Certificates ;
7
6
using System . Security . Cryptography . Xml ;
8
7
using System . Xml ;
9
- using System . Xml . Serialization ;
10
8
11
9
namespace CIE . AspNetCore . Authentication . Helpers
12
10
{
@@ -36,7 +34,7 @@ internal static XmlElement SignXMLDoc(XmlDocument doc,
36
34
37
35
try
38
36
{
39
- privateKey = certificate . PrivateKey ;
37
+ privateKey = certificate . GetRSAPrivateKey ( ) ;
40
38
}
41
39
catch ( Exception ex )
42
40
{
@@ -80,11 +78,9 @@ internal static bool VerifySignature(XmlDocument signedDocument, Saml.IdP.Entity
80
78
81
79
try
82
80
{
83
- SignedXml signedXml = new SignedXml ( signedDocument ) ;
84
-
85
81
if ( xmlMetadata is not null )
86
82
{
87
- bool validated = false ;
83
+ var validated = false ;
88
84
var idpSSODescriptor = xmlMetadata . Items . FirstOrDefault ( i => i is Saml . IdP . IDPSSODescriptorType ) as Saml . IdP . IDPSSODescriptorType ;
89
85
if ( idpSSODescriptor is not null )
90
86
{
@@ -96,14 +92,7 @@ internal static bool VerifySignature(XmlDocument signedDocument, Saml.IdP.Entity
96
92
var x509Cert = keyData . Items . FirstOrDefault ( i => i is byte [ ] ) as byte [ ] ;
97
93
if ( x509Cert is not null )
98
94
{
99
- var publicMetadataCert = new X509Certificate2 ( x509Cert ) ;
100
- XmlNodeList nodeList = ( signedDocument . GetElementsByTagName ( "ds:Signature" ) ? . Count > 1 ) ?
101
- signedDocument . GetElementsByTagName ( "ds:Signature" ) :
102
- ( signedDocument . GetElementsByTagName ( "ns2:Signature" ) ? . Count > 1 ) ?
103
- signedDocument . GetElementsByTagName ( "ns2:Signature" ) :
104
- signedDocument . GetElementsByTagName ( "Signature" ) ;
105
- signedXml . LoadXml ( ( XmlElement ) nodeList [ 0 ] ) ;
106
- validated |= signedXml . CheckSignature ( publicMetadataCert , true ) ;
95
+ validated |= VerifyAllSignatures ( signedDocument , new X509Certificate2 ( x509Cert ) ) ;
107
96
}
108
97
}
109
98
}
@@ -112,11 +101,7 @@ internal static bool VerifySignature(XmlDocument signedDocument, Saml.IdP.Entity
112
101
}
113
102
else
114
103
{
115
- XmlNodeList nodeList = ( signedDocument . GetElementsByTagName ( "ds:Signature" ) ? . Count > 0 ) ?
116
- signedDocument . GetElementsByTagName ( "ds:Signature" ) :
117
- signedDocument . GetElementsByTagName ( "Signature" ) ;
118
- signedXml . LoadXml ( ( XmlElement ) nodeList [ 0 ] ) ;
119
- return signedXml . CheckSignature ( ) ;
104
+ return VerifyAllSignatures ( signedDocument ) ;
120
105
}
121
106
}
122
107
catch ( Exception )
@@ -125,64 +110,26 @@ internal static bool VerifySignature(XmlDocument signedDocument, Saml.IdP.Entity
125
110
}
126
111
}
127
112
128
- private static readonly ConcurrentDictionary < Type , XmlSerializer > serializers = new ConcurrentDictionary < Type , XmlSerializer > ( ) ;
129
- /// <summary>
130
- /// Serializes to XML document.
131
- /// </summary>
132
- /// <param name="o">The o.</param>
133
- /// <returns></returns>
134
- public static XmlDocument SerializeToXmlDoc ( this object o )
113
+ private static bool VerifyAllSignatures ( XmlDocument signedDocument , X509Certificate2 ? publicMetadataCert = null )
135
114
{
136
- XmlDocument doc = new XmlDocument ( ) { PreserveWhitespace = true } ;
115
+ bool internalResult = true ;
116
+
117
+ XmlNodeList signatureNodes = signedDocument . GetElementsByTagName ( "Signature" , SignedXml . XmlDsigNamespaceUrl ) ;
137
118
138
- using XmlWriter writer = doc . CreateNavigator ( ) . AppendChild ( ) ;
139
- if ( ! serializers . ContainsKey ( o . GetType ( ) ) )
119
+ if ( signatureNodes . Count == 0 )
140
120
{
141
- var serializer = new XmlSerializer ( o . GetType ( ) ) ;
142
- serializers . AddOrUpdate ( o . GetType ( ) , serializer , ( key , value ) => serializer ) ;
121
+ return false ;
143
122
}
144
- serializers [ o . GetType ( ) ] . Serialize ( writer , o ) ;
145
-
146
- return doc ;
147
- }
148
-
149
- public static XmlElement SerializeInternalExtensionToXmlElement ( object o , string namespacePrefix , string xmlNamespace )
150
- {
151
- XmlDocument doc = SerializeExtensionToXmlElementInternal ( o , namespacePrefix , xmlNamespace ) ;
152
-
153
- return doc . DocumentElement . FirstChild as XmlElement ;
154
- }
155
123
156
- public static XmlElement SerializeExtensionToXmlElement ( object o , string namespacePrefix , string xmlNamespace )
157
- {
158
- XmlDocument doc = SerializeExtensionToXmlElementInternal ( o , namespacePrefix , xmlNamespace ) ;
159
-
160
- return doc . DocumentElement ;
161
- }
162
-
163
- private static XmlDocument SerializeExtensionToXmlElementInternal ( object o , string namespacePrefix , string xmlNamespace )
164
- {
165
- XmlDocument doc = new XmlDocument ( ) ;
166
-
167
- using ( XmlWriter writer = doc . CreateNavigator ( ) . AppendChild ( ) )
124
+ foreach ( var signatureNode in signatureNodes )
168
125
{
169
- var ns = new XmlSerializerNamespaces ( ) ;
170
- ns . Add ( namespacePrefix , xmlNamespace ) ;
171
- new XmlSerializer ( o . GetType ( ) ) . Serialize ( writer , o , ns ) ;
126
+ SignedXml signedXml = new ( signedDocument ) ;
127
+ signedXml . LoadXml ( ( XmlElement ) signatureNode ) ;
128
+ internalResult &= publicMetadataCert is null
129
+ ? signedXml . CheckSignature ( )
130
+ : signedXml . CheckSignature ( publicMetadataCert , true ) ;
172
131
}
173
-
174
- return doc ;
175
- }
176
-
177
- public static XmlElement GetXmlElement ( string prefix , string prefixNamespace , string tag , string value = null )
178
- {
179
- XmlDocument doc = new XmlDocument ( ) ;
180
-
181
- XmlElement elem = doc . CreateElement ( prefix , tag , prefixNamespace ) ;
182
- if ( ! string . IsNullOrEmpty ( value ) )
183
- elem . InnerText = value ;
184
-
185
- return elem ;
132
+ return internalResult ;
186
133
}
187
134
}
188
135
}
0 commit comments