You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

74 lines
2.1 KiB

"use strict";
var Key = require('./Key');
var KJUR = require('./x509');
var assert = require('assert');
var PayPro = require('../PayPro');
PayPro.sign = function(key) {
if (this.messageType !== 'PaymentRequest')
throw new Error('Signing can only be performed on a PaymentRequest');
var pki_type = this.get('pki_type');
if (pki_type === 'SIN') {
var sig = this.sinSign(key);
} else if (pki_type === 'none' || pki_type === 'x509+sha256' || pki_type === 'x509+sha1') {
throw new Error('x509 currently unsuported.');
} else if (pki_type === 'x509+sha1' || pki_type === 'x509+sha256') {
var crypto = require('crypto');
var pki_data = this.get('pki_data'); // contains one or more x509 certs
var type = pki_type.split('+').toUpperCase();
var buf = this.serializeForSig();
var jsrsaSig = KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
jsrsaSig.initSign(pki_data);
jsrsaSig.updateString(buf.toString());
var sig = new Buffer(jsrsasig.sign(), 'hex');
} else if (pki_type === 'none') {
return this;
} else {
throw new Error('Unsupported pki_type');
}
this.set('signature', sig);
return this;
};
PayPro.verify = function() {
if (this.messageType !== 'PaymentRequest')
throw new Error('Verifying can only be performed on a PaymentRequest');
var pki_type = this.get('pki_type');
if (pki_type === 'SIN') {
return this.sinVerify();
} else if (pki_type === 'x509+sha1' || pki_type === 'x509+sha256') {
var sig = this.get('signature');
var pki_data = this.get('pki_data');
var buf = this.serializeForSig();
var type = pki_type.split('+').toUpperCase();
var jsrsaSig = KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
jsrsaSig.initVerifyByCertificatePEM(pki_data);
jsrsaSig.updateString(buf.toString());
var result = jsrsaSig.verify(sig.toString(16)); // should be hex
return result;
} else if (pki_type === 'none') {
return true;
}
throw new Error('Unsupported pki_type');
};
module.exports = Point;