Coverage Report

Created: 2026-08-22 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/SecurityHandler.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SecurityHandler.cc
4
//
5
// Copyright 2004 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include "gmempp.h"
12
#include "GString.h"
13
#include "PDFDoc.h"
14
#include "Decrypt.h"
15
#include "Error.h"
16
#include "GlobalParams.h"
17
#include "PDFCore.h"
18
#include "SecurityHandler.h"
19
20
//------------------------------------------------------------------------
21
// SecurityHandler
22
//------------------------------------------------------------------------
23
24
849
SecurityHandler *SecurityHandler::make(PDFDoc *docA, Object *encryptDictA) {
25
849
  Object filterObj;
26
849
  SecurityHandler *secHdlr;
27
28
849
  encryptDictA->dictLookup("Filter", &filterObj);
29
849
  if (filterObj.isName("Standard")) {
30
847
    secHdlr = new StandardSecurityHandler(docA, encryptDictA);
31
847
  } else if (filterObj.isName()) {
32
0
    error(errSyntaxError, -1, "Couldn't find the '{0:s}' security handler",
33
0
    filterObj.getName());
34
0
    secHdlr = NULL;
35
2
  } else {
36
2
    error(errSyntaxError, -1,
37
2
    "Missing or invalid 'Filter' entry in encryption dictionary");
38
2
    secHdlr = NULL;
39
2
  }
40
849
  filterObj.free();
41
849
  return secHdlr;
42
849
}
43
44
847
SecurityHandler::SecurityHandler(PDFDoc *docA) {
45
847
  doc = docA;
46
847
}
47
48
847
SecurityHandler::~SecurityHandler() {
49
847
}
50
51
GBool SecurityHandler::checkEncryption(GString *ownerPassword,
52
840
               GString *userPassword) {
53
840
  void *authData;
54
840
  GBool ok;
55
840
  int i;
56
57
840
  if (ownerPassword || userPassword) {
58
0
    authData = makeAuthData(ownerPassword, userPassword);
59
840
  } else {
60
840
    authData = NULL;
61
840
  }
62
840
  ok = authorize(authData);
63
840
  if (authData) {
64
0
    freeAuthData(authData);
65
0
  }
66
840
  for (i = 0; !ok && i < 3; ++i) {
67
14
    if (!(authData = getAuthData())) {
68
14
      break;
69
14
    }
70
0
    ok = authorize(authData);
71
0
    if (authData) {
72
0
      freeAuthData(authData);
73
0
    }
74
0
  }
75
840
  if (!ok) {
76
14
    error(errCommandLine, -1, "Incorrect password");
77
14
  }
78
840
  return ok;
79
840
}
80
81
//------------------------------------------------------------------------
82
// StandardSecurityHandler
83
//------------------------------------------------------------------------
84
85
class StandardAuthData {
86
public:
87
88
0
  StandardAuthData(GString *ownerPasswordA, GString *userPasswordA) {
89
0
    ownerPassword = ownerPasswordA;
90
0
    userPassword = userPasswordA;
91
0
  }
92
93
0
  ~StandardAuthData() {
94
0
    if (ownerPassword) {
95
0
      delete ownerPassword;
96
0
    }
97
0
    if (userPassword) {
98
0
      delete userPassword;
99
0
    }
100
0
  }
101
102
  GString *ownerPassword;
103
  GString *userPassword;
104
};
105
106
StandardSecurityHandler::StandardSecurityHandler(PDFDoc *docA,
107
             Object *encryptDictA):
108
847
  SecurityHandler(docA)
