SslAnalyzer.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  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 3 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. package com.owncloud.android.network;
  18. import java.security.cert.CertPathValidatorException;
  19. import java.security.cert.CertificateExpiredException;
  20. import java.security.cert.CertificateNotYetValidException;
  21. import javax.net.ssl.SSLException;
  22. import javax.net.ssl.SSLPeerUnverifiedException;
  23. import com.owncloud.android.operations.RemoteOperationResult;
  24. /**
  25. * Helper class to check if a SSL error is related to a condition that could be avoided with assistance from the user.
  26. *
  27. * @author David A. Velasco
  28. */
  29. public class SslAnalyzer {
  30. /**
  31. * Search for a SSL-related exception in a remote operation result that can be recoverable
  32. * by allowing the user to state the reliability of the certificate from the server.
  33. *
  34. * @param result Result of a remote operation.
  35. * @return An exception instance that caused the failure of the remote operation and that can be avoided if the user
  36. * states the certificate from the server as reliable; or NULL if the result is that's not possible
  37. */
  38. public static Exception getRecoverableException(RemoteOperationResult result) {
  39. Exception ret = null;
  40. SSLException e = (SSLException)result.getException();
  41. Throwable cause = null;
  42. if (e != null) {
  43. if (e instanceof SSLPeerUnverifiedException) {
  44. ret = e;
  45. } else {
  46. cause = e.getCause();
  47. Throwable previousCause = null;
  48. boolean recoverableCertException = false;
  49. while (cause != null && cause != previousCause && !recoverableCertException) { // getCause() is not funny
  50. recoverableCertException = ( cause instanceof CertPathValidatorException ||
  51. cause instanceof CertificateExpiredException ||
  52. cause instanceof CertificateNotYetValidException );
  53. if (recoverableCertException)
  54. ret = (Exception)cause;
  55. previousCause = cause;
  56. cause = cause.getCause();
  57. }
  58. }
  59. }
  60. return ret;
  61. }
  62. /**
  63. * Checks if a remote operation result can be recoverable
  64. * by allowing the user to state the reliability of the certificate from the server.
  65. *
  66. * @param result Result of a remote operation.
  67. * @return An exception instance that caused the failure of the remote operation and that can be avoided if the user
  68. * states the certificate from the server as reliable; or NULL if the result is that's not possible
  69. */
  70. public static boolean isRecoverable(RemoteOperationResult result) {
  71. return (getRecoverableException(result) != null);
  72. }
  73. }