@@ -81,7 +81,6 @@ static SSLServerSocketFactory createServerSocketFactory(Context context, @NonNul
81
81
}
82
82
83
83
static boolean hasIdentity (ReadableMap options ) {
84
- boolean hasId = false ;
85
84
try {
86
85
final String keystoreName = options .hasKey ("androidKeyStore" ) ?
87
86
options .getString ("androidKeyStore" ) : KeyStore .getDefaultType ();
@@ -92,13 +91,11 @@ static boolean hasIdentity(ReadableMap options) {
92
91
return false ;
93
92
}
94
93
95
- // Get keystore instance
96
94
KeyStore keyStore = KeyStore .getInstance (keystoreName );
97
95
keyStore .load (null , null );
98
96
99
97
// Check if key entry exists with its certificate chain
100
- hasId = keyStore .isKeyEntry (keyAlias );
101
- return hasId ;
98
+ return keyStore .isKeyEntry (keyAlias );
102
99
} catch (Exception e ) {
103
100
return false ;
104
101
}
@@ -151,37 +148,45 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
151
148
final KeystoreInfo keystoreInfo ) throws IOException , GeneralSecurityException {
152
149
153
150
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 () ?
156
154
KeyStore .getDefaultType () :
157
155
keystoreInfo .getKeystoreName ();
158
- KeyStore keyStore = KeyStore .getInstance (keyStoreName );
159
- keyStore .load (null , null );
156
+ String keyAlias = keystoreInfo .getKeyAlias ();
160
157
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 ;
173
165
}
166
+ } else if (optionResCert != null && optionResKey != null ) {
167
+
168
+ keyStore = KeyStore .getInstance (keyStoreName );
169
+ keyStore .load (null , null );
174
170
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 ) {
177
180
InputStream caInput = getResolvableinputStream (context , optionResCa );
178
181
// Generate the CA Certificate from the raw resource file
179
182
Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
180
183
caInput .close ();
181
184
// Load the key store using the CA
182
185
keyStore .setCertificateEntry (keystoreInfo .getCaAlias (), ca );
183
186
}
184
-
187
+ }
188
+
189
+ if (keyStore != null ) {
185
190
// Initialize the KeyManagerFactory with this cert
186
191
KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
187
192
keyManagerFactory .init (keyStore , new char [0 ]);
@@ -190,15 +195,14 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
190
195
SSLContext sslContext = SSLContext .getInstance ("TLS" );
191
196
sslContext .init (keyManagerFactory .getKeyManagers (), new TrustManager []{new BlindTrustManager ()}, null );
192
197
return sslContext .getSocketFactory ();
193
-
194
198
} else {
195
199
// Keep old behavior
196
200
InputStream caInput = getResolvableinputStream (context , optionResCa );
197
201
// Generate the CA Certificate from the raw resource file
198
202
Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
199
203
caInput .close ();
200
204
// Load the key store using the CA
201
- KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
205
+ keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
202
206
keyStore .load (null , null );
203
207
keyStore .setCertificateEntry ("ca" , ca );
204
208
0 commit comments