package com.l7tech.security.xml.processor;

import com.l7tech.security.token.EncryptedKey;
import com.l7tech.security.token.SecurityTokenType;
import com.l7tech.security.xml.KeyInfoElement;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.SignerInfo;
import com.l7tech.security.xml.UnexpectedKeyInfoException;
import com.l7tech.security.xml.XencUtil;
import com.l7tech.util.DomUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.util.SoapConstants;
import com.l7tech.xml.soap.SoapUtil;
import java.security.GeneralSecurityException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Element;

public class EncryptedKeyImpl extends f implements EncryptedKey {
   private final String c;
   private final byte[] d;
   private final SignerInfo e;
   private final String f;
   private final String g;
   private SecurityTokenResolver h;
   private String i;
   private byte[] j;
   private static final String[] k;

   public EncryptedKeyImpl(@NotNull Element var1, @NotNull SecurityTokenResolver var2) throws InvalidDocumentFormatException, GeneralSecurityException, UnexpectedKeyInfoException {
      if (var1 == null) {
         a(0);
      }

      if (var2 == null) {
         a(1);
      }

      this(var1, var2, null, null);
   }

   public EncryptedKeyImpl(@NotNull Element var1, @NotNull SecurityTokenResolver var2, @Nullable String var3, @Nullable String var4) throws InvalidDocumentFormatException, GeneralSecurityException, UnexpectedKeyInfoException {
      if (var1 == null) {
         a(2);
      }

      if (var2 == null) {
         a(3);
      }

      super(var1);
      this.i = null;
      this.j = null;
      this.c = SoapUtil.getElementWsuId(var1);
      this.h = var2;
      this.e = KeyInfoElement.getTargetPrivateKeyForEncryptedType(var1, var2);
      String var5 = XencUtil.getEncryptedKeyCipherValue(var1);
      this.d = HexUtils.decodeBase64(var5.trim());
      this.f = var3;
      this.g = var4;
   }

   @Override
   public SecurityTokenType getType() {
      return SecurityTokenType.WSS_ENCRYPTEDKEY;
   }

   @Override
   public String getElementId() {
      return this.c;
   }

   @Override
   public String toString() {
      String[] var2 = k;
      StringBuilder var1 = new StringBuilder(var2[3]);

      try {
         var1.append(this.c).append(var2[8]).append(this.isUnwrapped());
         if (this.j != null) {
            var1.append(var2[0]).append(this.j.length);
         }
      } catch (IllegalArgumentException var3) {
         throw a(var3);
      }

      try {
         if (this.i != null) {
            var1.append(k[7]).append(this.i);
         }
      } catch (IllegalArgumentException var4) {
         throw a(var4);
      }

      return var1.toString();
   }

   private void a() throws InvalidDocumentFormatException, GeneralSecurityException {
      try {
         this.getEncryptedKeySHA1();
         if (this.j != null) {
            return;
         }
      } catch (InvalidDocumentFormatException var3) {
         throw a(var3);
      }

      Element var1 = DomUtils.findOnlyOneChildElementByName(this.asElement(), SoapConstants.XMLENC_NS, k[2]);

      try {
         this.j = XencUtil.decryptKey(this.d, XencUtil.getOaepDigestAlg(var1), XencUtil.getOaepBytes(var1), this.e.getPrivate());
         if (this.e.isRestrictedAccess()) {
            this.h = null;
         }
      } catch (InvalidDocumentFormatException var2) {
         throw a(var2);
      }

      this.b();
   }

   @Override
   public byte[] getSecretKey() throws InvalidDocumentFormatException, GeneralSecurityException {
      try {
         if (this.j == null) {
            this.a();
         }
      } catch (InvalidDocumentFormatException var1) {
         throw a(var1);
      }

      return this.j;
   }

   @Override
   public boolean isUnwrapped() {
      try {
         if (this.j != null) {
            return true;
         }
      } catch (IllegalArgumentException var1) {
         throw a(var1);
      }

      return false;
   }

   void a(byte[] var1) {
      this.j = var1;
      this.b();
   }

