PDFSignature.java
18.1 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.ByteReader
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosDocument
* com.adobe.internal.pdftoolkit.core.cos.CosEncryption
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions
* com.adobe.internal.pdftoolkit.core.cos.CosString
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager
* com.adobe.internal.pdftoolkit.core.types.ASDate
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.digsig;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosEncryption;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager;
import com.adobe.internal.pdftoolkit.core.types.ASDate;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFAppBuildData;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFBuildProp;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFFilterBuildData;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFPubSecBuildData;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFSigQBuildData;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFSignatureFilter;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFSignatureReferenceList;
import com.adobe.internal.pdftoolkit.pdf.digsig.PDFSignatureSubFilter;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
public class PDFSignature
extends PDFCosDictionary {
public static final ASName k_Changes = ASName.create((String)"Changes");
public static final ASName k_DocMDP = ASName.create((String)"DocMDP");
public static final ASName k_UR = ASName.create((String)"UR");
public static final ASName k_UB1 = ASName.create((String)"UB1");
public static final ASName k_UR3 = ASName.create((String)"UR3");
public static final ASName k_FieldMDP = ASName.create((String)"FieldMDP");
public static final ASName k_Identity = ASName.create((String)"Identity");
public static final ASName k_Reference = ASName.create((String)"Reference");
public static final ASName k_MD5 = ASName.create((String)"MD5");
public static final ASName k_SHA1 = ASName.create((String)"SHA1");
public static final ASName k_SHA256 = ASName.create((String)"SHA256");
public static final ASName k_SHA384 = ASName.create((String)"SHA384");
public static final ASName k_SHA512 = ASName.create((String)"SHA512");
public static final ASName k_RIPEMD160 = ASName.create((String)"RIPEMD160");
public static final ASName k_Adobe_PPKLite = ASName.create((String)"Adobe.PPKLite");
public static final ASName k_Adobe_PPKMS = ASName.create((String)"Adobe.PPKMS");
public static final ASName k_adbe_x509_rsa_sha1 = ASName.create((String)"adbe.x509.rsa_sha1");
public static final ASName k_adbe_pkcs7_sha1 = ASName.create((String)"adbe.pkcs7.sha1");
public static final ASName k_adbe_pkcs7_detached = ASName.create((String)"adbe.pkcs7.detached");
public static final ASName k_ETSI_RFC3161 = ASName.create((String)"ETSI.RFC3161");
public static final ASName k_ETSI_CADES_detached = ASName.create((String)"ETSI.CAdES.detached");
public static final ASName k_Prop_AuthTime = ASName.create((String)"Prop_AuthTime");
public static final String RSACipher = "RSA";
public static final String DSASigAlgorithm = "DSA";
public static final String RipeMDOID = "1.3.36.3.3.1.2";
public static final String ECCipher = "EC";
protected PDFSignature(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
public static PDFSignature getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFSignature pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFSignature.class);
if (pdfObject == null) {
pdfObject = new PDFSignature(cosObject);
}
return pdfObject;
}
public static PDFSignature newInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFSignature.newInstance(pdfDocument, PDFSignatureFilter.AdobePPKLite.toString(), PDFSignatureSubFilter.PKCS7Detached.toString());
}
public static PDFSignature newInstance(PDFDocument pdfDocument, PDFSignatureSubFilter subFilter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFSignature.newInstance(pdfDocument, null, subFilter);
}
public static PDFSignature newInstance(PDFDocument pdfDocument, PDFSignatureFilter filter, PDFSignatureSubFilter subFilter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (filter == null) {
filter = PDFSignatureFilter.AdobePPKLite;
}
if (subFilter == null) {
subFilter = PDFSignatureSubFilter.PKCS7Detached;
}
return PDFSignature.newInstance(pdfDocument, filter.toString(), subFilter.toString());
}
static PDFSignature newInstance(PDFDocument pdfDocument, String filter, String subfilter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDocument);
PDFSignature pdfObject = new PDFSignature((CosObject)cosObject);
pdfObject.setType(ASName.k_Sig);
pdfObject.setFilterName(ASName.create((String)filter));
pdfObject.setSubFilterName(ASName.create((String)subfilter));
return pdfObject;
}
protected void setType(ASName type) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryNameValue(ASName.k_Type, type);
}
public int getVersion() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.getCosDictionary().containsKey((Object)ASName.k_R)) {
return this.getDictionaryIntValue(ASName.k_R);
}
return 0;
}
public void setVersion(int version) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (version == 0) {
this.removeValue(ASName.k_R);
} else {
this.setDictionaryIntValue(ASName.k_R, version);
}
}
public ASName getFilterName() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.k_Filter);
}
void setFilterName(ASName filter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (filter == null) {
throw new PDFInvalidDocumentException("Invalid Parameter:filter entry can not be null");
}
this.setDictionaryNameValue(ASName.k_Filter, filter);
}
public ASName getSubFilterName() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.k_SubFilter);
}
public void setSubFilterName(ASName subFilter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryNameValue(ASName.k_SubFilter, subFilter);
}
public byte[] getContents() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosString contentsStr = this.getCosDictionary().getCosString(ASName.k_Contents);
if (contentsStr != null) {
return contentsStr.byteArrayValue();
}
return null;
}
public void setContents(byte[] contents) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (contents == null) {
throw new PDFInvalidDocumentException("Invalid Parameter:contents entry can not be null");
}
this.setDictionaryByteArrayValue(ASName.k_Contents, contents);
CosString contentsStr = this.getCosDictionary().getCosString(ASName.k_Contents);
contentsStr.setWriteHex(true);
if (contentsStr.getToEncrypt()) {
contentsStr.setToEncrypt(false);
}
}
public long[] getByteRanges() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosByteRange = this.getDictionaryArrayValue(ASName.k_ByteRange);
if (cosByteRange != null) {
return cosByteRange.getArrayLong();
}
return null;
}
public void setByteRanges(long[] byteRanges) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosByteRanges = null;
if (this.getCosDictionary().containsKey((Object)ASName.k_ByteRange)) {
cosByteRanges = this.getDictionaryArrayValue(ASName.k_ByteRange);
cosByteRanges.clear();
} else {
cosByteRanges = PDFCosObject.newCosArray(this.getPDFDocument());
this.setDictionaryArrayValue(ASName.k_ByteRange, cosByteRanges);
}
for (int i = 0; i < byteRanges.length; ++i) {
cosByteRanges.addLong(i, byteRanges[i]);
}
}
public byte[][] getCertificates() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosObject cosObj = this.getDictionaryCosObjectValue(ASName.k_Cert);
if (cosObj == null) {
return null;
}
if (cosObj instanceof CosArray) {
return ((CosArray)cosObj).getArrayBytes();
}
if (cosObj instanceof CosString) {
CosString cosCerts = (CosString)cosObj;
byte[][] certs = new byte[][]{cosCerts.byteArrayValue()};
return certs;
}
throw new PDFInvalidDocumentException("Cos String or Array Expected");
}
public void setCertificate(byte[] cert) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryByteArrayValue(ASName.k_Cert, cert);
}
public void setCertificates(byte[][] certs) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFDocument pdfDocument = this.getPDFDocument();
CosArray array = PDFCosObject.newCosArray(pdfDocument);
for (int i = 0; i < certs.length; ++i) {
CosString str = PDFCosObject.newCosString(pdfDocument, certs[i]);
array.add((CosObject)str);
}
this.setDictionaryArrayValue(ASName.k_Cert, array);
}
public PDFSignatureReferenceList getSignatureReferences() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosArray = this.getDictionaryArrayValue(k_Reference);
return PDFSignatureReferenceList.getInstance((CosObject)cosArray);
}
public void setReferences(PDFSignatureReferenceList sigRefList) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (sigRefList == null) {
this.removeValue(k_Reference);
} else {
this.setDictionaryArrayValue(k_Reference, sigRefList.getCosArray());
}
}
public void setPDFBuildPropDictionary(PDFFilterBuildData filterDict, PDFAppBuildData appDict, PDFSigQBuildData sigQDict, PDFPubSecBuildData pubSecDict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBuildProp buildPropDict = PDFBuildProp.newInstance(this.getPDFDocument());
if (filterDict != null) {
buildPropDict.setFilterDict(filterDict);
}
if (appDict != null) {
buildPropDict.setAppDict(appDict);
}
if (sigQDict != null) {
buildPropDict.setSigQDict(sigQDict);
}
if (pubSecDict != null) {
buildPropDict.setPubSecDict(pubSecDict);
}
this.setDictionaryValue(ASName.k_Prop_Build, buildPropDict);
}
public PDFBuildProp getPDFBuildPropDictionary() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosDictionary buildPropDict = this.getDictionaryDictionaryValue(ASName.k_Prop_Build);
if (buildPropDict != null) {
return PDFBuildProp.getInstance((CosObject)buildPropDict);
}
return null;
}
public ASName getType() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.k_Type);
}
public int getRevision() {
return this.getCosObject().getObjRevision();
}
public void setChanges(long[] changes) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (changes == null) {
this.removeValue(k_Changes);
} else {
this.setDictionaryArrayValue(k_Changes, changes);
}
}
public long[] getChanges() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosChanges = this.getDictionaryArrayValue(k_Changes);
if (cosChanges != null) {
return cosChanges.getArrayLong();
}
return null;
}
public String getName() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringValue(ASName.k_Name);
}
public void setName(String name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (name == null) {
this.removeValue(ASName.k_Name);
} else {
this.setDictionaryStringValue(ASName.k_Name, name);
}
}
public ASDate getDate() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryDateValue(ASName.k_M);
}
public void setDate(ASDate date) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (date == null) {
this.removeValue(ASName.k_M);
} else {
this.setDictionaryDateValue(ASName.k_M, date);
}
}
public String getLocation() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringValue(ASName.k_Location);
}
public void setLocation(String location) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (location == null) {
this.removeValue(ASName.k_Location);
} else {
this.setDictionaryStringValue(ASName.k_Location, location);
}
}
public String getReason() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringValue(ASName.k_Reason);
}
public void setReason(String reason) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (reason == null) {
this.removeValue(ASName.k_Reason);
} else {
this.setDictionaryStringValue(ASName.k_Reason, reason);
}
}
public String getContactInfo() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringValue(ASName.k_ContactInfo);
}
public void setContactInfo(String contact) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (contact == null) {
this.removeValue(ASName.k_ContactInfo);
} else {
this.setDictionaryStringValue(ASName.k_ContactInfo, contact);
}
}
public long getAuthenticationTime() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Number authTime = this.getDictionaryNumericValue(k_Prop_AuthTime);
if (authTime != null) {
return authTime.longValue();
}
return 0;
}
public void setAuthenticationTime(Long sec) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryIntValue(k_Prop_AuthTime, sec);
}
public ASName getAuthenticationType() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.create((String)"Prop_AuthType"));
}
public void setAuthenticationType(ASName type) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (type == null) {
this.removeValue(ASName.create((String)"Prop_AuthType"));
} else {
this.setDictionaryNameValue(ASName.create((String)"Prop_AuthType"), type);
}
}
public CosDocument generateRollBackDocument() throws PDFCosParseException, PDFSecurityException, PDFIOException {
SignatureEOFOptions options = new SignatureEOFOptions();
options.setEofValue(this.getCosObject().getObjEOF());
CosDocument newDoc = CosDocument.newDocument((ByteReader)this.getPDFDocument().getCosDocument().getByteReader(), (CosOpenOptions)options);
SecurityManager origSecMgr = this.getPDFDocument().getCosDocument().getEncryption().getDecryptionSecurityManager();
if (origSecMgr != null) {
newDoc.getEncryption().setDecryptionSecurityManager(origSecMgr);
}
return newDoc;
}
private static final class SignatureEOFOptions
extends CosOpenOptions {
private SignatureEOFOptions() {
}
}
}