Skip to content

Commit

Permalink
Improve logging and error handling in Android cert injection
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Nov 10, 2023
1 parent 61cb054 commit 15869c6
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions android/android-system-certificate-injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ Java.perform(() => {
const ByteArrayInputStream = Java.use('java.io.ByteArrayInputStream');
const CertFactory = Java.use('java.security.cert.CertificateFactory');

const certFactory = CertFactory.getInstance("X.509");
const certBytes = String.$new(CERT_PEM).getBytes();
const cert = certFactory.generateCertificate(ByteArrayInputStream.$new(certBytes));
let cert;
try {
const certFactory = CertFactory.getInstance("X.509");
const certBytes = String.$new(CERT_PEM).getBytes();
cert = certFactory.generateCertificate(ByteArrayInputStream.$new(certBytes));
} catch (e) {
console.error('Could not parse provided certificate PEM!');
console.error(e);
Java.use('java.lang.System').exit(1);
}

// Then we hook TrustedCertificateIndex. This is used for caching known trusted certs within Conscrypt -
// by prepopulating all instances, we ensure that all TrustManagerImpls (and potentially other
Expand All @@ -46,6 +53,9 @@ Java.perform(() => {
throw new Error(`${TrustedCertificateIndexClassname} not found - could not inject system certificate`);
} else {
// Other classnames are optional fallbacks
if (DEBUG_MODE) {
console.log(`[ ] Skipped cert injection for ${TrustedCertificateIndexClassname} (not present)`);
}
return;
}
}
Expand All @@ -66,6 +76,8 @@ Java.perform(() => {
return result;
};
});

if (DEBUG_MODE) console.log(`[+] Injected cert into ${TrustedCertificateIndexClassname}`);
});

// This effectively adds us to the system certs, and also defeats quite a bit of basic certificate
Expand Down

0 comments on commit 15869c6

Please sign in to comment.