]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/CheckPinnedMismatchHelper.java
Make pinned settings tab aware.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / CheckPinnedMismatchHelper.java
1 /*
2  * Copyright © 2018-2019 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.net.http.SslCertificate;
23
24 import androidx.fragment.app.DialogFragment;
25 import androidx.fragment.app.FragmentManager;
26
27 import com.stoutner.privacybrowser.dialogs.PinnedMismatchDialog;
28 import com.stoutner.privacybrowser.views.NestedScrollWebView;
29
30 import java.util.ArrayList;
31 import java.util.Date;
32
33 public class CheckPinnedMismatchHelper {
34     public static void checkPinnedMismatch(FragmentManager fragmentManager, NestedScrollWebView nestedScrollWebView) {
35         // Initialize the current SSL certificate variables.
36         String currentWebsiteIssuedToCName = "";
37         String currentWebsiteIssuedToOName = "";
38         String currentWebsiteIssuedToUName = "";
39         String currentWebsiteIssuedByCName = "";
40         String currentWebsiteIssuedByOName = "";
41         String currentWebsiteIssuedByUName = "";
42         Date currentWebsiteSslStartDate = null;
43         Date currentWebsiteSslEndDate = null;
44
45         // Initialize the pinned SSL certificate variables.
46         String pinnedSslIssuedToCName = "";
47         String pinnedSslIssuedToOName = "";
48         String pinnedSslIssuedToUName = "";
49         String pinnedSslIssuedByCName = "";
50         String pinnedSslIssuedByOName = "";
51         String pinnedSslIssuedByUName = "";
52         Date pinnedSslStartDate = null;
53         Date pinnedSslEndDate = null;
54
55         // Get the current website SSL certificate.
56         SslCertificate currentWebsiteSslCertificate = nestedScrollWebView.getCertificate();
57
58         // Extract the individual pieces of information from the current website SSL certificate if it is not null.
59         if (currentWebsiteSslCertificate != null) {
60             currentWebsiteIssuedToCName = currentWebsiteSslCertificate.getIssuedTo().getCName();
61             currentWebsiteIssuedToOName = currentWebsiteSslCertificate.getIssuedTo().getOName();
62             currentWebsiteIssuedToUName = currentWebsiteSslCertificate.getIssuedTo().getUName();
63             currentWebsiteIssuedByCName = currentWebsiteSslCertificate.getIssuedBy().getCName();
64             currentWebsiteIssuedByOName = currentWebsiteSslCertificate.getIssuedBy().getOName();
65             currentWebsiteIssuedByUName = currentWebsiteSslCertificate.getIssuedBy().getUName();
66             currentWebsiteSslStartDate = currentWebsiteSslCertificate.getValidNotBeforeDate();
67             currentWebsiteSslEndDate = currentWebsiteSslCertificate.getValidNotAfterDate();
68         }
69
70         // Get the pinned SSL certificate information if it exists.
71         if (nestedScrollWebView.hasPinnedSslCertificate()) {
72             // Get the pinned SSL certificate.
73             ArrayList<Object> pinnedSslCertificateArrayList = nestedScrollWebView.getPinnedSslCertificate();
74
75             // Extract the arrays from the array list.
76             String[] pinnedSslCertificateStringArray = (String[]) pinnedSslCertificateArrayList.get(0);
77             Date[] pinnedSslCertificateDateArray = (Date[]) pinnedSslCertificateArrayList.get(1);
78
79             // Populate the pinned SSL certificate string variables.
80             pinnedSslIssuedToCName = pinnedSslCertificateStringArray[0];
81             pinnedSslIssuedToOName = pinnedSslCertificateStringArray[1];
82             pinnedSslIssuedToUName = pinnedSslCertificateStringArray[2];
83             pinnedSslIssuedByCName = pinnedSslCertificateStringArray[3];
84             pinnedSslIssuedByOName = pinnedSslCertificateStringArray[4];
85             pinnedSslIssuedByUName = pinnedSslCertificateStringArray[5];
86
87             // Populate the pinned SSL certificate date variables.
88             pinnedSslStartDate = pinnedSslCertificateDateArray[0];
89             pinnedSslEndDate = pinnedSslCertificateDateArray[1];
90         }
91
92         // 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.
93         String currentWebsiteSslStartDateString = "";
94         String currentWebsiteSslEndDateString = "";
95         String pinnedSslStartDateString = "";
96         String pinnedSslEndDateString = "";
97
98         // Convert the dates to strings if they are not null.
99         if (currentWebsiteSslStartDate != null) {
100             currentWebsiteSslStartDateString = currentWebsiteSslStartDate.toString();
101         }
102
103         if (currentWebsiteSslEndDate != null) {
104             currentWebsiteSslEndDateString = currentWebsiteSslEndDate.toString();
105         }
106
107         if (pinnedSslStartDate != null) {
108             pinnedSslStartDateString = pinnedSslStartDate.toString();
109         }
110
111         if (pinnedSslEndDate != null) {
112             pinnedSslEndDateString = pinnedSslEndDate.toString();
113         }
114
115         // Check to see if the pinned information matches the current information.
116         if ((nestedScrollWebView.hasPinnedIpAddresses() && !nestedScrollWebView.getCurrentIpAddresses().equals(nestedScrollWebView.getPinnedIpAddresses())) ||
117                 (nestedScrollWebView.hasPinnedSslCertificate() && (!currentWebsiteIssuedToCName.equals(pinnedSslIssuedToCName) ||
118                 !currentWebsiteIssuedToOName.equals(pinnedSslIssuedToOName) || !currentWebsiteIssuedToUName.equals(pinnedSslIssuedToUName) ||
119                 !currentWebsiteIssuedByCName.equals(pinnedSslIssuedByCName) || !currentWebsiteIssuedByOName.equals(pinnedSslIssuedByOName) ||
120                 !currentWebsiteIssuedByUName.equals(pinnedSslIssuedByUName) || !currentWebsiteSslStartDateString.equals(pinnedSslStartDateString) ||
121                 !currentWebsiteSslEndDateString.equals(pinnedSslEndDateString)))) {
122
123             // Get a handle for the pinned mismatch alert dialog.
124             DialogFragment pinnedMismatchDialogFragment = PinnedMismatchDialog.displayDialog(nestedScrollWebView.getWebViewFragmentId());
125
126             // Show the pinned mismatch alert dialog.
127             pinnedMismatchDialogFragment.show(fragmentManager, "Pinned Mismatch");
128         }
129     }
130 }