Skip to content

Commit

Permalink
Annotated.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed May 19, 2023
1 parent 5ca93ef commit 480affc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/atatctech/hsps/ClassLoaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import java.net.URLClassLoader;

public final class ClassLoaderUtils {
/**
* Get a URLClassLoader from a file.
* @param file target file
* @return a classloader
*/
public static @Nullable ClassLoader getClassLoader(@NotNull File file) {
try {
return new URLClassLoader(new URL[]{file.toURI().toURL()}, ClassLoader.getSystemClassLoader());
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/com/atatctech/hsps/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,40 @@

import java.io.File;

/**
* Plugin loader.
*/
public class Plugin {
protected final @NotNull File file;
protected final @NotNull String mainClass;

/**
* Create a plugin loader.
* @param file target file
* @param mainClass the entry class, such as `com.example.plugin.Plugin`
*/
public Plugin(@NotNull File file, @NotNull String mainClass) {
this.file = file;
this.mainClass = mainClass;
}

/**
* Create a plugin loader.
* @param path path to target file
* @param mainClass the entry class, such as `com.example.plugin.Plugin`
*/
public Plugin(@NotNull String path, @NotNull String mainClass) {
this.file = new File(path);
this.mainClass = mainClass;
}

/**
* Load plugin.
* @param verification verification method, set to null to disable verification
* @return the main class
* @throws ClassNotFoundException main class not found
* @throws VerificationError verification failed
*/
public @Nullable Class<?> load(@Nullable Verification verification) throws ClassNotFoundException, VerificationError {
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(file);
if (classLoader == null) return null;
Expand All @@ -33,7 +53,14 @@ public Plugin(@NotNull String path, @NotNull String mainClass) {
return clz;
}

public @Nullable Class<?> load(@NotNull ARSA.APublicKey publicKey) throws VerificationError, ClassNotFoundException {
/**
* Load plugin with default verification method, which is RSA signature.
* @param publicKey RSA public key
* @return the main class
* @throws ClassNotFoundException main class not found
* @throws VerificationError verification failed
*/
public @Nullable Class<?> load(@NotNull ARSA.APublicKey publicKey) throws ClassNotFoundException, VerificationError {
return load(clz -> {
Signed signed = clz.getAnnotation(Signed.class);
return signed != null && Signature.verify(clz, signed.value(), publicKey);
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/atatctech/hsps/verification/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

public class Signature {
/**
* Signature utils.
*/
public final class Signature {
/**
* Verify a class's signature.
* @param clz target class
* @param signature signature to be verified
* @param publicKey RSA public key
* @return true if verified; false if failed
*/
public static boolean verify(@NotNull Class<?> clz, @NotNull String signature, @NotNull ARSA.APublicKey publicKey) {
try {
return ARSA.verify(clz.getName(), signature, publicKey);
Expand All @@ -16,6 +26,12 @@ public static boolean verify(@NotNull Class<?> clz, @NotNull String signature, @
}
}

/**
* Generate a signature.
* @param clz target class
* @param privateKey RSA private key
* @return generated signature
*/
public static @NotNull String sign(@NotNull Class<?> clz, @NotNull ARSA.APrivateKey privateKey) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
return ARSA.sign(clz.getName(), privateKey);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/atatctech/hsps/verification/Signed.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Use this annotation to state that the main class is signed.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Signed {
/**
* @return signature
*/
@NotNull String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

@FunctionalInterface
public interface Verification {
/**
* Verify the main class.
* @param clz the main class
* @return true if verified; false if failed
*/
boolean verify(@NotNull Class<?> clz);
}

0 comments on commit 480affc

Please sign in to comment.