Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/x_attrib.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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/objects.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/x509.h>
15
#include "x509_local.h"
16
#include <crypto/x509.h>
17
18
#include <crypto/asn1.h>
19
20
/*-
21
 * X509_ATTRIBUTE: this has the following form:
22
 *
23
 * typedef struct x509_attributes_st
24
 *      {
25
 *      ASN1_OBJECT *object;
26
 *      STACK_OF(ASN1_TYPE) *set;
27
 *      } X509_ATTRIBUTE;
28
 *
29
 */
30
31
ASN1_SEQUENCE(X509_ATTRIBUTE) = {
32
    ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
33
    ASN1_SET_OF(X509_ATTRIBUTE, set, ASN1_ANY)
34
3.92M
} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
35
3.92M
36
3.92M
IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
37
3.92M
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
38
3.92M
39
3.92M
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
40
3.92M
{
41
0
    X509_ATTRIBUTE *ret = NULL;
42
0
    ASN1_TYPE *val = NULL;
43
0
    ASN1_OBJECT *oid;
44
45
0
    if ((oid = OBJ_nid2obj(nid)) == NULL)
46
0
        return NULL;
47
0
    if ((ret = X509_ATTRIBUTE_new()) == NULL)
48
0
        return NULL;
49
0
    ret->object = oid;
50
0
    if ((val = ASN1_TYPE_new()) == NULL)
51
0
        goto err;
52
0
    if (!sk_ASN1_TYPE_push(ret->set, val))
53
0
        goto err;
54
55
0
    ASN1_TYPE_set(val, atrtype, value);
56
0
    return ret;
57
0
err:
58
0
    X509_ATTRIBUTE_free(ret);
59
0
    ASN1_TYPE_free(val);
60
0
    return NULL;
61
0
}
62
63
static int print_oid(BIO *out, const ASN1_OBJECT *oid)
64
8.36k
{
65
8.36k
    const char *ln;
66
8.36k
    char objbuf[80];
67
8.36k
    int rc;
68
69
8.36k
    if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0)
70
1
        return 0;
71
8.36k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
72
8.36k
    rc = (ln != NULL)
73
8.36k
        ? BIO_printf(out, "%s (%s)", objbuf, ln)
74
8.36k
        : BIO_printf(out, "%s", objbuf);
75
8.36k
    return (rc >= 0);
76
8.36k
}
77
78
int ossl_print_attribute_value(BIO *out,
79
    int obj_nid,
80
    const ASN1_TYPE *av,
81
    int indent)
