cms_io.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2008-2016 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_lcl.h"
  15. int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
  16. {
  17. ASN1_OCTET_STRING **pos;
  18. pos = CMS_get0_content(cms);
  19. if (pos == NULL)
  20. return 0;
  21. if (*pos == NULL)
  22. *pos = ASN1_OCTET_STRING_new();
  23. if (*pos != NULL) {
  24. (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
  25. (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
  26. *boundary = &(*pos)->data;
  27. return 1;
  28. }
  29. CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE);
  30. return 0;
  31. }
  32. CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
  33. {
  34. return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
  35. }
  36. int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
  37. {
  38. return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
  39. }
  40. IMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
  41. BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
  42. {
  43. return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
  44. ASN1_ITEM_rptr(CMS_ContentInfo));
  45. }
  46. /* CMS wrappers round generalised stream and MIME routines */
  47. int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
  48. {
  49. return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
  50. ASN1_ITEM_rptr(CMS_ContentInfo));
  51. }
  52. int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
  53. int flags)
  54. {
  55. return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
  56. "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
  57. }
  58. int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
  59. {
  60. STACK_OF(X509_ALGOR) *mdalgs;
  61. int ctype_nid = OBJ_obj2nid(cms->contentType);
  62. int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
  63. if (ctype_nid == NID_pkcs7_signed)
  64. mdalgs = cms->d.signedData->digestAlgorithms;
  65. else
  66. mdalgs = NULL;
  67. return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags,
  68. ctype_nid, econt_nid, mdalgs,
  69. ASN1_ITEM_rptr(CMS_ContentInfo));
  70. }
  71. CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
  72. {
  73. return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont,
  74. ASN1_ITEM_rptr
  75. (CMS_ContentInfo));
  76. }