aboutsummaryrefslogtreecommitdiff
path: root/openssl/demos/smime
diff options
context:
space:
mode:
Diffstat (limited to 'openssl/demos/smime')
-rw-r--r--openssl/demos/smime/smdec.c107
-rw-r--r--openssl/demos/smime/smenc.c126
-rw-r--r--openssl/demos/smime/smsign.c119
-rw-r--r--openssl/demos/smime/smsign2.c133
-rw-r--r--openssl/demos/smime/smver.c110
5 files changed, 291 insertions, 304 deletions
diff --git a/openssl/demos/smime/smdec.c b/openssl/demos/smime/smdec.c
index 8b1a8545a..6814d5199 100644
--- a/openssl/demos/smime/smdec.c
+++ b/openssl/demos/smime/smdec.c
@@ -4,80 +4,75 @@
#include <openssl/err.h>
int main(int argc, char **argv)
- {
- BIO *in = NULL, *out = NULL, *tbio = NULL;
- X509 *rcert = NULL;
- EVP_PKEY *rkey = NULL;
- PKCS7 *p7 = NULL;
- int ret = 1;
+{
+ BIO *in = NULL, *out = NULL, *tbio = NULL;
+ X509 *rcert = NULL;
+ EVP_PKEY *rkey = NULL;
+ PKCS7 *p7 = NULL;
+ int ret = 1;
- OpenSSL_add_all_algorithms();
- ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+ ERR_load_crypto_strings();
- /* Read in recipient certificate and private key */
- tbio = BIO_new_file("signer.pem", "r");
+ /* Read in recipient certificate and private key */
+ tbio = BIO_new_file("signer.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- BIO_reset(tbio);
+ BIO_reset(tbio);
- rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
+ rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
- if (!rcert || !rkey)
- goto err;
+ if (!rcert || !rkey)
+ goto err;
- /* Open content being signed */
+ /* Open content being signed */
- in = BIO_new_file("smencr.txt", "r");
+ in = BIO_new_file("smencr.txt", "r");
- if (!in)
- goto err;
+ if (!in)
+ goto err;
- /* Sign content */
- p7 = SMIME_read_PKCS7(in, NULL);
+ /* Sign content */
+ p7 = SMIME_read_PKCS7(in, NULL);
- if (!p7)
- goto err;
+ if (!p7)
+ goto err;
- out = BIO_new_file("encrout.txt", "w");
- if (!out)
- goto err;
+ out = BIO_new_file("encrout.txt", "w");
+ if (!out)
+ goto err;
- /* Decrypt S/MIME message */
- if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))
- goto err;
+ /* Decrypt S/MIME message */
+ if (!PKCS7_decrypt(p7, rkey, rcert, out, 0))
+ goto err;
- ret = 0;
+ ret = 0;
- err:
+ err:
- if (ret)
- {
- fprintf(stderr, "Error Signing Data\n");
- ERR_print_errors_fp(stderr);
- }
-
- if (p7)
- PKCS7_free(p7);
- if (rcert)
- X509_free(rcert);
- if (rkey)
- EVP_PKEY_free(rkey);
-
- if (in)
- BIO_free(in);
- if (out)
- BIO_free(out);
- if (tbio)
- BIO_free(tbio);
-
- return ret;
-
- }
+ if (ret) {
+ fprintf(stderr, "Error Signing Data\n");
+ ERR_print_errors_fp(stderr);
+ }
+ if (p7)
+ PKCS7_free(p7);
+ if (rcert)
+ X509_free(rcert);
+ if (rkey)
+ EVP_PKEY_free(rkey);
+ if (in)
+ BIO_free(in);
+ if (out)
+ BIO_free(out);
+ if (tbio)
+ BIO_free(tbio);
+ return ret;
+}
diff --git a/openssl/demos/smime/smenc.c b/openssl/demos/smime/smenc.c
index 77dd732fc..18bf7b161 100644
--- a/openssl/demos/smime/smenc.c
+++ b/openssl/demos/smime/smenc.c
@@ -4,89 +4,89 @@
#include <openssl/err.h>
int main(int argc, char **argv)
- {
- BIO *in = NULL, *out = NULL, *tbio = NULL;
- X509 *rcert = NULL;
- STACK_OF(X509) *recips = NULL;
- PKCS7 *p7 = NULL;
- int ret = 1;
+{
+ BIO *in = NULL, *out = NULL, *tbio = NULL;
+ X509 *rcert = NULL;
+ STACK_OF(X509) *recips = NULL;
+ PKCS7 *p7 = NULL;
+ int ret = 1;
- /*
- * On OpenSSL 0.9.9 only:
- * for streaming set PKCS7_STREAM
- */
- int flags = PKCS7_STREAM;
+ /*
+ * On OpenSSL 0.9.9 only:
+ * for streaming set PKCS7_STREAM
+ */
+ int flags = PKCS7_STREAM;
- OpenSSL_add_all_algorithms();
- ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+ ERR_load_crypto_strings();
- /* Read in recipient certificate */
- tbio = BIO_new_file("signer.pem", "r");
+ /* Read in recipient certificate */
+ tbio = BIO_new_file("signer.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- if (!rcert)
- goto err;
+ if (!rcert)
+ goto err;
- /* Create recipient STACK and add recipient cert to it */
- recips = sk_X509_new_null();
+ /* Create recipient STACK and add recipient cert to it */
+ recips = sk_X509_new_null();
- if (!recips || !sk_X509_push(recips, rcert))
- goto err;
+ if (!recips || !sk_X509_push(recips, rcert))
+ goto err;
- /* sk_X509_pop_free will free up recipient STACK and its contents
- * so set rcert to NULL so it isn't freed up twice.
- */
- rcert = NULL;
+ /*
+ * sk_X509_pop_free will free up recipient STACK and its contents so set
+ * rcert to NULL so it isn't freed up twice.
+ */
+ rcert = NULL;
- /* Open content being encrypted */
+ /* Open content being encrypted */
- in = BIO_new_file("encr.txt", "r");
+ in = BIO_new_file("encr.txt", "r");
- if (!in)
- goto err;
+ if (!in)
+ goto err;
- /* encrypt content */
- p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
+ /* encrypt content */
+ p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
- if (!p7)
- goto err;
+ if (!p7)
+ goto err;
- out = BIO_new_file("smencr.txt", "w");
- if (!out)
- goto err;
+ out = BIO_new_file("smencr.txt", "w");
+ if (!out)
+ goto err;
- /* Write out S/MIME message */
- if (!SMIME_write_PKCS7(out, p7, in, flags))
- goto err;
+ /* Write out S/MIME message */
+ if (!SMIME_write_PKCS7(out, p7, in, flags))
+ goto err;
- ret = 0;
+ ret = 0;
- err:
+ err:
- if (ret)
- {
- fprintf(stderr, "Error Encrypting Data\n");
- ERR_print_errors_fp(stderr);
- }
+ if (ret) {
+ fprintf(stderr, "Error Encrypting Data\n");
+ ERR_print_errors_fp(stderr);
+ }
- if (p7)
- PKCS7_free(p7);
- if (rcert)
- X509_free(rcert);
- if (recips)
- sk_X509_pop_free(recips, X509_free);
+ if (p7)
+ PKCS7_free(p7);
+ if (rcert)
+ X509_free(rcert);
+ if (recips)
+ sk_X509_pop_free(recips, X509_free);
- if (in)
- BIO_free(in);
- if (out)
- BIO_free(out);
- if (tbio)
- BIO_free(tbio);
+ if (in)
+ BIO_free(in);
+ if (out)
+ BIO_free(out);
+ if (tbio)
+ BIO_free(tbio);
- return ret;
+ return ret;
- }
+}
diff --git a/openssl/demos/smime/smsign.c b/openssl/demos/smime/smsign.c
index ba78830cf..c7bf86d37 100644
--- a/openssl/demos/smime/smsign.c
+++ b/openssl/demos/smime/smsign.c
@@ -4,86 +4,85 @@
#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;
+{
+ 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;
+ /*
+ * 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();
+ OpenSSL_add_all_algorithms();
+ ERR_load_crypto_strings();
- /* Read in signer certificate and private key */
- tbio = BIO_new_file("signer.pem", "r");
+ /* Read in signer certificate and private key */
+ tbio = BIO_new_file("signer.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- BIO_reset(tbio);
+ BIO_reset(tbio);
- skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
+ skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
- if (!scert || !skey)
- goto err;
+ if (!scert || !skey)
+ goto err;
- /* Open content being signed */
+ /* Open content being signed */
- in = BIO_new_file("sign.txt", "r");
+ in = BIO_new_file("sign.txt", "r");
- if (!in)
- goto err;
+ if (!in)
+ goto err;
- /* Sign content */
- p7 = PKCS7_sign(scert, skey, NULL, in, flags);
+ /* Sign content */
+ p7 = PKCS7_sign(scert, skey, NULL, in, flags);
- if (!p7)
- goto err;
+ if (!p7)
+ goto err;
- out = BIO_new_file("smout.txt", "w");
- if (!out)
- goto err;
+ out = BIO_new_file("smout.txt", "w");
+ if (!out)
+ goto err;
- if (!(flags & PKCS7_STREAM))
- BIO_reset(in);
+ if (!(flags & PKCS7_STREAM))
+ BIO_reset(in);
- /* Write out S/MIME message */
- if (!SMIME_write_PKCS7(out, p7, in, flags))
- goto err;
+ /* Write out S/MIME message */
+ if (!SMIME_write_PKCS7(out, p7, in, flags))
+ goto err;
- ret = 0;
+ ret = 0;
- err:
+ err:
- if (ret)
- {
- fprintf(stderr, "Error Signing Data\n");
- ERR_print_errors_fp(stderr);
- }
+ 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 (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);
+ if (in)
+ BIO_free(in);
+ if (out)
+ BIO_free(out);
+ if (tbio)
+ BIO_free(tbio);
- return ret;
+ return ret;
- }
+}
diff --git a/openssl/demos/smime/smsign2.c b/openssl/demos/smime/smsign2.c
index ff835c568..a11609434 100644
--- a/openssl/demos/smime/smsign2.c
+++ b/openssl/demos/smime/smsign2.c
@@ -4,104 +4,99 @@
#include <openssl/err.h>
int main(int argc, char **argv)
- {
- BIO *in = NULL, *out = NULL, *tbio = NULL;
- X509 *scert = NULL, *scert2 = NULL;
- EVP_PKEY *skey = NULL, *skey2 = NULL;
- PKCS7 *p7 = NULL;
- int ret = 1;
+{
+ BIO *in = NULL, *out = NULL, *tbio = NULL;
+ X509 *scert = NULL, *scert2 = NULL;
+ EVP_PKEY *skey = NULL, *skey2 = NULL;
+ PKCS7 *p7 = NULL;
+ int ret = 1;
- OpenSSL_add_all_algorithms();
- ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+ ERR_load_crypto_strings();
- tbio = BIO_new_file("signer.pem", "r");
+ tbio = BIO_new_file("signer.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ scert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- BIO_reset(tbio);
+ BIO_reset(tbio);
- skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
+ skey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
- BIO_free(tbio);
+ BIO_free(tbio);
- tbio = BIO_new_file("signer2.pem", "r");
+ tbio = BIO_new_file("signer2.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ scert2 = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- BIO_reset(tbio);
+ BIO_reset(tbio);
- skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
+ skey2 = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
- if (!scert2 || !skey2)
- goto err;
+ if (!scert2 || !skey2)
+ goto err;
- in = BIO_new_file("sign.txt", "r");
+ in = BIO_new_file("sign.txt", "r");
- if (!in)
- goto err;
+ if (!in)
+ goto err;
- p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM|PKCS7_PARTIAL);
+ p7 = PKCS7_sign(NULL, NULL, NULL, in, PKCS7_STREAM | PKCS7_PARTIAL);
- if (!p7)
- goto err;
+ if (!p7)
+ goto err;
- /* Add each signer in turn */
+ /* Add each signer in turn */
- if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
- goto err;
+ if (!PKCS7_sign_add_signer(p7, scert, skey, NULL, 0))
+ goto err;
- if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
- goto err;
+ if (!PKCS7_sign_add_signer(p7, scert2, skey2, NULL, 0))
+ goto err;
- out = BIO_new_file("smout.txt", "w");
- if (!out)
- goto err;
+ out = BIO_new_file("smout.txt", "w");
+ if (!out)
+ goto err;
- /* NB: content included and finalized by SMIME_write_PKCS7 */
+ /* NB: content included and finalized by SMIME_write_PKCS7 */
- if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
- goto err;
+ if (!SMIME_write_PKCS7(out, p7, in, PKCS7_STREAM))
+ goto err;
- ret = 0;
+ ret = 0;
- err:
+ err:
- if (ret)
- {
- fprintf(stderr, "Error Signing Data\n");
- ERR_print_errors_fp(stderr);
- }
+ if (ret) {
+ fprintf(stderr, "Error Signing Data\n");
+ ERR_print_errors_fp(stderr);
+ }
- if (p7)
- PKCS7_free(p7);
+ if (p7)
+ PKCS7_free(p7);
- if (scert)
- X509_free(scert);
- if (skey)
- EVP_PKEY_free(skey);
-
- if (scert2)
- X509_free(scert2);
- if (skey)
- EVP_PKEY_free(skey2);
-
- if (in)
- BIO_free(in);
- if (out)
- BIO_free(out);
- if (tbio)
- BIO_free(tbio);
-
- return ret;
-
- }
+ if (scert)
+ X509_free(scert);
+ if (skey)
+ EVP_PKEY_free(skey);
+ if (scert2)
+ X509_free(scert2);
+ if (skey)
+ EVP_PKEY_free(skey2);
+ if (in)
+ BIO_free(in);
+ if (out)
+ BIO_free(out);
+ if (tbio)
+ BIO_free(tbio);
+ return ret;
+}
diff --git a/openssl/demos/smime/smver.c b/openssl/demos/smime/smver.c
index 9d360c273..21937df30 100644
--- a/openssl/demos/smime/smver.c
+++ b/openssl/demos/smime/smver.c
@@ -4,84 +4,82 @@
#include <openssl/err.h>
int main(int argc, char **argv)
- {
- BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
- X509_STORE *st = NULL;
- X509 *cacert = NULL;
- PKCS7 *p7 = NULL;
+{
+ BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
+ X509_STORE *st = NULL;
+ X509 *cacert = NULL;
+ PKCS7 *p7 = NULL;
- int ret = 1;
+ int ret = 1;
- OpenSSL_add_all_algorithms();
- ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+ ERR_load_crypto_strings();
- /* Set up trusted CA certificate store */
+ /* Set up trusted CA certificate store */
- st = X509_STORE_new();
+ st = X509_STORE_new();
- /* Read in signer certificate and private key */
- tbio = BIO_new_file("cacert.pem", "r");
+ /* Read in signer certificate and private key */
+ tbio = BIO_new_file("cacert.pem", "r");
- if (!tbio)
- goto err;
+ if (!tbio)
+ goto err;
- cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+ cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
- if (!cacert)
- goto err;
+ if (!cacert)
+ goto err;
- if (!X509_STORE_add_cert(st, cacert))
- goto err;
+ if (!X509_STORE_add_cert(st, cacert))
+ goto err;
- /* Open content being signed */
+ /* Open content being signed */
- in = BIO_new_file("smout.txt", "r");
+ in = BIO_new_file("smout.txt", "r");
- if (!in)
- goto err;
+ if (!in)
+ goto err;
- /* Sign content */
- p7 = SMIME_read_PKCS7(in, &cont);
+ /* Sign content */
+ p7 = SMIME_read_PKCS7(in, &cont);
- if (!p7)
- goto err;
+ if (!p7)
+ goto err;
- /* File to output verified content to */
- out = BIO_new_file("smver.txt", "w");
- if (!out)
- goto err;
+ /* File to output verified content to */
+ out = BIO_new_file("smver.txt", "w");
+ if (!out)
+ goto err;
- if (!PKCS7_verify(p7, NULL, st, cont, out, 0))
- {
- fprintf(stderr, "Verification Failure\n");
- goto err;
- }
+ if (!PKCS7_verify(p7, NULL, st, cont, out, 0)) {
+ fprintf(stderr, "Verification Failure\n");
+ goto err;
+ }
- fprintf(stderr, "Verification Successful\n");
+ fprintf(stderr, "Verification Successful\n");
- ret = 0;
+ ret = 0;
- err:
+ err:
- if (ret)
- {
- fprintf(stderr, "Error Verifying Data\n");
- ERR_print_errors_fp(stderr);
- }
+ if (ret) {
+ fprintf(stderr, "Error Verifying Data\n");
+ ERR_print_errors_fp(stderr);
+ }
- if (p7)
- PKCS7_free(p7);
+ if (p7)
+ PKCS7_free(p7);
- if (cacert)
- X509_free(cacert);
+ if (cacert)
+ X509_free(cacert);
- if (in)
- BIO_free(in);
- if (out)
- BIO_free(out);
- if (tbio)
- BIO_free(tbio);
+ if (in)
+ BIO_free(in);
+ if (out)
+ BIO_free(out);
+ if (tbio)
+ BIO_free(tbio);
- return ret;
+ return ret;
- }
+}