From eb19a453a95052138c924f293643d7909d685177 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 18 Mar 2011 11:35:38 -0700 Subject: [PATCH 1/3] added additional convenience methods --- .../java/hudson/model/UsageStatistics.java | 30 ++++++++++++++----- .../hudson/model/UsageStatisticsTest.java | 10 ++----- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index 5af4ca3de0..8800d9764a 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -47,8 +47,11 @@ import java.io.FilterInputStream; import java.io.InputStream; import java.io.DataInputStream; import java.security.GeneralSecurityException; +import java.security.Key; import java.security.KeyFactory; import java.security.PublicKey; +import java.security.interfaces.RSAKey; +import java.security.interfaces.RSAPublicKey; import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; import java.util.List; @@ -64,7 +67,7 @@ public class UsageStatistics extends PageDecorator { /** * Lazily computed {@link PublicKey} representation of {@link #keyImage}. */ - private volatile transient PublicKey key; + private volatile transient RSAPublicKey key; /** * When was the last time we asked a browser to send the usage stats for us? @@ -99,16 +102,13 @@ public class UsageStatistics extends PageDecorator { return false; } - private Cipher getCipher() { + private RSAPublicKey getKey() { try { if (key == null) { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - key = keyFactory.generatePublic(new X509EncodedKeySpec(Util.fromHexString(keyImage))); + key = (RSAPublicKey)keyFactory.generatePublic(new X509EncodedKeySpec(Util.fromHexString(keyImage))); } - - Cipher cipher = Secret.getCipher("RSA"); - cipher.init(Cipher.ENCRYPT_MODE, key); - return cipher; + return key; } catch (GeneralSecurityException e) { throw new Error(e); // impossible } @@ -166,7 +166,7 @@ public class UsageStatistics extends PageDecorator { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string - OutputStreamWriter w = new OutputStreamWriter(new GZIPOutputStream(new CombinedCipherOutputStream(baos,getCipher(),"AES")), "UTF-8"); + OutputStreamWriter w = new OutputStreamWriter(new GZIPOutputStream(new CombinedCipherOutputStream(baos,getKey(),"AES")), "UTF-8"); o.write(w); w.close(); @@ -197,6 +197,10 @@ public class UsageStatistics extends PageDecorator { sym.init(Cipher.ENCRYPT_MODE,symKey); super.out = new CipherOutputStream(out,sym); } + + public CombinedCipherOutputStream(OutputStream out, RSAKey key, String algorithm) throws IOException, GeneralSecurityException { + this(out,toCipher(key,Cipher.ENCRYPT_MODE),algorithm); + } } /** @@ -221,6 +225,16 @@ public class UsageStatistics extends PageDecorator { sym.init(Cipher.DECRYPT_MODE,symKey); super.in = new CipherInputStream(in,sym); } + + public CombinedCipherInputStream(InputStream in, RSAKey key, String algorithm, int keyLength) throws IOException, GeneralSecurityException { + this(in,toCipher(key,Cipher.DECRYPT_MODE),algorithm,keyLength); + } + } + + private static Cipher toCipher(RSAKey key, int mode) throws GeneralSecurityException { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(mode, (Key)key); + return cipher; } /** diff --git a/test/src/test/java/hudson/model/UsageStatisticsTest.java b/test/src/test/java/hudson/model/UsageStatisticsTest.java index 723797663c..9acc9a4dda 100644 --- a/test/src/test/java/hudson/model/UsageStatisticsTest.java +++ b/test/src/test/java/hudson/model/UsageStatisticsTest.java @@ -30,11 +30,10 @@ import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; import org.jvnet.hudson.test.HudsonTestCase; -import javax.crypto.Cipher; import java.io.ByteArrayInputStream; import java.io.InputStreamReader; import java.security.KeyFactory; -import java.security.PrivateKey; +import java.security.interfaces.RSAPrivateKey; import java.security.spec.PKCS8EncodedKeySpec; import java.util.zip.GZIPInputStream; @@ -54,14 +53,11 @@ public class UsageStatisticsTest extends HudsonTestCase { System.out.println(data); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - PrivateKey priv = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Util.fromHexString(privateKey))); - - Cipher cipher = Cipher.getInstance("RSA"); - cipher.init(Cipher.DECRYPT_MODE, priv); + RSAPrivateKey priv = (RSAPrivateKey)keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Util.fromHexString(privateKey))); byte[] cipherText = Base64.decode(data.toCharArray()); InputStreamReader r = new InputStreamReader(new GZIPInputStream( - new CombinedCipherInputStream(new ByteArrayInputStream(cipherText),cipher,"AES",1024)), "UTF-8"); + new CombinedCipherInputStream(new ByteArrayInputStream(cipherText),priv,"AES",1024)), "UTF-8"); JSONObject o = JSONObject.fromObject(IOUtils.toString(r)); System.out.println(o); assertEquals(1,o.getInt("stat")); From c072b3cb6cefc0a1e544ed403bf224ccf662a5a3 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 18 Mar 2011 12:02:44 -0700 Subject: [PATCH 2/3] key length can be obtained from the key --- core/src/main/java/hudson/model/UsageStatistics.java | 4 ++-- test/src/test/java/hudson/model/UsageStatisticsTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index 8800d9764a..8de447e302 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -226,8 +226,8 @@ public class UsageStatistics extends PageDecorator { super.in = new CipherInputStream(in,sym); } - public CombinedCipherInputStream(InputStream in, RSAKey key, String algorithm, int keyLength) throws IOException, GeneralSecurityException { - this(in,toCipher(key,Cipher.DECRYPT_MODE),algorithm,keyLength); + public CombinedCipherInputStream(InputStream in, RSAKey key, String algorithm) throws IOException, GeneralSecurityException { + this(in,toCipher(key,Cipher.DECRYPT_MODE),algorithm,key.getModulus().bitLength()); } } diff --git a/test/src/test/java/hudson/model/UsageStatisticsTest.java b/test/src/test/java/hudson/model/UsageStatisticsTest.java index 9acc9a4dda..38cbe50477 100644 --- a/test/src/test/java/hudson/model/UsageStatisticsTest.java +++ b/test/src/test/java/hudson/model/UsageStatisticsTest.java @@ -57,7 +57,7 @@ public class UsageStatisticsTest extends HudsonTestCase { byte[] cipherText = Base64.decode(data.toCharArray()); InputStreamReader r = new InputStreamReader(new GZIPInputStream( - new CombinedCipherInputStream(new ByteArrayInputStream(cipherText),priv,"AES",1024)), "UTF-8"); + new CombinedCipherInputStream(new ByteArrayInputStream(cipherText),priv,"AES")), "UTF-8"); JSONObject o = JSONObject.fromObject(IOUtils.toString(r)); System.out.println(o); assertEquals(1,o.getInt("stat")); From 04584b806b9a657e757198ed1c6d7602ba3a6944 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 18 Mar 2011 15:14:48 -0700 Subject: [PATCH 3/3] support streaming ciphers with modes other than EBC. --- .../main/java/hudson/model/UsageStatistics.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/hudson/model/UsageStatistics.java b/core/src/main/java/hudson/model/UsageStatistics.java index 8de447e302..d7c799564d 100644 --- a/core/src/main/java/hudson/model/UsageStatistics.java +++ b/core/src/main/java/hudson/model/UsageStatistics.java @@ -38,6 +38,7 @@ import javax.crypto.CipherOutputStream; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.CipherInputStream; +import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.io.OutputStreamWriter; @@ -187,14 +188,15 @@ public class UsageStatistics extends PageDecorator { super(out); // create a new symmetric cipher key used for this stream - SecretKey symKey = KeyGenerator.getInstance(algorithm).generateKey(); + String keyAlgorithm = getKeyAlgorithm(algorithm); + SecretKey symKey = KeyGenerator.getInstance(keyAlgorithm).generateKey(); // place the symmetric key by encrypting it with asymmetric cipher out.write(asym.doFinal(symKey.getEncoded())); // the rest of the data will be encrypted by this symmetric cipher Cipher sym = Secret.getCipher(algorithm); - sym.init(Cipher.ENCRYPT_MODE,symKey); + sym.init(Cipher.ENCRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded())); super.out = new CipherOutputStream(out,sym); } @@ -215,14 +217,16 @@ public class UsageStatistics extends PageDecorator { public CombinedCipherInputStream(InputStream in, Cipher asym, String algorithm, int keyLength) throws IOException, GeneralSecurityException { super(in); + String keyAlgorithm = getKeyAlgorithm(algorithm); + // first read the symmetric key cipher byte[] symKeyBytes = new byte[keyLength/8]; new DataInputStream(in).readFully(symKeyBytes); - SecretKey symKey = new SecretKeySpec(asym.doFinal(symKeyBytes),algorithm); + SecretKey symKey = new SecretKeySpec(asym.doFinal(symKeyBytes),keyAlgorithm); // the rest of the data will be decrypted by this symmetric cipher Cipher sym = Secret.getCipher(algorithm); - sym.init(Cipher.DECRYPT_MODE,symKey); + sym.init(Cipher.DECRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded())); super.in = new CipherInputStream(in,sym); } @@ -231,6 +235,11 @@ public class UsageStatistics extends PageDecorator { } } + private static String getKeyAlgorithm(String algorithm) { + int index = algorithm.indexOf('/'); + return (index>0)?algorithm.substring(0,index):algorithm; + } + private static Cipher toCipher(RSAKey key, int mode) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(mode, (Key)key);