Skip to content

Commit 7a1c317

Browse files
author
Eugene Bochilo
committed
Create helper class for pades two step signing
DEVSIX-7807
1 parent 0733750 commit 7a1c317

40 files changed

+1461
-235
lines changed

sharpenConfiguration.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@
481481
<fileset reason="Different implementation on .NET and java">
482482
<file path="com/itextpdf/signatures/sign/IsoSignatureExtensionsRoundtripTest.java"/>
483483
<file path="com/itextpdf/signatures/sign/PdfPadesSignerLevelsTest.java"/>
484+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest.java"/>
484485
<file path="com/itextpdf/signatures/sign/LtvSigTest.java"/>
485486
<file path="com/itextpdf/signatures/sign/PadesSignatureLevelTest.java"/>
486487
<file path="com/itextpdf/signatures/sign/PdfPadesSignerTest.java"/>
@@ -608,6 +609,21 @@
608609
<file path="com/itextpdf/signatures/sign/PdfPadesSignerLevelsTest/cmp_prolongDocumentSignaturesTest2_FIPS.pdf"/>
609610
<file path="com/itextpdf/signatures/sign/PdfPadesSignerLevelsTest/cmp_prolongDocumentSignaturesTest3_FIPS.pdf"/>
610611

612+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTATest1.pdf"/>
613+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTATest2.pdf"/>
614+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTATest2_FIPS.pdf"/>
615+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTATest3.pdf"/>
616+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTATest4.pdf"/>
617+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTTest1.pdf"/>
618+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTTest2.pdf"/>
619+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTTest2_FIPS.pdf"/>
620+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTTest3.pdf"/>
621+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineLTTest4.pdf"/>
622+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineTTest1.pdf"/>
623+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineTTest2.pdf"/>
624+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineTTest3.pdf"/>
625+
<file path="com/itextpdf/signatures/sign/PadesTwoPhaseSigningLevelsTest/cmp_twoStepSigningBaselineTTest4.pdf"/>
626+
611627
<file path="com/itextpdf/signatures/sign/PdfPadesSignerTest/cmp_defaultSignerPropertiesTest.pdf"/>
612628

613629
<file path="com/itextpdf/signatures/sign/TimestampSigTest/cmp_timestampTest01.pdf"/>

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

