The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net.http; |
| 18 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 20 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 22 | import android.text.format.DateFormat; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
| 25 | import android.widget.TextView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Adam Vartanian | 6309c61 | 2018-07-27 10:35:14 +0100 | [diff] [blame] | 27 | import com.android.internal.util.HexDump; |
| 28 | import com.android.org.bouncycastle.asn1.x509.X509Name; |
| 29 | |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 30 | import java.io.ByteArrayInputStream; |
| 31 | import java.math.BigInteger; |
| 32 | import java.security.MessageDigest; |
| 33 | import java.security.NoSuchAlgorithmException; |
| 34 | import java.security.cert.Certificate; |
| 35 | import java.security.cert.CertificateEncodingException; |
| 36 | import java.security.cert.CertificateException; |
| 37 | import java.security.cert.CertificateFactory; |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 38 | import java.security.cert.X509Certificate; |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 39 | import java.text.ParseException; |
| 40 | import java.text.SimpleDateFormat; |
| 41 | import java.util.Date; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import java.util.Vector; |
| 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | /** |
| 45 | * SSL certificate info (certificate details) class |
| 46 | */ |
| 47 | public class SslCertificate { |
| 48 | |
| 49 | /** |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 50 | * SimpleDateFormat pattern for an ISO 8601 date |
| 51 | */ |
| 52 | private static String ISO_8601_DATE_FORMAT = "yyyy-MM-dd HH:mm:ssZ"; |
| 53 | |
| 54 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | * Name of the entity this certificate is issued to |
| 56 | */ |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 57 | private final DName mIssuedTo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Name of the entity this certificate is issued by |
| 61 | */ |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 62 | private final DName mIssuedBy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Not-before date from the validity period |
| 66 | */ |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 67 | private final Date mValidNotBefore; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Not-after date from the validity period |
| 71 | */ |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 72 | private final Date mValidNotAfter; |
| 73 | |
| 74 | /** |
| 75 | * The original source certificate, if available. |
| 76 | * |
| 77 | * TODO If deprecated constructors are removed, this should always |
| 78 | * be available, and saveState and restoreState can be simplified |
| 79 | * to be unconditional. |
| 80 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 81 | @UnsupportedAppUsage |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 82 | private final X509Certificate mX509Certificate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 84 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | * Bundle key names |
| 86 | */ |
| 87 | private static final String ISSUED_TO = "issued-to"; |
| 88 | private static final String ISSUED_BY = "issued-by"; |
| 89 | private static final String VALID_NOT_BEFORE = "valid-not-before"; |
| 90 | private static final String VALID_NOT_AFTER = "valid-not-after"; |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 91 | private static final String X509_CERTIFICATE = "x509-certificate"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Saves the certificate state to a bundle |
| 95 | * @param certificate The SSL certificate to store |
| 96 | * @return A bundle with the certificate stored in it or null if fails |
| 97 | */ |
| 98 | public static Bundle saveState(SslCertificate certificate) { |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 99 | if (certificate == null) { |
| 100 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 102 | Bundle bundle = new Bundle(); |
| 103 | bundle.putString(ISSUED_TO, certificate.getIssuedTo().getDName()); |
| 104 | bundle.putString(ISSUED_BY, certificate.getIssuedBy().getDName()); |
| 105 | bundle.putString(VALID_NOT_BEFORE, certificate.getValidNotBefore()); |
| 106 | bundle.putString(VALID_NOT_AFTER, certificate.getValidNotAfter()); |
| 107 | X509Certificate x509Certificate = certificate.mX509Certificate; |
| 108 | if (x509Certificate != null) { |
| 109 | try { |
| 110 | bundle.putByteArray(X509_CERTIFICATE, x509Certificate.getEncoded()); |
| 111 | } catch (CertificateEncodingException ignored) { |
| 112 | } |
| 113 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | return bundle; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Restores the certificate stored in the bundle |
| 119 | * @param bundle The bundle with the certificate state stored in it |
| 120 | * @return The SSL certificate stored in the bundle or null if fails |
| 121 | */ |
| 122 | public static SslCertificate restoreState(Bundle bundle) { |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 123 | if (bundle == null) { |
| 124 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 126 | X509Certificate x509Certificate; |
| 127 | byte[] bytes = bundle.getByteArray(X509_CERTIFICATE); |
| 128 | if (bytes == null) { |
| 129 | x509Certificate = null; |
| 130 | } else { |
| 131 | try { |
| 132 | CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 133 | Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes)); |
| 134 | x509Certificate = (X509Certificate) cert; |
| 135 | } catch (CertificateException e) { |
| 136 | x509Certificate = null; |
| 137 | } |
| 138 | } |
| 139 | return new SslCertificate(bundle.getString(ISSUED_TO), |
| 140 | bundle.getString(ISSUED_BY), |
| 141 | parseDate(bundle.getString(VALID_NOT_BEFORE)), |
| 142 | parseDate(bundle.getString(VALID_NOT_AFTER)), |
| 143 | x509Certificate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a new SSL certificate object |
| 148 | * @param issuedTo The entity this certificate is issued to |
| 149 | * @param issuedBy The entity that issued this certificate |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 150 | * @param validNotBefore The not-before date from the certificate |
| 151 | * validity period in ISO 8601 format |
| 152 | * @param validNotAfter The not-after date from the certificate |
| 153 | * validity period in ISO 8601 format |
Brian Carlstrom | cc687c5 | 2011-02-16 11:40:15 -0800 | [diff] [blame] | 154 | * @deprecated Use {@link #SslCertificate(X509Certificate)} |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 155 | */ |
Jesse Wilson | 28c7425 | 2010-11-04 14:09:18 -0700 | [diff] [blame] | 156 | @Deprecated |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 157 | public SslCertificate( |
| 158 | String issuedTo, String issuedBy, String validNotBefore, String validNotAfter) { |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 159 | this(issuedTo, issuedBy, parseDate(validNotBefore), parseDate(validNotAfter), null); |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Creates a new SSL certificate object |
| 164 | * @param issuedTo The entity this certificate is issued to |
| 165 | * @param issuedBy The entity that issued this certificate |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | * @param validNotBefore The not-before date from the certificate validity period |
| 167 | * @param validNotAfter The not-after date from the certificate validity period |
Brian Carlstrom | cc687c5 | 2011-02-16 11:40:15 -0800 | [diff] [blame] | 168 | * @deprecated Use {@link #SslCertificate(X509Certificate)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | */ |
Brian Carlstrom | cc687c5 | 2011-02-16 11:40:15 -0800 | [diff] [blame] | 170 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | public SslCertificate( |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 172 | String issuedTo, String issuedBy, Date validNotBefore, Date validNotAfter) { |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 173 | this(issuedTo, issuedBy, validNotBefore, validNotAfter, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Creates a new SSL certificate object from an X509 certificate |
| 178 | * @param certificate X509 certificate |
| 179 | */ |
| 180 | public SslCertificate(X509Certificate certificate) { |
| 181 | this(certificate.getSubjectDN().getName(), |
| 182 | certificate.getIssuerDN().getName(), |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 183 | certificate.getNotBefore(), |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 184 | certificate.getNotAfter(), |
| 185 | certificate); |
| 186 | } |
| 187 | |
| 188 | private SslCertificate( |
| 189 | String issuedTo, String issuedBy, |
| 190 | Date validNotBefore, Date validNotAfter, |
| 191 | X509Certificate x509Certificate) { |
| 192 | mIssuedTo = new DName(issuedTo); |
| 193 | mIssuedBy = new DName(issuedBy); |
| 194 | mValidNotBefore = cloneDate(validNotBefore); |
| 195 | mValidNotAfter = cloneDate(validNotAfter); |
| 196 | mX509Certificate = x509Certificate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @return Not-before date from the certificate validity period or |
| 201 | * "" if none has been set |
| 202 | */ |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 203 | public Date getValidNotBeforeDate() { |
Brian Carlstrom | a5987a5 | 2010-03-02 14:51:22 -0800 | [diff] [blame] | 204 | return cloneDate(mValidNotBefore); |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @return Not-before date from the certificate validity period in |
| 209 | * ISO 8601 format or "" if none has been set |
| 210 | * |
| 211 | * @deprecated Use {@link #getValidNotBeforeDate()} |
| 212 | */ |
Jesse Wilson | 28c7425 | 2010-11-04 14:09:18 -0700 | [diff] [blame] | 213 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | public String getValidNotBefore() { |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 215 | return formatDate(mValidNotBefore); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @return Not-after date from the certificate validity period or |
| 220 | * "" if none has been set |
| 221 | */ |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 222 | public Date getValidNotAfterDate() { |
Brian Carlstrom | a5987a5 | 2010-03-02 14:51:22 -0800 | [diff] [blame] | 223 | return cloneDate(mValidNotAfter); |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return Not-after date from the certificate validity period in |
| 228 | * ISO 8601 format or "" if none has been set |
| 229 | * |
Brian Carlstrom | 9147a0e | 2010-03-02 11:24:39 -0800 | [diff] [blame] | 230 | * @deprecated Use {@link #getValidNotAfterDate()} |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 231 | */ |
Jesse Wilson | 28c7425 | 2010-11-04 14:09:18 -0700 | [diff] [blame] | 232 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | public String getValidNotAfter() { |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 234 | return formatDate(mValidNotAfter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @return Issued-to distinguished name or null if none has been set |
| 239 | */ |
| 240 | public DName getIssuedTo() { |
| 241 | return mIssuedTo; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @return Issued-by distinguished name or null if none has been set |
| 246 | */ |
| 247 | public DName getIssuedBy() { |
| 248 | return mIssuedBy; |
| 249 | } |
| 250 | |
| 251 | /** |
Adam Vartanian | 6309c61 | 2018-07-27 10:35:14 +0100 | [diff] [blame] | 252 | * @return The {@code X509Certificate} used to create this {@code SslCertificate} or |
| 253 | * {@code null} if no certificate was provided. |
| 254 | */ |
| 255 | public X509Certificate getX509Certificate() { |
| 256 | return mX509Certificate; |
| 257 | } |
| 258 | |
| 259 | /** |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 260 | * Convenience for UI presentation, not intended as public API. |
| 261 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 262 | @UnsupportedAppUsage |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 263 | private static String getSerialNumber(X509Certificate x509Certificate) { |
| 264 | if (x509Certificate == null) { |
| 265 | return ""; |
| 266 | } |
| 267 | BigInteger serialNumber = x509Certificate.getSerialNumber(); |
| 268 | if (serialNumber == null) { |
| 269 | return ""; |
| 270 | } |
| 271 | return fingerprint(serialNumber.toByteArray()); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Convenience for UI presentation, not intended as public API. |
| 276 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 277 | @UnsupportedAppUsage |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 278 | private static String getDigest(X509Certificate x509Certificate, String algorithm) { |
| 279 | if (x509Certificate == null) { |
| 280 | return ""; |
| 281 | } |
| 282 | try { |
| 283 | byte[] bytes = x509Certificate.getEncoded(); |
| 284 | MessageDigest md = MessageDigest.getInstance(algorithm); |
| 285 | byte[] digest = md.digest(bytes); |
| 286 | return fingerprint(digest); |
| 287 | } catch (CertificateEncodingException ignored) { |
| 288 | return ""; |
| 289 | } catch (NoSuchAlgorithmException ignored) { |
| 290 | return ""; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | private static final String fingerprint(byte[] bytes) { |
| 295 | if (bytes == null) { |
| 296 | return ""; |
| 297 | } |
| 298 | StringBuilder sb = new StringBuilder(); |
| 299 | for (int i = 0; i < bytes.length; i++) { |
| 300 | byte b = bytes[i]; |
Przemyslaw Szczepaniak | 3f72604 | 2015-10-15 11:04:19 +0100 | [diff] [blame] | 301 | HexDump.appendByteAsHex(sb, b, true); |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 302 | if (i+1 != bytes.length) { |
| 303 | sb.append(':'); |
| 304 | } |
| 305 | } |
| 306 | return sb.toString(); |
| 307 | } |
| 308 | |
| 309 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | * @return A string representation of this certificate for debugging |
| 311 | */ |
| 312 | public String toString() { |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 313 | return ("Issued to: " + mIssuedTo.getDName() + ";\n" |
| 314 | + "Issued by: " + mIssuedBy.getDName() + ";\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Brian Carlstrom | 445d8f1 | 2010-03-02 10:02:19 -0800 | [diff] [blame] | 318 | * Parse an ISO 8601 date converting ParseExceptions to a null result; |
| 319 | */ |
| 320 | private static Date parseDate(String string) { |
| 321 | try { |
| 322 | return new SimpleDateFormat(ISO_8601_DATE_FORMAT).parse(string); |
| 323 | } catch (ParseException e) { |
| 324 | return null; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Format a date as an ISO 8601 string, return "" for a null date |
| 330 | */ |
| 331 | private static String formatDate(Date date) { |
| 332 | if (date == null) { |
| 333 | return ""; |
| 334 | } |
| 335 | return new SimpleDateFormat(ISO_8601_DATE_FORMAT).format(date); |
| 336 | } |
| 337 | |
| 338 | /** |
Brian Carlstrom | a5987a5 | 2010-03-02 14:51:22 -0800 | [diff] [blame] | 339 | * Clone a possibly null Date |
| 340 | */ |
| 341 | private static Date cloneDate(Date date) { |
| 342 | if (date == null) { |
| 343 | return null; |
| 344 | } |
| 345 | return (Date) date.clone(); |
| 346 | } |
| 347 | |
| 348 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * A distinguished name helper class: a 3-tuple of: |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 350 | * <ul> |
| 351 | * <li>the most specific common name (CN)</li> |
| 352 | * <li>the most specific organization (O)</li> |
| 353 | * <li>the most specific organizational unit (OU)</li> |
| 354 | * <ul> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | */ |
| 356 | public class DName { |
| 357 | /** |
| 358 | * Distinguished name (normally includes CN, O, and OU names) |
| 359 | */ |
| 360 | private String mDName; |
| 361 | |
| 362 | /** |
| 363 | * Common-name (CN) component of the name |
| 364 | */ |
| 365 | private String mCName; |
| 366 | |
| 367 | /** |
| 368 | * Organization (O) component of the name |
| 369 | */ |
| 370 | private String mOName; |
| 371 | |
| 372 | /** |
| 373 | * Organizational Unit (OU) component of the name |
| 374 | */ |
| 375 | private String mUName; |
| 376 | |
| 377 | /** |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 378 | * Creates a new {@code DName} from a string. The attributes |
| 379 | * are assumed to come in most significant to least |
| 380 | * significant order which is true of human readable values |
| 381 | * returned by methods such as {@code X500Principal.getName()}. |
| 382 | * Be aware that the underlying sources of distinguished names |
| 383 | * such as instances of {@code X509Certificate} are encoded in |
| 384 | * least significant to most significant order, so make sure |
| 385 | * the value passed here has the expected ordering of |
| 386 | * attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | */ |
| 388 | public DName(String dName) { |
| 389 | if (dName != null) { |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 390 | mDName = dName; |
| 391 | try { |
| 392 | X509Name x509Name = new X509Name(dName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 394 | Vector val = x509Name.getValues(); |
| 395 | Vector oid = x509Name.getOIDs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 397 | for (int i = 0; i < oid.size(); i++) { |
| 398 | if (oid.elementAt(i).equals(X509Name.CN)) { |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 399 | if (mCName == null) { |
| 400 | mCName = (String) val.elementAt(i); |
| 401 | } |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 402 | continue; |
| 403 | } |
| 404 | |
| 405 | if (oid.elementAt(i).equals(X509Name.O)) { |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 406 | if (mOName == null) { |
| 407 | mOName = (String) val.elementAt(i); |
| 408 | continue; |
| 409 | } |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | if (oid.elementAt(i).equals(X509Name.OU)) { |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 413 | if (mUName == null) { |
| 414 | mUName = (String) val.elementAt(i); |
| 415 | continue; |
| 416 | } |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
Brian Carlstrom | 6505122 | 2010-01-26 23:07:42 -0800 | [diff] [blame] | 419 | } catch (IllegalArgumentException ex) { |
| 420 | // thrown if there is an error parsing the string |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * @return The distinguished name (normally includes CN, O, and OU names) |
| 427 | */ |
| 428 | public String getDName() { |
| 429 | return mDName != null ? mDName : ""; |
| 430 | } |
| 431 | |
| 432 | /** |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 433 | * @return The most specific Common-name (CN) component of this name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | */ |
| 435 | public String getCName() { |
| 436 | return mCName != null ? mCName : ""; |
| 437 | } |
| 438 | |
| 439 | /** |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 440 | * @return The most specific Organization (O) component of this name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | */ |
| 442 | public String getOName() { |
| 443 | return mOName != null ? mOName : ""; |
| 444 | } |
| 445 | |
| 446 | /** |
Brian Carlstrom | 91154ce | 2012-12-21 17:28:04 -0800 | [diff] [blame] | 447 | * @return The most specific Organizational Unit (OU) component of this name |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | */ |
| 449 | public String getUName() { |
| 450 | return mUName != null ? mUName : ""; |
| 451 | } |
| 452 | } |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 453 | |
| 454 | /** |
| 455 | * Inflates the SSL certificate view (helper method). |
| 456 | * @return The resultant certificate view with issued-to, issued-by, |
| 457 | * issued-on, expires-on, and possibly other fields set. |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 458 | * |
| 459 | * @hide Used by Browser and Settings |
| 460 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 461 | @UnsupportedAppUsage |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 462 | public View inflateCertificateView(Context context) { |
| 463 | LayoutInflater factory = LayoutInflater.from(context); |
| 464 | |
| 465 | View certificateView = factory.inflate( |
| 466 | com.android.internal.R.layout.ssl_certificate, null); |
| 467 | |
| 468 | // issued to: |
| 469 | SslCertificate.DName issuedTo = getIssuedTo(); |
| 470 | if (issuedTo != null) { |
| 471 | ((TextView) certificateView.findViewById(com.android.internal.R.id.to_common)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 472 | .setText(issuedTo.getCName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 473 | ((TextView) certificateView.findViewById(com.android.internal.R.id.to_org)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 474 | .setText(issuedTo.getOName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 475 | ((TextView) certificateView.findViewById(com.android.internal.R.id.to_org_unit)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 476 | .setText(issuedTo.getUName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 477 | } |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 478 | // serial number: |
| 479 | ((TextView) certificateView.findViewById(com.android.internal.R.id.serial_number)) |
| 480 | .setText(getSerialNumber(mX509Certificate)); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 481 | |
| 482 | // issued by: |
| 483 | SslCertificate.DName issuedBy = getIssuedBy(); |
| 484 | if (issuedBy != null) { |
| 485 | ((TextView) certificateView.findViewById(com.android.internal.R.id.by_common)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 486 | .setText(issuedBy.getCName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 487 | ((TextView) certificateView.findViewById(com.android.internal.R.id.by_org)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 488 | .setText(issuedBy.getOName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 489 | ((TextView) certificateView.findViewById(com.android.internal.R.id.by_org_unit)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 490 | .setText(issuedBy.getUName()); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // issued on: |
| 494 | String issuedOn = formatCertificateDate(context, getValidNotBeforeDate()); |
| 495 | ((TextView) certificateView.findViewById(com.android.internal.R.id.issued_on)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 496 | .setText(issuedOn); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 497 | |
| 498 | // expires on: |
| 499 | String expiresOn = formatCertificateDate(context, getValidNotAfterDate()); |
| 500 | ((TextView) certificateView.findViewById(com.android.internal.R.id.expires_on)) |
Brian Carlstrom | 4a06281 | 2011-07-05 17:28:44 -0700 | [diff] [blame] | 501 | .setText(expiresOn); |
| 502 | |
| 503 | // fingerprints: |
| 504 | ((TextView) certificateView.findViewById(com.android.internal.R.id.sha256_fingerprint)) |
| 505 | .setText(getDigest(mX509Certificate, "SHA256")); |
| 506 | ((TextView) certificateView.findViewById(com.android.internal.R.id.sha1_fingerprint)) |
| 507 | .setText(getDigest(mX509Certificate, "SHA1")); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 508 | |
| 509 | return certificateView; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Formats the certificate date to a properly localized date string. |
| 514 | * @return Properly localized version of the certificate date string and |
| 515 | * the "" if it fails to localize. |
| 516 | */ |
| 517 | private String formatCertificateDate(Context context, Date certificateDate) { |
| 518 | if (certificateDate == null) { |
| 519 | return ""; |
| 520 | } |
Charles He | 2b47e74 | 2017-02-20 17:32:12 +0000 | [diff] [blame] | 521 | return DateFormat.getMediumDateFormat(context).format(certificateDate); |
Brian Carlstrom | 6da0033 | 2011-06-26 21:08:03 -0700 | [diff] [blame] | 522 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | } |