summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Identity.java
blob: 947d90adf77d5fd5eef2d58b873f948a6aa50cf5 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package org.openslx.satellitedaemon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Properties;
import java.util.Random;

import org.apache.log4j.Logger;
import org.openslx.encryption.AsymKeyHolder;
import org.openslx.satellitedaemon.filetransfer.ThriftConnection;
import org.openslx.satellitedaemon.util.Util;

public class Identity
{
	private static Logger log = Logger.getLogger( Identity.class );
	private static final Properties properties = new Properties();

	private static AsymKeyHolder akh = null;

	public static String getOrganizationName()
	{
		return properties.getProperty( "ORGANIZATION_NAME" );
	}

	private static BigInteger getModulus()
	{
		return toBigInt( properties.getProperty( "MODULUS" ) );
	}

	private static BigInteger getPublicExponent()
	{
		return toBigInt( properties.getProperty( "PUBLIC_EXPONENT" ) );
	}

	private static BigInteger getPrivateExponent()
	{
		return toBigInt( properties.getProperty( "PRIVATE_EXPONENT" ) );
	}

	/**
	 * Load properties
	 */
	static {
		InputStreamReader stream = null;
		try {
			// Load all entries of the config file into properties
			stream = new InputStreamReader(
					new FileInputStream( "config/identity.properties" ), StandardCharsets.UTF_8 );
			properties.load( stream );
			stream.close();
		} catch ( IOException e ) {
			log.error( "Could not load identity.properties. Exiting." );
			System.exit( 2 );
		} finally {
			Util.streamClose( stream );
		}

		Util.notNullOrEmptyFatal( getOrganizationName(), "Organiziation Name must not be empty!" );
		try {
			akh = new AsymKeyHolder( getPrivateExponent(), getPublicExponent(), getModulus() );
		} catch ( InvalidKeySpecException e ) {
			log.error( "InvalidKeySpecException", e );
		} catch ( NoSuchAlgorithmException e ) {
			log.error( "NoSuchAlgorithmException", e );
		}
	}

	/**
	 * Get private key for this server. If none exists yet, create a new one.
	 * 
	 * @return
	 */
	public static PrivateKey getPrivateKey()
	{
		if ( akh != null ) {
			return akh.getPrivateKey();
		}
		akh = new AsymKeyHolder();
		return akh.getPrivateKey();
	}

	/**
	 * Get public key for this server. If none exists yet, create a new one.
	 * 
	 * @return
	 */
	public static PublicKey getPublicKey()
	{
		if ( akh != null )
			return akh.getPublicKey();
		akh = new AsymKeyHolder();
		return akh.getPublicKey();
	}

	/**
	 * Get bit - length of key.
	 * 
	 * @return
	 */
	public static int keySize( BigInteger modulus )
	{
		return modulus.bitLength();
	}

