xref: /dflybsd-src/crypto/libressl/crypto/cms/cms_att.c (revision cca6fc5243d2098262ea81f83ad5b28d3b800f4a)
1*cca6fc52SDaniel Fojt /* $OpenBSD: cms_att.c,v 1.9 2019/08/10 18:15:52 jsing Exp $ */
2*cca6fc52SDaniel Fojt /*
3*cca6fc52SDaniel Fojt  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4*cca6fc52SDaniel Fojt  * project.
5*cca6fc52SDaniel Fojt  */
6*cca6fc52SDaniel Fojt /* ====================================================================
7*cca6fc52SDaniel Fojt  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8*cca6fc52SDaniel Fojt  *
9*cca6fc52SDaniel Fojt  * Redistribution and use in source and binary forms, with or without
10*cca6fc52SDaniel Fojt  * modification, are permitted provided that the following conditions
11*cca6fc52SDaniel Fojt  * are met:
12*cca6fc52SDaniel Fojt  *
13*cca6fc52SDaniel Fojt  * 1. Redistributions of source code must retain the above copyright
14*cca6fc52SDaniel Fojt  *    notice, this list of conditions and the following disclaimer.
15*cca6fc52SDaniel Fojt  *
16*cca6fc52SDaniel Fojt  * 2. Redistributions in binary form must reproduce the above copyright
17*cca6fc52SDaniel Fojt  *    notice, this list of conditions and the following disclaimer in
18*cca6fc52SDaniel Fojt  *    the documentation and/or other materials provided with the
19*cca6fc52SDaniel Fojt  *    distribution.
20*cca6fc52SDaniel Fojt  *
21*cca6fc52SDaniel Fojt  * 3. All advertising materials mentioning features or use of this
22*cca6fc52SDaniel Fojt  *    software must display the following acknowledgment:
23*cca6fc52SDaniel Fojt  *    "This product includes software developed by the OpenSSL Project
24*cca6fc52SDaniel Fojt  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25*cca6fc52SDaniel Fojt  *
26*cca6fc52SDaniel Fojt  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27*cca6fc52SDaniel Fojt  *    endorse or promote products derived from this software without
28*cca6fc52SDaniel Fojt  *    prior written permission. For written permission, please contact
29*cca6fc52SDaniel Fojt  *    licensing@OpenSSL.org.
30*cca6fc52SDaniel Fojt  *
31*cca6fc52SDaniel Fojt  * 5. Products derived from this software may not be called "OpenSSL"
32*cca6fc52SDaniel Fojt  *    nor may "OpenSSL" appear in their names without prior written
33*cca6fc52SDaniel Fojt  *    permission of the OpenSSL Project.
34*cca6fc52SDaniel Fojt  *
35*cca6fc52SDaniel Fojt  * 6. Redistributions of any form whatsoever must retain the following
36*cca6fc52SDaniel Fojt  *    acknowledgment:
37*cca6fc52SDaniel Fojt  *    "This product includes software developed by the OpenSSL Project
38*cca6fc52SDaniel Fojt  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39*cca6fc52SDaniel Fojt  *
40*cca6fc52SDaniel Fojt  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41*cca6fc52SDaniel Fojt  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42*cca6fc52SDaniel Fojt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43*cca6fc52SDaniel Fojt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44*cca6fc52SDaniel Fojt  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45*cca6fc52SDaniel Fojt  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46*cca6fc52SDaniel Fojt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47*cca6fc52SDaniel Fojt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*cca6fc52SDaniel Fojt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49*cca6fc52SDaniel Fojt  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50*cca6fc52SDaniel Fojt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51*cca6fc52SDaniel Fojt  * OF THE POSSIBILITY OF SUCH DAMAGE.
52*cca6fc52SDaniel Fojt  * ====================================================================
53*cca6fc52SDaniel Fojt  */
54*cca6fc52SDaniel Fojt 
55*cca6fc52SDaniel Fojt #include <openssl/asn1t.h>
56*cca6fc52SDaniel Fojt #include <openssl/pem.h>
57*cca6fc52SDaniel Fojt #include <openssl/x509v3.h>
58*cca6fc52SDaniel Fojt #include <openssl/err.h>
59*cca6fc52SDaniel Fojt #include <openssl/cms.h>
60*cca6fc52SDaniel Fojt #include "cms_lcl.h"
61*cca6fc52SDaniel Fojt 
62*cca6fc52SDaniel Fojt /* CMS SignedData Attribute utilities */
63*cca6fc52SDaniel Fojt 
64*cca6fc52SDaniel Fojt int
CMS_signed_get_attr_count(const CMS_SignerInfo * si)65*cca6fc52SDaniel Fojt CMS_signed_get_attr_count(const CMS_SignerInfo *si)
66*cca6fc52SDaniel Fojt {
67*cca6fc52SDaniel Fojt 	return X509at_get_attr_count(si->signedAttrs);
68*cca6fc52SDaniel Fojt }
69*cca6fc52SDaniel Fojt 
70*cca6fc52SDaniel Fojt int
CMS_signed_get_attr_by_NID(const CMS_SignerInfo * si,int nid,int lastpos)71*cca6fc52SDaniel Fojt CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
72*cca6fc52SDaniel Fojt {
73*cca6fc52SDaniel Fojt 	return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
74*cca6fc52SDaniel Fojt }
75*cca6fc52SDaniel Fojt 
76*cca6fc52SDaniel Fojt int
CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo * si,const ASN1_OBJECT * obj,int lastpos)77*cca6fc52SDaniel Fojt CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
78*cca6fc52SDaniel Fojt     int lastpos)
79*cca6fc52SDaniel Fojt {
80*cca6fc52SDaniel Fojt 	return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
81*cca6fc52SDaniel Fojt }
82*cca6fc52SDaniel Fojt 
83*cca6fc52SDaniel Fojt X509_ATTRIBUTE *
CMS_signed_get_attr(const CMS_SignerInfo * si,int loc)84*cca6fc52SDaniel Fojt CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
85*cca6fc52SDaniel Fojt {
86*cca6fc52SDaniel Fojt 	return X509at_get_attr(si->signedAttrs, loc);
87*cca6fc52SDaniel Fojt }
88*cca6fc52SDaniel Fojt 
89*cca6fc52SDaniel Fojt X509_ATTRIBUTE *
CMS_signed_delete_attr(CMS_SignerInfo * si,int loc)90*cca6fc52SDaniel Fojt CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
91*cca6fc52SDaniel Fojt {
92*cca6fc52SDaniel Fojt 	return X509at_delete_attr(si->signedAttrs, loc);
93*cca6fc52SDaniel Fojt }
94*cca6fc52SDaniel Fojt 
95*cca6fc52SDaniel Fojt int
CMS_signed_add1_attr(CMS_SignerInfo * si,X509_ATTRIBUTE * attr)96*cca6fc52SDaniel Fojt CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
97*cca6fc52SDaniel Fojt {
98*cca6fc52SDaniel Fojt 	if (X509at_add1_attr(&si->signedAttrs, attr))
99*cca6fc52SDaniel Fojt 		return 1;
100*cca6fc52SDaniel Fojt 	return 0;
101*cca6fc52SDaniel Fojt }
102*cca6fc52SDaniel Fojt 
103*cca6fc52SDaniel Fojt int
CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * obj,int type,const void * bytes,int len)104*cca6fc52SDaniel Fojt CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *obj, int type,
105*cca6fc52SDaniel Fojt     const void *bytes, int len)
106*cca6fc52SDaniel Fojt {
107*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
108*cca6fc52SDaniel Fojt 		return 1;
109*cca6fc52SDaniel Fojt 	return 0;
110*cca6fc52SDaniel Fojt }
111*cca6fc52SDaniel Fojt 
112*cca6fc52SDaniel Fojt int
CMS_signed_add1_attr_by_NID(CMS_SignerInfo * si,int nid,int type,const void * bytes,int len)113*cca6fc52SDaniel Fojt CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si, int nid, int type,
114*cca6fc52SDaniel Fojt     const void *bytes, int len)
115*cca6fc52SDaniel Fojt {
116*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
117*cca6fc52SDaniel Fojt 		return 1;
118*cca6fc52SDaniel Fojt 	return 0;
119*cca6fc52SDaniel Fojt }
120*cca6fc52SDaniel Fojt 
121*cca6fc52SDaniel Fojt int
CMS_signed_add1_attr_by_txt(CMS_SignerInfo * si,const char * attrname,int type,const void * bytes,int len)122*cca6fc52SDaniel Fojt CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si, const char *attrname, int type,
123*cca6fc52SDaniel Fojt     const void *bytes, int len)
124*cca6fc52SDaniel Fojt {
125*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes, len))
126*cca6fc52SDaniel Fojt 		return 1;
127*cca6fc52SDaniel Fojt 	return 0;
128*cca6fc52SDaniel Fojt }
129*cca6fc52SDaniel Fojt 
130*cca6fc52SDaniel Fojt void *
CMS_signed_get0_data_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * oid,int lastpos,int type)131*cca6fc52SDaniel Fojt CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid,
132*cca6fc52SDaniel Fojt     int lastpos, int type)
133*cca6fc52SDaniel Fojt {
134*cca6fc52SDaniel Fojt 	return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
135*cca6fc52SDaniel Fojt }
136*cca6fc52SDaniel Fojt 
137*cca6fc52SDaniel Fojt int
CMS_unsigned_get_attr_count(const CMS_SignerInfo * si)138*cca6fc52SDaniel Fojt CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
139*cca6fc52SDaniel Fojt {
140*cca6fc52SDaniel Fojt 	return X509at_get_attr_count(si->unsignedAttrs);
141*cca6fc52SDaniel Fojt }
142*cca6fc52SDaniel Fojt 
143*cca6fc52SDaniel Fojt int
CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo * si,int nid,int lastpos)144*cca6fc52SDaniel Fojt CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
145*cca6fc52SDaniel Fojt {
146*cca6fc52SDaniel Fojt 	return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
147*cca6fc52SDaniel Fojt }
148*cca6fc52SDaniel Fojt 
149*cca6fc52SDaniel Fojt int
CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo * si,const ASN1_OBJECT * obj,int lastpos)150*cca6fc52SDaniel Fojt CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
151*cca6fc52SDaniel Fojt     int lastpos)
152*cca6fc52SDaniel Fojt {
153*cca6fc52SDaniel Fojt 	return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
154*cca6fc52SDaniel Fojt }
155*cca6fc52SDaniel Fojt 
156*cca6fc52SDaniel Fojt X509_ATTRIBUTE *
CMS_unsigned_get_attr(const CMS_SignerInfo * si,int loc)157*cca6fc52SDaniel Fojt CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
158*cca6fc52SDaniel Fojt {
159*cca6fc52SDaniel Fojt 	return X509at_get_attr(si->unsignedAttrs, loc);
160*cca6fc52SDaniel Fojt }
161*cca6fc52SDaniel Fojt 
162*cca6fc52SDaniel Fojt X509_ATTRIBUTE *
CMS_unsigned_delete_attr(CMS_SignerInfo * si,int loc)163*cca6fc52SDaniel Fojt CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
164*cca6fc52SDaniel Fojt {
165*cca6fc52SDaniel Fojt 	return X509at_delete_attr(si->unsignedAttrs, loc);
166*cca6fc52SDaniel Fojt }
167*cca6fc52SDaniel Fojt 
168*cca6fc52SDaniel Fojt int
CMS_unsigned_add1_attr(CMS_SignerInfo * si,X509_ATTRIBUTE * attr)169*cca6fc52SDaniel Fojt CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
170*cca6fc52SDaniel Fojt {
171*cca6fc52SDaniel Fojt 	if (X509at_add1_attr(&si->unsignedAttrs, attr))
172*cca6fc52SDaniel Fojt 		return 1;
173*cca6fc52SDaniel Fojt 	return 0;
174*cca6fc52SDaniel Fojt }
175*cca6fc52SDaniel Fojt 
176*cca6fc52SDaniel Fojt int
CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo * si,const ASN1_OBJECT * obj,int type,const void * bytes,int len)177*cca6fc52SDaniel Fojt CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *obj,
178*cca6fc52SDaniel Fojt     int type, const void *bytes, int len)
179*cca6fc52SDaniel Fojt {
180*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
181*cca6fc52SDaniel Fojt 		return 1;
182*cca6fc52SDaniel Fojt 	return 0;
183*cca6fc52SDaniel Fojt }
184*cca6fc52SDaniel Fojt 
185*cca6fc52SDaniel Fojt int
CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo * si,int nid,int type,const void * bytes,int len)186*cca6fc52SDaniel Fojt CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si, int nid, int type,
187*cca6fc52SDaniel Fojt     const void *bytes, int len)
188*cca6fc52SDaniel Fojt {
189*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
190*cca6fc52SDaniel Fojt 		return 1;
191*cca6fc52SDaniel Fojt 	return 0;
192*cca6fc52SDaniel Fojt }
193*cca6fc52SDaniel Fojt 
194*cca6fc52SDaniel Fojt int
CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo * si,const char * attrname,int type,const void * bytes,int len)195*cca6fc52SDaniel Fojt CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si, const char *attrname,
196*cca6fc52SDaniel Fojt     int type, const void *bytes, int len)
197*cca6fc52SDaniel Fojt {
198*cca6fc52SDaniel Fojt 	if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname, type,
199*cca6fc52SDaniel Fojt 	    bytes, len))
200*cca6fc52SDaniel Fojt 		return 1;
201*cca6fc52SDaniel Fojt 	return 0;
202*cca6fc52SDaniel Fojt }
203*cca6fc52SDaniel Fojt 
204*cca6fc52SDaniel Fojt void *
CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo * si,ASN1_OBJECT * oid,int lastpos,int type)205*cca6fc52SDaniel Fojt CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid, int lastpos,
206*cca6fc52SDaniel Fojt     int type)
207*cca6fc52SDaniel Fojt {
208*cca6fc52SDaniel Fojt 	return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
209*cca6fc52SDaniel Fojt }
210*cca6fc52SDaniel Fojt 
211*cca6fc52SDaniel Fojt /* Specific attribute cases */
212