1 /* Simple S/MIME signing example */
2 #include <openssl/pem.h>
3 #include <openssl/cms.h>
4 #include <openssl/err.h>
6 int main(int argc
, char **argv
)
8 BIO
*in
= NULL
, *out
= NULL
, *tbio
= NULL
;
10 EVP_PKEY
*skey
= NULL
;
11 CMS_ContentInfo
*cms
= NULL
;
15 * For simple S/MIME signing use CMS_DETACHED. On OpenSSL 1.0.0 only: for
16 * streaming detached set CMS_DETACHED|CMS_STREAM for streaming
17 * non-detached set CMS_STREAM
19 int flags
= CMS_DETACHED
| CMS_STREAM
;
21 OpenSSL_add_all_algorithms();
22 ERR_load_crypto_strings();
24 /* Read in signer certificate and private key */
25 tbio
= BIO_new_file("signer.pem", "r");
30 scert
= PEM_read_bio_X509(tbio
, NULL
, 0, NULL
);
34 skey
= PEM_read_bio_PrivateKey(tbio
, NULL
, 0, NULL
);
39 /* Open content being signed */
41 in
= BIO_new_file("sign.txt", "r");
47 cms
= CMS_sign(scert
, skey
, NULL
, in
, flags
);
52 out
= BIO_new_file("smout.txt", "w");
56 if (!(flags
& CMS_STREAM
))
59 /* Write out S/MIME message */
60 if (!SMIME_write_CMS(out
, cms
, in
, flags
))
68 fprintf(stderr
, "Error Signing Data\n");
69 ERR_print_errors_fp(stderr
);
73 CMS_ContentInfo_free(cms
);