	/**
	 * Checks if given modulus, private exponent and public exponent are valid
	 * values for key pair. Idea is to encrypt and decrypt random text and compare
	 * the result with initial text.
	 * 
	 * @param mod
	 * @param privExp
	 * @param pubExp
	 * @return True, if mod, privExp and pubExp are valid values.
	 */
	public static boolean isValidKeyPair( BigInteger mod, BigInteger privExp, BigInteger pubExp )
	{
		// First check given values (modulus, privExp, pubExp).
		if ( ( mod == null ) || ( privExp == null ) || ( pubExp == null ) ) {
			log.error( "Given arguments not valid: got NULL for modulus, private or public exponent." );
			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();
		int size = rnd.nextInt( keySize( mod ) - 1 );
		BigInteger text = new BigInteger( size, rnd );
		// Encrypt.
		BigInteger cipher = text.modPow( pubExp, mod );
		// Decrypt again.
		BigInteger decrypted = cipher.modPow( privExp, mod );
		boolean isPassed = text.equals( decrypted );
		return isPassed;
	}

	/**
	 * Generate new identity with given organization name and new key pair.
	 * Write new identity to "config/identity.properties".
	 * 
	 * @param organizationName
	 * @return true, if successful.
	 */
	public static boolean generateIdentity( String organizationName )
	{
		// generate new key pair.
		Identity.akh = new AsymKeyHolder();
		return writeIdToFile( organizationName, akh.getModulus(), akh.getPrivateExponent(), akh.getPublicExponent() );
	}

	/**
	 * Import given identity with organization name, modulus, private and public
	 * exponent and store this identity to "config/identity.properties".
	 * 
	 * @param organizationName
	 * @param modulus
	 * @param privateExp
	 * @param publicExp
	 * @return true, if successful.
	 */
	public static boolean importIdentity( String organizationName, BigInteger modulus, BigInteger privateExp, BigInteger publicExp )
	{
		return writeIdToFile( organizationName, modulus, privateExp, publicExp );
	}

	/**
	 * Submit new satellite - ipAddress to master with organizationId, ipAddress
	 * and key - information.
	 * @param ipAddress
	 * @return true, if successful.
	 */
	public static boolean submitKey( String ipAddress )
	{
		RSAPublicKey pubKey = (RSAPublicKey)getPublicKey();
		RSAPrivateKey privKey = (RSAPrivateKey)getPrivateKey();
		assert ( pubKey.getModulus() == privKey.getModulus() );

		if ( !Identity.isValidKeyPair(
				privKey.getModulus(),
				privKey.getPrivateExponent(),
				pubKey.getPublicExponent() ) )
			return false;
		return ThriftConnection.registerSatellite(
				getOrganizationName(),
				ipAddress,
				pubKey.getModulus().toString(),
				pubKey.getPublicExponent().toString() );
	}
	
	/**
	 * Update already existing satellite - ipAddress in master - Db.
	 * @param ipAddress
	 * @return true, if successful.
	 */
	public static boolean updateAddress( String ipAddress )
	{
		return ThriftConnection.updateSatelliteAddress( ipAddress );
	}

	/**
	 * Write given organization name, modulus, public and private exponent to
	 * "config/identity.properties".
	 * 
	 * @param organizationName
	 * @param modulus
	 * @param privateExp
	 * @param publicExp
	 * @return true, if successful.
	 */
	private static boolean writeIdToFile( String organizationName, BigInteger modulus, BigInteger privateExp, BigInteger publicExp )
	{
		File configFile = new File( "config/identity.properties" );
		FileOutputStream stream = null;
		try {
			stream = new FileOutputStream( configFile );
		} catch ( FileNotFoundException e ) {
			log.error( "FileNotFoundException", e );
			return false;
		}

		// create strings for writing to file. 
		String orgNameString = "ORGANIZATION_NAME=" + organizationName + "\n";
		String modString = "MODULUS=" + modulus.toString() + "\n";
		String privExpString = "PRIVATE_EXPONENT=" + privateExp.toString() + "\n";
		String pubExpString = "PUBLIC_EXPONENT=" + publicExp.toString() + "\n";

		try {
			stream.write( orgNameString.getBytes() );
			stream.write( modString.getBytes() );
			stream.write( privExpString.getBytes() );
			stream.write( pubExpString.getBytes() );
			return true;
		} catch ( IOException e ) {
			log.error( "IOException", e );
			return false;
		} finally {
			try {
				stream.close();
			} catch ( IOException e ) {
			}
		}
	}

	/**
	 * Check modulus, privExp and pubExp for not being null.
	 * 
	 * @return
	 */
	private static boolean checkMembers()
	{
		return ( ( getModulus() != null ) &&
				( getPrivateExponent() != null ) && ( getPublicExponent() != null ) );
	}

	/**
	 * Get BigInteger of read String number.
	 * 
	 * @param str
	 * @return
	 */
	private static BigInteger toBigInt( String str )
	{
		try {
			return new BigInteger( str );
		} catch ( Exception e ) {
			return null;
		}
	}
}