45702f628d61ba73218e3bfa2140f215648d0899
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / SslCertificateError.java
1 /**
2  * Copyright 2015-2016 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;
21
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.app.DialogFragment;
26 import android.content.DialogInterface;
27 import android.graphics.drawable.BitmapDrawable;
28 import android.graphics.drawable.Drawable;
29 import android.net.http.SslCertificate;
30 import android.net.http.SslError;
31 import android.os.Bundle;
32 import android.support.v4.content.ContextCompat;
33 import android.text.SpannableString;
34 import android.text.SpannableStringBuilder;
35 import android.text.Spanned;
36 import android.text.style.ForegroundColorSpan;
37 import android.view.LayoutInflater;
38 import android.widget.TextView;
39
40 import org.w3c.dom.Text;
41
42 import java.util.Date;
43
44 public class SslCertificateError extends DialogFragment{
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;
55
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();
69
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", startDateForBundle.toString());
81         argumentsBundle.putString("EndDate", endDateForBundle.toString());
82
83         // Add the `Bundle` to this instance of `SslCertificateError`.
84         SslCertificateError thisSslCertificateErrorDialog = new SslCertificateError();
85         thisSslCertificateErrorDialog.setArguments(argumentsBundle);
86         return thisSslCertificateErrorDialog;
87     }
88
89     public void onCreate(Bundle savedInstanceState) {
90         super.onCreate(savedInstanceState);
91
92         // Save the components of the SSL error message in class variables.
93         urlWithError = getArguments().getString("UrlWithError");
94         issuedToCName = getArguments().getString("IssuedToCName");
95         issuedToOName = getArguments().getString("IssuedToOName");
96         issuedToUName = getArguments().getString("IssuedToUName");
97         issuedByCName = getArguments().getString("IssuedByCName");
98         issuedByOName = getArguments().getString("IssuedByOName");
99         issuedByUName = getArguments().getString("IssuedByUName");
100         startDate = getArguments().getString("StartDate");
101         endDate = getArguments().getString("EndDate");
102
103         // Get the appropriate string for `primaryError.
104         int primaryErrorInt = getArguments().getInt("PrimaryErrorInt");
105         switch (primaryErrorInt) {
106             case SslError.SSL_NOTYETVALID:
107                 primaryError = getString(R.string.future_certificate);
108                 break;
109
110             case SslError.SSL_EXPIRED:
111                 primaryError = getString(R.string.expired_certificate);
112                 break;
113
114             case SslError.SSL_IDMISMATCH:
115                 primaryError = getString(R.string.cn_mismatch);
116                 break;
117
118             case SslError.SSL_UNTRUSTED:
119                 primaryError = getString(R.string.untrusted);
120                 break;
121
122             case SslError.SSL_DATE_INVALID:
123                 primaryError = getString(R.string.invalid_date);
124                 break;
125
126             case SslError.SSL_INVALID:
127                 primaryError = getString(R.string.invalid_certificate);
128                 break;
129         }
130     }
131
132     // The public interface is used to send information back to the parent activity.
133     public interface SslCertificateErrorListener {
134         void onSslErrorCancel();
135
136         void onSslErrorProceed();
137     }
138
139     // `sslCertificateErrorListener` is used in `onAttach` and `onCreateDialog`.
140     private SslCertificateErrorListener sslCertificateErrorListener;
141
142     // Check to make sure that the parent activity implements the listener.
143     public void onAttach(Activity parentActivity) {
144         super.onAttach(parentActivity);
145
146         try {
147             sslCertificateErrorListener = (SslCertificateErrorListener) parentActivity;
148         } catch(ClassCastException exception) {
149             throw new ClassCastException(parentActivity.toString() + " must implement SslCertificateErrorListener");
150         }
151     }
152
153     @Override
154     public Dialog onCreateDialog(Bundle savedInstanceState) {
155         // Get the activity's layout inflater.
156         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
157
158         // Use `AlertDialog.Builder` to create the `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
159         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog);
160         dialogBuilder.setTitle(R.string.ssl_certificate_error);
161         // The parent view is `null` because it will be assigned by `AlertDialog`.
162         dialogBuilder.setView(layoutInflater.inflate(R.layout.ssl_certificate_error, null));
163
164         // Set an `onClick` listener on the negative button.  `null` doesn't do anything extra when the button is pressed.  The `Dialog` will automatically close.
165         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
166             @Override
167             public void onClick(DialogInterface dialog, int which) {
168                 sslCertificateErrorListener.onSslErrorCancel();
169             }
170         });
171
172         // Set an `onClick` listener on the positive button.
173         dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
174             @Override
175             public void onClick(DialogInterface dialog, int which) {
176                 sslCertificateErrorListener.onSslErrorProceed();
177             }
178         });
179
180
181         // Create an `AlertDialog` from the `AlertDialog.Builder`.
182         AlertDialog alertDialog = dialogBuilder.create();
183
184         // We have to show the `AlertDialog` before we can modify the content.
185         alertDialog.show();
186
187         // Get handles for the `TextViews`
188         TextView primaryErrorTextView = (TextView) alertDialog.findViewById(R.id.primary_error);
189         TextView urlTextView = (TextView) alertDialog.findViewById(R.id.url_error_dialog);
190         TextView issuedToCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_cname_error_dialog);
191         TextView issuedToONameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_oname_error_dialog);
192         TextView issuedToUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_to_uname_error_dialog);
193         TextView issuedByCNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_cname_error_dialog);
194         TextView issuedByONameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_oname_error_dialog);
195         TextView issuedByUNameTextView = (TextView) alertDialog.findViewById(R.id.issued_by_uname_error_dialog);
196         TextView startDateTextView = (TextView) alertDialog.findViewById(R.id.start_date_error_dialog);
197         TextView endDateTextView = (TextView) alertDialog.findViewById(R.id.end_date_error_dialog);
198
199         // Setup the common strings.
200         String urlLabel = getString(R.string.url_label) + "  ";
201         String cNameLabel = getString(R.string.common_name) + "  ";
202         String oNameLabel = getString(R.string.organization) + "  ";
203         String uNameLabel = getString(R.string.organizational_unit) + "  ";
204         String startDateLabel = getString(R.string.start_date) + "  ";
205         String endDateLabel = getString(R.string.end_date) + "  ";
206
207         // Create a `SpannableStringBuilder` for each `TextView` that needs multiple colors of text.
208         SpannableStringBuilder urlStringBuilder = new SpannableStringBuilder(urlLabel + urlWithError);
209         SpannableStringBuilder issuedToCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedToCName);
210         SpannableStringBuilder issuedToONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedToOName);
211         SpannableStringBuilder issuedToUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedToUName);
212         SpannableStringBuilder issuedByCNameStringBuilder = new SpannableStringBuilder(cNameLabel + issuedByCName);
213         SpannableStringBuilder issuedByONameStringBuilder = new SpannableStringBuilder(oNameLabel + issuedByOName);
214         SpannableStringBuilder issuedByUNameStringBuilder = new SpannableStringBuilder(uNameLabel + issuedByUName);
215         SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
216         SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
217
218         // Create a blue `ForegroundColorSpan`.  We have to use the deprecated `getColor` until API >= 23.
219         ForegroundColorSpan blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue));
220
221         // Setup the spans to display the certificate information in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
222         urlStringBuilder.setSpan(blueColorSpan, urlLabel.length(), urlStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
223         issuedToCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedToCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
224         issuedToONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedToONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
225         issuedToUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedToUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
226         issuedByCNameStringBuilder.setSpan(blueColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
227         issuedByONameStringBuilder.setSpan(blueColorSpan, oNameLabel.length(), issuedByONameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
228         issuedByUNameStringBuilder.setSpan(blueColorSpan, uNameLabel.length(), issuedByUNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
229         startDateStringBuilder.setSpan(blueColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
230         endDateStringBuilder.setSpan(blueColorSpan, endDateLabel.length(), endDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
231
232
233         // Display the strings.
234         primaryErrorTextView.setText(primaryError);
235         urlTextView.setText(urlStringBuilder);
236         issuedToCNameTextView.setText(issuedToCNameStringBuilder);
237         issuedToONameTextView.setText(issuedToONameStringBuilder);
238         issuedToUNameTextView.setText(issuedToUNameStringBuilder);
239         issuedByCNameTextView.setText(issuedByCNameStringBuilder);
240         issuedByONameTextView.setText(issuedByONameStringBuilder);
241         issuedByUNameTextView.setText(issuedByUNameStringBuilder);
242         startDateTextView.setText(startDateStringBuilder);
243         endDateTextView.setText(endDateStringBuilder);
244
245         // `onCreateDialog` requires the return of an `AlertDialog`.
246         return alertDialog;
247     }
248 }