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.wss.core;
024:
025: import com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
026: import com.sun.org.apache.xml.internal.security.encryption.XMLCipher;
027: import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
028: import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
029: import com.sun.xml.ws.security.Token;
030: import com.sun.xml.wss.XWSSecurityException;
031: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
032: import java.security.Key;
033: import javax.crypto.SecretKey;
034: import javax.xml.soap.SOAPElement;
035:
036: import com.sun.xml.wss.impl.MessageConstants;
037: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.NodeList;
040: import java.util.Iterator;
041: import javax.xml.namespace.QName;
042:
043: /**
044: *
045: * @author root
046: */
047:
048: public class EncryptedKeyToken extends SecurityHeaderBlockImpl
049: implements SecurityToken, Token {
050:
051: EncryptedKey encryptedKey = null;
052: SOAPElement elem = null;
053:
054: /** Creates a new instance of EncryptedKeyToken */
055: public EncryptedKeyToken(SOAPElement elem) {
056: this .elem = elem;
057: }
058:
059: public Key getSecretKey(Key privKey, String dataEncAlgo)
060: throws XWSSecurityException {
061: try {
062: XMLCipher xmlc = null;
063: String algorithm = null;
064: if (elem != null) {
065: NodeList nl = elem.getElementsByTagNameNS(
066: MessageConstants.XENC_NS, "EncryptionMethod");
067: if (nl != null)
068: algorithm = ((Element) nl.item(0))
069: .getAttribute("Algorithm");
070: xmlc = XMLCipher.getInstance(algorithm);
071: if (encryptedKey == null)
072: encryptedKey = xmlc.loadEncryptedKey(elem);
073: }
074: if (xmlc == null) {
075: throw new XWSSecurityException(
076: "XMLCipher is null while getting SecretKey from EncryptedKey");
077: }
078: xmlc.init(XMLCipher.UNWRAP_MODE, privKey);
079: SecretKey symmetricKey = (SecretKey) xmlc.decryptKey(
080: encryptedKey, dataEncAlgo);
081: return symmetricKey;
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: throw new XWSSecurityException(
085: "Error while getting SecretKey from EncryptedKey");
086: }
087: }
088:
089: public SOAPElement getAsSoapElement() {
090: //throw new UnsupportedOperationException("Not supported");
091: if (elem != null)
092: return elem;
093: else
094: throw new UnsupportedOperationException("Not supported");
095: }
096:
097: public String getId() {
098: try {
099: return elem.getAttribute("Id");
100: } catch (Exception ex) {
101: throw new RuntimeException("Error while extracting ID");
102: }
103: }
104:
105: public KeyInfoHeaderBlock getKeyInfo() {
106: try {
107: if (encryptedKey != null) {
108: return new KeyInfoHeaderBlock(encryptedKey.getKeyInfo());
109: } else {
110: Iterator iter = elem.getChildElements(new QName(
111: MessageConstants.DSIG_NS, "KeyInfo"));
112: Element keyInfoElem = null;
113: if (iter.hasNext()) {
114: keyInfoElem = (Element) iter.next();
115: }
116: KeyInfo keyInfo = new KeyInfo(keyInfoElem,
117: "MessageConstants.DSIG_NS");
118: return new KeyInfoHeaderBlock(keyInfo);
119: }
120: } catch (XWSSecurityException ex) {
121: throw new XWSSecurityRuntimeException(
122: "Error while extracting KeyInfo", ex);
123: } catch (XMLSecurityException ex) {
124: throw new XWSSecurityRuntimeException(
125: "Error while extracting KeyInfo", ex);
126: }
127: }
128:
129: public String getType() {
130: return MessageConstants.XENC_ENCRYPTED_KEY_QNAME;
131: }
132:
133: public Object getTokenValue() {
134: return this;
135: }
136: }
|