001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.security.impl.policyconv;
038:
039: import com.sun.xml.wss.impl.PolicyTypeUtil;
040: import com.sun.xml.wss.impl.policy.mls.SignaturePolicy;
041: import com.sun.xml.wss.impl.policy.mls.Target;
042: import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
043: import javax.xml.crypto.dsig.CanonicalizationMethod;
044: import javax.xml.namespace.QName;
045: import com.sun.xml.ws.security.policy.EncryptedParts;
046: import com.sun.xml.ws.security.policy.SignedParts;
047: import com.sun.xml.ws.security.policy.Token;
048: import com.sun.xml.wss.impl.MessageConstants;
049: import com.sun.xml.ws.security.impl.policy.Constants;
050: import com.sun.xml.ws.security.policy.AlgorithmSuite;
051:
052: /**
053: *
054: * @author K.Venugopal@sun.com
055: */
056: public class SecurityPolicyUtil {
057:
058: private static final QName signaturePolicy = new QName(
059: MessageConstants.DSIG_NS, MessageConstants.SIGNATURE_LNAME);
060: private static final QName usernameTokenPolicy = new QName(
061: MessageConstants.WSSE_NS,
062: MessageConstants.USERNAME_TOKEN_LNAME);
063: private static final QName x509TokenPolicy = new QName(
064: MessageConstants.WSSE_NS, "BinarySecurityToken");
065: private static final QName timestampPolicy = new QName(
066: MessageConstants.WSU_NS, MessageConstants.TIMESTAMP_LNAME);
067:
068: /** Creates a new instance of SecurityPolicyUtil */
069: public SecurityPolicyUtil() {
070: }
071:
072: public static boolean isSignedPartsEmpty(SignedParts sp) {
073: if (!sp.hasBody()) {
074: if (!sp.getHeaders().hasNext()) {
075: return true;
076: }
077: }
078: return false;
079: }
080:
081: public static boolean isEncryptedPartsEmpty(EncryptedParts ep) {
082: if (!ep.hasBody()) {
083: if (!ep.getTargets().hasNext()) {
084: return true;
085: }
086: }
087: return false;
088: }
089:
090: public static String convertToXWSSConstants(String type) {
091: if (type.contains(Token.REQUIRE_THUMBPRINT_REFERENCE)) {
092: return MessageConstants.THUMB_PRINT_TYPE;
093: } else if (type
094: .contains(Token.REQUIRE_KEY_IDENTIFIER_REFERENCE)) {
095: return MessageConstants.KEY_INDETIFIER_TYPE;
096: } else if (type.contains(Token.REQUIRE_ISSUER_SERIAL_REFERENCE)) {
097: return MessageConstants.X509_ISSUER_TYPE;
098: }
099: throw new UnsupportedOperationException(type
100: + " is not supported");
101: }
102:
103: public static void setName(Target target, WSSPolicy policy) {
104: if (target.getType() == Target.TARGET_TYPE_VALUE_URI) {
105: target.setPolicyName(getQNameFromPolicy(policy));
106: }
107: }
108:
109: private static QName getQNameFromPolicy(WSSPolicy policy) {
110: if (PolicyTypeUtil.signaturePolicy(policy)) {
111: return signaturePolicy;
112: } else if (PolicyTypeUtil.timestampPolicy(policy)) {
113: return timestampPolicy;
114: } else if (PolicyTypeUtil.x509CertificateBinding(policy)) {
115: return x509TokenPolicy;
116: } else if (PolicyTypeUtil.usernameTokenPolicy(policy)) {
117: return usernameTokenPolicy;
118: } else if (PolicyTypeUtil
119: .secureConversationTokenKeyBinding(policy)) {
120: return MessageConstants.SCT_NAME;
121: }
122: return null;
123: }
124:
125: public static void setCanonicalizationMethod(
126: SignaturePolicy.FeatureBinding spFB,
127: AlgorithmSuite algorithmSuite) {
128: if (algorithmSuite != null
129: && algorithmSuite.getAdditionalProps().contains(
130: Constants.InclusiveC14N)) {
131: spFB
132: .setCanonicalizationAlgorithm(CanonicalizationMethod.INCLUSIVE);
133: } else {
134: spFB
135: .setCanonicalizationAlgorithm(CanonicalizationMethod.EXCLUSIVE);
136: }
137:
138: if (algorithmSuite != null
139: && algorithmSuite.getAdditionalProps().contains(
140: Constants.InclusiveC14NWithCommentsForCm)) {
141: spFB
142: .setCanonicalizationAlgorithm(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS);
143: } else if (algorithmSuite != null
144: && algorithmSuite.getAdditionalProps().contains(
145: Constants.ExclusiveC14NWithCommentsForCm)) {
146: spFB
147: .setCanonicalizationAlgorithm(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS);
148: }
149: }
150: }
|