1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
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.
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.
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/>.
17 package com
.owncloud
.android
.network
;
19 import java
.security
.cert
.CertPathValidatorException
;
20 import java
.security
.cert
.CertificateExpiredException
;
21 import java
.security
.cert
.CertificateNotYetValidException
;
23 import javax
.net
.ssl
.SSLException
;
24 import javax
.net
.ssl
.SSLPeerUnverifiedException
;
26 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
29 * Helper class to check if a SSL error is related to a condition that could be avoided with assistance from the user.
31 * @author David A. Velasco
33 public class SslAnalyzer
{
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.
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
43 public static Exception
getRecoverableException(RemoteOperationResult result
) {
45 SSLException e
= null
;
46 Throwable cause
= null
;
47 if (result
.getException() instanceof SSLException
) {
48 e
= (SSLException
)result
.getException();
49 if (e
instanceof SSLPeerUnverifiedException
) {
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();
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.
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
79 public static boolean isRecoverable(RemoteOperationResult result
) {
80 return (getRecoverableException(result
) != null
);