Skip to content

Commit

Permalink
Merge pull request #296 from solver-it-sro/AG-151/use-signed-document…
Browse files Browse the repository at this point in the history
…s-signature-type

AG-151 Use already signed document's signature type
  • Loading branch information
celuchmarek authored Oct 12, 2023
2 parents 70b9b6b + ecc0f2d commit ba9d85b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static MimeType fromMimeTypeString(final String mimeTypeString) {

return new AutogramMimeType(mimeTypeString.split(";")[0], null);
}

public static boolean isAsice(MimeType mimeType) {
return mimeType.equals(MimeTypeEnum.ASICE);
}
Expand All @@ -42,4 +43,8 @@ public static boolean isXML(MimeType mimeType) {
public static boolean isXDC(MimeType mimeType) {
return mimeType.equals(XML_DATACONTAINER);
}

public static boolean isPDF(MimeType mimeType) {
return mimeType.equals(MimeTypeEnum.PDF);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.DSSException;
import eu.europa.esig.dss.service.crl.OnlineCRLSource;
import eu.europa.esig.dss.service.http.commons.CommonsDataLoader;
Expand Down Expand Up @@ -166,6 +168,19 @@ public static ValidationReports getSignatureCheckReport(SigningJob job) {
return new ValidationReports(validator.validateDocument(), job);
}

public static SignatureLevel getSignedDocumentSignatureLevel(DSSDocument document) {
var validator = createDocumentValidator(document);
if (validator == null)
return null;

validator.setCertificateVerifier(new CommonCertificateVerifier());
var report = validator.validateDocument().getSimpleReport();
if (report.getSignatureIdList().size() == 0)
return null;

return report.getSignatureFormat(report.getSignatureIdList().get(0));
}

public synchronized boolean areTLsLoaded() {
// TODO: consider validation turned off as well
return validationJob.getSummary().getNumberOfProcessedTLs() > 0;
Expand Down
47 changes: 28 additions & 19 deletions src/main/java/digital/slovensko/autogram/core/SigningJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import eu.europa.esig.dss.asic.xades.signature.ASiCWithXAdESService;
import eu.europa.esig.dss.cades.signature.CAdESService;
import eu.europa.esig.dss.enumerations.MimeType;
import eu.europa.esig.dss.enumerations.MimeTypeEnum;
import eu.europa.esig.dss.enumerations.SignatureLevel;
import eu.europa.esig.dss.model.CommonDocument;
import eu.europa.esig.dss.model.DSSDocument;
Expand All @@ -17,6 +16,8 @@
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.xades.signature.XAdESService;

import static digital.slovensko.autogram.core.AutogramMimeType.isPDF;

public class SigningJob {
private final Responder responder;
private final CommonDocument document;
Expand Down Expand Up @@ -161,37 +162,45 @@ private DSSDocument signDocumentAsPAdeS(SigningKey key) {

public static SigningJob buildFromFile(File file, Responder responder, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var document = new FileDocument(file);
SigningParameters parameters = getParametersForFile(file, checkPDFACompliance, signatureType, isEn319132);
SigningParameters parameters = getParametersForFile(document, checkPDFACompliance, signatureType, isEn319132);
return new SigningJob(document, parameters, responder);
}

public static SigningJob buildFromFileBatch(File file, Autogram autogram, Responder responder, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var document = new FileDocument(file);
var parameters = getParametersForFile(file, checkPDFACompliance, signatureType, isEn319132);
var parameters = getParametersForFile(document, checkPDFACompliance, signatureType, isEn319132);
return new SigningJob(document, parameters, responder);
}

private static SigningParameters getParametersForFile(File file, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var filename = file.getName();

var isFilePdf = filename.endsWith(".pdf");
private static SigningParameters getParametersForFile(FileDocument document, boolean checkPDFACompliance, SignatureLevel signatureType, boolean isEn319132) {
var level = SignatureValidator.getSignedDocumentSignatureLevel(document);
if (level != null) switch (level) {
case PAdES_BASELINE_B:
return SigningParameters.buildForPDF(document.getName(), checkPDFACompliance, isEn319132);
case XAdES_BASELINE_B:
return SigningParameters.buildForASiCWithXAdES(document.getName(), isEn319132);
case CAdES_BASELINE_B:
return SigningParameters.buildForASiCWithCAdES(document.getName(), isEn319132);
default:
;
}

if (isFilePdf)
switch (signatureType) {
case PAdES_BASELINE_B:
return SigningParameters.buildForPDF(filename, checkPDFACompliance, isEn319132);
case XAdES_BASELINE_B:
return SigningParameters.buildForASiCWithXAdES(filename, isEn319132);
case CAdES_BASELINE_B:
return SigningParameters.buildForASiCWithCAdES(filename, isEn319132);
default:
;
}
var filename = document.getName();
if (isPDF(document.getMimeType())) switch (signatureType) {
case PAdES_BASELINE_B:
return SigningParameters.buildForPDF(filename, checkPDFACompliance, isEn319132);
case XAdES_BASELINE_B:
return SigningParameters.buildForASiCWithXAdES(filename, isEn319132);
case CAdES_BASELINE_B:
return SigningParameters.buildForASiCWithCAdES(filename, isEn319132);
default:
;
}

return SigningParameters.buildForASiCWithXAdES(filename, isEn319132);
}

public boolean shouldCheckPDFCompliance() {
return parameters.getCheckPDFACompliance() && document.getMimeType().equals(MimeTypeEnum.PDF);
return parameters.getCheckPDFACompliance() && isPDF(document.getMimeType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,42 @@ public void onUploadButtonAction() {
}

public void onFilesSelected(List<File> list) {
if (list == null)
return;

try {
if (list != null) {
if (list.size() == 0) {
throw new NoFilesSelectedException();
}

var dirsList = list.stream().filter(f -> f.isDirectory()).toList();
var filesList = list.stream().filter(f -> f.isFile()).toList();
if (dirsList.size() == 1 && filesList.size() == 0) {
signDirectory(dirsList.get(0));
} else if (dirsList.size() > 1) {
throw new AutogramException("Zvolili ste viac ako jeden priečinok",
"Priečinky musíte podpísať po jednom",
"Podpisovanie viacerých priečinkov ešte nepodporujeme");
} else if (dirsList.size() == 0 && filesList.size() > 0) {
signFiles(list);
} else {
throw new AutogramException("Zvolili ste zmiešaný výber súborov a priečinkov",
"Podpisovanie zmesi súborov a priečinkov nepodporujeme",
"Priečinky musíte podpísať po jednom, súbory môžete po viacerých");
}
}
if (list.size() == 0)
throw new NoFilesSelectedException();

var dirsList = list.stream().filter(f -> f.isDirectory()).toList();
var filesList = list.stream().filter(f -> f.isFile()).toList();

if (dirsList.size() == 1 && filesList.size() == 0)
signDirectory(dirsList.get(0));

if (dirsList.size() == 0 && filesList.size() > 0)
signFiles(list);

if (dirsList.size() > 1)
throw new AutogramException("Zvolili ste viac ako jeden priečinok",
"Priečinky musíte podpísať po jednom",
"Podpisovanie viacerých priečinkov ešte nepodporujeme");

if (dirsList.size() > 0 && filesList.size() > 0)
throw new AutogramException("Zvolili ste zmiešaný výber súborov a priečinkov",
"Podpisovanie zmesi súborov a priečinkov nepodporujeme",
"Priečinky musíte podpísať po jednom, súbory môžete po viacerých");

} catch (AutogramException e) {
autogram.onSigningFailed(e);
} catch (Exception e) {
throw e;
}
}

private List<File> getFilesList(List<File> list) {
var filesList = list.stream().filter(f -> f.isFile()).toList();
if (filesList.size() == 0) {
if (filesList.size() == 0)
throw new NoFilesSelectedException();
}

return filesList;
}

Expand All @@ -112,11 +114,10 @@ private void signFiles(List<File> list) {

private void signDirectory(File dir) {
var directoryFiles = List.of(dir.listFiles());
if (directoryFiles.size() == 0) {
if (directoryFiles.size() == 0)
throw new EmptyDirectorySelectedException(dir.getAbsolutePath());
}
var filesList = getFilesList(directoryFiles);

var filesList = getFilesList(directoryFiles);
var targetDirectoryName = dir.getName() + "_signed";
var targetDirectory = dir.toPath().getParent().resolve(targetDirectoryName);
autogram.batchStart(filesList.size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
PAdES je všeobecne akceptovaný a&#160;najjednoduchší na používanie, no niektoré úrady môžu ešte vyžadovať XAdES alebo CAdES.
</Text>
</TextFlow>
<TextFlow>
<Text styleClass="autogram-description">
Ak je zvolený dokument už podpísaný iným typom podpisu, bude použitý rovnaký typ a toto nastavenie sa ignoruje.
</Text>
</TextFlow>
</VBox>
<VBox styleClass="autogram-dropdown">
<ChoiceBox fx:id="signatureLevelChoiceBoxBox" />
Expand Down

0 comments on commit ba9d85b

Please sign in to comment.