summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/Identity.java
blob: ae28def918d5d65f111eda29de1a6a1dd64759e0 (plain) (tree)























                                                                       

                                                










































                                                                                                                      






                                                                        








                                                                                 
                                  
                                                   


                                                  

                                                                   
                                    
                 
                                           








                                                                                
                                 
                                                  




                                                                 
                 
                                          

         
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;
	
	private static AsymKeyHolder akh = 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!" );
		try {
			akh = new AsymKeyHolder( privExp, pubExp, mod );
		} 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();
		} 
		try {
			akh = new AsymKeyHolder();
		} catch ( NoSuchAlgorithmException e ) {
			log.error( "NoSuchAlgorithmException", e );
			return null;
		}
		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();
		try {
			akh = new AsymKeyHolder();
		} catch ( NoSuchAlgorithmException e) {
			log.error("NoSuchAlgorithmException", e);
			return null;
		}
		return akh.getPublicKey();
	}
}