pk7_asn1.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* pk7_asn.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include "internal/cryptlib.h"
  61. #include <openssl/asn1t.h>
  62. #include <openssl/pkcs7.h>
  63. #include <openssl/x509.h>
  64. /* PKCS#7 ASN1 module */
  65. /* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
  66. ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);
  67. ASN1_ADB(PKCS7) = {
  68. ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(PKCS7, d.data, ASN1_OCTET_STRING_NDEF, 0)),
  69. ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(PKCS7, d.sign, PKCS7_SIGNED, 0)),
  70. ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.enveloped, PKCS7_ENVELOPE, 0)),
  71. ADB_ENTRY(NID_pkcs7_signedAndEnveloped, ASN1_NDEF_EXP_OPT(PKCS7, d.signed_and_enveloped, PKCS7_SIGN_ENVELOPE, 0)),
  72. ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP_OPT(PKCS7, d.digest, PKCS7_DIGEST, 0)),
  73. ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP_OPT(PKCS7, d.encrypted, PKCS7_ENCRYPT, 0))
  74. } ASN1_ADB_END(PKCS7, 0, type, 0, &p7default_tt, NULL);
  75. /* PKCS#7 streaming support */
  76. static int pk7_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  77. void *exarg)
  78. {
  79. ASN1_STREAM_ARG *sarg = exarg;
  80. PKCS7 **pp7 = (PKCS7 **)pval;
  81. switch (operation) {
  82. case ASN1_OP_STREAM_PRE:
  83. if (PKCS7_stream(&sarg->boundary, *pp7) <= 0)
  84. return 0;
  85. case ASN1_OP_DETACHED_PRE:
  86. sarg->ndef_bio = PKCS7_dataInit(*pp7, sarg->out);
  87. if (!sarg->ndef_bio)
  88. return 0;
  89. break;
  90. case ASN1_OP_STREAM_POST:
  91. case ASN1_OP_DETACHED_POST:
  92. if (PKCS7_dataFinal(*pp7, sarg->ndef_bio) <= 0)
  93. return 0;
  94. break;
  95. }
  96. return 1;
  97. }
  98. ASN1_NDEF_SEQUENCE_cb(PKCS7, pk7_cb) = {
  99. ASN1_SIMPLE(PKCS7, type, ASN1_OBJECT),
  100. ASN1_ADB_OBJECT(PKCS7)
  101. }ASN1_NDEF_SEQUENCE_END_cb(PKCS7, PKCS7)
  102. IMPLEMENT_ASN1_FUNCTIONS(PKCS7)
  103. IMPLEMENT_ASN1_NDEF_FUNCTION(PKCS7)
  104. IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)
  105. ASN1_NDEF_SEQUENCE(PKCS7_SIGNED) = {
  106. ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
  107. ASN1_SET_OF(PKCS7_SIGNED, md_algs, X509_ALGOR),
  108. ASN1_SIMPLE(PKCS7_SIGNED, contents, PKCS7),
  109. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNED, cert, X509, 0),
  110. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNED, crl, X509_CRL, 1),
  111. ASN1_SET_OF(PKCS7_SIGNED, signer_info, PKCS7_SIGNER_INFO)
  112. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGNED)
  113. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNED)
  114. /* Minor tweak to operation: free up EVP_PKEY */
  115. static int si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  116. void *exarg)
  117. {
  118. if (operation == ASN1_OP_FREE_POST) {
  119. PKCS7_SIGNER_INFO *si = (PKCS7_SIGNER_INFO *)*pval;
  120. EVP_PKEY_free(si->pkey);
  121. }
  122. return 1;
  123. }
  124. ASN1_SEQUENCE_cb(PKCS7_SIGNER_INFO, si_cb) = {
  125. ASN1_SIMPLE(PKCS7_SIGNER_INFO, version, ASN1_INTEGER),
  126. ASN1_SIMPLE(PKCS7_SIGNER_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  127. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_alg, X509_ALGOR),
  128. /* NB this should be a SET OF but we use a SEQUENCE OF so the
  129. * original order * is retained when the structure is reencoded.
  130. * Since the attributes are implicitly tagged this will not affect
  131. * the encoding.
  132. */
  133. ASN1_IMP_SEQUENCE_OF_OPT(PKCS7_SIGNER_INFO, auth_attr, X509_ATTRIBUTE, 0),
  134. ASN1_SIMPLE(PKCS7_SIGNER_INFO, digest_enc_alg, X509_ALGOR),
  135. ASN1_SIMPLE(PKCS7_SIGNER_INFO, enc_digest, ASN1_OCTET_STRING),
  136. ASN1_IMP_SET_OF_OPT(PKCS7_SIGNER_INFO, unauth_attr, X509_ATTRIBUTE, 1)
  137. } ASN1_SEQUENCE_END_cb(PKCS7_SIGNER_INFO, PKCS7_SIGNER_INFO)
  138. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
  139. ASN1_SEQUENCE(PKCS7_ISSUER_AND_SERIAL) = {
  140. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, issuer, X509_NAME),
  141. ASN1_SIMPLE(PKCS7_ISSUER_AND_SERIAL, serial, ASN1_INTEGER)
  142. } ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SERIAL)
  143. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
  144. ASN1_NDEF_SEQUENCE(PKCS7_ENVELOPE) = {
  145. ASN1_SIMPLE(PKCS7_ENVELOPE, version, ASN1_INTEGER),
  146. ASN1_SET_OF(PKCS7_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  147. ASN1_SIMPLE(PKCS7_ENVELOPE, enc_data, PKCS7_ENC_CONTENT)
  148. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENVELOPE)
  149. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
  150. /* Minor tweak to operation: free up X509 */
  151. static int ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  152. void *exarg)
  153. {
  154. if (operation == ASN1_OP_FREE_POST) {
  155. PKCS7_RECIP_INFO *ri = (PKCS7_RECIP_INFO *)*pval;
  156. X509_free(ri->cert);
  157. }
  158. return 1;
  159. }
  160. ASN1_SEQUENCE_cb(PKCS7_RECIP_INFO, ri_cb) = {
  161. ASN1_SIMPLE(PKCS7_RECIP_INFO, version, ASN1_INTEGER),
  162. ASN1_SIMPLE(PKCS7_RECIP_INFO, issuer_and_serial, PKCS7_ISSUER_AND_SERIAL),
  163. ASN1_SIMPLE(PKCS7_RECIP_INFO, key_enc_algor, X509_ALGOR),
  164. ASN1_SIMPLE(PKCS7_RECIP_INFO, enc_key, ASN1_OCTET_STRING)
  165. } ASN1_SEQUENCE_END_cb(PKCS7_RECIP_INFO, PKCS7_RECIP_INFO)
  166. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
  167. ASN1_NDEF_SEQUENCE(PKCS7_ENC_CONTENT) = {
  168. ASN1_SIMPLE(PKCS7_ENC_CONTENT, content_type, ASN1_OBJECT),
  169. ASN1_SIMPLE(PKCS7_ENC_CONTENT, algorithm, X509_ALGOR),
  170. ASN1_IMP_OPT(PKCS7_ENC_CONTENT, enc_data, ASN1_OCTET_STRING_NDEF, 0)
  171. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENC_CONTENT)
  172. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
  173. ASN1_NDEF_SEQUENCE(PKCS7_SIGN_ENVELOPE) = {
  174. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, version, ASN1_INTEGER),
  175. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, recipientinfo, PKCS7_RECIP_INFO),
  176. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, md_algs, X509_ALGOR),
  177. ASN1_SIMPLE(PKCS7_SIGN_ENVELOPE, enc_data, PKCS7_ENC_CONTENT),
  178. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, cert, X509, 0),
  179. ASN1_IMP_SET_OF_OPT(PKCS7_SIGN_ENVELOPE, crl, X509_CRL, 1),
  180. ASN1_SET_OF(PKCS7_SIGN_ENVELOPE, signer_info, PKCS7_SIGNER_INFO)
  181. } ASN1_NDEF_SEQUENCE_END(PKCS7_SIGN_ENVELOPE)
  182. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
  183. ASN1_NDEF_SEQUENCE(PKCS7_ENCRYPT) = {
  184. ASN1_SIMPLE(PKCS7_ENCRYPT, version, ASN1_INTEGER),
  185. ASN1_SIMPLE(PKCS7_ENCRYPT, enc_data, PKCS7_ENC_CONTENT)
  186. } ASN1_NDEF_SEQUENCE_END(PKCS7_ENCRYPT)
  187. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
  188. ASN1_NDEF_SEQUENCE(PKCS7_DIGEST) = {
  189. ASN1_SIMPLE(PKCS7_DIGEST, version, ASN1_INTEGER),
  190. ASN1_SIMPLE(PKCS7_DIGEST, md, X509_ALGOR),
  191. ASN1_SIMPLE(PKCS7_DIGEST, contents, PKCS7),
  192. ASN1_SIMPLE(PKCS7_DIGEST, digest, ASN1_OCTET_STRING)
  193. } ASN1_NDEF_SEQUENCE_END(PKCS7_DIGEST)
  194. IMPLEMENT_ASN1_FUNCTIONS(PKCS7_DIGEST)
  195. /* Specials for authenticated attributes */
  196. /*
  197. * When signing attributes we want to reorder them to match the sorted
  198. * encoding.
  199. */
  200. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_SIGN) =
  201. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  202. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_SIGN)
  203. /*
  204. * When verifying attributes we need to use the received order. So we use
  205. * SEQUENCE OF and tag it to SET OF
  206. */
  207. ASN1_ITEM_TEMPLATE(PKCS7_ATTR_VERIFY) =
  208. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
  209. V_ASN1_SET, PKCS7_ATTRIBUTES, X509_ATTRIBUTE)
  210. ASN1_ITEM_TEMPLATE_END(PKCS7_ATTR_VERIFY)
  211. IMPLEMENT_ASN1_PRINT_FUNCTION(PKCS7)