diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2016-06-20 12:45:07 +0200 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2016-06-20 12:45:07 +0200 |
commit | 53ae98cdfa510650c1e015efd0d11a96ad4dc87a (patch) | |
tree | 8e7ab77a9208b541416d14bbdb15e7211ff40c5b /moaSig/moa-asic/src/test/java/at/gv | |
parent | acf1b849ab835bc6797adfb91f8ab4fa88f0aff5 (diff) | |
download | moa-sig-53ae98cdfa510650c1e015efd0d11a96ad4dc87a.tar.gz moa-sig-53ae98cdfa510650c1e015efd0d11a96ad4dc87a.tar.bz2 moa-sig-53ae98cdfa510650c1e015efd0d11a96ad4dc87a.zip |
ASiC Library added
Diffstat (limited to 'moaSig/moa-asic/src/test/java/at/gv')
-rw-r--r-- | moaSig/moa-asic/src/test/java/at/gv/egiz/asic/dev/Main.java | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/moaSig/moa-asic/src/test/java/at/gv/egiz/asic/dev/Main.java b/moaSig/moa-asic/src/test/java/at/gv/egiz/asic/dev/Main.java new file mode 100644 index 0000000..5005a3b --- /dev/null +++ b/moaSig/moa-asic/src/test/java/at/gv/egiz/asic/dev/Main.java @@ -0,0 +1,238 @@ +package at.gv.egiz.asic.dev; + +import at.gv.egiz.asic.api.ASiC; +import at.gv.egiz.asic.api.ASiCFactory; +import at.gv.egiz.asic.api.ASiCFormat; +import at.gv.egiz.asic.api.ASiCVerificationResult; +import at.gv.egiz.asic.impl.ASiCMOAVerifier; +import at.gv.egiz.asic.xmlbind.VerifyASICSignatureResponseBuilder; +import at.gv.egovernment.moa.spss.MOASystemException; +import at.gv.egovernment.moa.spss.api.cmsverify.VerifyCMSSignatureResponse; +import at.gv.egovernment.moa.spss.api.cmsverify.VerifyCMSSignatureResponseElement; +import at.gv.egovernment.moa.spss.api.common.SignerInfo; +import at.gv.egovernment.moa.spss.api.xmlverify.VerifyXMLSignatureResponse; +import at.gv.egovernment.moa.spss.server.config.ConfigurationProvider; +import at.gv.egovernment.moa.spss.server.init.SystemInitializer; +import at.gv.egovernment.moa.spss.server.transaction.TransactionContext; +import at.gv.egovernment.moa.spss.server.transaction.TransactionContextManager; +import at.gv.egovernment.moa.spss.server.transaction.TransactionIDGenerator; +import at.gv.egovernment.moaspss.logging.LoggingContext; +import at.gv.egovernment.moaspss.logging.LoggingContextManager; +import at.gv.egovernment.moaspss.util.DOMUtils; +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.StringWriter; +import java.util.Iterator; +import java.util.List; + +/** + * Created by Andreas Fitzek on 6/15/16. + */ +public class Main { + + private static final Logger logger = LoggerFactory.getLogger(Main.class); + + private static void verify(String filename, boolean showInfo, boolean showCertificate, boolean showXML, ASiCMOAVerifier v) + throws Throwable{ + logger.info("========================================================================================"); + logger.info("Running ASiC Verification for {}", filename); + logger.info("========================================================================================"); + + ASiCFormat format = null; + + if (filename.endsWith("asics") || filename.endsWith("scs")) { + format = ASiCFormat.ASiCS; + } else if (filename.endsWith("asice") || filename.endsWith("sce")) { + format = ASiCFormat.ASiCE; + } + + ASiC aSiC = ASiCFactory.parseASiC(new FileInputStream(filename), format); + + logger.info("Build ASIC-S: {}", aSiC); + + + List<ASiCVerificationResult> results = v.verify(aSiC, "Test-Signaturdienste", null); + + if (results != null) { + for (int i = 0; i < results.size(); i++) { + ASiCVerificationResult aSiCVerificationResult = results.get(i); + + if (showInfo) { + // Auswertung der Response + System.out.println(); + System.out.println("Ergebnisse der Signaturprüfung:"); + System.out.println(); + + // Auswertung der Response + System.out.println(); + System.out.println("Signierte Eintraege"); + Iterator<String> signedFilesIterator = aSiCVerificationResult.getSignedFiles().iterator(); + while (signedFilesIterator.hasNext()) { + System.out.println(" " + signedFilesIterator.next()); + } + System.out.println(); + } + if (aSiCVerificationResult.getXmlResult() != null) { + VerifyXMLSignatureResponse verifyResponse = aSiCVerificationResult.getXmlResult(); + + // Ergebnisse von Signatur- und Zertifikatsprüfung + System.out.println(); + System.out.println("Ergebniscode der Signaturprüfung: " + verifyResponse.getSignatureCheck().getCode()); + System.out.println("Ergebniscode der Zertifikatsprüfung: " + verifyResponse.getCertificateCheck().getCode()); + + // Besondere Eigenschaften des Signatorzertifikats + SignerInfo signerInfo = verifyResponse.getSignerInfo(); + + if (showCertificate) { + showSignerInfo(signerInfo); + } + } else if (aSiCVerificationResult.getCmsResult() != null) { + VerifyCMSSignatureResponse verifyResponse = aSiCVerificationResult.getCmsResult(); + + Iterator responses = verifyResponse.getResponseElements().iterator(); + while (responses.hasNext()) { + Object response = responses.next(); + VerifyCMSSignatureResponseElement verifyCMS = (VerifyCMSSignatureResponseElement) response; + + // Besondere Eigenschaften des Signatorzertifikats + SignerInfo signerInfo = verifyCMS.getSignerInfo(); + + // Ergebnisse von Signatur- und Zertifikatsprüfung + System.out.println(); + System.out.println("Ergebniscode der Signaturprüfung: " + verifyCMS.getSignatureCheck().getCode()); + System.out.println("Ergebniscode der Zertifikatsprüfung: " + verifyCMS.getCertificateCheck().getCode()); + + + if (showCertificate) { + showSignerInfo(signerInfo); + } + } + } + } + + if (showXML) { + showXMLResponse(results); + } + } + } + + + private static void runAllPlugtests(boolean showInfo, boolean showCertificate, boolean showXML, ASiCMOAVerifier v) { + Iterator<File> filesIterator = FileUtils.iterateFiles(new File("/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/"), + new String[]{"asics", "asice", "scs", "sce"}, true); + + int total = 0; + int ok = 0; + int failed = 0; + + while (filesIterator.hasNext()) { + //for(int j = 0; j < testInputs.length; j++) { + //String filename = testInputs[j]; + String filename = filesIterator.next().getAbsolutePath(); + total++; + try { + verify(filename, showInfo, showCertificate, showXML, v); + ok++; + } catch (Throwable e) { + failed++; + logger.error("Verification failed! {}", e.getMessage(), e); + } + } + + logger.info("Total {} / OK {} / ERROR {}", total, ok, failed); + } + + private static void runSpecific(boolean showInfo, boolean showCertificate, boolean showXML, ASiCMOAVerifier v) { + String testInput2 = "/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/BG_BOR/Signature-A-BG_BOR-2.asics"; + + String testInput = "/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/HU_MIC/Signature-A-HU_MIC-1.asics"; + String testInputCMS = "/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/LU_NOW/Signature-A-LU_NOW-2.asics"; + String testInputE = "/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/EE_SER/Signature-A-EE_SER-35.asice"; + String testInputECMS = "/home/afitzek/Documents/PlugTest-2016/sig_data/ESIG-A/HU_POL/Signature-A-HU_POL-16.asice"; + + try { + verify(testInputE, showInfo, showCertificate, showXML, v); + } catch (Throwable e) { + logger.error("Verification failed! {}", e.getMessage(), e); + } + } + + public static void main(String[] args) { + try { + boolean showCertificate = true; + boolean showXML = true; + boolean showInfo = true; + System.setProperty("moa.spss.server.configuration", "/home/afitzek/server/moa-spss/apache-tomcat-8.0.0-RC3/conf/moa-spss/spss.config.xml"); + System.setProperty("iaik.esi.sva.configuration.location", "/home/afitzek/server/moa-spss/apache-tomcat-8.0.0-RC3/conf/moa-spss/svaconfig.example"); + + SystemInitializer.init(); + + //String[] testInputs = new String[] {testInput, testInputCMS, testInputE, testInput2, testInputECMS}; + //ASiCFormat[] formats = new ASiCFormat[] {null, null, null, null, ASiCFormat.ASiCE}; + + ConfigurationProvider configuration = ConfigurationProvider.getInstance(); + + TransactionContext context = new TransactionContext(TransactionIDGenerator.nextID(), null, + configuration, null, null); + + TransactionContextManager tcm = TransactionContextManager.getInstance(); + tcm.setTransactionContext(context); + + ASiCMOAVerifier v = new ASiCMOAVerifier(); + + // set the logging context in the LoggingContextManager + LoggingContextManager lcm = LoggingContextManager.getInstance(); + LoggingContext lc = new LoggingContext(context.getTransactionID()); + lcm.setLoggingContext(lc); + + runSpecific(showInfo, showCertificate, showXML, v); + + //runAllPlugtests(showInfo, showCertificate, showXML, v); + + //ASiC aSiCE = ASiCFactory.parseASiC(new FileInputStream(testInputE), ASiCFormat.ASiCE); + + //logger.info("Build ASIC-E: {}", aSiCE); + } catch (Throwable e) { + e.printStackTrace(); + } + } + + private static void showXMLResponse(List<ASiCVerificationResult> results) throws Exception { + VerifyASICSignatureResponseBuilder builder = new VerifyASICSignatureResponseBuilder(); + Document doc = builder.build(results); + + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StringWriter writer = new StringWriter(); + transformer.transform(new DOMSource(doc), new StreamResult(writer)); + String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); + + System.out.println("XML Response: " + output); + } + + private static void showSignerInfo(SignerInfo signerInfo) { + // Signatorzertifikat + System.out.println("*** Ist Zertifikat des Signators qualifiziert? " + ((signerInfo.isQualifiedCertificate()) ? "ja" : "nein")); + System.out.println("*** Ist Zertifikat des Signators von einer Behörde? " + ((signerInfo.isPublicAuthority()) ? "ja" : "nein")); + + System.out.println(); + System.out.println("*** Zertifikat des Signators:"); + System.out.println("Aussteller: " + signerInfo.getSignerCertificate().getIssuerDN()); + System.out.println("Subject: " + signerInfo.getSignerCertificate().getSubjectDN()); + System.out.println("Seriennummer: " + signerInfo.getSignerCertificate().getSerialNumber()); + } +} |