amattheu 2004/02/20 01:28:13
Modified: src/org/apache/xml/security/encryption ReferenceList.java
XMLCipher.java
Log:
Implemented ReferenceList.
Credit - Vishal Mahajan.
Revision Changes Path
1.11 +10 -102
xml-security/src/org/apache/xml/security/encryption/ReferenceList.java
Index: ReferenceList.java
===================================================================
RCS file:
/home/cvs/xml-security/src/org/apache/xml/security/encryption/ReferenceList.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ReferenceList.java 8 Feb 2004 06:10:28 -0000 1.10
+++ ReferenceList.java 20 Feb 2004 09:28:13 -0000 1.11
@@ -18,9 +18,6 @@
import java.util.Iterator;
-import java.util.List;
-import java.util.LinkedList;
-import org.w3c.dom.Element;
/**
@@ -43,29 +40,11 @@
* @author Axl Mattheus
* @see Reference.
*/
-public class ReferenceList {
+public interface ReferenceList {
+
public static final int DATA_REFERENCE = 0x00000001;
- public static final int KEY_REFERENCE = 0x00000002;
- private Class sentry;
- private List references;
- /**
- * Returns an instance of <code>ReferenceList</code>, initialized with
the
- * appropriate parameters.
- *
- * @param type the type of references this <code>ReferenceList</code>
will
- * hold.
- */
- public ReferenceList (int type) {
- if (type == DATA_REFERENCE) {
- sentry = DataReference.class;
- } else if (type == KEY_REFERENCE) {
- sentry = KeyReference.class;
- } else {
- throw new IllegalArgumentException();
- }
- references = new LinkedList();
- }
+ public static final int KEY_REFERENCE = 0x00000002;
/**
* Adds a reference to this reference list.
@@ -74,35 +53,21 @@
* @throws IllegalArgurmentException if the <code>Reference</code> is
not an
* instance of <code>DataReference</code> or <code>KeyReference</code>.
*/
- public void add(Reference reference) {
- if (!reference.getClass().equals(sentry)) {
- throw new IllegalArgumentException();
- } else {
- references.add(reference);
- }
- }
+ public void add(Reference reference);
/**
* Removes a reference from the <code>ReferenceList</code>.
*
* @param reference the reference to remove.
*/
- public void remove(Reference reference) {
- if (!reference.getClass().equals(sentry)) {
- throw new IllegalArgumentException();
- } else {
- references.remove(reference);
- }
- }
+ public void remove(Reference reference);
/**
* Returns the size of the <code>ReferenceList</code>.
*
* @return the size of the <code>ReferenceList</code>.
*/
- public int size() {
- return (references.size());
- }
+ public int size();
/**
* Indicates if the <code>ReferenceList</code> is empty.
@@ -110,9 +75,7 @@
* @return <code><b>true</b></code> if the <code>ReferenceList</code> is
* empty, else <code><b>false</b></code>.
*/
- public boolean isEmpty() {
- return (references.isEmpty());
- }
+ public boolean isEmpty();
/**
* Returns an <code>Iterator</code> over all the <code>Reference</code>s
@@ -120,72 +83,17 @@
*
* @return Iterator.
*/
- public Iterator getReferences() {
- return (references.iterator());
- }
+ public Iterator getReferences();
/**
* <code>DataReference</code> factory method. Returns a
* <code>DataReference</code>.
*/
- public static Reference newDataReference(String uri) {
- return (new DataReference(uri));
- }
+ public Reference newDataReference(String uri);
/**
* <code>KeyReference</code> factory method. Returns a
* <code>KeyReference</code>.
*/
- public static Reference newKeyReference(String uri) {
- return (new KeyReference(uri));
- }
-
- /**
- * <code>ReferenceImpl</code> is an implementation of
- * <code>Reference</code>.
- *
- * @see Reference.
- */
- private static class ReferenceImpl implements Reference {
- private String uri;
- private List referenceInformation;
-
- ReferenceImpl(String _uri) {
- this.uri = _uri;
- referenceInformation = new LinkedList();
- }
-
- public String getURI() {
- return (uri);
- }
-
- public Iterator getElementRetrievalInformation() {
- return (referenceInformation.iterator());
- }
-
- public void setURI(String _uri) {
- this.uri = _uri;
- }
-
- public void removeElementRetrievalInformation(Element node) {
- referenceInformation.remove(node);
- }
-
- public void addElementRetrievalInformation(Element node) {
- referenceInformation.add(node);
- }
- }
-
- private static class DataReference extends ReferenceImpl {
- DataReference(String uri) {
- super(uri);
- }
- }
-
- private static class KeyReference extends ReferenceImpl {
- KeyReference(String uri) {
- super (uri);
- }
- }
+ public Reference newKeyReference(String uri);
}
-
1.20 +187 -38
xml-security/src/org/apache/xml/security/encryption/XMLCipher.java
Index: XMLCipher.java
===================================================================
RCS file:
/home/cvs/xml-security/src/org/apache/xml/security/encryption/XMLCipher.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- XMLCipher.java 8 Feb 2004 06:10:28 -0000 1.19
+++ XMLCipher.java 20 Feb 2004 09:28:13 -0000 1.20
@@ -17,23 +17,20 @@
package org.apache.xml.security.encryption;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
-import java.io.ByteArrayOutputStream;
-import java.lang.Integer;
+import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
-import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
-import java.util.Vector;
+
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
@@ -42,37 +39,34 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import
org.apache.xml.security.keys.keyresolver.implementations.EncryptedKeyResolver;
-import org.apache.xml.security.keys.keyresolver.KeyResolverException;
-import org.apache.xml.security.keys.keyresolver.KeyResolverSpi;
-import org.apache.xml.security.keys.KeyInfo;
-import org.apache.xml.security.utils.Constants;
-import org.apache.xml.security.utils.EncryptionConstants;
-import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
+
import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
-import org.apache.xml.security.transforms.Transform;
-import org.apache.xml.security.utils.ElementProxy;
-import org.apache.xml.security.exceptions.Base64DecodingException;
import org.apache.xml.security.exceptions.XMLSecurityException;
+import org.apache.xml.security.keys.KeyInfo;
+import org.apache.xml.security.keys.keyresolver.KeyResolverException;
+import
org.apache.xml.security.keys.keyresolver.implementations.EncryptedKeyResolver;
import org.apache.xml.security.signature.XMLSignatureException;
import org.apache.xml.security.transforms.InvalidTransformException;
import org.apache.xml.security.transforms.TransformationException;
+import org.apache.xml.security.utils.Base64;
+import org.apache.xml.security.utils.Constants;
+import org.apache.xml.security.utils.ElementProxy;
+import org.apache.xml.security.utils.EncryptionConstants;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.utils.URI;
+import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
-import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
-import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-// import sun.misc.BASE64Encoder;
-import org.apache.xml.security.utils.Base64;
/**
@@ -1728,7 +1722,7 @@
* Create a new ReferenceList object
*/
public ReferenceList createReferenceList(int type) {
- return (new ReferenceList(type));
+ return (_factory.newReferenceList(type));
}
/**
@@ -2080,7 +2074,7 @@
*
*/
ReferenceList newReferenceList(int type) {
- return (new ReferenceList(type));
+ return (new ReferenceListImpl(type));
}
/**
@@ -2131,24 +2125,34 @@
// Figure out how to make this pesky line work..
// <any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
- // TODO:
///////////////////////////////////////////////////////////
- // Implement properly, implement a KeyInfo marshaler.
+ // TODO: Work out how to handle relative URI
+
Element originatorKeyInfoElement =
(Element) element.getElementsByTagNameNS(
EncryptionConstants.EncryptionSpecNS,
EncryptionConstants._TAG_ORIGINATORKEYINFO).item(0);
if (null != originatorKeyInfoElement) {
- result.setOriginatorKeyInfo(null);
+ try {
+ result.setOriginatorKeyInfo(
+ new KeyInfo(originatorKeyInfoElement, null));
+ } catch (XMLSecurityException xse) {
+ throw new XMLEncryptionException("empty", xse);
+ }
}
- // TODO:
///////////////////////////////////////////////////////////
- // Implement properly, implement a KeyInfo marshaler.
+ // TODO: Work out how to handle relative URI
+
Element recipientKeyInfoElement =
(Element) element.getElementsByTagNameNS(
EncryptionConstants.EncryptionSpecNS,
EncryptionConstants._TAG_RECIPIENTKEYINFO).item(0);
if (null != recipientKeyInfoElement) {
- result.setRecipientKeyInfo(null);
+ try {
+ result.setRecipientKeyInfo(
+ new KeyInfo(recipientKeyInfoElement, null));
+ } catch (XMLSecurityException xse) {
+ throw new XMLEncryptionException("empty", xse);
+ }
}
return (result);
@@ -2585,7 +2589,7 @@
// complain
}
- ReferenceList result = newReferenceList(type);
+ ReferenceList result = new ReferenceListImpl(type);
NodeList list = null;
switch (type) {
case ReferenceList.DATA_REFERENCE:
@@ -2599,7 +2603,7 @@
((Element)
list.item(0)).getNodeValue()).toString();
} catch (URI.MalformedURIException mfue) {
}
- result.add(ReferenceList.newDataReference(uri));
+ result.add(result.newDataReference(uri));
}
case ReferenceList.KEY_REFERENCE:
list = element.getElementsByTagNameNS(
@@ -2612,7 +2616,7 @@
((Element)
list.item(0)).getNodeValue()).toString();
} catch (URI.MalformedURIException mfue) {
}
- result.add(ReferenceList.newKeyReference(uri));
+ result.add(result.newKeyReference(uri));
}
}
@@ -2690,9 +2694,7 @@
}
Element toElement(ReferenceList referenceList) {
- // NOTE:
///////////////////////////////////////////////////////////
- // TODO: Complete
- return (null);
+ return ((ReferenceListImpl) referenceList).toElement();
}
/**
@@ -2814,10 +2816,10 @@
}
}
if (null != originatorKeyInfo) {
- // TODO: complete
+ result.appendChild(originatorKeyInfo.getElement());
}
if (null != recipientKeyInfo) {
- // TODO: complete
+ result.appendChild(recipientKeyInfo.getElement());
}
return (result);
@@ -3205,7 +3207,7 @@
super.getEncryptionMethod()).toElement());
}
if (null != super.getKeyInfo()) {
- // TODO: complete
+ result.appendChild(super.getKeyInfo().getElement());
}
result.appendChild(
((CipherDataImpl) super.getCipherData()).toElement());
@@ -3214,7 +3216,8 @@
super.getEncryptionProperties()).toElement());
}
if (referenceList != null && !referenceList.isEmpty()) {
- // TODO: complete
+ result.appendChild(((ReferenceListImpl)
+ getReferenceList()).toElement());
}
if (null != carriedName) {
result.appendChild(
@@ -3611,6 +3614,152 @@
return EncryptionConstants.EncryptionSpecNS;
}
+ }
+
+ //<element name='ReferenceList'>
+ // <complexType>
+ // <choice minOccurs='1' maxOccurs='unbounded'>
+ // <element name='DataReference'
type='xenc:ReferenceType'/>
+ // <element name='KeyReference'
type='xenc:ReferenceType'/>
+ // </choice>
+ // </complexType>
+ //</element>
+ private class ReferenceListImpl implements ReferenceList {
+ private Class sentry;
+ private List references;
+
+ public ReferenceListImpl(int type) {
+ if (type == ReferenceList.DATA_REFERENCE) {
+ sentry = DataReference.class;
+ } else if (type == ReferenceList.KEY_REFERENCE) {
+ sentry = KeyReference.class;
+ } else {
+ throw new IllegalArgumentException();
+ }
+ references = new LinkedList();
+ }
+
+ public void add(Reference reference) {
+ if (!reference.getClass().equals(sentry)) {
+ throw new IllegalArgumentException();
+ } else {
+ references.add(reference);
+ }
+ }
+
+ public void remove(Reference reference) {
+ if (!reference.getClass().equals(sentry)) {
+ throw new IllegalArgumentException();
+ } else {
+ references.remove(reference);
+ }
+ }
+
+ public int size() {
+ return (references.size());
+ }
+
+ public boolean isEmpty() {
+ return (references.isEmpty());
+ }
+
+ public Iterator getReferences() {
+ return (references.iterator());
+ }
+
+ Element toElement() {
+ Element result = ElementProxy.createElementForFamily(
+ _contextDocument,
+ EncryptionConstants.EncryptionSpecNS,
+ EncryptionConstants._TAG_REFERENCELIST);
+ Iterator eachReference = references.iterator();
+ while (eachReference.hasNext()) {
+ Reference reference = (Reference) eachReference.next();
+ result.appendChild(
+ ((ReferenceImpl) reference).toElement());
+ }
+ return (result);
+ }
+
+ public Reference newDataReference(String uri) {
+ return (new DataReference(uri));
+ }
+
+ public Reference newKeyReference(String uri) {
+ return (new KeyReference(uri));
+ }
+
+ /**
+ * <code>ReferenceImpl</code> is an implementation of
+ * <code>Reference</code>.
+ *
+ * @see Reference.
+ */
+ private abstract class ReferenceImpl implements Reference {
+ private String uri;
+ private List referenceInformation;
+
+ ReferenceImpl(String _uri) {
+ this.uri = _uri;
+ referenceInformation = new LinkedList();
+ }
+
+ public String getURI() {
+ return (uri);
+ }
+
+ public Iterator getElementRetrievalInformation() {
+ return (referenceInformation.iterator());
+ }
+
+ public void setURI(String _uri) {
+ this.uri = _uri;
+ }
+
+ public void removeElementRetrievalInformation(Element node) {
+ referenceInformation.remove(node);
+ }
+
+ public void addElementRetrievalInformation(Element node) {
+ referenceInformation.add(node);
+ }
+
+ public abstract Element toElement();
+
+ Element toElement(String tagName) {
+ Element result = ElementProxy.createElementForFamily(
+ _contextDocument,
+ EncryptionConstants.EncryptionSpecNS,
+ tagName);
+ result.setAttribute(EncryptionConstants._ATT_URI, uri);
+
+ // TODO: Need to martial referenceInformation
+ // Figure out how to make this work..
+ // <any namespace="##other" minOccurs="0"
maxOccurs="unbounded"/>
+
+ return (result);
+ }
+ }
+
+ private class DataReference extends ReferenceImpl {
+ DataReference(String uri) {
+ super(uri);
+ }
+
+ public Element toElement() {
+ return toElement(EncryptionConstants._TAG_DATAREFERENCE);
+ }
+ }
+
+ private class KeyReference extends ReferenceImpl {
+ KeyReference(String uri) {
+ super (uri);
+ }
+
+ public Element toElement() {
+ return toElement(EncryptionConstants._TAG_KEYREFERENCE);
+ }
+ }
}
}
}