adeilsoncs

Untitled

Dec 6th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.79 KB | None | 0 0
  1. package okhttp3;
  2.  
  3. import java.security.cert.Certificate;
  4. import java.security.cert.X509Certificate;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.LinkedHashSet;
  8. import java.util.List;
  9. import java.util.Set;
  10. import javax.annotation.Nullable;
  11. import javax.net.ssl.SSLPeerUnverifiedException;
  12. import okhttp3.internal.Util;
  13. import okhttp3.internal.tls.CertificateChainCleaner;
  14. import p363e.C11133f;
  15.  
  16. public final class CertificatePinner {
  17.     public static final CertificatePinner DEFAULT = new Builder().build();
  18.     @Nullable
  19.     private final CertificateChainCleaner certificateChainCleaner;
  20.     private final Set<Pin> pins;
  21.  
  22.     public static final class Builder {
  23.         private final List<Pin> pins = new ArrayList();
  24.  
  25.         public final Builder add(String str, String... strArr) {
  26.             if (str != null) {
  27.                 for (String pin : strArr) {
  28.                     this.pins.add(new Pin(str, pin));
  29.                 }
  30.                 return this;
  31.             }
  32.             throw new NullPointerException("pattern == null");
  33.         }
  34.  
  35.         public final CertificatePinner build() {
  36.             return new CertificatePinner(new LinkedHashSet(this.pins), null);
  37.         }
  38.     }
  39.  
  40.     static final class Pin {
  41.         final String canonicalHostname;
  42.         final C11133f hash;
  43.         final String hashAlgorithm;
  44.         final String pattern;
  45.  
  46.         Pin(String str, String str2) {
  47.             String str3;
  48.             this.pattern = str;
  49.             if (str.startsWith("*.")) {
  50.                 StringBuilder sb = new StringBuilder("http://");
  51.                 sb.append(str.substring(2));
  52.                 str3 = HttpUrl.parse(sb.toString()).host();
  53.             } else {
  54.                 str3 = HttpUrl.parse("http://".concat(String.valueOf(str))).host();
  55.             }
  56.             this.canonicalHostname = str3;
  57.             if (str2.startsWith("sha1/")) {
  58.                 this.hashAlgorithm = "sha1/";
  59.                 this.hash = C11133f.m35517b(str2.substring(5));
  60.             } else if (str2.startsWith("sha256/")) {
  61.                 this.hashAlgorithm = "sha256/";
  62.                 this.hash = C11133f.m35517b(str2.substring(7));
  63.             } else {
  64.                 throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': ".concat(String.valueOf(str2)));
  65.             }
  66.             if (this.hash == null) {
  67.                 throw new IllegalArgumentException("pins must be base64: ".concat(String.valueOf(str2)));
  68.             }
  69.         }
  70.  
  71.         /* access modifiers changed from: 0000 */
  72.         public final boolean matches(String str) {
  73.             if (!this.pattern.startsWith("*.")) {
  74.                 return str.equals(this.canonicalHostname);
  75.             }
  76.             int indexOf = str.indexOf(46);
  77.             if ((str.length() - indexOf) - 1 == this.canonicalHostname.length()) {
  78.                 if (str.regionMatches(false, indexOf + 1, this.canonicalHostname, 0, this.canonicalHostname.length())) {
  79.                     return true;
  80.                 }
  81.             }
  82.             return false;
  83.         }
  84.  
  85.         public final boolean equals(Object obj) {
  86.             if (obj instanceof Pin) {
  87.                 Pin pin = (Pin) obj;
  88.                 if (this.pattern.equals(pin.pattern) && this.hashAlgorithm.equals(pin.hashAlgorithm) && this.hash.equals(pin.hash)) {
  89.                     return true;
  90.                 }
  91.             }
  92.             return false;
  93.         }
  94.  
  95.         public final int hashCode() {
  96.             return ((((this.pattern.hashCode() + 527) * 31) + this.hashAlgorithm.hashCode()) * 31) + this.hash.hashCode();
  97.         }
  98.  
  99.         public final String toString() {
  100.             StringBuilder sb = new StringBuilder();
  101.             sb.append(this.hashAlgorithm);
  102.             sb.append(this.hash.mo32336b());
  103.             return sb.toString();
  104.         }
  105.     }
  106.  
  107.     CertificatePinner(Set<Pin> set, @Nullable CertificateChainCleaner certificateChainCleaner2) {
  108.         this.pins = set;
  109.         this.certificateChainCleaner = certificateChainCleaner2;
  110.     }
  111.  
  112.     public final boolean equals(@Nullable Object obj) {
  113.         if (obj == this) {
  114.             return true;
  115.         }
  116.         if (obj instanceof CertificatePinner) {
  117.             CertificatePinner certificatePinner = (CertificatePinner) obj;
  118.             if (Util.equal(this.certificateChainCleaner, certificatePinner.certificateChainCleaner) && this.pins.equals(certificatePinner.pins)) {
  119.                 return true;
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.  
  125.     public final int hashCode() {
  126.         return ((this.certificateChainCleaner != null ? this.certificateChainCleaner.hashCode() : 0) * 31) + this.pins.hashCode();
  127.     }
  128.  
  129.     public final void check(String str, List<Certificate> list) throws SSLPeerUnverifiedException {
  130.         List findMatchingPins = findMatchingPins(str);
  131.         if (!findMatchingPins.isEmpty()) {
  132.             if (this.certificateChainCleaner != null) {
  133.                 list = this.certificateChainCleaner.clean(list, str);
  134.             }
  135.             int size = list.size();
  136.             for (int i = 0; i < size; i++) {
  137.                 X509Certificate x509Certificate = (X509Certificate) list.get(i);
  138.                 int size2 = findMatchingPins.size();
  139.                 C11133f fVar = null;
  140.                 C11133f fVar2 = null;
  141.                 for (int i2 = 0; i2 < size2; i2++) {
  142.                     Pin pin = (Pin) findMatchingPins.get(i2);
  143.                     if (pin.hashAlgorithm.equals("sha256/")) {
  144.                         if (fVar == null) {
  145.                             fVar = sha256(x509Certificate);
  146.                         }
  147.                         if (pin.hash.equals(fVar)) {
  148.                             return;
  149.                         }
  150.                     } else if (pin.hashAlgorithm.equals("sha1/")) {
  151.                         if (fVar2 == null) {
  152.                             fVar2 = sha1(x509Certificate);
  153.                         }
  154.                         if (pin.hash.equals(fVar2)) {
  155.                             return;
  156.                         }
  157.                     } else {
  158.                         StringBuilder sb = new StringBuilder("unsupported hashAlgorithm: ");
  159.                         sb.append(pin.hashAlgorithm);
  160.                         throw new AssertionError(sb.toString());
  161.                     }
  162.                 }
  163.             }
  164.             StringBuilder sb2 = new StringBuilder("Certificate pinning failure!\n  Peer certificate chain:");
  165.             int size3 = list.size();
  166.             for (int i3 = 0; i3 < size3; i3++) {
  167.                 X509Certificate x509Certificate2 = (X509Certificate) list.get(i3);
  168.                 sb2.append("\n    ");
  169.                 sb2.append(pin(x509Certificate2));
  170.                 sb2.append(": ");
  171.                 sb2.append(x509Certificate2.getSubjectDN().getName());
  172.             }
  173.             sb2.append("\n  Pinned certificates for ");
  174.             sb2.append(str);
  175.             sb2.append(":");
  176.             int size4 = findMatchingPins.size();
  177.             for (int i4 = 0; i4 < size4; i4++) {
  178.                 Pin pin2 = (Pin) findMatchingPins.get(i4);
  179.                 sb2.append("\n    ");
  180.                 sb2.append(pin2);
  181.             }
  182.             throw new SSLPeerUnverifiedException(sb2.toString());
  183.         }
  184.     }
  185.  
  186.     /* access modifiers changed from: 0000 */
  187.     public final List<Pin> findMatchingPins(String str) {
  188.         List<Pin> emptyList = Collections.emptyList();
  189.         for (Pin pin : this.pins) {
  190.             if (pin.matches(str)) {
  191.                 if (emptyList.isEmpty()) {
  192.                     emptyList = new ArrayList<>();
  193.                 }
  194.                 emptyList.add(pin);
  195.             }
  196.         }
  197.         return emptyList;
  198.     }
  199.  
  200.     /* access modifiers changed from: 0000 */
  201.     public final CertificatePinner withCertificateChainCleaner(@Nullable CertificateChainCleaner certificateChainCleaner2) {
  202.         if (Util.equal(this.certificateChainCleaner, certificateChainCleaner2)) {
  203.             return this;
  204.         }
  205.         return new CertificatePinner(this.pins, certificateChainCleaner2);
  206.     }
  207.  
  208.     public static String pin(Certificate certificate) {
  209.         if (certificate instanceof X509Certificate) {
  210.             StringBuilder sb = new StringBuilder("sha256/");
  211.             sb.append(sha256((X509Certificate) certificate).mo32336b());
  212.             return sb.toString();
  213.         }
  214.         throw new IllegalArgumentException("Certificate pinning requires X509 certificates");
  215.     }
  216.  
  217.     static C11133f sha1(X509Certificate x509Certificate) {
  218.         return C11133f.m35516a(x509Certificate.getPublicKey().getEncoded()).mo32339d();
  219.     }
  220.  
  221.     static C11133f sha256(X509Certificate x509Certificate) {
  222.         return C11133f.m35516a(x509Certificate.getPublicKey().getEncoded()).mo32340e();
  223.     }
  224. }
Add Comment
Please, Sign In to add comment