summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjörn Hagemeister2014-10-13 17:20:21 +0200
committerBjörn Hagemeister2014-10-13 17:20:21 +0200
commit88e78edf4f813a5be3c760e8d363b45e744d830f (patch)
treee6031e17e7c2ba847473fe9baff649361f2bbafb
parentCleanup of AsymKeyHolder, Globals and Identity classes (diff)
downloadsatellite-daemon-88e78edf4f813a5be3c760e8d363b45e744d830f.tar.gz
satellite-daemon-88e78edf4f813a5be3c760e8d363b45e744d830f.tar.xz
satellite-daemon-88e78edf4f813a5be3c760e8d363b45e744d830f.zip
Started implementing parsing command line arguments.
-rw-r--r--src/main/java/org/openslx/satellitedaemon/App.java124
-rw-r--r--src/main/java/org/openslx/satellitedaemon/Identity.java6
2 files changed, 130 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java
index e29520f..7c87401 100644
--- a/src/main/java/org/openslx/satellitedaemon/App.java
+++ b/src/main/java/org/openslx/satellitedaemon/App.java
@@ -1,6 +1,10 @@
package org.openslx.satellitedaemon;
+import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
+import java.security.interfaces.RSAPrivateKey;
+import java.security.interfaces.RSAPublicKey;
+import java.util.Random;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
@@ -20,6 +24,77 @@ public class App
{
BasicConfigurator.configure();
+ int i = 0;
+ String arg;
+ String organizationName;
+ String modulus, privExp, pubExp;
+ String ipAddress;
+
+ // Check if there are arguments available and if they start with "--".
+ if (i < args.length && args[i].startsWith( "--" )) {
+ // Arguments available, take the first one.
+ arg = args[i++];
+ if (arg.equals( "--checkconfig" )) {
+ if (checkConfig()) {
+ System.exit( 0 );
+ }
+ System.exit( 2 );
+ } else if (arg.equals( "--genid" )) {
+ if (i < args.length) {
+ organizationName = args[i++];
+ if (genId(organizationName)) {
+ System.exit( 0 );
+ }
+ else
+ System.exit( 2 );
+ } else {
+ log.error( "--genid requires an organization name" );
+ System.exit( 2 );
+ }
+ } else if (arg.equals( "--import" )) {
+ if ((i + 3) < args.length) {
+ log.error( "Illelgal option: '--import' requires 4 arguments, <OrgName> <Modulus> <priv_Exponent> <pub_Exponent>" );
+ System.exit( 2 );
+ } else {
+ organizationName = args[i++];
+ modulus = args[i++];
+ privExp = args[i++];
+ pubExp = args[i++];
+ if (importId(organizationName, modulus, privExp, pubExp)) {
+ System.exit( 0 );
+ } else
+ System.exit( 2 );
+ }
+ } else if (arg.equals( "--submitkey")) {
+ if (i < args.length) {
+ ipAddress = args[i++];
+ if (submitKey(ipAddress))
+ System.exit( 0 );
+ else
+ System.exit( 2 );
+ } else {
+ log.error( "--submitkey requires <IPADDRESS>" );
+ System.exit( 2 );
+ }
+ } else if (arg.equals( "--updateaddress" )) {
+ if (i < args.length) {
+ ipAddress = args[i++];
+ if (updateAddress(ipAddress)) {
+ System.exit( 0 );
+ } else
+ System.exit( 2 );
+ } else {
+ log.error( "--updateaddress requires <IPADDRESS>" );
+ System.exit( 2 );
+ }
+ }
+ } else if (args.length == 0) {
+ // No Option choosed, try to load existing identity.
+ if (!tryLoadIdentity()) {
+ System.exit( 2 );
+ }
+ }
+
if ( !Globals.masterServerSslContextInit() ) {
log.error( "Problem with initializing the SSLContext" );
System.exit( 1 );
@@ -30,4 +105,53 @@ public class App
Thread downloadWorker = new Thread( new FileDownloadWorker() );
downloadWorker.start();
}
+
+ private static boolean checkConfig() {
+ if (Identity.getOrganizationName() == null)
+ return false;
+ // TODO: check members of identity.java.
+ // Testing encryption and description
+ Random rnd = new Random();
+ if (Identity.keySize() != -1) {
+ int size = rnd.nextInt(Identity.keySize() - 1);
+ BigInteger text = new BigInteger(size,rnd);
+ RSAPublicKey pub = (RSAPublicKey) Identity.getPublicKey();
+ RSAPrivateKey priv = (RSAPrivateKey) Identity.getPrivateKey();
+ BigInteger cipher = text.modPow(pub.getPublicExponent(), pub.getModulus());
+ BigInteger decrypted = cipher.modPow(priv.getPrivateExponent(), priv.getModulus());
+ boolean isPassed = text.equals(decrypted);
+ return isPassed;
+ }
+ return false;
+// System.out.println("--- RSA encryption test ---");
+// System.out.println("Is test passed: "+isPassed);
+// System.out.println("Original text: "+text);
+// System.out.println("Cipher text: "+cipher);
+// System.out.println("Decrypted text: "+decrypted);
+ }
+
+ private static boolean genId(String organizationName) {
+ // TODO.
+ return false;
+ }
+
+ private static boolean importId(String organizationName, String modulus, String privExp, String pubExp) {
+ // TODO.
+ return false;
+ }
+
+ private static boolean submitKey(String ipAddress) {
+ // TODO.
+ return false;
+ }
+
+ private static boolean updateAddress(String ipAddress) {
+ // TODO.
+ return false;
+ }
+
+ private static boolean tryLoadIdentity() {
+ // TODO.
+ return false;
+ }
}
diff --git a/src/main/java/org/openslx/satellitedaemon/Identity.java b/src/main/java/org/openslx/satellitedaemon/Identity.java
index 8126aa9..de30fbb 100644
--- a/src/main/java/org/openslx/satellitedaemon/Identity.java
+++ b/src/main/java/org/openslx/satellitedaemon/Identity.java
@@ -96,6 +96,12 @@ public class Identity
return akh.getPublicKey();
}
+ public static int keySize() {
+ if (getModulus() != null)
+ return getModulus().bitLength();
+ return -1;
+ }
+
private static BigInteger toBigInt( String str )
{
try {