001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.keyinfo;
024:
025: import com.sun.xml.ws.security.opt.api.SecurityElement;
026: import com.sun.xml.ws.security.opt.api.keyinfo.BuilderResult;
027: import com.sun.xml.ws.security.opt.api.reference.DirectReference;
028: import com.sun.xml.ws.security.IssuedTokenContext;
029: import com.sun.xml.ws.security.secconv.impl.bindings.SecurityContextTokenType;
030: import com.sun.xml.wss.XWSSecurityException;
031: import com.sun.xml.wss.impl.MessageConstants;
032: import com.sun.xml.wss.impl.misc.SecurityUtil;
033: import com.sun.xml.wss.impl.policy.mls.SecureConversationTokenKeyBinding;
034: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
035: import java.security.Key;
036: import java.util.logging.Level;
037: import javax.crypto.spec.SecretKeySpec;
038: import com.sun.xml.wss.logging.impl.opt.token.LogStringsMessages;
039:
040: /**
041: *
042: * @author K.Venugopal@sun.com
043: */
044: public class SCTBuilder extends TokenBuilder {
045: private SecureConversationTokenKeyBinding sctBinding = null;
046:
047: /** Creates a new instance of SCTBuilder */
048: public SCTBuilder(JAXBFilterProcessingContext context,
049: SecureConversationTokenKeyBinding kb) {
050: super (context);
051: this .sctBinding = kb;
052:
053: }
054:
055: public BuilderResult process() throws XWSSecurityException {
056: BuilderResult sctResult = new BuilderResult();
057:
058: String dataEncAlgo = SecurityUtil
059: .getDataEncryptionAlgo(context);
060:
061: String sctPolicyId = sctBinding.getUUID();
062: //Look for SCT in TokenCache
063: SecurityElement sct = context.getSecurityHeader()
064: .getChildElement(sctPolicyId);
065: IssuedTokenContext ictx = context
066: .getSecureConversationContext();
067: boolean includeToken = (sctBinding.INCLUDE_ALWAYS
068: .equals(sctBinding.getIncludeToken()) || (sctBinding.INCLUDE_ALWAYS_TO_RECIPIENT
069: .equals(sctBinding.getIncludeToken())));
070: if (sct == null) {
071: com.sun.xml.ws.security.SecurityContextToken sct1 = (com.sun.xml.ws.security.SecurityContextToken) ictx
072: .getSecurityToken();
073: if (sct1 == null) {
074: logger.log(Level.SEVERE, LogStringsMessages
075: .WSS_1809_SCT_NOT_FOUND());
076: throw new XWSSecurityException(
077: "SecureConversation Token not Found");
078: }
079: sct = context.getSecurityHeader().getChildElement(
080: sct1.getWsuId());
081: if (sct == null) {
082: SecurityContextToken scToken = new SecurityContextToken(
083: (SecurityContextTokenType) sct1, context
084: .getSOAPVersion());
085: //elementFactory.createSecurityContextToken(sct1.getIdentifier(),sct1.getInstance(),sct1.getWsuId());
086:
087: if (includeToken)
088: context.getSecurityHeader().add(scToken);
089: sct = scToken;
090: }
091: //Add ext elements;
092: }
093:
094: String sctWsuId = sct.getId();
095: if (sctWsuId == null) {
096: sct.setId(context.generateID());
097: sctWsuId = sct.getId();
098: }
099:
100: Key dataProtectionKey = null;
101:
102: DirectReference directRef = elementFactory
103: .createDirectReference();
104: if (includeToken) {
105: directRef.setURI("#" + sctWsuId);
106: } else {
107: directRef.setURI(((SecurityContextToken) sct)
108: .getIdentifier().toString());
109: }
110:
111: if (!sctBinding.INCLUDE_ALWAYS_TO_RECIPIENT.equals(sctBinding
112: .getIncludeToken())
113: || !sctBinding.INCLUDE_ALWAYS.equals(sctBinding
114: .getIncludeToken())) {
115: directRef.setValueType(MessageConstants.SCT_VALUETYPE);
116: }
117:
118: String jceAlgo = SecurityUtil
119: .getSecretKeyAlgorithm(dataEncAlgo);
120: dataProtectionKey = new SecretKeySpec(ictx.getProofKey(),
121: jceAlgo);
122: buildKeyInfo(directRef, context.generateID());
123: sctResult.setKeyInfo(super.keyInfo);
124: sctResult.setDataProtectionKey(dataProtectionKey);
125: return sctResult;
126: }
127: }
|