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; } }