This proposal defines a set of XPath 2.0 extension functions to perform cryptographic operations. It defines extension functions related to XML Digital Signature, to encryption and decryption, and to hash and digest messages. It has been designed to be compatible with XQuery 1.0 and XSLT 2.0, as well as any other standard based on XPath 2.0.

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.

This proposal defines a set of XPath 2.0 extension functions to perform these cryptographic operations. It defines extension functions related to XML Digital Signature, to encryption and decryption, and to hash and digest messages. It has been designed to be compatible with XQuery 1.0 and XSLT 2.0, as well as any other standard based on XPath 2.0.

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 namespace http://expath.org/ns/error. In this document, the err prefix, when used, is bound to this namespace URI.

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).

HMAC: Keyed-Hashing for Message Authentication

HMAC is a mechanism for message authentication using cryptographic hash functions. HMAC can be used with any iterative cryptographic hash function, e.g. MD5 or SHA-1, 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.

The crypto:hmac function

This function generates a message authentication code, based on the input message, by using a cryptographic algorithm and a secret key.

XML Digital Signature

The XML Signature is a method of associating a key with referenced data (octets); it does not normatively specify how keys are associated with persons or institutions, nor the meaning of the data being referenced and signed. XML Signatures are applied to arbitrary digital content (data objects) via an indirection. Data objects are digested, the resulting value is placed in an element (with other information) and that element is then digested and cryptographically signed.

The crypto:generate-signature function

This function has four signatures, as one have to consider that some applications require the ability to specify a subset of a given XML document as the information content to be signed (this is done by using XPath to indicate the subset), and also the ability to use a digital certificate to obtain the key pair, instead of an auto-generated key pair.

The first signature considers the case of auto-generated key pair, and signature for the whole document.

The second signature considers the case of auto-generated key pair, and signature for a subset of the document.

The third signature considers the case of key pair generated from an digital certificate, and signature for the whole document.

The fourth signature considers the case of key pair generated from an digital certificate, and signature for a subset of the document.

The crypto:validate-signature function

This function validates an XML Digital Signature.

The crypto:generate-self-signed-certificate function

This function generates, for development purposes, a self signed digital certificate. TODO: To be developed, if such function is of any use.

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 an input string.

The crypto:decrypt function

This function decrypts an input string.

Summary of Error Conditions

err:CX01
The canonicalization algorithm is not supported.
err:CX02
The digest algorithm is not supported.
err:CX03
The signature algorithm is not supported.
err:CX04
The XPath expression is invalid.
err:CX05
The root element of argument $digital-certificate must have the name 'digital-certificate'.
err:CX06
The child element of argument $digital-certificate having position $position must have the name $child-element-name.
err:CX07
The keystore is null.
err:CX08
I/O error while reading keystore.
err:CX09
Permission denied to read keystore.
err:CX10
The keystore URL is invalid.
err:CX11
The keystore type is not supported.
err:CX12
Cannot find key for alias in given keystore.
err:CX13
The hashing algorithm is not supported.
err:CX14
The encoding method is not supported.
err:CX15
Cannot find Signature element.
err:CX16
No such padding.
err:CX17
Incorrect padding.
err:CX18
The encryption type is not supported.
err:CX19
The secret key is invalid.
err:CX20
Illegal block size.
err:CX21
The algorithm is not supported.
err:CX22
The decryption type is not supported.