• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2016 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 <string.h>
11 
12 #include <openssl/buf.h>
13 #include <openssl/err.h>
14 #include <openssl/mem.h>
15 #include <openssl/obj.h>
16 #include <openssl/x509.h>
17 
18 #include "../internal.h"
19 #include "internal.h"
20 
21 
22 // Limit to ensure we don't overflow: much greater than
23 // anything enountered in practice.
24 
25 #define NAME_ONELINE_MAX (1024 * 1024)
26 
X509_NAME_oneline(const X509_NAME * a,char * buf,int len)27 char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len) {
28   X509_NAME_ENTRY *ne;
29   size_t i;
30   int n, lold, l, l1, l2, num, j, type;
31   const char *s;
32   char *p;
33   unsigned char *q;
34   BUF_MEM *b = NULL;
35   static const char hex[17] = "0123456789ABCDEF";
36   int gs_doit[4];
37   char tmp_buf[80];
38 
39   if (buf == NULL) {
40     if ((b = BUF_MEM_new()) == NULL) {
41       goto err;
42     }
43     if (!BUF_MEM_grow(b, 200)) {
44       goto err;
45     }
46     b->data[0] = '\0';
47     len = 200;
48   } else if (len <= 0) {
49     return NULL;
50   }
51   if (a == NULL) {
52     if (b) {
53       buf = b->data;
54       OPENSSL_free(b);
55     }
56     OPENSSL_strlcpy(buf, "NO X509_NAME", len);
57     return buf;
58   }
59 
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);
70 
71     type = ne->value->type;
72     num = ne->value->length;
73     if (num > NAME_ONELINE_MAX) {
74       OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG);
75       goto err;
76     }
77     q = ne->value->data;
78 
79     if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
80       gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
81       for (j = 0; j < num; j++) {
82         if (q[j] != 0) {
83           gs_doit[j & 3] = 1;
84         }
85       }
86 
87       if (gs_doit[0] | gs_doit[1] | gs_doit[2]) {
88         gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
89       } else {
90         gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
91         gs_doit[3] = 1;
92       }
93     } else {
94       gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
95     }
96 
97     for (l2 = j = 0; j < num; j++) {
98       if (!gs_doit[j & 3]) {
99         continue;
100       }
101       l2++;
102       if ((q[j] < ' ') || (q[j] > '~')) {
103         l2 += 3;
104       }
105     }
106 
107     lold = l;
108     l += 1 + l1 + 1 + l2;
109     if (l > NAME_ONELINE_MAX) {
110       OPENSSL_PUT_ERROR(X509, X509_R_NAME_TOO_LONG);
111       goto err;
112     }
113     if (b != NULL) {
114       if (!BUF_MEM_grow(b, l + 1)) {
115         goto err;
116       }
117       p = &(b->data[lold]);
118     } else if (l > len) {
119       break;
120     } else {
121       p = &(buf[lold]);
122     }
123     *(p++) = '/';
124     OPENSSL_memcpy(p, s, (unsigned int)l1);
125     p += l1;
126     *(p++) = '=';
127 
128     q = ne->value->data;
129 
130     for (j = 0; j < num; j++) {
131       if (!gs_doit[j & 3]) {
132         continue;
133       }
134       n = q[j];
135       if ((n < ' ') || (n > '~')) {
136         *(p++) = '\\';
137         *(p++) = 'x';
138         *(p++) = hex[(n >> 4) & 0x0f];
139         *(p++) = hex[n & 0x0f];
140       } else {
141         *(p++) = n;
142       }
143     }
144     *p = '\0';
145   }
146   if (b != NULL) {
147     p = b->data;
148     OPENSSL_free(b);
149   } else {
150     p = buf;
151   }
152   if (i == 0) {
153     *p = '\0';
154   }
155   return p;
156 err:
157   BUF_MEM_free(b);
158   return NULL;
159 }
160