   private void b() {
      // $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: bipush 0
      // 01: istore 1
      // 02: aload 0
      // 03: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 06: ifnull 38
      // 09: aload 0
      // 0a: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 0d: ifnull 38
      // 10: goto 17
      // 13: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 16: athrow
      // 17: aload 0
      // 18: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 1b: ifnull 38
      // 1e: goto 25
      // 21: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 24: athrow
      // 25: aload 0
      // 26: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 29: aload 0
      // 2a: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 2d: aload 0
      // 2e: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 31: invokeinterface com/l7tech/security/xml/SecurityTokenResolver.putSecretKeyByEncryptedKeySha1 (Ljava/lang/String;[B)V 3
      // 36: bipush 1
      // 37: istore 1
      // 38: aload 0
      // 39: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 3c: ifnull 80
      // 3f: aload 0
      // 40: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.f Ljava/lang/String;
      // 43: ifnull 80
      // 46: goto 4d
      // 49: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4c: athrow
      // 4d: aload 0
      // 4e: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.g Ljava/lang/String;
      // 51: ifnull 80
      // 54: goto 5b
      // 57: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 5a: athrow
      // 5b: aload 0
      // 5c: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 5f: ifnull 80
      // 62: goto 69
      // 65: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 68: athrow
      // 69: aload 0
      // 6a: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 6d: aload 0
      // 6e: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.f Ljava/lang/String;
      // 71: aload 0
      // 72: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.g Ljava/lang/String;
      // 75: aload 0
      // 76: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 79: invokeinterface com/l7tech/security/xml/SecurityTokenResolver.putSecretKeyByTokenIdentifier (Ljava/lang/String;Ljava/lang/String;[B)V 4
      // 7e: bipush 1
      // 7f: istore 1
      // 80: iload 1
      // 81: ifeq 90
      // 84: aload 0
      // 85: aconst_null
      // 86: putfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 89: goto 90
      // 8c: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 8f: athrow
      // 90: return
      // try (2 -> 8): 9 java/lang/IllegalArgumentException
      // try (5 -> 14): 15 java/lang/IllegalArgumentException
      // try (26 -> 32): 33 java/lang/IllegalArgumentException
      // try (29 -> 38): 39 java/lang/IllegalArgumentException
      // try (35 -> 44): 45 java/lang/IllegalArgumentException
      // try (58 -> 63): 64 java/lang/IllegalArgumentException
   }

   @Override
   public String getEncryptedKeySHA1() {
      // $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/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 04: ifnull 10
      // 07: aload 0
      // 08: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 0b: areturn
      // 0c: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0f: athrow
      // 10: aload 0
      // 11: aload 0
      // 12: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.d [B
      // 15: invokestatic com/l7tech/security/xml/XencUtil.computeEncryptedKeySha1 ([B)Ljava/lang/String;
      // 18: putfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 1b: aload 0
      // 1c: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 1f: ifnonnull 95
      // 22: aload 0
      // 23: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 26: ifnull 95
      // 29: goto 30
      // 2c: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2f: athrow
      // 30: aload 0
      // 31: aload 0
      // 32: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 35: aload 0
      // 36: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 39: invokeinterface com/l7tech/security/xml/SecurityTokenResolver.getSecretKeyByEncryptedKeySha1 (Ljava/lang/String;)[B 2
      // 3e: putfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 41: aload 0
      // 42: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 45: ifnull 99
      // 48: goto 4f
      // 4b: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4e: athrow
      // 4f: aload 0
      // 50: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.f Ljava/lang/String;
      // 53: ifnull 99
      // 56: goto 5d
      // 59: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 5c: athrow
      // 5d: aload 0
      // 5e: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.g Ljava/lang/String;
      // 61: ifnull 99
      // 64: goto 6b
      // 67: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 6a: athrow
      // 6b: aload 0
      // 6c: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 6f: ifnull 99
      // 72: goto 79
      // 75: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 78: athrow
      // 79: aload 0
      // 7a: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 7d: aload 0
      // 7e: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.f Ljava/lang/String;
      // 81: aload 0
      // 82: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.g Ljava/lang/String;
      // 85: aload 0
      // 86: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.j [B
      // 89: invokeinterface com/l7tech/security/xml/SecurityTokenResolver.putSecretKeyByTokenIdentifier (Ljava/lang/String;Ljava/lang/String;[B)V 4
      // 8e: goto 99
      // 91: invokestatic com/l7tech/security/xml/processor/EncryptedKeyImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 94: athrow
      // 95: aload 0
      // 96: invokevirtual com/l7tech/security/xml/processor/EncryptedKeyImpl.b ()V
      // 99: aload 0
      // 9a: getfield com/l7tech/security/xml/processor/EncryptedKeyImpl.i Ljava/lang/String;
      // 9d: areturn
      // try (0 -> 6): 6 java/lang/IllegalArgumentException
      // try (8 -> 19): 20 java/lang/IllegalArgumentException
      // try (16 -> 32): 33 java/lang/IllegalArgumentException
      // try (22 -> 38): 39 java/lang/IllegalArgumentException
      // try (35 -> 44): 45 java/lang/IllegalArgumentException
      // try (41 -> 50): 51 java/lang/IllegalArgumentException
      // try (47 -> 63): 63 java/lang/IllegalArgumentException
   }

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

