/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.message.SecurityKnob
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.WsSecurity
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.keys.UnsupportedKeyTypeException
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.security.xml.decorator.DecoratorException
 *  com.l7tech.security.xml.decorator.WssDecorator
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.InvalidDocumentFormatException
 *  com.l7tech.util.SoapConstants
 *  com.l7tech.xml.soap.SoapUtil
 *  com.l7tech.xml.soap.SoapVersion
 *  org.jetbrains.annotations.Nullable
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.message.SecurityKnob;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.WsSecurity;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.keys.UnsupportedKeyTypeException;
import com.l7tech.security.xml.decorator.DecorationRequirements;
import com.l7tech.security.xml.decorator.DecoratorException;
import com.l7tech.security.xml.decorator.WssDecorator;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.server.identity.cert.TrustedCertCache;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.xmlsec.d;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.server.policy.variable.ServerVariables;
import com.l7tech.server.util.WSSecurityProcessorUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.util.SoapConstants;
import com.l7tech.xml.soap.SoapUtil;
import com.l7tech.xml.soap.SoapVersion;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServerWsSecurity
extends AbstractMessageTargetableServerAssertion<WsSecurity> {
    private final TrustedCertCache d;
    private final WssDecorator e;
    private static final String[] f;

    public ServerWsSecurity(WsSecurity wsSecurity, ApplicationContext applicationContext) {
        super(wsSecurity);
        String[] stringArray = f;
        this.d = (TrustedCertCache)applicationContext.getBean(stringArray[0], TrustedCertCache.class);
        this.e = (WssDecorator)applicationContext.getBean(stringArray[2], WssDecorator.class);
    }

    /*
     * Loose catch block
     */
    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        try {
            if (!message.isSoap()) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_NON_SOAP);
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (DecoratorException decoratorException) {
            throw ServerWsSecurity.a((Exception)((Object)decoratorException));
        }
        catch (SAXException sAXException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_NON_SOAP);
            return AssertionStatus.NOT_APPLICABLE;
        }
        SecurityKnob securityKnob = message.getSecurityKnob();
        try {
            Object object;
            block29: {
                Element element;
                ArrayList<String> arrayList;
                Document document;
                block31: {
                    block30: {
                        block28: {
                            document = message.getXmlKnob().getDocumentWritable();
                            arrayList = new ArrayList<String>();
                            if (((WsSecurity)this.assertion).isApplyWsSecurity()) {
                                object = securityKnob.getDecorationRequirements();
                                SoapVersion soapVersion = SoapVersion.namespaceToSoapVersion((String)document.getDocumentElement().getNamespaceURI());
                                try {
                                    if (((WsSecurity)this.assertion).getWsSecurityVersion() != null) {
                                        securityKnob.setPolicyWssVersion(((WsSecurity)this.assertion).getWsSecurityVersion());
                                    }
                                }
                                catch (SAXException sAXException) {
                                    throw ServerWsSecurity.a(sAXException);
                                }
                                try {
                                    if (object == null || ((DecorationRequirements[])object).length <= 0) break block28;
                                }
                                catch (SAXException sAXException) {
                                    throw ServerWsSecurity.a(sAXException);
                                }
                                for (DecorationRequirements decorationRequirements : object) {
                                    this.a(policyEnforcementContext, decorationRequirements, ((WsSecurity)this.assertion).isUseSecureSpanActor(), arrayList);
                                    this.a(decorationRequirements, document, soapVersion, securityKnob, ((WsSecurity)this.assertion).isReplaceSecurityHeader(), ((WsSecurity)this.assertion).isUseSecurityHeaderMustUnderstand());
                                    this.a(decorationRequirements, ((WsSecurity)this.assertion).getRecipientTrustedCertificateGoid(), this.a(policyEnforcementContext, ((WsSecurity)this.assertion).getRecipientTrustedCertificateName()), this.b(policyEnforcementContext, ((WsSecurity)this.assertion).getRecipientTrustedCertificateVariable()));
                                    try {
                                        WSSecurityProcessorUtils.addSignatureConfirmations(message, this.getAudit());
                                        this.e.decorateMessage(message, decorationRequirements);
                                    }
                                    catch (UnsupportedKeyTypeException | DecoratorException throwable) {
                                        this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_ERROR, new String[]{((WsSecurity)this.assertion).getTargetName(), ExceptionUtils.getMessage((Throwable)throwable)}, ExceptionUtils.getDebugException((Throwable)throwable));
                                        throw new AssertionStatusException(AssertionStatus.FALSIFIED);
                                    }
                                }
                                securityKnob.removeAllDecorationRequirements();
                            }
                        }
                        if (!((WsSecurity)this.assertion).isRemoveUnmatchedSecurityHeaders()) break block29;
                        try {
                            block32: {
                                if (!arrayList.contains(SoapConstants.L7_SOAP_ACTOR)) break block30;
                                break block32;
                                catch (SAXException sAXException) {
                                    throw ServerWsSecurity.a(sAXException);
                                }
                            }
                            element = SoapUtil.getSecurityElementForL7((Document)document);
                            break block31;
                        }
                        catch (SAXException sAXException) {
                            throw ServerWsSecurity.a(sAXException);
                        }
                    }
                    element = null;
                }
                object = element;
                for (Object object2 : SoapUtil.getSecurityElements((Document)document)) {
                    String string2 = SoapUtil.getActorValue((Element)object2);
                    if (arrayList.contains(string2)) continue;
                    try {
                        block33: {
                            if (object == object2) continue;
                            break block33;
                            catch (SAXException sAXException) {
                                throw ServerWsSecurity.a(sAXException);
                            }
                        }
                        object2.getParentNode().removeChild((Node)object2);
                        securityKnob.removeDecorationResults(string2);
                    }
                    catch (SAXException sAXException) {
                        throw ServerWsSecurity.a(sAXException);
                    }
                }
                SoapUtil.removeEmptySoapHeader((Document)document);
            }
            if (((WsSecurity)this.assertion).isClearDecorationRequirements()) {
                securityKnob.removeAllDecorationRequirements();
                object = ServerVariables.getRootContext(policyEnforcementContext);
                object.addDeferredAssertion(this, new d(this, this.assertion, securityKnob));
            }
            return AssertionStatus.NONE;
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_ERROR, new String[]{((WsSecurity)this.assertion).getTargetName(), ExceptionUtils.getMessage((Throwable)invalidDocumentFormatException)}, invalidDocumentFormatException);
        }
        catch (GeneralSecurityException generalSecurityException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_ERROR, new String[]{((WsSecurity)this.assertion).getTargetName(), ExceptionUtils.getMessage((Throwable)generalSecurityException)}, generalSecurityException);
        }
        catch (SAXException sAXException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_ERROR, new String[]{((WsSecurity)this.assertion).getTargetName(), ExceptionUtils.getMessage((Throwable)sAXException)}, sAXException);
        }
        catch (AssertionStatusException assertionStatusException) {
            return assertionStatusException.status;
        }
        return AssertionStatus.FALSIFIED;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, DecorationRequirements decorationRequirements, boolean bl2, List<String> list) {
        block10: {
            block7: {
                block8: {
                    String string;
                    block9: {
                        try {
                            try {
                                if (!SoapConstants.L7_SOAP_ACTOR.equals(decorationRequirements.getSecurityHeaderActor()) && decorationRequirements.getSecurityHeaderActor() != null) break block7;
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw ServerWsSecurity.a(illegalArgumentException);
                            }
                            if (!bl2) break block8;
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw ServerWsSecurity.a(illegalArgumentException);
                        }
                        string = SoapConstants.L7_SOAP_ACTOR;
                        if (this.isResponse()) {
                            ProcessorResult processorResult = policyEnforcementContext.getRequest().getSecurityKnob().getProcessorResult();
                            try {
                                if (processorResult == null || processorResult.getProcessedActorUri() == null) break block9;
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw ServerWsSecurity.a(illegalArgumentException);
                            }
                            string = processorResult.getProcessedActorUri();
                        }
                    }
                    decorationRequirements.setSecurityHeaderActor(string);
                    list.add(string);
                    break block10;
                }
                decorationRequirements.setSecurityHeaderActor(null);
                list.add(null);
                break block10;
            }
            list.add(decorationRequirements.getSecurityHeaderActor());
        }
    }

    private void a(DecorationRequirements decorationRequirements, Document document, SoapVersion soapVersion, SecurityKnob securityKnob, boolean bl2, boolean bl3) throws InvalidDocumentFormatException {
        block19: {
            block17: {
                Element element;
                String string;
                block18: {
                    string = decorationRequirements.getSecurityHeaderActor();
                    element = SoapUtil.getSecurityElement((Document)document, (String)string);
                    try {
                        try {
                            if (element == null) break block17;
                            if (!bl2) break block18;
                        }
                        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                            throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                        }
                        element.getParentNode().removeChild(element);
                        decorationRequirements.setSecurityHeaderMustUnderstand(Boolean.valueOf(bl3), false);
                        securityKnob.removeDecorationResults(string);
                        break block19;
                    }
                    catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                        throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                    }
                }
                String string2 = document.getDocumentElement().getNamespaceURI();
                decorationRequirements.setSecurityHeaderReusable(true);
                if (SoapVersion.SOAP_1_2.isPriorVersion(soapVersion)) {
                    block22: {
                        block20: {
                            block21: {
                                boolean bl4 = element.hasAttribute(SoapConstants.ACTOR_ATTR_NAME);
                                try {
                                    try {
                                        element.removeAttribute(SoapConstants.ACTOR_ATTR_NAME);
                                        element.removeAttributeNS(string2, SoapConstants.ACTOR_ATTR_NAME);
                                        if (string == null) break block20;
                                        if (!bl4) break block21;
                                    }
                                    catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                                        throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                                    }
                                    element.setAttributeNS(null, SoapConstants.ACTOR_ATTR_NAME, string);
                                    break block20;
                                }
                                catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                                    throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                                }
                            }
                            SoapUtil.setSoapAttr((Element)element, (String)SoapConstants.ACTOR_ATTR_NAME, (String)string);
                        }
                        try {
                            if (!bl3) break block22;
                            SoapUtil.setSoapAttr((Element)element, (String)SoapConstants.MUSTUNDERSTAND_ATTR_NAME, (String)"1");
                            break block19;
                        }
                        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                            throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                        }
                    }
                    element.removeAttributeNS(string2, SoapConstants.MUSTUNDERSTAND_ATTR_NAME);
                } else {
                    block23: {
                        try {
                            element.removeAttributeNS(string2, SoapConstants.ROLE_ATTR_NAME);
                            if (string != null) {
                                SoapUtil.setSoapAttr((Element)element, (String)SoapConstants.ROLE_ATTR_NAME, (String)string);
                            }
                        }
                        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                            throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                        }
                        try {
                            if (!bl3) break block23;
                            SoapUtil.setSoapAttr((Element)element, (String)SoapConstants.MUSTUNDERSTAND_ATTR_NAME, (String)f[4]);
                            break block19;
                        }
                        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                            throw ServerWsSecurity.a((Exception)((Object)invalidDocumentFormatException));
                        }
                    }
                    element.removeAttributeNS(string2, SoapConstants.MUSTUNDERSTAND_ATTR_NAME);
                }
                break block19;
            }
            decorationRequirements.setSecurityHeaderMustUnderstand(Boolean.valueOf(bl3), false);
        }
    }

    /*
     * Unable to fully structure code
     */
    private void a(DecorationRequirements var1_1, Goid var2_2, @Nullable String var3_3, @Nullable X509Certificate var4_4) throws AssertionStatusException {
        block14: {
            var5_5 = "";
            try {
                block18: {
                    block15: {
                        block17: {
                            block16: {
                                block19: {
                                    if (var2_2 != null) {
                                        block13: {
                                            var11_6 = ServerWsSecurity.f;
                                            var5_5 = var11_6[6] + String.valueOf(var2_2);
                                            var6_7 = this.d.findByPrimaryKey(var2_2);
                                            try {
                                                if (var6_7 == null) break block13;
                                                var1_1.setRecipientCertificate(var6_7.getCertificate());
                                                break block14;
                                            }
                                            catch (FindException v0) {
                                                throw ServerWsSecurity.a((Exception)v0);
                                            }
                                        }
                                        this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_RECIP_NO_CERT, new String[]{var5_5});
                                        throw new AssertionStatusException(AssertionStatus.FALSIFIED);
                                    }
                                    if (var3_3 == null) break block18;
                                    var5_5 = ServerWsSecurity.f[3] + var3_3;
                                    var6_8 = this.d.findByName(var3_3);
                                    var7_10 = null;
                                    var8_11 = null;
                                    for (TrustedCert var10_13 : var6_8) {
                                        if (!this.a(var10_13)) {
                                            var7_10 = var10_13.getCertificate();
                                            break;
                                        }
                                        if (var8_11 != null) continue;
                                        var8_11 = var10_13.getCertificate();
                                    }
                                    if (var7_10 != null) ** GOTO lbl35
                                    if (var8_11 == null) break block15;
                                    break block19;
                                    catch (FindException v1) {
                                        throw ServerWsSecurity.a((Exception)v1);
                                    }
                                }
                                try {
                                    block20: {
                                        v2 = var1_1;
                                        if (var7_10 == null) break block16;
                                        break block20;
                                        catch (FindException v3) {
                                            throw ServerWsSecurity.a((Exception)v3);
                                        }
                                    }
                                    v4 = var7_10;
                                    break block17;
                                }
                                catch (FindException v5) {
                                    throw ServerWsSecurity.a((Exception)v5);
                                }
                            }
                            v4 = var8_11;
                        }
                        v2.setRecipientCertificate(v4);
                        break block14;
                    }
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_RECIP_NO_CERT, new String[]{var5_5});
                    throw new AssertionStatusException(AssertionStatus.FALSIFIED);
                }
                var1_1.setRecipientCertificate(var4_4);
            }
            catch (FindException var6_9) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_RECIP_CERT_ERROR, new String[]{var5_5});
                throw new AssertionStatusException(AssertionStatus.FALSIFIED);
            }
        }
    }

    private boolean a(TrustedCert trustedCert) {
        boolean bl2 = true;
        try {
            bl2 = trustedCert.isExpiredCert();
        }
        catch (CertificateException certificateException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_RECIP_CERT_EXP, new String[]{trustedCert.getName() + f[1] + String.valueOf(trustedCert.getGoid()) + ")"}, certificateException);
        }
        return bl2;
    }

    private String a(PolicyEnforcementContext policyEnforcementContext, String string) throws AssertionStatusException {
        String string2 = null;
        if (string != null) {
            String[] stringArray = Syntax.getReferencedNames((String)string);
            Map<String, Object> map = policyEnforcementContext.getVariableMap(stringArray, this.getAudit());
            try {
                string2 = ExpandVariables.process(string, map, this.getAudit(), true);
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw new AssertionStatusException(AssertionStatus.FALSIFIED);
            }
        }
        return string2;
    }

    private X509Certificate b(PolicyEnforcementContext policyEnforcementContext, String string) throws AssertionStatusException {
        X509Certificate x509Certificate = null;
        if (string != null) {
            String string2 = Syntax.getVariableExpression((String)string);
            String[] stringArray = Syntax.getReferencedNames((String)string2);
            Map<String, Object> map = policyEnforcementContext.getVariableMap(stringArray, this.getAudit());
            try {
                Object object = ExpandVariables.processSingleVariableAsObject(string2, map, this.getAudit(), true);
                if (object instanceof X509Certificate) {
                    x509Certificate = (X509Certificate)object;
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw new AssertionStatusException(AssertionStatus.FALSIFIED);
            }
            try {
                if (x509Certificate == null) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.WSSECURITY_RECIP_NO_CERT, f[5] + string);
                    throw new AssertionStatusException(AssertionStatus.FALSIFIED);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerWsSecurity.a(illegalArgumentException);
            }
        }
        return x509Certificate;
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = "}E[\u000b-S\u001cJR\\\f\u001aW\u001baR\u0003)\u001f\r\f~D]<<U\u0017{VZ\u0017+\u0005gVC\u001dy\u0004}E[\u001d";
                var4_3 = "}E[\u000b-S\u001cJR\\\f\u001aW\u001baR\u0003)\u001f\r\f~D]<<U\u0017{VZ\u0017+\u0005gVC\u001dy\u0004}E[\u001d".length();
                var1_4 = 16;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 69;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerWsSecurity.c(v0, ServerWsSecurity.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "]t~3\u001av6N5\u0004Bq,y";
                    var4_3 = "]t~3\u001av6N5\u0004Bq,y".length();
                    var1_4 = 9;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 103;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerWsSecurity.c(v0, ServerWsSecurity.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        ServerWsSecurity.f = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x3D);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 76;
                    case 1 -> 114;
                    case 2 -> 107;
                    case 3 -> 61;
                    case 4 -> 28;
                    case 5 -> 115;
                    default -> 61;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    private static class AssertionStatusException
    extends Exception {
        final AssertionStatus status;
        static final long serialVersionUID = -9150194779507359982L;

        AssertionStatusException(AssertionStatus assertionStatus) {
            this.status = assertionStatus;
        }
    }
}

