This document defines a set of [[xpath-30]] extension functions to perform cryptographic operations. These functions are related to XML Digital Signature, to encryption and decryption, and to hashing and digesting of messages. They have been designed to be compatible with [[xquery-30]] and [[xslt-30]], as well as any other standard based on [[xpath-30]].

The functions are defined on the datatypes defined in [[xmlschema-2]] and [[xpath-datamodel-30]]. Also, the functions are defined on nodes and node sequences as defined in the [[xpath-datamodel-30]] and are following the good practices inferred from [[xpath-functions-30]].

Status of this document

This document is in an interim draft stage. Comments are welcomed at [email protected] mailing list (archive).

Introduction

Cryptography is the science of communicating in secret code, by conversion of data with the help of a key. In modern times, cryptography is necessary when communicating over any untrusted medium, particularly the Internet.

Encryption of data can be of two types: symmetric and asymmetric. Symmetric encryption means that the same key is used for encryption and decryption. Asymmetric encryption means that a message can be encrypted by using a key that is public, but the decryption can be made only by using a private key, which form a pair with the respective public key.

A related technique of cryptography is to apply a one-way hash or digest function to data; replicating the operation with the same data and function can ensure the integrity of the data.

Namespace conventions

The module defined by this document defines functions and elements in the namespace http://expath.org/ns/crypto. In this document, the crypto prefix, when used, is bound to this namespace URI.

