Class WSSecurityUtil
java.lang.Object
org.apache.ws.security.util.WSSecurityUtil
WS-Security Utility methods.
- Author:
- Davanum Srinivas (dims@yahoo.com).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ElementappendChildElement(Document doc, Element parent, Element child) append a child elementstatic TextcreateBase64EncodedTextNode(Document doc, byte[] data) create a base64 test nodestatic ElementcreateBinarySecurityToken(Document doc, String wsuIdVal) Create a BinarySecurityToken elementstatic intdecodeAction(String action, Vector actions) static X509CertificateensureSignedTogether(Iterator results, Element[] elements) Search through a WSS4J results vector for a single signature covering all these elements.static WSSecurityEngineResultfetchActionResult(Vector wsResultVector, int action) Fetch the result of a given action from a given result vectorstatic VectorfetchAllActionResults(Vector wsResultVector, int action, Vector results) Fetch the result of a given action from a given result vectorstatic ElementfindBodyElement(Document doc, SOAPConstants sc) return the first soap "Body" element.static NodefindElement(Node startNode, String name, String namespace) Returns the first element that matchesnameandnamespace.static ElementfindElementById(Node startNode, String value, String namespace) Returns the single element that containes an Id with valueuriandnamespace.static ElementfindWsseSecurityHeaderBlock(Document doc, Element envelope, boolean doCreate) find the first ws-security header blockstatic ElementfindWsseSecurityHeaderBlock(Document doc, Element envelope, String actor, boolean doCreate) find a ws-security header block for a given actorstatic byte[]generateNonce(int length) Generate a nonce of the given lengthstatic CiphergetCipherInstance(String cipherAlgo) static NodegetDirectChild(Node fNode, String localName, String namespace) Gets a direct child with specified localname and namespace.static ElementgetElementByGenId(Document doc, String id) Search for an element given its generic id.static ElementgetElementByWsuId(Document doc, String id) Search for an element given its wsu:id.static QNamegetFullQNameFromString(String str, Node e) Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.static StringgetIDfromReference(String ref) Turn a reference (eg "#5") into an ID (eg "5").static intgetKeyLength(String algorithm) Returns the length of the key in # of bytesstatic StringgetNamespace(String prefix, Node e) static StringgetPrefixNS(String uri, Node e) static QNamegetQNameFromString(String str, Node e) Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node.static ElementgetSecurityHeader(Document doc, String actor, SOAPConstants sc) Returns the first WS-Security header element for a given actor.static SOAPConstantsgetSOAPConstants(Element startElement) static StringgetStringForQName(QName qname, Element e) Return a string for a particular QName, mapping a new prefix if necessary.static booleanisActorEqual(String actor, String hActor) Compares two actor strings and returns true if these are equal.static SecretKeyprepareSecretKey(String symEncAlgo, byte[] rawKey) static ElementprependChildElement(Document doc, Element parent, Element child, boolean addWhitespace) prepend a child elementstatic StringsetNamespace(Element element, String namespace, String prefix) set the namespace if it is not set already.
-
Constructor Details
-
WSSecurityUtil
public WSSecurityUtil()
-
-
Method Details
-
getSecurityHeader
Returns the first WS-Security header element for a given actor. Only one WS-Security header is allowed for an actor.- Parameters:
doc-actor-- Returns:
- the
wsse:Securityelement ornullif not such element found
-
isActorEqual
-
getDirectChild
Gets a direct child with specified localname and namespace.- Parameters:
fNode- the node where to start the searchlocalName- local name of the child to getnamespace- the namespace of the child to get- Returns:
- the node or
nullif not such node found
-
findBodyElement
return the first soap "Body" element.- Parameters:
doc-- Returns:
- the body element or
nullif document does not contain a SOAP body
-
findElement
Returns the first element that matchesnameandnamespace. This is a replacement for a XPath lookup//namewith the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI- Parameters:
startNode- Where to start the searchname- Local name of the elementnamespace- Namespace URI of the element- Returns:
- The found element or
null
-
findElementById
Returns the single element that containes an Id with valueuriandnamespace. This is a replacement for a XPath Id lookup with the given namespace. It's somewhat faster than XPath, and we do not deal with prefixes, just with the real namespace URI If there are multiple elements, we log a warning and return null as this can be used to get around the signature checking.- Parameters:
startNode- Where to start the searchvalue- Value of the Id attributenamespace- Namespace URI of the Id- Returns:
- The found element if there was exactly one match, or
nullotherwise
-
setNamespace
-
getPrefixNS
-
getNamespace
-
getQNameFromString
-
getFullQNameFromString
Return a QName when passed a string like "foo:bar" by mapping the "foo" prefix to a namespace in the context of the given Node. If default namespace is found it is returned as part of the QName.- Returns:
- a QName generated from the given string representation
-
getStringForQName
-
getElementByWsuId
-
getIDfromReference
-
getElementByGenId
-
createBinarySecurityToken
-
appendChildElement
-
prependChildElement
public static Element prependChildElement(Document doc, Element parent, Element child, boolean addWhitespace) prepend a child element- Parameters:
doc- the DOM document (SOAP request)parent- element of this child elementchild- the element to appendaddWhitespace- if true prepend a newline before child- Returns:
- the child element
-
findWsseSecurityHeaderBlock
find the first ws-security header block- Parameters:
doc- the DOM document (SOAP request)envelope- the SOAP envelopedoCreate- if true create a new WSS header block if none exists- Returns:
- the WSS header or null if none found and doCreate is false
-
findWsseSecurityHeaderBlock
public static Element findWsseSecurityHeaderBlock(Document doc, Element envelope, String actor, boolean doCreate) find a ws-security header block for a given actor- Parameters:
doc- the DOM document (SOAP request)envelope- the SOAP envelopeactor- the acttoer (role) name of the WSS headerdoCreate- if true create a new WSS header block if none exists- Returns:
- the WSS header or null if none found and doCreate is false
-
createBase64EncodedTextNode
-
prepareSecretKey
-
getSOAPConstants
-
getCipherInstance
- Throws:
WSSecurityException
-
fetchActionResult
Fetch the result of a given action from a given result vector- Parameters:
wsResultVector- The result vector to fetch an action fromaction- The action to fetch- Returns:
- The result fetched from the result vector, null if the result could not be found
-
fetchAllActionResults
Fetch the result of a given action from a given result vector- Parameters:
wsResultVector- The result vector to fetch an action fromaction- The action to fetchresults- where to store the found results data for the action- Returns:
- The result fetched from the result vector, null if the result could not be found
-
decodeAction
- Throws:
WSSecurityException
-
getKeyLength
Returns the length of the key in # of bytes- Parameters:
algorithm-- Returns:
- Throws:
WSSecurityException
-
generateNonce
Generate a nonce of the given length- Returns:
- Throws:
ExceptionWSSecurityException
-
ensureSignedTogether
public static X509Certificate ensureSignedTogether(Iterator results, Element[] elements) throws WSSecurityException Search through a WSS4J results vector for a single signature covering all these elements.- Parameters:
results- results (e.g., as stored as WSHandlerConstants.RECV_RESULTS on an Axis MessageContext)elements- the elements to check- Returns:
- the identity of the signer
- Throws:
WSSecurityException- if no suitable signature could be found or if any element didn't have a wsu:Id attribute
-