/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.CertGenParams
 *  com.l7tech.common.io.CertificateGeneratorException
 *  com.l7tech.common.io.DuplicateAliasException
 *  com.l7tech.common.io.KeyGenParams
 *  com.l7tech.gateway.common.security.keystore.SsgKeyMetadata
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.security.cert.BouncyCastleCertUtils
 *  com.l7tech.security.cert.ParamsKeyGenerator
 */
package com.l7tech.server.security.keystore;

import com.l7tech.common.io.CertGenParams;
import com.l7tech.common.io.CertificateGeneratorException;
import com.l7tech.common.io.DuplicateAliasException;
import com.l7tech.common.io.KeyGenParams;
import com.l7tech.gateway.common.security.keystore.SsgKeyMetadata;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.security.cert.BouncyCastleCertUtils;
import com.l7tech.security.cert.ParamsKeyGenerator;
import com.l7tech.server.security.keystore.JdkKeyStoreBackedSsgKeyStore;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.concurrent.Callable;
import javax.security.auth.x500.X500Principal;

class c
implements Callable<X509Certificate> {
    final String val$alias;
    final CertGenParams val$certGenParams;
    final KeyGenParams val$keyGenParams;
    final SsgKeyMetadata val$metadata;
    final JdkKeyStoreBackedSsgKeyStore this$0;
    private static final String[] a;

    /*
     * Ignored method signature, as it can't be verified against descriptor
     */
    c(JdkKeyStoreBackedSsgKeyStore jdkKeyStoreBackedSsgKeyStore, String string, CertGenParams certGenParams, KeyGenParams keyGenParams, SsgKeyMetadata ssgKeyMetadata) {
        this.this$0 = jdkKeyStoreBackedSsgKeyStore;
        this.val$alias = string;
        this.val$certGenParams = certGenParams;
        this.val$keyGenParams = keyGenParams;
        this.val$metadata = ssgKeyMetadata;
    }

    /*
     * Loose catch block
     */
    @Override
    public X509Certificate call() throws GeneralSecurityException, DuplicateAliasException, CertificateGeneratorException {
        KeyStore keyStore;
        block9: {
            keyStore = this.this$0.keyStore();
            try {
                if (keyStore.containsAlias(this.val$alias)) {
                    throw new DuplicateAliasException(a[0] + this.val$alias);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw c.a(objectModelException);
            }
            X500Principal x500Principal = this.val$certGenParams.getSubjectDn();
            if (this.this$0.a()) break block9;
            try {
                block10: {
                    if (!this.this$0.a(keyStore, x500Principal)) break block9;
                    break block10;
                    catch (ObjectModelException objectModelException) {
                        throw c.a(objectModelException);
                    }
                }
                throw new GeneralSecurityException(a[2]);
            }
            catch (ObjectModelException objectModelException) {
                throw c.a(objectModelException);
            }
        }
        KeyPair keyPair = new ParamsKeyGenerator(this.val$keyGenParams).generateKeyPair();
        X509Certificate x509Certificate = BouncyCastleCertUtils.generateSelfSignedCertificate((CertGenParams)this.val$certGenParams, (KeyPair)keyPair);
        keyStore.setKeyEntry(this.val$alias, keyPair.getPrivate(), this.this$0.getEntryPassword(), new Certificate[]{x509Certificate});
        try {
            this.this$0.h.updateMetadataForKey(this.this$0.getGoid(), this.val$alias, this.val$metadata);
        }
        catch (ObjectModelException objectModelException) {
            throw new KeyStoreException(a[1] + this.val$alias, objectModelException);
        }
        return x509Certificate;
    }

    private static ObjectModelException a(ObjectModelException objectModelException) {
        return objectModelException;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "\u0004\u001fJ8\"5k*ZR'$?x+\u0003\u0013(94m.\u0013]8v;u&\u001b@k \u001a\u0014R):?9;\u0015\u001387,|o\u0017V?7>x;\u001b\u0013-9(9$\u001fJk)\u001c\u000fQ!39mo>}k76k*\u001bW2v?a&\tG8v3wo\u000e[.v\u0011|6\tG$$?";
        int n3 = "\u0004\u001fJ8\"5k*ZR'$?x+\u0003\u0013(94m.\u0013]8v;u&\u001b@k \u001a\u0014R):?9;\u0015\u001387,|o\u0017V?7>x;\u001b\u0013-9(9$\u001fJk)\u001c\u000fQ!39mo>}k76k*\u001bW2v?a&\tG8v3wo\u000e[.v\u0011|6\tG$$?".length();
        int n4 = 32;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = c.a(66, c.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                a = 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] ^ 0x5B);
        }
        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 -> 13;
                    case 1 -> 56;
                    case 2 -> 113;
                    case 3 -> 9;
                    case 4 -> 20;
                    case 5 -> 24;
                    default -> 91;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

