package org.snmp4j.transport.tls;

import java.security.cert.X509Certificate;
import java.util.Properties;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OctetString;
import org.snmp4j.util.SnmpConfigurator;

/* JADX INFO: loaded from: classes.dex */
public class PropertiesTlsTmSecurityCallback implements TlsTmSecurityCallback<X509Certificate> {
    private static final LogAdapter LOGGER = LogFactory.getLogger(PropertiesTlsTmSecurityCallback.class);
    private Properties properties;
    private boolean serverMode;

    public PropertiesTlsTmSecurityCallback(boolean z) {
        this(System.getProperties(), z);
    }

    @Override // org.snmp4j.transport.tls.TlsTmSecurityCallback
    public String getLocalCertificateAlias(Address address) {
        return this.properties.getProperty(SnmpConfigurator.P_TLS_LOCAL_ID);
    }

    public PropertiesTlsTmSecurityCallback(Properties properties, boolean z) {
        this.serverMode = z;
        if (properties == null) {
            throw null;
        }
        this.properties = properties;
    }

    @Override // org.snmp4j.transport.tls.TlsTmSecurityCallback
    public OctetString getSecurityName(X509Certificate[] x509CertificateArr) {
        String property = this.properties.getProperty(SnmpConfigurator.P_SECURITY_NAME, null);
        if (property != null) {
            return new OctetString(property);
        }
        return null;
    }

    @Override // org.snmp4j.transport.tls.TlsTmSecurityCallback
    public boolean isAcceptedIssuer(X509Certificate x509Certificate) {
        return x509Certificate.getIssuerDN().getName().equals(this.properties.getProperty(SnmpConfigurator.P_TLS_TRUST_CA, ""));
    }

    @Override // org.snmp4j.transport.tls.TlsTmSecurityCallback
    public boolean isClientCertificateAccepted(X509Certificate x509Certificate) {
        String property = this.properties.getProperty(SnmpConfigurator.P_TLS_LOCAL_ID, "");
        if (this.serverMode) {
            property = this.properties.getProperty(SnmpConfigurator.P_TLS_PEER_ID, "");
        }
        return x509Certificate.getSubjectDN().getName().equals(property);
    }

    @Override // org.snmp4j.transport.tls.TlsTmSecurityCallback
    public boolean isServerCertificateAccepted(X509Certificate[] x509CertificateArr) {
        String property = this.properties.getProperty(SnmpConfigurator.P_TLS_PEER_ID, "");
        if (this.serverMode) {
            property = this.properties.getProperty(SnmpConfigurator.P_TLS_LOCAL_ID, "");
        }
        String name = x509CertificateArr[0].getSubjectDN().getName();
        if (name.equals(property)) {
            return true;
        }
        LOGGER.warn("Certificate subject '" + name + "' does not match accepted peer '" + property + "'");
        String property2 = this.properties.getProperty(SnmpConfigurator.P_TLS_TRUST_CA, "");
        for (int i2 = 1; i2 < x509CertificateArr.length; i2++) {
            String name2 = x509CertificateArr[i2].getIssuerDN().getName();
            if (name2.equals(property2)) {
                return true;
            }
            LOGGER.warn("Certification authority '" + name2 + "' does not match accepted CA '" + property2 + "'");
        }
        return false;
    }
}
