Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  2. if (chain == null) {
  3. throw new IllegalArgumentException(
  4. "checkServerTrusted: X509Certificate array is null");
  5. }
  6. if (!(chain.length > 0)) {
  7. throw new IllegalArgumentException(
  8. "checkServerTrusted: X509Certificate is empty");
  9. }
  10.  
  11. // Perform customary SSL/TLS checks
  12. TrustManagerFactory tmf;
  13. try {
  14. tmf = TrustManagerFactory.getInstance("X509");
  15. tmf.init((KeyStore) null);
  16.  
  17. for (TrustManager trustManager : tmf.getTrustManagers()) {
  18. ((X509TrustManager) trustManager).checkServerTrusted(
  19. chain, authType);
  20. }
  21.  
  22. } catch (Exception e) {
  23. throw new CertificateException(e.toString());
  24. }
  25.  
  26. // Hack ahead: BigInteger and toString(). We know a DER encoded Public
  27. // Key starts with 0x30 (ASN.1 SEQUENCE and CONSTRUCTED), so there is
  28. // no leading 0x00 to drop.
  29. RSAPublicKey pubkey = (RSAPublicKey) chain[0].getPublicKey();
  30. // String encoded = new BigInteger(1 /* positive */, pubkey.getEncoded())
  31. // .toString(16);
  32. String encoded = null;
  33. try {
  34. encoded = new String(Base64.encode(pubkey.getEncoded(), Base64.DEFAULT),"UTF-8");
  35. } catch (UnsupportedEncodingException e) {
  36. e.printStackTrace();
  37. }
  38.  
  39.  
  40. // Pin it!
  41. final boolean expected = publicKey.equalsIgnoreCase(encoded);
  42. // fail if expected public key is different from our public key
  43. if (!expected) {
  44. throw new CertificateException(
  45. "Not trusted");
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement