/*
 * 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.IdentityTarget
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.RequireWssTimestamp
 *  com.l7tech.policy.assertion.xmlsec.SecurityHeaderAddressableSupport
 *  com.l7tech.security.token.ParsedElement
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.security.xml.processor.ProcessorResultUtil
 *  com.l7tech.util.Config
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Pair
 *  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.IdentityTarget;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.RequireWssTimestamp;
import com.l7tech.policy.assertion.xmlsec.SecurityHeaderAddressableSupport;
import com.l7tech.security.token.ParsedElement;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.security.xml.processor.ProcessorResultUtil;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.util.WSSecurityProcessorUtils;
import com.l7tech.util.Config;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Pair;
import java.io.IOException;
import java.util.Collection;
import org.springframework.beans.factory.BeanFactory;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServerRequireWssTimestamp
extends AbstractMessageTargetableServerAssertion<RequireWssTimestamp> {
    private static final boolean d;
    private static final long e = 60000L;
    private static final int f = 151013;
    private final SecurityTokenResolver g;
    private final Config h;
    private static final String[] i;

    public ServerRequireWssTimestamp(RequireWssTimestamp requireWssTimestamp, BeanFactory beanFactory) {
        super(requireWssTimestamp);
        String[] stringArray = i;
        this.g = (SecurityTokenResolver)beanFactory.getBean(stringArray[1], SecurityTokenResolver.class);
        this.h = (Config)beanFactory.getBean(stringArray[0], Config.class);
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        try {
            if (!SecurityHeaderAddressableSupport.isLocalRecipient((Assertion)this.assertion)) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUESTWSS_NOT_FOR_US);
                return AssertionStatus.NONE;
            }
        }
        catch (IOException iOException) {
            throw ServerRequireWssTimestamp.a(iOException);
        }
        return super.checkRequest(policyEnforcementContext);
    }

    /*
     * Unable to fully structure code
     */
    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext var1_1, Message var2_2, String var3_3, AuthenticationContext var4_4) throws IOException, PolicyAssertionException {
        block61: {
            block62: {
                block58: {
                    block64: {
                        block54: {
                            block57: {
                                block56: {
                                    block63: {
                                        block53: {
                                            try {
                                                if (!var2_2.isSoap()) {
                                                    this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_NOTAPPLICABLE, new String[]{var3_3});
                                                    return AssertionStatus.NOT_APPLICABLE;
                                                }
                                            }
                                            catch (IOException v0) {
                                                throw ServerRequireWssTimestamp.a(v0);
                                            }
                                            catch (SAXException var5_5) {
                                                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, new String[]{ExceptionUtils.getMessage((Throwable)var5_5)});
                                                return this.getBadMessageStatus();
                                            }
                                            try {
                                                if (!this.isRequest() || this.h.getBooleanProperty(ServerRequireWssTimestamp.i[2], true)) break block53;
                                            }
                                            catch (SAXException v1) {
                                                throw ServerRequireWssTimestamp.a(v1);
                                            }
                                            var5_6 = var2_2.getSecurityKnob().getProcessorResult();
                                            break block63;
                                        }
                                        var5_6 = WSSecurityProcessorUtils.getWssResults(var2_2, var3_3, this.g, this.getAudit());
                                    }
                                    try {
                                        if (var5_6 == null) {
                                            return AssertionStatus.NOT_APPLICABLE;
                                        }
                                    }
                                    catch (SAXException v2) {
                                        throw ServerRequireWssTimestamp.a(v2);
                                    }
                                    var6_7 = var5_6.getTimestamp();
                                    try {
                                        if (var6_7 == null) {
                                            this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_NOTIMESTAMP, new String[]{var3_3});
                                            return this.getBadMessageStatus();
                                        }
                                    }
                                    catch (SAXException v3) {
                                        throw ServerRequireWssTimestamp.a(v3);
                                    }
                                    if (!((RequireWssTimestamp)this.assertion).isSignatureRequired()) break block64;
                                    var7_8 = ProcessorResultUtil.getParsedElementsForNode((Node)var6_7.asElement(), (ParsedElement[])var5_6.getElementsThatWereSigned());
                                    var8_10 = var2_2.getRelated(MessageRole.REQUEST);
                                    try {
                                        try {
                                            block55: {
                                                try {
                                                    try {
                                                        try {
                                                            if (!new IdentityTarget().equals((Object)new IdentityTarget(((RequireWssTimestamp)this.assertion).getIdentityTarget()))) break block54;
                                                            if (!var7_8.isEmpty()) {
                                                            }
                                                            ** GOTO lbl77
                                                        }
                                                        catch (SAXException v4) {
                                                            throw ServerRequireWssTimestamp.a(v4);
                                                        }
                                                        if (!this.isResponse()) break block55;
                                                    }
                                                    catch (SAXException v5) {
                                                        throw ServerRequireWssTimestamp.a(v5);
                                                    }
                                                    if (this.a(var2_2, var5_6, var7_8)) {
                                                    }
                                                    ** GOTO lbl77
                                                }
                                                catch (SAXException v6) {
                                                    throw ServerRequireWssTimestamp.a(v6);
                                                }
                                            }
                                            v7 = var4_4;
                                            v8 = var5_6;
                                            v9 = var7_8.toArray(new ParsedElement[var7_8.size()]);
                                            v10 = ServerRequireWssTimestamp.d;
                                            v11 = var8_10;
                                            if (var8_10 != null) break block56;
                                        }
                                        catch (SAXException v12) {
                                            throw ServerRequireWssTimestamp.a(v12);
                                        }
                                        v13 = null;
                                        break block57;
                                    }
                                    catch (SAXException v14) {
                                        throw ServerRequireWssTimestamp.a(v14);
                                    }
                                }
                                v13 = var1_1.getAuthenticationContext(var8_10);
                            }
                            try {
                                if (WSSecurityProcessorUtils.isValidSingleSigner(v7, v8, v9, v10, v11, v13, this.getAudit())) break block58;
lbl77:
                                // 3 sources

                                this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_NOT_SIGNED, new String[]{var3_3});
                                return this.getBadMessageStatus();
                            }
                            catch (SAXException v15) {
                                throw ServerRequireWssTimestamp.a(v15);
                            }
                        }
                        try {
                            block59: {
                                try {
                                    block60: {
                                        try {
                                            try {
                                                if (var7_8.isEmpty()) break block59;
                                                if (!this.isResponse()) break block60;
                                            }
                                            catch (SAXException v16) {
                                                throw ServerRequireWssTimestamp.a(v16);
                                            }
                                            if (!this.a(var2_2, var5_6, var7_8)) break block59;
                                        }
                                        catch (SAXException v17) {
                                            throw ServerRequireWssTimestamp.a(v17);
                                        }
                                    }
                                    if (WSSecurityProcessorUtils.isValidSigningIdentity(var4_4, ((RequireWssTimestamp)this.assertion).getIdentityTarget(), var5_6, var7_8.toArray(new ParsedElement[var7_8.size()]))) break block58;
                                }
                                catch (SAXException v18) {
                                    throw ServerRequireWssTimestamp.a(v18);
                                }
                            }
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_NOT_SIGNED, new String[]{var3_3});
                            return this.getBadMessageStatus();
                        }
                        catch (SAXException v19) {
                            throw ServerRequireWssTimestamp.a(v19);
                        }
                    }
                    try {
                        try {
                            if (!this.isResponse() || this.a(var2_2, var5_6, null)) break block58;
                        }
                        catch (SAXException v20) {
                            throw ServerRequireWssTimestamp.a(v20);
                        }
                        return AssertionStatus.FALSIFIED;
                    }
                    catch (SAXException v21) {
                        throw ServerRequireWssTimestamp.a(v21);
                    }
                }
                var7_9 = System.currentTimeMillis();
                var9_11 = var6_7.getCreated();
                try {
                    if (var9_11 == null) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_NO_CREATED, new String[]{var3_3});
                        return this.getBadMessageStatus();
                    }
                }
                catch (SAXException v22) {
                    throw ServerRequireWssTimestamp.a(v22);
                }
                var24_12 = ServerRequireWssTimestamp.i;
                var10_13 = this.h.getLongProperty(var24_12[5], 60000L);
                var12_14 = var9_11.asTime();
                try {
                    if (var12_14 - var10_13 > var7_9) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_CREATED_FUTURE, new String[]{var3_3});
                        return this.getBadMessageStatus();
                    }
                }
                catch (SAXException v23) {
                    throw ServerRequireWssTimestamp.a(v23);
                }
                var14_15 = var6_7.getExpires();
                var15_16 = var14_15 != null ? var14_15.asTime() : var12_14 + ((RequireWssTimestamp)this.assertion).getMaxExpiryMilliseconds();
                var17_17 = var15_16 - var12_14;
                var19_18 = false;
                var20_19 = var15_16;
                if (var17_17 > ((RequireWssTimestamp)this.assertion).getMaxExpiryMilliseconds()) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_EXPIRES_TOOLATE, new String[]{var3_3});
                    var19_18 = true;
                    var15_16 = var12_14 + ((RequireWssTimestamp)this.assertion).getMaxExpiryMilliseconds();
                }
                var22_20 = this.h.getLongProperty(ServerRequireWssTimestamp.i[3], 60000L);
                try {
                    try {
                        try {
                            if (var15_16 + var22_20 >= var7_9) break block61;
                            if (!var19_18) break block62;
                        }
                        catch (SAXException v24) {
                            throw ServerRequireWssTimestamp.a(v24);
                        }
                        if (var20_19 + var22_20 < var7_9) break block62;
                    }
                    catch (SAXException v25) {
                        throw ServerRequireWssTimestamp.a(v25);
                    }
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_EXPIRED_TRUNC, new String[]{var3_3});
                    return this.getBadMessageStatus();
                }
                catch (SAXException v26) {
                    throw ServerRequireWssTimestamp.a(v26);
                }
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUIRE_WSS_TIMESTAMP_EXPIRED, new String[]{var3_3});
            return this.getBadMessageStatus();
        }
        return AssertionStatus.NONE;
    }

    private boolean a(Message message, ProcessorResult processorResult, Collection<ParsedElement> collection) {
        Pair<Boolean, Collection<ParsedElement>> pair = WSSecurityProcessorUtils.processSignatureConfirmations(message.getSecurityKnob(), processorResult, this.getAudit());
        if (collection != null && pair.getValue() != null) {
            collection.addAll((Collection)pair.getValue());
        }
        return (Boolean)pair.getKey();
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "g\u0017\u0002eh.]{\u001c\u0016zj\u0015g\u0017\u0013f\u007f5jm&\u001fxh2Lq\u0001\u001f\u007f{9l-c\u0001\u0003=}.qw\u0017\u0003`b.0q\u001c\u0011qa9Zq\u0014\u0015a\u007f9zF\u0017\u0001fh/jD\u0000\u001fph/m}\u001c\u0017\u0019`\u001b\u001dv~(\u007fy\u00025k}5lq\u0001 r~(Yf\u0013\u0013v";
                var4_3 = "g\u0017\u0002eh.]{\u001c\u0016zj\u0015g\u0017\u0013f\u007f5jm&\u001fxh2Lq\u0001\u001f\u007f{9l-c\u0001\u0003=}.qw\u0017\u0003`b.0q\u001c\u0011qa9Zq\u0014\u0015a\u007f9zF\u0017\u0001fh/jD\u0000\u001fph/m}\u001c\u0017\u0019`\u001b\u001dv~(\u007fy\u00025k}5lq\u0001 r~(Yf\u0013\u0013v".length();
                var1_4 = 12;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerRequireWssTimestamp.c(v0, ServerRequireWssTimestamp.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "J  \u0000\\VWL,%\u0000C\u0004Q_*?\u0000@\u000eO@,4\u0000B\u0004R\\&?Kc\bDG&#Id\u000eHL!\u000e\\U\u0005FG;$O\\\u001b]& KC\u0015BD?\u000e\\U\u0000WL+\u000b[D\u0014QL\b?OS\u0004";
                    var4_3 = "J  \u0000\\VWL,%\u0000C\u0004Q_*?\u0000@\u000eO@,4\u0000B\u0004R\\&?Kc\bDG&#Id\u000eHL!\u000e\\U\u0005FG;$O\\\u001b]& KC\u0015BD?\u000e\\U\u0000WL+\u000b[D\u0014QL\b?OS\u0004".length();
                    var1_4 = 54;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 78;
                        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++] = ServerRequireWssTimestamp.c(v0, ServerRequireWssTimestamp.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;
            }
        }
        ServerRequireWssTimestamp.i = var5;
        ServerRequireWssTimestamp.d = ConfigFactory.getBooleanProperty((String)ServerRequireWssTimestamp.i[4], (boolean)true);
    }

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

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x6D);
        }
        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 -> 103;
                    case 1 -> 1;
                    case 2 -> 3;
                    case 3 -> 96;
                    case 4 -> 126;
                    case 5 -> 47;
                    default -> 109;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

