1 20 21 package org.jacorb.security.util; 22 23 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 43 private DefaultMutableTreeNode root; 44 45 46 private JTree tree; 47 48 49 JScrollPane pane; 50 51 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 listNodes() 74 { 75 String 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 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 106 107 public KeyNode getKeyNode(String 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 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 alias = (String ) aliases.nextElement(); 133 if( ks.isKeyEntry( alias )) { 135 136 System.out.println("bnv: iserted key entry for " + alias); 137 java.security.cert.Certificate [] 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 e) 161 { 162 e.printStackTrace(); 163 } 164 } 165 166 171 172 public void addKey(String alias, java.security.PrivateKey key, char [] password) 173 { 174 if( containsAlias( alias )) 175 return; 176 177 KeyNode child = 178 new KeyNode(new String (alias), key, password, ks); 179 model.insertNodeInto(child, root, root.getChildCount()); 180 tree.scrollPathToVisible(new TreePath( new TreeNode[]{ root, child })); 181 } 182 183 188 189 public void addTrustedCert(String alias, java.security.cert.Certificate cert) 190 { 191 if( containsAlias( alias )) 192 return; 193 194 TrustNode child = 195 new TrustNode(new String (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 207 208 public void addCert(String alias, int idx, java.security.cert.Certificate 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 ("Alias " + alias + " not found!"); 224 } 225 226 231 232 public void addCerts( String alias, java.security.cert.Certificate [] 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 ("Alias " + alias + " not found!"); 250 } 251 252 255 256 public void clean() 257 { 258 root.removeAllChildren(); 259 root.setUserObject("<empty>"); 260 model.reload(); 261 } 262 263 266 267 public String 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 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 302 303 public String 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 date = (String )currentNode.toString(); 313 model.removeNodeFromParent(currentNode); 314 return date; 315 } 316 } 317 return null; 318 } 319 320 324 325 public void removeCerts( String 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 ("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 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 |