   static {
      String[] var5 = new String[10];
      int var3 = 0;
      String var2 = "J9J\u000b\u0003$l\r&GO\u000e\u000f<L\u0000\u00161v\u000f6d\u0017\u0016\u0004n\u0010/<L\u0000\u00161v\u0003=A?\n5j\u00056\u0014/<L\u0000\u00161v\u000f6d\u0017\u0016{\"\u001d!Z;\u000b|>+ H\u0007\u0002$l\u001erI\u001d\u001daB$=[<\u001a-nJ\"N\u0000\u000e,g\u001e7]RHdqMr@\u0014OdqDw\\R\u00024q\u001erA\u001d\u001ba`\u000frA\u0007\u0003-\r\u001e=D\u0017\u0001\u0013g\u0019=C\u0004\n3\u0006V;A\u001b\u001b\u007f\u0012J7A\u0011\u001d8r\u001e7K9\n8Q\u00023\u001eO";
      int var4 = "J9J\u000b\u0003$l\r&GO\u000e\u000f<L\u0000\u00161v\u000f6d\u0017\u0016\u0004n\u0010/<L\u0000\u00161v\u0003=A?\n5j\u00056\u0014/<L\u0000\u00161v\u000f6d\u0017\u0016{\"\u001d!Z;\u000b|>+ H\u0007\u0002$l\u001erI\u001d\u001daB$=[<\u001a-nJ\"N\u0000\u000e,g\u001e7]RHdqMr@\u0014OdqDw\\R\u00024q\u001erA\u001d\u001ba`\u000frA\u0007\u0003-\r\u001e=D\u0017\u0001\u0013g\u0019=C\u0004\n3\u0006V;A\u001b\u001b\u007f\u0012J7A\u0011\u001d8r\u001e7K9\n8Q\u00023\u001eO"
         .length();
      char var1 = 11;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 103;
         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) {
                     k = var5;
                     return;
                  }

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

                  var2 = "3^8|dY\u000bcN262pD;$z\u000f\u000fvH>$e]\u0018fY?\u007fo\u0017\u0003~Gy{dW\u0018vX%dd\u0017>}H$rfL\u001ew`3r_U\u000b\u007f";
                  var4 = "3^8|dY\u000bcN262pD;$z\u000f\u000fvH>$e]\u0018fY?\u007fo\u0017\u0003~Gy{dW\u0018vX%dd\u0017>}H$rfL\u001ew`3r_U\u000b\u007f".length();
                  var1 = 11;
                  var7 = -1;
            }

            var10000 = 30;
            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] ^ 101);
      }

      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 -> 13;
            case 1 -> 53;
            case 2 -> 72;
            case 3 -> 21;
            case 4 -> 8;
            case 5 -> 38;
            default -> 101;
         });
      }

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