]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
f32b0abe | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
0f113f3e | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 BM |
12 | #include <openssl/objects.h> |
13 | #include <openssl/x509.h> | |
14 | #include <openssl/buffer.h> | |
2743e38c | 15 | #include "internal/x509_int.h" |
d02b48c6 | 16 | |
77076dc9 DSH |
17 | /* |
18 | * Limit to ensure we don't overflow: much greater than | |
9d22666e | 19 | * anything encountered in practice. |
77076dc9 DSH |
20 | */ |
21 | ||
22 | #define NAME_ONELINE_MAX (1024 * 1024) | |
23 | ||
9f5466b9 | 24 | char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) |
0f113f3e | 25 | { |
9f5466b9 | 26 | const X509_NAME_ENTRY *ne; |
0f113f3e MC |
27 | int i; |
28 | int n, lold, l, l1, l2, num, j, type; | |
29 | const char *s; | |
30 | char *p; | |
31 | unsigned char *q; | |
32 | BUF_MEM *b = NULL; | |
33 | static const char hex[17] = "0123456789ABCDEF"; | |
34 | int gs_doit[4]; | |
35 | char tmp_buf[80]; | |
a53955d8 | 36 | #ifdef CHARSET_EBCDIC |
4cd5c3f4 | 37 | unsigned char ebcdic_buf[1024]; |
a53955d8 | 38 | #endif |
d02b48c6 | 39 | |
0f113f3e MC |
40 | if (buf == NULL) { |
41 | if ((b = BUF_MEM_new()) == NULL) | |
42 | goto err; | |
43 | if (!BUF_MEM_grow(b, 200)) | |
44 | goto err; | |
45 | b->data[0] = '\0'; | |
46 | len = 200; | |
b33d1141 DSH |
47 | } else if (len == 0) { |
48 | return NULL; | |
0f113f3e MC |
49 | } |
50 | if (a == NULL) { | |
51 | if (b) { | |
52 | buf = b->data; | |
53 | OPENSSL_free(b); | |
54 | } | |
55 | strncpy(buf, "NO X509_NAME", len); | |
56 | buf[len - 1] = '\0'; | |
57 | return buf; | |
58 | } | |
d02b48c6 | 59 | |
0f113f3e MC |
60 | len--; /* space for '\0' */ |
61 | l = 0; | |
62 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | |
63 | ne = sk_X509_NAME_ENTRY_value(a->entries, i); | |
64 | n = OBJ_obj2nid(ne->object); | |
65 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { | |
66 | i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object); | |
67 | s = tmp_buf; | |
68 | } | |
69 | l1 = strlen(s); | |
d02b48c6 | 70 | |
0f113f3e MC |
71 | type = ne->value->type; |
72 | num = ne->value->length; | |
77076dc9 DSH |
73 | if (num > NAME_ONELINE_MAX) { |
74 | X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG); | |
75 | goto end; | |
76 | } | |
0f113f3e | 77 | q = ne->value->data; |
a53955d8 | 78 | #ifdef CHARSET_EBCDIC |
0f113f3e MC |
79 | if (type == V_ASN1_GENERALSTRING || |
80 | type == V_ASN1_VISIBLESTRING || | |
81 | type == V_ASN1_PRINTABLESTRING || | |
82 | type == V_ASN1_TELETEXSTRING || | |
2647e261 | 83 | type == V_ASN1_IA5STRING) { |
ea96ad5a MC |
84 | if (num > (int)sizeof(ebcdic_buf)) |
85 | num = sizeof(ebcdic_buf); | |
86 | ascii2ebcdic(ebcdic_buf, q, num); | |
0f113f3e MC |
87 | q = ebcdic_buf; |
88 | } | |
a53955d8 | 89 | #endif |
d02b48c6 | 90 | |
0f113f3e MC |
91 | if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) { |
92 | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0; | |
93 | for (j = 0; j < num; j++) | |
94 | if (q[j] != 0) | |
95 | gs_doit[j & 3] = 1; | |
d02b48c6 | 96 | |
0f113f3e MC |
97 | if (gs_doit[0] | gs_doit[1] | gs_doit[2]) |
98 | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; | |
99 | else { | |
100 | gs_doit[0] = gs_doit[1] = gs_doit[2] = 0; | |
101 | gs_doit[3] = 1; | |
102 | } | |
103 | } else | |
104 | gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; | |
d02b48c6 | 105 | |
0f113f3e MC |
106 | for (l2 = j = 0; j < num; j++) { |
107 | if (!gs_doit[j & 3]) | |
108 | continue; | |
109 | l2++; | |
a53955d8 | 110 | #ifndef CHARSET_EBCDIC |
0f113f3e MC |
111 | if ((q[j] < ' ') || (q[j] > '~')) |
112 | l2 += 3; | |
a53955d8 | 113 | #else |
0f113f3e MC |
114 | if ((os_toascii[q[j]] < os_toascii[' ']) || |
115 | (os_toascii[q[j]] > os_toascii['~'])) | |
116 | l2 += 3; | |
a53955d8 | 117 | #endif |
0f113f3e | 118 | } |
d02b48c6 | 119 | |
0f113f3e MC |
120 | lold = l; |
121 | l += 1 + l1 + 1 + l2; | |
77076dc9 DSH |
122 | if (l > NAME_ONELINE_MAX) { |
123 | X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG); | |
124 | goto end; | |
125 | } | |
0f113f3e MC |
126 | if (b != NULL) { |
127 | if (!BUF_MEM_grow(b, l + 1)) | |
128 | goto err; | |
129 | p = &(b->data[lold]); | |
130 | } else if (l > len) { | |
131 | break; | |
132 | } else | |
133 | p = &(buf[lold]); | |
134 | *(p++) = '/'; | |
135 | memcpy(p, s, (unsigned int)l1); | |
136 | p += l1; | |
137 | *(p++) = '='; | |
d02b48c6 | 138 | |
0f113f3e MC |
139 | #ifndef CHARSET_EBCDIC /* q was assigned above already. */ |
140 | q = ne->value->data; | |
a53955d8 | 141 | #endif |
d02b48c6 | 142 | |
0f113f3e MC |
143 | for (j = 0; j < num; j++) { |
144 | if (!gs_doit[j & 3]) | |
145 | continue; | |
a53955d8 | 146 | #ifndef CHARSET_EBCDIC |
0f113f3e MC |
147 | n = q[j]; |
148 | if ((n < ' ') || (n > '~')) { | |
149 | *(p++) = '\\'; | |
150 | *(p++) = 'x'; | |
151 | *(p++) = hex[(n >> 4) & 0x0f]; | |
152 | *(p++) = hex[n & 0x0f]; | |
153 | } else | |
154 | *(p++) = n; | |
a53955d8 | 155 | #else |
0f113f3e MC |
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]; | |
a53955d8 | 164 | #endif |
0f113f3e MC |
165 | } |
166 | *p = '\0'; | |
167 | } | |
168 | if (b != NULL) { | |
169 | p = b->data; | |
170 | OPENSSL_free(b); | |
171 | } else | |
172 | p = buf; | |
173 | if (i == 0) | |
174 | *p = '\0'; | |
26a7d938 | 175 | return p; |
0f113f3e MC |
176 | err: |
177 | X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE); | |
77076dc9 | 178 | end: |
25aaa98a | 179 | BUF_MEM_free(b); |
26a7d938 | 180 | return NULL; |
0f113f3e | 181 | } |