/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.AuditHaver
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.WssDecorationConfig
 *  com.l7tech.security.token.EncryptedKey
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.xml.KeyInfoDetails
 *  com.l7tech.security.xml.KeyInfoInclusionType
 *  com.l7tech.security.xml.KeyReference
 *  com.l7tech.security.xml.SignerInfo
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.security.xml.decorator.DecorationRequirements$PreferredSigningTokenType
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.InvalidDocumentFormatException
 *  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.Audit;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.AuditHaver;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.WssDecorationConfig;
import com.l7tech.security.token.EncryptedKey;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.xml.KeyInfoDetails;
import com.l7tech.security.xml.KeyInfoInclusionType;
import com.l7tech.security.xml.KeyReference;
import com.l7tech.security.xml.SignerInfo;
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.ServerAssertionUtils;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.UnrecoverableKeyException;
import org.springframework.beans.factory.BeanFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class AddWssSignatureSupport
implements AuditHaver {
    private final AuditHaver a;
    private final SignerInfo b;
    private final boolean c;
    private final WssDecorationConfig d;
    private final boolean e;
    private boolean f;
    private static final String[] g;

    public AddWssSignatureSupport(AuditHaver auditHaver, WssDecorationConfig wssDecorationConfig, BeanFactory beanFactory, boolean bl2, boolean bl3) {
        this.a = auditHaver;
        this.d = wssDecorationConfig;
        this.e = bl2;
        this.f = bl3;
        try {
            this.b = ServerAssertionUtils.getSignerInfo(beanFactory, wssDecorationConfig);
            this.c = ServerAssertionUtils.isPreferredSigner(wssDecorationConfig);
        }
        catch (KeyStoreException keyStoreException) {
            throw new RuntimeException(keyStoreException.getMessage(), keyStoreException);
        }
    }

    public Audit getAuditor() {
        return this.a.getAuditor();
    }

    public SignerInfo getSignerInfo() {
        return this.b;
    }

    /*
     * Unable to fully structure code
     */
    public AssertionStatus applySignatureDecorationRequirements(PolicyEnforcementContext var1_1, Message var2_2, String var3_3, AuthenticationContext var4_4, SignedElementSelector var5_5) throws IOException, PolicyAssertionException {
        block67: {
            block68: {
                block66: {
                    block65: {
                        block61: {
                            block62: {
                                block70: {
                                    block59: {
                                        block60: {
                                            var6_6 = this.a.getAuditor();
                                            var7_7 = null;
                                            if (this.f) {
                                                try {
                                                    if (var1_1.getRequest().isSoap()) {
                                                        var7_7 = var1_1.getRequest().getSecurityKnob().getProcessorResult();
                                                    }
                                                }
                                                catch (SAXException var8_8) {
                                                    throw new CausedIOException((Throwable)var8_8);
                                                }
                                            }
                                            var8_9 = this.d.getRecipientContext();
                                            try {
                                                if (!var2_2.isSoap()) {
                                                    var6_6.logAndAudit((AuditDetailMessage)AssertionMessages.ADD_WSS_SIGNATURE_MESSAGE_NOT_SOAP, new String[]{var3_3});
                                                    return AssertionStatus.NOT_APPLICABLE;
                                                }
                                            }
                                            catch (InvalidDocumentFormatException v0) {
                                                throw AddWssSignatureSupport.a((Exception)v0);
                                            }
                                            catch (SAXException var9_10) {
                                                throw new CausedIOException((Throwable)var9_10);
                                            }
                                            try {
                                                var10_12 = var2_2.getXmlKnob();
                                                var11_13 = var2_2.getSecurityKnob();
                                                var9_11 = var10_12.getDocumentReadOnly();
                                            }
                                            catch (SAXException var12_14) {
                                                var23_16 = AddWssSignatureSupport.g;
                                                var13_17 = var23_16[2] + var3_3 + var23_16[0];
                                                var6_6.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_SEVERE_WITH_MORE_INFO, new String[]{var13_17}, (Throwable)var12_14);
                                                return AssertionStatus.SERVER_ERROR;
                                            }
                                            var12_15 = var11_13.getAlternateDecorationRequirements(var8_9);
                                            var13_18 = var5_5.selectElementsToSign(var1_1, var4_4, var9_11, var12_15, var2_2);
                                            try {
                                                if (var13_18 < 0) {
                                                    return AssertionStatus.FAILED;
                                                }
                                            }
                                            catch (SAXException v1) {
                                                throw AddWssSignatureSupport.a(v1);
                                            }
                                            if (var13_18 != 0) break block59;
                                            try {
                                                block69: {
                                                    if (!this.e) break block60;
                                                    break block69;
                                                    catch (SAXException v2) {
                                                        throw AddWssSignatureSupport.a(v2);
                                                    }
                                                }
                                                var6_6.logAndAudit((AuditDetailMessage)AssertionMessages.ADD_WSS_SIGNATURE_MESSAGE_NO_MATCHING_EL, new String[]{var3_3});
                                                return AssertionStatus.FALSIFIED;
                                            }
                                            catch (SAXException v3) {
                                                throw AddWssSignatureSupport.a(v3);
                                            }
                                        }
                                        return AssertionStatus.NONE;
                                    }
                                    if (var7_7 == null) break block61;
                                    if (!var1_1.isResponseWss11()) break block61;
                                    break block70;
                                    catch (SAXException v4) {
                                        throw AddWssSignatureSupport.a(v4);
                                    }
                                }
                                try {
                                    block71: {
                                        if (var12_15.getEncryptedKeyReferenceInfo() == null) break block62;
                                        break block71;
                                        catch (SAXException v5) {
                                            throw AddWssSignatureSupport.a(v5);
                                        }
                                    }
                                    if (var12_15.getEncryptedKey() != null) break block61;
                                }
                                catch (SAXException v6) {
                                    throw AddWssSignatureSupport.a(v6);
                                }
                            }
                            block50: for (Object var15_21 : var1_1.getAuthenticationContext(var1_1.getRequest()).getCredentials()) {
                                for (SecurityToken var20_26 : var16_22 = var15_21.getSecurityTokens()) {
                                    try {
                                        if (!(var20_26 instanceof EncryptedKey) || !var15_21.isSecurityTokenPresent(var20_26)) continue;
                                    }
                                    catch (SAXException v7) {
                                        throw AddWssSignatureSupport.a(v7);
                                    }
                                    var21_27 = (EncryptedKey)var20_26;
                                    try {
                                        if (!var21_27.isUnwrapped()) continue;
                                        var12_15.setEncryptedKey(var21_27.getSecretKey());
                                        var12_15.setEncryptedKeyReferenceInfo(KeyInfoDetails.makeEncryptedKeySha1Ref((String)var21_27.getEncryptedKeySHA1()));
                                        continue block50;
                                    }
                                    catch (SAXException v8) {
                                        throw AddWssSignatureSupport.a(v8);
                                    }
                                    {
                                        catch (InvalidDocumentFormatException var22_28) {
                                            throw new IllegalStateException(var22_28);
                                        }
                                        catch (GeneralSecurityException var22_29) {
                                            throw new IllegalStateException(var22_29);
                                        }
                                    }
                                }
                            }
                        }
                        try {
                            block73: {
                                block63: {
                                    block64: {
                                        if (!this.c) break block63;
                                        try {
                                            block72: {
                                                if (var12_15.getPreferredSigningTokenType() != null) break block64;
                                                break block72;
                                                catch (SAXException v9) {
                                                    throw AddWssSignatureSupport.a(v9);
                                                }
                                            }
                                            var12_15.setPreferredSigningTokenType(DecorationRequirements.PreferredSigningTokenType.X509);
                                        }
                                        catch (SAXException v10) {
                                            throw AddWssSignatureSupport.a(v10);
                                        }
                                    }
                                    var12_15.setSenderMessageSigningCertificate(this.b.getCertificateChain()[0]);
                                    var12_15.setSenderMessageSigningPrivateKey(this.b.getPrivate());
                                    break block65;
                                }
                                if (var12_15.getEncryptedKeyReferenceInfo() == null) ** GOTO lbl119
                                if (var12_15.getEncryptedKey() != null) break block65;
                                break block73;
                                catch (SAXException v11) {
                                    throw AddWssSignatureSupport.a(v11);
                                }
                            }
                            try {
                                block74: {
                                    if (var12_15.getKerberosTicket() != null) break block65;
                                    break block74;
                                    catch (SAXException v12) {
                                        throw AddWssSignatureSupport.a(v12);
                                    }
                                }
                                var12_15.setSenderMessageSigningCertificate(this.b.getCertificateChain()[0]);
                                var12_15.setSenderMessageSigningPrivateKey(this.b.getPrivate());
                            }
                            catch (SAXException v13) {
                                throw AddWssSignatureSupport.a(v13);
                            }
                        }
                        catch (UnrecoverableKeyException var14_20) {
                            var15_21 = AddWssSignatureSupport.g[1] + ExceptionUtils.getMessage((Throwable)var14_20);
                            var6_6.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_SEVERE_WITH_MORE_INFO, new String[]{var15_21}, ExceptionUtils.getDebugException((Throwable)var14_20));
                            return AssertionStatus.SERVER_ERROR;
                        }
                    }
                    var14_19 = this.d.getKeyReference();
                    try {
                        if (this.d.isUsingProtectTokens()) {
                            var12_15.setProtectTokens(this.d.isProtectTokens());
                        }
                    }
                    catch (SAXException v14) {
                        throw AddWssSignatureSupport.a(v14);
                    }
                    try {
                        try {
                            if (var14_19 != null && !KeyReference.BST.getName().equals(var14_19)) break block66;
                        }
                        catch (SAXException v15) {
                            throw AddWssSignatureSupport.a(v15);
                        }
                        var12_15.setKeyInfoInclusionType(KeyInfoInclusionType.CERT);
                        break block67;
                    }
                    catch (SAXException v16) {
                        throw AddWssSignatureSupport.a(v16);
                    }
                }
                try {
                    if (!KeyReference.SKI.getName().equals(var14_19)) break block68;
                    var12_15.setKeyInfoInclusionType(KeyInfoInclusionType.STR_SKI);
                    break block67;
                }
                catch (SAXException v17) {
                    throw AddWssSignatureSupport.a(v17);
                }
            }
            try {
                if (KeyReference.ISSUER_SERIAL.getName().equals(var14_19)) {
                    var12_15.setKeyInfoInclusionType(KeyInfoInclusionType.ISSUER_SERIAL);
                }
            }
            catch (SAXException v18) {
                throw AddWssSignatureSupport.a(v18);
            }
        }
        var15_21 = this.d.getDigestAlgorithmName();
        try {
            if (var15_21 != null) {
                var12_15.setSignatureMessageDigest((String)var15_21);
            }
        }
        catch (SAXException v19) {
            throw AddWssSignatureSupport.a(v19);
        }
        var6_6.logAndAudit((AuditDetailMessage)AssertionMessages.ADD_WSS_SIGNATURE_MESSAGE_SIGNED, new String[]{var3_3, String.valueOf(var13_18)});
        return AssertionStatus.NONE;
    }

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "Ds\u0006\u0006\f(\u001f\n)1i\bD\u0013$X\u0010hIG\u001c\"\u001d\u0017tIE\u0010/\u001e\r`\u001cT\u001a%X\u0014u\u0000P\u001e5\u001dDl\f_Ea$\u0007f\u0007H\u00105X\u0003b\u001d\u0006\u001e/X\u001cj\u0005\u0006\u001b.\u001b\u0011j\fH\u000ba\u001e\u0016h\u0004\u0006\u000b)\u001dD";
        int n3 = "Ds\u0006\u0006\f(\u001f\n)1i\bD\u0013$X\u0010hIG\u001c\"\u001d\u0017tIE\u0010/\u001e\r`\u001cT\u001a%X\u0014u\u0000P\u001e5\u001dDl\f_Ea$\u0007f\u0007H\u00105X\u0003b\u001d\u0006\u001e/X\u001cj\u0005\u0006\u001b.\u001b\u0011j\fH\u000ba\u001e\u0016h\u0004\u0006\u000b)\u001dD".length();
        int n4 = 8;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = AddWssSignatureSupport.a(86, AddWssSignatureSupport.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] ^ 0x2E);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(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 -> 50;
                    case 1 -> 81;
                    case 2 -> 63;
                    case 3 -> 112;
                    case 4 -> 41;
                    case 5 -> 23;
                    default -> 46;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    public static interface SignedElementSelector {
        public int selectElementsToSign(PolicyEnforcementContext var1, AuthenticationContext var2, Document var3, DecorationRequirements var4, Message var5) throws PolicyAssertionException;
    }
}

