@@ -80,6 +80,27 @@ static SSLServerSocketFactory createServerSocketFactory(Context context, @NonNul
80
80
return sslContext .getServerSocketFactory ();
81
81
}
82
82
83
+ static boolean hasIdentity (ReadableMap options ) {
84
+ try {
85
+ final String keystoreName = options .hasKey ("androidKeyStore" ) ?
86
+ options .getString ("androidKeyStore" ) : KeyStore .getDefaultType ();
87
+ final String keyAlias = options .hasKey ("keyAlias" ) ?
88
+ options .getString ("keyAlias" ) : "" ;
89
+
90
+ if (keyAlias .isEmpty ()) {
91
+ return false ;
92
+ }
93
+
94
+ KeyStore keyStore = KeyStore .getInstance (keystoreName );
95
+ keyStore .load (null , null );
96
+
97
+ // Check if key entry exists with its certificate chain
98
+ return keyStore .isKeyEntry (keyAlias );
99
+ } catch (Exception e ) {
100
+ return false ;
101
+ }
102
+ }
103
+
83
104
public static PrivateKey getPrivateKeyFromPEM (InputStream keyStream ) {
84
105
try (PemReader pemReader = new PemReader (new InputStreamReader (keyStream ))) {
85
106
PemObject pemObject = pemReader .readPemObject ();
@@ -127,37 +148,45 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
127
148
final KeystoreInfo keystoreInfo ) throws IOException , GeneralSecurityException {
128
149
129
150
SSLSocketFactory ssf = null ;
130
- if (optionResCert != null && optionResKey != null ) {
131
- final String keyStoreName = keystoreInfo .getKeystoreName ().isEmpty () ?
151
+
152
+ KeyStore keyStore = null ;
153
+ final String keyStoreName = keystoreInfo .getKeystoreName ().isEmpty () ?
132
154
KeyStore .getDefaultType () :
133
155
keystoreInfo .getKeystoreName ();
134
- KeyStore keyStore = KeyStore .getInstance (keyStoreName );
135
- keyStore .load (null , null );
156
+ String keyAlias = keystoreInfo .getKeyAlias ();
136
157
137
- // Check if cert and key if already registered inside our keystore
138
- // If one is missing we insert again
139
- boolean hasCertInStore = keyStore .isCertificateEntry (keystoreInfo .getCertAlias ());
140
- boolean hasKeyInStore = keyStore .isKeyEntry (keystoreInfo .getKeyAlias ());
141
- if (!hasCertInStore || !hasKeyInStore ) {
142
- InputStream certInput = getResolvableinputStream (context , optionResCert );
143
- Certificate cert = CertificateFactory .getInstance ("X.509" ).generateCertificate (certInput );
144
- keyStore .setCertificateEntry (keystoreInfo .getCertAlias (), cert );
145
-
146
- InputStream keyInput = getResolvableinputStream (context , optionResKey );
147
- PrivateKey privateKey = getPrivateKeyFromPEM (keyInput );
148
- 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 ;
149
165
}
166
+ } else if (optionResCert != null && optionResKey != null ) {
167
+
168
+ keyStore = KeyStore .getInstance (keyStoreName );
169
+ keyStore .load (null , null );
150
170
151
- boolean hasCaInStore = keyStore .isCertificateEntry (keystoreInfo .getCaAlias ());
152
- 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 ) {
153
180
InputStream caInput = getResolvableinputStream (context , optionResCa );
154
181
// Generate the CA Certificate from the raw resource file
155
182
Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
156
183
caInput .close ();
157
184
// Load the key store using the CA
158
185
keyStore .setCertificateEntry (keystoreInfo .getCaAlias (), ca );
159
186
}
160
-
187
+ }
188
+
189
+ if (keyStore != null ) {
161
190
// Initialize the KeyManagerFactory with this cert
162
191
KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
163
192
keyManagerFactory .init (keyStore , new char [0 ]);
@@ -166,15 +195,14 @@ static SSLSocketFactory createCustomTrustedSocketFactory(
166
195
SSLContext sslContext = SSLContext .getInstance ("TLS" );
167
196
sslContext .init (keyManagerFactory .getKeyManagers (), new TrustManager []{new BlindTrustManager ()}, null );
168
197
return sslContext .getSocketFactory ();
169
-
170
198
} else {
171
199
// Keep old behavior
172
200
InputStream caInput = getResolvableinputStream (context , optionResCa );
173
201
// Generate the CA Certificate from the raw resource file
174
202
Certificate ca = CertificateFactory .getInstance ("X.509" ).generateCertificate (caInput );
175
203
caInput .close ();
176
204
// Load the key store using the CA
177
- KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
205
+ keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
178
206
keyStore .load (null , null );
179
207
keyStore .setCertificateEntry ("ca" , ca );
180
208
0 commit comments