1 /* $OpenBSD: cms_att.c,v 1.13 2024/08/27 01:19:27 tb Exp $ */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project.
5 */
6 /* ====================================================================
7 * Copyright (c) 2008 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
55 #include <openssl/asn1.h>
56 #include <openssl/cms.h>
57 #include <openssl/x509.h>
58
59 #include "cms_local.h"
60 #include "x509_local.h"
61
62 /* CMS SignedData Attribute utilities */
63
64 int
CMS_signed_get_attr_count(const CMS_SignerInfo * si)65 CMS_signed_get_attr_count(const CMS_SignerInfo *si)
66 {
67 return sk_X509_ATTRIBUTE_num(si->signedAttrs);
68 }
69 LCRYPTO_ALIAS(CMS_signed_get_attr_count);
70
71 int
CMS_signed_get_attr_by_NID(const CMS_SignerInfo * si,int nid,int lastpos)72 CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
73 {
74 return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
75 }
76 LCRYPTO_ALIAS(CMS_signed_get_attr_by_NID);
77
78 int
CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo * si,const ASN1_OBJECT * obj,int lastpos)79 CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
80 int lastpos)
81 {
82 return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
83 }
84 LCRYPTO_ALIAS(CMS_signed_get_attr_by_OBJ);
85
86 X509_ATTRIBUTE *
CMS_signed_get_attr(const CMS_SignerInfo * si,int loc)87 CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
88 {
89 return sk_X509_ATTRIBUTE_value(si->signedAttrs, loc);
90 }
91 LCRYPTO_ALIAS(CMS_signed_get_attr);
92
93 X509_ATTRIBUTE *
CMS_signed_delete_attr(CMS_SignerInfo * si,int loc)94 CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
95 {
96 return sk_X509_ATTRIBUTE_delete(si->signedAttrs, loc);
97 }
98 LCRYPTO_ALIAS(CMS_signed_delete_attr);
99
100 int
CMS_signed_add1_attr(CMS_SignerInfo * si,X509_ATTRIBUTE * attr)101 CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
102 {
103 if (X509at_add1_attr(&si->signedAttrs, attr))
104 return 1;
105 return 0;
106 }
107 LCRYPTO_ALIAS(CMS_signed_add1_attr);
108
109 int
CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * obj,int type,const void * bytes,int len)110 CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *obj, int type,
111 const void *bytes, int len)
112 {
113 if (X509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
114 return 1;
115 return 0;
116 }
117 LCRYPTO_ALIAS(CMS_signed_add1_attr_by_OBJ);
118
119 int
CMS_signed_add1_attr_by_NID(CMS_SignerInfo * si,int nid,int type,const void * bytes,int len)120 CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si, int nid, int type,
121 const void *bytes, int len)
122 {
123 if (X509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
124 return 1;
125 return 0;
126 }
127 LCRYPTO_ALIAS(CMS_signed_add1_attr_by_NID);
128
129 int
CMS_signed_add1_attr_by_txt(CMS_SignerInfo * si,const char * attrname,int type,const void * bytes,int len)130 CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si, const char *attrname, int type,
131 const void *bytes, int len)
132 {
133 if (X509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes, len))
134 return 1;
135 return 0;
136 }
137 LCRYPTO_ALIAS(CMS_signed_add1_attr_by_txt);
138
139 void *
CMS_signed_get0_data_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * oid,int lastpos,int type)140 CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid,
141 int lastpos, int type)
142 {
143 return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
144 }
145 LCRYPTO_ALIAS(CMS_signed_get0_data_by_OBJ);
146
147 int
CMS_unsigned_get_attr_count(const CMS_SignerInfo * si)148 CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
149 {
150 return sk_X509_ATTRIBUTE_num(si->unsignedAttrs);
151 }
152 LCRYPTO_ALIAS(CMS_unsigned_get_attr_count);
153
154 int
CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo * si,int nid,int lastpos)155 CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
156 {
157 return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
158 }
159 LCRYPTO_ALIAS(CMS_unsigned_get_attr_by_NID);
160
161 int
CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo * si,const ASN1_OBJECT * obj,int lastpos)162 CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
163 int lastpos)
164 {
165 return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
166 }
167 LCRYPTO_ALIAS(CMS_unsigned_get_attr_by_OBJ);
168
169 X509_ATTRIBUTE *
CMS_unsigned_get_attr(const CMS_SignerInfo * si,int loc)170 CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
171 {
172 return sk_X509_ATTRIBUTE_value(si->unsignedAttrs, loc);
173 }
174 LCRYPTO_ALIAS(CMS_unsigned_get_attr);
175
176 X509_ATTRIBUTE *
CMS_unsigned_delete_attr(CMS_SignerInfo * si,int loc)177 CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
178 {
179 return sk_X509_ATTRIBUTE_delete(si->unsignedAttrs, loc);
180 }
181 LCRYPTO_ALIAS(CMS_unsigned_delete_attr);
182
183 int
CMS_unsigned_add1_attr(CMS_SignerInfo * si,X509_ATTRIBUTE * attr)184 CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
185 {
186 if (X509at_add1_attr(&si->unsignedAttrs, attr))
187 return 1;
188 return 0;
189 }
190 LCRYPTO_ALIAS(CMS_unsigned_add1_attr);
191
192 int
CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * obj,int type,const void * bytes,int len)193 CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *obj,
194 int type, const void *bytes, int len)
195 {
196 if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
197 return 1;
198 return 0;
199 }
200 LCRYPTO_ALIAS(CMS_unsigned_add1_attr_by_OBJ);
201
202 int
CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo * si,int nid,int type,const void * bytes,int len)203 CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si, int nid, int type,
204 const void *bytes, int len)
205 {
206 if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
207 return 1;
208 return 0;
209 }
210 LCRYPTO_ALIAS(CMS_unsigned_add1_attr_by_NID);
211
212 int
CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo * si,const char * attrname,int type,const void * bytes,int len)213 CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si, const char *attrname,
214 int type, const void *bytes, int len)
215 {
216 if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname, type,
217 bytes, len))
218 return 1;
219 return 0;
220 }
221 LCRYPTO_ALIAS(CMS_unsigned_add1_attr_by_txt);
222
223 void *
CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo * si,ASN1_OBJECT * oid,int lastpos,int type)224 CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid, int lastpos,
225 int type)
226 {
227 return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
228 }
229 LCRYPTO_ALIAS(CMS_unsigned_get0_data_by_OBJ);
230
231 /* Specific attribute cases */
232