Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/crypto/x509/x509_obj.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/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.43M
#define NAME_ONELINE_MAX    (1024 * 1024)
24
25
char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
26
288k
{
27
288k
    const X509_NAME_ENTRY *ne;
28
288k
    int i;
29
288k
    int n, lold, l, l1, l2, num, j, type;
30
288k
    int prev_set = -1;
31
288k
    const char *s;
32
288k
    char *p;
33
288k
    unsigned char *q;
34
288k
    BUF_MEM *b = NULL;
35
288k
    int gs_doit[4];
36
288k
    char tmp_buf[80];
37
#ifdef CHARSET_EBCDIC
38
    unsigned char ebcdic_buf[1024];
39
#endif
40
41
288k
    if (buf == NULL) {
42
281k
        if ((b = BUF_MEM_new()) == NULL)
43
0
            goto buferr;
44
281k
        if (!BUF_MEM_grow(b, 200))
45
0
            goto buferr;
46
281k
        b->data[0] = '\0';
47
281k
        len = 200;
48
281k
    } else if (len == 0) {
49
0
        return NULL;
50
0
    }
51
288k
    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
288k
    len--;                      /* space for '\0' */
62
288k
    l = 0;
63
4.00M
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
64
3.71M
        ne = sk_X509_NAME_ENTRY_value(a->entries, i);
65
3.71M
        n = OBJ_obj2nid(ne->object);
66
3.71M
        if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
67
2.94M
            i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
68
2.94M
            s = tmp_buf;
69
2.94M
        }
70
3.71M
        l1 = strlen(s);
71
72
3.71M
        type = ne->value->type;
73
3.71M
        num = ne->value->length;
74
3.71M
        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.71M
        q = ne->value->data;
79
#ifdef CHARSET_EBCDIC
80
        if (type == V_ASN1_GENERALSTRING ||
81
            type == V_ASN1_VISIBLESTRING ||
82
            type == V_ASN1_PRINTABLESTRING ||
83
            type == V_ASN1_TELETEXSTRING ||
84
            type == V_ASN1_IA5STRING) {
85
            if (num > (int)sizeof(ebcdic_buf))
86
                num = sizeof(ebcdic_buf);
87
            ascii2ebcdic(ebcdic_buf, q, num);
88
            q = ebcdic_buf;
89
        }
90
#endif
91
92
3.71M
        if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
93
0
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
94
0
            for (j = 0; j < num; j++)
95
0
                if (q[j] != 0)
96
0
                    gs_doit[j & 3] = 1;
97
98
0
            if (gs_doit[0] | gs_doit[1] | gs_doit[2])
99
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
100
0
            else {
101
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
102
0
                gs_doit[3] = 1;
103
0
            }
104
0
        } else
105
3.71M
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
106
107
503M
        for (l2 = j = 0; j < num; j++) {
108
499M
            if (!gs_doit[j & 3])
109
0
                continue;
110
499M
            l2++;
111
499M
            if (q[j] == '/' || q[j] == '+')
112
2.27M
                l2++; /* char needs to be escaped */
113
497M
            else if ((ossl_toascii(q[j]) < ossl_toascii(' ')) ||
114
497M
                     (ossl_toascii(q[j]) > ossl_toascii('~')))
115
336M
                l2 += 3;
116
499M
        }
117
118
3.71M
        lold = l;
119
3.71M
        l += 1 + l1 + 1 + l2;
120
3.71M
        if (l > NAME_ONELINE_MAX) {
121
412
            ERR_raise(ERR_LIB_X509, X509_R_NAME_TOO_LONG);
122
412
            goto end;
123
412
        }
124
3.71M
        if (b != NULL) {
125
3.71M
            if (!BUF_MEM_grow(b, l + 1))
126
0
                goto buferr;
127
3.71M
            p = &(b->data[lold]);
128
3.71M
        } else if (l > len) {
129
744
            break;
130
744
        } else
131
5.07k
            p = &(buf[lold]);
132
3.71M
        *(p++) = prev_set == ne->set ? '+' : '/';
133
3.71M
        memcpy(p, s, (unsigned int)l1);
134
3.71M
        p += l1;
135
3.71M
        *(p++) = '=';
136
137
3.71M
#ifndef CHARSET_EBCDIC          /* q was assigned above already. */
138
3.71M
        q = ne->value->data;
139
3.71M
#endif
140
141
217M
        for (j = 0; j < num; j++) {
142
213M
            if (!gs_doit[j & 3])
143
0
                continue;
144
213M
#ifndef CHARSET_EBCDIC
145
213M
            n = q[j];
146
213M
            if ((n < ' ') || (n > '~')) {
147
148M
                *(p++) = '\\';
148
148M
                *(p++) = 'x';
149
148M
                p += ossl_to_hex(p, n);
150
148M
            } else {
151
64.8M
                if (n == '/' || n == '+')
152
1.10M
                    *(p++) = '\\';
153
64.8M
                *(p++) = n;
154
64.8M
            }
155
#else
156
            n = os_toascii[q[j]];
157
            if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
158
                *(p++) = '\\';
159
                *(p++) = 'x';
160
                p += ossl_to_hex(p, n);
161
            } else {
162
                if (n == os_toascii['/'] || n == os_toascii['+'])
163
                    *(p++) = '\\';
164
                *(p++) = q[j];
165
            }
166
#endif
167
213M
        }
168
3.71M
        *p = '\0';
169
3.71M
        prev_set = ne->set;
170
3.71M
    }
171
287k
    if (b != NULL) {
172
281k
        p = b->data;
173
281k
        OPENSSL_free(b);
174
281k
    } else
175
6.16k
        p = buf;
176
287k
    if (i == 0)
177
135k
        *p = '\0';
178
287k
    return p;
179
0
 buferr:
180
0
    ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
181
412
 end:
182
412
    BUF_MEM_free(b);
183
412
    return NULL;
184
0
}