You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/09/20 10:56:55 UTC
svn commit: r697335 - in
/geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main:
java/org/apache/geronimo/gshell/remote/crypto/ resources/META-INF/spring/
Author: jdillon
Date: Sat Sep 20 01:56:54 2008
New Revision: 697335
URL: http://svn.apache.org/viewvc?rev=697335&view=rev
Log:
Change CryptoContext to an interface, add CrytpoContextImpl
Make the transformation and provider spring configurable
Added some logging
Added:
geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java (contents, props changed)
- copied, changed from r697030, geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java
Modified:
geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java
geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/resources/META-INF/spring/components.xml
Modified: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java?rev=697335&r1=697334&r2=697335&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java (original)
+++ geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java Sat Sep 20 01:56:54 2008
@@ -20,117 +20,24 @@
package org.apache.geronimo.gshell.remote.crypto;
import java.security.Key;
-import java.security.KeyFactory;
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
import java.security.PublicKey;
-import java.security.spec.X509EncodedKeySpec;
-
-import javax.crypto.Cipher;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Provides an abstraction of the crypto bits which are required for some remote shell communications.
*
* @version $Rev$ $Date$
*/
-public class CryptoContext
+public interface CryptoContext
{
- private final Logger log = LoggerFactory.getLogger(getClass());
-
- //
- // TODO: See if we should use DSA or RSA for this...
- //
-
- private String transformation = "RSA";
-
- private String provider;
-
- private final KeyPair keyPair;
-
- public CryptoContext() throws Exception {
- KeyPairGenerator keyGen = createKeyPairGenerator();
- keyGen.initialize(1024);
-
- keyPair = keyGen.genKeyPair();
- }
-
- public CryptoContext(final String transformation, final String provider) throws Exception {
- this();
- this.transformation = transformation;
- this.provider = provider;
- }
-
- public PublicKey getPublicKey() {
- return keyPair.getPublic();
- }
-
- private byte[] codec(final int mode, final Key key, final byte[] bytes) throws Exception {
- assert key != null;
- assert bytes != null;
-
- Cipher cipher = createCipher();
- cipher.init(mode, key);
-
- return cipher.doFinal(bytes);
- }
-
- public byte[] encrypt(final Key key, final byte[] bytes) throws Exception {
- return codec(Cipher.ENCRYPT_MODE, key, bytes);
- }
-
- public byte[] encrypt(final byte[] bytes) throws Exception {
- return encrypt(keyPair.getPublic(), bytes);
- }
-
- public byte[] decrypt(final Key key, final byte[] bytes) throws Exception {
- return codec(Cipher.DECRYPT_MODE, key, bytes);
- }
-
- public byte[] decrypt(final byte[] bytes) throws Exception {
- return decrypt(keyPair.getPrivate(), bytes);
- }
-
- public PublicKey deserializePublicKey(final byte[] bytes) throws Exception {
- assert bytes != null;
-
- X509EncodedKeySpec spec = new X509EncodedKeySpec(bytes);
-
- KeyFactory keyFactory = createKeyFactory();
-
- return keyFactory.generatePublic(spec);
- }
-
- //
- // JCE Access
- //
-
- private KeyPairGenerator createKeyPairGenerator() throws Exception {
- if (provider != null) {
- return KeyPairGenerator.getInstance(transformation, provider);
- }
- else {
- return KeyPairGenerator.getInstance(transformation);
- }
- }
-
- private Cipher createCipher() throws Exception {
- if (provider != null) {
- return Cipher.getInstance(transformation, provider);
- }
- else {
- return Cipher.getInstance(transformation);
- }
- }
-
- private KeyFactory createKeyFactory() throws Exception {
- if (provider != null) {
- return KeyFactory.getInstance(transformation, provider);
- }
- else {
- return KeyFactory.getInstance(transformation);
- }
- }
+ PublicKey getPublicKey();
+
+ PublicKey deserializePublicKey(byte[] bytes) throws Exception;
+
+ byte[] encrypt(Key key, byte[] bytes) throws Exception;
+
+ byte[] encrypt(byte[] bytes) throws Exception;
+
+ byte[] decrypt(Key key, byte[] bytes) throws Exception;
+
+ byte[] decrypt(byte[] bytes) throws Exception;
}
\ No newline at end of file
Copied: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java (from r697030, geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java?p2=geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java&p1=geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java&r1=697030&r2=697335&rev=697335&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContext.java (original)
+++ geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java Sat Sep 20 01:56:54 2008
@@ -32,34 +32,41 @@
import org.slf4j.LoggerFactory;
/**
- * Provides an abstraction of the crypto bits which are required for some remote shell communications.
+ * Default {@link CryptoContext} component.
*
* @version $Rev$ $Date$
*/
-public class CryptoContext
+public class CryptoContextImpl
+ implements CryptoContext
{
private final Logger log = LoggerFactory.getLogger(getClass());
-
- //
- // TODO: See if we should use DSA or RSA for this...
- //
+
+ private final KeyPair keyPair;
private String transformation = "RSA";
private String provider;
- private final KeyPair keyPair;
-
- public CryptoContext() throws Exception {
+ public CryptoContextImpl() throws Exception {
KeyPairGenerator keyGen = createKeyPairGenerator();
keyGen.initialize(1024);
-
+
keyPair = keyGen.genKeyPair();
}
- public CryptoContext(final String transformation, final String provider) throws Exception {
- this();
+ public String getTransformation() {
+ return transformation;
+ }
+
+ public void setTransformation(final String transformation) {
this.transformation = transformation;
+ }
+
+ public String getProvider() {
+ return provider;
+ }
+
+ public void setProvider(final String provider) {
this.provider = provider;
}
@@ -102,34 +109,46 @@
return keyFactory.generatePublic(spec);
}
-
+
//
- // JCE Access
+ // JCE Component Access
//
private KeyPairGenerator createKeyPairGenerator() throws Exception {
- if (provider != null) {
+ if (provider != null && !provider.equals("default")) {
+ log.debug("Creating key-pair generator; using transformation: {} and provider: {}", transformation, provider);
+
return KeyPairGenerator.getInstance(transformation, provider);
}
else {
+ log.debug("Creating key-pair generator using transformation: {}", transformation);
+
return KeyPairGenerator.getInstance(transformation);
}
}
private Cipher createCipher() throws Exception {
- if (provider != null) {
+ if (provider != null && !provider.equals("default")) {
+ log.debug("Creating cipher; using transformation: {} and provider: {}", transformation, provider);
+
return Cipher.getInstance(transformation, provider);
}
else {
+ log.debug("Creating cipher; using transformation: {}", transformation);
+
return Cipher.getInstance(transformation);
}
}
private KeyFactory createKeyFactory() throws Exception {
- if (provider != null) {
+ if (provider != null && !provider.equals("default")) {
+ log.debug("Creating key factory; using transformation: {} and provider: {}", transformation, provider);
+
return KeyFactory.getInstance(transformation, provider);
}
else {
+ log.debug("Creating key factory; using transformation: {}", transformation);
+
return KeyFactory.getInstance(transformation);
}
}
Propchange: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java
------------------------------------------------------------------------------
svn:mergeinfo =
Propchange: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/crypto/CryptoContextImpl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/resources/META-INF/spring/components.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/resources/META-INF/spring/components.xml?rev=697335&r1=697334&r2=697335&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/resources/META-INF/spring/components.xml (original)
+++ geronimo/gshell/trunk/gshell-remote/gshell-remote-common/src/main/resources/META-INF/spring/components.xml Sat Sep 20 01:56:54 2008
@@ -29,6 +29,9 @@
<context:annotation-config/>
- <!-- TODO: -->
+ <bean id="cryptoContext" class="org.apache.geronimo.gshell.remote.crypto.CryptoContextImpl">
+ <property name="transformation" value="RSA"/>
+ <property name="provider" value="default"/>
+ </bean>
</beans>
\ No newline at end of file