我想使用java代码验证Docusign签名的PDF。我需要验证签名后是否修改PDF并且由适当的用户签名。怎么做?
以下是我用于使用JSignPdf库验证它的代码。这工作到今年3月7日,但现在失败了。没有得到什么问题。如果签名PDF中的Docusign签名证书有任何更改,请引导我。
import java.io.File;
import java.io.InputStream;
import net.sf.jsignpdf.verify.SignatureVerification;
import net.sf.jsignpdf.verify.VerificationResult;
import net.sf.jsignpdf.verify.VerifierLogic;
import org.apache.log4j.Logger;
import com.vit.common.signing.VerificationOutput;
public class PdfVerifier{
private static Logger logger = Logger.getLogger(PdfVerifier.class);
public VerificationOutput verify(String signedFileName) throws Exception {
return verify(signedFileName, null);
}
public VerificationOutput verify(String pdfFileName, String[] certFileNames) throws Exception {
VerificationOutput ret = new VerificationOutput();
logger.info("starting checking ... " + pdfFileName);
try {
boolean failFast = true;
final VerifierLogic tmpLogic = new VerifierLogic("", "", "");
tmpLogic.setFailFast(failFast);
if (certFileNames==null || certFileNames.length==0) {
//TODO: we may just do this once per jvm launching
ClassLoader classLoader = getClass().getClassLoader();
InputStream ins = null;
try {
ins = classLoader.getResourceAsStream("docusign.cer");
tmpLogic.addX509CertFile(ins);
ins = classLoader.getResourceAsStream("docusign_entrust.cer");
tmpLogic.addX509CertFile(ins);
} finally {
try {
if (ins!=null) ins.close();
} catch(Exception err) {}
}
} else {
for (String tmpCFile : certFileNames) {
tmpLogic.addX509CertFile(tmpCFile);
}
}
int exitCode = 0;
String tmpFilePath = pdfFileName;
int exitCodeForFile = 0;
logger.info("Verifying " + tmpFilePath);
final File tmpFile = new File(tmpFilePath);
if (!tmpFile.canRead()) {
exitCodeForFile = SignatureVerification.SIG_STAT_CODE_ERROR_FILE_NOT_READABLE;
logger.error("Couln't read the file " + tmpFile + ". Check the path and permissions.");
exitCode = Math.max(exitCode, exitCodeForFile);
ret.setPass(false);
ret.setMessage("Couln't read the file " + tmpFile);
}
final VerificationResult tmpResult = tmpLogic.verify(tmpFilePath, null);
if (tmpResult.getException() != null) {
Exception e = tmpResult.getException();
exitCodeForFile = SignatureVerification.SIG_STAT_CODE_ERROR_UNEXPECTED_PROBLEM;
exitCode = Math.max(exitCode, exitCodeForFile);
ret.setPass(false);
ret.setMessage(e.getMessage());
ret.setException(e);
logger.error("failed digital signature verification due to " + e.getMessage(), e);
} else {
logger.info("Total revisions: " + tmpResult.getTotalRevisions());
exitCodeForFile = tmpResult.getVerificationResultCode();
boolean modified = false;
boolean signatureVerified = false;
for (SignatureVerification verification: tmpResult.getVerifications()) {
if (verification.isModified()) modified = true;
if (verification.getFails()==null) {
signatureVerified = true;
//System.out.println(verification.getSubject());
String subject = verification.getSubject();
String[] firstSplit = subject.split(",");
String email = firstSplit[0].substring(4, firstSplit[0].length()-1);
//System.out.println(email);
ret.setSignerEmail(email);
} else if (verification.isSignCertTrustedAndValid()) {
signatureVerified = true;
}
}
if (modified) {
ret.setPass(false);
ret.setMessage("pdf has been modified");
} else {
if (signatureVerified) {
ret.setPass(true);
ret.setMessage("Both content and signing cert have been verified");
} else {
ret.setPass(false);
ret.setMessage("signature not verified");
}
}
logger.info("verification passed? " + ret.isPass());
if (!ret.isPass()) {
logger.info("reason failed is " + ret.getMessage());
}
}
exitCode = Math.max(exitCode, exitCodeForFile);
} catch(Exception e) {
throw e;
}
return ret;
}
}