From f89ef8d4385daeb260160c88db70e8ee1802495e Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Mon, 29 Sep 2014 18:14:59 +0200 Subject: Inserted key handling with private key and public key for handshake and switched arguments in call getImage to the right order. --- src/main/java/org/openslx/satellitedaemon/App.java | 24 ++++++++- .../java/org/openslx/satellitedaemon/Globals.java | 60 +++++++++++++++++++++- .../filetransfer/FileDownloadWorker.java | 6 ++- .../filetransfer/ThriftConnection.java | 22 ++++---- 4 files changed, 99 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 356034f..2dcaec6 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -1,5 +1,11 @@ package org.openslx.satellitedaemon; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; + import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker; @@ -14,9 +20,25 @@ public class App { private static Logger log = Logger.getLogger( App.class ); - public static void main( String[] args ) + public static void main( String[] args ) throws NoSuchAlgorithmException { BasicConfigurator.configure(); + +// 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); + // Loads all entries from the configuration file config/globals.properties Globals.init(); if (!Globals.masterServerSslContextInit()){ 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 diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java index 160e2fc..e8b8b4d 100644 --- a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java @@ -11,6 +11,7 @@ import org.openslx.filetransfer.WantRangeCallback; import org.openslx.imagemaster.thrift.iface.DownloadData; import org.openslx.satellitedaemon.Globals; import org.openslx.satellitedaemon.db.DbImage; +import org.openslx.satellitedaemon.db.DbImage.Status; public class FileDownloadWorker implements Runnable { private static Logger log = Logger.getLogger(FileDownloadWorker.class); @@ -50,8 +51,11 @@ public class FileDownloadWorker implements Runnable { @Override public FileRange get() { // get start of range. - if (pos >= size) + if (pos >= size) { + log.debug("Download completed."); + image.updateStatus(Status.successfully_decentralized); return null; + } long startOfRange = pos; long endOfRange = Math.min(pos + Globals.BLOCKSIZE, image.fileSize); diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java index da6c6df..673be05 100644 --- a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java @@ -19,6 +19,7 @@ import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; +import org.openslx.encryption.AsymEncryptionHandler; import org.openslx.imagemaster.crcchecker.CrcFile; import org.openslx.imagemaster.thrift.iface.AuthorizationError; import org.openslx.imagemaster.thrift.iface.AuthorizationException; @@ -165,8 +166,9 @@ public class ThriftConnection { log.error("Client was null!"); return null; } - return theClient.getImage(imDat.guid, sSD.sessionId); + return theClient.getImage(sSD.sessionId, imDat.guid); } catch (ImageDataException e) { + log.debug("In catch - blog of thrift connection"); if (e.isSetNumber() && e.getNumber().equals(ImageDataError.INVALID_DATA)) { // Data in the db is not valid @@ -187,10 +189,11 @@ public class ThriftConnection { if (e.isSetNumber() && e.getNumber().equals( AuthorizationError.NOT_AUTHENTICATED)) { + log.error("Not authenticated. SessionID is not valid.", e); // SessionID is not valid // TODO: Code for new SSID } else if (e.getNumber().equals(AuthorizationError.NO_PERMISSION)) { - + log.error("No permission error.", e); } else { e.printStackTrace(); } @@ -263,21 +266,20 @@ public class ThriftConnection { // } if (!isAuthenticated) { log.info("ThriftConnection: Client not yet Authenticated. Trying..."); - String toEncrypt; if (theClient == null) { log.debug("The client was null"); return null; } try { - toEncrypt = theClient.startServerAuthentication(Globals + ByteBuffer tmpBuffer = theClient.startServerAuthentication(Globals .getOrganizationName()); + byte[] toEncrypt = new byte[tmpBuffer.remaining()]; + tmpBuffer.get(toEncrypt); log.info("The random String we want to encrypt: " + toEncrypt); - log.info("Length of the random String : " + toEncrypt.length()); - EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( - Globals.getThriftKeystoreAlias(), - Globals.getThriftKeystorePassword(), - Globals.getThriftKeystorePath()); - byte[] byteArray = rse.encryptString(toEncrypt); + log.info("Length of the random String : " + toEncrypt.length); + AsymEncryptionHandler aeh = new AsymEncryptionHandler(Globals.getPrivateKey()); + + byte[] byteArray = aeh.encryptMessage(toEncrypt); log.info( "Length of the byteArray of the random string after encryption :" + byteArray.length ); ByteBuffer b = ByteBuffer.wrap( byteArray ); log.info( "Length of the byteBuffer after encryption :" + b.remaining() ); -- cgit v1.2.3-55-g7522