001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.security;
023:
024: import java.security.acl.Group;
025: import java.security.Principal;
026: import java.security.NoSuchAlgorithmException;
027: import java.security.InvalidKeyException;
028: import java.util.Map;
029: import java.math.BigInteger;
030: import javax.resource.spi.security.PasswordCredential;
031: import javax.security.auth.Subject;
032: import javax.security.auth.callback.CallbackHandler;
033: import javax.security.auth.login.LoginException;
034: import javax.crypto.NoSuchPaddingException;
035: import javax.crypto.BadPaddingException;
036: import javax.crypto.IllegalBlockSizeException;
037: import javax.crypto.Cipher;
038: import javax.crypto.spec.SecretKeySpec;
039:
040: import org.jboss.security.SimplePrincipal;
041: import org.jboss.logging.Logger;
042:
043: /** An example of how one could encrypt the database password for a jca
044: connection factory. The corresponding
045:
046: <application-policy name = "HsqlDbRealm">
047: <authentication>
048: <login-module code = "org.jboss.resource.security.SecureIdentityLoginMdule"
049: flag = "required">
050: <module-option name = "userName">sa</module-option>
051: <module-option name = "password">-207a6df87216de44</module-option>
052: <module-option name = "managedConnectionFactoryName">jboss.jca:servce=LocalTxCM,name=DefaultDS</module-option>
053: </login-module>
054: </authentication>
055: </application-policy>
056:
057: This uses a hard-coded cipher algo of Blowfish, and key derived from the
058: phrase 'jaas is the way'. Adjust to your requirements.
059:
060: * @author Scott.Stark@jboss.org
061: * @author <a href="mailto:noel.rocher@jboss.org">Noel Rocher</a> 29, june 2004 username & userName issue
062: * @version $Revision: 57189 $
063: */
064: public class SecureIdentityLoginModule extends
065: AbstractPasswordCredentialLoginModule {
066: /**
067: * Class logger
068: */
069: private static final Logger log = Logger
070: .getLogger(SecureIdentityLoginModule.class);
071:
072: private String username;
073: private String password;
074:
075: public void initialize(Subject subject, CallbackHandler handler,
076: Map sharedState, Map options) {
077: super .initialize(subject, handler, sharedState, options);
078: // NR : we keep this username for compatibility
079: username = (String) options.get("username");
080: if (username == null) {
081: // NR : try with userName
082: username = (String) options.get("userName");
083: if (username == null) {
084: throw new IllegalArgumentException(
085: "The user name is a required option");
086: }
087: }
088: password = (String) options.get("password");
089: if (password == null) {
090: throw new IllegalArgumentException(
091: "The password is a required option");
092: }
093: }
094:
095: public boolean login() throws LoginException {
096: log.trace("login called");
097: if (super .login() == true)
098: return true;
099:
100: super .loginOk = true;
101: return true;
102: }
103:
104: public boolean commit() throws LoginException {
105: Principal principal = new SimplePrincipal(username);
106: SubjectActions.addPrincipals(subject, principal);
107: sharedState.put("javax.security.auth.login.name", username);
108: // Decode the encrypted password
109: try {
110: char[] decodedPassword = decode(password);
111: PasswordCredential cred = new PasswordCredential(username,
112: decodedPassword);
113: cred.setManagedConnectionFactory(getMcf());
114: SubjectActions.addCredentials(subject, cred);
115: } catch (Exception e) {
116: log.debug("Failed to decode password", e);
117: throw new LoginException("Failed to decode password: "
118: + e.getMessage());
119: }
120: return true;
121: }
122:
123: public boolean abort() {
124: username = null;
125: password = null;
126: return true;
127: }
128:
129: protected Principal getIdentity() {
130: log.trace("getIdentity called, username=" + username);
131: Principal principal = new SimplePrincipal(username);
132: return principal;
133: }
134:
135: protected Group[] getRoleSets() throws LoginException {
136: Group[] empty = new Group[0];
137: return empty;
138: }
139:
140: private static String encode(String secret)
141: throws NoSuchPaddingException, NoSuchAlgorithmException,
142: InvalidKeyException, BadPaddingException,
143: IllegalBlockSizeException {
144: byte[] kbytes = "jaas is the way".getBytes();
145: SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
146:
147: Cipher cipher = Cipher.getInstance("Blowfish");
148: cipher.init(Cipher.ENCRYPT_MODE, key);
149: byte[] encoding = cipher.doFinal(secret.getBytes());
150: BigInteger n = new BigInteger(encoding);
151: return n.toString(16);
152: }
153:
154: private static char[] decode(String secret)
155: throws NoSuchPaddingException, NoSuchAlgorithmException,
156: InvalidKeyException, BadPaddingException,
157: IllegalBlockSizeException {
158: byte[] kbytes = "jaas is the way".getBytes();
159: SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
160:
161: BigInteger n = new BigInteger(secret, 16);
162: byte[] encoding = n.toByteArray();
163:
164: Cipher cipher = Cipher.getInstance("Blowfish");
165: cipher.init(Cipher.DECRYPT_MODE, key);
166: byte[] decode = cipher.doFinal(encoding);
167: return new String(decode).toCharArray();
168: }
169:
170: /** Main entry point to encrypt a password using the hard-coded pass phrase
171: *
172: * @param args - [0] = the password to encode
173: * @throws Exception
174: */
175: public static void main(String[] args) throws Exception {
176: String encode = encode(args[0]);
177: System.out.println("Encoded password: " + encode);
178: }
179: }
|