Giter VIP home page Giter VIP logo

asic's Introduction

Warning

Please note that this project is in maintenance mode.

It is currently being maintained for the purpose of eFormidlings use of the advanced signature container (ASiC) format.

Bugs will be fixed and dependencies kept up to date, but no new features will be added.

eFormidling is looking into alternatives and might archive the project in the future.

Alternatives to this project include:

Associated Signature Container (ASiC)

An ASiC file is simply a ZIP archive created according to some rules set forth in the specifications.

The benefits of using containers for message transfer are:

  • all files are kept together as a single collection.
  • very efficient with regards to space.
  • due to the compressed format, communication bandwith is utilized better
  • message integrity is provided, using message digests and signatures.
  • confidentiality is provied by encryption using AES-256 in GCM mode

This component provides an easy-to-use factory for creating ASiC-E containers.

Conformance is claimed according to 7.2.1 (TBA) and 7.2.2 in ETSI TS 102 918 V1.3.1.

Maven

<dependency>
	<groupId>no.difi.commons</groupId>
	<artifactId>commons-asic</artifactId>
	<version>0.9.2</version>
</dependency>

What does it look like?

In general the archive looks something like depicted below

asic-container.asice: 
   |
   +-- mimetype
   |
   +-- bii-envelope.xml
   |
   +-- bii-document.xml
   |
   +-- META-INF/
          |
          + asicmanifest.xml
          |
          + signature.p7s   
   

Consult the AsicCadesContainerWriterTest for sample usage. Here is a rough sketch on how to do it:

// Creates an ASiC archive after which every entry is read back from the archive.

// Name of the file to hold the the ASiC archive
File archiveOutputFile = new File(System.getProperty("java.io.tmpdir"), "asic-sample-default.zip");

// Creates an AsicWriterFactory with default signature method
AsicWriterFactory asicWriterFactory = AsicWriterFactory.newFactory();

// Creates the actual container with all the data objects (files) and signs it.
AsicWriter asicWriter = asicWriterFactory.newContainer(archiveOutputFile)
        // Adds an ordinary file, using the file name as the entry name
        .add(biiEnvelopeFile)
                // Adds another file, explicitly naming the entry and specifying the MIME type
        .add(biiMessageFile, BII_MESSAGE_XML, MimeType.forString("application/xml"))
                // Signing the contents of the archive, closes it for further changes.
        .sign(keystoreFile, TestUtil.keyStorePassword(), TestUtil.privateKeyPassword());

// Opens the generated archive and reads each entry
AsicReader asicReader = AsicReaderFactory.newFactory().open(archiveOutputFile);

String entryName;

// Iterates over each entry and writes the contents into a file having same name as the entry
while ((entryName = asicReader.getNextFile()) != null) {
    log.debug("Read entry " + entryName);
    
    // Creates file with same name as entry
    File file = new File(entryName);
    // Ensures we don't overwrite anything
    if (file.exists()) {
        throw new IllegalStateException("File already exists");
    }
    asicReader.writeFile(file);
    
    // Removes file immediately, since this is just a test 
    file.delete();  
}
asicReader.close(); 

Security

This library validate signatures, but does not validate the certificate. It's up to the implementer using the library to choose if and how to validate certificates. Certificate(s) used for validation is exposed by the library.

Creating an ASiC-E container manually

This is how you create an ASiC container manually:

  1. Create empty directory named asic-sample
  2. Copy the files bii-envelope.xmland bii-trns081.xml into asic-sample
  3. Create the directory META-INF:
  4. Compute the SHA-256 digest value for the files and save them:
