KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > security > util > KSEntryTree


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 2000-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.security.util;
22
23 /**
24  * This class manages the tree view and model for key store
25  * entries. These are represented as KeyNodes, CertNodes and
26  * TrustNodes
27  *
28  * @author Gerald Brose, FU Berlin
29  * @version $Id: KSEntryTree.java,v 1.8 2004/05/06 12:40:01 nicolas Exp $
30  */

31
32 import java.awt.*;
33 import javax.swing.tree.*;
34 import javax.swing.event.*;
35 import javax.swing.*;
36
37 import java.util.*;
38 import java.security.*;
39
40 public class KSEntryTree
41 {
42     /** the root node */
43     private DefaultMutableTreeNode root;
44
45     /** the tree */
46     private JTree tree;
47
48     /** the pane the tree is displayed on */
49     JScrollPane pane;
50
51     /** our tree model */
52     private DefaultTreeModel model;
53
54     private KeyStore ks;
55
56     public KSEntryTree()
57     {
58     root = new DefaultMutableTreeNode("<empty>");
59     model = new DefaultTreeModel( root );
60
61     tree = new JTree( model );
62     tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION) ;
63     tree.setShowsRootHandles(true);
64     tree.setVisible(true);
65     pane = new JScrollPane( tree );
66     }
67
68     public JScrollPane getPane()
69     {
70     return pane;
71     }
72
73     public String JavaDoc listNodes()
74     {
75         String JavaDoc out = "";
76         for( Enumeration e = root.children(); e.hasMoreElements(); )
77         {
78             KSNode node = (KSNode)e.nextElement();
79             if(node instanceof KeyNode)
80                 out = out + "KeyNode " +((KeyNode)node).getAlias() + "\n";
81             else if(node instanceof TrustNode)
82                 out = out + "TrustNode " +((TrustNode)node).getAlias() + "\n";
83             else
84                 out = out + "unknown node type\n";
85         }
86         return out;
87     }
88     
89     public KSNode getNode(String JavaDoc alias)
90     {
91         for( Enumeration e = root.children(); e.hasMoreElements(); )
92         {
93             KSNode node = (KSNode)e.nextElement();
94             if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ))
95             return node;
96             if( node instanceof TrustNode && ((TrustNode)node).getAlias().equals( alias ))
97             return node;
98         }
99         return null;
100     }
101     
102     /**
103      * @param alias - the key alias
104      * @return the KeyNode representing the key entry for the alias, null if not found
105      */

106
107     public KeyNode getKeyNode(String JavaDoc alias)
108     {
109     for( Enumeration e = root.children(); e.hasMoreElements(); )
110     {
111         KSNode node = (KSNode)e.nextElement();
112         if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ))
113         return (KeyNode)node;
114     }
115     return null;
116     }
117
118     /**
119      * load new keystore entries into the tree
120      *
121      * @param ks - the key store.
122      */

123
124     public void load(KeyStore ks )
125     {
126     this.ks = ks;
127     try
128     {
129         root.setUserObject("KeyStore");
130         for( Enumeration aliases = ks.aliases(); aliases.hasMoreElements();)
131         {
132         String JavaDoc alias = (String JavaDoc) aliases.nextElement();
133         if( ks.isKeyEntry( alias ))// || ks.isCertificateEntry ( alias ))
134
{
135
136 System.out.println("bnv: iserted key entry for " + alias);
137             java.security.cert.Certificate JavaDoc[] certChain =
138             ks.getCertificateChain( alias );
139
140             KeyNode keyNode = new KeyNode( alias, null, null, ks);
141             model.insertNodeInto( keyNode, root, root.getChildCount() );
142
143             for( int i = 0; i < certChain.length; i++ )
144             {
145             CertNode child =
146                 new CertNode((iaik.x509.X509Certificate)certChain[i],i);
147             model.insertNodeInto( child, keyNode, keyNode.getChildCount());
148             }
149             tree.scrollPathToVisible(new TreePath( new TreeNode[]{ root, keyNode }));
150         }
151         else if( ks.isCertificateEntry( alias ))
152         {
153             iaik.x509.X509Certificate cert =
154                 ( iaik.x509.X509Certificate )ks.getCertificate ( alias );
155
156             model.insertNodeInto( new TrustNode( alias, cert, ks ), root, root.getChildCount() );
157         }
158         }
159     }
160     catch( Exception JavaDoc e)
161     {
162         e.printStackTrace();
163     }
164     }
165
166     /**
167      * Add a new keystore entry to the tree, in this case a new
168      * key alias
169      * @param alias - the alias for the entry
170      */

171
172     public void addKey(String JavaDoc alias, java.security.PrivateKey JavaDoc key, char [] password)
173     {
174     if( containsAlias( alias ))
175         return;
176
177     KeyNode child =
178         new KeyNode(new String JavaDoc(alias), key, password, ks);
179     model.insertNodeInto(child, root, root.getChildCount());
180     tree.scrollPathToVisible(new TreePath( new TreeNode[]{ root, child }));
181     }
182
183     /**
184      * Add a new keystore entry, in this case add a trusted certificate
185      * @param alias - the alias for the entry
186      * @param cert - the trusted certificate
187      */

188
189     public void addTrustedCert(String JavaDoc alias, java.security.cert.Certificate JavaDoc cert)
190     {
191     if( containsAlias( alias ))
192         return;
193
194     TrustNode child =
195         new TrustNode(new String JavaDoc(alias), (iaik.x509.X509Certificate)cert, ks);
196     model.insertNodeInto(child, root, root.getChildCount());
197     tree.scrollPathToVisible(new TreePath(new TreeNode[]{ root, child }));
198     }
199
200     /**
201      * Add a new certificate in the entry ofr alias
202      * @param alias - the alias for the entry
203      * @param idx - index of the certificate in the chain of certificates of alias,
204      * if idx = -1, cert will be appended to the end of the chain
205      * @param cert - a new certificate for alias
206      */

