Author: matthieu
Date: Fri Dec 11 10:06:12 2015
New Revision: 1719309

URL: http://svn.apache.org/viewvc?rev=1719309&view=rev
Log:
JAMES-1644 Introduce Signature utils

Added:
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/JamesSignatureHandler.java
    
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/SignatureHandler.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerProvider.java
    
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerTest.java
    james/project/trunk/server/protocols/jmap/src/test/resources/
    james/project/trunk/server/protocols/jmap/src/test/resources/keystore
Modified:
    james/project/trunk/server/protocols/jmap/pom.xml

Modified: james/project/trunk/server/protocols/jmap/pom.xml
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/pom.xml?rev=1719309&r1=1719308&r2=1719309&view=diff
==============================================================================
--- james/project/trunk/server/protocols/jmap/pom.xml (original)
+++ james/project/trunk/server/protocols/jmap/pom.xml Fri Dec 11 10:06:12 2015
@@ -185,6 +185,10 @@
                     <scope>test</scope>
                 </dependency>
                 <dependency>
+                    <groupId>commons-codec</groupId>
+                    <artifactId>commons-codec</artifactId>
+                </dependency>
+                <dependency>
                     <groupId>javax.inject</groupId>
                     <artifactId>javax.inject</artifactId>
                 </dependency>

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/JamesSignatureHandler.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/JamesSignatureHandler.java?rev=1719309&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/JamesSignatureHandler.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/JamesSignatureHandler.java
 Fri Dec 11 10:06:12 2015
@@ -0,0 +1,88 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.crypto;
+
+import java.io.InputStream;
+import java.security.Key;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.Signature;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.lifecycle.api.Configurable;
+
+import com.google.common.base.Preconditions;
+
+public class JamesSignatureHandler implements SignatureHandler, Configurable {
+
+    public static final String ALIAS = "james";
+    public static final String ALGORITHM = "SHA1withRSA";
+    public static final String JKS = "JKS";
+    
+    private final FileSystem fileSystem;
+    private String secret;
+    private String keystoreURL;
+    private PrivateKey privateKey;
+    private PublicKey publicKey;
+
+    public JamesSignatureHandler(FileSystem fileSystem) {
+        this.fileSystem = fileSystem;
+    }
+
+    public void configure(HierarchicalConfiguration configuration) throws 
ConfigurationException {
+        keystoreURL = configuration.getString("tls.keystoreURL", 
"file://conf/keystoreURL");
+        secret = configuration.getString("tls.secret", "");
+    }
+
+    public void init() throws Exception {
+        KeyStore keystore = KeyStore.getInstance(JKS);
+        InputStream fis = fileSystem.getResource(keystoreURL);
+        keystore.load(fis, secret.toCharArray());
+        publicKey = keystore.getCertificate(ALIAS).getPublicKey();
+        Key key = keystore.getKey(ALIAS, secret.toCharArray());
+        if (! (key instanceof PrivateKey)) {
+            throw new Exception("Provided key is not a PrivateKey");
+        }
+        privateKey = (PrivateKey) key;
+    }
+
+    @Override
+    public String sign(String source) throws Exception {
+        Preconditions.checkNotNull(source);
+        Signature javaSignature = Signature.getInstance(ALGORITHM);
+        javaSignature.initSign(privateKey);
+        javaSignature.update(source.getBytes());
+        return new Base64().encodeAsString(javaSignature.sign());
+    }
+
+    @Override
+    public boolean verify(String source, String signature) throws Exception {
+        Preconditions.checkNotNull(source);
+        Preconditions.checkNotNull(signature);
+        Signature javaSignature = Signature.getInstance(ALGORITHM);
+        javaSignature.initVerify(publicKey);
+        javaSignature.update(source.getBytes());
+        return javaSignature.verify(new Base64().decode(signature));
+    }
+}

Added: 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/SignatureHandler.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/SignatureHandler.java?rev=1719309&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/SignatureHandler.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/main/java/org/apache/james/jmap/crypto/SignatureHandler.java
 Fri Dec 11 10:06:12 2015
