Skip to content

Commit 093efa2

Browse files
danielegiallonardoDaniele Giallonardo
andauthored
Fix xml signature verification and update to .net 9.0 (#84)
* Fix xml signature verification and update to .net 9.0 * Fix action sdk version * Fix action * fix pipe * fix action * fix action * fix action * fix action * fix action * test removed --------- Co-authored-by: Daniele Giallonardo <daniele.giallonardo@hotmail.it>
1 parent c3fda19 commit 093efa2

5 files changed

Lines changed: 65 additions & 62 deletions

File tree

.github/workflows/dotnet.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ jobs:
2222
- name: Setup .NET
2323
uses: actions/setup-dotnet@v1
2424
with:
25-
dotnet-version: 8.x
25+
dotnet-version: 9.x
2626
- name: Restore dependencies
2727
run: dotnet restore
2828
working-directory: src
2929
- name: Build
3030
run: dotnet build --no-restore
3131
working-directory: src
32+
- name: Test
33+
run: dotnet test --no-build **/*Tests.csproj
34+
working-directory: src
3235
- name: Restore dependencies
3336
run: dotnet restore
3437
working-directory: samples/1_SimpleSPWebApp
@@ -41,7 +44,7 @@ jobs:
4144
- name: Update apt repo
4245
run: sudo apt update
4346
- name: Install dependencies
44-
run: sudo apt install -y libxml2-dev libxmlsec1-dev libxmlsec1-openssl xmlsec1 python3-pip && pip install cryptography==38.0.4
47+
run: sudo apt install -y libxml2-dev libxmlsec1-dev libxmlsec1-openssl xmlsec1 python3-pip
4548
- name: Pip list
4649
run: pip list
4750
- name: Install spid-sp-test
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<CopyRefAssembliesToPublishDirectory>false</CopyRefAssembliesToPublishDirectory>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.5" />
10-
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.3.0" />
9+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.1" />
10+
<!--<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.3.0" />-->
1111
</ItemGroup>
1212

13-
<!--<ItemGroup>
13+
<ItemGroup>
1414
<Reference Include="SPID.AspNetCore.Authentication">
15-
<HintPath>..\..\..\src\SPID.AspNetCore.Authentication\bin\Debug\net8.0\SPID.AspNetCore.Authentication.dll</HintPath>
15+
<HintPath>..\..\..\src\SPID.AspNetCore.Authentication\bin\Debug\net9.0\SPID.AspNetCore.Authentication.dll</HintPath>
1616
</Reference>
17-
</ItemGroup>-->
17+
</ItemGroup>
1818

1919
</Project>
Binary file not shown.

src/SPID.AspNetCore.Authentication/Helpers/XmlHelpers.cs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SPID.AspNetCore.Authentication.Resources;
44
using System;
55
using System.Collections.Concurrent;
6+
using System.Linq;
67
using System.Security.Cryptography;
78
using System.Security.Cryptography.X509Certificates;
89
using System.Security.Cryptography.Xml;
@@ -37,7 +38,7 @@ internal static XmlElement SignXMLDoc(XmlDocument doc,
3738

3839
try
3940
{
40-
privateKey = certificate.PrivateKey;
41+
privateKey = certificate.GetRSAPrivateKey();
4142
}
4243
catch (Exception ex)
4344
{
@@ -81,31 +82,22 @@ internal static bool VerifySignature(XmlDocument signedDocument, IdentityProvide
8182

8283
try
8384
{
84-
SignedXml signedXml = new SignedXml(signedDocument);
85+
XmlNodeList signatureNodes = signedDocument.GetElementsByTagName("Signature", SignedXml.XmlDsigNamespaceUrl);
86+
87+
if (signatureNodes.Count == 0)
88+
{
89+
return false;
90+
}
8591

8692
if (identityProvider is not null)
8793
{
88-
bool validated = false;
89-
foreach (var certificate in identityProvider.X509SigningCertificates)
90-
{
91-
var publicMetadataCert = new X509Certificate2(Convert.FromBase64String(certificate));
92-
XmlNodeList nodeList = (signedDocument.GetElementsByTagName("ds:Signature")?.Count > 1) ?
93-
signedDocument.GetElementsByTagName("ds:Signature") :
94-
(signedDocument.GetElementsByTagName("ns2:Signature")?.Count > 1) ?
95-
signedDocument.GetElementsByTagName("ns2:Signature") :
96-
signedDocument.GetElementsByTagName("Signature");
97-
signedXml.LoadXml((XmlElement)nodeList[0]);
98-
validated |= signedXml.CheckSignature(publicMetadataCert, true);
99-
}
100-
return validated;
94+
return identityProvider.X509SigningCertificates
95+
.Any(certificate => VerifyAllSignatures(signedDocument, signatureNodes, new X509Certificate2(Convert.FromBase64String(certificate))));
10196
}
10297
else
10398
{
104-
XmlNodeList nodeList = (signedDocument.GetElementsByTagName("ds:Signature")?.Count > 0) ?
105-
signedDocument.GetElementsByTagName("ds:Signature") :
106-
signedDocument.GetElementsByTagName("Signature");
107-
signedXml.LoadXml((XmlElement)nodeList[0]);
108-
return signedXml.CheckSignature();
99+
100+
return VerifyAllSignatures(signedDocument, signatureNodes);
109101
}
110102
}
111103
catch (Exception)
@@ -114,27 +106,23 @@ internal static bool VerifySignature(XmlDocument signedDocument, IdentityProvide
114106
}
115107
}
116108

117-
private static readonly ConcurrentDictionary<Type, XmlSerializer> serializers = new ConcurrentDictionary<Type, XmlSerializer>();
118-
/// <summary>
119-
/// Serializes to XML document.
120-
/// </summary>
121-
/// <param name="o">The o.</param>
122-
/// <returns></returns>
123-
public static XmlDocument SerializeToXmlDoc(this object o)
109+
private static bool VerifyAllSignatures(XmlDocument signedDocument, XmlNodeList signatureNodes, X509Certificate2? publicMetadataCert = null)
124110
{
125-
XmlDocument doc = new XmlDocument() { PreserveWhitespace = true };
126-
127-
using XmlWriter writer = doc.CreateNavigator().AppendChild();
128-
if (!serializers.ContainsKey(o.GetType()))
111+
bool internalResult = true;
112+
foreach (var signatureNode in signatureNodes)
129113
{
130-
var serializer = new XmlSerializer(o.GetType());
131-
serializers.AddOrUpdate(o.GetType(), serializer, (key, value) => serializer);
114+
SignedXml signedXml = new(signedDocument);
115+
signedXml.LoadXml((XmlElement)signatureNode);
116+
internalResult &= publicMetadataCert is null
117+
? signedXml.CheckSignature()
118+
: signedXml.CheckSignature(publicMetadataCert, true);
132119
}
133-
serializers[o.GetType()].Serialize(writer, o);
134120

135-
return doc;
121+
return internalResult;
136122
}
137123

124+
private static readonly ConcurrentDictionary<Type, XmlSerializer> serializers = new();
125+
138126
public static XmlElement SerializeInternalExtensionToXmlElement(object o, string namespacePrefix, string xmlNamespace)
139127
{
140128
XmlDocument doc = SerializeExtensionToXmlElementInternal(o, namespacePrefix, xmlNamespace);

src/SPID.AspNetCore.Authentication/SPID.AspNetCore.Authentication.csproj

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
4+
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
66
<Description>AspNetCore Remote Authenticator for SPID</Description>
77
<Authors>Daniele Giallonardo, Stefano Mostarda</Authors>
@@ -12,37 +12,49 @@
1212
<PackageProjectUrl>https://github.com/italia/spid-aspnetcore</PackageProjectUrl>
1313
<PackageIcon>spid-nuget.png</PackageIcon>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
15-
<PackageVersion>3.3.0</PackageVersion>
16-
<Version>3.3.0</Version>
17-
<AssemblyVersion>3.3.0</AssemblyVersion>
18-
<FileVersion>3.3.0</FileVersion>
19-
<InformationalVersion>3.3.0</InformationalVersion>
15+
<PackageVersion>3.4.0</PackageVersion>
16+
<Version>3.4.0</Version>
17+
<AssemblyVersion>3.4.0</AssemblyVersion>
18+
<FileVersion>3.4.0</FileVersion>
19+
<InformationalVersion>3.4.0</InformationalVersion>
2020
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2121
<PackageReadmeFile>README.md</PackageReadmeFile>
2222
<RepositoryUrl>https://github.com/italia/spid-aspnetcore</RepositoryUrl>
2323
</PropertyGroup>
2424

25-
<ItemGroup>
25+
<ItemGroup>
26+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
27+
<_Parameter1>SPID.AspNetCore.Authentication.Tests</_Parameter1>
28+
</AssemblyAttribute>
29+
</ItemGroup>
30+
31+
<ItemGroup>
2632
<FrameworkReference Include="Microsoft.AspNetCore.App" />
2733
</ItemGroup>
2834

29-
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net6.0'">
30-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
31-
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
32-
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.0" />
33-
</ItemGroup>
35+
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
36+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
37+
<PackageReference Include="System.Security.Cryptography.Xml" Version="9.0.1" />
38+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="9.0.1" />
39+
</ItemGroup>
3440

35-
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
36-
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.4" />
37-
</ItemGroup>
41+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
42+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
43+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.12" />
44+
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.2" />
45+
</ItemGroup>
3846

3947
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
40-
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.12" />
41-
</ItemGroup>
48+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
49+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.12" />
50+
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.2" />
51+
</ItemGroup>
4252

4353
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
44-
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="6.0.21" />
45-
</ItemGroup>
54+
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
55+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="6.0.21" />
56+
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.2" />
57+
</ItemGroup>
4658

4759
<ItemGroup>
4860
<None Include="..\..\README.md" Pack="true" PackagePath="\" />

0 commit comments

Comments
 (0)