Sample code for 30+ languages & platforms
Zig

Create ICP-Brasil Compliant CMS Signature

See more CAdES Examples

Demonstrates how to create a .p7s signature that contains a data file, which in this case is a PDF. The .p7s will be compliant with the ICP-Brazil Digital Signature Standard.

The .p7s file created by this example can be verified at ICP-Brasil Online Verifier

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    // ------------------------------------------------------------------------------------------------------
    // Note: This example creates a CMS signature (.p7s) that contains the PDF.
    // It is different than signing a PDF.  To sign a PDF where the resulting PDF contains the CMS signature,
    // see this example:  Sign PDF for ICP-Brasil
    // ------------------------------------------------------------------------------------------------------
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // Any type of file can be signed.  It doesn't have to be a PDF.
    const in_file = "qa_data/pdf/helloWorld.pdf";

    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.loadFile(in_file) catch {
        std.debug.print("Failed to load {s}\n", .{in_file});
        return;
    };

    // We'll be using a certificate w/ private key stored on a smartcard for signing.
    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // If the smartcard or token requires a PIN, we can set it here to avoid the dialog...
    cert.setSmartCardPin("000000");

    cert.loadFromSmartcard("") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Tell the crypt component to use this cert.
    crypt.setSigningCert(cert) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Set properties for signing...
    crypt.setHashAlgorithm("sha256");

    const json_signing_attrs = try chilkat.JsonObject.init();
    defer json_signing_attrs.deinit();
    json_signing_attrs.updateInt("contentType", 1) catch {};
    json_signing_attrs.updateInt("signingTime", 1) catch {};
    json_signing_attrs.updateInt("messageDigest", 1) catch {};
    json_signing_attrs.updateBool("signingCertificateV2", true) catch {};

    // Listed here are the currently existing profiles. (Chilkat will add additional ICP Brasil policy profiles in future versions as new ones are created.)
    // See https://www.gov.br/iti/pt-br/assuntos/repositorio/artefatos-de-assinatura-digital for more information.
    //
    // PA_AD_RA --> 2.16.76.1.7.1.5.1
    // PA_AD_RA_v1_1 --> 2.16.76.1.7.1.5.1.1
    // PA_AD_RA_v1_2 --> 2.16.76.1.7.1.5.1.2
    // PA_AD_RA_v2_0 --> 2.16.76.1.7.1.5.2
    // PA_AD_RA_v2_1 --> 2.16.76.1.7.1.5.2.1
    // PA_AD_RA_v2_2 --> 2.16.76.1.7.1.5.2.2
    // PA_AD_RA_v2_3 --> 2.16.76.1.7.1.5.2.3
    // PA_AD_RA_v2_4 --> 2.16.76.1.7.1.5.2.4
    // PA_AD_RB --> 2.16.76.1.7.1.1.1
    // PA_AD_RB_v1_1 --> 2.16.76.1.7.1.1.1.1
    // PA_AD_RB_v2_0 --> 2.16.76.1.7.1.1.2
    // PA_AD_RB_v2_1 --> 2.16.76.1.7.1.1.2.1
    // PA_AD_RB_v2_2 --> 2.16.76.1.7.1.1.2.2
    // PA_AD_RB_v2_3 --> 2.16.76.1.7.1.1.2.3
    // PA_AD_RC --> 2.16.76.1.7.1.4.1
    // PA_AD_RC_v1_1 --> 2.16.76.1.7.1.4.1.1
    // PA_AD_RC_v2_0 --> 2.16.76.1.7.1.4.2
    // PA_AD_RC_v2_1 --> 2.16.76.1.7.1.4.2.1
    // PA_AD_RC_v2_2 --> 2.16.76.1.7.1.4.2.2
    // PA_AD_RC_v2_3 --> 2.16.76.1.7.1.4.2.3
    // PA_AD_RT --> 2.16.76.1.7.1.2.1
    // PA_AD_RT_v1_1 --> 2.16.76.1.7.1.2.1.1
    // PA_AD_RT_v2_0 --> 2.16.76.1.7.1.2.2
    // PA_AD_RT_v2_1 --> 2.16.76.1.7.1.2.2.1
    // PA_AD_RT_v2_2 --> 2.16.76.1.7.1.2.2.2
    // PA_AD_RT_v2_3 --> 2.16.76.1.7.1.2.2.3
    // PA_AD_RV --> 2.16.76.1.7.1.3.1
    // PA_AD_RV_v1_1 --> 2.16.76.1.7.1.3.1.1
    // PA_AD_RV_v2_0 --> 2.16.76.1.7.1.3.2
    // PA_AD_RV_v2_1 --> 2.16.76.1.7.1.3.2.1
    // PA_AD_RV_v2_2 --> 2.16.76.1.7.1.3.2.2
    // PA_AD_RV_v2_3 --> 2.16.76.1.7.1.3.2.3

    // Set the policy OID and the profile name
    json_signing_attrs.updateString("policyId.id", "2.16.76.1.7.1.1.2.3") catch {};
    json_signing_attrs.updateString("policyId.profile", "PA_AD_RB_v2_3") catch {};

    crypt.setSigningAttributes(try json_signing_attrs.emit(alloc));

    // The Brazil government validator requires the ASN.1 data to be in "constructed octets" form..
    crypt.setUncommonOptions("UseConstructedOctets,OmitAlgorithmIdNull");
    crypt.setIncludeCertChain(false);

    // Sign.
    crypt.opaqueSignBd(bd) catch {
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    // Save to a .p7s
    try bd.writeFile("qa_output/helloWorld.pdf.p7s");

    std.debug.print("Success\n", .{});
}