Lines changed: 359 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ private IDERSet getAuthenticatedAttributeSet(byte[] secondDigest, Collection<byt
12001200
v.add(BOUNCY_CASTLE_FACTORY.createASN1ObjectIdentifier(SecurityIDs.ID_AA_SIGNING_CERTIFICATE_V2));
12011201

12021202
IASN1EncodableVector aaV2 = BOUNCY_CASTLE_FACTORY.createASN1EncodableVector();
1203-
if (!digestAlgorithmOid.equals(SecurityIDs.ID_SHA256)) {
1203+
if (!SecurityIDs.ID_SHA256.equals(digestAlgorithmOid)) {
12041204
IAlgorithmIdentifier algoId = BOUNCY_CASTLE_FACTORY.createAlgorithmIdentifier(
12051205
BOUNCY_CASTLE_FACTORY.createASN1ObjectIdentifier(digestAlgorithmOid));
12061206
aaV2.add(algoId);

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -420,26 +420,13 @@ public PdfPadesSigner setTrustedCertificates(List<Certificate> certificateList)
420420
return this;
421421
}
422422

423-
private void performTimestamping(PdfDocument document, OutputStream outputStream, ITSAClient tsaClient)
423+
void performTimestamping(PdfDocument document, OutputStream outputStream, ITSAClient tsaClient)
424424
throws IOException, GeneralSecurityException {
425425
PdfSigner timestampSigner = new PdfSigner(document, outputStream, tempOutputStream, tempFile);
426426
timestampSigner.timestamp(tsaClient, timestampSignatureName);
427427
}
428428

429-
private void performSignDetached(SignerProperties signerProperties, boolean isFinal,
430-
IExternalSignature externalSignature, Certificate[] chain, ITSAClient tsaClient)
431-
throws GeneralSecurityException, IOException {
432-
Certificate[] fullChain = issuingCertificateRetriever.retrieveMissingCertificates(chain);
433-
PdfSigner signer = createPdfSigner(signerProperties, isFinal);
434-
try {
435-
signer.signDetached(externalDigest, externalSignature, fullChain, null, null, tsaClient,
436-
estimatedSize, CryptoStandard.CADES);
437-
} finally {
438-
signer.originalOS.close();
439-
}
440-
}
441-
442-
private PdfSigner createPdfSigner(SignerProperties signerProperties, boolean isFinal) throws IOException {
429+
PdfSigner createPdfSigner(SignerProperties signerProperties, boolean isFinal) throws IOException {
443430
String tempFilePath = null;
444431
if (temporaryDirectoryPath != null) {
445432
tempFilePath = getNextTempFile().getAbsolutePath();
@@ -464,7 +451,7 @@ private PdfSigner createPdfSigner(SignerProperties signerProperties, boolean isF
464451
return signer;
465452
}
466453

467-
private void performLtvVerification(PdfDocument pdfDocument, List<String> signatureNames,
454+
void performLtvVerification(PdfDocument pdfDocument, List<String> signatureNames,
468455
LtvVerification.RevocationDataNecessity revocationDataNecessity)
469456
throws IOException, GeneralSecurityException {
470457
LtvVerification ltvVerification = new LtvVerification(pdfDocument)
@@ -478,43 +465,28 @@ private void performLtvVerification(PdfDocument pdfDocument, List<String> signat
478465
ltvVerification.merge();
479466
}
480467

481-
private void deleteTempFiles() {
468+
void deleteTempFiles() {
482469
for (File tempFile : tempFiles) {
483470
tempFile.delete();
484471
}
485472
}
486473

487-
private OutputStream createOutputStream() throws FileNotFoundException {
474+
OutputStream createOutputStream() throws FileNotFoundException {
488475
if (temporaryDirectoryPath != null) {
489476
return FileUtil.getFileOutputStream(getNextTempFile());
490477
}
491478
tempOutputStream = new ByteArrayOutputStream();
492479
return tempOutputStream;
493480
}
494481

495-
private InputStream createInputStream() throws IOException {
482+
InputStream createInputStream() throws IOException {
496483
if (temporaryDirectoryPath != null) {
497484
return FileUtil.getInputStreamForFile(tempFile);
498485
}
499486
return new ByteArrayInputStream(tempOutputStream.toByteArray());
500487
}
501488

502-
private File getNextTempFile() {
503-
if (!FileUtil.directoryExists(temporaryDirectoryPath)) {
504-
throw new PdfException(MessageFormatUtil.format(SignExceptionMessageConstant.PATH_IS_NOT_DIRECTORY,
505-
temporaryDirectoryPath));
506-
}
507-
synchronized (LOCK_OBJECT) {
508-
do {
509-
increment++;
510-
tempFile = new File(temporaryDirectoryPath + "/" + TEMP_FILE_NAME + increment + ".pdf");
511-
} while (tempFile.exists());
512-
tempFiles.add(tempFile);
513-
}
514-
return tempFile;
515-
}
516-
517-
private void createRevocationClients(Certificate signingCert, boolean clientsRequired) {
489+
void createRevocationClients(Certificate signingCert, boolean clientsRequired) {
518490
if (crlClient == null && ocspClient == null && clientsRequired) {
519491
X509Certificate signingCertificate = (X509Certificate) signingCert;
520492
if (CertificateUtil.getOCSPURL(signingCertificate) == null &&
@@ -530,6 +502,34 @@ private void createRevocationClients(Certificate signingCert, boolean clientsReq
530502
}
531503
}
532504

505+
private void performSignDetached(SignerProperties signerProperties, boolean isFinal,
506+
IExternalSignature externalSignature, Certificate[] chain, ITSAClient tsaClient)
507+
throws GeneralSecurityException, IOException {
508+
Certificate[] fullChain = issuingCertificateRetriever.retrieveMissingCertificates(chain);
509+
PdfSigner signer = createPdfSigner(signerProperties, isFinal);
510+
try {
511+
signer.signDetached(externalDigest, externalSignature, fullChain, null, null, tsaClient,
512+
estimatedSize, CryptoStandard.CADES);
513+
} finally {
514+
signer.originalOS.close();
515+
}
516+
}
517+
518+
private File getNextTempFile() {
519+
if (!FileUtil.directoryExists(temporaryDirectoryPath)) {
520+
throw new PdfException(MessageFormatUtil.format(SignExceptionMessageConstant.PATH_IS_NOT_DIRECTORY,
521+
temporaryDirectoryPath));
522+
}
523+
synchronized (LOCK_OBJECT) {
524+
do {
525+
increment++;
526+
tempFile = new File(temporaryDirectoryPath + "/" + TEMP_FILE_NAME + increment + ".pdf");
527+
} while (tempFile.exists());
528+
tempFiles.add(tempFile);
529+
}
530+
return tempFile;
531+
}
532+
533533
private String getDigestAlgorithm(PrivateKey privateKey) {
534534
String signatureAlgorithm = SignUtils.getPrivateKeyAlgorithm(privateKey);
535535
switch (signatureAlgorithm) {

sign/src/main/java/com/itextpdf/signatures/cms/Attribute.java renamed to sign/src/main/java/com/itextpdf/signatures/cms/CmsAttribute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This file is part of the iText (R) project.
2727
/**
2828
* This class represents Attribute structure.
2929
*/
30-
public class Attribute {
30+
public class CmsAttribute {
3131
private final String type;
3232
private final IASN1Primitive value;
3333

@@ -37,7 +37,7 @@ public class Attribute {
3737
* @param type the type of the attribute
3838
* @param value the value
3939
*/
40-
public Attribute(String type, IASN1Primitive value) {
40+
public CmsAttribute(String type, IASN1Primitive value) {
4141
this.type = type;
4242
this.value = value;
4343
}

0 commit comments

Comments
 (0)