Coverage Report

Created: 2025-12-31 06:58

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