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