Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/x509/x509_att.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
139k
{
23
139k
    return sk_X509_ATTRIBUTE_num(x);
24
139k
}
25
26
int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
27
    int lastpos)
28
297
{
29
297
    const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
30
31
297
    if (obj == NULL)
32
0
        return -2;
33
297
    return X509at_get_attr_by_OBJ(x, obj, lastpos);
34
297
}
35
36
int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
37
    const ASN1_OBJECT *obj, int lastpos)
38
297
{
39
297
    int n;
40
297
    X509_ATTRIBUTE *ex;
41
42
297
    if (sk == NULL)
43
0
        return -1;
44
297
    lastpos++;
45
297
    if (lastpos < 0)
46
0
        lastpos = 0;
47
297
    n = sk_X509_ATTRIBUTE_num(sk);
48
1.05k
    for (; lastpos < n; lastpos++) {
49
835
        ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
50
835
        if (OBJ_cmp(ex->object, obj) == 0)
51
81
            return lastpos;
52
835
    }
53
216
    return -1;
54
297
}
55
56
X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
57
0
{
58
0
    if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
59
0
        return NULL;
60
61
0
    return sk_X509_ATTRIBUTE_value(x, loc);
62
0
}
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) *ossl_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) {
86
0
        if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
87
0
            goto err;
88
0
    } else {
89
0
        sk = *x;
90
0
    }
91
92
0
    if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
93
0
        goto err2;
94
0
    if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
95
0
        goto err;
96
0
    if (*x == NULL)
97
0
        *x = sk;
98
0
    return sk;
99
0
err:
100
0
    ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
101
0
err2:
102
0
    X509_ATTRIBUTE_free(new_attr);
103
0
    if (*x == NULL)
104
0
        sk_X509_ATTRIBUTE_free(sk);
105
0
    return NULL;
106
0
}
107
108
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
109
    X509_ATTRIBUTE *attr)
110
0
{
111
0
    if (x == NULL || attr == NULL) {
112
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
113
0
        return NULL;
114
0
    }
115
0
    if (*x != NULL && X509at_get_attr_by_OBJ(*x, attr->object, -1) != -1) {
116
0
        ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
117
0
        return NULL;
118
0
    }
119
120
0
    return ossl_x509at_add1_attr(x, attr);
121
0
}
122
123
STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
124
    const ASN1_OBJECT *obj,
125
    int type,
126
    const unsigned char *bytes,
127
    int len)
128
0
{
129
0
    X509_ATTRIBUTE *attr;
130
0
    STACK_OF(X509_ATTRIBUTE) *ret;
131
132
0
    attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
133
0
    if (attr == NULL)
134
0
        return 0;
135
0
    ret = ossl_x509at_add1_attr(x, attr);
136
0
    X509_ATTRIBUTE_free(attr);
137
0
    return ret;
138
0
}
139
140
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
141
                                                      **x,
142
    const ASN1_OBJECT *obj,
143
    int type,
144
    const unsigned char *bytes,
145
    int len)
146
0
{
147
0
    if (x == NULL || obj == NULL) {
148
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
149
0
        return NULL;
150
0
    }
151
0
    if (*x != NULL && X509at_get_attr_by_OBJ(*x, obj, -1) != -1) {
152
0
        ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
153
0
        return NULL;
154
0
    }
155
156
0
    return ossl_x509at_add1_attr_by_OBJ(x, obj, type, bytes, len);
157
0
}
158
159
STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
160
    int nid, int type,
161
    const unsigned char *bytes,
162
    int len)
163
0
{
164
0
    X509_ATTRIBUTE *attr;
165
0
    STACK_OF(X509_ATTRIBUTE) *ret;
166
167
0
    attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
168
0
    if (attr == NULL)
169
0
        return 0;
170
0
    ret = ossl_x509at_add1_attr(x, attr);
171
0
    X509_ATTRIBUTE_free(attr);
172
0
    return ret;
173
0
}
174
175
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
176
                                                      **x,
