Skip to content

Commit efeb4a7

Browse files
author
Samuel Huylebroeck
committed
Allow security provider to be set in LtvVerifier and LtvVerification
DEVSIX-1286, SUP-2073
1 parent ed30b7a commit efeb4a7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

sign/src/main/java/com/itextpdf/signatures/LtvVerification.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class LtvVerification {
8181
private PdfAcroForm acroForm;
8282
private Map<PdfName, ValidationData> validated = new HashMap<>();
8383
private boolean used = false;
84+
private String securityProviderCode = null;
8485
/**
8586
* What type of verification to include.
8687
*/
@@ -139,9 +140,22 @@ public enum CertificateInclusion {
139140
* @param document The {@link PdfDocument} to apply the validation to.
140141
*/
141142
public LtvVerification(PdfDocument document) {
143+
this(document,null);
144+
}
145+
146+
/**
147+
* The verification constructor. This class should only be created with
148+
* PdfStamper.getLtvVerification() otherwise the information will not be
149+
* added to the Pdf.
150+
*
151+
* @param document The {@link PdfDocument} to apply the validation to.
152+
* @param securityProviderCode Security provider to use
153+
*/
154+
public LtvVerification(PdfDocument document, String securityProviderCode){
142155
this.document = document;
143156
this.acroForm = PdfAcroForm.getAcroForm(document, true);
144157
this.sgnUtil = new SignatureUtil(document);
158+
this.securityProviderCode = securityProviderCode;
145159
}
146160

147161
/**
@@ -160,7 +174,7 @@ public LtvVerification(PdfDocument document) {
160174
public boolean addVerification(String signatureName, IOcspClient ocsp, ICrlClient crl, CertificateOption certOption, Level level, CertificateInclusion certInclude) throws IOException, GeneralSecurityException {
161175
if (used)
162176
throw new IllegalStateException(PdfException.VerificationAlreadyOutput);
163-
PdfPKCS7 pk = sgnUtil.verifySignature(signatureName, null);
177+
PdfPKCS7 pk = sgnUtil.verifySignature(signatureName, securityProviderCode);
164178
LOGGER.info("Adding verification for " + signatureName);
165179
Certificate[] xc = pk.getCertificates();
166180
X509Certificate cert;

sign/src/main/java/com/itextpdf/signatures/LtvVerifier.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public class LtvVerifier extends RootStoreVerifier {
9292
protected boolean latestRevision = true;
9393
/** The document security store for the revision that is being verified */
9494
protected PdfDictionary dss;
95+
/** Security provider to use, use null for default*/
96+
protected String securityProviderCode = null;
9597

9698
private SignatureUtil sgnUtil;
9799

@@ -101,7 +103,12 @@ public class LtvVerifier extends RootStoreVerifier {
101103
* @throws GeneralSecurityException
102104
*/
103105
public LtvVerifier(PdfDocument document) throws GeneralSecurityException {
106+
this(document,null);
107+
}
108+
109+
public LtvVerifier(PdfDocument document, String securityProviderCode) throws GeneralSecurityException {
104110
super(null);
111+
this.securityProviderCode = securityProviderCode;
105112
this.document = document;
106113
this.acroForm = PdfAcroForm.getAcroForm(document, true);
107114
this.sgnUtil = new SignatureUtil(document);
@@ -142,7 +149,7 @@ public void setVerifyRootCertificate(boolean verifyRootCertificate) {
142149
* @throws GeneralSecurityException
143150
*/
144151
protected PdfPKCS7 coversWholeDocument() throws GeneralSecurityException {
145-
PdfPKCS7 pkcs7 = sgnUtil.verifySignature(signatureName, null);
152+
PdfPKCS7 pkcs7 = sgnUtil.verifySignature(signatureName, securityProviderCode);
146153
if (sgnUtil.signatureCoversWholeDocument(signatureName)) {
147154
LOGGER.info("The timestamp covers whole document.");
148155
}

sign/src/test/java/com/itextpdf/signatures/verify/LtvVerifierTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,19 @@ public void validLtvDocTest01() throws IOException, GeneralSecurityException {
9090

9191
Assert.assertEquals(7, verificationMessages.size());
9292
}
93+
@Test
94+
public void validLtvDocTest02() throws IOException, GeneralSecurityException {
95+
String ltvTsFileName = sourceFolder + "ltvDoc.pdf";
96+
97+
BouncyCastleProvider provider = new BouncyCastleProvider();
98+
Security.addProvider(provider);
99+
100+
LtvVerifier verifier = new LtvVerifier(new PdfDocument(new PdfReader(ltvTsFileName)), provider.getName());
101+
verifier.setCertificateOption(LtvVerification.CertificateOption.WHOLE_CHAIN);
102+
verifier.setRootStore(Pkcs12FileHelper.initStore(certsSrc + "rootStore.p12", password));
103+
List<VerificationOK> verificationMessages = verifier.verify(null);
104+
105+
Assert.assertEquals(7, verificationMessages.size());
106+
}
107+
93108
}

0 commit comments

Comments
 (0)