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.security;
006:
007: import com.sun.portal.monitoring.utilities.Hallmark;
008: import com.sun.portal.monitoring.utilities.UtilityException;
009: import com.sun.portal.monitoring.utilities.PropertyHelper;
010:
011: import javax.crypto.Cipher;
012: import javax.crypto.CipherInputStream;
013: import javax.crypto.CipherOutputStream;
014: import javax.crypto.SealedObject;
015: import java.io.*;
016: import java.util.Properties;
017:
018: public class PasswordManager {
019: public PasswordManager(PropertyHelper propertyHelper) {
020: this .propertyHelper = propertyHelper;
021: if (propertyHelper == null) {
022: this .propertyHelper = new PropertyHelper(new Properties());
023: }
024:
025: hallmark = new Hallmark(propertyHelper);
026: }
027:
028: private PropertyHelper propertyHelper;
029: private Hallmark hallmark;
030:
031: public Password create(int length) throws SecurityException {
032: Password result = new Password(propertyHelper);
033: result.createPassword(length);
034:
035: return result;
036: }
037:
038: public void write(Password password, String pathName)
039: throws SecurityException {
040: FileOutputStream fos;
041: try {
042: fos = new FileOutputStream(pathName);
043: } catch (FileNotFoundException fnfe) {
044: throw new SecurityException(fnfe);
045: }
046:
047: CipherOutputStream cos;
048: try {
049: cos = new CipherOutputStream(fos, hallmark
050: .getCipher(Cipher.ENCRYPT_MODE));
051: } catch (UtilityException ue) {
052: throw new SecurityException(ue);
053: }
054:
055: ObjectOutputStream oos;
056: try {
057: oos = new ObjectOutputStream(cos);
058: } catch (IOException ioe) {
059: throw new SecurityException(ioe);
060: }
061:
062: try {
063: oos.writeObject(hallmark.seal(password));
064: } catch (IOException ioe) {
065: throw new SecurityException(ioe);
066: } catch (UtilityException ue) {
067: throw new SecurityException(ue);
068: }
069:
070: try {
071: oos.flush();
072: } catch (IOException ioe) {
073: throw new SecurityException(ioe);
074: }
075:
076: try {
077: oos.close();
078: } catch (IOException ioe) {
079: throw new SecurityException(ioe);
080: }
081: }
082:
083: public Password read(String pathName) throws SecurityException {
084: FileInputStream fis;
085: try {
086: fis = new FileInputStream(pathName);
087: } catch (FileNotFoundException fnfe) {
088: throw new SecurityException(fnfe);
089: }
090:
091: CipherInputStream cis;
092: try {
093: cis = new CipherInputStream(fis, hallmark
094: .getCipher(Cipher.DECRYPT_MODE));
095: } catch (UtilityException ue) {
096: throw new SecurityException(ue);
097: }
098:
099: ObjectInputStream ois = null;
100: try {
101: ois = new ObjectInputStream(cis);
102: } catch (IOException ioe) {
103: throw new SecurityException(ioe);
104: }
105:
106: SealedObject sealedObject;
107: try {
108: sealedObject = (SealedObject) ois.readObject();
109: } catch (IOException ioe) {
110: throw new SecurityException(ioe);
111: } catch (ClassNotFoundException cnfe) {
112: throw new SecurityException(cnfe);
113: }
114:
115: try {
116: ois.close();
117: } catch (IOException ioe) {
118: throw new SecurityException(ioe);
119: }
120:
121: Password result;
122: try {
123: result = (Password) hallmark.unSeal(sealedObject);
124: } catch (UtilityException ue) {
125: throw new SecurityException(ue);
126: }
127:
128: return result;
129: }
130: }
|