1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 
17 #include <openssl/buf.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20 #include <openssl/obj.h>
21 #include <openssl/x509.h>
22 
23 #include "../internal.h"
24 #include "internal.h"
25 
26 
27 // Limit to ensure we don't overflow: much greater than
28 // anything enountered in practice.
29 
30 #define NAME_ONELINE_MAX (1024 * 1024)
31 
X509_NAME_oneline(const X509_NAME * a,char * buf,int len)32 char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) {
33   X509_NAME_ENTRY *ne;
34   size_t i;
35   int n, lold, l, l1, l2, num, j, type;
36   const char *s;
37   char *p;
38   unsigned char *q;
39   BUF_MEM *b = NULL;
40   static const char hex[17] = "0123456789ABCDEF";
41   int gs_doit[4];
42   char tmp_buf[80];
43 
44   if (buf == NULL) {
45     if ((b = BUF_MEM_new()) == NULL) {
46       goto err;
47     }
48     if (!BUF_MEM_grow(b, 200)) {
49       goto err;
50     }
51     b->data[0] = '\0';
52     len = 200;
53   } else if (len <= 0) {
54     return NULL;
55   }
56   if (a == NULL) {
57     if (b) {
58       buf = b->data;
59       OPENSSL_free(b);
60     }
61     OPENSSL_strlcpy(buf, "NO X509_NAME", len);
62     return buf;
63   }
64 
65   len--;  // space for '\0'
66   l = 0;
67   for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
68     ne = sk_X509_NAME_ENTRY_value(a->entries, i);
69     n = OBJ_obj2nid(ne->object);
70     if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
71       i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
72       s = tmp_buf;
73     }
74     l1 = strlen(s);
75 
76     type = ne->value->type;
77     num = ne->value->length;
78     if (num > NAME_ONELINE_MAX) {
79       OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG);
80       goto err;
81     }
82     q = ne->value->data;
83 
84     if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
85       gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
86       for (j = 0; j < num; j++) {
87         if (q[j] != 0) {
88           gs_doit[j & 3] = 1;
89         }
90       }
91 
92       if (gs_doit[0] | gs_doit[1] | gs_doit[2]) {
93         gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
94       } else {
95         gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
96         gs_doit[3] = 1;
97       }
98     } else {
99       gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
100     }
101 
102     for (l2 = j = 0; j < num; j++) {
103       if (!gs_doit[j & 3]) {
104         continue;
105       }
106       l2++;
107       if ((q[j] < ' ') || (q[j] > '~')) {
108         l2 += 3;
109       }
110     }
111 
112     lold = l;
113     l += 1 + l1 + 1 + l2;
114     if (l > NAME_ONELINE_MAX) {
115       OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG);
116       goto err;
117     }
118     if (b != NULL) {
119       if (!BUF_MEM_grow(b, l + 1)) {
120         goto err;
121       }
122       p = &(b->data[lold]);
123     } else if (l > len) {
124       break;
125     } else {
126       p = &(buf[lold]);
127     }
128     *(p++) = '/';
129     OPENSSL_memcpy(p, s, (unsigned int)l1);
130     p += l1;
131     *(p++) = '=';
132 
133     q = ne->value->data;
134 
135     for (j = 0; j < num; j++) {
136       if (!gs_doit[j & 3]) {
137         continue;
138       }
139       n = q[j];
140       if ((n < ' ') || (n > '~')) {
141         *(p++) = '\\';
142         *(p++) = 'x';
143         *(p++) = hex[(n >> 4) & 0x0f];
144         *(p++) = hex[n & 0x0f];
145       } else {
146         *(p++) = n;
147       }
148     }
149     *p = '\0';
150   }
151   if (b != NULL) {
152     p = b->data;
153     OPENSSL_free(b);
154   } else {
155     p = buf;
156   }
157   if (i == 0) {
158     *p = '\0';
159   }
160   return p;
161 err:
162   BUF_MEM_free(b);
163   return NULL;
164 }
165