]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - privacybrowser/helpers/CheckPinnedMismatchHelper.java
Add an option to delete all domain settings at once. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / privacybrowser / helpers / CheckPinnedMismatchHelper.java
1 /*
2  * Copyright © 2018-2019,2021 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.helpers;
21
22 import android.app.Activity;
23 import android.net.http.SslCertificate;
24
25 import androidx.fragment.app.DialogFragment;
26 import androidx.fragment.app.FragmentManager;
27
28 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
29 import com.stoutner.privacybrowser.views.NestedScrollWebView;
30
31 import java.util.ArrayList;
32 import java.util.Date;
33
34 public class CheckPinnedMismatchHelper {
35     public static void checkPinnedMismatch(Activity activity, FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
36         // Initialize the current SSL certificate variables.
37         String currentWebsiteIssuedToCName = "";
38         String currentWebsiteIssuedToOName = "";
39         String currentWebsiteIssuedToUName = "";
40         String currentWebsiteIssuedByCName = "";
41         String currentWebsiteIssuedByOName = "";
42         String currentWebsiteIssuedByUName = "";
43         Date currentWebsiteSslStartDate = null;
44         Date currentWebsiteSslEndDate = null;
45
46         // Initialize the pinned SSL certificate variables.
47         String pinnedSslIssuedToCName = "";
48         String pinnedSslIssuedToOName = "";
49         String pinnedSslIssuedToUName = "";
50         String pinnedSslIssuedByCName = "";
51         String pinnedSslIssuedByOName = "";
52         String pinnedSslIssuedByUName = "";
53         Date pinnedSslStartDate = null;
54         Date pinnedSslEndDate = null;
55
56         // Get the current website SSL certificate.
57         SslCertificate currentWebsiteSslCertificate = nestedScrollWebView.getCertificate();
58
59         // Extract the individual pieces of information from the current website SSL certificate if it is not null.
60         if (currentWebsiteSslCertificate != null) {
61             currentWebsiteIssuedToCName = currentWebsiteSslCertificate.getIssuedTo().getCName();
62             currentWebsiteIssuedToOName = currentWebsiteSslCertificate.getIssuedTo().getOName();
63             currentWebsiteIssuedToUName = currentWebsiteSslCertificate.getIssuedTo().getUName();
64             currentWebsiteIssuedByCName = currentWebsiteSslCertificate.getIssuedBy().getCName();
65             currentWebsiteIssuedByOName = currentWebsiteSslCertificate.getIssuedBy().getOName();
66             currentWebsiteIssuedByUName = currentWebsiteSslCertificate.getIssuedBy().getUName();
67             currentWebsiteSslStartDate = currentWebsiteSslCertificate.getValidNotBeforeDate();
68             currentWebsiteSslEndDate = currentWebsiteSslCertificate.getValidNotAfterDate();
69         }
70
71         // Get the pinned SSL certificate information if it exists.
72         if (nestedScrollWebView.hasPinnedSslCertificate()) {
73             // Get the pinned SSL certificate.
74             ArrayList<Object> pinnedSslCertificateArrayList = nestedScrollWebView.getPinnedSslCertificate();
75
76             // Extract the arrays from the array list.
77             String[] pinnedSslCertificateStringArray = (String[]) pinnedSslCertificateArrayList.get(0);
78             Date[] pinnedSslCertificateDateArray = (Date[]) pinnedSslCertificateArrayList.get(1);
79
80             // Populate the pinned SSL certificate string variables.
81             pinnedSslIssuedToCName = pinnedSslCertificateStringArray[0];
82             pinnedSslIssuedToOName = pinnedSslCertificateStringArray[1];
83             pinnedSslIssuedToUName = pinnedSslCertificateStringArray[2];
84             pinnedSslIssuedByCName = pinnedSslCertificateStringArray[3];
85             pinnedSslIssuedByOName = pinnedSslCertificateStringArray[4];
86             pinnedSslIssuedByUName = pinnedSslCertificateStringArray[5];
87
88             // Populate the pinned SSL certificate date variables.
89             pinnedSslStartDate = pinnedSslCertificateDateArray[0];
90             pinnedSslEndDate = pinnedSslCertificateDateArray[1];
91         }
92
93         // Initialize string variables to store the SSL certificate dates.  Strings are needed to compare the values below, which doesn't work with dates if the first one is null.
94         String currentWebsiteSslStartDateString = "";
95         String currentWebsiteSslEndDateString = "";
96         String pinnedSslStartDateString = "";
97         String pinnedSslEndDateString = "";
98
99         // Convert the dates to strings if they are not null.
100         if (currentWebsiteSslStartDate != null) {
101             currentWebsiteSslStartDateString = currentWebsiteSslStartDate.toString();
102         }
103
104         if (currentWebsiteSslEndDate != null) {
105             currentWebsiteSslEndDateString = currentWebsiteSslEndDate.toString();
106         }
107
108         if (pinnedSslStartDate != null) {
109             pinnedSslStartDateString = pinnedSslStartDate.toString();
110         }
111
112         if (pinnedSslEndDate != null) {
113             pinnedSslEndDateString = pinnedSslEndDate.toString();
114         }
115
116         // Check to see if the pinned information matches the current information.
117         if ((nestedScrollWebView.hasPinnedIpAddresses() && !nestedScrollWebView.getCurrentIpAddresses().equals(nestedScrollWebView.getPinnedIpAddresses())) ||
118                 (nestedScrollWebView.hasPinnedSslCertificate() && (!currentWebsiteIssuedToCName.equals(pinnedSslIssuedToCName) ||
119                 !currentWebsiteIssuedToOName.equals(pinnedSslIssuedToOName) || !currentWebsiteIssuedToUName.equals(pinnedSslIssuedToUName) ||
120                 !currentWebsiteIssuedByCName.equals(pinnedSslIssuedByCName) || !currentWebsiteIssuedByOName.equals(pinnedSslIssuedByOName) ||
121                 !currentWebsiteIssuedByUName.equals(pinnedSslIssuedByUName) || !currentWebsiteSslStartDateString.equals(pinnedSslStartDateString) ||
122                 !currentWebsiteSslEndDateString.equals(pinnedSslEndDateString)))) {
123
124             // Prevent the dialog from displaying if the app window is not visible.
125             // The check pinned mismatch helper continues to function even when the app is paused.  Attempting to display a dialog in that state leads to a crash.
126             while (!activity.getWindow().isActive()) {
127                 try {
128                     // The window is not active.  Wait 1 second.
129                     activity.wait(1000);
130                 } catch (InterruptedException e) {
131                     // Do nothing.
132                 }
133             }
134
135             // Get a handle for the pinned mismatch alert dialog.
136             DialogFragment pinnedMismatchDialogFragment = PinnedMismatchDialog.displayDialog(nestedScrollWebView.getWebViewFragmentId());
137
138             // Show the pinned mismatch alert dialog.
139             pinnedMismatchDialogFragment.show(fragmentManager, "Pinned Mismatch");
140         }
141     }
142 }