2 * Copyright 2016-2017 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 import android.support.v7.app.AppCompatDialogFragment;
32 import android.text.SpannableStringBuilder;
33 import android.text.Spanned;
34 import android.text.style.ForegroundColorSpan;
35 import android.view.LayoutInflater;
36 import android.widget.TextView;
38 import com.stoutner.privacybrowser.R;
40 import java.text.DateFormat;
41 import java.util.Date;
43 public class SslCertificateError extends AppCompatDialogFragment {
45 private String primaryError;
46 private String urlWithError;
47 private String issuedToCName;
48 private String issuedToOName;
49 private String issuedToUName;
50 private String issuedByCName;
51 private String issuedByOName;
52 private String issuedByUName;
53 private String startDate;
54 private String endDate;
56 public static SslCertificateError displayDialog(SslError error) {
57 // Get the various components of the SSL error message.
58 int primaryErrorIntForBundle = error.getPrimaryError();
59 String urlWithErrorForBundle = error.getUrl();
60 SslCertificate sslCertificate = error.getCertificate();
61 String issuedToCNameForBundle = sslCertificate.getIssuedTo().getCName();
62 String issuedToONameForBundle = sslCertificate.getIssuedTo().getOName();
63 String issuedToUNameForBundle = sslCertificate.getIssuedTo().getUName();
64 String issuedByCNameForBundle = sslCertificate.getIssuedBy().getCName();
65 String issuedByONameForBundle = sslCertificate.getIssuedBy().getOName();
66 String issuedByUNameForBundle = sslCertificate.getIssuedBy().getUName();
67 Date startDateForBundle = sslCertificate.getValidNotBeforeDate();
68 Date endDateForBundle = sslCertificate.getValidNotAfterDate();
70 // Store the SSL error message components in a `Bundle`.
71 Bundle argumentsBundle = new Bundle();
72 argumentsBundle.putInt("PrimaryErrorInt", primaryErrorIntForBundle);
73 argumentsBundle.putString("UrlWithError", urlWithErrorForBundle);
74 argumentsBundle.putString("IssuedToCName", issuedToCNameForBundle);
75 argumentsBundle.putString("IssuedToOName", issuedToONameForBundle);
76 argumentsBundle.putString("IssuedToUName", issuedToUNameForBundle);
77 argumentsBundle.putString("IssuedByCName", issuedByCNameForBundle);
78 argumentsBundle.putString("IssuedByOName", issuedByONameForBundle);
79 argumentsBundle.putString("IssuedByUName", issuedByUNameForBundle);
80 argumentsBundle.putString("StartDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(startDateForBundle));
81 argumentsBundle.putString("EndDate", DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG).format(endDateForBundle));
83 // Add `argumentsBundle` to this instance of `SslCertificateError`.
84 SslCertificateError thisSslCertificateErrorDialog = new SslCertificateError();
85 thisSslCertificateErrorDialog.setArguments(argumentsBundle);
86 return thisSslCertificateErrorDialog;
90 public void onCreate(Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
93 // Save the components of the SSL error message in class variables.
94 urlWithError = getArguments().getString("UrlWithError");
95 issuedToCName = getArguments().getString("IssuedToCName");
96 issuedToOName = getArguments().getString("IssuedToOName");
97 issuedToUName = getArguments().getString("IssuedToUName");
98 issuedByCName = getArguments().getString("IssuedByCName");
99 issuedByOName = getArguments().getString("IssuedByOName");
100 issuedByUName = getArguments().getString("IssuedByUName");
101 startDate = getArguments().getString("StartDate");
102 endDate = getArguments().getString("EndDate");
104 // Get the appropriate string for `primaryError.
105 int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
106 switch (primaryErrorInt) {
107 case SslError.SSL_NOTYETVALID:
108 primaryError = getString(R.string.future_certificate);
111 case SslError.SSL_EXPIRED:
112 primaryError = getString(R.string.expired_certificate);
115 case SslError.SSL_IDMISMATCH:
116 primaryError = getString(R.string.cn_mismatch);
119 case SslError.SSL_UNTRUSTED:
120 primaryError = getString(R.string.untrusted);
123 case SslError.SSL_DATE_INVALID:
124 primaryError = getString(R.string.invalid_date);
127 case SslError.SSL_INVALID:
128 primaryError = getString(R.string.invalid_certificate);
133 // The public interface is used to send information back to the parent activity.
134 public interface SslCertificateErrorListener {
135 void onSslErrorCancel();
137 void onSslErrorProceed();
140 // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
141 private SslCertificateErrorListener sslCertificateErrorListener;
143 // Check to make sure that the parent activity implements the listener.
144 public void onAttach(Context context) {
145 super.onAttach(context);
148 sslCertificateErrorListener = (SslCertificateErrorListener) context;
149 } catch(ClassCastException exception) {
150 throw new ClassCastException(context.toString() + " must implement SslCertificateErrorListener");
154 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
155 @SuppressLint("InflateParams")
158 public Dialog onCreateDialog(Bundle savedInstanceState) {
159 // Get the activity's layout inflater.
160 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
162 // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.LightAlertDialog` formats the color of the button text.
163 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
164 dialogBuilder.setTitle(R.string.ssl_certificate_error);
165 // The parent view is `null` because it will be assigned by `AlertDialog`.
166 dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
168 // Set an `onClick` listener on the negative button. `null` doesn't do anything extra when the button is pressed. The `Dialog` will automatically close.
169 dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
171 public void onClick(DialogInterface dialog, int which) {
172 sslCertificateErrorListener.onSslErrorCancel();
176 // Set an `onClick` listener on the positive button.
177 dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
179 public void onClick(DialogInterface dialog, int which) {
180 sslCertificateErrorListener.onSslErrorProceed();
185 // Create an `AlertDialog` from the `AlertDialog.Builder`.
186 AlertDialog alertDialog = dialogBuilder.create();
188 // We have to show the `AlertDialog` before we can modify the content.
191 // Get handles for the `TextViews`
192 TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
193 TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
194 TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
195 TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
196 TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
197 TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
198 TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
199 TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
200 TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
201 TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
203 // Setup the common strings.
204 String urlLabel = getString(R.string.url_label) + " ";
205 String cNameLabel = getString(R.string.common_name) + " ";
206 String oNameLabel = getString(R.string.organization) + " ";
207 String uNameLabel = getString(R.string.organizational_unit) + " ";
208 String startDateLabel = getString(R.string.start_date) + " ";
209 String endDateLabel = getString(R.string.end_date) + " ";
211 // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
212 SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
213 SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
214 SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
215 SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
216 SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
217 SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
218 SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
219 SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
220 SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
222 // Create a blue `ForegroundColorSpan`. We have to use the deprecated `getColor` until API >= 23.
223 @SuppressWarnings("deprecation") ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
225 // Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
226 urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227 issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
228 issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229 issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
230 issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
231 issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
232 issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
233 startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
234 endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
237 // Display the strings.
238 primaryErrorTextView.setText(primaryError);
239 urlTextView.setText(urlStringBuilder);
240 issuedToCNameTextView.setText(issuedToCNameStringBuilder);
241 issuedToONameTextView.setText(issuedToONameStringBuilder);
242 issuedToUNameTextView.setText(issuedToUNameStringBuilder);
243 issuedByCNameTextView.setText(issuedByCNameStringBuilder);
244 issuedByONameTextView.setText(issuedByONameStringBuilder);
245 issuedByUNameTextView.setText(issuedByUNameStringBuilder);
246 startDateTextView.setText(startDateStringBuilder);
247 endDateTextView.setText(endDateStringBuilder);
249 // `onCreateDialog` requires the return of an `AlertDialog`.