cms_cd.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2008-2022 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. #include "internal/cryptlib.h"
  10. #include <openssl/asn1t.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/err.h>
  14. #include <openssl/cms.h>
  15. #include <openssl/bio.h>
  16. #include <openssl/comp.h>
  17. #include "cms_local.h"
  18. #ifndef OPENSSL_NO_ZLIB
  19. /* CMS CompressedData Utilities */
  20. CMS_ContentInfo *ossl_cms_CompressedData_create(int comp_nid,
  21. OSSL_LIB_CTX *libctx,
  22. const char *propq)
  23. {
  24. CMS_ContentInfo *cms;
  25. CMS_CompressedData *cd;
  26. /*
  27. * Will need something cleverer if there is ever more than one
  28. * compression algorithm or parameters have some meaning...
  29. */
  30. if (comp_nid != NID_zlib_compression) {
  31. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  32. return NULL;
  33. }
  34. cms = CMS_ContentInfo_new_ex(libctx, propq);
  35. if (cms == NULL)
  36. return NULL;
  37. cd = M_ASN1_new_of(CMS_CompressedData);
  38. if (cd == NULL)
  39. goto err;
  40. cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData);
  41. cms->d.compressedData = cd;
  42. cd->version = 0;
  43. (void)X509_ALGOR_set0(cd->compressionAlgorithm,
  44. OBJ_nid2obj(NID_zlib_compression),
  45. V_ASN1_UNDEF, NULL); /* cannot fail */
  46. cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
  47. return cms;
  48. err:
  49. CMS_ContentInfo_free(cms);
  50. return NULL;
  51. }
  52. BIO *ossl_cms_CompressedData_init_bio(const CMS_ContentInfo *cms)
  53. {
  54. CMS_CompressedData *cd;
  55. const ASN1_OBJECT *compoid;
  56. if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData) {
  57. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA);
  58. return NULL;
  59. }
  60. cd = cms->d.compressedData;
  61. X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm);
  62. if (OBJ_obj2nid(compoid) != NID_zlib_compression) {
  63. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  64. return NULL;
  65. }
  66. return BIO_new(BIO_f_zlib());
  67. }
  68. #endif