1 26 27 package org.objectweb.jonas.security.auth.spi; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.security.cert.CertStore ; 32 import java.security.cert.CertStoreParameters ; 33 import java.security.cert.CertificateFactory ; 34 import java.security.cert.CollectionCertStoreParameters ; 35 import java.security.cert.LDAPCertStoreParameters ; 36 import java.security.cert.X509CRL ; 37 import java.security.cert.X509CRLSelector ; 38 import java.security.cert.X509Certificate ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 import javax.security.auth.Subject ; 44 import javax.security.auth.callback.Callback ; 45 import javax.security.auth.callback.CallbackHandler ; 46 import javax.security.auth.callback.NameCallback ; 47 import javax.security.auth.login.LoginException ; 48 import javax.security.auth.spi.LoginModule ; 49 50 import org.objectweb.jonas.security.auth.JPrincipal; 51 import org.objectweb.jonas.security.auth.callback.CertificateCallback; 52 53 58 public class CRLLoginModule implements LoginModule { 59 60 63 private Subject subject = null; 64 65 68 private CallbackHandler callbackHandler = null; 69 70 73 private Map sharedState = null; 74 75 78 private Map options = null; 79 80 83 private String principalName = null; 84 85 88 private String password = null; 89 90 93 private ArrayList principalRoles = null; 94 95 98 private X509Certificate cert = null; 99 100 113 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 114 this.subject = subject; 115 this.callbackHandler = callbackHandler; 116 this.sharedState = sharedState; 117 this.options = options; 118 } 119 120 130 public boolean login() throws LoginException { 131 132 if (callbackHandler == null) { 134 throw new LoginException ("No handler has been defined."); 135 } 136 String crlsResourceName = (String ) options.get("CRLsResourceName"); 137 String certStoreAccessName = null; 138 NameCallback nameCallback = null; 139 CertificateCallback certificateCallback = null; 140 try { 141 nameCallback = new NameCallback ("User :"); 144 certificateCallback = new CertificateCallback(); 145 Callback [] callbacks = new Callback [] {nameCallback, certificateCallback}; 146 callbackHandler.handle(callbacks); 147 } catch (Exception e) { 148 throw new LoginException ("Problem while getting informations in the callbackhandler: " + e.getMessage()); 149 } 150 151 try { 152 this.cert = (X509Certificate ) certificateCallback.getUserCertificate(); 153 if (nameCallback.getName().startsWith("##DN##")) { 154 if ((this.cert == null)) { 155 throw new LoginException ("Client certificate not present, it can be verified with CRL"); 156 } 157 } else { 158 return true; 161 } 162 163 CertStoreParameters certStoreParameters = null; 165 if (crlsResourceName.equalsIgnoreCase("Directory")) { 168 certStoreAccessName = "Collection"; 169 String crlsDirectoryName = (String ) options.get("CRLsDirectoryName"); 171 if (crlsDirectoryName == null) { 173 throw new LoginException ( 174 "You have to give an argument to this login module. The \"CRLsDirectoryName\" parameter is required."); 175 } 176 File crlsDirectory = new File (crlsDirectoryName); 177 if (!crlsDirectory.isDirectory()) { 178 throw new LoginException (crlsDirectoryName + " is not a directory"); 179 } 180 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 182 FileInputStream fis = null; 183 String [] crlFileName = crlsDirectory.list(); 185 ArrayList crls = new ArrayList (crlFileName.length); 187 X509CRL crl = null; 188 189 for (int i = 0; i < crlFileName.length; i++) { 190 if (crlFileName[i].matches(".+\\.crl")) { 192 fis = new FileInputStream (crlsDirectory.getAbsolutePath() + File.separatorChar + crlFileName[i]); 193 crl = (X509CRL ) cf.generateCRL(fis); 194 crls.add(crl); 195 fis.close(); 196 fis = null; 197 crl = null; 198 } 199 } 200 certStoreParameters = new CollectionCertStoreParameters (crls); 202 } else if (crlsResourceName.equalsIgnoreCase("LDAP")) { 203 certStoreAccessName = "LDAP"; 205 String address = (String ) options.get("address"); 207 int port = Integer.parseInt((String ) options.get("port")); 208 if (address == null) { 209 throw new LoginException ( 210 "You have to give an argument to this login module. The \"address\" and \"port\" parameter are required."); 211 } 212 certStoreParameters = new LDAPCertStoreParameters (address, port); 214 } else { 215 throw new LoginException ( 216 "You have to give an argument to this login module. The \"CRLsResourceName\" is not valid. Must be set to \"Directory\" or \"LDAP\""); 217 } 218 219 CertStore crlsStore = CertStore.getInstance(certStoreAccessName, certStoreParameters); 221 X509CRLSelector x509CRLSelector = new X509CRLSelector (); 222 x509CRLSelector.addIssuerName(this.cert.getIssuerX500Principal().getEncoded()); 226 Iterator crlIterator = crlsStore.getCRLs(x509CRLSelector).iterator(); 227 while (crlIterator.hasNext()) { 228 if (((X509CRL ) crlIterator.next()).isRevoked(this.cert)) { 229 throw new LoginException ("Client certificate has been revoked"); 230 } 231 } 232 } catch (Exception e) { 233 throw new LoginException ("Error during the login phase : " + e.getMessage()); 234 } 235 236 return true; 237 } 238 239 253 public boolean commit() throws LoginException { 254 return true; 255 } 256 257 268 public boolean abort() throws LoginException { 269 270 principalName = null; 272 principalRoles = null; 273 274 return true; 275 } 276 277 284 public boolean logout() throws LoginException { 285 286 subject.getPrincipals().remove(new JPrincipal(principalName)); 288 289 return true; 290 } 291 292 } | Popular Tags |