cms_io.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <openssl/asn1t.h>
  10. #include <openssl/x509.h>
  11. #include <openssl/err.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/cms.h>
  14. #include "cms_local.h"
  15. /* unfortunately cannot constify BIO_new_NDEF() due to this and PKCS7_stream() */
  16. int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
  17. {
  18. ASN1_OCTET_STRING **pos;
  19. pos = CMS_get0_content(cms);
  20. if (pos == NULL)
  21. return 0;
  22. if (*pos == NULL)
  23. *pos = ASN1_OCTET_STRING_new();
  24. if (*pos != NULL) {
  25. (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
  26. (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
  27. *boundary = &(*pos)->data;
  28. return 1;
  29. }
  30. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  31. return 0;
  32. }
  33. CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
  34. {
  35. CMS_ContentInfo *ci;
  36. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
  37. ci = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms,
  38. ossl_cms_ctx_get0_libctx(ctx),
  39. ossl_cms_ctx_get0_propq(ctx));
  40. if (ci != NULL) {
  41. ERR_set_mark();
  42. ossl_cms_resolve_libctx(ci);
  43. ERR_pop_to_mark();
  44. }
  45. return ci;
  46. }
  47. int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
  48. {
  49. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
  50. }
  51. IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
  52. BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
  53. {
  54. return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
  55. ASN1_ITEM_rptr(CMS_ContentInfo));
  56. }
  57. /* CMS wrappers round generalised stream and MIME routines */
  58. int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
  59. {
  60. return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
  61. ASN1_ITEM_rptr(CMS_ContentInfo));
  62. }
  63. int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
  64. int flags)
  65. {
  66. return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
  67. "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
  68. }
  69. int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
  70. {
  71. STACK_OF(X509_ALGOR) *mdalgs;
  72. int ctype_nid = OBJ_obj2nid(cms->contentType);
  73. int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
  74. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  75. if (ctype_nid == NID_pkcs7_signed)
  76. mdalgs = cms->d.signedData->digestAlgorithms;
  77. else
  78. mdalgs = NULL;
  79. return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
  80. econt_nid, mdalgs,
  81. ASN1_ITEM_rptr(CMS_ContentInfo),
  82. ossl_cms_ctx_get0_libctx(ctx),
  83. ossl_cms_ctx_get0_propq(ctx));
  84. }
  85. CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont,
  86. CMS_ContentInfo **cms)
  87. {
  88. CMS_ContentInfo *ci;
  89. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
  90. ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, flags, bcont,
  91. ASN1_ITEM_rptr(CMS_ContentInfo),
  92. (ASN1_VALUE **)cms,
  93. ossl_cms_ctx_get0_libctx(ctx),
  94. ossl_cms_ctx_get0_propq(ctx));
  95. if (ci != NULL) {
  96. ERR_set_mark();
  97. ossl_cms_resolve_libctx(ci);
  98. ERR_pop_to_mark();
  99. }
  100. return ci;
  101. }
  102. CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
  103. {
  104. return SMIME_read_CMS_ex(bio, 0, bcont, NULL);
  105. }