| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.util.ArraySet; |
| 26 |
import android.util.PackageUtils; |
| 27 |
import android.util.Slog; |
| 28 |
|
| 29 |
import com.android.internal.annotations.VisibleForTesting; |
| 30 |
import com.android.internal.util.DataClass; |
| 31 |
|
| 32 |
import libcore.util.HexEncoding; |
| 33 |
|
| 34 |
import java.security.PublicKey; |
| 35 |
import java.security.cert.CertificateException; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Arrays; |
| 38 |
import java.util.Collections; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Set; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@DataClass(genConstructor = false, genConstDefs = false, genParcelable = true, genAidl = false) |
| 48 |
public final class SigningDetails implements Parcelable { |
| 49 |
|
| 50 |
private static final String TAG = "SigningDetails"; |
| 51 |
|
| 52 |
@IntDef({SignatureSchemeVersion.UNKNOWN, |
| 53 |
SignatureSchemeVersion.JAR, |
| 54 |
SignatureSchemeVersion.SIGNING_BLOCK_V2, |
| 55 |
SignatureSchemeVersion.SIGNING_BLOCK_V3, |
| 56 |
SignatureSchemeVersion.SIGNING_BLOCK_V4}) |
| 57 |
public @interface SignatureSchemeVersion { |
| 58 |
int UNKNOWN = 0; |
| 59 |
int JAR = 1; |
| 60 |
int SIGNING_BLOCK_V2 = 2; |
| 61 |
int SIGNING_BLOCK_V3 = 3; |
| 62 |
int SIGNING_BLOCK_V4 = 4; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
private final @Nullable Signature[] mSignatures; |
| 67 |
|
| 68 |
|
| 69 |
private final @SignatureSchemeVersion int mSignatureSchemeVersion; |
| 70 |
|
| 71 |
|
| 72 |
private final @Nullable ArraySet<PublicKey> mPublicKeys; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private final @Nullable Signature[] mPastSigningCertificates; |
| 88 |
|
| 89 |
|
| 90 |
private static final int PAST_CERT_EXISTS = 0; |
| 91 |
|
| 92 |
@IntDef(flag = true, |
| 93 |
value = {CertCapabilities.INSTALLED_DATA, |
| 94 |
CertCapabilities.SHARED_USER_ID, |
| 95 |
CertCapabilities.PERMISSION, |
| 96 |
CertCapabilities.ROLLBACK}) |
| 97 |
public @interface CertCapabilities { |
| 98 |
|
| 99 |
|
| 100 |
int INSTALLED_DATA = 1; |
| 101 |
|
| 102 |
|
| 103 |
int SHARED_USER_ID = 2; |
| 104 |
|
| 105 |
|
| 106 |
int PERMISSION = 4; |
| 107 |
|
| 108 |
|
| 109 |
int ROLLBACK = 8; |
| 110 |
|
| 111 |
|
| 112 |
int AUTH = 16; |
| 113 |
} |
| 114 |
|
| 115 |
@IntDef(value = {CapabilityMergeRule.MERGE_SELF_CAPABILITY, |
| 116 |
CapabilityMergeRule.MERGE_OTHER_CAPABILITY, |
| 117 |
CapabilityMergeRule.MERGE_RESTRICTED_CAPABILITY}) |
| 118 |
public @interface CapabilityMergeRule { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
int MERGE_SELF_CAPABILITY = 0; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
int MERGE_OTHER_CAPABILITY = 1; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
int MERGE_RESTRICTED_CAPABILITY = 2; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
public static final SigningDetails UNKNOWN = new SigningDetails( null, |
| 140 |
SignatureSchemeVersion.UNKNOWN, null, null); |
| 141 |
|
| 142 |
@VisibleForTesting |
| 143 |
public SigningDetails(@Nullable Signature[] signatures, |
| 144 |
@SignatureSchemeVersion int signatureSchemeVersion, |
| 145 |
@Nullable ArraySet<PublicKey> keys, @Nullable Signature[] pastSigningCertificates) { |
| 146 |
mSignatures = signatures; |
| 147 |
mSignatureSchemeVersion = signatureSchemeVersion; |
| 148 |
mPublicKeys = keys; |
| 149 |
mPastSigningCertificates = pastSigningCertificates; |
| 150 |
} |
| 151 |
|
| 152 |
public SigningDetails(@Nullable Signature[] signatures, |
| 153 |
@SignatureSchemeVersion int signatureSchemeVersion, |
| 154 |
@Nullable Signature[] pastSigningCertificates) |
| 155 |
throws CertificateException { |
| 156 |
this(signatures, signatureSchemeVersion, toSigningKeys(signatures), |
| 157 |
pastSigningCertificates); |
| 158 |
} |
| 159 |
|
| 160 |
public SigningDetails(@Nullable Signature[] signatures, |
| 161 |
@SignatureSchemeVersion int signatureSchemeVersion) |
| 162 |
throws CertificateException { |
| 163 |
this(signatures, signatureSchemeVersion, null); |
| 164 |
} |
| 165 |
|
| 166 |
public SigningDetails(@Nullable SigningDetails orig) { |
| 167 |
if (orig != null) { |
| 168 |
if (orig.mSignatures != null) { |
| 169 |
mSignatures = orig.mSignatures.clone(); |
| 170 |
} else { |
| 171 |
mSignatures = null; |
| 172 |
} |
| 173 |
mSignatureSchemeVersion = orig.mSignatureSchemeVersion; |
| 174 |
mPublicKeys = new ArraySet<>(orig.mPublicKeys); |
| 175 |
if (orig.mPastSigningCertificates != null) { |
| 176 |
mPastSigningCertificates = orig.mPastSigningCertificates.clone(); |
| 177 |
} else { |
| 178 |
mPastSigningCertificates = null; |
| 179 |
} |
| 180 |
} else { |
| 181 |
mSignatures = null; |
| 182 |
mSignatureSchemeVersion = SignatureSchemeVersion.UNKNOWN; |
| 183 |
mPublicKeys = null; |
| 184 |
mPastSigningCertificates = null; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public @NonNull SigningDetails mergeLineageWith(@NonNull SigningDetails otherSigningDetails) { |
| 200 |
return mergeLineageWith(otherSigningDetails, CapabilityMergeRule.MERGE_OTHER_CAPABILITY); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public @NonNull SigningDetails mergeLineageWith(@NonNull SigningDetails otherSigningDetails, |
| 243 |
@CapabilityMergeRule int mergeRule) { |
| 244 |
if (!hasPastSigningCertificates()) { |
| 245 |
return otherSigningDetails.hasPastSigningCertificates() |
| 246 |
&& otherSigningDetails.hasAncestorOrSelf(this) ? otherSigningDetails : this; |
| 247 |
} |
| 248 |
if (!otherSigningDetails.hasPastSigningCertificates()) { |
| 249 |
return this; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
SigningDetails descendantSigningDetails = getDescendantOrSelf(otherSigningDetails); |
| 254 |
if (descendantSigningDetails == null) { |
| 255 |
return this; |
| 256 |
} |
| 257 |
SigningDetails mergedDetails = this; |
| 258 |
if (descendantSigningDetails == this) { |
| 259 |
|
| 260 |
|
| 261 |
mergedDetails = mergeLineageWithAncestorOrSelf(otherSigningDetails, mergeRule); |
| 262 |
} else { |
| 263 |
|
| 264 |
|
| 265 |
switch (mergeRule) { |
| 266 |
case CapabilityMergeRule.MERGE_SELF_CAPABILITY: |
| 267 |
mergedDetails = otherSigningDetails.mergeLineageWithAncestorOrSelf(this, |
| 268 |
CapabilityMergeRule.MERGE_OTHER_CAPABILITY); |
| 269 |
break; |
| 270 |
case CapabilityMergeRule.MERGE_OTHER_CAPABILITY: |
| 271 |
mergedDetails = otherSigningDetails.mergeLineageWithAncestorOrSelf(this, |
| 272 |
CapabilityMergeRule.MERGE_SELF_CAPABILITY); |
| 273 |
break; |
| 274 |
case CapabilityMergeRule.MERGE_RESTRICTED_CAPABILITY: |
| 275 |
mergedDetails = otherSigningDetails.mergeLineageWithAncestorOrSelf(this, |
| 276 |
mergeRule); |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
return mergedDetails; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private @NonNull SigningDetails mergeLineageWithAncestorOrSelf( |
| 293 |
@NonNull SigningDetails otherSigningDetails, @CapabilityMergeRule int mergeRule) { |
| 294 |
|
| 295 |
int index = mPastSigningCertificates.length - 1; |
| 296 |
int otherIndex = otherSigningDetails.mPastSigningCertificates.length - 1; |
| 297 |
if (index < 0 || otherIndex < 0) { |
| 298 |
return this; |
| 299 |
} |
| 300 |
|
| 301 |
List<Signature> mergedSignatures = new ArrayList<>(); |
| 302 |
boolean capabilitiesModified = false; |
| 303 |
|
| 304 |
|
| 305 |
while (index >= 0 && !mPastSigningCertificates[index].equals( |
| 306 |
otherSigningDetails.mPastSigningCertificates[otherIndex])) { |
| 307 |
mergedSignatures.add(new Signature(mPastSigningCertificates[index--])); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if (index < 0) { |
| 312 |
return this; |
| 313 |
} |
| 314 |
|
| 315 |
do { |
| 316 |
|
| 317 |
|
| 318 |
Signature signature = mPastSigningCertificates[index--]; |
| 319 |
Signature ancestorSignature = |
| 320 |
otherSigningDetails.mPastSigningCertificates[otherIndex--]; |
| 321 |
Signature mergedSignature = new Signature(signature); |
| 322 |
if (signature.getFlags() != ancestorSignature.getFlags()) { |
| 323 |
capabilitiesModified = true; |
| 324 |
switch (mergeRule) { |
| 325 |
case CapabilityMergeRule.MERGE_SELF_CAPABILITY: |
| 326 |
mergedSignature.setFlags(signature.getFlags()); |
| 327 |
break; |
| 328 |
case CapabilityMergeRule.MERGE_OTHER_CAPABILITY: |
| 329 |
mergedSignature.setFlags(ancestorSignature.getFlags()); |
| 330 |
break; |
| 331 |
case CapabilityMergeRule.MERGE_RESTRICTED_CAPABILITY: |
| 332 |
mergedSignature.setFlags( |
| 333 |
signature.getFlags() & ancestorSignature.getFlags()); |
| 334 |
break; |
| 335 |
} |
| 336 |
} |
| 337 |
mergedSignatures.add(mergedSignature); |
| 338 |
} while (index >= 0 && otherIndex >= 0 && mPastSigningCertificates[index].equals( |
| 339 |
otherSigningDetails.mPastSigningCertificates[otherIndex])); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
if (index >= 0 && otherIndex >= 0) { |
| 344 |
return this; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
while (otherIndex >= 0) { |
| 350 |
mergedSignatures.add(new Signature( |
| 351 |
otherSigningDetails.mPastSigningCertificates[otherIndex--])); |
| 352 |
} |
| 353 |
while (index >= 0) { |
| 354 |
mergedSignatures.add(new Signature(mPastSigningCertificates[index--])); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if (mergedSignatures.size() == mPastSigningCertificates.length |
| 360 |
&& !capabilitiesModified) { |
| 361 |
return this; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
Collections.reverse(mergedSignatures); |
| 366 |
try { |
| 367 |
return new SigningDetails(new Signature[]{new Signature(mSignatures[0])}, |
| 368 |
mSignatureSchemeVersion, mergedSignatures.toArray(new Signature[0])); |
| 369 |
} catch (CertificateException e) { |
| 370 |
Slog.e(TAG, "Caught an exception creating the merged lineage: ", e); |
| 371 |
return this; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public boolean hasCommonAncestor(@NonNull SigningDetails otherSigningDetails) { |
| 387 |
if (!hasPastSigningCertificates()) { |
| 388 |
|
| 389 |
|
| 390 |
return otherSigningDetails.hasAncestorOrSelf(this); |
| 391 |
} |
| 392 |
if (!otherSigningDetails.hasPastSigningCertificates()) { |
| 393 |
return hasAncestorOrSelf(otherSigningDetails); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
return getDescendantOrSelf(otherSigningDetails) != null; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public boolean hasAncestorOrSelfWithDigest(@Nullable Set<String> certDigests) { |
| 412 |
if (this == UNKNOWN || certDigests == null || certDigests.size() == 0) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
if (mSignatures.length > 1) { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
if (certDigests.size() < mSignatures.length) { |
| 421 |
return false; |
| 422 |
} |
| 423 |
for (Signature signature : mSignatures) { |
| 424 |
String signatureDigest = PackageUtils.computeSha256Digest( |
| 425 |
signature.toByteArray()); |
| 426 |
if (!certDigests.contains(signatureDigest)) { |
| 427 |
return false; |
| 428 |
} |
| 429 |
} |
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
String signatureDigest = PackageUtils.computeSha256Digest(mSignatures[0].toByteArray()); |
| 434 |
if (certDigests.contains(signatureDigest)) { |
| 435 |
return true; |
| 436 |
} |
| 437 |
if (hasPastSigningCertificates()) { |
| 438 |
|
| 439 |
|
| 440 |
for (int i = 0; i < mPastSigningCertificates.length - 1; i++) { |
| 441 |
signatureDigest = PackageUtils.computeSha256Digest( |
| 442 |
mPastSigningCertificates[i].toByteArray()); |
| 443 |
if (certDigests.contains(signatureDigest)) { |
| 444 |
return true; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
return false; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
private @Nullable SigningDetails getDescendantOrSelf( |
| 460 |
@NonNull SigningDetails otherSigningDetails) { |
| 461 |
final SigningDetails descendantSigningDetails; |
| 462 |
final SigningDetails ancestorSigningDetails; |
| 463 |
if (hasAncestorOrSelf(otherSigningDetails)) { |
| 464 |
|
| 465 |
|
| 466 |
descendantSigningDetails = this; |
| 467 |
ancestorSigningDetails = otherSigningDetails; |
| 468 |
} else if (otherSigningDetails.hasAncestor(this)) { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
descendantSigningDetails = otherSigningDetails; |
| 473 |
ancestorSigningDetails = this; |
| 474 |
} else { |
| 475 |
|
| 476 |
|
| 477 |
return null; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
int descendantIndex = descendantSigningDetails.mPastSigningCertificates.length - 1; |
| 482 |
int ancestorIndex = ancestorSigningDetails.mPastSigningCertificates.length - 1; |
| 483 |
while (descendantIndex >= 0 |
| 484 |
&& !descendantSigningDetails.mPastSigningCertificates[descendantIndex].equals( |
| 485 |
ancestorSigningDetails.mPastSigningCertificates[ancestorIndex])) { |
| 486 |
descendantIndex--; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
if (descendantIndex < 0) { |
| 491 |
return null; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
do { |
| 496 |
descendantIndex--; |
| 497 |
ancestorIndex--; |
| 498 |
} while (descendantIndex >= 0 && ancestorIndex >= 0 |
| 499 |
&& descendantSigningDetails.mPastSigningCertificates[descendantIndex].equals( |
| 500 |
ancestorSigningDetails.mPastSigningCertificates[ancestorIndex])); |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
if (descendantIndex >= 0 && ancestorIndex >= 0) { |
| 505 |
return null; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
return descendantSigningDetails; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
public boolean hasSignatures() { |
| 514 |
return mSignatures != null && mSignatures.length > 0; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
public boolean hasPastSigningCertificates() { |
| 519 |
return mPastSigningCertificates != null && mPastSigningCertificates.length > 0; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public boolean hasAncestorOrSelf(@NonNull SigningDetails oldDetails) { |
| 530 |
if (this == UNKNOWN || oldDetails == UNKNOWN) { |
| 531 |
return false; |
| 532 |
} |
| 533 |
if (oldDetails.mSignatures.length > 1) { |
| 534 |
|
| 535 |
|
| 536 |
return signaturesMatchExactly(oldDetails); |
| 537 |
} else { |
| 538 |
|
| 539 |
|
| 540 |
return hasCertificate(oldDetails.mSignatures[0]); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public boolean hasAncestor(@NonNull SigningDetails oldDetails) { |
| 550 |
if (this == UNKNOWN || oldDetails == UNKNOWN) { |
| 551 |
return false; |
| 552 |
} |
| 553 |
if (hasPastSigningCertificates() && oldDetails.mSignatures.length == 1) { |
| 554 |
|
| 555 |
for (int i = 0; i < mPastSigningCertificates.length - 1; i++) { |
| 556 |
if (mPastSigningCertificates[i].equals(oldDetails.mSignatures[0])) { |
| 557 |
return true; |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
return false; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
public boolean hasCommonSignerWithCapability(@NonNull SigningDetails otherDetails, |
| 576 |
@CertCapabilities int flags) { |
| 577 |
if (this == UNKNOWN || otherDetails == UNKNOWN) { |
| 578 |
return false; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
if (mSignatures.length > 1 || otherDetails.mSignatures.length > 1) { |
| 583 |
return signaturesMatchExactly(otherDetails); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
Set<Signature> otherSignatures = new ArraySet<>(); |
| 588 |
if (otherDetails.hasPastSigningCertificates()) { |
| 589 |
otherSignatures.addAll(Arrays.asList(otherDetails.mPastSigningCertificates)); |
| 590 |
} else { |
| 591 |
otherSignatures.addAll(Arrays.asList(otherDetails.mSignatures)); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
if (otherSignatures.contains(mSignatures[0])) { |
| 596 |
return true; |
| 597 |
} |
| 598 |
if (hasPastSigningCertificates()) { |
| 599 |
|
| 600 |
|
| 601 |
for (int i = 0; i < mPastSigningCertificates.length - 1; i++) { |
| 602 |
if (otherSignatures.contains(mPastSigningCertificates[i])) { |
| 603 |
|
| 604 |
if ((mPastSigningCertificates[i].getFlags() & flags) == flags) { |
| 605 |
return true; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public boolean checkCapability(@NonNull SigningDetails oldDetails, |
| 623 |
@CertCapabilities int flags) { |
| 624 |
if (this == UNKNOWN || oldDetails == UNKNOWN) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
if (oldDetails.mSignatures.length > 1) { |
| 628 |
|
| 629 |
|
| 630 |
return signaturesMatchExactly(oldDetails); |
| 631 |
} else { |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
return hasCertificate(oldDetails.mSignatures[0], flags); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public boolean checkCapabilityRecover(@NonNull SigningDetails oldDetails, |
| 644 |
@CertCapabilities int flags) throws CertificateException { |
| 645 |
if (oldDetails == UNKNOWN || this == UNKNOWN) { |
| 646 |
return false; |
| 647 |
} |
| 648 |
if (hasPastSigningCertificates() && oldDetails.mSignatures.length == 1) { |
| 649 |
|
| 650 |
for (int i = 0; i < mPastSigningCertificates.length; i++) { |
| 651 |
if (Signature.areEffectiveMatch( |
| 652 |
oldDetails.mSignatures[0], |
| 653 |
mPastSigningCertificates[i]) |
| 654 |
&& mPastSigningCertificates[i].getFlags() == flags) { |
| 655 |
return true; |
| 656 |
} |
| 657 |
} |
| 658 |
} else { |
| 659 |
return Signature.areEffectiveMatch(oldDetails.mSignatures, mSignatures); |
| 660 |
} |
| 661 |
return false; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public boolean hasCertificate(@NonNull Signature signature) { |
| 671 |
return hasCertificateInternal(signature, PAST_CERT_EXISTS); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
public boolean hasCertificate(@NonNull Signature signature, @CertCapabilities int flags) { |
| 682 |
return hasCertificateInternal(signature, flags); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
public boolean hasCertificate(byte[] certificate) { |
| 687 |
Signature signature = new Signature(certificate); |
| 688 |
return hasCertificate(signature); |
| 689 |
} |
| 690 |
|
| 691 |
private boolean hasCertificateInternal(@NonNull Signature signature, int flags) { |
| 692 |
if (this == UNKNOWN) { |
| 693 |
return false; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
if (hasPastSigningCertificates()) { |
| 698 |
|
| 699 |
|
| 700 |
for (int i = 0; i < mPastSigningCertificates.length - 1; i++) { |
| 701 |
if (mPastSigningCertificates[i].equals(signature)) { |
| 702 |
if (flags == PAST_CERT_EXISTS |
| 703 |
|| (flags & mPastSigningCertificates[i].getFlags()) == flags) { |
| 704 |
return true; |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
return mSignatures.length == 1 && mSignatures[0].equals(signature); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public boolean checkCapability(@Nullable String sha256String, @CertCapabilities int flags) { |
| 731 |
if (this == UNKNOWN || TextUtils.isEmpty(sha256String)) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
final byte[] sha256Bytes = HexEncoding.decode(sha256String, false ); |
| 737 |
if (hasSha256Certificate(sha256Bytes, flags)) { |
| 738 |
return true; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
final String[] mSignaturesSha256Digests = |
| 747 |
PackageUtils.computeSignaturesSha256Digests(mSignatures); |
| 748 |
final String mSignaturesSha256Digest = |
| 749 |
PackageUtils.computeSignaturesSha256Digest(mSignaturesSha256Digests); |
| 750 |
return mSignaturesSha256Digest.equals(sha256String); |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
public boolean hasSha256Certificate(byte[] sha256Certificate) { |
| 759 |
return hasSha256CertificateInternal(sha256Certificate, PAST_CERT_EXISTS); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
public boolean hasSha256Certificate(byte[] sha256Certificate, @CertCapabilities int flags) { |
| 771 |
return hasSha256CertificateInternal(sha256Certificate, flags); |
| 772 |
} |
| 773 |
|
| 774 |
private boolean hasSha256CertificateInternal(byte[] sha256Certificate, int flags) { |
| 775 |
if (this == UNKNOWN) { |
| 776 |
return false; |
| 777 |
} |
| 778 |
if (hasPastSigningCertificates()) { |
| 779 |
|
| 780 |
|
| 781 |
for (int i = 0; i < mPastSigningCertificates.length - 1; i++) { |
| 782 |
byte[] digest = PackageUtils.computeSha256DigestBytes( |
| 783 |
mPastSigningCertificates[i].toByteArray()); |
| 784 |
if (Arrays.equals(sha256Certificate, digest)) { |
| 785 |
if (flags == PAST_CERT_EXISTS |
| 786 |
|| (flags & mPastSigningCertificates[i].getFlags()) == flags) { |
| 787 |
return true; |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
if (mSignatures.length == 1) { |
| 795 |
byte[] digest = PackageUtils.computeSha256DigestBytes(mSignatures[0].toByteArray()); |
| 796 |
return Arrays.equals(sha256Certificate, digest); |
| 797 |
} |
| 798 |
return false; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
public boolean signaturesMatchExactly(@NonNull SigningDetails other) { |
| 803 |
return Signature.areExactMatch(mSignatures, other.mSignatures); |
| 804 |
} |
| 805 |
|
| 806 |
@Override |
| 807 |
public int describeContents() { |
| 808 |
return 0; |
| 809 |
} |
| 810 |
|
| 811 |
@Override |
| 812 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 813 |
boolean isUnknown = UNKNOWN == this; |
| 814 |
dest.writeBoolean(isUnknown); |
| 815 |
if (isUnknown) { |
| 816 |
return; |
| 817 |
} |
| 818 |
dest.writeTypedArray(mSignatures, flags); |
| 819 |
dest.writeInt(mSignatureSchemeVersion); |
| 820 |
dest.writeArraySet(mPublicKeys); |
| 821 |
dest.writeTypedArray(mPastSigningCertificates, flags); |
| 822 |
} |
| 823 |
|
| 824 |
protected SigningDetails(@NonNull Parcel in) { |
| 825 |
final ClassLoader boot = Object.class.getClassLoader(); |
| 826 |
mSignatures = in.createTypedArray(Signature.CREATOR); |
| 827 |
mSignatureSchemeVersion = in.readInt(); |
| 828 |
mPublicKeys = (ArraySet<PublicKey>) in.readArraySet(boot); |
| 829 |
mPastSigningCertificates = in.createTypedArray(Signature.CREATOR); |
| 830 |
} |
| 831 |
|
| 832 |
public static final @NonNull Parcelable.Creator<SigningDetails> CREATOR = |
| 833 |
new Creator<SigningDetails>() { |
| 834 |
@Override |
| 835 |
public SigningDetails createFromParcel(@NonNull Parcel source) { |
| 836 |
if (source.readBoolean()) { |
| 837 |
return UNKNOWN; |
| 838 |
} |
| 839 |
return new SigningDetails(source); |
| 840 |
} |
| 841 |
|
| 842 |
@Override |
| 843 |
public SigningDetails[] newArray(int size) { |
| 844 |
return new SigningDetails[size]; |
| 845 |
} |
| 846 |
}; |
| 847 |
|
| 848 |
@Override |
| 849 |
public boolean equals(@Nullable Object o) { |
| 850 |
if (this == o) return true; |
| 851 |
if (!(o instanceof SigningDetails)) return false; |
| 852 |
|
| 853 |
final SigningDetails that = (SigningDetails) o; |
| 854 |
|
| 855 |
if (mSignatureSchemeVersion != that.mSignatureSchemeVersion) return false; |
| 856 |
if (!Signature.areExactMatch(mSignatures, that.mSignatures)) return false; |
| 857 |
if (mPublicKeys != null) { |
| 858 |
if (!mPublicKeys.equals((that.mPublicKeys))) { |
| 859 |
return false; |
| 860 |
} |
| 861 |
} else if (that.mPublicKeys != null) { |
| 862 |
return false; |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
if (!Arrays.equals(mPastSigningCertificates, that.mPastSigningCertificates)) { |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
for (int i = 0; i < mPastSigningCertificates.length; i++) { |
| 871 |
if (mPastSigningCertificates[i].getFlags() |
| 872 |
!= that.mPastSigningCertificates[i].getFlags()) { |
| 873 |
return false; |
| 874 |
} |
| 875 |
} |
| 876 |
return true; |
| 877 |
} |
| 878 |
|
| 879 |
@Override |
| 880 |
public int hashCode() { |
| 881 |
int result = +Arrays.hashCode(mSignatures); |
| 882 |
result = 31 * result + mSignatureSchemeVersion; |
| 883 |
result = 31 * result + (mPublicKeys != null ? mPublicKeys.hashCode() : 0); |
| 884 |
result = 31 * result + Arrays.hashCode(mPastSigningCertificates); |
| 885 |
return result; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
public static class Builder { |
| 892 |
private @NonNull Signature[] mSignatures; |
| 893 |
private @SignatureSchemeVersion int mSignatureSchemeVersion = |
| 894 |
SignatureSchemeVersion.UNKNOWN; |
| 895 |
private @Nullable Signature[] mPastSigningCertificates; |
| 896 |
|
| 897 |
public Builder() { |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
public SigningDetails.Builder setSignatures(@NonNull Signature[] signatures) { |
| 902 |
mSignatures = signatures; |
| 903 |
return this; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
public SigningDetails.Builder setSignatureSchemeVersion( |
| 908 |
@SignatureSchemeVersion int signatureSchemeVersion) { |
| 909 |
mSignatureSchemeVersion = signatureSchemeVersion; |
| 910 |
return this; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
public SigningDetails.Builder setPastSigningCertificates( |
| 915 |
@Nullable Signature[] pastSigningCertificates) { |
| 916 |
mPastSigningCertificates = pastSigningCertificates; |
| 917 |
return this; |
| 918 |
} |
| 919 |
|
| 920 |
private void checkInvariants() { |
| 921 |
|
| 922 |
if (mSignatures == null) { |
| 923 |
throw new IllegalStateException("SigningDetails requires the current signing" |
| 924 |
+ " certificates."); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
public SigningDetails build() |
| 929 |
throws CertificateException { |
| 930 |
checkInvariants(); |
| 931 |
return new SigningDetails(mSignatures, mSignatureSchemeVersion, |
| 932 |
mPastSigningCertificates); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
public static ArraySet<PublicKey> toSigningKeys(@NonNull Signature[] signatures) |
| 938 |
throws CertificateException { |
| 939 |
final ArraySet<PublicKey> keys = new ArraySet<>(signatures.length); |
| 940 |
for (int i = 0; i < signatures.length; i++) { |
| 941 |
keys.add(signatures[i].getPublicKey()); |
| 942 |
} |
| 943 |
return keys; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
@DataClass.Generated.Member |
| 965 |
public @Nullable Signature[] getSignatures() { |
| 966 |
return mSignatures; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
@DataClass.Generated.Member |
| 973 |
public @SignatureSchemeVersion int getSignatureSchemeVersion() { |
| 974 |
return mSignatureSchemeVersion; |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
@DataClass.Generated.Member |
| 981 |
public @Nullable ArraySet<PublicKey> getPublicKeys() { |
| 982 |
return mPublicKeys; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
@DataClass.Generated.Member |
| 999 |
public @Nullable Signature[] getPastSigningCertificates() { |
| 1000 |
return mPastSigningCertificates; |
| 1001 |
} |
| 1002 |
|
| 1003 |
@DataClass.Generated( |
| 1004 |
time = 1650058974710L, |
| 1005 |
codegenVersion = "1.0.23", |
| 1006 |
sourceFile = "frameworks/base/core/java/android/content/pm/SigningDetails.java", |
| 1007 |
inputSignatures = "private static final java.lang.String TAG\nprivate final @android.annotation.Nullable android.content.pm.Signature[] mSignatures\nprivate final @android.content.pm.SigningDetails.SignatureSchemeVersion int mSignatureSchemeVersion\nprivate final @android.annotation.Nullable android.util.ArraySet<java.security.PublicKey> mPublicKeys\nprivate final @android.annotation.Nullable android.content.pm.Signature[] mPastSigningCertificates\nprivate static final int PAST_CERT_EXISTS\npublic static final android.content.pm.SigningDetails UNKNOWN\npublic static final @android.annotation.NonNull android.os.Parcelable.Creator<android.content.pm.SigningDetails> CREATOR\npublic @android.annotation.NonNull android.content.pm.SigningDetails mergeLineageWith(android.content.pm.SigningDetails)\npublic @android.annotation.NonNull android.content.pm.SigningDetails mergeLineageWith(android.content.pm.SigningDetails,int)\nprivate @android.annotation.NonNull android.content.pm.SigningDetails mergeLineageWithAncestorOrSelf(android.content.pm.SigningDetails,int)\npublic boolean hasCommonAncestor(android.content.pm.SigningDetails)\npublic boolean hasAncestorOrSelfWithDigest(java.util.Set<java.lang.String>)\nprivate @android.annotation.Nullable android.content.pm.SigningDetails getDescendantOrSelf(android.content.pm.SigningDetails)\npublic boolean hasSignatures()\npublic boolean hasPastSigningCertificates()\npublic boolean hasAncestorOrSelf(android.content.pm.SigningDetails)\npublic boolean hasAncestor(android.content.pm.SigningDetails)\npublic boolean hasCommonSignerWithCapability(android.content.pm.SigningDetails,int)\npublic boolean checkCapability(android.content.pm.SigningDetails,int)\npublic boolean checkCapabilityRecover(android.content.pm.SigningDetails,int)\npublic boolean hasCertificate(android.content.pm.Signature)\npublic boolean hasCertificate(android.content.pm.Signature,int)\npublic boolean hasCertificate(byte[])\nprivate boolean hasCertificateInternal(android.content.pm.Signature,int)\npublic boolean checkCapability(java.lang.String,int)\npublic boolean hasSha256Certificate(byte[])\npublic boolean hasSha256Certificate(byte[],int)\nprivate boolean hasSha256CertificateInternal(byte[],int)\npublic boolean signaturesMatchExactly(android.content.pm.SigningDetails)\npublic @java.lang.Override int describeContents()\npublic @java.lang.Override void writeToParcel(android.os.Parcel,int)\npublic @java.lang.Override boolean equals(java.lang.Object)\npublic @java.lang.Override int hashCode()\npublic static android.util.ArraySet<java.security.PublicKey> toSigningKeys(android.content.pm.Signature[])\nclass SigningDetails extends java.lang.Object implements [android.os.Parcelable]\nprivate @android.annotation.NonNull android.content.pm.Signature[] mSignatures\nprivate @android.content.pm.SigningDetails.SignatureSchemeVersion int mSignatureSchemeVersion\nprivate @android.annotation.Nullable android.content.pm.Signature[] mPastSigningCertificates\npublic android.content.pm.SigningDetails.Builder setSignatures(android.content.pm.Signature[])\npublic android.content.pm.SigningDetails.Builder setSignatureSchemeVersion(int)\npublic android.content.pm.SigningDetails.Builder setPastSigningCertificates(android.content.pm.Signature[])\nprivate void checkInvariants()\npublic android.content.pm.SigningDetails build()\nclass Builder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false, genParcelable=true, genAidl=false)") |
| 1008 |
@Deprecated |
| 1009 |
private void __metadata() {} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
} |