bitcore/lib/browser/PayPro.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2014-07-16 09:03:51 -07:00
"use strict";
var Key = require('./Key');
2014-07-21 13:53:01 -07:00
var KJUR = require('jsrsasign');
2014-07-16 09:03:51 -07:00
var assert = require('assert');
var PayPro = require('../common/PayPro');
2014-07-21 13:52:09 -07:00
var RootCerts = require('../common/RootCerts');
2014-07-16 16:38:00 -07:00
PayPro.prototype.x509Sign = function(key) {
var crypto = require('crypto');
2014-07-16 09:03:51 -07:00
var pki_type = this.get('pki_type');
var pki_data = this.get('pki_data'); // contains one or more x509 certs
var type = pki_type.split('+')[1].toUpperCase();
var buf = this.serializeForSig();
var trusted = [].concat(pki_data).every(function(cert) {
var der = cert.toString('hex');
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE');
return RootCerts.isTrusted(pem);
});
if (!trusted) {
// XXX Figure out what to do here
// throw new Error('Unstrusted certificate.');
}
2014-07-16 09:03:51 -07:00
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
jsrsaSig.initSign(key);
jsrsaSig.updateHex(buf.toString('hex'));
2014-07-16 09:03:51 -07:00
var sig = new Buffer(jsrsasig.sign(), 'hex');
//var sig = new Buffer(new Uint8Array(jsrsasig.sign()), 'hex');
return sig;
2014-07-16 09:03:51 -07:00
};
PayPro.prototype.x509Verify = function(key) {
var sig = this.get('signature');
2014-07-16 09:03:51 -07:00
var pki_type = this.get('pki_type');
var pki_data = this.get('pki_data');
var buf = this.serializeForSig();
var type = pki_type.split('+')[1].toUpperCase();
2014-07-16 09:03:51 -07:00
var jsrsaSig = new KJUR.crypto.Signature({
alg: type + 'withRSA',
prov: 'cryptojs/jsrsa'
});
return [].concat(pki_data).every(function(cert) {
var der = cert.toString('hex');
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE');
if (!RootCerts.isTrusted(pem)) {
// XXX Figure out what to do here
// throw new Error('Unstrusted certificate.');
}
jsrsaSig.initVerifyByCertificatePEM(pem);
jsrsaSig.updateHex(buf.toString('hex'));
2014-07-16 09:03:51 -07:00
return jsrsaSig.verify(sig.toString('hex'));
});
2014-07-16 09:03:51 -07:00
};
module.exports = PayPro;