Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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; | |
| 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.text.format.DateFormat; | |
| 12 import android.util.Log; | |
| 13 import android.view.View; | |
| 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.chrome.R; | |
| 24 | |
| 25 import java.io.ByteArrayInputStream; | |
| 26 import java.security.MessageDigest; | |
| 27 import java.security.cert.Certificate; | |
| 28 import java.security.cert.CertificateException; | |
| 29 import java.security.cert.CertificateFactory; | |
| 30 import java.security.cert.X509Certificate; | |
| 31 import java.util.ArrayList; | |
| 32 | |
| 33 /** | |
| 34 * UI component for displaying certificate information. | |
| 35 */ | |
| 36 class CertificateViewer implements OnItemSelectedListener { | |
| 37 private static final String X_509 = "X.509"; | |
| 38 private final Context mContext; | |
| 39 private final ArrayList<LinearLayout> mViews; | |
| 40 private final ArrayList<String> mTitles; | |
| 41 private final int mPadding; | |
| 42 private CertificateFactory mCertificateFactory; | |
| 43 | |
| 44 /** | |
| 45 * Show a dialog with the provided certificate information. | |
| 46 * | |
| 47 * @param context The context this view should display in. | |
| 48 * @param derData DER-encoded data representing a X509 certificate chain. | |
| 49 */ | |
| 50 public static void showCertificateChain(Context context, byte[][] derData) { | |
| 51 CertificateViewer viewer = new CertificateViewer(context); | |
| 52 viewer.showCertificateChain(derData); | |
| 53 } | |
| 54 | |
| 55 private CertificateViewer(Context context) { | |
| 56 mContext = context; | |
| 57 mViews = new ArrayList<LinearLayout>(); | |
| 58 mTitles = new ArrayList<String>(); | |
| 59 mPadding = (int) context.getResources().getDimension( | |
| 60 R.dimen.connection_info_padding_wide) / 2; | |
| 61 } | |
| 62 | |
| 63 // Show information about an array of DER-encoded data representing a X509 c ertificate chain. | |
| 64 // A spinner will be displayed allowing the user to select which certificate to display. | |
| 65 private void showCertificateChain(byte[][] derData) { | |
| 66 for (int i = 0; i < derData.length; i++) { | |
| 67 addCertificate(derData[i]); | |
| 68 } | |
| 69 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, | |
| 70 android.R.layout.simple_spinner_item, | |
| 71 mTitles); | |
| 72 arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dro pdown_item); | |
| 73 | |
| 74 LinearLayout dialogContainer = new LinearLayout(mContext); | |
| 75 dialogContainer.setOrientation(LinearLayout.VERTICAL); | |
| 76 | |
| 77 TextView title = new TextView(mContext); | |
| 78 title.setText(R.string.certtitle); | |
| 79 title.setTextAppearance(mContext, android.R.style.TextAppearance_Large); | |
| 80 title.setTypeface(title.getTypeface(), Typeface.BOLD); | |
| 81 title.setPadding(mPadding, mPadding, mPadding, mPadding / 2); | |
| 82 dialogContainer.addView(title); | |
| 83 | |
| 84 // The simple_spinner_item has padding built-in, align it with the rest of the view | |
| 85 // manually. | |
| 86 int paddingSpinner = | |
| 87 (int) mContext.getResources().getDimension(R.dimen.connection_in fo_padding_spinner); | |
|
Ted C
2015/04/28 01:17:40
I suspect you'll find this value to be dependent o
tsergeant
2015/04/28 03:15:00
Good point - this was intended to compound with th
| |
| 88 | |
| 89 Spinner spinner = new Spinner(mContext); | |
| 90 spinner.setAdapter(arrayAdapter); | |
| 91 spinner.setOnItemSelectedListener(this); | |
| 92 spinner.setPadding(paddingSpinner, 0, mPadding / 2, mPadding); | |
| 93 dialogContainer.addView(spinner); | |
| 94 | |
| 95 LinearLayout certContainer = new LinearLayout(mContext); | |
| 96 certContainer.setOrientation(LinearLayout.VERTICAL); | |
| 97 for (int i = 0; i < mViews.size(); ++i) { | |
| 98 LinearLayout certificateView = mViews.get(i); | |
| 99 if (i != 0) { | |
| 100 certificateView.setVisibility(LinearLayout.GONE); | |
| 101 } | |
| 102 certContainer.addView(certificateView); | |
| 103 } | |
| 104 ScrollView scrollView = new ScrollView(mContext); | |
| 105 scrollView.addView(certContainer); | |
| 106 dialogContainer.addView(scrollView); | |
| 107 | |
| 108 showDialogForView(dialogContainer); | |
| 109 } | |
| 110 | |
| 111 // Displays a dialog with scrolling for the given view. | |
| 112 private void showDialogForView(View view) { | |
| 113 Dialog dialog = new Dialog(mContext); | |
| 114 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
| 115 dialog.addContentView(view, | |
| 116 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PA RENT, | |
| 117 LinearLayout.LayoutParams.MATCH_PARENT)); | |
| 118 dialog.show(); | |
| 119 } | |
| 120 | |
| 121 private void addCertificate(byte[] derData) { | |
| 122 try { | |
| 123 if (mCertificateFactory == null) { | |
| 124 mCertificateFactory = CertificateFactory.getInstance(X_509); | |
| 125 } | |
| 126 Certificate cert = mCertificateFactory.generateCertificate( | |
| 127 new ByteArrayInputStream(derData)); | |
| 128 addCertificateDetails(cert, getDigest(derData, "SHA-256"), getDigest (derData, "SHA-1")); | |
| 129 } catch (CertificateException e) { | |
| 130 Log.e("CertViewer", "Error parsing certificate" + e.toString()); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 private void addCertificateDetails(Certificate cert, byte[] sha256Digest, by te[] sha1Digest) { | |
| 135 LinearLayout certificateView = new LinearLayout(mContext); | |
| 136 mViews.add(certificateView); | |
| 137 certificateView.setOrientation(LinearLayout.VERTICAL); | |
| 138 | |
| 139 X509Certificate x509 = (X509Certificate) cert; | |
| 140 SslCertificate sslCert = new SslCertificate(x509); | |
| 141 | |
| 142 mTitles.add(sslCert.getIssuedTo().getCName()); | |
| 143 | |
| 144 addSectionTitle(certificateView, nativeGetCertIssuedToText()); | |
| 145 addItem(certificateView, nativeGetCertInfoCommonNameText(), | |
| 146 sslCert.getIssuedTo().getCName()); | |
| 147 addItem(certificateView, nativeGetCertInfoOrganizationText(), | |
| 148 sslCert.getIssuedTo().getOName()); | |
| 149 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), | |
| 150 sslCert.getIssuedTo().getUName()); | |
| 151 addItem(certificateView, nativeGetCertInfoSerialNumberText(), | |
| 152 formatBytes(x509.getSerialNumber().toByteArray(), ':')); | |
| 153 | |
| 154 addSectionTitle(certificateView, nativeGetCertIssuedByText()); | |
| 155 addItem(certificateView, nativeGetCertInfoCommonNameText(), | |
| 156 sslCert.getIssuedBy().getCName()); | |
| 157 addItem(certificateView, nativeGetCertInfoOrganizationText(), | |
| 158 sslCert.getIssuedBy().getOName()); | |
| 159 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), | |
| 160 sslCert.getIssuedBy().getUName()); | |
| 161 | |
| 162 addSectionTitle(certificateView, nativeGetCertValidityText()); | |
| 163 java.text.DateFormat dateFormat = DateFormat.getDateFormat(mContext); | |
| 164 addItem(certificateView, nativeGetCertIssuedOnText(), | |
| 165 dateFormat.format(sslCert.getValidNotBeforeDate())); | |
| 166 addItem(certificateView, nativeGetCertExpiresOnText(), | |
| 167 dateFormat.format(sslCert.getValidNotAfterDate())); | |
| 168 | |
| 169 addSectionTitle(certificateView, nativeGetCertFingerprintsText()); | |
| 170 addItem(certificateView, nativeGetCertSHA256FingerprintText(), | |
| 171 formatBytes(sha256Digest, ' ')); | |
| 172 addItem(certificateView, nativeGetCertSHA1FingerprintText(), | |
| 173 formatBytes(sha1Digest, ' ')); | |
| 174 } | |
| 175 | |
| 176 private void addSectionTitle(LinearLayout certificateView, String label) { | |
| 177 TextView title = addLabel(certificateView, label); | |
| 178 title.setAllCaps(true); | |
| 179 } | |
| 180 | |
| 181 private void addItem(LinearLayout certificateView, String label, String valu e) { | |
| 182 if (value.isEmpty()) return; | |
| 183 | |
| 184 addLabel(certificateView, label); | |
| 185 addValue(certificateView, value); | |
| 186 } | |
| 187 | |
| 188 private TextView addLabel(LinearLayout certificateView, String label) { | |
| 189 TextView t = new TextView(mContext); | |
| 190 t.setPadding(mPadding, mPadding / 2, mPadding, 0); | |
| 191 t.setText(label); | |
| 192 t.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); | |
| 193 t.setTextColor(mContext.getResources().getColor(R.color.connection_info_ popup_text)); | |
| 194 certificateView.addView(t); | |
| 195 return t; | |
| 196 } | |
| 197 | |
| 198 private void addValue(LinearLayout certificateView, String value) { | |
| 199 TextView t = new TextView(mContext); | |
| 200 t.setText(value); | |
| 201 t.setPadding(mPadding, 0, mPadding, mPadding / 2); | |
| 202 t.setTextColor(mContext.getResources().getColor(R.color.connection_info_ popup_text)); | |
| 203 certificateView.addView(t); | |
| 204 } | |
| 205 | |
| 206 private static String formatBytes(byte[] bytes, char separator) { | |
| 207 StringBuilder sb = new StringBuilder(); | |
| 208 for (int i = 0; i < bytes.length; i++) { | |
| 209 sb.append(String.format("%02X", bytes[i])); | |
| 210 if (i != bytes.length - 1) { | |
| 211 sb.append(separator); | |
| 212 } | |
| 213 } | |
| 214 return sb.toString(); | |
| 215 } | |
| 216 | |
| 217 private static byte[] getDigest(byte[] bytes, String algorithm) { | |
| 218 try { | |
| 219 MessageDigest md = MessageDigest.getInstance(algorithm); | |
| 220 md.update(bytes); | |
| 221 return md.digest(); | |
| 222 } catch (java.security.NoSuchAlgorithmException e) { | |
| 223 return null; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 @Override | |
| 228 public void onItemSelected(AdapterView<?> parent, View view, int position, l ong id) { | |
| 229 for (int i = 0; i < mViews.size(); ++i) { | |
| 230 mViews.get(i).setVisibility( | |
| 231 i == position ? LinearLayout.VISIBLE : LinearLayout.GONE); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 @Override | |
| 236 public void onNothingSelected(AdapterView<?> parent) { | |
| 237 } | |
| 238 | |
| 239 private static native String nativeGetCertIssuedToText(); | |
| 240 private static native String nativeGetCertInfoCommonNameText(); | |
| 241 private static native String nativeGetCertInfoOrganizationText(); | |
| 242 private static native String nativeGetCertInfoSerialNumberText(); | |
| 243 private static native String nativeGetCertInfoOrganizationUnitText(); | |
| 244 private static native String nativeGetCertIssuedByText(); | |
| 245 private static native String nativeGetCertValidityText(); | |
| 246 private static native String nativeGetCertIssuedOnText(); | |
| 247 private static native String nativeGetCertExpiresOnText(); | |
| 248 private static native String nativeGetCertFingerprintsText(); | |
| 249 private static native String nativeGetCertSHA256FingerprintText(); | |
| 250 private static native String nativeGetCertSHA1FingerprintText(); | |
| 251 } | |
| OLD | NEW |