1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
9 //========================================================================
11 // Modified under the Poppler project - http://poppler.freedesktop.org
13 // All changes made under the Poppler project to this file are licensed
14 // under GPL version 2 or later
16 // Copyright (C) 2008 Julien Rebetez <julien@fhtagn.net>
17 // Copyright (C) 2009 David Benjamin <davidben@mit.edu>
18 // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
19 // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
20 // Copyright (C) 2013 Albert Astals Cid <aacid@kde.org>
21 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
23 // To see a description of the changes please see the Changelog file that
24 // came with your tarball or type make ChangeLog if you are building from git
26 //========================================================================
31 #ifdef USE_GCC_PRAGMAS
35 #include "goo/gtypes.h"
36 #include "goo/GooString.h"
40 //------------------------------------------------------------------------
42 //------------------------------------------------------------------------
47 // Generate a file key. The <fileKey> buffer must have space for at
48 // least 16 bytes. Checks <ownerPassword> and then <userPassword>
49 // and returns true if either is correct. Sets <ownerPasswordOk> if
50 // the owner password was correct. Either or both of the passwords
51 // may be NULL, which is treated as an empty string.
52 static GBool
makeFileKey(int encVersion
, int encRevision
, int keyLength
,
53 GooString
*ownerKey
, GooString
*userKey
,
54 GooString
*ownerEnc
, GooString
*userEnc
,
55 int permissions
, GooString
*fileID
,
56 GooString
*ownerPassword
, GooString
*userPassword
,
57 Guchar
*fileKey
, GBool encryptMetadata
,
58 GBool
*ownerPasswordOk
);
62 static GBool
makeFileKey2(int encVersion
, int encRevision
, int keyLength
,
63 GooString
*ownerKey
, GooString
*userKey
,
64 int permissions
, GooString
*fileID
,
65 GooString
*userPassword
, Guchar
*fileKey
,
66 GBool encryptMetadata
);
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
73 /* DecryptRC4State, DecryptAESState, DecryptAES256State are named like this for
74 * historical reasons, but they're used for encryption too.
75 * In case of decryption, the cbc field in AES and AES-256 contains the previous
76 * input block or the CBC initialization vector (IV) if the stream has just been
77 * reset). In case of encryption, it always contains the IV, whereas the
78 * previous output is kept in buf. The paddingReached field is only used in
79 * case of encryption. */
80 struct DecryptRC4State
{
85 struct DecryptAESState
{
90 GBool paddingReached
; // encryption only
94 struct DecryptAES256State
{
99 GBool paddingReached
; // encryption only
103 class BaseCryptStream
: public FilterStream
{
106 BaseCryptStream(Stream
*strA
, Guchar
*fileKey
, CryptAlgorithm algoA
,
107 int keyLength
, int objNum
, int objGen
);
108 virtual ~BaseCryptStream();
109 virtual StreamKind
getKind() { return strCrypt
; }
110 virtual void reset();
111 virtual int getChar();
112 virtual int lookChar() = 0;
113 virtual Goffset
getPos();
114 virtual GBool
isBinary(GBool last
);
115 virtual Stream
*getUndecodedStream() { return this; }
116 void setAutoDelete(GBool val
);
122 Goffset charactersRead
; // so that getPos() can be correct
123 int nextCharBuff
; // EOF means not read yet
129 DecryptAES256State aes256
;
133 //------------------------------------------------------------------------
134 // EncryptStream / DecryptStream
135 //------------------------------------------------------------------------
137 class EncryptStream
: public BaseCryptStream
{
140 EncryptStream(Stream
*strA
, Guchar
*fileKey
, CryptAlgorithm algoA
,
141 int keyLength
, int objNum
, int objGen
);
143 virtual void reset();
144 virtual int lookChar();
147 class DecryptStream
: public BaseCryptStream
{
150 DecryptStream(Stream
*strA
, Guchar
*fileKey
, CryptAlgorithm algoA
,
151 int keyLength
, int objNum
, int objGen
);
153 virtual void reset();
154 virtual int lookChar();
157 //------------------------------------------------------------------------
159 extern void md5(Guchar
*msg
, int msgLen
, Guchar
*digest
);