From 8a5a90de7ed9712ad823dd60f0eecabddfd096bb Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 29 Sep 2014 18:15:35 +0200 Subject: [Db*] Fix SELECT for DbImage, change Timestamp to long, load public key from DB for satellite --- .../org/openslx/imagemaster/db/DbSatellite.java | 73 +++++++++++++++++----- 1 file changed, 59 insertions(+), 14 deletions(-) (limited to 'src/main/java/org/openslx/imagemaster/db/DbSatellite.java') diff --git a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java index fb35fb5..65450ed 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java +++ b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java @@ -1,6 +1,13 @@ package org.openslx.imagemaster.db; -import org.openslx.imagemaster.util.ByteArray; +import java.math.BigInteger; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPublicKeySpec; + +import org.apache.log4j.Logger; /** * Represents a satellite in the database. @@ -8,16 +15,31 @@ import org.openslx.imagemaster.util.ByteArray; */ public class DbSatellite { + private static final Logger LOG = Logger.getLogger( DbSatellite.class ); - private String organization, address, name, prefix; + private String organization, address, name, prefix, publickeyString; + private PublicKey publickey = null; + private static final KeyFactory keyFact; + + static + { + KeyFactory kf; + try { + kf = KeyFactory.getInstance( "RSA" ); + } catch ( NoSuchAlgorithmException e ) { + kf = null; + } + keyFact = kf; + } // needs to be public in order to be found by MySQL - public DbSatellite(String organization, String address, String name, String prefix) + public DbSatellite( String organization, String address, String name, String prefix, String publickeyString ) { this.organization = organization; this.address = address; this.name = name; this.prefix = prefix; + this.publickeyString = publickeyString; } public static DbSatellite fromOrganization( String organization ) @@ -25,10 +47,19 @@ public class DbSatellite return MySQL .findUniqueOrNull( DbSatellite.class, - "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix FROM satellite WHERE satellite.organization = ? LIMIT 1", + "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix, satellite.publickey FROM satellite WHERE satellite.organization = ? LIMIT 1", organization ); } + public static DbSatellite fromPrefix( String prefix ) + { + return MySQL + .findUniqueOrNull( + DbSatellite.class, + "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix, satellite.publickey FROM satellite WHERE satellite.prefix = ? LIMIT 1", + prefix ); + } + public String getAddress() { return address; @@ -49,17 +80,31 @@ public class DbSatellite return this.prefix; } - public static DbSatellite fromPrefix( String prefix ) + /** + * Get the public key of this organization, if known and valid. + * + * @return Public key, null on error or not known + */ + public PublicKey getPubkey() { - return MySQL - .findUniqueOrNull( - DbSatellite.class, - "SELECT satellite.organization, satellite.address, satellite.name, satellite.prefix FROM satellite WHERE satellite.prefix = ? LIMIT 1", - prefix ); + if ( publickey == null && keyFact != null && publickeyString != null ) { + String parts[] = publickeyString.split( " " ); + if ( parts.length != 2 ) + return null; + try { + BigInteger mod = new BigInteger( parts[0] ); + BigInteger exp = new BigInteger( parts[1] ); + RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, exp ); + synchronized ( keyFact ) { + publickey = keyFact.generatePublic( keySpec ); + } + } catch ( InvalidKeySpecException e ) { + LOG.info( "PubKey of " + this.organization + " is not valid.", e ); + } catch ( NumberFormatException e ) { + LOG.info( "PubKey of " + this.organization + " is corrupted in database!", e ); + } + } + return publickey; } - public static byte[] getKeyfromOrganization( String organization ) - { - return MySQL.findUniqueOrNull( ByteArray.class, "SELECT publickey FROM satellite WHERE organization = ?", organization ).array; - } } -- cgit v1.2.3-55-g7522