node-ebics-client/lib/orders/H004/signer.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
2018-06-15 06:33:41 +00:00
// const crypto = require('crypto');
const Crypto = require('../../crypto/Crypto');
2018-06-15 06:33:41 +00:00
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom');
const xpath = require('xpath');
const C14n = require('xml-crypto/lib/c14n-canonicalization').C14nCanonicalization;
2018-06-15 06:33:41 +00:00
const digest = (doc) => {
// get the xml node, where the digested value is supposed to be
const nodeDigestValue = doc.getElementsByTagName('ds:DigestValue')[0];
2018-06-15 06:33:41 +00:00
// canonicalize the node that has authenticate='true' attribute
2023-10-11 18:58:26 +00:00
const contentToDigest = xpath
.select("//*[@authenticate='true']", doc)
.map(x => new C14n().process(x))
.join('');
2018-06-15 06:33:41 +00:00
// fix the canonicalization
2023-10-11 18:58:26 +00:00
const fixedContent = contentToDigest.replace(
/xmlns="urn:org:ebics:H004"/g,
'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"',
2023-10-11 18:58:26 +00:00
);
2018-06-15 06:33:41 +00:00
if (nodeDigestValue)
2023-10-11 18:58:26 +00:00
nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent)
.toString('base64')
2023-10-11 18:58:26 +00:00
.trim();
2018-06-15 06:33:41 +00:00
return doc;
};
const sign = (doc, key) => {
const nodeSignatureValue = doc.getElementsByTagName('ds:SignatureValue')[0];
2018-06-15 06:33:41 +00:00
if (nodeSignatureValue) {
2023-10-11 18:58:26 +00:00
const select = xpath.useNamespaces({
ds: 'http://www.w3.org/2000/09/xmldsig#',
2023-10-11 18:58:26 +00:00
});
const contentToSign = new C14n()
.process(select('//ds:SignedInfo', doc)[0])
2023-10-11 18:58:26 +00:00
.replace(
'xmlns:ds="http://www.w3.org/2000/09/xmldsig#"',
'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"',
2023-10-11 18:58:26 +00:00
);
2018-06-15 06:33:41 +00:00
nodeSignatureValue.textContent = Crypto.privateSign(key, contentToSign); // this.keys.x().key.sign(contentToSign, 'base64');
}
return doc;
};
const toXML = doc => new XMLSerializer().serializeToString(doc);
2018-06-15 06:33:41 +00:00
module.exports = {
2018-06-20 09:20:03 +00:00
sign(data, keyX) {
const doc = new DOMParser().parseFromString(data, 'text/xml');
2018-06-15 06:33:41 +00:00
return toXML(sign(digest(doc), keyX));
},
};