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 --- .../org/openslx/satellitedaemon/AsymKeyHolder.java | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java (limited to 'src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java') diff --git a/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java b/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java new file mode 100644 index 0000000..7eab79f --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java @@ -0,0 +1,109 @@ +package org.openslx.satellitedaemon; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPrivateKeySpec; + +import org.apache.log4j.Logger; + +public class AsymKeyHolder +{ + private static final Logger LOG = Logger.getLogger( AsymKeyHolder.class ); + + private static PrivateKey privKey = null; + private static PublicKey pubKey = null; + + /** + * Get private key for this server. If none exists yet, create a new one. + * + * @return + */ + public static PrivateKey getPrivateKey() + { + if (privKey == null) { + if (!loadKey() && !generateKey()) { + LOG.warn( "Could not load or generate keypair for communication with masterserver" ); + } + } + + return privKey; + } + + private static boolean loadKey() + { + BufferedReader br = null; + String modulus, exponent; + KeyFactory keyFact; + + try { + keyFact = KeyFactory.getInstance( "RSA" ); + } catch ( NoSuchAlgorithmException nSAE ) { + LOG.warn( "Could not get a KeyFactory to load the key from disk", nSAE ); + return false; + } + + 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 false; + } catch ( IOException e ) { + LOG.error( "File 'private.key' not correct readable.", e ); + return false; + } finally { + try { + br.close(); + } catch ( IOException e ) { + } + } + if ( modulus == null || exponent == null ) { + return false; + } + + try { + BigInteger mod = new BigInteger( modulus ); + BigInteger exp = new BigInteger( exponent ); + + RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, exp ); + synchronized ( keyFact ) { + privKey = keyFact.generatePrivate( keySpec ); + } + return privKey != null; + } catch ( InvalidKeySpecException e ) { + LOG.error( "Not able to build key with given numbers.", e ); + } catch ( NumberFormatException e ) { + LOG.error( "Invalid number format.", e ); + } + return false; + } + + private static boolean generateKey() + { + // KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); + // kpg.initialize(4096); + // KeyPair kp = kpg.generateKeyPair(); + // RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate(); + // RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic(); + // + // log.debug("modulus: " + privateKey.getModulus().toString()); + // log.debug("exponent: " + privateKey.getPrivateExponent().toString()); + // + // + // log.debug("modulus: " + publicKey.getModulus().toString()); + // log.debug("exponent: " + publicKey.getPublicExponent().toString()); + // + // System.exit(1); + return true; + } + +} -- cgit v1.2.3-55-g7522