1 20 21 package org.jacorb.security.util; 22 23 29 30 import java.security.*; 31 import java.security.cert.*; 32 import java.util.*; 33 import javax.swing.tree.*; 34 import iaik.asn1.*; 35 import iaik.asn1.structures.*; 36 import iaik.x509.*; 37 import iaik.x509.extensions.*; 38 39 public class KeyNode 40 implements KSNode 41 { 42 44 45 private String alias; 46 private java.security.PrivateKey key; 47 private char[] password; 48 Vector children; 49 private String label; 50 private String keytype = "<unknown key type>"; 51 private TreeNode root = null; 52 private KeyStore ks; 53 54 private boolean dirty = false; 55 56 59 60 public KeyNode(String alias, java.security.PrivateKey key, char[] password, KeyStore ks) 61 { 62 this.alias = alias; 63 this.key = key; 64 this.password = password; 65 this.ks = ks; 66 children = new Vector(); 67 if( key != null ) 68 { 69 if( key instanceof java.security.interfaces.RSAPrivateKey || 70 key instanceof iaik.security.rsa.RSAPrivateKey) 71 keytype = "RSA"; 72 else if( key instanceof java.security.interfaces.DSAPrivateKey || 73 key instanceof iaik.security.dsa.DSAPrivateKey) 74 keytype = "DSA"; 75 else 76 keytype = key.getClass().getName(); 77 } 78 else 79 { 80 try 81 { 82 java.security.cert.Certificate cert = ks.getCertificate ( alias ); 83 java.security.PublicKey pubKey = cert.getPublicKey (); 84 if( pubKey instanceof java.security.interfaces.RSAPublicKey || 85 pubKey instanceof iaik.security.rsa.RSAPublicKey) 86 keytype = "RSA"; 87 else if( pubKey instanceof java.security.interfaces.DSAPublicKey || 88 pubKey instanceof iaik.security.dsa.DSAPublicKey) 89 keytype = "DSA"; 90 else 91 keytype = pubKey.getClass().getName(); 92 } 93 catch ( Exception ex ) {} 94 } 95 label = alias + " (" + keytype + " key)"; 96 } 97 98 public iaik.x509.X509Certificate getCert() 99 { 100 return ((CertNode)children.elementAt(0)).getCert(); 101 } 102 103 public iaik.x509.X509Certificate[] getCertificateChain() 104 { 105 iaik.x509.X509Certificate[] certs = 106 new iaik.x509.X509Certificate[getChildCount()]; 107 108 int ci = 0; 109 for( Enumeration e = children(); e.hasMoreElements(); ci++ ) 110 { 111 CertNode certNode = (CertNode)e.nextElement(); 112 certs[ci] = certNode.getCert(); 113 } 114 return certs; 115 } 116 117 118 119 120 public void insert(MutableTreeNode child, int index) 121 { 122 if( !checkAccess()) 123 return; 124 125 children.insertElementAt(child, index); 126 child.setParent( this ); 127 dirty = true; 128 } 129 130 public void remove(int index) 131 { 132 if( !checkAccess()) 133 return; 134 135 children.removeElementAt(index); 136 dirty = true; 137 } 138 139 public void remove(MutableTreeNode child) 140 { 141 if( !checkAccess()) 142 return; 143 144 children.remove(child); 145 dirty = true; 146 } 147 148 149 public void setParent(MutableTreeNode root) 150 { 151 this.root = root; 152 } 153 154 public void setUserObject(Object o) 155 {} 156 157 158 159 public Enumeration children() 160 { 161 return children.elements(); 162 } 163 164 public boolean getAllowsChildren() 165 { 166 return true; 167 } 168 169 public TreeNode getChildAt(int index) 170 { 171 return (TreeNode)children.elementAt(index); 172 } 173 174 public int getChildCount() 175 { 176 return children.size(); 177 } 178 179 public int getIndex(TreeNode node) 180 { 181 return children.indexOf(node); 182 } 183 184 public TreeNode getParent() 185 { 186 return root; 187 } 188 189 public String getAlias() 190 { 191 return alias; 192 } 193 194 private boolean charsEqual ( char [] a1, char [] a2 ) 195 { 196 if ( a1.length != a2.length ) return false; 197 else for ( int i = 0; i < a1.length; i++ ) 198 if ( a1[ i ] != a2[ i ] ) return false; 199 return true; 200 } 201 202 public void setKey(java.security.PrivateKey key, char[] password) { 204 if ( this.password == null ) 205 this.key = key; 206 else if ( charsEqual ( this.password, password )) 207 { 208 this.key = key; this.password = password; 209 } 210 else 211 return; 212 if( key != null ) 213 { 214 if( key instanceof java.security.interfaces.RSAPrivateKey || 215 key instanceof iaik.security.rsa.RSAPrivateKey) 216 keytype = "RSA"; 217 else if( key instanceof java.security.interfaces.DSAPrivateKey || 218 key instanceof iaik.security.dsa.DSAPrivateKey) 219 keytype = "DSA"; 220 else 221 keytype = key.getClass().getName(); 222 label = alias + " (" + keytype + " key)"; 223 } 224 } 225 226 public java.security.PrivateKey getKey( char[] password ) 227 { 228 if (( this.password == null ) || 230 ! ( charsEqual ( this.password, password ))) 231 { 232 System.out.println ( "coucou getKey wrong password >" + this.password + "<>" + password + "<" ); 233 return null; 234 } 235 else 236 return key; 237 } 238 239 java.security.PrivateKey getKey() 240 throws IllegalAccessException 241 { 242 if( !checkAccess()) 243 throw new IllegalAccessException ("Wrong password."); 244 245 return key; 246 } 247 248 249 public boolean isLeaf() 250 { 251 return false; 252 } 253 254 257 258 public void removeFromParent() 259 { 260 if( !checkAccess()) 261 return; 262 263 try 264 { 265 ks.deleteEntry( alias ); 266 } 267 catch( java.security.KeyStoreException kse ) 268 { 269 kse.printStackTrace(); 270 } 271 } 272 273 274 public void store() 275 { 276 if( !dirty || !checkAccess()) 277 return; 278 279 iaik.x509.X509Certificate[] chain = 280 new iaik.x509.X509Certificate[children.size()]; 281 282 for( int i = 0; i < chain.length; i++) 283 { 284 chain[i] = ((CertNode)children.elementAt(i)).getCert(); 285 } 286 287 try 288 { 289 ks.setKeyEntry( alias, key, password, chain ); 290 dirty = false; 291 } 292 catch( KeyStoreException kse ) 293 { 294 kse.printStackTrace(); 295 } 296 } 297 298 public boolean checkAccess() 299 { 300 if( key != null && password != null ) 301 return true; 302 303 try 304 { 305 password = UserSponsor.getPasswd("Please enter password for key alias " + alias ); 306 if( password != null ) 307 { 308 key = (java.security.PrivateKey )ks.getKey( alias, password ); 309 return true; 310 } 311 else 312 return false; 313 } 314 catch( java.security.UnrecoverableKeyException kse ) 315 { 316 kse.printStackTrace(); 317 return false; 318 } 319 catch(java.security.NoSuchAlgorithmException kse ) 320 { 321 kse.printStackTrace(); 322 return false; 323 } 324 catch( java.security.KeyStoreException kse ) 325 { 326 kse.printStackTrace(); 327 return false; 328 } 329 } 330 331 332 public String toString() 333 { 334 if( label.indexOf("(") > 0 ) 335 { 336 if( key != null ) 337 { 338 if( key instanceof java.security.interfaces.RSAPrivateKey || 339 key instanceof iaik.security.rsa.RSAPrivateKey) 340 keytype = "RSA"; 341 else if( key instanceof java.security.interfaces.DSAPrivateKey || 342 key instanceof iaik.security.dsa.DSAPrivateKey) 343 keytype = "DSA"; 344 else 345 keytype = key.getClass().getName(); 346 } 347 label = alias + " (" + keytype + " key)"; 348 } 349 return label; 350 } 351 352 } 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | Popular Tags |