Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ClientCertTLSSocketFactory.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a78db9ce16f0d516da750a9aa7d21d9381cc67cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.mapswithme.util;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;

public class ClientCertTLSSocketFactory
{
  private static final String PROTOCOL = "TLS";
  private static final String ALGORITHM = "X509";
  private static final String KEY_STORE_TYPE = "PKCS12";

  @NonNull
  public static SSLSocketFactory create(@NonNull byte[] payload, @Nullable char[] password)
  {
    InputStream inputStream = null;
    try
    {
      inputStream = new ByteArrayInputStream(payload);
      KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
      keyStore.load(inputStream, password);
      KeyManagerFactory kmf = KeyManagerFactory.getInstance(ALGORITHM);
      kmf.init(keyStore, null);
      SSLContext sslContext = SSLContext.getInstance(PROTOCOL);
      sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());
      return sslContext.getSocketFactory();
    }
    catch (Exception ex)
    {
      throw new RuntimeException(ex);
    }
    finally
    {
      Utils.closeSafely(inputStream);
    }
  }
}