Skip to content

Commit

Permalink
Fix code scanning alert no. 50: TrustManager that accepts all certi…
Browse files Browse the repository at this point in the history
…ficates

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
1 parent b5550b0 commit 988dc83
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@


import java.security.SecureRandom;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManagerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
Expand All @@ -21,8 +28,21 @@ class SSLUtils
*/
public static SSLContext createTrustAllContext() throws Exception
{
TrustManager[] trustManagers = new TrustManager[1];
trustManagers[0] = new TrustAllManager();
// 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);
TrustManager[] trustManagers = tmf.getTrustManagers();

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustManagers, new SecureRandom());
Expand Down

0 comments on commit 988dc83

Please sign in to comment.