]> git.ipfire.org Git - thirdparty/cups.git/blob - pdftops/SecurityHandler.h
Merge changes from CUPS 1.4svn-r7199.
[thirdparty/cups.git] / pdftops / SecurityHandler.h
1 //========================================================================
2 //
3 // SecurityHandler.h
4 //
5 // Copyright 2004 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef SECURITYHANDLER_H
10 #define SECURITYHANDLER_H
11
12 #include <config.h>
13
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17
18 #include "gtypes.h"
19 #include "Object.h"
20
21 class GString;
22 class PDFDoc;
23 struct XpdfSecurityHandler;
24
25 //------------------------------------------------------------------------
26 // SecurityHandler
27 //------------------------------------------------------------------------
28
29 class SecurityHandler {
30 public:
31
32 static SecurityHandler *make(PDFDoc *docA, Object *encryptDictA);
33
34 SecurityHandler(PDFDoc *docA);
35 virtual ~SecurityHandler();
36
37 // Check the document's encryption. If the document is encrypted,
38 // this will first try <ownerPassword> and <userPassword> (in
39 // "batch" mode), and if those fail, it will attempt to request a
40 // password from the user. This is the high-level function that
41 // calls the lower level functions for the specific security handler
42 // (requesting a password three times, etc.). Returns true if the
43 // document can be opened (if it's unencrypted, or if a correct
44 // password is obtained); false otherwise (encrypted and no correct
45 // password).
46 GBool checkEncryption(GString *ownerPassword,
47 GString *userPassword);
48
49 // Create authorization data for the specified owner and user
50 // passwords. If the security handler doesn't support "batch" mode,
51 // this function should return NULL.
52 virtual void *makeAuthData(GString *ownerPassword,
53 GString *userPassword) = 0;
54
55 // Construct authorization data, typically by prompting the user for
56 // a password. Returns an authorization data object, or NULL to
57 // cancel.
58 virtual void *getAuthData() = 0;
59
60 // Free the authorization data returned by makeAuthData or
61 // getAuthData.
62 virtual void freeAuthData(void *authData) = 0;
63
64 // Attempt to authorize the document, using the supplied
65 // authorization data (which may be NULL). Returns true if
66 // successful (i.e., if at least the right to open the document was
67 // granted).
68 virtual GBool authorize(void *authData) = 0;
69
70 // Return the various authorization parameters. These are only
71 // valid after authorize has returned true.
72 virtual int getPermissionFlags() = 0;
73 virtual GBool getOwnerPasswordOk() = 0;
74 virtual Guchar *getFileKey() = 0;
75 virtual int getFileKeyLength() = 0;
76 virtual int getEncVersion() = 0;
77
78 protected:
79
80 PDFDoc *doc;
81 };
82
83 //------------------------------------------------------------------------
84 // StandardSecurityHandler
85 //------------------------------------------------------------------------
86
87 class StandardSecurityHandler: public SecurityHandler {
88 public:
89
90 StandardSecurityHandler(PDFDoc *docA, Object *encryptDictA);
91 virtual ~StandardSecurityHandler();
92
93 virtual void *makeAuthData(GString *ownerPassword,
94 GString *userPassword);
95 virtual void *getAuthData();
96 virtual void freeAuthData(void *authData);
97 virtual GBool authorize(void *authData);
98 virtual int getPermissionFlags() { return permFlags; }
99 virtual GBool getOwnerPasswordOk() { return ownerPasswordOk; }
100 virtual Guchar *getFileKey() { return fileKey; }
101 virtual int getFileKeyLength() { return fileKeyLength; }
102 virtual int getEncVersion() { return encVersion; }
103
104 private:
105
106 int permFlags;
107 GBool ownerPasswordOk;
108 Guchar fileKey[16];
109 int fileKeyLength;
110 int encVersion;
111 int encRevision;
112 GBool encryptMetadata;
113
114 GString *ownerKey, *userKey;
115 GString *fileID;
116 GBool ok;
117 };
118
119 #ifdef ENABLE_PLUGINS
120 //------------------------------------------------------------------------
121 // ExternalSecurityHandler
122 //------------------------------------------------------------------------
123
124 class ExternalSecurityHandler: public SecurityHandler {
125 public:
126
127 ExternalSecurityHandler(PDFDoc *docA, Object *encryptDictA,
128 XpdfSecurityHandler *xshA);
129 virtual ~ExternalSecurityHandler();
130
131 virtual void *makeAuthData(GString *ownerPassword,
132 GString *userPassword);
133 virtual void *getAuthData();
134 virtual void freeAuthData(void *authData);
135 virtual GBool authorize(void *authData);
136 virtual int getPermissionFlags() { return permFlags; }
137 virtual GBool getOwnerPasswordOk() { return gFalse; }
138 virtual Guchar *getFileKey() { return fileKey; }
139 virtual int getFileKeyLength() { return fileKeyLength; }
140 virtual int getEncVersion() { return encVersion; }
141
142 private:
143
144 Object encryptDict;
145 XpdfSecurityHandler *xsh;
146 void *docData;
147 int permFlags;
148 Guchar fileKey[16];
149 int fileKeyLength;
150 int encVersion;
151 GBool ok;
152 };
153 #endif // ENABLE_PLUGINS
154
155 #endif