Coverage Report

Created: 2023-09-25 06:41

/src/openssl111/crypto/x509/x509_obj.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
17
/*
18
 * Limit to ensure we don't overflow: much greater than
19
 * anything encountered in practice.
20
 */
21
22
831k
#define NAME_ONELINE_MAX    (1024 * 1024)
23
24
char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
25
7.13k
{
26
7.13k
    const X509_NAME_ENTRY *ne;
27
7.13k
    int i;
28
7.13k
    int n, lold, l, l1, l2, num, j, type;
29
7.13k
    const char *s;
30
7.13k
    char *p;
31
7.13k
    unsigned char *q;
32
7.13k
    BUF_MEM *b = NULL;
33
7.13k
    static const char hex[17] = "0123456789ABCDEF";
34
7.13k
    int gs_doit[4];
35
7.13k
    char tmp_buf[80];
36
#ifdef CHARSET_EBCDIC
37
    unsigned char ebcdic_buf[1024];
38
#endif
39
40
7.13k
    if (buf == NULL) {
41
6.64k
        if ((b = BUF_MEM_new()) == NULL)
42
0
            goto err;
43
6.64k
        if (!BUF_MEM_grow(b, 200))
44
0
            goto err;
45
6.64k
        b->data[0] = '\0';
46
6.64k
        len = 200;
47
6.64k
    } else if (len == 0) {
48
0
        return NULL;
49
0
    }
50
7.13k
    if (a == NULL) {
51
0
        if (b) {
52
0
            buf = b->data;
53
0
            OPENSSL_free(b);
54
0
        }
55
0
        strncpy(buf, "NO X509_NAME", len);
56
0
        buf[len - 1] = '\0';
57
0
        return buf;
58
0
    }
59
60
7.13k
    len--;                      /* space for '\0' */
61
7.13k
    l = 0;
62
422k
    for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
63
415k
        ne = sk_X509_NAME_ENTRY_value(a->entries, i);
64
415k
        n = OBJ_obj2nid(ne->object);
65
415k
        if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
66
398k
            i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
67
398k
            s = tmp_buf;
68
398k
        }
69
415k
        l1 = strlen(s);
70
71
415k
        type = ne->value->type;
72
415k
        num = ne->value->length;
73
415k
        if (num > NAME_ONELINE_MAX) {
74
0
            X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
75
0
            goto end;
76
0
        }
77
415k
        q = ne->value->data;
78
#ifdef CHARSET_EBCDIC
79
        if (type == V_ASN1_GENERALSTRING ||
80
            type == V_ASN1_VISIBLESTRING ||
81
            type == V_ASN1_PRINTABLESTRING ||
82
            type == V_ASN1_TELETEXSTRING ||
83
            type == V_ASN1_IA5STRING) {
84
            if (num > (int)sizeof(ebcdic_buf))
85
                num = sizeof(ebcdic_buf);
86
            ascii2ebcdic(ebcdic_buf, q, num);
87
            q = ebcdic_buf;
88
        }
89
#endif
90
91
415k
        if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
92
0
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
93
0
            for (j = 0; j < num; j++)
94
0
                if (q[j] != 0)
95
0
                    gs_doit[j & 3] = 1;
96
97
0
            if (gs_doit[0] | gs_doit[1] | gs_doit[2])
98
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
99
0
            else {
100
0
                gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
101
0
                gs_doit[3] = 1;
102
0
            }
103
0
        } else
104
415k
            gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
105
106
44.1M
        for (l2 = j = 0; j < num; j++) {
107
43.6M
            if (!gs_doit[j & 3])
108
0
                continue;
109
43.6M
            l2++;
110
43.6M
#ifndef CHARSET_EBCDIC
111
43.6M
            if ((q[j] < ' ') || (q[j] > '~'))
112
15.7M
                l2 += 3;
113
#else
114
            if ((os_toascii[q[j]] < os_toascii[' ']) ||
115
                (os_toascii[q[j]] > os_toascii['~']))
116
                l2 += 3;
117
#endif
118
43.6M
        }
119
120
415k
        lold = l;
121
415k
        l += 1 + l1 + 1 + l2;
122
415k
        if (l > NAME_ONELINE_MAX) {
123
22
            X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
124
22
            goto end;
125
22
        }
126
415k
        if (b != NULL) {
127
415k
            if (!BUF_MEM_grow(b, l + 1))
128
0
                goto err;
129
415k
            p = &(b->data[lold]);
130
415k
        } else if (l > len) {
131
7
            break;
132
7
        } else
133
274
            p = &(buf[lold]);
134
415k
        *(p++) = '/';
135
415k
        memcpy(p, s, (unsigned int)l1);
136
415k
        p += l1;
137
415k
        *(p++) = '=';
138
139
415k
#ifndef CHARSET_EBCDIC          /* q was assigned above already. */
140
415k
        q = ne->value->data;
141
415k
#endif
142
143
27.2M
        for (j = 0; j < num; j++) {
144
26.8M
            if (!gs_doit[j & 3])
145
0
                continue;
146
26.8M
#ifndef CHARSET_EBCDIC
147
26.8M
            n = q[j];
148
26.8M
            if ((n < ' ') || (n > '~')) {
149
12.5M
                *(p++) = '\\';
150
12.5M
                *(p++) = 'x';
151
12.5M
                *(p++) = hex[(n >> 4) & 0x0f];
152
12.5M
                *(p++) = hex[n & 0x0f];
153
12.5M
            } else
154
14.2M
                *(p++) = n;
155
#else
156
            n = os_toascii[q[j]];
157
            if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
158
                *(p++) = '\\';
159
                *(p++) = 'x';
160
                *(p++) = hex[(n >> 4) & 0x0f];
161
                *(p++) = hex[n & 0x0f];
162
            } else
163
                *(p++) = q[j];
164
#endif
165
26.8M
        }
166
415k
        *p = '\0';
167
415k
    }
168
7.11k
    if (b != NULL) {
169
6.62k
        p = b->data;
170
6.62k
        OPENSSL_free(b);
171
6.62k
    } else
172
492
        p = buf;
173
7.11k
    if (i == 0)
174
6.07k
        *p = '\0';
175
7.11k
    return p;
176
0
 err:
177
0
    X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
178
22
 end:
179
22
    BUF_MEM_free(b);
180
22
    return NULL;
181
0
}