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.SecurityHeaderElement;
026: import com.sun.xml.ws.security.opt.api.keyinfo.BuilderResult;
027: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo;
028: import com.sun.xml.ws.security.opt.impl.message.GSHeaderElement;
029: import com.sun.xml.wss.XWSSecurityException;
030: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
031: import com.sun.xml.ws.security.secext10.SecurityTokenReferenceType;
032: import com.sun.xml.ws.security.trust.GenericToken;
033: import com.sun.xml.wss.impl.MessageConstants;
034: import com.sun.xml.wss.impl.misc.SecurityUtil;
035: import com.sun.xml.wss.impl.policy.mls.IssuedTokenKeyBinding;
036: import java.security.Key;
037: import java.security.cert.X509Certificate;
038: import java.util.Collections;
039: import java.util.HashMap;
040: import java.util.List;
041: import java.util.logging.Level;
042: import javax.crypto.spec.SecretKeySpec;
043: import javax.xml.bind.JAXBElement;
044: import org.w3c.dom.Element;
045: import com.sun.xml.wss.logging.impl.opt.token.LogStringsMessages;
046:
047: /**
048: *
049: * @author K.Venugopal@sun.com
050: */
051: public class IssuedTokenBuilder extends TokenBuilder {
052: private IssuedTokenKeyBinding ikb = null;
053:
054: /** Creates a new instance of IssuedTokenBuilder */
055: public IssuedTokenBuilder(JAXBFilterProcessingContext context,
056: IssuedTokenKeyBinding kb) {
057: super (context);
058: this .ikb = kb;
059: }
060:
061: public BuilderResult process() throws XWSSecurityException {
062: BuilderResult itkbResult = new BuilderResult();
063: byte[] proofKey = context.getTrustContext().getProofKey();
064: Key dataProtectionKey = null;
065: SecurityTokenReferenceType str = null;
066: Key cacheKey = null;
067: //For Encryption proofKey will be null.
068: if (proofKey == null) {
069: X509Certificate cert = context.getTrustContext()
070: .getRequestorCertificate();
071: if (cert == null) {
072: logger.log(Level.SEVERE, LogStringsMessages
073: .WSS_1807_CERT_PROOF_KEY_NULL_ISSUEDTOKEN());
074: throw new XWSSecurityException(
075: "Requestor Certificate and Proof Key are both null for Issued Token");
076: }
077: dataProtectionKey = context.getSecurityEnvironment()
078: .getPrivateKey(context.getExtraneousProperties(),
079: cert);
080: cacheKey = cert.getPublicKey();
081: } else {
082: String secretKeyAlg = "AES";
083: if (context.getAlgorithmSuite() != null) {
084: secretKeyAlg = SecurityUtil
085: .getSecretKeyAlgorithm(context
086: .getAlgorithmSuite()
087: .getEncryptionAlgorithm());
088: }
089: //TODO: assuming proofkey is a byte array in case of Trust as well
090: dataProtectionKey = new SecretKeySpec(proofKey,
091: secretKeyAlg);
092: cacheKey = dataProtectionKey;
093: //SecurityUtil.updateSamlVsKeyCache(str, context, dataProtectionKey);
094: }
095:
096: SecurityHeaderElement issuedTokenElement = null;
097: GenericToken issuedToken = (GenericToken) context
098: .getTrustContext().getSecurityToken();
099: if (issuedToken != null) {
100: issuedTokenElement = issuedToken.getElement();
101: if (issuedTokenElement == null) {
102: Element element = (Element) issuedToken.getTokenValue();
103: issuedTokenElement = new GSHeaderElement(element);
104: issuedTokenElement.setId(issuedToken.getId());
105: itkbResult.setDPTokenId(issuedToken.getId());
106: }
107: String tokId = issuedTokenElement.getId();
108: if ("".equals(tokId)
109: && MessageConstants.ENCRYPTED_DATA_LNAME
110: .equals(issuedTokenElement.getLocalPart())) {
111: logger.log(Level.SEVERE, LogStringsMessages
112: .WSS_1808_ID_NOTSET_ENCRYPTED_ISSUEDTOKEN());
113: throw new XWSSecurityException("ID attribute not set");
114: }
115: context.getTokenCache().put(ikb.getUUID(),
116: issuedTokenElement);
117:
118: HashMap sentSamlKeys = (HashMap) context
119: .getExtraneousProperty(MessageConstants.STORED_SAML_KEYS);
120: if (sentSamlKeys == null) {
121: sentSamlKeys = new HashMap();
122: }
123: sentSamlKeys.put(tokId, dataProtectionKey);
124: context.setExtraneousProperty(
125: MessageConstants.STORED_SAML_KEYS, sentSamlKeys);
126: }
127: boolean includeToken = (ikb.INCLUDE_ALWAYS.equals(ikb
128: .getIncludeToken()) || (ikb.INCLUDE_ALWAYS_TO_RECIPIENT
129: .equals(ikb.getIncludeToken())));
130:
131: if (includeToken) {
132: str = (SecurityTokenReferenceType) context
133: .getTrustContext()
134: .getAttachedSecurityTokenReference();
135: } else {
136: str = (SecurityTokenReferenceType) context
137: .getTrustContext()
138: .getUnAttachedSecurityTokenReference();
139: }
140:
141: if (issuedToken != null && includeToken) {
142: if (context.getSecurityHeader().getChildElement(
143: issuedTokenElement.getId()) == null) {
144: context.getSecurityHeader().add(issuedTokenElement);
145: }
146: }
147:
148: keyInfo = new KeyInfo();
149: JAXBElement je = new com.sun.xml.ws.security.secext10.ObjectFactory()
150: .createSecurityTokenReference(str);
151: List strList = Collections.singletonList(je);
152: keyInfo.setContent(strList);
153: if (str != null)
154: SecurityUtil.updateSamlVsKeyCache(str, context, cacheKey);
155: itkbResult.setDataProtectionKey(dataProtectionKey);
156: itkbResult.setKeyInfo(keyInfo);
157: return itkbResult;
158: }
159: }
|