001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.system.util;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.security.SecureRandom;
029:
030: import javax.crypto.spec.SecretKeySpec;
031:
032: import org.apache.geronimo.gbean.GBeanInfo;
033: import org.apache.geronimo.gbean.GBeanInfoBuilder;
034: import org.apache.geronimo.gbean.GBeanLifecycle;
035: import org.apache.geronimo.system.serverinfo.ServerInfo;
036: import org.apache.geronimo.crypto.AbstractEncryption;
037: import org.apache.geronimo.crypto.EncryptionManager;
038:
039: /**
040: * Like SimpleEncryption except it uses a stored secret key. If the key file is missing, it makes up a new one.
041: *
042: * WARNING: NOT RECOMMENDED. If you lose the secret key file your encrypted passwords will be unavailable. Instead, secure
043: * your operationg environment and use something like ldap or a database to store passwords in.
044: *
045: * To use, include something like this in the rmi-naming module of var/config/config.xml:
046: *
047: * <gbean name="org.apache.geronimo.framework/rmi-naming/2.1-SNAPSHOT/car?name=ConfiguredEncryption,j2eeType=GBean" gbeanInfo="org.apache.geronimo.system.util.ConfiguredEncryption">
048: * <attribute name="path">var/security/ConfiguredSecretKey.ser</attribute>
049: * <reference name="ServerInfo"><pattern><name>ServerInfo</name></pattern></reference>
050: * </gbean>
051: *
052: * @version $Rev: 617588 $ $Date: 2008-02-01 10:20:07 -0800 (Fri, 01 Feb 2008) $
053: */
054: public class ConfiguredEncryption extends AbstractEncryption implements
055: GBeanLifecycle {
056:
057: private final SecretKeySpec spec;
058:
059: public ConfiguredEncryption(String path, ServerInfo serverInfo)
060: throws IOException, ClassNotFoundException {
061: File location = serverInfo.resolve(path);
062: if (location.exists()) {
063: FileInputStream in = new FileInputStream(location);
064: try {
065: ObjectInputStream oin = new ObjectInputStream(in);
066: try {
067: spec = (SecretKeySpec) oin.readObject();
068: } finally {
069: oin.close();
070: }
071: } finally {
072: in.close();
073: }
074: } else {
075: SecureRandom random = new SecureRandom();
076: random.setSeed(System.currentTimeMillis());
077: byte[] bytes = new byte[16];
078: random.nextBytes(bytes);
079: spec = new SecretKeySpec(bytes, "AES");
080: File dir = location.getParentFile();
081: if (!dir.exists()) {
082: dir.mkdirs();
083: }
084: if (!dir.exists() || !dir.isDirectory()) {
085: throw new IllegalStateException(
086: "Could not create directory for secret key spec: "
087: + dir);
088: }
089: FileOutputStream out = new FileOutputStream(location);
090: try {
091: ObjectOutputStream oout = new ObjectOutputStream(out);
092: try {
093: oout.writeObject(spec);
094: oout.flush();
095: } finally {
096: oout.close();
097: }
098: } finally {
099: out.close();
100: }
101: }
102: }
103:
104: public void doStart() throws Exception {
105: EncryptionManager.setEncryptionPrefix("{Configured}", this );
106: }
107:
108: public void doStop() throws Exception {
109: }
110:
111: public void doFail() {
112: }
113:
114: protected SecretKeySpec getSecretKeySpec() {
115: return spec;
116: }
117:
118: public static final GBeanInfo GBEAN_INFO;
119:
120: static {
121: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(
122: ConfiguredEncryption.class, "GBean");
123: infoBuilder.addAttribute("path", String.class, true, true);
124: infoBuilder.addReference("ServerInfo", ServerInfo.class,
125: "GBean");
126: infoBuilder
127: .setConstructor(new String[] { "path", "ServerInfo" });
128: GBEAN_INFO = infoBuilder.getBeanInfo();
129: }
130:
131: public static GBeanInfo getGBeanInfo() {
132: return GBEAN_INFO;
133: }
134:
135: }
|