Sample code for 30+ languages & platforms
Go Requires Chilkat v11.8.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Go Downloads

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    email := chilkat.NewEmail()
    //  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
    //  is the password of the private key.
    pem := chilkat.NewPem()
    success = pem.LoadPemFile("recipient.pem","RECIPIENT_PASSWORD")
    if success != true {
        fmt.Println(pem.LastErrorText())
        email.DisposeEmail()
        pem.DisposePem()
        return
    }

    success = email.AddPemSource(pem)
    if success != true {
        fmt.Println(email.LastErrorText())
        email.DisposeEmail()
        pem.DisposePem()
        return
    }

    success = email.LoadEml("signed-encrypted.eml")
    if success != true {
        fmt.Println(email.LastErrorText())
        email.DisposeEmail()
        pem.DisposePem()
        return
    }

    if (email.ReceivedEncrypted() != true) || (email.Decrypted() != true) {
        fmt.Println("Expected a successfully decrypted S/MIME envelope.")
        email.DisposeEmail()
        pem.DisposePem()
        return
    }

    if (email.ReceivedSigned() != true) || (email.SignaturesValid() != true) {
        fmt.Println("Expected a valid signature inside the envelope.")
        email.DisposeEmail()
        pem.DisposePem()
        return
    }

    //  This message has one signer. Examine its certificate after verification.
    signer := chilkat.NewCert()
    success = email.LastSignerCert(0,signer)
    if success != true {
        fmt.Println(email.LastErrorText())
        email.DisposeEmail()
        pem.DisposePem()
        signer.DisposeCert()
        return
    }

    fmt.Println("Signer certificate: ", signer.SubjectDN())
    fmt.Println("Recovered subject: ", email.Subject())
    fmt.Println("Recovered attachments: ", email.NumAttachments())

    email.DisposeEmail()
    pem.DisposePem()
    signer.DisposeCert()