207
208     public void addCert(String JavaDoc alias, int idx, java.security.cert.Certificate JavaDoc cert)
209     {
210     for( Enumeration e = root.children(); e.hasMoreElements(); )
211     {
212         MutableTreeNode node = (MutableTreeNode)e.nextElement();
213         if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ))
214         {
215         if( idx == -1 )
216             idx = node.getChildCount();
217         CertNode child = new CertNode((iaik.x509.X509Certificate)cert, idx );
218         model.insertNodeInto(child, node, idx);
219         tree.scrollPathToVisible(new TreePath(child.getPath()));
220         return;
221         }
222     }
223     throw new RuntimeException JavaDoc("Alias " + alias + " not found!");
224     }
225
226     /**
227      * Add a new certificate in the entry ofr alias
228      * @param alias - the alias for the entry
229      * @param certs - a new certificate chain for alias
230      */

231
232     public void addCerts( String JavaDoc alias, java.security.cert.Certificate JavaDoc [] certs )
233     {
234     for( Enumeration e = root.children(); e.hasMoreElements(); )
235     {
236         MutableTreeNode node = (MutableTreeNode)e.nextElement();
237         if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ))
238         {
239         CertNode child;
240         for ( int i = 0; i < certs.length; i ++ ) {
241             child = new CertNode((iaik.x509.X509Certificate)certs [ i ], i );
242             model.insertNodeInto(child, node, i);
243             if ( i == ( certs.length - 1 ))
244                 tree.scrollPathToVisible(new TreePath(child.getPath()));
245         }
246         return;
247         }
248     }
249     throw new RuntimeException JavaDoc("Alias " + alias + " not found!");
250     }
251     
252     /**
253      * Remove all nodes from the tree.
254      */

255
256     public void clean()
257     {
258     root.removeAllChildren();
259     root.setUserObject("<empty>");
260     model.reload();
261     }
262      
263     /**
264      * @return the key store alias for the selected node.
265      */

266
267     public String JavaDoc getSelectedAlias()
268     {
269     TreePath path = tree.getSelectionPath();
270     if( path != null)
271     {
272         if( path.getLastPathComponent() instanceof KeyNode )
273         return ((KeyNode)path.getLastPathComponent()).getAlias();
274         else if( path.getLastPathComponent() instanceof TrustNode )
275         return ((TrustNode)path.getLastPathComponent()).getAlias();
276     }
277     return null;
278     }
279
280     /**
281      * @return the key store alias for the selected node.
282      */

283
284     public KSNode getSelectedNode()
285     {
286     TreePath path = tree.getSelectionPath();
287     if( path != null )
288     {
289         return (KSNode)path.getLastPathComponent();
290     }
291     else
292     {
293         return null;
294     }
295     }
296
297     /**
298      * Remove the selected node.
299      *
300      * @return the key store alias for the selected node.
301      */

302
303     public String JavaDoc removeSelectedNode()
304     {
305     TreePath path = tree.getSelectionPath();
306     if( path != null )
307     {
308         MutableTreeNode currentNode = (MutableTreeNode)path.getLastPathComponent();
309         MutableTreeNode parent = (MutableTreeNode)currentNode.getParent();
310         if( parent != null )
311         {
312         String JavaDoc date = (String JavaDoc)currentNode.toString();
313         model.removeNodeFromParent(currentNode);
314         return date;
315         }
316     }
317     return null;
318     }
319
320     /**
321      * Remove the childs from KeyNode.
322      * @param alias - the alias for the entry
323      */

324
325     public void removeCerts( String JavaDoc alias )
326     {
327         for( Enumeration e = root.children(); e.hasMoreElements(); )
328         {
329             MutableTreeNode node = (MutableTreeNode)e.nextElement();
330             if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ))
331             {
332                 KeyNode parent = (KeyNode)node;
333                 CertNode child;
334                 for ( int i = parent.getChildCount (); i > 0; i-- )
335                 {
336                     child = (CertNode)parent.getChildAt ( i - 1 );
337                     model.removeNodeFromParent ( child );
338                 }
339                 return;
340             }
341         }
342         throw new RuntimeException JavaDoc("Alias " + alias + " not found!");
343     }
344
345     public void reload()
346     {
347         model.reload(root);
348     }
349
350     public Enumeration getNodes()
351     {
352         return root.children();
353     }
354
355
356     public boolean isTrusted(iaik.x509.X509Certificate check)
357     {
358     for( Enumeration e = getNodes(); e.hasMoreElements(); )
359     {
360         KSNode node = (KSNode)e.nextElement();
361         if( node instanceof TrustNode && ((KSNode)node).getCert().equals(check))
362         return true;
363     }
364     return false;
365     }
366
367     public boolean containsAlias(String JavaDoc alias)
368     {
369     for( Enumeration e = root.children(); e.hasMoreElements(); )
370     {
371         MutableTreeNode node = (MutableTreeNode)e.nextElement();
372         if( node instanceof KeyNode && ((KeyNode)node).getAlias().equals( alias ) ||
373         node instanceof TrustNode && ((TrustNode)node).getAlias().equals( alias )
374         )
375         return true;
376     }
377     return false;
378     }
379
380 }
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
Popular Tags