@@ -26,24 +26,18 @@ if (!fs.existsSync(p12File)) {
2626 process . exit ( ) ;
2727}
2828
29- const p12Pass = String ( fs . readFileSync ( p12PassFile , "utf8" ) ) . replace ( "\n" , "" ) ;
30-
3129/* Convert P12 to PEM */
3230let cert , certData ;
3331try {
32+ const p12Pass = String ( fs . readFileSync ( p12PassFile , "utf8" ) ) . replace ( "\n" , "" ) . trim ( ) ;
3433 const p12 = forge . pkcs12 . pkcs12FromAsn1 ( forge . asn1 . fromDer ( fs . readFileSync ( p12File , { encoding :"binary" } ) ) , false , p12Pass ) ;
3534 certData = p12 . getBags ( { bagType : forge . pki . oids . certBag } ) ;
36- cert = forge . pki . certificateToPem ( certData [ forge . pki . oids . certBag ] [ 0 ] . cert ) ; // no need to assign to variable, but makes the ocsp.check() line shorter
35+ cert = forge . pki . certificateToPem ( certData [ forge . pki . oids . certBag ] [ 0 ] . cert ) ;
3736} catch ( err ) {
38- console . log ( `Failed to convert P12 to PEM. ${ err . message . includes ( "Invalid password" ) ? "Password is likely incorrect" : "Unknown error" } .` ) ;
37+ console . log ( `Failed to convert P12 to PEM. ${ err . message . includes ( "password" ) ? "Password is likely incorrect" : "Unknown error" } .` ) ;
3938 process . exit ( ) ;
4039}
4140
42- // Get certificate name
43- let certName = certData [ forge . pki . oids . certBag ] [ 0 ] . cert . subject . attributes . filter ( ( { name} ) => name === "organizationName" ) [ 0 ] . value ;
44- // Get expiration date
45- let certExpirationDate = new Date ( certData [ forge . pki . oids . certBag ] [ 0 ] . cert . validity . notAfter . getTime ( ) ) . toGMTString ( ) ;
46-
4741/* GET CERT SIGNATURE STATUS */
4842// Loop througn all CA certificates from CA-PEM folder
4943// Probably a better way than doing this, but it's fast anyway and doesn't really matter anyway
@@ -52,18 +46,24 @@ fs.readdirSync("CA-PEM").forEach(file => {
5246 if ( file . endsWith ( ".pem" ) ) { // If PEM file
5347 // Check if the certificate is signed by the CA
5448 ocsp . check ( { cert : cert , issuer : fs . readFileSync ( `CA-PEM/${ file } ` , "utf8" ) } , function ( error , res ) {
55- let certStatus ;
49+ let certStatus , certRevocationDate = null ;
5650 if ( error ) {
57- if ( error . toString ( ) . includes ( "revoked" ) ) certStatus = "Revoked" ;
51+ if ( error . toString ( ) . includes ( "revoked" ) ) {
52+ certStatus = "Revoked" ;
53+ certRevocationDate = new Date ( res . value . revocationTime ) . toGMTString ( ) ;
54+ }
5855 } else if ( res . type == "good" ) certStatus = "Signed" ;
5956
6057 if ( certStatus ) {
58+ const certName = certData [ forge . pki . oids . certBag ] [ 0 ] . cert . subject . attributes . filter ( ( { name} ) => name === "organizationName" ) [ 0 ] . value ;
59+ const certExpirationDate = new Date ( certData [ forge . pki . oids . certBag ] [ 0 ] . cert . validity . notAfter . getTime ( ) ) . toGMTString ( ) ;
6160 if ( jsonOutput ) { // If JSON output is requested
62- console . log ( JSON . stringify ( { name : certName , status : certStatus , expirationDate : certExpirationDate } ) ) ;
61+ console . log ( JSON . stringify ( { name : certName , status : certStatus , expirationDate : certExpirationDate , revocationDate : certRevocationDate } ) ) ;
6362 } else {
6463 console . log ( "Certificate Name: " + certName ) ;
6564 console . log ( "Certificate Status: " + certStatus ) ;
6665 console . log ( "Certificate Expiration Date: " + certExpirationDate ) ;
66+ if ( certRevocationDate ) console . log ( "Certificate Revocation Date: " + certRevocationDate ) ;
6767 }
6868 process . exit ( ) ; // Exit here so the script doesn't continue to check other certificates
6969 }
0 commit comments