diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml deleted file mode 100644 index b7fd1c3..0000000 --- a/dependency-reduced-pom.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - 4.0.0 - eu.m724 - realweather - 0.9-SNAPSHOT - - - - true - src/main/resources - - - - - maven-shade-plugin - 3.6.0 - - - package - - shade - - - - - eu.m724:wtapi - org.java-websocket:Java-WebSocket - - - - - eu.m724:* - - META-INF/MANIFEST.MF - - - - true - - - - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - 724rocks - https://git.724.rocks/api/packages/Minecon724/maven - - - - - org.spigotmc - spigot-api - 1.20.6-R0.1-SNAPSHOT - provided - - - - 17 - 17 - - diff --git a/pom.xml b/pom.xml index cd79c05..3b42290 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,9 @@ 17 17 + ${project.basedir}/testkeystore + testkey + 123456 @@ -68,10 +71,35 @@ true + false + + org.apache.maven.plugins + maven-jarsigner-plugin + 3.0.0 + + + sign + + sign + + + + verify + + verify + + + + + ${jarsigner.keystore} + ${jarsigner.alias} + ${jarsigner.storepass} + + diff --git a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java index a77b301..0b5f6e5 100644 --- a/src/main/java/eu/m724/realweather/RealWeatherPlugin.java +++ b/src/main/java/eu/m724/realweather/RealWeatherPlugin.java @@ -17,6 +17,7 @@ import eu.m724.realweather.commands.LocalTimeCommand; import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.MapperConfig; import eu.m724.realweather.object.UserException; +import eu.m724.realweather.sign.SignatureValidator; import eu.m724.realweather.thunder.ThunderConfig; import eu.m724.realweather.thunder.ThunderMaster; import eu.m724.realweather.time.TimeConfig; @@ -36,6 +37,17 @@ public class RealWeatherPlugin extends JavaPlugin { public void onEnable() { logger = getLogger(); + // TODO remove these lines + SignatureValidator signatureValidator = new SignatureValidator(this); + logger.info("Signature of this JAR: " + signatureValidator.getCertificate().getSubjectX500Principal().getName()); + + if (!signatureValidator.isValid()) { + logger.severe("Key is not valid"); + getServer().getPluginManager().disablePlugin(this); + return; + } + // TODO remove those lines + File dataFolder = getDataFolder(); File modulesFolder = new File("modules"); modulesFolder.mkdir(); diff --git a/src/main/java/eu/m724/realweather/sign/SignatureValidator.java b/src/main/java/eu/m724/realweather/sign/SignatureValidator.java new file mode 100644 index 0000000..3572c1c --- /dev/null +++ b/src/main/java/eu/m724/realweather/sign/SignatureValidator.java @@ -0,0 +1,40 @@ +package eu.m724.realweather.sign; + +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.cert.X509Certificate; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +import eu.m724.realweather.RealWeatherPlugin; + +// TODO rework this for updater +public class SignatureValidator { + public RealWeatherPlugin plugin; + public static final String encodedPublicKey = "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAptv/9qIJXrs/4T1MOkY1QPU/TuLsyCsJdoA2PIO1qdS3nRJBPgkRf2sK6nG1VhOaUHLXoj8lZtQQLcY76CNLqFGFmimo7RDnJjHpxHUzI9pKZJOQ9sEVCDFtoLQiit23t6MAO7GBjJXMNFLonxyay6pTABJo3VYyjg2bE4kd1wjg73RPMQY+zykaRQBUE167PAVkmuYxJK680EYmZph9kQTS12autU2qGFTvsPbmmdhtF7Xy8u84CtEucgRT9HSh0y8MuC0esMGhZtB9gsWcGET763DHtArEMekBnjByb3k+gGiG0Y1K9ygBn+nNVKP66KJGCWFuno8xy+LNiZKX4pUnrJcTyLvZg7PvjdZTye54PKkAAOACAbcFBiat38Zes5ZOKZIBEjC2IXbhfySoOn5WAk+XPsm3gVlgO9d51iOVDDBx5MCqq802lOyIGog1BlbhnGZ2+cSvFo7ZWpF0f93uG5UKBqRF+Q9cPA36SMUAoQ2DWFEZOYXwFgCXxVvFAgMBAAE="; + + public SignatureValidator(RealWeatherPlugin plugin) { + this.plugin = plugin; + } + + public X509Certificate getCertificate() { + return (X509Certificate) plugin.getClass().getProtectionDomain().getCodeSource().getCertificates()[0]; + } + + public boolean isValid() { + PublicKey currentPublicKey = getCertificate().getPublicKey(); + PublicKey expectedPublicKey = null; + + try { + X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getDecoder().decode(encodedPublicKey)); + expectedPublicKey = KeyFactory.getInstance("RSA").generatePublic(spec); + } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return expectedPublicKey.equals(currentPublicKey); + } +} diff --git a/testkeystore b/testkeystore new file mode 100644 index 0000000..a831c45 Binary files /dev/null and b/testkeystore differ