cms_att.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 2008-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 <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(const CMS_SignerInfo *si,
  115. const ASN1_OBJECT *oid,
  116. int lastpos, int type)
  117. {
  118. return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
  119. }
  120. int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
  121. {
  122. return X509at_get_attr_count(si->unsignedAttrs);
  123. }
  124. int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
  125. int lastpos)
  126. {
  127. return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
  128. }
  129. int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
  130. const ASN1_OBJECT *obj, int lastpos)
  131. {
  132. return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
  133. }
  134. X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
  135. {
  136. return X509at_get_attr(si->unsignedAttrs, loc);
  137. }
  138. X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
  139. {
  140. return X509at_delete_attr(si->unsignedAttrs, loc);
  141. }
  142. int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
  143. {
  144. if (X509at_add1_attr(&si->unsignedAttrs, attr))
  145. return 1;
  146. return 0;
  147. }
  148. int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
  149. const ASN1_OBJECT *obj, int type,
  150. const void *bytes, int len)
  151. {
  152. if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
  153. return 1;
  154. return 0;
  155. }
  156. int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
  157. int nid, int type,
  158. const void *bytes, int len)
  159. {
  160. if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
  161. return 1;
  162. return 0;
  163. }
  164. int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
  165. const char *attrname, int type,
  166. const void *bytes, int len)
  167. {
  168. if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
  169. type, bytes, len))
  170. return 1;
  171. return 0;
  172. }
  173. void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
  174. int lastpos, int type)
  175. {
  176. return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
  177. }
  178. /*
  179. * Retrieve an attribute by nid from a stack of attributes starting at index
  180. * *lastpos + 1.
  181. * Returns the attribute or NULL if there is no attribute.
  182. * If an attribute was found *lastpos returns the index of the found attribute.
  183. */
  184. static X509_ATTRIBUTE *cms_attrib_get(int nid,
  185. const STACK_OF(X509_ATTRIBUTE) *attrs,
  186. int *lastpos)
  187. {
  188. X509_ATTRIBUTE *at;
  189. int loc;
  190. loc = X509at_get_attr_by_NID(attrs, nid, *lastpos);
  191. if (loc < 0)
  192. return NULL;
  193. at = X509at_get_attr(attrs, loc);
  194. *lastpos = loc;
  195. return at;
  196. }
  197. static int cms_check_attribute(int nid, int flags, int type,
  198. const STACK_OF(X509_ATTRIBUTE) *attrs,
  199. int have_attrs)
  200. {
  201. int lastpos = -1;
  202. X509_ATTRIBUTE *at = cms_attrib_get(nid, attrs, &lastpos);
  203. if (at != NULL) {
  204. int count = X509_ATTRIBUTE_count(at);
  205. /* Is this attribute allowed? */
  206. if (((flags & type) == 0)
  207. /* check if multiple attributes of the same type are allowed */
  208. || (((flags & CMS_ATTR_F_ONLY_ONE) != 0)
  209. && cms_attrib_get(nid, attrs, &lastpos) != NULL)
  210. /* Check if attribute should have exactly one value in its set */
  211. || (((flags & CMS_ATTR_F_ONE_ATTR_VALUE) != 0)
  212. && count != 1)
  213. /* There should be at least one value */
  214. || count == 0)
  215. return 0;
  216. } else {
  217. /* fail if a required attribute is missing */
  218. if (have_attrs
  219. && ((flags & CMS_ATTR_F_REQUIRED_COND) != 0)
  220. && (flags & type) != 0)
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. /*
  226. * Check that the signerinfo attributes obey the attribute rules which includes
  227. * the following checks
  228. * - If any signed attributes exist then there must be a Content Type
  229. * and Message Digest attribute in the signed attributes.
  230. * - The countersignature attribute is an optional unsigned attribute only.
  231. * - Content Type, Message Digest, and Signing time attributes are signed
  232. * attributes. Only one instance of each is allowed, with each of these
  233. * attributes containing a single attribute value in its set.
  234. */
  235. int ossl_cms_si_check_attributes(const CMS_SignerInfo *si)
  236. {
  237. int i;
  238. int have_signed_attrs = (CMS_signed_get_attr_count(si) > 0);
  239. int have_unsigned_attrs = (CMS_unsigned_get_attr_count(si) > 0);
  240. for (i = 0; i < (int)OSSL_NELEM(cms_attribute_properties); ++i) {
  241. int nid = cms_attribute_properties[i].nid;
  242. int flags = cms_attribute_properties[i].flags;
  243. if (!cms_check_attribute(nid, flags, CMS_ATTR_F_SIGNED,
  244. si->signedAttrs, have_signed_attrs)
  245. || !cms_check_attribute(nid, flags, CMS_ATTR_F_UNSIGNED,
  246. si->unsignedAttrs, have_unsigned_attrs)) {
  247. ERR_raise(ERR_LIB_CMS, CMS_R_ATTRIBUTE_ERROR);
  248. return 0;
  249. }
  250. }
  251. return 1;
  252. }