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; |
| 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 Spinner spinner = new Spinner(mContext); |
| 85 spinner.setAdapter(arrayAdapter); |
| 86 spinner.setOnItemSelectedListener(this); |
| 87 spinner.setPadding(0, 0, mPadding / 2, mPadding); |
| 88 dialogContainer.addView(spinner); |
| 89 |
| 90 LinearLayout certContainer = new LinearLayout(mContext); |
| 91 certContainer.setOrientation(LinearLayout.VERTICAL); |
| 92 for (int i = 0; i < mViews.size(); ++i) { |
| 93 LinearLayout certificateView = mViews.get(i); |
| 94 if (i != 0) { |
| 95 certificateView.setVisibility(LinearLayout.GONE); |
| 96 } |
| 97 certContainer.addView(certificateView); |
| 98 } |
| 99 ScrollView scrollView = new ScrollView(mContext); |
| 100 scrollView.addView(certContainer); |
| 101 dialogContainer.addView(scrollView); |
| 102 |
| 103 showDialogForView(dialogContainer); |
| 104 } |
| 105 |
| 106 // Displays a dialog with scrolling for the given view. |
| 107 private void showDialogForView(View view) { |
| 108 Dialog dialog = new Dialog(mContext); |
| 109 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 110 dialog.addContentView(view, |
| 111 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PA
RENT, |
| 112 LinearLayout.LayoutParams.MATCH_PARENT)); |
| 113 dialog.show(); |
| 114 } |
| 115 |
| 116 private void addCertificate(byte[] derData) { |
| 117 try { |
| 118 if (mCertificateFactory == null) { |
| 119 mCertificateFactory = CertificateFactory.getInstance(X_509); |
| 120 } |
| 121 Certificate cert = mCertificateFactory.generateCertificate( |
| 122 new ByteArrayInputStream(derData)); |
| 123 addCertificateDetails(cert, getDigest(derData, "SHA-256"), getDigest
(derData, "SHA-1")); |
| 124 } catch (CertificateException e) { |
| 125 Log.e("CertViewer", "Error parsing certificate" + e.toString()); |
| 126 } |
| 127 } |
| 128 |
| 129 private void addCertificateDetails(Certificate cert, byte[] sha256Digest, by
te[] sha1Digest) { |
| 130 LinearLayout certificateView = new LinearLayout(mContext); |
| 131 mViews.add(certificateView); |
| 132 certificateView.setOrientation(LinearLayout.VERTICAL); |
| 133 |
| 134 X509Certificate x509 = (X509Certificate) cert; |
| 135 SslCertificate sslCert = new SslCertificate(x509); |
| 136 |
| 137 mTitles.add(sslCert.getIssuedTo().getCName()); |
| 138 |
| 139 addSectionTitle(certificateView, nativeGetCertIssuedToText()); |
| 140 addItem(certificateView, nativeGetCertInfoCommonNameText(), |
| 141 sslCert.getIssuedTo().getCName()); |
| 142 addItem(certificateView, nativeGetCertInfoOrganizationText(), |
| 143 sslCert.getIssuedTo().getOName()); |
| 144 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), |
| 145 sslCert.getIssuedTo().getUName()); |
| 146 addItem(certificateView, nativeGetCertInfoSerialNumberText(), |
| 147 formatBytes(x509.getSerialNumber().toByteArray(), ':')); |
| 148 |
| 149 addSectionTitle(certificateView, nativeGetCertIssuedByText()); |
| 150 addItem(certificateView, nativeGetCertInfoCommonNameText(), |
| 151 sslCert.getIssuedBy().getCName()); |
| 152 addItem(certificateView, nativeGetCertInfoOrganizationText(), |
| 153 sslCert.getIssuedBy().getOName()); |
| 154 addItem(certificateView, nativeGetCertInfoOrganizationUnitText(), |
| 155 sslCert.getIssuedBy().getUName()); |
| 156 |
| 157 addSectionTitle(certificateView, nativeGetCertValidityText()); |
| 158 java.text.DateFormat dateFormat = DateFormat.getDateFormat(mContext); |
| 159 addItem(certificateView, nativeGetCertIssuedOnText(), |
| 160 dateFormat.format(sslCert.getValidNotBeforeDate())); |
| 161 addItem(certificateView, nativeGetCertExpiresOnText(), |
| 162 dateFormat.format(sslCert.getValidNotAfterDate())); |
| 163 |
| 164 addSectionTitle(certificateView, nativeGetCertFingerprintsText()); |
| 165 addItem(certificateView, nativeGetCertSHA256FingerprintText(), |
| 166 formatBytes(sha256Digest, ' ')); |
| 167 addItem(certificateView, nativeGetCertSHA1FingerprintText(), |
| 168 formatBytes(sha1Digest, ' ')); |
| 169 } |
| 170 |
| 171 private void addSectionTitle(LinearLayout certificateView, String label) { |
| 172 TextView title = addLabel(certificateView, label); |
| 173 title.setAllCaps(true); |
| 174 } |
| 175 |
| 176 private void addItem(LinearLayout certificateView, String label, String valu
e) { |
| 177 if (value.isEmpty()) return; |
| 178 |
| 179 addLabel(certificateView, label); |
| 180 addValue(certificateView, value); |
| 181 } |
| 182 |
| 183 private TextView addLabel(LinearLayout certificateView, String label) { |
| 184 TextView t = new TextView(mContext); |
| 185 t.setPadding(mPadding, mPadding / 2, mPadding, 0); |
| 186 t.setText(label); |
| 187 t.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); |
| 188 t.setTextColor(mContext.getResources().getColor(R.color.connection_info_
popup_text)); |
| 189 certificateView.addView(t); |
| 190 return t; |
| 191 } |
| 192 |
| 193 private void addValue(LinearLayout certificateView, String value) { |
| 194 TextView t = new TextView(mContext); |
| 195 t.setText(value); |
| 196 t.setPadding(mPadding, 0, mPadding, mPadding / 2); |
| 197 t.setTextColor(mContext.getResources().getColor(R.color.connection_info_
popup_text)); |
| 198 certificateView.addView(t); |
| 199 } |
| 200 |
| 201 private static String formatBytes(byte[] bytes, char separator) { |
| 202 StringBuilder sb = new StringBuilder(); |
| 203 for (int i = 0; i < bytes.length; i++) { |
| 204 sb.append(String.format("%02X", bytes[i])); |
| 205 if (i != bytes.length - 1) { |
| 206 sb.append(separator); |
| 207 } |
| 208 } |
| 209 return sb.toString(); |
| 210 } |
| 211 |
| 212 private static byte[] getDigest(byte[] bytes, String algorithm) { |
| 213 try { |
| 214 MessageDigest md = MessageDigest.getInstance(algorithm); |
| 215 md.update(bytes); |
| 216 return md.digest(); |
| 217 } catch (java.security.NoSuchAlgorithmException e) { |
| 218 return null; |
| 219 } |
| 220 } |
| 221 |
| 222 @Override |
| 223 public void onItemSelected(AdapterView<?> parent, View view, int position, l
ong id) { |
| 224 for (int i = 0; i < mViews.size(); ++i) { |
| 225 mViews.get(i).setVisibility( |
| 226 i == position ? LinearLayout.VISIBLE : LinearLayout.GONE); |
| 227 } |
| 228 } |
| 229 |
| 230 @Override |
| 231 public void onNothingSelected(AdapterView<?> parent) { |
| 232 } |
| 233 |
| 234 private static native String nativeGetCertIssuedToText(); |
| 235 private static native String nativeGetCertInfoCommonNameText(); |
| 236 private static native String nativeGetCertInfoOrganizationText(); |
| 237 private static native String nativeGetCertInfoSerialNumberText(); |
| 238 private static native String nativeGetCertInfoOrganizationUnitText(); |
| 239 private static native String nativeGetCertIssuedByText(); |
| 240 private static native String nativeGetCertValidityText(); |
| 241 private static native String nativeGetCertIssuedOnText(); |
| 242 private static native String nativeGetCertExpiresOnText(); |
| 243 private static native String nativeGetCertFingerprintsText(); |
| 244 private static native String nativeGetCertSHA256FingerprintText(); |
| 245 private static native String nativeGetCertSHA1FingerprintText(); |
| 246 } |
OLD | NEW |