1+ require ( 'promise-polyfill' ) ; // Needed since we're supporting Node 0.10 environments
2+
13var x509 = require ( './build/Release/x509' ) ;
24var fs = require ( 'fs' ) ;
35
@@ -6,87 +8,36 @@ exports.getAltNames = x509.getAltNames;
68exports . getSubject = x509 . getSubject ;
79exports . getIssuer = x509 . getIssuer ;
810
9- function x509_verify ( certBuffer , caBuffer , cb ) {
10- try {
11- x509 . parseCert ( String ( certBuffer ) ) ;
12- }
13- catch ( Exception ) {
14- return cb ( new TypeError ( 'Unable to parse certificate.' ) ) ;
15- }
16-
17- try {
18- x509 . verify ( certBuffer , caBuffer ) ;
19- cb ( null ) ;
20- }
21- catch ( verificationError ) {
22- cb ( verificationError ) ;
23- }
24- }
25-
26- function x509_verify_cert_buffer_ca_buffer ( certBuffer , caBuffer , cb ) {
27- x509_verify ( certBuffer , caBuffer , cb ) ;
28- }
29- function x509_verify_cert_buffer_ca_path ( certBuffer , caPath , cb ) {
30- fs . stat ( caPath , function ( err ) {
31- if ( err ) {
32- return cb ( err ) ;
33- }
34- x509_verify ( certBuffer , fs . readFileSync ( caPath ) , cb ) ;
35- } )
36-
37- }
38- function x509_verify_cert_path_ca_buffer ( certPath , caBuffer , cb ) {
39- fs . stat ( certPath , function ( err ) {
40- if ( err ) {
41- return cb ( err ) ;
42- }
43- x509_verify ( fs . readFileSync ( certPath ) , caBuffer , cb ) ;
44- } )
45- }
46- function x509_verify_cert_path_ca_path ( certPath , caPath , cb ) {
47- fs . stat ( certPath , function ( err ) {
48- if ( err ) {
49- return cb ( err ) ;
50- }
51- fs . stat ( caPath , function ( err ) {
52- if ( err ) {
53- return cb ( err ) ;
54- }
55- x509_verify ( fs . readFileSync ( certPath ) , fs . readFileSync ( caPath ) , cb ) ;
56- } )
57- } )
58- }
59-
6011exports . verify = function ( certPathOrString , CABundlePathOrString , cb ) {
61- if ( ! String . prototype . startsWith ) {
62- String . prototype . startsWith = function ( searchString , position ) {
63- position = position || 0 ;
64- return this . substr ( position , searchString . length ) === searchString ;
65- } ;
66- }
6712 if ( ! certPathOrString ) {
68- throw new TypeError ( 'Certificate path is required' ) ;
13+ throw new TypeError ( 'The certificate path or the certificate string itself is required' ) ;
6914 }
7015 if ( ! CABundlePathOrString ) {
71- throw new TypeError ( 'CA Bundle path is required' ) ;
16+ throw new TypeError ( 'The certificate bundle path or the bundle string itself is required' ) ;
7217 }
7318
74- if ( String ( certPathOrString ) . startsWith ( '-----BEGIN' ) ) {
75- if ( String ( CABundlePathOrString ) . startsWith ( '-----BEGIN' ) ) {
76- return x509_verify ( String ( certPathOrString ) , CABundlePathOrString , cb ) ;
77- } else {
78- return x509_verify_cert_buffer_ca_path ( String ( certPathOrString ) , CABundlePathOrString , cb ) ;
19+ Promise . all ( [
20+ getPathOrStringBuffer ( certPathOrString ) ,
21+ getPathOrStringBuffer ( CABundlePathOrString )
22+ ] ) . then ( function ( results ) {
23+ var certBuffer = results [ 0 ] ;
24+ var caBuffer = results [ 1 ] ;
25+
26+ try {
27+ var parsedCert = x509 . parseCert ( String ( certBuffer ) ) ;
28+ } catch ( Exception ) {
29+ return cb ( new TypeError ( 'Unable to parse certificate.' ) ) ;
7930 }
80- } else {
81- if ( String ( CABundlePathOrString ) . startsWith ( '-----BEGIN' ) ) {
82- return x509_verify_cert_path_ca_buffer ( String ( certPathOrString ) , CABundlePathOrString , cb ) ;
83- } else {
84- return x509_verify_cert_path_ca_path ( String ( certPathOrString ) , CABundlePathOrString , cb ) ;
31+
32+ try {
33+ x509 . verify ( certBuffer , caBuffer ) ;
34+ cb ( null , parsedCert ) ; //Might as well pass back the parsed certificate on verify
35+ } catch ( verificationError ) {
36+ cb ( verificationError ) ;
8537 }
86- }
38+ } , cb ) ;
8739} ;
8840
89-
9041exports . parseCert = function ( pathOrBuffer ) {
9142 var ret = x509 . parseCert ( pathOrBuffer ) ;
9243 var exts = { } ;
@@ -99,3 +50,18 @@ exports.parseCert = function(pathOrBuffer) {
9950 ret . extensions = exts ;
10051 return ret ;
10152} ;
53+
54+ function getPathOrStringBuffer ( pathOrString ) {
55+ if ( String ( pathOrString ) . indexOf ( '-----BEGIN' ) === 0 ) {
56+ return Promise . resolve ( Buffer ( pathOrString , 'utf8' ) ) ;
57+ } else {
58+ return new Promise ( function ( res , rej ) {
59+ fs . readFile ( pathOrString , function ( err , fileBuffer ) {
60+ if ( err ) {
61+ return rej ( err ) ;
62+ }
63+ res ( fileBuffer )
64+ } )
65+ } ) ;
66+ }
67+ }
0 commit comments