<li>Make use of existing protocols for end-to-end encryption (&xep0373; and &xep0420;)</li>
<li>Reuse existing protocols for the actual transport of the data</li>
<li>Allow caching and forwarding without being required to decrypt the file</li>
<li>Backwards compatibility with existing, widely-deployed protocols<note>There is a widely-deployed protocol for encrypted file sharing known as "OMEMO media sharing" or "aesgcm-links" that was never accepted as a XEP. While backwards compatibility with such non-standard is not a maxime of the XSF, it was still considered during the design of this protocol.</note></li>
</ul>
</section1>
<section1topic='Use Cases'anchor='usecases'>
<p>
This protocol is only meaningful for end-to-end encrypted file sharing when transported as end-to-end encrypted XML, like it's possible using &xep0420;.
However, usage without such end-to-end encryption still has its usecase, as it allows sharing files through untrusted intermediaries for as long as the intermediary XMPP servers, if any, are trusted.
</p>
<pclass='box'>Note: To make the examples in this document more readable, no end-to-end encryption is used.</p>
<section2topic='Sharing a file'anchor='file-sharing'>
<p>
Before sharing the file, the sending entity MUST create random symmetric private key and initialization vector (IV) as required by the selected encryption cipher (see <linkurl='#ciphers'>Ciphers</link>). The file is then encrypted using selected encryption cipher and the generated key and IV. After this it can be uploaded using &xep0363; or prepared for any other means of file sharing.
The <tt><file/></tt> metadata element still refers to the original file, i.e. it describes the original file name, size and hashes. The <tt><size/></tt> element and one or multiple <tt><hash/></tt> elements are REQUIRED when sending encrypted files.
For the encrypted file, a source is added as an <tt><encrypted/></tt> element to the <tt><sources/></tt>. It carries an attribute <tt>cipher</tt> with the namespace of the encryption cipher being used. The <tt><encrypted/></tt> element contains a <tt><key/></tt> and an <tt><iv/></tt> element, containing both values as Base64-encoded strings. The <tt><encrypted/></tt> element MAY also include <tt><hash/></tt> elements as described in &xep0300;, referring to the hash of the encrypted file. At last, the <tt><encrypted/></tt> element also includes another <tt><sources/></tt> element as described in &xep0447;, specifying sources to obtain the encrypted file.
The outer <tt><sources/></tt> may contain additional sources that directly allow for end-to-end encrypted file transfers, for example &xep0234; using &xep0391;.
</p>
<examplecaption='Sharing summit.jpg with juliet@shakespeare.lit using encryption'><![CDATA[
<p>On receive of a message including a <tt><file-sharing/></tt> element, that has an <tt><encrypted/></tt> element in its sources, normal processing as described in &xep0447; applies.</p>
When the receiving entity tries to obtain the file from the source described by the <tt><encrypted/></tt> element, it will try to obtain any of its inner sources instead.
On success, it decrypts the obtained file using the encryption cipher, private key and IV provided.
If the resulting file is larger than the number of bytes specified in the <tt><size/></tt> metadata element, the additional bytes are cut off.
</p>
</section2>
<section2topic='Attaching a source'anchor='attach-source'>
<p>For compatibility reasons, it is RECOMMENDED to append the GCM authentication tag to the uploaded file when using any AES cipher with GCM. The GCM authentication tag is not needed when using the protocol described in this document as a hash of the resulting file is transported independently.</p>