summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Identity.java
blob: 92bf425eaaf0bd82bc546b93f3545a114fb948b6 (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
package org.openslx.satellitedaemon;

import java.io.FileInputStream;
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.spec.InvalidKeySpecException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openslx.encryption.AsymKeyHolder;
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() {
		if (getModulus() != null) 
			return getModulus().bitLength();
		return -1;
	}
	
	public 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;
		}
	}
}