@@ -116,26 +116,91 @@ public SignatureUtil(PdfDocument document) {
116
116
}
117
117
118
118
/**
119
- * Verifies a signature. Further verification can be done on the returned
120
- * {@link PdfPKCS7} object.
119
+ * Prepares an {@link PdfPKCS7} instance for the given signature.
120
+ * This method handles signature parsing and might throw an exception if
121
+ * signature is malformed.
122
+ * <p>
123
+ * The returned {@link PdfPKCS7} can be used to fetch additional info about the signature
124
+ * and also to perform integrity check of data signed by the given signature field.
125
+ * </p>
126
+ * In order to check that given signature covers the current PdfDocument revision please
127
+ * use {@link #signatureCoversWholeDocument(String)} method.
121
128
*
122
- * @param name String the signature field name
123
- * @return PdfPKCS7 object to continue the verification
129
+ * @param name the signature field name
130
+ * @return a {@link PdfPKCS7} instance which can be used to fetch additional info about the signature
131
+ * and also to perform integrity check of data signed by the given signature field.
132
+ * @deprecated This method is deprecated and will be removed in future versions.
133
+ * Please use {@link #readSignatureData(String)} instead.
124
134
*/
135
+ @ Deprecated
125
136
public PdfPKCS7 verifySignature (String name ) {
126
- return verifySignature (name , null );
137
+ return readSignatureData (name , null );
127
138
}
128
139
129
140
/**
130
- * Verifies a signature. Further verification can be done on the returned
131
- * {@link PdfPKCS7} object.
141
+ * Prepares an {@link PdfPKCS7} instance for the given signature.
142
+ * This method handles signature parsing and might throw an exception if
143
+ * signature is malformed.
144
+ * <p>
145
+ * The returned {@link PdfPKCS7} can be used to fetch additional info about the signature
146
+ * and also to perform integrity check of data signed by the given signature field.
147
+ * </p>
148
+ * In order to check that given signature covers the current PdfDocument revision please
149
+ * use {@link #signatureCoversWholeDocument(String)} method.
132
150
*
133
151
* @param name the signature field name
134
- * @param provider the provider or null for the default provider
135
- * @return PdfPKCS7 object to continue the verification
152
+ * @param provider the security provider or null for the default provider
153
+ * @return a {@link PdfPKCS7} instance which can be used to fetch additional info about the signature
154
+ * and also to perform integrity check of data signed by the given signature field.
155
+ * @deprecated This method is deprecated and will be removed in future versions.
156
+ * Please use {@link #readSignatureData(String, String)} instead.
136
157
*/
158
+ @ Deprecated
137
159
public PdfPKCS7 verifySignature (String name , String provider ) {
138
- PdfSignature signature = getSignature (name );
160
+ return readSignatureData (name , provider );
161
+ }
162
+
163
+ /**
164
+ * Prepares an {@link PdfPKCS7} instance for the given signature.
165
+ * This method handles signature parsing and might throw an exception if
166
+ * signature is malformed.
167
+ * <p>
168
+ * The returned {@link PdfPKCS7} can be used to fetch additional info about the signature
169
+ * and also to perform integrity check of data signed by the given signature field.
170
+ * </p>
171
+ * In order to validate the signature it is required to check if it covers the entire file,
172
+ * otherwise one cannot be sure that signature in question indeed signs the data
173
+ * that constitutes current {@link PdfDocument} with all its contents.
174
+ * In order to check that given signature covers the current {@link PdfDocument} please
175
+ * use {@link #signatureCoversWholeDocument(String)} method.
176
+ *
177
+ * @param signatureFieldName the signature field name
178
+ * @return a {@link PdfPKCS7} instance which can be used to fetch additional info about the signature
179
+ * and also to perform integrity check of data signed by the given signature field.
180
+ */
181
+ public PdfPKCS7 readSignatureData (String signatureFieldName ) {
182
+ return readSignatureData (signatureFieldName , null );
183
+ }
184
+
185
+ /**
186
+ * Prepares an {@link PdfPKCS7} instance for the given signature.
187
+ * This method handles signature parsing and might throw an exception if
188
+ * signature is malformed.
189
+ * <p>
190
+ * The returned {@link PdfPKCS7} can be used to fetch additional info about the signature
191
+ * and also to perform integrity check of data signed by the given signature field.
192
+ * </p>
193
+ * Prepared {@link PdfPKCS7} instance calculates digest based on signature's /ByteRange entry.
194
+ * In order to check that /ByteRange is properly defined and given signature indeed covers the current PDF document
195
+ * revision please use {@link #signatureCoversWholeDocument(String)} method.
196
+ *
197
+ * @param signatureFieldName the signature field name
198
+ * @param securityProvider the security provider or null for the default provider
199
+ * @return a {@link PdfPKCS7} instance which can be used to fetch additional info about the signature
200
+ * and also to perform integrity check of data signed by the given signature field.
201
+ */
202
+ public PdfPKCS7 readSignatureData (String signatureFieldName , String securityProvider ) {
203
+ PdfSignature signature = getSignature (signatureFieldName );
139
204
if (signature == null )
140
205
return null ;
141
206
try {
@@ -146,9 +211,9 @@ public PdfPKCS7 verifySignature(String name, String provider) {
146
211
PdfString cert = signature .getPdfObject ().getAsString (PdfName .Cert );
147
212
if (cert == null )
148
213
cert = signature .getPdfObject ().getAsArray (PdfName .Cert ).getAsString (0 );
149
- pk = new PdfPKCS7 (PdfEncodings .convertToBytes (contents .getValue (), null ), cert .getValueBytes (), provider );
214
+ pk = new PdfPKCS7 (PdfEncodings .convertToBytes (contents .getValue (), null ), cert .getValueBytes (), securityProvider );
150
215
} else
151
- pk = new PdfPKCS7 (PdfEncodings .convertToBytes (contents .getValue (), null ), sub , provider );
216
+ pk = new PdfPKCS7 (PdfEncodings .convertToBytes (contents .getValue (), null ), sub , securityProvider );
152
217
updateByteRange (pk , signature );
153
218
PdfString date = signature .getDate ();
154
219
if (date != null )
@@ -289,7 +354,11 @@ public InputStream extractRevision(String field) throws IOException {
289
354
290
355
/**
291
356
* Checks if the signature covers the entire document (except for signature's Contents) or just a part of it.
292
- *
357
+ * <p>
358
+ * If this method does not return {@code true} it means that signature in question does not cover the entire
359
+ * contents of current {@link PdfDocument}. Such signatures cannot be considered as verifying the PDF document,
360
+ * because content that is not covered by signature might have been modified since the signature creation.
361
+ * </p>
293
362
* @param name the signature field name
294
363
* @return true if the signature covers the entire document, false if it doesn't
295
364
*/
0 commit comments