Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2017 iText Group NV
Authors: Bruno Lowagie, Paulo Soares, et al.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation with the addition of the
following permission added to Section 15 as permitted in Section 7(a):
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
OF THIRD PARTY RIGHTS
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, see http://www.gnu.org/licenses or write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA, 02110-1301 USA, or download the license from the following URL:
http://itextpdf.com/terms-of-use/
The interactive user interfaces in modified source and object code versions
of this program must display Appropriate Legal Notices, as required under
Section 5 of the GNU Affero General Public License.
In accordance with Section 7(b) of the GNU Affero General Public License,
a covered work must retain the producer line in every PDF that is created
or manipulated using iText.
You can be released from the requirements of the license by purchasing
a commercial license. Buying such a license is mandatory as soon as you
develop commercial activities involving the iText software without
disclosing the source code of your own applications.
These activities include: offering paid services to customers as an ASP,
serving PDFs on the fly in a web application, shipping iText with a closed
source product.
For more information, please contact iText Software Corp. at this
address: [email protected]
*/
package com.itextpdf.signatures;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.source.RASInputStream;
import com.itextpdf.io.source.RandomAccessFileOrArray;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.io.source.WindowRandomAccessSource;
import com.itextpdf.kernel.PdfException;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfDate;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.kernel.pdf.PdfString;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Utility class that provides several convenience methods concerning digital signatures.
*/
// TODO: REFACTOR. At this moment this serves as storage for some signature-related methods from iText 5 AcroFields
public class SignatureUtil {
private PdfDocument document;
private PdfAcroForm acroForm;
private Map sigNames;
private List orderedSignatureNames;
private int totalRevisions;
/**
* Creates a SignatureUtil instance. Sets the acroForm field to the acroForm in the PdfDocument.
* iText will create a new AcroForm if the PdfDocument doesn't contain one.
*
* @param document PdfDocument to be inspected
*/
public SignatureUtil(PdfDocument document) {
this.document = document;
this.acroForm = PdfAcroForm.getAcroForm(document, true);
}
/**
* Verifies a signature. Further verification can be done on the returned
* {@link PdfPKCS7} object.
*
* @param name String the signature field name
* @return PdfPKCS7 object to continue the verification
*/
public PdfPKCS7 verifySignature(String name) {
return verifySignature(name, null);
}
/**
* Verifies a signature. Further verification can be done on the returned
* {@link PdfPKCS7} object.
*
* @param name the signature field name
* @param provider the provider or null for the default provider
* @return PdfPKCS7 object to continue the verification
*/
public PdfPKCS7 verifySignature(String name, String provider) {
PdfSignature signature = getSignature(name);
if (signature == null)
return null;
try {
PdfName sub = signature.getSubFilter();
PdfString contents = signature.getContents();
PdfPKCS7 pk = null;
if (sub.equals(PdfName.Adbe_x509_rsa_sha1)) {
PdfString cert = signature.getPdfObject().getAsString(PdfName.Cert);
if (cert == null)
cert = signature.getPdfObject().getAsArray(PdfName.Cert).getAsString(0);
pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), cert.getValueBytes(), provider);
}
else
pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), sub, provider);
updateByteRange(pk, signature);
PdfString date = signature.getDate();
if (date != null)
pk.setSignDate(PdfDate.decode(date.toString()));
String signName = signature.getName();
pk.setSignName(signName);
String reason = signature.getReason();
if (reason != null)
pk.setReason(reason);
String location = signature.getLocation();
if (location != null)
pk.setLocation(location);
return pk;
}
catch (Exception e) {
throw new PdfException(e);
}
}
public PdfSignature getSignature(String name) {
PdfDictionary sigDict = getSignatureDictionary(name);
return sigDict != null ? new PdfSignature(sigDict) : null;
}
/**
* Gets the signature dictionary, the one keyed by /V.
*
* @param name the field name
* @return the signature dictionary keyed by /V or null if the field is not
* a signature
*/
public PdfDictionary getSignatureDictionary(String name) {
getSignatureNames();
if (!sigNames.containsKey(name))
return null;
PdfFormField field = acroForm.getField(name);
PdfDictionary merged = field.getPdfObject();
return merged.getAsDictionary(PdfName.V);
}
/* Updates the /ByteRange with the provided value */
private void updateByteRange(PdfPKCS7 pkcs7, PdfSignature signature) {
PdfArray b = signature.getByteRange();
RandomAccessFileOrArray rf = document.getReader().getSafeFile();
InputStream rg = null;
try {
rg = new RASInputStream(new RandomAccessSourceFactory().createRanged(rf.createSourceView(), asLongArray(b)));
byte[] buf = new byte[8192];
int rd;
while ((rd = rg.read(buf, 0, buf.length)) > 0) {
pkcs7.update(buf, 0, rd);
}
}
catch (Exception e) {
throw new PdfException(e);
} finally {
try {
if (rg != null) rg.close();
} catch (IOException e) {
// this really shouldn't ever happen - the source view we use is based on a Safe view, which is a no-op anyway
throw new PdfException(e);
}
}
}
/**
* Gets the field names that have signatures and are signed.
*
* @return List containing the field names that have signatures and are signed
*/
public List getSignatureNames() {
if (sigNames != null)
return new ArrayList<>(orderedSignatureNames);
sigNames = new HashMap<>();
orderedSignatureNames = new ArrayList<>();
List