package com.l7tech.gateway.common.security.keystore;

import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.NameableEntity;
import com.l7tech.objectmodel.NamedEntity;
import com.l7tech.objectmodel.SecurityZone;
import com.l7tech.objectmodel.ZoneableEntity;
import com.l7tech.search.Dependency;
import com.l7tech.security.rbac.RbacAttribute;
import com.l7tech.security.xml.SignerInfo;
import com.l7tech.util.DeserializeSafe;
import com.l7tech.util.Functions.Nullary;
import java.io.Serializable;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.jetbrains.annotations.Nullable;

@DeserializeSafe
public class SsgKeyEntry extends SignerInfo implements NamedEntity, NameableEntity, Serializable, ZoneableEntity {
   private static final long serialVersionUID = 23272983482973430L;
   private static Nullary<Boolean> restrictedKeyAccessChecker;
   private Goid keystoreId;
   private String alias;
   private boolean restrictedAccess;
   private SsgKeyMetadata keyMetadata;
   private static final String[] b;

   public SsgKeyEntry(Goid param1, String param2, X509Certificate[] param3, PrivateKey param4) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: aload 4
      // 03: aload 3
      // 04: invokespecial com/l7tech/security/xml/SignerInfo.<init> (Ljava/security/PrivateKey;[Ljava/security/cert/X509Certificate;)V
      // 07: aload 1
      // 08: new com/l7tech/objectmodel/Goid
      // 0b: dup
      // 0c: lconst_0
      // 0d: ldc2_w -9223372036854775808
      // 10: invokespecial com/l7tech/objectmodel/Goid.<init> (JJ)V
      // 13: invokestatic com/l7tech/objectmodel/Goid.equals (Lcom/l7tech/objectmodel/Goid;Lcom/l7tech/objectmodel/Goid;)Z
      // 16: ifne 53
      // 19: aload 2
      // 1a: ifnull 53
      // 1d: aload 3
      // 1e: ifnull 42
      // 21: goto 28
      // 24: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 27: athrow
      // 28: aload 3
      // 29: arraylength
      // 2a: bipush 1
      // 2b: if_icmplt 42
      // 2e: goto 35
      // 31: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 34: athrow
      // 35: aload 3
      // 36: bipush 0
      // 37: aaload
      // 38: ifnonnull 53
      // 3b: goto 42
      // 3e: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 41: athrow
      // 42: new java/lang/IllegalArgumentException
      // 45: dup
      // 46: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b [Ljava/lang/String;
      // 49: bipush 3
      // 4a: aaload
      // 4b: invokespecial java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
      // 4e: athrow
      // 4f: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 52: athrow
      // 53: aload 0
      // 54: aload 1
      // 55: putfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.keystoreId Lcom/l7tech/objectmodel/Goid;
      // 58: aload 0
      // 59: aload 2
      // 5a: putfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.alias Ljava/lang/String;
      // 5d: return
      // try (12 -> 16): 17 java/lang/IllegalArgumentException
      // try (14 -> 23): 24 java/lang/IllegalArgumentException
      // try (19 -> 30): 31 java/lang/IllegalArgumentException
      // try (26 -> 40): 40 java/lang/IllegalArgumentException
   }

   public static void setRestrictedKeyAccessChecker(Nullary<Boolean> param0) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: ifnull 26
      // 04: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.restrictedKeyAccessChecker Lcom/l7tech/util/Functions$Nullary;
      // 07: ifnull 22
      // 0a: goto 11
      // 0d: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 10: athrow
      // 11: new java/lang/IllegalStateException
      // 14: dup
      // 15: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b [Ljava/lang/String;
      // 18: bipush 1
      // 19: aaload
      // 1a: invokespecial java/lang/IllegalStateException.<init> (Ljava/lang/String;)V
      // 1d: athrow
      // 1e: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 21: athrow
      // 22: aload 0
      // 23: putstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.restrictedKeyAccessChecker Lcom/l7tech/util/Functions$Nullary;
      // 26: return
      // try (0 -> 4): 5 java/lang/IllegalArgumentException
      // try (2 -> 14): 14 java/lang/IllegalArgumentException
   }

   private SsgKeyEntry(Goid var1, String var2) {
      super(null, null);
      if (var2 == null) {
         throw new IllegalArgumentException(b[4]);
      }

      this.keystoreId = var1;
      this.alias = var2;
   }

   public static SsgKeyEntry createDummyEntityForAuditing(Goid var0, String var1) {
      return new SsgKeyEntry(var0, var1);
   }

   public String getId() {
      try {
         if (this.alias == null) {
            return null;
         }
      } catch (IllegalArgumentException var1) {
         throw b(var1);
      }

      return this.keystoreId + ":" + this.alias;
   }

   @RbacAttribute
   public String getName() {
      return this.getAlias();
   }

   public void setName(String var1) {
      this.setAlias(var1);
   }

   public String getAlias() {
      return this.alias;
   }

   public boolean isPrivateKeyAvailable() {
      try {
         try {
            if (super.getPrivate() != null) {
               return true;
            }
         } catch (UnrecoverableKeyException var2) {
            throw b(var2);
         }

         return false;
      } catch (UnrecoverableKeyException var3) {
         return false;
      }
   }

   public boolean isPrivateKeyAvailableAndAccessible() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: invokevirtual com/l7tech/gateway/common/security/keystore/SsgKeyEntry.isPrivateKeyAvailable ()Z
      // 04: ifeq 1d
      // 07: aload 0
      // 08: invokevirtual com/l7tech/gateway/common/security/keystore/SsgKeyEntry.isAccessAllowed ()Z
      // 0b: ifeq 1d
      // 0e: goto 15
      // 11: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 14: athrow
      // 15: bipush 1
      // 16: goto 1e
      // 19: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1c: athrow
      // 1d: bipush 0
      // 1e: ireturn
      // try (0 -> 6): 7 java/lang/IllegalArgumentException
      // try (3 -> 11): 11 java/lang/IllegalArgumentException
   }

   public boolean isRestrictedAccess() {
      return this.restrictedAccess;
   }

   public PrivateKey getPrivate() throws UnrecoverableKeyException {
      return this.getPrivateKey();
   }

   public final PrivateKey getPrivateKey() throws UnrecoverableKeyException {
      PrivateKey var1 = super.getPrivate();

      try {
         if (var1 == null) {
            throw new UnrecoverableKeyException(b[2]);
         }
      } catch (UnrecoverableKeyException var2) {
         throw b(var2);
      }

      this.checkRestrictedAccess();
      return var1;
   }

   private boolean isAccessAllowed() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.restrictedAccess Z
      // 04: ifeq 2c
      // 07: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.restrictedKeyAccessChecker Lcom/l7tech/util/Functions$Nullary;
      // 0a: ifnull 2c
      // 0d: goto 14
      // 10: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 13: athrow
      // 14: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.restrictedKeyAccessChecker Lcom/l7tech/util/Functions$Nullary;
      // 17: invokeinterface com/l7tech/util/Functions$Nullary.call ()Ljava/lang/Object; 1
      // 1c: checkcast java/lang/Boolean
      // 1f: invokevirtual java/lang/Boolean.booleanValue ()Z
      // 22: ifeq 34
      // 25: goto 2c
      // 28: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2b: athrow
      // 2c: bipush 1
      // 2d: goto 35
      // 30: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 33: athrow
      // 34: bipush 0
      // 35: ireturn
      // try (0 -> 5): 6 java/lang/IllegalArgumentException
      // try (3 -> 13): 14 java/lang/IllegalArgumentException
      // try (8 -> 18): 18 java/lang/IllegalArgumentException
   }

   private void checkRestrictedAccess() throws UnrecoverableKeyException {
      try {
         if (!this.isAccessAllowed()) {
            throw new UnrecoverableKeyException(b[5]);
         }
      } catch (UnrecoverableKeyException var1) {
         throw b(var1);
      }
   }

   public Goid getKeystoreId() {
      return this.keystoreId;
   }

   public void setKeystoreId(Goid var1) {
      this.keystoreId = var1;
   }

   public void setAlias(String var1) {
      this.alias = var1;
   }

   public void setRestrictedAccess() {
      this.restrictedAccess = true;
   }

   @NotNull
   @Size(min = 1)
   public X509Certificate[] getCertificateChain() {
      return super.getCertificateChain();
   }

   public void setCertificateChain(X509Certificate[] var1) {
      super.setCertificateChain(var1);
   }

   public void setPrivateKey(PrivateKey var1) {
      super.setPrivate(var1);
   }

   public void attachMetadata(@NotNull SsgKeyMetadata param1) {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.keyMetadata Lcom/l7tech/gateway/common/security/keystore/SsgKeyMetadata;
      // 04: ifnull 27
      // 07: aload 0
      // 08: getfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.keyMetadata Lcom/l7tech/gateway/common/security/keystore/SsgKeyMetadata;
      // 0b: aload 1
      // 0c: if_acmpeq 27
      // 0f: goto 16
      // 12: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 15: athrow
      // 16: new java/lang/IllegalArgumentException
      // 19: dup
      // 1a: getstatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b [Ljava/lang/String;
      // 1d: bipush 0
      // 1e: aaload
      // 1f: invokespecial java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
      // 22: athrow
      // 23: invokestatic com/l7tech/gateway/common/security/keystore/SsgKeyEntry.b (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 26: athrow
      // 27: aload 0
      // 28: aload 1
      // 29: putfield com/l7tech/gateway/common/security/keystore/SsgKeyEntry.keyMetadata Lcom/l7tech/gateway/common/security/keystore/SsgKeyMetadata;
      // 2c: return
      // try (0 -> 7): 8 java/lang/IllegalArgumentException
      // try (3 -> 17): 17 java/lang/IllegalArgumentException
   }

   @Dependency(isDependency = false)
   @Nullable
   public SsgKeyMetadata getKeyMetadata() {
      return this.keyMetadata;
   }

   public SecurityZone getSecurityZone() {
      try {
         if (this.keyMetadata == null) {
            return null;
         }
      } catch (IllegalArgumentException var1) {
         throw b(var1);
      }

      return this.keyMetadata.getSecurityZone();
   }

   public void setSecurityZone(SecurityZone var1) {
      try {
         if (this.keyMetadata == null) {
            this.keyMetadata = new SsgKeyMetadata(this.keystoreId, this.alias, var1);
            return;
         }
      } catch (IllegalArgumentException var2) {
         throw b(var2);
      }

      this.keyMetadata.setSecurityZone(var1);
   }

   private static Exception b(Exception var0) {
      return var0;
   }

   static {
      String[] var5 = new String[6];
      int var3 = 0;
      String var2 = "T\u000et94o~^\u000flm8*kS\u0019hx=s*^\u001fyx:bo[3m\u000e~m+ciK\u000ei92os\u001f\nnz<yy\u001f\be|:aoMKex**kS\u0019hx=s*]\u000ehwyyoKE-k\u0003h9)xcI\ny|yaoFKdjydeKKlo8cf^\ta|y~e\u001f\u001fep**iP\u000fh6\\\u000e\u007fm0lc\\\ny|\u001abkV\u0005-t,y~\u001f\bbw-kcQKlmyfo^\u0018y96do\u001f\bhk-clV\blm<";
      int var4 = "T\u000et94o~^\u000flm8*kS\u0019hx=s*^\u001fyx:bo[3m\u000e~m+ciK\u000ei92os\u001f\nnz<yy\u001f\be|:aoMKex**kS\u0019hx=s*]\u000ehwyyoKE-k\u0003h9)xcI\ny|yaoFKdjydeKKlo8cf^\ta|y~e\u001f\u001fep**iP\u000fh6\\\u000e\u007fm0lc\\\ny|\u001abkV\u0005-t,y~\u001f\bbw-kcQKlmyfo^\u0018y96do\u001f\bhk-clV\blm<"
         .length();
      char var1 = 29;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 16;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var8 = b(var10000, b(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     b = var5;
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var8;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "\u0007^=!ss:\u0015\u0012&%q&:\u0014W0`f<!FS!$i':\bUt0u!#\tA13S3A1`o5s\u0012Z=3 #!\u000fD54es8\u0003Kt)ss!\u0003A 2i0'\u0003Vt!n7s\u000fF'`u 6F['`n<'FB12m:'\u0012W0`i=s\u0012Z=3 0<\bF18t}";
                  var4 = "\u0007^=!ss:\u0015\u0012&%q&:\u0014W0`f<!FS!$i':\bUt0u!#\tA13S3A1`o5s\u0012Z=3 #!\u000fD54es8\u0003Kt)ss!\u0003A 2i0'\u0003Vt!n7s\u000fF'`u 6F['`n<'FB12m:'\u0012W0`i=s\u0012Z=3 0<\bF18t}"
                     .length();
                  var1 = '\'';
                  var7 = -1;
            }

            var10000 = 73;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

   private static char[] b(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 26);
      }

      return var10000;
   }

   private static String b(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 47;
            case 1 -> 123;
            case 2 -> 29;
            case 3 -> 9;
            case 4 -> 73;
            case 5 -> 26;
            default -> 26;
         });
      }

      return new String(var10001).intern();
   }
}
