Coverage Report

Created: 2025-06-13 06:58

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