177
    int nid, int type,
178
    const unsigned char *bytes,
179
    int len)
180
0
{
181
0
    if (x == NULL) {
182
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return NULL;
184
0
    }
185
0
    if (*x != NULL && X509at_get_attr_by_NID(*x, nid, -1) != -1) {
186
0
        ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
187
0
        return NULL;
188
0
    }
189
190
0
    return ossl_x509at_add1_attr_by_NID(x, nid, type, bytes, len);
191
0
}
192
193
STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
194
    const char *attrname,
195
    int type,
196
    const unsigned char *bytes,
197
    int len)
198
0
{
199
0
    X509_ATTRIBUTE *attr;
200
0
    STACK_OF(X509_ATTRIBUTE) *ret;
201
202
0
    attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
203
0
    if (attr == NULL)
204
0
        return 0;
205
0
    ret = ossl_x509at_add1_attr(x, attr);
206
0
    X509_ATTRIBUTE_free(attr);
207
0
    return ret;
208
0
}
209
210
STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
211
                                                      **x,
212
    const char *attrname,
213
    int type,
214
    const unsigned char *bytes,
215
    int len)
216
0
{
217
0
    X509_ATTRIBUTE *attr;
218
0
    STACK_OF(X509_ATTRIBUTE) *ret;
219
0
    attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
220
0
    if (!attr)
221
0
        return 0;
222
0
    ret = X509at_add1_attr(x, attr);
223
0
    X509_ATTRIBUTE_free(attr);
224
0
    return ret;
225
0
}
226
227
void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
228
    const ASN1_OBJECT *obj, int lastpos, int type)
229
0
{
230
0
    int i;
231
0
    X509_ATTRIBUTE *at;
232
0
    i = X509at_get_attr_by_OBJ(x, obj, lastpos);
233
0
    if (i == -1)
234
0
        return NULL;
235
0
    if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
236
0
        return NULL;
237
0
    at = X509at_get_attr(x, i);
238
0
    if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
239
0
        return NULL;
240
0
    return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
241
0
}
242
243
STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x)
244
0
{
245
0
    int i, n;
246
0
    STACK_OF(X509_ATTRIBUTE) *sk = NULL;
247
248
0
    n = sk_X509_ATTRIBUTE_num(x);
249
0
    for (i = 0; i < n; ++i) {
250
0
        X509_ATTRIBUTE *attr = sk_X509_ATTRIBUTE_value(x, i);
251
252
0
        if (X509at_add1_attr(&sk, attr) == NULL) {
253
0
            sk_X509_ATTRIBUTE_pop_free(sk, X509_ATTRIBUTE_free);
254
0
            return NULL;
255
0
        }
256
0
    }
257
0
    return sk;
258
0
}
259
260
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
261
    int atrtype, const void *data,
262
    int len)
263
0
{
264
0
    ASN1_OBJECT *obj;
265
0
    X509_ATTRIBUTE *ret;
266
267
0
    obj = OBJ_nid2obj(nid);
268
0
    if (obj == NULL) {
269
0
        ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
270
0
        return NULL;
271
0
    }
272
0
    ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
273
0
    if (ret == NULL)
274
0
        ASN1_OBJECT_free(obj);
275
0
    return ret;
276
0
}
277
278
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
279
    const ASN1_OBJECT *obj,
280
    int atrtype, const void *data,
281
    int len)
282
0
{
283
0
    X509_ATTRIBUTE *ret;
284
285
0
    if ((attr == NULL) || (*attr == NULL)) {
286
0
        if ((ret = X509_ATTRIBUTE_new()) == NULL) {
287
0
            ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
288
0
            return NULL;
289
0
        }
290
0
    } else
291
0
        ret = *attr;
292
293
0
    if (!X509_ATTRIBUTE_set1_object(ret, obj))
294
0
        goto err;
295
0
    if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
296
0
        goto err;
297
298
0
    if ((attr != NULL) && (*attr == NULL))
299
0
        *attr = ret;
300
0
    return ret;
301
0
err:
302
0
    if ((attr == NULL) || (ret != *attr))
303
0
        X509_ATTRIBUTE_free(ret);
304
0
    return NULL;
305
0
}
306
307
X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
308
    const char *atrname, int type,
