Merge pull request #1048 from owncloud/shareWithYou_icon_in_fileList
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / SslUntrustedCertDialog.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 package com.owncloud.android.ui.dialog;
22
23 import java.io.IOException;
24 import java.security.GeneralSecurityException;
25 import java.security.cert.X509Certificate;
26
27 import android.app.Activity;
28 import android.app.Dialog;
29 import android.net.http.SslError;
30 import android.os.Bundle;
31 import android.support.v4.app.DialogFragment;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.Window;
36 import android.view.View.OnClickListener;
37 import android.webkit.SslErrorHandler;
38 import android.widget.Button;
39
40 import com.owncloud.android.R;
41 import com.owncloud.android.lib.common.network.CertificateCombinedException;
42 import com.owncloud.android.lib.common.network.NetworkUtils;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.ui.adapter.CertificateCombinedExceptionViewAdapter;
45 import com.owncloud.android.ui.adapter.SslCertificateViewAdapter;
46 import com.owncloud.android.ui.adapter.SslErrorViewAdapter;
47 import com.owncloud.android.ui.adapter.X509CertificateViewAdapter;
48
49 /**
50 * Dialog to show information about an untrusted certificate and allow the user
51 * to decide trust on it or not.
52 *
53 * Abstract implementation of common functionality for different dialogs that
54 * get the information about the error and the certificate from different classes.
55 */
56 public class SslUntrustedCertDialog extends DialogFragment {
57
58 private final static String TAG = SslUntrustedCertDialog.class.getSimpleName();
59
60 protected View mView = null;
61 protected SslErrorHandler mHandler = null;
62 protected X509Certificate m509Certificate = null;
63
64 private ErrorViewAdapter mErrorViewAdapter = null;
65 private CertificateViewAdapter mCertificateViewAdapter = null;
66
67 public static SslUntrustedCertDialog newInstanceForEmptySslError(SslError error, SslErrorHandler handler) {
68 if (error == null) {
69 throw new IllegalArgumentException("Trying to create instance with parameter error == null");
70 }
71 if (handler == null) {
72 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
73 }
74 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
75 dialog.mHandler = handler;
76 dialog.mErrorViewAdapter = new SslErrorViewAdapter(error);
77 dialog.mCertificateViewAdapter = new SslCertificateViewAdapter(error.getCertificate());
78 return dialog;
79 }
80
81 public static SslUntrustedCertDialog newInstanceForFullSslError(CertificateCombinedException sslException) {
82 if (sslException == null) {
83 throw new IllegalArgumentException("Trying to create instance with parameter sslException == null");
84 }
85 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
86 dialog.m509Certificate = sslException.getServerCertificate();
87 dialog.mErrorViewAdapter = new CertificateCombinedExceptionViewAdapter(sslException);
88 dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(sslException.getServerCertificate());
89 return dialog;
90 }
91
92 public static SslUntrustedCertDialog newInstanceForFullSslError(X509Certificate cert, SslError error, SslErrorHandler handler) {
93 if (cert == null) {
94 throw new IllegalArgumentException("Trying to create instance with parameter cert == null");
95 }
96 if (error == null) {
97 throw new IllegalArgumentException("Trying to create instance with parameter error == null");
98 }
99 if (handler == null) {
100 throw new IllegalArgumentException("Trying to create instance with parameter handler == null");
101 }
102 SslUntrustedCertDialog dialog = new SslUntrustedCertDialog();
103 dialog.m509Certificate = cert;
104 dialog.mHandler = handler;
105 dialog.mErrorViewAdapter = new SslErrorViewAdapter(error);
106 dialog.mCertificateViewAdapter = new X509CertificateViewAdapter(cert);
107 return dialog;
108 }
109
110
111 @Override
112 public void onAttach(Activity activity) {
113 Log_OC.d(TAG, "onAttach");
114 super.onAttach(activity);
115 if (!(activity instanceof OnSslUntrustedCertListener)) {
116 throw new IllegalArgumentException("The host activity must implement " + OnSslUntrustedCertListener.class.getCanonicalName());
117 }
118 }
119
120
121 @Override
122 public void onCreate(Bundle savedInstanceState) {
123 Log_OC.d(TAG, "onCreate, savedInstanceState is " + savedInstanceState);
124 super.onCreate(savedInstanceState);
125 setRetainInstance(true); // force to keep the state of the fragment on configuration changes (such as device rotations)
126 setCancelable(false);
127 mView = null;
128 }
129
130 @Override
131 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
132 Log_OC.d(TAG, "onCreateView, savedInsanceState is " + savedInstanceState);
133 // Create a view by inflating desired layout
134 if (mView == null) {
135 mView = inflater.inflate(R.layout.ssl_untrusted_cert_layout, container, false);
136 mView.findViewById(R.id.details_scroll).setVisibility(View.GONE);
137 mErrorViewAdapter.updateErrorView(mView);
138 } else {
139 ((ViewGroup)mView.getParent()).removeView(mView);
140 }
141
142 Button ok = (Button) mView.findViewById(R.id.ok);
143 ok.setOnClickListener(new OnCertificateTrusted());
144
145 Button cancel = (Button) mView.findViewById(R.id.cancel);
146 cancel.setOnClickListener(new OnCertificateNotTrusted());
147
148 Button details = (Button) mView.findViewById(R.id.details_btn);
149 details.setOnClickListener(new OnClickListener() {
150
151 @Override
152 public void onClick(View v) {
153 View detailsScroll = mView.findViewById(R.id.details_scroll);
154 if (detailsScroll.getVisibility() == View.VISIBLE) {
155 detailsScroll.setVisibility(View.GONE);
156 ((Button) v).setText(R.string.ssl_validator_btn_details_see);
157
158 } else {
159 detailsScroll.setVisibility(View.VISIBLE);
160 ((Button) v).setText(R.string.ssl_validator_btn_details_hide);
161 mCertificateViewAdapter.updateCertificateView(mView);
162 }
163 }
164
165 });
166
167 return mView;
168 }
169
170
171 @Override
172 public Dialog onCreateDialog(Bundle savedInstanceState) {
173 Log_OC.d(TAG, "onCreateDialog, savedInstanceState is " + savedInstanceState);
174 final Dialog dialog = super.onCreateDialog(savedInstanceState);
175 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
176 return dialog;
177 }
178
179 @Override
180 public void onDestroyView() {
181 Log_OC.d(TAG, "onDestroyView");
182 if (getDialog() != null && getRetainInstance())
183 getDialog().setDismissMessage(null);
184 super.onDestroyView();
185 }
186
187 private class OnCertificateNotTrusted implements OnClickListener {
188
189 @Override
190 public void onClick(View v) {
191 getDialog().cancel();
192 if (mHandler != null) {
193 mHandler.cancel();
194 }
195 ((OnSslUntrustedCertListener)getActivity()).onCancelCertificate();
196 }
197 }
198
199
200 private class OnCertificateTrusted implements OnClickListener {
201
202 @Override
203 public void onClick(View v) {
204 dismiss();
205 if (mHandler != null) {
206 mHandler.proceed();
207 }
208 if (m509Certificate != null) {
209 Activity activity = getActivity();
210 try {
211 NetworkUtils.addCertToKnownServersStore(m509Certificate, activity); // TODO make this asynchronously, it can take some time
212 ((OnSslUntrustedCertListener)activity).onSavedCertificate();
213
214 } catch (GeneralSecurityException e) {
215 ((OnSslUntrustedCertListener)activity).onFailedSavingCertificate();
216 Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e);
217
218 } catch (IOException e) {
219 ((OnSslUntrustedCertListener)activity).onFailedSavingCertificate();
220 Log_OC.e(TAG, "Server certificate could not be saved in the known-servers trust store ", e);
221 }
222 }
223 }
224
225 }
226
227
228 public interface OnSslUntrustedCertListener {
229 public void onSavedCertificate();
230 public void onFailedSavingCertificate();
231 public void onCancelCertificate();
232 }
233
234 public interface ErrorViewAdapter {
235 void updateErrorView(View mView);
236 }
237
238 public interface CertificateViewAdapter {
239 void updateCertificateView(View mView);
240 }
241
242 }