diff --git a/core/src/main/java/com/google/bitcoin/protocols/payments/PaymentSession.java b/core/src/main/java/com/google/bitcoin/protocols/payments/PaymentSession.java index fd1bc867..11236b82 100644 --- a/core/src/main/java/com/google/bitcoin/protocols/payments/PaymentSession.java +++ b/core/src/main/java/com/google/bitcoin/protocols/payments/PaymentSession.java @@ -172,7 +172,7 @@ public class PaymentSession { if (url == null) throw new PaymentRequestException.InvalidPaymentRequestURL("null paymentRequestUrl"); try { - return fetchPaymentRequest(new URI(url), true, trustStorePath); + return fetchPaymentRequest(new URI(url), verifyPki, trustStorePath); } catch(URISyntaxException e) { throw new PaymentRequestException.InvalidPaymentRequestURL(e); } @@ -186,8 +186,7 @@ public class PaymentSession { connection.setRequestProperty("Accept", "application/bitcoin-paymentrequest"); connection.setUseCaches(false); Protos.PaymentRequest paymentRequest = Protos.PaymentRequest.parseFrom(connection.getInputStream()); - PaymentSession paymentSession = new PaymentSession(paymentRequest, verifyPki, trustStorePath); - return paymentSession; + return new PaymentSession(paymentRequest, verifyPki, trustStorePath); } }); } @@ -511,33 +510,33 @@ public class PaymentSession { keyStore.load(is, defaultPassword); return keyStore; } - path = System.getProperty("javax.net.ssl.trustStore"); - if (path == null) { + try { // Check if we are on Android. - try { - Class Build = Class.forName("android.os.Build"); - Object version = Build.getDeclaredField("VERSION").get(Build); - // Build.VERSION_CODES.ICE_CREAM_SANDWICH is 14. - if (version.getClass().getDeclaredField("SDK_INT").getInt(version) >= 14) { - // After ICS, Android provided this nice method for loading the keystore, - // so we don't have to specify the location explicitly. - KeyStore keystore = KeyStore.getInstance("AndroidCAStore"); - keystore.load(null, null); - return keystore; - } else { - keyStoreType = "BKS"; - path = System.getProperty("java.home") + "/etc/security/cacerts.bks".replace('/', File.separatorChar); - } - } catch (ClassNotFoundException e) { - // NOP. android.os.Build is not present, so we are not on Android. - } catch (NoSuchFieldException e) { - // This should never happen. - } catch (IllegalAccessException e) { - // This should never happen. + Class Build = Class.forName("android.os.Build"); + Object version = Build.getDeclaredField("VERSION").get(Build); + // Build.VERSION_CODES.ICE_CREAM_SANDWICH is 14. + if (version.getClass().getDeclaredField("SDK_INT").getInt(version) >= 14) { + // After ICS, Android provided this nice method for loading the keystore, + // so we don't have to specify the location explicitly. + KeyStore keystore = KeyStore.getInstance("AndroidCAStore"); + keystore.load(null, null); + return keystore; + } else { + keyStoreType = "BKS"; + path = System.getProperty("java.home") + "/etc/security/cacerts.bks".replace('/', File.separatorChar); } + } catch (ClassNotFoundException e) { + // NOP. android.os.Build is not present, so we are not on Android. Fall through. + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); // Should never happen. + } catch (IllegalAccessException e) { + throw new RuntimeException(e); // Should never happen. } if (path == null) { - // We are not on Android. Try this default system location for Linux/Windows/OSX. + path = System.getProperty("javax.net.ssl.trustStore"); + } + if (path == null) { + // Try this default system location for Linux/Windows/OSX. path = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar); } try { @@ -546,7 +545,8 @@ public class PaymentSession { keyStore.load(is, defaultPassword); return keyStore; } catch (FileNotFoundException e) { - // If we failed to find a system trust store, load our own fallback trust store. + // If we failed to find a system trust store, load our own fallback trust store. This can fail on Android + // but we should never reach it there. KeyStore keyStore = KeyStore.getInstance("JKS"); InputStream is = getClass().getResourceAsStream("cacerts"); keyStore.load(is, defaultPassword); diff --git a/core/src/test/java/com/google/bitcoin/protocols/payments/PaymentSessionTest.java b/core/src/test/java/com/google/bitcoin/protocols/payments/PaymentSessionTest.java index 9ea26dc9..63555fa3 100644 --- a/core/src/test/java/com/google/bitcoin/protocols/payments/PaymentSessionTest.java +++ b/core/src/test/java/com/google/bitcoin/protocols/payments/PaymentSessionTest.java @@ -23,8 +23,6 @@ import com.google.protobuf.ByteString; import org.bitcoin.protocols.payments.Protos; import org.junit.Before; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.InputStream; import java.math.BigInteger; @@ -35,7 +33,6 @@ import java.util.Date; import static org.junit.Assert.*; public class PaymentSessionTest { - private static final Logger log = LoggerFactory.getLogger(PaymentSessionTest.class); private static final NetworkParameters params = TestNet3Params.get(); private static final String simplePaymentUrl = "http://a.simple.url.com/"; private static final String paymentRequestMemo = "send coinz noa plz kthx";