what-the-bank/sources/io/grpc/util/AdvancedTlsX509KeyManager.java

203 lines
8.0 KiB
Java

package io.grpc.util;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import o.C13001fNn;
/* loaded from: classes6.dex */
public final class AdvancedTlsX509KeyManager extends X509ExtendedKeyManager {
private static final Logger log = Logger.getLogger(AdvancedTlsX509KeyManager.class.getName());
private volatile KeyInfo keyInfo;
/* loaded from: classes6.dex */
public interface Closeable extends java.io.Closeable {
@Override // java.io.Closeable, java.lang.AutoCloseable
void close();
}
@Override // javax.net.ssl.X509KeyManager
public final PrivateKey getPrivateKey(String str) {
if (str.equals(C13001fNn.DEFAULT_IDENTIFIER)) {
return this.keyInfo.key;
}
return null;
}
@Override // javax.net.ssl.X509KeyManager
public final X509Certificate[] getCertificateChain(String str) {
if (str.equals(C13001fNn.DEFAULT_IDENTIFIER)) {
return (X509Certificate[]) Arrays.copyOf(this.keyInfo.certs, this.keyInfo.certs.length);
}
return null;
}
@Override // javax.net.ssl.X509KeyManager
public final String[] getClientAliases(String str, Principal[] principalArr) {
return new String[]{C13001fNn.DEFAULT_IDENTIFIER};
}
@Override // javax.net.ssl.X509KeyManager
public final String[] getServerAliases(String str, Principal[] principalArr) {
return new String[]{C13001fNn.DEFAULT_IDENTIFIER};
}
public final void updateIdentityCredentials(PrivateKey privateKey, X509Certificate[] x509CertificateArr) {
this.keyInfo = new KeyInfo((PrivateKey) Preconditions.checkNotNull(privateKey, "key"), (X509Certificate[]) Preconditions.checkNotNull(x509CertificateArr, "certs"));
}
public final Closeable updateIdentityCredentialsFromFile(File file, File file2, long j, TimeUnit timeUnit, ScheduledExecutorService scheduledExecutorService) throws IOException, GeneralSecurityException {
if (!readAndUpdate(file, file2, 0L, 0L).success) {
throw new GeneralSecurityException("Files were unmodified before their initial update. Probably a bug.");
}
return new Closeable(this, scheduledExecutorService.scheduleWithFixedDelay(new LoadFilePathExecution(this, file, file2), j, j, timeUnit)) { // from class: io.grpc.util.AdvancedTlsX509KeyManager.1
final AdvancedTlsX509KeyManager this$0;
final ScheduledFuture val$future;
{
this.this$0 = this;
this.val$future = r2;
}
@Override // io.grpc.util.AdvancedTlsX509KeyManager.Closeable, java.io.Closeable, java.lang.AutoCloseable
public void close() {
this.val$future.cancel(false);
}
};
}
public final void updateIdentityCredentialsFromFile(File file, File file2) throws IOException, GeneralSecurityException {
if (!readAndUpdate(file, file2, 0L, 0L).success) {
throw new GeneralSecurityException("Files were unmodified before their initial update. Probably a bug.");
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static class KeyInfo {
final X509Certificate[] certs;
final PrivateKey key;
public KeyInfo(PrivateKey privateKey, X509Certificate[] x509CertificateArr) {
this.key = privateKey;
this.certs = x509CertificateArr;
}
}
/* loaded from: classes6.dex */
class LoadFilePathExecution implements Runnable {
File certFile;
File keyFile;
final AdvancedTlsX509KeyManager this$0;
long currentKeyTime = 0;
long currentCertTime = 0;
public LoadFilePathExecution(AdvancedTlsX509KeyManager advancedTlsX509KeyManager, File file, File file2) {
this.this$0 = advancedTlsX509KeyManager;
this.keyFile = file;
this.certFile = file2;
}
@Override // java.lang.Runnable
public void run() {
try {
UpdateResult readAndUpdate = this.this$0.readAndUpdate(this.keyFile, this.certFile, this.currentKeyTime, this.currentCertTime);
if (readAndUpdate.success) {
this.currentKeyTime = readAndUpdate.keyTime;
this.currentCertTime = readAndUpdate.certTime;
}
} catch (IOException | GeneralSecurityException e) {
AdvancedTlsX509KeyManager.log.log(Level.SEVERE, "Failed refreshing private key and certificate chain from files. Using previous ones", e);
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes6.dex */
public static class UpdateResult {
long certTime;
long keyTime;
boolean success;
public UpdateResult(boolean z, long j, long j2) {
this.success = z;
this.keyTime = j;
this.certTime = j2;
}
}
/* JADX INFO: Access modifiers changed from: private */
public UpdateResult readAndUpdate(File file, File file2, long j, long j2) throws IOException, GeneralSecurityException {
long lastModified = file.lastModified();
long lastModified2 = file2.lastModified();
if (lastModified != j && lastModified2 != j2) {
FileInputStream fileInputStream = new FileInputStream(file);
try {
try {
PrivateKey privateKey = CertificateUtils.getPrivateKey(fileInputStream);
FileInputStream fileInputStream2 = new FileInputStream(file2);
try {
try {
updateIdentityCredentials(privateKey, CertificateUtils.getX509Certificates(fileInputStream2));
UpdateResult updateResult = new UpdateResult(true, lastModified, lastModified2);
fileInputStream2.close();
fileInputStream.close();
return updateResult;
} catch (Throwable th) {
th = th;
fileInputStream2.close();
throw th;
}
} catch (Throwable th2) {
th = th2;
}
} catch (Throwable th3) {
th = th3;
fileInputStream.close();
throw th;
}
} catch (Throwable th4) {
th = th4;
fileInputStream.close();
throw th;
}
} else {
return new UpdateResult(false, j, j2);
}
}
@Override // javax.net.ssl.X509KeyManager
public final String chooseServerAlias(String str, Principal[] principalArr, Socket socket) {
return C13001fNn.DEFAULT_IDENTIFIER;
}
@Override // javax.net.ssl.X509ExtendedKeyManager
public final String chooseEngineServerAlias(String str, Principal[] principalArr, SSLEngine sSLEngine) {
return C13001fNn.DEFAULT_IDENTIFIER;
}
@Override // javax.net.ssl.X509ExtendedKeyManager
public final String chooseEngineClientAlias(String[] strArr, Principal[] principalArr, SSLEngine sSLEngine) {
return C13001fNn.DEFAULT_IDENTIFIER;
}
@Override // javax.net.ssl.X509KeyManager
public final String chooseClientAlias(String[] strArr, Principal[] principalArr, Socket socket) {
return C13001fNn.DEFAULT_IDENTIFIER;
}
}