Browse Source

add xmlCertificateRetriever and xmlCertificateHandler classes

DEVSIX-9162

Autoported commit.
Original commit hash: [a2b4a8440]
Manual files:
sharpenConfiguration.xml
sign/src/main/java/com/itextpdf/signatures/XmlCertificateHandler.java
sign/src/main/java/com/itextpdf/signatures/XmlCertificateRetriever.java
pull/38/head
Alexandr Pliushchou 2 months ago
parent
commit
ffe9d751da
  1. 86
      itext.tests/itext.sign.tests/itext/signatures/XmlCertificateRetrieverTest.cs
  2. 27
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/certificate.pem
  3. 1
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/certificate.xml
  4. 0
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/emptyXml.xml
  5. 2113
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/eu-lotl-pivot-282.xml
  6. 2144
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/eu-lotl.xml
  7. 1
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/invalidCertificate.xml
  8. 3840
      itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/ttlAustria.xml
  9. 95
      itext/itext.sign/itext/signatures/XmlCertificateHandler.cs
  10. 70
      itext/itext.sign/itext/signatures/XmlCertificateRetriever.cs
  11. 4
      itext/itext.sign/itext/signatures/exceptions/SignExceptionMessageConstant.cs
  12. 2
      port-hash

86
itext.tests/itext.sign.tests/itext/signatures/XmlCertificateRetrieverTest.cs

@ -0,0 +1,86 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Utils;
using iText.Kernel.Exceptions;
using iText.Signatures.Exceptions;
using iText.Signatures.Testutils;
using iText.Test;
namespace iText.Signatures {
[NUnit.Framework.Category("UnitTest")]
public class XmlCertificateRetrieverTest : ExtendedITextTest {
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/signatures/XmlCertificateRetrieverTest/";
[NUnit.Framework.Test]
public virtual void ReadSingleCertificateTest() {
String xmlPath = SOURCE_FOLDER + "certificate.xml";
String certPath = SOURCE_FOLDER + "certificate.pem";
IX509Certificate actualCertificate = XmlCertificateRetriever.GetCertificates(xmlPath)[0];
IX509Certificate expectedCertificate = PemFileHelper.ReadFirstChain(certPath)[0];
NUnit.Framework.Assert.AreEqual(expectedCertificate, actualCertificate);
}
[NUnit.Framework.Test]
public virtual void ReadLotlCertificatesTest() {
String xmlPath = SOURCE_FOLDER + "eu-lotl.xml";
IList<IX509Certificate> certificateList = XmlCertificateRetriever.GetCertificates(xmlPath);
NUnit.Framework.Assert.AreEqual(142, certificateList.Count);
}
[NUnit.Framework.Test]
public virtual void ReadPivotCertificatesTest() {
String xmlPath = SOURCE_FOLDER + "eu-lotl-pivot-282.xml";
IList<IX509Certificate> certificateList = XmlCertificateRetriever.GetCertificates(xmlPath);
NUnit.Framework.Assert.AreEqual(126, certificateList.Count);
}
[NUnit.Framework.Test]
public virtual void ReadAustriaCertificatesTest() {
String xmlPath = SOURCE_FOLDER + "ttlAustria.xml";
IList<IX509Certificate> certificateList = XmlCertificateRetriever.GetCertificates(xmlPath);
NUnit.Framework.Assert.AreEqual(103, certificateList.Count);
}
[NUnit.Framework.Test]
public virtual void EmptyXmlTest() {
String xmlPath = SOURCE_FOLDER + "emptyXml.xml";
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => XmlCertificateRetriever.GetCertificates
(xmlPath));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(SignExceptionMessageConstant.FAILED_TO_READ_CERTIFICATE_BYTES_FROM_XML
, xmlPath), exception.Message);
}
[NUnit.Framework.Test]
public virtual void InvalidCertificateTest() {
String xmlPath = SOURCE_FOLDER + "invalidCertificate.xml";
Exception exception = NUnit.Framework.Assert.Catch(typeof(PdfException), () => XmlCertificateRetriever.GetCertificates
(xmlPath));
NUnit.Framework.Assert.AreEqual(SignExceptionMessageConstant.FAILED_TO_RETRIEVE_CERTIFICATE, exception.Message
);
}
}
}

27
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/certificate.pem

