From 6c4e2fa4523d4a4858d819064f0ed7c42fa8d89c Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Wed, 8 Oct 2014 18:02:30 +0200 Subject: Splitted Globals.java into two classes and splitted config file global.properties into global.properties and identity.properties. --- .../java/org/openslx/satellitedaemon/Identity.java | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/org/openslx/satellitedaemon/Identity.java (limited to 'src/main/java/org/openslx/satellitedaemon/Identity.java') diff --git a/src/main/java/org/openslx/satellitedaemon/Identity.java b/src/main/java/org/openslx/satellitedaemon/Identity.java new file mode 100644 index 0000000..b8ed1ee --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/Identity.java @@ -0,0 +1,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; + } +} -- cgit v1.2.3-55-g7522