2 * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs;
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.net.http.SslError;
29 import android.os.Bundle;
30 import android.support.annotation.NonNull;
31 // `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
32 import android.support.v7.app.AppCompatDialogFragment;
33 import android.text.SpannableStringBuilder;
34 import android.text.Spanned;
35 import android.text.style.ForegroundColorSpan;
36 import android.view.LayoutInflater;
37 import android.view.WindowManager;
38 import android.widget.TextView;
40 import com.stoutner.privacybrowser.R;
41 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
43 import java.text.DateFormat;
44 import java.util.Date;
46 public class SslCertificateErrorDialog extends AppCompatDialogFragment {
48 // The private variables are used in `onCreate()` and `onCreateDialog()`.
49 private int primaryErrorInt;
50 private String urlWithError;
51 private String issuedToCName;
52 private String issuedToOName;
53 private String issuedToUName;
54 private String issuedByCName;
55 private String issuedByOName;
56 private String issuedByUName;
57 private String startDate;
58 private String endDate;
60 public static SslCertificateErrorDialog displayDialog(SslError error) {
61 // Get the various components of the SSL error message.
62 int primaryErrorIntForBundle = error.getPrimaryError();
63 String urlWithErrorForBundle = error.getUrl();
64 SslCertificate sslCertificate = error.getCertificate();
65 String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
66 String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
67 String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
68 String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
69 String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
70 String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
71 Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
72 Date endDateForBundle = sslCertificate.getValidNotAfterDate();
74 // Store the SSL error message components in a `Bundle`.
75 Bundle argumentsBundle = new Bundle();
76 argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
77 argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
78 argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
79 argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
80 argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
81 argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
82 argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
83 argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
84 argumentsBundle.putString("StartDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDateForBundle));
85 argumentsBundle.putString("EndDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDateForBundle));
87 // Add `argumentsBundle` to this instance of `SslCertificateErrorDialog`.
88 SslCertificateErrorDialog thisSslCertificateErrorDialog = new SslCertificateErrorDialog();
89 thisSslCertificateErrorDialog.setArguments(argumentsBundle);
90 return thisSslCertificateErrorDialog;
94 public void onCreate(Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
97 // Remove the incorrect lint warning that `getArguments()` might be null.
98 assert getArguments() != null;
100 // Save the components of the SSL error message in class variables.
101 primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
102 urlWithError = getArguments().getString("UrlWithError");
103 issuedToCName = getArguments().getString("IssuedToCName");
104 issuedToOName = getArguments().getString("IssuedToOName");
105 issuedToUName = getArguments().getString("IssuedToUName");
106 issuedByCName = getArguments().getString("IssuedByCName");
107 issuedByOName = getArguments().getString("IssuedByOName");
108 issuedByUName = getArguments().getString("IssuedByUName");
109 startDate = getArguments().getString("StartDate");
110 endDate = getArguments().getString("EndDate");
113 // The public interface is used to send information back to the parent activity.
114 public interface SslCertificateErrorListener {
115 void onSslErrorCancel();
117 void onSslErrorProceed();
120 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
121 private SslCertificateErrorListener sslCertificateErrorListener;
123 // Check to make sure that the parent activity implements the listener.
124 public void onAttach(Context context) {
125 super.onAttach(context);
128 sslCertificateErrorListener = (SslCertificateErrorListener) context;
129 } catch(ClassCastException exception) {
130 throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
134 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
135 @SuppressLint("InflateParams")
136 @SuppressWarnings("deprecation")
139 public Dialog onCreateDialog(Bundle savedInstanceState) {
140 // Remove the incorrect lint warning that `getActivity()` might be null.
141 assert getActivity() != null;
143 // Get the activity's layout inflater.
144 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
146 // Use an alert dialog builder to create the alert dialog.
147 AlertDialog.Builder dialogBuilder;
149 // Set the style and icon according to the theme.
150 if (MainWebViewActivity.darkTheme) {
152 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
155 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_dark);
158 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
161 dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
165 dialogBuilder.setTitle(R.string.ssl_certificate_error);
167 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
168 dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
170 // Set a listener on the negative button.
171 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> sslCertificateErrorListener.onSslErrorCancel());
173 // Set a listener on the positive button.
174 dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> sslCertificateErrorListener.onSslErrorProceed());
177 // Create an alert dialog from the alert dialog builder.
178 AlertDialog alertDialog = dialogBuilder.create();
180 // Disable screenshots if not allowed.
181 if (!MainWebViewActivity.allowScreenshots) {
182 // Remove the warning below that `getWindow()` might be null.
183 assert alertDialog.getWindow() != null;
185 // Disable screenshots.
186 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
189 // We have to show the alert dialog before we can modify the content.
192 // Get handles for the `TextViews`
193 TextView primaryErrorTextView = alertDialog.findViewById(R.id.primary_error);
194 TextView urlTextView = alertDialog.findViewById(R.id.url_error_dialog);
195 TextView issuedToCNameTextView = alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
196 TextView issuedToONameTextView = alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
197 TextView issuedToUNameTextView = alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
198 TextView issuedByTextView = alertDialog.findViewById(R.id.issued_by_textview);
199 TextView issuedByCNameTextView = alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
200 TextView issuedByONameTextView = alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
201 TextView issuedByUNameTextView = alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
202 TextView validDatesTextView = alertDialog.findViewById(R.id.valid_dates_textview);
203 TextView startDateTextView = alertDialog.findViewById(R.id.start_date_error_dialog);
204 TextView endDateTextView = alertDialog.findViewById(R.id.end_date_error_dialog);
206 // Setup the common strings.
207 String urlLabel = getString(R.string.url_label) + " ";
208 String cNameLabel = getString(R.string.common_name) + " ";
209 String oNameLabel = getString(R.string.organization) + " ";
210 String uNameLabel = getString(R.string.organizational_unit) + " ";
211 String startDateLabel = getString(R.string.start_date) + " ";
212 String endDateLabel = getString(R.string.end_date) + " ";
214 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
215 SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
216 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
217 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
218 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
219 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
220 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
221 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
222 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
223 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
225 // Create a red `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
226 @SuppressWarnings("deprecation") ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
228 // Create a blue `ForegroundColorSpan`.
229 ForegroundColorSpan blueColorSpan;
231 // Set `blueColorSpan` according to the theme. We have to use the deprecated `getColor()` until API >= 23.
232 if (MainWebViewActivity.darkTheme) {
233 //noinspection deprecation
234 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
236 //noinspection deprecation
237 blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
240 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
241 urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
242 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
243 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
244 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
245 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
246 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
247 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
248 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
249 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
251 // Initialize `primaryErrorString`.
252 String primaryErrorString = "";
254 // Highlight the primary error in red and store the primary error string in `primaryErrorString`.
255 switch (primaryErrorInt) {
256 case SslError.SSL_IDMISMATCH:
257 // Change the URL span colors to red.
258 urlStringBuilder.setSpan(redColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
259 issuedToCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
261 // Store the primary error string.
262 primaryErrorString = getString(R.string.cn_mismatch);
265 case SslError.SSL_UNTRUSTED:
266 // Change the `issuesByTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
267 issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
269 // Change the issued by span color to red.
270 issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
271 issuedByONameStringBuilder.setSpan(redColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
272 issuedByUNameStringBuilder.setSpan(redColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
274 // Store the primary error string.
275 primaryErrorString = getString(R.string.untrusted);
278 case SslError.SSL_DATE_INVALID:
279 // Change the `validDatesTextView` text to red. We have to use the deprecated `getColor()` until API >= 23.
280 validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
282 // Change the date span colors to red.
283 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
284 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
286 // Store the primary error string.
287 primaryErrorString = getString(R.string.invalid_date);
290 case SslError.SSL_NOTYETVALID:
291 // Change the start date span color to red.
292 startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
294 // Store the primary error string.
295 primaryErrorString = getString(R.string.future_certificate);
298 case SslError.SSL_EXPIRED:
299 // Change the end date span color to red.
300 endDateStringBuilder.setSpan(redColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
302 // Store the primary error string.
303 primaryErrorString = getString(R.string.expired_certificate);
306 case SslError.SSL_INVALID:
307 // Store the primary error string.
308 primaryErrorString = getString(R.string.invalid_certificate);
313 // Display the strings.
314 primaryErrorTextView.setText(primaryErrorString);
315 urlTextView.setText(urlStringBuilder);
316 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
317 issuedToONameTextView.setText(issuedToONameStringBuilder);
318 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
319 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
320 issuedByONameTextView.setText(issuedByONameStringBuilder);
321 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
322 startDateTextView.setText(startDateStringBuilder);
323 endDateTextView.setText(endDateStringBuilder);
325 // `onCreateDialog` requires the return of an `AlertDialog`.