/*
 * 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.MessageRole
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.WssConfigurationAssertion
 *  com.l7tech.security.xml.KeyInfoInclusionType
 *  com.l7tech.security.xml.KeyReference
 *  com.l7tech.security.xml.SignerInfo
 *  com.l7tech.security.xml.WsSecurityVersion
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.security.xml.decorator.DecorationRequirements$WsaHeaderSigningStrategy
 *  com.l7tech.util.ExceptionUtils
 *  org.springframework.beans.factory.BeanFactory
 */
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.MessageRole;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.WssConfigurationAssertion;
import com.l7tech.security.xml.KeyInfoInclusionType;
import com.l7tech.security.xml.KeyReference;
import com.l7tech.security.xml.SignerInfo;
import com.l7tech.security.xml.WsSecurityVersion;
import com.l7tech.security.xml.decorator.DecorationRequirements;
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.ServerAssertionUtils;
import com.l7tech.util.ExceptionUtils;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.UnrecoverableKeyException;
import org.springframework.beans.factory.BeanFactory;

public class ServerWssConfigurationAssertion
extends AbstractMessageTargetableServerAssertion<WssConfigurationAssertion> {
    private final KeyInfoInclusionType d;
    private final KeyInfoInclusionType e;
    private final SignerInfo f;
    private static final String[] g;

    public ServerWssConfigurationAssertion(WssConfigurationAssertion wssConfigurationAssertion, BeanFactory beanFactory) throws PolicyAssertionException {
        super(wssConfigurationAssertion);
        KeyInfoInclusionType keyInfoInclusionType;
        KeyInfoInclusionType keyInfoInclusionType2;
        SignerInfo signerInfo = null;
        if (beanFactory != null) {
            try {
                signerInfo = ServerAssertionUtils.getSignerInfo(beanFactory, wssConfigurationAssertion);
            }
            catch (KeyStoreException keyStoreException) {
                throw new PolicyAssertionException((Assertion)wssConfigurationAssertion, keyStoreException.getMessage(), (Throwable)keyStoreException);
            }
        }
        try {
            this.f = signerInfo;
            ServerWssConfigurationAssertion serverWssConfigurationAssertion = this;
            keyInfoInclusionType2 = wssConfigurationAssertion.getKeyReference() == null ? null : ServerWssConfigurationAssertion.a(wssConfigurationAssertion, wssConfigurationAssertion.getKeyReference());
        }
        catch (KeyStoreException keyStoreException) {
            throw ServerWssConfigurationAssertion.a(keyStoreException);
        }
        try {
            serverWssConfigurationAssertion.d = keyInfoInclusionType2;
            ServerWssConfigurationAssertion serverWssConfigurationAssertion = this;
            keyInfoInclusionType = wssConfigurationAssertion.getEncryptionKeyReference() == null ? null : ServerWssConfigurationAssertion.a(wssConfigurationAssertion, wssConfigurationAssertion.getEncryptionKeyReference());
        }
        catch (KeyStoreException keyStoreException) {
            throw ServerWssConfigurationAssertion.a(keyStoreException);
        }
        serverWssConfigurationAssertion.e = keyInfoInclusionType;
    }

    /*
     * Loose catch block
     */
    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        block44: {
            DecorationRequirements decorationRequirements;
            block43: {
                block42: {
                    block38: {
                        boolean bl2;
                        block41: {
                            boolean bl3;
                            block40: {
                                block39: {
                                    decorationRequirements = message.getSecurityKnob().getAlternateDecorationRequirements(((WssConfigurationAssertion)this.assertion).getRecipientContext());
                                    if (((WssConfigurationAssertion)this.assertion).getWssVersion() == null) break block38;
                                    try {
                                        block45: {
                                            decorationRequirements.setWssVersion(((WssConfigurationAssertion)this.assertion).getWssVersion());
                                            if (((WssConfigurationAssertion)this.assertion).getWssVersion() != WsSecurityVersion.WSS11) break block39;
                                            break block45;
                                            catch (UnrecoverableKeyException unrecoverableKeyException) {
                                                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                                            }
                                        }
                                        bl3 = true;
                                        break block40;
                                    }
                                    catch (UnrecoverableKeyException unrecoverableKeyException) {
                                        throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                                    }
                                }
                                bl3 = false;
                            }
                            bl2 = bl3;
                            Message message2 = message.getRelated(MessageRole.REQUEST);
                            if (message2 == null) break block41;
                            try {
                                block46: {
                                    if (bl2) break block41;
                                    break block46;
                                    catch (UnrecoverableKeyException unrecoverableKeyException) {
                                        throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                                    }
                                }
                                message2.getSecurityKnob().setNeedsSignatureConfirmations(false);
                            }
                            catch (UnrecoverableKeyException unrecoverableKeyException) {
                                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                            }
                        }
                        try {
                            if (this.isResponse()) {
                                policyEnforcementContext.setResponseWss11(bl2);
                            }
                        }
                        catch (UnrecoverableKeyException unrecoverableKeyException) {
                            throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                        }
                    }
                    try {
                        decorationRequirements.setUseDerivedKeys(((WssConfigurationAssertion)this.assertion).isUseDerivedKeys());
                        if (((WssConfigurationAssertion)this.assertion).getSecureConversationNamespace() != null) {
                            decorationRequirements.getNamespaceFactory().setWsscNs(((WssConfigurationAssertion)this.assertion).getSecureConversationNamespace());
                        }
                    }
                    catch (UnrecoverableKeyException unrecoverableKeyException) {
                        throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                    }
                    try {
                        decorationRequirements.setIncludeTimestamp(((WssConfigurationAssertion)this.assertion).isAddTimestamp());
                        decorationRequirements.setSignTimestamp(((WssConfigurationAssertion)this.assertion).isSignTimestamp());
                        decorationRequirements.setNeverSignTimestamp(((WssConfigurationAssertion)this.assertion).isNeverSignTimestamp());
                        decorationRequirements.setOmitTimestamp(((WssConfigurationAssertion)this.assertion).isOmitTimestamp());
                        decorationRequirements.setProtectTokens(((WssConfigurationAssertion)this.assertion).isProtectTokens());
                        decorationRequirements.setEncryptSignature(((WssConfigurationAssertion)this.assertion).isEncryptSignature());
                        if (!((WssConfigurationAssertion)this.assertion).isSignWsAddressingHeaders()) break block42;
                        decorationRequirements.setWsaHeaderSignStrategy(DecorationRequirements.WsaHeaderSigningStrategy.ALWAYS_SIGN_WSA_HEADERS);
                        break block43;
                    }
                    catch (UnrecoverableKeyException unrecoverableKeyException) {
                        throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
                    }
                }
                decorationRequirements.setWsaHeaderSignStrategy(DecorationRequirements.WsaHeaderSigningStrategy.NEVER_SIGN_WSA_HEADERS);
            }
            try {
                if (((WssConfigurationAssertion)this.assertion).getEncryptionAlgorithmUri() != null) {
                    decorationRequirements.setEncryptionAlgorithm(((WssConfigurationAssertion)this.assertion).getEncryptionAlgorithmUri());
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (((WssConfigurationAssertion)this.assertion).getKeyWrappingAlgorithmUri() != null) {
                    decorationRequirements.setKeyEncryptionAlgorithm(((WssConfigurationAssertion)this.assertion).getKeyWrappingAlgorithmUri());
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (((WssConfigurationAssertion)this.assertion).getDigestAlgorithmName() != null) {
                    decorationRequirements.setSignatureMessageDigest(((WssConfigurationAssertion)this.assertion).getDigestAlgorithmName());
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (((WssConfigurationAssertion)this.assertion).getReferenceDigestAlgorithmName() != null) {
                    decorationRequirements.setSignatureReferenceMessageDigest(((WssConfigurationAssertion)this.assertion).getReferenceDigestAlgorithmName());
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (this.d != null) {
                    decorationRequirements.setKeyInfoInclusionType(this.d);
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (this.e != null) {
                    decorationRequirements.setEncryptionKeyInfoInclusionType(this.e);
                }
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                throw ServerWssConfigurationAssertion.a(unrecoverableKeyException);
            }
            try {
                if (this.f == null) break block44;
                decorationRequirements.setSenderMessageSigningCertificate(this.f.getCertificate());
                decorationRequirements.setSenderMessageSigningPrivateKey(this.f.getPrivate());
                break block44;
            }
            catch (IOException iOException) {
                throw ServerWssConfigurationAssertion.a(iOException);
            }
            {
                catch (UnrecoverableKeyException unrecoverableKeyException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.ASSERTION_MISCONFIGURED, new String[]{g[1] + ExceptionUtils.getMessage((Throwable)unrecoverableKeyException)}, ExceptionUtils.getDebugException((Throwable)unrecoverableKeyException));
                }
            }
        }
        return AssertionStatus.NONE;
    }

    private static KeyInfoInclusionType a(WssConfigurationAssertion wssConfigurationAssertion, String string) throws PolicyAssertionException {
        try {
            if (KeyReference.BST.getName().equals(string)) {
                return KeyInfoInclusionType.CERT;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        try {
            if (KeyReference.ISSUER_SERIAL.getName().equals(string)) {
                return KeyInfoInclusionType.ISSUER_SERIAL;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        try {
            if (KeyReference.SKI.getName().equals(string)) {
                return KeyInfoInclusionType.STR_SKI;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        try {
            if (KeyReference.KEY_NAME.getName().equals(string)) {
                return KeyInfoInclusionType.KEY_NAME;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        try {
            if (KeyReference.THUMBPRINT_SHA1.getName().equals(string)) {
                return KeyInfoInclusionType.STR_THUMBPRINT;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        try {
            if (KeyReference.STR_KEYID_X509_LITERAL.getName().equals(string)) {
                return KeyInfoInclusionType.STR_KEYID_LITERAL_X509;
            }
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw ServerWssConfigurationAssertion.a((Exception)((Object)policyAssertionException));
        }
        throw new PolicyAssertionException((Assertion)wssConfigurationAssertion, g[0] + string);
    }

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "\u0011'{B[\u0014Qx\"hZ\u0017\u000fP>,\u007fFY\u001ePx=tSRG\u0015)\r'lA[\u0018\u0015,&-BT\u001eP+:-@X\u0013S1.xQR\u0019\u0015(;dUV\tPx\"hZ\r]";
        int n3 = "\u0011'{B[\u0014Qx\"hZ\u0017\u000fP>,\u007fFY\u001ePx=tSRG\u0015)\r'lA[\u0018\u0015,&-BT\u001eP+:-@X\u0013S1.xQR\u0019\u0015(;dUV\tPx\"hZ\r]".length();
        int n4 = 28;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerWssConfigurationAssertion.c(101, ServerWssConfigurationAssertion.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                g = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x50);
        }
        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 -> 61;
                    case 1 -> 44;
                    case 2 -> 104;
                    case 3 -> 70;
                    case 4 -> 82;
                    case 5 -> 24;
                    default -> 80;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

