diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-08 13:31:47 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-08 13:31:47 +0200 |
commit | 84a9465b14154f3e56a6b772d21e70f1c60bdf47 (patch) | |
tree | c97968e424f8c4a3f0ad377d25d40c9dbf50a603 /pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java | |
parent | 47e1479b8cbb3d4f2f13abff8e5b514b39efb43a (diff) | |
download | pdf-over-84a9465b14154f3e56a6b772d21e70f1c60bdf47.tar.gz pdf-over-84a9465b14154f3e56a6b772d21e70f1c60bdf47.tar.bz2 pdf-over-84a9465b14154f3e56a6b772d21e70f1c60bdf47.zip |
synchronize pdf-as profile hackery to avoid potential race conditions
Diffstat (limited to 'pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java')
-rw-r--r-- | pdf-over-signer/src/main/java/at/asit/pdfover/signer/pdfas/PdfAs4Signer.java | 164 |
1 files changed, 84 insertions, 80 deletions
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 index 51e36037..628b5c11 100644 --- 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 @@ -52,52 +52,55 @@ public class PdfAs4Signer { if (parameter.signaturePosition != null) { sigPos = parameter.getPdfAsSignaturePosition(); } + PdfAs pdfas = PdfAs4Helper.getPdfAs(); - Configuration config = pdfas.getConfiguration(); - if (sigEmblem != null && !sigEmblem.trim().isEmpty()) { - config.setValue("sig_obj." + sigProfile + ".value.SIG_LABEL", sigEmblem); - } + 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); - } + 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() : 65; - int emblemHeight = (parameter.emblem != null) ? parameter.emblem.getHeight() : 65; - double aspectRatio = ((double)emblemWidth) / emblemHeight; - double targetWidth = 65.0; - double targetHeight = 65.0; - if (aspectRatio < 1) - targetWidth = 65.0 * aspectRatio; - else - targetHeight = 65.0 / 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); - } + // TODO encapsulate this parameter magic in PdfAs4SignatureParameter + if (parameter.signatureProfile == Profile.BASE_LOGO) + { + int emblemWidth = (parameter.emblem != null) ? parameter.emblem.getWidth() : 65; + int emblemHeight = (parameter.emblem != null) ? parameter.emblem.getHeight() : 65; + double aspectRatio = ((double)emblemWidth) / emblemHeight; + double targetWidth = 65.0; + double targetHeight = 65.0; + if (aspectRatio < 1) + targetWidth = 65.0 * aspectRatio; + else + targetHeight = 65.0 / 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); + 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); - } + 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; + state.signParameter = param; + state.output = output; + return state; + } } public static SignResult sign(PdfAs4SigningState state) throws SignatureException { @@ -106,48 +109,49 @@ public class PdfAs4Signer { throw new SignatureException("Incorrect SigningState!"); } - // Retrieve objects PdfAs pdfas = PdfAs4Helper.getPdfAs(); - - 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); + 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; } - - result.setSignedDocument(new ByteArrayDocumentSource(state.output.toByteArray())); - return result; } catch (PdfAsException | PDFASError e) { throw new SignatureException(e); } |