org.neuclear.xml.xmlsec
Class XMLSecTools

java.lang.Object
  extended by org.neuclear.xml.xmlsec.XMLSecTools

public final class XMLSecTools
extends java.lang.Object

Tools for dealing with XMLSig's.

Usage:

 KeyPair kp = ..... // Get RSA Key Pair
 Element someElement = ..... // Get Dom4j Element somehow
 XMLSecTools.signElement("uri:helloworld",someElement,kp);
 

if (XMLSecTools.verifySignature(someElement,kp.getPublicKey())) // Do something

In addition there are handy methods for dealing with KeyInfo elements for generating Key's from xml.


Field Summary
static org.dom4j.Namespace NS_DS
           
static java.lang.String XMLDSIG_NAMESPACE
           
 
Method Summary
static org.dom4j.Element base64ToElement(java.lang.String localName, byte[] bytes)
          Method base64ToElement
static org.dom4j.Element base64ToElement(java.lang.String localName, java.lang.String data)
          Method base64ToElement
static org.dom4j.Element bigIntToElement(java.lang.String localName, java.math.BigInteger big)
          Method base64ToElement
static byte[] canonicalize(Canonicalizer canon, java.lang.Object node)
          Canonicalizes an object based on the given Canonicalizer
static byte[] canonicalize(java.lang.Object node)
          This is canonicalizes a node and outputs it as a byte array
static byte[] canonicalizeSubset(org.dom4j.Node node, java.lang.String xpath)
          Canonicalises a subset of the node based on the given xpath.
static org.dom4j.Element createElementInSignatureSpace(java.lang.String elementName)
          Used to create an Element within the XMLSignature Standard's namespace
static org.dom4j.Element createKeyInfo(java.security.KeyStore ks, java.lang.String s)
          Creates a KeyInfo Element containing the public key of a key stored in the given keystore.
static org.dom4j.Element createKeyInfo(java.security.PublicKey pub)
          Creates a KeyInfo Element containing the XML Encoded KeyInfo of a given public key.
static org.dom4j.QName createQName(java.lang.String name)
          Used to create a QName within the XMLSignature Standard's namespace
static org.dom4j.Text createTextWithBigInteger(java.math.BigInteger biginteger)
          This method takes an (empty) Element and a BigInteger and adds the base64 encoded BigInteger to the Element.
static byte[] decodeBase64Element(org.dom4j.Element element)
          Method decodeBase64Element

Takes the Text children of the Element and interprets them as input for the Base64.decodeBase64Element() function.

static java.math.BigInteger decodeBigIntegerFromElement(org.dom4j.Element element)
          Method decodeBigIntegerFromElement
static java.math.BigInteger decodeBigIntegerFromText(org.dom4j.Text text)
          Method decodeBigIntegerFromText
static org.dom4j.Element decodeElementBase64(java.lang.String b64)
          Decodes a Base64 encoded xml element.
static java.lang.String encodeElementBase64(org.dom4j.Element elem)
          Creates a textual representation of an element and encodes the results as a base64.
static java.lang.String encodeElementBase64(SignedElement elem)
          Creates a textual representation of an element and encodes the results as a base64.
static byte[] getElementBytes(org.dom4j.Node node)
          This takes a node and outputs it as a byte array.
static org.dom4j.Element getSignatureElement(org.dom4j.Element elem)
          Attempts to find a Signature within an element
static boolean isInXMLSigNS(org.dom4j.Element xmlSigElement)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XMLDSIG_NAMESPACE

public static final java.lang.String XMLDSIG_NAMESPACE
See Also:
Constant Field Values

NS_DS

public static final org.dom4j.Namespace NS_DS
Method Detail

createKeyInfo

public static org.dom4j.Element createKeyInfo(java.security.KeyStore ks,
                                              java.lang.String s)
                                       throws java.security.KeyStoreException
Creates a KeyInfo Element containing the public key of a key stored in the given keystore.

Parameters:
ks - KeyStore to use
s - Identifier of Key
Returns:
Element containg valid KeyInfo
Throws:
java.security.KeyStoreException

createKeyInfo

public static org.dom4j.Element createKeyInfo(java.security.PublicKey pub)
Creates a KeyInfo Element containing the XML Encoded KeyInfo of a given public key.

Parameters:
pub - RSA PublicKey to encode
Returns:
Element containg valid KeyInfo

createQName

public static org.dom4j.QName createQName(java.lang.String name)
Used to create a QName within the XMLSignature Standard's namespace

Parameters:
name -
Returns:
valid QName

createElementInSignatureSpace

