Author: tyrell
Date: Thu May 15 10:26:39 2008
New Revision: 17058

Log:
Initial commit of the Certificate Manager page, which will eventually allow the 
user to manage his/her personal keystore.

Added:
   trunk/mashup/java/modules/www/cert_manager.jsp
Modified:
   
trunk/mashup/java/modules/admin/service/src/org/wso2/mashup/admin/service/MashupAdminService.java
   trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java

Modified: 
trunk/mashup/java/modules/admin/service/src/org/wso2/mashup/admin/service/MashupAdminService.java
==============================================================================
--- 
trunk/mashup/java/modules/admin/service/src/org/wso2/mashup/admin/service/MashupAdminService.java
   (original)
+++ 
trunk/mashup/java/modules/admin/service/src/org/wso2/mashup/admin/service/MashupAdminService.java
   Thu May 15 10:26:39 2008
@@ -515,4 +515,6 @@
 
         return Boolean.valueOf(success);
     }
+
+    
 }

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
Thu May 15 10:26:39 2008
@@ -61,8 +61,12 @@
 import org.wso2.utils.security.CryptoUtil;
 import org.wso2.wsas.ServerConstants;
 import org.wso2.wsas.ServerManager;
+import org.wso2.wsas.admin.service.util.KeyStoreData;
+import org.wso2.wsas.admin.service.util.CertData;
+import org.wso2.wsas.util.KeyStoreUtil;
 import org.wso2.wsas.persistence.PersistenceManager;
 import org.wso2.wsas.persistence.dataobject.ServiceUserDO;
+import org.wso2.wsas.persistence.dataobject.KeyStoreDO;
 
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanException;
@@ -84,6 +88,7 @@
 import java.io.UnsupportedEncodingException;
 import java.io.Reader;
 import java.io.InputStreamReader;
+import java.io.ByteArrayInputStream;
 import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -91,11 +96,22 @@
 import java.nio.channels.FileChannel;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Properties;
+import java.util.List;
+import java.util.Enumeration;
+import java.util.Collections;
+import java.util.Comparator;
+import java.text.Format;
+import java.text.SimpleDateFormat;
+
+import sun.misc.BASE64Encoder;
 
 public class MashupUtils {
 
@@ -409,8 +425,8 @@
         ServerManager serverManager = ServerManager.getInstance();
         ConfigurationContext configContext = serverManager.configContext;
         EmbeddedRegistry embeddedRegistry =
-                    (EmbeddedRegistry) 
configContext.getAxisConfiguration().getParameterValue(
-                            RegistryConstants.REGISTRY);
+                (EmbeddedRegistry) 
configContext.getAxisConfiguration().getParameterValue(
+                        RegistryConstants.REGISTRY);
 
         try {
             UserRegistry systemRegistry = embeddedRegistry.getSystemRegistry();
@@ -470,9 +486,9 @@
             ConfigurationContext context = serverManager.configContext;
             String wso2wsasHome = 
System.getProperty(MashupConstants.WSO2WSAS_HOME);
             ConfigurationContext configurationContext =
-                        
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
-                                wso2wsasHome + File.separator + 
MashupConstants.WORK_DIRECTORY +
-                                        File.separator + 
MashupConstants.REPO_DIRECTORY);
+                    
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
+                            wso2wsasHome + File.separator + 
MashupConstants.WORK_DIRECTORY +
+                                    File.separator + 
MashupConstants.REPO_DIRECTORY);
             ServiceClient client = new ServiceClient(configurationContext, 
null);
             client.engageModule(MashupConstants.RAMPART);
             client.engageModule(MashupConstants.ADDRESSING);
