summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/App.java
blob: 6a4ebf41eee3a7ce2466b8d0d2fd1b8fe08684e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker;
import org.openslx.satellitedaemon.filetransfer.FileUploadWorker;

/***********************************************************************************************/
/**
 * Main class for uploading images from the HS-Server to the Satellite Server.
 * 
 */
public class App
{
	private static Logger log = Logger.getLogger( App.class );

	public static void main( String[] args ) throws NoSuchAlgorithmException
	{
		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 );
		}
		// Start Up- and Download.
		Thread uploadWorker = new Thread( new FileUploadWorker() );
		uploadWorker.start();
		Thread downloadWorker = new Thread( new FileDownloadWorker() );
		downloadWorker.start();
	}
	
	private static boolean checkConfig() {
		if (Identity.getOrganizationName() == null)
			return false;
		// First check existing members (modulus, privExp, pubExp) of Identity.
		if (!Identity.checkMembers())
			return false;
		
		// Testing encryption and description with given public and private key.
		// Idea: creating random text for encrypting and decrypting again.
      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();
	      // Encrypt.
	      BigInteger cipher = text.modPow(pub.getPublicExponent(), pub.getModulus());
	      // Decrypt again.
	      BigInteger decrypted = cipher.modPow(priv.getPrivateExponent(), priv.getModulus());
	      boolean isPassed = text.equals(decrypted);
	      return isPassed;
      }
      return false;
	}
	
	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;
	}
}