| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util.apk; |
| 18 |
|
| 19 |
import static android.util.apk.ApkSigningBlockUtils.CONTENT_DIGEST_VERITY_CHUNKED_SHA256; |
| 20 |
import static android.util.apk.ApkSigningBlockUtils.compareSignatureAlgorithm; |
| 21 |
import static android.util.apk.ApkSigningBlockUtils.getContentDigestAlgorithmJcaDigestAlgorithm; |
| 22 |
import static android.util.apk.ApkSigningBlockUtils.getLengthPrefixedSlice; |
| 23 |
import static android.util.apk.ApkSigningBlockUtils.getSignatureAlgorithmContentDigestAlgorithm; |
| 24 |
import static android.util.apk.ApkSigningBlockUtils.getSignatureAlgorithmJcaKeyAlgorithm; |
| 25 |
import static android.util.apk.ApkSigningBlockUtils.getSignatureAlgorithmJcaSignatureAlgorithm; |
| 26 |
import static android.util.apk.ApkSigningBlockUtils.isSupportedSignatureAlgorithm; |
| 27 |
import static android.util.apk.ApkSigningBlockUtils.readLengthPrefixedByteArray; |
| 28 |
|
| 29 |
import android.util.ArrayMap; |
| 30 |
import android.util.Pair; |
| 31 |
|
| 32 |
import java.io.ByteArrayInputStream; |
| 33 |
import java.io.IOException; |
| 34 |
import java.io.RandomAccessFile; |
| 35 |
import java.nio.BufferUnderflowException; |
| 36 |
import java.nio.ByteBuffer; |
| 37 |
import java.security.DigestException; |
| 38 |
import java.security.InvalidAlgorithmParameterException; |
| 39 |
import java.security.InvalidKeyException; |
| 40 |
import java.security.KeyFactory; |
| 41 |
import java.security.MessageDigest; |
| 42 |
import java.security.NoSuchAlgorithmException; |
| 43 |
import java.security.PublicKey; |
| 44 |
import java.security.Signature; |
| 45 |
import java.security.SignatureException; |
| 46 |
import java.security.cert.CertificateException; |
| 47 |
import java.security.cert.CertificateFactory; |
| 48 |
import java.security.cert.X509Certificate; |
| 49 |
import java.security.spec.AlgorithmParameterSpec; |
| 50 |
import java.security.spec.InvalidKeySpecException; |
| 51 |
import java.security.spec.X509EncodedKeySpec; |
| 52 |
import java.util.ArrayList; |
| 53 |
import java.util.Arrays; |
| 54 |
import java.util.List; |
| 55 |
import java.util.Map; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public class ApkSignatureSchemeV2Verifier { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static final int SF_ATTRIBUTE_ANDROID_APK_SIGNED_ID = 2; |
| 74 |
|
| 75 |
private static final int APK_SIGNATURE_SCHEME_V2_BLOCK_ID = 0x7109871a; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static boolean hasSignature(String apkFile) throws IOException { |
| 83 |
try (RandomAccessFile apk = new RandomAccessFile(apkFile, "r")) { |
| 84 |
findSignature(apk); |
| 85 |
return true; |
| 86 |
} catch (SignatureNotFoundException e) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static X509Certificate[][] verify(String apkFile) |
| 101 |
throws SignatureNotFoundException, SecurityException, IOException { |
| 102 |
VerifiedSigner vSigner = verify(apkFile, true); |
| 103 |
return vSigner.certs; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static X509Certificate[][] unsafeGetCertsWithoutVerification(String apkFile) |
| 116 |
throws SignatureNotFoundException, SecurityException, IOException { |
| 117 |
VerifiedSigner vSigner = verify(apkFile, false); |
| 118 |
return vSigner.certs; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public static VerifiedSigner verify(String apkFile, boolean verifyIntegrity) |
| 125 |
throws SignatureNotFoundException, SecurityException, IOException { |
| 126 |
try (RandomAccessFile apk = new RandomAccessFile(apkFile, "r")) { |
| 127 |
return verify(apk, verifyIntegrity); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private static VerifiedSigner verify(RandomAccessFile apk, boolean verifyIntegrity) |
| 141 |
throws SignatureNotFoundException, SecurityException, IOException { |
| 142 |
SignatureInfo signatureInfo = findSignature(apk); |
| 143 |
return verify(apk, signatureInfo, verifyIntegrity); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static SignatureInfo findSignature(RandomAccessFile apk) |
| 154 |
throws IOException, SignatureNotFoundException { |
| 155 |
return ApkSigningBlockUtils.findSignature(apk, APK_SIGNATURE_SCHEME_V2_BLOCK_ID); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private static VerifiedSigner verify( |
| 166 |
RandomAccessFile apk, |
| 167 |
SignatureInfo signatureInfo, |
| 168 |
boolean doVerifyIntegrity) throws SecurityException, IOException { |
| 169 |
int signerCount = 0; |
| 170 |
Map<Integer, byte[]> contentDigests = new ArrayMap<>(); |
| 171 |
List<X509Certificate[]> signerCerts = new ArrayList<>(); |
| 172 |
CertificateFactory certFactory; |
| 173 |
try { |
| 174 |
certFactory = CertificateFactory.getInstance("X.509"); |
| 175 |
} catch (CertificateException e) { |
| 176 |
throw new RuntimeException("Failed to obtain X.509 CertificateFactory", e); |
| 177 |
} |
| 178 |
ByteBuffer signers; |
| 179 |
try { |
| 180 |
signers = getLengthPrefixedSlice(signatureInfo.signatureBlock); |
| 181 |
} catch (IOException e) { |
| 182 |
throw new SecurityException("Failed to read list of signers", e); |
| 183 |
} |
| 184 |
while (signers.hasRemaining()) { |
| 185 |
signerCount++; |
| 186 |
try { |
| 187 |
ByteBuffer signer = getLengthPrefixedSlice(signers); |
| 188 |
X509Certificate[] certs = verifySigner(signer, contentDigests, certFactory); |
| 189 |
signerCerts.add(certs); |
| 190 |
} catch (IOException | BufferUnderflowException | SecurityException e) { |
| 191 |
throw new SecurityException( |
| 192 |
"Failed to parse/verify signer #" + signerCount + " block", |
| 193 |
e); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if (signerCount < 1) { |
| 198 |
throw new SecurityException("No signers found"); |
| 199 |
} |
| 200 |
|
| 201 |
if (contentDigests.isEmpty()) { |
| 202 |
throw new SecurityException("No content digests found"); |
| 203 |
} |
| 204 |
|
| 205 |
if (doVerifyIntegrity) { |
| 206 |
ApkSigningBlockUtils.verifyIntegrity(contentDigests, apk, signatureInfo); |
| 207 |
} |
| 208 |
|
| 209 |
byte[] verityRootHash = null; |
| 210 |
if (contentDigests.containsKey(CONTENT_DIGEST_VERITY_CHUNKED_SHA256)) { |
| 211 |
byte[] verityDigest = contentDigests.get(CONTENT_DIGEST_VERITY_CHUNKED_SHA256); |
| 212 |
verityRootHash = ApkSigningBlockUtils.parseVerityDigestAndVerifySourceLength( |
| 213 |
verityDigest, apk.getChannel().size(), signatureInfo); |
| 214 |
} |
| 215 |
|
| 216 |
return new VerifiedSigner( |
| 217 |
signerCerts.toArray(new X509Certificate[signerCerts.size()][]), |
| 218 |
verityRootHash, contentDigests); |
| 219 |
} |
| 220 |
|
| 221 |
private static X509Certificate[] verifySigner( |
| 222 |
ByteBuffer signerBlock, |
| 223 |
Map<Integer, byte[]> contentDigests, |
| 224 |
CertificateFactory certFactory) throws SecurityException, IOException { |
| 225 |
ByteBuffer signedData = getLengthPrefixedSlice(signerBlock); |
| 226 |
ByteBuffer signatures = getLengthPrefixedSlice(signerBlock); |
| 227 |
byte[] publicKeyBytes = readLengthPrefixedByteArray(signerBlock); |
| 228 |
|
| 229 |
int signatureCount = 0; |
| 230 |
int bestSigAlgorithm = -1; |
| 231 |
byte[] bestSigAlgorithmSignatureBytes = null; |
| 232 |
List<Integer> signaturesSigAlgorithms = new ArrayList<>(); |
| 233 |
while (signatures.hasRemaining()) { |
| 234 |
signatureCount++; |
| 235 |
try { |
| 236 |
ByteBuffer signature = getLengthPrefixedSlice(signatures); |
| 237 |
if (signature.remaining() < 8) { |
| 238 |
throw new SecurityException("Signature record too short"); |
| 239 |
} |
| 240 |
int sigAlgorithm = signature.getInt(); |
| 241 |
signaturesSigAlgorithms.add(sigAlgorithm); |
| 242 |
if (!isSupportedSignatureAlgorithm(sigAlgorithm)) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
if ((bestSigAlgorithm == -1) |
| 246 |
|| (compareSignatureAlgorithm(sigAlgorithm, bestSigAlgorithm) > 0)) { |
| 247 |
bestSigAlgorithm = sigAlgorithm; |
| 248 |
bestSigAlgorithmSignatureBytes = readLengthPrefixedByteArray(signature); |
| 249 |
} |
| 250 |
} catch (IOException | BufferUnderflowException e) { |
| 251 |
throw new SecurityException( |
| 252 |
"Failed to parse signature record #" + signatureCount, |
| 253 |
e); |
| 254 |
} |
| 255 |
} |
| 256 |
if (bestSigAlgorithm == -1) { |
| 257 |
if (signatureCount == 0) { |
| 258 |
throw new SecurityException("No signatures found"); |
| 259 |
} else { |
| 260 |
throw new SecurityException("No supported signatures found"); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
String keyAlgorithm = getSignatureAlgorithmJcaKeyAlgorithm(bestSigAlgorithm); |
| 265 |
Pair<String, ? extends AlgorithmParameterSpec> signatureAlgorithmParams = |
| 266 |
getSignatureAlgorithmJcaSignatureAlgorithm(bestSigAlgorithm); |
| 267 |
String jcaSignatureAlgorithm = signatureAlgorithmParams.first; |
| 268 |
AlgorithmParameterSpec jcaSignatureAlgorithmParams = signatureAlgorithmParams.second; |
| 269 |
boolean sigVerified; |
| 270 |
try { |
| 271 |
PublicKey publicKey = |
| 272 |
KeyFactory.getInstance(keyAlgorithm) |
| 273 |
.generatePublic(new X509EncodedKeySpec(publicKeyBytes)); |
| 274 |
Signature sig = Signature.getInstance(jcaSignatureAlgorithm); |
| 275 |
sig.initVerify(publicKey); |
| 276 |
if (jcaSignatureAlgorithmParams != null) { |
| 277 |
sig.setParameter(jcaSignatureAlgorithmParams); |
| 278 |
} |
| 279 |
sig.update(signedData); |
| 280 |
sigVerified = sig.verify(bestSigAlgorithmSignatureBytes); |
| 281 |
} catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException |
| 282 |
| InvalidAlgorithmParameterException | SignatureException e) { |
| 283 |
throw new SecurityException( |
| 284 |
"Failed to verify " + jcaSignatureAlgorithm + " signature", e); |
| 285 |
} |
| 286 |
if (!sigVerified) { |
| 287 |
throw new SecurityException(jcaSignatureAlgorithm + " signature did not verify"); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
byte[] contentDigest = null; |
| 293 |
signedData.clear(); |
| 294 |
ByteBuffer digests = getLengthPrefixedSlice(signedData); |
| 295 |
List<Integer> digestsSigAlgorithms = new ArrayList<>(); |
| 296 |
int digestCount = 0; |
| 297 |
while (digests.hasRemaining()) { |
| 298 |
digestCount++; |
| 299 |
try { |
| 300 |
ByteBuffer digest = getLengthPrefixedSlice(digests); |
| 301 |
if (digest.remaining() < 8) { |
| 302 |
throw new IOException("Record too short"); |
| 303 |
} |
| 304 |
int sigAlgorithm = digest.getInt(); |
| 305 |
digestsSigAlgorithms.add(sigAlgorithm); |
| 306 |
if (sigAlgorithm == bestSigAlgorithm) { |
| 307 |
contentDigest = readLengthPrefixedByteArray(digest); |
| 308 |
} |
| 309 |
} catch (IOException | BufferUnderflowException e) { |
| 310 |
throw new IOException("Failed to parse digest record #" + digestCount, e); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (!signaturesSigAlgorithms.equals(digestsSigAlgorithms)) { |
| 315 |
throw new SecurityException( |
| 316 |
"Signature algorithms don't match between digests and signatures records"); |
| 317 |
} |
| 318 |
int digestAlgorithm = getSignatureAlgorithmContentDigestAlgorithm(bestSigAlgorithm); |
| 319 |
byte[] previousSignerDigest = contentDigests.put(digestAlgorithm, contentDigest); |
| 320 |
if ((previousSignerDigest != null) |
| 321 |
&& (!MessageDigest.isEqual(previousSignerDigest, contentDigest))) { |
| 322 |
throw new SecurityException( |
| 323 |
getContentDigestAlgorithmJcaDigestAlgorithm(digestAlgorithm) |
| 324 |
+ " contents digest does not match the digest specified by a preceding signer"); |
| 325 |
} |
| 326 |
|
| 327 |
ByteBuffer certificates = getLengthPrefixedSlice(signedData); |
| 328 |
List<X509Certificate> certs = new ArrayList<>(); |
| 329 |
int certificateCount = 0; |
| 330 |
while (certificates.hasRemaining()) { |
| 331 |
certificateCount++; |
| 332 |
byte[] encodedCert = readLengthPrefixedByteArray(certificates); |
| 333 |
X509Certificate certificate; |
| 334 |
try { |
| 335 |
certificate = (X509Certificate) |
| 336 |
certFactory.generateCertificate(new ByteArrayInputStream(encodedCert)); |
| 337 |
} catch (CertificateException e) { |
| 338 |
throw new SecurityException("Failed to decode certificate #" + certificateCount, e); |
| 339 |
} |
| 340 |
certificate = new VerbatimX509Certificate(certificate, encodedCert); |
| 341 |
certs.add(certificate); |
| 342 |
} |
| 343 |
|
| 344 |
if (certs.isEmpty()) { |
| 345 |
throw new SecurityException("No certificates listed"); |
| 346 |
} |
| 347 |
X509Certificate mainCertificate = certs.get(0); |
| 348 |
byte[] certificatePublicKeyBytes = mainCertificate.getPublicKey().getEncoded(); |
| 349 |
if (!Arrays.equals(publicKeyBytes, certificatePublicKeyBytes)) { |
| 350 |
throw new SecurityException( |
| 351 |
"Public key mismatch between certificate and signature record"); |
| 352 |
} |
| 353 |
|
| 354 |
ByteBuffer additionalAttrs = getLengthPrefixedSlice(signedData); |
| 355 |
verifyAdditionalAttributes(additionalAttrs); |
| 356 |
|
| 357 |
return certs.toArray(new X509Certificate[certs.size()]); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
private static final int STRIPPING_PROTECTION_ATTR_ID = 0xbeeff00d; |
| 362 |
|
| 363 |
private static void verifyAdditionalAttributes(ByteBuffer attrs) |
| 364 |
throws SecurityException, IOException { |
| 365 |
while (attrs.hasRemaining()) { |
| 366 |
ByteBuffer attr = getLengthPrefixedSlice(attrs); |
| 367 |
if (attr.remaining() < 4) { |
| 368 |
throw new IOException("Remaining buffer too short to contain additional attribute " |
| 369 |
+ "ID. Remaining: " + attr.remaining()); |
| 370 |
} |
| 371 |
int id = attr.getInt(); |
| 372 |
switch (id) { |
| 373 |
case STRIPPING_PROTECTION_ATTR_ID: |
| 374 |
if (attr.remaining() < 4) { |
| 375 |
throw new IOException("V2 Signature Scheme Stripping Protection Attribute " |
| 376 |
+ " value too small. Expected 4 bytes, but found " |
| 377 |
+ attr.remaining()); |
| 378 |
} |
| 379 |
int vers = attr.getInt(); |
| 380 |
if (vers == ApkSignatureSchemeV3Verifier.SF_ATTRIBUTE_ANDROID_APK_SIGNED_ID) { |
| 381 |
throw new SecurityException("V2 signature indicates APK is signed using APK" |
| 382 |
+ " Signature Scheme v3, but none was found. Signature stripped?"); |
| 383 |
} |
| 384 |
break; |
| 385 |
default: |
| 386 |
|
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
static byte[] getVerityRootHash(String apkPath) |
| 393 |
throws IOException, SignatureNotFoundException, SecurityException { |
| 394 |
try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) { |
| 395 |
SignatureInfo signatureInfo = findSignature(apk); |
| 396 |
VerifiedSigner vSigner = verify(apk, false); |
| 397 |
return vSigner.verityRootHash; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
static byte[] generateApkVerity(String apkPath, ByteBufferFactory bufferFactory) |
| 402 |
throws IOException, SignatureNotFoundException, SecurityException, DigestException, |
| 403 |
NoSuchAlgorithmException { |
| 404 |
try (RandomAccessFile apk = new RandomAccessFile(apkPath, "r")) { |
| 405 |
SignatureInfo signatureInfo = findSignature(apk); |
| 406 |
return VerityBuilder.generateApkVerity(apkPath, bufferFactory, signatureInfo); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public static class VerifiedSigner { |
| 416 |
public final X509Certificate[][] certs; |
| 417 |
|
| 418 |
public final byte[] verityRootHash; |
| 419 |
|
| 420 |
|
| 421 |
public final Map<Integer, byte[]> contentDigests; |
| 422 |
|
| 423 |
public VerifiedSigner(X509Certificate[][] certs, byte[] verityRootHash, |
| 424 |
Map<Integer, byte[]> contentDigests) { |
| 425 |
this.certs = certs; |
| 426 |
this.verityRootHash = verityRootHash; |
| 427 |
this.contentDigests = contentDigests; |
| 428 |
} |
| 429 |
|
| 430 |
} |
| 431 |
} |