Coverage Report

Created: 2026-04-09 06:50

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