diff options
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java')
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java | 216 |
1 files changed, 53 insertions, 163 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java b/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java index d69ce76..d6ee625 100644 --- a/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java +++ b/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java @@ -1,9 +1,5 @@ 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.KeyPair; @@ -22,195 +18,89 @@ 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; - public AsymKeyHolder(BigInteger privExp, BigInteger pubExp, BigInteger mod) - throws InvalidKeySpecException, NoSuchAlgorithmException { + private static RSAPrivateKey privKey = null; + private static RSAPublicKey pubKey = null; + + public AsymKeyHolder( BigInteger privExp, BigInteger pubExp, BigInteger mod ) + throws NoSuchAlgorithmException, InvalidKeySpecException + { + if ( mod == null ) + throw new InvalidKeySpecException( "No modulus given!" ); final KeyFactory keyFact; - try { - keyFact = KeyFactory.getInstance( "RSA" ); - } catch ( NoSuchAlgorithmException e ) { - throw new NoSuchAlgorithmException(e.getMessage()); + keyFact = KeyFactory.getInstance( "RSA" ); + if ( pubExp != null ) { + RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, pubExp ); + pubKey = (RSAPublicKey)keyFact.generatePublic( keySpec ); } - if (privExp == null) { - // private exponent == null. Generate public key. - if (mod != null) { - try { - RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, pubExp ); - synchronized ( keyFact ) { - pubKey = keyFact.generatePublic( keySpec ); - } - } catch ( InvalidKeySpecException e ) { - LOG.error( "Not able to build key with given numbers.", e ); - throw new InvalidKeySpecException( e.getMessage() ); - } catch ( NumberFormatException e ) { - LOG.error( "Invalid number format.", e ); - throw new NumberFormatException( e.toString() ); - } - } - } else if (pubExp == null) { - // public exponent == null. Generate private key. - if (mod != null) { - try { - RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, privExp ); - synchronized ( keyFact ) { - privKey = keyFact.generatePrivate( keySpec ); - } - } catch ( InvalidKeySpecException e ) { - LOG.error( "Not able to build key with given numbers.", e ); - throw new InvalidKeySpecException( e.getMessage() ); - } catch ( NumberFormatException e ) { - LOG.error( "Invalid number format.", e ); - throw new NumberFormatException( e.toString() ); - } - } - } else { - // create both keys. - if (mod != null) { - try { - RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( mod, pubExp ); - RSAPrivateKeySpec privkeySpec = new RSAPrivateKeySpec( mod, privExp ); - synchronized ( keyFact ) { - privKey = keyFact.generatePrivate( privkeySpec ); - pubKey = keyFact.generatePublic( pubKeySpec ); - } - } catch ( InvalidKeySpecException e ) { - LOG.error( "Not able to build key with given numbers.", e ); - throw new InvalidKeySpecException( e.getMessage() ); - } catch ( NumberFormatException e ) { - LOG.error( "Invalid number format.", e ); - throw new NumberFormatException( e.toString() ); - } - } + if ( privExp != null ) { + RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, privExp ); + privKey = (RSAPrivateKey)keyFact.generatePrivate( keySpec ); } } - - public AsymKeyHolder() throws NoSuchAlgorithmException { + + public AsymKeyHolder() + { generateKey(); } - - + /** - * Get private key for this server. If none exists yet, create a new one. + * Get private key. * * @return */ public PrivateKey getPrivateKey() { - if (privKey == null) { - if (!generateKey()) { - LOG.warn( "Could not load or generate keypair for communication with masterserver" ); - } - } - return privKey; } - + public PublicKey getPublicKey() { - if (pubKey == null) { - if (!generateKey()) { - LOG.warn( "Could not generate keypair for communication with masterserver" ); - } - } return pubKey; } -// private boolean loadKey() -// { -// BufferedReader br = null; -// String modulus, exponent; -// KeyFactory keyFact; -// -// try { -// keyFact = KeyFactory.getInstance( "RSA" ); -// } catch ( NoSuchAlgorithmException e ) { -// LOG.warn( "Could not get a KeyFactory to load the key from disk", e ); -// 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 boolean generateKey() { KeyPairGenerator kpg; try { - kpg = KeyPairGenerator.getInstance("RSA"); + kpg = KeyPairGenerator.getInstance( "RSA" ); } catch ( NoSuchAlgorithmException e ) { LOG.error( "NoSuchAlgorithmException", e ); return false; } - - kpg.initialize(4096); + + kpg.initialize( 4096 ); KeyPair kp = kpg.generateKeyPair(); - RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate(); - RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic(); - - BigInteger pubMod = publicKey.getModulus(); - BigInteger privMod = privateKey.getModulus(); - assert(pubMod == privMod); - - BigInteger pubExp = publicKey.getPublicExponent(); - BigInteger privExp = privateKey.getPrivateExponent(); - - RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec( privMod, privExp ); - RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( pubMod, pubExp ); - - KeyFactory keyFact; - try { - keyFact = KeyFactory.getInstance( "RSA" ); - } catch ( NoSuchAlgorithmException e ) { - LOG.error( "NoSuchAlgorithmException", e ); - return false; - } - - synchronized ( keyFact ) { - try { - privKey = keyFact.generatePrivate( privKeySpec ); - pubKey = keyFact.generatePublic( pubKeySpec ); - } catch ( InvalidKeySpecException e ) { - LOG.error( "InvalidKeySpecException", e ); - return false; - } - } + privKey = (RSAPrivateKey)kp.getPrivate(); + pubKey = (RSAPublicKey)kp.getPublic(); + + BigInteger pubMod = pubKey.getModulus(); + BigInteger privMod = privKey.getModulus(); + assert ( pubMod.equals( privMod ) ); return true; } + public BigInteger getModulus() + { + if ( privKey != null ) + return privKey.getModulus(); + if ( pubKey != null ) + return pubKey.getModulus(); + return null; // Should never happen + } + + public BigInteger getPrivateExponent() + { + if ( privKey == null ) + return null; + return privKey.getPrivateExponent(); + } + + public BigInteger getPublicExponent() + { + if ( pubKey == null ) + return null; + return pubKey.getPublicExponent(); + } + } |