Changes

Jump to navigation Jump to search
1,153 bytes removed ,  12:32, 8 May 2024
no edit summary
This [[Module Library|XQuery Module]] contains functions to perform cryptographic operations in XQuery.
The cryptographic module is based on an early draft of the [http://expath.org/spec/crypto /20110810 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 {{Code|<code><nowiki>http://expath.org/ns/crypto}} </nowiki></code> namespace, which is statically bound to the {{Code|crypto}} prefix.<br/>
All errors are assigned to the <code><nowiki>http://expath.org/ns/error</nowiki></code> namespace, which is statically bound to the {{Code|experr}} prefix.
==crypto:hmac==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|crypto:hmac|$message as xs:string, $key as xs:string, $algorithm as xs:string|xs:string}}<br/pre>{{Func|crypto:hmac|( $message data as xs:stringanyAtomicType, $key as xs:stringanyAtomicType, $algorithm as xs:string, $encoding as xs:string| := ()) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Creates a message an authentication code via a cryptographic hash function and a secret for the specified {{Code|$keydata}}. <br/>via a cryptographic hash function:* {{Code|$encodingkey}} must either not be {{Code|hex}}, {{Code|base64}} or the empty string and specifies the encoding of the returned authentication code. '''Default is {{Code|base64}}'''.<br/>* {{Code|$algorithm}} describes the hash algorithm which is used for encryption. Currently supported are {{Code|md5}}, {{Code|sha1}}, {{Code|sha256}}, {{Code|sha384}}, {{Code|sha512}}. '''Default is {{Code|md5}}'''.* {{Code|$encoding}} must either be {{Code|hex}} or {{Code|base64}}; it specifies the encoding of the returned authentication code. Default is {{Code|base64}}.|-valign="top"
| '''Errors'''
|{{Error|CX0013|XQuery Errors#Cryptographic Functions Errors}} the specified hashing algorithm is not supported.<br/>{{Error|CX0014|XQuery Errors#Cryptographic Functions Errors}} the specified encoding method is not supported.<br/>{{Error|CX0019|XQuery Errors#Cryptographic Functions Errors}} the specified secret key is invalid.<br/>|-valign="top"
| '''Example'''
|'''Returns the Return message authentication code (MAC) for a given string.''':
'''Query:'''
<pre classlang="brush:'xquery"'>crypto:hmac('message','secretkey','md5','base64hex')
</pre>
'''Result:'''
<pre classlang="brush:xml">
34D1E3818B347252A75A4F6D747B21C2
</pre>
=Encryption & Decryption=
 
The encryption and decryption functions underlie several limitations:
* Cryptographic algorithms are currently limited to {{Code|symmetric}} algorithms only. This means that the same secret key is used for encryption and decryption.
* Available algorithms are {{Code|DES}} and {{Code|AES}}.
* Padding is fixed to {{Code|PKCS5Padding}}.
==crypto:encrypt==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>crypto:encrypt|( $input data as xs:stringanyAtomicType, $encryption type as xs:string, $key as xs:stringanyAtomicType, $algorithm as xs:string|) as xs:string}}base64Binary</pre>|-valign="top"
| '''Summary'''
|Encrypts data with the given input string.<br/>specified key:* {{Code|$encryptiondata}} must be a string or binary item.* {{Code|symmetric$type}}, as asymmetric encryption is not supported so far. '''Default is must be {{Code|symmetric}}'''.<br/>* {{Code|$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 {{Code|8 bytes for DES}}, 16 bytes for {{Code|16 bytes for AES}}.<br/>* {{Code|$algorithm}} must either be {{Code|DES}} or {{Code|AES}}. Other algorithms are not supported so far, but, of course, can be added on demand. '''Default is {{Code|DES}}'''.|-valign="top"
| '''Errors'''
|{{Error|CX0016|XQuery Errors#Cryptographic Functions Errors}} padding problems arise.<br/>{{Error|CX0017|XQuery Errors#Cryptographic Functions Errors}} padding is incorrect.<br/>{{Error|CX0018|XQuery Errors#Cryptographic Functions Errors}} the encryption type is not supported.<br/>{{Error|CX0019|XQuery Errors#Cryptographic Functions Errors}} the secret key is invalid.<br/>{{Error|CX0020|XQuery Errors#Cryptographic Functions Errors}} the block size is incorrect.<br/>{{Error|CX0021|XQuery Errors#Cryptographic Functions Errors}} the specified encryption algorithm is not supported.<br/>|-valign="top"
| '''Example'''
|'''Encrypts Encrypt input data.''' '''Query:'''<pre classlang="brush:'xquery"'>crypto:encrypt('message', 'symmetric','keykeyke','DES')
</pre>
|}
==crypto:decrypt==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>crypto:decrypt|( $input data as xs:stringanyAtomicType, $type as xs:string, $key as xs:stringanyAtomicType, $algorithm as xs:string|) as xs:string}}</pre>|-valign="top"
| '''Summary'''
|Decrypts Encrypts data with the encrypted specified key:* {{Code|$inputdata}}must be a string or binary item.<br/>* {{Code|$type}} must be {{Code|symmetric}}. An option for asymmetric encryption will most likely be added with another version of BaseX. '''Default is {{Code|symmetric}}'''.<br/>* {{Code|$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 {{Code|8 bytes for DES}}, 16 bytes for {{Code|16 bytes for AES}}.<br/>* {{Code|$algorithm}} must either be {{Code|DES}} or {{Code|AES}}. Other algorithms are not supported so far, but, of course, can be added on demand. '''Default is {{Code|DES}}'''.|-valign="top"
| '''Errors'''
|{{Error|CX0016|XQuery Errors#Cryptographic Functions Errors}} padding problems arise.<br/>{{Error|CX0017|XQuery Errors#Cryptographic Functions Errors}} padding is incorrect.<br/>{{Error|CX0018|XQuery Errors#Cryptographic Functions Errors}} the encryption type is not supported.<br/>{{Error|CX0019|XQuery Errors#Cryptographic Functions Errors}} the secret key is invalid.<br/>{{Error|CX0020|XQuery Errors#Cryptographic Functions Errors}} the block size is incorrect.<br/>{{Error|CX0021|XQuery Errors#Cryptographic Functions Errors}} the specified encryption algorithm is not supported.<br/>|-valign="top"
| '''Example'''
|'''Decrypts Decrypt input data and returns the return original string.''':
'''Query:'''
<pre classlang="brush:'xquery"'>let $encrypted := crypto:encrypt('message', 'symmetric','keykeyke','DES')return crypto:decrypt($encrypted, 'symmetric','keykeyke','DES')
</pre>
'''Result:'''
<pre classlang="brush:xml">
message
</pre>
=XML Signatures=
 [httphttps://www.w3.org/TR/xmldsig-core/ 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'''
<pre classlang="brush:xml">
<Signature>
<SignedInfo>
The {{Code|generate-signature}} function allows to pass a {{Code|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 {{Code|digital certificate}} simply helps re-using the same key pair to sign and validate data. The {{Code|digital certificate}} is passed as a node and has the following form:
<pre classlang="brush:xml">
<digital-certificate>
<keystore-type>JKS</keystore-type>
==crypto:generate-signature==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|crypto:generate-signature|$input as node(), $canonicalization as xs:string, $digest as xs:string, $signature as xs:string, $prefix as xs:string, $type as xs:string|node()}}<br/pre>{{Func|crypto:generate-signature|( $input as node(), $canonicalization as xs:string, $digest as xs:string, $signature as xs:string, $prefix as xs:string, $type as xs:string, $xpath ext1 as xs:stringitem(), $certificate ext2 as node()|) as node()}}<br/pre>{{Func|crypto:generate-signature|$input as node(), $canonicalization as xs:string, $digest as xs:string, $signature as xs:string, $prefix as xs:string, $type as xs:string, $ext as item()|node()}}|-valign="top"
| '''Summary'''
|{{Code|$canonicalization}} must either be {{Code|inclusive-with-comments}}, {{Code|inclusive}}, {{Code|exclusive-with-comments}} or {{Code|exclusive}}. '''Default is {{Code|inclusive-with-comments}}'''.<br/>
{{Code|$prefix}} may be empty and prefixes the {{Code|Signature}} element accordingly.<br/>
{{Code|$type}} is the signature type. It must either be {{Code|enveloped}} or {{Code|enveloping}} (detached signatures are not supported so far). '''Default is {{Code|enveloped}}'''.<br/>
{{Code|$ext1}} may either be an {{Code|$xpath}} is an arbitrary XPath expression which specifies or a subset of the document that is to be signed.<br/>{{Code|$certificate}} is the digitial certificate used to sign the input document.<br/>If {{Code|$extext2}} may either be an is specified as well, {{Code|$xpathext1}} is an arbitrary XPath expression or which specifies a subset of the document that is to be signed, and {{Code|$certificateext2}}is the digitial certificate used to sign the input document.<br/>|-valign="top"
| '''Errors'''
|{{Error|CX0001|XQuery Errors#Cryptographic Functions Errors}} the canonicalization algorithm is not supported.<br/>{{Error|CX0002|XQuery Errors#Cryptographic Functions Errors}} the digest algorithm is not supported.<br/>{{Error|CX0003|XQuery Errors#Cryptographic Functions Errors}} the signature algorithm is not supported.<br/>{{Error|CX0004|XQuery Errors#Cryptographic Functions Errors}} the {{Code|$xpath-expression}} is invalid.<br/>{{Error|CX0005|XQuery Errors#Cryptographic Functions Errors}} the root name of {{Code|$digital-certificate}} is not 'digital-certificate.<br/>{{Error|CX0007|XQuery Errors#Cryptographic Functions Errors}} the key store is null.<br/>{{Error|CX0012|XQuery Errors#Cryptographic Functions Errors}} the key cannot be found in the specified key store.<br/>{{Error|CX0023|XQuery Errors#Cryptographic Functions Errors}} the certificate alias is invalid.<br/>{{Error|CX0024|XQuery Errors#Cryptographic Functions Errors}} an invalid algorithm is specified.<br/>{{Error|CX0025|XQuery Errors#Cryptographic Functions Errors}} an exception occurs while the signing the document.<br/>{{Error|CX0026|XQuery Errors#Cryptographic Functions Errors}} an exception occurs during key store initialization.<br/>{{Error|CX0027|XQuery Errors#Cryptographic Functions Errors}} an IO exception occurs.<br/>{{Error|CX0028|XQuery Errors#Cryptographic Functions Errors}} the signature type is not supported.<br/>|-valign="top"
| '''Example'''
|'''Generates an Generate [httphttps://www.w3.org/TR/xmldsig-core/ XML Signature].''':
'''Query:'''
<pre classlang="brush:'xquery"'>
crypto:generate-signature(<a/>, '', '', '', '', '')
</pre>
'''Result:'''
<pre classlang="brush:xml">
<a>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
==crypto:validate-signature==
 
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>crypto:validate-signature|( $input-doc as node()|) as xs:boolean}}</pre>|-valign="top"
| '''Summary'''
|Checks if the given node contains a {{Code|Signature}} element and whether the signature is valid. In this case {{Code|true}} is returned. If the signature is invalid the function returns {{Code|false}}.
|-valign="top"
| '''Errors'''
|{{Error|CX0015|XQuery Errors#Cryptographic Functions Errors}} the signature element cannot be found.<br/>{{Error|CX9994|XQuery Errors#Cryptographic Functions Errors}} an unspecified problem occurs during validation.<br/>{{Error|CX9996|XQuery Errors#Cryptographic Functions Errors}} an IO exception occurs during validation.<br/>|-| '''Example'''|'''Validates an [http://www.w3.org/TR/xmldsig-core/ XML Signature].''' '''Query:'''<pre class="brush:xquery">let $sig := crypto:generate-signature(<a/>, '', '', '', '', '')return crypto:validate-signature($sig)</pre> '''Result:'''<pre class="brush:xml">true</pre>
|}
! width="110"|Code
|Description
|-valign="top"
|{{Code|CX0001}}
|The canonicalization algorithm is not supported.
|-valign="top"
|{{Code|CX0002}}
|The digest algorithm is not supported.
|-valign="top"
|{{Code|CX0003}}
|The signature algorithm is not supported.
|-valign="top"
|{{Code|CX0004}}
|The XPath expression is invalid.
|-valign="top"
|{{Code|CX0005}}
|The root element of argument $digital-certificate must have the name 'digital-certificate'.
|-valign="top"
|{{Code|CX0006}}
|The child element of argument $digital-certificate having position $position must have the name $child-element-name.
|-valign="top"
|{{Code|CX0007}}
|The keystore is null.
|-valign="top"
|{{Code|CX0008}}
|I/O error while reading keystore.
|-valign="top"
|{{Code|CX0009}}
|Permission denied to read keystore.
|-valign="top"
|{{Code|CX0010}}
|The keystore URL is invalid.
|-valign="top"
|{{Code|CX0011}}
|The keystore type is not supported.
|-valign="top"
|{{Code|CX0012}}
|Cannot find key for alias in given keystore.
|-valign="top"
|{{Code|CX0013}}
|The hashing algorithm is not supported.
|-valign="top"
|{{Code|CX0014}}
|The encoding method is not supported.
|-valign="top"
|{{Code|CX0015}}
|Cannot find Signature element.
|-valign="top"
|{{Code|CX0016}}
|No such padding.
|-valign="top"
|{{Code|CX0017}}
|Incorrect padding.
|-valign="top"
|{{Code|CX0018}}
|The encryption type is not supported.
|-valign="top"
|{{Code|CX0019}}
|The secret key is invalid.
|-valign="top"
|{{Code|CX0020}}
|Illegal block size.
|-valign="top"
|{{Code|CX0021}}
|The algorithm is not supported.
|-valign="top"
|{{Code|CX0023}}
|An invalid certificate alias is specified. Added to the official specification.
|-valign="top"
|{{Code|CX0024}}
|The algorithm is invalid. Added to the official specification.
|-valign="top"
|{{Code|CX0025}}
|Signature cannot be processed. Added to the official specification.
|-valign="top"
|{{Code|CX0026}}
|Keystore cannot be processed. Added to the official specification.
|-valign="top"
|{{Code|CX0027}}
|An I/O Exception occurred. Added to the official specification.
|-valign="top"
|{{Code|CX0028}}
|The specified signature type is not supported. Added to the official specification.
=Changelog=
 
;Version 9.3
* Updated: {{Function||crypto:hmac}}, {{Function||crypto:encrypt}}, {{Function||crypto:decrypt}}: Function types revised.
 
;Version 8.6
* Updated: {{Function||crypto:hmac}}: The key can now be a string or a binary item.
The Module was introduced with Version 7.0.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu