Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/x509/x509_obj.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 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/x509.h>
14
#include <openssl/buffer.h>
15
#include "crypto/x509.h"
16
#include "crypto/ctype.h"
17
18
/*
19
 * Limit to ensure we don't overflow: much greater than
20
 * anything encountered in practice.
21
 */
22
23
7.85M
#define NAME_ONELINE_MAX (1024 * 1024)
24
25
char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
26
318k
{
27
318k
    const X509_NAME_ENTRY *ne;
28
318k
    int i;
29
318k
    int n, lold, l, l1, l2, num, j, type;
30
318k
    int prev_set = -1;
31
318k
    const char *s;
32
318k
    char *p;
33
318k
    unsigned char *q;
34
318k
    BUF_MEM *b = NULL;
35
318k
    int gs_doit[4];
36
318k
    char tmp_buf[80];
37
#ifdef CHARSET_EBCDIC
38
    unsigned char ebcdic_buf[1024];
39
#endif
40
41
318k
    if (buf == NULL) {
42
308k
        if ((b = BUF_MEM_new()) == NULL)
43
0
            goto buferr;
44
308k
        if (!BUF_MEM_grow(b, 200))
45
0
            goto buferr;
46
308k
        b->data[0] = '\0';
47
308k
        len = 200;
48
308k
    } else if (len == 0) {
49
0
        return NULL;
50
0
    }
51
318k
    if (a == NULL) {
52
0
        if (b) {
53
0
            buf = b->data;
54
0
            OPENSSL_free(b);
55
0
        }
56
0
        strncpy(buf, "NO X509_NAME", len);
57
0
        buf[len - 1] = '\0';
58
0
        return buf;
59
0
    }
60
61
318k
    len--; /* space for '\0' */
62
318k
    l = 0;
63
4.24M
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
64
3.92M
        ne = sk_X509_NAME_ENTRY_value(a->entries, i);
65
3.92M
        n = OBJ_obj2nid(ne->object);
66
3.92M
        if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
67
3.14M
            i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
68
3.14M
            s = tmp_buf;
69
3.14M
        }
70
3.92M
        l1 = (int)strlen(s);
71
72
3.92M
        type = ne->value->type;
73
3.92M
        num = ne->value->length;
74
3.92M
        if (num > NAME_ONELINE_MAX) {
75
0
            ERR_raise(ERR_LIB_X509, X509_R_NAME_TOO_LONG);
76
0
            goto end;
77
0
        }
78
3.92M
        q = ne->value->data;
79
#ifdef CHARSET_EBCDIC
80
        if (type == V_ASN1_GENERALSTRING || type == V_ASN1_VISIBLESTRING || type == V_ASN1_PRINTABLESTRING || type == V_ASN1_TELETEXSTRING || type == V_ASN1_IA5STRING) {
81
            if (num > (int)sizeof(ebcdic_buf))
82
                num = sizeof(ebcdic_buf);
83
            ascii2ebcdic(ebcdic_buf, q, num);
84
            q = ebcdic_buf;
85
        }
86
#endif
87
88
3.92M
        if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
89
0
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
90
0
            for (j = 0; j < num; j++)
91
0
                if (q[j] != 0)
92
0
                    gs_doit[j & 3] = 1;
93
94
0
            if (gs_doit[0] | gs_doit[1] | gs_doit[2])
95
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
96
0
            else {
97
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
98
0
                gs_doit[3] = 1;
99
0
            }
100
0
        } else
101
3.92M
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
102
103
760M
        for (l2 = j = 0; j < num; j++) {
104
756M
            if (!gs_doit[j & 3])
105
0
                continue;
106
756M
            l2++;
107
756M
            if (q[j] == '/' || q[j] == '+')
108
2.67M
                l2++; /* char needs to be escaped */
109
753M
            else if ((ossl_toascii(q[j]) < ossl_toascii(' ')) || (ossl_toascii(q[j]) > ossl_toascii('~')))
110
547M
                l2 += 3;
111
756M
        }
112
113
3.92M
        lold = l;
114
3.92M
        l += 1 + l1 + 1 + l2;
115
3.92M
        if (l > NAME_ONELINE_MAX) {
116
637
            ERR_raise(ERR_LIB_X509, X509_R_NAME_TOO_LONG);
117
637
            goto end;
118
637
        }
119
3.92M
        if (b != NULL) {
120
3.91M
            if (!BUF_MEM_grow(b, l + 1))
121
0
                goto buferr;
122
3.91M
            p = &(b->data[lold]);
123
3.91M
        } else if (l > len) {
124
1.69k
            break;
125
1.69k
        } else
126
9.86k
            p = &(buf[lold]);
127
3.92M
        *(p++) = prev_set == ne->set ? '+' : '/';
128
3.92M
        memcpy(p, s, (unsigned int)l1);
129
3.92M
        p += l1;
130
3.92M
        *(p++) = '=';
131
132
3.92M
#ifndef CHARSET_EBCDIC /* q was assigned above already. */
133
3.92M
        q = ne->value->data;
134
3.92M
#endif
135
136
320M
        for (j = 0; j < num; j++) {
137
316M
            if (!gs_doit[j & 3])
138
0
                continue;
139
316M
#ifndef CHARSET_EBCDIC
140
316M
            n = q[j];
141
316M
            if ((n < ' ') || (n > '~')) {
142
231M
                *(p++) = '\\';
143
231M
                *(p++) = 'x';
144
231M
                p += ossl_to_hex(p, n);
145
231M
            } else {
146
85.4M
                if (n == '/' || n == '+')
147
1.59M
                    *(p++) = '\\';
148
85.4M
                *(p++) = n;
149
85.4M
            }
150
#else
151
            n = os_toascii[q[j]];
152
            if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
153
                *(p++) = '\\';
154
                *(p++) = 'x';
155
                p += ossl_to_hex(p, n);
156
            } else {
157
                if (n == os_toascii['/'] || n == os_toascii['+'])
158
                    *(p++) = '\\';
159
                *(p++) = q[j];
160
            }
161
#endif
162
316M
        }
163
3.92M
        *p = '\0';
164
3.92M
        prev_set = ne->set;
165
3.92M
    }
166
317k
    if (b != NULL) {
167
308k
        p = b->data;
168
308k
        OPENSSL_free(b);
169
308k
    } else
170
9.69k
        p = buf;
171
317k
    if (i == 0)
172
150k
        *p = '\0';
173
317k
    return p;
174
0
buferr:
175
0
    ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
176
637
end:
177
637
    BUF_MEM_free(b);
178
    return NULL;
179
0
}