summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/AsymKeyHolder.java
blob: d69ce76b120e7dad1898ba591c249006d002a464 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package org.openslx.satellitedaemon;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import org.apache.log4j.Logger;

public class AsymKeyHolder
{
	private static final Logger LOG = Logger.getLogger( AsymKeyHolder.class );
	
	private static PrivateKey privKey = null;
	private static PublicKey pubKey = null;

	public AsymKeyHolder(BigInteger privExp, BigInteger pubExp, BigInteger mod)
			throws InvalidKeySpecException, NoSuchAlgorithmException {
		final KeyFactory keyFact;
		try {
			keyFact = KeyFactory.getInstance( "RSA" );
		} catch ( NoSuchAlgorithmException e ) {
			throw new NoSuchAlgorithmException(e.getMessage());
		}
		if (privExp == null) {
			// private exponent == null. Generate public key.
			if (mod != null) {
				try {
					RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, pubExp );
					synchronized ( keyFact ) {
						pubKey = keyFact.generatePublic( keySpec );
					}
				} catch ( InvalidKeySpecException e ) {
					LOG.error( "Not able to build key with given numbers.", e );
					throw new InvalidKeySpecException( e.getMessage() );
				} catch ( NumberFormatException e ) {
					LOG.error( "Invalid number format.", e );
					throw new NumberFormatException( e.toString() );
				}
			}
		} else if (pubExp == null) {
			// public exponent == null. Generate private key.
			if (mod != null) {
				try {
					RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, privExp );
					synchronized ( keyFact ) {
						privKey = keyFact.generatePrivate( keySpec );
					}
				} catch ( InvalidKeySpecException e ) {
					LOG.error( "Not able to build key with given numbers.", e );
					throw new InvalidKeySpecException( e.getMessage() );
				} catch ( NumberFormatException e ) {
					LOG.error( "Invalid number format.", e );
					throw new NumberFormatException( e.toString() );
				}
			}
		} else {
			// create both keys.
			if (mod != null) {
				try {
					RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( mod, pubExp );
					RSAPrivateKeySpec privkeySpec = new RSAPrivateKeySpec( mod, privExp );
					synchronized ( keyFact ) {
						privKey = keyFact.generatePrivate( privkeySpec );
						pubKey = keyFact.generatePublic( pubKeySpec );
					}
				} catch ( InvalidKeySpecException e ) {
					LOG.error( "Not able to build key with given numbers.", e );
					throw new InvalidKeySpecException( e.getMessage() );
				} catch ( NumberFormatException e ) {
					LOG.error( "Invalid number format.", e );
					throw new NumberFormatException( e.toString() );
				}
			}
		}
	}
	
	public AsymKeyHolder() throws NoSuchAlgorithmException {
		generateKey();
	}
	
	
	/**
	 * Get private key for this server. If none exists yet, create a new one.
	 * 
	 * @return
	 */
	public PrivateKey getPrivateKey()
	{
		if (privKey == null) {
			if (!generateKey()) {
				LOG.warn( "Could not load or generate keypair for communication with masterserver" );
			}
		}

		return privKey;
	}
	
	public PublicKey getPublicKey()
	{
		if (pubKey == null) {
			if (!generateKey()) {
				LOG.warn( "Could not generate keypair for communication with masterserver" );
			}
		}
		return pubKey;
	}

//	private boolean loadKey()
//	{
//		BufferedReader br = null;
//		String modulus, exponent;
//		KeyFactory keyFact;
//
//		try {
//			keyFact = KeyFactory.getInstance( "RSA" );
//		} catch ( NoSuchAlgorithmException e ) {
//			LOG.warn( "Could not get a KeyFactory to load the key from disk", e );
//			return false;
//		}
//
//		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 false;
//		} catch ( IOException e ) {
//			LOG.error( "File 'private.key' not correct readable.", e );
//			return false;
//		} finally {
//			try {
//				br.close();
//			} catch ( IOException e ) {
//			}
//		}
//		if ( modulus == null || exponent == null ) {
//			return false;
//		}
//
//		try {
//			BigInteger mod = new BigInteger( modulus );
//			BigInteger exp = new BigInteger( exponent );
//
//			RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, exp );
//			synchronized ( keyFact ) {
//				privKey = keyFact.generatePrivate( keySpec );
//			}
//			return privKey != null;
//		} catch ( InvalidKeySpecException e ) {
//			LOG.error( "Not able to build key with given numbers.", e );
//		} catch ( NumberFormatException e ) {
//			LOG.error( "Invalid number format.", e );
//		}
//		return false;
//	}
	
	private boolean generateKey()
	{
		KeyPairGenerator kpg;
		try {
			kpg = KeyPairGenerator.getInstance("RSA");
		} catch ( NoSuchAlgorithmException e ) {
			LOG.error( "NoSuchAlgorithmException", e );
			return false;
		}
		
		kpg.initialize(4096);
		KeyPair kp = kpg.generateKeyPair();
		RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate();
		RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
		
		BigInteger pubMod = publicKey.getModulus();
		BigInteger privMod = privateKey.getModulus();
		assert(pubMod == privMod);
		
		BigInteger pubExp = publicKey.getPublicExponent();
		BigInteger privExp = privateKey.getPrivateExponent();
		
		RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec( privMod, privExp );
		RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( pubMod, pubExp );
		
		KeyFactory keyFact;
		try {
			keyFact = KeyFactory.getInstance( "RSA" );
		} catch ( NoSuchAlgorithmException e ) {
			LOG.error( "NoSuchAlgorithmException", e );
			return false;
		}
		
		synchronized ( keyFact ) {
			try {
				privKey = keyFact.generatePrivate( privKeySpec );
				pubKey = keyFact.generatePublic( pubKeySpec );
			} catch ( InvalidKeySpecException e ) {
				LOG.error( "InvalidKeySpecException", e );
				return false;
			}
		}		
		return true;
	}

}