summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbSatellite.java
blob: a1c9b1ae94e5fac24cb40075e1bf25b2f761cb28 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                   







                                                  
 



                                          

                        
                                                                                
 
                                                                      












                                                             

                                                           
                                                                                                                       
         
                                                     

                                       
                                                       

         
                                                                             
         


                                                                  












                                                                                                                                                                                                    

         

                                                             
                  


                                                                  
                                                                                                                                                                                          
                                                         

                                                  

         

                                  


                               

                               


                            
                                         
         
                                      
         
 





                                                                       
         











                                                                                            
                                                                                           
                                                             
                                                                                                       


                                 
         
 
 
package org.openslx.imagemaster.db;

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.
 * Is used to authenticate the satellite.
 */
public class DbSatellite
{
	private static final Logger LOG = Logger.getLogger( DbSatellite.class );

	private String organizationId, address, name, 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 organizationId, String address, String name, String prefix, String publickeyString )
	{
		this.organizationId = organizationId;
		this.address = address;
		this.name = name;
		this.publickeyString = publickeyString;
	}

	public static DbSatellite fromOrganizationId( String organizationId )
	{
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"SELECT satellite.organizationid, satellite.address, satellite.name, satellite.publickey FROM satellite WHERE satellite.organizationid = ? LIMIT 1",
						organizationId );
	}

	public static DbSatellite fromSuffix( String suffix )
	{
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"SELECT satellite.organizationid, satellite.address, satellite.name, satellite.publickey FROM satellite"
						+ " INNER JOIN satellite_suffix USING (organizationid)"
						+ " WHERE satellite_suffix.suffix = ? LIMIT 1",
						suffix );
	}

	public static DbSatellite fromPrefix( String prefix )
	{
		/*
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"SELECT satellite.organization, satellite.address, satellite.name, satellite.publickey FROM satellite WHERE satellite.prefix = ? LIMIT 1",
						prefix );
						*/
		return null;
	}

	public String getAddress()
	{
		return address;
	}

	public String getName()
	{
		return name;
	}

	public String getOrganizationId()
	{
		return organizationId;
	}

	/**
	 * Get the public key of this organization, if known and valid.
	 * 
	 * @return Public key, null on error or not known
	 */
	public PublicKey getPubkey()
	{
		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.name + " is not valid.", e );
			} catch ( NumberFormatException e ) {
				LOG.info( "PubKey of " + this.name + " is corrupted in database!", e );
			}
		}
		return publickey;
	}

}