Skip to content

Commit

Permalink
修复在android7以下的手机上的兼容性问题
Browse files Browse the repository at this point in the history
  • Loading branch information
WindySha committed Feb 27, 2020
1 parent 30d6275 commit 48103b2
Show file tree
Hide file tree
Showing 13 changed files with 1,111 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -436,12 +437,17 @@ public Set<String> initWith(byte[] manifestBytes, Set<String> entryNames) {
if (V1SchemeSigner.isJarEntryDigestNeededInManifest(entry.getKey()) &&
isDebuggable(entryName)) {

Optional<V1SchemeVerifier.NamedDigest> extractedDigest =
V1SchemeVerifier.getDigestsToVerify(
entry.getValue(), "-Digest", mMinSdkVersion, Integer.MAX_VALUE)
.stream()
.filter(d -> d.jcaDigestAlgorithm == alg)
.findFirst();
Optional<V1SchemeVerifier.NamedDigest> extractedDigest = Optional.empty();

Collection<V1SchemeVerifier.NamedDigest> collection = V1SchemeVerifier.getDigestsToVerify(
entry.getValue(), "-Digest", mMinSdkVersion, Integer.MAX_VALUE);

for (V1SchemeVerifier.NamedDigest d : collection) {
if (d.jcaDigestAlgorithm == alg) {
extractedDigest = Optional.of(d);
break;
}
}

extractedDigest.ifPresent(
namedDigest -> mOutputJarEntryDigests.put(entryName, namedDigest.digest));
Expand Down
14 changes: 14 additions & 0 deletions apksigner/src/main/java/com/android/apksig/internal/Supplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.android.apksig.internal;

/**
* @author Windysha
*/
public interface Supplier<T> {

/**
* Gets a result.
*
* @return a result
*/
T get();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.android.apksig.apk.ApkFormatException;
import com.android.apksig.apk.ApkSigningBlockNotFoundException;
import com.android.apksig.apk.ApkUtils;
import com.android.apksig.internal.Supplier;
import com.android.apksig.internal.asn1.Asn1BerParser;
import com.android.apksig.internal.asn1.Asn1DecodingException;
import com.android.apksig.internal.asn1.Asn1DerEncoder;
Expand Down Expand Up @@ -60,14 +61,15 @@
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class ApkSigningBlockUtils {

Expand Down Expand Up @@ -418,10 +420,22 @@ public static Map<ContentDigestAlgorithm, byte[]> computeContentDigests(
DataSource centralDir,
DataSource eocd) throws IOException, NoSuchAlgorithmException, DigestException {
Map<ContentDigestAlgorithm, byte[]> contentDigests = new HashMap<>();
Set<ContentDigestAlgorithm> oneMbChunkBasedAlgorithm = digestAlgorithms.stream()
.filter(a -> a == ContentDigestAlgorithm.CHUNKED_SHA256 ||
a == ContentDigestAlgorithm.CHUNKED_SHA512)
.collect(Collectors.toSet());
// 不使用stream,以兼容android6以及一下
// Set<ContentDigestAlgorithm> oneMbChunkBasedAlgorithm = digestAlgorithms.stream()
// .filter(a -> a == ContentDigestAlgorithm.CHUNKED_SHA256 ||
// a == ContentDigestAlgorithm.CHUNKED_SHA512)
// .collect(Collectors.toSet());

Set<ContentDigestAlgorithm> oneMbChunkBasedAlgorithm = new HashSet<>();
if (digestAlgorithms != null && digestAlgorithms.size() > 0) {
for (ContentDigestAlgorithm a : digestAlgorithms) {
if (a == ContentDigestAlgorithm.CHUNKED_SHA256 ||
a == ContentDigestAlgorithm.CHUNKED_SHA512) {
oneMbChunkBasedAlgorithm.add(a);
}
}
}

computeOneMbChunkContentDigests(
executor,
oneMbChunkBasedAlgorithm,
Expand Down Expand Up @@ -1150,10 +1164,22 @@ public static List<SupportedSignature> getSignaturesToVerify(
if (bestSigAlgorithmOnSdkVersion.isEmpty()) {
throw new NoSupportedSignaturesException("No supported signature");
}
return bestSigAlgorithmOnSdkVersion.values().stream()
.sorted((sig1, sig2) -> Integer.compare(
sig1.algorithm.getId(), sig2.algorithm.getId()))
.collect(Collectors.toList());

// 不使用stream,以兼容android6以及一下
Collection<SupportedSignature> values = bestSigAlgorithmOnSdkVersion.values();
Comparator cmp = new Comparator<SupportedSignature>() {
@Override
public int compare(SupportedSignature sig1, SupportedSignature sig2) {
return Integer.compare(sig1.algorithm.getId(), sig2.algorithm.getId());
}
};
Collections.sort((List<SupportedSignature>) values, cmp);

return (List<SupportedSignature>) values;
// return bestSigAlgorithmOnSdkVersion.values().stream()
// .sorted((sig1, sig2) -> Integer.compare(
// sig1.algorithm.getId(), sig2.algorithm.getId()))
// .collect(Collectors.toList());
}

public static class NoSupportedSignaturesException extends Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Base64;
import com.android.apksig.internal.util.Base64;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ private static <T> List<T> parseSetOf(BerDataValue container, Class<T> elementCl

private static Asn1Type getContainerAsn1Type(Class<?> containerClass)
throws Asn1DecodingException {
Asn1Class containerAnnotation = containerClass.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = containerClass.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = containerClass.getAnnotation(Asn1Class.class);
}
if (containerAnnotation == null) {
throw new Asn1DecodingException(
containerClass.getName() + " is not annotated with "
Expand Down Expand Up @@ -531,7 +536,12 @@ private static List<AnnotatedField> getAnnotatedFields(Class<?> containerClass)
Field[] declaredFields = containerClass.getDeclaredFields();
List<AnnotatedField> result = new ArrayList<>(declaredFields.length);
for (Field field : declaredFields) {
Asn1Field annotation = field.getDeclaredAnnotation(Asn1Field.class);
Asn1Field annotation;
try {
annotation = containerClass.getDeclaredAnnotation(Asn1Field.class);
} catch (Throwable e) {
annotation = containerClass.getAnnotation(Asn1Field.class);
}
if (annotation == null) {
continue;
}
Expand Down Expand Up @@ -645,8 +655,12 @@ public static <T> T convert(
break;
case SEQUENCE:
{
Asn1Class containerAnnotation =
targetType.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = targetType.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = targetType.getAnnotation(Asn1Class.class);
}
if ((containerAnnotation != null)
&& (containerAnnotation.type() == Asn1Type.SEQUENCE)) {
return parseSequence(dataValue, targetType);
Expand All @@ -655,8 +669,12 @@ public static <T> T convert(
}
case CHOICE:
{
Asn1Class containerAnnotation =
targetType.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = targetType.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = targetType.getAnnotation(Asn1Class.class);
}
if ((containerAnnotation != null)
&& (containerAnnotation.type() == Asn1Type.CHOICE)) {
return parseChoice(dataValue, targetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ private Asn1DerEncoder() {}
*/
public static byte[] encode(Object container) throws Asn1EncodingException {
Class<?> containerClass = container.getClass();
Asn1Class containerAnnotation = containerClass.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = containerClass.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = containerClass.getAnnotation(Asn1Class.class);
}
if (containerAnnotation == null) {
throw new Asn1EncodingException(
containerClass.getName() + " not annotated with " + Asn1Class.class.getName());
Expand Down Expand Up @@ -216,7 +221,12 @@ private static List<AnnotatedField> getAnnotatedFields(Object container)
Field[] declaredFields = containerClass.getDeclaredFields();
List<AnnotatedField> result = new ArrayList<>(declaredFields.length);
for (Field field : declaredFields) {
Asn1Field annotation = field.getDeclaredAnnotation(Asn1Field.class);
Asn1Field annotation;
try {
annotation = field.getDeclaredAnnotation(Asn1Field.class);
} catch (Throwable e) {
annotation = field.getAnnotation(Asn1Field.class);
}
if (annotation == null) {
continue;
}
Expand Down Expand Up @@ -560,8 +570,12 @@ public static byte[] toDer(Object source, Asn1Type targetType, Asn1Type targetEl
break;
case SEQUENCE:
{
Asn1Class containerAnnotation =
sourceType.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = sourceType.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = sourceType.getAnnotation(Asn1Class.class);
}
if ((containerAnnotation != null)
&& (containerAnnotation.type() == Asn1Type.SEQUENCE)) {
return toSequence(source);
Expand All @@ -570,8 +584,12 @@ public static byte[] toDer(Object source, Asn1Type targetType, Asn1Type targetEl
}
case CHOICE:
{
Asn1Class containerAnnotation =
sourceType.getDeclaredAnnotation(Asn1Class.class);
Asn1Class containerAnnotation;
try {
containerAnnotation = sourceType.getDeclaredAnnotation(Asn1Class.class);
} catch (Throwable e) {
containerAnnotation = sourceType.getAnnotation(Asn1Class.class);
}
if ((containerAnnotation != null)
&& (containerAnnotation.type() == Asn1Type.CHOICE)) {
return toChoice(source);
Expand Down
Loading

0 comments on commit 48103b2

Please sign in to comment.