package com.l7tech.server.transport.http;

import com.l7tech.common.io.CertUtils;
import com.l7tech.objectmodel.FindException;
import com.l7tech.security.cert.CertVerifier;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.server.identity.cert.TrustedCertServices;
import com.l7tech.util.Config;
import com.l7tech.util.InetAddressUtil;
import java.security.cert.CertificateException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;

public class SslClientHostnameVerifier implements HostnameVerifier {
   private static final Logger a;
   private final Config b;
   private final TrustedCertServices c;
   private final boolean d;
   private static final String[] e;

   public SslClientHostnameVerifier(Config var1, TrustedCertServices var2) {
      this(var1, var2, false);
   }

   public SslClientHostnameVerifier(Config var1, TrustedCertServices var2, boolean var3) {
      this.b = var1;
      this.c = var2;
      this.d = var3;
   }

   @Override
   public boolean verify(String param1, SSLSession param2) {
      // $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 1
      // 01: ifnull 0f
      // 04: aload 2
      // 05: ifnonnull 18
      // 08: goto 0f
      // 0b: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0e: athrow
      // 0f: aload 0
      // 10: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a ()Z
      // 13: ireturn
      // 14: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 17: athrow
      // 18: aload 0
      // 19: aload 1
      // 1a: aload 2
      // 1b: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/String;Ljavax/net/ssl/SSLSession;)Z
      // 1e: ireturn
      // 1f: astore 3
      // 20: getstatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a Ljava/util/logging/Logger;
      // 23: getstatic java/util/logging/Level.WARNING Ljava/util/logging/Level;
      // 26: aload 1
      // 27: getstatic com/l7tech/server/transport/http/SslClientHostnameVerifier.e [Ljava/lang/String;
      // 2a: astore 4
      // 2c: aload 4
      // 2e: bipush 1
      // 2f: aaload
      // 30: swap
      // 31: aload 4
      // 33: bipush 2
      // 34: aaload
      // 35: invokedynamic makeConcatWithConstants (Ljava/lang/String;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\u0001" ]
      // 3a: aload 3
      // 3b: invokevirtual java/util/logging/Logger.log (Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
      // 3e: aload 0
      // 3f: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a ()Z
      // 42: ireturn
      // try (2 -> 10): 10 java/lang/Exception
      // try (0 -> 4): 5 java/lang/Exception
      // try (12 -> 16): 17 java/lang/Exception
   }

