Main Page » XQuery » Functions » Cryptographic Functions

Cryptographic Functions

This module contains functions to perform cryptographic operations in XQuery. The cryptographic module is based on an early draft of the EXPath Cryptographic Module and provides the following functionality: creation of message authentication codes (HMAC), encryption and decryption, and creation and validation of XML Digital Signatures.

Conventions

All functions in this module are assigned to the http://expath.org/ns/crypto namespace, which is statically bound to the crypto prefix.

All errors are assigned to the http://expath.org/ns/error namespace, which is statically bound to the experr prefix.

Message Authentication

crypto:hmac

Signature
crypto:hmac(
  $data       as xs:anyAtomicType,
  $key        as xs:anyAtomicType,
  $algorithm  as xs:string,
  $encoding   as xs:string?        := ()
) as xs:string
SummaryCreates an authentication code for the specified $data via a cryptographic hash function:
  • $key must not be empty.
  • $algorithm describes the hash algorithm which is used for encryption. Currently supported are md5, sha1, sha256, sha384, sha512. Default is md5.
  • $encoding must either be hex or base64; it specifies the encoding of the returned authentication code. Default is base64.
Errors
CX0013The hashing algorithm is not supported.
CX0014The encoding method is not supported.
CX0019The secret key is invalid.
Examples
crypto:hmac('message', 'secretkey', 'md5', 'hex')
Return the message authentication code 34D1E3818B347252A75A4F6D747B21C2.

Encryption & Decryption

The encryption and decryption functions underlie several limitations:

  • Cryptographic algorithms are currently limited to symmetric algorithms. This means that the same secret key is used for encryption and decryption.
  • Available algorithms are DES and AES.
  • Padding is fixed to PKCS5Padding.
  • The result of an encryption using the same message, algorithm and key looks different each time it is executed. This is due to a random initialization vector (IV) which is appended to the message and simply increases security.
  • As the IV has to be passed along with the encrypted message somehow, data which has been encrypted by the crypto:encrypt function in BaseX can only be decrypted by calling the crypto:decrypt function.

crypto:encrypt

Signature
crypto:encrypt(
  $data       as xs:anyAtomicType,
  $type       as xs:string,
  $key        as xs:anyAtomicType,
  $algorithm  as xs:string
) as xs:base64Binary
SummaryEncrypts data with the specified key:
  • $data must be a string or binary item.
  • $type must be symmetric.
  • $key is the secret key which is used for both encryption and decryption of input data. It must be a string or binary item. Its length is fixed and depends on the chosen algorithm: 8 bytes for DES, 16 bytes for AES.
  • $algorithm must either be DES or AES. Default is DES.
Errors
CX0016No such padding.
CX0017Incorrect padding.
CX0018The encryption type is not supported.
CX0019The secret key is invalid.
CX0020Illegal block size.
CX0021The algorithm is not supported.
Examples
crypto:encrypt('message', 'symmetric', 'keykeyke', 'DES')
Encrypt input data.

crypto:decrypt

Signature
crypto:decrypt(
  $data       as xs:anyAtomicType,
  $type       as xs:string,
  $key        as xs:anyAtomicType,
  $algorithm  as xs:string
) as xs:string
SummaryEncrypts data with the specified key:
  • $data must be a string or binary item.
  • $type must be symmetric.
  • $key is the secret key which is used for both encryption and decryption of input data. It must be a string or binary item. Its length is fixed and depends on the chosen algorithm: 8 bytes for DES, 16 bytes for AES.
  • $algorithm must either be DES or AES. Default is DES.
Errors
CX0016No such padding.
CX0017Incorrect padding.
CX0018The encryption type is not supported.
CX0019The secret key is invalid.
CX0020Illegal block size.
CX0021The algorithm is not supported.
Examples
let $encrypted := crypto:encrypt('message', 'symmetric', 'keykeyke', 'DES')
return crypto:decrypt($encrypted, 'symmetric', 'keykeyke', 'DES')
Decrypts the input and returns message.

XML Signatures

XML Signatures are used to sign data. In our case, the data which is signed is an XQuery node. The following example shows the basic structure of an XML signature. XML Signature
<Signature>
  <SignedInfo>
    <CanonicalizationMethod/>
    <SignatureMethod/>
    <Reference>
      <Transforms/>
      <DigestMethod/>
      <DigestValue/>
    </Reference>
    <Reference/>
  </SignedInfo>
  <SignatureValue/>
  <KeyInfo/>
  <Object/>
