AdvancedX509TrustManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.network;
  19. import java.security.KeyStore;
  20. import java.security.KeyStoreException;
  21. import java.security.NoSuchAlgorithmException;
  22. import java.security.cert.CertPathValidatorException;
  23. import java.security.cert.CertStoreException;
  24. import java.security.cert.CertificateException;
  25. import java.security.cert.CertificateExpiredException;
  26. import java.security.cert.CertificateNotYetValidException;
  27. import java.security.cert.X509Certificate;
  28. import javax.net.ssl.TrustManager;
  29. import javax.net.ssl.TrustManagerFactory;
  30. import javax.net.ssl.X509TrustManager;
  31. import android.util.Log;
  32. /**
  33. * @author David A. Velasco
  34. */
  35. public class AdvancedX509TrustManager implements X509TrustManager {
  36. private static final String TAG = AdvancedX509TrustManager.class.getSimpleName();
  37. private X509TrustManager mStandardTrustManager = null;
  38. private KeyStore mKnownServersKeyStore;
  39. /**
  40. * Constructor for AdvancedX509TrustManager
  41. *
  42. * @param knownServersCertStore Local certificates store with server certificates explicitly trusted by the user.
  43. * @throws CertStoreException When no default X509TrustManager instance was found in the system.
  44. */
  45. public AdvancedX509TrustManager(KeyStore knownServersKeyStore)
  46. throws NoSuchAlgorithmException, KeyStoreException, CertStoreException {
  47. super();
  48. TrustManagerFactory factory = TrustManagerFactory
  49. .getInstance(TrustManagerFactory.getDefaultAlgorithm());
  50. factory.init((KeyStore)null);
  51. mStandardTrustManager = findX509TrustManager(factory);
  52. mKnownServersKeyStore = knownServersKeyStore;
  53. }
  54. /**
  55. * Locates the first X509TrustManager provided by a given TrustManagerFactory
  56. * @param factory TrustManagerFactory to inspect in the search for a X509TrustManager
  57. * @return The first X509TrustManager found in factory.
  58. * @throws CertStoreException When no X509TrustManager instance was found in factory
  59. */
  60. private X509TrustManager findX509TrustManager(TrustManagerFactory factory) throws CertStoreException {
  61. TrustManager tms[] = factory.getTrustManagers();
  62. for (int i = 0; i < tms.length; i++) {
  63. if (tms[i] instanceof X509TrustManager) {
  64. return (X509TrustManager) tms[i];
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * @see javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate[],
  71. * String authType)
  72. */
  73. public void checkClientTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
  74. mStandardTrustManager.checkClientTrusted(certificates, authType);
  75. }
  76. /**
  77. * @see javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[],
  78. * String authType)
  79. */
  80. public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException {
  81. if (!isKnownServer(certificates[0])) {
  82. CertificateCombinedException result = new CertificateCombinedException(certificates[0]);
  83. try {
  84. certificates[0].checkValidity();
  85. } catch (CertificateExpiredException c) {
  86. result.setCertificateExpiredException(c);
  87. } catch (CertificateNotYetValidException c) {
  88. result.setCertificateNotYetException(c);
  89. }
  90. try {
  91. mStandardTrustManager.checkServerTrusted(certificates, authType);
  92. } catch (CertificateException c) {
  93. Throwable cause = c.getCause();
  94. Throwable previousCause = null;
  95. while (cause != null && cause != previousCause && !(cause instanceof CertPathValidatorException)) { // getCause() is not funny
  96. previousCause = cause;
  97. cause = cause.getCause();
  98. }
  99. if (cause != null && cause instanceof CertPathValidatorException) {
  100. result.setCertPathValidatorException((CertPathValidatorException)cause);
  101. } else {
  102. result.setOtherCertificateException(c);
  103. }
  104. }
  105. if (result.isException())
  106. throw result;
  107. }
  108. }
  109. /**
  110. * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
  111. */
  112. public X509Certificate[] getAcceptedIssuers() {
  113. return mStandardTrustManager.getAcceptedIssuers();
  114. }
  115. public boolean isKnownServer(X509Certificate cert) {
  116. try {
  117. return (mKnownServersKeyStore.getCertificateAlias(cert) != null);
  118. } catch (KeyStoreException e) {
  119. Log.d(TAG, "Fail while checking certificate in the known-servers store");
  120. return false;
  121. }
  122. }
  123. }