45787d4f8d8dd49145ccb3b90d5a56b2f5e615f8
[pub/Android/ownCloud.git] / src / com / owncloud / android / network / CertificateCombinedException.java
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 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 */
18
19 package com.owncloud.android.network;
20
21 import java.security.cert.CertPathValidatorException;
22 import java.security.cert.CertificateException;
23 import java.security.cert.CertificateExpiredException;
24 import java.security.cert.CertificateNotYetValidException;
25 import java.security.cert.X509Certificate;
26
27 import javax.net.ssl.SSLPeerUnverifiedException;
28
29 /**
30 * Exception joining all the problems that {@link AdvancedX509TrustManager} can find in
31 * a certificate chain for a server.
32 *
33 * This was initially created as an extension of CertificateException, but some
34 * implementations of the SSL socket layer in existing devices are REPLACING the CertificateException
35 * instances thrown by {@link javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[], String)}
36 * with SSLPeerUnverifiedException FORGETTING THE CAUSING EXCEPTION instead of wrapping it.
37 *
38 * Due to this, extending RuntimeException is necessary to get that the CertificateCombinedException
39 * instance reaches {@link AdvancedSslSocketFactory#verifyPeerIdentity}.
40 *
41 * BE CAREFUL. As a RuntimeException extensions, Java compilers do not require to handle it
42 * in client methods. Be sure to use it only when you know exactly where it will go.
43 *
44 * @author David A. Velasco
45 */
46 public class CertificateCombinedException extends RuntimeException {
47
48 /** Generated - to refresh every time the class changes */
49 private static final long serialVersionUID = -8875782030758554999L;
50
51 private X509Certificate mServerCert = null;
52 private String mHostInUrl;
53
54 private CertificateExpiredException mCertificateExpiredException = null;
55 private CertificateNotYetValidException mCertificateNotYetValidException = null;
56 private CertPathValidatorException mCertPathValidatorException = null;
57 private CertificateException mOtherCertificateException = null;
58 private SSLPeerUnverifiedException mSslPeerUnverifiedException = null;
59
60 public CertificateCombinedException(X509Certificate x509Certificate) {
61 mServerCert = x509Certificate;
62 }
63
64 public X509Certificate getServerCertificate() {
65 return mServerCert;
66 }
67
68 public String getHostInUrl() {
69 return mHostInUrl;
70 }
71
72 public void setHostInUrl(String host) {
73 mHostInUrl = host;
74 }
75
76 public CertificateExpiredException getCertificateExpiredException() {
77 return mCertificateExpiredException;
78 }
79
80 public void setCertificateExpiredException(CertificateExpiredException c) {
81 mCertificateExpiredException = c;
82 }
83
84 public CertificateNotYetValidException getCertificateNotYetValidException() {
85 return mCertificateNotYetValidException;
86 }
87
88 public void setCertificateNotYetException(CertificateNotYetValidException c) {
89 mCertificateNotYetValidException = c;
90 }
91
92 public CertPathValidatorException getCertPathValidatorException() {
93 return mCertPathValidatorException;
94 }
95
96 public void setCertPathValidatorException(CertPathValidatorException c) {
97 mCertPathValidatorException = c;
98 }
99
100 public CertificateException getOtherCertificateException() {
101 return mOtherCertificateException;
102 }
103
104 public void setOtherCertificateException(CertificateException c) {
105 mOtherCertificateException = c;
106 }
107
108 public SSLPeerUnverifiedException getSslPeerUnverifiedException() {
109 return mSslPeerUnverifiedException ;
110 }
111
112 public void setSslPeerUnverifiedException(SSLPeerUnverifiedException s) {
113 mSslPeerUnverifiedException = s;
114 }
115
116 public boolean isException() {
117 return (mCertificateExpiredException != null ||
118 mCertificateNotYetValidException != null ||
119 mCertPathValidatorException != null ||
120 mOtherCertificateException != null ||
121 mSslPeerUnverifiedException != null);
122 }
123
124 public boolean isRecoverable() {
125 return (mCertificateExpiredException != null ||
126 mCertificateNotYetValidException != null ||
127 mCertPathValidatorException != null ||
128 mSslPeerUnverifiedException != null);
129 }
130
131 }