Thread: SMIME digital signature

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    SMIME digital signature

    Hello,
    I'm trying to write an app which digitally signs emails. The problem I have is that SMIME_write_PKCS7() won't output any MIME headers the email previously had. I tested it with mails from my client so the headers should be ok.

    Here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <openssl/bio.h>
    #include <openssl/pkcs7.h>
    #include <openssl/pkcs12.h>
    #include <openssl/x509.h>
    
    / * Arguments: to be signed email, PKCS#12 certificate with private key
     */
    int main(int argc, char** argv) {
    
        if (argc < 3) exit(1);
        OpenSSL_add_all_algorithms();
    
        BIO *data = BIO_new_file(argv[1], "rb");
        if (!data) exit(1);
    
        BIO *p12file = BIO_new_file(argv[2], "rb");
        if (!p12file) exit(1);
        PKCS12 *p12 = d2i_PKCS12_bio(p12file, NULL);
        if (!p12) exit(1);
    
        X509 *signcert;
        EVP_PKEY *pkey;
        STACK_OF(X509) *ca;
        int ret = PKCS12_parse(p12, "", &pkey, &signcert, &ca);
        if (!ret) exit(1);
    
        PKCS7 *p7 = PKCS7_sign(signcert, pkey, ca, data, 0);
        if (!p7) exit(1);
    
        BIO *out = BIO_new_file("email.eml", "wb");
        if (!out) exit(1);
    
        ret = SMIME_write_PKCS7(out, p7, data, 0);
        if (!ret) exit(1);
    
        BIO_free(data);
        BIO_free(p12file);
        PKCS12_free(p12);
        PKCS7_free(p7);
    
        return (EXIT_SUCCESS);
    }
    Thank you for any advice I can get.
    Last edited by Yawney; 11-23-2010 at 05:53 AM. Reason: Added the return value of SMIME_write_PKCS7().

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OpenSSL: Documents, SMIME_write_PKCS7(3)
    Why is flags zero?
    Why are you ignoring the return result?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Ignoring the return is my fault but it doesn't change anything. The program actually produces signed email but it completely ignores the MIME headers. The flags are 0 because I figured I don't need any.

    I think it could have something to do with parsing the original mail and CRLF/LF endings of lines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help!! C8051F226 with uOLED-96-G1.
    By vailant in forum C Programming
    Replies: 11
    Last Post: 01-28-2010, 06:30 AM
  2. Displaying Values...
    By vailant in forum C Programming
    Replies: 35
    Last Post: 01-21-2010, 06:59 PM
  3. Creating a Digital Signature
    By jeeva in forum Linux Programming
    Replies: 8
    Last Post: 01-18-2002, 06:10 AM
  4. Wav file to digital value conversion
    By RpiMatty in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2001, 06:42 AM
  5. Creating a Digital Signature
    By jeeva in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2001, 02:37 PM