Advertisement
Guest User

Funambol FakeTrustManager.java

a guest
Dec 26th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package com.funambol.platform;
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.cert.CertificateEncodingException;
  6. import java.security.cert.CertificateException;
  7. import java.security.cert.X509Certificate;
  8.  
  9. import javax.net.ssl.X509TrustManager;
  10. import com.funambol.util.Log;
  11.  
  12. public class FakeTrustManager implements X509TrustManager {
  13.                 private String certKey = null;
  14.         private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
  15.                 private static final String LOG_NAME = "TrustManager";
  16.  
  17.         FakeTrustManager(String certKey){
  18.                 super();
  19.                 this.certKey = certKey;
  20.         }
  21.         FakeTrustManager(){
  22.                 super();
  23.         }
  24.         @Override
  25.         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  26.         }
  27.  
  28.         @Override
  29.         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  30.                 if( this.certKey == null ){
  31.                         // This is the Accept All certificates case.
  32.                         return;
  33.                 }
  34.  
  35.                 // Otherwise, we have a certKey defined. We should now examine the one we got from the server.
  36.                 // They match? All is good. They don't, throw an exception.
  37.                 String our_key = this.certKey.replaceAll("\\s+", "");
  38.                 try {
  39.                 //Assume self-signed root is okay?
  40.                 X509Certificate ss_cert = chain[0];
  41.                                 String thumbprint = FakeTrustManager.getThumbPrint(ss_cert);
  42.                     if (Log.isLoggable(Log.DEBUG)) {
  43.                     Log.debug("FakeTrustManager", "Certificate Thumbprint: " + thumbprint);
  44.                 }
  45.                                 if( our_key.equalsIgnoreCase(thumbprint) ){
  46.                                         return;
  47.                                 }
  48.                                 else {
  49.                                         throw new CertificateException("Certificate key [" + thumbprint + "] doesn't match expected value.");
  50.                                 }
  51.                         } catch (NoSuchAlgorithmException e) {
  52.                                 throw new CertificateException("Unable to check self-signed cert, unknown algorithm. " + e.toString());
  53.                         }
  54.  
  55.         }
  56.  
  57.         public boolean isClientTrusted(X509Certificate[] chain) {
  58.                 return true;
  59.         }
  60.  
  61.         public boolean isServerTrusted(X509Certificate[] chain) {
  62.                 return true;
  63.         }
  64.  
  65.         @Override
  66.         public X509Certificate[] getAcceptedIssuers() {
  67.                 return _AcceptedIssuers;
  68.         }
  69.  
  70.         // Thank you: http://stackoverflow.com/questions/1270703/how-to-retrieve-compute-an-x509-certificates-thumbprint-in-java
  71.         private static String getThumbPrint(X509Certificate cert) throws NoSuchAlgorithmException, CertificateEncodingException {
  72.             MessageDigest md = MessageDigest.getInstance("SHA-1");
  73.             byte[] der = cert.getEncoded();
  74.             md.update(der);
  75.             byte[] digest = md.digest();
  76.             return hexify(digest);
  77.         }
  78.  
  79.         private static String hexify (byte bytes[]) {
  80.             char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7',
  81.                             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  82.  
  83.             StringBuffer buf = new StringBuffer(bytes.length * 2);
  84.  
  85.             for (int i = 0; i < bytes.length; ++i) {
  86.                     buf.append(hexDigits[(bytes[i] & 0xf0) >> 4]);
  87.                 buf.append(hexDigits[bytes[i] & 0x0f]);
  88.             }
  89.  
  90.             return buf.toString();
  91.         }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement