X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=lib%2Fopenssl%2Fdemos%2Fcms%2Fcms_denc.c;h=adba69b96dbdca6dd59cd53092a5e2442f7eb936;hb=02ed66432c92de70694700164f986190aad3cbc5;hp=9265e47bf95d8637b508debb32517d38dfed0249;hpb=c8c91f12a38643790dcbb1d64eee95a986ef021d;p=cassiopeia.git diff --git a/lib/openssl/demos/cms/cms_denc.c b/lib/openssl/demos/cms/cms_denc.c index 9265e47..adba69b 100644 --- a/lib/openssl/demos/cms/cms_denc.c +++ b/lib/openssl/demos/cms/cms_denc.c @@ -1,97 +1,97 @@ -/* S/MIME detached data encrypt example: rarely done but - * should the need arise this is an example.... +/* + * Copyright 2008-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 + */ + +/* + * S/MIME detached data encrypt example: rarely done but should the need + * arise this is an example.... */ #include #include #include int main(int argc, char **argv) - { - BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL; - X509 *rcert = NULL; - STACK_OF(X509) *recips = NULL; - CMS_ContentInfo *cms = NULL; - int ret = 1; - - int flags = CMS_STREAM|CMS_DETACHED; - - OpenSSL_add_all_algorithms(); - ERR_load_crypto_strings(); - - /* Read in recipient certificate */ - tbio = BIO_new_file("signer.pem", "r"); +{ + BIO *in = NULL, *out = NULL, *tbio = NULL, *dout = NULL; + X509 *rcert = NULL; + STACK_OF(X509) *recips = NULL; + CMS_ContentInfo *cms = NULL; + int ret = 1; - if (!tbio) - goto err; + int flags = CMS_STREAM | CMS_DETACHED; - rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL); + OpenSSL_add_all_algorithms(); + ERR_load_crypto_strings(); - if (!rcert) - goto err; + /* Read in recipient certificate */ + tbio = BIO_new_file("signer.pem", "r"); - /* Create recipient STACK and add recipient cert to it */ - recips = sk_X509_new_null(); + if (!tbio) + goto err; - if (!recips || !sk_X509_push(recips, rcert)) - goto err; + rcert = PEM_read_bio_X509(tbio, NULL, 0, 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; + if (!rcert) + goto err; - /* Open content being encrypted */ + /* Create recipient STACK and add recipient cert to it */ + recips = sk_X509_new_null(); - in = BIO_new_file("encr.txt", "r"); + if (!recips || !sk_X509_push(recips, rcert)) + goto err; - dout = BIO_new_file("smencr.out", "wb"); + /* + * 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 (!in) - goto err; + /* Open content being encrypted */ - /* encrypt content */ - cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); + in = BIO_new_file("encr.txt", "r"); - if (!cms) - goto err; + dout = BIO_new_file("smencr.out", "wb"); - out = BIO_new_file("smencr.pem", "w"); - if (!out) - goto err; + if (!in) + goto err; - if (!CMS_final(cms, in, dout, flags)) - goto err; + /* encrypt content */ + cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags); - /* Write out CMS structure without content */ - if (!PEM_write_bio_CMS(out, cms)) - goto err; + if (!cms) + goto err; - ret = 0; + out = BIO_new_file("smencr.pem", "w"); + if (!out) + goto err; - err: + if (!CMS_final(cms, in, dout, flags)) + goto err; - if (ret) - { - fprintf(stderr, "Error Encrypting Data\n"); - ERR_print_errors_fp(stderr); - } + /* Write out CMS structure without content */ + if (!PEM_write_bio_CMS(out, cms)) + goto err; - if (cms) - CMS_ContentInfo_free(cms); - if (rcert) - X509_free(rcert); - if (recips) - sk_X509_pop_free(recips, X509_free); + ret = 0; - if (in) - BIO_free(in); - if (out) - BIO_free(out); - if (dout) - BIO_free(dout); - if (tbio) - BIO_free(tbio); + err: - return ret; + if (ret) { + fprintf(stderr, "Error Encrypting Data\n"); + ERR_print_errors_fp(stderr); + } - } + CMS_ContentInfo_free(cms); + X509_free(rcert); + sk_X509_pop_free(recips, X509_free); + BIO_free(in); + BIO_free(out); + BIO_free(dout); + BIO_free(tbio); + return ret; +}