package com.l7tech.security.xml;

import com.l7tech.common.io.CertUtils;
import com.l7tech.common.io.XmlUtil;
import com.l7tech.security.keys.XmlSecretKey;
import com.l7tech.security.keys.XmlSecretKeyFactory;
import com.l7tech.util.DomUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.util.MissingRequiredElementException;
import com.l7tech.util.Pair;
import com.l7tech.util.SoapConstants;
import com.l7tech.util.Triple;
import com.l7tech.util.Functions.UnaryVoid;
import com.l7tech.xml.soap.SoapUtil;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.security.auth.x500.X500Principal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XmlElementDecryptor {
   private static final String[] a;

   public static Triple<String, NodeList, X509Certificate> unwrapDecryptAndReplaceElement(
      Element var0, SecurityTokenResolver var1, UnaryVoid<Throwable> var2, @Nullable XmlElementDecryptor.KeyInfoErrorListener var3
   ) throws GeneralSecurityException, InvalidDocumentFormatException, XencUtil.XencException {
      Pair var4 = a(var0, var1, var3);
      X509Certificate var5 = (X509Certificate)var4.left;
      byte[] var6 = (byte[])var4.right;
      String var7 = XencUtil.findAlgorithmUri(var0);
      XmlSecretKey var8 = XmlSecretKeyFactory.createKey(var6, var7);
      NodeList var9 = XencUtil.decryptAndReplaceUsingKey(var0, var8, var2);
      return new Triple(var7, var9, var5);
   }

   @NotNull
   private static Pair<X509Certificate, byte[]> a(Element var0, SecurityTokenResolver var1, @Nullable XmlElementDecryptor.KeyInfoErrorListener var2) throws InvalidDocumentFormatException, GeneralSecurityException {
      String[] var10 = a;
      Element var3 = XmlUtil.findExactlyOneChildElementByName(var0, SoapUtil.DIGSIG_URI, var10[6]);
      Element var4 = XmlUtil.findExactlyOneChildElementByName(var3, SoapUtil.XMLENC_NS, var10[10]);
      Element var5 = DomUtils.findOnlyOneChildElementByName(var4, SoapConstants.XMLENC_NS, var10[13]);
      SignerInfo var6 = b(var4, var1, var2);
      String var7 = XencUtil.getEncryptedKeyCipherValue(var4);
      byte[] var8 = HexUtils.decodeBase64(var7.trim());
      byte[] var9 = XencUtil.decryptKey(var8, XencUtil.getOaepDigestAlg(var5), XencUtil.getOaepBytes(var5), var6.getPrivate());
      return new Pair(var6.getCertificate(), var9);
   }

   static SignerInfo b(Element param0, SecurityTokenResolver param1, @Nullable XmlElementDecryptor.KeyInfoErrorListener param2) throws InvalidDocumentFormatException, CertificateException {
      // $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: getstatic com/l7tech/util/SoapConstants.DIGSIG_URI Ljava/lang/String;
      // 04: getstatic com/l7tech/util/SoapConstants.KINFO_EL_NAME Ljava/lang/String;
      // 07: invokestatic com/l7tech/util/DomUtils.findChildElementsByName (Lorg/w3c/dom/Element;Ljava/lang/String;Ljava/lang/String;)Ljava/util/List;
      // 0a: astore 3
      // 0b: aload 3
      // 0c: ifnull 20
      // 0f: aload 3
      // 10: invokeinterface java/util/List.size ()I 1
      // 15: bipush 1
      // 16: if_icmpge 3d
      // 19: goto 20
      // 1c: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1f: athrow
      // 20: new com/l7tech/util/InvalidDocumentFormatException
      // 23: dup
      // 24: aload 0
      // 25: invokeinterface org/w3c/dom/Element.getLocalName ()Ljava/lang/String; 1
      // 2a: getstatic com/l7tech/security/xml/XmlElementDecryptor.a [Ljava/lang/String;
      // 2d: bipush 14
      // 2f: aaload
      // 30: invokedynamic makeConcatWithConstants (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; bsm=java/lang/invoke/StringConcatFactory.makeConcatWithConstants (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; args=[ "\u0001\u0001" ]
      // 35: invokespecial com/l7tech/util/InvalidDocumentFormatException.<init> (Ljava/lang/String;)V
      // 38: athrow
      // 39: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 3c: athrow
      // 3d: bipush 0
      // 3e: istore 4
      // 40: aload 3
      // 41: invokeinterface java/util/List.iterator ()Ljava/util/Iterator; 1
      // 46: astore 5
      // 48: aload 5
      // 4a: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 4f: ifeq cd
      // 52: aload 5
      // 54: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 59: checkcast org/w3c/dom/Element
      // 5c: astore 6
      // 5e: aload 6
      // 60: getstatic com/l7tech/util/SoapConstants.DIGSIG_URI Ljava/lang/String;
      // 63: getstatic com/l7tech/security/xml/XmlElementDecryptor.a [Ljava/lang/String;
      // 66: astore 9
      // 68: aload 9
      // 6a: bipush 5
      // 6b: aaload
      // 6c: invokestatic com/l7tech/util/DomUtils.findOnlyOneChildElementByName (Lorg/w3c/dom/Element;Ljava/lang/String;Ljava/lang/String;)Lorg/w3c/dom/Element;
      // 6f: astore 7
      // 71: aload 7
      // 73: ifnonnull 87
      // 76: new com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // 79: dup
      // 7a: aload 9
      // 7c: bipush 11
      // 7e: aaload
      // 7f: invokespecial com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException.<init> (Ljava/lang/String;)V
      // 82: athrow
      // 83: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 86: athrow
      // 87: aload 7
      // 89: aload 1
      // 8a: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Lorg/w3c/dom/Element;Lcom/l7tech/security/xml/SecurityTokenResolver;)Lcom/l7tech/security/xml/SignerInfo;
      // 8d: astore 8
      // 8f: aload 8
      // 91: ifnull 97
      // 94: aload 8
      // 96: areturn
      // 97: bipush 1
      // 98: istore 4
      // 9a: goto ca
      // 9d: astore 7
      // 9f: aload 2
      // a0: ifnull b2
      // a3: aload 2
      // a4: aload 7
      // a6: invokeinterface com/l7tech/security/xml/XmlElementDecryptor$KeyInfoErrorListener.onUnsupportedKeyInfoFormat (Lcom/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException;)V 2
      // ab: goto b2
      // ae: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // b1: athrow
      // b2: goto ca
      // b5: astore 7
      // b7: aload 2
      // b8: ifnull ca
      // bb: aload 2
      // bc: aload 7
      // be: invokeinterface com/l7tech/security/xml/XmlElementDecryptor$KeyInfoErrorListener.onInvalidDocumentFormat (Lcom/l7tech/util/InvalidDocumentFormatException;)V 2
      // c3: goto ca
      // c6: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // c9: athrow
      // ca: goto 48
      // cd: iload 4
      // cf: ifeq e4
      // d2: new com/l7tech/util/InvalidDocumentFormatException
      // d5: dup
      // d6: getstatic com/l7tech/security/xml/XmlElementDecryptor.a [Ljava/lang/String;
      // d9: bipush 15
      // db: aaload
      // dc: invokespecial com/l7tech/util/InvalidDocumentFormatException.<init> (Ljava/lang/String;)V
      // df: athrow
      // e0: invokestatic com/l7tech/security/xml/XmlElementDecryptor.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // e3: athrow
      // e4: new com/l7tech/util/InvalidDocumentFormatException
      // e7: dup
      // e8: getstatic com/l7tech/security/xml/XmlElementDecryptor.a [Ljava/lang/String;
      // eb: bipush 1
      // ec: aaload
      // ed: invokespecial com/l7tech/util/InvalidDocumentFormatException.<init> (Ljava/lang/String;)V
      // f0: athrow
      // try (47 -> 56): 56 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (7 -> 24): 24 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (5 -> 11): 12 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (38 -> 65): 69 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (66 -> 68): 69 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (38 -> 65): 79 com/l7tech/util/InvalidDocumentFormatException
      // try (66 -> 68): 79 com/l7tech/util/InvalidDocumentFormatException
      // try (70 -> 75): 76 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (80 -> 85): 86 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
      // try (89 -> 98): 98 com/l7tech/security/xml/KeyInfoElement$UnsupportedKeyInfoFormatException
   }

   static SignerInfo a(Element var0, SecurityTokenResolver var1) throws CertificateException, InvalidDocumentFormatException {
      String[] var9 = a;
      Element var2 = DomUtils.findOnlyOneChildElementByName(var0, SoapConstants.DIGSIG_URI, var9[2]);
      Element var3 = DomUtils.findOnlyOneChildElementByName(var0, SoapConstants.DIGSIG_URI, var9[4]);
      Element var4 = DomUtils.findOnlyOneChildElementByName(var0, SoapConstants.DIGSIG_URI, var9[0]);
      if (var2 != null) {
         String var13 = DomUtils.getTextValue(var2);
         byte[] var15 = HexUtils.decodeBase64(var13, true);
         return var1.lookupPrivateKeyByCert(CertUtils.decodeCert(var15));
      }

      if (var3 != null) {
         String var12 = DomUtils.getTextValue(var3);
         String var14 = HexUtils.encodeBase64(HexUtils.decodeBase64(var12, true), true);
         return var1.lookupPrivateKeyBySki(var14);
      }

      if (var4 != null) {
         var9 = a;
         Element var5 = DomUtils.findExactlyOneChildElementByName(var4, SoapConstants.DIGSIG_URI, var9[8]);
         Element var6 = DomUtils.findExactlyOneChildElementByName(var4, SoapConstants.DIGSIG_URI, var9[3]);
         String var7 = DomUtils.getTextValue(var5);

         try {
            if (var7.length() == 0) {
               throw new MissingRequiredElementException(var9[9]);
            }
         } catch (CertificateException var11) {
            throw a(var11);
         }

         String var8 = DomUtils.getTextValue(var6);

         try {
            if (var8.length() == 0) {
               throw new MissingRequiredElementException(a[7]);
            }
         } catch (CertificateException var10) {
            throw a(var10);
         }

         return var1.lookupPrivateKeyByIssuerAndSerial(new X500Principal(var7), new BigInteger(var8));
      } else {
         throw new KeyInfoElement.UnsupportedKeyInfoFormatException(a[12]);
      }
   }

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

   static {
      String[] var5 = new String[16];
      int var3 = 0;
      String var2 = "w jBd-hZp((H,rNy=j{9\tT.oJq\u000e\u0002];;K|>[C1o\u000fv5\u0015Y?rA5;[f;bf{<\u0014\r7u\u000ftz\bX.k@g.\u001eI~}@g7\u001aY\u000fw jBn;i[|<\u0012N?oJ\u0010w jB~;iFt65X3yJg\u0007w jB~\u0015R\bw jBi?oN\u0007dp#2C8t\u001aw jB~;iFt65X3yJgz\fL-;Jx*\u000fT\u000ew jBd-hZp(5L3~\u0018w jBd-hZp(5L3~\u000fb;\b\r;v_a#\fj{9\tT.oJq\u0011\u001eTCdp#2C8t\u000fq3\u001f\r0t[59\u0014C*zF{z\u001aC';]p9\u0014J0rUp>[N;i[|<\u0012N?oJ5(\u001eK;iJ{9\u001e\r8t]x;\u000f.dp#2C8t\u000fMoK\u0014\u001az[tz\fL-;Az.[D0;N5)\u000e].t]a?\u001f\r8t]x;\u000f\u0010j{9\tT.oFz46H*s@q";
      int var4 = "w jBd-hZp((H,rNy=j{9\tT.oJq\u000e\u0002];;K|>[C1o\u000fv5\u0015Y?rA5;[f;bf{<\u0014\r7u\u000ftz\bX.k@g.\u001eI~}@g7\u001aY\u000fw jBn;i[|<\u0012N?oJ\u0010w jB~;iFt65X3yJg\u0007w jB~\u0015R\bw jBi?oN\u0007dp#2C8t\u001aw jB~;iFt65X3yJgz\fL-;Jx*\u000fT\u000ew jBd-hZp(5L3~\u0018w jBd-hZp(5L3~\u000fb;\b\r;v_a#\fj{9\tT.oJq\u0011\u001eTCdp#2C8t\u000fq3\u001f\r0t[59\u0014C*zF{z\u001aC';]p9\u0014J0rUp>[N;i[|<\u0012N?oJ5(\u001eK;iJ{9\u001e\r8t]x;\u000f.dp#2C8t\u000fMoK\u0014\u001az[tz\fL-;Az.[D0;N5)\u000e].t]a?\u001f\r8t]x;\u000f\u0010j{9\tT.oFz46H*s@q"
         .length();
      char var1 = 16;
      int var7 = -1;

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

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

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

                  var2 = "n\u001dUy J\u001e+\u0007\u001bt#\u001f1+\rrt*PZ+\u0018^w)Q\u000e_\u000b\u001aXh5O\u000e'\u001bU:>Z\u0019'\u0004R\u007f\"KZ9\u0015H:\"P\u000en\u0006^y#X\u0014'\u000e^~l^\tn\u0015_~>Z\t=\u0011_:8PZ/TKh%I\u001b:\u0011\u001bq)FZ>\u001bHi)L\t+\u0010\u001bx5\u001f\u000e&\u001dH:\u000b^\u000e+\u0003Zc";
                  var4 = "n\u001dUy J\u001e+\u0007\u001bt#\u001f1+\rrt*PZ+\u0018^w)Q\u000e_\u000b\u001aXh5O\u000e'\u001bU:>Z\u0019'\u0004R\u007f\"KZ9\u0015H:\"P\u000en\u0006^y#X\u0014'\u000e^~l^\tn\u0015_~>Z\t=\u0011_:8PZ/TKh%I\u001b:\u0011\u001bq)FZ>\u001bHi)L\t+\u0010\u001bx5\u001f\u000e&\u001dH:\u000b^\u000e+\u0003Zc"
                     .length();
                  var1 = 28;
                  var7 = -1;
            }

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

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

      return var10000;
   }

   private static String a(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 -> 60;
            case 1 -> 6;
            case 2 -> 73;
            case 3 -> 104;
            case 4 -> 62;
            case 5 -> 77;
            default -> 8;
         });
      }

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

   public interface KeyInfoErrorListener {
      void onUnsupportedKeyInfoFormat(KeyInfoElement.UnsupportedKeyInfoFormatException var1);

      void onInvalidDocumentFormat(InvalidDocumentFormatException var1);
   }
}
