3030import java .io .IOException ;
3131import java .security .cert .CertStoreException ;
3232import java .security .cert .CertificateException ;
33+ import java .util .AbstractMap ;
3334import java .util .ArrayList ;
3435import java .util .List ;
3536import javax .annotation .Nullable ;
37+ import javax .net .ssl .TrustManager ;
3638
3739/** Base class for dynamic {@link SslContextProvider}s. */
3840@ Internal
3941public abstract class DynamicSslContextProvider extends SslContextProvider {
4042
4143 protected final List <Callback > pendingCallbacks = new ArrayList <>();
4244 @ Nullable protected final CertificateValidationContext staticCertificateValidationContext ;
43- @ Nullable protected SslContext sslContext ;
45+ @ Nullable protected AbstractMap .SimpleImmutableEntry <SslContext , TrustManager >
46+ sslContextAndExtendedX509TrustManager ;
4447
4548 protected DynamicSslContextProvider (
4649 BaseTlsContext tlsContext , CertificateValidationContext staticCertValidationContext ) {
@@ -49,14 +52,16 @@ protected DynamicSslContextProvider(
4952 }
5053
5154 @ Nullable
52- public SslContext getSslContext () {
53- return sslContext ;
55+ public AbstractMap . SimpleImmutableEntry < SslContext , TrustManager > getSslContextAndExtendedX509TrustManager () {
56+ return sslContextAndExtendedX509TrustManager ;
5457 }
5558
5659 protected abstract CertificateValidationContext generateCertificateValidationContext ();
5760
58- /** Gets a server or client side SslContextBuilder. */
59- protected abstract SslContextBuilder getSslContextBuilder (
61+ /**
62+ * Gets a server or client side SslContextBuilder.
63+ */
64+ protected abstract AbstractMap .SimpleImmutableEntry <SslContextBuilder , TrustManager > getSslContextBuilderAndExtendedX509TrustManager (
6065 CertificateValidationContext certificateValidationContext )
6166 throws CertificateException , IOException , CertStoreException ;
6267
@@ -65,7 +70,8 @@ protected final void updateSslContext() {
6570 try {
6671 CertificateValidationContext localCertValidationContext =
6772 generateCertificateValidationContext ();
68- SslContextBuilder sslContextBuilder = getSslContextBuilder (localCertValidationContext );
73+ AbstractMap .SimpleImmutableEntry <SslContextBuilder , TrustManager > sslContextBuilderAndTm =
74+ getSslContextBuilderAndExtendedX509TrustManager (localCertValidationContext );
6975 CommonTlsContext commonTlsContext = getCommonTlsContext ();
7076 if (commonTlsContext != null && commonTlsContext .getAlpnProtocolsCount () > 0 ) {
7177 List <String > alpnList = commonTlsContext .getAlpnProtocolsList ();
@@ -75,29 +81,30 @@ protected final void updateSslContext() {
7581 ApplicationProtocolConfig .SelectorFailureBehavior .NO_ADVERTISE ,
7682 ApplicationProtocolConfig .SelectedListenerFailureBehavior .ACCEPT ,
7783 alpnList );
78- sslContextBuilder .applicationProtocolConfig (apn );
84+ sslContextBuilderAndTm . getKey () .applicationProtocolConfig (apn );
7985 }
8086 List <Callback > pendingCallbacksCopy ;
81- SslContext sslContextCopy ;
87+ AbstractMap . SimpleImmutableEntry < SslContext , TrustManager > sslContextAndExtendedX09TrustManagerCopy ;
8288 synchronized (pendingCallbacks ) {
83- sslContext = sslContextBuilder .build ();
84- sslContextCopy = sslContext ;
89+ sslContextAndExtendedX509TrustManager = new AbstractMap .SimpleImmutableEntry <>(
90+ sslContextBuilderAndTm .getKey ().build (), sslContextBuilderAndTm .getValue ());
91+ sslContextAndExtendedX09TrustManagerCopy = sslContextAndExtendedX509TrustManager ;
8592 pendingCallbacksCopy = clonePendingCallbacksAndClear ();
8693 }
87- makePendingCallbacks (sslContextCopy , pendingCallbacksCopy );
94+ makePendingCallbacks (sslContextAndExtendedX09TrustManagerCopy , pendingCallbacksCopy );
8895 } catch (Exception e ) {
8996 onError (Status .fromThrowable (e ));
9097 throw new RuntimeException (e );
9198 }
9299 }
93100
94101 protected final void callPerformCallback (
95- Callback callback , final SslContext sslContextCopy ) {
102+ Callback callback , final AbstractMap . SimpleImmutableEntry < SslContext , TrustManager > sslContextAndTmCopy ) {
96103 performCallback (
97104 new SslContextGetter () {
98105 @ Override
99- public SslContext get () {
100- return sslContextCopy ;
106+ public AbstractMap . SimpleImmutableEntry < SslContext , TrustManager > get () {
107+ return sslContextAndTmCopy ;
101108 }
102109 },
103110 callback
@@ -108,10 +115,10 @@ public SslContext get() {
108115 public final void addCallback (Callback callback ) {
109116 checkNotNull (callback , "callback" );
110117 // if there is a computed sslContext just send it
111- SslContext sslContextCopy = null ;
118+ AbstractMap . SimpleImmutableEntry < SslContext , TrustManager > sslContextCopy = null ;
112119 synchronized (pendingCallbacks ) {
113- if (sslContext != null ) {
114- sslContextCopy = sslContext ;
120+ if (sslContextAndExtendedX509TrustManager != null ) {
121+ sslContextCopy = sslContextAndExtendedX509TrustManager ;
115122 } else {
116123 pendingCallbacks .add (callback );
117124 }
@@ -122,9 +129,11 @@ public final void addCallback(Callback callback) {
122129 }
123130
124131 private final void makePendingCallbacks (
125- SslContext sslContextCopy , List <Callback > pendingCallbacksCopy ) {
132+ AbstractMap .SimpleImmutableEntry <SslContext , TrustManager >
133+ sslContextAndExtendedX509TrustManagerCopy ,
134+ List <Callback > pendingCallbacksCopy ) {
126135 for (Callback callback : pendingCallbacksCopy ) {
127- callPerformCallback (callback , sslContextCopy );
136+ callPerformCallback (callback , sslContextAndExtendedX509TrustManagerCopy );
128137 }
129138 }
130139
0 commit comments