cms_att.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2008-2019 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 <openssl/asn1t.h>
  10. #include <openssl/pem.h>
  11. #include <openssl/x509v3.h>
  12. #include <openssl/err.h>
  13. #include <openssl/cms.h>
  14. #include "cms_local.h"
  15. #include "internal/nelem.h"
  16. /*-
  17. * Attribute flags.
  18. * CMS attribute restrictions are discussed in
  19. * - RFC 5652 Section 11.
  20. * ESS attribute restrictions are discussed in
  21. * - RFC 2634 Section 1.3.4 AND
  22. * - RFC 5035 Section 5.4
  23. */
  24. /* This is a signed attribute */
  25. #define CMS_ATTR_F_SIGNED 0x01
  26. /* This is an unsigned attribute */
  27. #define CMS_ATTR_F_UNSIGNED 0x02
  28. /* Must be present if there are any other attributes of the same type */
  29. #define CMS_ATTR_F_REQUIRED_COND 0x10
  30. /* There can only be one instance of this attribute */
  31. #define CMS_ATTR_F_ONLY_ONE 0x20
  32. /* The Attribute's value must have exactly one entry */
  33. #define CMS_ATTR_F_ONE_ATTR_VALUE 0x40
  34. /* Attributes rules for different attributes */
  35. static const struct {
  36. int nid; /* The attribute id */
  37. int flags;
  38. } cms_attribute_properties[] = {
  39. /* See RFC Section 11 */
  40. { NID_pkcs9_contentType, CMS_ATTR_F_SIGNED
  41. | CMS_ATTR_F_ONLY_ONE
  42. | CMS_ATTR_F_ONE_ATTR_VALUE
  43. | CMS_ATTR_F_REQUIRED_COND },
  44. { NID_pkcs9_messageDigest, CMS_ATTR_F_SIGNED
  45. | CMS_ATTR_F_ONLY_ONE
  46. | CMS_ATTR_F_ONE_ATTR_VALUE
  47. | CMS_ATTR_F_REQUIRED_COND },
  48. { NID_pkcs9_signingTime, CMS_ATTR_F_SIGNED
  49. | CMS_ATTR_F_ONLY_ONE
  50. | CMS_ATTR_F_ONE_ATTR_VALUE },
  51. { NID_pkcs9_countersignature, CMS_ATTR_F_UNSIGNED },
  52. /* ESS */
  53. { NID_id_smime_aa_signingCertificate, CMS_ATTR_F_SIGNED
  54. | CMS_ATTR_F_ONLY_ONE
  55. | CMS_ATTR_F_ONE_ATTR_VALUE },
  56. { NID_id_smime_aa_signingCertificateV2, CMS_ATTR_F_SIGNED
  57. | CMS_ATTR_F_ONLY_ONE
  58. | CMS_ATTR_F_ONE_ATTR_VALUE },
  59. { NID_id_smime_aa_receiptRequest, CMS_ATTR_F_SIGNED
  60. | CMS_ATTR_F_ONLY_ONE
  61. | CMS_ATTR_F_ONE_ATTR_VALUE }
  62. };
  63. /* CMS SignedData Attribute utilities */
  64. int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
  65. {
  66. return X509at_get_attr_count(si->signedAttrs);
  67. }
  68. int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
  69. {
  70. return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
  71. }
  72. int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
  73. int lastpos)
  74. {
  75. return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
  76. }
  77. X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
  78. {
  79. return X509at_get_attr(si->signedAttrs, loc);
  80. }
  81. X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
  82. {
  83. return X509at_delete_attr(si->signedAttrs, loc);
  84. }
  85. int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
  86. {
  87. if (X509at_add1_attr(&si->signedAttrs, attr))
  88. return 1;
  89. return 0;
  90. }
  91. int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
  92. const ASN1_OBJECT *obj, int type,
  93. const void *bytes, int len)
  94. {
  95. if (X509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
  96. return 1;
  97. return 0;
  98. }
  99. int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
  100. int nid, int type, const void *bytes, int len)
  101. {
  102. if (X509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
  103. return 1;
  104. return 0;
  105. }
  106. int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
  107. const char *attrname, int type,
  108. const void *bytes, int len)
  109. {
  110. if (X509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes, len))
  111. return 1;
  112. return 0;
  113. }
  114. void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid,
  115. int lastpos, int type)
  116. {
  117. return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
  118. }
  119. int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
  120. {
  121. return X509at_get_attr_count(si->unsignedAttrs);
  122. }
  123. int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
  124. int lastpos)
  125. {
  126. return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
  127. }
  128. int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
  129. const ASN1_OBJECT *obj, int lastpos)
  130. {
  131. return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
  132. }
  133. X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
  134. {
  135. return X509at_get_attr(si->unsignedAttrs, loc);
  136. }
  137. X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
  138. {
  139. return X509at_delete_attr(si->unsignedAttrs, loc);
  140. }
  141. int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
  142. {
  143. if (X509at_add1_attr(&si->unsignedAttrs, attr))
  144. return 1;
  145. return 0;
  146. }
  147. int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
  148. const ASN1_OBJECT *obj, int type,
  149. const void *bytes, int len)
  150. {
  151. if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
  152. return 1;
  153. return 0;
  154. }
  155. int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
  156. int nid, int type,
  157. const void *bytes, int len)
  158. {
  159. if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
  160. return 1;
  161. return 0;
  162. }
  163. int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
  164. const char *attrname, int type,
  165. const void *bytes, int len)
  166. {
  167. if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
  168. type, bytes, len))
  169. return 1;
  170. return 0;
  171. }
  172. void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
  173. int lastpos, int type)
  174. {
  175. return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
  176. }
  177. /*
  178. * Retrieve an attribute by nid from a stack of attributes starting at index
  179. * *lastpos + 1.
  180. * Returns the attribute or NULL if there is no attribute.
  181. * If an attribute was found *lastpos returns the index of the found attribute.
  182. */
  183. static X509_ATTRIBUTE *cms_attrib_get(int nid,
  184. const STACK_OF(X509_ATTRIBUTE) *attrs,
  185. int *lastpos)
  186. {
  187. X509_ATTRIBUTE *at;
  188. int loc;
  189. loc = X509at_get_attr_by_NID(attrs, nid, *lastpos);
  190. if (loc < 0)
  191. return NULL;
  192. at = X509at_get_attr(attrs, loc);
  193. *lastpos = loc;
  194. return at;
  195. }
  196. static int cms_check_attribute(int nid, int flags, int type,
  197. const STACK_OF(X509_ATTRIBUTE) *attrs,
  198. int have_attrs)
  199. {
  200. int lastpos = -1;
  201. X509_ATTRIBUTE *at = cms_attrib_get(nid, attrs, &lastpos);
  202. if (at != NULL) {
  203. int count = X509_ATTRIBUTE_count(at);
  204. /* Is this attribute allowed? */
  205. if (((flags & type) == 0)
  206. /* check if multiple attributes of the same type are allowed */
  207. || (((flags & CMS_ATTR_F_ONLY_ONE) != 0)
  208. && cms_attrib_get(nid, attrs, &lastpos) != NULL)
  209. /* Check if attribute should have exactly one value in its set */
  210. || (((flags & CMS_ATTR_F_ONE_ATTR_VALUE) != 0)
  211. && count != 1)
  212. /* There should be at least one value */
  213. || count == 0)
  214. return 0;
  215. } else {
  216. /* fail if a required attribute is missing */
  217. if (have_attrs
  218. && ((flags & CMS_ATTR_F_REQUIRED_COND) != 0)
  219. && (flags & type) != 0)
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. /*
  225. * Check that the signerinfo attributes obey the attribute rules which includes
  226. * the following checks
  227. * - If any signed attributes exist then there must be a Content Type
  228. * and Message Digest attribute in the signed attributes.
  229. * - The countersignature attribute is an optional unsigned attribute only.
  230. * - Content Type, Message Digest, and Signing time attributes are signed
  231. * attributes. Only one instance of each is allowed, with each of these
  232. * attributes containing a single attribute value in its set.
  233. */
  234. int CMS_si_check_attributes(const CMS_SignerInfo *si)
  235. {
  236. int i;
  237. int have_signed_attrs = (CMS_signed_get_attr_count(si) > 0);
  238. int have_unsigned_attrs = (CMS_unsigned_get_attr_count(si) > 0);
  239. for (i = 0; i < (int)OSSL_NELEM(cms_attribute_properties); ++i) {
  240. int nid = cms_attribute_properties[i].nid;
  241. int flags = cms_attribute_properties[i].flags;
  242. if (!cms_check_attribute(nid, flags, CMS_ATTR_F_SIGNED,
  243. si->signedAttrs, have_signed_attrs)
  244. || !cms_check_attribute(nid, flags, CMS_ATTR_F_UNSIGNED,
  245. si->unsignedAttrs, have_unsigned_attrs)) {
  246. CMSerr(CMS_F_CMS_SI_CHECK_ATTRIBUTES, CMS_R_ATTRIBUTE_ERROR);
  247. return 0;
  248. }
  249. }
  250. return 1;
  251. }