2 * Copyright 1995-2016 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 <openssl/stack.h>
12 #include "internal/cryptlib.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>
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,
30 obj = OBJ_nid2obj(nid);
33 return (X509at_get_attr_by_OBJ(x, obj, lastpos));
36 int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
37 ASN1_OBJECT *obj, int lastpos)
47 n = sk_X509_ATTRIBUTE_num(sk);
48 for (; lastpos < n; lastpos++) {
49 ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
50 if (OBJ_cmp(ex->object, obj) == 0)
56 X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
58 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
61 return sk_X509_ATTRIBUTE_value(x, loc);
64 X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
68 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
70 ret = sk_X509_ATTRIBUTE_delete(x, loc);
74 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
77 X509_ATTRIBUTE *new_attr = NULL;
78 STACK_OF(X509_ATTRIBUTE) *sk = NULL;
81 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
86 if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
91 if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
93 if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
99 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
101 X509_ATTRIBUTE_free(new_attr);
102 sk_X509_ATTRIBUTE_free(sk);
106 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
107 **x, const ASN1_OBJECT *obj,
109 const unsigned char *bytes,
112 X509_ATTRIBUTE *attr;
113 STACK_OF(X509_ATTRIBUTE) *ret;
114 attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
117 ret = X509at_add1_attr(x, attr);
118 X509_ATTRIBUTE_free(attr);
122 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
123 **x, int nid, int type,
124 const unsigned char *bytes,
127 X509_ATTRIBUTE *attr;
128 STACK_OF(X509_ATTRIBUTE) *ret;
129 attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
132 ret = X509at_add1_attr(x, attr);
133 X509_ATTRIBUTE_free(attr);
137 STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
138 **x, const char *attrname,
140 const unsigned char *bytes,
143 X509_ATTRIBUTE *attr;
144 STACK_OF(X509_ATTRIBUTE) *ret;
145 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
148 ret = X509at_add1_attr(x, attr);
149 X509_ATTRIBUTE_free(attr);
153 void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
154 ASN1_OBJECT *obj, int lastpos, int type)
158 i = X509at_get_attr_by_OBJ(x, obj, lastpos);
161 if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
163 at = X509at_get_attr(x, i);
164 if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
166 return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
169 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
170 int atrtype, const void *data,
176 obj = OBJ_nid2obj(nid);
178 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID, X509_R_UNKNOWN_NID);
181 ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
183 ASN1_OBJECT_free(obj);
187 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
188 const ASN1_OBJECT *obj,
189 int atrtype, const void *data,
194 if ((attr == NULL) || (*attr == NULL)) {
195 if ((ret = X509_ATTRIBUTE_new()) == NULL) {
196 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,
197 ERR_R_MALLOC_FAILURE);
203 if (!X509_ATTRIBUTE_set1_object(ret, obj))
205 if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
208 if ((attr != NULL) && (*attr == NULL))
212 if ((attr == NULL) || (ret != *attr))
213 X509_ATTRIBUTE_free(ret);
217 X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
218 const char *atrname, int type,
219 const unsigned char *bytes,
223 X509_ATTRIBUTE *nattr;
225 obj = OBJ_txt2obj(atrname, 0);
227 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
228 X509_R_INVALID_FIELD_NAME);
229 ERR_add_error_data(2, "name=", atrname);
232 nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
233 ASN1_OBJECT_free(obj);
237 int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
239 if ((attr == NULL) || (obj == NULL))
241 ASN1_OBJECT_free(attr->object);
242 attr->object = OBJ_dup(obj);
243 return attr->object != NULL;
246 int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
247 const void *data, int len)
249 ASN1_TYPE *ttmp = NULL;
250 ASN1_STRING *stmp = NULL;
254 if (attrtype & MBSTRING_FLAG) {
255 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
256 OBJ_obj2nid(attr->object));
258 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
262 } else if (len != -1) {
263 if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
265 if (!ASN1_STRING_set(stmp, data, len))
270 * This is a bit naughty because the attribute should really have at
271 * least one value but some types use and zero length SET and require
275 ASN1_STRING_free(stmp);
278 if ((ttmp = ASN1_TYPE_new()) == NULL)
280 if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
281 if (!ASN1_TYPE_set1(ttmp, attrtype, data))
284 ASN1_TYPE_set(ttmp, atype, stmp);
287 if (!sk_ASN1_TYPE_push(attr->set, ttmp))
291 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
292 ASN1_TYPE_free(ttmp);
293 ASN1_STRING_free(stmp);
297 int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
301 return sk_ASN1_TYPE_num(attr->set);
304 ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
308 return (attr->object);
311 void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
312 int atrtype, void *data)
315 ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
318 if (atrtype != ASN1_TYPE_get(ttmp)) {
319 X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
322 return ttmp->value.ptr;
325 ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
329 return sk_ASN1_TYPE_value(attr->set, idx);