/src/openssl31/crypto/x509/x509_obj.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 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 | 8.82M | #define NAME_ONELINE_MAX (1024 * 1024) |
24 | | |
25 | | char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) |
26 | 201k | { |
27 | 201k | const X509_NAME_ENTRY *ne; |
28 | 201k | int i; |
29 | 201k | int n, lold, l, l1, l2, num, j, type; |
30 | 201k | int prev_set = -1; |
31 | 201k | const char *s; |
32 | 201k | char *p; |
33 | 201k | unsigned char *q; |
34 | 201k | BUF_MEM *b = NULL; |
35 | 201k | static const char hex[17] = "0123456789ABCDEF"; |
36 | 201k | int gs_doit[4]; |
37 | 201k | char tmp_buf[80]; |
38 | | #ifdef CHARSET_EBCDIC |
39 | | unsigned char ebcdic_buf[1024]; |
40 | | #endif |
41 | | |
42 | 201k | if (buf == NULL) { |
43 | 194k | if ((b = BUF_MEM_new()) == NULL) |
44 | 0 | goto err; |
45 | 194k | if (!BUF_MEM_grow(b, 200)) |
46 | 0 | goto err; |
47 | 194k | b->data[0] = '\0'; |
48 | 194k | len = 200; |
49 | 194k | } else if (len == 0) { |
50 | 0 | return NULL; |
51 | 0 | } |
52 | 201k | if (a == NULL) { |
53 | 0 | if (b) { |
54 | 0 | buf = b->data; |
55 | 0 | OPENSSL_free(b); |
56 | 0 | } |
57 | 0 | strncpy(buf, "NO X509_NAME", len); |
58 | 0 | buf[len - 1] = '\0'; |
59 | 0 | return buf; |
60 | 0 | } |
61 | | |
62 | 201k | len--; /* space for '\0' */ |
63 | 201k | l = 0; |
64 | 4.61M | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
65 | 4.41M | ne = sk_X509_NAME_ENTRY_value(a->entries, i); |
66 | 4.41M | n = OBJ_obj2nid(ne->object); |
67 | 4.41M | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
68 | 3.99M | i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object); |
69 | 3.99M | s = tmp_buf; |
70 | 3.99M | } |
71 | 4.41M | l1 = strlen(s); |
72 | | |
73 | 4.41M | type = ne->value->type; |
74 | 4.41M | num = ne->value->length; |
75 | 4.41M | if (num > NAME_ONELINE_MAX) { |
76 | 0 | ERR_raise(ERR_LIB_X509, X509_R_NAME_TOO_LONG); |
77 | 0 | goto end; |
78 | 0 | } |
79 | 4.41M | q = ne->value->data; |
80 | | #ifdef CHARSET_EBCDIC |
81 | | if (type == V_ASN1_GENERALSTRING || |
82 | | type == V_ASN1_VISIBLESTRING || |
83 | | type == V_ASN1_PRINTABLESTRING || |
84 | | type == V_ASN1_TELETEXSTRING || |
85 | | type == V_ASN1_IA5STRING) { |
86 | | if (num > (int)sizeof(ebcdic_buf)) |
87 | | num = sizeof(ebcdic_buf); |
88 | | ascii2ebcdic(ebcdic_buf, q, num); |
89 | | q = ebcdic_buf; |
90 | | } |
91 | | #endif |
92 | | |
93 | 4.41M | if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) { |
94 | 0 | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0; |
95 | 0 | for (j = 0; j < num; j++) |
96 | 0 | if (q[j] != 0) |
97 | 0 | gs_doit[j & 3] = 1; |
98 | |
|
99 | 0 | if (gs_doit[0] | gs_doit[1] | gs_doit[2]) |
100 | 0 | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; |
101 | 0 | else { |
102 | 0 | gs_doit[0] = gs_doit[1] = gs_doit[2] = 0; |
103 | 0 | gs_doit[3] = 1; |
104 | 0 | } |
105 | 0 | } else |
106 | 4.41M | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; |
107 | | |
108 | 480M | for (l2 = j = 0; j < num; j++) { |
109 | 476M | if (!gs_doit[j & 3]) |
110 | 0 | continue; |
111 | 476M | l2++; |
112 | 476M | if (q[j] == '/' || q[j] == '+') |
113 | 919k | l2++; /* char needs to be escaped */ |
114 | 475M | else if ((ossl_toascii(q[j]) < ossl_toascii(' ')) || |
115 | 475M | (ossl_toascii(q[j]) > ossl_toascii('~'))) |
116 | 333M | l2 += 3; |
117 | 476M | } |
118 | | |
119 | 4.41M | lold = l; |
120 | 4.41M | l += 1 + l1 + 1 + l2; |
121 | 4.41M | if (l > NAME_ONELINE_MAX) { |
122 | 404 | ERR_raise(ERR_LIB_X509, X509_R_NAME_TOO_LONG); |
123 | 404 | goto end; |
124 | 404 | } |
125 | 4.41M | if (b != NULL) { |
126 | 4.40M | if (!BUF_MEM_grow(b, l + 1)) |
127 | 0 | goto err; |
128 | 4.40M | p = &(b->data[lold]); |
129 | 4.40M | } else if (l > len) { |
130 | 712 | break; |
131 | 712 | } else |
132 | 5.63k | p = &(buf[lold]); |
133 | 4.40M | *(p++) = prev_set == ne->set ? '+' : '/'; |
134 | 4.40M | memcpy(p, s, (unsigned int)l1); |
135 | 4.40M | p += l1; |
136 | 4.40M | *(p++) = '='; |
137 | | |
138 | 4.40M | #ifndef CHARSET_EBCDIC /* q was assigned above already. */ |
139 | 4.40M | q = ne->value->data; |
140 | 4.40M | #endif |
141 | | |
142 | 184M | for (j = 0; j < num; j++) { |
143 | 180M | if (!gs_doit[j & 3]) |
144 | 0 | continue; |
145 | 180M | #ifndef CHARSET_EBCDIC |
146 | 180M | n = q[j]; |
147 | 180M | if ((n < ' ') || (n > '~')) { |
148 | 139M | *(p++) = '\\'; |
149 | 139M | *(p++) = 'x'; |
150 | 139M | *(p++) = hex[(n >> 4) & 0x0f]; |
151 | 139M | *(p++) = hex[n & 0x0f]; |
152 | 139M | } else { |
153 | 40.9M | if (n == '/' || n == '+') |
154 | 494k | *(p++) = '\\'; |
155 | 40.9M | *(p++) = n; |
156 | 40.9M | } |
157 | | #else |
158 | | n = os_toascii[q[j]]; |
159 | | if ((n < os_toascii[' ']) || (n > os_toascii['~'])) { |
160 | | *(p++) = '\\'; |
161 | | *(p++) = 'x'; |
162 | | *(p++) = hex[(n >> 4) & 0x0f]; |
163 | | *(p++) = hex[n & 0x0f]; |
164 | | } else { |
165 | | if (n == os_toascii['/'] || n == os_toascii['+']) |
166 | | *(p++) = '\\'; |
167 | | *(p++) = q[j]; |
168 | | } |
169 | | #endif |
170 | 180M | } |
171 | 4.40M | *p = '\0'; |
172 | 4.40M | prev_set = ne->set; |
173 | 4.40M | } |
174 | 201k | if (b != NULL) { |
175 | 194k | p = b->data; |
176 | 194k | OPENSSL_free(b); |
177 | 194k | } else |
178 | 6.65k | p = buf; |
179 | 201k | if (i == 0) |
180 | 118k | *p = '\0'; |
181 | 201k | return p; |
182 | 0 | err: |
183 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
184 | 404 | end: |
185 | 404 | BUF_MEM_free(b); |
186 | 404 | return NULL; |
187 | 0 | } |