Coverage Report

Created: 2026-07-12 06:50

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
420
SecurityHandler *SecurityHandler::make(PDFDoc *docA, Object *encryptDictA) {
25
420
  Object filterObj;
26
420
  SecurityHandler *secHdlr;
27
28
420
  encryptDictA->dictLookup("Filter", &filterObj);
29
420
  if (filterObj.isName("Standard")) {
30
417
    secHdlr = new StandardSecurityHandler(docA, encryptDictA);
31
417
  } else if (filterObj.isName()) {
32
3
    error(errSyntaxError, -1, "Couldn't find the '{0:s}' security handler",
33
3
    filterObj.getName());
34
3
    secHdlr = NULL;
35
3
  } else {
36
0
    error(errSyntaxError, -1,
37
0
    "Missing or invalid 'Filter' entry in encryption dictionary");
38
0
    secHdlr = NULL;
39
0
  }
40
420
  filterObj.free();
41
420
  return secHdlr;
42
420
}
43
44
417
SecurityHandler::SecurityHandler(PDFDoc *docA) {
45
417
  doc = docA;
46
417
}
47
48
417
SecurityHandler::~SecurityHandler() {
49
417
}
50
51
GBool SecurityHandler::checkEncryption(GString *ownerPassword,
52
401
               GString *userPassword) {
53
401
  void *authData;
54
401
  GBool ok;
55
401
  int i;
56
57
401
  if (ownerPassword || userPassword) {
58
0
    authData = makeAuthData(ownerPassword, userPassword);
59
401
  } else {
60
401
    authData = NULL;
61
401
  }
62
401
  ok = authorize(authData);
63
401
  if (authData) {
64
0
    freeAuthData(authData);
65
0
  }
66
401
  for (i = 0; !ok && i < 3; ++i) {
67
104
    if (!(authData = getAuthData())) {
68
104
      break;
69
104
    }
70
0
    ok = authorize(authData);
71
0
    if (authData) {
72
0
      freeAuthData(authData);
73
0
    }
74
0
  }
75
401
  if (!ok) {
76
104
    error(errCommandLine, -1, "Incorrect password");
77
104
  }
78
401
  return ok;
79
401
}
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
417
  SecurityHandler(docA)