82
492k
{
83
492k
    ASN1_STRING *str;
84
492k
    unsigned char *value;
85
492k
    X509_NAME *xn = NULL;
86
492k
    int64_t int_val;
87
492k
    int ret = 1;
88
89
492k
    switch (av->type) {
90
9.62k
    case V_ASN1_BOOLEAN:
91
9.62k
        if (av->value.boolean) {
92
7.28k
            return BIO_printf(out, "%*sTRUE", indent, "") >= 4;
93
7.28k
        } else {
94
2.33k
            return BIO_printf(out, "%*sFALSE", indent, "") >= 5;
95
2.33k
        }
96
97
5.38k
    case V_ASN1_INTEGER:
98
21.7k
    case V_ASN1_ENUMERATED:
99
21.7k
        if (BIO_printf(out, "%*s", indent, "") < 0)
100
0
            return 0;
101
21.7k
        if (ASN1_ENUMERATED_get_int64(&int_val, av->value.integer) > 0) {
102
10.8k
            return BIO_printf(out, "%lld", (long long int)int_val) > 0;
103
10.8k
        }
104
10.8k
        str = av->value.integer;
105
10.8k
        return ossl_bio_print_hex(out, str->data, str->length);
106
107
9.75k
    case V_ASN1_BIT_STRING:
108
9.75k
        if (BIO_printf(out, "%*s", indent, "") < 0)
109
0
            return 0;
110
9.75k
        return ossl_bio_print_hex(out, av->value.bit_string->data,
111
9.75k
            av->value.bit_string->length);
112
113
12.7k
    case V_ASN1_OCTET_STRING:
114
22.8k
    case V_ASN1_VIDEOTEXSTRING:
115
22.8k
        if (BIO_printf(out, "%*s", indent, "") < 0)
116
0
            return 0;
117
22.8k
        return ossl_bio_print_hex(out, av->value.octet_string->data,
118
22.8k
            av->value.octet_string->length);
119
120
2.83k
    case V_ASN1_NULL:
121
2.83k
        return BIO_printf(out, "%*sNULL", indent, "") >= 4;
122
123
8.36k
    case V_ASN1_OBJECT:
124
8.36k
        if (BIO_printf(out, "%*s", indent, "") < 0)
125
0
            return 0;
126
8.36k
        return print_oid(out, av->value.object);
127
128
    /*
129
     * ObjectDescriptor is an IMPLICIT GraphicString, but GeneralString is a
130
     * superset supported by OpenSSL, so we will use that anywhere a
131
     * GraphicString is needed here.
132
     */
133
5.85k
    case V_ASN1_GENERALSTRING:
134
11.0k
    case V_ASN1_GRAPHICSTRING:
135
26.7k
    case V_ASN1_OBJECT_DESCRIPTOR:
136
26.7k
        return BIO_printf(out, "%*s%.*s", indent, "",
137
26.7k
                   av->value.generalstring->length,
138
26.7k
                   av->value.generalstring->data)
139
26.7k
            >= 0;
140
141
        /* EXTERNAL would go here. */
142
        /* EMBEDDED PDV would go here. */
143
144
5.51k
    case V_ASN1_UTF8STRING:
145
5.51k
        return BIO_printf(out, "%*s%.*s", indent, "",
146
5.51k
                   av->value.utf8string->length,
147
5.51k
                   av->value.utf8string->data)
148
5.51k
            >= 0;
149
150
2.72k
    case V_ASN1_REAL:
151
2.72k
        return BIO_printf(out, "%*sREAL", indent, "") >= 4;
152
153
        /* RELATIVE-OID would go here. */
154
        /* TIME would go here. */
155
156
128k
    case V_ASN1_SEQUENCE:
157
128k
        switch (obj_nid) {
158
111k
        case NID_undef: /* Unrecognized OID. */
159
111k
            break;
160
        /* Attribute types with DN syntax. */
161
1.05k
        case NID_member:
162
2.05k
        case NID_roleOccupant:
163
10.9k
        case NID_seeAlso:
164
10.9k
        case NID_manager:
165
10.9k
        case NID_documentAuthor:
166
10.9k
        case NID_secretary:
167
10.9k
        case NID_associatedName:
168
10.9k
        case NID_dITRedirect:
169
12.2k
        case NID_owner:
170
            /*
171
             * d2i_ functions increment the ppin pointer. See doc/man3/d2i_X509.pod.
172
             * This preserves the original  pointer. We don't want to corrupt this
173
             * value.
174
             */
175
12.2k
            value = av->value.sequence->data;
176
12.2k
            xn = d2i_X509_NAME(NULL,
177
12.2k
                (const unsigned char **)&value,
178
12.2k
                av->value.sequence->length);
179
12.2k
            if (xn == NULL) {
180
50
                BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n");
181
50
                return 0;
182
50
            }
183
12.1k
            if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0)
184
0
                ret = 0;
185
12.1k
            X509_NAME_free(xn);
186
12.1k
            return ret;
187
188
4.93k
        default:
189
4.93k
            break;
190
128k
        }
191
116k
        return ASN1_parse_dump(out, av->value.sequence->data,
192
116k
                   av->value.sequence->length, indent, 1)
193
116k
            > 0;
194
195
86.9k
    case V_ASN1_SET:
196
86.9k
        return ASN1_parse_dump(out, av->value.set->data,
197
86.9k
                   av->value.set->length, indent, 1)
198
86.9k
            > 0;
199
200
    /*
201
     * UTCTime ::= [UNIVERSAL 23] IMPLICIT VisibleString
202
     * GeneralizedTime ::= [UNIVERSAL 24] IMPLICIT VisibleString
203
     * VisibleString is a superset for NumericString, so it will work for that.
204
     */
205
3.29k
    case V_ASN1_VISIBLESTRING:
206
4.63k
    case V_ASN1_UTCTIME:
207
10.2k
    case V_ASN1_GENERALIZEDTIME:
208
23.0k
    case V_ASN1_NUMERICSTRING:
209
23.0k
        return BIO_printf(out, "%*s%.*s", indent, "",
210
23.0k
                   av->value.visiblestring->length,
211
23.0k
                   av->value.visiblestring->data)
212
23.0k
            >= 0;
213
214
4.39k
    case V_ASN1_PRINTABLESTRING:
215
4.39k
        return BIO_printf(out, "%*s%.*s", indent, "",
216
4.39k
                   av->value.printablestring->length,
217
4.39k
                   av->value.printablestring->data)
218
4.39k
            >= 0;
219
220
13.7k
    case V_ASN1_T61STRING:
221
13.7k
        return BIO_printf(out, "%*s%.*s", indent, "",
222
13.7k
                   av->value.t61string->length,
223
13.7k
                   av->value.t61string->data)
224
13.7k
            >= 0;
225
226
4.96k
    case V_ASN1_IA5STRING:
227
4.96k
        return BIO_printf(out, "%*s%.*s", indent, "",
228
4.96k
                   av->value.ia5string->length,
229
4.96k
                   av->value.ia5string->data)
230
4.96k
            >= 0;
231
232
    /* UniversalString would go here. */
233
    /* CHARACTER STRING would go here. */
234
    /* BMPString would go here. */
235
    /* DATE would go here. */
236
    /* TIME-OF-DAY would go here. */
237
    /* DATE-TIME would go here. */
238
    /* DURATION would go here. */
239
    /* OID-IRI would go here. */
240
    /* RELATIVE-OID-IRI would go here. */
241
242
    /* Would it be appropriate to just hexdump? */
243
121k
    default:
244
121k
        return BIO_printf(out,
245
121k
                   "%*s<Unsupported tag %d>",
246
121k
                   indent,
247
121k
                   "",
248
121k
                   av->type)
249
121k
            >= 0;
250
492k
    }
251
492k
}