From f0f414b063905de9051a242abb85f20285451941 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 7 Oct 2014 14:39:49 +0200 Subject: Adapt to login@uni format for user-ids, Put asym keys in extra class --- .../java/org/openslx/satellitedaemon/Globals.java | 226 +++++++++------------ 1 file changed, 96 insertions(+), 130 deletions(-) (limited to 'src/main/java/org/openslx/satellitedaemon/Globals.java') diff --git a/src/main/java/org/openslx/satellitedaemon/Globals.java b/src/main/java/org/openslx/satellitedaemon/Globals.java index 9ff6911..cf61aaf 100644 --- a/src/main/java/org/openslx/satellitedaemon/Globals.java +++ b/src/main/java/org/openslx/satellitedaemon/Globals.java @@ -1,12 +1,9 @@ package org.openslx.satellitedaemon; -import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; -import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.KeyManagementException; @@ -15,9 +12,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.CertificateException; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.RSAPrivateKeySpec; -import java.security.spec.RSAPublicKeySpec; import java.util.Properties; import javax.net.ssl.SSLContext; @@ -26,65 +20,72 @@ import javax.net.ssl.TrustManagerFactory; import org.apache.log4j.Logger; -public class Globals { - private static Logger log = Logger.getLogger(Globals.class); +public class Globals +{ + private static Logger log = Logger.getLogger( Globals.class ); private static final Properties properties = new Properties(); private static SSLContext context = null; - private static final KeyFactory keyFact; public static final int BLOCKSIZE = 16 * 1024 * 1024; // 16 MB blocksize - public static void init() { - } - /***********************************************************************************************/ /** * A call of Globals.getXXXXXX() returns the corresponding entry in config/global.properties */ - + // * Properties *// // Strings // - public static String getMasterserverHost() { - return properties.getProperty("MASTERSERVER_HOST"); + public static String getMasterserverHost() + { + return properties.getProperty( "MASTERSERVER_HOST" ); } - public static String getKeystoreType() { - return properties.getProperty("KEYSTORE_TYPE"); + public static String getKeystoreType() + { + return properties.getProperty( "KEYSTORE_TYPE" ); } - public static String getFiletransferKeystorePath() { - return properties.getProperty("FILETRANSFER_KEYSTORE_PATH"); + public static String getFiletransferKeystorePath() + { + return properties.getProperty( "FILETRANSFER_KEYSTORE_PATH" ); } - public static String getFiletransferKeystorePassword() { - return properties.getProperty("FILETRANSFER_KEYSTORE_PASSWORD"); + public static String getFiletransferKeystorePassword() + { + return properties.getProperty( "FILETRANSFER_KEYSTORE_PASSWORD" ); } - public static String getOrganizationName() { - return properties.getProperty("ORGANIZATION_NAME"); + public static String getOrganizationName() + { + return properties.getProperty( "ORGANIZATION_NAME" ); } - public static String getThriftKeystoreAlias() { - return properties.getProperty("THRIFT_KEYSTORE_ALIAS"); + public static String getThriftKeystoreAlias() + { + return properties.getProperty( "THRIFT_KEYSTORE_ALIAS" ); } - public static String getThriftKeystorePassword() { - return properties.getProperty("THRIFT_KEYSTORE_PASSWORD"); + public static String getThriftKeystorePassword() + { + return properties.getProperty( "THRIFT_KEYSTORE_PASSWORD" ); } - public static String getThriftKeystorePath() { - return properties.getProperty("THRIFT_KEYSTORE_PATH"); + public static String getThriftKeystorePath() + { + return properties.getProperty( "THRIFT_KEYSTORE_PATH" ); } - - public static String getImageFolder() { - return properties.getProperty("IMAGE_FOLDER"); + + public static String getImageFolder() + { + return properties.getProperty( "IMAGE_FOLDER" ); } // Integers // - public static int getThriftPort() { - return tryToParseInt(properties.getProperty("THRIFT_PORT")); + public static int getThriftPort() + { + return tryToParseInt( properties.getProperty( "THRIFT_PORT" ) ); } /** @@ -94,31 +95,23 @@ public class Globals { try { // Load all entries of the config file into properties InputStreamReader stream = new InputStreamReader( - new FileInputStream("config/global.properties"), StandardCharsets.UTF_8); - properties.load(stream); + new FileInputStream( "config/global.properties" ), StandardCharsets.UTF_8 ); + properties.load( stream ); stream.close(); - } catch (IOException e) { - log.error("Could not load properties. Exiting."); - System.exit(2); + } catch ( IOException e ) { + log.error( "Could not load properties. Exiting." ); + System.exit( 2 ); } - - KeyFactory kf; - try { - kf = KeyFactory.getInstance("RSA"); - } catch (NoSuchAlgorithmException nSAE) { - kf = null; - } - keyFact = kf; - - notNullOrEmptyFatal(getMasterserverHost(), "Masterserver Host must not be empty!"); - notNullOrEmptyFatal(getKeystoreType(), "Keystore Type must not be empty"); - notNullOrEmptyFatal(getFiletransferKeystorePassword(), "File transfer Keystore Password must not be empty!"); - notNullOrEmptyFatal(getFiletransferKeystorePath(), "File transfer Keystore Path must not be empty!"); - notNullOrEmptyFatal(getOrganizationName(), "Organiziation Name must not be empty!"); - notNullOrEmptyFatal(getThriftKeystoreAlias(), "Thrift Keystore Alias must not be empty!"); - notNullOrEmptyFatal(getThriftKeystorePassword(), "Thrift Keystore Password must not be empty!"); - notNullOrEmptyFatal(getThriftKeystorePath(), "Thrift Keystore Path must not be empty!"); - notNullOrEmptyFatal(getImageFolder(), "Image Folder must not be empty!"); + + notNullOrEmptyFatal( getMasterserverHost(), "Masterserver Host must not be empty!" ); + notNullOrEmptyFatal( getKeystoreType(), "Keystore Type must not be empty" ); + notNullOrEmptyFatal( getFiletransferKeystorePassword(), "File transfer Keystore Password must not be empty!" ); + notNullOrEmptyFatal( getFiletransferKeystorePath(), "File transfer Keystore Path must not be empty!" ); + notNullOrEmptyFatal( getOrganizationName(), "Organiziation Name must not be empty!" ); + notNullOrEmptyFatal( getThriftKeystoreAlias(), "Thrift Keystore Alias must not be empty!" ); + notNullOrEmptyFatal( getThriftKeystorePassword(), "Thrift Keystore Password must not be empty!" ); + notNullOrEmptyFatal( getThriftKeystorePath(), "Thrift Keystore Path must not be empty!" ); + notNullOrEmptyFatal( getImageFolder(), "Image Folder must not be empty!" ); } /***********************************************************************************************/ @@ -126,42 +119,44 @@ public class Globals { * * @return */ - public static boolean masterServerSslContextInit() { + public static boolean masterServerSslContextInit() + { char[] passphrase = getFiletransferKeystorePassword().toCharArray(); KeyStore keystore; try { - keystore = KeyStore.getInstance("JKS"); - keystore.load(new FileInputStream(getFiletransferKeystorePath()), - passphrase); + keystore = KeyStore.getInstance( "JKS" ); + keystore.load( new FileInputStream( getFiletransferKeystorePath() ), + passphrase ); TrustManagerFactory tmf = TrustManagerFactory - .getInstance(TrustManagerFactory.getDefaultAlgorithm()); - tmf.init(keystore); - context = SSLContext.getInstance("SSLv3"); + .getInstance( TrustManagerFactory.getDefaultAlgorithm() ); + tmf.init( keystore ); + context = SSLContext.getInstance( "SSLv3" ); TrustManager[] trustManagers = tmf.getTrustManagers(); - context.init(null, trustManagers, null); - } catch (KeyStoreException e) { - log.error("KeyStoreException"); + context.init( null, trustManagers, null ); + } catch ( KeyStoreException e ) { + log.error( "KeyStoreException" ); return false; - } catch (NoSuchAlgorithmException e) { - log.error("NoSuchAlgorithmException"); + } catch ( NoSuchAlgorithmException e ) { + log.error( "NoSuchAlgorithmException" ); return false; - } catch (CertificateException e) { - log.error("CertificateException"); + } catch ( CertificateException e ) { + log.error( "CertificateException" ); return false; - } catch (FileNotFoundException e) { - log.error("Could not find the keystore for the filetransfer. Path was '" + getFiletransferKeystorePath() + "'"); + } catch ( FileNotFoundException e ) { + log.error( "Could not find the keystore for the filetransfer. Path was '" + getFiletransferKeystorePath() + "'" ); return false; - } catch (IOException e) { - log.error("IOException", e); + } catch ( IOException e ) { + log.error( "IOException", e ); return false; - } catch (KeyManagementException e) { - log.error("KeyManagementException"); + } catch ( KeyManagementException e ) { + log.error( "KeyManagementException" ); return false; } return true; } - public static SSLContext getMasterServerSslContext() { + public static SSLContext getMasterServerSslContext() + { return Globals.context; } @@ -169,65 +164,36 @@ public class Globals { * Tries to parse an int. Returns 0 on error. * * @param s - * The strig to parse + * The strig to parse * @return The parsed int or 0 on error */ - public static int tryToParseInt(String s) { + public static int tryToParseInt( String s ) + { try { - return Integer.parseInt(s); - } catch (NumberFormatException e) { + return Integer.parseInt( s ); + } catch ( NumberFormatException e ) { return 0; } } - public static void notNullOrEmptyFatal(String something, String message) { - if (something == null || something.isEmpty()) { - if (message != null) - log.fatal("[NOTNULL] " + message); - log.warn(Thread.currentThread().getStackTrace().toString()); - System.exit(2); + public static void notNullOrEmptyFatal( String something, String message ) + { + if ( something == null || something.isEmpty() ) { + if ( message != null ) + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); + System.exit( 2 ); } } - - public static PrivateKey getPrivateKey() { - PrivateKey ret; - BufferedReader br = null; - String modulus, exponent; - try { - br = new BufferedReader(new FileReader("config/private.key")); - modulus = br.readLine(); - exponent = br.readLine(); - } catch (FileNotFoundException e) { - log.error("File 'private.key' not found!", e); - return null; - } catch (IOException e) { - log.error("File 'private.key' not correct readable.", e); - return null; - } finally { - try { - br.close(); - } catch (IOException e) { - } - } - if (modulus == null || exponent == null) { - return null; - } - - try { - BigInteger mod = new BigInteger(modulus); - BigInteger exp = new BigInteger(exponent); - - RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(mod, exp); - synchronized (keyFact) { - ret = keyFact.generatePrivate(keySpec); - } - } catch (InvalidKeySpecException e) { - log.error("Not able to build key with given numbers.", e); - return null; - } catch (NumberFormatException e) { - log.error("Invalid number format.", e); - return null; - } - return ret; + + /** + * Get private key for this server. If none exists yet, create a new one. + * + * @return + */ + public static PrivateKey getPrivateKey() + { + return AsymKeyHolder.getPrivateKey(); } -} \ No newline at end of file + +} -- cgit v1.2.3-55-g7522