[jboss-cvs] JBossAS SVN: r88159 - branches/Branch_5_x/security/src/main/org/jboss/security/plugins.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 4 12:29:59 EDT 2009


Author: mmoyses
Date: 2009-05-04 12:29:59 -0400 (Mon, 04 May 2009)
New Revision: 88159

Added:
   branches/Branch_5_x/security/src/main/org/jboss/security/plugins/SecurityKeyManager.java
Modified:
   branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomain.java
   branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomainMBean.java
Log:
JBAS-6861: allowing pkcs11 keystores

Modified: branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomain.java
===================================================================
--- branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomain.java	2009-05-04 16:24:18 UTC (rev 88158)
+++ branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomain.java	2009-05-04 16:29:59 UTC (rev 88159)
@@ -36,8 +36,10 @@
 import javax.crypto.spec.PBEParameterSpec;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
+import javax.net.ssl.KeyManager;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.TrustManagerFactory;
+import javax.net.ssl.X509KeyManager;
 import javax.security.auth.callback.CallbackHandler;
 
 import org.jboss.crypto.CryptoUtil;
@@ -127,7 +129,10 @@
 
    /** The keystore password for loading */
    private char[] keyStorePassword;
-
+   
+   /** The alias of the KeyStore to be used */
+   private String keyStoreAlias;
+   
    /** The secret key that corresponds to the keystore password */
    private SecretKey cipherKey;
 
@@ -249,6 +254,27 @@
    {
       this.keyStorePassword = Util.loadPassword(password);
    }
+   
+   /*
+    * (non-Javadoc)
+    * 
+    * @see org.jboss.security.plugins.JaasSecurityDomainMBean#getKeyStoreAlias()
+    */
+   @ManagementProperty(use = {ViewUse.CONFIGURATION}, description = "The keystore alias with the certificate to be used")
+   public String getKeyStoreAlias()
+   {
+      return this.keyStoreAlias;
+   }
+   
+   /*
+    * (non-Javadoc)
+    * 
+    * @see org.jboss.security.plugins.JaasSecurityDomainMBean#setKeyStoreAlias(java.lang.String)
+    */
+   public void setKeyStoreAlias(String alias)
+   {
+      this.keyStoreAlias = alias;
+   }
 
    /*
     * (non-Javadoc)
@@ -614,19 +640,39 @@
 
    private void loadKeyAndTrustStore() throws Exception
    {
-      if (keyStoreURL != null)
+      if (keyStorePassword != null)
       {
          keyStore = KeyStore.getInstance(keyStoreType);
-         InputStream is = keyStoreURL.openStream();
+         InputStream is = null;
+         if (!"PKCS11".equalsIgnoreCase(keyStoreType))
+         {
+            is = keyStoreURL.openStream();
+         }
          keyStore.load(is, keyStorePassword);
+         if (keyStoreAlias != null && !keyStore.isKeyEntry(keyStoreAlias))
+         {
+            throw new IOException("Cannot find key entry with alias " + keyStoreAlias + " in the keyStore");
+         }
          String algorithm = KeyManagerFactory.getDefaultAlgorithm();
          keyMgr = KeyManagerFactory.getInstance(algorithm);
          keyMgr.init(keyStore, keyStorePassword);
+         if (keyStoreAlias != null)
+         {
+            KeyManager[] keyManagers = keyMgr.getKeyManagers();
+            for (int i = 0; i < keyManagers.length; i++)
+            {
+               keyManagers[i] = new SecurityKeyManager((X509KeyManager) keyManagers[i], keyStoreAlias);
+            }
+         }
       }
-      if (trustStoreURL != null)
+      if (trustStorePassword != null)
       {
          trustStore = KeyStore.getInstance(trustStoreType);
-         InputStream is = trustStoreURL.openStream();
+         InputStream is = null;
+         if (!"PKCS11".equalsIgnoreCase(trustStoreType))
+         {
+            is = trustStoreURL.openStream();
+         }
          trustStore.load(is, trustStorePassword);
          String algorithm = TrustManagerFactory.getDefaultAlgorithm();
          trustMgr = TrustManagerFactory.getInstance(algorithm);

Modified: branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomainMBean.java
===================================================================
--- branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomainMBean.java	2009-05-04 16:24:18 UTC (rev 88158)
+++ branches/Branch_5_x/security/src/main/org/jboss/security/plugins/JaasSecurityDomainMBean.java	2009-05-04 16:29:59 UTC (rev 88159)
@@ -22,7 +22,9 @@
 package org.jboss.security.plugins;
 
 import java.io.IOException;
+
 import javax.management.ObjectName;
+
 import org.jboss.system.ServiceMBean;
 
 
@@ -52,7 +54,12 @@
     /** Set the credential string for the KeyStore.
     */
    public void setKeyStorePass(String password) throws Exception;
-
+   /** Get the alias of the KeyStore.
+    */
+   public String getKeyStoreAlias();
+   /** Set the alias of the KeyStore.
+    */
+   public void setKeyStoreAlias(String alias);
    /** Get the type of the trust store
     * @return the type of the trust store
     */ 

Added: branches/Branch_5_x/security/src/main/org/jboss/security/plugins/SecurityKeyManager.java
===================================================================
--- branches/Branch_5_x/security/src/main/org/jboss/security/plugins/SecurityKeyManager.java	                        (rev 0)
+++ branches/Branch_5_x/security/src/main/org/jboss/security/plugins/SecurityKeyManager.java	2009-05-04 16:29:59 UTC (rev 88159)
@@ -0,0 +1,97 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.plugins;
+
+import java.net.Socket;
+import java.security.Principal;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.X509KeyManager;
+
+/**
+ * X509KeyManager that allows selection of a key entry to be used.
+ * 
+ * @author <a href="mmoyses at redhat.com">Marcus Moyses</a>
+ * @version $Revision: 1.1 $
+ */
+public class SecurityKeyManager implements X509KeyManager
+{
+   private X509KeyManager delegate;
+   
+   private String keyAlias;
+   
+   public SecurityKeyManager(X509KeyManager keyManager, String alias)
+   {
+      this.delegate = keyManager;
+      this.keyAlias = alias;
+   }
+
+   /**
+    * @see X509KeyManager#chooseClientAlias(String[], Principal[], Socket)
+    */
+   public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)
+   {
+      return keyAlias;
+   }
+
+   /**
+    * @see X509KeyManager#chooseServerAlias(String, Principal[], Socket)
+    */
+   public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket)
+   {
+      return delegate.chooseServerAlias(keyType, issuers, socket);
+   }
+
+   /**
+    * @see X509KeyManager#getCertificateChain(String)
+    */
+   public X509Certificate[] getCertificateChain(String alias)
+   {
+      return delegate.getCertificateChain(alias);
+   }
+
+   /**
+    * @see X509KeyManager#getClientAliases(String, Principal[])
+    */
+   public String[] getClientAliases(String keyType, Principal[] issuers)
+   {
+      return delegate.getClientAliases(keyType, issuers);
+   }
+
+   /**
+    * @see X509KeyManager#getPrivateKey(String)
+    */
+   public PrivateKey getPrivateKey(String alias)
+   {
+      return delegate.getPrivateKey(alias);
+   }
+
+   /**
+    * @see X509KeyManager#getServerAliases(String, Principal[])
+    */
+   public String[] getServerAliases(String keyType, Principal[] issuers)
+   {
+      return delegate.getServerAliases(keyType, issuers);
+   }
+
+}




More information about the jboss-cvs-commits mailing list