</Signature>
  • SignedInfo contains or references the signed data and lists algorithm information
  • Reference references the signed node
  • Transforms contains transformations (i.e. XPath expressions) that are applied to the input node in order to sign a subset
  • DigestValue holds digest value of the transformed references
  • SignatureValue contains the Base64 encoded value of the encrypted digest of the SignedInfo element
  • KeyInfo provides information on the key that is used to validate the signature
  • Object contains the node which is signed if the signature is of type enveloping
Signature Types

Depending on the signature type, the signature element is either placed as a child of the signed node (enveloped type), or directly contains the signed node (enveloping type). Detached signatures are so far not supported.

Digital Certificate

The generate-signature function allows to pass a digital certificate. This certificate holds parameters that allow to access key information stored in a Java key store which is then used to sign the input document. Passing a digital certificate simply helps re-using the same key pair to sign and validate data. The digital certificate is passed as a node and has the following form:

<digital-certificate>
  <keystore-type>JKS</keystore-type>
  <keystore-password>...</keystore-password>
  <key-alias>...</key-alias>
  <private-key-password>...</private-key-password>
  <keystore-uri>...</keystore-uri>
</digital-certificate>

crypto:generate-signature

Signature
crypto:generate-signature(
  $input             as node(),
  $canonicalization  as xs:string,
  $as                as xs:string,
  $digest            as xs:string,
  $signature         as xs:string,
  $prefix            as xs:string,
  $type              as xs:string,
  $ext1              as item(),
  $ext2              as node()
) as node()
Summary$canonicalization must either be inclusive-with-comments, inclusive, exclusive-with-comments or exclusive. **Default is inclusive-with-comments**. $digest must be one of the following: SHA1, SHA256 or SHA512. **Default is SHA1**. $signature must either be RSA_SHA1 or DSA_SHA1. **Default is RSA_SHA1**. $prefix may be empty and prefixes the Signature element accordingly. $type is the signature type. It must either be enveloped or enveloping (detached signatures are not supported so far). **Default is enveloped**. $ext1 may either be an $xpath expression or a $certificate.

If $ext2 is specified as well, $ext1 is an arbitrary XPath expression which specifies a subset of the document that is to be signed, and $ext2 is the digitial certificate used to sign the input document.

Errors
CX0001The canonicalization algorithm is not supported.
CX0002The digest algorithm is not supported.
CX0003The signature algorithm is not supported.
CX0004The XPath expression is invalid.
CX0005The root element of argument $digital-certificate must have the name 'digital-certificate'.
CX0007The keystore is null.
CX0012Cannot find key for alias in given keystore.
CX0023An invalid certificate alias is specified. Added to the official specification.
CX0024The algorithm is invalid. Added to the official specification.
CX0025Signature cannot be processed. Added to the official specification.
CX0026Keystore cannot be processed. Added to the official specification.
CX0027An I/O Exception occurred. Added to the official specification.
CX0028The specified signature type is not supported. Added to the official specification.

crypto:validate-signature

Signature
crypto:validate-signature(
  $input-doc  as node()
) as xs:boolean
SummaryChecks if the given node contains a Signature element and whether the signature is valid. In this case true is returned. If the signature is invalid the function returns false.
Errors
CX0015Cannot find Signature element.
CX9994
CX9996

Errors

CodeDescription
CX0001The canonicalization algorithm is not supported.
CX0002The digest algorithm is not supported.
CX0003The signature algorithm is not supported.
CX0004The XPath expression is invalid.
CX0005The root element of argument $digital-certificate must have the name 'digital-certificate'.
CX0006The child element of argument $digital-certificate having position $position must have the name $child-element-name.
CX0007The keystore is null.
CX0008I/O error while reading keystore.
CX0009Permission denied to read keystore.
CX0010The keystore URL is invalid.
CX0011The keystore type is not supported.
CX0012Cannot find key for alias in given keystore.
CX0013The hashing algorithm is not supported.
CX0014The encoding method is not supported.
CX0015Cannot find Signature element.
CX0016No such padding.
CX0017Incorrect padding.
CX0018The encryption type is not supported.
CX0019The secret key is invalid.
CX0020Illegal block size.
CX0021The algorithm is not supported.
CX0023An invalid certificate alias is specified. Added to the official specification.
CX0024The algorithm is invalid. Added to the official specification.
CX0025Signature cannot be processed. Added to the official specification.
CX0026Keystore cannot be processed. Added to the official specification.
CX0027An I/O Exception occurred. Added to the official specification.
CX0028The specified signature type is not supported. Added to the official specification.

Changelog

Version 9.3Version 7.0
  • Added: New module added.

⚡Generated with XQuery