]> WPIA git - cassiopeia.git/blobdiff - lib/openssl/demos/smime/smenc.c
upd: openssl to 1.1.0
[cassiopeia.git] / lib / openssl / demos / smime / smenc.c
index 77dd732fc1665df97c7e9c66e851581e1d788183..5d36e9a4979444a012fe37efb0fc9fde79299162 100644 (file)
@@ -1,92 +1,91 @@
+/*
+ * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
 /* Simple S/MIME encrypt 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 *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;
-
-       OpenSSL_add_all_algorithms();
-       ERR_load_crypto_strings();
-
-       /* Read in recipient certificate */
-       tbio = BIO_new_file("signer.pem", "r");
-
-       if (!tbio)
-               goto err;
+{
+    BIO *in = NULL, *out = NULL, *tbio = NULL;
+    X509 *rcert = NULL;
+    STACK_OF(X509) *recips = NULL;
+    PKCS7 *p7 = NULL;
+    int ret = 1;
 
-       rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
+    /*
+     * On OpenSSL 0.9.9 only:
+     * for streaming set PKCS7_STREAM
+     */
+    int flags = PKCS7_STREAM;
 
-       if (!rcert)
-               goto err;
+    OpenSSL_add_all_algorithms();
+    ERR_load_crypto_strings();
 
-       /* Create recipient STACK and add recipient cert to it */
-       recips = sk_X509_new_null();
+    /* Read in recipient certificate */
+    tbio = BIO_new_file("signer.pem", "r");
 
-       if (!recips || !sk_X509_push(recips, rcert))
-               goto err;
+    if (!tbio)
+        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;
+    rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
 
-       /* Open content being encrypted */
+    if (!rcert)
+        goto err;
 
-       in = BIO_new_file("encr.txt", "r");
+    /* Create recipient STACK and add recipient cert to it */
+    recips = sk_X509_new_null();
 
-       if (!in)
-               goto err;
+    if (!recips || !sk_X509_push(recips, rcert))
+        goto err;
 
-       /* encrypt content */
-       p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
+    /*
+     * 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;
 
-       if (!p7)
-               goto err;
+    /* Open content being encrypted */
 
-       out = BIO_new_file("smencr.txt", "w");
-       if (!out)
-               goto err;
+    in = BIO_new_file("encr.txt", "r");
 
-       /* Write out S/MIME message */
-       if (!SMIME_write_PKCS7(out, p7, in, flags))
-               goto err;
+    if (!in)
+        goto err;
 
-       ret = 0;
+    /* encrypt content */
+    p7 = PKCS7_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
 
-       err:
+    if (!p7)
+        goto err;
 
-       if (ret)
-               {
-               fprintf(stderr, "Error Encrypting Data\n");
-               ERR_print_errors_fp(stderr);
-               }
+    out = BIO_new_file("smencr.txt", "w");
+    if (!out)
+        goto err;
 
-       if (p7)
-               PKCS7_free(p7);
-       if (rcert)
-               X509_free(rcert);
-       if (recips)
-               sk_X509_pop_free(recips, X509_free);
+    /* Write out S/MIME message */
+    if (!SMIME_write_PKCS7(out, p7, in, flags))
+        goto err;
 
-       if (in)
-               BIO_free(in);
-       if (out)
-               BIO_free(out);
-       if (tbio)
-               BIO_free(tbio);
+    ret = 0;
 
-       return ret;
+ err:
+    if (ret) {
+        fprintf(stderr, "Error Encrypting Data\n");
+        ERR_print_errors_fp(stderr);
+    }
+    PKCS7_free(p7);
+    X509_free(rcert);
+    sk_X509_pop_free(recips, X509_free);
+    BIO_free(in);
+    BIO_free(out);
+    BIO_free(tbio);
+    return ret;
 
-       }
+}