001: /*
002: * $Id: PrivateKeyBinding.java,v 1.4 2007/08/01 11:05:54 ashutoshshahi 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.MessageConstants;
030: import java.security.PrivateKey;
031: import java.security.KeyFactory;
032:
033: import com.sun.xml.wss.impl.PolicyTypeUtil;
034:
035: /**
036: * Objects of this class act as KeyBindings for AuthenticationTokens such
037: * as AuthenticationTokenPolicy.X509CertificateBinding and
038: * AuthenticationTokenPolicy.SAMLAssertionBinding. When associated with an
039: * AuthenticationToken they represent the PrivateKey associated with the
040: * AuthenticationToken.
041: */
042: public class PrivateKeyBinding extends WSSPolicy {
043:
044: /*
045: * Feature Bindings
046: * Key Bindings
047: */
048:
049: /* this keyalgorithm is not used by our impl */
050: String _keyAlgorithm = MessageConstants._EMPTY;
051: String _keyIdentifier = MessageConstants._EMPTY;
052:
053: PrivateKey _privateKey = null;
054:
055: /**
056: * Default constructor
057: */
058: public PrivateKeyBinding() {
059: setPolicyIdentifier(PolicyTypeUtil.PRIVATEKEY_BINDING_TYPE);
060: }
061:
062: /**
063: * Constructor
064: * @param keyIdentifier identifier for the Private Key
065: * @param keyAlgorithm identified for the Key Algorithm
066: */
067: public PrivateKeyBinding(String keyIdentifier, String keyAlgorithm) {
068: this ();
069:
070: this ._keyIdentifier = keyIdentifier;
071: this ._keyAlgorithm = keyAlgorithm;
072: }
073:
074: /**
075: * set the keyIdentifier for the Private Key
076: * @param keyIdentifier Key Identifier for the Private Key
077: */
078: public void setKeyIdentifier(String keyIdentifier) {
079: this ._keyIdentifier = keyIdentifier;
080: }
081:
082: /**
083: * @return key identifier for the Private Key
084: */
085: public String getKeyIdentifier() {
086: return this ._keyIdentifier;
087: }
088:
089: /**
090: * set the KeyAlgorithm of this Private Key.
091: *
092: * Implementation Note: This KeyAlgorithm is not used by XWS-Runtime,
093: * refer setKeyAlgorithm on X509CertificateBinding, SAMLAssertionBinding,
094: * and SymmetricKeyBinding instead.
095: * @param keyAlgorithm KeyAlgorithm of this Private Key
096: */
097: public void setKeyAlgorithm(String keyAlgorithm) {
098: this ._keyAlgorithm = keyAlgorithm;
099: }
100:
101: /**
102: * @return KeyAlgorithm of this Private Key
103: */
104: public String getKeyAlgorithm() {
105: return this ._keyAlgorithm;
106: }
107:
108: /**
109: * set the private key instance
110: * @param privateKey PrivateKey for this PrivateKeyBinding
111: */
112: public void setPrivateKey(PrivateKey privateKey) {
113: this ._privateKey = privateKey;
114: }
115:
116: /**
117: * @return PrivateKey associated with this PrivateKeyBinding
118: */
119: public PrivateKey getPrivateKey() {
120: return this ._privateKey;
121: }
122:
123: /**
124: * equality operator
125: * @param binding the Policy to be checked for equality
126: * @return true if the argument binding is equal to this PrivateKeyBinding.
127: */
128: public boolean equals(WSSPolicy binding) {
129:
130: try {
131: if (!PolicyTypeUtil.privateKeyBinding(binding))
132: return false;
133:
134: PrivateKeyBinding policy = (PrivateKeyBinding) binding;
135:
136: boolean b1 = _keyIdentifier.equals("") ? true
137: : _keyIdentifier.equals(policy.getKeyIdentifier());
138: if (!b1)
139: return false;
140: boolean b2 = _keyAlgorithm.equals("") ? true
141: : _keyAlgorithm.equals(policy.getKeyAlgorithm());
142: if (!b2)
143: return false;
144: } catch (Exception e) {
145: }
146:
147: return true;
148: }
149:
150: /*
151: * equality operator ignoring Target bindings
152: */
153: public boolean equalsIgnoreTargets(WSSPolicy binding) {
154: return equals(binding);
155: }
156:
157: /**
158: * clone operator
159: * @return a clone of this PrivateKeyBinding
160: */
161: public Object clone() {
162: PrivateKeyBinding pkBinding = new PrivateKeyBinding();
163:
164: try {
165: pkBinding.setKeyAlgorithm(_keyAlgorithm);
166: pkBinding.setKeyIdentifier(_keyIdentifier);
167:
168: KeyFactory factory = KeyFactory.getInstance(_privateKey
169: .getAlgorithm());
170: pkBinding.setPrivateKey((PrivateKey) factory
171: .translateKey(_privateKey));
172: } catch (Exception e) {
173: throw new RuntimeException(e);
174: }
175:
176: return pkBinding;
177: }
178:
179: /**
180: * @return the type of the policy
181: */
182: public String getType() {
183: return PolicyTypeUtil.PRIVATEKEY_BINDING_TYPE;
184: }
185:
186: public String toString() {
187: return PolicyTypeUtil.PRIVATEKEY_BINDING_TYPE + "::"
188: + getKeyAlgorithm() + "::" + _keyIdentifier;
189: }
190: }
|