node-ebics-client/lib/Signer.js

101 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2018-05-17 15:03:59 +00:00
'use strict';
2018-06-01 13:16:43 +00:00
const crypto = require('crypto');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
const { DOMParser, XMLSerializer } = require('xmldom');
const select = require('xpath.js');
const C14n = require('xml-crypto/lib/c14n-canonicalization').C14nCanonicalization;
2018-05-17 15:03:59 +00:00
module.exports = class Signer {
/**
* Contructor.
*
* @param {Client} client
* @param {String} data
*/
constructor(client, data) {
/**
* The main client
*
* @type {Signer}
*/
this.client = client;
/**
* Request data - generated xml
*
* @type {...}
*/
this.doc = new DOMParser().parseFromString(data, 'text/xml');
}
_junk() {
this.digest();
this.sign();
// console.log(this.toXML());
/* const headerSet = select(this.doc, "//*[@authenticate='true']").map(x => {
// x.setAttribute('xmlns:ds', 'http://www.w3.org/2000/09/xmldsig#');
return new c14n().process(x);
}).join();
const can = headerSet.replace('xmlns="urn:org:ebics:H004"', 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
const hash = crypto.createHash('sha256');
hash.update(can);
const digester = hash.digest('base64').trim();
if ( this.doc.getElementsByTagName("ds:DigestValue")[0] )
this.doc.getElementsByTagName("ds:DigestValue")[0].textContent = digester; */
/* const nodeSet = select(this.doc, "//ds:SignedInfo");
const canonicalized = nodeSet.map(x => {
const g = x.toString();
const res = new c14n().process(x);
return res;
}).join();
const canonicalizedString = canonicalized.replace('xmlns:ds="http://www.w3.org/2000/09/xmldsig#"', 'xmlns="urn:org:ebics:H004" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"');
// const SIGN = crypto.createSign('RSA-SHA256');
// SIGN.update(canonicalizedString);
// const key = SIGN.sign(this.client.x().key.exportKey("pkcs1-private-pem"), 'base64');
const f = this.client.x().key.sign(canonicalizedString, 'base64');
if ( this.doc.getElementsByTagName("ds:SignatureValue")[0] ) {
this.doc.getElementsByTagName("ds:SignatureValue")[0].textContent = f;
} */
}
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
2018-06-01 13:16:43 +00:00
// const nodes = select(this.doc, "//*[@authenticate='true']");
2018-05-17 15:03:59 +00:00
// canonicalize the node that has authenticate='true' attribute
2018-06-01 13:16:43 +00:00
// const contentToDigest = select(this.doc, '//*[@authenticate="true"]')
2018-05-17 15:03:59 +00:00
const contentToDigest = select(this.doc, "//*[@authenticate='true']")
2018-06-01 13:16:43 +00:00
.map(x => new C14n().process(x)).join('');
2018-05-17 15:03:59 +00:00
2018-06-01 13:16:43 +00:00
console.log('digest', 'contentToDigest', contentToDigest);
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)
2018-05-17 15:03:59 +00:00
nodeDigestValue.textContent = crypto.createHash('sha256').update(fixedContent).digest('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
2018-06-01 13:16:43 +00:00
console.log('sign =>');
2018-05-17 15:03:59 +00:00
if (nodeSignatureValue) {
2018-06-01 13:16:43 +00:00
const contentToSign = (new C14n().process(select(this.doc, '//ds:SignedInfo')[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 = this.client.x().key.sign(contentToSign, 'base64');
}
}
toXML() {
return new XMLSerializer().serializeToString(this.doc);
}
};