diff options
Diffstat (limited to 'pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper')
10 files changed, 419 insertions, 62 deletions
diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSink_OLD.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSink_OLD.java new file mode 100644 index 00000000..bb39a322 --- /dev/null +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSink_OLD.java @@ -0,0 +1,34 @@ +package at.gv.egiz.pdfas.wrapper; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import at.gv.egiz.pdfas.api.io.DataSink; + +public class ByteArrayDataSink_OLD implements DataSink { + + private ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + public ByteArrayOutputStream getBAOS() { + return baos; + } + + public OutputStream createOutputStream(String mimeType) throws IOException { + return baos; + } + + public OutputStream createOutputStream(String mimeType, + String characterEncoding) throws IOException { + return baos; + } + + public String getMimeType() { + return "application/pdf"; + } + + public String getCharacterEncoding() { + return "UTF-8"; + } + +} diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSource_OLD.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSource_OLD.java new file mode 100644 index 00000000..ad494acd --- /dev/null +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/ByteArrayDataSource_OLD.java @@ -0,0 +1,40 @@ +package at.gv.egiz.pdfas.wrapper; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import at.gv.egiz.pdfas.api.io.DataSource; + +public class ByteArrayDataSource_OLD implements DataSource { + + private InputStream is; + private int length; + private byte[] data; + + public ByteArrayDataSource_OLD(byte[] data) { + this.length = data.length; + this.is = new ByteArrayInputStream(data); + this.data = data; + } + + public InputStream createInputStream() { + return is; + } + + public int getLength() { + return length; + } + + public byte[] getAsByteArray() { + return this.data; + } + + public String getMimeType() { + return "application/pdf"; + } + + public String getCharacterEncoding() { + return "UTF-8"; + } + +} diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/FileDataSource.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/FileDataSource.java new file mode 100644 index 00000000..19394f0a --- /dev/null +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/FileDataSource.java @@ -0,0 +1,43 @@ +package at.gv.egiz.pdfas.wrapper; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +import javax.management.RuntimeErrorException; + +import at.gv.egiz.pdfas.api.io.DataSource; +import at.gv.egiz.pdfas.common.utils.StreamUtils; + +public class FileDataSource implements DataSource { + + private byte[] data; + + public FileDataSource(File file) throws FileNotFoundException, IOException { + data = StreamUtils.inputStreamToByteArray(new FileInputStream(file)); + } + + public InputStream createInputStream() { + return new ByteArrayInputStream(data); + } + + public int getLength() { + return data.length; + } + + public byte[] getAsByteArray() { + return data; + } + + public String getMimeType() { + return "application/pdf"; + } + + public String getCharacterEncoding() { + return "UTF-8"; + } + +} diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/LegacyMainTest.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/LegacyMainTest.java new file mode 100644 index 00000000..b64bb4c7 --- /dev/null +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/LegacyMainTest.java @@ -0,0 +1,51 @@ +package at.gv.egiz.pdfas.wrapper; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Iterator; + +import at.gv.egiz.pdfas.PdfAsFactory; +import at.gv.egiz.pdfas.api.PdfAs; +import at.gv.egiz.pdfas.api.sign.SignParameters; +import at.gv.egiz.pdfas.api.verify.VerifyParameters; +import at.gv.egiz.pdfas.api.verify.VerifyResult; +import at.gv.egiz.pdfas.api.verify.VerifyResults; + +public class LegacyMainTest { + public static void main(String[] args) { + try { + PdfAs pdfAsOld = PdfAsFactory.createPdfAs(); + SignParameters signParameters = new SignParameters(); + signParameters.setSignatureProfileId("SIGNATURBLOCK_DE"); + signParameters.setSignatureDevice("bku"); + signParameters.setSignatureType("binary"); + + FileDataSource dataSource = new FileDataSource(new File("/home/afitzek/simple.pdf")); + signParameters.setDocument(dataSource); + ByteArrayDataSink_OLD dataSink = new ByteArrayDataSink_OLD(); + signParameters.setOutput(dataSink); + pdfAsOld.sign(signParameters); + + FileOutputStream fos = new FileOutputStream(new File("/home/afitzek/simple_osigned.pdf")); + fos.write(dataSink.getBAOS().toByteArray()); + fos.close(); + + VerifyParameters parameters = new VerifyParameters(); + parameters.setDocument(new FileDataSource(new File("/home/afitzek/simple_osigned.pdf"))); + parameters.setSignatureDevice("bku"); + + VerifyResults verifyResults = pdfAsOld.verify(parameters); + + Iterator<Object> verifyIt = verifyResults.getResults().iterator(); + + while(verifyIt.hasNext()) { + Object obj = verifyIt.next(); + VerifyResult verify = (VerifyResult)obj; + System.out.println("Verify Code: " + verify.getValueCheckCode().getCode()); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java index 30814ff3..0f1a60c1 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/PdfAsObject.java @@ -1,6 +1,10 @@ package at.gv.egiz.pdfas.wrapper; +import iaik.x509.X509Certificate; + import java.io.File; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -46,38 +50,62 @@ public class PdfAsObject implements PdfAs { public SignResult sign(SignParameters signParameters, SignatureDetailInformation signatureDetailInformation) throws PdfAsException { - // Create the signature .... - SignParameter signParameter4 = PdfAsFactory.createSignParameter( - this.configuration, new ByteArrayDataSource(signParameters - .getDocument().getAsByteArray())); - SignParameterWrapper wrapper = new SignParameterWrapper(signParameters, signParameter4); - - // TODO wait for SL wrapper implementation - return null; + if (!(signatureDetailInformation instanceof SignatureDetailInformationWrapper)) { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + "Invalid state"); + } + + SignatureDetailInformationWrapper sdi = (SignatureDetailInformationWrapper) signatureDetailInformation; + StatusRequest request = sdi.getStatus(); + + if (request.needSignature()) { + try { + byte[] signature = sdi.wrapper.getSignParameter4().getPlainSigner().sign( + request.getSignatureData(), request.getSignatureDataByteRange()); + request.setSigature(signature); + request = this.pdfas4.process(request); + if(request.isReady()) { + at.gv.egiz.pdfas.lib.api.sign.SignResult result = this.pdfas4.finishSign(request); + sdi.wrapper.syncNewToOld(); + SignResultImpl oldresult = new SignResultImpl(sdi.wrapper.getSignParameters().getOutput(), + sdi.getX509Certificate()); + return oldresult; + } else { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + "Invalid state"); + } + } catch (at.gv.egiz.pdfas.common.exceptions.PdfAsException e) { + throw new PdfAsException( + ErrorCode.SIGNATURE_COULDNT_BE_CREATED, e.getMessage()); + } + } else { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + "Invalid state"); + } } public VerifyResults verify(VerifyParameters verifyParameters) throws PdfAsException { try { - VerifyParameter newParameter = VerifyParameterWrapper.toNewParameters(verifyParameters); - + VerifyParameter newParameter = VerifyParameterWrapper + .toNewParameters(verifyParameters, this.pdfas4.getConfiguration()); + List<VerifyResult> results = this.pdfas4.verify(newParameter); - + Iterator<VerifyResult> it = results.iterator(); - - List<at.gv.egiz.pdfas.api.verify.VerifyResult> resultList = - new ArrayList<at.gv.egiz.pdfas.api.verify.VerifyResult>(); - - while(it.hasNext()) { + + List<at.gv.egiz.pdfas.api.verify.VerifyResult> resultList = new ArrayList<at.gv.egiz.pdfas.api.verify.VerifyResult>(); + + while (it.hasNext()) { VerifyResult newResult = it.next(); - at.gv.egiz.pdfas.api.verify.VerifyResult oldResult = - new VerifyResultWrapper(newResult); + at.gv.egiz.pdfas.api.verify.VerifyResult oldResult = new VerifyResultWrapper( + newResult); resultList.add(oldResult); } - + return new VerifyResultsImpl(resultList); - } catch(at.gv.egiz.pdfas.common.exceptions.PdfAsException e) { + } catch (at.gv.egiz.pdfas.common.exceptions.PdfAsException e) { throw new PdfAsException(0, e.getMessage()); } } @@ -166,15 +194,39 @@ public class PdfAsObject implements PdfAs { this.configuration, new ByteArrayDataSource(signParameters .getDocument().getAsByteArray())); - SignParameterWrapper wrapper = new SignParameterWrapper(signParameters, signParameter4); - - // TODO: wrapper sync old to new - StatusRequest request = this.pdfas4.startSign(wrapper.getSignParameter4()); - // TODO: wait for SL implementation wrapper - return null; + SignParameterWrapper wrapper = new SignParameterWrapper( + signParameters, signParameter4); + SignatureDetailInformationWrapper sdi = null; + + wrapper.syncOldToNew(); + + StatusRequest request = this.pdfas4.startSign(wrapper + .getSignParameter4()); + + if (request.needCertificate()) { + X509Certificate certificate = signParameter4.getPlainSigner() + .getCertificate(); + sdi = new SignatureDetailInformationWrapper(certificate); + request.setCertificate(certificate.getEncoded()); + request = this.pdfas4.process(request); + if (request.needSignature()) { + sdi.setDataSource(new ByteArrayDataSource_OLD(request + .getSignatureData())); + } + sdi.wrapper = wrapper; + sdi.setStatus(request); + } + return sdi; } catch (at.gv.egiz.pdfas.common.exceptions.PdfAsException e) { - throw new PdfAsException(0, e.getMessage()); + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + e.getMessage()); + } catch (CertificateEncodingException e) { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + e.getMessage()); + } catch (CertificateException e) { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + e.getMessage()); } } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java index b51896bd..87278c85 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignParameterWrapper.java @@ -1,31 +1,104 @@ package at.gv.egiz.pdfas.wrapper; +import java.io.OutputStream; +import java.util.Enumeration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.api.exceptions.ErrorCode; +import at.gv.egiz.pdfas.api.exceptions.PdfAsException; import at.gv.egiz.pdfas.api.sign.SignParameters; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSink; import at.gv.egiz.pdfas.lib.api.sign.SignParameter; +import at.gv.egiz.pdfas.sigs.pades.PAdESSigner; +import at.gv.egiz.sl.util.BKUSLConnector; +import at.gv.egiz.sl.util.MOAConnector; public class SignParameterWrapper { + private static final Logger logger = LoggerFactory + .getLogger(SignParameterWrapper.class); + private SignParameter signParameter4; private SignParameters signParameters; - - public SignParameterWrapper(SignParameters signParameters, SignParameter signParameter4) { + private ByteArrayDataSink output; + + public SignParameterWrapper(SignParameters signParameters, + SignParameter signParameter4) { this.signParameter4 = signParameter4; this.signParameters = signParameters; } - - private void syncOldToNew() { - // TODO + + public void syncOldToNew() throws PdfAsException { + output = new ByteArrayDataSink(); + this.signParameter4.setOutput(output); + + if (this.signParameters.getSignaturePositioning() != null) { + // Create positioning string + String posString = this.signParameters.getSignaturePositioning() + .getPositionString(); + logger.info("Pos String: " + posString); + if (posString.equals("x:auto;y:auto;w:auto;p:auto;f:0.0")) { + this.signParameter4.setSignaturePosition(null); + } else { + this.signParameter4.setSignaturePosition(posString); + } + } else { + this.signParameter4.setSignaturePosition(null); + } + + // Select signing device + if (this.signParameters.getSignatureDevice().equals("moa")) { + try { + this.signParameter4 + .setPlainSigner(new PAdESSigner(new MOAConnector( + this.signParameter4.getConfiguration()))); + } catch (Exception e) { + throw new PdfAsException(ErrorCode.CERTIFICATE_NOT_FOUND, + "You need to specify MOA certificate file to use moa (moa.sign.Certificate)"); + } + } else if (this.signParameters.getSignatureDevice().equals("bku")) { + this.signParameter4 + .setPlainSigner(new PAdESSigner(new BKUSLConnector( + this.signParameter4.getConfiguration()))); + } else { + throw new PdfAsException(ErrorCode.UNSUPPORTED_SIGNATURE, + "Unsupported device! Use bku or moa!"); + } + + // Overwrite Configurations + Enumeration<Object> keys = this.signParameters + .getProfileOverrideProperties().keys(); + + while (keys.hasMoreElements()) { + Object obj = keys.nextElement(); + if (obj != null) { + String key = obj.toString(); + this.signParameter4.getConfiguration().setValue( + key, + this.signParameters.getProfileOverrideProperties() + .getProperty(key)); + } + } } - - private void syncNewToOld() { - // TODO + + public void syncNewToOld() throws PdfAsException { + try { + OutputStream os = this.signParameters.getOutput() + .createOutputStream("application/pdf"); + os.write(output.getData()); + os.close(); + } catch (Exception e) { + throw new PdfAsException(ErrorCode.SIGNATURE_COULDNT_BE_CREATED, + e.getMessage()); + } } - - + public SignParameter getSignParameter4() { return this.signParameter4; } - + public SignParameters getSignParameters() { return this.signParameters; } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignResultImpl.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignResultImpl.java new file mode 100644 index 00000000..b8a9ab33 --- /dev/null +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignResultImpl.java @@ -0,0 +1,40 @@ +package at.gv.egiz.pdfas.wrapper; + +import java.security.cert.X509Certificate; +import java.util.List; + +import at.gv.egiz.pdfas.api.io.DataSink; +import at.gv.egiz.pdfas.api.sign.SignResult; +import at.gv.egiz.pdfas.api.sign.pos.SignaturePosition; + +public class SignResultImpl implements SignResult { + + private DataSink sink; + private X509Certificate certificate; + + public SignResultImpl(DataSink data, X509Certificate cert) { + this.certificate = cert; + this.sink = data; + } + + public DataSink getOutputDocument() { + return this.sink; + } + + public X509Certificate getSignerCertificate() { + return certificate; + } + + public SignaturePosition getSignaturePosition() { + return null; + } + + public List getNonTextualObjects() { + return null; + } + + public boolean hasNonTextualObjects() { + return false; + } + +} diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignatureDetailInformationWrapper.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignatureDetailInformationWrapper.java index 683e083f..5192ffe4 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignatureDetailInformationWrapper.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/SignatureDetailInformationWrapper.java @@ -8,14 +8,36 @@ import java.util.Map; import at.gv.egiz.pdfas.api.io.DataSource; import at.gv.egiz.pdfas.api.sign.SignatureDetailInformation; import at.gv.egiz.pdfas.api.sign.pos.SignaturePosition; +import at.gv.egiz.pdfas.common.utils.DNUtils; +import at.gv.egiz.pdfas.lib.api.StatusRequest; -public class SignatureDetailInformationWrapper implements SignatureDetailInformation { +public class SignatureDetailInformationWrapper implements + SignatureDetailInformation { - private SignParameterWrapper wrapper; + public SignParameterWrapper wrapper; + private StatusRequest status; + private DataSource dataSource; + private iaik.x509.X509Certificate certificate; + + public SignatureDetailInformationWrapper(iaik.x509.X509Certificate cert) { + this.certificate = cert; + } + + public StatusRequest getStatus() { + return status; + } + + public void setStatus(StatusRequest status) { + this.status = status; + } + + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } + public DataSource getSignatureData() { - // TODO - return null; + return this.dataSource; } public SignaturePosition getSignaturePosition() { @@ -32,23 +54,28 @@ public class SignatureDetailInformationWrapper implements SignatureDetailInforma } public String getIssuer() { - return null; + return this.certificate.getIssuerDN().getName(); } public Map getIssuerDNMap() { - return null; + try { + return DNUtils.dnToMap(getIssuer()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } public String getSubjectName() { - return null; + return this.certificate.getSubjectDN().getName(); } public String getSerialNumber() { - return null; + return this.certificate.getSerialNumber().toString(); } public String getSigAlgorithm() { - return null; + return this.certificate.getSigAlgName(); } public String getSigID() { @@ -64,20 +91,23 @@ public class SignatureDetailInformationWrapper implements SignatureDetailInforma } public String getSigTimeStamp() { - // TODO Auto-generated method stub return null; } public Map getSubjectDNMap() { - return null; + try { + return DNUtils.dnToMap(getSubjectName()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } public X509Certificate getX509Certificate() { - return null; + return this.certificate; } public boolean isTextual() { - // TODO Auto-generated method stub return false; } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/VerifyParameterWrapper.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/VerifyParameterWrapper.java index 387d9ea4..51a8c843 100644 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/VerifyParameterWrapper.java +++ b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/VerifyParameterWrapper.java @@ -1,14 +1,16 @@ package at.gv.egiz.pdfas.wrapper; import at.gv.egiz.pdfas.api.verify.VerifyParameters; +import at.gv.egiz.pdfas.lib.api.ByteArrayDataSource; +import at.gv.egiz.pdfas.lib.api.Configuration; import at.gv.egiz.pdfas.lib.api.PdfAsFactory; import at.gv.egiz.pdfas.lib.api.verify.VerifyParameter; public class VerifyParameterWrapper { - public static VerifyParameter toNewParameters(VerifyParameters oldParameters) { - //TODO - return null; + public static VerifyParameter toNewParameters(VerifyParameters oldParameters, Configuration config) { + return PdfAsFactory.createVerifyParameter(config, + new ByteArrayDataSource(oldParameters.getDocument().getAsByteArray())); } } diff --git a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/package-info.java b/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/package-info.java deleted file mode 100644 index 37867d98..00000000 --- a/pdf-as-legacy/src/main/java/at/gv/egiz/pdfas/wrapper/package-info.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * - */ -/** - * @author afitzek - * - */ -package at.gv.egiz.pdfas.wrapper;
\ No newline at end of file |