cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1583010 - in /cayenne/main/trunk/cayenne-crypto/src: main/java/org/apache/cayenne/crypto/transformer/value/ test/java/org/apache/cayenne/crypto/transformer/value/
Date Sat, 29 Mar 2014 15:45:07 GMT
Author: aadamchik
Date: Sat Mar 29 15:45:07 2014
New Revision: 1583010

URL: http://svn.apache.org/r1583010
Log:
CAY-1916 cayenne-crypto module that enables data encryption for certain model attributes

* JceTransformer/JceTransformerFactory - in progress

Added:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/Base64FromBytesConverter.java
      - copied, changed from r1583009, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/FromBytesConverter.java
      - copied, changed from r1583009, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
Modified:
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptor.java
    cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
    cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptorTest.java

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/Base64FromBytesConverter.java
(from r1583009, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/Base64FromBytesConverter.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/Base64FromBytesConverter.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java&r1=1583009&r2=1583010&rev=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/Base64FromBytesConverter.java
Sat Mar 29 15:45:07 2014
@@ -18,26 +18,22 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.value;
 
-import java.nio.charset.Charset;
+import javax.xml.bind.DatatypeConverter;
 
 /**
+ * Generates a Base64-encoded String from a byte array. generated String does
+ * not contain line breaks that are used for MIME Base64, but are meaningless in
+ * a DB.
+ * 
  * @since 3.2
  */
-final class StringToBytesConverter implements ToBytesConverter {
+class Base64FromBytesConverter implements FromBytesConverter {
 
-    private static final String DEFAULT_CHARSET = "UTF-8";
-
-    static final ToBytesConverter INSTANCE = new StringToBytesConverter();
-
-    private Charset utf8;
-
-    StringToBytesConverter() {
-        this.utf8 = Charset.forName(DEFAULT_CHARSET);
-    }
+    static final FromBytesConverter INSTANCE = new Base64FromBytesConverter();
 
     @Override
-    public byte[] toBytes(Object value) {
-        return ((String) value).getBytes(utf8);
+    public Object fromBytes(byte[] bytes) {
+        return DatatypeConverter.printBase64Binary(bytes);
     }
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
Sat Mar 29 15:45:07 2014
@@ -21,13 +21,18 @@ package org.apache.cayenne.crypto.transf
 /**
  * @since 3.2
  */
-final class BytesToBytesConverter implements ToBytesConverter {
+final class BytesToBytesConverter implements ToBytesConverter, FromBytesConverter {
 
-    static final ToBytesConverter INSTANCE = new BytesToBytesConverter();
+    static final BytesToBytesConverter INSTANCE = new BytesToBytesConverter();
 
     @Override
     public byte[] toBytes(Object value) {
         return (byte[]) value;
     }
 
+    @Override
+    public Object fromBytes(byte[] bytes) {
+        return bytes;
+    }
+
 }

Copied: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/FromBytesConverter.java
(from r1583009, cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/FromBytesConverter.java?p2=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/FromBytesConverter.java&p1=cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java&r1=1583009&r2=1583010&rev=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/BytesToBytesConverter.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/FromBytesConverter.java
Sat Mar 29 15:45:07 2014
@@ -21,13 +21,8 @@ package org.apache.cayenne.crypto.transf
 /**
  * @since 3.2
  */
-final class BytesToBytesConverter implements ToBytesConverter {
+interface FromBytesConverter {
 
-    static final ToBytesConverter INSTANCE = new BytesToBytesConverter();
-
-    @Override
-    public byte[] toBytes(Object value) {
-        return (byte[]) value;
-    }
+    Object fromBytes(byte[] bytes);
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactory.java
Sat Mar 29 15:45:07 2014
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.cayenne.crypto.transformer.value;
 
+import java.sql.Types;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -41,10 +42,14 @@ import org.apache.cayenne.map.ObjEntity;
 public class JceTransformerFactory implements ValueTransformerFactory {
 
     private Map<String, ToBytesConverter> toBytesConverters;
+    private Map<Integer, FromBytesConverter> fromBytesConverters;
+
     private ConcurrentMap<DbAttribute, ValueTransformer> encryptors;
 
     public JceTransformerFactory() {
         this.toBytesConverters = createToBytesConverters();
+        this.fromBytesConverters = createFromBytesConverters();
+
         this.encryptors = new ConcurrentHashMap<DbAttribute, ValueTransformer>();
     }
 
@@ -77,16 +82,39 @@ public class JceTransformerFactory imple
         return map;
     }
 
+    protected Map<Integer, FromBytesConverter> createFromBytesConverters() {
+        Map<Integer, FromBytesConverter> map = new HashMap<Integer, FromBytesConverter>();
+
+        map.put(Types.BINARY, BytesToBytesConverter.INSTANCE);
+        map.put(Types.BLOB, BytesToBytesConverter.INSTANCE);
+        map.put(Types.VARBINARY, BytesToBytesConverter.INSTANCE);
+        map.put(Types.LONGVARBINARY, BytesToBytesConverter.INSTANCE);
+
+        map.put(Types.CHAR, Base64FromBytesConverter.INSTANCE);
+        map.put(Types.CLOB, Base64FromBytesConverter.INSTANCE);
+        map.put(Types.LONGNVARCHAR, Base64FromBytesConverter.INSTANCE);
+        map.put(Types.VARCHAR, Base64FromBytesConverter.INSTANCE);
+
+        return map;
+    }
+
     protected ValueTransformer createEncryptor(DbAttribute a) {
 
         String type = getJavaType(a);
+
         ToBytesConverter toBytes = toBytesConverters.get(type);
         if (toBytes == null) {
             throw new IllegalArgumentException("The type " + type + " for attribute " + a
-                    + " has no to-byte conversion");
+                    + " has no to-bytes conversion");
+        }
+
+        FromBytesConverter fromBytes = fromBytesConverters.get(a.getType());
+        if (fromBytes == null) {
+            throw new IllegalArgumentException("The type " + type + " for attribute " + a
+                    + " has no from-bytes conversion");
         }
 
-        return new JceValueEncryptor(toBytes);
+        return new JceValueEncryptor(toBytes, fromBytes);
     }
 
     // TODO: calculating Java type of ObjAttribute may become unneeded per

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptor.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptor.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptor.java
Sat Mar 29 15:45:07 2014
@@ -29,16 +29,26 @@ import org.apache.cayenne.crypto.Cayenne
  */
 public class JceValueEncryptor implements ValueTransformer {
 
-    final ToBytesConverter toBytes;
+    private ToBytesConverter preConverter;
+    private FromBytesConverter postConverter;
 
-    public JceValueEncryptor(ToBytesConverter toBytes) {
-        this.toBytes = toBytes;
+    public JceValueEncryptor(ToBytesConverter preConverter, FromBytesConverter postConverter)
{
+        this.preConverter = preConverter;
+        this.postConverter = postConverter;
+    }
+
+    ToBytesConverter getPreConverter() {
+        return preConverter;
+    }
+
+    FromBytesConverter getPostConverter() {
+        return postConverter;
     }
 
     @Override
     public Object transform(Cipher cipher, Object value) {
 
-        byte[] bytes = toBytes.toBytes(value);
+        byte[] bytes = preConverter.toBytes(value);
         byte[] transformed;
 
         try {
@@ -49,7 +59,7 @@ public class JceValueEncryptor implement
             throw new CayenneCryptoException("Bad padding", e);
         }
 
-        return transformed;
+        return postConverter.fromBytes(transformed);
     }
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/main/java/org/apache/cayenne/crypto/transformer/value/StringToBytesConverter.java
Sat Mar 29 15:45:07 2014
@@ -25,7 +25,7 @@ import java.nio.charset.Charset;
  */
 final class StringToBytesConverter implements ToBytesConverter {
 
-    private static final String DEFAULT_CHARSET = "UTF-8";
+    static final String DEFAULT_CHARSET = "UTF-8";
 
     static final ToBytesConverter INSTANCE = new StringToBytesConverter();
 

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceTransformerFactoryTest.java
Sat Mar 29 15:45:07 2014
@@ -20,7 +20,6 @@ package org.apache.cayenne.crypto.transf
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
@@ -84,14 +83,17 @@ public class JceTransformerFactoryTest {
         ValueTransformer t1 = f.createEncryptor(t1_ct);
         assertNotNull(t1);
         assertTrue(t1 instanceof JceValueEncryptor);
-        assertNotSame(BytesToBytesConverter.INSTANCE, ((JceValueEncryptor) t1).toBytes);
+        assertSame(StringToBytesConverter.INSTANCE, ((JceValueEncryptor) t1).getPreConverter());
+        assertSame(Base64FromBytesConverter.INSTANCE, ((JceValueEncryptor) t1).getPostConverter());
 
         DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
 
         ValueTransformer t2 = f.createEncryptor(t2_cb);
         assertNotNull(t2);
         assertTrue(t2 instanceof JceValueEncryptor);
-        assertSame(BytesToBytesConverter.INSTANCE, ((JceValueEncryptor) t2).toBytes);
+        assertSame(BytesToBytesConverter.INSTANCE, ((JceValueEncryptor) t2).getPreConverter());
+        assertSame(BytesToBytesConverter.INSTANCE, ((JceValueEncryptor) t2).getPostConverter());
+
     }
 
 }

Modified: cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptorTest.java?rev=1583010&r1=1583009&r2=1583010&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptorTest.java
(original)
+++ cayenne/main/trunk/cayenne-crypto/src/test/java/org/apache/cayenne/crypto/transformer/value/JceValueEncryptorTest.java
Sat Mar 29 15:45:07 2014
@@ -20,6 +20,7 @@ package org.apache.cayenne.crypto.transf
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.UnsupportedEncodingException;
@@ -32,6 +33,7 @@ import javax.crypto.IllegalBlockSizeExce
 import javax.crypto.KeyGenerator;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
+import javax.xml.bind.DatatypeConverter;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -60,7 +62,7 @@ public class JceValueEncryptorTest {
     @Test
     public void testTransform_BytesToBytes() throws IllegalBlockSizeException, BadPaddingException
{
 
-        JceValueEncryptor e = new JceValueEncryptor(BytesToBytesConverter.INSTANCE);
+        JceValueEncryptor e = new JceValueEncryptor(BytesToBytesConverter.INSTANCE, BytesToBytesConverter.INSTANCE);
 
         byte[] b1 = new byte[] { 1, 2 };
         byte[] b2 = new byte[] { 2, 3 };
@@ -81,7 +83,7 @@ public class JceValueEncryptorTest {
     @Test
     public void testTransform_BytesToBytes_DifferentSizes() {
 
-        JceValueEncryptor e = new JceValueEncryptor(BytesToBytesConverter.INSTANCE);
+        JceValueEncryptor e = new JceValueEncryptor(BytesToBytesConverter.INSTANCE, BytesToBytesConverter.INSTANCE);
 
         int blockSize = encCipher.getBlockSize();
 
@@ -119,7 +121,7 @@ public class JceValueEncryptorTest {
     public void testTransform_StringToBytes() throws UnsupportedEncodingException, IllegalBlockSizeException,
             BadPaddingException {
 
-        JceValueEncryptor e = new JceValueEncryptor(StringToBytesConverter.INSTANCE);
+        JceValueEncryptor e = new JceValueEncryptor(StringToBytesConverter.INSTANCE, BytesToBytesConverter.INSTANCE);
 
         String s1 = "ab";
         String s2 = "cd";
@@ -128,12 +130,44 @@ public class JceValueEncryptorTest {
 
         assertNotNull(b1_t);
         assertEquals(encCipher.getBlockSize(), b1_t.length);
-        assertEquals(s1, new String(decCipher.doFinal(b1_t), "UTF-8"));
+        assertEquals(s1, new String(decCipher.doFinal(b1_t), StringToBytesConverter.DEFAULT_CHARSET));
 
         byte[] b2_t = (byte[]) e.transform(encCipher, s2);
 
         assertNotNull(b2_t);
         assertEquals(encCipher.getBlockSize(), b2_t.length);
-        assertEquals(s2, new String(decCipher.doFinal(b2_t), "UTF-8"));
+        assertEquals(s2, new String(decCipher.doFinal(b2_t), StringToBytesConverter.DEFAULT_CHARSET));
+    }
+
+    @Test
+    public void testTransform_StringToString() throws UnsupportedEncodingException, IllegalBlockSizeException,
+            BadPaddingException {
+
+        JceValueEncryptor e = new JceValueEncryptor(StringToBytesConverter.INSTANCE, Base64FromBytesConverter.INSTANCE);
+
+        String s1 = "ab";
+
+        // try to get beyond a single block boundary and a Base64 line...
+        String s2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+                + "Pellentesque nisi sapien, mattis eu porttitor in, tempus quis lorem. "
+                + "Integer vel dignissim quam. Maecenas pellentesque est erat, eget semper
ipsum aliquet vitae. "
+                + "Donec convallis mi vitae luctus rutrum. Sed ut imperdiet ante. Praesent
condimentum velit eget "
+                + "felis pretium blandit. Praesent lacus tortor, facilisis eget sapien quis,
hendrerit iaculis tellus.";
+
+        String s1_t = (String) e.transform(encCipher, s1);
+
+        assertNotNull(s1_t);
+        assertNotEquals(s1_t, s1);
+
+        byte[] b1_t = DatatypeConverter.parseBase64Binary(s1_t);
+        assertEquals(s1, new String(decCipher.doFinal(b1_t), StringToBytesConverter.DEFAULT_CHARSET));
+
+        String s2_t = (String) e.transform(encCipher, s2);
+
+        assertNotNull(s2_t);
+        assertNotEquals(s2_t, s2);
+
+        byte[] b2_t = DatatypeConverter.parseBase64Binary(s2_t);
+        assertEquals(s2, new String(decCipher.doFinal(b2_t), StringToBytesConverter.DEFAULT_CHARSET));
     }
 }



Mime
View raw message