OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.pageinfo; | |
6 | |
7 import android.app.Dialog; | |
8 import android.content.Context; | |
9 import android.graphics.Typeface; | |
10 import android.net.http.SslCertificate; | |
11 import android.util.Log; | |
12 import android.view.View; | |
13 import android.view.ViewGroup; | |
14 import android.view.Window; | |
15 import android.widget.AdapterView; | |
16 import android.widget.AdapterView.OnItemSelectedListener; | |
17 import android.widget.ArrayAdapter; | |
18 import android.widget.LinearLayout; | |
19 import android.widget.ScrollView; | |
20 import android.widget.Spinner; | |
21 import android.widget.TextView; | |
22 | |
23 import org.chromium.base.ApiCompatibilityUtils; | |
24 import org.chromium.chrome.R; | |
25 | |
26 import java.io.ByteArrayInputStream; | |
27 import java.security.MessageDigest; | |
28 import java.security.cert.Certificate; | |
29 import java.security.cert.CertificateException; | |
30 import java.security.cert.CertificateFactory; | |
31 import java.security.cert.X509Certificate; | |
32 import java.text.DateFormat; | |
33 import java.util.ArrayList; | |
34 | |
35 /** | |
36 * UI component for displaying certificate information. | |
37 */ | |
38 class CertificateViewer implements OnItemSelectedListener { | |
39 private static final String X_509 = "X.509"; | |
40 private final Context mContext; | |
41 private final ArrayList<LinearLayout> mViews; | |
42 private final ArrayList<String> mTitles; | |
43 private final int mPadding; | |
44 private CertificateFactory mCertificateFactory; | |
45 | |
46 /** | |
47 * Show a dialog with the provided certificate information. | |
48 * | |
49 * @param context The context this view should display in. | |
50 * @param derData DER-encoded data representing a X509 certificate chain. | |
51 */ | |
52 public static void showCertificateChain(Context context, byte[][] derData) { | |
53 CertificateViewer viewer = new CertificateViewer(context); | |
54 viewer.showCertificateChain(derData); | |
55 } | |
56 | |
57 private CertificateViewer(Context context) { | |
58 mContext = context; | |
59 mViews = new ArrayList<LinearLayout>(); | |
60 mTitles = new ArrayList<String>(); | |
61 mPadding = (int) context.getResources().getDimension( | |
62 R.dimen.connection_info_padding_wide) / 2; | |
63 } | |
64 | |
65 // Show information about an array of DER-encoded data representing a X509 c
ertificate chain. | |
66 // A spinner will be displayed allowing the user to select which certificate
to display. | |
67 private void showCertificateChain(byte[][] derData) { | |
68 for (int i = 0; i < derData.length; i++) { | |
69 addCertificate(derData[i]); | |
70 } | |
71 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, | |
72 android.R.layout.simple_spinner_item, | |
73 mTitles) { | |
74 @Override | |
75 public View getView(int position, View convertView, ViewGroup parent
) { | |
76 TextView view = (TextView) super.getView(position, convertView,
parent); | |
77 // Add extra padding on the end side to avoid overlapping the dr
opdown arrow. | |
78 ApiCompatibilityUtils.setPaddingRelative(view, mPadding, mPaddin
g, mPadding * 2, | |
79 mPadding); | |
80 return view; | |
81 } | |
82 }; | |
83 arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dro
pdown_item); | |
84 | |
85 LinearLayout dialogContainer = new LinearLayout(mContext); | |
86 dialogContainer.setOrientation(LinearLayout.VERTICAL); | |
87 | |
88 TextView title = new TextView(mContext); | |
89 title.setText(R.string.certtitle); | |
90 ApiCompatibilityUtils.setTextAlignment(title, View.TEXT_ALIGNMENT_VIEW_S
TART); | |
91 ApiCompatibilityUtils.setTextAppearance(title, android.R.style.TextAppea
rance_Large); | |
92 title.setTypeface(title.getTypeface(), Typeface.BOLD); | |
93 title.setPadding(mPadding, mPadding, mPadding, mPadding / 2); | |
94 dialogContainer.addView(title); | |
95 | |
96 Spinner spinner = new Spinner(mContext); | |
97 ApiCompatibilityUtils.setTextAlignment(spinner, View.TEXT_ALIGNMENT_VIEW
_START); | |
98 spinner.setAdapter(arrayAdapter); | |
99 spinner.setOnItemSelectedListener(this); | |
100 spinner.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); | |
101 // Remove padding so that dropdown has same width as the spinner. | |
102 spinner.setPadding(0, 0, 0, 0); | |
103 dialogContainer.addView(spinner); | |
104 | |
105 LinearLayout certContainer = new LinearLayout(mContext); | |
106 certContainer.setOrientation(LinearLayout.VERTICAL); | |
107 for (int i = 0; i < mViews.size(); ++i) { | |
108 LinearLayout certificateView = mViews.get(i); | |
109 if (i != 0) { | |
110 certificateView.setVisibility(LinearLayout.GONE); | |
111 } | |
112 certContainer.addView(certificateView); | |
113 } | |
114 ScrollView scrollView = new ScrollView(mContext); | |
115 scrollView.addView(certContainer); | |
116 dialogContainer.addView(scrollView); | |
117 | |
118 showDialogForView(dialogContainer); | |
119 } | |
120 | |
121 // Displays a dialog with scrolling for the given view. | |
122 private void showDialogForView(View view) { | |
123 Dialog dialog = new Dialog(mContext); | |
124 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
125 dialog.addContentView(view, | |
126 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PA
RENT, | |
127 LinearLayout.LayoutParams.MATCH_PARENT)); | |
128 dialog.show(); | |
129 } | |
130 | |
131 private void addCertificate(byte[] derData) { | |
132 try { | |
133 if (mCertificateFactory == null) { | |
134 mCertificateFactory = CertificateFactory.getInstance(X_509); | |
135 } | |
136 Certificate cert = mCertificateFactory.generateCertificate( | |
137 new ByteArrayInputStream(derData)); | |
138 addCertificateDetails(cert, getDigest(derData, "SHA-256"), getDigest
(derData, "SHA-1")); | |
139 } catch (CertificateException e) { | |
140 Log.e("CertViewer", "Error parsing certificate" + e.toString()); | |
141 } | |
142 } | |
143 | |
144 private void addCertificateDetails(Certificate cert, byte[] sha256Digest, by
te[] sha1Digest) { | |
145 LinearLayout certificateView = new LinearLayout(mContext); | |
146 mViews.add(certificateView); | |
147 certificateView.setOrientation(LinearLayout.VERTICAL); | |
148 | |
149 X509Certificate x509 = (X509Certificate) cert; | |
150 SslCertificate sslCert = new SslCertificate(x509); | |
151 | |
152 mTitles.add(sslCert.getIssuedTo().getCName()); | |
153 | |
154 addSectionTitle(certificateView, nativeGetCertIssuedToText()); | |
155 addItem(certificateView, nativeGetCertInfoCommonNameText(), | |
156 sslCert.getIssuedTo().getCName()); | |
157 addItem(certificateView, nativeGetCertInfoOrganizationText(), | |
158 sslCert.getIssuedTo().getOName()); | |
159 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), | |
160 sslCert.getIssuedTo().getUName()); | |
161 addItem(certificateView, nativeGetCertInfoSerialNumberText(), | |
162 formatBytes(x509.getSerialNumber().toByteArray(), ':')); | |
163 | |
164 addSectionTitle(certificateView, nativeGetCertIssuedByText()); | |
165 addItem(certificateView, nativeGetCertInfoCommonNameText(), | |
166 sslCert.getIssuedBy().getCName()); | |
167 addItem(certificateView, nativeGetCertInfoOrganizationText(), | |
168 sslCert.getIssuedBy().getOName()); | |
169 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), | |
170 sslCert.getIssuedBy().getUName()); | |
171 | |
172 addSectionTitle(certificateView, nativeGetCertValidityText()); | |
173 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); | |
174 addItem(certificateView, nativeGetCertIssuedOnText(), | |
175 dateFormat.format(sslCert.getValidNotBeforeDate())); | |
176 addItem(certificateView, nativeGetCertExpiresOnText(), | |
177 dateFormat.format(sslCert.getValidNotAfterDate())); | |
178 | |
179 addSectionTitle(certificateView, nativeGetCertFingerprintsText()); | |
180 addItem(certificateView, nativeGetCertSHA256FingerprintText(), | |
181 formatBytes(sha256Digest, ' ')); | |
182 addItem(certificateView, nativeGetCertSHA1FingerprintText(), | |
183 formatBytes(sha1Digest, ' ')); | |
184 } | |
185 | |
186 private void addSectionTitle(LinearLayout certificateView, String label) { | |
187 TextView title = addLabel(certificateView, label); | |
188 title.setAllCaps(true); | |
189 } | |
190 | |
191 private void addItem(LinearLayout certificateView, String label, String valu
e) { | |
192 if (value.isEmpty()) return; | |
193 | |
194 addLabel(certificateView, label); | |
195 addValue(certificateView, value); | |
196 } | |
197 | |
198 private TextView addLabel(LinearLayout certificateView, String label) { | |
199 TextView t = new TextView(mContext); | |
200 ApiCompatibilityUtils.setTextAlignment(t, View.TEXT_ALIGNMENT_VIEW_START
); | |
201 t.setPadding(mPadding, mPadding / 2, mPadding, 0); | |
202 t.setText(label); | |
203 t.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); | |
204 t.setTextColor(ApiCompatibilityUtils.getColor(mContext.getResources(), | |
205 R.color.connection_info_popup_text)); | |
206 certificateView.addView(t); | |
207 return t; | |
208 } | |
209 | |
210 private void addValue(LinearLayout certificateView, String value) { | |
211 TextView t = new TextView(mContext); | |
212 ApiCompatibilityUtils.setTextAlignment(t, View.TEXT_ALIGNMENT_VIEW_START
); | |
213 t.setText(value); | |
214 t.setPadding(mPadding, 0, mPadding, mPadding / 2); | |
215 t.setTextColor(ApiCompatibilityUtils.getColor(mContext.getResources(), | |
216 R.color.connection_info_popup_text)); | |
217 certificateView.addView(t); | |
218 } | |
219 | |
220 private static String formatBytes(byte[] bytes, char separator) { | |
221 StringBuilder sb = new StringBuilder(); | |
222 for (int i = 0; i < bytes.length; i++) { | |
223 sb.append(String.format("%02X", bytes[i])); | |
224 if (i != bytes.length - 1) { | |
225 sb.append(separator); | |
226 } | |
227 } | |
228 return sb.toString(); | |
229 } | |
230 | |
231 private static byte[] getDigest(byte[] bytes, String algorithm) { | |
232 try { | |
233 MessageDigest md = MessageDigest.getInstance(algorithm); | |
234 md.update(bytes); | |
235 return md.digest(); | |
236 } catch (java.security.NoSuchAlgorithmException e) { | |
237 return null; | |
238 } | |
239 } | |
240 | |
241 @Override | |
242 public void onItemSelected(AdapterView<?> parent, View view, int position, l
ong id) { | |
243 for (int i = 0; i < mViews.size(); ++i) { | |
244 mViews.get(i).setVisibility( | |
245 i == position ? LinearLayout.VISIBLE : LinearLayout.GONE); | |
246 } | |
247 } | |
248 | |
249 @Override | |
250 public void onNothingSelected(AdapterView<?> parent) { | |
251 } | |
252 | |
253 private static native String nativeGetCertIssuedToText(); | |
254 private static native String nativeGetCertInfoCommonNameText(); | |
255 private static native String nativeGetCertInfoOrganizationText(); | |
256 private static native String nativeGetCertInfoSerialNumberText(); | |
257 private static native String nativeGetCertInfoOrganizationUnitText(); | |
258 private static native String nativeGetCertIssuedByText(); | |
259 private static native String nativeGetCertValidityText(); | |
260 private static native String nativeGetCertIssuedOnText(); | |
261 private static native String nativeGetCertExpiresOnText(); | |
262 private static native String nativeGetCertFingerprintsText(); | |
263 private static native String nativeGetCertSHA256FingerprintText(); | |
264 private static native String nativeGetCertSHA1FingerprintText(); | |
265 } | |
OLD | NEW |