summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/models/LocalOrganization.java
blob: 4b5f076ba17a2b0a4e36a3a948a032a592d86438 (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
package org.openslx.imagemaster.db.models;

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;

import org.apache.log4j.Logger;
import org.openslx.encryption.AsymKeyHolder;

public class LocalOrganization
{

	private static final Logger LOGGER = Logger.getLogger( LocalOrganization.class );

	private PublicKey publickey;

	private String publickeyString;

	private String name;

	/**
	 * 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 && publickeyString != null ) {
			String parts[] = publickeyString.split( " " );
			BigInteger mod = null;
			BigInteger exp = null;
			for ( int i = 0; i < parts.length; ++i ) {
				if ( parts[i].startsWith( "mod:" ) ) {
					// modulus found.
					mod = new BigInteger( parts[i].substring( 4 ) );
				}
				if ( parts[i].startsWith( "exp:" ) ) {
					// exponent found.
					exp = new BigInteger( parts[i].substring( 4 ) );
				}
			}
			if ( mod == null ) {
				LOGGER.error( "No modulus for building public key was found." );
				return null;
			}
			if ( exp == null ) {
				LOGGER.error( "No public exponent for building public key was found." );
				return null;
			}
			try {
				publickey = new AsymKeyHolder( null, exp, mod ).getPublicKey();
			} catch ( InvalidKeySpecException | NoSuchAlgorithmException e ) {
				LOGGER.info( "PubKey of " + this.name + " is not valid.", e );
			} catch ( NumberFormatException e ) {
				LOGGER.info( "PubKey of " + this.name + " is corrupted in database!", e );
			}
		}
		return publickey;
	}

}