| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net.http; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.os.Bundle; |
| 23 |
import android.text.format.DateFormat; |
| 24 |
import android.view.LayoutInflater; |
| 25 |
import android.view.View; |
| 26 |
import android.widget.TextView; |
| 27 |
|
| 28 |
import com.android.internal.util.HexDump; |
| 29 |
import com.android.internal.org.bouncycastle.asn1.x509.X509Name; |
| 30 |
|
| 31 |
import java.io.ByteArrayInputStream; |
| 32 |
import java.math.BigInteger; |
| 33 |
import java.security.MessageDigest; |
| 34 |
import java.security.NoSuchAlgorithmException; |
| 35 |
import java.security.cert.Certificate; |
| 36 |
import java.security.cert.CertificateEncodingException; |
| 37 |
import java.security.cert.CertificateException; |
| 38 |
import java.security.cert.CertificateFactory; |
| 39 |
import java.security.cert.X509Certificate; |
| 40 |
import java.text.ParseException; |
| 41 |
import java.text.SimpleDateFormat; |
| 42 |
import java.util.Date; |
| 43 |
import java.util.Vector; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class SslCertificate { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private static String ISO_8601_DATE_FORMAT = "yyyy-MM-dd HH:mm:ssZ"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private final DName mIssuedTo; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private final DName mIssuedBy; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private final Date mValidNotBefore; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private final Date mValidNotAfter; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@UnsupportedAppUsage |
| 83 |
private final X509Certificate mX509Certificate; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private static final String ISSUED_TO = "issued-to"; |
| 89 |
private static final String ISSUED_BY = "issued-by"; |
| 90 |
private static final String VALID_NOT_BEFORE = "valid-not-before"; |
| 91 |
private static final String VALID_NOT_AFTER = "valid-not-after"; |
| 92 |
private static final String X509_CERTIFICATE = "x509-certificate"; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static Bundle saveState(SslCertificate certificate) { |
| 100 |
if (certificate == null) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
Bundle bundle = new Bundle(); |
| 104 |
bundle.putString(ISSUED_TO, certificate.getIssuedTo().getDName()); |
| 105 |
bundle.putString(ISSUED_BY, certificate.getIssuedBy().getDName()); |
| 106 |
bundle.putString(VALID_NOT_BEFORE, certificate.getValidNotBefore()); |
| 107 |
bundle.putString(VALID_NOT_AFTER, certificate.getValidNotAfter()); |
| 108 |
X509Certificate x509Certificate = certificate.mX509Certificate; |
| 109 |
if (x509Certificate != null) { |
| 110 |
try { |
| 111 |
bundle.putByteArray(X509_CERTIFICATE, x509Certificate.getEncoded()); |
| 112 |
} catch (CertificateEncodingException ignored) { |
| 113 |
} |
| 114 |
} |
| 115 |
return bundle; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static SslCertificate restoreState(Bundle bundle) { |
| 124 |
if (bundle == null) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
X509Certificate x509Certificate; |
| 128 |
byte[] bytes = bundle.getByteArray(X509_CERTIFICATE); |
| 129 |
if (bytes == null) { |
| 130 |
x509Certificate = null; |
| 131 |
} else { |
| 132 |
try { |
| 133 |
CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 134 |
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes)); |
| 135 |
x509Certificate = (X509Certificate) cert; |
| 136 |
} catch (CertificateException e) { |
| 137 |
x509Certificate = null; |
| 138 |
} |
| 139 |
} |
| 140 |
return new SslCertificate(bundle.getString(ISSUED_TO), |
| 141 |
bundle.getString(ISSUED_BY), |
| 142 |
parseDate(bundle.getString(VALID_NOT_BEFORE)), |
| 143 |
parseDate(bundle.getString(VALID_NOT_AFTER)), |
| 144 |
x509Certificate); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Deprecated |
| 158 |
public SslCertificate( |
| 159 |
String issuedTo, String issuedBy, String validNotBefore, String validNotAfter) { |
| 160 |
this(issuedTo, issuedBy, parseDate(validNotBefore), parseDate(validNotAfter), null); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
@Deprecated |
| 172 |
public SslCertificate( |
| 173 |
String issuedTo, String issuedBy, Date validNotBefore, Date validNotAfter) { |
| 174 |
this(issuedTo, issuedBy, validNotBefore, validNotAfter, null); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public SslCertificate(X509Certificate certificate) { |
| 182 |
this(certificate.getSubjectDN().getName(), |
| 183 |
certificate.getIssuerDN().getName(), |
| 184 |
certificate.getNotBefore(), |
| 185 |
certificate.getNotAfter(), |
| 186 |
certificate); |
| 187 |
} |
| 188 |
|
| 189 |
private SslCertificate( |
| 190 |
String issuedTo, String issuedBy, |
| 191 |
Date validNotBefore, Date validNotAfter, |
| 192 |
X509Certificate x509Certificate) { |
| 193 |
mIssuedTo = new DName(issuedTo); |
| 194 |
mIssuedBy = new DName(issuedBy); |
| 195 |
mValidNotBefore = cloneDate(validNotBefore); |
| 196 |
mValidNotAfter = cloneDate(validNotAfter); |
| 197 |
mX509Certificate = x509Certificate; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public Date getValidNotBeforeDate() { |
| 205 |
return cloneDate(mValidNotBefore); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@Deprecated |
| 215 |
public String getValidNotBefore() { |
| 216 |
return formatDate(mValidNotBefore); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public Date getValidNotAfterDate() { |
| 224 |
return cloneDate(mValidNotAfter); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
@Deprecated |
| 234 |
public String getValidNotAfter() { |
| 235 |
return formatDate(mValidNotAfter); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public DName getIssuedTo() { |
| 242 |
return mIssuedTo; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public DName getIssuedBy() { |
| 249 |
return mIssuedBy; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public @Nullable X509Certificate getX509Certificate() { |
| 257 |
return mX509Certificate; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
@UnsupportedAppUsage |
| 264 |
private static String getSerialNumber(X509Certificate x509Certificate) { |
| 265 |
if (x509Certificate == null) { |
| 266 |
return ""; |
| 267 |
} |
| 268 |
BigInteger serialNumber = x509Certificate.getSerialNumber(); |
| 269 |
if (serialNumber == null) { |
| 270 |
return ""; |
| 271 |
} |
| 272 |
return fingerprint(serialNumber.toByteArray()); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@UnsupportedAppUsage |
| 279 |
private static String getDigest(X509Certificate x509Certificate, String algorithm) { |
| 280 |
if (x509Certificate == null) { |
| 281 |
return ""; |
| 282 |
} |
| 283 |
try { |
| 284 |
byte[] bytes = x509Certificate.getEncoded(); |
| 285 |
MessageDigest md = MessageDigest.getInstance(algorithm); |
| 286 |
byte[] digest = md.digest(bytes); |
| 287 |
return fingerprint(digest); |
| 288 |
} catch (CertificateEncodingException ignored) { |
| 289 |
return ""; |
| 290 |
} catch (NoSuchAlgorithmException ignored) { |
| 291 |
return ""; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
private static final String fingerprint(byte[] bytes) { |
| 296 |
if (bytes == null) { |
| 297 |
return ""; |
| 298 |
} |
| 299 |
StringBuilder sb = new StringBuilder(); |
| 300 |
for (int i = 0; i < bytes.length; i++) { |
| 301 |
byte b = bytes[i]; |
| 302 |
HexDump.appendByteAsHex(sb, b, true); |
| 303 |
if (i+1 != bytes.length) { |
| 304 |
sb.append(':'); |
| 305 |
} |
| 306 |
} |
| 307 |
return sb.toString(); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public String toString() { |
| 314 |
return ("Issued to: " + mIssuedTo.getDName() + ";\n" |
| 315 |
+ "Issued by: " + mIssuedBy.getDName() + ";\n"); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
private static Date parseDate(String string) { |
| 322 |
try { |
| 323 |
return new SimpleDateFormat(ISO_8601_DATE_FORMAT).parse(string); |
| 324 |
} catch (ParseException e) { |
| 325 |
return null; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
private static String formatDate(Date date) { |
| 333 |
if (date == null) { |
| 334 |
return ""; |
| 335 |
} |
| 336 |
return new SimpleDateFormat(ISO_8601_DATE_FORMAT).format(date); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private static Date cloneDate(Date date) { |
| 343 |
if (date == null) { |
| 344 |
return null; |
| 345 |
} |
| 346 |
return (Date) date.clone(); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public class DName { |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private String mDName; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private String mCName; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
private String mOName; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private String mUName; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public DName(String dName) { |
| 390 |
if (dName != null) { |
| 391 |
mDName = dName; |
| 392 |
try { |
| 393 |
X509Name x509Name = new X509Name(dName); |
| 394 |
|
| 395 |
Vector val = x509Name.getValues(); |
| 396 |
Vector oid = x509Name.getOIDs(); |
| 397 |
|
| 398 |
for (int i = 0; i < oid.size(); i++) { |
| 399 |
if (oid.elementAt(i).equals(X509Name.CN)) { |
| 400 |
if (mCName == null) { |
| 401 |
mCName = (String) val.elementAt(i); |
| 402 |
} |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
if (oid.elementAt(i).equals(X509Name.O)) { |
| 407 |
if (mOName == null) { |
| 408 |
mOName = (String) val.elementAt(i); |
| 409 |
continue; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
if (oid.elementAt(i).equals(X509Name.OU)) { |
| 414 |
if (mUName == null) { |
| 415 |
mUName = (String) val.elementAt(i); |
| 416 |
continue; |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} catch (IllegalArgumentException ex) { |
| 421 |
|
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public String getDName() { |
| 430 |
return mDName != null ? mDName : ""; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public String getCName() { |
| 437 |
return mCName != null ? mCName : ""; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public String getOName() { |
| 444 |
return mOName != null ? mOName : ""; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public String getUName() { |
| 451 |
return mUName != null ? mUName : ""; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
@UnsupportedAppUsage |
| 463 |
public View inflateCertificateView(Context context) { |
| 464 |
LayoutInflater factory = LayoutInflater.from(context); |
| 465 |
|
| 466 |
View certificateView = factory.inflate( |
| 467 |
com.android.internal.R.layout.ssl_certificate, null); |
| 468 |
|
| 469 |
|
| 470 |
SslCertificate.DName issuedTo = getIssuedTo(); |
| 471 |
if (issuedTo != null) { |
| 472 |
((TextView) certificateView.findViewById(com.android.internal.R.id.to_common)) |
| 473 |
.setText(issuedTo.getCName()); |
| 474 |
((TextView) certificateView.findViewById(com.android.internal.R.id.to_org)) |
| 475 |
.setText(issuedTo.getOName()); |
| 476 |
((TextView) certificateView.findViewById(com.android.internal.R.id.to_org_unit)) |
| 477 |
.setText(issuedTo.getUName()); |
| 478 |
} |
| 479 |
|
| 480 |
((TextView) certificateView.findViewById(com.android.internal.R.id.serial_number)) |
| 481 |
.setText(getSerialNumber(mX509Certificate)); |
| 482 |
|
| 483 |
|
| 484 |
SslCertificate.DName issuedBy = getIssuedBy(); |
| 485 |
if (issuedBy != null) { |
| 486 |
((TextView) certificateView.findViewById(com.android.internal.R.id.by_common)) |
| 487 |
.setText(issuedBy.getCName()); |
| 488 |
((TextView) certificateView.findViewById(com.android.internal.R.id.by_org)) |
| 489 |
.setText(issuedBy.getOName()); |
| 490 |
((TextView) certificateView.findViewById(com.android.internal.R.id.by_org_unit)) |
| 491 |
.setText(issuedBy.getUName()); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
String issuedOn = formatCertificateDate(context, getValidNotBeforeDate()); |
| 496 |
((TextView) certificateView.findViewById(com.android.internal.R.id.issued_on)) |
| 497 |
.setText(issuedOn); |
| 498 |
|
| 499 |
|
| 500 |
String expiresOn = formatCertificateDate(context, getValidNotAfterDate()); |
| 501 |
((TextView) certificateView.findViewById(com.android.internal.R.id.expires_on)) |
| 502 |
.setText(expiresOn); |
| 503 |
|
| 504 |
|
| 505 |
((TextView) certificateView.findViewById(com.android.internal.R.id.sha256_fingerprint)) |
| 506 |
.setText(getDigest(mX509Certificate, "SHA256")); |
| 507 |
((TextView) certificateView.findViewById(com.android.internal.R.id.sha1_fingerprint)) |
| 508 |
.setText(getDigest(mX509Certificate, "SHA1")); |
| 509 |
|
| 510 |
return certificateView; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
private String formatCertificateDate(Context context, Date certificateDate) { |
| 519 |
if (certificateDate == null) { |
| 520 |
return ""; |
| 521 |
} |
| 522 |
return DateFormat.getMediumDateFormat(context).format(certificateDate); |
| 523 |
} |
| 524 |
} |