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