2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/safestack.h>
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include "x509_local.h"
20 DEFINE_STACK_OF(X509_ATTRIBUTE
)
21 DEFINE_STACK_OF(ASN1_TYPE
)
23 int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE
) *x
)
25 return sk_X509_ATTRIBUTE_num(x
);
28 int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE
) *x
, int nid
,
31 const ASN1_OBJECT
*obj
= OBJ_nid2obj(nid
);
35 return X509at_get_attr_by_OBJ(x
, obj
, lastpos
);
38 int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE
) *sk
,
39 const ASN1_OBJECT
*obj
, int lastpos
)
49 n
= sk_X509_ATTRIBUTE_num(sk
);
50 for (; lastpos
< n
; lastpos
++) {
51 ex
= sk_X509_ATTRIBUTE_value(sk
, lastpos
);
52 if (OBJ_cmp(ex
->object
, obj
) == 0)
58 X509_ATTRIBUTE
*X509at_get_attr(const STACK_OF(X509_ATTRIBUTE
) *x
, int loc
)
60 if (x
== NULL
|| sk_X509_ATTRIBUTE_num(x
) <= loc
|| loc
< 0)
63 return sk_X509_ATTRIBUTE_value(x
, loc
);
66 X509_ATTRIBUTE
*X509at_delete_attr(STACK_OF(X509_ATTRIBUTE
) *x
, int loc
)
70 if (x
== NULL
|| sk_X509_ATTRIBUTE_num(x
) <= loc
|| loc
< 0)
72 ret
= sk_X509_ATTRIBUTE_delete(x
, loc
);
76 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE
) **x
,
79 X509_ATTRIBUTE
*new_attr
= NULL
;
80 STACK_OF(X509_ATTRIBUTE
) *sk
= NULL
;
83 X509err(X509_F_X509AT_ADD1_ATTR
, ERR_R_PASSED_NULL_PARAMETER
);
88 if ((sk
= sk_X509_ATTRIBUTE_new_null()) == NULL
)
93 if ((new_attr
= X509_ATTRIBUTE_dup(attr
)) == NULL
)
95 if (!sk_X509_ATTRIBUTE_push(sk
, new_attr
))
101 X509err(X509_F_X509AT_ADD1_ATTR
, ERR_R_MALLOC_FAILURE
);
103 X509_ATTRIBUTE_free(new_attr
);
104 sk_X509_ATTRIBUTE_free(sk
);
108 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE
)
109 **x
, const ASN1_OBJECT
*obj
,
111 const unsigned char *bytes
,
114 X509_ATTRIBUTE
*attr
;
115 STACK_OF(X509_ATTRIBUTE
) *ret
;
116 attr
= X509_ATTRIBUTE_create_by_OBJ(NULL
, obj
, type
, bytes
, len
);
119 ret
= X509at_add1_attr(x
, attr
);
120 X509_ATTRIBUTE_free(attr
);
124 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE
)
125 **x
, int nid
, int type
,
126 const unsigned char *bytes
,
129 X509_ATTRIBUTE
*attr
;
130 STACK_OF(X509_ATTRIBUTE
) *ret
;
131 attr
= X509_ATTRIBUTE_create_by_NID(NULL
, nid
, type
, bytes
, len
);
134 ret
= X509at_add1_attr(x
, attr
);
135 X509_ATTRIBUTE_free(attr
);
139 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE
)
140 **x
, const char *attrname
,
142 const unsigned char *bytes
,
145 X509_ATTRIBUTE
*attr
;
146 STACK_OF(X509_ATTRIBUTE
) *ret
;
147 attr
= X509_ATTRIBUTE_create_by_txt(NULL
, attrname
, type
, bytes
, len
);
150 ret
= X509at_add1_attr(x
, attr
);
151 X509_ATTRIBUTE_free(attr
);
155 void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE
) *x
,
156 const ASN1_OBJECT
*obj
, int lastpos
, int type
)
160 i
= X509at_get_attr_by_OBJ(x
, obj
, lastpos
);
163 if ((lastpos
<= -2) && (X509at_get_attr_by_OBJ(x
, obj
, i
) != -1))
165 at
= X509at_get_attr(x
, i
);
166 if (lastpos
<= -3 && (X509_ATTRIBUTE_count(at
) != 1))
168 return X509_ATTRIBUTE_get0_data(at
, 0, type
, NULL
);
171 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE
**attr
, int nid
,
172 int atrtype
, const void *data
,
178 obj
= OBJ_nid2obj(nid
);
180 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID
, X509_R_UNKNOWN_NID
);
183 ret
= X509_ATTRIBUTE_create_by_OBJ(attr
, obj
, atrtype
, data
, len
);
185 ASN1_OBJECT_free(obj
);
189 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE
**attr
,
190 const ASN1_OBJECT
*obj
,
191 int atrtype
, const void *data
,
196 if ((attr
== NULL
) || (*attr
== NULL
)) {
197 if ((ret
= X509_ATTRIBUTE_new()) == NULL
) {
198 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ
,
199 ERR_R_MALLOC_FAILURE
);
205 if (!X509_ATTRIBUTE_set1_object(ret
, obj
))
207 if (!X509_ATTRIBUTE_set1_data(ret
, atrtype
, data
, len
))
210 if ((attr
!= NULL
) && (*attr
== NULL
))
214 if ((attr
== NULL
) || (ret
!= *attr
))
215 X509_ATTRIBUTE_free(ret
);
219 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE
**attr
,
220 const char *atrname
, int type
,
221 const unsigned char *bytes
,
225 X509_ATTRIBUTE
*nattr
;
227 obj
= OBJ_txt2obj(atrname
, 0);
229 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT
,
230 X509_R_INVALID_FIELD_NAME
);
231 ERR_add_error_data(2, "name=", atrname
);
234 nattr
= X509_ATTRIBUTE_create_by_OBJ(attr
, obj
, type
, bytes
, len
);
235 ASN1_OBJECT_free(obj
);
239 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE
*attr
, const ASN1_OBJECT
*obj
)
241 if ((attr
== NULL
) || (obj
== NULL
))
243 ASN1_OBJECT_free(attr
->object
);
244 attr
->object
= OBJ_dup(obj
);
245 return attr
->object
!= NULL
;
248 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE
*attr
, int attrtype
,
249 const void *data
, int len
)
251 ASN1_TYPE
*ttmp
= NULL
;
252 ASN1_STRING
*stmp
= NULL
;
256 if (attrtype
& MBSTRING_FLAG
) {
257 stmp
= ASN1_STRING_set_by_NID(NULL
, data
, len
, attrtype
,
258 OBJ_obj2nid(attr
->object
));
260 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA
, ERR_R_ASN1_LIB
);
264 } else if (len
!= -1) {
265 if ((stmp
= ASN1_STRING_type_new(attrtype
)) == NULL
)
267 if (!ASN1_STRING_set(stmp
, data
, len
))
272 * This is a bit naughty because the attribute should really have at
273 * least one value but some types use and zero length SET and require
277 ASN1_STRING_free(stmp
);
280 if ((ttmp
= ASN1_TYPE_new()) == NULL
)
282 if ((len
== -1) && !(attrtype
& MBSTRING_FLAG
)) {
283 if (!ASN1_TYPE_set1(ttmp
, attrtype
, data
))
286 ASN1_TYPE_set(ttmp
, atype
, stmp
);
289 if (!sk_ASN1_TYPE_push(attr
->set
, ttmp
))
293 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA
, ERR_R_MALLOC_FAILURE
);
294 ASN1_TYPE_free(ttmp
);
295 ASN1_STRING_free(stmp
);
299 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE
*attr
)
303 return sk_ASN1_TYPE_num(attr
->set
);
306 ASN1_OBJECT
*X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE
*attr
)
313 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE
*attr
, int idx
,
314 int atrtype
, void *data
)
317 ttmp
= X509_ATTRIBUTE_get0_type(attr
, idx
);
320 if (atrtype
== V_ASN1_BOOLEAN
321 || atrtype
== V_ASN1_NULL
322 || atrtype
!= ASN1_TYPE_get(ttmp
)) {
323 X509err(X509_F_X509_ATTRIBUTE_GET0_DATA
, X509_R_WRONG_TYPE
);
326 return ttmp
->value
.ptr
;
329 ASN1_TYPE
*X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE
*attr
, int idx
)
333 return sk_ASN1_TYPE_value(attr
->set
, idx
);