EncryptionImpl.java
10.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.io.stream.OutputByteStream
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityAuthorizationException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException
* com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState
* com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler
* com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler
* com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager
* com.adobe.internal.pdftoolkit.core.securityframework.impl.EnableEncryption
*/
package com.adobe.internal.pdftoolkit.core.encryption;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityAuthorizationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException;
import com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager;
import com.adobe.internal.pdftoolkit.core.securityframework.impl.EnableEncryption;
import java.util.HashMap;
import java.util.Map;
public class EncryptionImpl
implements EnableEncryption {
private EncryptionState decryptionHandle = null;
private EncryptionState encryptionHandle = null;
private SecurityHandler securityHandler = null;
public void setDecryptionSecurityManager(SecurityManager secMgr, Map encryptParms, byte[] id) throws PDFSecurityAuthorizationException {
if (this.decryptionHandle != null) {
throw new PDFSecurityAuthorizationException("Cannot unlock again already unlocked document");
}
this.decryptionHandle = new EncryptionState(secMgr, encryptParms, id);
}
public void setEncryptionSecurityManager(SecurityManager secMgr, Map encryptParms, byte[] id) {
this.encryptionHandle = new EncryptionState(secMgr, encryptParms, id);
}
public void resetEncryptionSecurityManager() {
this.encryptionHandle = null;
}
public void resetDecryptionSecurityManager() {
this.decryptionHandle = null;
this.resetEncryptionSecurityManager();
}
public SecurityManager getEncryptionSecurityManager() {
return this.encryptionHandle != null ? this.encryptionHandle.getSecurityManager() : null;
}
public SecurityManager getDecryptionSecurityManager() {
return this.decryptionHandle == null ? null : this.decryptionHandle.getSecurityManager();
}
public Map getDecryptionParameters() throws PDFSecurityAuthorizationException {
if (this.decryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
return this.decryptionHandle.getEncryptionParams();
}
public Map getEncryptionParameters() throws PDFSecurityAuthorizationException {
if (this.encryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for encryption is not set");
}
return this.encryptionHandle.getEncryptionParams();
}
public boolean authenticateSecurity(SecurityHandler handler) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
if (this.decryptionHandle == null) {
throw new PDFSecurityConfigurationException("Security Manager for decryption is not set");
}
return this.authenticateSecurity(handler, null);
}
public boolean authenticateSecurity(Map params, byte[] docID, SecurityHandler handler, DecryptedState decryptedState) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
try {
this.securityHandler = handler;
if (decryptedState == null) {
return handler.authenticate(params, docID);
}
return handler.authenticate(params, docID, decryptedState);
}
catch (PDFSecurityAuthorizationException e) {
this.resetDecryptionSecurityManager();
throw e;
}
catch (PDFSecurityConfigurationException e1) {
this.resetDecryptionSecurityManager();
throw e1;
}
}
public boolean authenticateSecurity(SecurityHandler handler, DecryptedState decryptedState) throws PDFSecurityAuthorizationException, PDFSecurityConfigurationException {
if (this.decryptionHandle == null) {
throw new PDFSecurityConfigurationException("Security Manager for decryption is not set");
}
return this.authenticateSecurity(this.getDecryptionParameters(), this.decryptionHandle.getDocID(), handler, decryptedState);
}
public byte[] encryptString(byte[] content, byte[] addKey) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
if (this.encryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
EncryptionHandler encryption = this.getStringEncryption(this.encryptionHandle);
return encryption.encrypt(content, addKey);
}
public byte[] decryptString(byte[] content, byte[] addKey) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
if (this.decryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
EncryptionHandler encryption = this.getStringEncryption(this.decryptionHandle);
return encryption.decrypt(content, addKey);
}
public void encryptStream(String name, InputByteStream src, OutputByteStream dest, byte[] addKey) throws PDFSecurityConfigurationException, PDFIOException, PDFSecurityAuthorizationException {
if (this.encryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
EncryptionHandler encryption = this.getStreamEncryption(name, this.encryptionHandle);
encryption.encrypt(src, dest, addKey);
}
public void decryptStream(String name, InputByteStream src, OutputByteStream dest, byte[] addKey) throws PDFSecurityConfigurationException, PDFIOException, PDFSecurityAuthorizationException {
if (this.decryptionHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
EncryptionHandler encryption = this.getStreamEncryption(name, this.decryptionHandle);
encryption.decrypt(src, dest, addKey);
}
private EncryptionHandler getStringEncryption(EncryptionState encryptHandle) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
Map encryptParams = encryptHandle.getEncryptionParams();
String filterName = (String)encryptParams.get("StrF");
if (filterName == null && encryptParams.containsKey("CF")) {
filterName = "Identity";
}
if (filterName == null) {
filterName = (String)encryptParams.get("Filter");
}
return this.getStreamEncryption(filterName, encryptHandle);
}
private EncryptionHandler getStreamEncryption(String cryptName, EncryptionState encryptHandle) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
if (encryptHandle == null) {
throw new PDFSecurityAuthorizationException("Security Manager for decryption is not set");
}
String filterName = cryptName;
Map encryptParams = encryptHandle.getEncryptionParams();
String handlerName = (String)encryptParams.get("Filter");
if (cryptName == null) {
filterName = (String)encryptParams.get("StmF");
if (filterName == null && encryptParams.containsKey("CF")) {
filterName = "Identity";
}
if (filterName == null) {
filterName = handlerName;
}
}
if (!encryptHandle.getEncryptionHandlers().containsKey(filterName)) {
encryptHandle.getEncryptionHandlers().put(filterName, encryptHandle.getSecurityManager().getSecurityHandler(handlerName, encryptParams).getEncryptionHandler(filterName, encryptParams, encryptHandle.getDocID()));
}
return (EncryptionHandler)encryptHandle.getEncryptionHandlers().get(filterName);
}
public EncryptionHandler getStreamDecryptionHandler(String name) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
return this.getStreamEncryption(name, this.decryptionHandle);
}
public EncryptionHandler getStreamEncryptionHandler(String name) throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
return this.getStreamEncryption(name, this.encryptionHandle);
}
public EncryptionHandler getStringDecryptionHandler() throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
return this.getStringEncryption(this.decryptionHandle);
}
public EncryptionHandler getStringEncryptionHandler() throws PDFSecurityConfigurationException, PDFSecurityAuthorizationException {
return this.getStringEncryption(this.encryptionHandle);
}
public SecurityHandler getSecurityHandler() {
return this.securityHandler;
}
private static class EncryptionState {
private SecurityManager mSecurityMgr = null;
private Map mEncryptParams = null;
private byte[] mDocID = null;
private HashMap mEncryptHandlers = new HashMap();
EncryptionState(SecurityManager secMgr, Map encryptParams, byte[] docID) {
this.mSecurityMgr = secMgr;
this.mEncryptParams = encryptParams;
this.mDocID = docID;
}
SecurityManager getSecurityManager() {
return this.mSecurityMgr;
}
Map getEncryptionParams() {
return this.mEncryptParams;
}
byte[] getDocID() {
return this.mDocID;
}
Map getEncryptionHandlers() {
return this.mEncryptHandlers;
}
}
}