/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.security.xml.SecureConversationKeyDeriver
 *  com.l7tech.security.xml.processor.SecurityContext
 *  com.l7tech.security.xml.processor.SecurityContextFinder
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 */
package com.l7tech.server.secureconversation;

import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.security.xml.SecureConversationKeyDeriver;
import com.l7tech.security.xml.processor.SecurityContext;
import com.l7tech.security.xml.processor.SecurityContextFinder;
import com.l7tech.server.secureconversation.SecureConversationContextManager;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.server.secureconversation.SessionCreationException;
import com.l7tech.server.secureconversation.StoredSecureConversationSession;
import com.l7tech.server.secureconversation.StoredSecureConversationSessionManager;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

public class InboundSecureConversationContextManager
extends SecureConversationContextManager<String>
implements SecurityContextFinder {
    private static final Logger m;
    private final StoredSecureConversationSessionManager n;
    private static final String[] p;

    public InboundSecureConversationContextManager(Config config, StoredSecureConversationSessionManager storedSecureConversationSessionManager) {
        super(m, config, true);
        this.n = storedSecureConversationSessionManager;
    }

    public SecurityContext getSecurityContext(String string) {
        return this.getSession(string);
    }

    public SecureConversationSession createContextForUser(User user, String string) throws SessionCreationException {
        return this.createContextForUser(user, string, this.getDefaultSessionDuration(), null, -1);
    }

    /*
     * Loose catch block
     */
    public SecureConversationSession createContextForUser(User user, String string, long l2, byte[] byArray, int n2) throws SessionCreationException {
        byte[] byArray2;
        byte[] byArray3;
        byte[] byArray4;
        int n3;
        String string2;
        block10: {
            String[] stringArray = p;
            string2 = stringArray[1] + this.randomUuid();
            try {
                if (l2 <= 0L) {
                    throw new SessionCreationException(stringArray[2]);
                }
            }
            catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                throw InboundSecureConversationContextManager.a(noSuchAlgorithmException);
            }
            n3 = this.a(n2, string);
            if (byArray == null) break block10;
            try {
                block11: {
                    if (byArray.length < MIN_CLIENT_ENTROPY_BYTES) break block10;
                    break block11;
                    catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                        throw InboundSecureConversationContextManager.a(noSuchAlgorithmException);
                    }
                }
                if (byArray.length > MAX_CLIENT_ENTROPY_BYTES) break block10;
            }
            catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                throw InboundSecureConversationContextManager.a(noSuchAlgorithmException);
            }
            byArray4 = byArray;
            byArray3 = this.generateNewSecret(n3);
            try {
                byArray2 = SecureConversationKeyDeriver.pSHA1((byte[])byArray4, (byte[])byArray3, (int)n3);
            }
            catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                stringArray = p;
                throw new SessionCreationException(stringArray[0] + ExceptionUtils.getMessage((Throwable)noSuchAlgorithmException), noSuchAlgorithmException);
            }
            catch (InvalidKeyException invalidKeyException) {
                throw new SessionCreationException(p[3] + ExceptionUtils.getMessage((Throwable)invalidKeyException), invalidKeyException);
            }
        }
        byArray4 = null;
        byArray3 = null;
        byArray2 = this.generateNewSecret(n3);
        long l3 = System.currentTimeMillis();
        SecureConversationSession secureConversationSession = new SecureConversationSession(string, string2, byArray4, byArray3, byArray2, l3, l3 + l2, user, null);
        this.saveSession(secureConversationSession.getIdentifier(), secureConversationSession);
        return secureConversationSession;
    }

    @Override
    protected void storeSession(String string, SecureConversationSession secureConversationSession) throws SaveException {
        this.n.save((PersistentEntity)this.a(secureConversationSession));
    }

    @Override
    protected SecureConversationSession loadSession(String string) throws FindException {
        SecureConversationSession secureConversationSession;
        StoredSecureConversationSession storedSecureConversationSession = this.n.findInboundSessionByIdentifier(string);
        try {
            secureConversationSession = storedSecureConversationSession == null ? null : this.fromStored(storedSecureConversationSession, this.n.decryptSessionKey(storedSecureConversationSession.getEncryptedKey()));
        }
        catch (FindException findException) {
            throw InboundSecureConversationContextManager.a((Exception)((Object)findException));
        }
        return secureConversationSession;
    }

    @Override
    protected void deleteSession(String string) throws DeleteException {
        this.n.deleteInboundSessionByIdentifer(string);
    }

    private StoredSecureConversationSession a(SecureConversationSession secureConversationSession) {
        return new StoredSecureConversationSession(secureConversationSession.getIdentifier(), secureConversationSession.getCreation(), secureConversationSession.getExpiration(), secureConversationSession.getSCNamespace(), this.n.encryptSessionKey(secureConversationSession.getSecretKey()), secureConversationSession.getUsedBy().getProviderId(), secureConversationSession.getUsedBy().getId(), secureConversationSession.getUsedBy().getLogin());
    }

    private int a(int n2, String string) {
        int n3;
        int n4 = this.getDefaultKeySize(string);
        int n5 = n2 > 0 ? ((n3 = n2 / 8 + (n2 % 8 > 0 ? 1 : 0)) < MIN_KEY_SIZE ? MIN_KEY_SIZE : (n3 > MAX_KEY_SIZE ? MAX_KEY_SIZE : n3)) : n4;
        return n5;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = "jbW\u000f\fL/Kc\u0016\n\u0005GjMmB\b@ZjL\u007f_\u0002\u000e\tdZu\fM\u001fWxB\u001dZ\u0006 H{AC\fHvZ~\u0001\u0019\u0005Jg\u0011oY\u0000O\\zVh\u0019";
                var4_3 = "jbW\u000f\fL/Kc\u0016\n\u0005GjMmB\b@ZjL\u007f_\u0002\u000e\tdZu\fM\u001fWxB\u001dZ\u0006 H{AC\fHvZ~\u0001\u0019\u0005Jg\u0011oY\u0000O\\zVh\u0019".length();
                var1_4 = 32;
                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++] = InboundSecureConversationContextManager.b(v0, InboundSecureConversationContextManager.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u001c\u00195ny6\u0011o\u00183oq-\u0016 \u0012fpe*\u000bo\u001e#=w+\u001a.\b#o0-\u0017.\u0012fgu+\u0010a \u001a\u0012'\u007f|<_;\u0013fzu7\u001a=\u001d2x0*\u001a<\u000f/r~y\u0014*\u0005|=";
                    var4_3 = "\u001c\u00195ny6\u0011o\u00183oq-\u0016 \u0012fpe*\u000bo\u001e#=w+\u001a.\b#o0-\u0017.\u0012fgu+\u0010a \u001a\u0012'\u007f|<_;\u0013fzu7\u001a=\u001d2x0*\u001a<\u000f/r~y\u0014*\u0005|=".length();
                    var1_4 = 43;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 3;
                        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++] = InboundSecureConversationContextManager.b(v0, InboundSecureConversationContextManager.b(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;
            }
        }
        InboundSecureConversationContextManager.p = var5;
        InboundSecureConversationContextManager.m = Logger.getLogger(InboundSecureConversationContextManager.class.getName());
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 127;
                    case 2 -> 69;
                    case 3 -> 30;
                    case 4 -> 19;
                    case 5 -> 90;
                    default -> 124;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

