Coverage Report

Created: 2025-08-28 07:07

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