Coverage Report

Created: 2026-08-31 06:45

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