0

I'm trying to sign xlm document using Certificate. Code pretty much looks like this:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
InputSource inputSource = new InputSource(new StringReader(xml));
inputSource.setEncoding("UTF-8");
Document document = dbf.newDocumentBuilder().parse(inputSource);
Element element = document.getDocumentElement();
DOMHelper.useIdAsXmlId(element);
FirstCertSelector firstCertSelector = new FirstCertSelector();
PassProvider passProvider = new PassProvider(cert.getCertPass());
KeyingDataProvider kdp = new FileSystemKeyStoreKeyingDataProvider("pkcs12", cert.getCertFilePath(), firstCertSelector, passProvider, passProvider, true);

DataObjectDesc dataObjectDesc = (new DataObjectReference("")).withTransform(new EnvelopedSignatureTransform());
SignedDataObjects signedDataObjects = (new SignedDataObjects()).withSignedDataObject(dataObjectDesc);
XadesSigner xadesSigner = (new XadesBesSigningProfile(kdp)).withAlgorithmsProviderEx(new SigningAlgorithm()).newSigner();
xadesSigner.sign(signedDataObjects, element);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
document.setXmlStandalone(true);

DOMSource source = new DOMSource(document);
StringWriter result = new StringWriter();
transformer.transform(source, new StreamResult(result));
String signedXML = result.toString();

Unfortunately signedXML in the signature and certificate part has some very strange characters at the end of line:

<ds:SignatureValue Id="xmldsig-some-id-bla-bla-sigvalue">
blablabla...&#13;
...
blablabla...&#13;
blabla==
</ds:SignatureValue>
<ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>
blablabla...&#13;
...
blablabla...&#13;
blablabla...&#13;
blablabla...&#13;
blablabla...&#13;
blabla==
</ds:X509Certificate>

I've no idea where from comes this strange &#13; ending. I've tried may things (like -Dfile.encoding=UTF-8) to get read of this but without success.

I'm using

<dependency>
    <groupId>com.googlecode.xades4j</groupId>
    <artifactId>xades4j</artifactId>
    <version>1.5.1</version>
</dependency>

Please help me.

Roman
  • 167
  • 2
  • 9
  • Why is it an issue ? dx:x509 mandates the use of base 64 encoding. Base 64 adds a line break at each 76 characters. Line breaks can be defined as "\r\n". is "\r" in XML entity encoding, it is each time followed by a "\n" as your sample shows. So it's all correct to me, unless XMLDSig has some extra requirements that forbid this. – GPI Sep 03 '20 at 12:35
  • According to the system that I integrate to, after decoding B64 there is coming some white character which is problematic for XSD validator. I've removed manually those characters before I send signed XML and use cases proceeds. That's the reason why I started to deal with this. – Roman Sep 03 '20 at 14:40
  • I know this is the kind of non-answer nobody likes to hear, but if the "system you integrate to" is not compliant, is it your problem to fix ? Doing some XML tuning "by hand" just to please a non compliant processor usually ends up by exploding at your face some time later. It breaks stuff. XML is hard, so each party must do its job and be compliant, nothing more. So I'm not an XML DSig expert, but from what I know about it, your output is valid, and their signature verification may not be, unless proven otherwise. – GPI Sep 03 '20 at 15:36
  • That being said : maybe this works https://stackoverflow.com/questions/4728300/how-to-produce-xml-signature-with-no-whitespaces-and-line-breaks-in-java because xades4J is Apache XML DSig based. – GPI Sep 03 '20 at 15:40
  • Unfortunately receiver of signed XML is goverment system hence I've absolutely 0 influence on it. – Roman Sep 03 '20 at 19:48
  • This is the same question as https://stackoverflow.com/questions/26293066/xades4j-and-base64-in-envelopedxmlobject . Looks like "hand manipulating" the output XML to clean it up is the only way. That being said, I did not find a definitive answer on the XSD validity of XML-encoding line breaks in a base64Binary field - but the way I read it : "However, decoding of base64Binary data in an XML entity is to be performed on the Unicode characters obtained after character encoding processing as specified by [XML 1.0 (Second Edition)]", your processing looks correct. – GPI Sep 04 '20 at 07:10
  • Please check https://github.com/luisgoncalves/xades4j/issues/189 and https://github.com/luisgoncalves/xades4j/issues/186 – lgoncalves Sep 04 '20 at 07:41

0 Answers0