Skip to content

Commit

Permalink
Update certificates to latest version, making sure there are no comme…
Browse files Browse the repository at this point in the history
…nts (this breaks on Android). Check if no certificates are present before returning ssl context.
  • Loading branch information
rolftimmermans committed Jan 18, 2017
1 parent 651e62c commit d0b801e
Show file tree
Hide file tree
Showing 4 changed files with 490 additions and 907 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/tinify/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public final Response request(final Method method, final String endpoint) throws
}

public final Response request(final Method method, final String endpoint, final Options options) throws Exception {
/* OkHttp does not support null request bodies if the method is POST. */
if (method.equals(Method.GET)) {
return request(method, endpoint, options.isEmpty() ? null : RequestBody.create(JSON, options.toJson()));
} else {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/tinify/SSLContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ public static SSLSocketFactory getSocketFactory() {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(certificateStream());

// Put the certificates a new key store.
char[] password = "password".toCharArray(); // Any password will work.
KeyStore keyStore = newEmptyKeyStore(password);
KeyStore keyStore = newEmptyKeyStore();
int index = 0;
for (Certificate certificate : certificates) {
String certificateAlias = Integer.toString(index++);
keyStore.setCertificateEntry(certificateAlias, certificate);
}

if (keyStore.size() == 0) {
/* The resource stream was empty, no certificates were found. */
throw new ConnectionException("Unable to load any CA certificates.", null);
}

KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, password);
keyManagerFactory.init(keyStore, null);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
Expand All @@ -49,11 +52,11 @@ public static InputStream certificateStream() throws IOException {
return SSLContext.class.getResourceAsStream("/cacert.pem");
}

private static KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {
private static KeyStore newEmptyKeyStore() throws GeneralSecurityException {
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream in = null; // By convention, 'null' creates an empty key store.
keyStore.load(in, password);
/* By convention, a null InputStream creates an empty key store. */
keyStore.load(null, null);
return keyStore;
} catch (IOException e) {
throw new AssertionError(e);
Expand Down
Loading

0 comments on commit d0b801e

Please sign in to comment.