001: /*
002: * SignatureProcessor.java
003: *
004: * Created on August 10, 2006, 2:56 PM
005: *
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.ws.security.opt.impl.dsig;
028:
029: import com.sun.xml.ws.security.opt.api.keyinfo.BuilderResult;
030: import com.sun.xml.ws.security.opt.impl.util.NamespaceAndPrefixMapper;
031: import com.sun.xml.ws.security.opt.impl.util.NamespaceContextEx;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import javax.xml.crypto.dsig.keyinfo.KeyInfo;
036: import com.sun.xml.ws.security.opt.crypto.jaxb.JAXBSignContext;
037: import com.sun.xml.wss.impl.policy.mls.SignaturePolicy;
038: import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
039: import com.sun.xml.wss.logging.LogDomainConstants;
040: import com.sun.xml.wss.logging.impl.opt.signature.LogStringsMessages;
041: import com.sun.xml.wss.XWSSecurityException;
042: import com.sun.xml.wss.impl.MessageConstants;
043: import com.sun.xml.wss.impl.misc.Base64;
044: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
045: import com.sun.xml.ws.security.opt.impl.outgoing.SecurityHeader;
046:
047: import javax.xml.crypto.dsig.SignedInfo;
048: import javax.xml.crypto.dsig.XMLSignature;
049: import java.security.Key;
050: import java.util.List;
051: import java.util.ArrayList;
052:
053: /**
054: *
055: * @author Ashutosh.Shahi@sun.com
056: */
057:
058: public class SignatureProcessor {
059:
060: private static final Logger logger = Logger.getLogger(
061: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN,
062: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN_BUNDLE);
063:
064: /** Creates a new instance of SignatureProcessor */
065: public SignatureProcessor() {
066: }
067:
068: /**
069: *
070: *
071: * @param context JAXBFilterProcessingContext
072: * @return errorCode
073: * @throws XWSSecurityException
074: */
075: public static int sign(JAXBFilterProcessingContext context)
076: throws XWSSecurityException {
077: try {
078: SignaturePolicy signaturePolicy = (SignaturePolicy) context
079: .getSecurityPolicy();
080: ((NamespaceContextEx) context.getNamespaceContext())
081: .addSignatureNS();
082: WSSPolicy keyBinding = (WSSPolicy) signaturePolicy
083: .getKeyBinding();
084: if (logger.isLoggable(Level.FINEST)) {
085: logger.log(Level.FINEST, "KeyBinding is " + keyBinding);
086: }
087:
088: Key signingKey = null;
089:
090: SignatureElementFactory signFactory = new SignatureElementFactory();
091:
092: KeyInfo keyInfo = null;
093: SecurityHeader securityHeader = context.getSecurityHeader();
094:
095: //Get the Signing key and KeyInfo from TokenProcessor
096: TokenProcessor tokenProcessor = new TokenProcessor(
097: signaturePolicy, context);
098: BuilderResult builderResult = tokenProcessor.process();
099: signingKey = builderResult.getDataProtectionKey();
100: keyInfo = builderResult.getKeyInfo();
101:
102: SignedInfo signedInfo = signFactory
103: .constructSignedInfo(context);
104: JAXBSignContext signContext = new JAXBSignContext(
105: signingKey);
106: signContext.setURIDereferencer(DSigResolver.getInstance());
107: XMLSignature signature = signFactory.constructSignature(
108: signedInfo, keyInfo, signaturePolicy.getUUID());
109: signContext.put(MessageConstants.WSS_PROCESSING_CONTEXT,
110: context);
111: NamespaceAndPrefixMapper npMapper = new NamespaceAndPrefixMapper(
112: context.getNamespaceContext(), context
113: .getDisableIncPrefix());
114: signContext.put(NamespaceAndPrefixMapper.NS_PREFIX_MAPPER,
115: npMapper);
116: signContext.putNamespacePrefix(MessageConstants.DSIG_NS,
117: MessageConstants.DSIG_PREFIX);
118: signature.sign(signContext);
119:
120: JAXBSignatureHeaderElement jaxBSign = new JAXBSignatureHeaderElement(
121: (com.sun.xml.ws.security.opt.crypto.dsig.Signature) signature,
122: context.getSOAPVersion());
123: securityHeader.add(jaxBSign);
124:
125: //For SignatureConfirmation
126: List scList = (ArrayList) context
127: .getExtraneousProperty("SignatureConfirmation");
128: if (scList != null) {
129: scList.add(Base64.encode(signature.getSignatureValue()
130: .getValue()));
131: }
132: //End SignatureConfirmation specific code
133:
134: } catch (XWSSecurityException xe) {
135: logger.log(Level.SEVERE, LogStringsMessages
136: .WSS_1701_SIGN_FAILED(), xe);
137: throw xe;
138: } catch (Exception ex) {
139: logger.log(Level.SEVERE, LogStringsMessages
140: .WSS_1701_SIGN_FAILED(), ex);
141: throw new XWSSecurityException(ex);
142: }
143: return 0;
144: }
145:
146: }
|