PKISecurityHandler.java
17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.credentials.Credentials
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityAuthorizationException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException
* com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProvider
* com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProviderStandard
* com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState
* com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler
* com.adobe.internal.pdftoolkit.core.securityframework.PKCS7EnvelopedDataHandler
* com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler
* com.adobe.internal.pdftoolkit.core.securityframework.impl.SecurityProvidersImpl
* com.adobe.internal.pdftoolkit.core.securityframework.pki.Identities
*/
package com.adobe.internal.pdftoolkit.core.encryption;
import com.adobe.internal.pdftoolkit.core.credentials.Credentials;
import com.adobe.internal.pdftoolkit.core.encryption.DecryptedStatePKI;
import com.adobe.internal.pdftoolkit.core.encryption.EncryptionKeyCalc;
import com.adobe.internal.pdftoolkit.core.encryption.EncryptionKeyImpl;
import com.adobe.internal.pdftoolkit.core.encryption.IdentityEncryptionHandler;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityAuthorizationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException;
import com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProvider;
import com.adobe.internal.pdftoolkit.core.permissionprovider.PermissionProviderStandard;
import com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.PKCS7EnvelopedDataHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.impl.SecurityProvidersImpl;
import com.adobe.internal.pdftoolkit.core.securityframework.pki.Identities;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class PKISecurityHandler
implements SecurityHandler {
static final String RECIPIENTS = "Recipients";
private DecryptedState decryptedState;
private boolean decryptedUsingState;
private int encryptDecryptedStatePerms = 3902;
private byte[] mSeed = null;
private Exception mFail = null;
private Integer mPerms = null;
private Credentials mRecipient = null;
private Map mEncryptParams = null;
private MessageDigest mSHADigest = null;
private MessageDigest mMD5Digest = null;
private SecurityProvidersImpl mProviders = null;
private PKCS7EnvelopedDataHandler mPKCS7Handler = null;
public PKISecurityHandler(Credentials recipient, SecurityProvidersImpl providers) {
this.mRecipient = recipient;
this.mProviders = providers;
this.setPKCS7Handler();
}
public PKISecurityHandler(Map encryptParams, SecurityProvidersImpl providers) throws PDFSecurityConfigurationException {
try {
SecureRandom generator;
this.mProviders = providers;
SecureRandom secureRandom = generator = this.mProviders != null ? this.mProviders.getRandomGenerator() : null;
if (generator == null) {
Provider randomProvider = this.mProviders != null ? this.mProviders.requireSHA1PRNG() : null;
generator = randomProvider == null ? SecureRandom.getInstance("SHA1PRNG") : SecureRandom.getInstance("SHA1PRNG", randomProvider);
}
this.mSeed = generator.generateSeed(20);
this.mEncryptParams = encryptParams;
this.setPKCS7Handler();
}
catch (NoSuchAlgorithmException e) {
this.mFail = e;
}
}
private void setPKCS7Handler() {
if (this.mProviders != null) {
this.mPKCS7Handler = this.mProviders.getPKCS7EnvelopedDataHandler();
}
if (this.mPKCS7Handler == null) {
try {
Class pkcs7Handler = Class.forName("com.adobe.internal.pdftoolkit.core.encryption.impl.PKCS7EnvelopedDataRSANonFIPSHandler");
this.mPKCS7Handler = (PKCS7EnvelopedDataHandler)pkcs7Handler.newInstance();
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
catch (InstantiationException e) {
throw new RuntimeException(e);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public EncryptionHandler getEncryptionHandler(String cryptName, Map encryptParams, byte[] docId) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
if (this.mFail != null) {
throw new PDFSecurityConfigurationException("Failure to initialize Security handler", (Throwable)this.mFail);
}
if (cryptName.equals("Identity")) {
return new IdentityEncryptionHandler();
}
int keyLength = EncryptionKeyCalc.calculateEncryptionKey(encryptParams, cryptName, 128);
if (keyLength < 0) {
throw new PDFSecurityConfigurationException("Encryption with key length greater than 56 is not supported");
}
if (keyLength == 0) {
keyLength = 128;
}
this.initDigest(keyLength /= 8);
byte[] encryptionKey = null;
if (!this.decryptedUsingState) {
byte[] seed = this.mSeed;
if (seed == null) {
if (this.mRecipient == null) {
throw new PDFSecurityAuthorizationException("Absent authorization credentials");
}
byte[] envelopedData = this.getEnvelopeData(cryptName, encryptParams, this.mRecipient);
if (envelopedData == null || envelopedData.length < 20) {
throw new PDFSecurityConfigurationException("Failure to get enveloped data");
}
seed = envelopedData;
} else {
ArrayList recipients = null;
Map cfDict = EncryptionKeyImpl.getCFDict(encryptParams);
if (cfDict != null && (recipients = this.getRecipients(cfDict, (String)encryptParams.get("EFF"))) == null) {
recipients = this.getRecipients(cfDict, (String)encryptParams.get("StmF"));
}
if (recipients == null) {
recipients = (ArrayList)encryptParams.get("Recipients");
}
if (recipients == null) {
throw new PDFSecurityConfigurationException("Failure to find Recipients entry in the encryption parameters map");
}
}
encryptionKey = this.createEncryptionKey(cryptName, encryptParams, keyLength, seed);
this.decryptedState.setEncryptKey(cryptName, encryptionKey);
this.decryptedState.setPerms(Integer.valueOf(this.encryptDecryptedStatePerms));
} else {
encryptionKey = this.decryptedState.getEncryptKey(cryptName);
}
String algorithm = EncryptionKeyImpl.getEncryptionAlgorithm(encryptParams, cryptName);
return EncryptionKeyImpl.getEncryptionHandler(encryptionKey, algorithm, this.mMD5Digest, encryptParams, this.mProviders);
}
private void initDigest(int keyLength) throws PDFSecurityConfigurationException {
try {
if (this.mSHADigest == null) {
Provider sha1Provider;
Provider provider = sha1Provider = this.mProviders != null ? this.mProviders.requireSHA1() : null;
if (keyLength <= 16) {
this.mSHADigest = sha1Provider == null ? MessageDigest.getInstance("SHA-1") : MessageDigest.getInstance("SHA-1", sha1Provider);
} else {
MessageDigest messageDigest = this.mSHADigest = sha1Provider == null ? MessageDigest.getInstance("SHA-256") : MessageDigest.getInstance("SHA-256", sha1Provider);
}
}
if (this.mMD5Digest == null) {
Provider md5Provider = this.mProviders != null ? this.mProviders.requireMD5() : null;
this.mMD5Digest = md5Provider == null ? MessageDigest.getInstance("MD5") : MessageDigest.getInstance("MD5", md5Provider);
}
}
catch (NoSuchAlgorithmException e) {
throw new PDFSecurityConfigurationException((Throwable)e);
}
}
private byte[] createEncryptionKey(String cryptName, Map encryptParams, int keyLength, byte[] seed) {
byte[] dataDigest;
this.mSHADigest.update(seed, 0, 20);
RecipientsIterator recipientsIterator = new RecipientsIterator(encryptParams, cryptName);
while (recipientsIterator.hasNext()) {
byte[] curRecipient = recipientsIterator.next();
this.mSHADigest.update(curRecipient);
}
if (!EncryptionKeyImpl.toEncryptMetadata(encryptParams, cryptName)) {
this.mSHADigest.update(EncryptionKeyImpl.defaultMetadataMark);
}
if (keyLength > (dataDigest = this.mSHADigest.digest()).length) {
keyLength = dataDigest.length;
}
byte[] encryptionKey = new byte[keyLength];
System.arraycopy(dataDigest, 0, encryptionKey, 0, keyLength);
return encryptionKey;
}
public Map getEncryptParameters() {
return this.mEncryptParams;
}
public boolean authenticate(Map params, byte[] docID) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
this.decryptedState = new DecryptedStatePKI();
EncryptionKeyImpl.verifyEncryptionVersion(params, false);
this.mEncryptParams = params;
if (this.mRecipient == null) {
if (this.mFail != null) {
throw new PDFSecurityConfigurationException("Failure to initialize Security handler", (Throwable)this.mFail);
}
if (this.mSeed == null) {
return false;
}
return true;
}
String cryptName = null;
Map cfDict = EncryptionKeyImpl.getCFDict(params);
if (cfDict != null) {
for (Map.Entry curCrypt : cfDict.entrySet()) {
if (!((Map)curCrypt.getValue()).containsKey("AuthEvent")) {
cryptName = (String)curCrypt.getKey();
break;
}
String authEvent = (String)((Map)curCrypt.getValue()).get("AuthEvent");
if (!"DocOpen".equals(authEvent)) continue;
cryptName = (String)curCrypt.getKey();
break;
}
}
if (cryptName == null) {
String subFilter = params.containsKey("SubFilter") ? params.get("SubFilter") : null;
if (subFilter != null && subFilter.compareTo("adbe.pkcs7.s3") != 0 && subFilter.compareTo("adbe.pkcs7.s4") != 0) {
cryptName = "DefEmbeddedFile";
}
this.calculateEncryptionKey(params, cryptName);
return true;
}
return this.calculateEncryptionKey(params, cryptName);
}
private boolean calculateEncryptionKey(Map params, String cryptName) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
byte[] envelopedData;
String handlerName = (String)params.get("Filter");
if (cryptName == null) {
cryptName = (String)params.get("StmF");
if (cryptName == null && params.containsKey("CF")) {
cryptName = "Identity";
}
if (cryptName == null) {
cryptName = handlerName;
}
}
if ((envelopedData = this.getEnvelopeData(cryptName, params, this.mRecipient)) != null) {
int keyLength;
if (envelopedData.length == 24) {
int perms = (envelopedData[20] & 255) << 24 | (envelopedData[21] & 255) << 16 | (envelopedData[22] & 255) << 8 | envelopedData[23] & 255;
this.mPerms = perms;
}
if ((keyLength = EncryptionKeyCalc.calculateEncryptionKey(params, cryptName, 128)) < 0) {
throw new PDFSecurityConfigurationException("Encryption with key length greater than 56 is not supported");
}
if (keyLength == 0) {
keyLength = 128;
}
this.initDigest(keyLength /= 8);
byte[] encKey = this.createEncryptionKey(cryptName, params, keyLength, envelopedData);
this.decryptedState.setEncryptKey(cryptName, encKey);
this.decryptedState.setPerms(this.mPerms);
return true;
}
return false;
}
public PermissionProvider getPermissionProvider() {
if (this.mPerms == null) {
return new PermissionProviderStandard(0);
}
return new PermissionProviderStandard(this.mPerms.intValue());
}
MessageDigest getMD5Digest() {
return this.mMD5Digest;
}
MessageDigest getSHA1Digest() {
return this.mSHADigest;
}
private byte[] getEnvelopeData(String cryptName, Map encryptParams, Credentials recipient) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
Exception excp = null;
if (recipient == null) {
return null;
}
String cryptFilter = cryptName == null ? (String)encryptParams.get("StmF") : cryptName;
RecipientsIterator recipientsIter = new RecipientsIterator(encryptParams, cryptFilter);
byte[] envelopedData = null;
while (recipientsIter.hasNext()) {
byte[] curRecipient = recipientsIter.next();
try {
envelopedData = this.mPKCS7Handler.getEnvelopeData(this.mRecipient, curRecipient);
if (envelopedData == null) continue;
excp = null;
break;
}
catch (Exception e) {
excp = e;
continue;
}
}
if (excp != null) {
throw new PDFSecurityAuthorizationException((Throwable)excp);
}
if (envelopedData == null) {
throw new PDFSecurityAuthorizationException("Recipient is not authorized");
}
return envelopedData;
}
public byte[] buildPKCS7(Identities identity, boolean inCrypt, Map encryptParams) throws Exception {
byte[] envelope;
if (!inCrypt) {
PermissionProviderStandard encryptPerms = PermissionProviderStandard.newInstance((PermissionProvider)identity.getPermissions());
envelope = new byte[24];
System.arraycopy(this.mSeed, 0, envelope, 0, 20);
int perms = encryptPerms.getPermissionBits();
perms |= 1;
for (int permsInd = 0; permsInd < 4; ++permsInd) {
envelope[23 - permsInd] = (byte)(perms & 255);
perms >>= 8;
}
} else {
envelope = new byte[20];
System.arraycopy(this.mSeed, 0, envelope, 0, 20);
}
Credentials[] credentials = identity.getCredentials();
return this.mPKCS7Handler.buildPKCS7EnvelopedData(credentials, envelope, encryptParams);
}
private ArrayList getRecipients(Map cfDict, String stmCrypt) {
Map stmDict;
ArrayList recipients = null;
if (stmCrypt != null && (stmDict = (Map)cfDict.get(stmCrypt)) != null) {
Object curRecipients = stmDict.get("Recipients");
if (curRecipients instanceof ArrayList) {
recipients = (ArrayList)curRecipients;
} else {
recipients = new ArrayList();
recipients.add(curRecipients);
}
}
return recipients;
}
public boolean authenticate(Map params, byte[] docID, DecryptedState decryptedState) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
if (decryptedState != null) {
this.decryptedState = decryptedState;
this.decryptedUsingState = true;
EncryptionKeyImpl.verifyEncryptionVersion(params, false);
this.mEncryptParams = params;
this.mPerms = decryptedState.getPerms();
return true;
}
return this.authenticate(params, docID);
}
public DecryptedState getDecryptedState() {
return this.decryptedState;
}
static class RecipientsIterator {
Iterator mIterator = null;
byte[] mNext = null;
RecipientsIterator(Map encryptParams, String cryptFilter) {
Map strDict;
Object recipients = encryptParams.get("Recipients");
Map cfDict = EncryptionKeyImpl.getCFDict(encryptParams);
if (cfDict != null && cryptFilter != null && (strDict = (Map)cfDict.get(cryptFilter)) != null && strDict.containsKey("Recipients")) {
recipients = strDict.get("Recipients");
}
if (recipients instanceof ArrayList) {
this.mIterator = ((ArrayList)recipients).iterator();
} else {
this.mNext = (byte[])recipients;
}
}
boolean hasNext() {
return this.mIterator != null ? this.mIterator.hasNext() : this.mNext != null;
}
byte[] next() {
if (this.mIterator != null) {
return (byte[])this.mIterator.next();
}
byte[] retNext = this.mNext;
this.mNext = null;
return retNext;
}
}
}