]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/PinnedSslCertificateMismatchDialog.java
Add a requests activity. https://redmine.stoutner.com/issues/170
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / PinnedSslCertificateMismatchDialog.java
1 /*
2  * Copyright © 2017-2018 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.view.WindowManager;
41 import android.widget.TextView;
42
43 import com.stoutner.privacybrowser.R;
44 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
45 import com.stoutner.privacybrowser.definitions.WrapVerticalContentViewPager;
46 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
47
48 import java.text.DateFormat;
49 import java.util.Date;
50
51 public class PinnedSslCertificateMismatchDialog extends AppCompatDialogFragment {
52     // Instantiate the class variables.
53     private PinnedSslCertificateMismatchListener pinnedSslCertificateMismatchListener;
54     private LayoutInflater layoutInflater;
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     // Check to make sure that the parent activity implements the listener.
72     public void onAttach(Context context) {
73         // Run the default commands.
74         super.onAttach(context);
75
76         // Get a handle for `PinnedSslCertificateMismatchListener` from the launching context.
77         pinnedSslCertificateMismatchListener = (PinnedSslCertificateMismatchListener) context;
78     }
79
80     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
81     @SuppressLint("InflateParams")
82     @Override
83     @NonNull
84     public Dialog onCreateDialog(Bundle savedInstanceState) {
85         // Remove the incorrect lint warning that `getActivity()` might be null.
86         assert getActivity() != null;
87
88         // Get the activity's layout inflater.
89         layoutInflater = getActivity().getLayoutInflater();
90
91         // Use an alert dialog builder to create the alert dialog.
92         AlertDialog.Builder dialogBuilder;
93
94         // Set the style according to the theme.
95         if (MainWebViewActivity.darkTheme) {
96             // Set the dialog theme.
97             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
98
99             // Set the icon.
100             dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
101         } else {
102             // Set the dialog theme.
103             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
104
105             // Set the icon.
106             dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
107         }
108
109         // Setup the neutral button.
110         dialogBuilder.setNeutralButton(R.string.update_ssl, (DialogInterface dialog, int which) -> {
111             // Initialize the `long` date variables.  If the date is `null`, a long value of `0` will be stored in the Domains database entry.
112             long currentSslStartDateLong = 0;
113             long currentSslEndDateLong = 0;
114
115             // Convert the `Dates` into `longs`.
116             if (currentSslStartDate != null) {
117                 currentSslStartDateLong = currentSslStartDate.getTime();
118             }
119
120             if (currentSslEndDate != null) {
121                 currentSslEndDateLong = currentSslEndDate.getTime();
122             }
123
124             // Initialize the database handler.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
125             DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
126
127             // Update the pinned SSL certificate for this domain.
128             domainsDatabaseHelper.updateCertificate(MainWebViewActivity.domainSettingsDatabaseId, currentSslIssuedToCNameString, currentSslIssuedToONameString, currentSslIssuedToUNameString,
129                     currentSslIssuedByCNameString, currentSslIssuedByONameString, currentSslIssuedByUNameString, currentSslStartDateLong, currentSslEndDateLong);
130
131             // Update the pinned SSL certificate global variables to match the information that is now in the database.
132             MainWebViewActivity.pinnedDomainSslIssuedToCNameString = currentSslIssuedToCNameString;
133             MainWebViewActivity.pinnedDomainSslIssuedToONameString = currentSslIssuedToONameString;
134             MainWebViewActivity.pinnedDomainSslIssuedToUNameString = currentSslIssuedToUNameString;
135             MainWebViewActivity.pinnedDomainSslIssuedByCNameString = currentSslIssuedByCNameString;
136             MainWebViewActivity.pinnedDomainSslIssuedByONameString = currentSslIssuedByONameString;
137             MainWebViewActivity.pinnedDomainSslIssuedByUNameString = currentSslIssuedByUNameString;
138             MainWebViewActivity.pinnedDomainSslStartDate = currentSslStartDate;
139             MainWebViewActivity.pinnedDomainSslEndDate = currentSslEndDate;
140         });
141
142         // Setup the negative button.
143         dialogBuilder.setNegativeButton(R.string.back, (DialogInterface dialog, int which) -> {
144             // Call the `onSslMismatchBack` public interface to send the `WebView` back one page.
145             pinnedSslCertificateMismatchListener.onSslMismatchBack();
146         });
147
148         // Setup the positive button.
149         dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> {
150             // Call the `onSslMismatchProceed` public interface.
151             pinnedSslCertificateMismatchListener.onSslMismatchProceed();
152         });
153
154         // Set the title.
155         dialogBuilder.setTitle(R.string.ssl_certificate_mismatch);
156
157         // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
158         dialogBuilder.setView(layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_linearlayout, null));
159
160         // Create an alert dialog from the alert dialog builder.
161         final AlertDialog alertDialog = dialogBuilder.create();
162
163         // Disable screenshots if not allowed.
164         if (!MainWebViewActivity.allowScreenshots) {
165             // Remove the warning below that `getWindow()` might be null.
166             assert alertDialog.getWindow() != null;
167
168             // Disable screenshots.
169             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
170         }
171
172         // Show the alert dialog so the items in the layout can be modified.
173         alertDialog.show();
174
175         //  Setup the view pager.
176         WrapVerticalContentViewPager wrapVerticalContentViewPager = alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_viewpager);
177         wrapVerticalContentViewPager.setAdapter(new pagerAdapter());
178
179         // Setup the tab layout and connect it to the view pager.
180         TabLayout tabLayout = alertDialog.findViewById(R.id.pinned_ssl_certificate_mismatch_tablayout);
181         tabLayout.setupWithViewPager(wrapVerticalContentViewPager);
182
183         // `onCreateDialog()` requires the return of an `AlertDialog`.
184         return alertDialog;
185     }
186
187     private class pagerAdapter extends PagerAdapter {
188         @Override
189         public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
190             // Check to see if the `View` and the `Object` are the same.
191             return (view == object);
192         }
193
194         @Override
195         public int getCount() {
196             // There are two tabs.
197             return 2;
198         }
199
200         @Override
201         public CharSequence getPageTitle(int position) {
202             // Return the current tab title.
203             if (position == 0) {  // The current SSL certificate tab.
204                 return getString(R.string.current_ssl);
205             } else {  // The pinned SSL certificate tab.
206                 return getString(R.string.pinned_ssl);
207             }
208         }
209
210         @Override
211         @NonNull
212         public Object instantiateItem(@NonNull ViewGroup container, int position) {
213             // Inflate the `ScrollView` for this tab.
214             ViewGroup tabViewGroup = (ViewGroup) layoutInflater.inflate(R.layout.pinned_ssl_certificate_mismatch_scrollview, container, false);
215
216             // Get handles for the `TextViews`.
217             TextView issuedToCNameTextView = tabViewGroup.findViewById(R.id.issued_to_cname);
218             TextView issuedToONameTextView = tabViewGroup.findViewById(R.id.issued_to_oname);
219             TextView issuedToUNameTextView = tabViewGroup.findViewById(R.id.issued_to_uname);
220             TextView issuedByCNameTextView = tabViewGroup.findViewById(R.id.issued_by_cname);
221             TextView issuedByONameTextView = tabViewGroup.findViewById(R.id.issued_by_oname);
222             TextView issuedByUNameTextView = tabViewGroup.findViewById(R.id.issued_by_uname);
223             TextView startDateTextView = tabViewGroup.findViewById(R.id.start_date);
224             TextView endDateTextView = tabViewGroup.findViewById(R.id.end_date);
225
226             // Setup the labels.
227             String cNameLabel = getString(R.string.common_name) + "  ";
228             String oNameLabel = getString(R.string.organization) + "  ";
229             String uNameLabel = getString(R.string.organizational_unit) + "  ";
230             String startDateLabel = getString(R.string.start_date) + "  ";
231             String endDateLabel = getString(R.string.end_date) + "  ";
232
233             // Get the current website SSL certificate.
234             SslCertificate sslCertificate = MainWebViewActivity.sslCertificate;
235
236             // Extract the individual pieces of information from the current website SSL certificate if it is not null.
237             if (sslCertificate != null) {
238                 currentSslIssuedToCNameString = sslCertificate.getIssuedTo().getCName();
239                 currentSslIssuedToONameString = sslCertificate.getIssuedTo().getOName();
240                 currentSslIssuedToUNameString = sslCertificate.getIssuedTo().getUName();
241                 currentSslIssuedByCNameString = sslCertificate.getIssuedBy().getCName();
242                 currentSslIssuedByONameString = sslCertificate.getIssuedBy().getOName();
243                 currentSslIssuedByUNameString = sslCertificate.getIssuedBy().getUName();
244                 currentSslStartDate = sslCertificate.getValidNotBeforeDate();
245                 currentSslEndDate = sslCertificate.getValidNotAfterDate();
246             } else {
247                 // Initialize the current website SSL certificate variables with blank information.
248                 currentSslIssuedToCNameString = "";
249                 currentSslIssuedToONameString = "";
250                 currentSslIssuedToUNameString = "";
251                 currentSslIssuedByCNameString = "";
252                 currentSslIssuedByONameString = "";
253                 currentSslIssuedByUNameString = "";
254             }
255
256             // Initialize the `SpannableStringBuilders`.
257             SpannableStringBuilder issuedToCNameStringBuilder;
258             SpannableStringBuilder issuedToONameStringBuilder;
259             SpannableStringBuilder issuedToUNameStringBuilder;
260             SpannableStringBuilder issuedByCNameStringBuilder;
261             SpannableStringBuilder issuedByONameStringBuilder;
262             SpannableStringBuilder issuedByUNameStringBuilder;
263             SpannableStringBuilder startDateStringBuilder;
264             SpannableStringBuilder endDateStringBuilder;
265
266             // Setup the `SpannableStringBuilders` for each tab.
267             if (position == 0) {  // Setup the current SSL certificate tab.
268                 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedToCNameString);
269                 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedToONameString);
270                 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedToUNameString);
271                 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + currentSslIssuedByCNameString);
272                 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + currentSslIssuedByONameString);
273                 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + currentSslIssuedByUNameString);
274
275                 // Set the dates if they aren't `null`.
276                 if (currentSslStartDate == null) {
277                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
278                 } else {
279                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslStartDate));
280                 }
281
282                 if (currentSslEndDate == null) {
283                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel);
284                 } else {
285                     endDateStringBuilder = new SpannableStringBuilder(endDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(currentSslEndDate));
286                 }
287             } else {  // Setup the pinned SSL certificate tab.
288                 issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToCNameString);
289                 issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToONameString);
290                 issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedToUNameString);
291                 issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByCNameString);
292                 issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByONameString);
293                 issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + MainWebViewActivity.pinnedDomainSslIssuedByUNameString);
294
295                 // Set the dates if they aren't `null`.
296                 if (MainWebViewActivity.pinnedDomainSslStartDate == null) {
297                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel);
298                 } else {
299                     startDateStringBuilder = new SpannableStringBuilder(startDateLabel + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG)
300                             .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 }