Sample code for 30+ languages & platforms
SQL Server

Convert a MIME Part's ASN.1 Body to XML

See more MIME Examples

Demonstrates the Chilkat Mime.AsnBodyToXml method, which parses the current part's decoded body as ASN.1 DER and returns an XML representation of the ASN.1 structure. It takes no arguments.

Tip: Code to parse the returned XML can be generated at Chilkat Tools.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own file.

Background: ASN.1 DER is the binary encoding behind PKCS #7/CMS structures — the signatures and encrypted blobs in S/MIME — and it is opaque to the naked eye. Rendering it as XML exposes the nested structure (content types, certificates, signer info) so you can inspect exactly what a signature or encrypted part contains. It is primarily a diagnostic and learning tool.

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
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Mime.AsnBodyToXml method, which parses the current part's decoded body as
    --  ASN.1 DER and returns an XML representation of the ASN.1 structure.  It takes no arguments.
    --  This is primarily useful for inspecting binary ASN.1 content such as PKCS #7 structures.

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

    EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'qa_data/pkcs7_part.p7m'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    DECLARE @asnXml nvarchar(4000)
    EXEC sp_OAMethod @mime, 'AsnBodyToXml', @asnXml OUT
    EXEC sp_OAGetProperty @mime, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    PRINT @asnXml
    --  Code to parse this XML can be generated at Chilkat's online tool:
    --  https://tools.chilkat.io/xmlParse

    EXEC @hr = sp_OADestroy @mime


END
GO