   private boolean a(String param1, SSLSession param2) throws SSLPeerUnverifiedException, FindException, 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 2
      // 01: invokeinterface javax/net/ssl/SSLSession.getPeerCertificates ()[Ljava/security/cert/Certificate; 1
      // 06: astore 3
      // 07: aload 3
      // 08: arraylength
      // 09: bipush 1
      // 0a: if_icmplt 1d
      // 0d: aload 3
      // 0e: bipush 0
      // 0f: aaload
      // 10: instanceof java/security/cert/X509Certificate
      // 13: ifne 26
      // 16: goto 1d
      // 19: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1c: athrow
      // 1d: aload 0
      // 1e: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a ()Z
      // 21: ireturn
      // 22: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 25: athrow
      // 26: aload 3
      // 27: bipush 0
      // 28: aaload
      // 29: checkcast java/security/cert/X509Certificate
      // 2c: astore 4
      // 2e: aload 0
      // 2f: aload 1
      // 30: aload 4
      // 32: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/String;Ljava/security/cert/X509Certificate;)Z
      // 35: ifne 48
      // 38: aload 0
      // 39: aload 4
      // 3b: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/security/cert/X509Certificate;)Z
      // 3e: ifeq 50
      // 41: goto 48
      // 44: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 47: athrow
      // 48: bipush 1
      // 49: goto 51
      // 4c: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4f: athrow
      // 50: bipush 0
      // 51: ireturn
      // try (3 -> 12): 13 javax/net/ssl/SSLPeerUnverifiedException
      // try (7 -> 18): 18 javax/net/ssl/SSLPeerUnverifiedException
      // try (25 -> 34): 35 javax/net/ssl/SSLPeerUnverifiedException
      // try (30 -> 39): 39 javax/net/ssl/SSLPeerUnverifiedException
   }

   private boolean a(X509Certificate param1) throws FindException {
      // $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 1
      // 02: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.c (Ljava/security/cert/X509Certificate;)Lcom/l7tech/security/cert/TrustedCert;
      // 05: astore 2
      // 06: aconst_null
      // 07: aload 2
      // 08: if_acmpne 11
      // 0b: aload 0
      // 0c: aload 1
      // 0d: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.b (Ljava/security/cert/X509Certificate;)Lcom/l7tech/security/cert/TrustedCert;
      // 10: astore 2
      // 11: aconst_null
      // 12: aload 2
      // 13: if_acmpeq 30
      // 16: aload 2
      // 17: invokevirtual com/l7tech/security/cert/TrustedCert.isVerifyHostname ()Z
      // 1a: ifne 2c
      // 1d: goto 24
      // 20: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 23: athrow
      // 24: bipush 1
      // 25: goto 34
      // 28: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2b: athrow
      // 2c: bipush 0
      // 2d: goto 34
      // 30: aload 0
      // 31: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a ()Z
      // 34: ireturn
      // try (11 -> 17): 18 com/l7tech/objectmodel/FindException
      // try (14 -> 22): 22 com/l7tech/objectmodel/FindException
   }

   private boolean a() {
      boolean var1 = true;
      String var2 = this.b.getProperty(e[3]);
      if (var2 != null) {
         var1 = Boolean.valueOf(var2.trim());
      }

      return !var1;
   }

   private boolean b() {
      boolean var1 = false;
      String var2 = this.b.getProperty(e[0]);
      if (var2 != null) {
         var1 = Boolean.valueOf(var2.trim());
      }

      return var1;
   }

   private boolean a(String var1, X509Certificate var2) throws CertificateParsingException {
      try {
         if (var1 == null) {
            return false;
         }
      } catch (CertificateParsingException var11) {
         throw a(var11);
      }

      boolean var3 = InetAddressUtil.looksLikeIpAddressV4OrV6(var1);

      X509Certificate var10000;
      byte var10001;
      label61: {
         try {
            var10000 = var2;
            if (var3) {
               var10001 = 7;
               break label61;
            }
         } catch (CertificateParsingException var10) {
            throw a(var10);
         }

         var10001 = 2;
      }

      Collection var4 = CertUtils.getSubjectAlternativeNames(var10000, Integer.valueOf(var10001));
      if (var4.isEmpty()) {
         String var5 = CertUtils.extractFirstCommonNameFromCertificate(var2);
         if (var5 != null) {
            var4 = Collections.singletonList(var5);
         }
      }

      boolean var12 = false;

      for (String var7 : var4) {
         label50: {
            label49: {
               try {
                  if (!var3 && this.b()) {
                     break label49;
                  }
               } catch (CertificateParsingException var9) {
                  throw a(var9);
               }

               var12 = var1.equalsIgnoreCase(var7);
               break label50;
            }

            var12 = CertUtils.domainNameMatchesPattern(var1, var7, this.d);
         }

         try {
            if (var12) {
               break;
            }
         } catch (CertificateParsingException var8) {
            throw a(var8);
         }
      }

      return var12;
   }

   private TrustedCert b(X509Certificate param1) throws FindException {
      // $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 1
      // 01: invokestatic com/l7tech/common/io/CertUtils.getIssuerDN (Ljava/security/cert/X509Certificate;)Ljava/lang/String;
      // 04: astore 2
      // 05: aload 0
      // 06: getfield com/l7tech/server/transport/http/SslClientHostnameVerifier.c Lcom/l7tech/server/identity/cert/TrustedCertServices;
      // 09: aload 2
      // 0a: bipush 0
      // 0b: aconst_null
      // 0c: aconst_null
      // 0d: invokeinterface com/l7tech/server/identity/cert/TrustedCertServices.getCertsBySubjectDnFiltered (Ljava/lang/String;ZLjava/util/Set;Ljava/util/Set;)Ljava/util/Collection; 5
      // 12: astore 3
      // 13: aload 3
      // 14: invokeinterface java/util/Collection.iterator ()Ljava/util/Iterator; 1
      // 19: astore 4
      // 1b: aload 4
      // 1d: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 22: ifeq 57
      // 25: aload 4
      // 27: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 2c: checkcast com/l7tech/security/cert/TrustedCert
      // 2f: astore 5
      // 31: aload 5
      // 33: getstatic com/l7tech/security/cert/TrustedCert$TrustedFor.SIGNING_SERVER_CERTS Lcom/l7tech/security/cert/TrustedCert$TrustedFor;
      // 36: invokevirtual com/l7tech/security/cert/TrustedCert.isTrustedFor (Lcom/l7tech/security/cert/TrustedCert$TrustedFor;)Z
      // 39: ifeq 54
      // 3c: aload 0
      // 3d: aload 1
      // 3e: aload 5
      // 40: invokevirtual com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/security/cert/X509Certificate;Lcom/l7tech/security/cert/TrustedCert;)Z
      // 43: ifeq 54
      // 46: goto 4d
      // 49: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4c: athrow
      // 4d: aload 5
      // 4f: areturn
      // 50: invokestatic com/l7tech/server/transport/http/SslClientHostnameVerifier.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 53: athrow
      // 54: goto 1b
      // 57: aconst_null
      // 58: areturn
      // try (21 -> 30): 31 com/l7tech/objectmodel/FindException
      // try (25 -> 35): 35 com/l7tech/objectmodel/FindException
   }

   boolean a(X509Certificate var1, TrustedCert var2) {
      try {
         CertVerifier.cachedVerify(var1, var2.getCertificate());
         return true;
      } catch (Exception var4) {
         return false;
      }
   }

   private TrustedCert c(X509Certificate var1) throws FindException {
      String var2 = CertUtils.getSubjectDN(var1);

      for (TrustedCert var5 : this.c.getCertsBySubjectDnFiltered(var2, false, null, null)) {
         try {
            if (CertUtils.certsAreEqual(var5.getCertificate(), var1)) {
               return var5;
            }
         } catch (FindException var6) {
            throw a(var6);
         }
      }

      return null;
   }

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "R&8)4BQs&\u0003)\u0001^NT>'4,VAZ;\u0014\u001bx&\u00051$\u0012LT=P+%@K]0P5/AVU(\u001d8`\u0015";
      int var4 = "R&8)4BQs&\u0003)\u0001^NT>'4,VAZ;\u0014\u001bx&\u00051$\u0012LT=P+%@K]0P5/AVU(\u001d8`\u0015".length();
      char var1 = 24;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 51;
         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) {
                     e = var5;
                     a = Logger.getLogger(SslClientHostnameVerifier.class.getName());
                     return;
                  }

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

                  var2 = "5N\u0011{\u000f\u0011\u0000\u001dkxZ\u000f*\u0000?~y{\u0006 ";
                  var4 = "5N\u0011{\u000f\u0011\u0000\u001dkxZ\u000f*\u0000?~y{\u0006 ".length();
                  var1 = 2;
                  var7 = -1;
            }

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

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

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

      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 -> 8;
            case 1 -> 122;
            case 2 -> 67;
            case 3 -> 110;
            case 4 -> 115;
            case 5 -> 1;
            default -> 17;
         });
      }

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