The import of supplier e-invoices needs a reading layer that turns a received file into plain records, whatever form the file arrives in. - Fiscal.Abstractions: IIncomingInvoiceReader, an optional pack feature, with the IncomingInvoiceFile records and the incoming.* error codes the import service and screens build on. - Infrastructure: CmsSignedData, a minimal CMS SignedData (CAdES .p7m) reader on System.Formats.Asn1, since System.Security.Cryptography.Pkcs is a separate package. It accepts DER, BER with indefinite lengths and chunked content, base64 text and nested envelopes; it checks the message digest and the RSA (PKCS#1, PSS) or ECDSA signature with the embedded certificate and reports signer, tax code, validity and signing time. No trust chain or revocation check: no trusted list offline. - Fiscal.Italy: FatturaPaReader for FPR12/FPA12 v1.2.x and FSM10, matched by local name, DTD prohibited and no resolver, BOM/windows-1252/missing declaration tolerated, batches as one invoice per body, attachments decoded; registered in ItalyFiscalPack. - Tests build every sample and signed envelope in code (AsnWriter and self-signed certificates), nothing downloaded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
6.9 KiB
C#
168 lines
6.9 KiB
C#
using System.Text;
|
|
using Khaemhat.Infrastructure.Security;
|
|
|
|
namespace Khaemhat.Infrastructure.Tests.Security;
|
|
|
|
/// <summary>The reader of CMS SignedData envelopes (.p7m) used for received e-invoices (issue #57).</summary>
|
|
[TestClass]
|
|
public sealed class CmsSignedDataTests
|
|
{
|
|
private static readonly byte[] Content = Encoding.UTF8.GetBytes("<?xml version=\"1.0\"?><FatturaElettronica>contenuto firmato</FatturaElettronica>");
|
|
|
|
[TestMethod]
|
|
public void Read_RsaSignature_ReturnsTheContentVerified_WithTheSigner()
|
|
{
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa);
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
CollectionAssert.AreEqual(Content, result.Content);
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
Assert.IsNull(result.Problem);
|
|
Assert.AreEqual(1, result.Layers);
|
|
var signer = result.Signer!;
|
|
Assert.AreEqual("Mario Rossi", signer.Name);
|
|
Assert.AreEqual("RSSMRA80A01H501U", signer.TaxCode);
|
|
Assert.AreEqual(CmsTestSigner.SigningTime, signer.SigningTime);
|
|
Assert.AreEqual(new DateTimeOffset(2025, 1, 1, 0, 0, 0, TimeSpan.Zero), signer.CertificateNotBefore);
|
|
Assert.AreEqual(new DateTimeOffset(2028, 1, 1, 0, 0, 0, TimeSpan.Zero), signer.CertificateNotAfter);
|
|
CollectionAssert.AreEqual(CmsTestSigner.Rsa.RawData, signer.Certificate);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_EcdsaSignature_BySubjectKeyIdentifier_IsVerified()
|
|
{
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Ecdsa, new CmsTestOptions { BySubjectKeyIdentifier = true });
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
Assert.AreEqual("RSSMRA80A01H501U", result.Signer!.TaxCode);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_WithoutSignedAttributes_VerifiesTheSignatureOverTheContent()
|
|
{
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa, new CmsTestOptions { SignedAttributes = false });
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
Assert.IsNull(result.Signer!.SigningTime);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_TamperedContent_ReturnsItNotVerified_WithTheProblem()
|
|
{
|
|
var tampered = Encoding.UTF8.GetBytes("<?xml version=\"1.0\"?><FatturaElettronica>contenuto alterato</FatturaElettronica>");
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa, new CmsTestOptions { TamperedContent = tampered });
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
CollectionAssert.AreEqual(tampered, result.Content);
|
|
Assert.IsFalse(result.Verified);
|
|
StringAssert.Contains(result.Problem, "changed after signing");
|
|
Assert.AreEqual("Mario Rossi", result.Signer!.Name);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_BrokenSignature_IsNotVerified()
|
|
{
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa, new CmsTestOptions { BreakSignature = true });
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
Assert.IsFalse(result.Verified);
|
|
StringAssert.Contains(result.Problem, "does not match");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_BerWithIndefiniteLengthsAndChunkedContent_IsVerified()
|
|
{
|
|
var large = Encoding.UTF8.GetBytes(string.Concat(Enumerable.Repeat("<Linea>Bulloni M8 zincati</Linea>", 40)));
|
|
var envelope = CmsTestSigner.Sign(large, CmsTestSigner.Rsa, new CmsTestOptions { Ber = true });
|
|
Assert.AreEqual(0x80, envelope[1], "The outer SEQUENCE must have an indefinite length.");
|
|
|
|
var result = CmsSignedData.Read(envelope);
|
|
|
|
CollectionAssert.AreEqual(large, result.Content);
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_Base64TextForm_WithLineBreaksAndArmour_IsVerified()
|
|
{
|
|
var envelope = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa);
|
|
var plain = Encoding.ASCII.GetBytes(Convert.ToBase64String(envelope));
|
|
var wrapped = Encoding.ASCII.GetBytes(
|
|
"-----BEGIN PKCS7-----\r\n" + Convert.ToBase64String(envelope, Base64FormattingOptions.InsertLineBreaks) + "\r\n-----END PKCS7-----\r\n");
|
|
|
|
Assert.IsTrue(CmsSignedData.LooksLikeCms(plain));
|
|
Assert.IsTrue(CmsSignedData.LooksLikeCms(wrapped));
|
|
foreach (var text in new[] { plain, wrapped })
|
|
{
|
|
var result = CmsSignedData.Read(text);
|
|
CollectionAssert.AreEqual(Content, result.Content);
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_NestedEnvelopes_AreOpenedUntilTheContent()
|
|
{
|
|
var inner = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa);
|
|
var outer = CmsTestSigner.Sign(inner, CmsTestSigner.Ecdsa, new CmsTestOptions { Ber = true });
|
|
|
|
var result = CmsSignedData.Read(outer);
|
|
|
|
CollectionAssert.AreEqual(Content, result.Content);
|
|
Assert.AreEqual(2, result.Layers);
|
|
Assert.HasCount(2, result.Signers);
|
|
Assert.IsTrue(result.Verified, result.Problem);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_NestedEnvelope_WithABrokenInnerSignature_IsNotVerified()
|
|
{
|
|
var inner = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa, new CmsTestOptions { BreakSignature = true });
|
|
var outer = CmsTestSigner.Sign(inner, CmsTestSigner.Rsa);
|
|
|
|
var result = CmsSignedData.Read(outer);
|
|
|
|
Assert.IsTrue(result.Signers[0].Verified);
|
|
Assert.IsFalse(result.Signers[1].Verified);
|
|
Assert.IsFalse(result.Verified);
|
|
Assert.IsNotNull(result.Problem);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Read_Garbage_Throws()
|
|
{
|
|
Assert.ThrowsExactly<CmsFormatException>(() => CmsSignedData.Read(Encoding.UTF8.GetBytes("questo non è un file firmato")));
|
|
Assert.ThrowsExactly<CmsFormatException>(() => CmsSignedData.Read([]));
|
|
Assert.ThrowsExactly<CmsFormatException>(() => CmsSignedData.Read(Content));
|
|
|
|
// The right start and nothing after it.
|
|
var truncated = CmsTestSigner.Sign(Content, CmsTestSigner.Rsa)[..40];
|
|
Assert.IsTrue(CmsSignedData.LooksLikeCms(truncated));
|
|
Assert.ThrowsExactly<CmsFormatException>(() => CmsSignedData.Read(truncated));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void LooksLikeCms_SniffsTheContent()
|
|
{
|
|
Assert.IsTrue(CmsSignedData.LooksLikeCms(CmsTestSigner.Sign(Content, CmsTestSigner.Rsa)));
|
|
Assert.IsTrue(CmsSignedData.LooksLikeCms(CmsTestSigner.Sign(Content, CmsTestSigner.Rsa, new CmsTestOptions { Ber = true })));
|
|
Assert.IsFalse(CmsSignedData.LooksLikeCms(Content));
|
|
Assert.IsFalse(CmsSignedData.LooksLikeCms("QUJDRA=="u8));
|
|
Assert.IsFalse(CmsSignedData.LooksLikeCms([]));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DecodeBase64Text_RejectsWhatIsNotBase64()
|
|
{
|
|
Assert.IsNull(CmsSignedData.DecodeBase64Text("non è base64!"u8));
|
|
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, CmsSignedData.DecodeBase64Text("AQID\r\n"u8));
|
|
}
|
|
}
|