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.CertificateContext;
008: import com.sun.portal.log.common.PortalLogger;
009:
010: import javax.net.ssl.X509KeyManager;
011: import java.net.Socket;
012: import java.net.UnknownHostException;
013: import java.security.*;
014: import java.security.cert.Certificate;
015: import java.security.cert.X509Certificate;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018: import java.util.logging.LogRecord;
019:
020: public class DemoKeyManager extends DemoManagersBase implements
021: X509KeyManager {
022: private static final Logger logger = PortalLogger
023: .getLogger(DemoKeyManager.class);
024:
025: private static LogRecord getLogRecord(Level level, String message,
026: Object[] parameters, Throwable t) {
027: LogRecord result = new LogRecord(level, message);
028: result.setLoggerName(logger.getName());
029: result.setParameters(parameters);
030: result.setThrown(t);
031: return result;
032: }
033:
034: private void printIssuers(Principal[] issuers) {
035: if (logger.isLoggable(Level.FINEST)) {
036: if (issuers == null) {
037: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6000",
038: new Object[] { null }, null));
039: } else {
040: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6001",
041: new Object[] { new Integer(issuers.length) },
042: null));
043: for (int i = 0; i < issuers.length; i++) {
044: logger.log(getLogRecord(Level.FINEST,
045: "PSMN_CSPM6002", new Object[] {
046: new Integer(i),
047: issuers[i].getName() }, null));
048: }
049: }
050: }
051: }
052:
053: private void printKeyTypes(String[] keyTypes) {
054: if (logger.isLoggable(Level.FINEST)) {
055: if (keyTypes == null) {
056: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6003",
057: new Object[] { null }, null));
058: } else {
059: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6004",
060: new Object[] { new Integer(keyTypes.length) },
061: null));
062: for (int i = 0; i < keyTypes.length; i++) {
063: logger
064: .log(getLogRecord(Level.FINEST,
065: "PSMN_CSPM6002",
066: new Object[] { new Integer(i),
067: keyTypes[i] }, null));
068: }
069: }
070: }
071: }
072:
073: public String[] getClientAliases(String keyType, Principal[] issuers) {
074: if (logger.isLoggable(Level.FINEST)) {
075: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
076: new Object[] { "getClientAliases" }, null));
077: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6003",
078: new Object[] { keyType }, null));
079: printIssuers(issuers);
080: }
081:
082: try {
083: CertificateContext certificateContext = getSslContext()
084: .getSecurityContext().getCertificateContext();
085: return new String[] { certificateContext.getAlias() };
086: } catch (UnknownHostException e) {
087: if (logger.isLoggable(Level.SEVERE)) {
088: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
089: new Object[] { e.getLocalizedMessage() }, e));
090: }
091: return null;
092: }
093: }
094:
095: public String chooseClientAlias(String[] keyTypes,
096: Principal[] issuers, Socket socket) {
097: if (logger.isLoggable(Level.FINEST)) {
098: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
099: new Object[] { "chooseClientAlias" }, null));
100: printKeyTypes(keyTypes);
101: printIssuers(issuers);
102: if (socket != null) {
103: logger
104: .log(getLogRecord(Level.FINEST,
105: "PSMN_CSPM6006", new Object[] { socket
106: .getLocalSocketAddress() },
107: null));
108: logger
109: .log(getLogRecord(Level.FINEST,
110: "PSMN_CSPM6007", new Object[] { socket
111: .getRemoteSocketAddress() },
112: null));
113: }
114: }
115:
116: try {
117: CertificateContext certificateContext = getSslContext()
118: .getSecurityContext().getCertificateContext();
119: return certificateContext.getAlias();
120: } catch (UnknownHostException e) {
121: if (logger.isLoggable(Level.SEVERE)) {
122: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
123: new Object[] { e.getLocalizedMessage() }, e));
124: }
125: return null;
126: }
127: }
128:
129: public String[] getServerAliases(String keyType, Principal[] issuers) {
130: if (logger.isLoggable(Level.FINEST)) {
131: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
132: new Object[] { "getServerAliases" }, null));
133: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6003",
134: new Object[] { keyType }, null));
135: printIssuers(issuers);
136: }
137:
138: try {
139: CertificateContext certificateContext = getSslContext()
140: .getSecurityContext().getCertificateContext();
141: return new String[] { certificateContext.getAlias() };
142: } catch (UnknownHostException e) {
143: if (logger.isLoggable(Level.SEVERE)) {
144: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
145: new Object[] { e.getLocalizedMessage() }, e));
146: }
147: return null;
148: }
149: }
150:
151: public String chooseServerAlias(String keyType,
152: Principal[] issuers, Socket socket) {
153: if (logger.isLoggable(Level.FINEST)) {
154: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
155: new Object[] { "chooseServerAlias" }, null));
156: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6003",
157: new Object[] { keyType }, null));
158: printIssuers(issuers);
159: if (socket != null) {
160: logger
161: .log(getLogRecord(Level.FINEST,
162: "PSMN_CSPM6006", new Object[] { socket
163: .getLocalSocketAddress() },
164: null));
165: logger
166: .log(getLogRecord(Level.FINEST,
167: "PSMN_CSPM6007", new Object[] { socket
168: .getRemoteSocketAddress() },
169: null));
170: }
171: }
172:
173: try {
174: CertificateContext certificateContext = getSslContext()
175: .getSecurityContext().getCertificateContext();
176: return certificateContext.getAlias();
177: } catch (UnknownHostException e) {
178: if (logger.isLoggable(Level.SEVERE)) {
179: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
180: new Object[] { e.getLocalizedMessage() }, e));
181: }
182: return null;
183: }
184: }
185:
186: public X509Certificate[] getCertificateChain(String alias) {
187: if (logger.isLoggable(Level.FINEST)) {
188: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
189: new Object[] { "getCertificateChain" }, null));
190: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6008",
191: new Object[] { alias }, null));
192: }
193:
194: try {
195: Certificate[] chain = getKeyStore().getCertificateChain(
196: alias);
197: X509Certificate[] result = new X509Certificate[chain.length];
198: for (int i = 0; i < chain.length; i++) {
199: result[i] = (X509Certificate) chain[i];
200: }
201: return result;
202: } catch (KeyStoreException e) {
203: if (logger.isLoggable(Level.SEVERE)) {
204: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
205: new Object[] { e.getLocalizedMessage() }, e));
206: }
207: }
208:
209: return new X509Certificate[0];
210: }
211:
212: public PrivateKey getPrivateKey(String alias) {
213: if (logger.isLoggable(Level.FINEST)) {
214: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6005",
215: new Object[] { "getPrivateKey" }, null));
216: logger.log(getLogRecord(Level.FINEST, "PSMN_CSPM6008",
217: new Object[] { alias }, null));
218: }
219:
220: try {
221: return (PrivateKey) getKeyStore()
222: .getKey(
223: alias,
224: getSslContext().getSecurityContext()
225: .getKeyStoreContext()
226: .getKeyStorePassword());
227: } catch (KeyStoreException e) {
228: if (logger.isLoggable(Level.SEVERE)) {
229: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
230: new Object[] { e.getLocalizedMessage() }, e));
231: }
232: } catch (NoSuchAlgorithmException e) {
233: if (logger.isLoggable(Level.SEVERE)) {
234: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
235: new Object[] { e.getLocalizedMessage() }, e));
236: }
237: } catch (UnrecoverableKeyException e) {
238: if (logger.isLoggable(Level.SEVERE)) {
239: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
240: new Object[] { e.getLocalizedMessage() }, e));
241: }
242: }
243:
244: return null;
245: }
246: }
|