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.ssl;
006:
007: import com.sun.portal.monitoring.security.KeyStoreContext;
008: import com.sun.portal.log.common.PortalLogger;
009:
010: import java.io.*;
011: import java.security.KeyStore;
012: import java.security.KeyStoreException;
013: import java.security.NoSuchAlgorithmException;
014: import java.security.NoSuchProviderException;
015: import java.security.cert.CertificateException;
016: import java.util.logging.Logger;
017: import java.util.logging.LogRecord;
018: import java.util.logging.Level;
019:
020: public class KeyStoreFactory {
021: private static final Logger logger = PortalLogger
022: .getLogger(KeyStoreFactory.class);
023:
024: private static LogRecord getLogRecord(Level level, String message,
025: Object[] parameters, Throwable t) {
026: LogRecord result = new LogRecord(level, message);
027: result.setLoggerName(logger.getName());
028: result.setParameters(parameters);
029: result.setThrown(t);
030: return result;
031: }
032:
033: public SslContext getSslContext() {
034: return sslContext;
035: }
036:
037: public void setSslContext(SslContext sslContext) {
038: this .sslContext = sslContext;
039: }
040:
041: private InputStream getKeyStoreInputStream(Boolean isTrustStore)
042: throws SslException {
043: KeyStoreContext keyStoreContext = isTrustStore.booleanValue() ? sslContext
044: .getSecurityContext().getTrustStoreContext()
045: : sslContext.getSecurityContext().getKeyStoreContext();
046: final String ksLocation = keyStoreContext
047: .getKeyStoreDirectory()
048: + File.separator
049: + keyStoreContext.getKeyStoreFileName();
050: InputStream result;
051: try {
052: result = new FileInputStream(ksLocation);
053: } catch (FileNotFoundException fnfe) {
054: result = ClassLoader.getSystemResourceAsStream(ksLocation);
055: }
056:
057: if (result == null) {
058: if (logger.isLoggable(Level.CONFIG)) {
059: logger.log(getLogRecord(Level.CONFIG, "PSMN_CSPM3000",
060: new Object[] { ksLocation }, null));
061: }
062: throw new SslException("Cannot load " + ksLocation);
063: }
064:
065: return result;
066: }
067:
068: public java.security.KeyStore getKeyStore(Boolean isTrustStore)
069: throws SslException {
070: java.security.KeyStore result;
071:
072: String providerName = isTrustStore.booleanValue() ? sslContext
073: .getTrustStoreProviderName() : sslContext
074: .getKeyStoreProviderName();
075: String defaultProviderName = isTrustStore.booleanValue() ? SslContext.TRUST_STORE_PROVIDER_NAME
076: : SslContext.KEY_STORE_PROVIDER_NAME;
077: if (providerName.equals(defaultProviderName)) {
078: try {
079: result = KeyStore.getInstance(
080: PORTAL_SERVER_KEY_STORE_TYPE,
081: PORTAL_SERVER_KEY_STORE_PROVIDER_NAME);
082: } catch (KeyStoreException kse) {
083: throw new SslException(kse);
084: } catch (NoSuchProviderException nspe) {
085: throw new SslException(nspe);
086: }
087:
088: final InputStream isKeyStore = getKeyStoreInputStream(isTrustStore);
089: try {
090: KeyStoreContext keyStoreContext = isTrustStore
091: .booleanValue() ? sslContext
092: .getSecurityContext().getTrustStoreContext()
093: : sslContext.getSecurityContext()
094: .getKeyStoreContext();
095: result.load(isKeyStore, keyStoreContext
096: .getKeyStorePassword());
097: } catch (IOException ioe) {
098: throw new SslException(ioe);
099: } catch (NoSuchAlgorithmException nsae) {
100: throw new SslException(nsae);
101: } catch (CertificateException ce) {
102: throw new SslException(ce);
103: }
104: } else {
105: try {
106: String keyStoreType = isTrustStore.booleanValue() ? sslContext
107: .getTrustStoreType()
108: : sslContext.getKeyStoreType();
109: result = KeyStore.getInstance(keyStoreType,
110: providerName);
111: } catch (KeyStoreException kse) {
112: throw new SslException(kse);
113: } catch (NoSuchProviderException nspe) {
114: throw new SslException(nspe);
115: }
116: }
117:
118: return result;
119: }
120:
121: private SslContext sslContext;
122:
123: private static String PORTAL_SERVER_KEY_STORE_TYPE = "jks";
124: private static String PORTAL_SERVER_KEY_STORE_PROVIDER_NAME = "SUN";
125: }
|