Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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.27M
} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
33
3.27M
34
3.27M
IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
35
3.27M
IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
36
3.27M
37
3.27M
X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
38
3.27M
{
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
2.53k
{
63
2.53k
    int result = 1;
64
2.53k
    char *hexbuf;
65
66
2.53k
    if (len == 0)
67
1.07k
        return 1;
68
69
1.45k
    hexbuf = OPENSSL_buf2hexstr(buf, len);
70
1.45k
    if (hexbuf == NULL)
71
0
        return 0;
72
1.45k
    result = BIO_puts(out, hexbuf) > 0;
73
74
1.45k
    OPENSSL_free(hexbuf);
75
1.45k
    return result;
76
1.45k
}
77
78
7.83k
static int print_oid(BIO *out, const ASN1_OBJECT *oid) {
79
7.83k
    const char *ln;
80
7.83k
    char objbuf[80];
81
7.83k
    int rc;
82
83
7.83k
    if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0)
84
1
        return 0;
85
7.83k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
86
7.83k
    rc = (ln != NULL)
87
7.83k
           ? BIO_printf(out, "%s (%s)", objbuf, ln)
88
7.83k
           : BIO_printf(out, "%s", objbuf);
89
7.83k
    return (rc >= 0);
90
7.83k
}
91
92
int ossl_print_attribute_value(BIO *out,
93
                               int obj_nid,
94
                               const ASN1_TYPE *av,
95
                               int indent)
96
469k
{
97
469k
    ASN1_STRING *str;
98
469k
    unsigned char *value;
99
469k
    X509_NAME *xn = NULL;
100
469k
    int64_t int_val;
101
469k
    int ret = 1;
102
103
469k
    switch (av->type) {
104
4.36k
    case V_ASN1_BOOLEAN:
105
4.36k
        if (av->value.boolean) {
106
1.71k
            return BIO_printf(out, "%*sTRUE", indent, "") >= 4;
107
2.64k
        } else {
108
2.64k
            return BIO_printf(out, "%*sFALSE", indent, "") >= 5;
109
2.64k
        }
110
111
3.79k
    case V_ASN1_INTEGER:
112
11.5k
    case V_ASN1_ENUMERATED:
113
11.5k
        if (BIO_printf(out, "%*s", indent, "") < 0)
114
0
            return 0;
115
11.5k
        if (ASN1_ENUMERATED_get_int64(&int_val, av->value.integer) > 0) {
116
6.90k
            return BIO_printf(out, "%lld", (long long int)int_val) > 0;
117
6.90k
        }
118
4.64k
        str = av->value.integer;
119
4.64k
        return print_hex(out, str->data, str->length);
120
121
12.8k
    case V_ASN1_BIT_STRING:
122
12.8k
        if (BIO_printf(out, "%*s", indent, "") < 0)
123
0
            return 0;
124
12.8k
        return print_hex(out, av->value.bit_string->data,
125
12.8k
                         av->value.bit_string->length);
126
127
6.89k
    case V_ASN1_OCTET_STRING:
128
10.7k
    case V_ASN1_VIDEOTEXSTRING:
129
10.7k
        if (BIO_printf(out, "%*s", indent, "") < 0)
130
0
            return 0;
131
10.7k
        return print_hex(out, av->value.octet_string->data,
132
10.7k
                         av->value.octet_string->length);
133
134
703
    case V_ASN1_NULL:
135
703
        return BIO_printf(out, "%*sNULL", indent, "") >= 4;
136
137
7.83k
    case V_ASN1_OBJECT:
138
7.83k
        if (BIO_printf(out, "%*s", indent, "") < 0)
139
0
            return 0;
140
7.83k
        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
4.72k
    case V_ASN1_GENERALSTRING:
148
13.9k
    case V_ASN1_GRAPHICSTRING:
149
35.9k
    case V_ASN1_OBJECT_DESCRIPTOR:
150
35.9k
        return BIO_printf(out, "%*s%.*s", indent, "",
151
35.9k
                          av->value.generalstring->length,
152
35.9k
                          av->value.generalstring->data) >= 0;
153
154
    /* EXTERNAL would go here. */
155
    /* EMBEDDED PDV would go here. */
156
157
30.2k
    case V_ASN1_UTF8STRING:
158
30.2k
        return BIO_printf(out, "%*s%.*s", indent, "",
159
30.2k
                          av->value.utf8string->length,
160
30.2k
                          av->value.utf8string->data) >= 0;
161
162
1.90k
    case V_ASN1_REAL:
163
1.90k
        return BIO_printf(out, "%*sREAL", indent, "") >= 4;
164
165
    /* RELATIVE-OID would go here. */
166
    /* TIME would go here. */
167
168
91.9k
    case V_ASN1_SEQUENCE:
169
91.9k
        switch (obj_nid) {
170
83.7k
        case NID_undef: /* Unrecognized OID. */
171
83.7k
            break;
172
        /* Attribute types with DN syntax. */
173
128
        case NID_member:
174
267
        case NID_roleOccupant:
175
359
        case NID_seeAlso:
176
359
        case NID_manager:
177
359
        case NID_documentAuthor:
178
359
        case NID_secretary:
179
359
        case NID_associatedName:
180
359
        case NID_dITRedirect:
181
569
        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
569
            value = av->value.sequence->data;
188
569
            xn = d2i_X509_NAME(NULL,
189
569
                               (const unsigned char **)&value,
190
569
                               av->value.sequence->length);
191
569
            if (xn == NULL) {
192
52
                BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n");
193
52
                return 0;
194
52
            }
195
517
            if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0)
196
0
                ret = 0;
197
517
            X509_NAME_free(xn);
198
517
            return ret;
199
200
7.63k
        default:
201
7.63k
            break;
202
91.9k
        }
203
91.3k
        return ASN1_parse_dump(out, av->value.sequence->data,
204
91.3k
                               av->value.sequence->length, indent, 1) > 0;
205
206
84.5k
    case V_ASN1_SET:
207
84.5k
        return ASN1_parse_dump(out, av->value.set->data,
208
84.5k
                               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
2.82k
    case V_ASN1_VISIBLESTRING:
216
8.49k
    case V_ASN1_UTCTIME:
217
18.1k
    case V_ASN1_GENERALIZEDTIME:
218
33.8k
    case V_ASN1_NUMERICSTRING:
219
33.8k
        return BIO_printf(out, "%*s%.*s", indent, "",
220
33.8k
                          av->value.visiblestring->length,
221
33.8k
                          av->value.visiblestring->data) >= 0;
222
223
3.95k
    case V_ASN1_PRINTABLESTRING:
224
3.95k
        return BIO_printf(out, "%*s%.*s", indent, "",
225
3.95k
                          av->value.printablestring->length,
226
3.95k
                          av->value.printablestring->data) >= 0;
227
228
19.0k
    case V_ASN1_T61STRING:
229
19.0k
        return BIO_printf(out, "%*s%.*s", indent, "",
230
19.0k
                          av->value.t61string->length,
231
19.0k
                          av->value.t61string->data) >= 0;
232
233
6.65k
    case V_ASN1_IA5STRING:
234
6.65k
        return BIO_printf(out, "%*s%.*s", indent, "",
235
6.65k
                          av->value.ia5string->length,
236
6.65k
                          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
112k
    default:
250
112k
        return BIO_printf(out,
251
112k
                          "%*s<Unsupported tag %d>",
252
112k
                          indent,
253
112k
                          "",
254
112k
                          av->type) >= 0;
255
469k
    }
256
469k
}