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