diff options
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon/Globals.java')
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/Globals.java | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/Globals.java b/src/main/java/org/openslx/satellitedaemon/Globals.java index 00d1e0a..2bb9f68 100644 --- a/src/main/java/org/openslx/satellitedaemon/Globals.java +++ b/src/main/java/org/openslx/satellitedaemon/Globals.java @@ -1,16 +1,23 @@ 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.InputStream; import java.io.InputStreamReader; +import java.math.BigInteger; import java.nio.charset.StandardCharsets; +import java.security.KeyFactory; import java.security.KeyManagementException; import java.security.KeyStore; 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; @@ -23,6 +30,7 @@ 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 @@ -94,6 +102,14 @@ public class Globals { 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!"); // TODO: check properties } @@ -165,4 +181,46 @@ public class Globals { 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; + } }
\ No newline at end of file |