1 package org.jacorb.security.util; 2 3 22 23 import java.security.*; 24 import java.security.cert.*; 25 import java.io.*; 26 import javax.swing.*; 27 import javax.swing.event.*; 28 import javax.swing.table.*; 29 import javax.swing.tree.*; 30 import java.awt.*; 31 import java.awt.event.*; 32 33 import java.math.BigInteger ; 34 import java.util.*; 35 36 import iaik.asn1.*; 37 import iaik.asn1.structures.*; 38 import iaik.x509.*; 39 import iaik.x509.extensions.*; 40 41 47 48 public class CertUtils 49 { 50 59 60 public static iaik.x509.X509Certificate createPublicKeyCert(iaik.asn1.structures.Name subject, 61 iaik.asn1.structures.Name issuer, 62 java.security.PublicKey subjectKey, 63 java.security.PrivateKey privKey) 64 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 65 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException 66 { 67 iaik.x509.X509Certificate cert = new iaik.x509.X509Certificate(); 68 69 if( subject == null ) 70 subject = emptyName(); 71 if( issuer == null ) 72 issuer = emptyName(); 73 74 cert.setIssuerDN( issuer ); 75 cert.setSubjectDN( subject ); 76 cert.setPublicKey( subjectKey ); 77 78 java.util.Date now = new java.util.Date (); 79 80 cert.setSerialNumber( new BigInteger ( Long.toString( now.getTime() ))); 81 cert.setValidNotBefore( now ); 82 83 java.util.Calendar cal = java.util.Calendar.getInstance(); 84 cal.add(java.util.Calendar.MONTH, 12); 85 cert.setValidNotAfter(cal.getTime()); 86 87 if( privKey instanceof java.security.interfaces.DSAPrivateKey ) 88 cert.sign(iaik.asn1.structures.AlgorithmID.dsaWithSHA1, 89 privKey); 90 else if ( privKey instanceof java.security.interfaces.RSAPrivateKey ) 91 cert.sign(iaik.asn1.structures.AlgorithmID.md5WithRSAEncryption, 92 privKey); 93 else 94 throw new java.security.InvalidKeyException ("Unknown private key: " + 95 privKey.getClass().getName()); 96 97 return cert; 98 } 99 100 101 106 107 public static iaik.x509.X509Certificate certifyRoleMembership( 108 String rolename, 109 iaik.asn1.structures.Name subject, 110 iaik.asn1.structures.Name issuer, 111 java.security.PublicKey subjectKey, 112 java.security.PrivateKey privKey) 113 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 114 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException 115 { 116 117 iaik.x509.X509Certificate cert = new iaik.x509.X509Certificate(); 118 119 if( subject == null ) 120 subject = emptyName(); 121 if( issuer == null ) 122 issuer = emptyName(); 123 124 cert.setIssuerDN( issuer ); 125 cert.setSubjectDN( subject ); 126 cert.setPublicKey( subjectKey ); 127 128 java.util.Date now = new java.util.Date (); 129 130 cert.setSerialNumber( new BigInteger ( Long.toString( now.getTime() ))); 131 cert.setValidNotBefore( now ); 132 133 java.util.Calendar cal = java.util.Calendar.getInstance(); 134 cal.add(java.util.Calendar.MONTH, 12); 135 cert.setValidNotAfter(cal.getTime()); 136 137 138 139 GeneralName gn = new GeneralName( GeneralName.rfc822Name,"role:" + rolename ); 140 GeneralNames generalNames = new GeneralNames(gn ); 141 142 iaik.x509.extensions.SubjectAltName ext = new SubjectAltName( generalNames ); 143 cert.addExtension(ext); 144 145 if( privKey instanceof java.security.interfaces.DSAPrivateKey ) 146 cert.sign(iaik.asn1.structures.AlgorithmID.dsaWithSHA1, 147 (java.security.PrivateKey )privKey); 148 else if ( privKey instanceof java.security.interfaces.RSAPrivateKey ) 149 cert.sign(iaik.asn1.structures.AlgorithmID.md5WithRSAEncryption, 150 (java.security.PrivateKey )privKey); 151 152 System.out.println("Cert signed"); 153 return cert; 154 } 155 156 public static iaik.asn1.structures.Name emptyName() 157 { 158 iaik.asn1.structures.Name subject = new iaik.asn1.structures.Name(); 159 subject.addRDN(ObjectID.commonName, ""); 160 subject.addRDN(ObjectID.organizationalUnit, ""); 161 subject.addRDN(ObjectID.organization, ""); 162 subject.addRDN(ObjectID.locality, ""); 163 subject.addRDN(ObjectID.country, ""); 164 return subject; 165 } 166 167 public static iaik.asn1.structures.Name createName(String alias) 168 { 169 iaik.asn1.structures.Name subject = new iaik.asn1.structures.Name(); 170 subject.addRDN(ObjectID.commonName, alias); 171 subject.addRDN(ObjectID.organizationalUnit, "AGSS"); 172 subject.addRDN(ObjectID.organization, "FU Berlin"); 173 subject.addRDN(ObjectID.locality, "Berlin"); 174 subject.addRDN(ObjectID.country, "DE"); 175 return subject; 176 } 177 178 public static String getCertLabel(java.security.cert.X509Certificate cert) 179 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 180 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException 181 { 182 String label = getRoleName(cert); 183 if( label == null ) 184 { 185 label = cert.getSubjectDN() + ", signer: " + cert.getIssuerDN(); 186 } 187 return label; 188 } 189 190 public static String getRoleName(java.security.cert.X509Certificate cert) 191 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 192 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException 193 { 194 iaik.x509.X509Certificate c; 195 196 try 197 { 198 c = (iaik.x509.X509Certificate)cert; 199 } 200 catch( ClassCastException ccce ) 201 { 202 c = new iaik.x509.X509Certificate( cert.getEncoded()); 203 } 204 205 if( !c.hasExtensions()) 206 return null; 207 208 c.checkValidity(); 209 210 for( Enumeration extensions = c.listExtensions(); extensions.hasMoreElements();) 211 { 212 iaik.x509.V3Extension e = (iaik.x509.V3Extension)extensions.nextElement(); 213 if( e instanceof SubjectAltName ) 214 { 215 SubjectAltName san = (SubjectAltName)e; 216 GeneralNames gn = san.getGeneralNames(); 217 for( Enumeration g = gn.getNames(); g.hasMoreElements(); ) 218 { 219 GeneralName generalName = (GeneralName)g.nextElement(); 220 if( generalName.getType() == GeneralName.rfc822Name ) 221 { 222 String value = (String )generalName.getName(); 223 if( value.startsWith("role:")) 224 return value.substring(5);; 225 } 226 } 227 } 228 } 229 return null; 231 } 238 239 public static boolean isRoleCert(java.security.cert.X509Certificate cert) 240 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 241 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException 242 { 243 try 244 { 245 iaik.x509.X509Certificate c; 246 247 try 248 { 249 c = (iaik.x509.X509Certificate)cert; 250 } 251 catch( ClassCastException ccce ) 252 { 253 c = new iaik.x509.X509Certificate( cert.getEncoded()); 254 } 255 256 if( !c.hasExtensions()) 257 return false; 258 259 for( Enumeration extensions = c.listExtensions(); extensions.hasMoreElements();) 260 { 261 iaik.x509.V3Extension e = (iaik.x509.V3Extension)extensions.nextElement(); 262 if( e instanceof SubjectAltName ) 263 { 264 SubjectAltName san = (SubjectAltName)e; 265 GeneralNames gn = san.getGeneralNames(); 266 for( Enumeration g = gn.getNames(); g.hasMoreElements(); ) 267 { 268 GeneralName generalName = (GeneralName)g.nextElement(); 269 if( generalName.getType() == GeneralName.rfc822Name ) 270 { 271 String value = (String )generalName.getName(); 272 if( value.startsWith("role:")) 273 return true; 274 } 275 } 276 } 277 } 278 return false; 280 281 } 282 catch( Exception e) 283 { 284 e.printStackTrace(); 285 return false; 286 } 287 } 288 289 306 307 public static boolean verifyCertificateChain ( java.security.cert.X509Certificate [] chain, 308 java.security.KeyStore keyStore 309 ) 310 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 311 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException , 312 java.security.NoSuchProviderException , java.security.KeyStoreException , 313 java.security.SignatureException 314 { 315 int len = chain.length; 316 chain[ len - 1 ].verify( chain [ len - 1 ].getPublicKey ()); 317 318 for ( int i = len - 1; i > 0; i-- ) 319 { 320 chain[ i - 1 ].verify( chain[ i ].getPublicKey ()); 321 } 322 323 String alias = chain[ len - 1 ].getIssuerDN ().getName(); 325 int index = alias.indexOf ( "CN=" ) + 3; 326 int l = alias.length (); 327 alias = alias.substring ( index, l ); 328 return keyStore.isCertificateEntry( alias ); 329 } 330 331 public static java.security.cert.X509Certificate readCertificate(String fileName) 332 throws iaik.x509.X509ExtensionException, java.security.cert.CertificateException , 333 java.security.NoSuchAlgorithmException , java.security.InvalidKeyException , 334 java.security.NoSuchProviderException , java.io.IOException 335 { 336 java.security.cert.CertificateFactory factory = 337 java.security.cert.CertificateFactory.getInstance("X.509", "IAIK") ; 338 return (java.security.cert.X509Certificate )factory.generateCertificate( 339 new FileInputStream( fileName )); 340 } 341 342 343 } 344 345 346 347 | Popular Tags |