@@ -511,7 +527,8 @@
 
     /**
      * Call login service using identifier.
-     * @param identifier InfoCard ppid or OpenID URL.
+     *
+     * @param identifier   InfoCard ppid or OpenID URL.
      * @param cookieString
      * @return
      * @throws MashupFault
@@ -524,9 +541,9 @@
             ConfigurationContext context = serverManager.configContext;
             String wso2wsasHome = 
System.getProperty(MashupConstants.WSO2WSAS_HOME);
             ConfigurationContext configurationContext =
-                        
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
-                                wso2wsasHome + File.separator + 
MashupConstants.WORK_DIRECTORY +
-                                        File.separator + 
MashupConstants.REPO_DIRECTORY);
+                    
ConfigurationContextFactory.createConfigurationContextFromFileSystem(
+                            wso2wsasHome + File.separator + 
MashupConstants.WORK_DIRECTORY +
+                                    File.separator + 
MashupConstants.REPO_DIRECTORY);
             RPCServiceClient client = new 
RPCServiceClient(configurationContext, null);
             client.engageModule(MashupConstants.RAMPART);
             client.engageModule(MashupConstants.ADDRESSING);
@@ -808,8 +825,10 @@
         return retMD5;
     }
 
-    public static String populateRegistry(ConfigurationContext configCtx, 
DeploymentFileData currentFile, String documentation,
-                                    String name, boolean faulty, String 
mediaType, String serviceType)
+    public static String populateRegistry(ConfigurationContext configCtx,
+                                          DeploymentFileData currentFile, 
String documentation,
+                                          String name, boolean faulty, String 
mediaType,
+                                          String serviceType)
             throws DeploymentException {
 
         try {
@@ -900,9 +919,11 @@
         }
     }
 
-    private static void addToRegistry(DeploymentFileData currentFile, String 
documentation, boolean faulty,
-                               File file, String username, String path1,
-                               UserRegistry userRegistry, String mediaType, 
String serviceType)
+    private static void addToRegistry(DeploymentFileData currentFile, String 
documentation,
+                                      boolean faulty,
+                                      File file, String username, String path1,
+                                      UserRegistry userRegistry, String 
mediaType,
+                                      String serviceType)
             throws IOException, RegistryException {
         ResourceImpl resource1;// create a content resource
         resource1 = new ResourceImpl();
@@ -950,13 +971,14 @@
         return username;
     }
 
-    public static HashMap getAvailableAxisTransports(){
+    public static HashMap getAvailableAxisTransports() {
         ServerManager serverManager = ServerManager.getInstance();
         ConfigurationContext configContext = serverManager.configContext;
         return configContext.getAxisConfiguration().getTransportsIn();
     }
 
-    public static boolean isTransportExposed(String serviceName, String 
transportName) throws AxisFault {
+    public static boolean isTransportExposed(String serviceName, String 
transportName)
+            throws AxisFault {
         ServerManager serverManager = ServerManager.getInstance();
         ConfigurationContext configContext = serverManager.configContext;
 
@@ -1032,16 +1054,17 @@
     }
 
 
-    public static Resource getUserKeystoreResource(String userName){
+    public static Resource getUserKeystoreResource(String userName) {
         ServerManager serverManager = ServerManager.getInstance();
         ConfigurationContext configContext = serverManager.configContext;
         EmbeddedRegistry embeddedRegistry =
                 (EmbeddedRegistry) 
configContext.getAxisConfiguration().getParameterValue(
                         RegistryConstants.REGISTRY);
-        
+
         try {
             UserRegistry systemRegistry = embeddedRegistry.getSystemRegistry();
-            String path = MashupConstants.USERS_PATH + "/" + userName + 
MashupConstants.USER_KEYSTORE_PATH;
+            String path = MashupConstants.USERS_PATH + "/" + userName + 
MashupConstants
+                    .USER_KEYSTORE_PATH;
             return systemRegistry.get(path);
         } catch (RegistryException e) {
             log.error(e);
@@ -1050,7 +1073,72 @@
         return null;
     }
 
-     public static byte[] getBytesFromFile(File file) throws IOException {
+
+    /**
+     * 
+     * Gets all certificates from a user's keystore
+     *
+     * @param userName The name of the user
+     * @return An array of CertData objects containing certificate meta-data
+     */
+    public static CertData[] getCerts(String userName) {
+        try {
+            Format formatter = new SimpleDateFormat("dd/MM/yyyy");
+
+            // Getting this users keystore from registry
+            Resource ksResource = getUserKeystoreResource(userName);
+
+            KeyStore keyStore = KeyStore.getInstance("JKS");
+            char[] keyPassphrase =
+                    
ksResource.getProperty(MashupConstants.USER_KEYSTORE_PASSWORD).toCharArray();
+            keyStore.load(new ByteArrayInputStream((byte[]) 
ksResource.getContent()),
+                          keyPassphrase);
+
+            Enumeration aliases = keyStore.aliases();
+            List certDataList = new ArrayList();
+            while (aliases.hasMoreElements()) {
+                String alias = (String) aliases.nextElement();
+                if (keyStore.isCertificateEntry(alias)) {
+                    X509Certificate cert = (X509Certificate) 
keyStore.getCertificate(alias);
+                    certDataList.add(fillCertData(cert, alias, formatter));
+                }
+            }
+
+            return (CertData[]) certDataList.toArray(new 
CertData[certDataList.size()]);
+
+        } catch (Exception e) {
+            String msg =
+                    "An error occured while retrieving certificates from the 
keystore of user " +
+                            userName;
+            log.error(msg, e);
+
+        }
+        return null;
+    }
+
+
+    private static CertData fillCertData(X509Certificate cert, String alias, 
Format formatter) {
+        CertData certData = new CertData();
+        certData.setAlias(alias);
+        certData.setSubjectDN(cert.getSubjectDN().getName());
+        certData.setIssuerDN(cert.getIssuerDN().getName());
+        certData.setSerialNumber(cert.getSerialNumber());
+        certData.setVersion(cert.getVersion());
+        certData.setNotAfter(formatter.format(cert.getNotAfter()));
+        certData.setNotBefore(formatter.format(cert.getNotBefore()));
+        return certData;
+    }
+
+
+    /**
+     *
+     * Retrieves a File's content as a byte array
+     *
+     * @param file A File object
+     * @return byte[] containing the file's content
+     * @throws IOException
+     */
+    public static byte[] getBytesFromFile(File file) throws IOException {
         InputStream is = new FileInputStream(file);
 
         // Get the size of the file

Added: trunk/mashup/java/modules/www/cert_manager.jsp
==============================================================================
--- (empty file)
+++ trunk/mashup/java/modules/www/cert_manager.jsp      Thu May 15 10:26:39 2008
@@ -0,0 +1,118 @@
+<%--
+ * Copyright 2006,2007 WSO2, Inc. http://www.wso2.org
+ *
+ * Licensed 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.
+--%>
+<%@ page errorPage="error.jsp" %>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ page import="java.util.ResourceBundle" %>
+<%@ page import="org.wso2.wsas.admin.service.util.CertData" %>
+
+<!--Required to keep a user logged in if 'Remember Me' option is selected-->
+<%@ include file="validate_login.jsp" %>
+
+<%
+    ResourceBundle bundle = ResourceBundle.getBundle("UI");
+    String currentUser = RegistryUtils.getCurrentUser(userRegistry);
+    String thisPage = "cert_manager.jsp";
+%>
+
+<html>
+<head>
+    <title><%= bundle.getString("main.title")%> - Certificate Manager</title>
+
+    <!-- Required CSS -->
+    <link href="css/styles.css" rel="stylesheet" type="text/css"/>
+
+    <!-- Required Javascript -->
+    <script language="javascript" src="js/common.js" 
type="text/javascript"></script>
+</head>
+<body>
+<div id="page">
+    <%@ include file="header.jsp" %>
+    <%@ include file="searchbox.jsp" %>
+
+    <div id="content">
+        <%
+            if (!RegistryUtils.isLoggedIn(userRegistry)) {
+        %>
+        You need to <a href="signin.jsp?bounceback=<%=thisPage%>">sign in</a> 
to perform this
+        operation.
+        <%
+        } else {
+        %>
+        <fieldset>
+            <legend>Add a new certificate</legend>
+            <div><label>Certificate<font color="red">*</font></label><input
+                    type="file" size="50" id="certificateFile" 
name="certificateFile"/>
+
+            <input type="submit" value="Upload" align="right"
+                                                        onclick="return 
validateimportCertificate();"/>
+            </div>
+        </fieldset>
+        <fieldset>
+            <legend>You are currently trusting the following 
certificates</legend>
+            <table id="certificates">
+                <thead>
+                    <tr class="mashup_label">
+                        <th>Alias</th>
+                        <th>IssuerDN</th>
+                        <th>NotAfter</th>
+                        <th>NotBefore</th>
+                        <th>SerialNumber</th>
+                        <th>SubjectDN</th>
+                        <th>Version</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <%
+                        CertData[] certs = MashupUtils.getCerts(currentUser);
+                        for (int x = 0; x < certs.length; x++) {
+                            if (x % 2 == 0) {
+                    %>
+                    <tr style="background-color: gainsboro;">
+                        <%
+                            }
+                        %>
+                        <td><%=certs[x].getAlias()%>
+                        </td>
+                        <td><%=certs[x].getIssuerDN()%>
+                        </td>
+                        <td><%=certs[x].getNotAfter()%>
+                        </td>
+                        <td><%=certs[x].getNotBefore()%>
+                        </td>
+                        <td><%=certs[x].getSerialNumber()%>
+                        </td>
+                        <td><%=certs[x].getSubjectDN()%>
+                        </td>
+                        <td><%=certs[x].getVersion()%>
+                        </td>
+                    </tr>
+                    <%
+
+                        }
+                    %>
+
+                </tbody>
+            </table>
+        </fieldset>
+        <%
+            }
+        %>
+    </div>
+
+    <%@ include file="footer.jsp" %>
+</div>
+</body>
+</html>
\ No newline at end of file

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to