node-ebics-client/lib/Signer.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-05-17 15:03:59 +00:00
'use strict';
// const crypto = require('crypto');
const Crypto = require('./crypto/Crypto');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
const { DOMParser, XMLSerializer } = require('xmldom');
2018-06-04 12:30:33 +00:00
const xpath = require('xpath');
2018-06-01 13:16:43 +00:00
const C14n = require('xml-crypto/lib/c14n-canonicalization').C14nCanonicalization;
2018-05-17 15:03:59 +00:00
module.exports = class Signer {
/**
* Contructor.
*
* @param {Keys} keys
2018-05-17 15:03:59 +00:00
* @param {String} data
*/
constructor(keys, data) {
2018-05-17 15:03:59 +00:00
/**
* Keys to operate with
2018-05-17 15:03:59 +00:00
*
* @type {Keys}
2018-05-17 15:03:59 +00:00
*/
this.keys = keys;
2018-05-17 15:03:59 +00:00
/**
* Request data - generated xml
*
* @type {String}
2018-05-17 15:03:59 +00:00
*/
this.doc = new DOMParser().parseFromString(data, 'text/xml');
}
digest() {
// get the xml node, where the digested value is supposed to be
2018-06-01 13:16:43 +00:00
const nodeDigestValue = this.doc.getElementsByTagName('ds:DigestValue')[0];
2018-05-17 15:03:59 +00:00
// canonicalize the node that has authenticate='true' attribute
2018-06-04 12:30:33 +00:00
const contentToDigest = xpath.select("//*[@authenticate='true']", this.doc)
2018-06-01 13:16:43 +00:00
.map(x => new C14n().process(x)).join('');
2018-05-17 15:03:59 +00:00
// fix the canonicalization
const fixedContent = contentToDigest.replace(/xmlns="urn:org:ebics:H004"/g, 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
2018-06-01 13:16:43 +00:00
if (nodeDigestValue)
nodeDigestValue.textContent = Crypto.digestWithHash(fixedContent).toString('base64').trim();
2018-06-01 13:16:43 +00:00
}
2018-05-17 15:03:59 +00:00
sign() {
2018-06-01 13:16:43 +00:00
const nodeSignatureValue = this.doc.getElementsByTagName('ds:SignatureValue')[0];
2018-05-17 15:03:59 +00:00
if (nodeSignatureValue) {
2018-06-04 12:30:33 +00:00
const select = xpath.useNamespaces({ ds: 'http://www.w3.org/2000/09/xmldsig#' });
const contentToSign = (new C14n().process(select('//ds:SignedInfo', this.doc)[0])).replace('xmlns:ds="http://www.w3.org/2000/09/xmldsig#"', 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
2018-05-17 15:03:59 +00:00
nodeSignatureValue.textContent = Crypto.privateSign(this.keys.x(), contentToSign); // this.keys.x().key.sign(contentToSign, 'base64');
2018-05-17 15:03:59 +00:00
}
}
toXML() {
return new XMLSerializer().serializeToString(this.doc);
}
};