From e518f2a61c5d0f08e0f0f0505d9ade4b47dfe7e6 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Fri, 15 Jul 2022 09:29:54 +0200 Subject: flatten signer directory --- .../pdfover/signer/pdfas/PdfAs4BKUSLConnector.java | 150 ++++++++++++++ .../at/asit/pdfover/signer/pdfas/PdfAs4Helper.java | 76 +++++++ .../asit/pdfover/signer/pdfas/PdfAs4SLRequest.java | 47 +++++ .../signer/pdfas/PdfAs4SignatureParameter.java | 230 +++++++++++++++++++++ .../at/asit/pdfover/signer/pdfas/PdfAs4Signer.java | 164 +++++++++++++++ .../pdfover/signer/pdfas/PdfAs4SignerFactory.java | 31 +++ .../pdfover/signer/pdfas/PdfAs4SigningState.java | 192 +++++++++++++++++ .../pdfas/exceptions/PdfAs4SLRequestException.java | 34 +++ 8 files changed, 924 insertions(+) create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignerFactory.java create mode 100644 pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java create 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') 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 new file mode 100644 index 00000000..124bf43d --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4BKUSLConnector.java @@ -0,0 +1,150 @@ +/* + * 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.signator.BkuSlConnector; +import at.asit.pdfover.signator.SignatureException; +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 { + String slRequestString = SLMarschaller.marshalToString(this.of.createInfoboxReadRequest(request)); + //log.trace(slRequestString); + + PdfAs4SLRequest slRequest = new PdfAs4SLRequest(slRequestString, null); + String slResponse = this.connector.handleSLRequest(slRequest).getSLRespone(); + + element = (JAXBElement) SLMarschaller + .unmarshalFromString(slResponse); + } 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); + } + + if (element == null) { + throw new PDFIOException("error.pdf.io.04"); + } + + if (element.getValue() instanceof InfoboxReadResponseType) { + InfoboxReadResponseType infoboxReadResponseType = (InfoboxReadResponseType) element + .getValue(); + return infoboxReadResponseType; + } 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"); + } + + /* (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 { + JAXBElement element = null; + try { + String slRequestString = SLMarschaller.marshalToString(this.of.createCreateCMSSignatureRequest(pack.getRequestType())); + //log.trace(slRequestString); + + 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); + String slResponse = this.connector.handleSLRequest(slRequest).getSLRespone(); + + element = (JAXBElement) SLMarschaller + .unmarshalFromString(slResponse); + } 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); + } + + 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"); + } +} 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 new file mode 100644 index 00000000..e0b8ff46 --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Helper.java @@ -0,0 +1,76 @@ +/* + * 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.signator.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 new file mode 100644 index 00000000..87b97ac9 --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SLRequest.java @@ -0,0 +1,47 @@ +/* + * 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.asit.pdfover.signator.ByteArrayDocumentSource; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signer.pdfas.exceptions.PdfAs4SLRequestException; + +/** + * PDF - AS Security Layer Request implementation + */ +public class PdfAs4SLRequest extends SLRequest { + + /** + * SLF4J Logger instance + **/ + @SuppressWarnings("unused") + private static final Logger log = LoggerFactory.getLogger(PdfAs4SLRequest.class); + + /** + * Default constructor + * @param slRequest + * @param signData + * @throws PdfAs4SLRequestException + */ + public PdfAs4SLRequest(String slRequest, byte[] signData) throws PdfAs4SLRequestException { + setRequest(slRequest); + setSignatureData(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 new file mode 100644 index 00000000..032416cd --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignatureParameter.java @@ -0,0 +1,230 @@ +/* + * 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.Image; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Locale; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +import at.asit.pdfover.signator.SignatureDimension; +import at.asit.pdfover.signator.SignatureParameter; +import at.asit.pdfover.signator.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.Profile; + +/** + * Implementation of SignatureParameter for PDF-AS 4 Library + */ +public class PdfAs4SignatureParameter extends SignatureParameter { + /** + * The profile ID extension for the German signature block + */ + private static final String PROFILE_ID_LANG_DE = "_DE"; + /** + * The profile ID extension for the English signature block + */ + private static final String PROFILE_ID_LANG_EN = "_EN"; + /** + * The profile ID extension for the signature note + */ + private static final String PROFILE_ID_NOTE = "_NOTE"; + /** + * The profile ID extension for PDF/A compatibility + */ + private static final String PROFILE_ID_PDFA = "_PDFA"; + + private static final String PROFILE_ID_RECOMMENDED = "_RECOMMENDED"; + + /** + * Visibility of signature block + */ + public static boolean PROFILE_VISIBILITY = true; + + private HashMap genericProperties = new HashMap(); + + /** + * This parameters are defining the signature block size + */ + private int sig_w = 229; + private int sig_h = 77; + + /** + * SLF4J Logger instance + **/ + static final Logger log = LoggerFactory + .getLogger(PdfAs4SignatureParameter.class); + private String profile = Profile.getDefaultProfile(); + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SignatureParameter#getPlaceholderDimension() + */ + @Override + public SignatureDimension getPlaceholderDimension() { + return new SignatureDimension(this.sig_w, this.sig_h); + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SignatureParameter#getPlaceholder() + */ + @Override + public Image getPlaceholder() { + String sigProfile = getPdfAsSignatureProfileId(); + + String sigEmblem = (getEmblem() == null ? null : getEmblem().getFileName()); + String sigNote = getProperty("SIG_NOTE"); + + try { + X509Certificate cert = new X509Certificate(PdfAs4SignatureParameter.class.getResourceAsStream("/qualified.cer")); + PdfAs pdfas = PdfAs4Helper.getPdfAs(); + 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); + } + SignParameter param = PdfAsFactory + .createSignParameter(conf, null, null); + param.setSignatureProfileId(sigProfile); + Image img = pdfas.generateVisibleSignaturePreview(param, cert, 72 * 4); + this.sig_w = img.getWidth(null) / 4; + this.sig_h = img.getHeight(null) / 4; + + return img; + } catch (Exception e) { + log.error("Failed to get signature placeholder", e); + return new BufferedImage(getPlaceholderDimension().getWidth(), + getPlaceholderDimension().getHeight(), + BufferedImage.TYPE_INT_RGB); + } + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SignatureParameter#setProperty(java.lang.String, java.lang.String) + */ + @Override + public void setProperty(String key, String value) { + this.genericProperties.put(key, value); + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SignatureParameter#getProperty(java.lang.String) + */ + @Override + public String getProperty(String key) { + return this.genericProperties.get(key); + } + + /** + * Gets the Signature Position String for PDF-AS + * + * @return Signature Position String + */ + public String getPdfAsSignaturePosition() { + SignaturePosition in_pos = getSignaturePosition(); + String out_pos; + + if (!in_pos.useAutoPositioning()) { + if (in_pos.getPage() < 1) { + out_pos = String.format( + (Locale) null, + "p:new;x:%f;y:%f", in_pos.getX(), in_pos.getY()); + } else { + out_pos = String.format( + (Locale) null, + "p:%d;x:%f;y:%f", in_pos.getPage(), in_pos.getX(), in_pos.getY()); + } + } else { + out_pos = "p:auto;x:auto;y:auto"; + } + + return out_pos; + } + + /** + * Get the Signature Profile ID for this set of parameters + * + * @return the Signature Profile ID + */ + public String getPdfAsSignatureProfileId() { + String lang = getSignatureLanguage(); + boolean useNote = (getProperty("SIG_NOTE") != null); + boolean usePdfACompat = (getSignaturePdfACompat()); + + //Add Signature Param here// + String profileId; + + if (!PROFILE_VISIBILITY) { + log.debug("Profile visibility was set to false"); + return Profile.INVISIBLE.name(); + } + + Profile profile = Profile.getProfile(this.profile); + switch (profile) { + case BASE_LOGO: + case INVISIBLE: + return this.profile; + case AMTSSIGNATURBLOCK: + profileId = this.profile; + profileId += getLangProfilePart(lang); + profileId += PROFILE_ID_RECOMMENDED; + return profileId; + default: + profileId = this.profile; + profileId += getLangProfilePart(lang); + break; + } + + if (useNote) + profileId += PROFILE_ID_NOTE; + + if (usePdfACompat) + profileId += PROFILE_ID_PDFA; + + log.debug("Profile ID: {}", profileId); + return profileId; + } + + private String getLangProfilePart(String lang) { + return (lang != null && lang.equals("en")) ? PROFILE_ID_LANG_EN : PROFILE_ID_LANG_DE; + } + + @Override + public void setSignatureProfile(String profile) { + this.profile = profile; + } + + @Override + public String getSignatureProfile() { + return this.profile; + } + +} + + + 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 new file mode 100644 index 00000000..9943ec0e --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java @@ -0,0 +1,164 @@ +package at.asit.pdfover.signer.pdfas; + +import java.io.ByteArrayOutputStream; +import java.util.UUID; + +import javax.activation.DataSource; + +import at.asit.pdfover.commons.Profile; +import at.asit.pdfover.signator.ByteArrayDocumentSource; +import at.asit.pdfover.signator.SignResult; +import at.asit.pdfover.signator.SignResultImpl; +import at.asit.pdfover.signator.SignatureException; +import at.asit.pdfover.signator.SignatureParameter; +import at.asit.pdfover.signator.SignaturePosition; +import at.asit.pdfover.signator.Signer; +import at.asit.pdfover.signator.SigningState; +import at.gv.egiz.pdfas.common.exceptions.PDFASError; +import at.gv.egiz.pdfas.common.exceptions.PdfAsException; +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 implements Signer { + + + + + /** + * 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 + + ""; + + @Override + public SigningState prepare(SignatureParameter parameter) + throws SignatureException { + PdfAs4SignatureParameter sign_para = null; + + if (PdfAs4SignatureParameter.class.isInstance(parameter)) { + sign_para = PdfAs4SignatureParameter.class.cast(parameter); + } + + if (sign_para == null) { + throw new SignatureException("Incorrect SignatureParameter!"); + } + + String sigProfile = sign_para.getPdfAsSignatureProfileId(); + String sigEmblem = (sign_para.getEmblem() == null ? null : sign_para.getEmblem().getFileName()); + String sigNote = sign_para.getProperty("SIG_NOTE"); + String sigPos = null; + if (sign_para.getSignaturePosition() != null) { + sigPos = sign_para.getPdfAsSignaturePosition(); + } + PdfAs pdfas = PdfAs4Helper.getPdfAs(); + 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); + } + + PdfAs4SigningState state = new PdfAs4SigningState(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + DataSource input = new ByteArrayDataSource(parameter.getInputDocument().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.isSearchForPlaceholderSignatures()) { + param.getConfiguration().setValue(IConfigurationConstants.PLACEHOLDER_MODE, "1"); + param.getConfiguration().setValue(IConfigurationConstants.PLACEHOLDER_SEARCH_ENABLED, IConfigurationConstants.TRUE); + } + + state.setSignParameter(param); + state.setOutput(output); + return state; + } + + @Override + public SignResult sign(SigningState state) throws SignatureException { + try { + PdfAs4SigningState sstate = null; + + if (PdfAs4SigningState.class.isInstance(state)) { + sstate = PdfAs4SigningState.class.cast(state); + } + + if (sstate == null) { + throw new SignatureException("Incorrect SigningState!"); + } + + // Retrieve objects + PdfAs pdfas = PdfAs4Helper.getPdfAs(); + + SignParameter param = sstate.getSignParameter(); + + Configuration config = param.getConfiguration(); + config.setValue(IConfigurationConstants.SL_REQUEST_TYPE, + sstate.getUseBase64Request() ? + IConfigurationConstants.SL_REQUEST_TYPE_BASE64 : + IConfigurationConstants.SL_REQUEST_TYPE_UPLOAD); + + IPlainSigner signer; + if (sstate.hasBKUConnector()) { + ISLConnector connector = new PdfAs4BKUSLConnector(sstate.getBKUConnector()); + signer = new PAdESSigner(connector); + } else if (sstate.hasKSSigner()) { + signer = sstate.getKSSigner(); + } else { + throw new SignatureException("SigningState doesn't have a signer"); + } + param.setPlainSigner(signer); + + pdfas.sign(param); + + SignResultImpl result = new SignResultImpl(); + + 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(sstate.getOutput().toByteArray())); + return result; + } catch (PdfAsException | PDFASError e) { + throw new SignatureException(e); + } + } + + @Override + public SignatureParameter newParameter() { + return new PdfAs4SignatureParameter(); + } +} diff --git a/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignerFactory.java b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignerFactory.java new file mode 100644 index 00000000..3fa7ce16 --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SignerFactory.java @@ -0,0 +1,31 @@ +/* + * 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 at.asit.pdfover.signator.Signer; +import at.asit.pdfover.signator.SignerFactory; + +/** + * Factory class for PDF-AS Signer + */ +public class PdfAs4SignerFactory extends SignerFactory { + + @Override + public Signer createSigner() { + return new PdfAs4Signer(); + } +} 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 new file mode 100644 index 00000000..f0aff1c8 --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4SigningState.java @@ -0,0 +1,192 @@ +/* + * 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.signator.BkuSlConnector; +import at.asit.pdfover.signator.SLRequest; +import at.asit.pdfover.signator.SLResponse; +import at.asit.pdfover.signator.SignatureException; +import at.asit.pdfover.signator.SigningState; +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 implements SigningState { + + /** + * The Signature Layer request + */ + private SLRequest slrequest; + + /** + * The Signature Layer response + */ + private SLResponse slresponse; + + /** + * The Sign Parameters + */ + private SignParameter parameter; + + private ByteArrayOutputStream output; + + private BkuSlConnector bkuconnector = null; + + private IPlainSigner kssigner = null; + + private boolean useBase64Request; + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#getSignatureRequest() + */ + @Override + public SLRequest getSignatureRequest() { + return this.slrequest; + } + + /** + * Sets the SL Request + * @param request The SL Request + */ + public void setSignatureRequest(SLRequest request) { + this.slrequest = request; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#setUseBase64Request(boolean) + */ + @Override + public void setUseBase64Request(boolean useBase64Request) { + this.useBase64Request = useBase64Request; + } + + /** + * Gets whether to use base64 (or FileUpload) for request data + * @return whether to use base64 for request data + */ + public boolean getUseBase64Request() { + return this.useBase64Request; + } + + /** + * Gets the SL Response + * @return The SL Response object + */ + public SLResponse getSignatureResponse() { + return this.slresponse; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#setSignatureResponse(at.asit.pdfover.signator.SLResponse) + */ + @Override + public void setSignatureResponse(SLResponse response) { + this.slresponse = response; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#hasSignatureResponse() + */ + @Override + public boolean hasSignatureResponse() { + return this.slresponse != null; + } + + /** + * @return the output + */ + public ByteArrayOutputStream getOutput() { + return this.output; + } + + /** + * @param output the output to set + */ + public void setOutput(ByteArrayOutputStream output) { + this.output = output; + } + + /** + * @return the parameter + */ + public SignParameter getSignParameter() { + return this.parameter; + } + + /** + * @param parameter the parameter to set + */ + public void setSignParameter(SignParameter parameter) { + this.parameter = parameter; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#setBKUConnector(at.asit.pdfover.signator.BkuSlConnector) + */ + @Override + public void setBKUConnector(BkuSlConnector connector) { + this.bkuconnector = connector; + } + + /* (non-Javadoc) + * @see at.asit.pdfover.signator.SigningState#setKSSigner(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + */ + @Override + public void setKSSigner(String file, String alias, String kspassword, + String keypassword, String type) throws SignatureException { + try { + this.kssigner = new PAdESSignerKeystore(file, alias, kspassword, keypassword, type); + } catch (PDFASError e) { + throw new SignatureException(e); + } + } + + /** + * @return whether a BKU connector was set + */ + public boolean hasBKUConnector() { + return this.bkuconnector != null; + } + + /** + * @return the BKU connector + */ + public BkuSlConnector getBKUConnector() { + return this.bkuconnector; + } + + /** + * @return whether a KS signer was set + */ + public boolean hasKSSigner() { + return this.kssigner != null; + } + + /** + * @return the KS signer + */ + public IPlainSigner getKSSigner() { + return this.kssigner; + } + +} 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 new file mode 100644 index 00000000..ab50e78b --- /dev/null +++ b/pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/exceptions/PdfAs4SLRequestException.java @@ -0,0 +1,34 @@ +/* + * 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