summaryrefslogtreecommitdiff
path: root/qpid/java/common
diff options
context:
space:
mode:
authorAndrew MacBean <macbean@apache.org>2015-01-14 10:38:04 +0000
committerAndrew MacBean <macbean@apache.org>2015-01-14 10:38:04 +0000
commit9b1d37a0cbef71478b58c6acee4f72a2474a9f7d (patch)
tree3ee1529447ca2606eed37d1d6d0293b86f3c2e58 /qpid/java/common
parentf457cc314c6bc692731a87e8fed86d049e7c66c6 (diff)
downloadqpid-python-9b1d37a0cbef71478b58c6acee4f72a2474a9f7d.tar.gz
QPID-6304: [Java Broker] Allow truststore and keystore (JKS) files to be stored as a data:// URL inside the config
* Added truststore/keystore unit tests too to cover both new and (most of) the existing functionality, retiring the equivilent slower REST system tests. * Added single REST test exercising the creation of a keystore/teststore from data:// URL. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1651615 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/common')
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java11
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java18
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java32
-rw-r--r--qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java64
4 files changed, 87 insertions, 38 deletions
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
index 0dccf37979..c61684e2bb 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/QpidClientX509KeyManager.java
@@ -27,6 +27,7 @@ import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import java.io.IOException;
import java.net.Socket;
+import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Principal;
@@ -50,6 +51,16 @@ public class QpidClientX509KeyManager extends X509ExtendedKeyManager
this.delegate = (X509ExtendedKeyManager)kmf.getKeyManagers()[0];
}
+ public QpidClientX509KeyManager(String alias, URL keyStoreUrl, String keyStoreType,
+ String keyStorePassword, String keyManagerFactoryAlgorithmName) throws GeneralSecurityException, IOException
+ {
+ this.alias = alias;
+ KeyStore ks = SSLUtil.getInitializedKeyStore(keyStoreUrl,keyStorePassword,keyStoreType);
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithmName);
+ kmf.init(ks, keyStorePassword.toCharArray());
+ this.delegate = (X509ExtendedKeyManager)kmf.getKeyManagers()[0];
+ }
+
public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)
{
log.debug("chooseClientAlias:Returning alias " + alias);
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
index 98229fd2a1..b6ae2ab4a3 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/transport/network/security/ssl/SSLUtil.java
@@ -24,6 +24,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.Principal;
@@ -248,6 +249,23 @@ public class SSLUtil
return ks;
}
+ public static KeyStore getInitializedKeyStore(URL storePath, String storePassword, String keyStoreType) throws GeneralSecurityException, IOException
+ {
+ KeyStore ks = KeyStore.getInstance(keyStoreType);
+ try(InputStream in = storePath.openStream())
+ {
+ if (in == null && !"PKCS11".equalsIgnoreCase(keyStoreType)) // PKCS11 will not require an explicit path
+ {
+ throw new IOException("Unable to load keystore resource: " + storePath);
+ }
+
+ char[] storeCharPassword = storePassword == null ? null : storePassword.toCharArray();
+
+ ks.load(in, storeCharPassword);
+ }
+ return ks;
+ }
+
public static void removeSSLv3Support(final SSLEngine engine)
{
List<String> enabledProtocols = Arrays.asList(engine.getEnabledProtocols());
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java
new file mode 100644
index 0000000000..16c5012d88
--- /dev/null
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/DataUrlUtils.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qpid.util;
+
+import javax.xml.bind.DatatypeConverter;
+
+public class DataUrlUtils
+{
+ public static String getDataUrlForBytes(final byte[] bytes)
+ {
+ StringBuilder inlineURL = new StringBuilder("data:;base64,");
+ inlineURL.append(DatatypeConverter.printBase64Binary(bytes));
+ return inlineURL.toString();
+ }
+}
diff --git a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
index dd347b54eb..70607f49db 100644
--- a/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
+++ b/qpid/java/common/src/main/java/org/apache/qpid/util/FileUtils.java
@@ -22,6 +22,7 @@ package org.apache.qpid.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -51,39 +52,32 @@ public class FileUtils
*
* @return The contents of the file.
*/
- public static String readFileAsString(String filename)
+ public static byte[] readFileAsBytes(String filename)
{
- BufferedInputStream is = null;
- try
+ try(BufferedInputStream is = new BufferedInputStream(new FileInputStream(filename)))
{
- try
- {
- is = new BufferedInputStream(new FileInputStream(filename));
- }
- catch (FileNotFoundException e)
- {
- throw new RuntimeException(e);
- }
-
return readStreamAsString(is);
}
- finally
+ catch (IOException e)
{
- if (is != null)
- {
- try
- {
- is.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- }
+ throw new RuntimeException(e);
}
}
+
+ /**
+ * Reads a text file as a string.
+ *
+ * @param filename The name of the file.
+ *
+ * @return The contents of the file.
+ */
+ public static String readFileAsString(String filename)
+ {
+ return new String(readFileAsBytes(filename));
+ }
+
/**
* Reads a text file as a string.
*
@@ -93,18 +87,15 @@ public class FileUtils
*/
public static String readFileAsString(File file)
{
- BufferedInputStream is = null;
-
- try
+ try(BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)))
{
- is = new BufferedInputStream(new FileInputStream(file));
+
+ return new String(readStreamAsString(is));
}
- catch (FileNotFoundException e)
+ catch (IOException e)
{
throw new RuntimeException(e);
}
-
- return readStreamAsString(is);
}
/**
@@ -115,23 +106,20 @@ public class FileUtils
*
* @return The contents of the reader.
*/
- private static String readStreamAsString(BufferedInputStream is)
+ private static byte[] readStreamAsString(BufferedInputStream is)
{
- try
+ try(ByteArrayOutputStream inBuffer = new ByteArrayOutputStream())
{
byte[] data = new byte[4096];
- StringBuffer inBuffer = new StringBuffer();
-
int read;
while ((read = is.read(data)) != -1)
{
- String s = new String(data, 0, read);
- inBuffer.append(s);
+ inBuffer.write(data, 0, read);
}
- return inBuffer.toString();
+ return inBuffer.toByteArray();
}
catch (IOException e)
{