Zig Requires Chilkat v11.0.0+
Zig
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Connect to an IMAP server.
// Use TLS
imap.setSsl(true);
imap.setPort(993);
success = if (imap.connect("imap.example.com")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
success = if (imap.login("myLogin", "myPassword")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Select an IMAP mailbox
success = if (imap.selectMailbox("Inbox")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Download the 1st email (as MIME) in the Inbox by sequence number.
const sb_mime = try chilkat.StringBuilder.init();
defer sb_mime.deinit();
success = if (imap.fetchSingleAsMimeSb(1, false, sb_mime)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Load it into a MIME object and check to see if it is signed
const mime = try chilkat.Mime.init();
defer mime.deinit();
mime.loadMimeSb(sb_mime) catch {};
var already_saved_parts: bool = false;
if (mime.containsSignedParts()) {
// This will save the .p7s and other parts...
const st = try chilkat.StringTable.init();
defer st.deinit();
success = if (mime.partsToFiles("c:/temp/qa_output", st)) true else |_| false;
if (success) {
const num_files = st.getCount();
var i: i32 = 0;
while (i < num_files) {
std.debug.print("Created: {s}\n", .{try st.stringAt(alloc, i)});
i = i + 1;
}
already_saved_parts = true;
}
}
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
const email = try chilkat.Email.init();
defer email.deinit();
email.setFromMimeSb(sb_mime) catch {};
if (email.getReceivedSigned()) {
std.debug.print("This email was signed.\n", .{});
// Check to see if the signatures were verified.
if (email.getSignaturesValid()) {
std.debug.print("Digital signature(s) verified.\n", .{});
std.debug.print("Signer: {s}\n", .{try email.getSignedBy(alloc)});
// The certificate used for signing may be obtained
// by calling email.LastSignerCert. Index 0 is the first signer.
const cert = try chilkat.Cert.init();
defer cert.deinit();
success = if (email.lastSignerCert(0, cert)) true else |_| false;
if (!success) {
std.debug.print("Failed to get signing certificate object.\n", .{});
} else {
std.debug.print("Signing cert: {s}\n", .{try cert.getSubjectCN(alloc)});
}
} else {
std.debug.print("Digital signature verification failed.\n", .{});
}
} else {
std.debug.print("This email was not signed.\n", .{});
}
if (!already_saved_parts) {
email.saveAllAttachments("c:/temp/qa_output") catch {};
}
}