From 6d93831488aea063166edd2ff5517b0ec125ffb7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 20 May 2018 15:24:47 +0800 Subject: [PATCH] Fix zipsigner when using external keys --- build.py | 2 +- utils/build.gradle | 8 +++--- .../topjohnwu/utils/ReusableInputStream.java | 28 +++++++++++++++++++ .../java/com/topjohnwu/utils/SignAPK.java | 17 +++++++++-- .../java/com/topjohnwu/utils/ZipSigner.java | 4 +-- 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 utils/src/main/java/com/topjohnwu/utils/ReusableInputStream.java diff --git a/build.py b/build.py index ae9270a68..858dbb826 100755 --- a/build.py +++ b/build.py @@ -361,7 +361,7 @@ def zip_uninstaller(args): header('Output: ' + output) def sign_adjust_zip(unsigned, output): - signer_name = 'zipsigner-2.1.jar' + signer_name = 'zipsigner-2.2.jar' jarsigner = os.path.join('utils', 'build', 'libs', signer_name) if not os.path.exists(jarsigner): diff --git a/utils/build.gradle b/utils/build.gradle index bc18545a3..a59b5ef49 100644 --- a/utils/build.gradle +++ b/utils/build.gradle @@ -15,7 +15,7 @@ jar { shadowJar { baseName = 'zipsigner' classifier = null - version = 2.1 + version = 2.2 } buildscript { @@ -23,7 +23,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2' + classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4' } } @@ -33,6 +33,6 @@ repositories { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'org.bouncycastle:bcprov-jdk15on:1.58' - implementation 'org.bouncycastle:bcpkix-jdk15on:1.58' + implementation 'org.bouncycastle:bcprov-jdk15on:1.59' + implementation 'org.bouncycastle:bcpkix-jdk15on:1.59' } diff --git a/utils/src/main/java/com/topjohnwu/utils/ReusableInputStream.java b/utils/src/main/java/com/topjohnwu/utils/ReusableInputStream.java new file mode 100644 index 000000000..d4627d24f --- /dev/null +++ b/utils/src/main/java/com/topjohnwu/utils/ReusableInputStream.java @@ -0,0 +1,28 @@ +package com.topjohnwu.utils; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class ReusableInputStream extends BufferedInputStream { + + public ReusableInputStream(InputStream in) { + super(in); + mark(Integer.MAX_VALUE); + } + + public ReusableInputStream(InputStream in, int size) { + super(in, size); + mark(Integer.MAX_VALUE); + } + + @Override + public void close() throws IOException { + /* Reset at close so we can reuse it */ + reset(); + } + + public void destroy() throws IOException { + super.close(); + } +} diff --git a/utils/src/main/java/com/topjohnwu/utils/SignAPK.java b/utils/src/main/java/com/topjohnwu/utils/SignAPK.java index 084421bb2..709efbcbf 100644 --- a/utils/src/main/java/com/topjohnwu/utils/SignAPK.java +++ b/utils/src/main/java/com/topjohnwu/utils/SignAPK.java @@ -18,6 +18,7 @@ import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; import org.bouncycastle.util.encoders.Base64; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -73,19 +74,31 @@ public class SignAPK { JarMap input, OutputStream output) throws Exception { File temp1 = File.createTempFile("signAPK", null); File temp2 = File.createTempFile("signAPK", null); + if (cert == null) { + cert = SignAPK.class.getResourceAsStream("/keys/testkey.x509.pem"); + } + if (key == null) { + key = SignAPK.class.getResourceAsStream("/keys/testkey.pk8"); + } + + ReusableInputStream c = new ReusableInputStream(cert); + ReusableInputStream k = new ReusableInputStream(key); + try { try (OutputStream out = new BufferedOutputStream(new FileOutputStream(temp1))) { - signZip(cert, key, input, out, false); + signZip(c, k, input, out, false); } ZipAdjust.adjust(temp1, temp2); try (JarMap map = new JarMap(temp2, false)) { - signZip(cert, key, map, output, true); + signZip(c, k, map, output, true); } } finally { temp1.delete(); temp2.delete(); + c.destroy(); + k.destroy(); } } diff --git a/utils/src/main/java/com/topjohnwu/utils/ZipSigner.java b/utils/src/main/java/com/topjohnwu/utils/ZipSigner.java index b51afd9cc..869745d37 100644 --- a/utils/src/main/java/com/topjohnwu/utils/ZipSigner.java +++ b/utils/src/main/java/com/topjohnwu/utils/ZipSigner.java @@ -28,8 +28,8 @@ public class ZipSigner { InputStream key = null; if (args.length - argStart == 4) { - cert = new BufferedInputStream(new FileInputStream(new File(args[argStart]))); - key = new BufferedInputStream(new FileInputStream(new File(args[argStart + 1]))); + cert = new FileInputStream(new File(args[argStart])); + key = new FileInputStream(new File(args[argStart + 1])); argStart += 2; }