@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE-----
MIIEfjCCA2agAwIBAgIEWOn8GzANBgkqhkiG9w0BAQsFADBUMQswCQYDVQQGEwJC
WTEOMAwGA1UEBwwFTWluc2sxDjAMBgNVBAoMBWlUZXh0MQ0wCwYDVQQLDAR0ZXN0
MRYwFAYDVQQDDA1pVGV4dFRlc3RSb290MCAXDTE3MDQwOTA5MTgyM1oYDzIxMTcw
NDA5MDkxODIzWjBZMQswCQYDVQQGEwJCWTEOMAwGA1UEBwwFTWluc2sxDjAMBgNV
BAoMBWlUZXh0MQ0wCwYDVQQLDAR0ZXN0MRswGQYDVQQDDBJpVGV4dFRlc3REc2FD
ZXJ0MDEwggG3MIIBLAYHKoZIzjgEATCCAR8CgYEA/X9TgR11EilS30qcLuzk5/YR
t1I870QAwx4/gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZ
UKWkn5/oBHsQIsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOu
K2HX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wJCWTEOMAwGA1UEBwwFTWluc2sxDjAMBgNVBAoMBWlUZXh0MQ0wCwYDVQQLDAR0ZXN0MRYwFAYDVQQDDA1pVGV4dFRlc3RSb290ggRY55HWMB0GA1UdDgQWBBRDh4HV9SQZBJG4y4OtR/PNr+UkfTALBgNVHQ8EBAMCBsAwDQYJKoZIhvcNAQELBQADggEBAE0BeajWwDJM6m3s0sGI+3bxjkxzrMcK+S9L0Te1ynQLvISKwxjFwLeILX6wa1WonMyVatRDGiVURH9d5arXbTBquh1xcHc6yEUAh1wSDb1I4I0nAffZgaBzKQFKBEoup8yLxz6IClLq2oDhKLYBHa6oGfY0ijUHcXfw7ad0xocCML5KUw9rHGty+anS7X8y4cxf+77LaarwuLzMGuCcs43v99NkRd/6o/rEXMnaYrv7RIaI7QLnUcvR4g5MPAtpyyHnYz8JrbRvE25IUx1V1YOWR6hZg3PcSKRaF27a+X15piE69Zu+Dhged62tds+n04CgQqpOKTPQ+Q18Kob4fTw=</X509Certificate>

0
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/emptyXml.xml

2113
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/eu-lotl-pivot-282.xml
File diff suppressed because it is too large
View File

2144
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/eu-lotl.xml
File diff suppressed because it is too large
View File

1
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/invalidCertificate.xml

@ -0,0 +1 @@
<X509Certificate>MIIIBDCCBeygAwIBAgIUKH9cdKE3vD5rBCmH3Krsppm6rkUwDQYJKoZIhvcNAQENBQAwXzELMAkGA1UEBhMCUFQxKjAoBgNVBAoMIURpZ2l0YWxTaWduIENlcnRpZmljYWRvcmEgRGlnaXRhbDEkMCIGA1UEAwwbRElHSVRBTFNJR04gUVVBTElGSUVEIENBIEcxMB4XDTIzMDIyMjE1MzYyOVoXDTI2MDIyMTE1MzYyOVowggFWMQswCQYDVQQG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h3LoQrUvkvj2uNLoWX15WMt37KRVgiAdOkVFFIsQRvTJM9OYBI/4Rmc1g3TD4O0TBlqzjoMnLGD8d3nhtKsfb29gW7Pj9lGZLI4ObIzeBZwQZKK5U8LAh38mzlUJ2UGYC1FHbyNKLn9L6bVn0F7OdelRiNrnby9TiKwl8gwZUAC5bxOv/5dfswUFN1kQ2LTiRMM+8G/1IDe1CAuN4H2W+0hAvkBy2KIAzcxa6nCOilHzu1BikfV7x4qojYoFAXI+tKe8JtIuJIFR+gWb7T3ymRFGsZMDoabXIRvnvrks/KRqhi8/6YmCDpx4jDTxQf3GOVC2AXULtNtV1i28md5isoH3GHgqPeKYdNxOMJN7</X509Certificate>

3840
itext.tests/itext.sign.tests/resources/itext/signatures/XmlCertificateRetrieverTest/ttlAustria.xml
File diff suppressed because it is too large
View File

