Draft
Conversation
|
| final SSLContext sslContext1 = sslContext; | ||
| try { | ||
| sslContext1.init(keyManagers, trustManagers, secureRandom); | ||
| sslContext.init(keyManagers, trustManagers, secureRandom); |
Check failure
Code scanning / CodeQL
`TrustManager` that accepts all certificates
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the problem, we need to replace the NullTrustManager with a TrustManager that only trusts specific self-signed certificates. This involves creating a KeyStore containing the trusted certificates and initializing the TrustManagerFactory with this KeyStore. This way, only the specified certificates will be trusted, and the risk of a machine-in-the-middle attack is mitigated.
- Load the self-signed certificate into a
KeyStore. - Initialize a
TrustManagerFactorywith theKeyStore. - Use the
TrustManagerFactoryto get theTrustManagerarray. - Initialize the
SSLContextwith theTrustManagerarray.
Suggested changeset
1
src/main/java/com/github/kaklakariada/fritzbox/http/TrustSelfSignedCertificates.java
| @@ -22,2 +22,7 @@ | ||
| import javax.net.ssl.*; | ||
| import java.io.FileInputStream; | ||
| import java.io.InputStream; | ||
| import java.security.KeyStore; | ||
| import java.security.cert.CertificateFactory; | ||
| import java.security.cert.X509Certificate; | ||
|
|
||
| @@ -42,3 +47,3 @@ | ||
| final KeyManager[] keyManagers = null; | ||
| final TrustManager[] trustManagers = new TrustManager[] { new NullTrustManager() }; | ||
| final TrustManager[] trustManagers = getTrustManagers(); | ||
| final SecureRandom secureRandom = new SecureRandom(); | ||
| @@ -57,2 +62,23 @@ | ||
| } | ||
| } | ||
| private static TrustManager[] getTrustManagers() { | ||
| try { | ||
| // Load the self-signed certificate | ||
| File certificateFile = new File("path/to/self-signed-certificate"); | ||
| KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| keyStore.load(null, null); | ||
| X509Certificate generatedCertificate; | ||
| try (InputStream cert = new FileInputStream(certificateFile)) { | ||
| generatedCertificate = (X509Certificate) CertificateFactory.getInstance("X509") | ||
| .generateCertificate(cert); | ||
| } | ||
| keyStore.setCertificateEntry(certificateFile.getName(), generatedCertificate); | ||
|
|
||
| // Initialize TrustManagerFactory with the KeyStore | ||
| TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| tmf.init(keyStore); | ||
| return tmf.getTrustManagers(); | ||
| } catch (Exception e) { | ||
| throw new HttpException("Error initializing trust managers", e); | ||
| } | ||
| } |
Copilot is powered by AI and may make mistakes. Always verify output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Contributed by Manfred