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(ASN1_TYPE
)
22 int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE
) *x
)
24 return sk_X509_ATTRIBUTE_num(x
);
27 int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE
) *x
, int nid
,
30 const ASN1_OBJECT
*obj
= OBJ_nid2obj(nid
);
34 return X509at_get_attr_by_OBJ(x
, obj
, lastpos
);
37 int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE
) *sk
,
38 const ASN1_OBJECT
*obj
, int lastpos
)
48 n
= sk_X509_ATTRIBUTE_num(sk
);
49 for (; lastpos
< n
; lastpos
++) {
50 ex
= sk_X509_ATTRIBUTE_value(sk
, lastpos
);
51 if (OBJ_cmp(ex
->object
, obj
) == 0)
57 X509_ATTRIBUTE
*X509at_get_attr(const STACK_OF(X509_ATTRIBUTE
) *x
, int loc
)
59 if (x
== NULL
|| sk_X509_ATTRIBUTE_num(x
) <= loc
|| loc
< 0)
62 return sk_X509_ATTRIBUTE_value(x
, loc
);
65 X509_ATTRIBUTE
*X509at_delete_attr(STACK_OF(X509_ATTRIBUTE
) *x
, int loc
)
69 if (x
== NULL
|| sk_X509_ATTRIBUTE_num(x
) <= loc
|| loc
< 0)
71 ret
= sk_X509_ATTRIBUTE_delete(x
, loc
);
75 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE
) **x
,
78 X509_ATTRIBUTE
*new_attr
= NULL
;
79 STACK_OF(X509_ATTRIBUTE
) *sk
= NULL
;
82 X509err(X509_F_X509AT_ADD1_ATTR
, ERR_R_PASSED_NULL_PARAMETER
);
87 if ((sk
= sk_X509_ATTRIBUTE_new_null()) == NULL
)
92 if ((new_attr
= X509_ATTRIBUTE_dup(attr
)) == NULL
)
94 if (!sk_X509_ATTRIBUTE_push(sk
, new_attr
))
100 X509err(X509_F_X509AT_ADD1_ATTR
, ERR_R_MALLOC_FAILURE
);
102 X509_ATTRIBUTE_free(new_attr
);
103 sk_X509_ATTRIBUTE_free(sk
);
107 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE
)
108 **x
, const ASN1_OBJECT
*obj
,
110 const unsigned char *bytes
,
113 X509_ATTRIBUTE
*attr
;
114 STACK_OF(X509_ATTRIBUTE
) *ret
;
115 attr
= X509_ATTRIBUTE_create_by_OBJ(NULL
, obj
, type
, bytes
, len
);
118 ret
= X509at_add1_attr(x
, attr
);
119 X509_ATTRIBUTE_free(attr
);
123 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE
)
124 **x
, int nid
, int type
,
125 const unsigned char *bytes
,
128 X509_ATTRIBUTE
*attr
;
129 STACK_OF(X509_ATTRIBUTE
) *ret
;
130 attr
= X509_ATTRIBUTE_create_by_NID(NULL
, nid
, type
, bytes
, len
);
133 ret
= X509at_add1_attr(x
, attr
);
134 X509_ATTRIBUTE_free(attr
);
138 STACK_OF(X509_ATTRIBUTE
) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE
)
139 **x
, const char *attrname
,
141 const unsigned char *bytes
,
144 X509_ATTRIBUTE
*attr
;
145 STACK_OF(X509_ATTRIBUTE
) *ret
;
146 attr
= X509_ATTRIBUTE_create_by_txt(NULL
, attrname
, type
, bytes
, len
);
149 ret
= X509at_add1_attr(x
, attr
);
150 X509_ATTRIBUTE_free(attr
);
154 void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE
) *x
,
155 const ASN1_OBJECT
*obj
, int lastpos
, int type
)
159 i
= X509at_get_attr_by_OBJ(x
, obj
, lastpos
);
162 if ((lastpos
<= -2) && (X509at_get_attr_by_OBJ(x
, obj
, i
) != -1))
164 at
= X509at_get_attr(x
, i
);
165 if (lastpos
<= -3 && (X509_ATTRIBUTE_count(at
) != 1))
167 return X509_ATTRIBUTE_get0_data(at
, 0, type
, NULL
);
170 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE
**attr
, int nid
,
171 int atrtype
, const void *data
,
177 obj
= OBJ_nid2obj(nid
);
179 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID
, X509_R_UNKNOWN_NID
);
182 ret
= X509_ATTRIBUTE_create_by_OBJ(attr
, obj
, atrtype
, data
, len
);
184 ASN1_OBJECT_free(obj
);
188 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE
**attr
,
189 const ASN1_OBJECT
*obj
,
190 int atrtype
, const void *data
,
195 if ((attr
== NULL
) || (*attr
== NULL
)) {
196 if ((ret
= X509_ATTRIBUTE_new()) == NULL
) {
197 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ
,
198 ERR_R_MALLOC_FAILURE
);
204 if (!X509_ATTRIBUTE_set1_object(ret
, obj
))
206 if (!X509_ATTRIBUTE_set1_data(ret
, atrtype
, data
, len
))
209 if ((attr
!= NULL
) && (*attr
== NULL
))
213 if ((attr
== NULL
) || (ret
!= *attr
))
214 X509_ATTRIBUTE_free(ret
);
218 X509_ATTRIBUTE
*X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE
**attr
,
219 const char *atrname
, int type
,
220 const unsigned char *bytes
,
224 X509_ATTRIBUTE
*nattr
;
226 obj
= OBJ_txt2obj(atrname
, 0);
228 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT
,
229 X509_R_INVALID_FIELD_NAME
);
230 ERR_add_error_data(2, "name=", atrname
);
233 nattr
= X509_ATTRIBUTE_create_by_OBJ(attr
, obj
, type
, bytes
, len
);
234 ASN1_OBJECT_free(obj
);
238 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE
*attr
, const ASN1_OBJECT
*obj
)
240 if ((attr
== NULL
) || (obj
== NULL
))
242 ASN1_OBJECT_free(attr
->object
);
243 attr
->object
= OBJ_dup(obj
);
244 return attr
->object
!= NULL
;
247 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE
*attr
, int attrtype
,
248 const void *data
, int len
)
250 ASN1_TYPE
*ttmp
= NULL
;
251 ASN1_STRING
*stmp
= NULL
;
255 if (attrtype
& MBSTRING_FLAG
) {
256 stmp
= ASN1_STRING_set_by_NID(NULL
, data
, len
, attrtype
,
257 OBJ_obj2nid(attr
->object
));
259 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA
, ERR_R_ASN1_LIB
);
263 } else if (len
!= -1) {
264 if ((stmp
= ASN1_STRING_type_new(attrtype
)) == NULL
)
266 if (!ASN1_STRING_set(stmp
, data
, len
))
271 * This is a bit naughty because the attribute should really have at
272 * least one value but some types use and zero length SET and require
276 ASN1_STRING_free(stmp
);
279 if ((ttmp
= ASN1_TYPE_new()) == NULL
)
281 if ((len
== -1) && !(attrtype
& MBSTRING_FLAG
)) {
282 if (!ASN1_TYPE_set1(ttmp
, attrtype
, data
))
285 ASN1_TYPE_set(ttmp
, atype
, stmp
);
288 if (!sk_ASN1_TYPE_push(attr
->set
, ttmp
))
292 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA
, ERR_R_MALLOC_FAILURE
);
293 ASN1_TYPE_free(ttmp
);
294 ASN1_STRING_free(stmp
);
298 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE
*attr
)
302 return sk_ASN1_TYPE_num(attr
->set
);
305 ASN1_OBJECT
*X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE
*attr
)
312 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE
*attr
, int idx
,
313 int atrtype
, void *data
)
316 ttmp
= X509_ATTRIBUTE_get0_type(attr
, idx
);
319 if (atrtype
== V_ASN1_BOOLEAN
320 || atrtype
== V_ASN1_NULL
321 || atrtype
!= ASN1_TYPE_get(ttmp
)) {
322 X509err(X509_F_X509_ATTRIBUTE_GET0_DATA
, X509_R_WRONG_TYPE
);
325 return ttmp
->value
.ptr
;
328 ASN1_TYPE
*X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE
*attr
, int idx
)
332 return sk_ASN1_TYPE_value(attr
->set
, idx
);