Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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.47M
} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
35
3.47M
36
3.47M
IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
37
3.47M
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
38
3.47M
39
3.47M
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
40
3.47M
{
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
7.91k
{
65
7.91k
    const char *ln;
66
7.91k
    char objbuf[80];
67
7.91k
    int rc;
68
69
7.91k
    if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0)
70
1
        return 0;
71
7.91k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
72
7.91k
    rc = (ln != NULL)
73
7.91k
        ? BIO_printf(out, "%s (%s)", objbuf, ln)
74
7.91k
        : BIO_printf(out, "%s", objbuf);
75
7.91k
    return (rc >= 0);
76
7.91k
}
77
78
int ossl_print_attribute_value(BIO *out,
79
    int obj_nid,
80
    const ASN1_TYPE *av,
81
    int indent)
82
310k
{
83
310k
    ASN1_STRING *str;
84
310k
    unsigned char *value;
85
310k
    X509_NAME *xn = NULL;
86
310k
    int64_t int_val;
87
310k
    int ret = 1;
88
89
310k
    switch (av->type) {
90
15.3k
    case V_ASN1_BOOLEAN:
91
15.3k
        if (av->value.boolean) {
92
13.7k
            return BIO_printf(out, "%*sTRUE", indent, "") >= 4;
93
13.7k
        } else {
94
1.59k
            return BIO_printf(out, "%*sFALSE", indent, "") >= 5;
95
1.59k
        }
96
97
8.86k
    case V_ASN1_INTEGER:
98
21.9k
    case V_ASN1_ENUMERATED:
99
21.9k
        if (BIO_printf(out, "%*s", indent, "") < 0)
100
0
            return 0;
101
21.9k
        if (ASN1_ENUMERATED_get_int64(&int_val, av->value.integer) > 0) {
102
12.2k
            return BIO_printf(out, "%lld", (long long int)int_val) > 0;
103
12.2k
        }
104
9.72k
        str = av->value.integer;
105
9.72k
        return ossl_bio_print_hex(out, str->data, str->length);
106
107
2.50k
    case V_ASN1_BIT_STRING:
108
2.50k
        if (BIO_printf(out, "%*s", indent, "") < 0)
109
0
            return 0;
110
2.50k
        return ossl_bio_print_hex(out, av->value.bit_string->data,
111
2.50k
            av->value.bit_string->length);
112
113
7.38k
    case V_ASN1_OCTET_STRING:
114
15.0k
    case V_ASN1_VIDEOTEXSTRING:
115
15.0k
        if (BIO_printf(out, "%*s", indent, "") < 0)
116
0
            return 0;
117
15.0k
        return ossl_bio_print_hex(out, av->value.octet_string->data,
118
15.0k
            av->value.octet_string->length);
119
120
2.72k
    case V_ASN1_NULL:
121
2.72k
        return BIO_printf(out, "%*sNULL", indent, "") >= 4;
122
123
4.89k
    case V_ASN1_OBJECT:
124
4.89k
        if (BIO_printf(out, "%*s", indent, "") < 0)
125
0
            return 0;
126
4.89k
        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
3.23k
    case V_ASN1_GENERALSTRING:
134
7.87k
    case V_ASN1_GRAPHICSTRING:
135
14.5k
    case V_ASN1_OBJECT_DESCRIPTOR:
136
14.5k
        return BIO_printf(out, "%*s%.*s", indent, "",
137
14.5k
                   av->value.generalstring->length,
138
14.5k
                   av->value.generalstring->data)
139
14.5k
            >= 0;
140
141
        /* EXTERNAL would go here. */
142
        /* EMBEDDED PDV would go here. */
143
144
3.55k
    case V_ASN1_UTF8STRING:
145
3.55k
        return BIO_printf(out, "%*s%.*s", indent, "",
146
3.55k
                   av->value.utf8string->length,
147
3.55k
                   av->value.utf8string->data)
148
3.55k
            >= 0;
149
150
2.04k
    case V_ASN1_REAL:
151
2.04k
        return BIO_printf(out, "%*sREAL", indent, "") >= 4;
152
153
        /* RELATIVE-OID would go here. */
154
        /* TIME would go here. */
155
156
81.7k
    case V_ASN1_SEQUENCE:
157
81.7k
        switch (obj_nid) {
158
77.9k
        case NID_undef: /* Unrecognized OID. */
159
77.9k
            break;
160
        /* Attribute types with DN syntax. */
161
92
        case NID_member:
162
163
        case NID_roleOccupant:
163
256
        case NID_seeAlso:
164
256
        case NID_manager:
165
256
        case NID_documentAuthor:
166
256
        case NID_secretary:
167
256
        case NID_associatedName:
168
256
        case NID_dITRedirect:
169
303
        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
303
            value = av->value.sequence->data;
176
303
            xn = d2i_X509_NAME(NULL,
177
303
                (const unsigned char **)&value,
178
303
                av->value.sequence->length);
179
303
            if (xn == NULL) {
180
31
                BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n");
181
31
                return 0;
182
31
            }
183
272
            if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0)
184
0
                ret = 0;
185
272
            X509_NAME_free(xn);
186
272
            return ret;
187
188
3.50k
        default:
189
3.50k
            break;
190
81.7k
        }
191
81.4k
        return ASN1_parse_dump(out, av->value.sequence->data,
192
81.4k
                   av->value.sequence->length, indent, 1)
193
81.4k
            > 0;
194
195
73.5k
    case V_ASN1_SET:
196
73.5k
        return ASN1_parse_dump(out, av->value.set->data,
197
73.5k
                   av->value.set->length, indent, 1)
198
73.5k
            > 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
2.13k
    case V_ASN1_VISIBLESTRING:
206
3.17k
    case V_ASN1_UTCTIME:
207
3.59k
    case V_ASN1_GENERALIZEDTIME:
208
10.2k
    case V_ASN1_NUMERICSTRING:
209
10.2k
        return BIO_printf(out, "%*s%.*s", indent, "",
210
10.2k
                   av->value.visiblestring->length,
211
10.2k
                   av->value.visiblestring->data)
212
10.2k
            >= 0;
213
214
3.25k
    case V_ASN1_PRINTABLESTRING:
215
3.25k
        return BIO_printf(out, "%*s%.*s", indent, "",
216
3.25k
                   av->value.printablestring->length,
217
3.25k
                   av->value.printablestring->data)
218
3.25k
            >= 0;
219
220
7.94k
    case V_ASN1_T61STRING:
221
7.94k
        return BIO_printf(out, "%*s%.*s", indent, "",
222
7.94k
                   av->value.t61string->length,
223
7.94k
                   av->value.t61string->data)
224
7.94k
            >= 0;
225
226
1.41k
    case V_ASN1_IA5STRING:
227
1.41k
        return BIO_printf(out, "%*s%.*s", indent, "",
228
1.41k
                   av->value.ia5string->length,
229
1.41k
                   av->value.ia5string->data)
230
1.41k
            >= 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
49.5k
    default:
244
49.5k
        return BIO_printf(out,
245
49.5k
                   "%*s<Unsupported tag %d>",
246
49.5k
                   indent,
247
49.5k
                   "",
248
49.5k
                   av->type)
249
49.5k
            >= 0;
250
310k
    }
251
310k
}