summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/encryption/AsymKeyHolder.java
blob: 6469f30e77816d0b00b2f3b54da032690b015b5b (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
package org.openslx.encryption;

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 RSAPrivateKey privKey = null;
	private static RSAPublicKey pubKey = null;

	/**
	 * Create private and/or public key according to given numbers.
	 * 
	 * @param privExp private exponent
	 * @param pubExp public exponent
	 * @param mod modulus of keypair
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	public AsymKeyHolder( BigInteger privExp, BigInteger pubExp, BigInteger mod )
			throws NoSuchAlgorithmException, InvalidKeySpecException
	{
		if ( mod == null )
			throw new InvalidKeySpecException( "No modulus given!" );
		final KeyFactory keyFact;
		keyFact = KeyFactory.getInstance( "RSA" );
		if ( pubExp != null ) {
			RSAPublicKeySpec keySpec = new RSAPublicKeySpec( mod, pubExp );
			pubKey = (RSAPublicKey)keyFact.generatePublic( keySpec );
		}
		if ( privExp != null ) {
			RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec( mod, privExp );
			privKey = (RSAPrivateKey)keyFact.generatePrivate( keySpec );
		}
	}

	/**
	 * Create new keypair.
	 */
	public AsymKeyHolder()
	{
		generateKey();
	}

	/**
	 * Get private key or null if not known.
	 * 
	 * @return sausages
	 */
	public PrivateKey getPrivateKey()
	{
		return privKey;
	}

	/**
	 * Get public key or null if not known.
	 * 
	 * @return public key
	 */
	public PublicKey getPublicKey()
	{
		return pubKey;
	}

	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();
		privKey = (RSAPrivateKey)kp.getPrivate();
		pubKey = (RSAPublicKey)kp.getPublic();

		BigInteger pubMod = pubKey.getModulus();
		BigInteger privMod = privKey.getModulus();
		assert ( pubMod.equals( privMod ) );
		return true;
	}

	public BigInteger getModulus()
	{
		if ( privKey != null )
			return privKey.getModulus();
		if ( pubKey != null )
			return pubKey.getModulus();
		return null; // Should never happen, unless only a modulus was given
	}

	public BigInteger getPrivateExponent()
	{
		if ( privKey == null )
			return null;
		return privKey.getPrivateExponent();
	}

	public BigInteger getPublicExponent()
	{
		if ( pubKey == null )
			return null;
		return pubKey.getPublicExponent();
	}

}