| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package android.util.jar; |
| 19 |
|
| 20 |
import android.util.apk.ApkSignatureSchemeV2Verifier; |
| 21 |
import android.util.apk.ApkSignatureSchemeV3Verifier; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
import java.io.OutputStream; |
| 25 |
import java.nio.charset.StandardCharsets; |
| 26 |
import java.security.GeneralSecurityException; |
| 27 |
import java.security.MessageDigest; |
| 28 |
import java.security.NoSuchAlgorithmException; |
| 29 |
import java.security.cert.Certificate; |
| 30 |
import java.security.cert.X509Certificate; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.HashMap; |
| 33 |
import java.util.Hashtable; |
| 34 |
import java.util.Iterator; |
| 35 |
import java.util.List; |
| 36 |
import java.util.Locale; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.StringTokenizer; |
| 39 |
import java.util.jar.Attributes; |
| 40 |
import java.util.jar.JarFile; |
| 41 |
|
| 42 |
import sun.security.jca.Providers; |
| 43 |
import sun.security.pkcs.PKCS7; |
| 44 |
import sun.security.pkcs.SignerInfo; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class StrictJarVerifier { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private static final String SF_ATTRIBUTE_ANDROID_APK_SIGNED_NAME = "X-Android-APK-Signed"; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private static final String[] DIGEST_ALGORITHMS = new String[] { |
| 75 |
"SHA-512", |
| 76 |
"SHA-384", |
| 77 |
"SHA-256", |
| 78 |
"SHA1", |
| 79 |
}; |
| 80 |
|
| 81 |
private final String jarName; |
| 82 |
private final StrictJarManifest manifest; |
| 83 |
private final HashMap<String, byte[]> metaEntries; |
| 84 |
private final int mainAttributesEnd; |
| 85 |
private final boolean signatureSchemeRollbackProtectionsEnforced; |
| 86 |
|
| 87 |
private final Hashtable<String, HashMap<String, Attributes>> signatures = |
| 88 |
new Hashtable<String, HashMap<String, Attributes>>(5); |
| 89 |
|
| 90 |
private final Hashtable<String, Certificate[]> certificates = |
| 91 |
new Hashtable<String, Certificate[]>(5); |
| 92 |
|
| 93 |
private final Hashtable<String, Certificate[][]> verifiedEntries = |
| 94 |
new Hashtable<String, Certificate[][]>(); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static class VerifierEntry extends OutputStream { |
| 101 |
|
| 102 |
private final String name; |
| 103 |
|
| 104 |
private final MessageDigest digest; |
| 105 |
|
| 106 |
private final byte[] hash; |
| 107 |
|
| 108 |
private final Certificate[][] certChains; |
| 109 |
|
| 110 |
private final Hashtable<String, Certificate[][]> verifiedEntries; |
| 111 |
|
| 112 |
VerifierEntry(String name, MessageDigest digest, byte[] hash, |
| 113 |
Certificate[][] certChains, Hashtable<String, Certificate[][]> verifedEntries) { |
| 114 |
this.name = name; |
| 115 |
this.digest = digest; |
| 116 |
this.hash = hash; |
| 117 |
this.certChains = certChains; |
| 118 |
this.verifiedEntries = verifedEntries; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
public void write(int value) { |
| 126 |
digest.update((byte) value); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@Override |
| 133 |
public void write(byte[] buf, int off, int nbytes) { |
| 134 |
digest.update(buf, off, nbytes); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
void verify() { |
| 149 |
byte[] d = digest.digest(); |
| 150 |
if (!verifyMessageDigest(d, hash)) { |
| 151 |
throw invalidDigest(JarFile.MANIFEST_NAME, name, name); |
| 152 |
} |
| 153 |
verifiedEntries.put(name, certChains); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
private static SecurityException invalidDigest(String signatureFile, String name, |
| 158 |
String jarName) { |
| 159 |
throw new SecurityException(signatureFile + " has invalid digest for " + name + |
| 160 |
" in " + jarName); |
| 161 |
} |
| 162 |
|
| 163 |
private static SecurityException failedVerification(String jarName, String signatureFile) { |
| 164 |
throw new SecurityException(jarName + " failed verification of " + signatureFile); |
| 165 |
} |
| 166 |
|
| 167 |
private static SecurityException failedVerification(String jarName, String signatureFile, |
| 168 |
Throwable e) { |
| 169 |
throw new SecurityException(jarName + " failed verification of " + signatureFile, e); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
StrictJarVerifier(String name, StrictJarManifest manifest, |
| 184 |
HashMap<String, byte[]> metaEntries, boolean signatureSchemeRollbackProtectionsEnforced) { |
| 185 |
jarName = name; |
| 186 |
this.manifest = manifest; |
| 187 |
this.metaEntries = metaEntries; |
| 188 |
this.mainAttributesEnd = manifest.getMainAttributesEnd(); |
| 189 |
this.signatureSchemeRollbackProtectionsEnforced = |
| 190 |
signatureSchemeRollbackProtectionsEnforced; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
VerifierEntry initEntry(String name) { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if (manifest == null || signatures.isEmpty()) { |
| 210 |
return null; |
| 211 |
} |
| 212 |
|
| 213 |
Attributes attributes = manifest.getAttributes(name); |
| 214 |
|
| 215 |
if (attributes == null) { |
| 216 |
return null; |
| 217 |
} |
| 218 |
|
| 219 |
ArrayList<Certificate[]> certChains = new ArrayList<Certificate[]>(); |
| 220 |
Iterator<Map.Entry<String, HashMap<String, Attributes>>> it = signatures.entrySet().iterator(); |
| 221 |
while (it.hasNext()) { |
| 222 |
Map.Entry<String, HashMap<String, Attributes>> entry = it.next(); |
| 223 |
HashMap<String, Attributes> hm = entry.getValue(); |
| 224 |
if (hm.get(name) != null) { |
| 225 |
|
| 226 |
String signatureFile = entry.getKey(); |
| 227 |
Certificate[] certChain = certificates.get(signatureFile); |
| 228 |
if (certChain != null) { |
| 229 |
certChains.add(certChain); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
if (certChains.isEmpty()) { |
| 236 |
return null; |
| 237 |
} |
| 238 |
Certificate[][] certChainsArray = certChains.toArray(new Certificate[certChains.size()][]); |
| 239 |
|
| 240 |
for (int i = 0; i < DIGEST_ALGORITHMS.length; i++) { |
| 241 |
final String algorithm = DIGEST_ALGORITHMS[i]; |
| 242 |
final String hash = attributes.getValue(algorithm + "-Digest"); |
| 243 |
if (hash == null) { |
| 244 |
continue; |
| 245 |
} |
| 246 |
byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1); |
| 247 |
|
| 248 |
try { |
| 249 |
return new VerifierEntry(name, MessageDigest.getInstance(algorithm), hashBytes, |
| 250 |
certChainsArray, verifiedEntries); |
| 251 |
} catch (NoSuchAlgorithmException ignored) { |
| 252 |
} |
| 253 |
} |
| 254 |
return null; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
void addMetaEntry(String name, byte[] buf) { |
| 271 |
metaEntries.put(name.toUpperCase(Locale.US), buf); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
synchronized boolean readCertificates() { |
| 292 |
if (metaEntries.isEmpty()) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
Iterator<String> it = metaEntries.keySet().iterator(); |
| 297 |
while (it.hasNext()) { |
| 298 |
String key = it.next(); |
| 299 |
if (key.endsWith(".DSA") || key.endsWith(".RSA") || key.endsWith(".EC")) { |
| 300 |
verifyCertificate(key); |
| 301 |
it.remove(); |
| 302 |
} |
| 303 |
} |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static Certificate[] verifyBytes(byte[] blockBytes, byte[] sfBytes) |
| 314 |
throws GeneralSecurityException { |
| 315 |
|
| 316 |
Object obj = null; |
| 317 |
try { |
| 318 |
|
| 319 |
obj = Providers.startJarVerification(); |
| 320 |
PKCS7 block = new PKCS7(blockBytes); |
| 321 |
SignerInfo[] verifiedSignerInfos = block.verify(sfBytes); |
| 322 |
if ((verifiedSignerInfos == null) || (verifiedSignerInfos.length == 0)) { |
| 323 |
throw new GeneralSecurityException( |
| 324 |
"Failed to verify signature: no verified SignerInfos"); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
SignerInfo verifiedSignerInfo = verifiedSignerInfos[0]; |
| 331 |
List<X509Certificate> verifiedSignerCertChain = |
| 332 |
verifiedSignerInfo.getCertificateChain(block); |
| 333 |
if (verifiedSignerCertChain == null) { |
| 334 |
|
| 335 |
throw new GeneralSecurityException( |
| 336 |
"Failed to find verified SignerInfo certificate chain"); |
| 337 |
} else if (verifiedSignerCertChain.isEmpty()) { |
| 338 |
|
| 339 |
throw new GeneralSecurityException( |
| 340 |
"Verified SignerInfo certificate chain is emtpy"); |
| 341 |
} |
| 342 |
return verifiedSignerCertChain.toArray( |
| 343 |
new X509Certificate[verifiedSignerCertChain.size()]); |
| 344 |
} catch (IOException e) { |
| 345 |
throw new GeneralSecurityException("IO exception verifying jar cert", e); |
| 346 |
} finally { |
| 347 |
Providers.stopJarVerification(obj); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
private void verifyCertificate(String certFile) { |
| 355 |
|
| 356 |
String signatureFile = certFile.substring(0, certFile.lastIndexOf('.')) + ".SF"; |
| 357 |
byte[] sfBytes = metaEntries.get(signatureFile); |
| 358 |
if (sfBytes == null) { |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
byte[] manifestBytes = metaEntries.get(JarFile.MANIFEST_NAME); |
| 363 |
|
| 364 |
if (manifestBytes == null) { |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
byte[] sBlockBytes = metaEntries.get(certFile); |
| 369 |
try { |
| 370 |
Certificate[] signerCertChain = verifyBytes(sBlockBytes, sfBytes); |
| 371 |
if (signerCertChain != null) { |
| 372 |
certificates.put(signatureFile, signerCertChain); |
| 373 |
} |
| 374 |
} catch (GeneralSecurityException e) { |
| 375 |
throw failedVerification(jarName, signatureFile, e); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
Attributes attributes = new Attributes(); |
| 380 |
HashMap<String, Attributes> entries = new HashMap<String, Attributes>(); |
| 381 |
try { |
| 382 |
StrictJarManifestReader im = new StrictJarManifestReader(sfBytes, attributes); |
| 383 |
im.readEntries(entries, null); |
| 384 |
} catch (IOException e) { |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
if (signatureSchemeRollbackProtectionsEnforced) { |
| 390 |
String apkSignatureSchemeIdList = |
| 391 |
attributes.getValue(SF_ATTRIBUTE_ANDROID_APK_SIGNED_NAME); |
| 392 |
if (apkSignatureSchemeIdList != null) { |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
boolean v2SignatureGenerated = false; |
| 398 |
boolean v3SignatureGenerated = false; |
| 399 |
StringTokenizer tokenizer = new StringTokenizer(apkSignatureSchemeIdList, ","); |
| 400 |
while (tokenizer.hasMoreTokens()) { |
| 401 |
String idText = tokenizer.nextToken().trim(); |
| 402 |
if (idText.isEmpty()) { |
| 403 |
continue; |
| 404 |
} |
| 405 |
int id; |
| 406 |
try { |
| 407 |
id = Integer.parseInt(idText); |
| 408 |
} catch (Exception ignored) { |
| 409 |
continue; |
| 410 |
} |
| 411 |
if (id == ApkSignatureSchemeV2Verifier.SF_ATTRIBUTE_ANDROID_APK_SIGNED_ID) { |
| 412 |
|
| 413 |
|
| 414 |
v2SignatureGenerated = true; |
| 415 |
break; |
| 416 |
} |
| 417 |
if (id == ApkSignatureSchemeV3Verifier.SF_ATTRIBUTE_ANDROID_APK_SIGNED_ID) { |
| 418 |
|
| 419 |
|
| 420 |
v3SignatureGenerated = true; |
| 421 |
break; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
if (v2SignatureGenerated) { |
| 426 |
throw new SecurityException(signatureFile + " indicates " + jarName |
| 427 |
+ " is signed using APK Signature Scheme v2, but no such signature was" |
| 428 |
+ " found. Signature stripped?"); |
| 429 |
} |
| 430 |
if (v3SignatureGenerated) { |
| 431 |
throw new SecurityException(signatureFile + " indicates " + jarName |
| 432 |
+ " is signed using APK Signature Scheme v3, but no such signature was" |
| 433 |
+ " found. Signature stripped?"); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
if (attributes.get(Attributes.Name.SIGNATURE_VERSION) == null) { |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
boolean createdBySigntool = false; |
| 444 |
String createdBy = attributes.getValue("Created-By"); |
| 445 |
if (createdBy != null) { |
| 446 |
createdBySigntool = createdBy.indexOf("signtool") != -1; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
if (mainAttributesEnd > 0 && !createdBySigntool) { |
| 454 |
String digestAttribute = "-Digest-Manifest-Main-Attributes"; |
| 455 |
if (!verify(attributes, digestAttribute, manifestBytes, 0, mainAttributesEnd, false, true)) { |
| 456 |
throw failedVerification(jarName, signatureFile); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
String digestAttribute = createdBySigntool ? "-Digest" : "-Digest-Manifest"; |
| 462 |
if (!verify(attributes, digestAttribute, manifestBytes, 0, manifestBytes.length, false, false)) { |
| 463 |
Iterator<Map.Entry<String, Attributes>> it = entries.entrySet().iterator(); |
| 464 |
while (it.hasNext()) { |
| 465 |
Map.Entry<String, Attributes> entry = it.next(); |
| 466 |
StrictJarManifest.Chunk chunk = manifest.getChunk(entry.getKey()); |
| 467 |
if (chunk == null) { |
| 468 |
return; |
| 469 |
} |
| 470 |
if (!verify(entry.getValue(), "-Digest", manifestBytes, |
| 471 |
chunk.start, chunk.end, createdBySigntool, false)) { |
| 472 |
throw invalidDigest(signatureFile, entry.getKey(), jarName); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
metaEntries.put(signatureFile, null); |
| 477 |
signatures.put(signatureFile, entries); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
boolean isSignedJar() { |
| 488 |
return certificates.size() > 0; |
| 489 |
} |
| 490 |
|
| 491 |
private boolean verify(Attributes attributes, String entry, byte[] data, |
| 492 |
int start, int end, boolean ignoreSecondEndline, boolean ignorable) { |
| 493 |
for (int i = 0; i < DIGEST_ALGORITHMS.length; i++) { |
| 494 |
String algorithm = DIGEST_ALGORITHMS[i]; |
| 495 |
String hash = attributes.getValue(algorithm + entry); |
| 496 |
if (hash == null) { |
| 497 |
continue; |
| 498 |
} |
| 499 |
|
| 500 |
MessageDigest md; |
| 501 |
try { |
| 502 |
md = MessageDigest.getInstance(algorithm); |
| 503 |
} catch (NoSuchAlgorithmException e) { |
| 504 |
continue; |
| 505 |
} |
| 506 |
if (ignoreSecondEndline && data[end - 1] == '\n' && data[end - 2] == '\n') { |
| 507 |
md.update(data, start, end - 1 - start); |
| 508 |
} else { |
| 509 |
md.update(data, start, end - start); |
| 510 |
} |
| 511 |
byte[] b = md.digest(); |
| 512 |
byte[] encodedHashBytes = hash.getBytes(StandardCharsets.ISO_8859_1); |
| 513 |
return verifyMessageDigest(b, encodedHashBytes); |
| 514 |
} |
| 515 |
return ignorable; |
| 516 |
} |
| 517 |
|
| 518 |
private static boolean verifyMessageDigest(byte[] expected, byte[] encodedActual) { |
| 519 |
byte[] actual; |
| 520 |
try { |
| 521 |
actual = java.util.Base64.getDecoder().decode(encodedActual); |
| 522 |
} catch (IllegalArgumentException e) { |
| 523 |
return false; |
| 524 |
} |
| 525 |
return MessageDigest.isEqual(expected, actual); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
Certificate[][] getCertificateChains(String name) { |
| 538 |
return verifiedEntries.get(name); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
void removeMetaEntries() { |
| 546 |
metaEntries.clear(); |
| 547 |
} |
| 548 |
} |