Sample code for 30+ languages & platforms
SQL Server

Extract XML from FatturaPA .p7m

See more Digital Signatures Examples

Demonstrates how to verify the signature and extract the XML from a FatturaPA .p7m file.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/p7m/IT01879020517_abc.xml.p7m'
    IF @success <> 1
      BEGIN

        PRINT 'Failed to load the .p7m file'
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT

    -- Verify and extrct the payload contained within the .p7m.
    -- In this case, the payload is the FatturaPA XML.
    -- If successful, the resulting bd will contain only the XML.
    DECLARE @bVerified int
    EXEC sp_OAMethod @crypt, 'OpaqueVerifyBd', @bVerified OUT, @bd
    IF @bVerified <> 1
      BEGIN
        EXEC sp_OAGetProperty @crypt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed to extract and verify.'
        EXEC @hr = sp_OADestroy @bd
        EXEC @hr = sp_OADestroy @crypt
        RETURN
      END

    -- Save the XML to a file.
    EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, 'qa_output/zIT01879020517_abc.xml'

    -- Alternatively, load into an XML object and emit.
    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAMethod @bd, 'GetString', @sTmp0 OUT, 'utf-8'
    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0

    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @crypt
    EXEC @hr = sp_OADestroy @xml


END
GO