109
847
{
110
847
  Object versionObj, revisionObj, lengthObj;
111
847
  Object ownerKeyObj, userKeyObj, ownerEncObj, userEncObj;
112
847
  Object permObj, fileIDObj, fileIDObj1;
113
847
  Object cryptFiltersObj, streamFilterObj, stringFilterObj;
114
847
  Object cryptFilterObj, cfmObj, cfLengthObj;
115
847
  Object encryptMetadataObj;
116
117
847
  ok = gFalse;
118
847
  fileID = NULL;
119
847
  ownerKey = NULL;
120
847
  userKey = NULL;
121
847
  ownerEnc = NULL;
122
847
  userEnc = NULL;
123
847
  fileKeyLength = 0;
124
847
  encVersion = -1;
125
847
  encRevision = -1;
126
127
  //--- get the main parameters
128
847
  encryptDictA->dictLookup("V", &versionObj);
129
847
  encryptDictA->dictLookup("R", &revisionObj);
130
847
  encryptDictA->dictLookup("Length", &lengthObj);
131
847
  encryptDictA->dictLookup("O", &ownerKeyObj);
132
847
  encryptDictA->dictLookup("U", &userKeyObj);
133
847
  encryptDictA->dictLookup("OE", &ownerEncObj);
134
847
  encryptDictA->dictLookup("UE", &userEncObj);
135
847
  encryptDictA->dictLookup("P", &permObj);
136
847
  doc->getXRef()->getTrailerDict()->dictLookup("ID", &fileIDObj);
137
847
  if (!versionObj.isInt() ||
138
847
      !revisionObj.isInt() ||
139
847
      !permObj.isInt() ||
140
840
      !ownerKeyObj.isString() ||
141
840
      !userKeyObj.isString()) {
142
7
    error(errSyntaxError, -1, "Invalid encryption parameters");
143
7
    goto done;
144
7
  }
145
840
  encVersion = versionObj.getInt();
146
840
  encRevision = revisionObj.getInt();
147
840
  encAlgorithm = cryptRC4;
148
  // revision 2 forces a 40-bit key - some buggy PDF generators
149
  // set the Length value incorrectly
150
840
  if (encRevision == 2 || !lengthObj.isInt()) {
151
822
    fileKeyLength = 5;
152
822
  } else {
153
18
    fileKeyLength = lengthObj.getInt() / 8;
154
18
  }
155
840
  encryptMetadata = gTrue;
156
157
  //--- check for a crypt filter (which can modify the parameters)
158
  //~ this currently only handles a subset of crypt filter functionality
159
  //~ (in particular, it ignores the EFF entry in encryptDictA, and
160
  //~ doesn't handle the case where StmF, StrF, and EFF are not all the
161
  //~ same)
162
840
  if ((encVersion == 4 || encVersion == 5) &&
163
82
      (encRevision == 4 || encRevision == 5 || encRevision == 6)) {
164
82
    encryptDictA->dictLookup("CF", &cryptFiltersObj);
165
82
    encryptDictA->dictLookup("StmF", &streamFilterObj);
166
82
    encryptDictA->dictLookup("StrF", &stringFilterObj);
167
82
    if (cryptFiltersObj.isDict() &&
168
75
  streamFilterObj.isName() &&
169
75
  stringFilterObj.isName() &&
170
75
  !strcmp(streamFilterObj.getName(), stringFilterObj.getName())) {
171
75
      if (!strcmp(streamFilterObj.getName(), "Identity")) {
172
  // no encryption on streams or strings
173
0
  stringFilterObj.free();
174
0
  streamFilterObj.free();
175
0
  cryptFiltersObj.free();
176
0
  goto done;
177
0
      }
178
75
      if (cryptFiltersObj.dictLookup(streamFilterObj.getName(),
179
75
             &cryptFilterObj)->isDict()) {
180
75
  cryptFilterObj.dictLookup("CFM", &cfmObj);
181
75
  if (cfmObj.isName("V2")) {
182
1
    if (cryptFilterObj.dictLookup("Length",
183
1
          &cfLengthObj)->isInt()) {
184
0
      fileKeyLength = cfLengthObj.getInt();
185
0
    }
186
1
    cfLengthObj.free();
187
1
    encVersion = 2;
188
1
    encRevision = 3;
189
74
  } else if (cfmObj.isName("AESV2")) {
190
65
    if (cryptFilterObj.dictLookup("Length",
191
65
          &cfLengthObj)->isInt()) {
192
58
      fileKeyLength = cfLengthObj.getInt();
193
58
    }
194
65
    cfLengthObj.free();
195
65
    encVersion = 2;
196
65
    encRevision = 3;
197
65
    encAlgorithm = cryptAES;
198
65
  } else if (cfmObj.isName("AESV3")) {
199
7
    if (cryptFilterObj.dictLookup("Length",
200
7
          &cfLengthObj)->isInt()) {
201
0
      fileKeyLength = cfLengthObj.getInt();
202
0
    }
203
7
    cfLengthObj.free();
204
7
    encVersion = 5;
205
7
    if (encRevision != 5 && encRevision != 6) {
206
0
      encRevision = 6;
207
0
    }
208
7
    encAlgorithm = cryptAES256;
209
    // The PDF 2.0 spec says Length and CF.Length are both deprecated.
210
    // Acrobat X honors Length and ignores CF.Length.
211
    // I think it's safest to ignore both.
212
7
    fileKeyLength = 32;
213
7
  }
214
75
  cfmObj.free();
215
75
      }
216
75
      cryptFilterObj.free();
217
75
    }
218
82
    stringFilterObj.free();
219
82
    streamFilterObj.free();
220
82
    cryptFiltersObj.free();
221
82
    if (encryptDictA->dictLookup("EncryptMetadata",
222
82
         &encryptMetadataObj)->isBool()) {
223
0
      encryptMetadata = encryptMetadataObj.getBool();
224
0
    }
225
82
    encryptMetadataObj.free();
226
82
  }
227
228
  //--- version-specific parameters
229
840
  if (encRevision <= 4) {
230
824
    if (ownerKeyObj.getString()->getLength() != 32 ||
231
761
  userKeyObj.getString()->getLength() != 32) {
232
65
      error(errSyntaxError, -1, "Invalid encryption key length");
233
      // this is non-fatal -- see below
234
65
    }
235
824
  } else if (encRevision <= 6) {
236
    // the spec says 48 bytes, but Acrobat pads them out longer
237
16
    if (ownerKeyObj.getString()->getLength() < 48 ||
238
16
  userKeyObj.getString()->getLength() < 48 ||
239
16
  !ownerEncObj.isString() ||
240
16
  ownerEncObj.getString()->getLength() != 32 ||
241
16
  !userEncObj.isString() ||
242
16
  userEncObj.getString()->getLength() != 32) {
243
0
      error(errSyntaxError, -1, "Invalid encryption key length");
244
0
      goto done;
245
0
    }
246
16
  }
247
840
  permFlags = permObj.getInt();
248
840
  ownerKey = ownerKeyObj.getString()->copy();
249
840
  userKey = userKeyObj.getString()->copy();
250
840
  if (encRevision <= 4) {
251
    // Adobe apparently zero-pads the U value (and maybe the O value?)
252
    // if it's short
253
900
    while (ownerKey->getLength() < 32) {
254
76
      ownerKey->append((char)0x00);
255
76
    }
256
887
    while (userKey->getLength() < 32) {
257
63
      userKey->append((char)0x00);
258
63
    }
259
824
  }
260
840
  if (encVersion >= 1 && encVersion <= 2 &&
261
824
      encRevision >= 2 && encRevision <= 3) {
262
824
    if (fileIDObj.isArray()) {
263
824
      if (fileIDObj.arrayGet(0, &fileIDObj1)->isString()) {
264
824
  fileID = fileIDObj1.getString()->copy();
265
824
      } else {
266
0
  fileID = new GString();
267
0
      }
268
824
      fileIDObj1.free();
269
824
    } else {
270
0
      fileID = new GString();
271
0
    }
272
824
    if (fileKeyLength > 16 || fileKeyLength <= 0) {
273
59
      fileKeyLength = 16;
274
59
    }
275
824
    ok = gTrue;
276
824
  } else if (encVersion == 5 && (encRevision == 5 || encRevision == 6)) {
277
16
    fileID = new GString(); // unused for V=R=5
278
16
    ownerEnc = ownerEncObj.getString()->copy();
279
16
    userEnc = userEncObj.getString()->copy();
280
16
    if (fileKeyLength > 32 || fileKeyLength <= 0) {
281
0
      fileKeyLength = 32;
282
0
    }
283
16
    ok = gTrue;
284
16
  } else {
285
0
    error(errUnimplemented, -1,
286
0
    "Unsupported version/revision ({0:d}/{1:d}) of Standard security handler",
287
0
    encVersion, encRevision);
288
0
  }
289
290
847
 done:
291
847
  fileIDObj.free();
292
847
  permObj.free();
293
847
  userEncObj.free();
294
847
  ownerEncObj.free();
295
847
  userKeyObj.free();
296
847
  ownerKeyObj.free();
297
847
  lengthObj.free();
298
847
  revisionObj.free();
299
847
  versionObj.free();
300
847
}
301
302
847
StandardSecurityHandler::~StandardSecurityHandler() {
303
847
  if (fileID) {
304
840
    delete fileID;
305
840
  }
306
847
  if (ownerKey) {
307
840
    delete ownerKey;
308
840
  }
309
847
  if (userKey) {
310
840
    delete userKey;
311
840
  }
312
847
  if (ownerEnc) {
313
16
    delete ownerEnc;
314
16
  }
315
847
  if (userEnc) {
316
16
    delete userEnc;
317
16
  }
318
847
}
319
320
847
GBool StandardSecurityHandler::isUnencrypted() {
321
847
  return encVersion == -1 && encRevision == -1;
322
847
}
323
324
void *StandardSecurityHandler::makeAuthData(GString *ownerPassword,
325
0
              GString *userPassword) {
326
0
  return new StandardAuthData(ownerPassword ? ownerPassword->copy()
327
0
                          : (GString *)NULL,
328
0
            userPassword ? userPassword->copy()
329
0
                         : (GString *)NULL);
330
0
}
331
332
14
void *StandardSecurityHandler::getAuthData() {
333
14
  PDFCore *core;
334
14
  GString *password;
335
336
14
  if (!(core = doc->getCore()) ||
337
14
      !(password = core->getPassword())) {
338
14
    return NULL;
339
14
  }
340
0
  return new StandardAuthData(password, password->copy());
341
14
}
342
343
0
void StandardSecurityHandler::freeAuthData(void *authData) {
344
0
  delete (StandardAuthData *)authData;
345
0
}
346
347
840
GBool StandardSecurityHandler::authorize(void *authData) {
348
840
  GString *ownerPassword, *userPassword;
349
350
840
  if (!ok) {
351
0
    return gFalse;
352
0
  }
353
840
  if (authData) {
354
0
    ownerPassword = ((StandardAuthData *)authData)->ownerPassword;
355
0
    userPassword = ((StandardAuthData *)authData)->userPassword;
356
840
  } else {
357
840
    ownerPassword = NULL;
358
840
    userPassword = NULL;
359
840
  }
360
840
  if (!Decrypt::makeFileKey(encVersion, encRevision, fileKeyLength,
361
840
          ownerKey, userKey, ownerEnc, userEnc,
362
840
          permFlags, fileID,
363
840
          ownerPassword, userPassword, fileKey,
364
840
          encryptMetadata, &ownerPasswordOk)) {
365
14
    return gFalse;
366
14
  }
367
826
  return gTrue;
368
840
}