Skip to content

Commit af07d18

Browse files
Jimmy Dahlqvistfosterzhang
authored andcommitted
Allow custom trust manager on client configuration (#165)
To be able to validate the SSL certificate for a https connection, to ensure server end point, allow a custom trust manger to be set on client configuration.
1 parent b11f6b9 commit af07d18

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

aws-android-sdk-core/src/main/java/com/amazonaws/ClientConfiguration.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.net.InetAddress;
2424

25+
import javax.net.ssl.TrustManager;
26+
2527
/**
2628
* Client configuration options such as proxy settings, user agent string, max
2729
* retry attempts, etc.
@@ -166,6 +168,11 @@ public class ClientConfiguration {
166168
*/
167169
private String signerOverride;
168170

171+
/**
172+
* Optional override to control how to perform authentication for secure connections.
173+
*/
174+
private TrustManager trustManager = null;
175+
169176
public ClientConfiguration() {
170177
}
171178

@@ -189,6 +196,7 @@ public ClientConfiguration(ClientConfiguration other) {
189196
this.socketReceiveBufferSizeHint = other.socketReceiveBufferSizeHint;
190197
this.socketSendBufferSizeHint = other.socketSendBufferSizeHint;
191198
this.signerOverride = other.signerOverride;
199+
this.trustManager = other.trustManager;
192200
}
193201

194202
/**
@@ -985,4 +993,38 @@ public ClientConfiguration withPreemptiveBasicProxyAuth(boolean preemptiveBasicP
985993
return this;
986994
}
987995

996+
/**
997+
* Gets the trust manager to use for secure connections from this client.
998+
* If null the default authentication will be used.
999+
*
1000+
* @return The trust manager to use for this client, or null to use the default
1001+
* authentication for secure connections.
1002+
*/
1003+
public TrustManager getTrustManager() {
1004+
return trustManager;
1005+
}
1006+
1007+
/**
1008+
* Sets the trust manager to use for secure connections from this client.
1009+
* If null the default authentication will be used.
1010+
*
1011+
* @param trustManager The trust manager to use for this client.
1012+
*/
1013+
public void setTrustManager(TrustManager trustManager) {
1014+
this.trustManager = trustManager;
1015+
}
1016+
1017+
/**
1018+
* Sets the trust manager to use for secure connections from this client, and returns the
1019+
* updated ClientConfiguration object so that additional calls may be chained together.
1020+
* If null the default authentication will be used.
1021+
*
1022+
* @param trustManager The trust manager to use for this client.
1023+
* @return The updated ClientConfiguration object.
1024+
*/
1025+
public ClientConfiguration withTrustManager(TrustManager trustManager) {
1026+
setTrustManager(trustManager);
1027+
return this;
1028+
}
1029+
9881030
}

aws-android-sdk-core/src/main/java/com/amazonaws/http/UrlHttpClient.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,32 @@ void configureConnection(HttpURLConnection connection) {
191191
disableCertificateValidation(https);
192192
}
193193
*/
194+
195+
if (config.getTrustManager() != null) {
196+
enableCustomTrustManager(https);
197+
}
194198
}
195199
}
196200

197-
/*
198201
private SSLContext sc = null;
199202

203+
private void enableCustomTrustManager(HttpsURLConnection connection) {
204+
if (sc == null) {
205+
TrustManager[] customTrustManagers = new TrustManager[] {
206+
config.getTrustManager()
207+
};
208+
try {
209+
sc = SSLContext.getInstance("TLS");
210+
sc.init(null, customTrustManagers, null);
211+
} catch (GeneralSecurityException e) {
212+
throw new RuntimeException(e);
213+
}
214+
}
215+
216+
connection.setSSLSocketFactory(sc.getSocketFactory());
217+
}
218+
219+
/*
200220
private void disableCertificateValidation(HttpsURLConnection connection) {
201221
if (sc == null) {
202222
TrustManager[] trustAllCerts = new TrustManager[] {

aws-android-sdk-core/src/test/java/com/amazonaws/ClientConfigurationTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.net.InetAddress;
3030
import java.net.UnknownHostException;
3131

32+
import javax.net.ssl.TrustManager;
33+
3234
public class ClientConfigurationTest {
3335

3436
@Test
@@ -119,6 +121,11 @@ public void testWithAndSetMethods() throws UnknownHostException {
119121
c.setUserAgent("set");
120122
assertEquals(c.getUserAgent(), "set");
121123

124+
TrustManager trustManager = new TrustManager() {};
125+
c.withTrustManager(trustManager);
126+
assertSame(trustManager, c.getTrustManager());
127+
c.setTrustManager(null);
128+
assertNull(c.getTrustManager());
122129
}
123130

124131
@Test
@@ -143,6 +150,8 @@ public void testCopyConstructor() throws UnknownHostException {
143150
c.withSocketBufferSizeHints(0, 1);
144151
c.withSocketTimeout(0);
145152
c.withUserAgent("ua");
153+
TrustManager trustManager = new TrustManager() {};
154+
c.withTrustManager(trustManager);
146155

147156
ClientConfiguration n = new ClientConfiguration(c);
148157
assertEquals(c.getConnectionTimeout(), n.getConnectionTimeout());
@@ -161,7 +170,8 @@ public void testCopyConstructor() throws UnknownHostException {
161170
assertArrayEquals(c.getSocketBufferSizeHints(), n.getSocketBufferSizeHints());
162171
assertEquals(c.getSocketTimeout(), n.getSocketTimeout());
163172
assertEquals(c.getUserAgent(), n.getUserAgent());
164-
173+
assertEquals(c.getUserAgent(), n.getUserAgent());
174+
assertSame(c.getTrustManager(), n.getTrustManager());
165175
}
166176

167177
}

0 commit comments

Comments
 (0)