Error codes are defined in the same namespace (http://expath.org/ns/crypto), and in this document are displayed with the same prefix, crypto.

Error management

Error conditions are identified by a code (a QName). When such an error condition is reached during the execution of the function, a dynamic error is thrown, with the corresponding error code (as if the standard XPath function error had been called).

Integrity and Authentication of Data

The crypto:hash function

This function generates a "message digest" of the input data, by using a cryptographic algorithm. It returns the hash value as xs:string.

The crypto:hmac function

HMAC (Keyed-Hashing for Message Authentication) [[rfc2104]] is a mechanism for message authentication using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function in combination with a secret shared key. Typically, message authentication codes are used between two parties that share a secret key in order to validate information transmitted between these parties.

This function generates a message authentication code, based on the input message, by using a cryptographic algorithm and a secret key. It has two signatures; the first one outputs the result as xs:byte*, while the second one outputs the result as encoded xs:string.

XML Digital Signature

A digital signature is a technique to certify that a digital document is authentic. This means that the origin of the document is known and there is the ability to identify if the digital document has been changed since the digital signature was applied to it. This is done by means of a cryptographic transformation.

This function can apply an XML digital signature to one or more resources. There are three types of XML digital signatures, namely enveloped, enveloping, and detached signatures.

According to [[xmldsig-core]], the enveloped and enveloping signatures are applied to the same digital resource (and XML document), while the detached signatures can be applied to more than one digital resources.

The designation of these digital resources is to be made based upon the explanations given in [[xmldsig-core]].

In order to reference these digital resources, this function is using an XML syntax inspired by [[xmldsig-core]]. For details, see below the parameter $resources.

The crypto:generate-signature function

The function has its parameters passed as a map(xs:string, item()). After the function's signature, an example of parameters is given.

Explanations about $parameters parameter

Parameters used for generating a XML digital signature
Key (as xs:string) Value Meaning
canonicalization-algorithm xs:string The canonicalization algorithm applied to the SignedInfo element prior to performing signature calculations. Possible values are given in the list below. The default value is "inclusive-with-comments-1.1". If the parameter specifies an unsupported algorithm, this is an error [=crypto:unknown-algorithm=]. (Optional)
  • inclusive-1.1, identifier http://www.w3.org/2006/12/xml-c14n11, for XML 1.1 documents, see [[xmldsig-core]], section 6.5.2 Canonical XML 1.1.

  • inclusive-with-comments-1.1, identifier http://www.w3.org/2006/12/xml-c14n11#WithComments, for XML 1.1 documents, see [[xmldsig-core]], section 6.5.2 Canonical XML 1.1.

  • inclusive-1.0, identifier http://www.w3.org/TR/2001/REC-xml-c14n-20010315, for XML 1.0 documents, see [[xmldsig-core]], section 6.5.1 Canonical XML 1.0.

  • inclusive-with-comments-1.0, identifier http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments, for XML 1.0 documents, see [[xmldsig-core]], section 6.5.1 Canonical XML 1.0.

  • exclusive, identifier http://www.w3.org/2001/10/xml-exc-c14n#, see [[xml-exc-c14n]], section 4. Use in XML Security.

  • exclusive-with-comments, identifier http://www.w3.org/2001/10/xml-exc-c14n#WithComments, see [[xml-exc-c14n]], section 4. Use in XML Security.

digest-algorithm xs:string The digest algorithm to be applied to the signed object. Possible values are: "SHA1" (identifier http://www.w3.org/2000/09/xmldsig#sha1, see [[xmldsig-core]], section 6.1 Algorithm Identifiers and Implementation Requirements), "SHA256", and "SHA512". The default value is "SHA1". If the parameter specifies an unsupported algorithm, this is an error [=crypto:unknown-algorithm=]. (Optional)
signature-algorithm xs:string The algorithm used for signature generation and validation. Possible values are: "DSAwithSHA1" (identifier http://www.w3.org/2000/09/xmldsig#dsa-sha1, see [[xmldsig-core]], section 6.1 Algorithm Identifiers and Implementation Requirements), and "RSAwithSHA1" (identifier http://www.w3.org/2000/09/xmldsig#rsa-sha1, see [[xmldsig-core]], section 6.1 Algorithm Identifiers and Implementation Requirements). The default value is "DSAwithSHA1". If the parameter specifies an unsupported algorithm, this is an error [=crypto:unknown-algorithm=]. (Optional)
signature-namespace-prefix xs:string The namespace prefix for signature. If this parameter is not provided, the default value of 'dsig' will be used. (Optional)
signature-type xs:string The method used for signing the content of signature. Possible values are: "enveloping", "enveloped", and "detached". The default value is "enveloped". If the parameter specifies an unsupported signature type, this is an error [=crypto:signature-type=]. (Optional)
references element(Reference)* The references to the digital resources to be signed. If this parameter is not provided, the whole input document will be signed. If the signature is of enveloped or enveloping type, and more than one resource is inputted to be signed, this is an error [=crypto:input-resources=]. More details about this parameter can be found at . (Optional)
key xs:anyAtomicType The secret key used for signing. This parameter can be of type xs:string, xs:base64Binary, or xs:hexBinary. If the key is invalid or missing, this is an error [=crypto:invalid-crypto-key=].

Example of $parameters parameter

Explanations about $resources parameter

The $resources parameter
Key (as xs:string) Value Meaning
Reference element() This is the parent element for a reference to a resource to be signed. Its namespace is http://www.w3.org/2000/09/xmldsig#. Its URI attribute identifies a resource using a URI-Reference (for details see [[xmldsig-core]], section 4.3.3.1 The URI Attribute).
Transforms element() This element contains an ordered list of Transform elements.
Transform element() This element describes how the signer obtained the data object that is to be digested. The Algorithm attribute's value specifies the name of the algorithm to be applied, and the Transform element's text content provides additional data for processing using that algorithm. For more details and schema of this element, see [[xmldsig-core]], section 4.3.3.4 The Transforms Element.
XPath element() The XPath expression to be applied to the input document, in order to get the resource to be signed. The version of XPath to be used is 1.0, plus a function named here. The Filter attribute's value provides the name of the operation (intersection, subtraction and union) used for combining the XPath-selected subtrees. For more details, see [[xmldsig-core]], section 6.6.3 XPath Filtering, and [[xmldsig-filter2]].

The crypto:validate-signature function

This function validates an XML Digital Signature.

Encryption and Decryption

Encryption represents the process of conversion of data, by using a secret key (a cipher), in a form (called cipher text) that cannot be understood by unautorized persons.

The decryption represents the reverse process, of converting encrypted data back to plain text (original text).

There are two main types of encryption: symmetric encryption, when both parties, the sender and the receiver, use the same secret key, previously exchanged, and asymmetric encryption, when a key pair, consisting of a private key and a private key, is used, of which the public key is used by sender to encrypt a message that can only be decrypted by the receiver, who holds the private key of that key pair.

The crypto:encrypt function

This function encrypts data.

Explanations about $parameters parameter

The $parameters parameter of crypto:encrypt
Key (as xs:string) Value Meaning
key xs:anyAtomicType This is the cryptographic key used for encryption. This parameter can be of type xs:string, xs:base64Binary, or xs:hexBinary. If the key is invalid or missing, this is an error [=crypto:invalid-crypto-key=]. If the length of data provided to the block cipher is incorrect, this is an error [=crypto:block-size=].
algorithm xs:string This is the cryptographic algorithm used for encryption. For symmetric encryption, a transformation name can be used. If the parameter specifies an unsupported algorithm or transformation name or the parameter is missing, this is an error [=crypto:unknown-algorithm=]. The supported algorithms are implementation dependent.
iv xs:string This is the initialization vector for symmetric encryption. If the initialization vector is missing or it is not as it is requested by the encryption algorithm, this is an error [=crypto:incorrect-initialization-vector=]. For asymmetric encryption, this parameter is not needed.

The crypto:decrypt function

This function decrypts data.

Explanations about $parameters parameter

The $parameters parameter of crypto:decrypt
Key (as xs:string) Value Meaning
key xs:anyAtomicType This is the cryptographic key used for decryption. This parameter can be of type xs:string, xs:base64Binary, or xs:hexBinary. If the key is invalid or missing, this is an error [=crypto:invalid-crypto-key=]. If the length of data provided to the block cipher is incorrect, this is an error [=crypto:block-size=].
algorithm xs:string This is the cryptographic algorithm used for decryption. For symmetric decryption, a transformation name can be used. If the parameter specifies an unsupported algorithm or transformation name or the parameter is missing, this is an error [=crypto:unknown-algorithm=]. The supported algorithms are implementation dependent.
iv xs:string This is the initialization vector for symmetric decryption. If the initialization vector is missing or it is not as it is requested by the decryption algorithm, this is an error [=crypto:incorrect-initialization-vector=]. For asymmetric decryption, this parameter is not needed.

Scenarios of usage

Generation of OAuth signature

Generation of AWS Signature Version 4 signature

Generation of enveloped digital XML signature of a document

Summary of Error Conditions

crypto:unknown-algorithm
The specified algorithm is not supported.
crypto:signature-type
The specified signature type is not supported.
crypto:unreadable-keystore
I/O error while reading keystore, or the password is incorrect.
crypto:denied-keystore
Permission denied to read keystore.
crypto:keystore-url
The keystore URL is invalid.
crypto:keystore-type
The keystore type is not supported.
crypto:alias-key
Cannot find key for alias in given keystore.
crypto:signature-element
Cannot find Signature element.
crypto:inexistent-padding
No such padding.
crypto:incorrect-padding
Incorrect padding.
crypto:encryption-type
The encryption type is not supported.
crypto:invalid-crypto-key
The cryptographic key is invalid.
crypto:block-size
Illegal block size.
crypto:decryption-type
The decryption type is not supported.
crypto:no-provider
The provider is not set.
crypto:input-resources
The 'enveloped' and 'enveloping' signatures have to be applied to only one resource.
crypto:incorrect-initialization-vector
The initialization vector is not correct.