@@ -73,15 +73,6 @@ public final class GoogleUtils {
7373 /** Cached value for {@link #getCertificateTrustStore()}. */
7474 @ VisibleForTesting static KeyStore certTrustStore ;
7575
76- /** Default JDK cacerts file path relative to java.home. */
77- @ VisibleForTesting static String defaultCacertsPath = "lib/security/cacerts" ;
78-
79- /** Default password for JDK cacerts file. */
80- static final String DEFAULT_CACERTS_PASSWORD = "changeit" ;
81-
82- /** Java home system property key. */
83- static final String JAVA_HOME_KEY = "java.home" ;
84-
8576 /** Name of bundled keystore. */
8677 static final String BUNDLED_KEYSTORE = "google.p12" ;
8778
@@ -102,21 +93,53 @@ static KeyStore getBundledKeystore() throws IOException, GeneralSecurityExceptio
10293 }
10394
10495 /**
105- * Loads the default JDK keystore (cacerts) containing trusted root certificates. Determines the
106- * path to the cacerts file based on the java.home system property .
96+ * Loads the default JDK keystore (cacerts) containing trusted root certificates.
97+ * Uses Java's system properties to locate the default trust store .
10798 *
10899 * @return the loaded keystore
109100 */
110101 @ VisibleForTesting
111102 static KeyStore getJdkDefaultKeyStore () throws IOException , GeneralSecurityException {
112- String javaHome = System .getProperty (JAVA_HOME_KEY );
113- File file = new File (javaHome , defaultCacertsPath );
114-
115- KeyStore trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
116- try (FileInputStream is = new FileInputStream (file )) {
117- trustStore .load (is , DEFAULT_CACERTS_PASSWORD .toCharArray ());
103+ // Get trust store location and type from system properties, or use defaults
104+ String trustStoreType = System .getProperty ("javax.net.ssl.trustStoreType" , KeyStore .getDefaultType ());
105+ String trustStorePath = System .getProperty ("javax.net.ssl.trustStore" );
106+ String trustStorePassword = System .getProperty ("javax.net.ssl.trustStorePassword" , "changeit" );
107+
108+ KeyStore keyStore = KeyStore .getInstance (trustStoreType );
109+
110+ if (trustStorePath != null && !trustStorePath .isEmpty ()) {
111+ // User specified a custom trust store via system property
112+ try (FileInputStream fis = new FileInputStream (trustStorePath )) {
113+ keyStore .load (fis , trustStorePassword .toCharArray ());
114+ System .out .println ("loaded keystore from truststore path" );
115+ }
116+ } else {
117+ // Find the default JDK cacerts location
118+ String javaHome = System .getProperty ("java.home" );
119+ String [] possiblePaths = {
120+ "lib/security/cacerts" , // Java 9+
121+ "jre/lib/security/cacerts" // Java 8 and earlier
122+ };
123+
124+ File cacertsFile = null ;
125+ for (String path : possiblePaths ) {
126+ File candidate = new File (javaHome , path );
127+ if (candidate .exists () && candidate .canRead ()) {
128+ cacertsFile = candidate ;
129+ break ;
130+ }
131+ }
132+
133+ if (cacertsFile == null ) {
134+ throw new IOException ("Unable to find JDK cacerts file in java.home: " + javaHome );
135+ }
136+
137+ try (FileInputStream fis = new FileInputStream (cacertsFile )) {
138+ keyStore .load (fis , trustStorePassword .toCharArray ());
139+ }
118140 }
119- return trustStore ;
141+
142+ return keyStore ;
120143 }
121144
122145 /**
0 commit comments