Skip to content

Commit

Permalink
Print apk V2 certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Apr 23, 2024
1 parent f538e96 commit 05bb63f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private void print(ApkModule apkModule) throws IOException {

printResources(apkModule);
printDex(apkModule);
printSignatures(apkModule);
}
private void printSourceFile() throws IOException {
InfoOptions options = getOptions();
Expand Down Expand Up @@ -126,6 +127,14 @@ private void printDex(ApkModule apkModule) throws IOException {
DexDirectory dexDirectory = DexDirectory.readStrings(apkModule.getZipEntryMap());
infoWriter.writeDexInfo(dexDirectory);
}
private void printSignatures(ApkModule apkModule) throws IOException {
InfoOptions options = getOptions();
if(!options.signatures && !options.signatures_base64){
return;
}
InfoWriter infoWriter = getInfoWriter();
infoWriter.writeSignatureInfo(apkModule.getApkSignatureBlock(), options.signatures_base64);
}
private void printResList(ApkModule apkModule) throws IOException {
InfoOptions options = getOptions();
if(options.resList.size() == 0){
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ public class InfoOptions extends Options {
public boolean resources = false;
public final List<String> typeFilterList;
public boolean dex = false;
public boolean signatures = false;
public boolean signatures_base64 = false;

public InfoOptions(){
super();
this.resList = new ArrayList<>();
this.typeFilterList = new ArrayList<>();
}

@Override
public void parse(String[] args) throws ARGException {
parseInput(args);
Expand Down Expand Up @@ -77,6 +81,8 @@ public void parse(String[] args) throws ARGException {
activities = containsArg(ARG_activities, args, activities);
resources = containsArg(ARG_resources, args, false);
dex = containsArg(ARG_dex, args, false);
signatures = containsArg(ARG_signatures, args, false);
signatures_base64 = containsArg(ARG_signatures_base64, args, false);



Expand Down Expand Up @@ -184,6 +190,8 @@ public static String getHelp(){
new String[]{ARG_resources, ARG_DESC_resources},
new String[]{" ", " "},
new String[]{ARG_dex, ARG_DESC_dex},
new String[]{ARG_signatures, ARG_DESC_signatures},
new String[]{ARG_signatures_base64, ARG_DESC_signatures_base64},
new String[]{" ", " "},
new String[]{ARG_ALL_help, ARG_DESC_help}
};
Expand Down Expand Up @@ -274,6 +282,12 @@ public static String getHelp(){
private static final String ARG_dex = "-dex";
private static final String ARG_DESC_dex = "Prints dex information";

private static final String ARG_signatures = "-signatures";
private static final String ARG_DESC_signatures = "Prints signature information";

private static final String ARG_signatures_base64 = "-signatures-base64";
private static final String ARG_DESC_signatures_base64 = "Prints signature information with base64 certificates";


private static final String[] availableTypes = new String[]{TYPE_TEXT, TYPE_JSON, TYPE_XML};
}
15 changes: 15 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.reandroid.apkeditor.info;

import com.reandroid.archive.block.ApkSignatureBlock;
import com.reandroid.archive.block.CertificateBlock;
import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.coder.ValueCoder;
import com.reandroid.arsc.container.SpecTypePair;
Expand All @@ -23,6 +25,7 @@
import com.reandroid.dex.model.DexFile;
import com.reandroid.utils.HexUtil;
import com.reandroid.arsc.value.*;
import com.reandroid.utils.collection.CollectionUtil;

import java.io.Closeable;
import java.io.IOException;
Expand All @@ -35,6 +38,13 @@ public InfoWriter(Writer writer){
this.writer = writer;
}

public void writeSignatureInfo(ApkSignatureBlock signatureBlock, boolean base64) throws IOException {
if(signatureBlock == null){
writeNameValue("certificates", "null");
}else {
writeCertificates(CollectionUtil.toList(signatureBlock.getCertificates()), base64);
}
}
public void writeResources(PackageBlock packageBlock, List<String> typeFilters, boolean writeEntries) throws IOException {
Iterator<ResourceEntry> itr = packageBlock.iterator();
while (itr.hasNext()){
Expand All @@ -47,6 +57,8 @@ public void writeDexInfo(DexDirectory dexDirectory) throws IOException {
writeDexInfo(dexFile, true);
}
}

public abstract void writeCertificates(List<CertificateBlock> certificateList, boolean base64) throws IOException;
public abstract void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException;
public abstract void writeResources(ResourceEntry resourceEntry, boolean writeEntries) throws IOException;
public abstract void writePackageNames(Collection<PackageBlock> packageBlocks) throws IOException;
Expand Down Expand Up @@ -98,6 +110,9 @@ static String getValueAsString(Value value){
}
return HexUtil.toHex8("0x", value.getData());
}
static String toBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}

static final String TAG_RES_PACKAGES = "resource-packages";
static final String TAG_PUBLIC = "public";
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriterJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.reandroid.apkeditor.info;

import com.reandroid.archive.block.CertificateBlock;
import com.reandroid.arsc.array.ResValueMapArray;
import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.container.SpecTypePair;
Expand All @@ -25,6 +26,7 @@
import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.dex.model.DexFile;
import com.reandroid.dex.sections.Marker;
import com.reandroid.json.JSONObject;
import com.reandroid.json.JSONWriter;

import java.io.IOException;
Expand All @@ -43,6 +45,20 @@ public InfoWriterJson(Writer writer) {
this.mJsonWriter = jsonWriter;
}

@Override
public void writeCertificates(List<CertificateBlock> certificateList, boolean base64) throws IOException {
JSONWriter jsonWriter = mJsonWriter.object()
.key("certificates").array();
for(CertificateBlock certificateBlock : certificateList){
JSONObject jsonObject = certificateBlock.toJson();
if(base64){
jsonObject.put("base64", toBase64(certificateBlock.getCertificateBytes()));
}
jsonWriter.value(jsonObject);
}
jsonWriter.endArray().endObject();
}

@Override
public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException {
JSONWriter jsonWriter = mJsonWriter.object()
Expand Down
31 changes: 29 additions & 2 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriterText.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.reandroid.apkeditor.info;

import com.reandroid.archive.block.CertificateBlock;
import com.reandroid.arsc.array.ResValueMapArray;
import com.reandroid.arsc.chunk.PackageBlock;
import com.reandroid.arsc.container.SpecTypePair;
Expand All @@ -28,19 +29,36 @@
import com.reandroid.dex.sections.MapList;
import com.reandroid.dex.sections.Marker;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.StringsUtil;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class InfoWriterText extends InfoWriter{
public class InfoWriterText extends InfoWriter {

public InfoWriterText(Writer writer) {
super(writer);
}


@Override
public void writeCertificates(List<CertificateBlock> certificateList, boolean base64) throws IOException {
Writer writer = getWriter();
writer.write("\n");
writeNameValue("Certificates", certificateList.size());
for(CertificateBlock certificateBlock : certificateList) {
writeWithTab(writer, ARRAY_TAB, certificateBlock.printCertificate());
if(base64) {
writer.write(ARRAY_TAB);
writer.write("Base64: ");
writer.write(toBase64(certificateBlock.getCertificateBytes()));
writer.write("\n");
}
}
flush();
}
@Override
public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException {
Writer writer = getWriter();
Expand Down Expand Up @@ -276,6 +294,15 @@ public void flush() throws IOException {
writer.flush();
}


private void writeWithTab(Writer writer, String tab, String value) throws IOException {
String[] splits = StringsUtil.split(value, '\n');
for(String line : splits){
writer.write(tab);
writer.write(line.trim());
writer.write("\n");
}
}
private static int getDecimalPlaces(int max){
int i = 0;
while (max != 0){
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriterXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package com.reandroid.apkeditor.info;

import com.reandroid.archive.block.CertificateBlock;
import com.reandroid.dex.model.DexFile;
import com.reandroid.dex.sections.MapItem;
import com.reandroid.dex.sections.MapList;
import com.reandroid.dex.sections.Marker;
import com.reandroid.utils.collection.ComputeList;
import com.reandroid.xml.kxml2.KXmlSerializer;
import com.reandroid.arsc.array.ResValueMapArray;
import com.reandroid.arsc.chunk.PackageBlock;
Expand All @@ -45,6 +47,17 @@ public InfoWriterXml(Writer writer) {
super(writer);
}

@Override
public void writeCertificates(List<CertificateBlock> certificateList, boolean base64) throws IOException {
List<String> infoList = new ComputeList<>(certificateList, certificateBlock -> {
String value = certificateBlock.printCertificate();
if(base64) {
value = value + "\n" + toBase64(certificateBlock.getCertificateBytes());
}
return value;
});
writeArray("certificates", infoList.toArray());
}
@Override
public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException {
KXmlSerializer serializer = getSerializer();
Expand Down

0 comments on commit 05bb63f

Please sign in to comment.