diff options
author | Dr. Stephen Henson <steve@openssl.org> | 2007-04-13 20:40:47 +0000 |
---|---|---|
committer | Dr. Stephen Henson <steve@openssl.org> | 2007-04-13 20:40:47 +0000 |
commit | b2b2dafc287a948e1118de3aed49b4517b2b7fb2 (patch) | |
tree | 95885d52af391d06d36b54f5a0e2418e7c3e0895 /demos/smime/smsign.c | |
parent | 14ab6cdd69341aef005891ab2aa4aec8760c2eea (diff) |
Add a bunch of S/MIME sample programs and data.
Diffstat (limited to 'demos/smime/smsign.c')
-rw-r--r-- | demos/smime/smsign.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/demos/smime/smsign.c b/demos/smime/smsign.c new file mode 100644 index 0000000000..ba78830cff --- /dev/null +++ b/demos/smime/smsign.c @@ -0,0 +1,89 @@ +/* Simple S/MIME signing example */ +#include <openssl/pem.h> +#include <openssl/pkcs7.h> +#include <openssl/err.h> + +int main(int argc, char **argv) + { + BIO *in = NULL, *out = NULL, *tbio = NULL; + X509 *scert = NULL; + EVP_PKEY *skey = NULL; + PKCS7 *p7 = NULL; + int ret = 1; + + /* For simple S/MIME signing use PKCS7_DETACHED. + * On OpenSSL 0.9.9 only: + * for streaming detached set PKCS7_DETACHED|PKCS7_STREAM + * for streaming non-detached set PKCS7_STREAM + */ + int flags = PKCS7_DETACHED|PKCS7_STREAM; + + OpenSSL_add_all_algorithms(); + ERR_load_crypto_strings(); + + /* Read in signer certificate and private key */ + tbio = BIO_new_file("signer.pem", "r"); + + if (!tbio) + goto err; + + scert = PEM_read_bio_X509(tbio, NULL, 0, NULL); + + BIO_reset(tbio); + + skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL); + + if (!scert || !skey) + goto err; + + /* Open content being signed */ + + in = BIO_new_file("sign.txt", "r"); + + if (!in) + goto err; + + /* Sign content */ + p7 = PKCS7_sign(scert, skey, NULL, in, flags); + + if (!p7) + goto err; + + out = BIO_new_file("smout.txt", "w"); + if (!out) + goto err; + + if (!(flags & PKCS7_STREAM)) + BIO_reset(in); + + /* Write out S/MIME message */ + if (!SMIME_write_PKCS7(out, p7, in, flags)) + goto err; + + ret = 0; + + err: + + if (ret) + { + fprintf(stderr, "Error Signing Data\n"); + ERR_print_errors_fp(stderr); + } + + if (p7) + PKCS7_free(p7); + if (scert) + X509_free(scert); + if (skey) + EVP_PKEY_free(skey); + + if (in) + BIO_free(in); + if (out) + BIO_free(out); + if (tbio) + BIO_free(tbio); + + return ret; + + } |