diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cz/crcs/ectester/standalone/ECTesterStandalone.java | 10 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java | 2 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/CECLibrary.java | 31 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java | 131 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java (renamed from src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java) | 8 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/SunECLib.java | 2 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/TomcryptLib.java | 16 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/standalone/libs/native.h | 21 |
8 files changed, 181 insertions, 40 deletions
diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java index 76b98ce..e36ffcd 100644 --- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java +++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java @@ -10,7 +10,7 @@ import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; import cz.crcs.ectester.standalone.consts.SignatureIdent; import cz.crcs.ectester.standalone.libs.BouncyCastleLib; import cz.crcs.ectester.standalone.libs.ECLibrary; -import cz.crcs.ectester.standalone.libs.JavaECLibrary; +import cz.crcs.ectester.standalone.libs.ProviderECLibrary; import cz.crcs.ectester.standalone.libs.SunECLib; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; @@ -186,8 +186,8 @@ public class ECTesterStandalone { * */ private void generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { - if (cfg.selected instanceof JavaECLibrary) { - JavaECLibrary jlib = (JavaECLibrary) cfg.selected; + if (cfg.selected instanceof ProviderECLibrary) { + ProviderECLibrary jlib = (ProviderECLibrary) cfg.selected; KeyPairGeneratorIdent ident = null; String algo = cli.getOptionValue("generate.type", "EC"); for (KeyPairGeneratorIdent kpIdent : jlib.getKPGs()) { @@ -241,8 +241,8 @@ public class ECTesterStandalone { * */ private void export() throws NoSuchAlgorithmException, IOException { - if (cfg.selected instanceof JavaECLibrary) { - JavaECLibrary jlib = (JavaECLibrary) cfg.selected; + if (cfg.selected instanceof ProviderECLibrary) { + ProviderECLibrary jlib = (ProviderECLibrary) cfg.selected; KeyPairGeneratorIdent ident = null; String algo = cli.getOptionValue("export.type", "EC"); for (KeyPairGeneratorIdent kpIdent : jlib.getKPGs()) { diff --git a/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java b/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java index 73cd197..aaf76be 100644 --- a/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java +++ b/src/cz/crcs/ectester/standalone/libs/BouncyCastleLib.java @@ -5,7 +5,7 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; /** * @author Jan Jancar johny@neuromancer.sk */ -public class BouncyCastleLib extends JavaECLibrary { +public class BouncyCastleLib extends ProviderECLibrary { public BouncyCastleLib() { super(new BouncyCastleProvider()); diff --git a/src/cz/crcs/ectester/standalone/libs/CECLibrary.java b/src/cz/crcs/ectester/standalone/libs/CECLibrary.java deleted file mode 100644 index 82a4555..0000000 --- a/src/cz/crcs/ectester/standalone/libs/CECLibrary.java +++ /dev/null @@ -1,31 +0,0 @@ -package cz.crcs.ectester.standalone.libs; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public abstract class CECLibrary implements ECLibrary { - - private String resourcePath; - private String libname; - - public CECLibrary(String resourcePath, String libname) { - this.resourcePath = resourcePath; - this.libname = libname; - } - - @Override - public boolean initialize() { - // load the library here. - return false; - } - - @Override - public String name() { - return libname; - } - - @Override - public String toString() { - return name(); - } -} diff --git a/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java new file mode 100644 index 0000000..40691e6 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java @@ -0,0 +1,131 @@ +package cz.crcs.ectester.standalone.libs; + +import cz.crcs.ectester.standalone.consts.KeyAgreementIdent; +import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent; +import cz.crcs.ectester.standalone.consts.SignatureIdent; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.security.Provider; +import java.util.Collections; +import java.util.Set; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public abstract class NativeECLibrary implements ECLibrary { + private ProviderECLibrary loaded; + private String resource; + private String libname; + + public NativeECLibrary(String resource, String libname) { + this.resource = resource; + this.libname = libname; + } + + @Override + public boolean initialize() { + try { + String suffix; + Path appData; + if (System.getProperty("os.name").startsWith("Windows")) { + suffix = "dll"; + appData = Paths.get(System.getenv("AppData")); + } else { + suffix = "so"; + if (System.getProperty("os.name").startsWith("Linux")) { + appData = Paths.get(System.getenv("XDG_DATA_HOME")); + if (appData == null) { + appData = Paths.get(System.getProperty("user.home"), ".local", "share"); + } + } else { + appData = Paths.get(System.getProperty("user.home"), ".local", "share"); + } + } + Path libDir = appData.resolve("ECTesterStandalone"); + File libDirFile = libDir.toFile(); + Path libPath = libDir.resolve(libname + "." + suffix); + File libFile = libPath.toFile(); + + URL jarURL = NativeECLibrary.class.getResource("/cz/crcs/ectester/standalone/libs/" + resource + "." + suffix); + if (jarURL == null) { + return false; + } + URLConnection jarConnection = jarURL.openConnection(); + + boolean write = false; + if (libDirFile.isDirectory() && libFile.isFile()) { + long jarModified = jarConnection.getLastModified(); + + long libModified = Files.getLastModifiedTime(libPath).toMillis(); + if (jarModified > libModified) { + write = true; + } + } else { + libDir.toFile().mkdirs(); + libFile.createNewFile(); + write = true; + } + + if (write) { + Files.copy(jarConnection.getInputStream(), libPath, StandardCopyOption.REPLACE_EXISTING); + } + jarConnection.getInputStream().close(); + + System.load(libPath.toString()); + + loaded = new ProviderECLibrary(getProvider()); + return true; + } catch (IOException ignored) { + + } + return false; + } + + public abstract Provider getProvider(); + + @Override + public boolean isInitialized() { + return loaded != null && loaded.isInitialized(); + } + + @Override + public Set<KeyAgreementIdent> getECKAs() { + if (!isInitialized()) { + return Collections.emptySet(); + } + return loaded.getECKAs(); + } + + @Override + public Set<SignatureIdent> getECSigs() { + if (!isInitialized()) { + return Collections.emptySet(); + } + return loaded.getECSigs(); + } + + @Override + public Set<KeyPairGeneratorIdent> getKPGs() { + if (!isInitialized()) { + return Collections.emptySet(); + } + return loaded.getKPGs(); + } + + @Override + public String name() { + return loaded.name(); + } + + @Override + public String toString() { + return name(); + } +} diff --git a/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java b/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java index 5689b2b..879cc16 100644 --- a/src/cz/crcs/ectester/standalone/libs/JavaECLibrary.java +++ b/src/cz/crcs/ectester/standalone/libs/ProviderECLibrary.java @@ -14,11 +14,11 @@ import java.util.function.Function; /** * @author Jan Jancar johny@neuromancer.sk */ -public abstract class JavaECLibrary implements ECLibrary { +public class ProviderECLibrary implements ECLibrary { private Provider provider; private boolean initialized; - public JavaECLibrary(Provider provider) { + public ProviderECLibrary(Provider provider) { this.provider = provider; this.initialized = false; } @@ -44,6 +44,10 @@ public abstract class JavaECLibrary implements ECLibrary { private <T extends Ident> Set<T> getIdents(String type, Function<String, T> getter) { Set<T> results = new HashSet<>(); + if (!initialized) { + return results; + } + for (Provider.Service service : provider.getServices()) { if (service.getType().equals(type)) { T id = getter.apply(service.getAlgorithm()); diff --git a/src/cz/crcs/ectester/standalone/libs/SunECLib.java b/src/cz/crcs/ectester/standalone/libs/SunECLib.java index 408908e..b64b740 100644 --- a/src/cz/crcs/ectester/standalone/libs/SunECLib.java +++ b/src/cz/crcs/ectester/standalone/libs/SunECLib.java @@ -5,7 +5,7 @@ import sun.security.ec.SunEC; /** * @author Jan Jancar johny@neuromancer.sk */ -public class SunECLib extends JavaECLibrary { +public class SunECLib extends ProviderECLibrary { public SunECLib() { super(new SunEC()); diff --git a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java new file mode 100644 index 0000000..31d6812 --- /dev/null +++ b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java @@ -0,0 +1,16 @@ +package cz.crcs.ectester.standalone.libs; + +import java.security.Provider; + +/** + * @author Jan Jancar johny@neuromancer.sk + */ +public class TomcryptLib extends NativeECLibrary { + + public TomcryptLib() { + super("tomcrypt", "libtomcrypt"); + } + + @Override + public native Provider getProvider(); +} diff --git a/src/cz/crcs/ectester/standalone/libs/native.h b/src/cz/crcs/ectester/standalone/libs/native.h new file mode 100644 index 0000000..979f04a --- /dev/null +++ b/src/cz/crcs/ectester/standalone/libs/native.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class cz_crcs_ectester_standalone_libs_TomcryptLib */ + +#ifndef _Included_cz_crcs_ectester_standalone_libs_TomcryptLib +#define _Included_cz_crcs_ectester_standalone_libs_TomcryptLib +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: cz_crcs_ectester_standalone_libs_TomcryptLib + * Method: getProvider + * Signature: ()Ljava/security/Provider; + */ +JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_getProvider + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif |
