pk7_asn1.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. /* PKCS#7 ASN1 module */
  15. /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
  16. ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
  17. ASN1_ADB(PKCS7) = {
  18. ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
  19. ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
  20. ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
  21. ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
  22. ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
  23. ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
  24. } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
  25. /* PKCS#7 streaming support */
  26. static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  27. void *exarg)
  28. {
  29. ASN1_STREAM_ARG *sarg = exarg;
  30. PKCS7 **pp7 = (PKCS7 **)pval;
  31. switch (operation) {
  32. case ASN1_OP_STREAM_PRE:
  33. if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
  34. return 0;
  35. /* fall thru */
  36. case ASN1_OP_DETACHED_PRE:
  37. sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
  38. if (!sarg->ndef_bio)
  39. return 0;
  40. break;
  41. case ASN1_OP_STREAM_POST:
  42. case ASN1_OP_DETACHED_POST:
  43. if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
  44. return 0;
  45. break;
  46. }
  47. return 1;
  48. }
  49. ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = {
  50. ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
  51. ASN1_ADB_OBJECT(PKCS7)
  52. }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7)
  53. IMPLEMENT_ASN1_FUNCTIONS(PKCS7)
  54. IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
  55. IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
  56. ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
  57. ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
  58. ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
  59. ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
  60. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
  61. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
  62. ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
  63. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
  64. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
  65. /* Minor tweak to operation: free up EVP_PKEY */
  66. static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  67. void *exarg)
  68. {
  69. if (operation == ASN1_OP_FREE_POST) {
  70. PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
  71. EVP_PKEY_free(si->pkey);
  72. }
  73. return 1;
  74. }
  75. ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
  76. ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
  77. ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  78. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
  79. /* NB this should be a SET OF but we use a SEQUENCE OF so the
  80. * original order * is retained when the structure is reencoded.
  81. * Since the attributes are implicitly tagged this will not affect
  82. * the encoding.
  83. */
  84. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
  85. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
  86. ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
  87. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
  88. } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
  89. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
  90. ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
  91. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
  92. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
  93. } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
  94. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
  95. ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
  96. ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
  97. ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  98. ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
  99. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
  100. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
  101. /* Minor tweak to operation: free up X509 */
  102. static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  103. void *exarg)
  104. {
  105. if (operation == ASN1_OP_FREE_POST) {
  106. PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
  107. X509_free(ri->cert);
  108. }
  109. return 1;
  110. }
  111. ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
  112. ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
  113. ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  114. ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
  115. ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
  116. } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
  117. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
  118. ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
  119. ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
  120. ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
  121. ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
  122. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
  123. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
  124. ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
  125. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
  126. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  127. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
  128. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
  129. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
  130. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
  131. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
  132. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
  133. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
  134. ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
  135. ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
  136. ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
  137. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
  138. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
  139. ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
  140. ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
  141. ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
  142. ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
  143. ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
  144. } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
  145. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
  146. /* Specials for authenticated attributes */
  147. /*
  148. * When signing attributes we want to reorder them to match the sorted
  149. * encoding.
  150. */
  151. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
  152. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  153. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
  154. /*
  155. * When verifying attributes we need to use the received order. So we use
  156. * SEQUENCE OF and tag it to SET OF
  157. */
  158. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
  159. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
  160. V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  161. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
  162. IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)