bitcore/lib/browser/PayPro.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-07-16 09:03:51 -07:00
"use strict";
var Key = require('./Key');
var KJUR = require('./x509');
2014-07-16 09:03:51 -07:00
var assert = require('assert');
var PayPro = require('../PayPro');
2014-07-21 13:52:09 -07:00
var RootCerts = require('../common/RootCerts');
2014-07-16 16:38:00 -07:00
2014-07-16 09:03:51 -07:00
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);
2014-07-16 16:38:00 -07:00
} else if (pki_type === 'x509+sha256' || pki_type === 'x509+sha1') {
2014-07-16 09:03:51 -07:00
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
2014-07-16 18:10:23 -07:00
var type = pki_type.split('+')[1].toUpperCase();
2014-07-16 09:03:51 -07:00
var buf = this.serializeForSig();
2014-07-16 16:38:00 -07:00
2014-07-17 17:15:25 -07:00
var trusted = [].concat(pki_data).every(function(cert) {
var der = cert.toString('hex');
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE');
return !!RootCerts[pem.replace(/\s+/g, '')];
2014-07-17 17:15:25 -07:00
});
2014-07-16 16:38:00 -07:00
2014-07-17 17:15:25 -07:00
if (!trusted) {
// throw new Error('Unstrusted certificate.');
}
2014-07-16 15:08:02 -07:00
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
jsrsaSig.initSign(key);
2014-07-16 15:08:02 -07:00
jsrsaSig.updateHex(buf.toString('hex'));
var sig = new Buffer(jsrsasig.sign(), 'hex');
2014-07-16 09:03:51 -07:00
} 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();
2014-07-16 18:10:23 -07:00
var type = pki_type.split('+')[1].toUpperCase();
2014-07-16 15:08:02 -07:00
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
2014-07-17 17:15:25 -07:00
return [].concat(pki_data).every(function(cert) {
var der = cert.toString('hex');
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE');
if (!RootCerts[pem.replace(/\s+/g, '')]) {
2014-07-17 17:15:25 -07:00
// throw new Error('Unstrusted certificate.');
}
2014-07-17 17:15:25 -07:00
jsrsaSig.initVerifyByCertificatePEM(pem);
2014-07-17 17:15:25 -07:00
jsrsaSig.updateHex(buf.toString('hex'));
2014-07-17 17:15:25 -07:00
return jsrsaSig.verify(sig.toString('hex'));
});
2014-07-16 09:03:51 -07:00
} else if (pki_type === 'none') {
return true;
}
throw new Error('Unsupported pki_type');
};
module.exports = Point;