Coverage Report

Created: 2025-06-13 06:58

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