Browse Source

paypro: move tbs parsing into common.

patch-2
Christopher Jeffrey 11 years ago
parent
commit
a39aeeb446
  1. 45
      lib/PayPro.js
  2. 41
      lib/common/PayPro.js

45
lib/PayPro.js

@ -134,47 +134,12 @@ PayPro.prototype.x509Verify = function() {
}); });
// //
// Verify current certificate signature // Verify current Certificate signature
// //
// Grab the raw DER To-Be-Signed Certificate to verify: // Get the raw DER TBSCertificate
// First 10 bytes usually look like: // from the DER Certificate:
// [ 48, 130, 5, 32, 48, 130, 4, 8, 160, 3 ] var tbs = PayPro.getTBSCertificate(data);
var start = 0;
var starts = 0;
for (var start = 0; start < data.length; start++) {
if (starts === 1 && data[start] === 48) {
break;
}
if (starts < 1 && data[start] === 48) {
starts++;
}
}
// The bytes *after* the TBS (including the last TBS byte) will look like
// (note the 48 - the start of the sig, and the 122 - the end of the TBS):
// [ 122, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 11, 5, 0, 3, ... ]
// The certificate in these examples has a `start` of 4, and an `end` of
// 1040. The 4 bytes is the DER SEQ of the Certificate, right before the
// SEQ of the TBSCertificate.
var end = 0;
var ends = 0;
for (var end = data.length - 1; end > 0; end--) {
if (ends === 2 && data[end] === 48) {
break;
}
if (ends < 2 && data[end] === 0) {
ends++;
}
}
console.log(Array.prototype.slice.call(data.slice(end - 1)));
console.log(Array.prototype.slice.call(data.slice(0, start + 6)));
console.log('start=%d, end=%d', start, end);
var tbs = data.slice(start, end);
var verifier = crypto.createVerify('RSA-' + sigAlg); var verifier = crypto.createVerify('RSA-' + sigAlg);
verifier.update(tbs); verifier.update(tbs);
@ -185,8 +150,6 @@ PayPro.prototype.x509Verify = function() {
&& sigVerified; && sigVerified;
}); });
console.log('verified && chainVerified:', verified && chainVerified);
return verified && chainVerified; return verified && chainVerified;
}; };

41
lib/common/PayPro.js

@ -52,6 +52,47 @@ PayPro.getAlgorithm = function(value, index) {
return value; return value;
}; };
// Grab the raw DER To-Be-Signed Certificate
// from a DER Certificate to verify
PayPro.getTBSCertificate = function(data) {
// We start by slicing off the first SEQ of the
// Certificate (TBSCertificate is its own SEQ).
// The first 10 bytes usually look like:
// [ 48, 130, 5, 32, 48, 130, 4, 8, 160, 3 ]
var start = 0;
var starts = 0;
for (var start = 0; start < data.length; start++) {
if (starts === 1 && data[start] === 48) {
break;
}
if (starts < 1 && data[start] === 48) {
starts++;
}
}
// The bytes *after* the TBS (including the last TBS byte) will look like
// (note the 48 - the start of the sig, and the 122 - the end of the TBS):
// [ 122, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 11, 5, 0, 3, ... ]
// The certificate in these examples has a `start` of 4, and an `end` of
// 1040. The 4 bytes is the DER SEQ of the Certificate, right before the
// SEQ of the TBSCertificate.
var end = 0;
var ends = 0;
for (var end = data.length - 1; end > 0; end--) {
if (ends === 2 && data[end] === 48) {
break;
}
if (ends < 2 && data[end] === 0) {
ends++;
}
}
// Return our raw DER TBSCertificate:
return data.slice(start, end);
};
PayPro.RootCerts = RootCerts; PayPro.RootCerts = RootCerts;
PayPro.proto = {}; PayPro.proto = {};

Loading…
Cancel
Save