You are viewing a plain text version of this content. The canonical link for it is here.
Posted to tsik-dev@ws.apache.org by ha...@apache.org on 2005/08/08 22:03:01 UTC
svn commit: r230871 [14/61] - in /incubator/tsik/trunk: ./ data/
data/schema/ resources/ resources/junit/ src/ src/org/ src/org/apache/
src/org/apache/tsik/ src/org/apache/tsik/c14n/
src/org/apache/tsik/c14n/elements/ src/org/apache/tsik/common/ src/or...
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/DefaultTokenResolver.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/DefaultTokenResolver.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/DefaultTokenResolver.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/DefaultTokenResolver.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,329 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+import java.security.Key;
+import java.security.cert.X509Certificate;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.wss.elements.KeyIdentifier;
+import org.apache.tsik.wss.elements.SecurityTokenReference;
+
+/**
+ * A default implementation of a token resolver.
+ *
+ */
+public class DefaultTokenResolver implements TokenResolver
+{
+ private Logger logger =LoggerFactory.getLogger(DefaultTokenResolver.class);
+
+ private class MappedValue
+ {
+ Key key;
+ X509Certificate cert;
+ byte[] ski;
+
+ Key getKey(){return key;}
+ void setKey(Key key){this.key = key;}
+ boolean hasKey(){ return key != null;}
+
+ X509Certificate getCert(){return cert;}
+ void setCert(X509Certificate cert){this.cert = cert;}
+ boolean hasCert(){ return cert != null;}
+
+ byte[] getSki(){return ski;}
+ void setSki(byte[] ski){this.ski = ski;}
+ boolean hasSki(){ return ski != null;}
+
+ public String toString()
+ {
+ String s = "[MappedValue";
+ if (key != null) {
+ s+= " key="+encDec.encode(key.getEncoded())
+ .substring(0,10);
+ }
+ if (cert != null) {
+ s+= " cert="+cert.getSubjectDN();
+ }
+ if (ski != null) {
+ s+= " ski=" + encDec.encode(ski);
+ }
+ return s+="]";
+ }
+ }
+
+ private Map map;
+ private EncoderDecoder encDec;
+
+ /**
+ * Creates a new DefaultTokenResolver.
+ *
+ */
+ public DefaultTokenResolver()
+ {
+ map = new HashMap();
+ encDec = new Base64EncoderDecoder();
+ }
+
+ private MappedValue getOnString(String s)
+ {
+ MappedValue mv = (MappedValue) map.get(s);
+ if (mv == null) {
+ mv = new MappedValue();
+ }
+ return mv;
+ }
+
+ private MappedValue getOnId(String id)
+ {
+ return getOnString(id);
+ }
+
+ private MappedValue getOnSki(byte[] ski)
+ {
+ return getOnString(encDec.encode(ski));
+ }
+
+ // inherit javadoc
+ public void map(String id, Key key)
+ {
+ if (id == null) {
+ throw new IllegalArgumentException("id cannot be null");
+ }
+ MappedValue mv = getOnId(id);
+ mv.setKey(key);
+ map.put(id, mv);
+ logger.debug("Mapping id "+id+" to "+mv);
+ }
+
+ // inherit javadoc
+ public void map(String id, byte[] ski)
+ {
+ if (id == null) {
+ throw new IllegalArgumentException("id cannot be null");
+ }
+ MappedValue mv = getOnId(id);
+ mv.setSki(ski);
+ map.put(id, mv);
+ logger.debug("Mapping id "+id+" to "+mv);
+ }
+
+ // inherit javadoc
+ public void map(X509Certificate cert, Key key)
+ {
+ if (cert == null) {
+ throw new IllegalArgumentException("cert cannot be null");
+ }
+ byte[] ski = org.apache.tsik.xmlsig.KeyInfo.getSKIExtension(cert);
+ map(ski, cert);
+ map(ski, key);
+ }
+
+ // inherit javadoc
+ public void map(byte[] ski, X509Certificate cert)
+ {
+ if (ski == null) {
+ throw new IllegalArgumentException("ski cannot be null");
+ }
+ MappedValue mv = getOnSki(ski);
+ mv.setCert(cert);
+ map.put(encDec.encode(ski), mv);
+ logger.debug("Mapping ski "+encDec.encode(ski)+" to "+mv);
+ }
+
+ // inherit javadoc
+ public void map(String id, X509Certificate cert)
+ {
+ if (id == null) {
+ throw new IllegalArgumentException("id cannot be null");
+ }
+ MappedValue mv = getOnString(id);
+ mv.setCert(cert);
+ map.put(id, mv);
+ logger.debug("Mapping id "+id+" to "+mv);
+ }
+
+ // inherit javadoc
+ public void map(byte[] ski, Key key)
+ {
+ if (ski == null) {
+ throw new IllegalArgumentException("ski cannot be null");
+ }
+ MappedValue mv = getOnSki(ski);
+ mv.setKey(key);
+ map.put(encDec.encode(ski), mv);
+ logger.debug("Mapping ski "+encDec.encode(ski)+" to "+mv);
+ }
+
+ // inherit javadoc
+ public Key resolveKey(String id)
+ {
+ logger.debug("Resolving key on #"+id);
+ MappedValue mv = getOnString("#"+id);
+ if (mv != null){
+ return mv.getKey();
+ }
+ return null;
+ }
+
+ // inherit javadoc
+ public Key resolveKey(byte[] ski)
+ {
+ if (ski == null) {
+ throw new IllegalArgumentException("ski cannot be null");
+ }
+ MappedValue mv = getOnSki(ski);
+ if (mv != null){
+ return mv.getKey();
+ }
+ return null;
+ }
+
+ // inherit javadoc
+ public X509Certificate resolveCert(String id)
+ {
+ MappedValue mv = getOnString("#"+id);
+ if (mv != null){
+ return mv.getCert();
+ }
+ return null;
+ }
+
+ // inherit javadoc
+ public X509Certificate resolveCert(byte[] ski)
+ {
+ if (ski == null) {
+ throw new IllegalArgumentException("ski cannot be null");
+ }
+ MappedValue mv = getOnSki(ski);
+ if (mv != null){
+ return mv.getCert();
+ }
+ return null;
+ }
+
+ private MappedValue getOnKeyIdentifier(KeyIdentifier ki)
+ {
+ if (ki == null) {
+ return null;
+ }
+
+ if (ki.hasSki()){
+ return getOnSki(ki.getSki());
+ }
+
+ if (ki.hasCertificate()){
+ X509Certificate cert = ki.getCertificate();
+ byte[] ski = org.apache.tsik.xmlsig.KeyInfo.getSKIExtension(cert);
+ return getOnSki(ski);
+ }
+ return null;
+ }
+
+
+ private X509Certificate resolveCertOnKeyIdentifier(KeyIdentifier ki)
+ {
+ MappedValue mv = getOnKeyIdentifier(ki);
+ if (mv != null){
+ return mv.getCert();
+ }
+ return null;
+ }
+
+ private X509Certificate resolveCertOnReference(String ref)
+ {
+ MappedValue mv = getOnString(ref);
+ if (mv != null){
+ if (mv.hasCert()){
+ return mv.getCert();
+ } else if (mv.hasSki()){
+ return resolveCert(mv.getSki());
+ }
+ }
+ return null;
+ }
+
+ // inherit javadoc
+ public X509Certificate resolveCert(SecurityTokenReference str)
+ {
+ logger.debug("Resolving "+str);
+
+ if (str.hasKeyIdentifier()){
+ return resolveCertOnKeyIdentifier(str.getKeyIdentifier());
+ }
+
+ if (str.hasReference()){
+ return resolveCertOnReference(str.getReference());
+ }
+ return null;
+ }
+
+
+ private Key resolveKeyOnKeyIdentifier(KeyIdentifier ki)
+ {
+ MappedValue mv = getOnKeyIdentifier(ki);
+ if (mv != null){
+ return mv.getKey();
+ }
+ return null;
+ }
+
+ private Key resolveKeyOnReference(String ref)
+ {
+ MappedValue mv = getOnString(ref);
+ if (mv != null){
+ if (mv.hasKey()){
+ return mv.getKey();
+ } else if (mv.hasSki()){
+ return resolveKey(mv.getSki());
+ }
+ }
+ return null;
+ }
+
+ // inherit javadoc
+ public Key resolveKey(SecurityTokenReference str)
+ {
+ logger.debug("Resolving "+str);
+
+ if (str.hasKeyIdentifier()){
+ return resolveKeyOnKeyIdentifier(str.getKeyIdentifier());
+ }
+
+ if (str.hasReference()){
+ return resolveKeyOnReference(str.getReference());
+ }
+ return null;
+ }
+
+ public String toString()
+ {
+ String s = "map=[";
+ s += map.toString();
+// for (Iterator iter = map.keySet().iterator; iter.hasNext();
+// Object o = iter.next()){
+// s += o +" ";
+// }
+
+ return s +"]";
+ }
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/DefaultTokenResolver.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/EncoderDecoder.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/EncoderDecoder.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/EncoderDecoder.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/EncoderDecoder.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,50 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+/**
+ * Encodes and decodes data.
+ */
+public interface EncoderDecoder
+{
+ /**
+ * Decodes the string into bytes.
+ *
+ * @param s the string to decode.
+ * @return the decoded value.
+ */
+ public byte[] decode(String s);
+
+ /**
+ * Encodes the bytes into a string.
+ *
+ * @param b the bytes to encode.
+ * @return the encoded value.
+ */
+ public String encode(byte[] b);
+
+ /**
+ * Returns the attribute value of this encoding so that it
+ * can be used in WS-Security processing.
+ *
+ * @return the attribute value.
+ */
+ public String getAttributeValue();
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/EncoderDecoder.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/Id.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/Id.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/Id.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/Id.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,97 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.uuid.UUID;
+
+/**
+ * The wsu:Id element.
+ *
+ */
+public class Id
+{
+ private static Class c = Id.class;
+ private static Logger log = LoggerFactory.getLogger(c);
+
+ /** TODO javadoc. */
+ public static String attributeName = SchemaMapper.getName(c);
+ /** TODO javadoc. */
+ public static String prefix = SchemaMapper.getPrefix(c);
+ /** TODO javadoc. */
+ public static String uri = SchemaMapper.getUri(c);
+
+ private Id() {}
+
+ /**
+ * Generates a UUID prepended with "wsse-".
+ *
+ * @return the UUID.
+ */
+ public static String generateId()
+ {
+ return "wsse-" + UUID.generate();
+ }
+
+ /**
+ * Inserts the id as an wsu:Id into the cursor.
+ *
+ * @param wc the cursor will get the Id inserted at its current position.
+ * @param id the id to insert.
+ */
+ public static void insertInto(DOMWriteCursor wc, String id)
+ {
+ wc.setAttribute(uri, prefix, attributeName, id);
+ }
+
+ /**
+ * Gets the ID or wsu:Id attribute.
+ *
+ * @param c the cursor positioned at the element.
+ * @return the ID or null if no ID can be found.
+ * @throws IllegalArgumentException if there are two IDs specified.
+ */
+ public static String getId(DOMCursor c) throws IllegalArgumentException
+ {
+ String id = c.getAttribute("Id");
+ String id2 = null;
+ try {
+ id2 = c.getAttribute(uri, "Id");
+ } catch (NullPointerException e) {
+ log.debug(e);
+ // TODO fix DOMOperations to handle this? (XML parser is throwing
+ // NPE when attr with namespace is missing)
+ }
+ if (id != null && id2 != null) {
+ throw new IllegalArgumentException("two IDs not supported");
+ }
+ if (id != null) {
+ return id;
+ }
+ if (id2 != null) {
+ return id2;
+ }
+ return null;
+ }
+
+}
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/Id.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/TokenResolver.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/TokenResolver.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/TokenResolver.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/TokenResolver.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,136 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+import org.apache.tsik.wss.elements.SecurityTokenReference;
+
+import java.security.Key;
+import java.security.cert.X509Certificate;
+
+/**
+ * A token resolver maps identifiers to value. Identifiers include
+ * IDs (ws:Id), certificates (and their SKI extensions). Values include
+ * certificates (and SKIs) and keys.
+ *
+ * <p>A token resolver is used during WSS processing to find the needed
+ * keying information.</p>
+ */
+public interface TokenResolver
+{
+ /**
+ * Maps a ski to a cert.
+ *
+ * @param ski the ski to map.
+ * @param cert the cert to map ski to.
+ */
+ public void map(byte[] ski, X509Certificate cert);
+
+ /**
+ * Maps a ski to a key.
+ *
+ * @param ski the map key.
+ * @param key the mapped value.
+ */
+ public void map(byte[] ski, Key key);
+
+ /**
+ * Maps an id to a key.
+ *
+ * @param id the id to map.
+ * @param key the key to map id to.
+ */
+ public void map(String id, Key key);
+
+ /**
+ * Maps an id to a subject key identifier.
+ *
+ * @param id the id to map.
+ * @param ski the ski to map id to.
+ */
+ public void map(String id, byte[] ski);
+
+ /**
+ * Maps an id to a cert.
+ *
+ * @param id the key.
+ * @param cert the value.
+ */
+ public void map(String id, X509Certificate cert);
+
+ /**
+ * Maps a cert to a key. Also maps the cert's ski, should it exist, to
+ * the same key.
+ *
+ * @param cert the cert to map.
+ * @param key the key to map cert to.
+ */
+ public void map(X509Certificate cert, Key key);
+
+ /**
+ * Resolves the cert based on a security token reference, based on
+ * key identifier or reference.
+ *
+ * @param str the SecurityTokenReference to resolve.
+ * @return the cert found, or null.
+ */
+ public X509Certificate resolveCert(SecurityTokenReference str);
+
+ /**
+ * Resolves a cert based on an id.
+ *
+ * @param id the id to look for.
+ * @return the cert found, or null.
+ */
+ public X509Certificate resolveCert(String id);
+
+ /**
+ * Resolves a cert based in a ski.
+ *
+ * @param ski the ski.
+ * @return the resolved cert, or null.
+ */
+ public X509Certificate resolveCert(byte[] ski);
+
+ /**
+ * Resolves a key based on a security token reference, based on
+ * key identifier or reference.
+ *
+ * @param str the SecurityTokenReference to resolve.
+ * @return the key found, or null.
+ */
+ public Key resolveKey(SecurityTokenReference str);
+
+ /**
+ * Resolves the key based on the id.
+ *
+ * @param id the id to look for.
+ * @return the key found, or null.
+ */
+ public Key resolveKey(String id);
+
+ /**
+ * Resolves the key based on the ski.
+ *
+ * @param ski the ski to look for.
+ * @return the key found, or null.
+ */
+ public Key resolveKey(byte[] ski);
+
+ public String toString();
+}
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/TokenResolver.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/Wss.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/Wss.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/Wss.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/Wss.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,382 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.SignatureException;
+import java.security.cert.CertificateException;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.domutil.elements.ElementImpl;
+import org.apache.tsik.messaging.SoapFault;
+import org.apache.tsik.messaging.SoapMessage;
+import org.apache.tsik.wss.Id;
+import org.apache.tsik.wss.TokenResolver;
+import org.apache.tsik.wss.elements.BinarySecurityToken;
+import org.apache.tsik.wss.elements.EncryptedData;
+import org.apache.tsik.wss.elements.EncryptedKey;
+import org.apache.tsik.wss.elements.KeyInfo;
+import org.apache.tsik.wss.elements.Security;
+import org.apache.tsik.wss.elements.SecurityTokenReference;
+import org.apache.tsik.wss.elements.Signature;
+import org.apache.tsik.wss.elements.SoapHeaderWrapper;
+import org.apache.tsik.xmlsig.Signer;
+import org.apache.tsik.xpath.XPath;
+import org.apache.tsik.xpath.XPathException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Implements WS-Security.
+ *
+ * <p>To create a WS-Security message, the processing model is to
+ * <ol>
+ * <li>create the WS-Security message,</li>
+ * <li>create the wsse:Security header(s),</li>
+ * <li>create and add sub-elements to the header(s),</li>
+ * <li>specify if and how to encrypt/sign,</li>
+ * <li>perform encryption and signing, and</li>
+ * <li>create a SOAP message.</li>
+ * </ol>
+ * </p>
+ *
+ * <p>To process and existing WS-Security message, the processing model is to
+ * <ol>
+ * <li>create a TokenResolver,</li>
+ * <li>map the needed keying information in the token resolver,</li>
+ * <li>read the SOAP message,</li>
+ * <li>make sure the expected headers are present,</li>
+ * <li>instruct the Wss processor which (if any) parts of the message
+ * must be signed, and</li>
+ * <li>process the Security header(s) via
+ * {@link Wss#process(Security, TokenResolver)}</li>.
+ * </ol>
+ * </p>
+ *
+ * <p>For more information, see {@link org.apache.tsik.wss}.
+ */
+public class Wss
+{
+ private Logger log = LoggerFactory.getLogger(Wss.class);
+
+ private List headers;
+ private SoapMessage soap;
+
+ /**
+ * Creates the Wss object. The SOAP message will be scanned for
+ * Security elements.
+ *
+ * @param soap the SOAP message to scan.
+ */
+ public Wss(SoapMessage soap)
+ {
+ this.soap = soap;
+ headers = new ArrayList();
+
+ // Create a list of the parsed Security elements and keep them
+ // in 'headers'
+ //
+ if (soap != null){
+ Element[] h = soap.getHeaders();
+ for (int i = 0; i < h.length; i += 1){
+ DOMCursor c = new DOMCursor(h[i]);
+ if (c.atElement(Security.uri, Security.elementName)){
+ try {
+ Security s
+ = Security
+ .fromXmlMustUnderstand(soap
+ .getMustUnderstand(h[i]),
+ new DOMCursor(h[i]));
+ headers.add(s);
+ } catch (Exception e){
+ e.printStackTrace();
+ throw new IllegalArgumentException(e.toString());
+ }
+ } else {
+ SoapHeaderWrapper shw = new SoapHeaderWrapper(h[i]);
+ headers.add(shw);
+ }
+ }
+ }
+ }
+
+ /**
+ * Adds a WS-Security header.
+ *
+ * @param security the WS-Security header to add.
+ */
+ public void addSecurity(Security security)
+ {
+ headers.add(security);
+ }
+
+ /**
+ * Returns the one and only Security header.
+ *
+ * @return the one and only Security header.
+ */
+ public Security getSecurity()
+ {
+ return this.getSecurity(0);
+ }
+
+ /**
+ * Returns the n:th Security header.
+ *
+ * @param n the ordinal of Security header to return.
+ *
+ * @return the security header, or null if the n:th header doesn't
+ * exist.
+ */
+ public Security getSecurity(int n)
+ {
+ if (headers == null) {
+ return null;
+ }
+ int size = headers.size();
+ for (int i = 0; i < size; i += 1){
+ ElementImpl e = (ElementImpl) headers.get(i);
+ if ((i == n) && (e instanceof Security)){
+ return (Security) e;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the number of security headers.
+ *
+ * @return the number of security headers.
+ */
+ public int numberOfSecurityHeaders()
+ {
+ if (headers == null) {
+ return 0;
+ }
+ int size = headers.size();
+ int num = 0;
+ for (int i = 0; i < size; i += 1){
+ ElementImpl e = (ElementImpl) headers.get(i);
+ if (e instanceof Security){
+ num += 1;
+ }
+ }
+ return num;
+ }
+
+ /**
+ * Iterates through the current object and returns it as a
+ * SOAP message.
+ *
+ * @return the SOAP message.
+ */
+ public SoapMessage toSoapMessage()
+ {
+ ElementImpl[] h = new ElementImpl[headers.size()];
+ headers.toArray(h);
+
+ Element[] elems = new Element[h.length];
+ for (int i = 0; i < h.length; i += 1){
+ DOMWriteCursor wc = new DOMWriteCursor();
+ h[i].toXml(wc);
+ elems[i] = wc.getElement();
+ }
+
+ for (int i = 0; i < h.length; i += 1){
+ if ((h[i] instanceof Security)
+ && ((Security)h[i]).getMustUnderstand()){
+ soap.setMustUnderstand(elems[i], true);
+ }
+ }
+ soap.setHeaders(elems);
+ return soap;
+ }
+
+ public String toString()
+ {
+ String s = "[Wss ";
+
+ int len = headers.size();
+ for (int i = 0; i < len; i += 1){
+ s += "{header "+i+":"+ headers.get(i)+"}";
+ }
+ return s + "]";
+ }
+
+ /**
+ * Process the Wss message.
+ *
+ * @param security the security header
+ * @param resolver the token resolver to use
+ *
+ * @return a soapfault if an error happened during processing, otherwise
+ * null is returned (that is, null == good).
+ */
+ public SoapFault process(Security security, TokenResolver resolver)
+ {
+ // This method is put in wss and not in security, in case the SOAP
+ // body needs, e.g., to be decrypted
+ //
+ try {
+ security.process(soap, resolver);
+ return null;
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter printWriter = new PrintWriter(stringWriter);
+ e.printStackTrace(printWriter);
+ return new SoapFault(SoapFault.SERVER,
+ stringWriter.toString(), "actor",null);
+ }
+ }
+
+ /**
+ * Signs the SOAP body.
+ *
+ * @param security the security header.
+ * @param bstId the security token whose reference will be inserted via
+ * param str. The bstId must refer to a binary security token.
+ * @param str is the SecurityTokenReference that will be inserted into
+ * the /ds:Signature/ds:KeyInfo
+ * @param key the signing key
+ * @param toSign an optional array of elements to be signed
+ * @param toSignId an optional array of the ID's of elements to be
+ * signed. This parameter is nexessary if toSign is present.
+ * @return the Signature element.
+ *
+ * @throws CertificateException TODO.
+ * @throws InvalidKeyException TODO.
+ * @throws NoSuchAlgorithmException TODO.
+ * @throws SignatureException TODO.
+ * @throws XPathException TODO.
+ */
+ public Signature signBody(Security security, String bstId,
+ SecurityTokenReference str, PrivateKey key,
+ ElementImpl[] toSign, String[] toSignId)
+ throws InvalidKeyException, SignatureException,
+ NoSuchAlgorithmException, CertificateException,
+ XPathException
+ {
+ ElementImpl ei = security.getElement(bstId);
+ if (!(ei instanceof BinarySecurityToken)){
+ throw new
+ IllegalArgumentException(bstId + " is not a BinarySecurityToken");
+ }
+
+ // TODO: Generalize signBody() into a sign() method where
+ // body is optional.
+ //
+
+ Element elem = soap.getBody();
+ DOMWriteCursor wc = new DOMWriteCursor(elem);
+ wc.moveToParent();
+
+ String id = wc.getAttribute("Id");
+ if (id == null){
+ id = Id.getId(wc);
+ }
+ if (id == null){
+ id = Id.generateId();
+ Id.insertInto(wc, id);
+ }
+
+ // create a copy of the entire soap message
+ // TODO: avoid this duplication
+ //
+ Document doc = soap.getDocument();
+ DOMWriteCursor origSoapCursor = new DOMWriteCursor(doc);
+ DOMWriteCursor temp = new DOMWriteCursor();
+ temp.copyUnder(origSoapCursor);
+
+ if (toSign != null){
+ for (int i = 0; i < toSign.length; i += 1){
+ DOMWriteCursor c = new DOMWriteCursor();
+ toSign[i].toXml(c);
+ Id.insertInto(c, toSignId[i]);
+ temp.copyUnder(c);
+ }
+ }
+
+ // create a soap envelope
+ //
+ SoapMessage soapCopy = new SoapMessage(temp.getDocument());
+ doc = soapCopy.getDocument();
+
+ org.apache.tsik.xmlsig.KeyInfo ki
+ = new org.apache.tsik.xmlsig.KeyInfo();
+ Signer signer = new Signer(doc, key, ki);
+ signer.useExclusiveCanonicalizer(Collections.EMPTY_LIST);
+ signer.addReference(XPath.fromID(id));
+ if (toSign != null){
+ for (int i = 0; i < toSign.length; i += 1){
+ signer.addReference(XPath.fromID(toSignId[i]));
+ }
+ }
+ doc = signer.sign(new XPath("/"));
+
+ DOMCursor c = new DOMCursor(doc);
+
+ Signature sig = Signature.fromXml(c);
+ KeyInfo keyInfo = new KeyInfo(str);
+ sig.setKeyInfo(keyInfo);
+ return sig;
+ }
+
+ /**
+ * Encrypts the body using the DEK of the encrypted key. The
+ * resulting EncryptedData will have a wsu:Id of encId
+ *
+ * @param security the security header currently in use
+ * @param ek the data encryption key of this key will be used to
+ * encrypt.
+ * @param encId the resulting Id of the resulting EncryptedData.
+ * @throws NoSuchAlgorithmException TODO.
+ */
+ public void encryptBody(Security security, EncryptedKey ek, String encId)
+ throws NoSuchAlgorithmException
+ {
+ Element elem = soap.getBody();
+ DOMWriteCursor c = new DOMWriteCursor(elem);
+ c.moveToParent();
+ DOMWriteCursor wc = new DOMWriteCursor();
+ wc.copyUnder(c);
+
+ EncryptedData ed = security.encryptData(ek, encId, wc, true);
+ wc = new DOMWriteCursor();
+ ed.toXml(wc);
+
+ elem = soap.getBody();
+ c = new DOMWriteCursor(elem);
+ c.copyOver(wc);
+ }
+
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/Wss.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/WssException.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/WssException.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/WssException.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/WssException.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,56 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss;
+
+import org.apache.tsik.messaging.SoapFault;
+
+/**
+ * Signals an error in the WS-Security processing.
+ */
+public class WssException extends Exception
+{
+ private SoapFault soapFault;
+
+ /**
+ * Creates a WssException.
+ *
+ * @param soapFault the SOAP fault to wrap.
+ */
+ public WssException(SoapFault soapFault)
+ {
+ super();
+ this.soapFault = soapFault;
+ }
+
+ /**
+ * Returns the SOAP fault.
+ *
+ * @return the soap faul.
+ */
+ public SoapFault getSoapFault()
+ {
+ return soapFault;
+ }
+
+ public String toString()
+ {
+ return soapFault.getFaultString();
+ }
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/WssException.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-secext-1.0.xsd
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-secext-1.0.xsd?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-secext-1.0.xsd (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-secext-1.0.xsd Mon Aug 8 12:58:58 2005
@@ -0,0 +1,195 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+OASIS takes no position regarding the validity or scope of any intellectual property or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; neither does it represent that it has made any effort to identify any such rights. Information on OASIS's procedures with respect to rights in OASIS specifications can be found at the OASIS website. Copies of claims of rights made available for publication and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementors or users of this specification, can be obtained from the OASIS Executive Director.
+OASIS invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights which may cover technology that may be required to implement this specification. Please address the information to the OASIS Executive Director.
+Copyright © OASIS Open 2002-2004. All Rights Reserved.
+This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself does not be modified in any way, such as by removing the copyright notice or references to OASIS, except as needed for the purpose of developing OASIS specifications, in which case the procedures for copyrights defined in the OASIS Intellectual Property Rights document must be followed, or as required to translate it into languages other than English.
+The limited permissions granted above are perpetual and will not be revoked by OASIS or its successors or assigns.
+This document and the information contained herein is provided on an âAS ISâ basis and OASIS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+-->
+<xsd:schema targetNamespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" elementFormDefault="qualified" attributeFormDefault="unqualified" blockDefault="#all" version="0.2">
+ <xsd:import namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" schemaLocation="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"/>
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>
+ <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd"/>
+ <xsd:complexType name="AttributedString">
+ <xsd:annotation>
+ <xsd:documentation>This type represents an element with arbitrary attributes.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attribute ref="wsu:Id"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:complexType name="PasswordString">
+ <xsd:annotation>
+ <xsd:documentation>This type is used for password elements per Section 4.1.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="wsse:AttributedString">
+ <xsd:attribute name="Type" type="xsd:anyURI"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:complexType name="EncodedString">
+ <xsd:annotation>
+ <xsd:documentation>This type is used for elements containing stringified binary data.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="wsse:AttributedString">
+ <xsd:attribute name="EncodingType" type="xsd:anyURI"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:complexType name="UsernameTokenType">
+ <xsd:annotation>
+ <xsd:documentation>This type represents a username token per Section 4.1</xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:element name="Username" type="wsse:AttributedString"/>
+ <xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute ref="wsu:Id"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:complexType name="BinarySecurityTokenType">
+ <xsd:annotation>
+ <xsd:documentation>A security token that is encoded in binary</xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="wsse:EncodedString">
+ <xsd:attribute name="ValueType" type="xsd:anyURI"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:complexType name="KeyIdentifierType">
+ <xsd:annotation>
+ <xsd:documentation>A security token key identifier</xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="wsse:EncodedString">
+ <xsd:attribute name="ValueType" type="xsd:anyURI"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:simpleType name="tUsage">
+ <xsd:annotation>
+ <xsd:documentation>Typedef to allow a list of usages (as URIs).</xsd:documentation>
+ </xsd:annotation>
+ <xsd:list itemType="xsd:anyURI"/>
+ </xsd:simpleType>
+ <xsd:attribute name="Usage" type="tUsage">
+ <xsd:annotation>
+ <xsd:documentation>This global attribute is used to indicate the usage of a referenced or indicated token within the containing context</xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ <xsd:complexType name="ReferenceType">
+ <xsd:annotation>
+ <xsd:documentation>This type represents a reference to an external security token.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:attribute name="URI" type="xsd:anyURI"/>
+ <xsd:attribute name="ValueType" type="xsd:anyURI"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:complexType name="EmbeddedType">
+ <xsd:annotation>
+ <xsd:documentation>This type represents a reference to an embedded security token.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:choice minOccurs="0" maxOccurs="unbounded">
+ <xsd:any processContents="lax"/>
+ </xsd:choice>
+ <xsd:attribute name="ValueType" type="xsd:anyURI"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:complexType name="SecurityTokenReferenceType">
+ <xsd:annotation>
+ <xsd:documentation>This type is used reference a security token.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:choice minOccurs="0" maxOccurs="unbounded">
+ <xsd:any processContents="lax"/>
+ </xsd:choice>
+ <xsd:attribute ref="wsu:Id"/>
+ <xsd:attribute ref="wsse:Usage"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:complexType name="SecurityHeaderType">
+ <xsd:annotation>
+ <xsd:documentation>This complexType defines header block to use for security-relevant data directed at a specific SOAP actor.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded">
+ <xsd:annotation>
+ <xsd:documentation>The use of "any" is to allow extensibility and different forms of security data.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:any>
+ </xsd:sequence>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:complexType name="TransformationParametersType">
+ <xsd:annotation>
+ <xsd:documentation>This complexType defines a container for elements to be specified from any namespace as properties/parameters of a DSIG transformation.</xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded">
+ <xsd:annotation>
+ <xsd:documentation>The use of "any" is to allow extensibility from any namespace.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:any>
+ </xsd:sequence>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:complexType>
+ <xsd:element name="UsernameToken" type="wsse:UsernameTokenType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines the wsse:UsernameToken element per Section 4.1.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="BinarySecurityToken" type="wsse:BinarySecurityTokenType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines the wsse:BinarySecurityToken element per Section 4.2.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="Reference" type="wsse:ReferenceType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines a security token reference</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="Embedded" type="wsse:EmbeddedType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines a security token embedded reference</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="KeyIdentifier" type="wsse:KeyIdentifierType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines a key identifier reference</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="SecurityTokenReference" type="wsse:SecurityTokenReferenceType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines the wsse:SecurityTokenReference per Section 4.3.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="Security" type="wsse:SecurityHeaderType">
+ <xsd:annotation>
+ <xsd:documentation>This element defines the wsse:Security SOAP header element per Section 4.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="TransformationParameters" type="wsse:TransformationParametersType">
+ <xsd:annotation>
+ <xsd:documentation>This element contains properties for transformations from any namespace, including DSIG.</xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="Password" type="wsse:PasswordString"/>
+ <xsd:element name="Nonce" type="wsse:EncodedString"/>
+ <xsd:simpleType name="FaultcodeEnum">
+ <xsd:restriction base="xsd:QName">
+ <xsd:enumeration value="wsse:UnsupportedSecurityToken"/>
+ <xsd:enumeration value="wsse:UnsupportedAlgorithm"/>
+ <xsd:enumeration value="wsse:InvalidSecurity"/>
+ <xsd:enumeration value="wsse:InvalidSecurityToken"/>
+ <xsd:enumeration value="wsse:FailedAuthentication"/>
+ <xsd:enumeration value="wsse:FailedCheck"/>
+ <xsd:enumeration value="wsse:SecurityTokenUnavailable"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+</xsd:schema>
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-secext-1.0.xsd
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-utility-1.0.xsd
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-utility-1.0.xsd?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-utility-1.0.xsd (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-utility-1.0.xsd Mon Aug 8 12:58:58 2005
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+OASIS takes no position regarding the validity or scope of any intellectual property or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; neither does it represent that it has made any effort to identify any such rights. Information on OASIS's procedures with respect to rights in OASIS specifications can be found at the OASIS website. Copies of claims of rights made available for publication and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementors or users of this specification, can be obtained from the OASIS Executive Director.
+OASIS invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights which may cover technology that may be required to implement this specification. Please address the information to the OASIS Executive Director.
+Copyright © OASIS Open 2002-2004. All Rights Reserved.
+This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself does not be modified in any way, such as by removing the copyright notice or references to OASIS, except as needed for the purpose of developing OASIS specifications, in which case the procedures for copyrights defined in the OASIS Intellectual Property Rights document must be followed, or as required to translate it into languages other than English.
+The limited permissions granted above are perpetual and will not be revoked by OASIS or its successors or assigns.
+This document and the information contained herein is provided on an âAS ISâ basis and OASIS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+-->
+<xsd:schema targetNamespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+
+
+
+xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
+elementFormDefault="qualified" attributeFormDefault="unqualified" version="0.1">
+ <!-- // Fault Codes /////////////////////////////////////////// -->
+ <xsd:simpleType name="tTimestampFault">
+ <xsd:annotation>
+ <xsd:documentation>
+This type defines the fault code value for Timestamp message expiration.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:restriction base="xsd:QName">
+ <xsd:enumeration value="wsu:MessageExpired"/>
+ </xsd:restriction>
+ </xsd:simpleType>
+ <!-- // Global attributes //////////////////////////////////// -->
+ <xsd:attribute name="Id" type="xsd:ID">
+ <xsd:annotation>
+ <xsd:documentation>
+This global attribute supports annotating arbitrary elements with an ID.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ <xsd:attributeGroup name="commonAtts">
+ <xsd:annotation>
+ <xsd:documentation>
+Convenience attribute group used to simplify this schema.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:attribute ref="wsu:Id" use="optional"/>
+ <xsd:anyAttribute namespace="##other" processContents="lax"/>
+ </xsd:attributeGroup>
+ <!-- // Utility types //////////////////////////////////////// -->
+ <xsd:complexType name="AttributedDateTime">
+ <xsd:annotation>
+ <xsd:documentation>
+This type is for elements whose [children] is a psuedo-dateTime and can have arbitrary attributes.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string">
+ <xsd:attributeGroup ref="wsu:commonAtts"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <xsd:complexType name="AttributedURI">
+ <xsd:annotation>
+ <xsd:documentation>
+This type is for elements whose [children] is an anyURI and can have arbitrary attributes.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:anyURI">
+ <xsd:attributeGroup ref="wsu:commonAtts"/>
+ </xsd:extension>
+ </xsd:simpleContent>
+ </xsd:complexType>
+ <!-- // Timestamp header components /////////////////////////// -->
+ <xsd:complexType name="TimestampType">
+ <xsd:annotation>
+ <xsd:documentation>
+This complex type ties together the timestamp related elements into a composite type.
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:element ref="wsu:Created" minOccurs="0"/>
+ <xsd:element ref="wsu:Expires" minOccurs="0"/>
+ <xsd:choice minOccurs="0" maxOccurs="unbounded">
+ <xsd:any namespace="##other" processContents="lax"/>
+ </xsd:choice>
+ </xsd:sequence>
+ <xsd:attributeGroup ref="wsu:commonAtts"/>
+ </xsd:complexType>
+ <xsd:element name="Timestamp" type="wsu:TimestampType">
+ <xsd:annotation>
+ <xsd:documentation>
+This element allows Timestamps to be applied anywhere element wildcards are present,
+including as a SOAP header.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <!-- global element decls to allow individual elements to appear anywhere -->
+ <xsd:element name="Expires" type="wsu:AttributedDateTime">
+ <xsd:annotation>
+ <xsd:documentation>
+This element allows an expiration time to be applied anywhere element wildcards are present.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+ <xsd:element name="Created" type="wsu:AttributedDateTime">
+ <xsd:annotation>
+ <xsd:documentation>
+This element allows a creation time to be applied anywhere element wildcards are present.
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:element>
+</xsd:schema>
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/docs/oasis-200401-wss-wssecurity-utility-1.0.xsd
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/BinarySecurityToken.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/elements/BinarySecurityToken.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/elements/BinarySecurityToken.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/elements/BinarySecurityToken.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,241 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss.elements;
+
+import org.apache.tsik.wss.EncoderDecoder;
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.wss.Base64EncoderDecoder;
+
+import java.io.ByteArrayInputStream;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+
+/**
+ * Implements the BinarySecurityToken element.
+ */
+public class BinarySecurityToken extends SecurityToken
+{
+ private static Class c = BinarySecurityToken.class;
+
+ static String elementName = SchemaMapper.getName(c);
+ static String prefix = SchemaMapper.getPrefix(c);
+ static String uri = SchemaMapper.getUri(c);
+ private static String[] ns = {prefix, uri};
+
+ private static String x509TokenProfileFragmentUri
+ = SchemaMapper.get("$x509-token-profile-fragment-uri");
+ private static String wssFragmentUri
+ = SchemaMapper.get("$wss-fragment-uri");
+
+
+ private static String v3IdString = "X509v3";
+ private static String skiIdString = "X509SubjectKeyIdentifier";
+
+ private EncoderDecoder encDec;
+
+ private X509Certificate cert;
+ private byte[] ski;
+
+ /**
+ * TODO javadoc.
+ *
+ * @return TODO.
+ */
+ public X509Certificate getCertificate()
+ {
+ return cert;
+ }
+
+ /**
+ * TODO javadoc.
+ *
+ * @return TODO.
+ */
+ public byte[] getSki()
+ {
+ return ski;
+ }
+
+ /**
+ * TODO javadoc.
+ *
+ * @return TODO.
+ */
+ public boolean hasSki()
+ {
+ return (this.ski != null);
+ }
+
+ /**
+ * TODO javadoc.
+ *
+ * @return TODO.
+ */
+ public boolean hasCertificate()
+ {
+ return (this.cert != null);
+ }
+
+ /**
+ * Creates the BinarySecurityToken on a certificate.
+ *
+ * @param cert the certificate.
+ */
+ public BinarySecurityToken(X509Certificate cert)
+ {
+ this.encDec = new Base64EncoderDecoder();
+ this.cert = cert;
+ }
+
+ /**
+ * Creates the BinarySecurityToken on a SKI value.
+ *
+ * @param ski the SKI value.
+ */
+ public BinarySecurityToken(byte[] ski)
+ {
+ this.encDec = new Base64EncoderDecoder();
+ this.ski = ski;
+ }
+
+ /**
+ * Returns the SKI value from a cert. The SKI may be calculated
+ * if it is not present.
+ *
+ * @param cert the certificate.
+ * @return the SKI value.
+ */
+ public static byte[] getSki(X509Certificate cert)
+ {
+ return org.apache.tsik.xmlsig.KeyInfo.getSKIExtension(cert);
+ }
+
+ public void toXml(DOMWriteCursor wc)
+ {
+ wc = wc.addUnder(BinarySecurityToken.uri, BinarySecurityToken.prefix,
+ BinarySecurityToken.elementName);
+
+ // See KeyIdentifier about QName.toString()
+ //
+ if (cert != null){
+ wc.setAttribute("ValueType", x509TokenProfileFragmentUri
+ + "#" + v3IdString);
+ }
+ if (ski != null){
+ wc.setAttribute("ValueType", x509TokenProfileFragmentUri
+ + "#" + skiIdString);
+ }
+
+ wc.setAttribute("EncodingType", wssFragmentUri
+ + "#" + encDec.getAttributeValue());
+
+ String enc = null;
+ try {
+ if (cert != null){
+ enc = encDec.encode(cert.getEncoded());
+ } else if (ski != null){
+ enc = encDec.encode(ski);
+ }
+ } catch (CertificateEncodingException e){
+ enc = e.toString();
+ }
+ wc.setText(enc);
+ }
+
+ /**
+ * Constructs the element from XML.
+ *
+ * @param cursor the cursor positioned at or above (ancestor-or-self)
+ * the element to read.
+ * @return the created element.
+ * @throws CertificateException TODO.
+ */
+ public static BinarySecurityToken fromXml(DOMCursor cursor)
+ throws CertificateException
+ {
+ DOMCursor c = BinarySecurityToken.placeCursor(cursor, elementName,
+ prefix, uri, ns);
+
+ X509Certificate cert = null;
+
+ String encodingType = c.getAttribute("EncodingType");
+ EncoderDecoder encDec = null;
+
+ if (encodingType != null){
+ if (encodingType.equals(wssFragmentUri + "#Base64Binary")){
+ encDec = new Base64EncoderDecoder();
+ } else {
+ throw new IllegalArgumentException("encoding type unknown:"
+ + encodingType);
+ }
+ }
+ if (encDec == null){
+ // default value
+ encDec = new Base64EncoderDecoder();
+ }
+
+ String valueType = c.getAttribute("ValueType");
+ if (valueType == null){
+ throw new IllegalArgumentException("default value type unknown");
+ }
+
+ if (valueType.equals(x509TokenProfileFragmentUri+"#"+v3IdString)){
+ byte[] b = encDec.decode(c.getText());
+ CertificateFactory cf
+ = CertificateFactory.getInstance("X.509");
+ ByteArrayInputStream bais = new ByteArrayInputStream(b);
+ cert = (X509Certificate) cf.generateCertificate(bais);
+ return new BinarySecurityToken(cert);
+ } else if (valueType.equals(x509TokenProfileFragmentUri
+ +"#"+skiIdString)){
+ byte[] ski = encDec.decode(c.getText());
+ return new BinarySecurityToken(ski);
+ } else {
+ throw new IllegalArgumentException("value type unknown:"
+ + valueType);
+ }
+ }
+
+ public String toString(){
+ String s = "[BinarySecurityToken ";
+
+ s += " encodingType=" +wssFragmentUri+"#"+ encDec.getAttributeValue();
+ if (cert != null) {
+ s += " valueType=" +x509TokenProfileFragmentUri+ "#" +v3IdString;
+ try{
+ s += " cert=" + encDec.encode(cert.getEncoded());
+ s += cert;
+ } catch (CertificateEncodingException e){
+ s += e.toString();
+ }
+ }
+ if (ski != null) {
+ s += " valueType=" +x509TokenProfileFragmentUri+ "#" +skiIdString;
+ s += " ski=" + encDec.encode(ski);
+ }
+
+ return s + "]";
+ }
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/BinarySecurityToken.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedData.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedData.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedData.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedData.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,178 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss.elements;
+
+import java.io.StringReader;
+import java.security.Key;
+
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.domutil.elements.ElementException;
+import org.apache.tsik.domutil.elements.ElementImpl;
+import org.apache.tsik.resource.ResourceFactory;
+import org.apache.tsik.resource.XMLResource;
+import org.apache.tsik.util.Namespaces;
+import org.apache.tsik.wss.Id;
+import org.apache.tsik.wss.TokenResolver;
+import org.apache.tsik.xmlenc.tools.ConversionException;
+import org.apache.tsik.xmlenc.tools.Converter;
+
+import org.w3c.dom.Document;
+
+/**
+ * Implements the EncryptedData element.
+ */
+public class EncryptedData extends ElementImpl
+{
+ private static Class c = EncryptedData.class;
+ private Logger logger = LoggerFactory.getLogger(c);
+
+ static String elementName = SchemaMapper.getName(c);
+ static String prefix = Namespaces.XMLENC.getPrefix();
+ static String uri = Namespaces.XMLENC.getUri();
+ private static String[] ns = {prefix, uri};
+
+ private org.apache.tsik.xmlenc.elements.EncryptedData ed;
+ private String id;
+
+ private EncryptedData(String id,
+ org.apache.tsik.xmlenc.elements.EncryptedData ed)
+ {
+ this.id = id;
+ this.ed = ed;
+ }
+
+ /**
+ * Sets the XML WSU identifier for this object.
+ *
+ * @param id the XML WSU id.
+ */
+ public void setId(String id)
+ {
+ this.id = id;
+ }
+
+ public void toXml(DOMWriteCursor wc)
+ {
+ DOMWriteCursor tmp = new DOMWriteCursor();
+ ed.toXml(tmp);
+ if (id != null) {
+ tmp.setAttribute("Id", id);
+ }
+ wc.copyUnder(tmp);
+ }
+
+ /**
+ * Creates an EncryptedData from existing XML.
+ *
+ * @param cursor the cursor to read from.
+ * @return the newly created <code>EncryptedData</code>.
+ */
+ public static EncryptedData fromXml(DOMCursor cursor)
+ {
+ DOMCursor c = placeCursor(cursor, elementName, prefix, uri, ns);
+
+ org.apache.tsik.xmlenc.elements.EncryptedData ed = null;
+
+ String id = Id.getId(c);
+ try {
+ ed = org.apache.tsik.xmlenc.elements.EncryptedData.fromXml(c);
+ c.moveToParent();
+ } catch (ElementException e){
+ // use a unified error mechanism here
+ e.printStackTrace();
+ }
+ return new EncryptedData(id, ed);
+ }
+
+ public String toString()
+ {
+ String s = "[EncryptedData ";
+
+ if (id != null) s += "id="+id+" ";
+ if (ed != null) s += ed;
+
+ return s + "]";
+ }
+
+ private DOMWriteCursor decryptedDataCursor;
+
+ /**
+ * Returns the decrypted data.
+ *
+ * @return the decrypted data, or null if there has not taken place any
+ * decryption yet.
+ */
+ public DOMCursor getDecryptedData()
+ {
+ // TBD clone
+ return decryptedDataCursor;
+ }
+
+ /**
+ * Decrypts this element. Also see {@link #getDecryptedData()}
+ *
+ * @param resolver the resolver will be used to find mapped keying
+ * information.
+ */
+ public void decrypt(TokenResolver resolver)
+ {
+ Key key = resolver.resolveKey(id);
+ logger.info("Decrypting with " +key);
+ ed.setKey(key);
+ ed.decrypt(false);
+ String s = ed.getDecryptedData();
+
+ logger.debug("Decrypted data="+s);
+
+ XMLResource xmlres = ResourceFactory.getXMLResource();
+ StringReader reader = new StringReader(s);
+ try {
+ Document doc = xmlres.parseXML(reader, false);
+ decryptedDataCursor = new DOMWriteCursor(doc);
+ } catch (Exception e){
+ throw new IllegalArgumentException(e.toString());
+ }
+ }
+
+ /**
+ * Decrypts this element and inserts it into an existing cursor.
+ * Also see {@link #decrypt(TokenResolver)}
+ *
+ * @param wc the cursor to insert the decrypted data into.
+ * @param resolver the resolver will be used to find mapped keying
+ * information.
+ * @throws ConversionException if the decrypted data is not well-formed
+ * XML.
+ */
+ public void decryptInPlace(DOMWriteCursor wc, TokenResolver resolver)
+ throws ConversionException
+ {
+ Key key = resolver.resolveKey(id);
+ logger.info("Decrypting with " +key);
+ ed.setKey(key);
+ ed.decrypt(false);
+ String s = ed.getDecryptedData();
+ logger.debug("Decrypted data="+s);
+ Converter.stringToXmlContent(s, wc);
+ }
+}
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedData.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedKey.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedKey.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedKey.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedKey.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,232 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss.elements;
+
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+
+import org.apache.tsik.common.Logger;
+import org.apache.tsik.common.LoggerFactory;
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.domutil.elements.ElementException;
+import org.apache.tsik.domutil.elements.ElementImpl;
+import org.apache.tsik.util.Namespaces;
+import org.apache.tsik.wss.TokenResolver;
+import org.apache.tsik.xmlenc.AlgorithmType;
+import org.apache.tsik.xmlenc.Encryptor;
+import org.apache.tsik.xpath.XPathException;
+
+/**
+ * Implements the BinarySecurityToken element.
+ */
+public class EncryptedKey extends ElementImpl
+{
+ private static Class c = EncryptedKey.class;
+ private Logger log = LoggerFactory.getLogger(c);
+ static String elementName = SchemaMapper.getName(c);
+ static String prefix = Namespaces.XMLENC.getPrefix();
+ public static String uri = Namespaces.XMLENC.getUri();
+ private static String[] ns = {prefix, uri};
+
+ private Key key;
+
+ private org.apache.tsik.xmlenc.elements.EncryptedKey ek;
+ private KeyInfo keyInfo;
+ private ReferenceList refList;
+
+ /**
+ * Creates a KeyInfo.
+ *
+ * @param keyInfo TODO.
+ * @param refList TODO.
+ */
+ public EncryptedKey(KeyInfo keyInfo, ReferenceList refList)
+ {
+ this(null, keyInfo, refList);
+ }
+
+ private EncryptedKey(org.apache.tsik.xmlenc.elements.EncryptedKey ek,
+ KeyInfo keyInfo, ReferenceList refList)
+ {
+ this.ek = ek;
+ this.keyInfo = keyInfo;
+ this.refList = refList;
+ }
+
+ /**
+ * Returns the data encryption key.
+ *
+ * @return the data encryption key.
+ */
+ public Key getDekKey()
+ {
+ return key;
+ }
+
+ /**
+ * Encrypts without using a token resolver. See
+ * {@link #encrypt(Key, TokenResolver)}.
+ *
+ * @param key the key to encrypt.
+ * @throws NoSuchAlgorithmException TODO
+ */
+ public void encrypt(Key key) throws NoSuchAlgorithmException
+ {
+ encrypt(key, null);
+ }
+
+ /**
+ * Encrypts a key using the encryption key given in the previous
+ * KeyInfo. The key will be encrypted using RSA 1.5 encryption.
+ *
+ * @param key the key to encrypt.
+ * @param res the token resolver to use.
+ *
+ * @throws NoSuchAlgorithmException TODO
+ */
+ public void encrypt(Key key, TokenResolver res)
+ throws NoSuchAlgorithmException
+ {
+ this.key = key;
+
+ DOMWriteCursor tmp = new DOMWriteCursor();
+ tmp.addUnder("test", "test", "test");
+
+ SecurityTokenReference str = keyInfo.getSecurityTokenReference();
+ KeyIdentifier ki = str.getKeyIdentifier();
+ X509Certificate cert = ki.getCertificate();
+
+ if (cert == null){
+ cert = res.resolveCert(str);
+ }
+
+ Encryptor e = new Encryptor(tmp.getDocument(), cert.getPublicKey(),
+ AlgorithmType.RSA1_5, null);
+ try {
+ e.encryptKeyInPlace(key, tmp.createXPath(), false);
+ } catch (XPathException ex){
+ log.warn(ex.toString());
+ ex.printStackTrace();
+ }
+ try {
+ ek = org.apache.tsik.xmlenc.elements.EncryptedKey.fromXmlStatic(tmp);
+ } catch (ElementException ex){
+ log.warn(ex.toString());
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * Returns the list of references this key encrypts.
+ *
+ * @return the list.
+ */
+ public ReferenceList getReferenceList()
+ {
+ return refList;
+ }
+
+ public void toXml(DOMWriteCursor wc)
+ {
+ DOMWriteCursor tmp = new DOMWriteCursor();
+ ek.toXml(tmp);
+
+ DOMWriteCursor foo = new DOMWriteCursor();
+ keyInfo.toXml(foo);
+ tmp.moveToChild(Namespaces.XMLENC.getUri(), "CipherData");
+ tmp.copyBefore(foo);
+ tmp.moveToParent();
+
+ refList.toXml(tmp);
+ tmp.moveToParent();
+
+ wc.copyUnder(tmp);
+ }
+
+ /**
+ * Creates an encrypted key from existing XML.
+ *
+ * @param cursor the cursor to read from.
+ * @return the newly created <code>EncryptedKey</code>.
+ * @throws IllegalArgumentException TODO.
+ */
+ public static EncryptedKey fromXml(DOMCursor cursor)
+ {
+ DOMCursor c = placeCursor(cursor, elementName, prefix, uri, ns);
+
+ org.apache.tsik.xmlenc.elements.EncryptedKey ek = null;
+ KeyInfo keyInfo = null;
+ ReferenceList refList = null;
+
+ try {
+ ek = org.apache.tsik.xmlenc.elements.EncryptedKey.fromXmlStatic(c);
+ } catch (ElementException e){
+ // use a unified error mechanism here
+ e.printStackTrace();
+ }
+ if (c.moveToChild(Namespaces.XMLSIG.getUri(), "KeyInfo")){
+ keyInfo = KeyInfo.fromXml(c);
+ c.moveToParent();
+ }
+ if (c.moveToChild(Namespaces.XMLENC.getUri(), "ReferenceList")){
+ refList = ReferenceList.fromXml(c);
+ c.moveToParent();
+ }
+ return new EncryptedKey(ek, keyInfo, refList);
+ }
+
+ public String toString(){
+ String s = "[EncryptedKey ";
+
+ if (key != null) s += " data encryption key="+key;
+ if (ek != null) s += " embedded EncryptedKey="+ek;
+ if (keyInfo != null) s += " keyInfo="+keyInfo;
+ if (refList != null) s += " referenceList=" + refList;
+
+ return s + "]";
+ }
+
+ /**
+ * Decrypts this EncryptedKey using key mapped via resolver using
+ * previously initiated KeyInfo.
+ *
+ * @param resolver the token resolver to use.
+ */
+ public void decrypt(TokenResolver resolver) throws NoSuchAlgorithmException
+ {
+ log.debug(this);
+ log.debug(resolver);
+ Key key = resolver.resolveKey(keyInfo.getSecurityTokenReference());
+ if (key == null){
+ throw new IllegalStateException("No key found for " +
+ keyInfo.getSecurityTokenReference());
+ }
+ ek.setDecryptionKey(key);
+ ek.decrypt();
+ this.key = ek.getDecryptedKey();
+ }
+
+}
+
+
+
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/EncryptedKey.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyIdentifier.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyIdentifier.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyIdentifier.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyIdentifier.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,266 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss.elements;
+
+import java.io.ByteArrayInputStream;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.domutil.elements.ElementImpl;
+import org.apache.tsik.wss.Base64EncoderDecoder;
+import org.apache.tsik.wss.EncoderDecoder;
+import org.apache.tsik.wss.Id;
+
+/**
+ * Implements the wsse:KeyIdentifier element.
+ *
+ */
+public class KeyIdentifier extends ElementImpl
+{
+ private static Class c = KeyIdentifier.class;
+
+ static String elementName = SchemaMapper.getName(c);
+ static String prefix = SchemaMapper.getPrefix(c);
+ static String uri = SchemaMapper.getUri(c);
+ private static String[] ns = {prefix, uri};
+
+ private static String x509TokenProfileFragmentUri
+ = SchemaMapper.get("$x509-token-profile-fragment-uri");
+ private static String wssFragmentUri
+ = SchemaMapper.get("$wss-fragment-uri");
+
+ private static String v3IdString = "X509v3";
+ private static String skiIdString = "X509SubjectKeyIdentifier";
+ private EncoderDecoder encDec;
+
+ private String id;
+
+ private X509Certificate cert;
+ private byte[] ski;
+
+ /**
+ * Creates a new <code>KeyIdentifier</code> instance.
+ *
+ * @param cert a <code>X509Certificate</code> value.
+ */
+ public KeyIdentifier(X509Certificate cert)
+ {
+ this.cert = cert;
+ this.encDec = new Base64EncoderDecoder();
+ }
+
+ /**
+ * Creates a new <code>KeyIdentifier</code> instance.
+ *
+ * @param ski a <code>byte[]</code> value representing the subject
+ * key identifier (ski)
+ */
+ public KeyIdentifier(byte[] ski)
+ {
+ this.ski = ski;
+ this.encDec = new Base64EncoderDecoder();
+ }
+
+ /**
+ * Returns whether this KeyIdentifier represents a SKI.
+ *
+ * @return true if this KeyIdentifier represents a SKI, false otherwise.
+ */
+ public boolean hasSki()
+ {
+ return (this.ski != null);
+ }
+
+ /**
+ * Returns whether this KeyIdentifier represents a certificate.
+ *
+ * @return true if this KeyIdentifier represents a certificate,
+ * false otherwise.
+ */
+ public boolean hasCertificate()
+ {
+ return (this.cert != null);
+ }
+
+
+ /**
+ * Returns the certificate.
+ *
+ * @return a <code>X509Certificate</code> cert, or null if this
+ * represents an SKI.
+ */
+ public X509Certificate getCertificate()
+ {
+ return cert;
+ }
+
+ /**
+ * Returns the SKI.
+ *
+ * @return the SKI or null if this represents a certificate.
+ */
+ public byte[] getSki()
+ {
+ return ski;
+ }
+
+ /**
+ * Sets the wsu:Id on this KeyIdentifier.
+ *
+ * @param id the Id.
+ */
+ public void setId(String id)
+ {
+ this.id = id;
+ }
+
+ /**
+ * Unrolls this object onto the cursor as XML.
+ *
+ * @param wc the cursor to write to.
+ */
+ public void toXml(DOMWriteCursor wc)
+ {
+ wc = wc.addUnder(KeyIdentifier.uri, KeyIdentifier.prefix,
+ KeyIdentifier.elementName);
+
+ if (id != null){
+ Id.insertInto(wc, id);
+ }
+
+ // wc.setAttribute("EncodingType", encodingType.toString());
+ // won't work as the QName prefix:value format is not what we
+ // want. See also BinarySecurityToken.
+ //
+ if (hasCertificate()){
+ wc.setAttribute("ValueType", x509TokenProfileFragmentUri
+ + "#" + v3IdString);
+ }
+ if (hasSki()){
+ wc.setAttribute("ValueType", x509TokenProfileFragmentUri
+ + "#" + skiIdString);
+ }
+
+ wc.setAttribute("EncodingType", wssFragmentUri
+ + "#" + encDec.getAttributeValue());
+
+ String enc = null;
+ try {
+ if (hasCertificate()){
+ enc = encDec.encode(cert.getEncoded());
+ } else if (hasSki()){
+ enc = encDec.encode(ski);
+ }
+ } catch (CertificateEncodingException e){
+ enc = e.toString();
+ }
+ wc.setText(enc);
+ }
+
+ /**
+ * Creates a KeyIdentifier from existing XML.
+ *
+ * @param cursor the cursor to read from.
+ * @return the newly created <code>KeyIdentifier</code>.
+ * @exception CertificateException if an error occurs while
+ * parsing certificates.
+ */
+ public static KeyIdentifier fromXml(DOMCursor cursor)
+ throws CertificateException
+ {
+ DOMCursor c = KeyIdentifier.placeCursor(cursor, elementName,
+ prefix, uri, ns);
+
+ String id = c.getAttribute(Id.uri, Id.attributeName);
+ X509Certificate cert = null;
+
+ String encodingType = c.getAttribute("EncodingType");
+ EncoderDecoder encDec = null;
+
+ if (encodingType != null){
+ if (encodingType.equals(wssFragmentUri+"#Base64Binary")){
+ encDec = new Base64EncoderDecoder();
+ } else {
+ throw new IllegalArgumentException("encoding type unknown:"
+ + encodingType);
+ }
+ }
+ if (encDec == null){
+ // default value
+ encDec = new Base64EncoderDecoder();
+ }
+
+ String valueType = c.getAttribute("ValueType");
+ if (valueType == null){
+ throw new IllegalArgumentException("default value type unknown");
+ }
+
+ if (valueType.equals(x509TokenProfileFragmentUri+"#"+v3IdString)){
+ byte[] b = encDec.decode(c.getText());
+ CertificateFactory cf
+ = CertificateFactory.getInstance("X.509");
+ ByteArrayInputStream bais = new ByteArrayInputStream(b);
+ cert = (X509Certificate) cf.generateCertificate(bais);
+
+ KeyIdentifier ki = new KeyIdentifier(cert);
+ ki.setId(id);
+ return ki;
+ } else if (valueType.equals(x509TokenProfileFragmentUri
+ +"#"+skiIdString)){
+ byte[] ski = encDec.decode(c.getText());
+
+ KeyIdentifier ki = new KeyIdentifier(ski);
+ ki.setId(id);
+ return ki;
+ } else {
+ throw new IllegalArgumentException("value type unknown:"
+ + valueType);
+ }
+ }
+
+ // no javadoc
+ //
+ public String toString()
+ {
+ String s = "[KeyIdentifier ";
+
+ s += " encodingType=" +wssFragmentUri+"#"+ encDec.getAttributeValue();
+ if (id != null) s += " id=" + id;
+ if (cert != null) {
+ s += " valueType=" +x509TokenProfileFragmentUri+ "#" +v3IdString;
+ try{
+ s += " cert=" + encDec.encode(cert.getEncoded());
+ } catch (CertificateEncodingException e){
+ s += e.toString();
+ }
+ }
+ if (ski != null) {
+ s += " valueType=" +x509TokenProfileFragmentUri+ "#" +skiIdString;
+ s += " ski=" + encDec.encode(ski);
+ }
+ return s + "]";
+ }
+
+
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyIdentifier.java
------------------------------------------------------------------------------
svn:executable =
Added: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyInfo.java
URL: http://svn.apache.org/viewcvs/incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyInfo.java?rev=230871&view=auto
==============================================================================
--- incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyInfo.java (added)
+++ incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyInfo.java Mon Aug 8 12:58:58 2005
@@ -0,0 +1,99 @@
+// $Id$
+
+//
+// (C) Copyright 2005 VeriSign, Inc. All Rights Reserved.
+//
+// VeriSign, Inc. shall have no responsibility, financial or
+// otherwise, for any consequences arising out of the use of
+// this material. The program material is provided on an "AS IS"
+// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
+// express or implied. The user is responsible for determining
+// any necessary third party rights or authorizations that may
+// be required for the use of the materials. Users are advised
+// that they may need authorizations under certain patents from
+// Microsoft and IBM, or others. Please see notice.txt file.
+// VeriSign disclaims any obligation to notify the user of any
+// such third party rights.
+//
+
+package org.apache.tsik.wss.elements;
+
+import org.apache.tsik.common.SchemaMapper;
+import org.apache.tsik.domutil.DOMCursor;
+import org.apache.tsik.domutil.DOMWriteCursor;
+import org.apache.tsik.domutil.elements.ElementImpl;
+import org.apache.tsik.util.Namespaces;
+
+/**
+ * Implements the KeyInfo element.
+ */
+public class KeyInfo extends ElementImpl
+{
+ private static Class c = KeyInfo.class;
+
+ public static String elementName = SchemaMapper.getName(c);
+
+ public static String prefix = Namespaces.XMLSIG.getPrefix();
+ public static String uri = Namespaces.XMLSIG.getUri();
+ private static String[] ns = {prefix, uri};
+
+ private SecurityTokenReference securityTokenReference;
+
+ /**
+ * Creates a key info.
+ *
+ * @param securityTokenReference a reference to the security token
+ * that this key info contains.
+ */
+ public KeyInfo(SecurityTokenReference securityTokenReference)
+ {
+ this.securityTokenReference = securityTokenReference;
+ }
+
+ SecurityTokenReference getSecurityTokenReference()
+ {
+ return securityTokenReference;
+ }
+
+ public void toXml(DOMWriteCursor wc)
+ {
+ wc = wc.addUnder(KeyInfo.uri, KeyInfo.prefix,
+ KeyInfo.elementName);
+
+ if (securityTokenReference != null){
+ securityTokenReference.toXml(wc);
+ }
+ }
+
+ /**
+ * Creates a KeyInfo from existing XML.
+ *
+ * @param cursor the cursor to read from.
+ * @return the newly created <code>KeyInfo</code>.
+ */
+ public static KeyInfo fromXml(DOMCursor cursor)
+ {
+ DOMCursor c = KeyInfo.placeCursor(cursor, elementName,
+ prefix, uri, ns);
+
+ SecurityTokenReference srt = null;
+ if (c.moveToChild(SecurityTokenReference.uri,
+ SecurityTokenReference.elementName)){
+ srt = SecurityTokenReference.fromXml(c);
+ c.moveToParent();
+ }
+ return new KeyInfo(srt);
+ }
+
+ public String toString(){
+ String s = "[KeyInfo ";
+
+ if (securityTokenReference != null) {
+ s += " securityTokenReference=" + securityTokenReference;
+ }
+ return s + "]";
+ }
+
+
+}
+
Propchange: incubator/tsik/trunk/src/org/apache/tsik/wss/elements/KeyInfo.java
------------------------------------------------------------------------------
svn:executable =
---------------------------------------------------------------------
To unsubscribe, e-mail: tsik-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tsik-dev-help@ws.apache.org