Coverage Report

Created: 2026-07-16 06:52

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