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.util.Map;
027: import javax.resource.spi.security.PasswordCredential;
028: import javax.security.auth.Subject;
029: import javax.security.auth.callback.CallbackHandler;
030: import javax.security.auth.login.LoginException;
031: import javax.crypto.Cipher;
032: import javax.crypto.SecretKey;
033: import javax.crypto.SecretKeyFactory;
034: import javax.crypto.spec.PBEKeySpec;
035: import javax.crypto.spec.PBEParameterSpec;
036:
037: import org.jboss.security.SimplePrincipal;
038: import org.jboss.security.Util;
039: import org.jboss.logging.Logger;
040:
041: /** An example of how one could encrypt the database password for a jca
042: connection factory. The corresponding login config entry illustrates
043: the usage:
044:
045: <application-policy name = "testPBEIdentityLoginModule">
046: <authentication>
047: <login-module code = "org.jboss.resource.security.PBEIdentityLoginModule"
048: flag = "required">
049: <module-option name = "principal">sa</module-option>
050: <module-option name = "userName">sa</module-option>
051: <!--
052: output from:
053: org.jboss.resource.security.PBEIdentityLoginModule
054: thesecret testPBEIdentityLoginModule abcdefgh 19 PBEWithMD5AndDES
055: -->
056: <module-option name = "password">3fp7R/7TMjyTTxhmePdJVk</module-option>
057: <module-option name = "ignoreMissigingMCF">true</module-option>
058: <module-option name = "pbealgo">PBEWithMD5AndDES</module-option>
059: <module-option name = "pbepass">testPBEIdentityLoginModule</module-option>
060: <module-option name = "salt">abcdefgh</module-option>
061: <module-option name = "iterationCount">19</module-option>
062: <module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=DefaultDS</module-option>
063: </login-module>
064: </authentication>
065: </application-policy>
066:
067: This uses password based encryption (PBE) with algorithm parameters dervived
068: from pbealgo, pbepass, salt, iterationCount options:
069: + pbealgo - the PBE algorithm to use. Defaults to PBEwithMD5andDES.
070: + pbepass - the PBE password to use. Can use the JaasSecurityDomain {CLASS}
071: and {EXT} syntax to obtain the password from outside of the configuration.
072: Defaults to "jaas is the way".
073: + salt - the PBE salt as a string. Defaults to {1, 7, 2, 9, 3, 11, 4, 13}.
074: + iterationCount - the PBE iterationCount. Defaults to 37.
075:
076: * @author Scott.Stark@jboss.org
077: * @author <a href="mailto:noel.rocher@jboss.org">Noel Rocher</a> 29, june 2004 username & userName issue
078: * @version $Revision: 57189 $
079: */
080: public class PBEIdentityLoginModule extends
081: AbstractPasswordCredentialLoginModule {
082: /**
083: * Class logger
084: */
085: private static final Logger log = Logger
086: .getLogger(SecureIdentityLoginModule.class);
087:
088: private String username;
089: private String password;
090: /** The Blowfish key material */
091: private char[] pbepass = "jaas is the way".toCharArray();
092: private String pbealgo = "PBEwithMD5andDES";
093: private byte[] salt = { 1, 7, 2, 9, 3, 11, 4, 13 };
094: private int iterationCount = 37;
095: private PBEParameterSpec cipherSpec;
096:
097: public PBEIdentityLoginModule() {
098: }
099:
100: PBEIdentityLoginModule(String algo, char[] pass, byte[] pbesalt,
101: int iter) {
102: if (pass != null)
103: pbepass = pass;
104: if (algo != null)
105: pbealgo = algo;
106: if (pbesalt != null)
107: salt = pbesalt;
108: if (iter > 0)
109: iterationCount = iter;
110: }
111:
112: public void initialize(Subject subject, CallbackHandler handler,
113: Map sharedState, Map options) {
114: super .initialize(subject, handler, sharedState, options);
115: // NR : we keep this username for compatibility
116: username = (String) options.get("username");
117: if (username == null) {
118: // NR : try with userName
119: username = (String) options.get("userName");
120: if (username == null) {
121: throw new IllegalArgumentException(
122: "The user name is a required option");
123: }
124: }
125: password = (String) options.get("password");
126: if (password == null) {
127: throw new IllegalArgumentException(
128: "The password is a required option");
129: }
130: // Look for the cipher password and algo parameters
131: String tmp = (String) options.get("pbepass");
132: if (tmp != null) {
133: try {
134: pbepass = Util.loadPassword(tmp);
135: } catch (Exception e) {
136: throw new IllegalStateException(e);
137: }
138: }
139: tmp = (String) options.get("pbealgo");
140: if (tmp != null)
141: pbealgo = tmp;
142: tmp = (String) options.get("salt");
143: if (tmp != null)
144: salt = tmp.substring(0, 8).getBytes();
145: tmp = (String) options.get("iterationCount");
146: if (tmp != null)
147: iterationCount = Integer.parseInt(tmp);
148: }
149:
150: public boolean login() throws LoginException {
151: log.trace("login called");
152: if (super .login() == true)
153: return true;
154:
155: super .loginOk = true;
156: return true;
157: }
158:
159: public boolean commit() throws LoginException {
160: Principal principal = new SimplePrincipal(username);
161: SubjectActions.addPrincipals(subject, principal);
162: sharedState.put("javax.security.auth.login.name", username);
163: // Decode the encrypted password
164: try {
165: char[] decodedPassword = decode(password);
166: PasswordCredential cred = new PasswordCredential(username,
167: decodedPassword);
168: cred.setManagedConnectionFactory(getMcf());
169: SubjectActions.addCredentials(subject, cred);
170: } catch (Exception e) {
171: log.debug("Failed to decode password", e);
172: throw new LoginException("Failed to decode password: "
173: + e.getMessage());
174: }
175: return true;
176: }
177:
178: public boolean abort() {
179: username = null;
180: password = null;
181: return true;
182: }
183:
184: protected Principal getIdentity() {
185: log.trace("getIdentity called, username=" + username);
186: Principal principal = new SimplePrincipal(username);
187: return principal;
188: }
189:
190: protected Group[] getRoleSets() throws LoginException {
191: Group[] empty = new Group[0];
192: return empty;
193: }
194:
195: private String encode(String secret) throws Exception {
196: // Create the PBE secret key
197: cipherSpec = new PBEParameterSpec(salt, iterationCount);
198: PBEKeySpec keySpec = new PBEKeySpec(pbepass);
199: SecretKeyFactory factory = SecretKeyFactory
200: .getInstance(pbealgo);
201: SecretKey cipherKey = factory.generateSecret(keySpec);
202:
203: // Decode the secret
204: Cipher cipher = Cipher.getInstance(pbealgo);
205: cipher.init(Cipher.ENCRYPT_MODE, cipherKey, cipherSpec);
206: byte[] encoding = cipher.doFinal(secret.getBytes());
207: return Util.tob64(encoding);
208: }
209:
210: private char[] decode(String secret) throws Exception {
211: // Create the PBE secret key
212: cipherSpec = new PBEParameterSpec(salt, iterationCount);
213: PBEKeySpec keySpec = new PBEKeySpec(pbepass);
214: SecretKeyFactory factory = SecretKeyFactory
215: .getInstance(pbealgo);
216: SecretKey cipherKey = factory.generateSecret(keySpec);
217: // Decode the secret
218: byte[] encoding = Util.fromb64(secret);
219: Cipher cipher = Cipher.getInstance(pbealgo);
220: cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherSpec);
221: byte[] decode = cipher.doFinal(encoding);
222: return new String(decode).toCharArray();
223: }
224:
225: /** Main entry point to encrypt a password using the hard-coded pass phrase
226: *
227: * @param args - [0] = the password to encode
228: * [1] = PBE password
229: * [2] = PBE salt
230: * [3] = PBE iterationCount
231: * [4] = PBE algo
232: * @throws Exception
233: */
234: public static void main(String[] args) throws Exception {
235: String algo = null;
236: char[] pass = "jaas is the way".toCharArray();
237: byte[] salt = null;
238: int iter = -1;
239: if (args.length >= 2)
240: pass = args[1].toCharArray();
241: if (args.length >= 3)
242: salt = args[2].getBytes();
243: if (args.length >= 4)
244: iter = Integer.decode(args[3]).intValue();
245: if (args.length >= 5)
246: algo = args[4];
247:
248: PBEIdentityLoginModule pbe = new PBEIdentityLoginModule(algo,
249: pass, salt, iter);
250: String encode = pbe.encode(args[0]);
251: System.out.println("Encoded password: " + encode);
252: }
253: }
|