openssl dgst -sha256 -binary bii-envelope |base64
openssl dgst -sha256 -binary bii-message |base64

  1. Create the file META-INF/asicmanifest.xml, add an entry for each file and paste the SHA-256 values computed in the previous step. The file should look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ASiCManifest xmlns="http://uri.etsi.org/02918/v1.2.1#" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
    <SigReference URI="META-INF/signature.p7s" MimeType="application/x-pkcs7-signature"/>
    <DataObjectReference URI="bii-trns081.xml" MimeType="application/xml">
        <ns2:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <ns2:DigestValue>morANIlh3TGxMUsJWKfICly7YXoduG7LCohAKc2Sip8=</ns2:DigestValue>
    </DataObjectReference>
    <DataObjectReference URI="bii-envelope.xml" MimeType="application/xml">
        <ns2:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
        <ns2:DigestValue>IZ9yiwKHsTWMcyFebi7csqOOIHohy2gPd02VSfbyUCI=</ns2:DigestValue>
    </DataObjectReference>
</ASiCManifest>
  1. Create the signature, which should be placed into signature.p7s. The file comodo.pem should be replaced with the PEM-file holding your private key for the signature, and the certificate to prove it.
openssl cms -sign -in META-INF/asicmanifest.xml -binary -outform der -out META-INF/signature.p7s -signer comodo.pem
  1. Verify the signature:
openssl cms -verify -in META-INF/signature.p7s -inform der -content META-INF/asicmanifest.xml -noverify

Note! The -noverify option omits verifying the certificate chain of trust and should only be used to verify that the files were created properly

  1. Create the ZIP-archive using your favourite tool :-)

Disclaimer: The procedure liste above works on a Mac or Linux machine with the various tools pre-installed. If you are running on a windows machine you need to download and install the openssl and base64 tool and adapt the procedure according to your liking.

Verifying the contents using openssl

Here is how to verify the signature using the openssl(1) command line tool:

openssl cms -verify -in META-INF/signature.p7s -inform der -content META-INF/asicmanifest.xml -noverify

The -noverify option will allow self signed certificates, and should normally be omitted :-).

Programmers notes

You might encounter memory problems when using Java 1.7. This is due to the memory consumption of JAXB.

Try this before you run maven, you might need to increase this even further (your mileage may vary):

export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"

or on Windows:

set MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=512m

asic's People

Contributors

dependabot[bot] avatar elfogre avatar frodebjerkholt avatar glennbech avatar klakegg avatar steinarcook avatar steinarhenriksen avatar zapodot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asic's Issues

ASiCManifest.xml file name

The rules dictate that ASiCManifest.xml should be named like this, with several uppercase letters. The class CadesAsicWriter uses lowercase only, naming the file asicmanifest.xml. For some recipients this is no problem, others expect correct case and will fail. Can we simply change this line in CadesAsicWriter?

release of 0.9.3?

Hi,

Do you have an estimated release date for the 0.9.3 release? Your change in etsi and xmldsig namespaces will make it easier to work with difi/asic and espd in the same jvm, so we are looking forward to having a release in maven central.

Regards,
Finn

UnmarshalException: Unexpected element on ibm server

While unmarshaling ACiCManifest object, websphere as throws this exception:

Caused by: javax.xml.bind.UnmarshalException: Unexpected element "{http://uri.etsi.org/02918/v1.2.1#}ASiCManifest". Expected elements are "".
	at com.ibm.xml.xlxp2.jaxb.msg.JAXBMessageProvider.throwUnmarshalExceptionWrapper(JAXBMessageProvider.java:93)
	at com.ibm.xml.xlxp2.jaxb.unmarshal.impl.DeserializationContext.handleSkippedRootElementEvent(DeserializationContext.java:341)
	at com.ibm.xml.xlxp2.jaxb.unmarshal.impl.JAXBDocumentScanner.produceRootElementEvent(JAXBDocumentScanner.java:189)
	at com.ibm.xml.xlxp2.scan.DocumentScanner.scanRootElement(DocumentScanner.java:2241)
	at com.ibm.xml.xlxp2.scan.DocumentScanner.scanProlog(DocumentScanner.java:1733)
	at com.ibm.xml.xlxp2.scan.DocumentScanner.nextEvent(DocumentScanner.java:1323)
	at com.ibm.xml.xlxp2.scan.DocumentScanner.parseDocumentEntity(DocumentScanner.java:1175)
	at com.ibm.xml.xlxp2.jaxb.unmarshal.impl.JAXBDocumentScanner.unmarshal(JAXBDocumentScanner.java:125)
	at com.ibm.xml.xlxp2.jaxb.unmarshal.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:107)
	at no.difi.asic.CadesAsicManifest.extractAndVerify(CadesAsicManifest.java:107)

Problem is with initializing JAXBContext for IBM unmarshaler. It can be solved with initialization with package. like this:

static {
        try {
            String manifestTypeClassName = ASiCManifestType.class.getCanonicalName();
            jaxbContext = JAXBContext.newInstance(manifestTypeClassName.substring(0, manifestTypeClassName.lastIndexOf(".")));
        } catch (JAXBException e) {
            throw new IllegalStateException(String.format("Unable to create JAXBContext: %s ", e.getMessage()), e);
        }
    }

It works for IBM and java8 sun implementation of Unmarshaler.

Can you fix this?

Thanks

New release 0.9.4

Hi.

Could you please make a new release with the changes since 0.9.3?
The keyStoreType is a nice change ๐Ÿ‘

Thanks

Encryption of payload

Hi

  1. When encrypting the payload before zip'ing it, the resulting asic archive will not be compressed as much as possible because encryption makes the file binary. It is better to create the ASIC archive without encrypted files inside, and then encrypt the resulting zip file. The resulting encrypted archive will most likely be much smaller!

  2. Encryption algorithm. Using AES-GCM is not "good". Why? For example, if using OpenSSL to create the encrypted file (PKCS7), AES256 GCM is not supported by OpenSSL commandline. So it will not be possible to create an encrypted ASIC file without coding. Creating a ASIC archive should be simple to implement on different platforms.. Not necessary having Java as an option. I suggest to use AES-256 CBC instead. That way, Creating an encrypted archive can be scripted on platforms that have OpenSSL.

Verification of archives fails with OPENSSL

When following the instructions for verifying archives at https://github.com/difi/asic, verification fails.

I've attempted to validate own files generated through code as well as the examples used in the source code. Running on a Windows platform.

I get the following error upon validation:

22124:error:2E09A09E:CMS routines:CMS_SignerInfo_verify_content:verification failure:crypto\cms\cms_sd.c:848:
22124:error:2E09D06D:CMS routines:CMS_verify:content verify error:crypto\cms\cms_smime.c:393:

The following procedure was used:

  1. Download source code.
  2. Run createSampleContainer() in AsicCadesWriterTest method
  3. Take the generated file asic-sample-cades-zip and unzip it
  4. If not installed, install OPENSSL
  5. Open command prompt and navigate to root of unzipped folder
  6. Follow instructions for verifying signature using command openssl cms -verify -in META-INF/signature_file_name_here.p7s -inform der -content META-INF/asicmanifest.xml -noverify
  7. Validation fails in response with
    22124:error:2E09A09E:CMS routines:CMS_SignerInfo_verify_content:verification failure:crypto\cms\cms_sd.c:848:
    22124:error:2E09D06D:CMS routines:CMS_verify:content verify error:crypto\cms\cms_smime.c:393:

Unsure if it is related to #8 or not.

Library does not work with new jakarta jaxb bindings package

We have an application run with Spring Boot 3 / Spring Framework 6, that uses the new jakarta.xml... packages.

The asic-Library still requires the old javax.xml... packages. It runs only if we install the old library next to the new ones thus duplicating the size of the bundles jaxb libraries.

In addition to that, even new versions of jaxb-impl do not work, the last version, I was able to use the library with, is 2.4.0-b180830.0438 โ€“ a version that is 4 1/2 years old.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.