@@ -81,7 +81,6 @@ static SSLServerSocketFactory createServerSocketFactory(Context context, @NonNul
8181 }
8282
8383 static boolean hasIdentity (ReadableMap options ) {
84- boolean hasId = false ;
8584 try {
8685 final String keystoreName = options .hasKey ("androidKeyStore" ) ?
8786 options .getString ("androidKeyStore" ) : KeyStore .getDefaultType ();
@@ -92,13 +91,11 @@ static boolean hasIdentity(ReadableMap options) {
9291 return false ;
9392 }
9493
95- // Get keystore instance
9694 KeyStore keyStore = KeyStore .getInstance (keystoreName );
9795 keyStore .load (null , null );
9896
9997 // Check if key entry exists with its certificate chain
100- hasId = keyStore .isKeyEntry (keyAlias );
101- return hasId ;
98+ return keyStore .isKeyEntry (keyAlias );
10299 } catch (Exception e ) {
103100 return false ;
104101 }
@@ -151,37 +148,45 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
151148 final KeystoreInfo keystoreInfo ) throws IOException , GeneralSecurityException {
152149
153150 SSLSocketFactory ssf = null ;
154- if (optionResCert != null && optionResKey != null ) {
155- final String keyStoreName = keystoreInfo .getKeystoreName ().isEmpty () ?
151+
152+ KeyStore keyStore = null ;
153+ final String keyStoreName = keystoreInfo .getKeystoreName ().isEmpty () ?
156154 KeyStore .getDefaultType () :
157155 keystoreInfo .getKeystoreName ();
158- KeyStore keyStore = KeyStore .getInstance (keyStoreName );
159- keyStore .load (null , null );
156+ String keyAlias = keystoreInfo .getKeyAlias ();
160157
161- // Check if cert and key if already registered inside our keystore
162- // If one is missing we insert again
163- boolean hasCertInStore = keyStore .isCertificateEntry (keystoreInfo .getCertAlias ());
164- boolean hasKeyInStore = keyStore .isKeyEntry (keystoreInfo .getKeyAlias ());
165- if (!hasCertInStore || !hasKeyInStore ) {
166- InputStream certInput = getResolvableinputStream (context , optionResCert );
167- Certificate cert = CertificateFactory .getInstance ("X.509" ).generateCertificate (certInput );
168- keyStore .setCertificateEntry (keystoreInfo .getCertAlias (), cert );
169-
170- InputStream keyInput = getResolvableinputStream (context , optionResKey );
171- PrivateKey privateKey = getPrivateKeyFromPEM (keyInput );
172- keyStore .setKeyEntry (keystoreInfo .getKeyAlias (), privateKey , null , new Certificate []{cert });
158+ // if user provides keyAlias without key it means an identity(cert+key) has already been
159+ // inserted in keychain.
160+ if (keyAlias != null && !keyAlias .isEmpty () && optionResKey == null ) {
161+ keyStore = KeyStore .getInstance (keyStoreName );
162+ keyStore .load (null , null );
163+ if (!keyStore .isKeyEntry (keyAlias )) {
164+ keyStore = null ;
173165 }
166+ } else if (optionResCert != null && optionResKey != null ) {
167+
168+ keyStore = KeyStore .getInstance (keyStoreName );
169+ keyStore .load (null , null );
174170
175- boolean hasCaInStore = keyStore .isCertificateEntry (keystoreInfo .getCaAlias ());
176- if (optionResCa != null && !hasCaInStore ) {
171+ InputStream certInput = getResolvableinputStream (context , optionResCert );
172+ Certificate cert = CertificateFactory .getInstance ("X.509" ).generateCertificate (certInput );
173+ keyStore .setCertificateEntry (keystoreInfo .getCertAlias (), cert );
174+
175+ InputStream keyInput = getResolvableinputStream (context , optionResKey );
176+ PrivateKey privateKey = getPrivateKeyFromPEM (keyInput );
177+ keyStore .setKeyEntry (keystoreInfo .getKeyAlias (), privateKey , null , new Certificate []{cert });
178+
179+ if (optionResCa != null ) {
177180 InputStream caInput = getResolvableinputStream (context , optionResCa );
178181 // Generate the CA Certificate from the raw resource file
179182 Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
180183 caInput .close ();
181184 // Load the key store using the CA
182185 keyStore .setCertificateEntry (keystoreInfo .getCaAlias (), ca );
183186 }
184-
187+ }
188+
189+ if (keyStore != null ) {
185190 // Initialize the KeyManagerFactory with this cert
186191 KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
187192 keyManagerFactory .init (keyStore , new char [0 ]);
@@ -190,15 +195,14 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
190195 SSLContext sslContext = SSLContext .getInstance ("TLS" );
191196 sslContext .init (keyManagerFactory .getKeyManagers (), new TrustManager []{new BlindTrustManager ()}, null );
192197 return sslContext .getSocketFactory ();
193-
194198 } else {
195199 // Keep old behavior
196200 InputStream caInput = getResolvableinputStream (context , optionResCa );
197201 // Generate the CA Certificate from the raw resource file
198202 Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
199203 caInput .close ();
200204 // Load the key store using the CA
201- KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
205+ keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
202206 keyStore .load (null , null );
203207 keyStore .setCertificateEntry ("ca" , ca );
204208
0 commit comments