cms_comp.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Simple S/MIME compress example */
  10. #include <openssl/pem.h>
  11. #include <openssl/cms.h>
  12. #include <openssl/err.h>
  13. int main(int argc, char **argv)
  14. {
  15. BIO *in = NULL, *out = NULL;
  16. CMS_ContentInfo *cms = NULL;
  17. int ret = EXIT_FAILURE;
  18. /*
  19. * On OpenSSL 1.0.0+ only:
  20. * for streaming set CMS_STREAM
  21. */
  22. int flags = CMS_STREAM;
  23. OpenSSL_add_all_algorithms();
  24. ERR_load_crypto_strings();
  25. /* Open content being compressed */
  26. in = BIO_new_file("comp.txt", "r");
  27. if (!in)
  28. goto err;
  29. /* compress content */
  30. cms = CMS_compress(in, NID_zlib_compression, flags);
  31. if (!cms)
  32. goto err;
  33. out = BIO_new_file("smcomp.txt", "w");
  34. if (!out)
  35. goto err;
  36. /* Write out S/MIME message */
  37. if (!SMIME_write_CMS(out, cms, in, flags))
  38. goto err;
  39. ret = EXIT_SUCCESS;
  40. err:
  41. if (ret != EXIT_SUCCESS) {
  42. fprintf(stderr, "Error Compressing Data\n");
  43. ERR_print_errors_fp(stderr);
  44. }
  45. CMS_ContentInfo_free(cms);
  46. BIO_free(in);
  47. BIO_free(out);
  48. return ret;
  49. }