f9769e62d7586d516b966b4f407d76643f51ca45
[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 = null;
46 Throwable cause = null;
47 if (result.getException() instanceof SSLException) {
48 e = (SSLException)result.getException();
49 if (e instanceof SSLPeerUnverifiedException) {
50 ret = e;
51
52 } else {
53 cause = e.getCause();
54 Throwable previousCause = null;
55 boolean recoverableCertException = false;
56 while (cause != null && cause != previousCause && !recoverableCertException) { // getCause() is not funny
57 recoverableCertException = ( cause instanceof CertPathValidatorException ||
58 cause instanceof CertificateExpiredException ||
59 cause instanceof CertificateNotYetValidException );
60 if (recoverableCertException)
61 ret = (Exception)cause;
62 previousCause = cause;
63 cause = cause.getCause();
64 }
65 }
66 }
67 return ret;
68 }
69
70
71 /**
72 * Checks if a remote operation result can be recoverable
73 * by allowing the user to state the reliability of the certificate from the server.
74 *
75 * @param result Result of a remote operation.
76 * @return An exception instance that caused the failure of the remote operation and that can be avoided if the user
77 * states the certificate from the server as reliable; or NULL if the result is that's not possible
78 */
79 public static boolean isRecoverable(RemoteOperationResult result) {
80 return (getRecoverableException(result) != null);
81 }
82
83 }