|
|
@ -251,7 +251,7 @@ PayPro.prototype.x509Sign = function(key) { |
|
|
|
var trusted = [].concat(pki_data).every(function(cert) { |
|
|
|
var der = cert.toString('hex'); |
|
|
|
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE'); |
|
|
|
// var pem = DERtoPEM(der, 'CERTIFICATE');
|
|
|
|
// var pem = this._DERtoPEM(der, 'CERTIFICATE');
|
|
|
|
return !!RootCerts[pem.replace(/\s+/g, '')]; |
|
|
|
}); |
|
|
|
|
|
|
@ -281,7 +281,7 @@ PayPro.prototype.x509Verify = function() { |
|
|
|
return [].concat(pki_data).every(function(cert) { |
|
|
|
var der = cert.toString('hex'); |
|
|
|
var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex(der, 'CERTIFICATE'); |
|
|
|
// var pem = DERtoPEM(der, 'CERTIFICATE');
|
|
|
|
// var pem = this._DERtoPEM(der, 'CERTIFICATE');
|
|
|
|
|
|
|
|
if (!RootCerts[pem.replace(/\s+/g, '')]) { |
|
|
|
// throw new Error('Unstrusted certificate.');
|
|
|
@ -308,56 +308,33 @@ PayPro.prototype.sinVerify = function() { |
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
function PEMtoDER(pem) { |
|
|
|
pem = pem.replace(/^-----END [^-]+-----$/gmi, ''); |
|
|
|
var parts = pem.split(/-----BEGIN [^-]+-----/); |
|
|
|
return parts.map(function(part) { |
|
|
|
part = part.replace(/\s+/g, ''); |
|
|
|
return new Buffer(part, 'base64'); |
|
|
|
}); |
|
|
|
} |
|
|
|
PayPro.prototype._PEMtoDER = function(pem) { |
|
|
|
return this._PEMtoDERParam(pem); |
|
|
|
}; |
|
|
|
|
|
|
|
function PEMtoDERParam(pem, param) { |
|
|
|
var start = new RegExp('(?=-----BEGIN ' + param + '-----)', 'i'); |
|
|
|
var end = new RegExp('^-----END ' + param + '-----$', 'gmi'); |
|
|
|
PayPro.prototype._PEMtoDERParam = function(pem, param) { |
|
|
|
var start = new RegExp('(?=-----BEGIN ' + (param || '[^-]+') + '-----)', 'i'); |
|
|
|
var end = new RegExp('^-----END ' + (param || '[^-]+') + '-----$', 'gmi'); |
|
|
|
pem = pem.replace(end, ''); |
|
|
|
var parts = pem.split(start); |
|
|
|
return parts.map(function(part) { |
|
|
|
part = part.replace(/\s+/g, ''); |
|
|
|
var type = /-----BEGIN ([^-]+)-----/.exec(part)[1]; |
|
|
|
part = part.replace(/-----BEGIN ([^-]+)-----/g, ''); |
|
|
|
if (type !== param) return; |
|
|
|
part = part.replace(/\s+/g, ''); |
|
|
|
if (!param || type !== param) return; |
|
|
|
return new Buffer(part, 'base64'); |
|
|
|
}).filter(Boolean); |
|
|
|
} |
|
|
|
|
|
|
|
function wrapText(text, cols) { |
|
|
|
var j = 0; |
|
|
|
var part = ''; |
|
|
|
var parts = []; |
|
|
|
for (var i = 0; i < text.length; i++) { |
|
|
|
if (j === cols) { |
|
|
|
parts.push(part); |
|
|
|
j = 0; |
|
|
|
part = '' |
|
|
|
continue; |
|
|
|
} |
|
|
|
part += text[i]; |
|
|
|
j++; |
|
|
|
} |
|
|
|
var total = parts.join('').length; |
|
|
|
if (total < text.length) { |
|
|
|
parts.push(text.slice(-(text.length - total))); |
|
|
|
} |
|
|
|
return parts.join('\n'); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
function DERtoPEM(der, type) { |
|
|
|
PayPro.prototype._DERtoPEM = function(der, type) { |
|
|
|
var type = type || 'UNKNOWN'; |
|
|
|
der = der.toString('base64'); |
|
|
|
der = der.replace(/(.{64})/g, '$1\r\n'); |
|
|
|
der = der.replace(/\r\n$/, ''); |
|
|
|
return '' |
|
|
|
+ '-----BEGIN ' + type + '-----\n' |
|
|
|
+ wrapText(der.toString('base64'), 64) + '\n' |
|
|
|
+ '-----END ' + type + '-----\n'; |
|
|
|
} |
|
|
|
+ '-----BEGIN ' + type + '-----\r\n' |
|
|
|
+ der |
|
|
|
+ '\r\n-----END ' + type + '-----\r\n'; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = PayPro; |
|
|
|