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