1/*
2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
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"
19
20int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
21{
22 return sk_X509_ATTRIBUTE_num(x);
23}
24
25int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
26 int lastpos)
27{
28 const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
29
30 if (obj == NULL)
31 return -2;
32 return X509at_get_attr_by_OBJ(x, obj, lastpos);
33}
34
35int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
36 const ASN1_OBJECT *obj, int lastpos)
37{
38 int n;
39 X509_ATTRIBUTE *ex;
40
41 if (sk == NULL)
42 return -1;
43 lastpos++;
44 if (lastpos < 0)
45 lastpos = 0;
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)
50 return lastpos;
51 }
52 return -1;
53}
54
55X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
56{
57 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
58 return NULL;
59
60 return sk_X509_ATTRIBUTE_value(x, loc);
61}
62
63X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
64{
65 X509_ATTRIBUTE *ret;
66
67 if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
68 return NULL;
69 ret = sk_X509_ATTRIBUTE_delete(x, loc);
70 return ret;
71}
72
73STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
74 X509_ATTRIBUTE *attr)
75{
76 X509_ATTRIBUTE *new_attr = NULL;
77 STACK_OF(X509_ATTRIBUTE) *sk = NULL;
78
79 if (x == NULL) {
80 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
81 goto err2;
82 }
83
84 if (*x == NULL) {
85 if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
86 goto err;
87 } else
88 sk = *x;
89
90 if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
91 goto err2;
92 if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
93 goto err;
94 if (*x == NULL)
95 *x = sk;
96 return sk;
97 err:
98 X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
99 err2:
100 X509_ATTRIBUTE_free(new_attr);
101 sk_X509_ATTRIBUTE_free(sk);
102 return NULL;
103}
104
105STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
106 **x, const ASN1_OBJECT *obj,
107 int type,
108 const unsigned char *bytes,
109 int len)
110{
111 X509_ATTRIBUTE *attr;
112 STACK_OF(X509_ATTRIBUTE) *ret;
113 attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
114 if (!attr)
115 return 0;
116 ret = X509at_add1_attr(x, attr);
117 X509_ATTRIBUTE_free(attr);
118 return ret;
119}
120
121STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
122 **x, int nid, int type,
123 const unsigned char *bytes,
124 int len)
125{
126 X509_ATTRIBUTE *attr;
127 STACK_OF(X509_ATTRIBUTE) *ret;
128 attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
129 if (!attr)
130 return 0;
131 ret = X509at_add1_attr(x, attr);
132 X509_ATTRIBUTE_free(attr);
133 return ret;
134}
135
136STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
137 **x, const char *attrname,
138 int type,
139 const unsigned char *bytes,
140 int len)
141{
142 X509_ATTRIBUTE *attr;
143 STACK_OF(X509_ATTRIBUTE) *ret;
144 attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
145 if (!attr)
146 return 0;
147 ret = X509at_add1_attr(x, attr);
148 X509_ATTRIBUTE_free(attr);
149 return ret;
150}
151
152void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
153 const ASN1_OBJECT *obj, int lastpos, int type)
154{
155 int i;
156 X509_ATTRIBUTE *at;
157 i = X509at_get_attr_by_OBJ(x, obj, lastpos);
158 if (i == -1)
159 return NULL;
160 if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
161 return NULL;
162 at = X509at_get_attr(x, i);
163 if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
164 return NULL;
165 return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
166}
167
168X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
169 int atrtype, const void *data,
170 int len)
171{
172 ASN1_OBJECT *obj;
173 X509_ATTRIBUTE *ret;
174
175 obj = OBJ_nid2obj(nid);
176 if (obj == NULL) {
177 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID, X509_R_UNKNOWN_NID);
178 return NULL;
179 }
180 ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
181 if (ret == NULL)
182 ASN1_OBJECT_free(obj);
183 return ret;
184}
185
186X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
187 const ASN1_OBJECT *obj,
188 int atrtype, const void *data,
189 int len)
190{
191 X509_ATTRIBUTE *ret;
192
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);
197 return NULL;
198 }
199 } else
200 ret = *attr;
201
202 if (!X509_ATTRIBUTE_set1_object(ret, obj))
203 goto err;
204 if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
205 goto err;
206
207 if ((attr != NULL) && (*attr == NULL))
208 *attr = ret;
209 return ret;
210 err:
211 if ((attr == NULL) || (ret != *attr))
212 X509_ATTRIBUTE_free(ret);
213 return NULL;
214}
215
216X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
217 const char *atrname, int type,
218 const unsigned char *bytes,
219 int len)
220{
221 ASN1_OBJECT *obj;
222 X509_ATTRIBUTE *nattr;
223
224 obj = OBJ_txt2obj(atrname, 0);
225 if (obj == NULL) {
226 X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
227 X509_R_INVALID_FIELD_NAME);
228 ERR_add_error_data(2, "name=", atrname);
229 return NULL;
230 }
231 nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
232 ASN1_OBJECT_free(obj);
233 return nattr;
234}
235
236int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
237{
238 if ((attr == NULL) || (obj == NULL))
239 return 0;
240 ASN1_OBJECT_free(attr->object);
241 attr->object = OBJ_dup(obj);
242 return attr->object != NULL;
243}
244
245int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
246 const void *data, int len)
247{
248 ASN1_TYPE *ttmp = NULL;
249 ASN1_STRING *stmp = NULL;
250 int atype = 0;
251 if (!attr)
252 return 0;
253 if (attrtype & MBSTRING_FLAG) {
254 stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
255 OBJ_obj2nid(attr->object));
256 if (!stmp) {
257 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
258 return 0;
259 }
260 atype = stmp->type;
261 } else if (len != -1) {
262 if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
263 goto err;
264 if (!ASN1_STRING_set(stmp, data, len))
265 goto err;
266 atype = attrtype;
267 }
268 /*
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
271 * this.
272 */
273 if (attrtype == 0) {
274 ASN1_STRING_free(stmp);
275 return 1;
276 }
277 if ((ttmp = ASN1_TYPE_new()) == NULL)
278 goto err;
279 if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
280 if (!ASN1_TYPE_set1(ttmp, attrtype, data))
281 goto err;
282 } else {
283 ASN1_TYPE_set(ttmp, atype, stmp);
284 stmp = NULL;
285 }
286 if (!sk_ASN1_TYPE_push(attr->set, ttmp))
287 goto err;
288 return 1;
289 err:
290 X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
291 ASN1_TYPE_free(ttmp);
292 ASN1_STRING_free(stmp);
293 return 0;
294}
295
296int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
297{
298 if (attr == NULL)
299 return 0;
300 return sk_ASN1_TYPE_num(attr->set);
301}
302
303ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
304{
305 if (attr == NULL)
306 return NULL;
307 return attr->object;
308}
309
310void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
311 int atrtype, void *data)
312{
313 ASN1_TYPE *ttmp;
314 ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
315 if (!ttmp)
316 return NULL;
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);
321 return NULL;
322 }
323 return ttmp->value.ptr;
324}
325
326ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
327{
328 if (attr == NULL)
329 return NULL;
330 return sk_ASN1_TYPE_value(attr->set, idx);
331}
332