160 lines
6.7 KiB
Java
160 lines
6.7 KiB
Java
|
package org.jmrtd;
|
||
|
|
||
|
import com.kofax.mobile.sdk.w.ao;
|
||
|
import java.security.Provider;
|
||
|
import java.security.Security;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Arrays;
|
||
|
import java.util.Collections;
|
||
|
import java.util.List;
|
||
|
import java.util.logging.Logger;
|
||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class JMRTDSecurityProvider extends Provider {
|
||
|
private static final Provider BC_PROVIDER;
|
||
|
private static final String BC_PROVIDER_CLASS_NAME = "org.bouncycastle.jce.provider.BouncyCastleProvider";
|
||
|
private static final Provider JMRTD_PROVIDER;
|
||
|
private static final Logger LOGGER = Logger.getLogger("org.jmrtd");
|
||
|
private static final Provider SC_PROVIDER;
|
||
|
private static final String SC_PROVIDER_CLASS_NAME = "o.gqX";
|
||
|
private static final String SUN_PROVIDER_CLASS_NAME = "sun.security.provider.Sun";
|
||
|
private static final long serialVersionUID = -2881416441551680704L;
|
||
|
|
||
|
static {
|
||
|
BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
|
||
|
BC_PROVIDER = bouncyCastleProvider;
|
||
|
BouncyCastleProvider bouncyCastleProvider2 = new BouncyCastleProvider();
|
||
|
SC_PROVIDER = bouncyCastleProvider2;
|
||
|
JMRTDSecurityProvider jMRTDSecurityProvider = new JMRTDSecurityProvider();
|
||
|
JMRTD_PROVIDER = jMRTDSecurityProvider;
|
||
|
Security.insertProviderAt(bouncyCastleProvider, 1);
|
||
|
Security.insertProviderAt(bouncyCastleProvider2, 2);
|
||
|
Security.insertProviderAt(jMRTDSecurityProvider, 3);
|
||
|
}
|
||
|
|
||
|
private JMRTDSecurityProvider() {
|
||
|
super("JMRTD", 0.1d, "JMRTD Security Provider");
|
||
|
put("CertificateFactory.CVC", "org.jmrtd.cert.CVCertificateFactorySpi");
|
||
|
if (BC_PROVIDER != null) {
|
||
|
replicateFromProvider("CertificateFactory", "X.509", getBouncyCastleProvider());
|
||
|
replicateFromProvider("CertStore", "Collection", getBouncyCastleProvider());
|
||
|
replicateFromProvider("MessageDigest", "SHA1", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA1withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "MD2withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "MD4withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "MD5withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA1withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA1withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA256withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA256withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA384withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA384withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA512withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA512withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA224withRSA", getBouncyCastleProvider());
|
||
|
replicateFromProvider(ao.Qz, "SHA224withRSA/ISO9796-2", getBouncyCastleProvider());
|
||
|
put("Alg.Alias.Mac.ISO9797Alg3Mac", "ISO9797ALG3MAC");
|
||
|
put("Alg.Alias.CertificateFactory.X509", "X.509");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int beginPreferBouncyCastleProvider() {
|
||
|
Provider bouncyCastleProvider = getBouncyCastleProvider();
|
||
|
if (bouncyCastleProvider == null) {
|
||
|
return -1;
|
||
|
}
|
||
|
Provider[] providers = Security.getProviders();
|
||
|
for (int i = 0; i < providers.length; i++) {
|
||
|
Provider provider = providers[i];
|
||
|
if (bouncyCastleProvider.getClass().getCanonicalName().equals(provider.getClass().getCanonicalName())) {
|
||
|
Security.removeProvider(provider.getName());
|
||
|
Security.insertProviderAt(bouncyCastleProvider, 1);
|
||
|
return i + 1;
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
public static void endPreferBouncyCastleProvider(int i) {
|
||
|
Provider bouncyCastleProvider = getBouncyCastleProvider();
|
||
|
Security.removeProvider(bouncyCastleProvider.getName());
|
||
|
if (i > 0) {
|
||
|
Security.insertProviderAt(bouncyCastleProvider, i);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static Provider getBouncyCastleProvider() {
|
||
|
Provider provider = BC_PROVIDER;
|
||
|
if (provider != null) {
|
||
|
return provider;
|
||
|
}
|
||
|
Provider provider2 = SC_PROVIDER;
|
||
|
if (provider2 != null) {
|
||
|
return provider2;
|
||
|
}
|
||
|
LOGGER.severe("No Bouncy or Spongy provider");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static Provider getSpongyCastleProvider() {
|
||
|
Provider provider = SC_PROVIDER;
|
||
|
if (provider != null) {
|
||
|
return provider;
|
||
|
}
|
||
|
Provider provider2 = BC_PROVIDER;
|
||
|
if (provider2 != null) {
|
||
|
return provider2;
|
||
|
}
|
||
|
LOGGER.severe("No Bouncy or Spongy provider");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
private void replicateFromProvider(String str, String str2, Provider provider) {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.append(str);
|
||
|
sb.append(".");
|
||
|
sb.append(str2);
|
||
|
String obj = sb.toString();
|
||
|
Object obj2 = provider.get(obj);
|
||
|
if (obj2 != null) {
|
||
|
put(obj, obj2);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static Provider getProvider(String str, String str2) {
|
||
|
List<Provider> providers = getProviders(str, str2);
|
||
|
if (providers == null || providers.size() <= 0) {
|
||
|
return null;
|
||
|
}
|
||
|
return providers.get(0);
|
||
|
}
|
||
|
|
||
|
private static List<Provider> getProviders(String str, String str2) {
|
||
|
if (Security.getAlgorithms(str).contains(str2)) {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
sb.append(str);
|
||
|
sb.append(".");
|
||
|
sb.append(str2);
|
||
|
return new ArrayList(Arrays.asList(Security.getProviders(sb.toString())));
|
||
|
}
|
||
|
Provider provider = BC_PROVIDER;
|
||
|
if (provider != null && provider.getService(str, str2) != null) {
|
||
|
return new ArrayList(Collections.singletonList(provider));
|
||
|
}
|
||
|
Provider provider2 = SC_PROVIDER;
|
||
|
if (provider2 != null && provider2.getService(str, str2) != null) {
|
||
|
return new ArrayList(Collections.singletonList(provider2));
|
||
|
}
|
||
|
Provider provider3 = JMRTD_PROVIDER;
|
||
|
if (provider3 == null || provider3.getService(str, str2) == null) {
|
||
|
return null;
|
||
|
}
|
||
|
return new ArrayList(Collections.singletonList(provider3));
|
||
|
}
|
||
|
|
||
|
public static Provider getInstance() {
|
||
|
return JMRTD_PROVIDER;
|
||
|
}
|
||
|
}
|