001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring.utilities;
006:
007: import javax.crypto.BadPaddingException;
008: import javax.crypto.Cipher;
009: import javax.crypto.CipherInputStream;
010: import javax.crypto.CipherOutputStream;
011: import javax.crypto.IllegalBlockSizeException;
012: import javax.crypto.NoSuchPaddingException;
013: import javax.crypto.SealedObject;
014: import javax.crypto.SecretKey;
015: import javax.crypto.SecretKeyFactory;
016: import javax.crypto.spec.PBEKeySpec;
017: import javax.crypto.spec.PBEParameterSpec;
018: import java.io.FileInputStream;
019: import java.io.FileNotFoundException;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import java.security.InvalidAlgorithmParameterException;
026: import java.security.InvalidKeyException;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.NoSuchProviderException;
029: import java.security.spec.InvalidKeySpecException;
030: import java.util.Properties;
031:
032: public class Hallmark {
033: public Hallmark(PropertyHelper propertyHelper) {
034: this .propertyHelper = propertyHelper;
035: if (propertyHelper == null) {
036: this .propertyHelper = new PropertyHelper(new Properties());
037: }
038: }
039:
040: private PropertyHelper propertyHelper;
041:
042: private SecretKey getSecretKey() throws NoSuchProviderException,
043: NoSuchAlgorithmException, InvalidKeySpecException {
044: SecretKeyFactory secretKeyFactory = SecretKeyFactory
045: .getInstance(
046: propertyHelper.getProperty(Hallmark.class
047: .getName(),
048: PROPERTY_SUFFIX_SECRET_KEY_ALGORITHM,
049: SECRET_KEY_ALGORITHM),
050: propertyHelper
051: .getProperty(
052: Hallmark.class.getName(),
053: PROPERTY_SUFFIX_SECRET_KEY_ALGORITHM_PROVIDER_NAME,
054: SECRET_KEY_ALGORITHM_PROVIDER_NAME));
055:
056: PBEKeySpec pbeKeySpec = new PBEKeySpec(propertyHelper
057: .getProperty(Hallmark.class.getName(),
058: PROPERTY_SUFFIX_PBE_PASSWORD, PBE_PASSWORD)
059: .toCharArray(), salt, iterationCount, keyLength);
060:
061: return secretKeyFactory.generateSecret(pbeKeySpec);
062: }
063:
064: public Cipher getCipher(int mode) throws UtilityException {
065: Cipher result;
066:
067: try {
068: result = Cipher
069: .getInstance(
070: propertyHelper
071: .getProperty(
072: Hallmark.class.getName(),
073: PROPERTY_SUFFIX_CIPHER_TRANSFORMATION,
074: CIPHER_TRANSFORMATION),
075: propertyHelper
076: .getProperty(
077: Hallmark.class.getName(),
078: PROPERTY_SUFFIX_CIPHER_TRANSFORMATION_PROVIDER_NAME,
079: CIPHER_TRANSFORMATION_PROVIDER_NAME));
080: } catch (NoSuchAlgorithmException nsae) {
081: throw new UtilityException(nsae);
082: } catch (NoSuchProviderException nspe) {
083: throw new UtilityException(nspe);
084: } catch (NoSuchPaddingException nspae) {
085: throw new UtilityException(nspae);
086: }
087:
088: SecretKey secretKey;
089: try {
090: secretKey = getSecretKey();
091: } catch (NoSuchProviderException nspe) {
092: throw new UtilityException(nspe);
093: } catch (NoSuchAlgorithmException nsae) {
094: throw new UtilityException(nsae);
095: } catch (InvalidKeySpecException ikee) {
096: throw new UtilityException(ikee);
097: }
098:
099: try {
100: result.init(mode, secretKey, new PBEParameterSpec(salt,
101: iterationCount));
102: } catch (InvalidKeyException ike) {
103: throw new UtilityException(ike);
104: } catch (InvalidAlgorithmParameterException iape) {
105: throw new UtilityException(iape);
106: }
107:
108: return result;
109: }
110:
111: public SealedObject seal(Serializable serializable)
112: throws UtilityException {
113: SealedObject result;
114: try {
115: result = new SealedObject(serializable,
116: getCipher(Cipher.ENCRYPT_MODE));
117: } catch (IOException ioe) {
118: throw new UtilityException(ioe);
119: } catch (IllegalBlockSizeException ibse) {
120: throw new UtilityException(ibse);
121: }
122:
123: return result;
124: }
125:
126: public Object unSeal(SealedObject sealedObject)
127: throws UtilityException {
128: Object result;
129: try {
130: result = sealedObject
131: .getObject(getCipher(Cipher.DECRYPT_MODE));
132: } catch (IOException ioe) {
133: throw new UtilityException(ioe);
134: } catch (ClassNotFoundException cnfe) {
135: throw new UtilityException(cnfe);
136: } catch (IllegalBlockSizeException ibse) {
137: throw new UtilityException(ibse);
138: } catch (BadPaddingException bpe) {
139: throw new UtilityException(bpe);
140: }
141:
142: return result;
143: }
144:
145: public void write(Serializable serializable, String pathName)
146: throws UtilityException {
147: FileOutputStream fos;
148: try {
149: fos = new FileOutputStream(pathName);
150: } catch (FileNotFoundException fnfe) {
151: throw new UtilityException(fnfe);
152: }
153:
154: CipherOutputStream cos = new CipherOutputStream(fos,
155: getCipher(Cipher.ENCRYPT_MODE));
156:
157: ObjectOutputStream oos;
158: try {
159: oos = new ObjectOutputStream(cos);
160: } catch (IOException ioe) {
161: throw new UtilityException(ioe);
162: }
163:
164: try {
165: oos.writeObject(seal(serializable));
166: } catch (IOException ioe) {
167: throw new UtilityException(ioe);
168: }
169:
170: try {
171: oos.flush();
172: } catch (IOException ioe) {
173: throw new UtilityException(ioe);
174: }
175:
176: try {
177: oos.close();
178: } catch (IOException ioe) {
179: throw new UtilityException(ioe);
180: }
181: }
182:
183: public Serializable read(String pathName) throws UtilityException {
184: FileInputStream fis;
185: try {
186: fis = new FileInputStream(pathName);
187: } catch (FileNotFoundException fnfe) {
188: throw new UtilityException(fnfe);
189: }
190:
191: CipherInputStream cis = new CipherInputStream(fis,
192: getCipher(Cipher.DECRYPT_MODE));
193:
194: ObjectInputStream ois = null;
195: try {
196: ois = new ObjectInputStream(cis);
197: } catch (IOException ioe) {
198: throw new UtilityException(ioe);
199: }
200:
201: SealedObject sealedObject;
202: try {
203: sealedObject = (SealedObject) ois.readObject();
204: } catch (IOException ioe) {
205: throw new UtilityException(ioe);
206: } catch (ClassNotFoundException cnfe) {
207: throw new UtilityException(cnfe);
208: }
209:
210: try {
211: ois.close();
212: } catch (IOException ioe) {
213: throw new UtilityException(ioe);
214: }
215:
216: Serializable result = (Serializable) unSeal(sealedObject);
217:
218: return result;
219: }
220:
221: private static byte[] salt = { (byte) 0xc7, (byte) 0x73,
222: (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8,
223: (byte) 0xee, (byte) 0x99 };
224: private static int iterationCount = 16;
225: private static int keyLength = 1024;
226:
227: private static String PROPERTY_SUFFIX_SECRET_KEY_ALGORITHM = "secret.key.algorithm";
228: private static String SECRET_KEY_ALGORITHM = "PBEWithMD5AndDES";
229:
230: private static String PROPERTY_SUFFIX_SECRET_KEY_ALGORITHM_PROVIDER_NAME = "secret.key.algorithm.provider.name";
231: private static String SECRET_KEY_ALGORITHM_PROVIDER_NAME = "SunJCE";
232:
233: private static String PROPERTY_SUFFIX_PBE_PASSWORD = "pbe.password";
234: private static String PBE_PASSWORD = "DemoPBEPassword";
235:
236: private static String PROPERTY_SUFFIX_CIPHER_TRANSFORMATION = "cipher.transformation";
237: private static String CIPHER_TRANSFORMATION = "PBEWithMD5AndDES";
238:
239: private static String PROPERTY_SUFFIX_CIPHER_TRANSFORMATION_PROVIDER_NAME = "cipher.transformation.provider.name";
240: private static String CIPHER_TRANSFORMATION_PROVIDER_NAME = "SunJCE";
241: }
|