Coverage Report

Created: 2023-09-25 06:45

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