cf48423c600b3b48fb72fa910d176363e524f86b
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / oc_framework / network / CertificateCombinedException.java
1 /* ownCloud webDAV Library for Android is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 *
23 */
24
25 package com.owncloud.android.oc_framework.network;
26
27 import java.security.cert.CertPathValidatorException;
28 import java.security.cert.CertificateException;
29 import java.security.cert.CertificateExpiredException;
30 import java.security.cert.CertificateNotYetValidException;
31 import java.security.cert.X509Certificate;
32
33 import javax.net.ssl.SSLPeerUnverifiedException;
34
35 /**
36 * Exception joining all the problems that {@link AdvancedX509TrustManager} can find in
37 * a certificate chain for a server.
38 *
39 * This was initially created as an extension of CertificateException, but some
40 * implementations of the SSL socket layer in existing devices are REPLACING the CertificateException
41 * instances thrown by {@link javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate[], String)}
42 * with SSLPeerUnverifiedException FORGETTING THE CAUSING EXCEPTION instead of wrapping it.
43 *
44 * Due to this, extending RuntimeException is necessary to get that the CertificateCombinedException
45 * instance reaches {@link AdvancedSslSocketFactory#verifyPeerIdentity}.
46 *
47 * BE CAREFUL. As a RuntimeException extensions, Java compilers do not require to handle it
48 * in client methods. Be sure to use it only when you know exactly where it will go.
49 *
50 * @author David A. Velasco
51 */
52 public class CertificateCombinedException extends RuntimeException {
53
54 /** Generated - to refresh every time the class changes */
55 private static final long serialVersionUID = -8875782030758554999L;
56
57 private X509Certificate mServerCert = null;
58 private String mHostInUrl;
59
60 private CertificateExpiredException mCertificateExpiredException = null;
61 private CertificateNotYetValidException mCertificateNotYetValidException = null;
62 private CertPathValidatorException mCertPathValidatorException = null;
63 private CertificateException mOtherCertificateException = null;
64 private SSLPeerUnverifiedException mSslPeerUnverifiedException = null;
65
66 public CertificateCombinedException(X509Certificate x509Certificate) {
67 mServerCert = x509Certificate;
68 }
69
70 public X509Certificate getServerCertificate() {
71 return mServerCert;
72 }
73
74 public String getHostInUrl() {
75 return mHostInUrl;
76 }
77
78 public void setHostInUrl(String host) {
79 mHostInUrl = host;
80 }
81
82 public CertificateExpiredException getCertificateExpiredException() {
83 return mCertificateExpiredException;
84 }
85
86 public void setCertificateExpiredException(CertificateExpiredException c) {
87 mCertificateExpiredException = c;
88 }
89
90 public CertificateNotYetValidException getCertificateNotYetValidException() {
91 return mCertificateNotYetValidException;
92 }
93
94 public void setCertificateNotYetException(CertificateNotYetValidException c) {
95 mCertificateNotYetValidException = c;
96 }
97
98 public CertPathValidatorException getCertPathValidatorException() {
99 return mCertPathValidatorException;
100 }
101
102 public void setCertPathValidatorException(CertPathValidatorException c) {
103 mCertPathValidatorException = c;
104 }
105
106 public CertificateException getOtherCertificateException() {
107 return mOtherCertificateException;
108 }
109
110 public void setOtherCertificateException(CertificateException c) {
111 mOtherCertificateException = c;
112 }
113
114 public SSLPeerUnverifiedException getSslPeerUnverifiedException() {
115 return mSslPeerUnverifiedException ;
116 }
117
118 public void setSslPeerUnverifiedException(SSLPeerUnverifiedException s) {
119 mSslPeerUnverifiedException = s;
120 }
121
122 public boolean isException() {
123 return (mCertificateExpiredException != null ||
124 mCertificateNotYetValidException != null ||
125 mCertPathValidatorException != null ||
126 mOtherCertificateException != null ||
127 mSslPeerUnverifiedException != null);
128 }
129
130 public boolean isRecoverable() {
131 return (mCertificateExpiredException != null ||
132 mCertificateNotYetValidException != null ||
133 mCertPathValidatorException != null ||
134 mSslPeerUnverifiedException != null);
135 }
136
137 }