309
    const unsigned char *bytes,
310
    int len)
311
0
{
312
0
    ASN1_OBJECT *obj;
313
0
    X509_ATTRIBUTE *nattr;
314
315
0
    obj = OBJ_txt2obj(atrname, 0);
316
0
    if (obj == NULL) {
317
0
        ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
318
0
            "name=%s", atrname);
319
0
        return NULL;
320
0
    }
321
0
    nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
322
0
    ASN1_OBJECT_free(obj);
323
0
    return nattr;
324
0
}
325
326
int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
327
0
{
328
0
    if ((attr == NULL) || (obj == NULL))
329
0
        return 0;
330
0
    ASN1_OBJECT_free(attr->object);
331
0
    attr->object = OBJ_dup(obj);
332
0
    return attr->object != NULL;
333
0
}
334
335
int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
336
    const void *data, int len)
337
0
{
338
0
    ASN1_TYPE *ttmp = NULL;
339
0
    ASN1_STRING *stmp = NULL;
340
0
    int atype = 0;
341
0
    if (!attr)
342
0
        return 0;
343
0
    if (attrtype & MBSTRING_FLAG) {
344
0
        stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
345
0
            OBJ_obj2nid(attr->object));
346
0
        if (!stmp) {
347
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
348
0
            return 0;
349
0
        }
350
0
        atype = stmp->type;
351
0
    } else if (len != -1) {
352
0
        if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
353
0
            goto err;
354
0
        if (!ASN1_STRING_set(stmp, data, len))
355
0
            goto err;
356
0
        atype = attrtype;
357
0
    }
358
    /*
359
     * This is a bit naughty because the attribute should really have at
360
     * least one value but some types use and zero length SET and require
361
     * this.
362
     */
363
0
    if (attrtype == 0) {
364
0
        ASN1_STRING_free(stmp);
365
0
        return 1;
366
0
    }
367
0
    if ((ttmp = ASN1_TYPE_new()) == NULL)
368
0
        goto err;
369
0
    if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
370
0
        if (!ASN1_TYPE_set1(ttmp, attrtype, data))
371
0
            goto err;
372
0
    } else {
373
0
        ASN1_TYPE_set(ttmp, atype, stmp);
374
0
        stmp = NULL;
375
0
    }
376
0
    if (!sk_ASN1_TYPE_push(attr->set, ttmp))
377
0
        goto err;
378
0
    return 1;
379
0
err:
380
0
    ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
381
0
    ASN1_TYPE_free(ttmp);
382
0
    ASN1_STRING_free(stmp);
383
0
    return 0;
384
0
}
385
386
int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
387
749k
{
388
749k
    if (attr == NULL)
389
0
        return 0;
390
749k
    return sk_ASN1_TYPE_num(attr->set);
391
749k
}
392
393
ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
394
224k
{
395
224k
    if (attr == NULL)
396
0
        return NULL;
397
224k
    return attr->object;
398
224k
}
399
400
void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
401
    int atrtype, void *data)
402
0
{
403
0
    ASN1_TYPE *ttmp;
404
0
    ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
405
0
    if (!ttmp)
406
0
        return NULL;
407
0
    if (atrtype == V_ASN1_BOOLEAN
408
0
        || atrtype == V_ASN1_NULL
409
0
        || atrtype != ASN1_TYPE_get(ttmp)) {
410
0
        ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
411
0
        return NULL;
412
0
    }
413
0
    return ttmp->value.ptr;
414
0
}
415
416
ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
417
5.66M
{
418
5.66M
    if (attr == NULL)
419
0
        return NULL;
420
5.66M
    return sk_ASN1_TYPE_value(attr->set, idx);
421
5.66M
}