Enable HTTP authentication. Implements https://redmine.stoutner.com/issues/52.
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / PinnedSslCertificateMismatchDialog.java
1 /*
2  * Copyright © 2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.net.http.SslCertificate;
28 import android.os.Bundle;
29 import android.support.annotation.NonNull;
30 import android.support.design.widget.TabLayout;
31 import android.support.v4.view.PagerAdapter;
32 // `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
33 import android.support.v7.app.AppCompatDialogFragment;
34 import android.text.SpannableStringBuilder;
35 import android.text.Spanned;
36 import android.text.style.ForegroundColorSpan;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.TextView;
41
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
44 import com.stoutner.privacybrowser.definitions.WrapVerticalContentViewPager;
45 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
46
47 import java.text.DateFormat;
48 import java.util.Date;
49
50 public class PinnedSslCertificateMismatchDialog extends AppCompatDialogFragment {
51     // `layoutInflater` is used in `onCreateDialog()` and `pagerAdapter`.
52     private LayoutInflater layoutInflater;
53
54     // The current website SSL certificate variables are used in `onCreateDialog()` and `pagerAdapter()`.
55     private String currentSslIssuedToCNameString;
56     private String currentSslIssuedToONameString;
57     private String currentSslIssuedToUNameString;
58     private String currentSslIssuedByCNameString;
59     private String currentSslIssuedByONameString;
60     private String currentSslIssuedByUNameString;
61     private Date currentSslStartDate;
62     private Date currentSslEndDate;
63
64     // The public interface is used to send information back to the parent activity.
65     public interface PinnedSslCertificateMismatchListener {
66         void onSslMismatchBack();
67
68         void onSslMismatchProceed();
69     }
70
71     // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
72     private PinnedSslCertificateMismatchDialog.PinnedSslCertificateMismatchListener pinnedSslCertificateMismatchListener;
73
74     // Check to make sure that the parent activity implements the listener.
75     public void onAttach(Context context) {
76         super.onAttach(context);
77
78         try {
79             pinnedSslCertificateMismatchListener = (PinnedSslCertificateMismatchDialog.PinnedSslCertificateMismatchListener) context;
80         } catch(ClassCastException exception) {
81             throw new ClassCastException(context.toString() + " must implement PinnedSslCertificateMismatchListener");
82         }
83     }
84
85     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
86     @SuppressLint("InflateParams")
87     @Override
88     @NonNull
89     public Dialog onCreateDialog(Bundle savedInstanceState) {
90         // Get the activity's layout inflater.
91         layoutInflater = getActivity().getLayoutInflater();
92
93         // Use `AlertDialog.Builder` to create the `AlertDialog`.
94         AlertDialog.Builder dialogBuilder;
95
96         // Set the style according to the theme.
97         if (MainWebViewActivity.darkTheme) {
98             // Set the dialog theme.
99             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
100
101             // Set the icon.
102             dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
103         } else {
104             // Set the dialog theme.
105             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
106
107             // Set the icon.
108             dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
109         }
110
111         // Setup the neutral button.
112         dialogBuilder.setNeutralButton(R.string.update_ssl, new DialogInterface.OnClickListener() {
113             @Override
114             public void onClick(DialogInterface dialog, int which) {
115                 // Initialize the `long` date variables.  If the date is `null`, a long value of `0` will be stored in the Domains database entry.
116                 long currentSslStartDateLong = 0;
117                 long currentSslEndDateLong = 0;
118
119                 // Convert the `Dates` into `longs`.
120                 if (currentSslStartDate != null) {
121                     currentSslStartDateLong = currentSslStartDate.getTime();
122                 }
123
124                 if (currentSslEndDate != null) {
125                     currentSslEndDateLong = currentSslEndDate.getTime();
126                 }
127
128                 // Initialize the database handler.  The two `nulls` do not specify the database name or a `CursorFactory`.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
129                 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
130
131                 // Update the pinned SSL certificate for this domain.
132                 domainsDatabaseHelper.updateCertificate(MainWebViewActivity.domainSettingsDatabaseId, currentSslIssuedToCNameString, currentSslIssuedToONameString, currentSslIssuedToUNameString, currentSslIssuedByCNameString, currentSslIssuedByONameString,
133                         currentSslIssuedByUNameString, currentSslStartDateLong, currentSslEndDateLong);
134
135                 // Update the pinned SSL certificate global variables to match the information that is now in the database.
136                 MainWebViewActivity.pinnedDomainSslIssuedToCNameString = currentSslIssuedToCNameString;
137                 MainWebViewActivity.pinnedDomainSslIssuedToONameString = currentSslIssuedToONameString;
138                 MainWebViewActivity.pinnedDomainSslIssuedToUNameString = currentSslIssuedToUNameString;
139                 MainWebViewActivity.pinnedDomainSslIssuedByCNameString = currentSslIssuedByCNameString;
140                 MainWebViewActivity.pinnedDomainSslIssuedByONameString = currentSslIssuedByONameString;
141                 MainWebViewActivity.pinnedDomainSslIssuedByUNameString = currentSslIssuedByUNameString;
142                 MainWebViewActivity.pinnedDomainSslStartDate = currentSslStartDate;
143                 MainWebViewActivity.pinnedDomainSslEndDate = currentSslEndDate;
144             }
145         });
146
147         // Setup the negative button.
148         dialogBuilder.setNegativeButton(R.string.back, new DialogInterface.OnClickListener() {
149             @Override
150             public void onClick(DialogInterface dialog, int which) {
151                 // Call the `onSslMismatchBack` public interface to send the `WebView` back one page.
152                 pinnedSslCertificateMismatchListener.onSslMismatchBack();
153             }
154         });
155
156         // Setup the positive button.
157         dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
158             @Override
159             public void onClick(DialogInterface dialog, int which) {
160                 // Call the `onSslMismatchProceed` public interface.
161                 pinnedSslCertificateMismatchListener.onSslMismatchProceed();
162             }
163         });
164
165         // Set the title.
166         dialogBuilder.setTitle(R.string.ssl_certificate_mismatch);
167
168         // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
169         dialogBuilder.setView(layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_linearlayout, null));
170
171         // Create an `AlertDialog` from the `AlertDialog.Builder`
172         final AlertDialog alertDialog = dialogBuilder.create();
173
174         // Show the `AlertDialog` so the items in the layout can be modified.
175         alertDialog.show();
176
177         //  Setup `wrapVerticalContentViewPager`.
178         WrapVerticalContentViewPager wrapVerticalContentViewPager = (WrapVerticalContentViewPager) alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_viewpager);
179         wrapVerticalContentViewPager.setAdapter(new pagerAdapter());
180
181         // Setup the `TabLayout` and connect it to the `WrapVerticalContentViewPager`.
182         TabLayout tabLayout = (TabLayout) alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_tablayout);
183         tabLayout.setupWithViewPager(wrapVerticalContentViewPager);
184
185         // `onCreateDialog` requires the return of an `AlertDialog`.
186         return alertDialog;
187     }
188
189     private class pagerAdapter extends PagerAdapter {
190         @Override
191         public boolean isViewFromObject(View view, Object object) {
192             // Check to see if the `View` and the `Object` are the same.
193             return (view == object);
194         }
195
196         @Override
197         public int getCount() {
198             // There are two tabs.
199             return 2;
200         }
201
202         @Override
203         public CharSequence getPageTitle(int position) {
204             // Return the current tab title.
205             if (position == 0) {  // The current SSL certificate tab.
206                 return getString(R.string.current_ssl);
207             } else {  // The pinned SSL certificate tab.
208                 return getString(R.string.pinned_ssl);
209             }
210         }
211
212         @Override
213         public Object instantiateItem(ViewGroup container, int position) {
214             // Inflate the `ScrollView` for this tab.
215             ViewGroup tabViewGroup = (ViewGroup) layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_scrollview, container, false);
216
217             // Get handles for the `TextViews`.
218             TextView issuedToCNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_cname);
219             TextView issuedToONameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_oname);
220             TextView issuedToUNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_to_uname);
221             TextView issuedByCNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_cname);
222             TextView issuedByONameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_oname);
223             TextView issuedByUNameTextView = (TextView) tabViewGroup.findViewById(R.id.issued_by_uname);
224             TextView startDateTextView = (TextView) tabViewGroup.findViewById(R.id.start_date);
225             TextView endDateTextView = (TextView) tabViewGroup.findViewById(R.id.end_date);
226
227             // Setup the labels.
228             String cNameLabel = getString(R.string.common_name) + "  ";
229             String oNameLabel = getString(R.string.organization) + "  ";
230             String uNameLabel = getString(R.string.organizational_unit) + "  ";
231             String startDateLabel = getString(R.string.start_date) + "  ";
232             String endDateLabel = getString(R.string.end_date) + "  ";
233
234             // Get the current website SSL certificate.
235             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
236
237             // Extract the individual pieces of information from the current website SSL certificate if it is not null.
238             if (sslCertificate != null) {
239                 currentSslIssuedToCNameString = sslCertificate.getIssuedTo().getCName();
240                 currentSslIssuedToONameString = sslCertificate.getIssuedTo().getOName();
241                 currentSslIssuedToUNameString = sslCertificate.getIssuedTo().getUName();
242                 currentSslIssuedByCNameString = sslCertificate.getIssuedBy().getCName();
243                 currentSslIssuedByONameString = sslCertificate.getIssuedBy().getOName();
244                 currentSslIssuedByUNameString = sslCertificate.getIssuedBy().getUName();
245                 currentSslStartDate = sslCertificate.getValidNotBeforeDate();
246                 currentSslEndDate = sslCertificate.getValidNotAfterDate();
247             } else {
248                 // Initialize the current website SSL certificate variables with blank information.
249                 currentSslIssuedToCNameString = "";
250                 currentSslIssuedToONameString = "";
251                 currentSslIssuedToUNameString = "";
252                 currentSslIssuedByCNameString = "";
253                 currentSslIssuedByONameString = "";
254                 currentSslIssuedByUNameString = "";
255             }
256
257             // Initialize the `SpannableStringBuilders`.
258             SpannableStringBuilder issuedToCNameStringBuilder;
259             SpannableStringBuilder issuedToONameStringBuilder;
260             SpannableStringBuilder issuedToUNameStringBuilder;
261             SpannableStringBuilder issuedByCNameStringBuilder;
262             SpannableStringBuilder issuedByONameStringBuilder;
263             SpannableStringBuilder issuedByUNameStringBuilder;
264             SpannableStringBuilder startDateStringBuilder;
265             SpannableStringBuilder endDateStringBuilder;
266
267             // Setup the `SpannableStringBuilders` for each tab.
268             if (position == 0) {  // Setup the current SSL certificate tab.
269                 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedToCNameString);
270                 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedToONameString);
271                 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedToUNameString);
272                 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedByCNameString);
273                 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedByONameString);
274                 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedByUNameString);
275
276                 // Set the dates if they aren't `null`.
277                 if (currentSslStartDate == null) {
278                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
279                 } else {
280                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslStartDate));
281                 }
282
283                 if (currentSslEndDate == null) {
284                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
285                 } else {
286                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslEndDate));
287                 }
288             } else {  // Setup the pinned SSL certificate tab.
289                 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToCNameString);
290                 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToONameString);
291                 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToUNameString);
292                 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByCNameString);
293                 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByONameString);
294                 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByUNameString);
295
296                 // Set the dates if they aren't `null`.
297                 if (MainWebViewActivity.pinnedDomainSslStartDate == null) {
298                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
299                 } else {
300                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(MainWebViewActivity.pinnedDomainSslStartDate));
301                 }
302
303                 if (MainWebViewActivity.pinnedDomainSslEndDate == null) {
304                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
305                 } else {
306                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(MainWebViewActivity.pinnedDomainSslEndDate));
307                 }
308             }
309
310             // Create a red `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
311             @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
312
313             // Create a blue `ForegroundColorSpan`.
314             ForegroundColorSpan blueColorSpan;
315
316             // Set `blueColorSpan` according to the theme.  We have to use the deprecated `getColor()` until API >= 23.
317             if (MainWebViewActivity.darkTheme) {
318                 //noinspection deprecation
319                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
320             } else {
321                 //noinspection deprecation
322                 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
323             }
324
325             // Configure the spans to display conflicting information in red.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
326             if (currentSslIssuedToCNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToCNameString)) {
327                 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
328             } else {
329                 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
330             }
331
332             if (currentSslIssuedToONameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToONameString)) {
333                 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
334             } else {
335                 issuedToONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
336             }
337
338             if (currentSslIssuedToUNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedToUNameString)) {
339                 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
340             } else {
341                 issuedToUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
342             }
343
344             if (currentSslIssuedByCNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByCNameString)) {
345                 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
346             } else {
347                 issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
348             }
349
350             if (currentSslIssuedByONameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByONameString)) {
351                 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
352             } else {
353                 issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
354             }
355
356             if (currentSslIssuedByUNameString.equals(MainWebViewActivity.pinnedDomainSslIssuedByUNameString)) {
357                 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
358             } else {
359                 issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
360             }
361
362             if ((currentSslStartDate != null) && (MainWebViewActivity.pinnedDomainSslStartDate != null) && currentSslStartDate.equals(MainWebViewActivity.pinnedDomainSslStartDate)) {
363                 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
364             } else {
365                 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
366             }
367
368             if ((currentSslEndDate != null) && (MainWebViewActivity.pinnedDomainSslEndDate != null) && currentSslEndDate.equals(MainWebViewActivity.pinnedDomainSslEndDate)) {
369                 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
370             } else {
371                 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
372             }
373
374             // Display the strings.
375             issuedToCNameTextView.setText(issuedToCNameStringBuilder);
376             issuedToONameTextView.setText(issuedToONameStringBuilder);
377             issuedToUNameTextView.setText(issuedToUNameStringBuilder);
378             issuedByCNameTextView.setText(issuedByCNameStringBuilder);
379             issuedByONameTextView.setText(issuedByONameStringBuilder);
380             issuedByUNameTextView.setText(issuedByUNameStringBuilder);
381             startDateTextView.setText(startDateStringBuilder);
382             endDateTextView.setText(endDateStringBuilder);
383
384             // Display the tab.
385             container.addView(tabViewGroup);
386
387             // Make it so.
388             return tabViewGroup;
389         }
390     }
391 }