pk7_asn1.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2000-2021 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/pkcs7.h>
  13. #include <openssl/x509.h>
  14. #include "pk7_local.h"
  15. /* PKCS#7 ASN1 module */
  16. /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
  17. ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
  18. ASN1_ADB(PKCS7) = {
  19. ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
  20. ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
  21. ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
  22. ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
  23. ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
  24. ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
  25. } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
  26. /* PKCS#7 streaming support */
  27. static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  28. void *exarg)
  29. {
  30. ASN1_STREAM_ARG *sarg = exarg;
  31. PKCS7 **pp7 = (PKCS7 **)pval;
  32. switch (operation) {
  33. case ASN1_OP_STREAM_PRE:
  34. if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
  35. return 0;
  36. /* fall through */
  37. case ASN1_OP_DETACHED_PRE:
  38. sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
  39. if (!sarg->ndef_bio)
  40. return 0;
  41. break;
  42. case ASN1_OP_STREAM_POST:
  43. case ASN1_OP_DETACHED_POST:
  44. if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
  45. return 0;
  46. break;
  47. }
  48. return 1;
  49. }
  50. ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = {
  51. ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
  52. ASN1_ADB_OBJECT(PKCS7)
  53. }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7)
  54. PKCS7 *d2i_PKCS7(PKCS7 **a, const unsigned char **in, long len)
  55. {
  56. PKCS7 *ret;
  57. OSSL_LIB_CTX *libctx = NULL;
  58. const char *propq = NULL;
  59. if (a != NULL && *a != NULL) {
  60. libctx = (*a)->ctx.libctx;
  61. propq = (*a)->ctx.propq;
  62. }
  63. ret = (PKCS7 *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len, (PKCS7_it()),
  64. libctx, propq);
  65. if (ret != NULL)
  66. ossl_pkcs7_resolve_libctx(ret);
  67. return ret;
  68. }
  69. int i2d_PKCS7(const PKCS7 *a, unsigned char **out)
  70. {
  71. return ASN1_item_i2d((const ASN1_VALUE *)a, out, (PKCS7_it()));\
  72. }
  73. PKCS7 *PKCS7_new(void)
  74. {
  75. return (PKCS7 *)ASN1_item_new(ASN1_ITEM_rptr(PKCS7));
  76. }
  77. PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
  78. {
  79. PKCS7 *pkcs7 = (PKCS7 *)ASN1_item_new_ex(ASN1_ITEM_rptr(PKCS7), libctx,
  80. propq);
  81. if (pkcs7 != NULL) {
  82. pkcs7->ctx.libctx = libctx;
  83. pkcs7->ctx.propq = NULL;
  84. if (propq != NULL) {
  85. pkcs7->ctx.propq = OPENSSL_strdup(propq);
  86. if (pkcs7->ctx.propq == NULL) {
  87. PKCS7_free(pkcs7);
  88. pkcs7 = NULL;
  89. }
  90. }
  91. }
  92. return pkcs7;
  93. }
  94. void PKCS7_free(PKCS7 *p7)
  95. {
  96. if (p7 != NULL) {
  97. OPENSSL_free(p7->ctx.propq);
  98. ASN1_item_free((ASN1_VALUE *)p7, ASN1_ITEM_rptr(PKCS7));
  99. }
  100. }
  101. IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
  102. IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
  103. ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
  104. ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
  105. ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
  106. ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
  107. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
  108. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
  109. ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
  110. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
  111. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
  112. /* Minor tweak to operation: free up EVP_PKEY */
  113. static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  114. void *exarg)
  115. {
  116. if (operation == ASN1_OP_FREE_POST) {
  117. PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
  118. EVP_PKEY_free(si->pkey);
  119. }
  120. return 1;
  121. }
  122. ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
  123. ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
  124. ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  125. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
  126. /* NB this should be a SET OF but we use a SEQUENCE OF so the
  127. * original order * is retained when the structure is reencoded.
  128. * Since the attributes are implicitly tagged this will not affect
  129. * the encoding.
  130. */
  131. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
  132. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
  133. ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
  134. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
  135. } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
  136. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
  137. ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
  138. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
  139. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
  140. } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
  141. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
  142. ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
  143. ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
  144. ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  145. ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
  146. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
  147. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
  148. /* Minor tweak to operation: free up X509 */
  149. static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  150. void *exarg)
  151. {
  152. if (operation == ASN1_OP_FREE_POST) {
  153. PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
  154. X509_free(ri->cert);
  155. }
  156. return 1;
  157. }
  158. ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
  159. ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
  160. ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  161. ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
  162. ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
  163. } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
  164. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
  165. ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
  166. ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
  167. ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
  168. ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
  169. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
  170. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
  171. ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
  172. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
  173. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  174. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
  175. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
  176. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
  177. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
  178. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
  179. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
  180. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
  181. ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
  182. ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
  183. ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
  184. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
  185. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
  186. ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
  187. ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
  188. ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
  189. ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
  190. ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
  191. } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
  192. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
  193. /* Specials for authenticated attributes */
  194. /*
  195. * When signing attributes we want to reorder them to match the sorted
  196. * encoding.
  197. */
  198. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
  199. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  200. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
  201. /*
  202. * When verifying attributes we need to use the received order. So we use
  203. * SEQUENCE OF and tag it to SET OF
  204. */
  205. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
  206. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
  207. V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  208. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
  209. IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)