blob: 7eab79f07be390427653e29fb3944a4c6fdd71e7 (
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
|
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.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
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;
/**
* Get private key for this server. If none exists yet, create a new one.
*
* @return
*/
public static PrivateKey getPrivateKey()
{
if (privKey == null) {
if (!loadKey() && !generateKey()) {
LOG.warn( "Could not load or generate keypair for communication with masterserver" );
}
}
return privKey;
}
private static boolean loadKey()
{
BufferedReader br = null;
String modulus, exponent;
KeyFactory keyFact;
try {
keyFact = KeyFactory.getInstance( "RSA" );
} catch ( NoSuchAlgorithmException nSAE ) {
LOG.warn( "Could not get a KeyFactory to load the key from disk", nSAE );
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 static boolean generateKey()
{
// KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// kpg.initialize(4096);
// KeyPair kp = kpg.generateKeyPair();
// RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate();
// RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
//
// log.debug("modulus: " + privateKey.getModulus().toString());
// log.debug("exponent: " + privateKey.getPrivateExponent().toString());
//
//
// log.debug("modulus: " + publicKey.getModulus().toString());
// log.debug("exponent: " + publicKey.getPublicExponent().toString());
//
// System.exit(1);
return true;
}
}
|