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

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

	private static BigInteger mod = null;
	private static BigInteger privExp = null;
	private static BigInteger pubExp = null;

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

	public static BigInteger getModulus()
	{
		String privateModulus = properties.getProperty( "MODULUS" );
		mod = new BigInteger( privateModulus );
		return mod;
	}

	public static BigInteger getPublicExponent()
	{
		String publicModulus = properties.getProperty( "PUBLIC_EXPONENT" );
		pubExp = new BigInteger( publicModulus );
		return pubExp;
	}

	public static BigInteger getPrivateExponent()
	{
		String exponent = properties.getProperty( "PRIVATE_EXPONENT" );
		privExp = new BigInteger( exponent );
		return privExp;
	}

	/**
	 * Load properties
	 */
	static {
		try {
			// Load all entries of the config file into properties
			InputStreamReader stream = new InputStreamReader(
					new FileInputStream( "config/identity.properties" ), StandardCharsets.UTF_8 );
			properties.load( stream );
			stream.close();
		} catch ( IOException e ) {
			log.error( "Could not load properties. Exiting." );
			System.exit( 2 );
		}
		
		Util.notNullOrEmptyFatal( getOrganizationName(), "Organiziation Name must not be empty!" );
	}

	/**
	 * Get private key for this server. If none exists yet, create a new one.
	 * 
	 * @return
	 */
	public static PrivateKey getPrivateKey()
	{
		AsymKeyHolder akh;
		try {
			akh = new AsymKeyHolder( privExp, null, mod );
			return akh.getPrivateKey();
		} catch ( InvalidKeySpecException e ) {
			log.error( "InvalidKeySpecException", e );
		} catch ( NoSuchAlgorithmException e ) {
			log.error( "NoSuchAlgorithmException", e );
		}
		return null;
	}

	/**
	 * Get public key for this server. If none exists yet, create a new one.
	 * 
	 * @return
	 */
	public static PublicKey getPublicKey()
	{
		AsymKeyHolder akh;
		try {
			akh = new AsymKeyHolder( null, pubExp, mod );
			return akh.getPublicKey();
		} catch ( InvalidKeySpecException e ) {
			log.error( "InvalidKeySpecException", e );
		} catch ( NoSuchAlgorithmException e ) {
			log.error( "NoSuchAlgorithmException", e );
		}
		return null;
	}
}