'Using .pfx certificate in https request error
I have a problem using pfx certificate in my app. I've really tried to do a lot, but I can't send the certificate properly.
public class WebApiManager {
private static WebApiManager webApiManager;
private Context appContext;
private WebApi webApi;
private OkHttpClient.Builder client;
private WebApiManager(Context appContext) {
this.appContext = appContext;
}
public static WebApiManager get(Context context) {
if (webApiManager == null) {
webApiManager = new WebApiManager(context.getApplicationContext());
}
return webApiManager;
}
Gson gson = new GsonBuilder()
.setLenient()
.create();
public WebApi getWebApi() throws UnrecoverableKeyException, CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {
if (webApi == null) {
webApi = new Retrofit.Builder()
.baseUrl(WebApi.BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(WebApi.class);
}
return webApi;
}
OkHttpClient getOkHttpClient() throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException {
client = new OkHttpClient.Builder()
.connectTimeout(WebApi.TIMEOUT, TimeUnit.SECONDS)
.readTimeout(WebApi.TIMEOUT, TimeUnit.SECONDS);
InputStream inputStream = appContext.getResources().openRawResource(R.raw.stefan);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, "nusiceva5".toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
keyManagerFactory.init(keyStore, "nusiceva5".toCharArray());
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
return client
.sslSocketFactory(sslContext.getSocketFactory())
.protocols(Util.immutableListOf(Protocol.HTTP_1_1))
.build();
}
}
And the error i make.
java.io.IOException: exception unwrapping private key - java.security.NoSuchAlgorithmException: 1.2.840.113549.1.5.12 SecretKeyFactory not available W/System.err: at com.android.org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.unwrapKey(PKCS12KeyStoreSpi.java:652) W/System.err: at com.android.org.bouncycastle.jcajce.provider.keystore.pkcs12.PKCS12KeyStoreSpi.engineLoad(PKCS12KeyStoreSpi.java:891) W/System.err: at java.security.KeyStore.load(KeyStore.java:1484)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