95
itext/itext.sign/itext/signatures/XmlCertificateHandler.cs

@ -0,0 +1,95 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace iText.Signatures {
internal class XmlCertificateHandler {
private const String CERTIFICATE_TAG = "X509Certificate";
private const String SIGNATURE_CERTIFICATE_TAG = "ds:X509Certificate";
private bool isReadingCertificate = false;
private StringBuilder certificateByteBuilder;
internal IList<byte[]> certificateBytes = new List<byte[]>();
internal XmlCertificateHandler() {
//empty constructor
}
public void ReadXml(System.IO.Stream stream)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = true;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
StartElement(reader.Name);
break;
case XmlNodeType.Text:
Characters(reader.GetValueAsync().Result);
break;
case XmlNodeType.EndElement:
EndElement(reader.Name);
break;
default:
break;
}
}
}
}
private void StartElement(String qName) {
if (CERTIFICATE_TAG.Equals(qName) || SIGNATURE_CERTIFICATE_TAG.Equals(qName)) {
isReadingCertificate = true;
certificateByteBuilder = new StringBuilder();
}
}
private void EndElement(String qName) {
if (CERTIFICATE_TAG.Equals(qName) || SIGNATURE_CERTIFICATE_TAG.Equals(qName)) {
certificateBytes.Add(Convert.FromBase64String(certificateByteBuilder.ToString()));
}
}
private void Characters(String value) {
if (isReadingCertificate) {
certificateByteBuilder.Append(value);
}
}
public virtual IList<byte[]> GetCertificatesBytes() {
return certificateBytes;
}
}
}

70
itext/itext.sign/itext/signatures/XmlCertificateRetriever.cs

@ -0,0 +1,70 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Bouncycastleconnector;
using iText.Commons.Bouncycastle;
using iText.Commons.Bouncycastle.Cert;
using iText.Commons.Utils;
using iText.Kernel.Exceptions;
using iText.Signatures.Exceptions;
using iText.Commons.Bouncycastle.Cert;
using Org.BouncyCastle.Security.Certificates;
using System.Xml;
namespace iText.Signatures {
internal class XmlCertificateRetriever {
private static readonly IBouncyCastleFactory BOUNCY_CASTLE_FACTORY = BouncyCastleFactoryCreator.GetFactory();
internal XmlCertificateRetriever() {
}
internal static IList<IX509Certificate> GetCertificates(String path) {
XmlCertificateHandler handler = new XmlCertificateHandler();
XmlCertificateHandler certificateHandler = new XmlCertificateHandler();
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
try {
handler.ReadXml(stream);
}
catch (XmlException e) {
throw new PdfException(MessageFormatUtil.Format(SignExceptionMessageConstant.FAILED_TO_READ_CERTIFICATE_BYTES_FROM_XML
, path), e);
}
IList<byte[]> certificateBytes = handler.GetCertificatesBytes();
IList<IX509Certificate> certificates = new List<IX509Certificate>();
foreach (byte[] certificateByte in certificateBytes) {
try {
IX509Certificate certificate = BOUNCY_CASTLE_FACTORY.CreateX509Certificate(certificateByte);
certificates.Add(certificate);
}
catch (CertificateException e) {
throw new PdfException(SignExceptionMessageConstant.FAILED_TO_RETRIEVE_CERTIFICATE, e);
}
}
return certificates;
}
}
}

4
itext/itext.sign/itext/signatures/exceptions/SignExceptionMessageConstant.cs

@ -151,6 +151,10 @@ namespace iText.Signatures.Exceptions {
public const String CMS_MISSING_CERTIFICATES = "The certificate set must at least contains the signer certificate";
public const String FAILED_TO_READ_CERTIFICATE_BYTES_FROM_XML = "Failed to read certificate binary data from {0}.";
public const String FAILED_TO_RETRIEVE_CERTIFICATE = "Failed to retrieve certificates from binary data";
private SignExceptionMessageConstant() {
}
// Private constructor will prevent the instantiation of this class directly

2
port-hash

@ -1 +1 @@
5c5f3337e0b84adedca2d2580201966cfdf7169f
945137b8aff3b10a5bc1b12b6c734e863f72c256
Loading…
Cancel
Save