summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbSatellite.java
blob: 65450ed08236a979acde3e844f7f8d2e740d16f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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 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, String publickeyString )
	{
		this.organization = organization;
		this.address = address;
		this.name = name;
		this.prefix = prefix;
		this.publickeyString = publickeyString;
	}

	public static DbSatellite fromOrganization( String organization )
	{
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"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;
	}

	public String getName()
	{
		return name;
	}

	public String getOrganization()
	{
		return organization;
	}

	public String getPrefix()
	{
		return this.prefix;
	}

	/**
	 * 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.organization + " is not valid.", e );
			} catch ( NumberFormatException e ) {
				LOG.info( "PubKey of " + this.organization + " is corrupted in database!", e );
			}
		}
		return publickey;
	}

}