001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SecretKeyCallbackHandler.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * SecretKeyCallbackHandler.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on February 23, 2005, 1:53 PM
037: */package com.sun.jbi.internal.security.callback;
038:
039: import com.sun.enterprise.security.jauth.callback.SecretKeyCallback;
040: import com.sun.jbi.internal.security.KeyStoreManager;
041:
042: import java.io.IOException;
043: import java.security.Key;
044: import java.security.KeyStore;
045: import javax.crypto.SecretKey;
046:
047: import javax.security.auth.callback.Callback;
048: import javax.security.auth.callback.CallbackHandler;
049: import javax.security.auth.callback.UnsupportedCallbackException;
050:
051: /**
052: * This CallbackHandler handles the SecretKey Callback.
053: *
054: * @author Sun Microsystems, Inc.
055: */
056: public class SecretKeyCallbackHandler implements CallbackHandler {
057: /** The reference to the KeyStoreManager. */
058: private KeyStoreManager mKeyMgr;
059:
060: /** The X509 Certificate Type. */
061: private static final String X509 = "X509";
062:
063: /**
064: * Creates a new instance of SecretKeyCallbackHandler.
065: *
066: * @param mgr - KeyStoreManager instance which provides the handle
067: * to the KeyStores.
068: */
069: public SecretKeyCallbackHandler(KeyStoreManager mgr) {
070: mKeyMgr = mgr;
071: }
072:
073: /**
074: * The implementation on the CallbackInterface. This method only handles
075: * SecretKeyCallback.
076: *
077: * @param callbacks - array of Callbacks to be handled.
078: * @throws IOException - if an input or output error occurs.
079: * @throws UnsupportedCallbackException - if the implementation of this method
080: * does not support one or more of the Callbacks specified in the callbacks
081: * parameter.
082: */
083: public void handle(Callback[] callbacks) throws IOException,
084: UnsupportedCallbackException {
085: for (int i = 0; i < callbacks.length; i++) {
086: CallbackHandler handler = null;
087:
088: if (callbacks[i] instanceof SecretKeyCallback) {
089: SecretKeyCallback cb = (SecretKeyCallback) callbacks[i];
090: Object req = cb.getRequest();
091: cb.setKey(null);
092: if (req instanceof SecretKeyCallback.AliasRequest) {
093: handleAliasRequest(
094: (SecretKeyCallback.AliasRequest) req, cb);
095: }
096: } else {
097: throw new UnsupportedCallbackException(callbacks[i]);
098: }
099: }
100: }
101:
102: /**
103: * Handle a request for a Secret Key based on alias. Go through the KeyStore and
104: * get the Secret Key for the alias. If there is no key entry for the alias then
105: * the key the Callback is null.
106: *
107: * @param req is the alias request from the SecretKeyCallback
108: * @param cb is the SecretKeyCallback
109: * @throws IOException - if an input or output error occurs. This would indicate
110: * that the Key could not be retrieved from the store.
111: */
112: private void handleAliasRequest(SecretKeyCallback.AliasRequest req,
113: SecretKeyCallback cb) throws IOException {
114: KeyStore ks = mKeyMgr.getKeyStore();
115:
116: if (req.getAlias() == null) {
117: cb.setKey(null);
118: }
119:
120: try {
121: if (ks.containsAlias(req.getAlias())) {
122: Key key = ks.getKey(req.getAlias(), mKeyMgr
123: .getKeyStorePassword().toCharArray());
124: if (key instanceof SecretKey) {
125: cb.setKey((SecretKey) key);
126: }
127: }
128: } catch (Exception ex) {
129: throw new IOException(ex.getMessage());
130: }
131:
132: }
133:
134: }
|