1 package org.jacorb.security.util; 2 3 22 23 24 import java.security.*; 25 import java.security.cert.*; 26 import java.io.*; 27 import javax.swing.*; 28 import javax.swing.event.*; 29 import javax.swing.table.*; 30 import javax.swing.tree.*; 31 import java.awt.*; 32 import java.awt.event.*; 33 34 import java.math.BigInteger ; 35 import java.util.*; 36 37 import iaik.asn1.*; 38 import iaik.asn1.structures.*; 39 import iaik.x509.*; 40 import iaik.x509.extensions.*; 41 42 48 49 public class KeyStoreUtil 50 { 51 56 57 public static KeyStore getKeyStore(String file_name, char[] storepass ) 58 throws java.io.IOException , KeyStoreException, NoSuchAlgorithmException, 59 CertificateException 60 { 61 File f = new File( file_name ); 63 64 if( ! f.exists() ) 65 { 66 String name = 68 System.getProperty( "user.home" ) + 69 System.getProperty( "file.separator" ) + 70 file_name; 71 72 f = new File( name ); 73 } 74 75 FileInputStream in = new FileInputStream( f ); 76 77 KeyStore ks; 78 80 try 81 { 82 ks = KeyStore.getInstance( "IAIKKeyStore", "IAIK" ); 83 } 84 catch ( java.security.NoSuchProviderException ex ) 85 { 86 System.err.println ( ex.toString ()); 87 ks = KeyStore.getInstance("jks"); 88 } 89 ks.load(in, storepass); 90 in.close(); 91 return ks; 92 } 93 94 95 102 103 public static java.security.KeyPair getKeyPair(String keystore, 104 String alias, 105 char[] storepass, 106 char[] password) 107 throws java.io.IOException , 108 KeyStoreException, NoSuchAlgorithmException, 109 UnrecoverableKeyException, CertificateException 110 { 111 KeyStore ks = getKeyStore( keystore, storepass ); 112 113 if(! ks.isKeyEntry(alias)) 114 return null; 115 116 java.security.PrivateKey privateKey = 117 (java.security.PrivateKey )ks.getKey(alias,password); 118 java.security.cert.X509Certificate c = 119 (java.security.cert.X509Certificate )ks.getCertificate(alias); 120 java.security.PublicKey publicKey = c.getPublicKey(); 121 122 return new java.security.KeyPair ( publicKey, privateKey); 123 } 124 125 132 133 public static java.security.KeyPair getKeyPair(KeyStore ks, 134 String alias, 135 char[] password) 136 throws java.io.IOException , 137 KeyStoreException, NoSuchAlgorithmException, 138 UnrecoverableKeyException, CertificateException 139 { 140 if(! ks.isKeyEntry(alias)) 141 return null; 142 143 java.security.PrivateKey privateKey = 144 (java.security.PrivateKey )ks.getKey(alias,password); 145 java.security.cert.X509Certificate c = 146 (java.security.cert.X509Certificate )ks.getCertificate(alias); 147 148 java.security.PublicKey publicKey = c.getPublicKey(); 149 return new java.security.KeyPair ( publicKey, privateKey); 150 } 151 152 157 158 public static java.security.KeyPair getKeyPair(KeyStore ks) 159 throws java.io.IOException , 160 KeyStoreException, NoSuchAlgorithmException, 161 UnrecoverableKeyException, CertificateException 162 { 163 String [] clear_input = new String []{ "Entry Alias"}; 164 char[][] opaque_input = new char[1][]; 165 166 UserSponsor us = new UserSponsor("", 167 "Please authenticate to retrieve key pair", 168 clear_input, 169 new String []{ "Entry Password" } 170 ); 171 172 if( !us.getInput(clear_input, opaque_input)) 173 { 174 System.err.println("Input cancelled"); 175 System.exit(1); 176 } 177 178 String alias = clear_input[0]; 179 char [] password = opaque_input[0]; 180 181 if( alias == null || password == null ) 182 { 183 return null; 184 } 185 186 return getKeyPair( ks, alias, password); 187 } 188 189 190 194 195 public static java.security.KeyPair getKeyPair() 196 throws java.io.IOException , 197 KeyStoreException, NoSuchAlgorithmException, 198 UnrecoverableKeyException, CertificateException 199 { 200 String [] clear_input = new String []{ "Keystore file", "Entry Alias"}; 201 char[][] opaque_input = new char[2][]; 202 203 UserSponsor us = new UserSponsor("", 204 "Please authenticate to retrieve key pair", 205 clear_input, 206 new String []{ "Keystore Password", "Entry Password" }); 207 208 if( !us.getInput(clear_input, opaque_input)) 209 { 210 System.err.println("Input cancelled"); 211 System.exit(1); 212 } 213 214 String fname = clear_input[0]; 215 String name = clear_input[1]; 216 char [] ksPassword = opaque_input[0]; 217 char [] entryPassword = opaque_input[1]; 218 219 if( fname == null || name == null || ksPassword == null || entryPassword == null ) 220 { 221 System.err.println("no input"); 222 System.exit(1); 223 } 224 225 return getKeyPair( fname, name, ksPassword, entryPassword); 226 } 227 228 232 233 public static java.security.cert.X509Certificate [] getCerts(String fileName, 234 String name, 235 char[] password) 236 throws java.io.IOException , 237 KeyStoreException, NoSuchAlgorithmException, 238 UnrecoverableKeyException, CertificateException 239 { 240 if( name == null || name.length() == 0 || password == null ) 241 { 242 return getCerts( fileName ); 243 } 244 245 KeyStore ks = getKeyStore(fileName, password ); 246 if(! ks.isKeyEntry(name)) 247 return null; 248 249 return (java.security.cert.X509Certificate [])ks.getCertificateChain( name ); 250 251 } 252 253 254 258 259 public static java.security.cert.X509Certificate [] getCerts(String fileName) 260 throws IOException, KeyStoreException, NoSuchAlgorithmException, 261 UnrecoverableKeyException, CertificateException 262 { 263 String [] clear_input = new String []{ "User name"}; 264 char[][] opaque_input = new char[1][]; 265 266 UserSponsor us = new UserSponsor("Authentication", 267 "Please authenticate to retrieve certificates", 268 clear_input, 269 new String []{ "Password"}); 270 271 if( !us.getInput(clear_input, opaque_input)) 272 { 273 System.err.println("Input cancelled"); 274 System.exit(1); 275 } 276 277 String name = clear_input[0]; 278 char [] password = opaque_input[0]; 279 280 if( name == null || password == null ) 281 { 282 System.err.println("no input"); 283 System.exit(1); 284 } 285 286 287 KeyStore ks = getKeyStore(fileName, password ); 288 289 if(! ks.isKeyEntry(name)) 290 return null; 291 292 return (java.security.cert.X509Certificate [])ks.getCertificateChain( name ); 293 294 } 295 296 297 302 303 public static java.security.cert.X509Certificate [] getRoleCerts(KeyStore ks, 304 String alias, 305 java.security.PublicKey [] trustees ) 306 throws java.security.KeyStoreException ,java.security.cert.CertificateEncodingException 307 { 308 if(! ks.isKeyEntry(alias)) 309 return null; 310 311 Vector vector = new Vector(); 312 313 java.security.cert.Certificate [] chain = 314 (java.security.cert.Certificate [])ks.getCertificateChain( alias ); 315 for( int i = 0; i < chain.length; i++ ) 316 { 317 try 318 { 319 iaik.x509.X509Certificate c = (iaik.x509.X509Certificate)chain[i]; 320 if( !c.hasExtensions()) 321 continue; 322 323 for( Enumeration extensions = c.listExtensions(); extensions.hasMoreElements();) 324 { 325 iaik.x509.V3Extension e = (iaik.x509.V3Extension)extensions.nextElement(); 326 if( e instanceof SubjectAltName ) 327 { 328 SubjectAltName san = (SubjectAltName)e; 329 GeneralNames gn = san.getGeneralNames(); 330 for( Enumeration g = gn.getNames(); g.hasMoreElements(); ) 331 { 332 GeneralName generalName = (GeneralName)g.nextElement(); 333 if( generalName.getType() == GeneralName.rfc822Name ) 334 { 335 String value = (String )generalName.getName(); 336 if( value.startsWith("role:")) 337 { 338 c.checkValidity(); 339 java.security.Signature sig = 340 java.security.Signature.getInstance( c.getSigAlgName()); 341 342 for( int ii = 0; ii < trustees.length; ii++) 343 { 344 try 345 { 346 sig.initVerify( trustees[ii] ); 347 sig.verify( c.getSignature() ); 348 vector.addElement(c); 349 } 350 catch( SignatureException se ) 351 { 352 continue; 353 } 354 catch( InvalidKeyException se ) 355 { 356 continue; 357 } 358 } 359 } 360 } 361 } 362 } 363 } 364 365 } 366 catch(Exception e) 367 { 368 e.printStackTrace(); 369 continue; 370 } 371 } 372 373 java.security.cert.X509Certificate [] result = 374 new java.security.cert.X509Certificate [vector.size()]; 375 for( int i = 0; i < result.length; i++) 376 { 377 result[i] = (java.security.cert.X509Certificate )vector.elementAt(i); 378 } 379 return result; 380 } 381 382 383 384 } 385 386 387 388 389 390 391 392 393 394 395 396 397 | Popular Tags |