@@ -0,0 +1,28 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.crypto;
+
+public interface SignatureHandler {
+
+    String sign(String source) throws Exception;
+
+    boolean verify(String source, String signature) throws Exception;
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerProvider.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerProvider.java?rev=1719309&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerProvider.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerProvider.java
 Fri Dec 11 10:06:12 2015
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.crypto;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.james.filesystem.api.FileSystem;
+
+import com.google.common.collect.Lists;
+
+public class JamesSignatureHandlerProvider {
+
+    public JamesSignatureHandler provide() throws Exception {
+        FileSystem fileSystem = new FileSystem() {
+            @Override
+            public InputStream getResource(String url) throws IOException {
+                return ClassLoader.getSystemResourceAsStream("keystore");
+            }
+
+            @Override
+            public File getFile(String fileURL) throws FileNotFoundException {
+                return null;
+            }
+
+            @Override
+            public File getBasedir() throws FileNotFoundException {
+                return null;
+            }
+        };
+        JamesSignatureHandler signatureHandler = new 
JamesSignatureHandler(fileSystem);
+        signatureHandler.configure(createTestCConfiguration());
+        signatureHandler.init();
+        return signatureHandler;
+    }
+
+    private HierarchicalConfiguration createTestCConfiguration() {
+        HierarchicalConfiguration configuration = new 
HierarchicalConfiguration();
+        HierarchicalConfiguration.Node secretNode = new 
HierarchicalConfiguration.Node();
+        secretNode.setName("secret");
+        secretNode.setValue("james72laBalle");
+        configuration.addNodes("tls", Lists.newArrayList(secretNode));
+        return configuration;
+    }
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerTest.java?rev=1719309&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JamesSignatureHandlerTest.java
 Fri Dec 11 10:06:12 2015
@@ -0,0 +1,85 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.crypto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.security.SignatureException;
+
+public class JamesSignatureHandlerTest {
+
+    public static final String SIGNATURE = 
"NeIFNei4p6vn085wCEw0pbEwJ+Oak5yEIRLZsDcRVzT9rWWOcLvDFUA3S6awi/bxPiFxqJFreVz6xqzehnUI4tUBupk3sIsqeXShhFWBpaV+m58mC41lT/A0RJa3GgCvg6kmweCRf3tOo0+gvwOQJdwCL2B21GjDCKqBHaiK+OHcsSjrQW0xuew5z84EAz3ErdH4MMNjITksxK5FG/cGQ9V6LQgwcPk0RrprVC4eY7FFHw/sQNlJpZKsSFLnn5igPQkQtjiQ4ay1/xoB7FU7aJLakxRhYOnTKgper/Ur7UWOZJaE+4EjcLwCFLF9GaCILwp9W+mf/f7j92PVEU50Vg==";
+    private static final String FAKE_SIGNATURE = 
"MeIFNei4p6vn085wCEw0pbEwJ+Oak5yEIRLZsDcRVzT9rWWOcLvDFUA3S6awi/bxPiFxqJFreVz6xqzehnUI4tUBupk3sIsqeXShhFWBpaV+m58mC41lT/A0RJa3GgCvg6kmweCRf3tOo0+gvwOQJdwCL2B21GjDCKqBHaiK+OHcsSjrQW0xuew5z84EAz3ErdH4MMNjITksxK5FG/cGQ9V6LQgwcPk0RrprVC4eY7FFHw/sQNlJpZKsSFLnn5igPQkQtjiQ4ay1/xoB7FU7aJLakxRhYOnTKgper/Ur7UWOZJaE+4EjcLwCFLF9GaCILwp9W+mf/f7j92PVEU50Vg==";
+    public static final String SOURCE = "plop";
+
+    private JamesSignatureHandler signatureHandler;
+
+    @Before
+    public void setUp() throws Exception {
+        signatureHandler = new JamesSignatureHandlerProvider().provide();
+    }
+
+    @Test
+    public void validSignatureShouldBeRecognised() throws Exception {
+        assertThat(signatureHandler.verify(SOURCE, 
signatureHandler.sign(SOURCE))).isTrue();
+    }
+
+    @Test
+    public void invalidSignatureShouldNotBeRecognised() throws Exception {
+        assertThat(signatureHandler.verify(SOURCE, 
signatureHandler.sign(FAKE_SIGNATURE))).isFalse();
+    }
+
+    @Test(expected = SignatureException.class)
+    public void incorrectLengthSignatureShouldThrow() throws Exception {
+        signatureHandler.verify(SOURCE, "signature");
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void signShouldThrowOnNullSource() throws Exception {
+        signatureHandler.sign(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void verifyShouldThrowOnNullSource() throws Exception {
+        signatureHandler.verify(null, "signature");
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void verifyShouldThrowOnNullSignature() throws Exception {
+        signatureHandler.verify(SOURCE, null);
+    }
+
+    @Test
+    public void signOutputShouldBeValid() throws Exception {
+        assertThat(signatureHandler.sign(SOURCE))
+            .isEqualTo(SIGNATURE);
+    }
+
+    @Test
+    public void verifyOutputShouldBeValid() throws Exception {
+        assertThat(signatureHandler.verify(SOURCE,
+            SIGNATURE))
+            .isTrue();
+    }
+
+}

Added: james/project/trunk/server/protocols/jmap/src/test/resources/keystore
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/resources/keystore?rev=1719309&view=auto
==============================================================================
    (empty)



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to