109
417
{
110
417
  Object versionObj, revisionObj, lengthObj;
111
417
  Object ownerKeyObj, userKeyObj, ownerEncObj, userEncObj;
112
417
  Object permObj, fileIDObj, fileIDObj1;
113
417
  Object cryptFiltersObj, streamFilterObj, stringFilterObj;
114
417
  Object cryptFilterObj, cfmObj, cfLengthObj;
115
417
  Object encryptMetadataObj;
116
117
417
  ok = gFalse;
118
417
  fileID = NULL;
119
417
  ownerKey = NULL;
120
417
  userKey = NULL;
121
417
  ownerEnc = NULL;
122
417
  userEnc = NULL;
123
417
  fileKeyLength = 0;
124
417
  encVersion = -1;
125
417
  encRevision = -1;
126
127
  //--- get the main parameters
128
417
  encryptDictA->dictLookup("V", &versionObj);
129
417
  encryptDictA->dictLookup("R", &revisionObj);
130
417
  encryptDictA->dictLookup("Length", &lengthObj);
131
417
  encryptDictA->dictLookup("O", &ownerKeyObj);
132
417
  encryptDictA->dictLookup("U", &userKeyObj);
133
417
  encryptDictA->dictLookup("OE", &ownerEncObj);
134
417
  encryptDictA->dictLookup("UE", &userEncObj);
135
417
  encryptDictA->dictLookup("P", &permObj);
136
417
  doc->getXRef()->getTrailerDict()->dictLookup("ID", &fileIDObj);
137
417
  if (!versionObj.isInt() ||
138
410
      !revisionObj.isInt() ||
139
408
      !permObj.isInt() ||
140
406
      !ownerKeyObj.isString() ||
141
404
      !userKeyObj.isString()) {
142
16
    error(errSyntaxError, -1, "Invalid encryption parameters");
143
16
    goto done;
144
16
  }
145
401
  encVersion = versionObj.getInt();
146
401
  encRevision = revisionObj.getInt();
147
401
  encAlgorithm = cryptRC4;
148
  // revision 2 forces a 40-bit key - some buggy PDF generators
149
  // set the Length value incorrectly
150
401
  if (encRevision == 2 || !lengthObj.isInt()) {
151
94
    fileKeyLength = 5;
152
307
  } else {
153
307
    fileKeyLength = lengthObj.getInt() / 8;
154
307
  }
155
401
  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
401
  if ((encVersion == 4 || encVersion == 5) &&
163
384
      (encRevision == 4 || encRevision == 5 || encRevision == 6)) {
164
378
    encryptDictA->dictLookup("CF", &cryptFiltersObj);
165
378
    encryptDictA->dictLookup("StmF", &streamFilterObj);
166
378
    encryptDictA->dictLookup("StrF", &stringFilterObj);
167
378
    if (cryptFiltersObj.isDict() &&
168
365
  streamFilterObj.isName() &&
169
351
  stringFilterObj.isName() &&
170
347
  !strcmp(streamFilterObj.getName(), stringFilterObj.getName())) {
171
334
      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
334
      if (cryptFiltersObj.dictLookup(streamFilterObj.getName(),
179
334
             &cryptFilterObj)->isDict()) {
180
283
  cryptFilterObj.dictLookup("CFM", &cfmObj);
181
283
  if (cfmObj.isName("V2")) {
182
7
    if (cryptFilterObj.dictLookup("Length",
183
7
          &cfLengthObj)->isInt()) {
184
3
      fileKeyLength = cfLengthObj.getInt();
185
3
    }
186
7
    cfLengthObj.free();
187
7
    encVersion = 2;
188
7
    encRevision = 3;
189
276
  } else if (cfmObj.isName("AESV2")) {
190
120
    if (cryptFilterObj.dictLookup("Length",
191
120
          &cfLengthObj)->isInt()) {
192
15
      fileKeyLength = cfLengthObj.getInt();
193
15
    }
194
120
    cfLengthObj.free();
195
120
    encVersion = 2;
196
120
    encRevision = 3;
197
120
    encAlgorithm = cryptAES;
198
156
  } else if (cfmObj.isName("AESV3")) {
199
142
    if (cryptFilterObj.dictLookup("Length",
200
142
          &cfLengthObj)->isInt()) {
201
8
      fileKeyLength = cfLengthObj.getInt();
202
8
    }
203
142
    cfLengthObj.free();
204
142
    encVersion = 5;
205
142
    if (encRevision != 5 && encRevision != 6) {
206
1
      encRevision = 6;
207
1
    }
208
142
    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
142
    fileKeyLength = 32;
213
142
  }
214
283
  cfmObj.free();
215
283
      }
216
334
      cryptFilterObj.free();
217
334
    }
218
378
    stringFilterObj.free();
219
378
    streamFilterObj.free();
220
378
    cryptFiltersObj.free();
221
378
    if (encryptDictA->dictLookup("EncryptMetadata",
222
378
         &encryptMetadataObj)->isBool()) {
223
0
      encryptMetadata = encryptMetadataObj.getBool();
224
0
    }
225
378
    encryptMetadataObj.free();
226
378
  }
227
228
  //--- version-specific parameters
229
401
  if (encRevision <= 4) {
230
156
    if (ownerKeyObj.getString()->getLength() != 32 ||
231
125
  userKeyObj.getString()->getLength() != 32) {
232
125
      error(errSyntaxError, -1, "Invalid encryption key length");
233
      // this is non-fatal -- see below
234
125
    }
235
245
  } else if (encRevision <= 6) {
236
    // the spec says 48 bytes, but Acrobat pads them out longer
237
242
    if (ownerKeyObj.getString()->getLength() < 48 ||
238
241
  userKeyObj.getString()->getLength() < 48 ||
239
239
  !ownerEncObj.isString() ||
240
238
  ownerEncObj.getString()->getLength() != 32 ||
241
236
  !userEncObj.isString() ||
242
235
  userEncObj.getString()->getLength() != 32) {
243
8
      error(errSyntaxError, -1, "Invalid encryption key length");
244
8
      goto done;
245
8
    }
246
242
  }
247
393
  permFlags = permObj.getInt();
248
393
  ownerKey = ownerKeyObj.getString()->copy();
249
393
  userKey = userKeyObj.getString()->copy();
250
393
  if (encRevision <= 4) {
251
    // Adobe apparently zero-pads the U value (and maybe the O value?)
252
    // if it's short
253
386
    while (ownerKey->getLength() < 32) {
254
230
      ownerKey->append((char)0x00);
255
230
    }
256
567
    while (userKey->getLength() < 32) {
257
411
      userKey->append((char)0x00);
258
411
    }
259
156
  }
260
393
  if (encVersion >= 1 && encVersion <= 2 &&
261
139
      encRevision >= 2 && encRevision <= 3) {
262
138
    if (fileIDObj.isArray()) {
263
136
      if (fileIDObj.arrayGet(0, &fileIDObj1)->isString()) {
264
134
  fileID = fileIDObj1.getString()->copy();
265
134
      } else {
266
2
  fileID = new GString();
267
2
      }
268
136
      fileIDObj1.free();
269
136
    } else {
270
2
      fileID = new GString();
271
2
    }
272
138
    if (fileKeyLength > 16 || fileKeyLength <= 0) {
273
31
      fileKeyLength = 16;
274
31
    }
275
138
    ok = gTrue;
276
255
  } else if (encVersion == 5 && (encRevision == 5 || encRevision == 6)) {
277
230
    fileID = new GString(); // unused for V=R=5
278
230
    ownerEnc = ownerEncObj.getString()->copy();
279
230
    userEnc = userEncObj.getString()->copy();
280
230
    if (fileKeyLength > 32 || fileKeyLength <= 0) {
281
10
      fileKeyLength = 32;
282
10
    }
283
230
    ok = gTrue;
284
230
  } else {
285
25
    error(errUnimplemented, -1,
286
25
    "Unsupported version/revision ({0:d}/{1:d}) of Standard security handler",
287
25
    encVersion, encRevision);
288
25
  }
289
290
417
 done:
291
417
  fileIDObj.free();
292
417
  permObj.free();
293
417
  userEncObj.free();
294
417
  ownerEncObj.free();
295
417
  userKeyObj.free();
296
417
  ownerKeyObj.free();
297
417
  lengthObj.free();
298
417
  revisionObj.free();
299
417
  versionObj.free();
300
417
}
301
302
417
StandardSecurityHandler::~StandardSecurityHandler() {
303
417
  if (fileID) {
304
368
    delete fileID;
305
368
  }
306
417
  if (ownerKey) {
307
393
    delete ownerKey;
308
393
  }
309
417
  if (userKey) {
310
393
    delete userKey;
311
393
  }
312
417
  if (ownerEnc) {
313
230
    delete ownerEnc;
314
230
  }
315
417
  if (userEnc) {
316
230
    delete userEnc;
317
230
  }
318
417
}
319
320
417
GBool StandardSecurityHandler::isUnencrypted() {
321
417
  return encVersion == -1 && encRevision == -1;
322
417
}
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
104
void *StandardSecurityHandler::getAuthData() {
333
104
  PDFCore *core;
334
104
  GString *password;
335
336
104
  if (!(core = doc->getCore()) ||
337
104
      !(password = core->getPassword())) {
338
104
    return NULL;
339
104
  }
340
0
  return new StandardAuthData(password, password->copy());
341
104
}
342
343
0
void StandardSecurityHandler::freeAuthData(void *authData) {
344
0
  delete (StandardAuthData *)authData;
345
0
}
346
347
401
GBool StandardSecurityHandler::authorize(void *authData) {
348
401
  GString *ownerPassword, *userPassword;
349
350
401
  if (!ok) {
351
33
    return gFalse;
352
33
  }
353
368
  if (authData) {
354
0
    ownerPassword = ((StandardAuthData *)authData)->ownerPassword;
355
0
    userPassword = ((StandardAuthData *)authData)->userPassword;
356
368
  } else {
357
368
    ownerPassword = NULL;
358
368
    userPassword = NULL;
359
368
  }
360
368
  if (!Decrypt::makeFileKey(encVersion, encRevision, fileKeyLength,
361
368
          ownerKey, userKey, ownerEnc, userEnc,
362
368
          permFlags, fileID,
363
368
          ownerPassword, userPassword, fileKey,
364
368
          encryptMetadata, &ownerPasswordOk)) {
365
71
    return gFalse;
366
71
  }
367
297
  return gTrue;
368
368
}