2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
22 return sk_X509_ATTRIBUTE_num(x);
25 int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
28 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
32 return X509at_get_attr_by_OBJ(x, obj, lastpos);
35 int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
36 const ASN1_OBJECT *obj, int lastpos)
46 n = sk_X509_ATTRIBUTE_num(sk);
47 for (; lastpos < n; lastpos++) {
48 ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
49 if (OBJ_cmp(ex->object, obj) == 0)
55 X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
57 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
60 return sk_X509_ATTRIBUTE_value(x, loc);
63 X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
67 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
69 ret = sk_X509_ATTRIBUTE_delete(x, loc);
73 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
76 X509_ATTRIBUTE *new_attr = NULL;
77 STACK_OF(X509_ATTRIBUTE) *sk = NULL;
80 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
85 if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
90 if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
92 if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
98 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
100 X509_ATTRIBUTE_free(new_attr);
101 sk_X509_ATTRIBUTE_free(sk);
105 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
106 **x, const ASN1_OBJECT *obj,
108 const unsigned char *bytes,
111 X509_ATTRIBUTE *attr;
112 STACK_OF(X509_ATTRIBUTE) *ret;
113 attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
116 ret = X509at_add1_attr(x, attr);
117 X509_ATTRIBUTE_free(attr);
121 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
122 **x, int nid, int type,
123 const unsigned char *bytes,
126 X509_ATTRIBUTE *attr;
127 STACK_OF(X509_ATTRIBUTE) *ret;
128 attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
131 ret = X509at_add1_attr(x, attr);
132 X509_ATTRIBUTE_free(attr);
136 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
137 **x, const char *attrname,
139 const unsigned char *bytes,
142 X509_ATTRIBUTE *attr;
143 STACK_OF(X509_ATTRIBUTE) *ret;
144 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
147 ret = X509at_add1_attr(x, attr);
148 X509_ATTRIBUTE_free(attr);
152 void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
153 const ASN1_OBJECT *obj, int lastpos, int type)
157 i = X509at_get_attr_by_OBJ(x, obj, lastpos);
160 if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
162 at = X509at_get_attr(x, i);
163 if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
165 return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
168 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
169 int atrtype, const void *data,
175 obj = OBJ_nid2obj(nid);
177 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID, X509_R_UNKNOWN_NID);
180 ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
182 ASN1_OBJECT_free(obj);
186 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
187 const ASN1_OBJECT *obj,
188 int atrtype, const void *data,
193 if ((attr == NULL) || (*attr == NULL)) {
194 if ((ret = X509_ATTRIBUTE_new()) == NULL) {
195 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,
196 ERR_R_MALLOC_FAILURE);
202 if (!X509_ATTRIBUTE_set1_object(ret, obj))
204 if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
207 if ((attr != NULL) && (*attr == NULL))
211 if ((attr == NULL) || (ret != *attr))
212 X509_ATTRIBUTE_free(ret);
216 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
217 const char *atrname, int type,
218 const unsigned char *bytes,
222 X509_ATTRIBUTE *nattr;
224 obj = OBJ_txt2obj(atrname, 0);
226 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
227 X509_R_INVALID_FIELD_NAME);
228 ERR_add_error_data(2, "name=", atrname);
231 nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
232 ASN1_OBJECT_free(obj);
236 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
238 if ((attr == NULL) || (obj == NULL))
240 ASN1_OBJECT_free(attr->object);
241 attr->object = OBJ_dup(obj);
242 return attr->object != NULL;
245 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
246 const void *data, int len)
248 ASN1_TYPE *ttmp = NULL;
249 ASN1_STRING *stmp = NULL;
253 if (attrtype & MBSTRING_FLAG) {
254 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
255 OBJ_obj2nid(attr->object));
257 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
261 } else if (len != -1) {
262 if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
264 if (!ASN1_STRING_set(stmp, data, len))
269 * This is a bit naughty because the attribute should really have at
270 * least one value but some types use and zero length SET and require
274 ASN1_STRING_free(stmp);
277 if ((ttmp = ASN1_TYPE_new()) == NULL)
279 if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
280 if (!ASN1_TYPE_set1(ttmp, attrtype, data))
283 ASN1_TYPE_set(ttmp, atype, stmp);
286 if (!sk_ASN1_TYPE_push(attr->set, ttmp))
290 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
291 ASN1_TYPE_free(ttmp);
292 ASN1_STRING_free(stmp);
296 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
300 return sk_ASN1_TYPE_num(attr->set);
303 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
310 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
311 int atrtype, void *data)
314 ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
317 if (atrtype == V_ASN1_BOOLEAN
318 || atrtype == V_ASN1_NULL
319 || atrtype != ASN1_TYPE_get(ttmp)) {
320 X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
323 return ttmp->value.ptr;
326 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
330 return sk_ASN1_TYPE_value(attr->set, idx);