From f39ab43fc0120b7fa97028d40acd7851de8d4a99 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Thu, 24 Nov 2022 14:14:37 +0100 Subject: Repository moved to GitHub: https://github.com/a-sit/pdf-over --- .../pdfover/signer/pdfas/PdfAs4BKUSLConnector.java | 172 ----------------- .../at/asit/pdfover/signer/pdfas/PdfAs4Helper.java | 75 -------- .../asit/pdfover/signer/pdfas/PdfAs4SLRequest.java | 46 ----- .../signer/pdfas/PdfAs4SignatureParameter.java | 213 --------------------- .../signer/pdfas/PdfAs4SignaturePlaceholder.java | 78 -------- .../at/asit/pdfover/signer/pdfas/PdfAs4Signer.java | 183 ------------------ .../pdfover/signer/pdfas/PdfAs4SigningState.java | 82 -------- .../pdfas/exceptions/PdfAs4SLRequestException.java | 34 ---- 8 files changed, 883 deletions(-) delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java delete mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PdfAs4SLRequestException.java (limited to 'pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas') diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java deleted file mode 100644 index 1396255a..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas; - -// Imports -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.asit.pdfover.signer.BkuSlConnector; -import at.asit.pdfover.signer.SignatureException; -import at.asit.pdfover.signer.UserCancelledException; -import at.asit.pdfover.signer.pdfas.exceptions.PdfAs4SLRequestException; -import at.gv.egiz.pdfas.common.exceptions.PDFIOException; -import at.gv.egiz.pdfas.common.exceptions.PdfAsException; -import at.gv.egiz.pdfas.common.exceptions.SLPdfAsException; -import at.gv.egiz.pdfas.common.utils.PDFUtils; -import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; -import at.gv.egiz.pdfas.lib.api.sign.SignParameter; -import at.gv.egiz.sl.schema.CreateCMSSignatureResponseType; -import at.gv.egiz.sl.schema.ErrorResponseType; -import at.gv.egiz.sl.schema.InfoboxReadRequestType; -import at.gv.egiz.sl.schema.InfoboxReadResponseType; -import at.gv.egiz.sl.util.BaseSLConnector; -import at.gv.egiz.sl.util.RequestPackage; -import at.gv.egiz.sl.util.SLMarschaller; - -/** - * - */ -public class PdfAs4BKUSLConnector extends BaseSLConnector { - /** - * SLF4J Logger instance - **/ - private static final Logger log = LoggerFactory.getLogger(PdfAs4BKUSLConnector.class); - - private BkuSlConnector connector; - - /** - * Constructor - * @param connector the BKU SL Connector - */ - public PdfAs4BKUSLConnector(BkuSlConnector connector) { - this.connector = connector; - } - - - /* (non-Javadoc) - * @see at.gv.egiz.sl.util.ISLConnector#sendInfoboxReadRequest(at.gv.egiz.sl.schema.InfoboxReadRequestType, at.gv.egiz.pdfas.lib.api.sign.SignParameter) - */ - @Override - public InfoboxReadResponseType sendInfoboxReadRequest( - InfoboxReadRequestType request, SignParameter parameter) - throws PdfAsException { - JAXBElement element = null; - try { - try { - String slRequestString = SLMarschaller.marshalToString(this.of.createInfoboxReadRequest(request)); - - String slResponse = this.connector.handleSLRequest(new PdfAs4SLRequest(slRequestString, null)); - - element = (JAXBElement) SLMarschaller.unmarshalFromString(slResponse); - } catch (SignatureException e) { - Throwable c = e; - while (c.getCause() != null) - c = c.getCause(); - if (c instanceof IllegalStateException) // TODO: this is a legacy hack, remove it? - throw new UserCancelledException(e); - else - throw e; - } - } catch (JAXBException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (PdfAs4SLRequestException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (SignatureException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (UserCancelledException e) { - throw new SLPdfAsException(6001, "Vorgang durch den Benutzer abgebrochen."); - } - - if (element == null) { - throw new PDFIOException("error.pdf.io.04"); - } - - if (element.getValue() instanceof InfoboxReadResponseType) { - return (InfoboxReadResponseType) element.getValue(); - } else if (element.getValue() instanceof ErrorResponseType) { - ErrorResponseType errorResponseType = (ErrorResponseType)element.getValue(); - throw new SLPdfAsException(errorResponseType.getErrorCode(), errorResponseType.getInfo()); - } - throw new PdfAsException("error.pdf.io.03"); - } - - /** hack cf. #52 */ - public static Exception originalExceptionSwallowedByPDFASNPE = null; - /* (non-Javadoc) - * @see at.gv.egiz.sl.util.ISLConnector#sendCMSRequest(at.gv.egiz.sl.util.RequestPackage, at.gv.egiz.pdfas.lib.api.sign.SignParameter) - */ - @Override - public CreateCMSSignatureResponseType sendCMSRequest(RequestPackage pack, - SignParameter parameter) throws PdfAsException { - /* outermost try blocks are a hack cf. #52 */ -try { try { - JAXBElement element = null; - try { - - String slRequestString = SLMarschaller.marshalToString(this.of.createCreateCMSSignatureRequest(pack.getRequestType())); - - byte[] signatureData = pack.getSignatureData(); - if (IConfigurationConstants.SL_REQUEST_TYPE_UPLOAD.equals(parameter.getConfiguration().getValue(IConfigurationConstants.SL_REQUEST_TYPE))) - signatureData = PDFUtils.blackOutSignature(signatureData, pack.getByteRange()); - - PdfAs4SLRequest slRequest = new PdfAs4SLRequest(slRequestString, signatureData); - - try { - String slResponse = this.connector.handleSLRequest(slRequest); - - element = (JAXBElement) SLMarschaller.unmarshalFromString(slResponse); - } catch (SignatureException e) { - Throwable c = e; - while (c.getCause() != null) - c = c.getCause(); - if (c instanceof IllegalStateException) // TODO: this is a legacy hack, remove it? - throw new UserCancelledException(e); - else - throw e; - } - } catch (JAXBException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (PdfAs4SLRequestException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (SignatureException e) { - throw new PDFIOException("error.pdf.io.03", e); - } catch (UserCancelledException e) { - throw new SLPdfAsException(6001, "Vorgang durch den Benutzer abgebrochen."); - } - - if (element == null) { - throw new PDFIOException("error.pdf.io.05"); - } - - if (element.getValue() instanceof CreateCMSSignatureResponseType) { - CreateCMSSignatureResponseType createCMSSignatureResponseType = (CreateCMSSignatureResponseType) element - .getValue(); - log.trace(createCMSSignatureResponseType.toString()); - return createCMSSignatureResponseType; - } else if (element.getValue() instanceof ErrorResponseType) { - ErrorResponseType errorResponseType = (ErrorResponseType) element - .getValue(); - throw new SLPdfAsException(errorResponseType.getErrorCode(), errorResponseType.getInfo()); - } - throw new PdfAsException("error.pdf.io.03"); -} finally { originalExceptionSwallowedByPDFASNPE = null; } } catch (Exception e) { originalExceptionSwallowedByPDFASNPE = e; throw e; } - } - -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java deleted file mode 100644 index 45dc2724..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas; - -// Imports -import java.io.File; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.asit.pdfover.signer.SignatureException; -import at.gv.egiz.pdfas.lib.api.PdfAs; -import at.gv.egiz.pdfas.lib.api.PdfAsFactory; - -/** - * PDF-AS 4 Helper - */ -public class PdfAs4Helper { - /** - * SLF4J Logger instance - **/ - @SuppressWarnings("unused") - private static final Logger log = LoggerFactory.getLogger(PdfAs4Helper.class); - - /** - * PDF-AS Object - */ - private static PdfAs pdfAs = null; - - /** - * Gets PDF-AS Object - * @return the PDF-AS Object - * @throws SignatureException - */ - public static synchronized PdfAs getPdfAs() throws SignatureException { - if (pdfAs == null) { - try { - pdfAs = createPdfAs(); - } catch(Exception e) { - throw new SignatureException(e); - } - } - return pdfAs; - } - - /** - * Creates PDF-AS Object - * @return the PDF-AS Object - * @throws PdfAsException - */ - private static PdfAs createPdfAs() { - return PdfAsFactory.createPdfAs(new File(getWorkDir())); - } - - /** - * Provides the working directory - * @return the working directory - */ - public static String getWorkDir() { - return System.getProperty("user.home") + "/.pdf-over"; - } -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java deleted file mode 100644 index d4e46e17..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas; - -import at.asit.pdfover.signer.ByteArrayDocumentSource; -import at.asit.pdfover.signer.pdfas.exceptions.PdfAs4SLRequestException; - -/** - * PDF - AS Security Layer Request implementation - */ -public class PdfAs4SLRequest { - - /** - * The security layer request - */ - public final String xmlRequest; - - /** - * The document to be signed - */ - public final ByteArrayDocumentSource signatureData; - - /** - * Default constructor - * @param slRequest - * @param signData - * @throws PdfAs4SLRequestException - */ - public PdfAs4SLRequest(String slRequest, byte[] signData) throws PdfAs4SLRequestException { - this.xmlRequest = slRequest; - this.signatureData = (signData == null ? null : new ByteArrayDocumentSource(signData)); - } -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java deleted file mode 100644 index ee8c2741..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas; - -//Imports - -import iaik.x509.X509Certificate; - -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.image.BufferedImage; -import java.util.Locale; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.asit.pdfover.signer.DocumentSource; -import at.asit.pdfover.signer.Emblem; -import at.asit.pdfover.signer.SignaturePosition; -import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.PdfAs; -import at.gv.egiz.pdfas.lib.api.PdfAsFactory; -import at.gv.egiz.pdfas.lib.api.sign.SignParameter; -import at.asit.pdfover.commons.BKUs; -import at.asit.pdfover.commons.Constants; -import at.asit.pdfover.commons.Profile; - -/** - * Implementation of SignatureParameter for PDF-AS 4 Library - */ -public class PdfAs4SignatureParameter { - /** - * SLF4J Logger instance - **/ - private static final Logger log = LoggerFactory.getLogger(PdfAs4SignatureParameter.class); - - /** - * this is set by CliArguments.InvisibleProfile - * TODO: this is a no good, very bad, hack - */ - public static boolean PROFILE_VISIBILITY = true; - - /** The Signature Position */ - public SignaturePosition signaturePosition = null; - - /** The Signature language */ - public String signatureLanguage = null; - - /** The key identifier */ - public String keyIdentifier = null; - - /** The input document */ - public DocumentSource inputDocument = null; - - /** Holds the emblem */ - public Emblem emblem; - - /** Whether to use PDF/A compatibility */ - public boolean enablePDFACompat; - - /** The signature device */ - public BKUs signatureDevice; - - /** The siganture note, if any */ - public String signatureNote = null; - - /** Whether so look for placeholder signatures or not. */ - public boolean searchForPlaceholderSignatures = false; - - /** The size to scale the logo to (in mm) */ - public double targetLogoSize = Constants.DEFAULT_LOGO_ONLY_SIZE; - - /** The signature profile in use */ - public Profile signatureProfile = Profile.getDefaultProfile(); - - Image getPlaceholder() { - String sigProfile = getPdfAsSignatureProfileId(); - - String sigEmblem = (this.emblem == null ? null : this.emblem.getCachedFileName()); - String sigNote = this.signatureNote; - - try { - X509Certificate cert = new X509Certificate(PdfAs4SignatureParameter.class.getResourceAsStream("/example.cer")); - - PdfAs pdfas = PdfAs4Helper.getPdfAs(); - synchronized (PdfAs4Helper.class) { - Configuration conf = pdfas.getConfiguration(); - if (sigEmblem != null && !sigEmblem.trim().equals("")) { - conf.setValue("sig_obj." + sigProfile + ".value.SIG_LABEL", sigEmblem); - } - if (sigNote != null) { - conf.setValue("sig_obj." + sigProfile + ".value.SIG_NOTE", sigNote); - } - if (this.signatureProfile == Profile.BASE_LOGO) - { - int emblemWidth = (this.emblem != null) ? this.emblem.getWidth() : 1; - int emblemHeight = (this.emblem != null) ? this.emblem.getHeight() : 1; - double aspectRatio = ((double)emblemWidth) / emblemHeight; - double targetWidth = this.targetLogoSize * Constants.PDF_UNITS_PER_MM; - double targetHeight = this.targetLogoSize * Constants.PDF_UNITS_PER_MM; - if (aspectRatio < 1) - targetWidth *= aspectRatio; - else - targetHeight /= aspectRatio; - conf.setValue("sig_obj." + sigProfile + ".table.main.Style.padding", "0"); - conf.setValue("sig_obj." + sigProfile + ".pos", "w:"+targetWidth+";f:0"); - conf.setValue("sig_obj." + sigProfile + ".table.main.Style.imagescaletofit", targetWidth+";"+targetHeight); - } - SignParameter param = PdfAsFactory.createSignParameter(conf, null, null); - param.setSignatureProfileId(sigProfile); - - Image placeholder = pdfas.generateVisibleSignaturePreview(param, cert, 72 * 4); - - // WORKAROUND for #110, manually paint a black border - if ((placeholder != null) && !this.signatureProfile.equals(Profile.BASE_LOGO)) - { - Graphics2D ctx = (Graphics2D)placeholder.getGraphics(); - ctx.setColor(Color.BLACK); - ctx.drawRect(0, 0, placeholder.getWidth(null)-1, placeholder.getHeight(null)-1); - } - - return placeholder; - } - } catch (Exception e) { - log.error("Failed to get signature placeholder", e); - return new BufferedImage(229, 77, BufferedImage.TYPE_INT_RGB); - } - } - - /** - * Gets the Signature Position String for PDF-AS - * - * @return Signature Position String - */ - public String getPdfAsSignaturePosition() { - SignaturePosition in_pos = this.signaturePosition; - - if (in_pos.useAutoPositioning()) - return "p:auto;x:auto;y:auto"; - - if (in_pos.getPage() < 1) - { - return String.format( - (Locale) null, - "p:new;x:%f;y:%f", in_pos.getX(), in_pos.getY()); - } else { - return String.format( - (Locale) null, - "p:%d;x:%f;y:%f", in_pos.getPage(), in_pos.getX(), in_pos.getY()); - } - } - - /** - * Get the Signature Profile ID for this set of parameters - * - * @return the Signature Profile ID - */ - public String getPdfAsSignatureProfileId() { - - //Add Signature Param here// - String profileId; - - if (!PROFILE_VISIBILITY) { - log.debug("Profile visibility was set to false"); - return Profile.INVISIBLE.name(); - } - - switch (this.signatureProfile) { - case BASE_LOGO: - case INVISIBLE: - return this.signatureProfile.name(); - case AMTSSIGNATURBLOCK: - profileId = this.signatureProfile.name(); - profileId += getLangProfilePart(this.signatureLanguage); - profileId += "_RECOMMENDED"; - return profileId; - default: - profileId = this.signatureProfile.name(); - profileId += getLangProfilePart(this.signatureLanguage); - break; - } - - if (this.signatureNote != null) - profileId += "_NOTE"; - - if (this.enablePDFACompat) - profileId += "_PDFA"; - - log.debug("Profile ID: {}", profileId); - return profileId; - } - - private static String getLangProfilePart(String lang) { - return ("en".equals(lang)) ? "_EN" : "_DE"; - } -} - - - diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java deleted file mode 100644 index 04bbcfdf..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignaturePlaceholder.java +++ /dev/null @@ -1,78 +0,0 @@ -package at.asit.pdfover.signer.pdfas; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.TreeMap; -import java.util.function.Consumer; - -import at.asit.pdfover.commons.Profile; -import at.asit.pdfover.commons.utils.ImageUtil; - -/** - * caches placeholders for signature parameters (placeholder generation is pretty slow) - */ -public final class PdfAs4SignaturePlaceholder implements Runnable { - private static TreeMap cache = new TreeMap<>( - Comparator - .comparing(PdfAs4SignatureParameter::getPdfAsSignatureProfileId) - .thenComparing((p) -> { return (p.emblem != null) ? p.emblem.getOriginalFileHash() : ""; }) - .thenComparing((p) -> { return p.signatureNote; }, Comparator.nullsFirst(String::compareTo)) - .thenComparing((p) -> { return p.targetLogoSize; }) - ); - - /** - * request a placeholder for the specified parameter asynchronously - * @param callback the callback to be invoked on completion (may also be invoked before this function returns!) - */ - public static void For(PdfAs4SignatureParameter param, Consumer callback) { - synchronized(cache) { - cache.computeIfAbsent(param, (p) -> new PdfAs4SignaturePlaceholder(p)).AddCallback(callback); - } - } - - private final PdfAs4SignatureParameter param; - private PdfAs4SignaturePlaceholder(PdfAs4SignatureParameter param) { - this.param = param; - new Thread(this).start(); - } - - private java.awt.image.BufferedImage awtImageData; - public boolean hasImage() { return ((this.awtImageData != null) && (this.swtImageData != null)); } - /** AWT image data for the placeholder */ - public java.awt.image.BufferedImage getAWTImage() { return this.awtImageData; } - private org.eclipse.swt.graphics.ImageData swtImageData; - /** SWT image data for the placeholder */ - public org.eclipse.swt.graphics.ImageData getSWTImage() { return this.swtImageData; } - - private ArrayList> callbacks = new ArrayList<>(); - private void AddCallback(Consumer c) { - synchronized (this) { - if (this.callbacks != null) - { - this.callbacks.add(c); - return; - } /* else... */ - } - /* ... else, not synchronized */ - c.accept(this); - } - - - @Override - public void run() { - if (!Profile.INVISIBLE.name().equals(this.param.getPdfAsSignatureProfileId())) { - this.awtImageData = (java.awt.image.BufferedImage) this.param.getPlaceholder(); - this.swtImageData = ImageUtil.convertToSWT(this.awtImageData); - } else { - this.awtImageData = null; - this.swtImageData = null; - } - ArrayList> _callbacks; - synchronized (this) { - _callbacks = this.callbacks; - this.callbacks = null; - } - _callbacks.forEach((c) -> c.accept(this)); - } - -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java deleted file mode 100644 index 7afa4ec5..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java +++ /dev/null @@ -1,183 +0,0 @@ -package at.asit.pdfover.signer.pdfas; - -import java.io.ByteArrayOutputStream; -import java.util.Objects; -import java.util.UUID; - -import javax.activation.DataSource; - -import at.asit.pdfover.commons.Constants; -import at.asit.pdfover.commons.Profile; -import at.asit.pdfover.signer.ByteArrayDocumentSource; -import at.asit.pdfover.signer.SignResult; -import at.asit.pdfover.signer.SignatureException; -import at.asit.pdfover.signer.SignaturePosition; -import at.asit.pdfover.signer.UserCancelledException; -import at.gv.egiz.pdfas.common.exceptions.PDFASError; -import at.gv.egiz.pdfas.common.exceptions.PdfAsException; -import at.gv.egiz.pdfas.common.exceptions.SLPdfAsException; -import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; -import at.gv.egiz.pdfas.lib.api.Configuration; -import at.gv.egiz.pdfas.lib.api.IConfigurationConstants; -import at.gv.egiz.pdfas.lib.api.PdfAs; -import at.gv.egiz.pdfas.lib.api.PdfAsFactory; -import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; -import at.gv.egiz.pdfas.lib.api.sign.SignParameter; -import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; -import at.gv.egiz.sl.util.ISLConnector; -import at.knowcenter.wag.egov.egiz.pdf.TablePos; - -/** - * PDF AS Signer Implementation - */ -public class PdfAs4Signer { - - /** - * The template URL - */ - protected static final String URL_TEMPLATE = "http://pdfover.4.gv.at/template"; - - /** - * Location reference string - */ - protected static final String LOC_REF = "" + URL_TEMPLATE - + ""; - - public static PdfAs4SigningState prepare(PdfAs4SignatureParameter parameter) throws SignatureException { - - if (parameter == null) { - throw new SignatureException("Incorrect SignatureParameter!"); - } - - String sigProfile = parameter.getPdfAsSignatureProfileId(); - String sigEmblem = (parameter.emblem == null ? null : parameter.emblem.getCachedFileName()); - String sigNote = parameter.signatureNote; - String sigPos = null; - if (parameter.signaturePosition != null) { - sigPos = parameter.getPdfAsSignaturePosition(); - } - - PdfAs pdfas = PdfAs4Helper.getPdfAs(); - synchronized (PdfAs4Helper.class) { - Configuration config = pdfas.getConfiguration(); - if (sigEmblem != null && !sigEmblem.trim().isEmpty()) { - config.setValue("sig_obj." + sigProfile + ".value.SIG_LABEL", sigEmblem); - } - - if(sigNote != null) { - config.setValue("sig_obj." + sigProfile + ".value.SIG_NOTE", sigNote); - } - - // TODO encapsulate this parameter magic in PdfAs4SignatureParameter - if (parameter.signatureProfile == Profile.BASE_LOGO) - { - int emblemWidth = (parameter.emblem != null) ? parameter.emblem.getWidth() : 1; - int emblemHeight = (parameter.emblem != null) ? parameter.emblem.getHeight() : 1; - double aspectRatio = ((double)emblemWidth) / emblemHeight; - double targetWidth = parameter.targetLogoSize * Constants.PDF_UNITS_PER_MM; - double targetHeight = parameter.targetLogoSize * Constants.PDF_UNITS_PER_MM; - if (aspectRatio < 1) - targetWidth *= aspectRatio; - else - targetHeight /= aspectRatio; - config.setValue("sig_obj." + sigProfile + ".table.main.Style.padding", "0"); - config.setValue("sig_obj." + sigProfile + ".pos", "w:"+targetWidth+";f:0"); - config.setValue("sig_obj." + sigProfile + ".table.main.Style.imagescaletofit", targetWidth+";"+targetHeight); - } - - PdfAs4SigningState state = new PdfAs4SigningState(); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - DataSource input = new ByteArrayDataSource(parameter.inputDocument.getByteArray()); - SignParameter param = PdfAsFactory.createSignParameter(config, input, output); - if (sigPos != null) { - param.setSignaturePosition(sigPos); - } - param.setSignatureProfileId(sigProfile); - String id = UUID.randomUUID().toString(); - param.setTransactionId(id); - - if (parameter.searchForPlaceholderSignatures) { - param.getConfiguration().setValue(IConfigurationConstants.PLACEHOLDER_MODE, "1"); - param.getConfiguration().setValue(IConfigurationConstants.PLACEHOLDER_SEARCH_ENABLED, IConfigurationConstants.TRUE); - } - - state.signParameter = param; - state.output = output; - return state; - } - } - - public static SignResult sign(PdfAs4SigningState state) throws SignatureException, UserCancelledException { - try { - if (state == null) { - throw new SignatureException("Incorrect SigningState!"); - } - - PdfAs pdfas = PdfAs4Helper.getPdfAs(); - synchronized (PdfAs4Helper.class) { - // Retrieve objects - SignParameter param = state.signParameter; - - Configuration config = param.getConfiguration(); - config.setValue(IConfigurationConstants.SL_REQUEST_TYPE, - state.useBase64Request ? - IConfigurationConstants.SL_REQUEST_TYPE_BASE64 : - IConfigurationConstants.SL_REQUEST_TYPE_UPLOAD); - - IPlainSigner signer; - if (state.bkuConnector != null) { - ISLConnector connector = new PdfAs4BKUSLConnector(state.bkuConnector); - signer = new PAdESSigner(connector); - } else if (state.hasKeystoreSigner()) { - signer = state.getKeystoreSigner(); - } else { - throw new SignatureException("SigningState doesn't have a signer"); - } - param.setPlainSigner(signer); - - pdfas.sign(param); - - SignResult result = new SignResult(); - - if (param.getSignaturePosition() != null) { - TablePos tp = new TablePos(param.getSignaturePosition()); - SignaturePosition sp; - if (tp.isXauto() && tp.isYauto()) - sp = new SignaturePosition(); - else if (tp.isPauto()) - sp = new SignaturePosition(tp.getPosX(), tp.getPosY()); - else if (param.getSignatureProfileId().contains(Profile.AMTSSIGNATURBLOCK.name())) - sp = new SignaturePosition(); - else - sp = new SignaturePosition(tp.getPosX(), tp.getPosY(), tp.getPage()); - result.setSignaturePosition(sp); - } - - result.setSignedDocument(new ByteArrayDocumentSource(state.output.toByteArray())); - return result; - } - } catch (PdfAsException | PDFASError ex) { - // workaround for PDF-AS nullpointerexception intercepting the actual exception - // cf. issue #52 - // this is a bit of a hack... - Exception e = ex; - { - if ((e instanceof PDFASError) && (e.getCause() instanceof NullPointerException)) - e = Objects.requireNonNullElse(PdfAs4BKUSLConnector.originalExceptionSwallowedByPDFASNPE, e); - } - - { - Throwable rootCause = e; - while (rootCause.getCause() != null) - rootCause = rootCause.getCause(); - try { /* error code 60xx is user cancellation */ - int errorCode = ((SLPdfAsException)rootCause).getCode(); - if ((6000 <= errorCode) && (errorCode <= 6099)) - throw new UserCancelledException(); - } catch (ClassCastException e2) { /* fall through to wrapped throw */} - } - - throw new SignatureException(e); - } - } -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java deleted file mode 100644 index 3ff0f75a..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas; - -//Imports -import java.io.ByteArrayOutputStream; - -import at.asit.pdfover.signer.BkuSlConnector; -import at.asit.pdfover.signer.SignatureException; -import at.gv.egiz.pdfas.common.exceptions.PDFASError; -import at.gv.egiz.pdfas.lib.api.sign.IPlainSigner; -import at.gv.egiz.pdfas.lib.api.sign.SignParameter; -import at.gv.egiz.pdfas.sigs.pades.PAdESSignerKeystore; - -/** - * Signing State for PDFAS Wrapper - */ -public class PdfAs4SigningState { - - /** - * The Signature Layer request - */ - public PdfAs4SLRequest signatureRequest; - - /** - * The Signature Layer response - */ - public String signatureResponse; - - /** - * The Sign Parameters - */ - public SignParameter signParameter; - - public ByteArrayOutputStream output; - - public BkuSlConnector bkuConnector = null; - - private IPlainSigner keystoreSigner = null; - - public boolean useBase64Request; - - /* (non-Javadoc) - * @see at.asit.pdfover.signator.SigningState#setKSSigner(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - public void setKeystoreSigner(String file, String alias, String kspassword, - String keypassword, String type) throws SignatureException { - try { - this.keystoreSigner = new PAdESSignerKeystore(file, alias, kspassword, keypassword, type); - } catch (PDFASError e) { - throw new SignatureException(e); - } - } - - /** - * @return whether a KS signer was set - */ - public boolean hasKeystoreSigner() { - return this.keystoreSigner != null; - } - - /** - * @return the KS signer - */ - public IPlainSigner getKeystoreSigner() { - return this.keystoreSigner; - } - -} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PdfAs4SLRequestException.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PdfAs4SLRequestException.java deleted file mode 100644 index ab50e78b..00000000 --- a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PdfAs4SLRequestException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2012 by A-SIT, Secure Information Technology Center Austria - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * http://joinup.ec.europa.eu/software/page/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package at.asit.pdfover.signer.pdfas.exceptions; - -/** - * - */ -public class PdfAs4SLRequestException extends Exception { - /** - * - */ - private static final long serialVersionUID = 6855855001105199269L; - - /** - * Constructor - * @param msg - */ - public PdfAs4SLRequestException(String msg) { - super(msg); - } -} -- cgit v1.2.3