001: /*
002: * $Id: SymmetricKeyBinding.java,v 1.4 2006/10/04 15:43:19 kumarjayanti Exp $
003: */
004:
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.wss.impl.policy.mls;
028:
029: import com.sun.xml.wss.impl.policy.MLSPolicy;
030: import com.sun.xml.wss.impl.MessageConstants;
031: import javax.crypto.SecretKey;
032: import javax.crypto.spec.SecretKeySpec;
033:
034: import com.sun.xml.wss.impl.PolicyTypeUtil;
035:
036: /**
037: * A policy representing a SymmetricKey that can be used as the
038: * KeyBinding for a SignaturePolicy or an EncryptionPolicy.
039: */
040: public class SymmetricKeyBinding extends KeyBindingBase {
041:
042: /*
043: * Feature Binding
044: * Key Binding
045: */
046:
047: String _keyAlgorithm = MessageConstants._EMPTY;
048:
049: String _keyIdentifier = MessageConstants._EMPTY;
050:
051: String _certAlias = MessageConstants._EMPTY;
052:
053: boolean _useReceivedSecret = false;
054:
055: SecretKey _secretKey = null;
056:
057: /**
058: * Default constructor
059: */
060: public SymmetricKeyBinding() {
061: setPolicyIdentifier(PolicyTypeUtil.SYMMETRIC_KEY_TYPE);
062: }
063:
064: /**
065: * @param keyIdentifier identifier for Key
066: * @param keyAlgorithm Key Algorithm
067: */
068: public SymmetricKeyBinding(String keyIdentifier, String keyAlgorithm) {
069: this ();
070:
071: this ._keyIdentifier = keyIdentifier;
072: this ._keyAlgorithm = keyAlgorithm;
073: }
074:
075: /**
076: * set the key identifier for the symmetric key
077: * @param keyIdentifier
078: */
079: public void setKeyIdentifier(String keyIdentifier) {
080: this ._keyIdentifier = keyIdentifier;
081: }
082:
083: /**
084: * @return key identifier for the symmetric key
085: */
086: public String getKeyIdentifier() {
087: return this ._keyIdentifier;
088: }
089:
090: public void setCertAlias(String certAlias) {
091: this ._certAlias = certAlias;
092: }
093:
094: public String getCertAlias() {
095: return this ._certAlias;
096: }
097:
098: public void setUseReceivedSecret(boolean useReceivedSecret) {
099: this ._useReceivedSecret = useReceivedSecret;
100: }
101:
102: public boolean getUseReceivedSecret() {
103: return this ._useReceivedSecret;
104: }
105:
106: /**
107: * set the Key Algorithm of the Symmetric Key
108: * @param keyAlgorithm
109: */
110: public void setKeyAlgorithm(String keyAlgorithm) {
111: this ._keyAlgorithm = keyAlgorithm;
112: }
113:
114: /**
115: * @return keyAlgorithm for the Symmetric Key
116: */
117: public String getKeyAlgorithm() {
118: return this ._keyAlgorithm;
119: }
120:
121: /**
122: * Set the symmetric key
123: * @param secretKey the SecretKey
124: */
125: public void setSecretKey(SecretKey secretKey) {
126: this ._secretKey = secretKey;
127: }
128:
129: /**
130: * @return SecretKey the symmetric key
131: */
132: public SecretKey getSecretKey() {
133: return this ._secretKey;
134: }
135:
136: /**
137: * Create and set the KeyBinding for this WSSPolicy to an X509CertificateBinding
138: * @return a new X509CertificateBinding as a KeyBinding for this WSSPolicy
139: * @see SignaturePolicy
140: * @see EncryptionPolicy
141: * @see AuthenticationTokenPolicy
142: */
143: public MLSPolicy newX509CertificateKeyBinding() {
144: if (isReadOnly()) {
145: throw new RuntimeException(
146: "Can not create X509CertificateKeyBinding : Policy is Readonly");
147: }
148: this ._keyBinding = new AuthenticationTokenPolicy.X509CertificateBinding();
149: return _keyBinding;
150: }
151:
152: /**
153: * @param policy the policy to be compared for equality
154: * @return true if the argument policy is equal to this
155: */
156: public boolean equals(WSSPolicy policy) {
157:
158: boolean assrt = false;
159:
160: try {
161: SymmetricKeyBinding skBinding = (SymmetricKeyBinding) policy;
162:
163: boolean b1 = _keyIdentifier.equals("") ? true
164: : _keyIdentifier.equals(skBinding
165: .getKeyIdentifier());
166:
167: boolean b2 = _keyAlgorithm.equals("") ? true
168: : _keyAlgorithm.equals(skBinding.getKeyAlgorithm());
169:
170: boolean b3 = _certAlias.equals("") ? true : _certAlias
171: .equals(skBinding.getCertAlias());
172:
173: boolean b4 = (_useReceivedSecret == false) ? true
174: : (_useReceivedSecret == skBinding
175: .getUseReceivedSecret());
176: boolean b5 = (this ._keyBinding.equals(policy._keyBinding));
177:
178: assrt = b1 && b2 && b3 && b4 && b5;
179: } catch (Exception e) {
180: }
181:
182: return assrt;
183: }
184:
185: /*
186: * Equality comparision ignoring the Targets
187: * @param policy the policy to be compared for equality
188: * @return true if the argument policy is equal to this
189: */
190: public boolean equalsIgnoreTargets(WSSPolicy binding) {
191: return equals(binding);
192: }
193:
194: /**
195: * Clone operator
196: * @return clone of this policy
197: */
198: public Object clone() {
199: SymmetricKeyBinding skBinding = new SymmetricKeyBinding();
200:
201: try {
202: skBinding.setUUID(this .getUUID());
203: skBinding.setKeyIdentifier(_keyIdentifier);
204: skBinding.setKeyAlgorithm(_keyAlgorithm);
205: skBinding.setCertAlias(_certAlias);
206: skBinding.setUseReceivedSecret(_useReceivedSecret);
207:
208: SecretKeySpec ky0 = (SecretKeySpec) _secretKey;
209: if (ky0 != null) {
210: SecretKeySpec key = new SecretKeySpec(ky0.getEncoded(),
211: ky0.getAlgorithm());
212: skBinding.setSecretKey(key);
213: }
214:
215: if (this ._keyBinding != null) {
216: if (this ._keyBinding instanceof AuthenticationTokenPolicy.X509CertificateBinding) {
217: skBinding
218: .setKeyBinding((AuthenticationTokenPolicy.X509CertificateBinding) ((AuthenticationTokenPolicy.X509CertificateBinding) this ._keyBinding)
219: .clone());
220: }
221: }
222:
223: } catch (Exception e) {
224: // log
225: }
226:
227: return skBinding;
228: }
229:
230: /**
231: * @return the type of the policy
232: */
233: public String getType() {
234: return PolicyTypeUtil.SYMMETRIC_KEY_TYPE;
235: }
236:
237: public String toString() {
238: return PolicyTypeUtil.SYMMETRIC_KEY_TYPE + "::"
239: + getKeyAlgorithm() + "::" + _keyIdentifier;
240: }
241: }
|