Coverage Report

Created: 2026-11-16 07:12

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
0
} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
35
0
36
0
IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
37
0
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
38
0
39
0
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
40
0
{
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
0
{
65
0
    const char *ln;
66
0
    char objbuf[80];
67
0
    int rc;
68
69
0
    if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0)
70
0
        return 0;
71
0
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
72
0
    rc = (ln != NULL)
73
0
        ? BIO_printf(out, "%s (%s)", objbuf, ln)
74
0
        : BIO_printf(out, "%s", objbuf);
75
0
    return (rc >= 0);
76
0
}
77
78
int ossl_print_attribute_value(BIO *out,
79
    int obj_nid,
80
    const ASN1_TYPE *av,
81
    int indent)
82
0
{
83
0
    ASN1_STRING *str;
84
0
    unsigned char *value;
85
0
    X509_NAME *xn = NULL;
86
0
    int64_t int_val;
87
0
    int ret = 1;
88
89
0
    switch (av->type) {
90
0
    case V_ASN1_BOOLEAN:
91
0
        if (av->value.boolean) {
92
0
            return BIO_printf(out, "%*sTRUE", indent, "") >= 4;
93
0
        } else {
94
0
            return BIO_printf(out, "%*sFALSE", indent, "") >= 5;
95
0
        }
96
97
0
    case V_ASN1_INTEGER:
98
0
        if (BIO_printf(out, "%*s", indent, "") < 0)
99
0
            return 0;
100
0
        if (ASN1_INTEGER_get_int64(&int_val, av->value.integer) > 0)
101
0
            return BIO_printf(out, "%lld", (long long int)int_val) > 0;
102
0
        str = av->value.integer;
103
0
        return ossl_bio_print_hex(out, str->data, str->length);
104
105
0
    case V_ASN1_ENUMERATED:
106
0
        if (BIO_printf(out, "%*s", indent, "") < 0)
107
0
            return 0;
108
0
        if (ASN1_ENUMERATED_get_int64(&int_val, av->value.enumerated) > 0)
109
0
            return BIO_printf(out, "%lld", (long long int)int_val) > 0;
110
0
        str = av->value.enumerated;
111
0
        return ossl_bio_print_hex(out, str->data, str->length);
112
113
0
    case V_ASN1_BIT_STRING:
114
0
        if (BIO_printf(out, "%*s", indent, "") < 0)
115
0
            return 0;
116
0
        return ossl_bio_print_hex(out, av->value.bit_string->data,
117
0
            av->value.bit_string->length);
118
119
0
    case V_ASN1_OCTET_STRING:
120
0
    case V_ASN1_VIDEOTEXSTRING:
121
0
        if (BIO_printf(out, "%*s", indent, "") < 0)
122
0
            return 0;
123
0
        return ossl_bio_print_hex(out, av->value.octet_string->data,
124
0
            av->value.octet_string->length);
125
126
0
    case V_ASN1_NULL:
127
0
        return BIO_printf(out, "%*sNULL", indent, "") >= 4;
128
129
0
    case V_ASN1_OBJECT:
130
0
        if (BIO_printf(out, "%*s", indent, "") < 0)
131
0
            return 0;
132
0
        return print_oid(out, av->value.object);
133
134
    /*
135
     * ObjectDescriptor is an IMPLICIT GraphicString, but GeneralString is a
136
     * superset supported by OpenSSL, so we will use that anywhere a
137
     * GraphicString is needed here.
138
     */
139
0
    case V_ASN1_GENERALSTRING:
140
0
    case V_ASN1_GRAPHICSTRING:
141
0
    case V_ASN1_OBJECT_DESCRIPTOR:
142
0
        return BIO_printf(out, "%*s%.*s", indent, "",
143
0
                   av->value.generalstring->length,
144
0
                   av->value.generalstring->length
145
0
                       ? av->value.generalstring->data
146
0
                       : (const unsigned char *)"")
147
0
            >= 0;
148
149
        /* EXTERNAL would go here. */
150
        /* EMBEDDED PDV would go here. */
151
152
0
    case V_ASN1_UTF8STRING:
153
0
        return BIO_printf(out, "%*s%.*s", indent, "",
154
0
                   av->value.utf8string->length,
155
0
                   av->value.utf8string->length
156
0
                       ? av->value.utf8string->data
157
0
                       : (const unsigned char *)"")
158
0
            >= 0;
159
160
0
    case V_ASN1_REAL:
161
0
        return BIO_printf(out, "%*sREAL", indent, "") >= 4;
162
163
        /* RELATIVE-OID would go here. */
164
        /* TIME would go here. */
165
166
0
    case V_ASN1_SEQUENCE:
167
0
        switch (obj_nid) {
168
0
        case NID_undef: /* Unrecognized OID. */
169
0
            break;
170
        /* Attribute types with DN syntax. */
171
0
        case NID_member:
172
0
        case NID_roleOccupant:
173
0
        case NID_seeAlso:
174
0
        case NID_manager:
175
0
        case NID_documentAuthor:
176
0
        case NID_secretary:
177
0
        case NID_associatedName:
178
0
        case NID_dITRedirect:
179
0
        case NID_owner:
180
            /*
181
             * d2i_ functions increment the ppin pointer. See doc/man3/d2i_X509.pod.
182
             * This preserves the original  pointer. We don't want to corrupt this
183
             * value.
184
             */
185
0
            value = av->value.sequence->data;
186
0
            xn = d2i_X509_NAME(NULL,
187
0
                (const unsigned char **)&value,
188
0
                av->value.sequence->length);
189
0
            if (xn == NULL) {
190
0
                BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n");
191
0
                return 0;
192
0
            }
193
0
            if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0)
194
0
                ret = 0;
195
0
            X509_NAME_free(xn);
196
0
            return ret;
197
198
0
        default:
199
0
            break;
200
0
        }
201
0
        return ASN1_parse_dump(out, av->value.sequence->data,
202
0
                   av->value.sequence->length, indent, 1)
203
0
            > 0;
204
205
0
    case V_ASN1_SET:
206
0
        return ASN1_parse_dump(out, av->value.set->data,
207
0
                   av->value.set->length, indent, 1)
208
0
            > 0;
209
210
    /*
211
     * UTCTime ::= [UNIVERSAL 23] IMPLICIT VisibleString
212
     * GeneralizedTime ::= [UNIVERSAL 24] IMPLICIT VisibleString
213
     * VisibleString is a superset for NumericString, so it will work for that.
214
     */
215
0
    case V_ASN1_VISIBLESTRING:
216
0
    case V_ASN1_UTCTIME:
217
0
    case V_ASN1_GENERALIZEDTIME:
218
0
    case V_ASN1_NUMERICSTRING:
219
0
        return BIO_printf(out, "%*s%.*s", indent, "",
220
0
                   av->value.visiblestring->length,
221
0
                   av->value.visiblestring->length
222
0
                       ? av->value.visiblestring->data
223
0
                       : (const unsigned char *)"")
224
0
            >= 0;
225
226
0
    case V_ASN1_PRINTABLESTRING:
227
0
        return BIO_printf(out, "%*s%.*s", indent, "",
228
0
                   av->value.printablestring->length,
229
0
                   av->value.printablestring->length
230
0
                       ? av->value.printablestring->data
231
0
                       : (const unsigned char *)"")
232
0
            >= 0;
233
234
0
    case V_ASN1_T61STRING:
235
0
        return BIO_printf(out, "%*s%.*s", indent, "",
236
0
                   av->value.t61string->length,
237
0
                   av->value.t61string->length
238
0
                       ? av->value.t61string->data
239
0
                       : (const unsigned char *)"")
240
0
            >= 0;
241
242
0
    case V_ASN1_IA5STRING:
243
0
        return BIO_printf(out, "%*s%.*s", indent, "",
244
0
                   av->value.ia5string->length,
245
0
                   av->value.ia5string->length
246
0
                       ? av->value.ia5string->data
247
0
                       : (const unsigned char *)"")
248
0
            >= 0;
249
250
    /* UniversalString would go here. */
251
    /* CHARACTER STRING would go here. */
252
    /* BMPString would go here. */
253
    /* DATE would go here. */
254
    /* TIME-OF-DAY would go here. */
255
    /* DATE-TIME would go here. */
256
    /* DURATION would go here. */
257
    /* OID-IRI would go here. */
258
    /* RELATIVE-OID-IRI would go here. */
259
260
    /* Would it be appropriate to just hexdump? */
261
0
    default:
262
0
        return BIO_printf(out,
263
0
                   "%*s<Unsupported tag %d>",
264
0
                   indent,
265
0
                   "",
266
0
                   av->type)
267
0
            >= 0;
268
0
    }
269
0
}