Coverage Report

Created: 2025-12-10 06:24

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