Search code examples
phpopensslp7m

PHP: how to extract wrapped content of a .p7m file?


We receive a lot of p7m files, "wrapped Cades" only.

We need to extract content from p7m.

I am able to do manually using an hex editor, cutting before and after the PDF content, or using openssl command.

Is there a way to do it using php ? I can see openssl extension but I'm not able to figure out a simple extraction method.

Note: Content is wrapped, not crypted.

I am amble to find python only answers, like this


Solution

  • I found a perfect package on github

    https://github.com/filippotoso/p7m-extractor

    It does exactly what I need. And it works in Laravel (I tried it on laravel 9)

        use FilippoToso\P7MExtractor\P7M;
    
        .....
    
        $success = (new P7M("<full path to openssl executable>"))
            ->setSource($pathToFileToExtract)
            ->setDestination($pathToExtractedPdf)
            ->save()
        ;
    

    It requires your system has an openssl.exe already installed. It easy to find a windows build here: https://slproweb.com/products/Win32OpenSSL.html