Skip to content

AS2 compression using ZLIB seem not respect RFC #46

@rde-infologic

Description

@rde-infologic

Hi,

I try to consume compressed data from PHPAS2 to JAVA (mendelson, Hermes AS2, OpenAS2 or other product), in Java its seem impossible to read correctly the compressed flow, we always have java.util.zip.ZipException: incorrect header check

For me the root cause is here base64 should not be use on ZLIB ASN1 struct :
Image

It's work well between 2 PHPAS2 because of there is an optionnal base64 check and if true => decode :
Image

So I made a litlle test case in java, with data obtain from PHPAS2

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DLSequence;
import org.bouncycastle.asn1.DLTaggedObject;
import org.bouncycastle.asn1.cms.CompressedData;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.cms.CMSCompressedData;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.jcajce.ZlibExpanderProvider;
import org.bouncycastle.operator.InputExpander;
import org.junit.Test;

import jakarta.mail.MessagingException;

public class demoJava
{
    @Test
    public void testPHPAS2Compress() throws MessagingException, IOException, CMSException
    {
        String base64 = "MIGmBgsqhkiG9w0BCRABCaCBljCBkwIBADANBgsqhkiG9w0BCRADCDB/BgkqhkiG" +
            "9w0BBwGgcgRwZUp3THljZ3NWZ0NpUklYay9OemMwcnpNNU1TU3pQdzhoWkxVNGhL" +
            "RjhzeVNESVdTakZRRngyQWpCZWRnWDhlZ0VJWGlFckFDUFY2dWdKelV4T0pVaFpU" +
            "TTRxTFU5TVNpRktES3pHSTlBSTVmSERVPQ==";

        byte[] cmsBytes = Base64.getMimeDecoder().decode(base64);
        CMSCompressedData compressedData = new CMSCompressedData(cmsBytes);

        ContentInfo ct = ContentInfo.getInstance(new ASN1InputStream(cmsBytes).readObject());
        CompressedData comData = CompressedData.getInstance(ct.getContent());

        //GET compressed DER data on ASN OBJECT
        DEROctetString ddd = (DEROctetString) ((DLTaggedObject) ((DLSequence) ((DLSequence) ct.getContent()).getObjectAt(2)).getObjectAt(1)).getBaseObject();

        ZlibExpanderProvider expanderProvider = new ZlibExpanderProvider();
        ContentInfo content = comData.getEncapContentInfo();

        ASN1OctetString bytes = (ASN1OctetString) content.getContent();
        InputExpander expander = expanderProvider.get(comData.getCompressionAlgorithmIdentifier());

        // Force B64 decode ! NOT IN RFC
        InputStream zIn = expander.getInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(ddd.getOctets())));
        String result = new String(zIn.readAllBytes(), StandardCharsets.US_ASCII);

        // WORK WELL
        System.out.println(result);

        // FAIL HERE, STANDARD ZLIB don't support B64 on compressed data
        compressedData.getContentStream(new ZlibExpanderProvider()).getContentStream().readAllBytes();
        
        // =>> java.util.zip.ZipException: incorrect header check
    }
}

In the RFC :
https://datatracker.ietf.org/doc/html/rfc3274
Which is part of rfc2630 :
https://datatracker.ietf.org/doc/html/rfc2630
Image

Regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions