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