From e3877dcffb8bd8e24a03d44bd39cded2750825c3 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 1 Dec 2018 14:00:50 +0700 Subject: [PATCH] Refactor license parser --- source/net/filebot/License.java | 18 ++++++++-------- source/net/filebot/util/PGP.java | 35 ++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/source/net/filebot/License.java b/source/net/filebot/License.java index 87e3ff13..ca7ce1c7 100644 --- a/source/net/filebot/License.java +++ b/source/net/filebot/License.java @@ -1,5 +1,6 @@ package net.filebot; +import static java.nio.charset.StandardCharsets.*; import static java.util.stream.Collectors.*; import static net.filebot.Settings.*; import static net.filebot.util.JsonUtilities.*; @@ -20,8 +21,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; -import org.apache.commons.io.IOUtils; - import net.filebot.util.SystemProperty; import net.filebot.web.WebRequest; @@ -88,10 +87,10 @@ public class License { } // read and verify license file - byte[] bytes = Files.readAllBytes(file.toPath()); + String psm = findClearSignMessage(file); // verify and get clear signed content - Map properties = getProperties(bytes); + Map properties = getProperties(psm); String product = properties.get("Product"); String id = properties.get("Order"); @@ -101,7 +100,7 @@ public class License { checkExpirationDate(expires); // verify license online - verifyLicense(id, bytes); + verifyLicense(id, psm); return new License(product, id, expires); } catch (Exception error) { @@ -109,9 +108,8 @@ public class License { } } - private static Map getProperties(byte[] bytes) throws Exception { - byte[] pub = IOUtils.toByteArray(License.class.getResource("license.key")); - String msg = verifyClearSignMessage(bytes, pub); + private static Map getProperties(String psm) throws Exception { + String msg = verifyClearSignMessage(psm, License.class.getResourceAsStream("license.key")); return NEWLINE.splitAsStream(msg).map(s -> s.split(": ", 2)).collect(toMap(a -> a[0], a -> a[1])); } @@ -130,9 +128,9 @@ public class License { } } - private static void verifyLicense(String id, byte[] bytes) throws Exception { + private static void verifyLicense(String id, String psm) throws Exception { Cache cache = CacheManager.getInstance().getCache("license", CacheType.Persistent); - Object json = cache.json(id, i -> new URL("https://license.filebot.net/verify/" + i)).fetch((url, modified) -> WebRequest.post(url, bytes, "application/octet-stream", getRequestParameters())).expire(Cache.ONE_MONTH).get(); + Object json = cache.json(id, i -> new URL("https://license.filebot.net/verify/" + i)).fetch((url, modified) -> WebRequest.post(url, psm.getBytes(UTF_8), "application/octet-stream", getRequestParameters())).expire(Cache.ONE_MONTH).get(); if (getInteger(json, "status") != 200) { throw new IllegalStateException(getString(json, "message")); diff --git a/source/net/filebot/util/PGP.java b/source/net/filebot/util/PGP.java index 1d7e860b..e644efa6 100644 --- a/source/net/filebot/util/PGP.java +++ b/source/net/filebot/util/PGP.java @@ -5,6 +5,15 @@ import static java.util.stream.Collectors.*; import static net.filebot.util.RegularExpressions.*; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; +import java.util.Scanner; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.openpgp.PGPException; @@ -18,8 +27,8 @@ import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; public class PGP { - public static String verifyClearSignMessage(byte[] psm, byte[] pub) throws Exception { - ArmoredInputStream armoredInput = new ArmoredInputStream(new ByteArrayInputStream(psm)); + public static String verifyClearSignMessage(String psm, InputStream pub) throws Exception { + ArmoredInputStream armoredInput = new ArmoredInputStream(new ByteArrayInputStream(psm.getBytes(UTF_8))); // read content ByteBufferOutputStream content = new ByteBufferOutputStream(256); @@ -51,4 +60,26 @@ public class PGP { return clearSignMessage; } + private static final Pattern PGP_SIGNED_MESSAGE = Pattern.compile("-----BEGIN PGP SIGNED MESSAGE-----(.*?)-----END PGP SIGNATURE-----", Pattern.MULTILINE | Pattern.DOTALL); + + public static String findClearSignMessage(CharSequence content) { + Matcher matcher = PGP_SIGNED_MESSAGE.matcher(content); + if (matcher.find()) { + return matcher.group(); + } + + throw new IllegalArgumentException("PGP SIGNED MESSAGE not found"); + } + + public static String findClearSignMessage(File file) throws IOException { + try (Scanner scanner = new Scanner(new FileInputStream(file), UTF_8)) { + Optional match = scanner.findAll(PGP_SIGNED_MESSAGE).findFirst(); + if (match.isPresent()) { + return match.get().group(); + } + } + + throw new IllegalArgumentException("PGP SIGNED MESSAGE not found"); + } + }