public static org.dom4j.Element createElementInSignatureSpace(java.lang.String elementName)
Used to create an Element within the XMLSignature Standard's namespace

Parameters:
elementName -
Returns:

getSignatureElement

public static org.dom4j.Element getSignatureElement(org.dom4j.Element elem)
                                             throws XMLSecurityException
Attempts to find a Signature within an element

Parameters:
elem - Element to search.
Returns:
XMLSignature object
Throws:
XMLSecurityException

isInXMLSigNS

public static boolean isInXMLSigNS(org.dom4j.Element xmlSigElement)

getElementBytes

public static byte[] getElementBytes(org.dom4j.Node node)
This takes a node and outputs it as a byte array. Note this is not canonicalized

Parameters:
node - Dom4J node to canonicalize
Returns:
byte array of signature

canonicalize

public static byte[] canonicalize(java.lang.Object node)
                           throws XMLSecurityException
This is canonicalizes a node and outputs it as a byte array

Parameters:
node - Dom4J node to canonicalize
Returns:
byte array of signature
Throws:
XMLSecurityException

canonicalize

public static byte[] canonicalize(Canonicalizer canon,
                                  java.lang.Object node)
                           throws XMLSecurityException
Canonicalizes an object based on the given Canonicalizer

Parameters:
canon -
node -
Returns:
Throws:
XMLSecurityException

canonicalizeSubset

public static byte[] canonicalizeSubset(org.dom4j.Node node,
                                        java.lang.String xpath)
Canonicalises a subset of the node based on the given xpath. Remember this doesnt necesarily return a wellformed XML Document. It depends on the given xpath.

Parameters:
node -
xpath -
Returns:

encodeElementBase64

public static java.lang.String encodeElementBase64(org.dom4j.Element elem)
Creates a textual representation of an element and encodes the results as a base64.

This is useful when passing xml as hidden html form fields.

Parameters:
elem - Element to Encode
Returns:
String containing Base64 encoded Element

encodeElementBase64

public static java.lang.String encodeElementBase64(SignedElement elem)
                                            throws XMLException
Creates a textual representation of an element and encodes the results as a base64.

This is useful when passing xml as hidden html form fields.

Parameters:
elem - SignedElement to Encode
Returns:
String containing Base64 encoded Element
Throws:
XMLException

decodeElementBase64

public static org.dom4j.Element decodeElementBase64(java.lang.String b64)
                                             throws XMLSecurityException,
                                                    org.neuclear.commons.crypto.CryptoException
Decodes a Base64 encoded xml element.

Parameters:
b64 - the Encoded string
Returns:
Element
Throws:
XMLSecurityException - is thrown if there is a problem parsing the string
org.neuclear.commons.crypto.CryptoException

decodeBigIntegerFromElement

public static java.math.BigInteger decodeBigIntegerFromElement(org.dom4j.Element element)
                                                        throws XMLSecurityException
Method decodeBigIntegerFromElement

Parameters:
element -
Returns:
Throws:
XMLSecurityException

decodeBigIntegerFromText

public static java.math.BigInteger decodeBigIntegerFromText(org.dom4j.Text text)
                                                     throws org.neuclear.commons.crypto.CryptoException
Method decodeBigIntegerFromText

Parameters:
text -
Returns:
Throws:
org.neuclear.commons.crypto.CryptoException

createTextWithBigInteger

public static org.dom4j.Text createTextWithBigInteger(java.math.BigInteger biginteger)
This method takes an (empty) Element and a BigInteger and adds the base64 encoded BigInteger to the Element.

Parameters:
biginteger -
Returns:
Text

decodeBase64Element

public static byte[] decodeBase64Element(org.dom4j.Element element)
                                  throws XMLSecurityException
Method decodeBase64Element

Takes the Text children of the Element and interprets them as input for the Base64.decodeBase64Element() function.

Parameters:
element -
Returns:
Throws:
XMLSecurityException

base64ToElement

public static org.dom4j.Element base64ToElement(java.lang.String localName,
                                                byte[] bytes)
Method base64ToElement

Parameters:
localName -
bytes -
Returns:

base64ToElement

public static org.dom4j.Element base64ToElement(java.lang.String localName,
                                                java.lang.String data)
Method base64ToElement

Parameters:
localName -
data -
Returns:

bigIntToElement

public static org.dom4j.Element bigIntToElement(java.lang.String localName,
                                                java.math.BigInteger big)
Method base64ToElement

Parameters:
localName -
big -
Returns:


Copyright © 2002-2004 Antilles Software Ventures SA. All Rights Reserved.