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 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(); } public static int keySize() { if (getModulus() != null) return getModulus().bitLength(); return -1; } private static BigInteger toBigInt( String str ) { try { return new BigInteger( str ); } catch ( Exception e ) { return null; } } }