An .epub file is a ZIP archive laid out
by the Open Container Format (OCF), part of the EPUB specification. The same rules have
applied, with small changes, since EPUB 2 (OCF 2.0.1, 2010); this page follows EPUB 3.3
(W3C, 2023) and notes where EPUB 2 differs.
ZIP rules
- Each file is stored (method 0) or compressed with Deflate (method 8); no other method is allowed.
- ZIP encryption is not allowed; EPUB has its own, in
encryption.xml. - The archive may not be split or spanned over several files.
- ZIP64 may be used, but only when the archive needs it.
- File names are UTF-8.
- The “version needed to extract” field is 10, 20 or 45 (1.0, 2.0 or 4.5).
The mimetype file
The first entry in the archive must be a file named mimetype, stored
without compression, unencrypted and with no extra field, whose content is exactly the
20 ASCII bytes application/epub+zip, with no line end, padding or byte
order mark. Because its position and length are fixed, the bytes from offset 30 of
every valid EPUB read mimetypeapplication/epub+zip, and a program can
recognise the format from them without unzipping.
00000000 50 4B 03 04 0A 00 00 00 00 00 xx xx xx xx xx xx PK..............
00000010 xx xx 14 00 00 00 14 00 00 00 08 00 00 00 6D 69 ..............mi
00000020 6D 65 74 79 70 65 61 70 70 6C 69 63 61 74 69 6F metypeapplicatio
00000030 6E 2F 65 70 75 62 2B 7A 69 70 50 4B 03 04 … n/epub+zipPK..
| Offset | Bytes | ZIP field | Value in an EPUB |
|---|---|---|---|
0 | 50 4B 03 04 | local file header signature | PK\3\4 |
4 | 0A 00 | version needed | 10 (often 20) |
6 | 00 00 | flags | 0: no encryption and no data descriptor |
8 | 00 00 | method | 0, stored |
10 | 4 bytes | time and date | any |
14 | 4 bytes | CRC-32 | the CRC of the 20 bytes |
18 | 14 00 00 00 | compressed size | 20 |
22 | 14 00 00 00 | uncompressed size | 20 |
26 | 08 00 | name length | 8 |
28 | 00 00 | extra field length | 0, required |
30 | mimetype | file name | |
38 | application/epub+zip | file data | |
58 | 50 4B 03 04 | next entry | usually META-INF/container.xml |
File names and paths
- Paths and names are case-sensitive, and the folder separator is
/. - A file name is at most 255 bytes of UTF-8; a full path at most 65,535 bytes.
- A name must not contain
/"*:<>?\|or DEL (U+007F), C0 or C1 control characters, private-use characters or Unicode non-characters, and must not end with a full stop. - Two names in the same folder must not be the same after Unicode canonical normalisation and then full case folding (EPUB 3.3 section 4.2.3). The explorer follows the “Unicode canonical case fold” step of Character Model for the Web: String Matching (3.2.2.3): NFC, full case folding with the C and F mappings of CaseFolding.txt (Unicode 18.0.0: 1,501 common and 105 full mappings, so “ß”, “ẞ” and “ss” are one name, as are “fi” and “fi”), then NFC again. Characters not in that file fold to themselves. EPUB 2 (OCF 2.0.1) says only “case normalization” (UTR 21) and that names differing only in case are disallowed; the full folding is the stricter and later rule and covers it.
- Names should avoid spaces, which some older reading systems mishandle.
The META-INF folder
The root of the container must hold a folder META-INF. Its files are
reserved; they are never listed in the package manifest and the book’s content must not
link to them.
| File | Status | Content |
|---|---|---|
container.xml | Required | Where the package document is: see below. |
encryption.xml | Optional; required when any file is encrypted or a font is obfuscated | Which files are encrypted and how: see encryption. |
signatures.xml | Optional | XML Digital Signatures over files in the container. |
rights.xml | Optional | Digital rights information, in a format the specification does not define (DRM systems such as Adobe’s put their licence here). OCF 2.0.1 (3.5.6) says it must be well-formed XML that conforms to XML Namespaces, its contents should use namespace-qualified elements, and it must never be encrypted; the explorer checks all three (an unqualified element is a recommendation). |
metadata.xml | Optional | Metadata about the container as a whole. EPUB 3.3 (4.2.6.3.4) says it should use only namespace-qualified elements and should contain the root element metadata in the namespace http://www.idpf.org/2013/metadata (the form the Multiple-Rendition note defines), but allows other root elements for backwards compatibility; an EPUB 2 book often has an Atom entry there. The explorer applies the Multiple-Rendition rules only when the root is that metadata or the book has several renditions. OCF 2.0.1 (3.5.3) likewise says the contents must be valid XML with namespace-qualified elements and that the file must not be encrypted, and defines no container-level metadata vocabulary of its own; an element without a namespace is reported as a recommendation. |
manifest.xml | Optional | A manifest of the container, kept for OpenDocument compatibility. EPUB 3.3 (4.2.6.3.3) says OCF mandates no format for it and reading systems do not use it. OCF 2.0.1 (3.5.2) says the content must be as the ODF 1.0 manifest schema: a root manifest:manifest in urn:oasis:names:tc:opendocument:xmlns:manifest:1.0 holding one or more manifest:file-entry elements (attributes full-path and media-type required, size a non-negative integer), each with at most one manifest:encryption-data (attributes checksum-type and a base64 checksum; exactly one empty manifest:algorithm with algorithm-name and a base64 initialisation-vector, and exactly one empty manifest:key-derivation with key-derivation-name, a base64 salt and a non-negative iteration-count). The explorer reports a departure from that schema as a problem in an EPUB 2 book and as a recommendation in an EPUB 3 book (schema read from OpenDocument-manifest-schema-v1.0-os.rng). |
A reading system may also find files such as com.apple.ibooks.display-options.xml
here, added by Apple and other vendors for their own settings; the specification does
not define them.
container.xml
The file uses the namespace
urn:oasis:names:tc:opendocument:xmlns:container.
| Element | Attributes | Content |
|---|---|---|
container | version: required, 1.0 | One rootfiles, then at most one links. |
rootfiles | One or more rootfile. | |
rootfile | full-path: required, the path of a package document from the container root, with no leading /. media-type: required, application/oebps-package+xml. | Empty. |
links | One or more link. | |
link | href: required. rel: required, the relationship. media-type: required when the target is inside the container. | Empty. Links resources about the whole container. |
The first rootfile is the default rendition. Several rootfiles make a
multiple-rendition publication (for example a fixed-layout and a reflowable version of
one book), described by the separate EPUB Multiple-Rendition Publications note; most
reading systems use only the first.
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
encryption.xml
The root element is encryption in the same container namespace. It
holds, in any order, EncryptedKey and EncryptedData elements
from W3C XML Encryption (namespace http://www.w3.org/2001/04/xmlenc#),
and may hold signatures.
| Element | Meaning |
|---|---|
enc:EncryptedData | One per encrypted file. |
enc:EncryptionMethod Algorithm="…" | The algorithm, as a URI: for example http://www.w3.org/2001/04/xmlenc#aes128-cbc, or the font obfuscation URIs below. |
ds:KeyInfo | Which key, often by name or by reference to an EncryptedKey. |
enc:CipherData / enc:CipherReference URI="…" | The encrypted file, by its path in the container. |
enc:EncryptionProperties / enc:EncryptionProperty / Compression | Optional, namespace http://www.idpf.org/2016/encryption#compression: Method 0 or 8 and OriginalLength, when the file was deflated before it was encrypted. |
These files must never be encrypted: mimetype, the
META-INF files listed above, and the package documents. Text-like files
are deflated before they are encrypted; already compressed media (JPEG, MP3 and so on)
should not be. Once encrypted, a file is a stream of random-looking bytes that Deflate
cannot shrink: EPUB 3.3 (4.2.6.3.2.1) says creators should store encrypted files
in the ZIP directory rather than deflate them. The explorer reports an encrypted file that is
also Deflated as a recommendation.
Opening encrypted files
OCF defines where the encryption is described, not how a reader gets the key: the key is never in the file in the clear, and the
specification’s own example wraps the data key for a named person (an EncryptedKey with the RSA 1.5 algorithm and a
KeyName). Two ways of getting the key are defined by XML Encryption 1.1 itself, and the explorer offers both in its options bar:
- A key derived from a password. The
EncryptedKeyuses an AES key wrap (kw-aes128,kw-aes192orkw-aes256) and itsKeyInfoholds aDerivedKeywith the PBKDF2 method: a salt, an iteration count, a key length and a PRF (HMAC with SHA-1, 256, 384 or 512). The reader types the password; the key encryption key is derived with PBKDF2, the data key is unwrapped (the key wrap’s integrity check says at once whether the password was right) and the files are decrypted. - A symmetric key. The reader gives a key in hex: it unwraps an AES-wrapped data key named by
KeyName, or it is the data key itself for anEncryptedDatathat names no key.
The data ciphers are AES-128, 192 and 256 in CBC (the first 16 bytes are the IV; the last plain byte counts the padding), AES in GCM (a 12-byte IV, a 16-byte tag that authenticates the data) and Triple DES in CBC. A file that was deflated before it was
encrypted (the Compression property) is inflated after decryption. An EncryptedKey encrypted for a public key (RSA 1.5, RSA-OAEP, ECDH) needs the recipient’s private key, which the explorer
cannot take; it names the recipient and says so. Then what is missing is the key, not support. AES-CBC has no integrity check, so with a wrong data key given directly the padding is wrong about 94 times in 100 and the file is refused, and otherwise the output is garbage; the key-wrap and GCM routes always tell.
The Container tab lists every EncryptedData (algorithm, key, cipher data, properties, whether it opens) and every EncryptedKey (algorithm and its parameters, recipient, key names, derived-key parameters, what it protects) in full.
signatures.xml
The root is signatures in the container namespace, holding one or more W3C XML Signature Signature elements (namespace http://www.w3.org/2000/09/xmldsig#) that sign files of the container, or other
data, by Reference. The explorer verifies each signature in the browser with WebCrypto and says one of four things:
| Result | Meaning |
|---|---|
| verified | Every Reference digest (SHA-1, SHA-256, SHA-384 or SHA-512) equals the digest of the data it names, after its transforms (enveloped-signature, XPath, XPath Filter 2.0, the Decryption Transform in XML and binary mode (with the Key option, the data key or the key-wrap key of an EncryptedKey, when something is to be decrypted), Canonical XML 1.0 and 1.1, Exclusive XML Canonicalization, each with or without comments, base64), and the SignatureValue is valid over the canonicalised SignedInfo for the public key of the embedded X.509 certificate (RSA PKCS#1 v1.5, RSASSA-PSS or ECDSA on P-256, P-384 or P-521, with SHA-1 to SHA-512), or for an RSAKeyValue. |
| digest mismatch | A signed file or element no longer has the digest in the file: it was changed after signing (or canonicalised in a way this reader does not do). |
| signature invalid | The digests match but the signature value does not verify for the certificate’s key. |
| cannot be checked | An algorithm or transform this reader does not do (an XSLT transform whose output is text or whose stylesheet is missing or calls document(); a Decryption Transform that has something to decrypt when no Key option is given, an EncryptedKey for a public key, which needs a private key, or a CipherReference to a file outside the container; an AES-wrapped EncryptedKey, with the Key option as the wrapping key, and a CipherReference to a file of the container with its transforms are handled), a key named but not supplied (an HMAC signature needs its shared secret: give it as the Key option), or a signed file that is not in the container; the reason is given for each reference. |
What EPUB 3.3 (4.2.6.3.6.1) asks of signatures:
- A creator can sign any or all files of the container, except
signatures.xml, which holds the signatures. Signing it only works with the enveloped signature transform (which removes the signature from the data before the digest), and a creator who wants signatures to be added or removed without breaking this one should not signsignatures.xml. The explorer reports aReferenceto it without the enveloped transform. - A signature over the container as a whole should be added as the last
Signaturechild ofsignatures, so that earlier ones are inside what it signs. - A signed resource may afterwards be encrypted (may), if the signature uses the Decryption Transform for XML Signature (a W3C specification of its own; EPUB 3.3 only names it):
it lets a reading system tell data that was encrypted before signing from data encrypted after. The explorer applies the XML mode (
http://www.w3.org/2002/07/decrypt#XML):dcrpt:Exceptelements (a bare name or#xpointer(...); one that finds nothing is ignored) exclude data, and when noEncryptedDatais left in the node-set the transform changes nothing. When one is left, decrypting it needs a key that a signatures file does not hold, and the binary mode (decrypt#Binary) needs one too, so those references are “cannot be checked”, with the reason. - The signature algorithms the explorer checks: RSA PKCS#1 v1.5, RSASSA-PSS (RFC 6931 2.3.9:
rsa-psswithRSAPSSParams, whose digest defaults to SHA-256, salt to the hash length and trailer to 1; and 2.3.10:sha1,sha256,sha384andsha512-rsa-MGF1; checked on signatures made by OpenSSL; MD5, SHA-224 and SHA-3 variants, an MGF1 hash other than the digest and a trailer other than 1 are named as not checked, because WebCrypto cannot do them) and ECDSA with the key of an embedded certificate or anRSAKeyValue(called a bare key: the file does not say whose it is), DSA (SHA-1 and SHA-256; a 2048-bit DSA signature made by OpenSSL was verified) and HMAC with a secret. An ECDSA value must be the two integers r and s side by side (XML Signature 1.1); a DER-encoded one, which OpenSSL and many libraries write, is reported as a problem even when it is valid for the key.
“Verified” does not mean trusted. The explorer checks the certificates of the signature against one another, as RFC 5280 describes: each certificate’s signature by the next one in the chain, validity dates, that an issuer is a CA (basicConstraints, keyUsage keyCertSign, pathLenConstraint), name constraints (directoryName, DNS, email, URI and IP forms) and certificate policies with policy mappings and the explicit-policy, policy-mapping and anyPolicy limits (RFC 5280 6.1; a self-issued top certificate is the trust anchor), the key identifiers, and that X509SubjectName, X509SKI, X509IssuerSerial and dsig11:X509Digest describe the certificate that holds the key (XML Signature 1.1 4.5.4); a KeyName is shown as a label, because it carries no key (4.5.1). It does not check that the top of the chain is a trusted root (there is no trust store), nor revocation, nor whether the signer is the right person; it shows the subject, issuer, key type and validity dates (and marks an expired one) so you can judge it. The chain checks are tested against chains made by OpenSSL (a missing intermediate, a non-CA issuer, a missing keyCertSign, a broken pathLenConstraint, an expired certificate, ECDSA- and PSS-signed certificates), and OpenSSL’s own verify reports the same path-length fault.
The canonicalisation is the one of the Canonical XML recommendations, including the general entities a DOCTYPE declares (replaced where used) and the Canonical XML 1.1 xml:base fix-up (checked against the examples of the recommendation and RFC 3986, not against another program); it is tested against xmllint, which agrees on every document tried (75 checks).
Font obfuscation
EPUB lets a publisher scramble embedded fonts slightly, so that a font cannot be pulled out of a book and installed as it is. It is not encryption: anyone with the book can undo it.
- Take the publication’s unique identifier: the
dc:identifiernamed by the package’sunique-identifierattribute. - Remove every space (U+0020), tab (U+0009), carriage return (U+000D) and line feed (U+000A).
- Encode it as UTF-8 and take its SHA-1 hash: a 20-byte key.
- XOR the first 1,040 bytes of the font file with the key repeated 52 times; leave the rest unchanged. A font shorter than 1,040 bytes is XORed whole.
- Then deflate the result, if compressing, and store it in the ZIP.
Each obfuscated font has an EncryptedData entry with
Algorithm="http://www.idpf.org/2008/embedding". The key is never written
in the file. Reading systems undo it in the reverse order.
Adobe defined an earlier scheme, still found in many EPUB 2 books, with the algorithm
URI http://ns.adobe.com/pdf/enc#RC: the key is the 16 bytes of the
identifier read as a UUID (its hex digits, without urn:uuid: and hyphens),
and only the first 1,024 bytes are XORed. It is not part of the EPUB
specification.
Differences in EPUB 2
The mimetype file, the META-INF files and
container.xml work the same way in EPUB 2 (OCF 2.0.1); the
links element of container.xml and the compression property
of encryption.xml were added later. An EPUB 2 and an EPUB 3 file are told
apart only by the version attribute of the package document, not by the
container.
OCF 2.0.1 3.5.1 lets container.xml list alternate rootfiles of other media
types (its example is a PDF). In an EPUB 2 book such a rootfile is accepted and not read
as a package; in an EPUB 3 book EPUB 3.3 4.1.1 requires every rootfile to be
application/oebps-package+xml, so it is reported. Either way at least one
rootfile of the package type is required, and an unqualified attribute that the container
schema (OCF 2.0.1 Appendix A) does not define is reported.
OCF 2.0.1 3.3 forbids only / " * : < > ? \ and a final full stop in a
file name. The longer list in EPUB 3.3 4.2.3 (for example |) is reported as a
recommendation for an EPUB 2 book and as a problem for an EPUB 3 book.